ai 7.0.177 → 7.0.179
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 +33 -0
- package/dist/default-stop-condition-wtuJTiTo.js +4213 -0
- package/dist/default-stop-condition-wtuJTiTo.js.map +1 -0
- package/dist/index-CM_8u8gB.d.ts +12107 -0
- package/dist/index-CM_8u8gB.d.ts.map +1 -0
- package/dist/index.d.ts +3 -11788
- package/dist/index.js +680 -4732
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +4 -3301
- package/dist/internal/index.d.ts.map +1 -1
- package/dist/internal/index.js +2 -3819
- package/dist/simulate-readable-stream-Bi5kQKbv.js +25 -0
- package/dist/simulate-readable-stream-Bi5kQKbv.js.map +1 -0
- package/dist/simulate-readable-stream-BuwR4IbD.d.ts +21 -0
- package/dist/simulate-readable-stream-BuwR4IbD.d.ts.map +1 -0
- package/dist/test/index.d.ts +2 -20
- package/dist/test/index.d.ts.map +1 -1
- package/dist/test/index.js +3 -23
- package/dist/test/index.js.map +1 -1
- package/docs/02-foundations/02-providers-and-models.mdx +1 -0
- package/docs/03-agents/06-policy-tool-approvals.mdx +27 -2
- package/docs/03-agents/06-tool-approvals.mdx +2 -1
- package/docs/03-ai-sdk-core/18-tools.mdx +7 -2
- package/docs/03-ai-sdk-core/33-extractions.mdx +21 -4
- package/docs/03-ai-sdk-core/36-realtime.mdx +3 -1
- package/docs/03-ai-sdk-core/36-transcription.mdx +2 -2
- package/docs/03-ai-sdk-core/37-speech.mdx +1 -0
- package/docs/03-ai-sdk-core/60-telemetry.mdx +52 -4
- package/docs/03-ai-sdk-core/65-lifecycle-callbacks.mdx +124 -2
- package/docs/07-reference/01-ai-sdk-core/14-extract.mdx +13 -4
- package/docs/07-reference/02-ai-sdk-ui/05-use-realtime.mdx +6 -2
- package/docs/07-reference/05-ai-sdk-errors/ai-extraction-refusal-error.mdx +33 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-extraction-unsupported-field-type-error.mdx +36 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-approval-signature-error.mdx +1 -0
- package/package.json +13 -13
- package/src/agent/tool-loop-agent-settings.ts +2 -1
- package/src/extract/extract-events.ts +135 -0
- package/src/extract/extract.ts +177 -74
- package/src/extract/index.ts +6 -0
- package/src/extract/restricted-telemetry-dispatcher.ts +97 -0
- package/src/generate-text/execute-tools-from-stream.ts +14 -8
- package/src/generate-text/generate-text.ts +26 -10
- package/src/generate-text/stream-language-model-call.ts +3 -0
- package/src/generate-text/stream-text.ts +11 -2
- package/src/realtime/browser-realtime-live-websocket.ts +23 -5
- package/src/realtime/realtime-session.ts +68 -29
- package/src/telemetry/create-telemetry-dispatcher.ts +12 -0
- package/src/telemetry/filter-telemetry-event.ts +2 -0
- package/src/telemetry/redact-telemetry-credentials.ts +13 -1
- package/src/telemetry/telemetry.ts +34 -0
- package/src/telemetry/tracing-channel.ts +2 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/internal/index.js.map +0 -1
package/src/extract/extract.ts
CHANGED
|
@@ -5,7 +5,9 @@ import {
|
|
|
5
5
|
type Experimental_ExtractionModelV4Field as ExtractionField,
|
|
6
6
|
} from '@ai-sdk/provider';
|
|
7
7
|
import {
|
|
8
|
+
createIdGenerator,
|
|
8
9
|
withUserAgentSuffix,
|
|
10
|
+
type Context,
|
|
9
11
|
type ProviderOptions,
|
|
10
12
|
} from '@ai-sdk/provider-utils';
|
|
11
13
|
import {
|
|
@@ -16,18 +18,32 @@ import { isRecord } from '../decide/validate-decision';
|
|
|
16
18
|
import { logWarnings } from '../logger/log-warnings';
|
|
17
19
|
import { resolveExtractionModel } from '../model/resolve-model';
|
|
18
20
|
import { prepareLanguageModelCallOptions } from '../prompt/prepare-language-model-call-options';
|
|
21
|
+
import type { TelemetryOptions } from '../telemetry/telemetry-options';
|
|
22
|
+
import type { Callback } from '../util/callback';
|
|
19
23
|
import type { DownloadFunction } from '../util/download/download-function';
|
|
24
|
+
import { notify } from '../util/notify';
|
|
20
25
|
import { prepareRetries } from '../util/prepare-retries';
|
|
21
26
|
import { VERSION } from '../version';
|
|
27
|
+
import type { ExtractEndEvent, ExtractStartEvent } from './extract-events';
|
|
22
28
|
import type { ExtractionModel, ExtractionResult } from './extraction-result';
|
|
29
|
+
import {
|
|
30
|
+
createRestrictedTelemetryDispatcher,
|
|
31
|
+
toTelemetryEvent,
|
|
32
|
+
} from './restricted-telemetry-dispatcher';
|
|
23
33
|
import {
|
|
24
34
|
validateExtractionAnswers,
|
|
25
35
|
validateExtractionInput,
|
|
26
36
|
} from './validate-extraction';
|
|
27
37
|
|
|
38
|
+
const originalGenerateCallId = createIdGenerator({
|
|
39
|
+
prefix: 'call',
|
|
40
|
+
size: 24,
|
|
41
|
+
});
|
|
42
|
+
|
|
28
43
|
/** Extract typed field values from one shared state. Experimental. */
|
|
29
44
|
export async function extract<
|
|
30
45
|
const FIELDS extends Record<string, ExtractionField>,
|
|
46
|
+
RUNTIME_CONTEXT extends Context = Context,
|
|
31
47
|
>({
|
|
32
48
|
model: modelArg,
|
|
33
49
|
state,
|
|
@@ -40,6 +56,11 @@ export async function extract<
|
|
|
40
56
|
experimental_download: download,
|
|
41
57
|
headers,
|
|
42
58
|
providerOptions = {},
|
|
59
|
+
telemetry,
|
|
60
|
+
runtimeContext = {} as RUNTIME_CONTEXT,
|
|
61
|
+
onStart,
|
|
62
|
+
onEnd,
|
|
63
|
+
_internal: { generateCallId = originalGenerateCallId } = {},
|
|
43
64
|
}: {
|
|
44
65
|
/** An extraction model instance or an ID resolved by the configured default provider. */
|
|
45
66
|
model: ExtractionModel;
|
|
@@ -61,6 +82,18 @@ export async function extract<
|
|
|
61
82
|
experimental_download?: DownloadFunction;
|
|
62
83
|
headers?: Record<string, string>;
|
|
63
84
|
providerOptions?: ProviderOptions;
|
|
85
|
+
/** Optional telemetry configuration. */
|
|
86
|
+
telemetry?: TelemetryOptions<RUNTIME_CONTEXT>;
|
|
87
|
+
/** User-defined runtime context. Treat runtime context as immutable. */
|
|
88
|
+
runtimeContext?: RUNTIME_CONTEXT;
|
|
89
|
+
/** Called when the extract operation begins. */
|
|
90
|
+
onStart?: Callback<ExtractStartEvent<RUNTIME_CONTEXT>>;
|
|
91
|
+
/** Called when the extract operation completes. */
|
|
92
|
+
onEnd?: Callback<ExtractEndEvent<RUNTIME_CONTEXT>>;
|
|
93
|
+
/** Internal. For test use only. May change without notice. */
|
|
94
|
+
_internal?: {
|
|
95
|
+
generateCallId?: () => string;
|
|
96
|
+
};
|
|
64
97
|
}): Promise<ExtractionResult<FIELDS>> {
|
|
65
98
|
const model = resolveExtractionModel(modelArg);
|
|
66
99
|
|
|
@@ -78,85 +111,155 @@ export async function extract<
|
|
|
78
111
|
}
|
|
79
112
|
}
|
|
80
113
|
|
|
81
|
-
const
|
|
82
|
-
const
|
|
83
|
-
|
|
114
|
+
const callId = generateCallId();
|
|
115
|
+
const { maxRetries: resolvedMaxRetries, retry } = prepareRetries({
|
|
116
|
+
maxRetries,
|
|
84
117
|
abortSignal,
|
|
85
|
-
download,
|
|
86
118
|
});
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
return await model.doExtract({
|
|
90
|
-
state: preparedState,
|
|
91
|
-
fields,
|
|
92
|
-
referenceDate,
|
|
93
|
-
maxOutputTokens,
|
|
94
|
-
reasoning,
|
|
95
|
-
abortSignal,
|
|
96
|
-
headers: withUserAgentSuffix(headers ?? {}, `ai/${VERSION}`),
|
|
97
|
-
providerOptions,
|
|
98
|
-
});
|
|
119
|
+
const telemetryDispatcher = createRestrictedTelemetryDispatcher({
|
|
120
|
+
telemetry,
|
|
99
121
|
});
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
) {
|
|
112
|
-
const normalized = { ...answer, value: null };
|
|
113
|
-
if (normalized.type === 'list') delete normalized.items;
|
|
114
|
-
return [id, normalized];
|
|
115
|
-
}
|
|
116
|
-
return [id, answer];
|
|
117
|
-
}),
|
|
118
|
-
)
|
|
119
|
-
: result.answers;
|
|
120
|
-
validateExtractionAnswers({
|
|
122
|
+
const runInTracingChannelSpan =
|
|
123
|
+
telemetryDispatcher.runInTracingChannelSpan ??
|
|
124
|
+
(async <T>({ execute }: { execute: () => PromiseLike<T> }) =>
|
|
125
|
+
await execute());
|
|
126
|
+
const startEvent = {
|
|
127
|
+
callId,
|
|
128
|
+
operationId: 'ai.extract' as const,
|
|
129
|
+
runtimeContext,
|
|
130
|
+
provider: model.provider,
|
|
131
|
+
modelId: model.modelId,
|
|
132
|
+
state,
|
|
121
133
|
fields,
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
134
|
+
referenceDate,
|
|
135
|
+
maxOutputTokens,
|
|
136
|
+
reasoning,
|
|
137
|
+
maxRetries: resolvedMaxRetries,
|
|
138
|
+
headers,
|
|
139
|
+
providerOptions,
|
|
140
|
+
};
|
|
141
|
+
return await runInTracingChannelSpan({
|
|
142
|
+
type: 'experimental_extract',
|
|
143
|
+
event: toTelemetryEvent(startEvent),
|
|
144
|
+
execute: async () => {
|
|
145
|
+
await notify({
|
|
146
|
+
event: startEvent,
|
|
147
|
+
callbacks: [onStart, telemetryDispatcher.onStart],
|
|
148
|
+
});
|
|
135
149
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
150
|
+
try {
|
|
151
|
+
const preparedState = await prepareDecisionState({
|
|
152
|
+
state,
|
|
153
|
+
abortSignal,
|
|
154
|
+
download,
|
|
155
|
+
});
|
|
156
|
+
const modelCallEvent = {
|
|
157
|
+
callId,
|
|
158
|
+
operationId: 'ai.extract.doExtract' as const,
|
|
159
|
+
provider: model.provider,
|
|
160
|
+
modelId: model.modelId,
|
|
161
|
+
state: preparedState,
|
|
162
|
+
fields,
|
|
163
|
+
referenceDate,
|
|
164
|
+
};
|
|
165
|
+
await notify({
|
|
166
|
+
event: modelCallEvent,
|
|
167
|
+
callbacks: [
|
|
168
|
+
telemetryDispatcher.experimental_onExtractionModelCallStart,
|
|
169
|
+
],
|
|
170
|
+
});
|
|
171
|
+
const result = await retry(async () => {
|
|
172
|
+
abortSignal?.throwIfAborted();
|
|
173
|
+
return await model.doExtract({
|
|
174
|
+
state: preparedState,
|
|
175
|
+
fields,
|
|
176
|
+
referenceDate,
|
|
177
|
+
maxOutputTokens,
|
|
178
|
+
reasoning,
|
|
179
|
+
abortSignal,
|
|
180
|
+
headers: withUserAgentSuffix(headers ?? {}, `ai/${VERSION}`),
|
|
181
|
+
providerOptions,
|
|
182
|
+
});
|
|
183
|
+
});
|
|
141
184
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
185
|
+
abortSignal?.throwIfAborted();
|
|
186
|
+
const answers = isRecord(result.answers)
|
|
187
|
+
? Object.fromEntries(
|
|
188
|
+
Object.entries(result.answers).map(([id, answer]) => {
|
|
189
|
+
if (!isRecord(answer) || answer.type === 'refusal') {
|
|
190
|
+
return [id, answer];
|
|
191
|
+
}
|
|
192
|
+
if (
|
|
193
|
+
answer.value === '' ||
|
|
194
|
+
(Array.isArray(answer.value) && answer.value.length === 0)
|
|
195
|
+
) {
|
|
196
|
+
const normalized = { ...answer, value: null };
|
|
197
|
+
if (normalized.type === 'list') delete normalized.items;
|
|
198
|
+
return [id, normalized];
|
|
199
|
+
}
|
|
200
|
+
return [id, answer];
|
|
201
|
+
}),
|
|
202
|
+
)
|
|
203
|
+
: result.answers;
|
|
204
|
+
validateExtractionAnswers({
|
|
205
|
+
fields,
|
|
206
|
+
state: preparedState,
|
|
207
|
+
answers,
|
|
208
|
+
});
|
|
209
|
+
const refusedFieldIds = Object.keys(fields).filter(
|
|
210
|
+
id => answers[id]?.type === 'refusal',
|
|
211
|
+
);
|
|
212
|
+
if (refusedFieldIds.length > 0) {
|
|
213
|
+
throw new ExtractionRefusalError({
|
|
214
|
+
fieldIds: refusedFieldIds,
|
|
215
|
+
provider: model.provider,
|
|
216
|
+
modelId: model.modelId,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
await notify({
|
|
220
|
+
event: { ...modelCallEvent, ...result, answers },
|
|
221
|
+
callbacks: [
|
|
222
|
+
telemetryDispatcher.experimental_onExtractionModelCallEnd,
|
|
223
|
+
],
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
logWarnings({
|
|
227
|
+
warnings: result.warnings,
|
|
228
|
+
provider: model.provider,
|
|
229
|
+
model: model.modelId,
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
const inputTokens = result.usage?.inputTokens;
|
|
233
|
+
const outputTokens = result.usage?.outputTokens;
|
|
234
|
+
const extractionResult: ExtractionResult<FIELDS> = {
|
|
235
|
+
answers: answers as ExtractionResult<FIELDS>['answers'],
|
|
236
|
+
usage: {
|
|
237
|
+
inputTokens,
|
|
238
|
+
outputTokens,
|
|
239
|
+
totalTokens:
|
|
240
|
+
inputTokens != null && outputTokens != null
|
|
241
|
+
? inputTokens + outputTokens
|
|
242
|
+
: undefined,
|
|
243
|
+
},
|
|
244
|
+
warnings: result.warnings,
|
|
245
|
+
providerMetadata: result.providerMetadata,
|
|
246
|
+
response: {
|
|
247
|
+
...result.response,
|
|
248
|
+
timestamp: result.response?.timestamp ?? new Date(),
|
|
249
|
+
modelId: result.response?.modelId ?? model.modelId,
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
await notify({
|
|
254
|
+
event: { ...startEvent, ...extractionResult },
|
|
255
|
+
callbacks: [onEnd, telemetryDispatcher.onEnd],
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
return extractionResult;
|
|
259
|
+
} catch (error) {
|
|
260
|
+
await telemetryDispatcher.onError?.({ callId, error });
|
|
261
|
+
throw error;
|
|
262
|
+
}
|
|
160
263
|
},
|
|
161
|
-
};
|
|
264
|
+
});
|
|
162
265
|
}
|
package/src/extract/index.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
export { extract as experimental_extract } from './extract';
|
|
2
|
+
export type {
|
|
3
|
+
ExtractStartEvent as Experimental_ExtractStartEvent,
|
|
4
|
+
ExtractEndEvent as Experimental_ExtractEndEvent,
|
|
5
|
+
ExtractionModelCallStartEvent as Experimental_ExtractionModelCallStartEvent,
|
|
6
|
+
ExtractionModelCallEndEvent as Experimental_ExtractionModelCallEndEvent,
|
|
7
|
+
} from './extract-events';
|
|
2
8
|
export type {
|
|
3
9
|
ExtractionAnswer as Experimental_ExtractionAnswer,
|
|
4
10
|
ExtractionModel as Experimental_ExtractionModel,
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { isProviderReference, type Context } from '@ai-sdk/provider-utils';
|
|
2
|
+
import { createTelemetryDispatcher } from '../telemetry/create-telemetry-dispatcher';
|
|
3
|
+
import { filterIncludedContext } from '../telemetry/filter-included-context';
|
|
4
|
+
import type { TelemetryDispatcher } from '../telemetry/telemetry';
|
|
5
|
+
import type { TelemetryOptions } from '../telemetry/telemetry-options';
|
|
6
|
+
import type { Callback } from '../util/callback';
|
|
7
|
+
import type { ExtractEndEvent, ExtractStartEvent } from './extract-events';
|
|
8
|
+
|
|
9
|
+
export function toTelemetryEvent<
|
|
10
|
+
EVENT extends Pick<
|
|
11
|
+
ExtractStartEvent,
|
|
12
|
+
'headers' | 'providerOptions' | 'state'
|
|
13
|
+
>,
|
|
14
|
+
>(event: EVENT): EVENT {
|
|
15
|
+
return {
|
|
16
|
+
...event,
|
|
17
|
+
headers: undefined,
|
|
18
|
+
providerOptions: {},
|
|
19
|
+
state: redactFileLocations(event.state),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function redactFileLocations<STATE>(state: STATE): STATE {
|
|
24
|
+
if (!Array.isArray(state)) return state;
|
|
25
|
+
return state.map(part =>
|
|
26
|
+
part?.type === 'file' && isFileLocation(part.data)
|
|
27
|
+
? { ...part, data: '[redacted]' }
|
|
28
|
+
: part,
|
|
29
|
+
) as STATE;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function isFileLocation(data: unknown): boolean {
|
|
33
|
+
if (data instanceof URL) return true;
|
|
34
|
+
if (typeof data === 'string') {
|
|
35
|
+
try {
|
|
36
|
+
return new URL(data).protocol !== 'data:';
|
|
37
|
+
} catch {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return (
|
|
42
|
+
isProviderReference(data) ||
|
|
43
|
+
(data != null &&
|
|
44
|
+
typeof data === 'object' &&
|
|
45
|
+
'type' in data &&
|
|
46
|
+
(data.type === 'url' || data.type === 'reference'))
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function createRestrictedTelemetryDispatcher<
|
|
51
|
+
RUNTIME_CONTEXT extends Context,
|
|
52
|
+
>({
|
|
53
|
+
telemetry,
|
|
54
|
+
}: {
|
|
55
|
+
telemetry?: TelemetryOptions<RUNTIME_CONTEXT>;
|
|
56
|
+
}): Omit<
|
|
57
|
+
TelemetryDispatcher,
|
|
58
|
+
| 'onStart'
|
|
59
|
+
| 'onEnd'
|
|
60
|
+
| 'experimental_onExtractStart'
|
|
61
|
+
| 'experimental_onExtractEnd'
|
|
62
|
+
> & {
|
|
63
|
+
onStart: Callback<ExtractStartEvent<RUNTIME_CONTEXT>>;
|
|
64
|
+
onEnd: Callback<ExtractEndEvent<RUNTIME_CONTEXT>>;
|
|
65
|
+
} {
|
|
66
|
+
const dispatcher = createTelemetryDispatcher({ telemetry });
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
...dispatcher,
|
|
70
|
+
experimental_onExtractionModelCallStart: event =>
|
|
71
|
+
dispatcher.experimental_onExtractionModelCallStart?.({
|
|
72
|
+
...event,
|
|
73
|
+
state: redactFileLocations(event.state),
|
|
74
|
+
}),
|
|
75
|
+
experimental_onExtractionModelCallEnd: event =>
|
|
76
|
+
dispatcher.experimental_onExtractionModelCallEnd?.({
|
|
77
|
+
...event,
|
|
78
|
+
state: redactFileLocations(event.state),
|
|
79
|
+
}),
|
|
80
|
+
onStart: event =>
|
|
81
|
+
dispatcher.experimental_onExtractStart?.({
|
|
82
|
+
...toTelemetryEvent(event),
|
|
83
|
+
runtimeContext: filterIncludedContext({
|
|
84
|
+
context: event.runtimeContext,
|
|
85
|
+
includeContext: telemetry?.includeRuntimeContext,
|
|
86
|
+
}),
|
|
87
|
+
}),
|
|
88
|
+
onEnd: event =>
|
|
89
|
+
dispatcher.experimental_onExtractEnd?.({
|
|
90
|
+
...toTelemetryEvent(event),
|
|
91
|
+
runtimeContext: filterIncludedContext({
|
|
92
|
+
context: event.runtimeContext,
|
|
93
|
+
includeContext: telemetry?.includeRuntimeContext,
|
|
94
|
+
}),
|
|
95
|
+
}),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
@@ -146,13 +146,20 @@ export function executeToolsFromStream<
|
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
const approvalId = generateId();
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
149
|
+
|
|
150
|
+
// A signature lets a later request execute this tool call by
|
|
151
|
+
// replaying the approval from client-supplied history, so a denied
|
|
152
|
+
// tool call must not get one.
|
|
153
|
+
const signature =
|
|
154
|
+
toolApprovalStatus.type === 'denied'
|
|
155
|
+
? undefined
|
|
156
|
+
: await maybeSignApproval({
|
|
157
|
+
secret: toolApprovalSecret,
|
|
158
|
+
approvalId,
|
|
159
|
+
toolCallId: chunk.toolCallId,
|
|
160
|
+
toolName: chunk.toolName,
|
|
161
|
+
input: chunk.input,
|
|
162
|
+
});
|
|
156
163
|
|
|
157
164
|
switch (toolApprovalStatus.type) {
|
|
158
165
|
case 'user-approval': {
|
|
@@ -175,7 +182,6 @@ export function executeToolsFromStream<
|
|
|
175
182
|
approvalId,
|
|
176
183
|
toolCall: chunk,
|
|
177
184
|
isAutomatic: true,
|
|
178
|
-
...(signature != null ? { signature } : {}),
|
|
179
185
|
});
|
|
180
186
|
controller.enqueue({
|
|
181
187
|
type: 'tool-approval-response',
|
|
@@ -440,8 +440,9 @@ async function generateTextInternal<
|
|
|
440
440
|
|
|
441
441
|
/**
|
|
442
442
|
* Secret for HMAC-signing tool approval requests. When set, the server
|
|
443
|
-
* signs
|
|
443
|
+
* signs approval requests at issuance and verifies the signature when
|
|
444
444
|
* the approval is replayed, preventing client-forged approvals.
|
|
445
|
+
* Approval requests for automatically denied tool calls are not signed.
|
|
445
446
|
*/
|
|
446
447
|
experimental_toolApprovalSecret?: string | Uint8Array;
|
|
447
448
|
|
|
@@ -1147,8 +1148,17 @@ async function generateTextInternal<
|
|
|
1147
1148
|
const stepExecutionTools =
|
|
1148
1149
|
stepToolChoice.type === 'none' ? undefined : executionTools;
|
|
1149
1150
|
|
|
1151
|
+
// The messages array of a step is created for that step.
|
|
1152
|
+
// `prepareStep` can return the same array for several steps
|
|
1153
|
+
// or calls, so its messages are copied. Tool approval
|
|
1154
|
+
// functions can then tell the tool calls of one response
|
|
1155
|
+
// from the tool calls of another response by the array they
|
|
1156
|
+
// receive.
|
|
1150
1157
|
const stepMessages = appendToolCallerMessages({
|
|
1151
|
-
messages:
|
|
1158
|
+
messages:
|
|
1159
|
+
prepareStepResult?.messages != null
|
|
1160
|
+
? [...prepareStepResult.messages]
|
|
1161
|
+
: stepInputMessages,
|
|
1152
1162
|
toolCallerMessages,
|
|
1153
1163
|
});
|
|
1154
1164
|
|
|
@@ -1511,13 +1521,20 @@ async function generateTextInternal<
|
|
|
1511
1521
|
}
|
|
1512
1522
|
|
|
1513
1523
|
const approvalId = generateId();
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1524
|
+
|
|
1525
|
+
// A signature lets a later request execute this tool call by
|
|
1526
|
+
// replaying the approval from client-supplied history, so a
|
|
1527
|
+
// denied tool call must not get one.
|
|
1528
|
+
const signature =
|
|
1529
|
+
toolApprovalStatus.type === 'denied'
|
|
1530
|
+
? undefined
|
|
1531
|
+
: await maybeSignApproval({
|
|
1532
|
+
secret: experimental_toolApprovalSecret,
|
|
1533
|
+
approvalId,
|
|
1534
|
+
toolCallId: toolCall.toolCallId,
|
|
1535
|
+
toolName: toolCall.toolName,
|
|
1536
|
+
input: toolCall.input,
|
|
1537
|
+
});
|
|
1521
1538
|
|
|
1522
1539
|
switch (toolApprovalStatus.type) {
|
|
1523
1540
|
case 'user-approval': {
|
|
@@ -1559,7 +1576,6 @@ async function generateTextInternal<
|
|
|
1559
1576
|
approvalId,
|
|
1560
1577
|
toolCall,
|
|
1561
1578
|
isAutomatic: true,
|
|
1562
|
-
...(signature != null ? { signature } : {}),
|
|
1563
1579
|
};
|
|
1564
1580
|
stepToolApprovalResponses[toolCall.toolCallId] = {
|
|
1565
1581
|
type: 'tool-approval-response',
|
|
@@ -849,6 +849,9 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform<
|
|
|
849
849
|
output: chunk.result,
|
|
850
850
|
providerExecuted: true,
|
|
851
851
|
dynamic: chunk.dynamic,
|
|
852
|
+
...(chunk.preliminary != null
|
|
853
|
+
? { preliminary: chunk.preliminary }
|
|
854
|
+
: {}),
|
|
852
855
|
...(chunk.providerMetadata != null
|
|
853
856
|
? { providerMetadata: chunk.providerMetadata }
|
|
854
857
|
: {}),
|
|
@@ -634,8 +634,9 @@ function streamTextInternal<
|
|
|
634
634
|
|
|
635
635
|
/**
|
|
636
636
|
* Secret for HMAC-signing tool approval requests. When set, the server
|
|
637
|
-
* signs
|
|
637
|
+
* signs approval requests at issuance and verifies the signature when
|
|
638
638
|
* the approval is replayed, preventing client-forged approvals.
|
|
639
|
+
* Approval requests for automatically denied tool calls are not signed.
|
|
639
640
|
*/
|
|
640
641
|
experimental_toolApprovalSecret?: string | Uint8Array;
|
|
641
642
|
|
|
@@ -2699,8 +2700,16 @@ class DefaultStreamTextResult<
|
|
|
2699
2700
|
const stepExecutionTools =
|
|
2700
2701
|
stepToolChoice.type === 'none' ? undefined : executionTools;
|
|
2701
2702
|
|
|
2703
|
+
// The messages array of a step is created for that step.
|
|
2704
|
+
// `prepareStep` can return the same array for several steps or
|
|
2705
|
+
// calls, so its messages are copied. Tool approval functions can
|
|
2706
|
+
// then tell the tool calls of one response from the tool calls of
|
|
2707
|
+
// another response by the array they receive.
|
|
2702
2708
|
const stepMessages = appendToolCallerMessages({
|
|
2703
|
-
messages:
|
|
2709
|
+
messages:
|
|
2710
|
+
prepareStepResult?.messages != null
|
|
2711
|
+
? [...prepareStepResult.messages]
|
|
2712
|
+
: stepInputMessages,
|
|
2704
2713
|
toolCallerMessages,
|
|
2705
2714
|
});
|
|
2706
2715
|
currentStepMessages = stepMessages;
|
|
@@ -8,7 +8,7 @@ import type {
|
|
|
8
8
|
import { BrowserRealtimeAudio } from './browser-realtime-audio';
|
|
9
9
|
import { BrowserRealtimeTransport } from './browser-realtime-transport';
|
|
10
10
|
|
|
11
|
-
/** Continuous PCM16 audio-chunk media
|
|
11
|
+
/** Continuous PCM16 audio-chunk media over a WebSocket relay or client-secret connection. */
|
|
12
12
|
export class BrowserRealtimeLiveWebSocket {
|
|
13
13
|
private readonly transport: BrowserRealtimeTransport;
|
|
14
14
|
private readonly audio: BrowserRealtimeAudio;
|
|
@@ -84,9 +84,20 @@ export class BrowserRealtimeLiveWebSocket {
|
|
|
84
84
|
stream?: MediaStream;
|
|
85
85
|
capture?: boolean;
|
|
86
86
|
}): void {
|
|
87
|
+
const generation = this.prepare(options);
|
|
88
|
+
if (generation !== this.generation) return;
|
|
89
|
+
this.open({
|
|
90
|
+
mode: 'relay',
|
|
91
|
+
url: options.url,
|
|
92
|
+
protocols: options.protocols,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Resets media and primes playback; call while still in the user gesture. */
|
|
97
|
+
prepare(options: { stream?: MediaStream; capture?: boolean }): number {
|
|
87
98
|
const generation = this.generation + 1;
|
|
88
99
|
this.dispose();
|
|
89
|
-
if (generation !== this.generation) return;
|
|
100
|
+
if (generation !== this.generation) return generation;
|
|
90
101
|
for (const format of [
|
|
91
102
|
this.config.inputAudioFormat,
|
|
92
103
|
this.config.outputAudioFormat,
|
|
@@ -111,10 +122,17 @@ export class BrowserRealtimeLiveWebSocket {
|
|
|
111
122
|
void this.audio.resumePlayback().catch(error => {
|
|
112
123
|
if (generation === this.generation) this.options.onError(error);
|
|
113
124
|
});
|
|
125
|
+
return generation;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Opens the socket through an application relay or with a client secret. */
|
|
129
|
+
open(
|
|
130
|
+
connection:
|
|
131
|
+
| { mode: 'relay'; url: string; protocols?: string[] }
|
|
132
|
+
| { mode: 'client-secret'; url: string; token: string },
|
|
133
|
+
): void {
|
|
114
134
|
this.transport.connect({
|
|
115
|
-
|
|
116
|
-
url: options.url,
|
|
117
|
-
protocols: options.protocols,
|
|
135
|
+
...connection,
|
|
118
136
|
onOpen: () => {
|
|
119
137
|
return this.transport.sendEvent({
|
|
120
138
|
type: this.options.model.capabilities?.startup ?? 'session-update',
|