ai 7.0.97 → 7.0.98
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 +20 -0
- package/dist/index.d.ts +558 -472
- package/dist/index.js +1368 -1182
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +13 -5
- package/dist/internal/index.js +16 -14
- package/dist/internal/index.js.map +1 -1
- package/docs/03-ai-sdk-core/42-batch.mdx +48 -9
- package/docs/03-ai-sdk-core/60-telemetry.mdx +28 -5
- package/docs/04-ai-sdk-ui/03-chatbot-tool-usage.mdx +16 -1
- package/docs/07-reference/01-ai-sdk-core/05-embed.mdx +31 -5
- package/docs/07-reference/01-ai-sdk-core/06-embed-many.mdx +31 -5
- package/docs/07-reference/01-ai-sdk-core/06-rerank.mdx +19 -5
- package/docs/07-reference/01-ai-sdk-core/20-start-batch.mdx +1 -2
- package/docs/07-reference/01-ai-sdk-core/31-ui-message.mdx +28 -0
- package/package.json +12 -12
- package/src/batch/batch-types.ts +64 -5
- package/src/batch/batch.ts +100 -4
- package/src/batch/index.ts +3 -0
- package/src/embed/embed-events.ts +9 -3
- package/src/embed/embed-many.ts +20 -9
- package/src/embed/embed.ts +19 -9
- package/src/embed/restricted-telemetry-dispatcher.ts +44 -0
- package/src/generate-image/generate-image.ts +2 -2
- package/src/generate-text/restricted-telemetry-dispatcher.ts +1 -22
- package/src/rerank/rerank-events.ts +9 -3
- package/src/rerank/rerank.ts +53 -14
- package/src/rerank/restricted-telemetry-dispatcher.ts +44 -0
- package/src/telemetry/filter-included-context.ts +24 -0
- package/src/ui/index.ts +2 -0
- package/src/ui/ui-messages.ts +24 -2
- package/src/ui/validate-ui-messages.ts +13 -4
package/src/batch/batch.ts
CHANGED
|
@@ -4,12 +4,22 @@ import {
|
|
|
4
4
|
type Experimental_BatchV4ItemResult as BatchV4ItemResult,
|
|
5
5
|
type LanguageModelV4GenerateResult,
|
|
6
6
|
type LanguageModelV4ToolCall,
|
|
7
|
+
type ImageModelV4Result,
|
|
7
8
|
type ProviderV4,
|
|
8
9
|
} from '@ai-sdk/provider';
|
|
9
10
|
import { gateway } from '@ai-sdk/gateway';
|
|
10
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
detectMediaType,
|
|
13
|
+
type ToolSet,
|
|
14
|
+
withUserAgentSuffix,
|
|
15
|
+
} from '@ai-sdk/provider-utils';
|
|
11
16
|
import { InvalidArgumentError } from '../error/invalid-argument-error';
|
|
12
17
|
import { convertLanguageModelContent } from '../generate-text/convert-language-model-content';
|
|
18
|
+
import {
|
|
19
|
+
getImageProviderMetadata,
|
|
20
|
+
normalizePrompt as normalizeImagePrompt,
|
|
21
|
+
} from '../generate-image/generate-image';
|
|
22
|
+
import { DefaultGeneratedFile } from '../generate-text/generated-file';
|
|
13
23
|
import { parseToolCall } from '../generate-text/parse-tool-call';
|
|
14
24
|
import { prepareToolChoice } from '../prompt/prepare-tool-choice';
|
|
15
25
|
import { prepareTools } from '../prompt/prepare-tools';
|
|
@@ -40,7 +50,7 @@ import type {
|
|
|
40
50
|
StartBatchOptions,
|
|
41
51
|
StartBatchResult,
|
|
42
52
|
TextBatchGenerationResult,
|
|
43
|
-
|
|
53
|
+
ImageBatchGenerationResult,
|
|
44
54
|
} from './batch-types';
|
|
45
55
|
|
|
46
56
|
/**
|
|
@@ -167,7 +177,8 @@ export async function startBatch<
|
|
|
167
177
|
const toolsByName = new Map<string, unknown>();
|
|
168
178
|
|
|
169
179
|
for (const request of requests) {
|
|
170
|
-
|
|
180
|
+
const requestType = request.type;
|
|
181
|
+
switch (requestType) {
|
|
171
182
|
case 'text': {
|
|
172
183
|
const standardizedPrompt = await standardizePrompt(request);
|
|
173
184
|
const preparedTools = await prepareTools({
|
|
@@ -200,6 +211,33 @@ export async function startBatch<
|
|
|
200
211
|
});
|
|
201
212
|
break;
|
|
202
213
|
}
|
|
214
|
+
case 'image': {
|
|
215
|
+
const { prompt, files, mask } = normalizeImagePrompt(request.prompt);
|
|
216
|
+
normalizedRequests.push({
|
|
217
|
+
id: request.id,
|
|
218
|
+
type: request.type,
|
|
219
|
+
modelId: request.model,
|
|
220
|
+
options: {
|
|
221
|
+
prompt,
|
|
222
|
+
n: request.n ?? 1,
|
|
223
|
+
size: request.size,
|
|
224
|
+
aspectRatio: request.aspectRatio,
|
|
225
|
+
seed: request.seed,
|
|
226
|
+
files,
|
|
227
|
+
mask,
|
|
228
|
+
providerOptions: request.providerOptions ?? {},
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
default: {
|
|
234
|
+
const _exhaustiveCheck: never = requestType;
|
|
235
|
+
throw new InvalidArgumentError({
|
|
236
|
+
parameter: 'requests',
|
|
237
|
+
value: _exhaustiveCheck,
|
|
238
|
+
message: `Unsupported batch request type "${_exhaustiveCheck}".`,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
203
241
|
}
|
|
204
242
|
operationAbortSignal?.throwIfAborted();
|
|
205
243
|
}
|
|
@@ -471,18 +509,47 @@ async function convertBatchItemResult<TOOLS extends ToolSet>({
|
|
|
471
509
|
}: {
|
|
472
510
|
item: BatchV4ItemResult;
|
|
473
511
|
tools: TOOLS | undefined;
|
|
474
|
-
}): Promise<
|
|
512
|
+
}): Promise<BatchItemResult<TOOLS>> {
|
|
475
513
|
switch (item.type) {
|
|
476
514
|
case 'text':
|
|
477
515
|
switch (item.status) {
|
|
478
516
|
case 'succeeded':
|
|
479
517
|
return {
|
|
518
|
+
type: item.type,
|
|
480
519
|
id: item.id,
|
|
481
520
|
status: item.status,
|
|
482
521
|
...(await convertGenerateResult({ result: item.result, tools })),
|
|
483
522
|
};
|
|
484
523
|
case 'failed':
|
|
485
524
|
return {
|
|
525
|
+
type: item.type,
|
|
526
|
+
id: item.id,
|
|
527
|
+
status: item.status,
|
|
528
|
+
error: item.error,
|
|
529
|
+
providerMetadata: item.providerMetadata,
|
|
530
|
+
};
|
|
531
|
+
case 'cancelled':
|
|
532
|
+
case 'expired':
|
|
533
|
+
return {
|
|
534
|
+
type: item.type,
|
|
535
|
+
id: item.id,
|
|
536
|
+
status: item.status,
|
|
537
|
+
error: item.error,
|
|
538
|
+
providerMetadata: item.providerMetadata,
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
case 'image':
|
|
542
|
+
switch (item.status) {
|
|
543
|
+
case 'succeeded':
|
|
544
|
+
return {
|
|
545
|
+
type: item.type,
|
|
546
|
+
id: item.id,
|
|
547
|
+
status: item.status,
|
|
548
|
+
...convertImageResult(item.result),
|
|
549
|
+
};
|
|
550
|
+
case 'failed':
|
|
551
|
+
return {
|
|
552
|
+
type: item.type,
|
|
486
553
|
id: item.id,
|
|
487
554
|
status: item.status,
|
|
488
555
|
error: item.error,
|
|
@@ -491,6 +558,7 @@ async function convertBatchItemResult<TOOLS extends ToolSet>({
|
|
|
491
558
|
case 'cancelled':
|
|
492
559
|
case 'expired':
|
|
493
560
|
return {
|
|
561
|
+
type: item.type,
|
|
494
562
|
id: item.id,
|
|
495
563
|
status: item.status,
|
|
496
564
|
error: item.error,
|
|
@@ -500,6 +568,34 @@ async function convertBatchItemResult<TOOLS extends ToolSet>({
|
|
|
500
568
|
}
|
|
501
569
|
}
|
|
502
570
|
|
|
571
|
+
function convertImageResult(
|
|
572
|
+
result: ImageModelV4Result,
|
|
573
|
+
): ImageBatchGenerationResult {
|
|
574
|
+
return {
|
|
575
|
+
images: result.images.map(
|
|
576
|
+
(image, index) =>
|
|
577
|
+
new DefaultGeneratedFile({
|
|
578
|
+
data: image,
|
|
579
|
+
mediaType:
|
|
580
|
+
detectMediaType({ data: image, topLevelType: 'image' }) ??
|
|
581
|
+
'image/png',
|
|
582
|
+
providerMetadata: getImageProviderMetadata(
|
|
583
|
+
result.providerMetadata,
|
|
584
|
+
index,
|
|
585
|
+
),
|
|
586
|
+
}),
|
|
587
|
+
),
|
|
588
|
+
warnings: result.warnings,
|
|
589
|
+
response: {
|
|
590
|
+
timestamp: result.response.timestamp,
|
|
591
|
+
modelId: result.response.modelId,
|
|
592
|
+
headers: result.response.headers,
|
|
593
|
+
},
|
|
594
|
+
providerMetadata: result.providerMetadata,
|
|
595
|
+
usage: result.usage,
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
|
|
503
599
|
async function convertGenerateResult<TOOLS extends ToolSet>({
|
|
504
600
|
result,
|
|
505
601
|
tools,
|
package/src/batch/index.ts
CHANGED
|
@@ -21,6 +21,9 @@ export type {
|
|
|
21
21
|
Batch as Experimental_Batch,
|
|
22
22
|
BatchItemResult as Experimental_BatchItemResult,
|
|
23
23
|
BatchRequest as Experimental_BatchRequest,
|
|
24
|
+
ImageBatchGenerationResult as Experimental_ImageBatchGenerationResult,
|
|
25
|
+
ImageBatchItemResult as Experimental_ImageBatchItemResult,
|
|
26
|
+
ImageBatchRequest as Experimental_ImageBatchRequest,
|
|
24
27
|
TextBatchGenerationResult as Experimental_TextBatchGenerationResult,
|
|
25
28
|
TextBatchItemResult as Experimental_TextBatchItemResult,
|
|
26
29
|
TextBatchRequest as Experimental_TextBatchRequest,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ProviderOptions } from '@ai-sdk/provider-utils';
|
|
1
|
+
import type { Context, ProviderOptions } from '@ai-sdk/provider-utils';
|
|
2
2
|
import type { Embedding, ProviderMetadata } from '../types';
|
|
3
3
|
import type { EmbeddingModelUsage } from '../types/usage';
|
|
4
4
|
import type { Warning } from '../types/warning';
|
|
@@ -8,7 +8,10 @@ import type { Warning } from '../types/warning';
|
|
|
8
8
|
*
|
|
9
9
|
* Called when the operation begins, before the embedding model is called.
|
|
10
10
|
*/
|
|
11
|
-
export type EmbedStartEvent = {
|
|
11
|
+
export type EmbedStartEvent<RUNTIME_CONTEXT extends Context = Context> = {
|
|
12
|
+
/** User-defined runtime context. */
|
|
13
|
+
readonly runtimeContext: RUNTIME_CONTEXT;
|
|
14
|
+
|
|
12
15
|
/** Unique identifier for this embed call, used to correlate events. */
|
|
13
16
|
readonly callId: string;
|
|
14
17
|
|
|
@@ -39,7 +42,10 @@ export type EmbedStartEvent = {
|
|
|
39
42
|
*
|
|
40
43
|
* Called when the operation completes, after the embedding model returns.
|
|
41
44
|
*/
|
|
42
|
-
export type EmbedEndEvent = {
|
|
45
|
+
export type EmbedEndEvent<RUNTIME_CONTEXT extends Context = Context> = {
|
|
46
|
+
/** User-defined runtime context. */
|
|
47
|
+
readonly runtimeContext: RUNTIME_CONTEXT;
|
|
48
|
+
|
|
43
49
|
/** Unique identifier for this embed call, used to correlate events. */
|
|
44
50
|
readonly callId: string;
|
|
45
51
|
|
package/src/embed/embed-many.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { InvalidResponseDataError } from '@ai-sdk/provider';
|
|
2
2
|
import {
|
|
3
3
|
createIdGenerator,
|
|
4
|
+
type Context,
|
|
4
5
|
withUserAgentSuffix,
|
|
5
6
|
type ProviderOptions,
|
|
6
7
|
} from '@ai-sdk/provider-utils';
|
|
7
8
|
import { logWarnings } from '../logger/log-warnings';
|
|
8
9
|
import { getEmbeddingModelMaxInputBytesPerCall } from '../model/get-embedding-model-max-input-bytes-per-call';
|
|
9
10
|
import { resolveEmbeddingModel } from '../model/resolve-model';
|
|
10
|
-
import {
|
|
11
|
+
import { createRestrictedTelemetryDispatcher } from './restricted-telemetry-dispatcher';
|
|
11
12
|
import type { TelemetryOptions } from '../telemetry/telemetry-options';
|
|
12
13
|
import type { Embedding, EmbeddingModel, ProviderMetadata } from '../types';
|
|
13
14
|
import type { Warning } from '../types/warning';
|
|
@@ -42,6 +43,7 @@ const originalGenerateCallId = createIdGenerator({
|
|
|
42
43
|
* @param maxParallelCalls - Maximum number of concurrent requests. Default: Infinity.
|
|
43
44
|
*
|
|
44
45
|
* @param telemetry - Optional telemetry configuration.
|
|
46
|
+
* @param runtimeContext - User-defined runtime context passed to callbacks and, when explicitly included, telemetry.
|
|
45
47
|
*
|
|
46
48
|
* @param providerOptions - Additional provider-specific options. They are passed through
|
|
47
49
|
* to the provider from the AI SDK and enable provider-specific
|
|
@@ -49,7 +51,7 @@ const originalGenerateCallId = createIdGenerator({
|
|
|
49
51
|
*
|
|
50
52
|
* @returns A result object that contains the embeddings, the value, and additional information.
|
|
51
53
|
*/
|
|
52
|
-
export async function embedMany({
|
|
54
|
+
export async function embedMany<RUNTIME_CONTEXT extends Context = Context>({
|
|
53
55
|
model: modelArg,
|
|
54
56
|
values,
|
|
55
57
|
maxParallelCalls = Infinity,
|
|
@@ -59,6 +61,7 @@ export async function embedMany({
|
|
|
59
61
|
providerOptions,
|
|
60
62
|
experimental_telemetry,
|
|
61
63
|
telemetry = experimental_telemetry,
|
|
64
|
+
runtimeContext = {} as RUNTIME_CONTEXT,
|
|
62
65
|
onStart,
|
|
63
66
|
experimental_onStart,
|
|
64
67
|
onEnd,
|
|
@@ -96,14 +99,19 @@ export async function embedMany({
|
|
|
96
99
|
/**
|
|
97
100
|
* Optional telemetry configuration.
|
|
98
101
|
*/
|
|
99
|
-
telemetry?: TelemetryOptions
|
|
102
|
+
telemetry?: TelemetryOptions<RUNTIME_CONTEXT>;
|
|
100
103
|
|
|
101
104
|
/**
|
|
102
105
|
* Optional telemetry configuration.
|
|
103
106
|
*
|
|
104
107
|
* @deprecated Use `telemetry` instead. This alias will be removed in a future major release.
|
|
105
108
|
*/
|
|
106
|
-
experimental_telemetry?: TelemetryOptions
|
|
109
|
+
experimental_telemetry?: TelemetryOptions<RUNTIME_CONTEXT>;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* User-defined runtime context. Treat runtime context as immutable.
|
|
113
|
+
*/
|
|
114
|
+
runtimeContext?: RUNTIME_CONTEXT;
|
|
107
115
|
|
|
108
116
|
/**
|
|
109
117
|
* Additional provider-specific options. They are passed through
|
|
@@ -123,7 +131,7 @@ export async function embedMany({
|
|
|
123
131
|
* Callback that is called when the embedMany operation begins,
|
|
124
132
|
* before the embedding model is called.
|
|
125
133
|
*/
|
|
126
|
-
onStart?: Callback<EmbedStartEvent
|
|
134
|
+
onStart?: Callback<EmbedStartEvent<RUNTIME_CONTEXT>>;
|
|
127
135
|
|
|
128
136
|
/**
|
|
129
137
|
* Callback that is called when the embedMany operation begins,
|
|
@@ -131,13 +139,13 @@ export async function embedMany({
|
|
|
131
139
|
*
|
|
132
140
|
* @deprecated Use `onStart` instead.
|
|
133
141
|
*/
|
|
134
|
-
experimental_onStart?: Callback<EmbedStartEvent
|
|
142
|
+
experimental_onStart?: Callback<EmbedStartEvent<RUNTIME_CONTEXT>>;
|
|
135
143
|
|
|
136
144
|
/**
|
|
137
145
|
* Callback that is called when the embedMany operation completes,
|
|
138
146
|
* after all embedding model calls return.
|
|
139
147
|
*/
|
|
140
|
-
onEnd?: Callback<EmbedEndEvent
|
|
148
|
+
onEnd?: Callback<EmbedEndEvent<RUNTIME_CONTEXT>>;
|
|
141
149
|
|
|
142
150
|
/**
|
|
143
151
|
* Callback that is called when the embedMany operation completes,
|
|
@@ -145,7 +153,7 @@ export async function embedMany({
|
|
|
145
153
|
*
|
|
146
154
|
* @deprecated Use `onEnd` instead.
|
|
147
155
|
*/
|
|
148
|
-
experimental_onEnd?: Callback<EmbedEndEvent
|
|
156
|
+
experimental_onEnd?: Callback<EmbedEndEvent<RUNTIME_CONTEXT>>;
|
|
149
157
|
|
|
150
158
|
/**
|
|
151
159
|
* Internal. For test use only. May change without notice.
|
|
@@ -170,7 +178,7 @@ export async function embedMany({
|
|
|
170
178
|
|
|
171
179
|
const callId = generateCallId();
|
|
172
180
|
|
|
173
|
-
const telemetryDispatcher =
|
|
181
|
+
const telemetryDispatcher = createRestrictedTelemetryDispatcher({
|
|
174
182
|
telemetry,
|
|
175
183
|
});
|
|
176
184
|
|
|
@@ -182,6 +190,7 @@ export async function embedMany({
|
|
|
182
190
|
const startEvent = {
|
|
183
191
|
callId,
|
|
184
192
|
operationId: 'ai.embedMany',
|
|
193
|
+
runtimeContext,
|
|
185
194
|
provider: model.provider,
|
|
186
195
|
modelId: model.modelId,
|
|
187
196
|
value: values,
|
|
@@ -277,6 +286,7 @@ export async function embedMany({
|
|
|
277
286
|
event: {
|
|
278
287
|
callId,
|
|
279
288
|
operationId: 'ai.embedMany',
|
|
289
|
+
runtimeContext,
|
|
280
290
|
provider: model.provider,
|
|
281
291
|
modelId: model.modelId,
|
|
282
292
|
value: values,
|
|
@@ -418,6 +428,7 @@ export async function embedMany({
|
|
|
418
428
|
event: {
|
|
419
429
|
callId,
|
|
420
430
|
operationId: 'ai.embedMany',
|
|
431
|
+
runtimeContext,
|
|
421
432
|
provider: model.provider,
|
|
422
433
|
modelId: model.modelId,
|
|
423
434
|
value: values,
|
package/src/embed/embed.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createIdGenerator,
|
|
3
|
+
type Context,
|
|
3
4
|
withUserAgentSuffix,
|
|
4
5
|
type ProviderOptions,
|
|
5
6
|
} from '@ai-sdk/provider-utils';
|
|
6
7
|
import { InvalidResponseDataError } from '../error';
|
|
7
8
|
import { logWarnings } from '../logger/log-warnings';
|
|
8
9
|
import { resolveEmbeddingModel } from '../model/resolve-model';
|
|
9
|
-
import {
|
|
10
|
+
import { createRestrictedTelemetryDispatcher } from './restricted-telemetry-dispatcher';
|
|
10
11
|
import type { TelemetryOptions } from '../telemetry/telemetry-options';
|
|
11
12
|
import type { EmbeddingModel } from '../types';
|
|
12
13
|
import type { Callback } from '../util/callback';
|
|
@@ -32,6 +33,7 @@ const originalGenerateCallId = createIdGenerator({
|
|
|
32
33
|
* @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
|
|
33
34
|
*
|
|
34
35
|
* @param telemetry - Optional telemetry configuration.
|
|
36
|
+
* @param runtimeContext - User-defined runtime context passed to callbacks and, when explicitly included, telemetry.
|
|
35
37
|
*
|
|
36
38
|
* @param providerOptions - Additional provider-specific options. They are passed through
|
|
37
39
|
* to the provider from the AI SDK and enable provider-specific
|
|
@@ -39,7 +41,7 @@ const originalGenerateCallId = createIdGenerator({
|
|
|
39
41
|
*
|
|
40
42
|
* @returns A result object that contains the embedding, the value, and additional information.
|
|
41
43
|
*/
|
|
42
|
-
export async function embed({
|
|
44
|
+
export async function embed<RUNTIME_CONTEXT extends Context = Context>({
|
|
43
45
|
model: modelArg,
|
|
44
46
|
value,
|
|
45
47
|
providerOptions,
|
|
@@ -48,6 +50,7 @@ export async function embed({
|
|
|
48
50
|
headers,
|
|
49
51
|
experimental_telemetry,
|
|
50
52
|
telemetry = experimental_telemetry,
|
|
53
|
+
runtimeContext = {} as RUNTIME_CONTEXT,
|
|
51
54
|
onStart,
|
|
52
55
|
experimental_onStart,
|
|
53
56
|
onEnd,
|
|
@@ -92,20 +95,25 @@ export async function embed({
|
|
|
92
95
|
/**
|
|
93
96
|
* Optional telemetry configuration.
|
|
94
97
|
*/
|
|
95
|
-
telemetry?: TelemetryOptions
|
|
98
|
+
telemetry?: TelemetryOptions<RUNTIME_CONTEXT>;
|
|
96
99
|
|
|
97
100
|
/**
|
|
98
101
|
* Optional telemetry configuration.
|
|
99
102
|
*
|
|
100
103
|
* @deprecated Use `telemetry` instead. This alias will be removed in a future major release.
|
|
101
104
|
*/
|
|
102
|
-
experimental_telemetry?: TelemetryOptions
|
|
105
|
+
experimental_telemetry?: TelemetryOptions<RUNTIME_CONTEXT>;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* User-defined runtime context. Treat runtime context as immutable.
|
|
109
|
+
*/
|
|
110
|
+
runtimeContext?: RUNTIME_CONTEXT;
|
|
103
111
|
|
|
104
112
|
/**
|
|
105
113
|
* Callback that is called when the embed operation begins,
|
|
106
114
|
* before the embedding model is called.
|
|
107
115
|
*/
|
|
108
|
-
onStart?: Callback<EmbedStartEvent
|
|
116
|
+
onStart?: Callback<EmbedStartEvent<RUNTIME_CONTEXT>>;
|
|
109
117
|
|
|
110
118
|
/**
|
|
111
119
|
* Callback that is called when the embed operation begins,
|
|
@@ -113,13 +121,13 @@ export async function embed({
|
|
|
113
121
|
*
|
|
114
122
|
* @deprecated Use `onStart` instead.
|
|
115
123
|
*/
|
|
116
|
-
experimental_onStart?: Callback<EmbedStartEvent
|
|
124
|
+
experimental_onStart?: Callback<EmbedStartEvent<RUNTIME_CONTEXT>>;
|
|
117
125
|
|
|
118
126
|
/**
|
|
119
127
|
* Callback that is called when the embed operation completes,
|
|
120
128
|
* after the embedding model returns.
|
|
121
129
|
*/
|
|
122
|
-
onEnd?: Callback<EmbedEndEvent
|
|
130
|
+
onEnd?: Callback<EmbedEndEvent<RUNTIME_CONTEXT>>;
|
|
123
131
|
|
|
124
132
|
/**
|
|
125
133
|
* Callback that is called when the embed operation completes,
|
|
@@ -127,7 +135,7 @@ export async function embed({
|
|
|
127
135
|
*
|
|
128
136
|
* @deprecated Use `onEnd` instead.
|
|
129
137
|
*/
|
|
130
|
-
experimental_onEnd?: Callback<EmbedEndEvent
|
|
138
|
+
experimental_onEnd?: Callback<EmbedEndEvent<RUNTIME_CONTEXT>>;
|
|
131
139
|
|
|
132
140
|
/**
|
|
133
141
|
* Internal. For test use only. May change without notice.
|
|
@@ -152,7 +160,7 @@ export async function embed({
|
|
|
152
160
|
|
|
153
161
|
const callId = generateCallId();
|
|
154
162
|
|
|
155
|
-
const telemetryDispatcher =
|
|
163
|
+
const telemetryDispatcher = createRestrictedTelemetryDispatcher({
|
|
156
164
|
telemetry,
|
|
157
165
|
});
|
|
158
166
|
|
|
@@ -164,6 +172,7 @@ export async function embed({
|
|
|
164
172
|
const startEvent = {
|
|
165
173
|
callId,
|
|
166
174
|
operationId: 'ai.embed',
|
|
175
|
+
runtimeContext,
|
|
167
176
|
provider: model.provider,
|
|
168
177
|
modelId: model.modelId,
|
|
169
178
|
value,
|
|
@@ -248,6 +257,7 @@ export async function embed({
|
|
|
248
257
|
event: {
|
|
249
258
|
callId,
|
|
250
259
|
operationId: 'ai.embed',
|
|
260
|
+
runtimeContext,
|
|
251
261
|
provider: model.provider,
|
|
252
262
|
modelId: model.modelId,
|
|
253
263
|
value,
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import 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 { EmbedStartEvent, EmbedEndEvent } from './embed-events';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Filters runtime context before sending operation events to telemetry integrations.
|
|
11
|
+
* User callbacks continue to receive the original context.
|
|
12
|
+
*/
|
|
13
|
+
export function createRestrictedTelemetryDispatcher<
|
|
14
|
+
RUNTIME_CONTEXT extends Context,
|
|
15
|
+
>({
|
|
16
|
+
telemetry,
|
|
17
|
+
}: {
|
|
18
|
+
telemetry?: TelemetryOptions<RUNTIME_CONTEXT>;
|
|
19
|
+
}): Omit<TelemetryDispatcher, 'onStart' | 'onEnd'> & {
|
|
20
|
+
onStart: Callback<EmbedStartEvent<RUNTIME_CONTEXT>>;
|
|
21
|
+
onEnd: Callback<EmbedEndEvent<RUNTIME_CONTEXT>>;
|
|
22
|
+
} {
|
|
23
|
+
const dispatcher = createTelemetryDispatcher({ telemetry });
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
...dispatcher,
|
|
27
|
+
onStart: event =>
|
|
28
|
+
dispatcher.onStart?.({
|
|
29
|
+
...event,
|
|
30
|
+
runtimeContext: filterIncludedContext({
|
|
31
|
+
context: event.runtimeContext,
|
|
32
|
+
includeContext: telemetry?.includeRuntimeContext,
|
|
33
|
+
}),
|
|
34
|
+
}),
|
|
35
|
+
onEnd: event =>
|
|
36
|
+
dispatcher.onEnd?.({
|
|
37
|
+
...event,
|
|
38
|
+
runtimeContext: filterIncludedContext({
|
|
39
|
+
context: event.runtimeContext,
|
|
40
|
+
includeContext: telemetry?.includeRuntimeContext,
|
|
41
|
+
}),
|
|
42
|
+
}),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -381,7 +381,7 @@ class DefaultGenerateImageResult implements GenerateImageResult {
|
|
|
381
381
|
/**
|
|
382
382
|
* Extracts per-image metadata from the legacy `providerMetadata.<provider>.images` result shape.
|
|
383
383
|
*/
|
|
384
|
-
function getImageProviderMetadata(
|
|
384
|
+
export function getImageProviderMetadata(
|
|
385
385
|
providerMetadata: ImageModelV4ProviderMetadata | undefined,
|
|
386
386
|
imageIndex: number,
|
|
387
387
|
): Record<string, JSONObject> | undefined {
|
|
@@ -442,7 +442,7 @@ function addDecimalStrings(
|
|
|
442
442
|
)}`.replace(/\.?0+$/, '');
|
|
443
443
|
}
|
|
444
444
|
|
|
445
|
-
function normalizePrompt(
|
|
445
|
+
export function normalizePrompt(
|
|
446
446
|
prompt: GenerateImagePrompt,
|
|
447
447
|
): Pick<ImageModelV4CallOptions, 'prompt' | 'files' | 'mask'> {
|
|
448
448
|
if (typeof prompt === 'string') {
|
|
@@ -4,6 +4,7 @@ import type {
|
|
|
4
4
|
InferToolSetContext,
|
|
5
5
|
ToolSet,
|
|
6
6
|
} from '@ai-sdk/provider-utils';
|
|
7
|
+
import { filterIncludedContext } from '../telemetry/filter-included-context';
|
|
7
8
|
import { createTelemetryDispatcher } from '../telemetry/create-telemetry-dispatcher';
|
|
8
9
|
import type { TelemetryDispatcher } from '../telemetry/telemetry';
|
|
9
10
|
import type {
|
|
@@ -56,28 +57,6 @@ export type RestrictedTelemetryDispatcher<
|
|
|
56
57
|
onToolExecutionEnd?: OnToolExecutionEndCallback<TOOLS>;
|
|
57
58
|
};
|
|
58
59
|
|
|
59
|
-
/**
|
|
60
|
-
* Returns a shallow copy of the runtime context with only top-level
|
|
61
|
-
* properties marked for telemetry inclusion.
|
|
62
|
-
*/
|
|
63
|
-
function filterIncludedContext<CONTEXT extends Context>({
|
|
64
|
-
context,
|
|
65
|
-
includeContext,
|
|
66
|
-
}: {
|
|
67
|
-
context: CONTEXT;
|
|
68
|
-
includeContext: IncludedContext<CONTEXT>;
|
|
69
|
-
}): Context {
|
|
70
|
-
if (context == null) {
|
|
71
|
-
return {};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return Object.fromEntries(
|
|
75
|
-
Object.entries(context).filter(
|
|
76
|
-
([key]) => includeContext?.[key as keyof CONTEXT] === true,
|
|
77
|
-
),
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
60
|
/**
|
|
82
61
|
* Creates a copy of a step result whose runtime context only contains
|
|
83
62
|
* top-level properties marked for telemetry inclusion.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { JSONObject } from '@ai-sdk/provider';
|
|
2
|
-
import type { ProviderOptions } from '@ai-sdk/provider-utils';
|
|
2
|
+
import type { Context, ProviderOptions } from '@ai-sdk/provider-utils';
|
|
3
3
|
import type { ProviderMetadata } from '../types';
|
|
4
4
|
import type { Warning } from '../types/warning';
|
|
5
5
|
|
|
@@ -8,7 +8,10 @@ import type { Warning } from '../types/warning';
|
|
|
8
8
|
*
|
|
9
9
|
* Called when the operation begins, before the reranking model is called.
|
|
10
10
|
*/
|
|
11
|
-
export type RerankStartEvent = {
|
|
11
|
+
export type RerankStartEvent<RUNTIME_CONTEXT extends Context = Context> = {
|
|
12
|
+
/** User-defined runtime context. */
|
|
13
|
+
readonly runtimeContext: RUNTIME_CONTEXT;
|
|
14
|
+
|
|
12
15
|
/** Unique identifier for this rerank call, used to correlate events. */
|
|
13
16
|
readonly callId: string;
|
|
14
17
|
|
|
@@ -43,7 +46,10 @@ export type RerankStartEvent = {
|
|
|
43
46
|
*
|
|
44
47
|
* Called when the operation completes, after the reranking model returns.
|
|
45
48
|
*/
|
|
46
|
-
export type RerankEndEvent = {
|
|
49
|
+
export type RerankEndEvent<RUNTIME_CONTEXT extends Context = Context> = {
|
|
50
|
+
/** User-defined runtime context. */
|
|
51
|
+
readonly runtimeContext: RUNTIME_CONTEXT;
|
|
52
|
+
|
|
47
53
|
/** Unique identifier for this rerank call, used to correlate events. */
|
|
48
54
|
readonly callId: string;
|
|
49
55
|
|