Skip to content

Commit

Permalink
fix (ai): trigger onFinal when stream adapter finishes (#3860)
Browse files Browse the repository at this point in the history
Co-authored-by: thucpn <[email protected]>
  • Loading branch information
lgrammel and thucpn authored Nov 25, 2024
1 parent e0599ba commit 6ff6689
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/rare-beers-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

fix (ai): trigger onFinal when stream adapter finishes
5 changes: 5 additions & 0 deletions .changeset/warm-geckos-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

chore (ai): deprecate onCompletion (stream callbacks)
13 changes: 12 additions & 1 deletion packages/ai/streams/stream-callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
export interface StreamCallbacks {
/** `onStart`: Called once when the stream is initialized. */
onStart?: () => Promise<void> | void;
/** `onCompletion`: Called for each tokenized message. */

/**
* `onCompletion`: Called for each tokenized message.
*
* @deprecated Use `onFinal` instead.
*/
onCompletion?: (completion: string) => Promise<void> | void;

/** `onFinal`: Called once when the stream is closed with the final completion message. */
onFinal?: (completion: string) => Promise<void> | void;

/** `onToken`: Called for each tokenized message. */
onToken?: (token: string) => Promise<void> | void;

/** `onText`: Called for each text chunk. */
onText?: (text: string) => Promise<void> | void;
}
Expand Down Expand Up @@ -62,6 +70,9 @@ export function createCallbacksTransformer(
if (callbacks.onCompletion) {
await callbacks.onCompletion(aggregatedResponse);
}
if (callbacks.onFinal) {
await callbacks.onFinal(aggregatedResponse);
}
},
});
}

0 comments on commit 6ff6689

Please sign in to comment.