ai 3.1.0-canary.2 → 3.1.0-canary.3
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/ai-model-specification/dist/index.d.mts +76 -9
- package/ai-model-specification/dist/index.d.ts +76 -9
- package/ai-model-specification/dist/index.js +52 -16
- package/ai-model-specification/dist/index.js.map +1 -1
- package/ai-model-specification/dist/index.mjs +49 -15
- package/ai-model-specification/dist/index.mjs.map +1 -1
- package/core/dist/index.d.mts +79 -14
- package/core/dist/index.d.ts +79 -14
- package/core/dist/index.js +246 -338
- package/core/dist/index.js.map +1 -1
- package/core/dist/index.mjs +246 -337
- package/core/dist/index.mjs.map +1 -1
- package/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/provider/dist/index.d.mts +124 -45
- package/provider/dist/index.d.ts +124 -45
- package/provider/dist/index.js +131 -93
- package/provider/dist/index.js.map +1 -1
- package/provider/dist/index.mjs +129 -92
- package/provider/dist/index.mjs.map +1 -1
- package/react/dist/index.d.mts +4 -4
- package/react/dist/index.d.ts +4 -4
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs.map +1 -1
package/core/dist/index.mjs
CHANGED
@@ -78,6 +78,30 @@ var NoTextGeneratedError = class extends Error {
|
|
78
78
|
}
|
79
79
|
};
|
80
80
|
|
81
|
+
// ai-model-specification/errors/retry-error.ts
|
82
|
+
var RetryError = class extends Error {
|
83
|
+
constructor({
|
84
|
+
message,
|
85
|
+
reason,
|
86
|
+
errors
|
87
|
+
}) {
|
88
|
+
super(message);
|
89
|
+
this.name = "RetryError";
|
90
|
+
this.reason = reason;
|
91
|
+
this.errors = errors;
|
92
|
+
this.lastError = errors[errors.length - 1];
|
93
|
+
}
|
94
|
+
toJSON() {
|
95
|
+
return {
|
96
|
+
name: this.name,
|
97
|
+
message: this.message,
|
98
|
+
reason: this.reason,
|
99
|
+
lastError: this.lastError,
|
100
|
+
errors: this.errors
|
101
|
+
};
|
102
|
+
}
|
103
|
+
};
|
104
|
+
|
81
105
|
// ai-model-specification/errors/type-validation-error.ts
|
82
106
|
var TypeValidationError = class extends Error {
|
83
107
|
constructor({ value, cause }) {
|
@@ -167,6 +191,15 @@ function convertUint8ArrayToBase64(array) {
|
|
167
191
|
return globalThis.btoa(latin1string);
|
168
192
|
}
|
169
193
|
|
194
|
+
// core/generate-text/token-usage.ts
|
195
|
+
function calculateTokenUsage(usage) {
|
196
|
+
return {
|
197
|
+
promptTokens: usage.promptTokens,
|
198
|
+
completionTokens: usage.completionTokens,
|
199
|
+
totalTokens: usage.promptTokens + usage.completionTokens
|
200
|
+
};
|
201
|
+
}
|
202
|
+
|
170
203
|
// core/prompt/data-content.ts
|
171
204
|
function convertDataContentToBase64String(content) {
|
172
205
|
if (typeof content === "string") {
|
@@ -236,7 +269,7 @@ function convertToLanguageModelPrompt({
|
|
236
269
|
case "image": {
|
237
270
|
return {
|
238
271
|
type: "image",
|
239
|
-
image: convertDataContentToUint8Array(part.image),
|
272
|
+
image: part.image instanceof URL ? part.image : convertDataContentToUint8Array(part.image),
|
240
273
|
mimeType: part.mimeType
|
241
274
|
};
|
242
275
|
}
|
@@ -372,6 +405,66 @@ function validateCallSettings(settings) {
|
|
372
405
|
return settings;
|
373
406
|
}
|
374
407
|
|
408
|
+
// core/util/delay.ts
|
409
|
+
async function delay(delayInMs) {
|
410
|
+
return new Promise((resolve) => setTimeout(resolve, delayInMs));
|
411
|
+
}
|
412
|
+
|
413
|
+
// core/util/retry-with-exponential-backoff.ts
|
414
|
+
var retryWithExponentialBackoff = ({
|
415
|
+
maxRetries = 2,
|
416
|
+
initialDelayInMs = 2e3,
|
417
|
+
backoffFactor = 2
|
418
|
+
} = {}) => async (f) => _retryWithExponentialBackoff(f, {
|
419
|
+
maxRetries,
|
420
|
+
delayInMs: initialDelayInMs,
|
421
|
+
backoffFactor
|
422
|
+
});
|
423
|
+
async function _retryWithExponentialBackoff(f, {
|
424
|
+
maxRetries,
|
425
|
+
delayInMs,
|
426
|
+
backoffFactor
|
427
|
+
}, errors = []) {
|
428
|
+
try {
|
429
|
+
return await f();
|
430
|
+
} catch (error) {
|
431
|
+
if (maxRetries === 0) {
|
432
|
+
throw error;
|
433
|
+
}
|
434
|
+
const errorMessage = getErrorMessage(error);
|
435
|
+
const newErrors = [...errors, error];
|
436
|
+
const tryNumber = newErrors.length;
|
437
|
+
if (tryNumber > maxRetries) {
|
438
|
+
throw new RetryError({
|
439
|
+
message: `Failed after ${tryNumber} tries. Last error: ${errorMessage}`,
|
440
|
+
reason: "maxRetriesExceeded",
|
441
|
+
errors: newErrors
|
442
|
+
});
|
443
|
+
}
|
444
|
+
if (error instanceof Error) {
|
445
|
+
if (error.name === "AbortError") {
|
446
|
+
throw error;
|
447
|
+
}
|
448
|
+
if (
|
449
|
+
// deal with bundling duplication by using names
|
450
|
+
error.name === "ApiCallError" && error.isRetryable === true && tryNumber <= maxRetries
|
451
|
+
) {
|
452
|
+
await delay(delayInMs);
|
453
|
+
return _retryWithExponentialBackoff(
|
454
|
+
f,
|
455
|
+
{ maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
|
456
|
+
newErrors
|
457
|
+
);
|
458
|
+
}
|
459
|
+
}
|
460
|
+
throw new RetryError({
|
461
|
+
message: `Failed after ${tryNumber} tries with non-retryable error: '${errorMessage}'`,
|
462
|
+
reason: "errorNotRetryable",
|
463
|
+
errors: newErrors
|
464
|
+
});
|
465
|
+
}
|
466
|
+
}
|
467
|
+
|
375
468
|
// core/generate-object/inject-json-schema-into-system.ts
|
376
469
|
var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
|
377
470
|
var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
|
@@ -399,69 +492,89 @@ async function generateObject({
|
|
399
492
|
system,
|
400
493
|
prompt,
|
401
494
|
messages,
|
495
|
+
maxRetries,
|
496
|
+
abortSignal,
|
402
497
|
...settings
|
403
498
|
}) {
|
404
499
|
var _a, _b;
|
500
|
+
const retry = retryWithExponentialBackoff({ maxRetries });
|
405
501
|
const jsonSchema = zodToJsonSchema(schema);
|
406
502
|
if (mode === "auto" || mode == null) {
|
407
503
|
mode = model.defaultObjectGenerationMode;
|
408
504
|
}
|
409
505
|
let result;
|
506
|
+
let finishReason;
|
507
|
+
let usage;
|
410
508
|
switch (mode) {
|
411
509
|
case "json": {
|
412
|
-
const generateResult = await
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
510
|
+
const generateResult = await retry(
|
511
|
+
() => model.doGenerate({
|
512
|
+
mode: { type: "object-json" },
|
513
|
+
...validateCallSettings(settings),
|
514
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
515
|
+
prompt: convertToLanguageModelPrompt({
|
516
|
+
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
517
|
+
prompt,
|
518
|
+
messages
|
519
|
+
}),
|
520
|
+
abortSignal
|
420
521
|
})
|
421
|
-
|
522
|
+
);
|
422
523
|
if (generateResult.text === void 0) {
|
423
524
|
throw new NoTextGeneratedError();
|
424
525
|
}
|
425
526
|
result = generateResult.text;
|
527
|
+
finishReason = generateResult.finishReason;
|
528
|
+
usage = generateResult.usage;
|
426
529
|
break;
|
427
530
|
}
|
428
531
|
case "grammar": {
|
429
|
-
const generateResult = await
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
532
|
+
const generateResult = await retry(
|
533
|
+
() => model.doGenerate({
|
534
|
+
mode: { type: "object-grammar", schema: jsonSchema },
|
535
|
+
...settings,
|
536
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
537
|
+
prompt: convertToLanguageModelPrompt({
|
538
|
+
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
539
|
+
prompt,
|
540
|
+
messages
|
541
|
+
}),
|
542
|
+
abortSignal
|
437
543
|
})
|
438
|
-
|
544
|
+
);
|
439
545
|
if (generateResult.text === void 0) {
|
440
546
|
throw new NoTextGeneratedError();
|
441
547
|
}
|
442
548
|
result = generateResult.text;
|
549
|
+
finishReason = generateResult.finishReason;
|
550
|
+
usage = generateResult.usage;
|
443
551
|
break;
|
444
552
|
}
|
445
553
|
case "tool": {
|
446
|
-
const generateResult = await
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
554
|
+
const generateResult = await retry(
|
555
|
+
() => model.doGenerate({
|
556
|
+
mode: {
|
557
|
+
type: "object-tool",
|
558
|
+
tool: {
|
559
|
+
type: "function",
|
560
|
+
name: "json",
|
561
|
+
description: "Respond with a JSON object.",
|
562
|
+
parameters: jsonSchema
|
563
|
+
}
|
564
|
+
},
|
565
|
+
...settings,
|
566
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
567
|
+
prompt: convertToLanguageModelPrompt({ system, prompt, messages }),
|
568
|
+
abortSignal
|
569
|
+
})
|
570
|
+
);
|
460
571
|
const functionArgs = (_b = (_a = generateResult.toolCalls) == null ? void 0 : _a[0]) == null ? void 0 : _b.args;
|
461
572
|
if (functionArgs === void 0) {
|
462
573
|
throw new NoTextGeneratedError();
|
463
574
|
}
|
464
575
|
result = functionArgs;
|
576
|
+
finishReason = generateResult.finishReason;
|
577
|
+
usage = generateResult.usage;
|
465
578
|
break;
|
466
579
|
}
|
467
580
|
case void 0: {
|
@@ -477,12 +590,16 @@ async function generateObject({
|
|
477
590
|
throw parseResult.error;
|
478
591
|
}
|
479
592
|
return new GenerateObjectResult({
|
480
|
-
object: parseResult.value
|
593
|
+
object: parseResult.value,
|
594
|
+
finishReason,
|
595
|
+
usage: calculateTokenUsage(usage)
|
481
596
|
});
|
482
597
|
}
|
483
598
|
var GenerateObjectResult = class {
|
484
599
|
constructor(options) {
|
485
600
|
this.object = options.object;
|
601
|
+
this.finishReason = options.finishReason;
|
602
|
+
this.usage = options.usage;
|
486
603
|
}
|
487
604
|
};
|
488
605
|
|
@@ -869,8 +986,11 @@ async function streamObject({
|
|
869
986
|
system,
|
870
987
|
prompt,
|
871
988
|
messages,
|
989
|
+
maxRetries,
|
990
|
+
abortSignal,
|
872
991
|
...settings
|
873
992
|
}) {
|
993
|
+
const retry = retryWithExponentialBackoff({ maxRetries });
|
874
994
|
const jsonSchema = zodToJsonSchema2(schema);
|
875
995
|
let modelStream;
|
876
996
|
if (mode === "auto" || mode == null) {
|
@@ -878,16 +998,19 @@ async function streamObject({
|
|
878
998
|
}
|
879
999
|
switch (mode) {
|
880
1000
|
case "json": {
|
881
|
-
const { stream, warnings } = await
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
1001
|
+
const { stream, warnings } = await retry(
|
1002
|
+
() => model.doStream({
|
1003
|
+
mode: { type: "object-json" },
|
1004
|
+
...validateCallSettings(settings),
|
1005
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1006
|
+
prompt: convertToLanguageModelPrompt({
|
1007
|
+
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
1008
|
+
prompt,
|
1009
|
+
messages
|
1010
|
+
}),
|
1011
|
+
abortSignal
|
889
1012
|
})
|
890
|
-
|
1013
|
+
);
|
891
1014
|
modelStream = stream.pipeThrough(
|
892
1015
|
new TransformStream({
|
893
1016
|
transform(chunk, controller) {
|
@@ -905,16 +1028,19 @@ async function streamObject({
|
|
905
1028
|
break;
|
906
1029
|
}
|
907
1030
|
case "grammar": {
|
908
|
-
const { stream, warnings } = await
|
909
|
-
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
1031
|
+
const { stream, warnings } = await retry(
|
1032
|
+
() => model.doStream({
|
1033
|
+
mode: { type: "object-grammar", schema: jsonSchema },
|
1034
|
+
...settings,
|
1035
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1036
|
+
prompt: convertToLanguageModelPrompt({
|
1037
|
+
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
1038
|
+
prompt,
|
1039
|
+
messages
|
1040
|
+
}),
|
1041
|
+
abortSignal
|
916
1042
|
})
|
917
|
-
|
1043
|
+
);
|
918
1044
|
modelStream = stream.pipeThrough(
|
919
1045
|
new TransformStream({
|
920
1046
|
transform(chunk, controller) {
|
@@ -932,20 +1058,23 @@ async function streamObject({
|
|
932
1058
|
break;
|
933
1059
|
}
|
934
1060
|
case "tool": {
|
935
|
-
const { stream, warnings } = await
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
1061
|
+
const { stream, warnings } = await retry(
|
1062
|
+
() => model.doStream({
|
1063
|
+
mode: {
|
1064
|
+
type: "object-tool",
|
1065
|
+
tool: {
|
1066
|
+
type: "function",
|
1067
|
+
name: "json",
|
1068
|
+
description: "Respond with a JSON object.",
|
1069
|
+
parameters: jsonSchema
|
1070
|
+
}
|
1071
|
+
},
|
1072
|
+
...settings,
|
1073
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1074
|
+
prompt: convertToLanguageModelPrompt({ system, prompt, messages }),
|
1075
|
+
abortSignal
|
1076
|
+
})
|
1077
|
+
);
|
949
1078
|
modelStream = stream.pipeThrough(
|
950
1079
|
new TransformStream({
|
951
1080
|
transform(chunk, controller) {
|
@@ -1044,27 +1173,33 @@ async function generateText({
|
|
1044
1173
|
system,
|
1045
1174
|
prompt,
|
1046
1175
|
messages,
|
1176
|
+
maxRetries,
|
1177
|
+
abortSignal,
|
1047
1178
|
...settings
|
1048
1179
|
}) {
|
1049
1180
|
var _a, _b;
|
1050
|
-
const
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
type: "
|
1055
|
-
name,
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
prompt
|
1065
|
-
|
1181
|
+
const retry = retryWithExponentialBackoff({ maxRetries });
|
1182
|
+
const modelResponse = await retry(
|
1183
|
+
() => model.doGenerate({
|
1184
|
+
mode: {
|
1185
|
+
type: "regular",
|
1186
|
+
tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
|
1187
|
+
type: "function",
|
1188
|
+
name,
|
1189
|
+
description: tool2.description,
|
1190
|
+
parameters: zodToJsonSchema3(tool2.parameters)
|
1191
|
+
}))
|
1192
|
+
},
|
1193
|
+
...validateCallSettings(settings),
|
1194
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1195
|
+
prompt: convertToLanguageModelPrompt({
|
1196
|
+
system,
|
1197
|
+
prompt,
|
1198
|
+
messages
|
1199
|
+
}),
|
1200
|
+
abortSignal
|
1066
1201
|
})
|
1067
|
-
|
1202
|
+
);
|
1068
1203
|
const toolCalls = [];
|
1069
1204
|
for (const modelToolCall of (_a = modelResponse.toolCalls) != null ? _a : []) {
|
1070
1205
|
toolCalls.push(parseToolCall({ toolCall: modelToolCall, tools }));
|
@@ -1076,7 +1211,9 @@ async function generateText({
|
|
1076
1211
|
// they can check the length of the string:
|
1077
1212
|
text: (_b = modelResponse.text) != null ? _b : "",
|
1078
1213
|
toolCalls,
|
1079
|
-
toolResults
|
1214
|
+
toolResults,
|
1215
|
+
finishReason: modelResponse.finishReason,
|
1216
|
+
usage: calculateTokenUsage(modelResponse.usage)
|
1080
1217
|
});
|
1081
1218
|
}
|
1082
1219
|
async function executeTools({
|
@@ -1107,6 +1244,8 @@ var GenerateTextResult = class {
|
|
1107
1244
|
this.text = options.text;
|
1108
1245
|
this.toolCalls = options.toolCalls;
|
1109
1246
|
this.toolResults = options.toolResults;
|
1247
|
+
this.finishReason = options.finishReason;
|
1248
|
+
this.usage = options.usage;
|
1110
1249
|
}
|
1111
1250
|
};
|
1112
1251
|
|
@@ -1232,238 +1371,6 @@ function runToolsTransformation({
|
|
1232
1371
|
});
|
1233
1372
|
}
|
1234
1373
|
|
1235
|
-
// shared/stream-parts.ts
|
1236
|
-
var textStreamPart = {
|
1237
|
-
code: "0",
|
1238
|
-
name: "text",
|
1239
|
-
parse: (value) => {
|
1240
|
-
if (typeof value !== "string") {
|
1241
|
-
throw new Error('"text" parts expect a string value.');
|
1242
|
-
}
|
1243
|
-
return { type: "text", value };
|
1244
|
-
}
|
1245
|
-
};
|
1246
|
-
var functionCallStreamPart = {
|
1247
|
-
code: "1",
|
1248
|
-
name: "function_call",
|
1249
|
-
parse: (value) => {
|
1250
|
-
if (value == null || typeof value !== "object" || !("function_call" in value) || typeof value.function_call !== "object" || value.function_call == null || !("name" in value.function_call) || !("arguments" in value.function_call) || typeof value.function_call.name !== "string" || typeof value.function_call.arguments !== "string") {
|
1251
|
-
throw new Error(
|
1252
|
-
'"function_call" parts expect an object with a "function_call" property.'
|
1253
|
-
);
|
1254
|
-
}
|
1255
|
-
return {
|
1256
|
-
type: "function_call",
|
1257
|
-
value
|
1258
|
-
};
|
1259
|
-
}
|
1260
|
-
};
|
1261
|
-
var dataStreamPart = {
|
1262
|
-
code: "2",
|
1263
|
-
name: "data",
|
1264
|
-
parse: (value) => {
|
1265
|
-
if (!Array.isArray(value)) {
|
1266
|
-
throw new Error('"data" parts expect an array value.');
|
1267
|
-
}
|
1268
|
-
return { type: "data", value };
|
1269
|
-
}
|
1270
|
-
};
|
1271
|
-
var errorStreamPart = {
|
1272
|
-
code: "3",
|
1273
|
-
name: "error",
|
1274
|
-
parse: (value) => {
|
1275
|
-
if (typeof value !== "string") {
|
1276
|
-
throw new Error('"error" parts expect a string value.');
|
1277
|
-
}
|
1278
|
-
return { type: "error", value };
|
1279
|
-
}
|
1280
|
-
};
|
1281
|
-
var assistantMessageStreamPart = {
|
1282
|
-
code: "4",
|
1283
|
-
name: "assistant_message",
|
1284
|
-
parse: (value) => {
|
1285
|
-
if (value == null || typeof value !== "object" || !("id" in value) || !("role" in value) || !("content" in value) || typeof value.id !== "string" || typeof value.role !== "string" || value.role !== "assistant" || !Array.isArray(value.content) || !value.content.every(
|
1286
|
-
(item) => item != null && typeof item === "object" && "type" in item && item.type === "text" && "text" in item && item.text != null && typeof item.text === "object" && "value" in item.text && typeof item.text.value === "string"
|
1287
|
-
)) {
|
1288
|
-
throw new Error(
|
1289
|
-
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
1290
|
-
);
|
1291
|
-
}
|
1292
|
-
return {
|
1293
|
-
type: "assistant_message",
|
1294
|
-
value
|
1295
|
-
};
|
1296
|
-
}
|
1297
|
-
};
|
1298
|
-
var assistantControlDataStreamPart = {
|
1299
|
-
code: "5",
|
1300
|
-
name: "assistant_control_data",
|
1301
|
-
parse: (value) => {
|
1302
|
-
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
1303
|
-
throw new Error(
|
1304
|
-
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
1305
|
-
);
|
1306
|
-
}
|
1307
|
-
return {
|
1308
|
-
type: "assistant_control_data",
|
1309
|
-
value: {
|
1310
|
-
threadId: value.threadId,
|
1311
|
-
messageId: value.messageId
|
1312
|
-
}
|
1313
|
-
};
|
1314
|
-
}
|
1315
|
-
};
|
1316
|
-
var dataMessageStreamPart = {
|
1317
|
-
code: "6",
|
1318
|
-
name: "data_message",
|
1319
|
-
parse: (value) => {
|
1320
|
-
if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
|
1321
|
-
throw new Error(
|
1322
|
-
'"data_message" parts expect an object with a "role" and "data" property.'
|
1323
|
-
);
|
1324
|
-
}
|
1325
|
-
return {
|
1326
|
-
type: "data_message",
|
1327
|
-
value
|
1328
|
-
};
|
1329
|
-
}
|
1330
|
-
};
|
1331
|
-
var toolCallStreamPart = {
|
1332
|
-
code: "7",
|
1333
|
-
name: "tool_calls",
|
1334
|
-
parse: (value) => {
|
1335
|
-
if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some((tc) => {
|
1336
|
-
tc == null || typeof tc !== "object" || !("id" in tc) || typeof tc.id !== "string" || !("type" in tc) || typeof tc.type !== "string" || !("function" in tc) || tc.function == null || typeof tc.function !== "object" || !("arguments" in tc.function) || typeof tc.function.name !== "string" || typeof tc.function.arguments !== "string";
|
1337
|
-
})) {
|
1338
|
-
throw new Error(
|
1339
|
-
'"tool_calls" parts expect an object with a ToolCallPayload.'
|
1340
|
-
);
|
1341
|
-
}
|
1342
|
-
return {
|
1343
|
-
type: "tool_calls",
|
1344
|
-
value
|
1345
|
-
};
|
1346
|
-
}
|
1347
|
-
};
|
1348
|
-
var messageAnnotationsStreamPart = {
|
1349
|
-
code: "8",
|
1350
|
-
name: "message_annotations",
|
1351
|
-
parse: (value) => {
|
1352
|
-
if (!Array.isArray(value)) {
|
1353
|
-
throw new Error('"message_annotations" parts expect an array value.');
|
1354
|
-
}
|
1355
|
-
return { type: "message_annotations", value };
|
1356
|
-
}
|
1357
|
-
};
|
1358
|
-
var streamParts = [
|
1359
|
-
textStreamPart,
|
1360
|
-
functionCallStreamPart,
|
1361
|
-
dataStreamPart,
|
1362
|
-
errorStreamPart,
|
1363
|
-
assistantMessageStreamPart,
|
1364
|
-
assistantControlDataStreamPart,
|
1365
|
-
dataMessageStreamPart,
|
1366
|
-
toolCallStreamPart,
|
1367
|
-
messageAnnotationsStreamPart
|
1368
|
-
];
|
1369
|
-
var streamPartsByCode = {
|
1370
|
-
[textStreamPart.code]: textStreamPart,
|
1371
|
-
[functionCallStreamPart.code]: functionCallStreamPart,
|
1372
|
-
[dataStreamPart.code]: dataStreamPart,
|
1373
|
-
[errorStreamPart.code]: errorStreamPart,
|
1374
|
-
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
1375
|
-
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
1376
|
-
[dataMessageStreamPart.code]: dataMessageStreamPart,
|
1377
|
-
[toolCallStreamPart.code]: toolCallStreamPart,
|
1378
|
-
[messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
|
1379
|
-
};
|
1380
|
-
var StreamStringPrefixes = {
|
1381
|
-
[textStreamPart.name]: textStreamPart.code,
|
1382
|
-
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
1383
|
-
[dataStreamPart.name]: dataStreamPart.code,
|
1384
|
-
[errorStreamPart.name]: errorStreamPart.code,
|
1385
|
-
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
1386
|
-
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
1387
|
-
[dataMessageStreamPart.name]: dataMessageStreamPart.code,
|
1388
|
-
[toolCallStreamPart.name]: toolCallStreamPart.code,
|
1389
|
-
[messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
|
1390
|
-
};
|
1391
|
-
var validCodes = streamParts.map((part) => part.code);
|
1392
|
-
function formatStreamPart(type, value) {
|
1393
|
-
const streamPart = streamParts.find((part) => part.name === type);
|
1394
|
-
if (!streamPart) {
|
1395
|
-
throw new Error(`Invalid stream part type: ${type}`);
|
1396
|
-
}
|
1397
|
-
return `${streamPart.code}:${JSON.stringify(value)}
|
1398
|
-
`;
|
1399
|
-
}
|
1400
|
-
|
1401
|
-
// shared/utils.ts
|
1402
|
-
import { customAlphabet } from "nanoid/non-secure";
|
1403
|
-
var nanoid2 = customAlphabet(
|
1404
|
-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
1405
|
-
7
|
1406
|
-
);
|
1407
|
-
var COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
1408
|
-
|
1409
|
-
// core/generate-text/stream-text-http-response.ts
|
1410
|
-
var StreamTextHttpResponse = class extends Response {
|
1411
|
-
constructor(messageStream) {
|
1412
|
-
super(
|
1413
|
-
messageStream.pipeThrough(
|
1414
|
-
new TransformStream({
|
1415
|
-
transform(chunk, controller) {
|
1416
|
-
var _a;
|
1417
|
-
switch (chunk.type) {
|
1418
|
-
case "error": {
|
1419
|
-
break;
|
1420
|
-
}
|
1421
|
-
case "text-delta": {
|
1422
|
-
controller.enqueue(formatStreamPart("text", chunk.textDelta));
|
1423
|
-
break;
|
1424
|
-
}
|
1425
|
-
case "tool-call": {
|
1426
|
-
controller.enqueue(
|
1427
|
-
formatStreamPart("tool_calls", {
|
1428
|
-
tool_calls: [
|
1429
|
-
{
|
1430
|
-
type: "function",
|
1431
|
-
id: (_a = chunk.toolCallId) != null ? _a : "",
|
1432
|
-
// TODO client need to support null id
|
1433
|
-
function: {
|
1434
|
-
name: chunk.toolName,
|
1435
|
-
arguments: JSON.stringify(chunk.args)
|
1436
|
-
}
|
1437
|
-
}
|
1438
|
-
]
|
1439
|
-
})
|
1440
|
-
);
|
1441
|
-
break;
|
1442
|
-
}
|
1443
|
-
case "tool-result": {
|
1444
|
-
break;
|
1445
|
-
}
|
1446
|
-
default: {
|
1447
|
-
const exhaustiveCheck = chunk;
|
1448
|
-
throw new Error(
|
1449
|
-
`Unhandled stream part type: ${exhaustiveCheck}`
|
1450
|
-
);
|
1451
|
-
}
|
1452
|
-
}
|
1453
|
-
}
|
1454
|
-
})
|
1455
|
-
),
|
1456
|
-
{
|
1457
|
-
status: 200,
|
1458
|
-
headers: {
|
1459
|
-
"Content-Type": "text/plain; charset=utf-8",
|
1460
|
-
[COMPLEX_HEADER]: "true"
|
1461
|
-
}
|
1462
|
-
}
|
1463
|
-
);
|
1464
|
-
}
|
1465
|
-
};
|
1466
|
-
|
1467
1374
|
// core/generate-text/stream-text.ts
|
1468
1375
|
async function streamText({
|
1469
1376
|
model,
|
@@ -1471,26 +1378,32 @@ async function streamText({
|
|
1471
1378
|
system,
|
1472
1379
|
prompt,
|
1473
1380
|
messages,
|
1381
|
+
maxRetries,
|
1382
|
+
abortSignal,
|
1474
1383
|
...settings
|
1475
1384
|
}) {
|
1476
|
-
const
|
1477
|
-
|
1478
|
-
|
1479
|
-
|
1480
|
-
type: "
|
1481
|
-
name,
|
1482
|
-
|
1483
|
-
|
1484
|
-
|
1485
|
-
|
1486
|
-
|
1487
|
-
|
1488
|
-
|
1489
|
-
|
1490
|
-
prompt
|
1491
|
-
|
1385
|
+
const retry = retryWithExponentialBackoff({ maxRetries });
|
1386
|
+
const { stream, warnings } = await retry(
|
1387
|
+
() => model.doStream({
|
1388
|
+
mode: {
|
1389
|
+
type: "regular",
|
1390
|
+
tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
|
1391
|
+
type: "function",
|
1392
|
+
name,
|
1393
|
+
description: tool2.description,
|
1394
|
+
parameters: zodToJsonSchema4(tool2.parameters)
|
1395
|
+
}))
|
1396
|
+
},
|
1397
|
+
...validateCallSettings(settings),
|
1398
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1399
|
+
prompt: convertToLanguageModelPrompt({
|
1400
|
+
system,
|
1401
|
+
prompt,
|
1402
|
+
messages
|
1403
|
+
}),
|
1404
|
+
abortSignal
|
1492
1405
|
})
|
1493
|
-
|
1406
|
+
);
|
1494
1407
|
const toolStream = runToolsTransformation({
|
1495
1408
|
tools,
|
1496
1409
|
generatorStream: stream
|
@@ -1546,9 +1459,6 @@ var StreamTextResult = class {
|
|
1546
1459
|
}
|
1547
1460
|
};
|
1548
1461
|
}
|
1549
|
-
toResponse() {
|
1550
|
-
return new StreamTextHttpResponse(this.rootStream);
|
1551
|
-
}
|
1552
1462
|
};
|
1553
1463
|
|
1554
1464
|
// core/tool/tool.ts
|
@@ -1559,7 +1469,6 @@ export {
|
|
1559
1469
|
GenerateObjectResult,
|
1560
1470
|
GenerateTextResult,
|
1561
1471
|
StreamObjectResult,
|
1562
|
-
StreamTextHttpResponse,
|
1563
1472
|
StreamTextResult,
|
1564
1473
|
convertDataContentToBase64String,
|
1565
1474
|
convertDataContentToUint8Array,
|