(
chatId: string,
messages: PersistedMessage[],
options?: { chatModel?: string | null; streamId?: string | null },
executor: DbOrTx = db
)
| 50 | * and the `seq, created_at, id` read order breaks any residual tie. |
| 51 | */ |
| 52 | export 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). |
no test coverage detected