Skip to content

Commit fef74f4

Browse files
committed
fix(references): persist chat.history mutations in aiChatHydrated.onAction
The hydrate-mode task uses `hydrateMessages` to read the full message chain from Postgres on every turn, so any `chat.history.*` mutation inside `onAction` (undo / rollback / remove / replace) was lost the moment the next turn started — the in-memory accumulator was overwritten by a fresh DB read. Write the mutated chain through with `prisma.chat.update` at the end of `onAction` so next turn's hydrate sees the pruned/edited state.
1 parent 42f8d64 commit fef74f4

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

  • references/ai-chat/src/trigger

references/ai-chat/src/trigger/chat.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ export const aiChatHydrated = chat
890890
}),
891891
]),
892892

893-
onAction: async ({ action }) => {
893+
onAction: async ({ action, chatId }) => {
894894
switch (action.type) {
895895
case "undo":
896896
// Remove the last user message + assistant response
@@ -912,6 +912,18 @@ export const aiChatHydrated = chat
912912
});
913913
break;
914914
}
915+
// Hydrate-mode task: `chat.history.*` mutations live in the
916+
// in-memory accumulator for this turn only. The NEXT turn's
917+
// `hydrateMessages` reads from Postgres, so any action that
918+
// mutates history must also be persisted back to the DB or it'll
919+
// be overwritten on the next message. Write the mutated chain
920+
// through here.
921+
await prisma.chat.update({
922+
where: { id: chatId },
923+
data: {
924+
messages: chat.history.all() as unknown as ChatMessagesForWrite,
925+
},
926+
});
915927
},
916928

917929
onChatStart: async ({ chatId, runId, chatAccessToken, clientData, preloaded }) => {

0 commit comments

Comments
 (0)