ai 7.0.5 → 7.0.7
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 +14 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +7 -5
- package/dist/index.js.map +1 -1
- package/dist/internal/index.js +1 -1
- package/docs/03-ai-sdk-core/40-middleware.mdx +15 -7
- package/docs/04-ai-sdk-ui/03-chatbot-message-persistence.mdx +11 -2
- package/docs/07-reference/01-ai-sdk-core/65-language-model-v2-middleware.mdx +2 -2
- package/package.json +2 -2
- package/src/agent/agent.ts +14 -0
- package/src/ui/convert-to-model-messages.ts +6 -4
package/dist/internal/index.js
CHANGED
|
@@ -271,15 +271,15 @@ Find more examples at this [link](https://github.com/minpeter/ai-sdk-tool-call-m
|
|
|
271
271
|
<Note>
|
|
272
272
|
Implementing language model middleware is advanced functionality and requires
|
|
273
273
|
a solid understanding of the [language model
|
|
274
|
-
specification](https://github.com/vercel/ai/blob/
|
|
274
|
+
specification](https://github.com/vercel/ai/blob/main/packages/provider/src/language-model/v4/language-model-v4.ts).
|
|
275
275
|
</Note>
|
|
276
276
|
|
|
277
277
|
You can implement any of the following three function to modify the behavior of the language model:
|
|
278
278
|
|
|
279
279
|
1. `transformParams`: Transforms the parameters before they are passed to the language model, for both `doGenerate` and `doStream`.
|
|
280
|
-
2. `wrapGenerate`: Wraps the `doGenerate` method of the [language model](https://github.com/vercel/ai/blob/
|
|
280
|
+
2. `wrapGenerate`: Wraps the `doGenerate` method of the [language model](https://github.com/vercel/ai/blob/main/packages/provider/src/language-model/v4/language-model-v4.ts).
|
|
281
281
|
You can modify the parameters, call the language model, and modify the result.
|
|
282
|
-
3. `wrapStream`: Wraps the `doStream` method of the [language model](https://github.com/vercel/ai/blob/
|
|
282
|
+
3. `wrapStream`: Wraps the `doStream` method of the [language model](https://github.com/vercel/ai/blob/main/packages/provider/src/language-model/v4/language-model-v4.ts).
|
|
283
283
|
You can modify the parameters, call the language model, and modify the result.
|
|
284
284
|
|
|
285
285
|
Here are some examples of how to implement language model middleware:
|
|
@@ -307,9 +307,13 @@ export const yourLogMiddleware: LanguageModelV4Middleware = {
|
|
|
307
307
|
console.log(`params: ${JSON.stringify(params, null, 2)}`);
|
|
308
308
|
|
|
309
309
|
const result = await doGenerate();
|
|
310
|
+
const generatedText = result.content
|
|
311
|
+
.filter(part => part.type === 'text')
|
|
312
|
+
.map(part => part.text)
|
|
313
|
+
.join('');
|
|
310
314
|
|
|
311
315
|
console.log('doGenerate finished');
|
|
312
|
-
console.log(`generated text: ${
|
|
316
|
+
console.log(`generated text: ${generatedText}`);
|
|
313
317
|
|
|
314
318
|
return result;
|
|
315
319
|
},
|
|
@@ -437,12 +441,16 @@ import type { LanguageModelV4Middleware } from '@ai-sdk/provider';
|
|
|
437
441
|
|
|
438
442
|
export const yourGuardrailMiddleware: LanguageModelV4Middleware = {
|
|
439
443
|
wrapGenerate: async ({ doGenerate }) => {
|
|
440
|
-
const
|
|
444
|
+
const result = await doGenerate();
|
|
441
445
|
|
|
442
446
|
// filtering approach, e.g. for PII or other sensitive information:
|
|
443
|
-
const
|
|
447
|
+
const content = result.content.map(part =>
|
|
448
|
+
part.type === 'text'
|
|
449
|
+
? { ...part, text: part.text.replace(/badword/g, '<REDACTED>') }
|
|
450
|
+
: part,
|
|
451
|
+
);
|
|
444
452
|
|
|
445
|
-
return {
|
|
453
|
+
return { ...result, content };
|
|
446
454
|
},
|
|
447
455
|
|
|
448
456
|
// here you would implement the guardrail logic for streaming
|
|
@@ -359,11 +359,20 @@ By default, message IDs are generated client-side:
|
|
|
359
359
|
- AI response message IDs are generated by `streamText` on the server
|
|
360
360
|
|
|
361
361
|
For applications without persistence, client-side ID generation works perfectly.
|
|
362
|
-
However, **for persistence, you
|
|
362
|
+
However, **for persistence, you should use IDs that are stable before messages
|
|
363
|
+
are stored** to ensure consistency across sessions and prevent ID conflicts when
|
|
364
|
+
messages are restored.
|
|
365
|
+
|
|
366
|
+
The server-side options below control IDs for generated assistant response
|
|
367
|
+
messages. User message IDs are created by `useChat` before the request is sent
|
|
368
|
+
to your API route, so keep those client-generated IDs when saving incoming
|
|
369
|
+
request messages, or generate and persist your own user message IDs before
|
|
370
|
+
sending/storing them.
|
|
363
371
|
|
|
364
372
|
### Setting Up Server-side ID Generation
|
|
365
373
|
|
|
366
|
-
When implementing persistence, you have two options for generating server-side
|
|
374
|
+
When implementing persistence, you have two options for generating server-side
|
|
375
|
+
IDs for assistant response messages:
|
|
367
376
|
|
|
368
377
|
1. **Using `generateMessageId` in `toUIMessageStream`**
|
|
369
378
|
2. **Setting IDs in your start message part with `createUIMessageStream`**
|
|
@@ -28,8 +28,8 @@ See [Language Model Middleware](/docs/ai-sdk-core/middleware) for more informati
|
|
|
28
28
|
content={[
|
|
29
29
|
{
|
|
30
30
|
name: 'specificationVersion',
|
|
31
|
-
type: "'
|
|
32
|
-
description: 'The specification version of the middleware. Must be "
|
|
31
|
+
type: "'v4'",
|
|
32
|
+
description: 'The specification version of the middleware. Must be "v4".',
|
|
33
33
|
},
|
|
34
34
|
{
|
|
35
35
|
name: 'transformParams',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.7",
|
|
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,7 +42,7 @@
|
|
|
42
42
|
}
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@ai-sdk/gateway": "4.0.
|
|
45
|
+
"@ai-sdk/gateway": "4.0.5",
|
|
46
46
|
"@ai-sdk/provider": "4.0.0",
|
|
47
47
|
"@ai-sdk/provider-utils": "5.0.1"
|
|
48
48
|
},
|
package/src/agent/agent.ts
CHANGED
|
@@ -106,11 +106,25 @@ export type AgentCallParameters<
|
|
|
106
106
|
*/
|
|
107
107
|
onToolExecutionStart?: OnToolExecutionStartCallback<TOOLS>;
|
|
108
108
|
|
|
109
|
+
/**
|
|
110
|
+
* Callback that is called before each tool execution begins.
|
|
111
|
+
*
|
|
112
|
+
* @deprecated Use `onToolExecutionStart` instead.
|
|
113
|
+
*/
|
|
114
|
+
experimental_onToolCallStart?: OnToolExecutionStartCallback<TOOLS>;
|
|
115
|
+
|
|
109
116
|
/**
|
|
110
117
|
* Callback that is called after each tool execution completes.
|
|
111
118
|
*/
|
|
112
119
|
onToolExecutionEnd?: OnToolExecutionEndCallback<TOOLS>;
|
|
113
120
|
|
|
121
|
+
/**
|
|
122
|
+
* Callback that is called after each tool execution completes.
|
|
123
|
+
*
|
|
124
|
+
* @deprecated Use `onToolExecutionEnd` instead.
|
|
125
|
+
*/
|
|
126
|
+
experimental_onToolCallFinish?: OnToolExecutionEndCallback<TOOLS>;
|
|
127
|
+
|
|
114
128
|
/**
|
|
115
129
|
* Callback that is called when each step (LLM call) ends, including intermediate steps.
|
|
116
130
|
*/
|
|
@@ -280,10 +280,12 @@ export async function convertToModelMessages<UI_MESSAGE extends UIMessage>(
|
|
|
280
280
|
}
|
|
281
281
|
}
|
|
282
282
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
283
|
+
if (content.length > 0) {
|
|
284
|
+
modelMessages.push({
|
|
285
|
+
role: 'assistant',
|
|
286
|
+
content,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
287
289
|
|
|
288
290
|
// check if there are tool invocations with results in the block
|
|
289
291
|
// Include non-provider-executed tools, OR provider-executed tools with approval responses
|