Skip to content

Commit 32f0536

Browse files
committed
fix(sdk): guard no-source turn.complete() to final handover turns
1 parent c4bc008 commit 32f0536

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

  • packages/trigger-sdk/src/v3

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9476,11 +9476,24 @@ function createChatSession(
94769476

94779477
async complete(source?: UIMessageStreamable) {
94789478
// Head-start final turn: the warm step-1 partial is already spliced
9479-
// into the accumulator and IS the response — nothing to pipe.
9479+
// into the accumulator and IS the response — nothing to pipe. Only
9480+
// valid on a final handover; a missing source on any other turn is a
9481+
// mistake (it would silently finalize without an assistant response).
94809482
if (!source) {
9483+
if (!handoverThisTurn?.isFinal) {
9484+
throw new Error(
9485+
"turn.complete() requires a stream source unless turn.handover.isFinal is true"
9486+
);
9487+
}
9488+
const response = accumulator.uiMessages.at(-1);
9489+
if (!response || response.role !== "assistant") {
9490+
throw new Error(
9491+
"turn.complete() could not find the spliced handover response"
9492+
);
9493+
}
94819494
sessionMsgSub.off();
94829495
await chatWriteTurnComplete();
9483-
return accumulator.uiMessages.at(-1);
9496+
return response;
94849497
}
94859498
let response: UIMessage | undefined;
94869499
try {

0 commit comments

Comments
 (0)