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.js
CHANGED
@@ -33,7 +33,6 @@ __export(core_exports, {
|
|
33
33
|
GenerateObjectResult: () => GenerateObjectResult,
|
34
34
|
GenerateTextResult: () => GenerateTextResult,
|
35
35
|
StreamObjectResult: () => StreamObjectResult,
|
36
|
-
StreamTextHttpResponse: () => StreamTextHttpResponse,
|
37
36
|
StreamTextResult: () => StreamTextResult,
|
38
37
|
convertDataContentToBase64String: () => convertDataContentToBase64String,
|
39
38
|
convertDataContentToUint8Array: () => convertDataContentToUint8Array,
|
@@ -125,6 +124,30 @@ var NoTextGeneratedError = class extends Error {
|
|
125
124
|
}
|
126
125
|
};
|
127
126
|
|
127
|
+
// ai-model-specification/errors/retry-error.ts
|
128
|
+
var RetryError = class extends Error {
|
129
|
+
constructor({
|
130
|
+
message,
|
131
|
+
reason,
|
132
|
+
errors
|
133
|
+
}) {
|
134
|
+
super(message);
|
135
|
+
this.name = "RetryError";
|
136
|
+
this.reason = reason;
|
137
|
+
this.errors = errors;
|
138
|
+
this.lastError = errors[errors.length - 1];
|
139
|
+
}
|
140
|
+
toJSON() {
|
141
|
+
return {
|
142
|
+
name: this.name,
|
143
|
+
message: this.message,
|
144
|
+
reason: this.reason,
|
145
|
+
lastError: this.lastError,
|
146
|
+
errors: this.errors
|
147
|
+
};
|
148
|
+
}
|
149
|
+
};
|
150
|
+
|
128
151
|
// ai-model-specification/errors/type-validation-error.ts
|
129
152
|
var TypeValidationError = class extends Error {
|
130
153
|
constructor({ value, cause }) {
|
@@ -214,6 +237,15 @@ function convertUint8ArrayToBase64(array) {
|
|
214
237
|
return globalThis.btoa(latin1string);
|
215
238
|
}
|
216
239
|
|
240
|
+
// core/generate-text/token-usage.ts
|
241
|
+
function calculateTokenUsage(usage) {
|
242
|
+
return {
|
243
|
+
promptTokens: usage.promptTokens,
|
244
|
+
completionTokens: usage.completionTokens,
|
245
|
+
totalTokens: usage.promptTokens + usage.completionTokens
|
246
|
+
};
|
247
|
+
}
|
248
|
+
|
217
249
|
// core/prompt/data-content.ts
|
218
250
|
function convertDataContentToBase64String(content) {
|
219
251
|
if (typeof content === "string") {
|
@@ -283,7 +315,7 @@ function convertToLanguageModelPrompt({
|
|
283
315
|
case "image": {
|
284
316
|
return {
|
285
317
|
type: "image",
|
286
|
-
image: convertDataContentToUint8Array(part.image),
|
318
|
+
image: part.image instanceof URL ? part.image : convertDataContentToUint8Array(part.image),
|
287
319
|
mimeType: part.mimeType
|
288
320
|
};
|
289
321
|
}
|
@@ -419,6 +451,66 @@ function validateCallSettings(settings) {
|
|
419
451
|
return settings;
|
420
452
|
}
|
421
453
|
|
454
|
+
// core/util/delay.ts
|
455
|
+
async function delay(delayInMs) {
|
456
|
+
return new Promise((resolve) => setTimeout(resolve, delayInMs));
|
457
|
+
}
|
458
|
+
|
459
|
+
// core/util/retry-with-exponential-backoff.ts
|
460
|
+
var retryWithExponentialBackoff = ({
|
461
|
+
maxRetries = 2,
|
462
|
+
initialDelayInMs = 2e3,
|
463
|
+
backoffFactor = 2
|
464
|
+
} = {}) => async (f) => _retryWithExponentialBackoff(f, {
|
465
|
+
maxRetries,
|
466
|
+
delayInMs: initialDelayInMs,
|
467
|
+
backoffFactor
|
468
|
+
});
|
469
|
+
async function _retryWithExponentialBackoff(f, {
|
470
|
+
maxRetries,
|
471
|
+
delayInMs,
|
472
|
+
backoffFactor
|
473
|
+
}, errors = []) {
|
474
|
+
try {
|
475
|
+
return await f();
|
476
|
+
} catch (error) {
|
477
|
+
if (maxRetries === 0) {
|
478
|
+
throw error;
|
479
|
+
}
|
480
|
+
const errorMessage = getErrorMessage(error);
|
481
|
+
const newErrors = [...errors, error];
|
482
|
+
const tryNumber = newErrors.length;
|
483
|
+
if (tryNumber > maxRetries) {
|
484
|
+
throw new RetryError({
|
485
|
+
message: `Failed after ${tryNumber} tries. Last error: ${errorMessage}`,
|
486
|
+
reason: "maxRetriesExceeded",
|
487
|
+
errors: newErrors
|
488
|
+
});
|
489
|
+
}
|
490
|
+
if (error instanceof Error) {
|
491
|
+
if (error.name === "AbortError") {
|
492
|
+
throw error;
|
493
|
+
}
|
494
|
+
if (
|
495
|
+
// deal with bundling duplication by using names
|
496
|
+
error.name === "ApiCallError" && error.isRetryable === true && tryNumber <= maxRetries
|
497
|
+
) {
|
498
|
+
await delay(delayInMs);
|
499
|
+
return _retryWithExponentialBackoff(
|
500
|
+
f,
|
501
|
+
{ maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
|
502
|
+
newErrors
|
503
|
+
);
|
504
|
+
}
|
505
|
+
}
|
506
|
+
throw new RetryError({
|
507
|
+
message: `Failed after ${tryNumber} tries with non-retryable error: '${errorMessage}'`,
|
508
|
+
reason: "errorNotRetryable",
|
509
|
+
errors: newErrors
|
510
|
+
});
|
511
|
+
}
|
512
|
+
}
|
513
|
+
|
422
514
|
// core/generate-object/inject-json-schema-into-system.ts
|
423
515
|
var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
|
424
516
|
var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
|
@@ -446,69 +538,89 @@ async function generateObject({
|
|
446
538
|
system,
|
447
539
|
prompt,
|
448
540
|
messages,
|
541
|
+
maxRetries,
|
542
|
+
abortSignal,
|
449
543
|
...settings
|
450
544
|
}) {
|
451
545
|
var _a, _b;
|
546
|
+
const retry = retryWithExponentialBackoff({ maxRetries });
|
452
547
|
const jsonSchema = (0, import_zod_to_json_schema.default)(schema);
|
453
548
|
if (mode === "auto" || mode == null) {
|
454
549
|
mode = model.defaultObjectGenerationMode;
|
455
550
|
}
|
456
551
|
let result;
|
552
|
+
let finishReason;
|
553
|
+
let usage;
|
457
554
|
switch (mode) {
|
458
555
|
case "json": {
|
459
|
-
const generateResult = await
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
556
|
+
const generateResult = await retry(
|
557
|
+
() => model.doGenerate({
|
558
|
+
mode: { type: "object-json" },
|
559
|
+
...validateCallSettings(settings),
|
560
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
561
|
+
prompt: convertToLanguageModelPrompt({
|
562
|
+
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
563
|
+
prompt,
|
564
|
+
messages
|
565
|
+
}),
|
566
|
+
abortSignal
|
467
567
|
})
|
468
|
-
|
568
|
+
);
|
469
569
|
if (generateResult.text === void 0) {
|
470
570
|
throw new NoTextGeneratedError();
|
471
571
|
}
|
472
572
|
result = generateResult.text;
|
573
|
+
finishReason = generateResult.finishReason;
|
574
|
+
usage = generateResult.usage;
|
473
575
|
break;
|
474
576
|
}
|
475
577
|
case "grammar": {
|
476
|
-
const generateResult = await
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
578
|
+
const generateResult = await retry(
|
579
|
+
() => model.doGenerate({
|
580
|
+
mode: { type: "object-grammar", schema: jsonSchema },
|
581
|
+
...settings,
|
582
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
583
|
+
prompt: convertToLanguageModelPrompt({
|
584
|
+
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
585
|
+
prompt,
|
586
|
+
messages
|
587
|
+
}),
|
588
|
+
abortSignal
|
484
589
|
})
|
485
|
-
|
590
|
+
);
|
486
591
|
if (generateResult.text === void 0) {
|
487
592
|
throw new NoTextGeneratedError();
|
488
593
|
}
|
489
594
|
result = generateResult.text;
|
595
|
+
finishReason = generateResult.finishReason;
|
596
|
+
usage = generateResult.usage;
|
490
597
|
break;
|
491
598
|
}
|
492
599
|
case "tool": {
|
493
|
-
const generateResult = await
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
600
|
+
const generateResult = await retry(
|
601
|
+
() => model.doGenerate({
|
602
|
+
mode: {
|
603
|
+
type: "object-tool",
|
604
|
+
tool: {
|
605
|
+
type: "function",
|
606
|
+
name: "json",
|
607
|
+
description: "Respond with a JSON object.",
|
608
|
+
parameters: jsonSchema
|
609
|
+
}
|
610
|
+
},
|
611
|
+
...settings,
|
612
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
613
|
+
prompt: convertToLanguageModelPrompt({ system, prompt, messages }),
|
614
|
+
abortSignal
|
615
|
+
})
|
616
|
+
);
|
507
617
|
const functionArgs = (_b = (_a = generateResult.toolCalls) == null ? void 0 : _a[0]) == null ? void 0 : _b.args;
|
508
618
|
if (functionArgs === void 0) {
|
509
619
|
throw new NoTextGeneratedError();
|
510
620
|
}
|
511
621
|
result = functionArgs;
|
622
|
+
finishReason = generateResult.finishReason;
|
623
|
+
usage = generateResult.usage;
|
512
624
|
break;
|
513
625
|
}
|
514
626
|
case void 0: {
|
@@ -524,12 +636,16 @@ async function generateObject({
|
|
524
636
|
throw parseResult.error;
|
525
637
|
}
|
526
638
|
return new GenerateObjectResult({
|
527
|
-
object: parseResult.value
|
639
|
+
object: parseResult.value,
|
640
|
+
finishReason,
|
641
|
+
usage: calculateTokenUsage(usage)
|
528
642
|
});
|
529
643
|
}
|
530
644
|
var GenerateObjectResult = class {
|
531
645
|
constructor(options) {
|
532
646
|
this.object = options.object;
|
647
|
+
this.finishReason = options.finishReason;
|
648
|
+
this.usage = options.usage;
|
533
649
|
}
|
534
650
|
};
|
535
651
|
|
@@ -916,8 +1032,11 @@ async function streamObject({
|
|
916
1032
|
system,
|
917
1033
|
prompt,
|
918
1034
|
messages,
|
1035
|
+
maxRetries,
|
1036
|
+
abortSignal,
|
919
1037
|
...settings
|
920
1038
|
}) {
|
1039
|
+
const retry = retryWithExponentialBackoff({ maxRetries });
|
921
1040
|
const jsonSchema = (0, import_zod_to_json_schema2.default)(schema);
|
922
1041
|
let modelStream;
|
923
1042
|
if (mode === "auto" || mode == null) {
|
@@ -925,16 +1044,19 @@ async function streamObject({
|
|
925
1044
|
}
|
926
1045
|
switch (mode) {
|
927
1046
|
case "json": {
|
928
|
-
const { stream, warnings } = await
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
1047
|
+
const { stream, warnings } = await retry(
|
1048
|
+
() => model.doStream({
|
1049
|
+
mode: { type: "object-json" },
|
1050
|
+
...validateCallSettings(settings),
|
1051
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1052
|
+
prompt: convertToLanguageModelPrompt({
|
1053
|
+
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
1054
|
+
prompt,
|
1055
|
+
messages
|
1056
|
+
}),
|
1057
|
+
abortSignal
|
936
1058
|
})
|
937
|
-
|
1059
|
+
);
|
938
1060
|
modelStream = stream.pipeThrough(
|
939
1061
|
new TransformStream({
|
940
1062
|
transform(chunk, controller) {
|
@@ -952,16 +1074,19 @@ async function streamObject({
|
|
952
1074
|
break;
|
953
1075
|
}
|
954
1076
|
case "grammar": {
|
955
|
-
const { stream, warnings } = await
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
1077
|
+
const { stream, warnings } = await retry(
|
1078
|
+
() => model.doStream({
|
1079
|
+
mode: { type: "object-grammar", schema: jsonSchema },
|
1080
|
+
...settings,
|
1081
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1082
|
+
prompt: convertToLanguageModelPrompt({
|
1083
|
+
system: injectJsonSchemaIntoSystem({ system, schema: jsonSchema }),
|
1084
|
+
prompt,
|
1085
|
+
messages
|
1086
|
+
}),
|
1087
|
+
abortSignal
|
963
1088
|
})
|
964
|
-
|
1089
|
+
);
|
965
1090
|
modelStream = stream.pipeThrough(
|
966
1091
|
new TransformStream({
|
967
1092
|
transform(chunk, controller) {
|
@@ -979,20 +1104,23 @@ async function streamObject({
|
|
979
1104
|
break;
|
980
1105
|
}
|
981
1106
|
case "tool": {
|
982
|
-
const { stream, warnings } = await
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
1107
|
+
const { stream, warnings } = await retry(
|
1108
|
+
() => model.doStream({
|
1109
|
+
mode: {
|
1110
|
+
type: "object-tool",
|
1111
|
+
tool: {
|
1112
|
+
type: "function",
|
1113
|
+
name: "json",
|
1114
|
+
description: "Respond with a JSON object.",
|
1115
|
+
parameters: jsonSchema
|
1116
|
+
}
|
1117
|
+
},
|
1118
|
+
...settings,
|
1119
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1120
|
+
prompt: convertToLanguageModelPrompt({ system, prompt, messages }),
|
1121
|
+
abortSignal
|
1122
|
+
})
|
1123
|
+
);
|
996
1124
|
modelStream = stream.pipeThrough(
|
997
1125
|
new TransformStream({
|
998
1126
|
transform(chunk, controller) {
|
@@ -1091,27 +1219,33 @@ async function generateText({
|
|
1091
1219
|
system,
|
1092
1220
|
prompt,
|
1093
1221
|
messages,
|
1222
|
+
maxRetries,
|
1223
|
+
abortSignal,
|
1094
1224
|
...settings
|
1095
1225
|
}) {
|
1096
1226
|
var _a, _b;
|
1097
|
-
const
|
1098
|
-
|
1099
|
-
|
1100
|
-
|
1101
|
-
type: "
|
1102
|
-
name,
|
1103
|
-
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1110
|
-
|
1111
|
-
prompt
|
1112
|
-
|
1227
|
+
const retry = retryWithExponentialBackoff({ maxRetries });
|
1228
|
+
const modelResponse = await retry(
|
1229
|
+
() => model.doGenerate({
|
1230
|
+
mode: {
|
1231
|
+
type: "regular",
|
1232
|
+
tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
|
1233
|
+
type: "function",
|
1234
|
+
name,
|
1235
|
+
description: tool2.description,
|
1236
|
+
parameters: (0, import_zod_to_json_schema3.default)(tool2.parameters)
|
1237
|
+
}))
|
1238
|
+
},
|
1239
|
+
...validateCallSettings(settings),
|
1240
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1241
|
+
prompt: convertToLanguageModelPrompt({
|
1242
|
+
system,
|
1243
|
+
prompt,
|
1244
|
+
messages
|
1245
|
+
}),
|
1246
|
+
abortSignal
|
1113
1247
|
})
|
1114
|
-
|
1248
|
+
);
|
1115
1249
|
const toolCalls = [];
|
1116
1250
|
for (const modelToolCall of (_a = modelResponse.toolCalls) != null ? _a : []) {
|
1117
1251
|
toolCalls.push(parseToolCall({ toolCall: modelToolCall, tools }));
|
@@ -1123,7 +1257,9 @@ async function generateText({
|
|
1123
1257
|
// they can check the length of the string:
|
1124
1258
|
text: (_b = modelResponse.text) != null ? _b : "",
|
1125
1259
|
toolCalls,
|
1126
|
-
toolResults
|
1260
|
+
toolResults,
|
1261
|
+
finishReason: modelResponse.finishReason,
|
1262
|
+
usage: calculateTokenUsage(modelResponse.usage)
|
1127
1263
|
});
|
1128
1264
|
}
|
1129
1265
|
async function executeTools({
|
@@ -1154,6 +1290,8 @@ var GenerateTextResult = class {
|
|
1154
1290
|
this.text = options.text;
|
1155
1291
|
this.toolCalls = options.toolCalls;
|
1156
1292
|
this.toolResults = options.toolResults;
|
1293
|
+
this.finishReason = options.finishReason;
|
1294
|
+
this.usage = options.usage;
|
1157
1295
|
}
|
1158
1296
|
};
|
1159
1297
|
|
@@ -1279,238 +1417,6 @@ function runToolsTransformation({
|
|
1279
1417
|
});
|
1280
1418
|
}
|
1281
1419
|
|
1282
|
-
// shared/stream-parts.ts
|
1283
|
-
var textStreamPart = {
|
1284
|
-
code: "0",
|
1285
|
-
name: "text",
|
1286
|
-
parse: (value) => {
|
1287
|
-
if (typeof value !== "string") {
|
1288
|
-
throw new Error('"text" parts expect a string value.');
|
1289
|
-
}
|
1290
|
-
return { type: "text", value };
|
1291
|
-
}
|
1292
|
-
};
|
1293
|
-
var functionCallStreamPart = {
|
1294
|
-
code: "1",
|
1295
|
-
name: "function_call",
|
1296
|
-
parse: (value) => {
|
1297
|
-
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") {
|
1298
|
-
throw new Error(
|
1299
|
-
'"function_call" parts expect an object with a "function_call" property.'
|
1300
|
-
);
|
1301
|
-
}
|
1302
|
-
return {
|
1303
|
-
type: "function_call",
|
1304
|
-
value
|
1305
|
-
};
|
1306
|
-
}
|
1307
|
-
};
|
1308
|
-
var dataStreamPart = {
|
1309
|
-
code: "2",
|
1310
|
-
name: "data",
|
1311
|
-
parse: (value) => {
|
1312
|
-
if (!Array.isArray(value)) {
|
1313
|
-
throw new Error('"data" parts expect an array value.');
|
1314
|
-
}
|
1315
|
-
return { type: "data", value };
|
1316
|
-
}
|
1317
|
-
};
|
1318
|
-
var errorStreamPart = {
|
1319
|
-
code: "3",
|
1320
|
-
name: "error",
|
1321
|
-
parse: (value) => {
|
1322
|
-
if (typeof value !== "string") {
|
1323
|
-
throw new Error('"error" parts expect a string value.');
|
1324
|
-
}
|
1325
|
-
return { type: "error", value };
|
1326
|
-
}
|
1327
|
-
};
|
1328
|
-
var assistantMessageStreamPart = {
|
1329
|
-
code: "4",
|
1330
|
-
name: "assistant_message",
|
1331
|
-
parse: (value) => {
|
1332
|
-
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(
|
1333
|
-
(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"
|
1334
|
-
)) {
|
1335
|
-
throw new Error(
|
1336
|
-
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
1337
|
-
);
|
1338
|
-
}
|
1339
|
-
return {
|
1340
|
-
type: "assistant_message",
|
1341
|
-
value
|
1342
|
-
};
|
1343
|
-
}
|
1344
|
-
};
|
1345
|
-
var assistantControlDataStreamPart = {
|
1346
|
-
code: "5",
|
1347
|
-
name: "assistant_control_data",
|
1348
|
-
parse: (value) => {
|
1349
|
-
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
1350
|
-
throw new Error(
|
1351
|
-
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
1352
|
-
);
|
1353
|
-
}
|
1354
|
-
return {
|
1355
|
-
type: "assistant_control_data",
|
1356
|
-
value: {
|
1357
|
-
threadId: value.threadId,
|
1358
|
-
messageId: value.messageId
|
1359
|
-
}
|
1360
|
-
};
|
1361
|
-
}
|
1362
|
-
};
|
1363
|
-
var dataMessageStreamPart = {
|
1364
|
-
code: "6",
|
1365
|
-
name: "data_message",
|
1366
|
-
parse: (value) => {
|
1367
|
-
if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
|
1368
|
-
throw new Error(
|
1369
|
-
'"data_message" parts expect an object with a "role" and "data" property.'
|
1370
|
-
);
|
1371
|
-
}
|
1372
|
-
return {
|
1373
|
-
type: "data_message",
|
1374
|
-
value
|
1375
|
-
};
|
1376
|
-
}
|
1377
|
-
};
|
1378
|
-
var toolCallStreamPart = {
|
1379
|
-
code: "7",
|
1380
|
-
name: "tool_calls",
|
1381
|
-
parse: (value) => {
|
1382
|
-
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) => {
|
1383
|
-
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";
|
1384
|
-
})) {
|
1385
|
-
throw new Error(
|
1386
|
-
'"tool_calls" parts expect an object with a ToolCallPayload.'
|
1387
|
-
);
|
1388
|
-
}
|
1389
|
-
return {
|
1390
|
-
type: "tool_calls",
|
1391
|
-
value
|
1392
|
-
};
|
1393
|
-
}
|
1394
|
-
};
|
1395
|
-
var messageAnnotationsStreamPart = {
|
1396
|
-
code: "8",
|
1397
|
-
name: "message_annotations",
|
1398
|
-
parse: (value) => {
|
1399
|
-
if (!Array.isArray(value)) {
|
1400
|
-
throw new Error('"message_annotations" parts expect an array value.');
|
1401
|
-
}
|
1402
|
-
return { type: "message_annotations", value };
|
1403
|
-
}
|
1404
|
-
};
|
1405
|
-
var streamParts = [
|
1406
|
-
textStreamPart,
|
1407
|
-
functionCallStreamPart,
|
1408
|
-
dataStreamPart,
|
1409
|
-
errorStreamPart,
|
1410
|
-
assistantMessageStreamPart,
|
1411
|
-
assistantControlDataStreamPart,
|
1412
|
-
dataMessageStreamPart,
|
1413
|
-
toolCallStreamPart,
|
1414
|
-
messageAnnotationsStreamPart
|
1415
|
-
];
|
1416
|
-
var streamPartsByCode = {
|
1417
|
-
[textStreamPart.code]: textStreamPart,
|
1418
|
-
[functionCallStreamPart.code]: functionCallStreamPart,
|
1419
|
-
[dataStreamPart.code]: dataStreamPart,
|
1420
|
-
[errorStreamPart.code]: errorStreamPart,
|
1421
|
-
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
1422
|
-
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
1423
|
-
[dataMessageStreamPart.code]: dataMessageStreamPart,
|
1424
|
-
[toolCallStreamPart.code]: toolCallStreamPart,
|
1425
|
-
[messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
|
1426
|
-
};
|
1427
|
-
var StreamStringPrefixes = {
|
1428
|
-
[textStreamPart.name]: textStreamPart.code,
|
1429
|
-
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
1430
|
-
[dataStreamPart.name]: dataStreamPart.code,
|
1431
|
-
[errorStreamPart.name]: errorStreamPart.code,
|
1432
|
-
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
1433
|
-
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
1434
|
-
[dataMessageStreamPart.name]: dataMessageStreamPart.code,
|
1435
|
-
[toolCallStreamPart.name]: toolCallStreamPart.code,
|
1436
|
-
[messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
|
1437
|
-
};
|
1438
|
-
var validCodes = streamParts.map((part) => part.code);
|
1439
|
-
function formatStreamPart(type, value) {
|
1440
|
-
const streamPart = streamParts.find((part) => part.name === type);
|
1441
|
-
if (!streamPart) {
|
1442
|
-
throw new Error(`Invalid stream part type: ${type}`);
|
1443
|
-
}
|
1444
|
-
return `${streamPart.code}:${JSON.stringify(value)}
|
1445
|
-
`;
|
1446
|
-
}
|
1447
|
-
|
1448
|
-
// shared/utils.ts
|
1449
|
-
var import_non_secure = require("nanoid/non-secure");
|
1450
|
-
var nanoid2 = (0, import_non_secure.customAlphabet)(
|
1451
|
-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
1452
|
-
7
|
1453
|
-
);
|
1454
|
-
var COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
1455
|
-
|
1456
|
-
// core/generate-text/stream-text-http-response.ts
|
1457
|
-
var StreamTextHttpResponse = class extends Response {
|
1458
|
-
constructor(messageStream) {
|
1459
|
-
super(
|
1460
|
-
messageStream.pipeThrough(
|
1461
|
-
new TransformStream({
|
1462
|
-
transform(chunk, controller) {
|
1463
|
-
var _a;
|
1464
|
-
switch (chunk.type) {
|
1465
|
-
case "error": {
|
1466
|
-
break;
|
1467
|
-
}
|
1468
|
-
case "text-delta": {
|
1469
|
-
controller.enqueue(formatStreamPart("text", chunk.textDelta));
|
1470
|
-
break;
|
1471
|
-
}
|
1472
|
-
case "tool-call": {
|
1473
|
-
controller.enqueue(
|
1474
|
-
formatStreamPart("tool_calls", {
|
1475
|
-
tool_calls: [
|
1476
|
-
{
|
1477
|
-
type: "function",
|
1478
|
-
id: (_a = chunk.toolCallId) != null ? _a : "",
|
1479
|
-
// TODO client need to support null id
|
1480
|
-
function: {
|
1481
|
-
name: chunk.toolName,
|
1482
|
-
arguments: JSON.stringify(chunk.args)
|
1483
|
-
}
|
1484
|
-
}
|
1485
|
-
]
|
1486
|
-
})
|
1487
|
-
);
|
1488
|
-
break;
|
1489
|
-
}
|
1490
|
-
case "tool-result": {
|
1491
|
-
break;
|
1492
|
-
}
|
1493
|
-
default: {
|
1494
|
-
const exhaustiveCheck = chunk;
|
1495
|
-
throw new Error(
|
1496
|
-
`Unhandled stream part type: ${exhaustiveCheck}`
|
1497
|
-
);
|
1498
|
-
}
|
1499
|
-
}
|
1500
|
-
}
|
1501
|
-
})
|
1502
|
-
),
|
1503
|
-
{
|
1504
|
-
status: 200,
|
1505
|
-
headers: {
|
1506
|
-
"Content-Type": "text/plain; charset=utf-8",
|
1507
|
-
[COMPLEX_HEADER]: "true"
|
1508
|
-
}
|
1509
|
-
}
|
1510
|
-
);
|
1511
|
-
}
|
1512
|
-
};
|
1513
|
-
|
1514
1420
|
// core/generate-text/stream-text.ts
|
1515
1421
|
async function streamText({
|
1516
1422
|
model,
|
@@ -1518,26 +1424,32 @@ async function streamText({
|
|
1518
1424
|
system,
|
1519
1425
|
prompt,
|
1520
1426
|
messages,
|
1427
|
+
maxRetries,
|
1428
|
+
abortSignal,
|
1521
1429
|
...settings
|
1522
1430
|
}) {
|
1523
|
-
const
|
1524
|
-
|
1525
|
-
|
1526
|
-
|
1527
|
-
type: "
|
1528
|
-
name,
|
1529
|
-
|
1530
|
-
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1534
|
-
|
1535
|
-
|
1536
|
-
|
1537
|
-
prompt
|
1538
|
-
|
1431
|
+
const retry = retryWithExponentialBackoff({ maxRetries });
|
1432
|
+
const { stream, warnings } = await retry(
|
1433
|
+
() => model.doStream({
|
1434
|
+
mode: {
|
1435
|
+
type: "regular",
|
1436
|
+
tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
|
1437
|
+
type: "function",
|
1438
|
+
name,
|
1439
|
+
description: tool2.description,
|
1440
|
+
parameters: (0, import_zod_to_json_schema4.default)(tool2.parameters)
|
1441
|
+
}))
|
1442
|
+
},
|
1443
|
+
...validateCallSettings(settings),
|
1444
|
+
inputFormat: getInputFormat({ prompt, messages }),
|
1445
|
+
prompt: convertToLanguageModelPrompt({
|
1446
|
+
system,
|
1447
|
+
prompt,
|
1448
|
+
messages
|
1449
|
+
}),
|
1450
|
+
abortSignal
|
1539
1451
|
})
|
1540
|
-
|
1452
|
+
);
|
1541
1453
|
const toolStream = runToolsTransformation({
|
1542
1454
|
tools,
|
1543
1455
|
generatorStream: stream
|
@@ -1593,9 +1505,6 @@ var StreamTextResult = class {
|
|
1593
1505
|
}
|
1594
1506
|
};
|
1595
1507
|
}
|
1596
|
-
toResponse() {
|
1597
|
-
return new StreamTextHttpResponse(this.rootStream);
|
1598
|
-
}
|
1599
1508
|
};
|
1600
1509
|
|
1601
1510
|
// core/tool/tool.ts
|
@@ -1607,7 +1516,6 @@ function tool(tool2) {
|
|
1607
1516
|
GenerateObjectResult,
|
1608
1517
|
GenerateTextResult,
|
1609
1518
|
StreamObjectResult,
|
1610
|
-
StreamTextHttpResponse,
|
1611
1519
|
StreamTextResult,
|
1612
1520
|
convertDataContentToBase64String,
|
1613
1521
|
convertDataContentToUint8Array,
|