MCPcopy
hub / github.com/simstudioai/sim / appendCopilotChatMessages

Function appendCopilotChatMessages

apps/sim/lib/copilot/chat/messages-store.ts:52–79  ·  view source on GitHub ↗
(
  chatId: string,
  messages: PersistedMessage[],
  options?: { chatModel?: string | null; streamId?: string | null },
  executor: DbOrTx = db
)

Source from the content-addressed store, hash-verified

50 * and the `seq, created_at, id` read order breaks any residual tie.
51 */
52export async function appendCopilotChatMessages(
53 chatId: string,
54 messages: PersistedMessage[],
55 options?: { chatModel?: string | null; streamId?: string | null },
56 executor: DbOrTx = db
57): Promise<void> {
58 if (messages.length === 0) return
59 const deduped = dedupeById(messages)
60 const [maxRow] = await executor
61 .select({ maxSeq: sql<number | null>`max(${copilotMessages.seq})` })
62 .from(copilotMessages)
63 .where(eq(copilotMessages.chatId, chatId))
64 const base = (maxRow?.maxSeq ?? -1) + 1
65 await executor
66 .insert(copilotMessages)
67 .values(deduped.map((m, i) => toRow(chatId, m, base + i, options)))
68 .onConflictDoUpdate({
69 target: [copilotMessages.chatId, copilotMessages.messageId],
70 set: {
71 content: sql`excluded.content`,
72 role: sql`excluded.role`,
73 model: sql`COALESCE(excluded.model, ${copilotMessages.model})`,
74 streamId: sql`COALESCE(excluded.stream_id, ${copilotMessages.streamId})`,
75 seq: sql`COALESCE(${copilotMessages.seq}, excluded.seq)`,
76 updatedAt: sql`now()`,
77 },
78 })
79}
80
81/**
82 * Replace all messages for a chat from a full snapshot (used by update-messages).

Callers 6

persistChatMessagesFunction · 0.90
persistUserMessageFunction · 0.90
finalizeAssistantTurnFunction · 0.90
route.tsFile · 0.90
route.tsFile · 0.90

Calls 3

dedupeByIdFunction · 0.85
toRowFunction · 0.85
eqFunction · 0.50

Tested by

no test coverage detected