Skip to content

Commit a225c36

Browse files
committed
fix(sdk): include the recovered partial in the error event's newMessages
The error-path onTurnComplete added the partial to newUIMessages/uiMessages but left newMessages (the model-message delta) empty, so apps persisting the model delta missed the partial. Populate it from the same conversion.
1 parent b6ea02f commit a225c36

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

packages/trigger-sdk/src/v3/ai.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7960,6 +7960,8 @@ function chatAgent<
79607960
erroredNewUIMessages.push(partialResponse);
79617961
}
79627962

7963+
let erroredNewModelMessages: ModelMessage[] = [];
7964+
79637965
// Commit the complete error state to the canonical accumulator so the
79647966
// errored user message and any recovered partial survive past this
79657967
// hook: the run stays alive after an error, so the next turn sees
@@ -7978,10 +7980,13 @@ function chatAgent<
79787980
accumulatedUIMessages = erroredUIMessagesWithPartial;
79797981
locals.set(chatCurrentUIMessagesKey, accumulatedUIMessages);
79807982
try {
7983+
if (partialResponse) {
7984+
erroredNewModelMessages = await toModelMessages([
7985+
stripProviderMetadata(partialResponse),
7986+
]);
7987+
}
79817988
if (onlyAppendedPartial) {
7982-
accumulatedMessages.push(
7983-
...(await toModelMessages([stripProviderMetadata(partialResponse!)]))
7984-
);
7989+
accumulatedMessages.push(...erroredNewModelMessages);
79857990
} else {
79867991
accumulatedMessages = await toModelMessages(accumulatedUIMessages);
79877992
}
@@ -8004,7 +8009,7 @@ function chatAgent<
80048009
chatId: currentWirePayload.chatId,
80058010
messages: accumulatedMessages,
80068011
uiMessages: erroredUIMessagesWithPartial,
8007-
newMessages: [],
8012+
newMessages: erroredNewModelMessages,
80088013
newUIMessages: erroredNewUIMessages,
80098014
responseMessage: partialResponse,
80108015
rawResponseMessage: partialResponse,

packages/trigger-sdk/test/chat-agent-source-stream-error.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ describe("chat.agent managed loop — source-stream failure", () => {
9595
// dropped (responseMessage: undefined).
9696
expect(evt.responseMessage).toBeDefined();
9797
expect(extractText(evt.responseMessage)).toBe("partial answer");
98+
99+
const newAssistantText = (evt.newMessages as ModelMessage[])
100+
.filter((m) => m.role === "assistant")
101+
.map((m) =>
102+
typeof m.content === "string"
103+
? m.content
104+
: (m.content as Array<{ type: string; text?: string }>)
105+
.filter((p) => p.type === "text")
106+
.map((p) => p.text ?? "")
107+
.join("")
108+
)
109+
.join("");
110+
expect(newAssistantText).toBe("partial answer");
98111
} finally {
99112
await harness.close();
100113
}

0 commit comments

Comments
 (0)