ai 7.0.107 → 7.0.108
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 +15 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +205 -39
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +4 -1
- package/dist/internal/index.js +180 -19
- package/dist/internal/index.js.map +1 -1
- package/docs/00-introduction/index.mdx +1 -1
- package/docs/02-getting-started/03-nextjs-pages-router.mdx +1 -1
- package/docs/02-getting-started/05-nuxt.mdx +1 -1
- package/docs/03-ai-sdk-core/16-mcp-tools.mdx +31 -0
- package/docs/04-ai-sdk-ui/02-chatbot.mdx +26 -11
- package/docs/04-ai-sdk-ui/05-completion.mdx +1 -1
- package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +7 -0
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +7 -0
- package/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx +1 -1
- package/package.json +6 -6
- package/src/generate-text/generate-text.ts +2 -1
- package/src/generate-text/parse-tool-call.ts +64 -12
- package/src/generate-text/stream-language-model-call.ts +32 -21
- package/src/generate-text/tool-call-repair-function.ts +2 -0
- package/src/model/as-language-model-v4.ts +166 -6
|
@@ -26,7 +26,7 @@ For example, here’s how you can generate text with various models using the AI
|
|
|
26
26
|
The AI SDK has these primary surfaces:
|
|
27
27
|
|
|
28
28
|
- **[AI SDK Core](/docs/ai-sdk-core):** A unified API for generating text, structured objects, tool calls, and building agents with LLMs.
|
|
29
|
-
- **[AI SDK UI](/docs/ai-sdk-ui):** A set of framework-agnostic hooks for quickly building chat and generative user
|
|
29
|
+
- **[AI SDK UI](/docs/ai-sdk-ui):** A set of framework-agnostic hooks for quickly building chat and generative user interfaces.
|
|
30
30
|
- **[AI SDK Harnesses](/docs/ai-sdk-harnesses):** A uniform API for running established agent harnesses with `HarnessAgent`.
|
|
31
31
|
|
|
32
32
|
## Model Providers
|
|
@@ -153,7 +153,7 @@ Pick the approach that best matches how you want to manage providers across your
|
|
|
153
153
|
|
|
154
154
|
## Wire up the UI
|
|
155
155
|
|
|
156
|
-
Now that you have an API route that can query an LLM, it's time to
|
|
156
|
+
Now that you have an API route that can query an LLM, it's time to set up your frontend. The AI SDK's [ UI ](/docs/ai-sdk-ui) package abstracts the complexity of a chat interface into one hook, [`useChat`](/docs/reference/ai-sdk-ui/use-chat).
|
|
157
157
|
|
|
158
158
|
Update your root page (`pages/index.tsx`) with the following code to show a list of chat messages and provide a user message input:
|
|
159
159
|
|
|
@@ -154,7 +154,7 @@ model: openai('gpt-5.1');
|
|
|
154
154
|
|
|
155
155
|
## Wire up the UI
|
|
156
156
|
|
|
157
|
-
Now that you have an API route that can query an LLM, it's time to
|
|
157
|
+
Now that you have an API route that can query an LLM, it's time to set up your frontend. The AI SDK's [ UI ](/docs/ai-sdk-ui/overview) package abstracts the complexity of a chat interface into one hook, [`useChat`](/docs/reference/ai-sdk-ui/use-chat).
|
|
158
158
|
|
|
159
159
|
Update your root page (`pages/index.vue`) with the following code to show a list of chat messages and provide a user message input:
|
|
160
160
|
|
|
@@ -236,6 +236,37 @@ client infers `native` for loopback, localhost, and custom-scheme redirects,
|
|
|
236
236
|
and `web` for remote HTTP(S) redirects. You can override this by setting
|
|
237
237
|
`application_type` in the provider's `clientMetadata`.
|
|
238
238
|
|
|
239
|
+
### Dynamic Client Credential Recovery
|
|
240
|
+
|
|
241
|
+
To let the SDK replace invalid credentials created through dynamic client
|
|
242
|
+
registration, implement `isClientInformationDynamicallyRegistered` on the
|
|
243
|
+
OAuth client provider. Return `true` only when the current client information
|
|
244
|
+
came from a dynamic registration response. Persist this provenance alongside
|
|
245
|
+
the credentials when the provider is recreated between requests.
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
const myOAuthClientProvider = {
|
|
249
|
+
// ...other OAuthClientProvider methods
|
|
250
|
+
|
|
251
|
+
async saveClientInformation(clientInformation) {
|
|
252
|
+
await saveClientRecord({
|
|
253
|
+
clientInformation,
|
|
254
|
+
dynamicallyRegistered: true,
|
|
255
|
+
});
|
|
256
|
+
},
|
|
257
|
+
|
|
258
|
+
async isClientInformationDynamicallyRegistered() {
|
|
259
|
+
return (await loadClientRecord())?.dynamicallyRegistered === true;
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
When this method is omitted or returns `false`, client information is treated
|
|
265
|
+
as pre-registered and is preserved after `invalid_client` and
|
|
266
|
+
`unauthorized_client` errors. The SDK does not infer provenance from fields such
|
|
267
|
+
as `redirect_uris`, because pre-registered clients can contain the same
|
|
268
|
+
metadata as dynamically registered clients.
|
|
269
|
+
|
|
239
270
|
### Retrying Transient Tool Failures
|
|
240
271
|
|
|
241
272
|
MCP tool calls can fail for transient transport reasons, such as rate limits,
|
|
@@ -323,17 +323,32 @@ When the user clicks the "Regenerate" button, the AI provider will regenerate th
|
|
|
323
323
|
|
|
324
324
|
### Throttling UI Updates
|
|
325
325
|
|
|
326
|
-
<Note>This feature is currently only available for React.</Note>
|
|
327
|
-
|
|
328
326
|
By default, the `useChat` hook will trigger a render every time a new chunk is received.
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
}
|
|
336
|
-
|
|
327
|
+
React and Vue applications can throttle reactive message updates with the `throttle` option.
|
|
328
|
+
Stream processing and event callbacks remain immediate, and the latest messages are published before the chat enters a terminal `ready` or `error` status.
|
|
329
|
+
|
|
330
|
+
<Tabs items={['React', 'Vue']}>
|
|
331
|
+
<Tab>
|
|
332
|
+
```tsx filename="page.tsx" highlight="2-3"
|
|
333
|
+
const { messages, ... } = useChat({
|
|
334
|
+
// Throttle reactive message updates to 50ms:
|
|
335
|
+
throttle: 50,
|
|
336
|
+
});
|
|
337
|
+
```
|
|
338
|
+
</Tab>
|
|
339
|
+
<Tab>
|
|
340
|
+
```vue filename="pages/index.vue" highlight="4-6"
|
|
341
|
+
<script setup lang="ts">
|
|
342
|
+
import { useChat } from '@ai-sdk/vue';
|
|
343
|
+
|
|
344
|
+
const { messages } = useChat({
|
|
345
|
+
throttle: 50,
|
|
346
|
+
});
|
|
347
|
+
</script>
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
</Tab>
|
|
351
|
+
</Tabs>
|
|
337
352
|
|
|
338
353
|
## Event Callbacks
|
|
339
354
|
|
|
@@ -369,7 +384,7 @@ It's worth noting that you can abort the processing by throwing an error in the
|
|
|
369
384
|
|
|
370
385
|
### Custom headers, body, and credentials
|
|
371
386
|
|
|
372
|
-
By default, the `useChat` hook sends
|
|
387
|
+
By default, the `useChat` hook sends an HTTP POST request to the `/api/chat` endpoint with the message list as the request body. You can customize the request in two ways:
|
|
373
388
|
|
|
374
389
|
#### Hook-Level Configuration (Applied to all requests)
|
|
375
390
|
|
|
@@ -169,7 +169,7 @@ const { ... } = useCompletion({
|
|
|
169
169
|
|
|
170
170
|
## Configure Request Options
|
|
171
171
|
|
|
172
|
-
By default, the `useCompletion` hook sends
|
|
172
|
+
By default, the `useCompletion` hook sends an HTTP POST request to the `/api/completion` endpoint with the prompt as part of the request body. You can customize the request by passing additional options to the `useCompletion` hook:
|
|
173
173
|
|
|
174
174
|
```tsx
|
|
175
175
|
const { messages, input, handleInputChange, handleSubmit } = useCompletion({
|
|
@@ -929,6 +929,13 @@ To see `generateText` in action, check out [these examples](#examples).
|
|
|
929
929
|
description:
|
|
930
930
|
'The error that occurred while parsing the tool call.',
|
|
931
931
|
},
|
|
932
|
+
{
|
|
933
|
+
name: 'abortSignal',
|
|
934
|
+
type: 'AbortSignal',
|
|
935
|
+
isOptional: true,
|
|
936
|
+
description:
|
|
937
|
+
'An optional signal for cancelling the tool call repair.',
|
|
938
|
+
},
|
|
932
939
|
],
|
|
933
940
|
},
|
|
934
941
|
],
|
|
@@ -980,6 +980,13 @@ To see `streamText` in action, check out [these examples](#examples).
|
|
|
980
980
|
description:
|
|
981
981
|
'The error that occurred while parsing the tool call.',
|
|
982
982
|
},
|
|
983
|
+
{
|
|
984
|
+
name: 'abortSignal',
|
|
985
|
+
type: 'AbortSignal',
|
|
986
|
+
isOptional: true,
|
|
987
|
+
description:
|
|
988
|
+
'An optional signal for cancelling the tool call repair.',
|
|
989
|
+
},
|
|
983
990
|
],
|
|
984
991
|
},
|
|
985
992
|
],
|
|
@@ -338,7 +338,7 @@ Allows you to easily create a conversational user interface for your chatbot app
|
|
|
338
338
|
type: 'number',
|
|
339
339
|
isOptional: true,
|
|
340
340
|
description:
|
|
341
|
-
'Custom throttle wait in
|
|
341
|
+
'React and Vue only. Custom throttle wait time in milliseconds for reactive chat message updates. Positive values reduce UI update frequency without delaying stream processing or callbacks, and the latest messages are published before a ready or error status. Default is undefined, which disables throttling.',
|
|
342
342
|
},
|
|
343
343
|
{
|
|
344
344
|
name: 'resume',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.108",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -42,20 +42,20 @@
|
|
|
42
42
|
}
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@ai-sdk/gateway": "4.0.
|
|
45
|
+
"@ai-sdk/gateway": "4.0.88",
|
|
46
46
|
"@ai-sdk/provider": "4.0.17",
|
|
47
47
|
"@ai-sdk/provider-utils": "5.0.45"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@ai-sdk/amazon-bedrock": "5.0.
|
|
51
|
-
"@ai-sdk/deepseek": "3.0.
|
|
50
|
+
"@ai-sdk/amazon-bedrock": "5.0.89",
|
|
51
|
+
"@ai-sdk/deepseek": "3.0.50",
|
|
52
52
|
"@ai-sdk/google": "4.0.76",
|
|
53
53
|
"@ai-sdk/groq": "4.0.46",
|
|
54
54
|
"@ai-sdk/huggingface": "2.0.53",
|
|
55
55
|
"@ai-sdk/moonshotai": "3.0.54",
|
|
56
|
-
"@ai-sdk/openai": "4.0.
|
|
56
|
+
"@ai-sdk/openai": "4.0.72",
|
|
57
57
|
"@ai-sdk/test-server": "2.0.1",
|
|
58
|
-
"@ai-sdk/xai": "5.0.
|
|
58
|
+
"@ai-sdk/xai": "5.0.5",
|
|
59
59
|
"@edge-runtime/vm": "^5.0.0",
|
|
60
60
|
"@smithy/eventstream-codec": "^4.3.3",
|
|
61
61
|
"@smithy/util-utf8": "^4.3.3",
|
|
@@ -1079,11 +1079,12 @@ export async function generateText<
|
|
|
1079
1079
|
.map(toolCall =>
|
|
1080
1080
|
parseToolCall({
|
|
1081
1081
|
toolCall,
|
|
1082
|
-
tools:
|
|
1082
|
+
tools: stepModelTools as TOOLS,
|
|
1083
1083
|
repairToolCall,
|
|
1084
1084
|
refineToolInput,
|
|
1085
1085
|
instructions: stepInstructions,
|
|
1086
1086
|
messages: stepMessages,
|
|
1087
|
+
abortSignal: mergedAbortSignal,
|
|
1087
1088
|
}),
|
|
1088
1089
|
),
|
|
1089
1090
|
);
|
|
@@ -23,6 +23,7 @@ export async function parseToolCall<TOOLS extends ToolSet>({
|
|
|
23
23
|
refineToolInput,
|
|
24
24
|
messages,
|
|
25
25
|
instructions,
|
|
26
|
+
abortSignal,
|
|
26
27
|
}: {
|
|
27
28
|
toolCall: LanguageModelV4ToolCall;
|
|
28
29
|
tools: TOOLS | undefined;
|
|
@@ -30,6 +31,7 @@ export async function parseToolCall<TOOLS extends ToolSet>({
|
|
|
30
31
|
refineToolInput?: ToolInputRefinement<TOOLS> | undefined;
|
|
31
32
|
instructions: Instructions | undefined;
|
|
32
33
|
messages: ModelMessage[];
|
|
34
|
+
abortSignal?: AbortSignal;
|
|
33
35
|
}): Promise<TypedToolCall<TOOLS>> {
|
|
34
36
|
try {
|
|
35
37
|
if (tools == null) {
|
|
@@ -63,19 +65,25 @@ export async function parseToolCall<TOOLS extends ToolSet>({
|
|
|
63
65
|
let repairedToolCall: LanguageModelV4ToolCall | null = null;
|
|
64
66
|
|
|
65
67
|
try {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
68
|
+
abortSignal?.throwIfAborted();
|
|
69
|
+
repairedToolCall = await waitForPromiseWithAbortSignal({
|
|
70
|
+
promise: repairToolCall({
|
|
71
|
+
toolCall,
|
|
72
|
+
tools,
|
|
73
|
+
inputSchema: async ({ toolName }) => {
|
|
74
|
+
const inputSchema = getOwn(tools, toolName)?.inputSchema;
|
|
75
|
+
return await asSchema(inputSchema).jsonSchema;
|
|
76
|
+
},
|
|
77
|
+
instructions,
|
|
78
|
+
system: instructions,
|
|
79
|
+
messages,
|
|
80
|
+
error,
|
|
81
|
+
abortSignal,
|
|
82
|
+
}),
|
|
83
|
+
abortSignal,
|
|
77
84
|
});
|
|
78
85
|
} catch (repairError) {
|
|
86
|
+
abortSignal?.throwIfAborted();
|
|
79
87
|
throw new ToolCallRepairError({
|
|
80
88
|
cause: repairError,
|
|
81
89
|
originalError: error,
|
|
@@ -87,12 +95,18 @@ export async function parseToolCall<TOOLS extends ToolSet>({
|
|
|
87
95
|
throw error;
|
|
88
96
|
}
|
|
89
97
|
|
|
90
|
-
|
|
98
|
+
const parsedRepairedToolCall = await refineParsedToolCallInput({
|
|
91
99
|
toolCall: await doParseToolCall({ toolCall: repairedToolCall, tools }),
|
|
92
100
|
refineToolInput,
|
|
93
101
|
});
|
|
102
|
+
|
|
103
|
+
abortSignal?.throwIfAborted();
|
|
104
|
+
|
|
105
|
+
return parsedRepairedToolCall;
|
|
94
106
|
}
|
|
95
107
|
} catch (error) {
|
|
108
|
+
abortSignal?.throwIfAborted();
|
|
109
|
+
|
|
96
110
|
// use parsed input when possible
|
|
97
111
|
const parsedInput = await safeParseJSON({ text: toolCall.input });
|
|
98
112
|
const input = parsedInput.success ? parsedInput.value : toolCall.input;
|
|
@@ -115,6 +129,44 @@ export async function parseToolCall<TOOLS extends ToolSet>({
|
|
|
115
129
|
}
|
|
116
130
|
}
|
|
117
131
|
|
|
132
|
+
async function waitForPromiseWithAbortSignal<T>({
|
|
133
|
+
promise,
|
|
134
|
+
abortSignal,
|
|
135
|
+
}: {
|
|
136
|
+
promise: PromiseLike<T>;
|
|
137
|
+
abortSignal: AbortSignal | undefined;
|
|
138
|
+
}): Promise<T> {
|
|
139
|
+
if (abortSignal == null) {
|
|
140
|
+
return await promise;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return await new Promise<T>((resolve, reject) => {
|
|
144
|
+
const cleanup = () => {
|
|
145
|
+
abortSignal.removeEventListener('abort', onAbort);
|
|
146
|
+
};
|
|
147
|
+
const onAbort = () => {
|
|
148
|
+
cleanup();
|
|
149
|
+
reject(abortSignal.reason);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
Promise.resolve(promise)
|
|
153
|
+
.then(value => {
|
|
154
|
+
cleanup();
|
|
155
|
+
resolve(value);
|
|
156
|
+
})
|
|
157
|
+
.catch(error => {
|
|
158
|
+
cleanup();
|
|
159
|
+
reject(error);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
abortSignal.addEventListener('abort', onAbort, { once: true });
|
|
163
|
+
|
|
164
|
+
if (abortSignal.aborted) {
|
|
165
|
+
onAbort();
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
118
170
|
async function refineParsedToolCallInput<TOOLS extends ToolSet>({
|
|
119
171
|
toolCall,
|
|
120
172
|
refineToolInput,
|
|
@@ -373,6 +373,7 @@ export async function streamLanguageModelCall<
|
|
|
373
373
|
messages: standardizedPrompt.messages,
|
|
374
374
|
repairToolCall,
|
|
375
375
|
refineToolInput,
|
|
376
|
+
abortSignal,
|
|
376
377
|
callId: effectiveCallId,
|
|
377
378
|
provider: resolvedModel.provider,
|
|
378
379
|
modelId: resolvedModel.modelId,
|
|
@@ -400,6 +401,7 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform<
|
|
|
400
401
|
messages,
|
|
401
402
|
repairToolCall,
|
|
402
403
|
refineToolInput,
|
|
404
|
+
abortSignal,
|
|
403
405
|
callId,
|
|
404
406
|
provider,
|
|
405
407
|
modelId,
|
|
@@ -414,6 +416,7 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform<
|
|
|
414
416
|
messages: ModelMessage[];
|
|
415
417
|
repairToolCall: ToolCallRepairFunction<TOOLS> | undefined;
|
|
416
418
|
refineToolInput: ToolInputRefinement<TOOLS> | undefined;
|
|
419
|
+
abortSignal: AbortSignal | undefined;
|
|
417
420
|
callId: string;
|
|
418
421
|
provider: string;
|
|
419
422
|
modelId: string;
|
|
@@ -637,40 +640,47 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform<
|
|
|
637
640
|
callbacks: onLanguageModelCallEnd,
|
|
638
641
|
});
|
|
639
642
|
|
|
640
|
-
// Preserve the completed model call's usage, metadata, and
|
|
641
|
-
// performance even when response validation below surfaces a
|
|
642
|
-
// semantic error.
|
|
643
|
-
controller.enqueue({
|
|
644
|
-
type: 'model-call-end',
|
|
645
|
-
finishReason: chunk.finishReason.unified,
|
|
646
|
-
rawFinishReason: chunk.finishReason.raw,
|
|
647
|
-
usage,
|
|
648
|
-
providerMetadata: chunk.providerMetadata,
|
|
649
|
-
performance,
|
|
650
|
-
});
|
|
651
|
-
|
|
652
643
|
const enforcedToolChoice =
|
|
653
644
|
toolChoice.type === 'required' || toolChoice.type === 'tool'
|
|
654
645
|
? toolChoice
|
|
655
646
|
: undefined;
|
|
656
647
|
|
|
657
|
-
|
|
648
|
+
const toolChoiceViolationError =
|
|
658
649
|
enforcedToolChoice != null &&
|
|
659
650
|
![...toolCallsByToolCallId.values()].some(
|
|
660
651
|
toolCall =>
|
|
661
652
|
enforcedToolChoice.type === 'required' ||
|
|
662
653
|
toolCall.toolName === enforcedToolChoice.toolName,
|
|
663
654
|
)
|
|
664
|
-
|
|
655
|
+
? new ToolChoiceViolationError({
|
|
656
|
+
toolChoice: enforcedToolChoice,
|
|
657
|
+
finishReason: chunk.finishReason.unified,
|
|
658
|
+
provider,
|
|
659
|
+
modelId,
|
|
660
|
+
content: rawModelCallContent,
|
|
661
|
+
})
|
|
662
|
+
: undefined;
|
|
663
|
+
|
|
664
|
+
// Preserve the completed model call's usage, metadata, and
|
|
665
|
+
// performance even when response validation below surfaces a
|
|
666
|
+
// semantic error. Prevent invalid tool calls from being executed
|
|
667
|
+
// when the model-call-end event reaches the tool executor.
|
|
668
|
+
controller.enqueue({
|
|
669
|
+
type: 'model-call-end',
|
|
670
|
+
finishReason:
|
|
671
|
+
toolChoiceViolationError == null
|
|
672
|
+
? chunk.finishReason.unified
|
|
673
|
+
: 'error',
|
|
674
|
+
rawFinishReason: chunk.finishReason.raw,
|
|
675
|
+
usage,
|
|
676
|
+
providerMetadata: chunk.providerMetadata,
|
|
677
|
+
performance,
|
|
678
|
+
});
|
|
679
|
+
|
|
680
|
+
if (toolChoiceViolationError != null) {
|
|
665
681
|
controller.enqueue({
|
|
666
682
|
type: 'error',
|
|
667
|
-
error:
|
|
668
|
-
toolChoice: enforcedToolChoice,
|
|
669
|
-
finishReason: chunk.finishReason.unified,
|
|
670
|
-
provider,
|
|
671
|
-
modelId,
|
|
672
|
-
content: rawModelCallContent,
|
|
673
|
-
}),
|
|
683
|
+
error: toolChoiceViolationError,
|
|
674
684
|
});
|
|
675
685
|
break;
|
|
676
686
|
}
|
|
@@ -688,6 +698,7 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform<
|
|
|
688
698
|
refineToolInput,
|
|
689
699
|
instructions,
|
|
690
700
|
messages,
|
|
701
|
+
abortSignal,
|
|
691
702
|
});
|
|
692
703
|
|
|
693
704
|
toolCallsByToolCallId.set(toolCall.toolCallId, toolCall);
|
|
@@ -17,6 +17,7 @@ import type { ToolSet } from '@ai-sdk/provider-utils';
|
|
|
17
17
|
* @param options.tools - The tools that are available.
|
|
18
18
|
* @param options.inputSchema - A function that returns the JSON Schema for a tool.
|
|
19
19
|
* @param options.error - The error that occurred while parsing the tool call.
|
|
20
|
+
* @param options.abortSignal - An optional signal for cancelling the repair.
|
|
20
21
|
*/
|
|
21
22
|
export type ToolCallRepairFunction<TOOLS extends ToolSet> = (options: {
|
|
22
23
|
instructions: Instructions | undefined;
|
|
@@ -29,4 +30,5 @@ export type ToolCallRepairFunction<TOOLS extends ToolSet> = (options: {
|
|
|
29
30
|
tools: TOOLS;
|
|
30
31
|
inputSchema: (options: { toolName: string }) => PromiseLike<JSONSchema7>;
|
|
31
32
|
error: NoSuchToolError | InvalidToolInputError;
|
|
33
|
+
abortSignal?: AbortSignal;
|
|
32
34
|
}) => Promise<LanguageModelV4ToolCall | null>;
|
|
@@ -1,8 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
LanguageModelV2,
|
|
3
|
-
LanguageModelV3,
|
|
4
|
-
|
|
1
|
+
import {
|
|
2
|
+
type LanguageModelV2,
|
|
3
|
+
type LanguageModelV3,
|
|
4
|
+
type LanguageModelV3Content,
|
|
5
|
+
type LanguageModelV3DataContent,
|
|
6
|
+
type LanguageModelV3Prompt,
|
|
7
|
+
type LanguageModelV3StreamPart,
|
|
8
|
+
type LanguageModelV3ToolResultOutput,
|
|
9
|
+
type LanguageModelV4,
|
|
10
|
+
type LanguageModelV4CallOptions,
|
|
11
|
+
type LanguageModelV4Content,
|
|
12
|
+
type LanguageModelV4Prompt,
|
|
13
|
+
type LanguageModelV4StreamPart,
|
|
14
|
+
type LanguageModelV4ToolResultOutput,
|
|
15
|
+
type SharedV4FileData,
|
|
5
16
|
} from '@ai-sdk/provider';
|
|
17
|
+
import { convertUint8ArrayToBase64 } from '@ai-sdk/provider-utils';
|
|
6
18
|
import { asLanguageModelV3 } from './as-language-model-v3';
|
|
7
19
|
|
|
8
20
|
export function asLanguageModelV4(
|
|
@@ -18,8 +30,156 @@ export function asLanguageModelV4(
|
|
|
18
30
|
|
|
19
31
|
return new Proxy(v3Model, {
|
|
20
32
|
get(target, prop: keyof LanguageModelV3) {
|
|
21
|
-
|
|
22
|
-
|
|
33
|
+
switch (prop) {
|
|
34
|
+
case 'specificationVersion':
|
|
35
|
+
return 'v4';
|
|
36
|
+
case 'doGenerate':
|
|
37
|
+
return async (options: LanguageModelV4CallOptions) => {
|
|
38
|
+
const result = await target.doGenerate({
|
|
39
|
+
...options,
|
|
40
|
+
prompt: convertV4PromptToV3(options.prompt),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
...result,
|
|
45
|
+
content: result.content.map(convertV3ContentToV4),
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
case 'doStream':
|
|
49
|
+
return async (options: LanguageModelV4CallOptions) => {
|
|
50
|
+
const result = await target.doStream({
|
|
51
|
+
...options,
|
|
52
|
+
prompt: convertV4PromptToV3(options.prompt),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
...result,
|
|
57
|
+
stream: convertV3StreamToV4(result.stream),
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
default:
|
|
61
|
+
return target[prop];
|
|
62
|
+
}
|
|
23
63
|
},
|
|
24
64
|
}) as unknown as LanguageModelV4;
|
|
25
65
|
}
|
|
66
|
+
|
|
67
|
+
function convertV4PromptToV3(
|
|
68
|
+
prompt: LanguageModelV4Prompt,
|
|
69
|
+
): LanguageModelV3Prompt {
|
|
70
|
+
return prompt.map(message => {
|
|
71
|
+
if (message.role === 'system') {
|
|
72
|
+
return message;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
...message,
|
|
77
|
+
content: message.content.map(part => {
|
|
78
|
+
switch (part.type) {
|
|
79
|
+
case 'file':
|
|
80
|
+
return {
|
|
81
|
+
...part,
|
|
82
|
+
data: convertV4FileDataToV3(part.data),
|
|
83
|
+
};
|
|
84
|
+
case 'tool-result':
|
|
85
|
+
return {
|
|
86
|
+
...part,
|
|
87
|
+
output: convertV4ToolResultOutputToV3(part.output),
|
|
88
|
+
};
|
|
89
|
+
default:
|
|
90
|
+
return part;
|
|
91
|
+
}
|
|
92
|
+
}),
|
|
93
|
+
};
|
|
94
|
+
}) as LanguageModelV3Prompt;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function convertV4FileDataToV3(
|
|
98
|
+
data: SharedV4FileData,
|
|
99
|
+
): LanguageModelV3DataContent {
|
|
100
|
+
switch (data.type) {
|
|
101
|
+
case 'data':
|
|
102
|
+
return data.data;
|
|
103
|
+
case 'url':
|
|
104
|
+
return data.url;
|
|
105
|
+
case 'reference':
|
|
106
|
+
case 'text':
|
|
107
|
+
// pass through unsupported types as is
|
|
108
|
+
return data as unknown as LanguageModelV3DataContent;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function convertV4ToolResultOutputToV3(
|
|
113
|
+
output: LanguageModelV4ToolResultOutput,
|
|
114
|
+
): LanguageModelV3ToolResultOutput {
|
|
115
|
+
if (output.type !== 'content') {
|
|
116
|
+
return output;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
...output,
|
|
121
|
+
value: output.value.map(part => {
|
|
122
|
+
if (part.type !== 'file') {
|
|
123
|
+
return part;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
switch (part.data.type) {
|
|
127
|
+
case 'data':
|
|
128
|
+
return {
|
|
129
|
+
type: 'file-data' as const,
|
|
130
|
+
data:
|
|
131
|
+
typeof part.data.data === 'string'
|
|
132
|
+
? part.data.data
|
|
133
|
+
: convertUint8ArrayToBase64(part.data.data),
|
|
134
|
+
mediaType: part.mediaType,
|
|
135
|
+
filename: part.filename,
|
|
136
|
+
providerOptions: part.providerOptions,
|
|
137
|
+
};
|
|
138
|
+
case 'url':
|
|
139
|
+
return {
|
|
140
|
+
type: 'file-url' as const,
|
|
141
|
+
url: part.data.url.toString(),
|
|
142
|
+
providerOptions: part.providerOptions,
|
|
143
|
+
};
|
|
144
|
+
case 'reference':
|
|
145
|
+
return {
|
|
146
|
+
type: 'file-id' as const,
|
|
147
|
+
fileId: part.data.reference,
|
|
148
|
+
providerOptions: part.providerOptions,
|
|
149
|
+
};
|
|
150
|
+
case 'text':
|
|
151
|
+
return part;
|
|
152
|
+
}
|
|
153
|
+
}),
|
|
154
|
+
} as LanguageModelV3ToolResultOutput;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function convertV3ContentToV4(
|
|
158
|
+
content: LanguageModelV3Content,
|
|
159
|
+
): LanguageModelV4Content {
|
|
160
|
+
return content.type === 'file'
|
|
161
|
+
? {
|
|
162
|
+
...content,
|
|
163
|
+
data: { type: 'data', data: content.data },
|
|
164
|
+
}
|
|
165
|
+
: content;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function convertV3StreamToV4(
|
|
169
|
+
stream: ReadableStream<LanguageModelV3StreamPart>,
|
|
170
|
+
): ReadableStream<LanguageModelV4StreamPart> {
|
|
171
|
+
return stream.pipeThrough(
|
|
172
|
+
new TransformStream<LanguageModelV3StreamPart, LanguageModelV4StreamPart>({
|
|
173
|
+
transform(chunk, controller) {
|
|
174
|
+
controller.enqueue(
|
|
175
|
+
chunk.type === 'file'
|
|
176
|
+
? {
|
|
177
|
+
...chunk,
|
|
178
|
+
data: { type: 'data', data: chunk.data },
|
|
179
|
+
}
|
|
180
|
+
: chunk,
|
|
181
|
+
);
|
|
182
|
+
},
|
|
183
|
+
}),
|
|
184
|
+
);
|
|
185
|
+
}
|