Skip to content

Commit

Permalink
feat (ai/core): add stopStream option to streamText transforms (#4344)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel authored Jan 10, 2025
1 parent 084690d commit 5510ee7
Show file tree
Hide file tree
Showing 8 changed files with 821 additions and 628 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-files-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

feat (ai/core): add stopStream option to streamText transforms
2 changes: 1 addition & 1 deletion content/docs/03-ai-sdk-core/05-generating-text.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ all text to uppercase:
```ts
const upperCaseTransform =
<TOOLS extends Record<string, CoreTool>>() =>
(options: { tools: TOOLS }) =>
(options: { tools: TOOLS; stopStream: () => void }) =>
new TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>({
transform(chunk, controller) {
controller.enqueue(
Expand Down
19 changes: 18 additions & 1 deletion content/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,27 @@ To see `streamText` in action, check out [these examples](#examples).
},
{
name: 'experimental_transform',
type: '({ tools: TOOLS }) => TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>',
type: '(options: TransformOptions) => TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>',
isOptional: true,
description:
'Optional transformation that is applied to the stream. Tools are passed in for type inference.',
properties: [
{
type: 'TransformOptions',
parameters: [
{
name: 'stopStream',
type: '() => void',
description: 'A function that stops the stream.',
},
{
name: 'tools',
type: 'TOOLS',
description: 'The tools that are available.',
},
],
},
],
},
{
name: 'experimental_providerMetadata',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1858,7 +1858,7 @@ exports[`streamText > options.onFinish > should send correct information 1`] = `
}
`;

exports[`streamText > options.transform > options.onFinish should receive transformed data 1`] = `
exports[`streamText > options.transform > with base transformation > options.onFinish should receive transformed data 1`] = `
{
"experimental_providerMetadata": {
"testProvider": {
Expand Down Expand Up @@ -2017,7 +2017,7 @@ exports[`streamText > options.transform > options.onFinish should receive transf
}
`;

exports[`streamText > options.transform > options.onStepFinish should receive transformed data 1`] = `
exports[`streamText > options.transform > with base transformation > options.onStepFinish should receive transformed data 1`] = `
{
"experimental_providerMetadata": {
"testProvider": {
Expand Down Expand Up @@ -2098,7 +2098,7 @@ exports[`streamText > options.transform > options.onStepFinish should receive tr
}
`;

exports[`streamText > options.transform > telemetry should record transformed data when enabled 1`] = `
exports[`streamText > options.transform > with base transformation > telemetry should record transformed data when enabled 1`] = `
[
{
"attributes": {
Expand Down
Loading

0 comments on commit 5510ee7

Please sign in to comment.