ai 6.0.40 → 6.0.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/dist/index.d.mts +0 -4
- package/dist/index.d.ts +0 -4
- package/dist/index.js +322 -349
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +322 -349
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -20
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +1 -20
- package/dist/internal/index.mjs.map +1 -1
- package/docs/02-foundations/02-providers-and-models.mdx +1 -0
- package/docs/02-foundations/04-tools.mdx +89 -0
- package/docs/02-getting-started/02-nextjs-app-router.mdx +3 -3
- package/docs/02-getting-started/03-nextjs-pages-router.mdx +2 -2
- package/docs/02-getting-started/04-svelte.mdx +2 -2
- package/docs/02-getting-started/05-nuxt.mdx +2 -2
- package/docs/02-getting-started/06-nodejs.mdx +2 -2
- package/docs/02-getting-started/07-expo.mdx +1 -1
- package/docs/02-getting-started/08-tanstack-start.mdx +2 -2
- package/docs/05-ai-sdk-rsc/03-generative-ui-state.mdx +1 -1
- package/docs/05-ai-sdk-rsc/05-streaming-values.mdx +1 -1
- package/docs/06-advanced/02-stopping-streams.mdx +2 -2
- package/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx +0 -7
- package/docs/07-reference/02-ai-sdk-ui/02-use-completion.mdx +1 -1
- package/docs/08-migration-guides/26-migration-guide-5-0.mdx +2 -2
- package/docs/09-troubleshooting/15-abort-breaks-resumable-streams.mdx +1 -1
- package/package.json +2 -2
- package/src/agent/create-agent-ui-stream-response.test.ts +1 -1
- package/src/generate-text/__snapshots__/stream-text.test.ts.snap +4 -52
- package/src/generate-text/stream-text.test.ts +7 -407
- package/src/generate-text/stream-text.ts +0 -1
- package/src/types/usage.ts +0 -24
- package/src/ui/chat.ts +0 -6
- package/src/ui/process-ui-message-stream.ts +0 -5
- package/src/ui-message-stream/ui-message-chunks.ts +0 -3
|
@@ -5,7 +5,7 @@ description: Learn how to build your first agent with the AI SDK and Node.js.
|
|
|
5
5
|
|
|
6
6
|
# Node.js Quickstart
|
|
7
7
|
|
|
8
|
-
The AI SDK is a powerful
|
|
8
|
+
The AI SDK is a powerful TypeScript library designed to help developers build AI-powered applications.
|
|
9
9
|
|
|
10
10
|
In this quickstart tutorial, you'll build a simple agent with a streaming chat user interface. Along the way, you'll learn key concepts and techniques that are fundamental to using the SDK in your own projects.
|
|
11
11
|
|
|
@@ -47,7 +47,7 @@ pnpm add ai zod dotenv
|
|
|
47
47
|
pnpm add -D @types/node tsx typescript
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
The `ai` package contains the AI SDK. You will use `zod` to define type-safe schemas that you will pass to the large language model (LLM). You will use `dotenv` to access environment variables (your Vercel AI Gateway key) within your application. There are also three development dependencies, installed with the `-D` flag, that are necessary to run your
|
|
50
|
+
The `ai` package contains the AI SDK. You will use `zod` to define type-safe schemas that you will pass to the large language model (LLM). You will use `dotenv` to access environment variables (your Vercel AI Gateway key) within your application. There are also three development dependencies, installed with the `-D` flag, that are necessary to run your TypeScript code.
|
|
51
51
|
|
|
52
52
|
### Configure Vercel AI Gateway API key
|
|
53
53
|
|
|
@@ -105,7 +105,7 @@ export async function POST(req: Request) {
|
|
|
105
105
|
Let's take a look at what is happening in this code:
|
|
106
106
|
|
|
107
107
|
1. Define an asynchronous `POST` request handler and extract `messages` from the body of the request. The `messages` variable contains a history of the conversation between you and the chatbot and provides the chatbot with the necessary context to make the next generation.
|
|
108
|
-
2. Call [`streamText`](/docs/reference/ai-sdk-core/stream-text), which is imported from the `ai` package. This function accepts a configuration object that contains a `model` provider (imported from `ai`) and `messages` (defined in step 1). You can pass additional [settings](/docs/ai-sdk-core/settings) to further
|
|
108
|
+
2. Call [`streamText`](/docs/reference/ai-sdk-core/stream-text), which is imported from the `ai` package. This function accepts a configuration object that contains a `model` provider (imported from `ai`) and `messages` (defined in step 1). You can pass additional [settings](/docs/ai-sdk-core/settings) to further customize the model's behavior.
|
|
109
109
|
3. The `streamText` function returns a [`StreamTextResult`](/docs/reference/ai-sdk-core/stream-text#result-object). This result object contains the [ `toUIMessageStreamResponse` ](/docs/reference/ai-sdk-core/stream-text#to-ui-message-stream-response) function which converts the result to a streamed response object.
|
|
110
110
|
4. Finally, return the result to the client to stream the response.
|
|
111
111
|
|
|
@@ -5,7 +5,7 @@ description: Learn how to build your first agent with the AI SDK and TanStack St
|
|
|
5
5
|
|
|
6
6
|
# TanStack Start Quickstart
|
|
7
7
|
|
|
8
|
-
The AI SDK is a powerful
|
|
8
|
+
The AI SDK is a powerful TypeScript library designed to help developers build AI-powered applications.
|
|
9
9
|
|
|
10
10
|
In this quickstart tutorial, you'll build a simple agent with a streaming chat user interface. Along the way, you'll learn key concepts and techniques that are fundamental to using the AI SDK in your own projects.
|
|
11
11
|
|
|
@@ -109,7 +109,7 @@ export const Route = createFileRoute('/api/chat')({
|
|
|
109
109
|
Let's take a look at what is happening in this code:
|
|
110
110
|
|
|
111
111
|
1. Define an asynchronous `POST` request handler using TanStack Start's server routes and extract `messages` from the body of the request. The `messages` variable contains a history of the conversation between you and the chatbot and provides the chatbot with the necessary context to make the next generation. The `messages` are of UIMessage type, which are designed for use in application UI - they contain the entire message history and associated metadata like timestamps.
|
|
112
|
-
2. Call [`streamText`](/docs/reference/ai-sdk-core/stream-text), which is imported from the `ai` package. This function accepts a configuration object that contains a `model` provider and `messages` (defined in step 1). You can pass additional [settings](/docs/ai-sdk-core/settings) to further
|
|
112
|
+
2. Call [`streamText`](/docs/reference/ai-sdk-core/stream-text), which is imported from the `ai` package. This function accepts a configuration object that contains a `model` provider and `messages` (defined in step 1). You can pass additional [settings](/docs/ai-sdk-core/settings) to further customize the model's behavior. The `messages` key expects a `ModelMessage[]` array. This type is different from `UIMessage` in that it does not include metadata, such as timestamps or sender information. To convert between these types, we use the `convertToModelMessages` function, which strips the UI-specific metadata and transforms the `UIMessage[]` array into the `ModelMessage[]` format that the model expects.
|
|
113
113
|
3. The `streamText` function returns a [`StreamTextResult`](/docs/reference/ai-sdk-core/stream-text#result-object). This result object contains the [ `toUIMessageStreamResponse` ](/docs/reference/ai-sdk-core/stream-text#to-data-stream-response) function which converts the result to a streamed response object.
|
|
114
114
|
4. Finally, return the result to the client to stream the response.
|
|
115
115
|
|
|
@@ -44,7 +44,7 @@ For a chat app, the AI State is the conversation history (messages) between the
|
|
|
44
44
|
|
|
45
45
|
### UI State
|
|
46
46
|
|
|
47
|
-
UI State refers to the state of your application that is rendered on the client. It is a fully client-side state (similar to `useState`) that can store anything from
|
|
47
|
+
UI State refers to the state of your application that is rendered on the client. It is a fully client-side state (similar to `useState`) that can store anything from JavaScript values to React elements. UI state is a list of actual UI elements that are rendered on the client.
|
|
48
48
|
|
|
49
49
|
<Note>**Note**: UI State can only be accessed client-side.</Note>
|
|
50
50
|
|
|
@@ -32,7 +32,7 @@ There are two functions provided by the RSC API that allow you to create streama
|
|
|
32
32
|
|
|
33
33
|
## `createStreamableValue`
|
|
34
34
|
|
|
35
|
-
The RSC API allows you to stream serializable
|
|
35
|
+
The RSC API allows you to stream serializable JavaScript values from the server to the client using [`createStreamableValue`](/docs/reference/ai-sdk-rsc/create-streamable-value), such as strings, numbers, objects, and arrays.
|
|
36
36
|
|
|
37
37
|
This is useful when you want to stream:
|
|
38
38
|
|
|
@@ -5,10 +5,10 @@ description: Learn how to cancel streams with the AI SDK
|
|
|
5
5
|
|
|
6
6
|
# Stopping Streams
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
Canceling ongoing streams is often needed.
|
|
9
9
|
For example, users might want to stop a stream when they realize that the response is not what they want.
|
|
10
10
|
|
|
11
|
-
The different parts of the AI SDK support
|
|
11
|
+
The different parts of the AI SDK support canceling streams in different ways.
|
|
12
12
|
|
|
13
13
|
## AI SDK Core
|
|
14
14
|
|
|
@@ -282,13 +282,6 @@ Allows you to easily create a conversational user interface for your chatbot app
|
|
|
282
282
|
description:
|
|
283
283
|
'The reason why the model finished generating the response. Undefined if the finish reason was not provided by the model.',
|
|
284
284
|
},
|
|
285
|
-
{
|
|
286
|
-
name: 'usage',
|
|
287
|
-
type: 'LanguageModelUsage',
|
|
288
|
-
isOptional: true,
|
|
289
|
-
description:
|
|
290
|
-
'Token usage information for the response, including inputTokens, outputTokens, and totalTokens.',
|
|
291
|
-
},
|
|
292
285
|
],
|
|
293
286
|
},
|
|
294
287
|
],
|
|
@@ -50,7 +50,7 @@ Allows you to create text completion based capabilities for your application. It
|
|
|
50
50
|
name: 'id',
|
|
51
51
|
type: 'string',
|
|
52
52
|
description:
|
|
53
|
-
'
|
|
53
|
+
'A unique identifier for the completion. If not provided, a random one will be generated. When provided, the `useCompletion` hook with the same `id` will have shared states across components. This is useful when you have multiple components showing the same chat stream',
|
|
54
54
|
},
|
|
55
55
|
{
|
|
56
56
|
name: 'initialInput',
|
|
@@ -3104,7 +3104,7 @@ const result = await generateObject({
|
|
|
3104
3104
|
}),
|
|
3105
3105
|
providerOptions: {
|
|
3106
3106
|
openai: {
|
|
3107
|
-
strictSchemas: true, // default
|
|
3107
|
+
strictSchemas: true, // default behavior in AI SDK 4
|
|
3108
3108
|
} satisfies OpenAIResponsesProviderOptions,
|
|
3109
3109
|
},
|
|
3110
3110
|
});
|
|
@@ -3122,7 +3122,7 @@ const result = await generateObject({
|
|
|
3122
3122
|
}),
|
|
3123
3123
|
providerOptions: {
|
|
3124
3124
|
openai: {
|
|
3125
|
-
strictJsonSchema: true, // defaults to false, opt back in to the AI SDK 4 strict
|
|
3125
|
+
strictJsonSchema: true, // defaults to false, opt back in to the AI SDK 4 strict behavior
|
|
3126
3126
|
} satisfies OpenAIResponsesProviderOptions,
|
|
3127
3127
|
},
|
|
3128
3128
|
});
|
|
@@ -45,7 +45,7 @@ If you need to allow users to stop streams manually:
|
|
|
45
45
|
```tsx
|
|
46
46
|
const { messages, sendMessage, stop } = useChat({
|
|
47
47
|
id: chatId,
|
|
48
|
-
resume: false, // Disable stream resumption (default
|
|
48
|
+
resume: false, // Disable stream resumption (default behavior)
|
|
49
49
|
});
|
|
50
50
|
```
|
|
51
51
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.42",
|
|
4
4
|
"description": "AI SDK by Vercel - The AI Toolkit for TypeScript and JavaScript",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@opentelemetry/api": "1.9.0",
|
|
45
|
-
"@ai-sdk/gateway": "3.0.
|
|
45
|
+
"@ai-sdk/gateway": "3.0.17",
|
|
46
46
|
"@ai-sdk/provider": "3.0.4",
|
|
47
47
|
"@ai-sdk/provider-utils": "4.0.8"
|
|
48
48
|
},
|
|
@@ -245,7 +245,7 @@ describe('createAgentUIStreamResponse', () => {
|
|
|
245
245
|
"data: {"type":"finish-step"}
|
|
246
246
|
|
|
247
247
|
",
|
|
248
|
-
"data: {"type":"finish","finishReason":"stop"
|
|
248
|
+
"data: {"type":"finish","finishReason":"stop"}
|
|
249
249
|
|
|
250
250
|
",
|
|
251
251
|
"data: [DONE]
|
|
@@ -655,7 +655,7 @@ exports[`streamText > result.pipeUIMessageStreamToResponse > should mask error m
|
|
|
655
655
|
"data: {"type":"finish-step"}
|
|
656
656
|
|
|
657
657
|
",
|
|
658
|
-
"data: {"type":"finish","finishReason":"error"
|
|
658
|
+
"data: {"type":"finish","finishReason":"error"}
|
|
659
659
|
|
|
660
660
|
",
|
|
661
661
|
"data: [DONE]
|
|
@@ -678,7 +678,7 @@ exports[`streamText > result.pipeUIMessageStreamToResponse > should support cust
|
|
|
678
678
|
"data: {"type":"finish-step"}
|
|
679
679
|
|
|
680
680
|
",
|
|
681
|
-
"data: {"type":"finish","finishReason":"error"
|
|
681
|
+
"data: {"type":"finish","finishReason":"error"}
|
|
682
682
|
|
|
683
683
|
",
|
|
684
684
|
"data: [DONE]
|
|
@@ -803,22 +803,6 @@ exports[`streamText > result.toUIMessageStream > should mask error messages by d
|
|
|
803
803
|
{
|
|
804
804
|
"finishReason": "error",
|
|
805
805
|
"type": "finish",
|
|
806
|
-
"usage": {
|
|
807
|
-
"cachedInputTokens": undefined,
|
|
808
|
-
"inputTokenDetails": {
|
|
809
|
-
"cacheReadTokens": undefined,
|
|
810
|
-
"cacheWriteTokens": undefined,
|
|
811
|
-
"noCacheTokens": undefined,
|
|
812
|
-
},
|
|
813
|
-
"inputTokens": undefined,
|
|
814
|
-
"outputTokenDetails": {
|
|
815
|
-
"reasoningTokens": undefined,
|
|
816
|
-
"textTokens": undefined,
|
|
817
|
-
},
|
|
818
|
-
"outputTokens": undefined,
|
|
819
|
-
"reasoningTokens": undefined,
|
|
820
|
-
"totalTokens": undefined,
|
|
821
|
-
},
|
|
822
806
|
},
|
|
823
807
|
]
|
|
824
808
|
`;
|
|
@@ -865,22 +849,6 @@ exports[`streamText > result.toUIMessageStream > should send tool call, tool cal
|
|
|
865
849
|
{
|
|
866
850
|
"finishReason": "stop",
|
|
867
851
|
"type": "finish",
|
|
868
|
-
"usage": {
|
|
869
|
-
"cachedInputTokens": undefined,
|
|
870
|
-
"inputTokenDetails": {
|
|
871
|
-
"cacheReadTokens": undefined,
|
|
872
|
-
"cacheWriteTokens": undefined,
|
|
873
|
-
"noCacheTokens": 3,
|
|
874
|
-
},
|
|
875
|
-
"inputTokens": 3,
|
|
876
|
-
"outputTokenDetails": {
|
|
877
|
-
"reasoningTokens": undefined,
|
|
878
|
-
"textTokens": 10,
|
|
879
|
-
},
|
|
880
|
-
"outputTokens": 10,
|
|
881
|
-
"reasoningTokens": undefined,
|
|
882
|
-
"totalTokens": 13,
|
|
883
|
-
},
|
|
884
852
|
},
|
|
885
853
|
]
|
|
886
854
|
`;
|
|
@@ -903,22 +871,6 @@ exports[`streamText > result.toUIMessageStream > should support custom error mes
|
|
|
903
871
|
{
|
|
904
872
|
"finishReason": "error",
|
|
905
873
|
"type": "finish",
|
|
906
|
-
"usage": {
|
|
907
|
-
"cachedInputTokens": undefined,
|
|
908
|
-
"inputTokenDetails": {
|
|
909
|
-
"cacheReadTokens": undefined,
|
|
910
|
-
"cacheWriteTokens": undefined,
|
|
911
|
-
"noCacheTokens": undefined,
|
|
912
|
-
},
|
|
913
|
-
"inputTokens": undefined,
|
|
914
|
-
"outputTokenDetails": {
|
|
915
|
-
"reasoningTokens": undefined,
|
|
916
|
-
"textTokens": undefined,
|
|
917
|
-
},
|
|
918
|
-
"outputTokens": undefined,
|
|
919
|
-
"reasoningTokens": undefined,
|
|
920
|
-
"totalTokens": undefined,
|
|
921
|
-
},
|
|
922
874
|
},
|
|
923
875
|
]
|
|
924
876
|
`;
|
|
@@ -937,7 +889,7 @@ exports[`streamText > result.toUIMessageStreamResponse > should mask error messa
|
|
|
937
889
|
"data: {"type":"finish-step"}
|
|
938
890
|
|
|
939
891
|
",
|
|
940
|
-
"data: {"type":"finish","finishReason":"error"
|
|
892
|
+
"data: {"type":"finish","finishReason":"error"}
|
|
941
893
|
|
|
942
894
|
",
|
|
943
895
|
"data: [DONE]
|
|
@@ -960,7 +912,7 @@ exports[`streamText > result.toUIMessageStreamResponse > should support custom e
|
|
|
960
912
|
"data: {"type":"finish-step"}
|
|
961
913
|
|
|
962
914
|
",
|
|
963
|
-
"data: {"type":"finish","finishReason":"error"
|
|
915
|
+
"data: {"type":"finish","finishReason":"error"}
|
|
964
916
|
|
|
965
917
|
",
|
|
966
918
|
"data: [DONE]
|