Skip to content

Commit 1916507

Browse files
committed
fix(sdk): preserve compaction and avoid double-persist on the error path
When committing the errored turn's state to the accumulator, append only the new tail's model messages (errored user message and/or recovered partial) instead of reconverting the whole UI history, so a prior turn's model-only compaction survives an errored turn. Skip re-adding the partial once the response was already committed (a later compaction may have folded it into a summary), and roll the event/snapshot views back if the model conversion fails so they never advertise a partial the accumulator didn't take.
1 parent ecfede6 commit 1916507

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

  • packages/trigger-sdk/src/v3

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7956,19 +7956,18 @@ function chatAgent<
79567956
if (partialResponse && !partialResponse.id) {
79577957
partialResponse = { ...partialResponse, id: generateMessageId() } as TUIMessage;
79587958
}
7959-
const erroredUIMessagesWithPartial: TUIMessage[] = !partialResponse
7959+
const includePartial = partialResponse != null && !responseCommitted;
7960+
let erroredUIMessagesWithPartial: TUIMessage[] = !includePartial
79607961
? erroredUIMessages
79617962
: partialIdx === -1
79627963
? [...erroredUIMessages, partialResponse]
79637964
: (erroredUIMessages.map((m, i) =>
79647965
i === partialIdx ? partialResponse : m
79657966
) as TUIMessage[]);
79667967

7967-
const erroredNewUIMessages: TUIMessage[] = erroredWireMessage
7968-
? [erroredWireMessage]
7969-
: [];
7970-
if (partialResponse && !responseCommitted) {
7971-
erroredNewUIMessages.push(partialResponse);
7968+
let erroredNewUIMessages: TUIMessage[] = erroredWireMessage ? [erroredWireMessage] : [];
7969+
if (includePartial) {
7970+
erroredNewUIMessages.push(partialResponse!);
79727971
}
79737972

79747973
let erroredNewModelMessages: ModelMessage[] = [];
@@ -7984,18 +7983,17 @@ function chatAgent<
79847983
// the success path's append branch); otherwise reconvert from UI.
79857984
// Guard the conversion so a secondary failure can't crash the run.
79867985
if (!responseCommitted && erroredUIMessagesWithPartial !== accumulatedUIMessages) {
7987-
const onlyAppendedPartial =
7988-
partialResponse != null &&
7989-
partialIdx === -1 &&
7990-
erroredUIMessages === accumulatedUIMessages;
79917986
try {
7992-
if (partialResponse) {
7987+
if (includePartial) {
79937988
erroredNewModelMessages = await toModelMessages([
7994-
stripProviderMetadata(partialResponse),
7989+
stripProviderMetadata(partialResponse!),
79957990
]);
79967991
}
7997-
if (onlyAppendedPartial) {
7998-
accumulatedMessages.push(...erroredNewModelMessages);
7992+
if (partialIdx === -1) {
7993+
const appended = erroredUIMessagesWithPartial.slice(accumulatedUIMessages.length);
7994+
accumulatedMessages.push(
7995+
...(await toModelMessages(appended.map((m) => stripProviderMetadata(m))))
7996+
);
79997997
} else {
80007998
accumulatedMessages = await toModelMessages(erroredUIMessagesWithPartial);
80017999
}
@@ -8004,6 +8002,8 @@ function chatAgent<
80048002
} catch {
80058003
// Keep the prior model accumulator if conversion fails.
80068004
erroredNewModelMessages = [];
8005+
erroredUIMessagesWithPartial = erroredUIMessages;
8006+
erroredNewUIMessages = erroredWireMessage ? [erroredWireMessage] : [];
80078007
}
80088008
}
80098009

0 commit comments

Comments
 (0)