ai 7.0.166 → 7.0.169
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 +37 -0
- package/dist/index.d.ts +51 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +747 -513
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +1 -0
- package/dist/internal/index.d.ts.map +1 -1
- package/dist/internal/index.js +90 -10
- package/dist/internal/index.js.map +1 -1
- package/docs/02-foundations/02-providers-and-models.mdx +2 -0
- package/docs/03-ai-sdk-core/16-mcp-tools.mdx +13 -0
- package/docs/03-ai-sdk-core/60-telemetry.mdx +9 -0
- package/docs/03-ai-sdk-harnesses/02-harness-agent.mdx +4 -0
- package/docs/03-ai-sdk-harnesses/06-workflow-utilities.mdx +5 -0
- package/docs/04-ai-sdk-ui/02-chatbot.mdx +58 -0
- package/docs/06-advanced/11-secure-url-fetching.mdx +12 -0
- package/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx +24 -0
- package/package.json +12 -12
- package/src/prompt/content-part.ts +1 -1
- package/src/prompt/message.ts +1 -1
- package/src/realtime/validate-realtime-setup.ts +23 -19
- package/src/telemetry/create-telemetry-dispatcher.ts +30 -3
- package/src/telemetry/redact-telemetry-credentials.ts +80 -0
- package/src/telemetry/tracing-channel-publisher.ts +7 -4
- package/src/types/citation.ts +7 -7
- package/src/types/json-value.ts +1 -1
- package/src/types/provider-metadata.ts +1 -1
- package/src/ui/chat.ts +313 -7
- package/src/ui/validate-ui-messages.ts +157 -155
- package/src/ui-message-stream/to-ui-message-stream.ts +27 -10
- package/src/ui-message-stream/ui-message-chunks.ts +66 -63
- package/src/util/classic-zod.ts +37 -0
- package/src/util/zod.ts +25 -10
|
@@ -173,6 +173,7 @@ Here are the capabilities of popular models:
|
|
|
173
173
|
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-sonnet-5-5` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
174
174
|
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-sonnet-5` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
175
175
|
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-haiku-5-5` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
176
|
+
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-fable-5-2` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
176
177
|
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-fable-5-1` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
177
178
|
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-fable-5` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
178
179
|
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-opus-4-8` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
@@ -187,6 +188,7 @@ Here are the capabilities of popular models:
|
|
|
187
188
|
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-sonnet-4-0` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
188
189
|
| [Google](/providers/ai-sdk-providers/google) | `gemini-nano-banana-2.1` | <Check /> | <Cross /> | <Cross /> | <Cross /> |
|
|
189
190
|
| [Google](/providers/ai-sdk-providers/google) | `gemini-4-argon` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
191
|
+
| [Google](/providers/ai-sdk-providers/google) | `gemini-4-carbon` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
190
192
|
| [Google](/providers/ai-sdk-providers/google) | `gemini-3.8-flash` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
191
193
|
| [Google](/providers/ai-sdk-providers/google) | `gemini-3.7-flash` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
192
194
|
| [Google](/providers/ai-sdk-providers/google) | `gemini-3.6-flash` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
@@ -290,6 +290,19 @@ This hook is called before the SDK fetches authorization server metadata, so a
|
|
|
290
290
|
rejected URL is not requested. The hook is an optional additional restriction;
|
|
291
291
|
it does not replace the credential binding required for confidential clients.
|
|
292
292
|
|
|
293
|
+
On Node.js, OAuth discovery validates and pins DNS results for untrusted
|
|
294
|
+
URLs. On Bun, Deno, and other server runtimes without this transport, discovery
|
|
295
|
+
blocks untrusted authorization-server URLs, redirects, token endpoints, and
|
|
296
|
+
registration endpoints unless you supply a
|
|
297
|
+
custom `fetch` with equivalent connect-time address validation and DNS pinning.
|
|
298
|
+
Passing global `fetch` explicitly does not opt out of this requirement. The
|
|
299
|
+
configured MCP server origin remains supported, including local endpoints;
|
|
300
|
+
response-supplied authorization-server hosts are not automatically trusted.
|
|
301
|
+
Browser window discovery remains supported. Browser Web Workers and Service
|
|
302
|
+
Workers without a DOM follow the strict custom-fetch requirement as well;
|
|
303
|
+
worker globals alone cannot establish that the runtime is a browser rather
|
|
304
|
+
than a server worker.
|
|
305
|
+
|
|
293
306
|
### OAuth Callback Issuer Validation
|
|
294
307
|
|
|
295
308
|
When completing an OAuth authorization callback, pass the callback's `iss`
|
|
@@ -81,6 +81,15 @@ are also omitted in that case because they can contain both inputs and outputs.
|
|
|
81
81
|
Tracing span errors use a generic failure message.
|
|
82
82
|
Application lifecycle callbacks and returned results retain their full content.
|
|
83
83
|
|
|
84
|
+
OpenAI Code Interpreter and shell domain secrets, and MCP authorization and
|
|
85
|
+
header values, are redacted from tool definitions and object request/response
|
|
86
|
+
bodies before they reach telemetry integrations or tracing-channel subscribers,
|
|
87
|
+
including when recording is enabled. Redaction uses telemetry-only copies;
|
|
88
|
+
provider requests and application results retain their original values.
|
|
89
|
+
The OpenAI provider supplies these rules, so update `@ai-sdk/openai` together
|
|
90
|
+
with `ai` to use this protection. Older provider versions do not supply these
|
|
91
|
+
telemetry views.
|
|
92
|
+
|
|
84
93
|
For `generateSpeech`, input telemetry contains the source text and output
|
|
85
94
|
telemetry contains the generated audio byte length, media type, and format.
|
|
86
95
|
For `transcribe` and `experimental_streamTranscribe`, telemetry contains the
|
|
@@ -458,6 +458,10 @@ new prompt. The Vercel resume function reattaches whether the sandbox is still
|
|
|
458
458
|
running after `detach()` or stopped with a restorable snapshot. After
|
|
459
459
|
`sandboxSession.destroy()` deletes it, that ID cannot be resumed.
|
|
460
460
|
|
|
461
|
+
Treat resume state as a secret. For sandbox bridge adapters, it contains the
|
|
462
|
+
private key that authorizes reconnecting to the running bridge. Store it on the
|
|
463
|
+
server like a credential, and never send it to the browser or another client.
|
|
464
|
+
|
|
461
465
|
## Continue a Suspended Turn
|
|
462
466
|
|
|
463
467
|
For advanced workflows that must hand off an active turn across a process
|
|
@@ -339,6 +339,11 @@ step, including `continueFrom`, for the duration of the current workflow run.
|
|
|
339
339
|
To continue the native harness session across separate user-turn workflow runs,
|
|
340
340
|
persist the opaque `resumeFrom` state by `sessionId`.
|
|
341
341
|
|
|
342
|
+
For sandbox bridge adapters, `resumeFrom` and `continueFrom` contain the private
|
|
343
|
+
key that authorizes reconnecting to the running bridge. Workflow stores this key
|
|
344
|
+
with the step state, so treat your Workflow storage and any resume storage as
|
|
345
|
+
sensitive.
|
|
346
|
+
|
|
342
347
|
The storage implementation is the same for semantic agent steps and time
|
|
343
348
|
slices. This example uses Workflow steps because filesystem access must stay out
|
|
344
349
|
of the workflow function itself. Use durable storage instead of local files in
|
|
@@ -351,6 +351,64 @@ Stream processing and event callbacks remain immediate, and the latest messages
|
|
|
351
351
|
</Tab>
|
|
352
352
|
</Tabs>
|
|
353
353
|
|
|
354
|
+
### Queueing Messages
|
|
355
|
+
|
|
356
|
+
<Note>Message queueing is experimental.</Note>
|
|
357
|
+
|
|
358
|
+
By default, the input is disabled while a response is in progress. With `experimental_queue`, users can keep typing: messages sent while a response is in progress wait in `experimental_queuedMessages` and are sent one at a time after each response finishes.
|
|
359
|
+
|
|
360
|
+
```tsx filename="page.tsx"
|
|
361
|
+
const {
|
|
362
|
+
messages,
|
|
363
|
+
experimental_queuedMessages,
|
|
364
|
+
sendMessage,
|
|
365
|
+
experimental_removeQueuedMessage,
|
|
366
|
+
experimental_sendQueuedMessageNow,
|
|
367
|
+
} = useChat({
|
|
368
|
+
experimental_queue: true,
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
return (
|
|
372
|
+
<>
|
|
373
|
+
{/* messages */}
|
|
374
|
+
|
|
375
|
+
{experimental_queuedMessages.map(message => (
|
|
376
|
+
<div key={message.id}>
|
|
377
|
+
{message.parts.map(part => (part.type === 'text' ? part.text : ''))}
|
|
378
|
+
<button onClick={() => experimental_sendQueuedMessageNow(message.id)}>
|
|
379
|
+
Send now
|
|
380
|
+
</button>
|
|
381
|
+
<button onClick={() => experimental_removeQueuedMessage(message.id)}>
|
|
382
|
+
Remove
|
|
383
|
+
</button>
|
|
384
|
+
</div>
|
|
385
|
+
))}
|
|
386
|
+
|
|
387
|
+
<form
|
|
388
|
+
onSubmit={e => {
|
|
389
|
+
e.preventDefault();
|
|
390
|
+
sendMessage({ text: input });
|
|
391
|
+
setInput('');
|
|
392
|
+
}}
|
|
393
|
+
>
|
|
394
|
+
{/* the input stays enabled while a response is in progress */}
|
|
395
|
+
<input value={input} onChange={e => setInput(e.target.value)} />
|
|
396
|
+
</form>
|
|
397
|
+
</>
|
|
398
|
+
);
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
- Queued messages are sent in the order they were sent. A new message never skips the queue.
|
|
402
|
+
- The queue also waits while the assistant waits for the user, e.g. a tool call that needs an approval or a client-side tool result, and continues once the result is added. Automatic follow-ups from `sendAutomaticallyWhen` are sent first.
|
|
403
|
+
- An answered approval always reaches the model before the next queued message. Without `sendAutomaticallyWhen`, it is sent when the next message is sent.
|
|
404
|
+
- Stopping, an error or an interrupted response pauses the queue. It resumes when the next message is sent or when you call `experimental_sendQueuedMessageNow`.
|
|
405
|
+
- Editing a message or regenerating stops the response in progress and clears the queue, since the queued messages were sent after the part of the conversation that changes.
|
|
406
|
+
- To edit a queued message, remove it and send the new text. It is added to the end of the queue.
|
|
407
|
+
- `sendMessage` resolves once the message is queued. Use `experimental_queuedMessages` to see where it is.
|
|
408
|
+
- The queue lives in memory in the chat, so it is not saved and is lost on reload.
|
|
409
|
+
|
|
410
|
+
To add a message to the response that is in progress instead of waiting for it to finish, use [steering](/docs/agents/loop-control#steering) on the server.
|
|
411
|
+
|
|
354
412
|
## Event Callbacks
|
|
355
413
|
|
|
356
414
|
`useChat` provides optional event callbacks that you can use to handle different stages of the chatbot lifecycle:
|
|
@@ -114,6 +114,18 @@ validation and connection pinning. Other runtimes do not expose Node's
|
|
|
114
114
|
DNS/socket hooks, so server deployments on those runtimes should restrict
|
|
115
115
|
network egress to private, loopback, link-local, and cloud-metadata ranges.
|
|
116
116
|
|
|
117
|
+
MCP OAuth discovery requires a DNS-pinned transport for untrusted URLs on
|
|
118
|
+
server runtimes. On Bun, Deno, Workers, and framework edge runtimes, it blocks
|
|
119
|
+
untrusted authorization-server hosts and redirects unless you explicitly supply
|
|
120
|
+
a custom fetch with equivalent connect-time validation. The configured MCP
|
|
121
|
+
server origin and browser window discovery remain supported. Browser Web
|
|
122
|
+
Workers and Service Workers without a DOM also require an explicit custom
|
|
123
|
+
fetch under this strict policy: worker globals alone are not sufficient to
|
|
124
|
+
distinguish them from server workers. Direct callers of
|
|
125
|
+
`fetchWithValidatedEndpoint` can opt into this policy with
|
|
126
|
+
`requireDnsValidation: true`; other download helpers retain their existing
|
|
127
|
+
runtime behavior.
|
|
128
|
+
|
|
117
129
|
## Hardening your deployment
|
|
118
130
|
|
|
119
131
|
If your server fetches provider-supplied URLs and you want to close the DNS
|
|
@@ -279,6 +279,13 @@ Allows you to easily create a conversational user interface for your chatbot app
|
|
|
279
279
|
description:
|
|
280
280
|
'When provided, this function will be called when the stream is finished or a tool call is added to determine if the current messages should be resubmitted. You can use the lastAssistantMessageIsCompleteWithToolCalls helper for common scenarios.',
|
|
281
281
|
},
|
|
282
|
+
{
|
|
283
|
+
name: 'experimental_queue',
|
|
284
|
+
type: 'boolean',
|
|
285
|
+
isOptional: true,
|
|
286
|
+
description:
|
|
287
|
+
'Experimental. When true, messages sent while a response is in progress, or while the assistant waits for a tool result or approval, are queued and sent one at a time after each response finishes. Stopping, an error or an interrupted response pauses the queue until the next message is sent. Editing a message or regenerating clears the queue. Defaults to false.',
|
|
288
|
+
},
|
|
282
289
|
{
|
|
283
290
|
name: 'onFinish',
|
|
284
291
|
type: '(options: OnFinishOptions) => void',
|
|
@@ -461,6 +468,23 @@ Allows you to easily create a conversational user interface for your chatbot app
|
|
|
461
468
|
type: '() => void',
|
|
462
469
|
description: 'Clears the error state.',
|
|
463
470
|
},
|
|
471
|
+
{
|
|
472
|
+
name: 'experimental_queuedMessages',
|
|
473
|
+
type: 'UIMessage[]',
|
|
474
|
+
description:
|
|
475
|
+
'Experimental. Messages that wait to be sent when `experimental_queue` is enabled, in the order they will be sent.',
|
|
476
|
+
},
|
|
477
|
+
{
|
|
478
|
+
name: 'experimental_removeQueuedMessage',
|
|
479
|
+
type: '(id: string) => void',
|
|
480
|
+
description: 'Experimental. Removes a queued message so it is not sent.',
|
|
481
|
+
},
|
|
482
|
+
{
|
|
483
|
+
name: 'experimental_sendQueuedMessageNow',
|
|
484
|
+
type: '(id: string) => Promise<void>',
|
|
485
|
+
description:
|
|
486
|
+
'Experimental. Stops the response in progress and sends the queued message with the given id next. The rest of the queue is sent after it.',
|
|
487
|
+
},
|
|
464
488
|
{
|
|
465
489
|
name: 'resumeStream',
|
|
466
490
|
type: '() => void',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.169",
|
|
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.142",
|
|
46
46
|
"@ai-sdk/provider": "4.0.37",
|
|
47
|
-
"@ai-sdk/provider-utils": "5.0.
|
|
47
|
+
"@ai-sdk/provider-utils": "5.0.79"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@ai-sdk/amazon-bedrock": "5.0.
|
|
51
|
-
"@ai-sdk/deepseek": "3.0.
|
|
52
|
-
"@ai-sdk/google": "4.0.
|
|
53
|
-
"@ai-sdk/groq": "4.0.
|
|
54
|
-
"@ai-sdk/huggingface": "2.0.
|
|
55
|
-
"@ai-sdk/moonshotai": "3.0.
|
|
56
|
-
"@ai-sdk/openai": "4.0.
|
|
50
|
+
"@ai-sdk/amazon-bedrock": "5.0.138",
|
|
51
|
+
"@ai-sdk/deepseek": "3.0.84",
|
|
52
|
+
"@ai-sdk/google": "4.0.117",
|
|
53
|
+
"@ai-sdk/groq": "4.0.81",
|
|
54
|
+
"@ai-sdk/huggingface": "2.0.90",
|
|
55
|
+
"@ai-sdk/moonshotai": "3.0.90",
|
|
56
|
+
"@ai-sdk/openai": "4.0.114",
|
|
57
57
|
"@ai-sdk/test-server": "2.0.4",
|
|
58
|
-
"@ai-sdk/xai": "5.0.
|
|
58
|
+
"@ai-sdk/xai": "5.0.43",
|
|
59
59
|
"@edge-runtime/vm": "^5.0.0",
|
|
60
60
|
"@smithy/eventstream-codec": "^4.3.3",
|
|
61
61
|
"@smithy/util-utf8": "^4.3.3",
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"tsdown": "^0.23.0",
|
|
68
68
|
"tsx": "4.23.12",
|
|
69
69
|
"typescript": "6.0.3",
|
|
70
|
-
"zod": "
|
|
70
|
+
"zod": "4.6.7"
|
|
71
71
|
},
|
|
72
72
|
"peerDependencies": {
|
|
73
73
|
"zod": "^3.25.76 || ^4.1.8"
|
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
} from '@ai-sdk/provider-utils';
|
|
18
18
|
import { jsonValueSchema } from '../types/json-value';
|
|
19
19
|
import { providerMetadataSchema } from '../types/provider-metadata';
|
|
20
|
-
import { z, type ZodType } from '../util/zod';
|
|
20
|
+
import { z, type ZodType } from '../util/classic-zod';
|
|
21
21
|
|
|
22
22
|
const fileInlineDataSchema: ZodType<DataContent> = z.union([
|
|
23
23
|
z.string(),
|
package/src/prompt/message.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
UserModelMessage,
|
|
7
7
|
} from '@ai-sdk/provider-utils';
|
|
8
8
|
import { providerMetadataSchema } from '../types/provider-metadata';
|
|
9
|
-
import { z, type ZodType } from '../util/zod';
|
|
9
|
+
import { z, type ZodType } from '../util/classic-zod';
|
|
10
10
|
import {
|
|
11
11
|
customPartSchema,
|
|
12
12
|
filePartSchema,
|
|
@@ -2,29 +2,33 @@ import { z } from '../util/zod';
|
|
|
2
2
|
import type { RealtimeSetupResponse } from './realtime-types';
|
|
3
3
|
|
|
4
4
|
const setupSchema = z.object({
|
|
5
|
-
token: z.string().refine(value => value.trim().length > 0),
|
|
6
|
-
url: z.string().
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
5
|
+
token: z.string().check(z.refine(value => value.trim().length > 0)),
|
|
6
|
+
url: z.string().check(
|
|
7
|
+
z.refine(value => {
|
|
8
|
+
try {
|
|
9
|
+
const url = new URL(value);
|
|
10
|
+
return (
|
|
11
|
+
(url.protocol === 'ws:' || url.protocol === 'wss:') &&
|
|
12
|
+
url.hostname !== ''
|
|
13
|
+
);
|
|
14
|
+
} catch {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}),
|
|
18
|
+
),
|
|
19
|
+
expiresAt: z.optional(
|
|
20
|
+
z.number().check(z.positive()).check(z.maximum(Number.MAX_SAFE_INTEGER)),
|
|
21
|
+
),
|
|
22
|
+
tools: z.optional(
|
|
23
|
+
z.array(
|
|
20
24
|
z.object({
|
|
21
25
|
type: z.literal('function'),
|
|
22
|
-
name: z.string().
|
|
23
|
-
description: z.
|
|
26
|
+
name: z.string().check(z.minLength(1)),
|
|
27
|
+
description: z.optional(z.string()),
|
|
24
28
|
parameters: z.record(z.string(), z.unknown()),
|
|
25
29
|
}),
|
|
26
|
-
)
|
|
27
|
-
|
|
30
|
+
),
|
|
31
|
+
),
|
|
28
32
|
});
|
|
29
33
|
|
|
30
34
|
export function validateRealtimeSetup(payload: unknown): RealtimeSetupResponse {
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
import { getGlobalTelemetryIntegrations } from './telemetry-registry';
|
|
20
20
|
import type { TelemetryOptions } from './telemetry-options';
|
|
21
21
|
import { filterTelemetryEvent } from './filter-telemetry-event';
|
|
22
|
+
import { redactTelemetryCredentials } from './redact-telemetry-credentials';
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* The subset of `TelemetryDispatcher` keys whose values are Callback callbacks.
|
|
@@ -101,7 +102,9 @@ function augmentEvent<EVENT>(
|
|
|
101
102
|
}
|
|
102
103
|
}
|
|
103
104
|
|
|
104
|
-
return
|
|
105
|
+
return redactTelemetryCredentials(
|
|
106
|
+
filterTelemetryEvent(augmentedEvent, telemetry),
|
|
107
|
+
);
|
|
105
108
|
}
|
|
106
109
|
|
|
107
110
|
/**
|
|
@@ -275,8 +278,32 @@ export function createTelemetryDispatcher({
|
|
|
275
278
|
let wrappedExecute = execute;
|
|
276
279
|
for (const executeWrapper of executeLanguageModelCallWrappers) {
|
|
277
280
|
const innerExecute = wrappedExecute;
|
|
278
|
-
wrappedExecute = () =>
|
|
279
|
-
|
|
281
|
+
wrappedExecute = async () => {
|
|
282
|
+
const originals = new Map<unknown, unknown>();
|
|
283
|
+
const redact = <T>(original: T): T => {
|
|
284
|
+
const redacted = redactTelemetryCredentials(original);
|
|
285
|
+
if (redacted !== original) originals.set(redacted, original);
|
|
286
|
+
return redacted;
|
|
287
|
+
};
|
|
288
|
+
const restore = <T>(value: T): T =>
|
|
289
|
+
(originals.has(value) ? originals.get(value) : value) as T;
|
|
290
|
+
try {
|
|
291
|
+
return restore(
|
|
292
|
+
await executeWrapper({
|
|
293
|
+
...augmentedEvent,
|
|
294
|
+
execute: async () => {
|
|
295
|
+
try {
|
|
296
|
+
return redact(await innerExecute());
|
|
297
|
+
} catch (error) {
|
|
298
|
+
throw redact(error);
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
}),
|
|
302
|
+
);
|
|
303
|
+
} catch (error) {
|
|
304
|
+
throw restore(error);
|
|
305
|
+
}
|
|
306
|
+
};
|
|
280
307
|
}
|
|
281
308
|
|
|
282
309
|
return await runWithTracingChannelSpan(
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { redactTelemetryValue } from '@ai-sdk/provider-utils';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Apply provider-owned credential redaction on telemetry-only copies. Only visit SDK
|
|
5
|
+
* metadata containers, never arbitrary prompts, tool outputs, or user context.
|
|
6
|
+
*/
|
|
7
|
+
export function redactTelemetryCredentials<T>(value: T): T {
|
|
8
|
+
return replaceProperties(value, {
|
|
9
|
+
args: redactTelemetryValue,
|
|
10
|
+
tools: redactTools,
|
|
11
|
+
stepTools: redactTools,
|
|
12
|
+
request: redactMetadata,
|
|
13
|
+
response: redactMetadata,
|
|
14
|
+
steps: steps => mapArray(steps, redactTelemetryCredentials),
|
|
15
|
+
finalStep: redactTelemetryCredentials,
|
|
16
|
+
error: redactTelemetryCredentials,
|
|
17
|
+
cause: redactTelemetryCredentials,
|
|
18
|
+
// RetryError exposes both the full attempt history and the final error.
|
|
19
|
+
errors: errors => mapArray(errors, redactTelemetryCredentials),
|
|
20
|
+
lastError: redactTelemetryCredentials,
|
|
21
|
+
// Provider errors carry the same request body outside request metadata.
|
|
22
|
+
requestBodyValues: redactBody,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function redactMetadata(value: unknown): unknown {
|
|
27
|
+
return Array.isArray(value)
|
|
28
|
+
? mapArray(value, redactMetadata)
|
|
29
|
+
: replaceProperties(value, { body: redactBody });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function redactBody(value: unknown): unknown {
|
|
33
|
+
// Gateways can forward standardized provider tool definitions in their body.
|
|
34
|
+
return replaceProperties(redactTelemetryValue(value), { tools: redactTools });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function redactTools(value: unknown): unknown {
|
|
38
|
+
if (Array.isArray(value)) return mapArray(value, redactTelemetryCredentials);
|
|
39
|
+
if (!isRecord(value)) return value;
|
|
40
|
+
return replaceProperties(
|
|
41
|
+
value,
|
|
42
|
+
Object.fromEntries(
|
|
43
|
+
Object.keys(value).map(key => [key, redactTelemetryCredentials]),
|
|
44
|
+
),
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
49
|
+
return value != null && typeof value === 'object' && !Array.isArray(value);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function mapArray(
|
|
53
|
+
value: unknown,
|
|
54
|
+
redact: (value: unknown) => unknown,
|
|
55
|
+
): unknown {
|
|
56
|
+
if (!Array.isArray(value)) return value;
|
|
57
|
+
const copy = value.map(redact);
|
|
58
|
+
return copy.some((entry, index) => entry !== value[index]) ? copy : value;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Preserve result getters, streams, and property descriptors without mutation. */
|
|
62
|
+
function replaceProperties<T>(
|
|
63
|
+
value: T,
|
|
64
|
+
redactors: Record<string, (value: unknown) => unknown>,
|
|
65
|
+
): T {
|
|
66
|
+
if (!isRecord(value)) return value;
|
|
67
|
+
let descriptors: PropertyDescriptorMap | undefined;
|
|
68
|
+
for (const [key, redact] of Object.entries(redactors)) {
|
|
69
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
70
|
+
if (descriptor == null || !('value' in descriptor)) continue;
|
|
71
|
+
const redacted = redact(descriptor.value);
|
|
72
|
+
if (redacted !== descriptor.value) {
|
|
73
|
+
descriptors ??= Object.getOwnPropertyDescriptors(value);
|
|
74
|
+
descriptors[key] = { ...descriptor, value: redacted };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return descriptors == null
|
|
78
|
+
? value
|
|
79
|
+
: Object.create(Object.getPrototypeOf(value), descriptors);
|
|
80
|
+
}
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
type TelemetryTracingChannelMessage,
|
|
6
6
|
} from './tracing-channel';
|
|
7
7
|
import { isNodeRuntime } from '../util/is-node-runtime';
|
|
8
|
+
import { redactTelemetryCredentials } from './redact-telemetry-credentials';
|
|
8
9
|
|
|
9
10
|
type DiagnosticsChannel = typeof diagnosticsChannelModule;
|
|
10
11
|
type AsyncHooks = typeof asyncHooksModule;
|
|
@@ -104,9 +105,11 @@ export async function runWithTracingChannelSpan<T>(
|
|
|
104
105
|
);
|
|
105
106
|
|
|
106
107
|
// tracePromise publishes its resolved value on the channel. Keep the
|
|
107
|
-
// application's result separately
|
|
108
|
+
// application's result separately from the redacted telemetry copy.
|
|
108
109
|
return recordResult
|
|
109
|
-
? executePromise
|
|
110
|
+
? executePromise.then(redactTelemetryCredentials, error => {
|
|
111
|
+
throw redactTelemetryCredentials(error);
|
|
112
|
+
})
|
|
110
113
|
: executePromise.then(
|
|
111
114
|
() => undefined,
|
|
112
115
|
() => {
|
|
@@ -200,13 +203,13 @@ export function openTelemetryChannelSpanContext({
|
|
|
200
203
|
|
|
201
204
|
if (error !== undefined) {
|
|
202
205
|
context.error = recordResult
|
|
203
|
-
? error
|
|
206
|
+
? redactTelemetryCredentials(error)
|
|
204
207
|
: new Error('AI SDK operation failed');
|
|
205
208
|
safePublish(() => tracingChannel.error.publish(context));
|
|
206
209
|
}
|
|
207
210
|
|
|
208
211
|
if (recordResult && result !== undefined) {
|
|
209
|
-
context.result = result;
|
|
212
|
+
context.result = redactTelemetryCredentials(result);
|
|
210
213
|
}
|
|
211
214
|
|
|
212
215
|
safePublish(() => tracingChannel.asyncEnd.publish(context));
|
package/src/types/citation.ts
CHANGED
|
@@ -12,8 +12,8 @@ export const citationSchema: ZodType<Citation> = z.object({
|
|
|
12
12
|
sourceType: z.literal('url'),
|
|
13
13
|
id: z.string(),
|
|
14
14
|
url: z.string(),
|
|
15
|
-
title: z.
|
|
16
|
-
providerMetadata:
|
|
15
|
+
title: z.optional(z.string()),
|
|
16
|
+
providerMetadata: z.optional(providerMetadataSchema),
|
|
17
17
|
}),
|
|
18
18
|
z.object({
|
|
19
19
|
type: z.literal('source'),
|
|
@@ -21,11 +21,11 @@ export const citationSchema: ZodType<Citation> = z.object({
|
|
|
21
21
|
id: z.string(),
|
|
22
22
|
mediaType: z.string(),
|
|
23
23
|
title: z.string(),
|
|
24
|
-
filename: z.
|
|
25
|
-
providerMetadata:
|
|
24
|
+
filename: z.optional(z.string()),
|
|
25
|
+
providerMetadata: z.optional(providerMetadataSchema),
|
|
26
26
|
}),
|
|
27
27
|
]),
|
|
28
|
-
startIndex: z.number().int().
|
|
29
|
-
endIndex: z.number().int().
|
|
30
|
-
citedText: z.
|
|
28
|
+
startIndex: z.optional(z.number().check(z.int()).check(z.nonnegative())),
|
|
29
|
+
endIndex: z.optional(z.number().check(z.int()).check(z.nonnegative())),
|
|
30
|
+
citedText: z.optional(z.string()),
|
|
31
31
|
});
|
package/src/types/json-value.ts
CHANGED