MCPcopy Index your code
hub / github.com/simstudioai/sim / processPastChatFromDb

Function processPastChatFromDb

apps/sim/lib/copilot/chat/process-contents.ts:284–335  ·  view source on GitHub ↗
(
  chatId: string,
  userId: string,
  tag: string,
  currentWorkspaceId?: string
)

Source from the content-addressed store, hash-verified

282}
283
284async function processPastChatFromDb(
285 chatId: string,
286 userId: string,
287 tag: string,
288 currentWorkspaceId?: string
289): Promise<AgentContext | null> {
290 try {
291 const { getAccessibleCopilotChatWithMessages } = await import('./lifecycle')
292 const chat = await getAccessibleCopilotChatWithMessages(chatId, userId)
293 if (!chat) {
294 return null
295 }
296
297 if (currentWorkspaceId) {
298 if (chat.workspaceId && chat.workspaceId !== currentWorkspaceId) {
299 return null
300 }
301 if (chat.workflowId) {
302 const activeWorkflow = await getActiveWorkflowRecord(chat.workflowId)
303 if (!activeWorkflow || activeWorkflow.workspaceId !== currentWorkspaceId) {
304 return null
305 }
306 }
307 }
308 const messages = Array.isArray(chat.messages) ? (chat as any).messages : []
309 const content = messages
310 .map((m: any) => {
311 const role = m.role || 'user'
312 let text = ''
313 if (Array.isArray(m.contentBlocks) && m.contentBlocks.length > 0) {
314 text = m.contentBlocks
315 .filter((b: any) => b?.type === 'text')
316 .map((b: any) => String(b.content || ''))
317 .join('')
318 .trim()
319 }
320 if (!text && typeof m.content === 'string') text = m.content
321 return `${role}: ${text}`.trim()
322 })
323 .filter((s: string) => s.length > 0)
324 .join('\n')
325 logger.info('Processed past_chat context from DB', {
326 chatId,
327 length: content.length,
328 lines: content ? content.split('\n').length : 0,
329 })
330 return { type: 'past_chat', tag, content }
331 } catch (error) {
332 logger.error('Error processing past chat from db', { chatId, error })
333 return null
334 }
335}
336
337/**
338 * Resolve a workflow folder id to its canonical, per-segment-encoded VFS folder

Callers 1

processContextsServerFunction · 0.85

Calls 5

getActiveWorkflowRecordFunction · 0.90
joinMethod · 0.80
infoMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected