Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion apps/sim/lib/copilot/chat/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,19 @@ async function persistUserMessage(params: {
contexts,
})

// Append the user message idempotently: only concatenate when the array
// doesn't already contain a message with this id. A repeated id here is
// always a retry/double-submit of the same message (the client mints a
// fresh id per distinct send), so skipping the append avoids duplicate
// entries while keeping the stream marker / updatedAt current.
const [updated] = await db
.update(copilotChats)
.set({
messages: sql`${copilotChats.messages} || ${JSON.stringify([userMsg])}::jsonb`,
messages: sql`CASE
WHEN ${copilotChats.messages} @> ${JSON.stringify([{ id: userMessageId }])}::jsonb
THEN ${copilotChats.messages}
ELSE ${copilotChats.messages} || ${JSON.stringify([userMsg])}::jsonb
END`,
conversationId: userMessageId,
updatedAt: new Date(),
})
Expand Down
Loading