diff --git a/.changeset/rare-beers-pay.md b/.changeset/rare-beers-pay.md new file mode 100644 index 000000000000..83baf4adf772 --- /dev/null +++ b/.changeset/rare-beers-pay.md @@ -0,0 +1,5 @@ +--- +'ai': patch +--- + +fix (ai): trigger onFinal when stream adapter finishes diff --git a/.changeset/warm-geckos-fly.md b/.changeset/warm-geckos-fly.md new file mode 100644 index 000000000000..2031e1c3e971 --- /dev/null +++ b/.changeset/warm-geckos-fly.md @@ -0,0 +1,5 @@ +--- +'ai': patch +--- + +chore (ai): deprecate onCompletion (stream callbacks) diff --git a/packages/ai/streams/stream-callbacks.ts b/packages/ai/streams/stream-callbacks.ts index bb7867aa533b..1281e55a379f 100644 --- a/packages/ai/streams/stream-callbacks.ts +++ b/packages/ai/streams/stream-callbacks.ts @@ -4,12 +4,20 @@ export interface StreamCallbacks { /** `onStart`: Called once when the stream is initialized. */ onStart?: () => Promise | void; - /** `onCompletion`: Called for each tokenized message. */ + + /** + * `onCompletion`: Called for each tokenized message. + * + * @deprecated Use `onFinal` instead. + */ onCompletion?: (completion: string) => Promise | void; + /** `onFinal`: Called once when the stream is closed with the final completion message. */ onFinal?: (completion: string) => Promise | void; + /** `onToken`: Called for each tokenized message. */ onToken?: (token: string) => Promise | void; + /** `onText`: Called for each text chunk. */ onText?: (text: string) => Promise | void; } @@ -62,6 +70,9 @@ export function createCallbacksTransformer( if (callbacks.onCompletion) { await callbacks.onCompletion(aggregatedResponse); } + if (callbacks.onFinal) { + await callbacks.onFinal(aggregatedResponse); + } }, }); }