ai 6.0.0-beta.99 → 6.0.1
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 +830 -0
- package/README.md +17 -13
- package/dist/index.d.mts +924 -624
- package/dist/index.d.ts +924 -624
- package/dist/index.js +1394 -976
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1209 -785
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +103 -6
- package/dist/internal/index.d.ts +103 -6
- package/dist/internal/index.js +124 -107
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +97 -79
- package/dist/internal/index.mjs.map +1 -1
- package/dist/test/index.d.mts +19 -19
- package/dist/test/index.d.ts +19 -19
- package/dist/test/index.js +2 -2
- package/dist/test/index.js.map +1 -1
- package/dist/test/index.mjs +2 -2
- package/dist/test/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __export = (target, all) => {
|
|
3
|
-
for (var
|
|
4
|
-
__defProp(target,
|
|
3
|
+
for (var name14 in all)
|
|
4
|
+
__defProp(target, name14, { get: all[name14], enumerable: true });
|
|
5
5
|
};
|
|
6
6
|
|
|
7
7
|
// src/index.ts
|
|
@@ -24,79 +24,9 @@ import {
|
|
|
24
24
|
withUserAgentSuffix as withUserAgentSuffix2
|
|
25
25
|
} from "@ai-sdk/provider-utils";
|
|
26
26
|
|
|
27
|
-
// src/logger/log-warnings.ts
|
|
28
|
-
function formatWarning({
|
|
29
|
-
warning,
|
|
30
|
-
provider,
|
|
31
|
-
model
|
|
32
|
-
}) {
|
|
33
|
-
const prefix = `AI SDK Warning (${provider} / ${model}):`;
|
|
34
|
-
switch (warning.type) {
|
|
35
|
-
case "unsupported-setting": {
|
|
36
|
-
let message = `${prefix} The "${warning.setting}" setting is not supported.`;
|
|
37
|
-
if (warning.details) {
|
|
38
|
-
message += ` ${warning.details}`;
|
|
39
|
-
}
|
|
40
|
-
return message;
|
|
41
|
-
}
|
|
42
|
-
case "compatibility": {
|
|
43
|
-
let message = `${prefix} The "${warning.feature}" feature is not fully supported.`;
|
|
44
|
-
if (warning.details) {
|
|
45
|
-
message += ` ${warning.details}`;
|
|
46
|
-
}
|
|
47
|
-
return message;
|
|
48
|
-
}
|
|
49
|
-
case "unsupported-tool": {
|
|
50
|
-
const toolName = "name" in warning.tool ? warning.tool.name : "unknown tool";
|
|
51
|
-
let message = `${prefix} The tool "${toolName}" is not supported.`;
|
|
52
|
-
if (warning.details) {
|
|
53
|
-
message += ` ${warning.details}`;
|
|
54
|
-
}
|
|
55
|
-
return message;
|
|
56
|
-
}
|
|
57
|
-
case "other": {
|
|
58
|
-
return `${prefix} ${warning.message}`;
|
|
59
|
-
}
|
|
60
|
-
default: {
|
|
61
|
-
return `${prefix} ${JSON.stringify(warning, null, 2)}`;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
var FIRST_WARNING_INFO_MESSAGE = "AI SDK Warning System: To turn off warning logging, set the AI_SDK_LOG_WARNINGS global to false.";
|
|
66
|
-
var hasLoggedBefore = false;
|
|
67
|
-
var logWarnings = (options) => {
|
|
68
|
-
if (options.warnings.length === 0) {
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
const logger = globalThis.AI_SDK_LOG_WARNINGS;
|
|
72
|
-
if (logger === false) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
if (typeof logger === "function") {
|
|
76
|
-
logger(options);
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
if (!hasLoggedBefore) {
|
|
80
|
-
hasLoggedBefore = true;
|
|
81
|
-
console.info(FIRST_WARNING_INFO_MESSAGE);
|
|
82
|
-
}
|
|
83
|
-
for (const warning of options.warnings) {
|
|
84
|
-
console.warn(
|
|
85
|
-
formatWarning({
|
|
86
|
-
warning,
|
|
87
|
-
provider: options.provider,
|
|
88
|
-
model: options.model
|
|
89
|
-
})
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
// src/model/resolve-model.ts
|
|
95
|
-
import { gateway } from "@ai-sdk/gateway";
|
|
96
|
-
|
|
97
27
|
// src/error/index.ts
|
|
98
28
|
import {
|
|
99
|
-
AISDKError as
|
|
29
|
+
AISDKError as AISDKError15,
|
|
100
30
|
APICallError,
|
|
101
31
|
EmptyResponseBodyError,
|
|
102
32
|
InvalidPromptError,
|
|
@@ -388,25 +318,26 @@ var MessageConversionError = class extends AISDKError13 {
|
|
|
388
318
|
};
|
|
389
319
|
_a11 = symbol11;
|
|
390
320
|
|
|
391
|
-
// src/
|
|
321
|
+
// src/error/index.ts
|
|
322
|
+
import { DownloadError } from "@ai-sdk/provider-utils";
|
|
323
|
+
|
|
324
|
+
// src/util/retry-error.ts
|
|
392
325
|
import { AISDKError as AISDKError14 } from "@ai-sdk/provider";
|
|
393
|
-
var name12 = "
|
|
326
|
+
var name12 = "AI_RetryError";
|
|
394
327
|
var marker12 = `vercel.ai.error.${name12}`;
|
|
395
328
|
var symbol12 = Symbol.for(marker12);
|
|
396
329
|
var _a12;
|
|
397
|
-
var
|
|
330
|
+
var RetryError = class extends AISDKError14 {
|
|
398
331
|
constructor({
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
cause,
|
|
403
|
-
message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}`
|
|
332
|
+
message,
|
|
333
|
+
reason,
|
|
334
|
+
errors
|
|
404
335
|
}) {
|
|
405
|
-
super({ name: name12, message
|
|
336
|
+
super({ name: name12, message });
|
|
406
337
|
this[_a12] = true;
|
|
407
|
-
this.
|
|
408
|
-
this.
|
|
409
|
-
this.
|
|
338
|
+
this.reason = reason;
|
|
339
|
+
this.errors = errors;
|
|
340
|
+
this.lastError = errors[errors.length - 1];
|
|
410
341
|
}
|
|
411
342
|
static isInstance(error) {
|
|
412
343
|
return AISDKError14.hasMarker(error, marker12);
|
|
@@ -414,35 +345,95 @@ var DownloadError = class extends AISDKError14 {
|
|
|
414
345
|
};
|
|
415
346
|
_a12 = symbol12;
|
|
416
347
|
|
|
417
|
-
// src/
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
348
|
+
// src/logger/log-warnings.ts
|
|
349
|
+
function formatWarning({
|
|
350
|
+
warning,
|
|
351
|
+
provider,
|
|
352
|
+
model
|
|
353
|
+
}) {
|
|
354
|
+
const prefix = `AI SDK Warning (${provider} / ${model}):`;
|
|
355
|
+
switch (warning.type) {
|
|
356
|
+
case "unsupported": {
|
|
357
|
+
let message = `${prefix} The feature "${warning.feature}" is not supported.`;
|
|
358
|
+
if (warning.details) {
|
|
359
|
+
message += ` ${warning.details}`;
|
|
360
|
+
}
|
|
361
|
+
return message;
|
|
362
|
+
}
|
|
363
|
+
case "compatibility": {
|
|
364
|
+
let message = `${prefix} The feature "${warning.feature}" is used in a compatibility mode.`;
|
|
365
|
+
if (warning.details) {
|
|
366
|
+
message += ` ${warning.details}`;
|
|
367
|
+
}
|
|
368
|
+
return message;
|
|
369
|
+
}
|
|
370
|
+
case "other": {
|
|
371
|
+
return `${prefix} ${warning.message}`;
|
|
372
|
+
}
|
|
373
|
+
default: {
|
|
374
|
+
return `${prefix} ${JSON.stringify(warning, null, 2)}`;
|
|
375
|
+
}
|
|
434
376
|
}
|
|
435
|
-
|
|
436
|
-
|
|
377
|
+
}
|
|
378
|
+
var FIRST_WARNING_INFO_MESSAGE = "AI SDK Warning System: To turn off warning logging, set the AI_SDK_LOG_WARNINGS global to false.";
|
|
379
|
+
var hasLoggedBefore = false;
|
|
380
|
+
var logWarnings = (options) => {
|
|
381
|
+
if (options.warnings.length === 0) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const logger = globalThis.AI_SDK_LOG_WARNINGS;
|
|
385
|
+
if (logger === false) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
if (typeof logger === "function") {
|
|
389
|
+
logger(options);
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
if (!hasLoggedBefore) {
|
|
393
|
+
hasLoggedBefore = true;
|
|
394
|
+
console.info(FIRST_WARNING_INFO_MESSAGE);
|
|
395
|
+
}
|
|
396
|
+
for (const warning of options.warnings) {
|
|
397
|
+
console.warn(
|
|
398
|
+
formatWarning({
|
|
399
|
+
warning,
|
|
400
|
+
provider: options.provider,
|
|
401
|
+
model: options.model
|
|
402
|
+
})
|
|
403
|
+
);
|
|
437
404
|
}
|
|
438
405
|
};
|
|
439
|
-
|
|
406
|
+
|
|
407
|
+
// src/model/resolve-model.ts
|
|
408
|
+
import { gateway } from "@ai-sdk/gateway";
|
|
409
|
+
|
|
410
|
+
// src/util/log-v2-compatibility-warning.ts
|
|
411
|
+
function logV2CompatibilityWarning({
|
|
412
|
+
provider,
|
|
413
|
+
modelId
|
|
414
|
+
}) {
|
|
415
|
+
logWarnings({
|
|
416
|
+
warnings: [
|
|
417
|
+
{
|
|
418
|
+
type: "compatibility",
|
|
419
|
+
feature: "specificationVersion",
|
|
420
|
+
details: `Using v2 specification compatibility mode. Some features may not be available.`
|
|
421
|
+
}
|
|
422
|
+
],
|
|
423
|
+
provider,
|
|
424
|
+
model: modelId
|
|
425
|
+
});
|
|
426
|
+
}
|
|
440
427
|
|
|
441
428
|
// src/model/as-embedding-model-v3.ts
|
|
442
429
|
function asEmbeddingModelV3(model) {
|
|
443
430
|
if (model.specificationVersion === "v3") {
|
|
444
431
|
return model;
|
|
445
432
|
}
|
|
433
|
+
logV2CompatibilityWarning({
|
|
434
|
+
provider: model.provider,
|
|
435
|
+
modelId: model.modelId
|
|
436
|
+
});
|
|
446
437
|
return new Proxy(model, {
|
|
447
438
|
get(target, prop) {
|
|
448
439
|
if (prop === "specificationVersion")
|
|
@@ -452,11 +443,15 @@ function asEmbeddingModelV3(model) {
|
|
|
452
443
|
});
|
|
453
444
|
}
|
|
454
445
|
|
|
455
|
-
// src/model/as-
|
|
456
|
-
function
|
|
446
|
+
// src/model/as-image-model-v3.ts
|
|
447
|
+
function asImageModelV3(model) {
|
|
457
448
|
if (model.specificationVersion === "v3") {
|
|
458
449
|
return model;
|
|
459
450
|
}
|
|
451
|
+
logV2CompatibilityWarning({
|
|
452
|
+
provider: model.provider,
|
|
453
|
+
modelId: model.modelId
|
|
454
|
+
});
|
|
460
455
|
return new Proxy(model, {
|
|
461
456
|
get(target, prop) {
|
|
462
457
|
if (prop === "specificationVersion")
|
|
@@ -466,11 +461,94 @@ function asLanguageModelV3(model) {
|
|
|
466
461
|
});
|
|
467
462
|
}
|
|
468
463
|
|
|
464
|
+
// src/model/as-language-model-v3.ts
|
|
465
|
+
function asLanguageModelV3(model) {
|
|
466
|
+
if (model.specificationVersion === "v3") {
|
|
467
|
+
return model;
|
|
468
|
+
}
|
|
469
|
+
logV2CompatibilityWarning({
|
|
470
|
+
provider: model.provider,
|
|
471
|
+
modelId: model.modelId
|
|
472
|
+
});
|
|
473
|
+
return new Proxy(model, {
|
|
474
|
+
get(target, prop) {
|
|
475
|
+
switch (prop) {
|
|
476
|
+
case "specificationVersion":
|
|
477
|
+
return "v3";
|
|
478
|
+
case "doGenerate":
|
|
479
|
+
return async (...args) => {
|
|
480
|
+
const result = await target.doGenerate(...args);
|
|
481
|
+
return {
|
|
482
|
+
...result,
|
|
483
|
+
finishReason: convertV2FinishReasonToV3(result.finishReason),
|
|
484
|
+
usage: convertV2UsageToV3(result.usage)
|
|
485
|
+
};
|
|
486
|
+
};
|
|
487
|
+
case "doStream":
|
|
488
|
+
return async (...args) => {
|
|
489
|
+
const result = await target.doStream(...args);
|
|
490
|
+
return {
|
|
491
|
+
...result,
|
|
492
|
+
stream: convertV2StreamToV3(result.stream)
|
|
493
|
+
};
|
|
494
|
+
};
|
|
495
|
+
default:
|
|
496
|
+
return target[prop];
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
function convertV2StreamToV3(stream) {
|
|
502
|
+
return stream.pipeThrough(
|
|
503
|
+
new TransformStream({
|
|
504
|
+
transform(chunk, controller) {
|
|
505
|
+
switch (chunk.type) {
|
|
506
|
+
case "finish":
|
|
507
|
+
controller.enqueue({
|
|
508
|
+
...chunk,
|
|
509
|
+
finishReason: convertV2FinishReasonToV3(chunk.finishReason),
|
|
510
|
+
usage: convertV2UsageToV3(chunk.usage)
|
|
511
|
+
});
|
|
512
|
+
break;
|
|
513
|
+
default:
|
|
514
|
+
controller.enqueue(chunk);
|
|
515
|
+
break;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
})
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
function convertV2FinishReasonToV3(finishReason) {
|
|
522
|
+
return {
|
|
523
|
+
unified: finishReason === "unknown" ? "other" : finishReason,
|
|
524
|
+
raw: void 0
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
function convertV2UsageToV3(usage) {
|
|
528
|
+
return {
|
|
529
|
+
inputTokens: {
|
|
530
|
+
total: usage.inputTokens,
|
|
531
|
+
noCache: void 0,
|
|
532
|
+
cacheRead: usage.cachedInputTokens,
|
|
533
|
+
cacheWrite: void 0
|
|
534
|
+
},
|
|
535
|
+
outputTokens: {
|
|
536
|
+
total: usage.outputTokens,
|
|
537
|
+
text: void 0,
|
|
538
|
+
reasoning: usage.reasoningTokens
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
|
|
469
543
|
// src/model/as-speech-model-v3.ts
|
|
470
544
|
function asSpeechModelV3(model) {
|
|
471
545
|
if (model.specificationVersion === "v3") {
|
|
472
546
|
return model;
|
|
473
547
|
}
|
|
548
|
+
logV2CompatibilityWarning({
|
|
549
|
+
provider: model.provider,
|
|
550
|
+
modelId: model.modelId
|
|
551
|
+
});
|
|
474
552
|
return new Proxy(model, {
|
|
475
553
|
get(target, prop) {
|
|
476
554
|
if (prop === "specificationVersion")
|
|
@@ -485,6 +563,10 @@ function asTranscriptionModelV3(model) {
|
|
|
485
563
|
if (model.specificationVersion === "v3") {
|
|
486
564
|
return model;
|
|
487
565
|
}
|
|
566
|
+
logV2CompatibilityWarning({
|
|
567
|
+
provider: model.provider,
|
|
568
|
+
modelId: model.modelId
|
|
569
|
+
});
|
|
488
570
|
return new Proxy(model, {
|
|
489
571
|
get(target, prop) {
|
|
490
572
|
if (prop === "specificationVersion")
|
|
@@ -521,12 +603,10 @@ function resolveEmbeddingModel(model) {
|
|
|
521
603
|
}
|
|
522
604
|
return asEmbeddingModelV3(model);
|
|
523
605
|
}
|
|
524
|
-
return getGlobalProvider().
|
|
525
|
-
model
|
|
526
|
-
);
|
|
606
|
+
return getGlobalProvider().embeddingModel(model);
|
|
527
607
|
}
|
|
528
608
|
function resolveTranscriptionModel(model) {
|
|
529
|
-
var
|
|
609
|
+
var _a14, _b;
|
|
530
610
|
if (typeof model !== "string") {
|
|
531
611
|
if (model.specificationVersion !== "v3" && model.specificationVersion !== "v2") {
|
|
532
612
|
const unsupportedModel = model;
|
|
@@ -538,10 +618,10 @@ function resolveTranscriptionModel(model) {
|
|
|
538
618
|
}
|
|
539
619
|
return asTranscriptionModelV3(model);
|
|
540
620
|
}
|
|
541
|
-
return (_b = (
|
|
621
|
+
return (_b = (_a14 = getGlobalProvider()).transcriptionModel) == null ? void 0 : _b.call(_a14, model);
|
|
542
622
|
}
|
|
543
623
|
function resolveSpeechModel(model) {
|
|
544
|
-
var
|
|
624
|
+
var _a14, _b;
|
|
545
625
|
if (typeof model !== "string") {
|
|
546
626
|
if (model.specificationVersion !== "v3" && model.specificationVersion !== "v2") {
|
|
547
627
|
const unsupportedModel = model;
|
|
@@ -553,11 +633,25 @@ function resolveSpeechModel(model) {
|
|
|
553
633
|
}
|
|
554
634
|
return asSpeechModelV3(model);
|
|
555
635
|
}
|
|
556
|
-
return (_b = (
|
|
636
|
+
return (_b = (_a14 = getGlobalProvider()).speechModel) == null ? void 0 : _b.call(_a14, model);
|
|
637
|
+
}
|
|
638
|
+
function resolveImageModel(model) {
|
|
639
|
+
if (typeof model !== "string") {
|
|
640
|
+
if (model.specificationVersion !== "v3" && model.specificationVersion !== "v2") {
|
|
641
|
+
const unsupportedModel = model;
|
|
642
|
+
throw new UnsupportedModelVersionError({
|
|
643
|
+
version: unsupportedModel.specificationVersion,
|
|
644
|
+
provider: unsupportedModel.provider,
|
|
645
|
+
modelId: unsupportedModel.modelId
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
return asImageModelV3(model);
|
|
649
|
+
}
|
|
650
|
+
return getGlobalProvider().imageModel(model);
|
|
557
651
|
}
|
|
558
652
|
function getGlobalProvider() {
|
|
559
|
-
var
|
|
560
|
-
return (
|
|
653
|
+
var _a14;
|
|
654
|
+
return (_a14 = globalThis.AI_SDK_DEFAULT_PROVIDER) != null ? _a14 : gateway;
|
|
561
655
|
}
|
|
562
656
|
|
|
563
657
|
// src/prompt/convert-to-language-model-prompt.ts
|
|
@@ -751,17 +845,18 @@ function detectMediaType({
|
|
|
751
845
|
}
|
|
752
846
|
|
|
753
847
|
// src/util/download/download.ts
|
|
848
|
+
import { DownloadError as DownloadError2 } from "@ai-sdk/provider-utils";
|
|
754
849
|
import {
|
|
755
850
|
withUserAgentSuffix,
|
|
756
851
|
getRuntimeEnvironmentUserAgent
|
|
757
852
|
} from "@ai-sdk/provider-utils";
|
|
758
853
|
|
|
759
854
|
// src/version.ts
|
|
760
|
-
var VERSION = true ? "6.0.
|
|
855
|
+
var VERSION = true ? "6.0.1" : "0.0.0-test";
|
|
761
856
|
|
|
762
857
|
// src/util/download/download.ts
|
|
763
858
|
var download = async ({ url }) => {
|
|
764
|
-
var
|
|
859
|
+
var _a14;
|
|
765
860
|
const urlText = url.toString();
|
|
766
861
|
try {
|
|
767
862
|
const response = await fetch(urlText, {
|
|
@@ -772,7 +867,7 @@ var download = async ({ url }) => {
|
|
|
772
867
|
)
|
|
773
868
|
});
|
|
774
869
|
if (!response.ok) {
|
|
775
|
-
throw new
|
|
870
|
+
throw new DownloadError2({
|
|
776
871
|
url: urlText,
|
|
777
872
|
statusCode: response.status,
|
|
778
873
|
statusText: response.statusText
|
|
@@ -780,13 +875,13 @@ var download = async ({ url }) => {
|
|
|
780
875
|
}
|
|
781
876
|
return {
|
|
782
877
|
data: new Uint8Array(await response.arrayBuffer()),
|
|
783
|
-
mediaType: (
|
|
878
|
+
mediaType: (_a14 = response.headers.get("content-type")) != null ? _a14 : void 0
|
|
784
879
|
};
|
|
785
880
|
} catch (error) {
|
|
786
|
-
if (
|
|
881
|
+
if (DownloadError2.isInstance(error)) {
|
|
787
882
|
throw error;
|
|
788
883
|
}
|
|
789
|
-
throw new
|
|
884
|
+
throw new DownloadError2({ url: urlText, cause: error });
|
|
790
885
|
}
|
|
791
886
|
};
|
|
792
887
|
|
|
@@ -798,7 +893,7 @@ var createDefaultDownloadFunction = (download2 = download) => (requestedDownload
|
|
|
798
893
|
);
|
|
799
894
|
|
|
800
895
|
// src/prompt/data-content.ts
|
|
801
|
-
import { AISDKError as
|
|
896
|
+
import { AISDKError as AISDKError16 } from "@ai-sdk/provider";
|
|
802
897
|
import {
|
|
803
898
|
convertBase64ToUint8Array as convertBase64ToUint8Array2,
|
|
804
899
|
convertUint8ArrayToBase64
|
|
@@ -829,8 +924,8 @@ var dataContentSchema = z.union([
|
|
|
829
924
|
z.custom(
|
|
830
925
|
// Buffer might not be available in some environments such as CloudFlare:
|
|
831
926
|
(value) => {
|
|
832
|
-
var
|
|
833
|
-
return (_b = (
|
|
927
|
+
var _a14, _b;
|
|
928
|
+
return (_b = (_a14 = globalThis.Buffer) == null ? void 0 : _a14.isBuffer(value)) != null ? _b : false;
|
|
834
929
|
},
|
|
835
930
|
{ message: "Must be a Buffer" }
|
|
836
931
|
)
|
|
@@ -853,7 +948,7 @@ function convertToLanguageModelV3DataContent(content) {
|
|
|
853
948
|
content.toString()
|
|
854
949
|
);
|
|
855
950
|
if (dataUrlMediaType == null || base64Content == null) {
|
|
856
|
-
throw new
|
|
951
|
+
throw new AISDKError16({
|
|
857
952
|
name: "InvalidDataContentError",
|
|
858
953
|
message: `Invalid data URL format in content ${content.toString()}`
|
|
859
954
|
});
|
|
@@ -892,6 +987,11 @@ function convertDataContentToUint8Array(content) {
|
|
|
892
987
|
throw new InvalidDataContentError({ content });
|
|
893
988
|
}
|
|
894
989
|
|
|
990
|
+
// src/util/as-array.ts
|
|
991
|
+
function asArray(value) {
|
|
992
|
+
return value === void 0 ? [] : Array.isArray(value) ? value : [value];
|
|
993
|
+
}
|
|
994
|
+
|
|
895
995
|
// src/prompt/convert-to-language-model-prompt.ts
|
|
896
996
|
async function convertToLanguageModelPrompt({
|
|
897
997
|
prompt,
|
|
@@ -904,7 +1004,11 @@ async function convertToLanguageModelPrompt({
|
|
|
904
1004
|
supportedUrls
|
|
905
1005
|
);
|
|
906
1006
|
const messages = [
|
|
907
|
-
...prompt.system != null ? [{ role: "system", content: prompt.system }] :
|
|
1007
|
+
...prompt.system != null ? typeof prompt.system === "string" ? [{ role: "system", content: prompt.system }] : asArray(prompt.system).map((message) => ({
|
|
1008
|
+
role: "system",
|
|
1009
|
+
content: message.content,
|
|
1010
|
+
providerOptions: message.providerOptions
|
|
1011
|
+
})) : [],
|
|
908
1012
|
...prompt.messages.map(
|
|
909
1013
|
(message) => convertToLanguageModelMessage({ message, downloadedAssets })
|
|
910
1014
|
)
|
|
@@ -1044,8 +1148,8 @@ async function downloadAssets(messages, download2, supportedUrls) {
|
|
|
1044
1148
|
).flat().filter(
|
|
1045
1149
|
(part) => part.type === "image" || part.type === "file"
|
|
1046
1150
|
).map((part) => {
|
|
1047
|
-
var
|
|
1048
|
-
const mediaType = (
|
|
1151
|
+
var _a14;
|
|
1152
|
+
const mediaType = (_a14 = part.mediaType) != null ? _a14 : part.type === "image" ? "image/*" : void 0;
|
|
1049
1153
|
let data = part.type === "image" ? part.image : part.data;
|
|
1050
1154
|
if (typeof data === "string") {
|
|
1051
1155
|
try {
|
|
@@ -1075,7 +1179,7 @@ async function downloadAssets(messages, download2, supportedUrls) {
|
|
|
1075
1179
|
);
|
|
1076
1180
|
}
|
|
1077
1181
|
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
|
1078
|
-
var
|
|
1182
|
+
var _a14;
|
|
1079
1183
|
if (part.type === "text") {
|
|
1080
1184
|
return {
|
|
1081
1185
|
type: "text",
|
|
@@ -1108,7 +1212,7 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
|
|
|
1108
1212
|
switch (type) {
|
|
1109
1213
|
case "image": {
|
|
1110
1214
|
if (data instanceof Uint8Array || typeof data === "string") {
|
|
1111
|
-
mediaType = (
|
|
1215
|
+
mediaType = (_a14 = detectMediaType({ data, signatures: imageMediaTypeSignatures })) != null ? _a14 : mediaType;
|
|
1112
1216
|
}
|
|
1113
1217
|
return {
|
|
1114
1218
|
type: "file",
|
|
@@ -1161,7 +1265,9 @@ function mapToolResultOutput(output) {
|
|
|
1161
1265
|
|
|
1162
1266
|
// src/prompt/create-tool-model-output.ts
|
|
1163
1267
|
import { getErrorMessage as getErrorMessage3 } from "@ai-sdk/provider";
|
|
1164
|
-
function createToolModelOutput({
|
|
1268
|
+
async function createToolModelOutput({
|
|
1269
|
+
toolCallId,
|
|
1270
|
+
input,
|
|
1165
1271
|
output,
|
|
1166
1272
|
tool: tool2,
|
|
1167
1273
|
errorMode
|
|
@@ -1172,7 +1278,7 @@ function createToolModelOutput({
|
|
|
1172
1278
|
return { type: "error-json", value: toJSONValue(output) };
|
|
1173
1279
|
}
|
|
1174
1280
|
if (tool2 == null ? void 0 : tool2.toModelOutput) {
|
|
1175
|
-
return tool2.toModelOutput(output);
|
|
1281
|
+
return await tool2.toModelOutput({ toolCallId, input, output });
|
|
1176
1282
|
}
|
|
1177
1283
|
return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: toJSONValue(output) };
|
|
1178
1284
|
}
|
|
@@ -1294,10 +1400,10 @@ async function prepareToolsAndToolChoice({
|
|
|
1294
1400
|
};
|
|
1295
1401
|
}
|
|
1296
1402
|
const filteredTools = activeTools != null ? Object.entries(tools).filter(
|
|
1297
|
-
([
|
|
1403
|
+
([name14]) => activeTools.includes(name14)
|
|
1298
1404
|
) : Object.entries(tools);
|
|
1299
1405
|
const languageModelTools = [];
|
|
1300
|
-
for (const [
|
|
1406
|
+
for (const [name14, tool2] of filteredTools) {
|
|
1301
1407
|
const toolType = tool2.type;
|
|
1302
1408
|
switch (toolType) {
|
|
1303
1409
|
case void 0:
|
|
@@ -1305,16 +1411,18 @@ async function prepareToolsAndToolChoice({
|
|
|
1305
1411
|
case "function":
|
|
1306
1412
|
languageModelTools.push({
|
|
1307
1413
|
type: "function",
|
|
1308
|
-
name:
|
|
1414
|
+
name: name14,
|
|
1309
1415
|
description: tool2.description,
|
|
1310
1416
|
inputSchema: await asSchema(tool2.inputSchema).jsonSchema,
|
|
1311
|
-
|
|
1417
|
+
...tool2.inputExamples != null ? { inputExamples: tool2.inputExamples } : {},
|
|
1418
|
+
providerOptions: tool2.providerOptions,
|
|
1419
|
+
...tool2.strict != null ? { strict: tool2.strict } : {}
|
|
1312
1420
|
});
|
|
1313
1421
|
break;
|
|
1314
|
-
case "provider
|
|
1422
|
+
case "provider":
|
|
1315
1423
|
languageModelTools.push({
|
|
1316
|
-
type: "provider
|
|
1317
|
-
name:
|
|
1424
|
+
type: "provider",
|
|
1425
|
+
name: name14,
|
|
1318
1426
|
id: tool2.id,
|
|
1319
1427
|
args: tool2.args
|
|
1320
1428
|
});
|
|
@@ -1333,7 +1441,9 @@ async function prepareToolsAndToolChoice({
|
|
|
1333
1441
|
|
|
1334
1442
|
// src/prompt/standardize-prompt.ts
|
|
1335
1443
|
import { InvalidPromptError as InvalidPromptError2 } from "@ai-sdk/provider";
|
|
1336
|
-
import {
|
|
1444
|
+
import {
|
|
1445
|
+
safeValidateTypes
|
|
1446
|
+
} from "@ai-sdk/provider-utils";
|
|
1337
1447
|
import { z as z6 } from "zod/v4";
|
|
1338
1448
|
|
|
1339
1449
|
// src/prompt/message.ts
|
|
@@ -1505,7 +1615,6 @@ var systemModelMessageSchema = z5.object(
|
|
|
1505
1615
|
providerOptions: providerMetadataSchema.optional()
|
|
1506
1616
|
}
|
|
1507
1617
|
);
|
|
1508
|
-
var coreSystemMessageSchema = systemModelMessageSchema;
|
|
1509
1618
|
var userModelMessageSchema = z5.object({
|
|
1510
1619
|
role: z5.literal("user"),
|
|
1511
1620
|
content: z5.union([
|
|
@@ -1514,7 +1623,6 @@ var userModelMessageSchema = z5.object({
|
|
|
1514
1623
|
]),
|
|
1515
1624
|
providerOptions: providerMetadataSchema.optional()
|
|
1516
1625
|
});
|
|
1517
|
-
var coreUserMessageSchema = userModelMessageSchema;
|
|
1518
1626
|
var assistantModelMessageSchema = z5.object({
|
|
1519
1627
|
role: z5.literal("assistant"),
|
|
1520
1628
|
content: z5.union([
|
|
@@ -1532,20 +1640,17 @@ var assistantModelMessageSchema = z5.object({
|
|
|
1532
1640
|
]),
|
|
1533
1641
|
providerOptions: providerMetadataSchema.optional()
|
|
1534
1642
|
});
|
|
1535
|
-
var coreAssistantMessageSchema = assistantModelMessageSchema;
|
|
1536
1643
|
var toolModelMessageSchema = z5.object({
|
|
1537
1644
|
role: z5.literal("tool"),
|
|
1538
1645
|
content: z5.array(z5.union([toolResultPartSchema, toolApprovalResponseSchema])),
|
|
1539
1646
|
providerOptions: providerMetadataSchema.optional()
|
|
1540
1647
|
});
|
|
1541
|
-
var coreToolMessageSchema = toolModelMessageSchema;
|
|
1542
1648
|
var modelMessageSchema = z5.union([
|
|
1543
1649
|
systemModelMessageSchema,
|
|
1544
1650
|
userModelMessageSchema,
|
|
1545
1651
|
assistantModelMessageSchema,
|
|
1546
1652
|
toolModelMessageSchema
|
|
1547
1653
|
]);
|
|
1548
|
-
var coreMessageSchema = modelMessageSchema;
|
|
1549
1654
|
|
|
1550
1655
|
// src/prompt/standardize-prompt.ts
|
|
1551
1656
|
async function standardizePrompt(prompt) {
|
|
@@ -1561,10 +1666,12 @@ async function standardizePrompt(prompt) {
|
|
|
1561
1666
|
message: "prompt and messages cannot be defined at the same time"
|
|
1562
1667
|
});
|
|
1563
1668
|
}
|
|
1564
|
-
if (prompt.system != null && typeof prompt.system !== "string")
|
|
1669
|
+
if (prompt.system != null && typeof prompt.system !== "string" && !asArray(prompt.system).every(
|
|
1670
|
+
(message) => typeof message === "object" && message !== null && "role" in message && message.role === "system"
|
|
1671
|
+
)) {
|
|
1565
1672
|
throw new InvalidPromptError2({
|
|
1566
1673
|
prompt,
|
|
1567
|
-
message: "system must be a string"
|
|
1674
|
+
message: "system must be a string, SystemModelMessage, or array of SystemModelMessage"
|
|
1568
1675
|
});
|
|
1569
1676
|
}
|
|
1570
1677
|
let messages;
|
|
@@ -1593,7 +1700,7 @@ async function standardizePrompt(prompt) {
|
|
|
1593
1700
|
if (!validationResult.success) {
|
|
1594
1701
|
throw new InvalidPromptError2({
|
|
1595
1702
|
prompt,
|
|
1596
|
-
message: "The messages
|
|
1703
|
+
message: "The messages do not match the ModelMessage[] schema.",
|
|
1597
1704
|
cause: validationResult.error
|
|
1598
1705
|
});
|
|
1599
1706
|
}
|
|
@@ -1604,20 +1711,31 @@ async function standardizePrompt(prompt) {
|
|
|
1604
1711
|
}
|
|
1605
1712
|
|
|
1606
1713
|
// src/prompt/wrap-gateway-error.ts
|
|
1607
|
-
import {
|
|
1608
|
-
|
|
1609
|
-
GatewayModelNotFoundError
|
|
1610
|
-
} from "@ai-sdk/gateway";
|
|
1611
|
-
import { AISDKError as AISDKError18 } from "@ai-sdk/provider";
|
|
1714
|
+
import { GatewayAuthenticationError } from "@ai-sdk/gateway";
|
|
1715
|
+
import { AISDKError as AISDKError17 } from "@ai-sdk/provider";
|
|
1612
1716
|
function wrapGatewayError(error) {
|
|
1613
|
-
if (GatewayAuthenticationError.isInstance(error)
|
|
1614
|
-
return
|
|
1717
|
+
if (!GatewayAuthenticationError.isInstance(error))
|
|
1718
|
+
return error;
|
|
1719
|
+
const isProductionEnv = (process == null ? void 0 : process.env.NODE_ENV) === "production";
|
|
1720
|
+
const moreInfoURL = "https://ai-sdk.dev/unauthenticated-ai-gateway";
|
|
1721
|
+
if (isProductionEnv) {
|
|
1722
|
+
return new AISDKError17({
|
|
1615
1723
|
name: "GatewayError",
|
|
1616
|
-
message:
|
|
1617
|
-
cause: error
|
|
1724
|
+
message: `Unauthenticated. Configure AI_GATEWAY_API_KEY or use a provider module. Learn more: ${moreInfoURL}`
|
|
1618
1725
|
});
|
|
1619
1726
|
}
|
|
1620
|
-
return
|
|
1727
|
+
return Object.assign(
|
|
1728
|
+
new Error(`\x1B[1m\x1B[31mUnauthenticated request to AI Gateway.\x1B[0m
|
|
1729
|
+
|
|
1730
|
+
To authenticate, set the \x1B[33mAI_GATEWAY_API_KEY\x1B[0m environment variable with your API key.
|
|
1731
|
+
|
|
1732
|
+
Alternatively, you can use a provider module instead of the AI Gateway.
|
|
1733
|
+
|
|
1734
|
+
Learn more: \x1B[34m${moreInfoURL}\x1B[0m
|
|
1735
|
+
|
|
1736
|
+
`),
|
|
1737
|
+
{ name: "GatewayAuthenticationError" }
|
|
1738
|
+
);
|
|
1621
1739
|
}
|
|
1622
1740
|
|
|
1623
1741
|
// src/telemetry/assemble-operation-name.ts
|
|
@@ -1642,7 +1760,7 @@ function getBaseTelemetryAttributes({
|
|
|
1642
1760
|
telemetry,
|
|
1643
1761
|
headers
|
|
1644
1762
|
}) {
|
|
1645
|
-
var
|
|
1763
|
+
var _a14;
|
|
1646
1764
|
return {
|
|
1647
1765
|
"ai.model.provider": model.provider,
|
|
1648
1766
|
"ai.model.id": model.modelId,
|
|
@@ -1652,7 +1770,7 @@ function getBaseTelemetryAttributes({
|
|
|
1652
1770
|
return attributes;
|
|
1653
1771
|
}, {}),
|
|
1654
1772
|
// add metadata as attributes:
|
|
1655
|
-
...Object.entries((
|
|
1773
|
+
...Object.entries((_a14 = telemetry == null ? void 0 : telemetry.metadata) != null ? _a14 : {}).reduce(
|
|
1656
1774
|
(attributes, [key, value]) => {
|
|
1657
1775
|
attributes[`ai.telemetry.metadata.${key}`] = value;
|
|
1658
1776
|
return attributes;
|
|
@@ -1677,7 +1795,7 @@ var noopTracer = {
|
|
|
1677
1795
|
startSpan() {
|
|
1678
1796
|
return noopSpan;
|
|
1679
1797
|
},
|
|
1680
|
-
startActiveSpan(
|
|
1798
|
+
startActiveSpan(name14, arg1, arg2, arg3) {
|
|
1681
1799
|
if (typeof arg1 === "function") {
|
|
1682
1800
|
return arg1(noopSpan);
|
|
1683
1801
|
}
|
|
@@ -1747,14 +1865,14 @@ function getTracer({
|
|
|
1747
1865
|
// src/telemetry/record-span.ts
|
|
1748
1866
|
import { SpanStatusCode } from "@opentelemetry/api";
|
|
1749
1867
|
async function recordSpan({
|
|
1750
|
-
name:
|
|
1868
|
+
name: name14,
|
|
1751
1869
|
tracer,
|
|
1752
1870
|
attributes,
|
|
1753
1871
|
fn,
|
|
1754
1872
|
endWhenDone = true
|
|
1755
1873
|
}) {
|
|
1756
1874
|
return tracer.startActiveSpan(
|
|
1757
|
-
|
|
1875
|
+
name14,
|
|
1758
1876
|
{ attributes: await attributes },
|
|
1759
1877
|
async (span) => {
|
|
1760
1878
|
try {
|
|
@@ -1844,10 +1962,74 @@ function stringifyForTelemetry(prompt) {
|
|
|
1844
1962
|
}
|
|
1845
1963
|
|
|
1846
1964
|
// src/types/usage.ts
|
|
1965
|
+
function asLanguageModelUsage(usage) {
|
|
1966
|
+
return {
|
|
1967
|
+
inputTokens: usage.inputTokens.total,
|
|
1968
|
+
inputTokenDetails: {
|
|
1969
|
+
noCacheTokens: usage.inputTokens.noCache,
|
|
1970
|
+
cacheReadTokens: usage.inputTokens.cacheRead,
|
|
1971
|
+
cacheWriteTokens: usage.inputTokens.cacheWrite
|
|
1972
|
+
},
|
|
1973
|
+
outputTokens: usage.outputTokens.total,
|
|
1974
|
+
outputTokenDetails: {
|
|
1975
|
+
textTokens: usage.outputTokens.text,
|
|
1976
|
+
reasoningTokens: usage.outputTokens.reasoning
|
|
1977
|
+
},
|
|
1978
|
+
totalTokens: addTokenCounts(
|
|
1979
|
+
usage.inputTokens.total,
|
|
1980
|
+
usage.outputTokens.total
|
|
1981
|
+
),
|
|
1982
|
+
raw: usage.raw,
|
|
1983
|
+
reasoningTokens: usage.outputTokens.reasoning,
|
|
1984
|
+
cachedInputTokens: usage.inputTokens.cacheRead
|
|
1985
|
+
};
|
|
1986
|
+
}
|
|
1987
|
+
function createNullLanguageModelUsage() {
|
|
1988
|
+
return {
|
|
1989
|
+
inputTokens: void 0,
|
|
1990
|
+
inputTokenDetails: {
|
|
1991
|
+
noCacheTokens: void 0,
|
|
1992
|
+
cacheReadTokens: void 0,
|
|
1993
|
+
cacheWriteTokens: void 0
|
|
1994
|
+
},
|
|
1995
|
+
outputTokens: void 0,
|
|
1996
|
+
outputTokenDetails: {
|
|
1997
|
+
textTokens: void 0,
|
|
1998
|
+
reasoningTokens: void 0
|
|
1999
|
+
},
|
|
2000
|
+
totalTokens: void 0,
|
|
2001
|
+
raw: void 0
|
|
2002
|
+
};
|
|
2003
|
+
}
|
|
1847
2004
|
function addLanguageModelUsage(usage1, usage2) {
|
|
2005
|
+
var _a14, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
1848
2006
|
return {
|
|
1849
2007
|
inputTokens: addTokenCounts(usage1.inputTokens, usage2.inputTokens),
|
|
2008
|
+
inputTokenDetails: {
|
|
2009
|
+
noCacheTokens: addTokenCounts(
|
|
2010
|
+
(_a14 = usage1.inputTokenDetails) == null ? void 0 : _a14.noCacheTokens,
|
|
2011
|
+
(_b = usage2.inputTokenDetails) == null ? void 0 : _b.noCacheTokens
|
|
2012
|
+
),
|
|
2013
|
+
cacheReadTokens: addTokenCounts(
|
|
2014
|
+
(_c = usage1.inputTokenDetails) == null ? void 0 : _c.cacheReadTokens,
|
|
2015
|
+
(_d = usage2.inputTokenDetails) == null ? void 0 : _d.cacheReadTokens
|
|
2016
|
+
),
|
|
2017
|
+
cacheWriteTokens: addTokenCounts(
|
|
2018
|
+
(_e = usage1.inputTokenDetails) == null ? void 0 : _e.cacheWriteTokens,
|
|
2019
|
+
(_f = usage2.inputTokenDetails) == null ? void 0 : _f.cacheWriteTokens
|
|
2020
|
+
)
|
|
2021
|
+
},
|
|
1850
2022
|
outputTokens: addTokenCounts(usage1.outputTokens, usage2.outputTokens),
|
|
2023
|
+
outputTokenDetails: {
|
|
2024
|
+
textTokens: addTokenCounts(
|
|
2025
|
+
(_g = usage1.outputTokenDetails) == null ? void 0 : _g.textTokens,
|
|
2026
|
+
(_h = usage2.outputTokenDetails) == null ? void 0 : _h.textTokens
|
|
2027
|
+
),
|
|
2028
|
+
reasoningTokens: addTokenCounts(
|
|
2029
|
+
(_i = usage1.outputTokenDetails) == null ? void 0 : _i.reasoningTokens,
|
|
2030
|
+
(_j = usage2.outputTokenDetails) == null ? void 0 : _j.reasoningTokens
|
|
2031
|
+
)
|
|
2032
|
+
},
|
|
1851
2033
|
totalTokens: addTokenCounts(usage1.totalTokens, usage2.totalTokens),
|
|
1852
2034
|
reasoningTokens: addTokenCounts(
|
|
1853
2035
|
usage1.reasoningTokens,
|
|
@@ -1870,9 +2052,37 @@ function addImageModelUsage(usage1, usage2) {
|
|
|
1870
2052
|
};
|
|
1871
2053
|
}
|
|
1872
2054
|
|
|
1873
|
-
// src/util/
|
|
1874
|
-
function
|
|
1875
|
-
|
|
2055
|
+
// src/util/merge-objects.ts
|
|
2056
|
+
function mergeObjects(base, overrides) {
|
|
2057
|
+
if (base === void 0 && overrides === void 0) {
|
|
2058
|
+
return void 0;
|
|
2059
|
+
}
|
|
2060
|
+
if (base === void 0) {
|
|
2061
|
+
return overrides;
|
|
2062
|
+
}
|
|
2063
|
+
if (overrides === void 0) {
|
|
2064
|
+
return base;
|
|
2065
|
+
}
|
|
2066
|
+
const result = { ...base };
|
|
2067
|
+
for (const key in overrides) {
|
|
2068
|
+
if (Object.prototype.hasOwnProperty.call(overrides, key)) {
|
|
2069
|
+
const overridesValue = overrides[key];
|
|
2070
|
+
if (overridesValue === void 0)
|
|
2071
|
+
continue;
|
|
2072
|
+
const baseValue = key in base ? base[key] : void 0;
|
|
2073
|
+
const isSourceObject = overridesValue !== null && typeof overridesValue === "object" && !Array.isArray(overridesValue) && !(overridesValue instanceof Date) && !(overridesValue instanceof RegExp);
|
|
2074
|
+
const isTargetObject = baseValue !== null && baseValue !== void 0 && typeof baseValue === "object" && !Array.isArray(baseValue) && !(baseValue instanceof Date) && !(baseValue instanceof RegExp);
|
|
2075
|
+
if (isSourceObject && isTargetObject) {
|
|
2076
|
+
result[key] = mergeObjects(
|
|
2077
|
+
baseValue,
|
|
2078
|
+
overridesValue
|
|
2079
|
+
);
|
|
2080
|
+
} else {
|
|
2081
|
+
result[key] = overridesValue;
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
return result;
|
|
1876
2086
|
}
|
|
1877
2087
|
|
|
1878
2088
|
// src/util/retry-with-exponential-backoff.ts
|
|
@@ -2134,7 +2344,8 @@ async function executeToolCall({
|
|
|
2134
2344
|
toolName,
|
|
2135
2345
|
input,
|
|
2136
2346
|
error,
|
|
2137
|
-
dynamic: tool2.type === "dynamic"
|
|
2347
|
+
dynamic: tool2.type === "dynamic",
|
|
2348
|
+
...toolCall.providerMetadata != null ? { providerMetadata: toolCall.providerMetadata } : {}
|
|
2138
2349
|
};
|
|
2139
2350
|
}
|
|
2140
2351
|
try {
|
|
@@ -2156,7 +2367,8 @@ async function executeToolCall({
|
|
|
2156
2367
|
toolName,
|
|
2157
2368
|
input,
|
|
2158
2369
|
output,
|
|
2159
|
-
dynamic: tool2.type === "dynamic"
|
|
2370
|
+
dynamic: tool2.type === "dynamic",
|
|
2371
|
+
...toolCall.providerMetadata != null ? { providerMetadata: toolCall.providerMetadata } : {}
|
|
2160
2372
|
};
|
|
2161
2373
|
}
|
|
2162
2374
|
});
|
|
@@ -2597,13 +2809,17 @@ var text = () => ({
|
|
|
2597
2809
|
}
|
|
2598
2810
|
});
|
|
2599
2811
|
var object = ({
|
|
2600
|
-
schema: inputSchema
|
|
2812
|
+
schema: inputSchema,
|
|
2813
|
+
name: name14,
|
|
2814
|
+
description
|
|
2601
2815
|
}) => {
|
|
2602
2816
|
const schema = asSchema2(inputSchema);
|
|
2603
2817
|
return {
|
|
2604
2818
|
responseFormat: resolve(schema.jsonSchema).then((jsonSchema2) => ({
|
|
2605
2819
|
type: "json",
|
|
2606
|
-
schema: jsonSchema2
|
|
2820
|
+
schema: jsonSchema2,
|
|
2821
|
+
...name14 != null && { name: name14 },
|
|
2822
|
+
...description != null && { description }
|
|
2607
2823
|
})),
|
|
2608
2824
|
async parseCompleteOutput({ text: text2 }, context) {
|
|
2609
2825
|
const parseResult = await safeParseJSON2({ text: text2 });
|
|
@@ -2652,7 +2868,9 @@ var object = ({
|
|
|
2652
2868
|
};
|
|
2653
2869
|
};
|
|
2654
2870
|
var array = ({
|
|
2655
|
-
element: inputElementSchema
|
|
2871
|
+
element: inputElementSchema,
|
|
2872
|
+
name: name14,
|
|
2873
|
+
description
|
|
2656
2874
|
}) => {
|
|
2657
2875
|
const elementSchema = asSchema2(inputElementSchema);
|
|
2658
2876
|
return {
|
|
@@ -2669,7 +2887,9 @@ var array = ({
|
|
|
2669
2887
|
},
|
|
2670
2888
|
required: ["elements"],
|
|
2671
2889
|
additionalProperties: false
|
|
2672
|
-
}
|
|
2890
|
+
},
|
|
2891
|
+
...name14 != null && { name: name14 },
|
|
2892
|
+
...description != null && { description }
|
|
2673
2893
|
};
|
|
2674
2894
|
}),
|
|
2675
2895
|
async parseCompleteOutput({ text: text2 }, context) {
|
|
@@ -2747,7 +2967,9 @@ var array = ({
|
|
|
2747
2967
|
};
|
|
2748
2968
|
};
|
|
2749
2969
|
var choice = ({
|
|
2750
|
-
options: choiceOptions
|
|
2970
|
+
options: choiceOptions,
|
|
2971
|
+
name: name14,
|
|
2972
|
+
description
|
|
2751
2973
|
}) => {
|
|
2752
2974
|
return {
|
|
2753
2975
|
// JSON schema that describes an enumeration:
|
|
@@ -2761,7 +2983,9 @@ var choice = ({
|
|
|
2761
2983
|
},
|
|
2762
2984
|
required: ["result"],
|
|
2763
2985
|
additionalProperties: false
|
|
2764
|
-
}
|
|
2986
|
+
},
|
|
2987
|
+
...name14 != null && { name: name14 },
|
|
2988
|
+
...description != null && { description }
|
|
2765
2989
|
}),
|
|
2766
2990
|
async parseCompleteOutput({ text: text2 }, context) {
|
|
2767
2991
|
const parseResult = await safeParseJSON2({ text: text2 });
|
|
@@ -2817,10 +3041,15 @@ var choice = ({
|
|
|
2817
3041
|
}
|
|
2818
3042
|
};
|
|
2819
3043
|
};
|
|
2820
|
-
var json = (
|
|
3044
|
+
var json = ({
|
|
3045
|
+
name: name14,
|
|
3046
|
+
description
|
|
3047
|
+
} = {}) => {
|
|
2821
3048
|
return {
|
|
2822
3049
|
responseFormat: Promise.resolve({
|
|
2823
|
-
type: "json"
|
|
3050
|
+
type: "json",
|
|
3051
|
+
...name14 != null && { name: name14 },
|
|
3052
|
+
...description != null && { description }
|
|
2824
3053
|
}),
|
|
2825
3054
|
async parseCompleteOutput({ text: text2 }, context) {
|
|
2826
3055
|
const parseResult = await safeParseJSON2({ text: text2 });
|
|
@@ -2865,7 +3094,7 @@ async function parseToolCall({
|
|
|
2865
3094
|
system,
|
|
2866
3095
|
messages
|
|
2867
3096
|
}) {
|
|
2868
|
-
var
|
|
3097
|
+
var _a14;
|
|
2869
3098
|
try {
|
|
2870
3099
|
if (tools == null) {
|
|
2871
3100
|
if (toolCall.providerExecuted && toolCall.dynamic) {
|
|
@@ -2914,7 +3143,9 @@ async function parseToolCall({
|
|
|
2914
3143
|
dynamic: true,
|
|
2915
3144
|
invalid: true,
|
|
2916
3145
|
error,
|
|
2917
|
-
title: (
|
|
3146
|
+
title: (_a14 = tools == null ? void 0 : tools[toolCall.toolName]) == null ? void 0 : _a14.title,
|
|
3147
|
+
providerExecuted: toolCall.providerExecuted,
|
|
3148
|
+
providerMetadata: toolCall.providerMetadata
|
|
2918
3149
|
};
|
|
2919
3150
|
}
|
|
2920
3151
|
}
|
|
@@ -2986,6 +3217,7 @@ var DefaultStepResult = class {
|
|
|
2986
3217
|
constructor({
|
|
2987
3218
|
content,
|
|
2988
3219
|
finishReason,
|
|
3220
|
+
rawFinishReason,
|
|
2989
3221
|
usage,
|
|
2990
3222
|
warnings,
|
|
2991
3223
|
request,
|
|
@@ -2994,6 +3226,7 @@ var DefaultStepResult = class {
|
|
|
2994
3226
|
}) {
|
|
2995
3227
|
this.content = content;
|
|
2996
3228
|
this.finishReason = finishReason;
|
|
3229
|
+
this.rawFinishReason = rawFinishReason;
|
|
2997
3230
|
this.usage = usage;
|
|
2998
3231
|
this.warnings = warnings;
|
|
2999
3232
|
this.request = request;
|
|
@@ -3049,8 +3282,8 @@ function stepCountIs(stepCount) {
|
|
|
3049
3282
|
}
|
|
3050
3283
|
function hasToolCall(toolName) {
|
|
3051
3284
|
return ({ steps }) => {
|
|
3052
|
-
var
|
|
3053
|
-
return (_c = (_b = (
|
|
3285
|
+
var _a14, _b, _c;
|
|
3286
|
+
return (_c = (_b = (_a14 = steps[steps.length - 1]) == null ? void 0 : _a14.toolCalls) == null ? void 0 : _b.some(
|
|
3054
3287
|
(toolCall) => toolCall.toolName === toolName
|
|
3055
3288
|
)) != null ? _c : false;
|
|
3056
3289
|
};
|
|
@@ -3063,92 +3296,118 @@ async function isStopConditionMet({
|
|
|
3063
3296
|
}
|
|
3064
3297
|
|
|
3065
3298
|
// src/generate-text/to-response-messages.ts
|
|
3066
|
-
function toResponseMessages({
|
|
3299
|
+
async function toResponseMessages({
|
|
3067
3300
|
content: inputContent,
|
|
3068
3301
|
tools
|
|
3069
3302
|
}) {
|
|
3070
3303
|
const responseMessages = [];
|
|
3071
|
-
const content =
|
|
3072
|
-
|
|
3073
|
-
|
|
3304
|
+
const content = [];
|
|
3305
|
+
for (const part of inputContent) {
|
|
3306
|
+
if (part.type === "source" || (part.type === "tool-result" || part.type === "tool-error") && !part.providerExecuted || part.type === "text" && part.text.length === 0) {
|
|
3307
|
+
continue;
|
|
3308
|
+
}
|
|
3074
3309
|
switch (part.type) {
|
|
3075
3310
|
case "text":
|
|
3076
|
-
|
|
3311
|
+
content.push({
|
|
3077
3312
|
type: "text",
|
|
3078
3313
|
text: part.text,
|
|
3079
3314
|
providerOptions: part.providerMetadata
|
|
3080
|
-
};
|
|
3315
|
+
});
|
|
3316
|
+
break;
|
|
3081
3317
|
case "reasoning":
|
|
3082
|
-
|
|
3318
|
+
content.push({
|
|
3083
3319
|
type: "reasoning",
|
|
3084
3320
|
text: part.text,
|
|
3085
3321
|
providerOptions: part.providerMetadata
|
|
3086
|
-
};
|
|
3322
|
+
});
|
|
3323
|
+
break;
|
|
3087
3324
|
case "file":
|
|
3088
|
-
|
|
3325
|
+
content.push({
|
|
3089
3326
|
type: "file",
|
|
3090
3327
|
data: part.file.base64,
|
|
3091
3328
|
mediaType: part.file.mediaType,
|
|
3092
3329
|
providerOptions: part.providerMetadata
|
|
3093
|
-
};
|
|
3330
|
+
});
|
|
3331
|
+
break;
|
|
3094
3332
|
case "tool-call":
|
|
3095
|
-
|
|
3333
|
+
content.push({
|
|
3096
3334
|
type: "tool-call",
|
|
3097
3335
|
toolCallId: part.toolCallId,
|
|
3098
3336
|
toolName: part.toolName,
|
|
3099
3337
|
input: part.input,
|
|
3100
3338
|
providerExecuted: part.providerExecuted,
|
|
3101
3339
|
providerOptions: part.providerMetadata
|
|
3102
|
-
};
|
|
3103
|
-
|
|
3104
|
-
|
|
3340
|
+
});
|
|
3341
|
+
break;
|
|
3342
|
+
case "tool-result": {
|
|
3343
|
+
const output = await createToolModelOutput({
|
|
3344
|
+
toolCallId: part.toolCallId,
|
|
3345
|
+
input: part.input,
|
|
3346
|
+
tool: tools == null ? void 0 : tools[part.toolName],
|
|
3347
|
+
output: part.output,
|
|
3348
|
+
errorMode: "none"
|
|
3349
|
+
});
|
|
3350
|
+
content.push({
|
|
3105
3351
|
type: "tool-result",
|
|
3106
3352
|
toolCallId: part.toolCallId,
|
|
3107
3353
|
toolName: part.toolName,
|
|
3108
|
-
output
|
|
3109
|
-
tool: tools == null ? void 0 : tools[part.toolName],
|
|
3110
|
-
output: part.output,
|
|
3111
|
-
errorMode: "none"
|
|
3112
|
-
}),
|
|
3113
|
-
providerExecuted: true,
|
|
3354
|
+
output,
|
|
3114
3355
|
providerOptions: part.providerMetadata
|
|
3115
|
-
};
|
|
3116
|
-
|
|
3117
|
-
|
|
3356
|
+
});
|
|
3357
|
+
break;
|
|
3358
|
+
}
|
|
3359
|
+
case "tool-error": {
|
|
3360
|
+
const output = await createToolModelOutput({
|
|
3361
|
+
toolCallId: part.toolCallId,
|
|
3362
|
+
input: part.input,
|
|
3363
|
+
tool: tools == null ? void 0 : tools[part.toolName],
|
|
3364
|
+
output: part.error,
|
|
3365
|
+
errorMode: "json"
|
|
3366
|
+
});
|
|
3367
|
+
content.push({
|
|
3118
3368
|
type: "tool-result",
|
|
3119
3369
|
toolCallId: part.toolCallId,
|
|
3120
3370
|
toolName: part.toolName,
|
|
3121
|
-
output
|
|
3122
|
-
tool: tools == null ? void 0 : tools[part.toolName],
|
|
3123
|
-
output: part.error,
|
|
3124
|
-
errorMode: "json"
|
|
3125
|
-
}),
|
|
3371
|
+
output,
|
|
3126
3372
|
providerOptions: part.providerMetadata
|
|
3127
|
-
};
|
|
3373
|
+
});
|
|
3374
|
+
break;
|
|
3375
|
+
}
|
|
3128
3376
|
case "tool-approval-request":
|
|
3129
|
-
|
|
3377
|
+
content.push({
|
|
3130
3378
|
type: "tool-approval-request",
|
|
3131
3379
|
approvalId: part.approvalId,
|
|
3132
3380
|
toolCallId: part.toolCall.toolCallId
|
|
3133
|
-
};
|
|
3381
|
+
});
|
|
3382
|
+
break;
|
|
3134
3383
|
}
|
|
3135
|
-
}
|
|
3384
|
+
}
|
|
3136
3385
|
if (content.length > 0) {
|
|
3137
3386
|
responseMessages.push({
|
|
3138
3387
|
role: "assistant",
|
|
3139
3388
|
content
|
|
3140
3389
|
});
|
|
3141
3390
|
}
|
|
3142
|
-
const toolResultContent =
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
3149
|
-
|
|
3150
|
-
|
|
3151
|
-
|
|
3391
|
+
const toolResultContent = [];
|
|
3392
|
+
for (const part of inputContent) {
|
|
3393
|
+
if (!(part.type === "tool-result" || part.type === "tool-error") || part.providerExecuted) {
|
|
3394
|
+
continue;
|
|
3395
|
+
}
|
|
3396
|
+
const output = await createToolModelOutput({
|
|
3397
|
+
toolCallId: part.toolCallId,
|
|
3398
|
+
input: part.input,
|
|
3399
|
+
tool: tools == null ? void 0 : tools[part.toolName],
|
|
3400
|
+
output: part.type === "tool-result" ? part.output : part.error,
|
|
3401
|
+
errorMode: part.type === "tool-error" ? "text" : "none"
|
|
3402
|
+
});
|
|
3403
|
+
toolResultContent.push({
|
|
3404
|
+
type: "tool-result",
|
|
3405
|
+
toolCallId: part.toolCallId,
|
|
3406
|
+
toolName: part.toolName,
|
|
3407
|
+
output,
|
|
3408
|
+
...part.providerMetadata != null ? { providerOptions: part.providerMetadata } : {}
|
|
3409
|
+
});
|
|
3410
|
+
}
|
|
3152
3411
|
if (toolResultContent.length > 0) {
|
|
3153
3412
|
responseMessages.push({
|
|
3154
3413
|
role: "tool",
|
|
@@ -3238,7 +3497,7 @@ async function generateText({
|
|
|
3238
3497
|
}),
|
|
3239
3498
|
tracer,
|
|
3240
3499
|
fn: async (span) => {
|
|
3241
|
-
var
|
|
3500
|
+
var _a14, _b, _c, _d, _e, _f, _g, _h;
|
|
3242
3501
|
const initialMessages = initialPrompt.messages;
|
|
3243
3502
|
const responseMessages = [];
|
|
3244
3503
|
const { approvedToolApprovals, deniedToolApprovals } = collectToolApprovals({ messages: initialMessages });
|
|
@@ -3254,31 +3513,36 @@ async function generateText({
|
|
|
3254
3513
|
abortSignal,
|
|
3255
3514
|
experimental_context
|
|
3256
3515
|
});
|
|
3516
|
+
const toolContent = [];
|
|
3517
|
+
for (const output2 of toolOutputs) {
|
|
3518
|
+
const modelOutput = await createToolModelOutput({
|
|
3519
|
+
toolCallId: output2.toolCallId,
|
|
3520
|
+
input: output2.input,
|
|
3521
|
+
tool: tools == null ? void 0 : tools[output2.toolName],
|
|
3522
|
+
output: output2.type === "tool-result" ? output2.output : output2.error,
|
|
3523
|
+
errorMode: output2.type === "tool-error" ? "json" : "none"
|
|
3524
|
+
});
|
|
3525
|
+
toolContent.push({
|
|
3526
|
+
type: "tool-result",
|
|
3527
|
+
toolCallId: output2.toolCallId,
|
|
3528
|
+
toolName: output2.toolName,
|
|
3529
|
+
output: modelOutput
|
|
3530
|
+
});
|
|
3531
|
+
}
|
|
3532
|
+
for (const toolApproval of deniedToolApprovals) {
|
|
3533
|
+
toolContent.push({
|
|
3534
|
+
type: "tool-result",
|
|
3535
|
+
toolCallId: toolApproval.toolCall.toolCallId,
|
|
3536
|
+
toolName: toolApproval.toolCall.toolName,
|
|
3537
|
+
output: {
|
|
3538
|
+
type: "execution-denied",
|
|
3539
|
+
reason: toolApproval.approvalResponse.reason
|
|
3540
|
+
}
|
|
3541
|
+
});
|
|
3542
|
+
}
|
|
3257
3543
|
responseMessages.push({
|
|
3258
3544
|
role: "tool",
|
|
3259
|
-
content:
|
|
3260
|
-
// add regular tool results for approved tool calls:
|
|
3261
|
-
...toolOutputs.map((output2) => ({
|
|
3262
|
-
type: "tool-result",
|
|
3263
|
-
toolCallId: output2.toolCallId,
|
|
3264
|
-
toolName: output2.toolName,
|
|
3265
|
-
output: createToolModelOutput({
|
|
3266
|
-
tool: tools == null ? void 0 : tools[output2.toolName],
|
|
3267
|
-
output: output2.type === "tool-result" ? output2.output : output2.error,
|
|
3268
|
-
errorMode: output2.type === "tool-error" ? "json" : "none"
|
|
3269
|
-
})
|
|
3270
|
-
})),
|
|
3271
|
-
// add execution denied tool results for denied tool approvals:
|
|
3272
|
-
...deniedToolApprovals.map((toolApproval) => ({
|
|
3273
|
-
type: "tool-result",
|
|
3274
|
-
toolCallId: toolApproval.toolCall.toolCallId,
|
|
3275
|
-
toolName: toolApproval.toolCall.toolName,
|
|
3276
|
-
output: {
|
|
3277
|
-
type: "execution-denied",
|
|
3278
|
-
reason: toolApproval.approvalResponse.reason
|
|
3279
|
-
}
|
|
3280
|
-
}))
|
|
3281
|
-
]
|
|
3545
|
+
content: toolContent
|
|
3282
3546
|
});
|
|
3283
3547
|
}
|
|
3284
3548
|
const callSettings2 = prepareCallSettings(settings);
|
|
@@ -3286,16 +3550,18 @@ async function generateText({
|
|
|
3286
3550
|
let clientToolCalls = [];
|
|
3287
3551
|
let clientToolOutputs = [];
|
|
3288
3552
|
const steps = [];
|
|
3553
|
+
const pendingDeferredToolCalls = /* @__PURE__ */ new Map();
|
|
3289
3554
|
do {
|
|
3290
3555
|
const stepInputMessages = [...initialMessages, ...responseMessages];
|
|
3291
3556
|
const prepareStepResult = await (prepareStep == null ? void 0 : prepareStep({
|
|
3292
3557
|
model,
|
|
3293
3558
|
steps,
|
|
3294
3559
|
stepNumber: steps.length,
|
|
3295
|
-
messages: stepInputMessages
|
|
3560
|
+
messages: stepInputMessages,
|
|
3561
|
+
experimental_context
|
|
3296
3562
|
}));
|
|
3297
3563
|
const stepModel = resolveLanguageModel(
|
|
3298
|
-
(
|
|
3564
|
+
(_a14 = prepareStepResult == null ? void 0 : prepareStepResult.model) != null ? _a14 : model
|
|
3299
3565
|
);
|
|
3300
3566
|
const promptMessages = await convertToLanguageModelPrompt({
|
|
3301
3567
|
prompt: {
|
|
@@ -3305,14 +3571,15 @@ async function generateText({
|
|
|
3305
3571
|
supportedUrls: await stepModel.supportedUrls,
|
|
3306
3572
|
download: download2
|
|
3307
3573
|
});
|
|
3574
|
+
experimental_context = (_d = prepareStepResult == null ? void 0 : prepareStepResult.experimental_context) != null ? _d : experimental_context;
|
|
3308
3575
|
const { toolChoice: stepToolChoice, tools: stepTools } = await prepareToolsAndToolChoice({
|
|
3309
3576
|
tools,
|
|
3310
|
-
toolChoice: (
|
|
3311
|
-
activeTools: (
|
|
3577
|
+
toolChoice: (_e = prepareStepResult == null ? void 0 : prepareStepResult.toolChoice) != null ? _e : toolChoice,
|
|
3578
|
+
activeTools: (_f = prepareStepResult == null ? void 0 : prepareStepResult.activeTools) != null ? _f : activeTools
|
|
3312
3579
|
});
|
|
3313
3580
|
currentModelResponse = await retry(
|
|
3314
3581
|
() => {
|
|
3315
|
-
var
|
|
3582
|
+
var _a15;
|
|
3316
3583
|
return recordSpan({
|
|
3317
3584
|
name: "ai.generateText.doGenerate",
|
|
3318
3585
|
attributes: selectTelemetryAttributes({
|
|
@@ -3344,36 +3611,40 @@ async function generateText({
|
|
|
3344
3611
|
"gen_ai.request.max_tokens": settings.maxOutputTokens,
|
|
3345
3612
|
"gen_ai.request.presence_penalty": settings.presencePenalty,
|
|
3346
3613
|
"gen_ai.request.stop_sequences": settings.stopSequences,
|
|
3347
|
-
"gen_ai.request.temperature": (
|
|
3614
|
+
"gen_ai.request.temperature": (_a15 = settings.temperature) != null ? _a15 : void 0,
|
|
3348
3615
|
"gen_ai.request.top_k": settings.topK,
|
|
3349
3616
|
"gen_ai.request.top_p": settings.topP
|
|
3350
3617
|
}
|
|
3351
3618
|
}),
|
|
3352
3619
|
tracer,
|
|
3353
3620
|
fn: async (span2) => {
|
|
3354
|
-
var
|
|
3621
|
+
var _a16, _b2, _c2, _d2, _e2, _f2, _g2, _h2;
|
|
3622
|
+
const stepProviderOptions = mergeObjects(
|
|
3623
|
+
providerOptions,
|
|
3624
|
+
prepareStepResult == null ? void 0 : prepareStepResult.providerOptions
|
|
3625
|
+
);
|
|
3355
3626
|
const result = await stepModel.doGenerate({
|
|
3356
3627
|
...callSettings2,
|
|
3357
3628
|
tools: stepTools,
|
|
3358
3629
|
toolChoice: stepToolChoice,
|
|
3359
3630
|
responseFormat: await (output == null ? void 0 : output.responseFormat),
|
|
3360
3631
|
prompt: promptMessages,
|
|
3361
|
-
providerOptions,
|
|
3632
|
+
providerOptions: stepProviderOptions,
|
|
3362
3633
|
abortSignal,
|
|
3363
3634
|
headers: headersWithUserAgent
|
|
3364
3635
|
});
|
|
3365
3636
|
const responseData = {
|
|
3366
|
-
id: (_b2 = (
|
|
3637
|
+
id: (_b2 = (_a16 = result.response) == null ? void 0 : _a16.id) != null ? _b2 : generateId2(),
|
|
3367
3638
|
timestamp: (_d2 = (_c2 = result.response) == null ? void 0 : _c2.timestamp) != null ? _d2 : currentDate(),
|
|
3368
3639
|
modelId: (_f2 = (_e2 = result.response) == null ? void 0 : _e2.modelId) != null ? _f2 : stepModel.modelId,
|
|
3369
3640
|
headers: (_g2 = result.response) == null ? void 0 : _g2.headers,
|
|
3370
|
-
body: (
|
|
3641
|
+
body: (_h2 = result.response) == null ? void 0 : _h2.body
|
|
3371
3642
|
};
|
|
3372
3643
|
span2.setAttributes(
|
|
3373
3644
|
await selectTelemetryAttributes({
|
|
3374
3645
|
telemetry,
|
|
3375
3646
|
attributes: {
|
|
3376
|
-
"ai.response.finishReason": result.finishReason,
|
|
3647
|
+
"ai.response.finishReason": result.finishReason.unified,
|
|
3377
3648
|
"ai.response.text": {
|
|
3378
3649
|
output: () => extractTextContent(result.content)
|
|
3379
3650
|
},
|
|
@@ -3390,14 +3661,16 @@ async function generateText({
|
|
|
3390
3661
|
result.providerMetadata
|
|
3391
3662
|
),
|
|
3392
3663
|
// TODO rename telemetry attributes to inputTokens and outputTokens
|
|
3393
|
-
"ai.usage.promptTokens": result.usage.inputTokens,
|
|
3394
|
-
"ai.usage.completionTokens": result.usage.outputTokens,
|
|
3664
|
+
"ai.usage.promptTokens": result.usage.inputTokens.total,
|
|
3665
|
+
"ai.usage.completionTokens": result.usage.outputTokens.total,
|
|
3395
3666
|
// standardized gen-ai llm span attributes:
|
|
3396
|
-
"gen_ai.response.finish_reasons": [
|
|
3667
|
+
"gen_ai.response.finish_reasons": [
|
|
3668
|
+
result.finishReason.unified
|
|
3669
|
+
],
|
|
3397
3670
|
"gen_ai.response.id": responseData.id,
|
|
3398
3671
|
"gen_ai.response.model": responseData.modelId,
|
|
3399
|
-
"gen_ai.usage.input_tokens": result.usage.inputTokens,
|
|
3400
|
-
"gen_ai.usage.output_tokens": result.usage.outputTokens
|
|
3672
|
+
"gen_ai.usage.input_tokens": result.usage.inputTokens.total,
|
|
3673
|
+
"gen_ai.usage.output_tokens": result.usage.outputTokens.total
|
|
3401
3674
|
}
|
|
3402
3675
|
})
|
|
3403
3676
|
);
|
|
@@ -3482,25 +3755,47 @@ async function generateText({
|
|
|
3482
3755
|
})
|
|
3483
3756
|
);
|
|
3484
3757
|
}
|
|
3758
|
+
for (const toolCall of stepToolCalls) {
|
|
3759
|
+
if (!toolCall.providerExecuted)
|
|
3760
|
+
continue;
|
|
3761
|
+
const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
|
|
3762
|
+
if ((tool2 == null ? void 0 : tool2.type) === "provider" && tool2.supportsDeferredResults) {
|
|
3763
|
+
const hasResultInResponse = currentModelResponse.content.some(
|
|
3764
|
+
(part) => part.type === "tool-result" && part.toolCallId === toolCall.toolCallId
|
|
3765
|
+
);
|
|
3766
|
+
if (!hasResultInResponse) {
|
|
3767
|
+
pendingDeferredToolCalls.set(toolCall.toolCallId, {
|
|
3768
|
+
toolName: toolCall.toolName
|
|
3769
|
+
});
|
|
3770
|
+
}
|
|
3771
|
+
}
|
|
3772
|
+
}
|
|
3773
|
+
for (const part of currentModelResponse.content) {
|
|
3774
|
+
if (part.type === "tool-result") {
|
|
3775
|
+
pendingDeferredToolCalls.delete(part.toolCallId);
|
|
3776
|
+
}
|
|
3777
|
+
}
|
|
3485
3778
|
const stepContent = asContent({
|
|
3486
3779
|
content: currentModelResponse.content,
|
|
3487
3780
|
toolCalls: stepToolCalls,
|
|
3488
3781
|
toolOutputs: clientToolOutputs,
|
|
3489
|
-
toolApprovalRequests: Object.values(toolApprovalRequests)
|
|
3782
|
+
toolApprovalRequests: Object.values(toolApprovalRequests),
|
|
3783
|
+
tools
|
|
3490
3784
|
});
|
|
3491
3785
|
responseMessages.push(
|
|
3492
|
-
...toResponseMessages({
|
|
3786
|
+
...await toResponseMessages({
|
|
3493
3787
|
content: stepContent,
|
|
3494
3788
|
tools
|
|
3495
3789
|
})
|
|
3496
3790
|
);
|
|
3497
3791
|
const currentStepResult = new DefaultStepResult({
|
|
3498
3792
|
content: stepContent,
|
|
3499
|
-
finishReason: currentModelResponse.finishReason,
|
|
3500
|
-
|
|
3793
|
+
finishReason: currentModelResponse.finishReason.unified,
|
|
3794
|
+
rawFinishReason: currentModelResponse.finishReason.raw,
|
|
3795
|
+
usage: asLanguageModelUsage(currentModelResponse.usage),
|
|
3501
3796
|
warnings: currentModelResponse.warnings,
|
|
3502
3797
|
providerMetadata: currentModelResponse.providerMetadata,
|
|
3503
|
-
request: (
|
|
3798
|
+
request: (_g = currentModelResponse.request) != null ? _g : {},
|
|
3504
3799
|
response: {
|
|
3505
3800
|
...currentModelResponse.response,
|
|
3506
3801
|
// deep clone msgs to avoid mutating past messages in multi-step:
|
|
@@ -3508,23 +3803,24 @@ async function generateText({
|
|
|
3508
3803
|
}
|
|
3509
3804
|
});
|
|
3510
3805
|
logWarnings({
|
|
3511
|
-
warnings: (
|
|
3806
|
+
warnings: (_h = currentModelResponse.warnings) != null ? _h : [],
|
|
3512
3807
|
provider: stepModel.provider,
|
|
3513
3808
|
model: stepModel.modelId
|
|
3514
3809
|
});
|
|
3515
3810
|
steps.push(currentStepResult);
|
|
3516
3811
|
await (onStepFinish == null ? void 0 : onStepFinish(currentStepResult));
|
|
3517
3812
|
} while (
|
|
3518
|
-
//
|
|
3519
|
-
|
|
3520
|
-
|
|
3813
|
+
// Continue if:
|
|
3814
|
+
// 1. There are client tool calls that have all been executed, OR
|
|
3815
|
+
// 2. There are pending deferred results from provider-executed tools
|
|
3816
|
+
(clientToolCalls.length > 0 && clientToolOutputs.length === clientToolCalls.length || pendingDeferredToolCalls.size > 0) && // continue until a stop condition is met:
|
|
3521
3817
|
!await isStopConditionMet({ stopConditions, steps })
|
|
3522
3818
|
);
|
|
3523
3819
|
span.setAttributes(
|
|
3524
3820
|
await selectTelemetryAttributes({
|
|
3525
3821
|
telemetry,
|
|
3526
3822
|
attributes: {
|
|
3527
|
-
"ai.response.finishReason": currentModelResponse.finishReason,
|
|
3823
|
+
"ai.response.finishReason": currentModelResponse.finishReason.unified,
|
|
3528
3824
|
"ai.response.text": {
|
|
3529
3825
|
output: () => extractTextContent(currentModelResponse.content)
|
|
3530
3826
|
},
|
|
@@ -3538,8 +3834,8 @@ async function generateText({
|
|
|
3538
3834
|
currentModelResponse.providerMetadata
|
|
3539
3835
|
),
|
|
3540
3836
|
// TODO rename telemetry attributes to inputTokens and outputTokens
|
|
3541
|
-
"ai.usage.promptTokens": currentModelResponse.usage.inputTokens,
|
|
3542
|
-
"ai.usage.completionTokens": currentModelResponse.usage.outputTokens
|
|
3837
|
+
"ai.usage.promptTokens": currentModelResponse.usage.inputTokens.total,
|
|
3838
|
+
"ai.usage.completionTokens": currentModelResponse.usage.outputTokens.total
|
|
3543
3839
|
}
|
|
3544
3840
|
})
|
|
3545
3841
|
);
|
|
@@ -3558,6 +3854,7 @@ async function generateText({
|
|
|
3558
3854
|
);
|
|
3559
3855
|
await (onFinish == null ? void 0 : onFinish({
|
|
3560
3856
|
finishReason: lastStep.finishReason,
|
|
3857
|
+
rawFinishReason: lastStep.rawFinishReason,
|
|
3561
3858
|
usage: lastStep.usage,
|
|
3562
3859
|
content: lastStep.content,
|
|
3563
3860
|
text: lastStep.text,
|
|
@@ -3576,7 +3873,8 @@ async function generateText({
|
|
|
3576
3873
|
warnings: lastStep.warnings,
|
|
3577
3874
|
providerMetadata: lastStep.providerMetadata,
|
|
3578
3875
|
steps,
|
|
3579
|
-
totalUsage
|
|
3876
|
+
totalUsage,
|
|
3877
|
+
experimental_context
|
|
3580
3878
|
}));
|
|
3581
3879
|
let resolvedOutput;
|
|
3582
3880
|
if (lastStep.finishReason === "stop") {
|
|
@@ -3675,6 +3973,9 @@ var DefaultGenerateTextResult = class {
|
|
|
3675
3973
|
get finishReason() {
|
|
3676
3974
|
return this.finalStep.finishReason;
|
|
3677
3975
|
}
|
|
3976
|
+
get rawFinishReason() {
|
|
3977
|
+
return this.finalStep.rawFinishReason;
|
|
3978
|
+
}
|
|
3678
3979
|
get warnings() {
|
|
3679
3980
|
return this.finalStep.warnings;
|
|
3680
3981
|
}
|
|
@@ -3717,45 +4018,76 @@ function asContent({
|
|
|
3717
4018
|
content,
|
|
3718
4019
|
toolCalls,
|
|
3719
4020
|
toolOutputs,
|
|
3720
|
-
toolApprovalRequests
|
|
4021
|
+
toolApprovalRequests,
|
|
4022
|
+
tools
|
|
3721
4023
|
}) {
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
|
|
4024
|
+
const contentParts = [];
|
|
4025
|
+
for (const part of content) {
|
|
4026
|
+
switch (part.type) {
|
|
4027
|
+
case "text":
|
|
4028
|
+
case "reasoning":
|
|
4029
|
+
case "source":
|
|
4030
|
+
contentParts.push(part);
|
|
4031
|
+
break;
|
|
4032
|
+
case "file": {
|
|
4033
|
+
contentParts.push({
|
|
4034
|
+
type: "file",
|
|
4035
|
+
file: new DefaultGeneratedFile(part),
|
|
4036
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
4037
|
+
});
|
|
4038
|
+
break;
|
|
4039
|
+
}
|
|
4040
|
+
case "tool-call": {
|
|
4041
|
+
contentParts.push(
|
|
4042
|
+
toolCalls.find((toolCall) => toolCall.toolCallId === part.toolCallId)
|
|
4043
|
+
);
|
|
4044
|
+
break;
|
|
4045
|
+
}
|
|
4046
|
+
case "tool-result": {
|
|
4047
|
+
const toolCall = toolCalls.find(
|
|
4048
|
+
(toolCall2) => toolCall2.toolCallId === part.toolCallId
|
|
4049
|
+
);
|
|
4050
|
+
if (toolCall == null) {
|
|
4051
|
+
const tool2 = tools == null ? void 0 : tools[part.toolName];
|
|
4052
|
+
const supportsDeferredResults = (tool2 == null ? void 0 : tool2.type) === "provider" && tool2.supportsDeferredResults;
|
|
4053
|
+
if (!supportsDeferredResults) {
|
|
3745
4054
|
throw new Error(`Tool call ${part.toolCallId} not found.`);
|
|
3746
4055
|
}
|
|
3747
4056
|
if (part.isError) {
|
|
3748
|
-
|
|
4057
|
+
contentParts.push({
|
|
3749
4058
|
type: "tool-error",
|
|
3750
4059
|
toolCallId: part.toolCallId,
|
|
3751
4060
|
toolName: part.toolName,
|
|
3752
|
-
input:
|
|
4061
|
+
input: void 0,
|
|
3753
4062
|
error: part.result,
|
|
3754
4063
|
providerExecuted: true,
|
|
3755
|
-
dynamic:
|
|
3756
|
-
};
|
|
4064
|
+
dynamic: part.dynamic
|
|
4065
|
+
});
|
|
4066
|
+
} else {
|
|
4067
|
+
contentParts.push({
|
|
4068
|
+
type: "tool-result",
|
|
4069
|
+
toolCallId: part.toolCallId,
|
|
4070
|
+
toolName: part.toolName,
|
|
4071
|
+
input: void 0,
|
|
4072
|
+
output: part.result,
|
|
4073
|
+
providerExecuted: true,
|
|
4074
|
+
dynamic: part.dynamic
|
|
4075
|
+
});
|
|
3757
4076
|
}
|
|
3758
|
-
|
|
4077
|
+
break;
|
|
4078
|
+
}
|
|
4079
|
+
if (part.isError) {
|
|
4080
|
+
contentParts.push({
|
|
4081
|
+
type: "tool-error",
|
|
4082
|
+
toolCallId: part.toolCallId,
|
|
4083
|
+
toolName: part.toolName,
|
|
4084
|
+
input: toolCall.input,
|
|
4085
|
+
error: part.result,
|
|
4086
|
+
providerExecuted: true,
|
|
4087
|
+
dynamic: toolCall.dynamic
|
|
4088
|
+
});
|
|
4089
|
+
} else {
|
|
4090
|
+
contentParts.push({
|
|
3759
4091
|
type: "tool-result",
|
|
3760
4092
|
toolCallId: part.toolCallId,
|
|
3761
4093
|
toolName: part.toolName,
|
|
@@ -3763,13 +4095,16 @@ function asContent({
|
|
|
3763
4095
|
output: part.result,
|
|
3764
4096
|
providerExecuted: true,
|
|
3765
4097
|
dynamic: toolCall.dynamic
|
|
3766
|
-
};
|
|
4098
|
+
});
|
|
3767
4099
|
}
|
|
4100
|
+
break;
|
|
3768
4101
|
}
|
|
3769
|
-
|
|
3770
|
-
|
|
3771
|
-
|
|
3772
|
-
|
|
4102
|
+
case "tool-approval-request": {
|
|
4103
|
+
break;
|
|
4104
|
+
}
|
|
4105
|
+
}
|
|
4106
|
+
}
|
|
4107
|
+
return [...contentParts, ...toolOutputs, ...toolApprovalRequests];
|
|
3773
4108
|
}
|
|
3774
4109
|
|
|
3775
4110
|
// src/generate-text/stream-text.ts
|
|
@@ -3778,6 +4113,7 @@ import {
|
|
|
3778
4113
|
} from "@ai-sdk/provider";
|
|
3779
4114
|
import {
|
|
3780
4115
|
createIdGenerator as createIdGenerator2,
|
|
4116
|
+
DelayedPromise,
|
|
3781
4117
|
isAbortError as isAbortError2
|
|
3782
4118
|
} from "@ai-sdk/provider-utils";
|
|
3783
4119
|
|
|
@@ -3816,7 +4152,12 @@ function writeToServerResponse({
|
|
|
3816
4152
|
headers,
|
|
3817
4153
|
stream
|
|
3818
4154
|
}) {
|
|
3819
|
-
|
|
4155
|
+
const statusCode = status != null ? status : 200;
|
|
4156
|
+
if (statusText !== void 0) {
|
|
4157
|
+
response.writeHead(statusCode, statusText, headers);
|
|
4158
|
+
} else {
|
|
4159
|
+
response.writeHead(statusCode, headers);
|
|
4160
|
+
}
|
|
3820
4161
|
const reader = stream.getReader();
|
|
3821
4162
|
const read = async () => {
|
|
3822
4163
|
try {
|
|
@@ -4072,56 +4413,22 @@ var uiMessageChunkSchema = lazySchema(
|
|
|
4072
4413
|
"content-filter",
|
|
4073
4414
|
"tool-calls",
|
|
4074
4415
|
"error",
|
|
4075
|
-
"other"
|
|
4076
|
-
"unknown"
|
|
4416
|
+
"other"
|
|
4077
4417
|
]).optional(),
|
|
4078
4418
|
messageMetadata: z7.unknown().optional()
|
|
4079
4419
|
}),
|
|
4080
4420
|
z7.strictObject({
|
|
4081
|
-
type: z7.literal("abort")
|
|
4082
|
-
}),
|
|
4083
|
-
z7.strictObject({
|
|
4084
|
-
type: z7.literal("message-metadata"),
|
|
4085
|
-
messageMetadata: z7.unknown()
|
|
4086
|
-
})
|
|
4087
|
-
])
|
|
4088
|
-
)
|
|
4089
|
-
);
|
|
4090
|
-
function isDataUIMessageChunk(chunk) {
|
|
4091
|
-
return chunk.type.startsWith("data-");
|
|
4092
|
-
}
|
|
4093
|
-
|
|
4094
|
-
// src/util/merge-objects.ts
|
|
4095
|
-
function mergeObjects(base, overrides) {
|
|
4096
|
-
if (base === void 0 && overrides === void 0) {
|
|
4097
|
-
return void 0;
|
|
4098
|
-
}
|
|
4099
|
-
if (base === void 0) {
|
|
4100
|
-
return overrides;
|
|
4101
|
-
}
|
|
4102
|
-
if (overrides === void 0) {
|
|
4103
|
-
return base;
|
|
4104
|
-
}
|
|
4105
|
-
const result = { ...base };
|
|
4106
|
-
for (const key in overrides) {
|
|
4107
|
-
if (Object.prototype.hasOwnProperty.call(overrides, key)) {
|
|
4108
|
-
const overridesValue = overrides[key];
|
|
4109
|
-
if (overridesValue === void 0)
|
|
4110
|
-
continue;
|
|
4111
|
-
const baseValue = key in base ? base[key] : void 0;
|
|
4112
|
-
const isSourceObject = overridesValue !== null && typeof overridesValue === "object" && !Array.isArray(overridesValue) && !(overridesValue instanceof Date) && !(overridesValue instanceof RegExp);
|
|
4113
|
-
const isTargetObject = baseValue !== null && baseValue !== void 0 && typeof baseValue === "object" && !Array.isArray(baseValue) && !(baseValue instanceof Date) && !(baseValue instanceof RegExp);
|
|
4114
|
-
if (isSourceObject && isTargetObject) {
|
|
4115
|
-
result[key] = mergeObjects(
|
|
4116
|
-
baseValue,
|
|
4117
|
-
overridesValue
|
|
4118
|
-
);
|
|
4119
|
-
} else {
|
|
4120
|
-
result[key] = overridesValue;
|
|
4121
|
-
}
|
|
4122
|
-
}
|
|
4123
|
-
}
|
|
4124
|
-
return result;
|
|
4421
|
+
type: z7.literal("abort")
|
|
4422
|
+
}),
|
|
4423
|
+
z7.strictObject({
|
|
4424
|
+
type: z7.literal("message-metadata"),
|
|
4425
|
+
messageMetadata: z7.unknown()
|
|
4426
|
+
})
|
|
4427
|
+
])
|
|
4428
|
+
)
|
|
4429
|
+
);
|
|
4430
|
+
function isDataUIMessageChunk(chunk) {
|
|
4431
|
+
return chunk.type.startsWith("data-");
|
|
4125
4432
|
}
|
|
4126
4433
|
|
|
4127
4434
|
// src/ui/ui-messages.ts
|
|
@@ -4137,21 +4444,23 @@ function isFileUIPart(part) {
|
|
|
4137
4444
|
function isReasoningUIPart(part) {
|
|
4138
4445
|
return part.type === "reasoning";
|
|
4139
4446
|
}
|
|
4140
|
-
function
|
|
4447
|
+
function isStaticToolUIPart(part) {
|
|
4141
4448
|
return part.type.startsWith("tool-");
|
|
4142
4449
|
}
|
|
4143
4450
|
function isDynamicToolUIPart(part) {
|
|
4144
4451
|
return part.type === "dynamic-tool";
|
|
4145
4452
|
}
|
|
4146
|
-
function
|
|
4147
|
-
return
|
|
4453
|
+
function isToolUIPart(part) {
|
|
4454
|
+
return isStaticToolUIPart(part) || isDynamicToolUIPart(part);
|
|
4148
4455
|
}
|
|
4149
|
-
|
|
4456
|
+
var isToolOrDynamicToolUIPart = isToolUIPart;
|
|
4457
|
+
function getStaticToolName(part) {
|
|
4150
4458
|
return part.type.split("-").slice(1).join("-");
|
|
4151
4459
|
}
|
|
4152
|
-
function
|
|
4153
|
-
return isDynamicToolUIPart(part) ? part.toolName :
|
|
4460
|
+
function getToolName(part) {
|
|
4461
|
+
return isDynamicToolUIPart(part) ? part.toolName : getStaticToolName(part);
|
|
4154
4462
|
}
|
|
4463
|
+
var getToolOrDynamicToolName = getToolName;
|
|
4155
4464
|
|
|
4156
4465
|
// src/ui/process-ui-message-stream.ts
|
|
4157
4466
|
function createStreamingUIMessageState({
|
|
@@ -4183,11 +4492,9 @@ function processUIMessageStream({
|
|
|
4183
4492
|
new TransformStream({
|
|
4184
4493
|
async transform(chunk, controller) {
|
|
4185
4494
|
await runUpdateMessageJob(async ({ state, write }) => {
|
|
4186
|
-
var
|
|
4495
|
+
var _a14, _b, _c, _d;
|
|
4187
4496
|
function getToolInvocation(toolCallId) {
|
|
4188
|
-
const toolInvocations = state.message.parts.filter(
|
|
4189
|
-
isToolOrDynamicToolUIPart
|
|
4190
|
-
);
|
|
4497
|
+
const toolInvocations = state.message.parts.filter(isToolUIPart);
|
|
4191
4498
|
const toolInvocation = toolInvocations.find(
|
|
4192
4499
|
(invocation) => invocation.toolCallId === toolCallId
|
|
4193
4500
|
);
|
|
@@ -4199,9 +4506,9 @@ function processUIMessageStream({
|
|
|
4199
4506
|
return toolInvocation;
|
|
4200
4507
|
}
|
|
4201
4508
|
function updateToolPart(options) {
|
|
4202
|
-
var
|
|
4509
|
+
var _a15;
|
|
4203
4510
|
const part = state.message.parts.find(
|
|
4204
|
-
(part2) =>
|
|
4511
|
+
(part2) => isStaticToolUIPart(part2) && part2.toolCallId === options.toolCallId
|
|
4205
4512
|
);
|
|
4206
4513
|
const anyOptions = options;
|
|
4207
4514
|
const anyPart = part;
|
|
@@ -4215,7 +4522,7 @@ function processUIMessageStream({
|
|
|
4215
4522
|
if (options.title !== void 0) {
|
|
4216
4523
|
anyPart.title = options.title;
|
|
4217
4524
|
}
|
|
4218
|
-
anyPart.providerExecuted = (
|
|
4525
|
+
anyPart.providerExecuted = (_a15 = anyOptions.providerExecuted) != null ? _a15 : part.providerExecuted;
|
|
4219
4526
|
if (anyOptions.providerMetadata != null && part.state === "input-available") {
|
|
4220
4527
|
part.callProviderMetadata = anyOptions.providerMetadata;
|
|
4221
4528
|
}
|
|
@@ -4236,7 +4543,7 @@ function processUIMessageStream({
|
|
|
4236
4543
|
}
|
|
4237
4544
|
}
|
|
4238
4545
|
function updateDynamicToolPart(options) {
|
|
4239
|
-
var
|
|
4546
|
+
var _a15, _b2;
|
|
4240
4547
|
const part = state.message.parts.find(
|
|
4241
4548
|
(part2) => part2.type === "dynamic-tool" && part2.toolCallId === options.toolCallId
|
|
4242
4549
|
);
|
|
@@ -4248,7 +4555,7 @@ function processUIMessageStream({
|
|
|
4248
4555
|
anyPart.input = anyOptions.input;
|
|
4249
4556
|
anyPart.output = anyOptions.output;
|
|
4250
4557
|
anyPart.errorText = anyOptions.errorText;
|
|
4251
|
-
anyPart.rawInput = (
|
|
4558
|
+
anyPart.rawInput = (_a15 = anyOptions.rawInput) != null ? _a15 : anyPart.rawInput;
|
|
4252
4559
|
anyPart.preliminary = anyOptions.preliminary;
|
|
4253
4560
|
if (options.title !== void 0) {
|
|
4254
4561
|
anyPart.title = options.title;
|
|
@@ -4301,7 +4608,7 @@ function processUIMessageStream({
|
|
|
4301
4608
|
case "text-delta": {
|
|
4302
4609
|
const textPart = state.activeTextParts[chunk.id];
|
|
4303
4610
|
textPart.text += chunk.delta;
|
|
4304
|
-
textPart.providerMetadata = (
|
|
4611
|
+
textPart.providerMetadata = (_a14 = chunk.providerMetadata) != null ? _a14 : textPart.providerMetadata;
|
|
4305
4612
|
write();
|
|
4306
4613
|
break;
|
|
4307
4614
|
}
|
|
@@ -4373,7 +4680,7 @@ function processUIMessageStream({
|
|
|
4373
4680
|
break;
|
|
4374
4681
|
}
|
|
4375
4682
|
case "tool-input-start": {
|
|
4376
|
-
const toolInvocations = state.message.parts.filter(
|
|
4683
|
+
const toolInvocations = state.message.parts.filter(isStaticToolUIPart);
|
|
4377
4684
|
state.partialToolCalls[chunk.toolCallId] = {
|
|
4378
4685
|
text: "",
|
|
4379
4686
|
toolName: chunk.toolName,
|
|
@@ -4514,7 +4821,7 @@ function processUIMessageStream({
|
|
|
4514
4821
|
} else {
|
|
4515
4822
|
updateToolPart({
|
|
4516
4823
|
toolCallId: chunk.toolCallId,
|
|
4517
|
-
toolName:
|
|
4824
|
+
toolName: getStaticToolName(toolInvocation),
|
|
4518
4825
|
state: "output-available",
|
|
4519
4826
|
input: toolInvocation.input,
|
|
4520
4827
|
output: chunk.output,
|
|
@@ -4541,7 +4848,7 @@ function processUIMessageStream({
|
|
|
4541
4848
|
} else {
|
|
4542
4849
|
updateToolPart({
|
|
4543
4850
|
toolCallId: chunk.toolCallId,
|
|
4544
|
-
toolName:
|
|
4851
|
+
toolName: getStaticToolName(toolInvocation),
|
|
4545
4852
|
state: "output-error",
|
|
4546
4853
|
input: toolInvocation.input,
|
|
4547
4854
|
rawInput: toolInvocation.rawInput,
|
|
@@ -4740,11 +5047,11 @@ function createAsyncIterableStream(source) {
|
|
|
4740
5047
|
const reader = this.getReader();
|
|
4741
5048
|
let finished = false;
|
|
4742
5049
|
async function cleanup(cancelStream) {
|
|
4743
|
-
var
|
|
5050
|
+
var _a14;
|
|
4744
5051
|
finished = true;
|
|
4745
5052
|
try {
|
|
4746
5053
|
if (cancelStream) {
|
|
4747
|
-
await ((
|
|
5054
|
+
await ((_a14 = reader.cancel) == null ? void 0 : _a14.call(reader));
|
|
4748
5055
|
}
|
|
4749
5056
|
} finally {
|
|
4750
5057
|
try {
|
|
@@ -4908,48 +5215,10 @@ function createStitchableStream() {
|
|
|
4908
5215
|
};
|
|
4909
5216
|
}
|
|
4910
5217
|
|
|
4911
|
-
// src/util/delayed-promise.ts
|
|
4912
|
-
var DelayedPromise = class {
|
|
4913
|
-
constructor() {
|
|
4914
|
-
this.status = { type: "pending" };
|
|
4915
|
-
this._resolve = void 0;
|
|
4916
|
-
this._reject = void 0;
|
|
4917
|
-
}
|
|
4918
|
-
get promise() {
|
|
4919
|
-
if (this._promise) {
|
|
4920
|
-
return this._promise;
|
|
4921
|
-
}
|
|
4922
|
-
this._promise = new Promise((resolve3, reject) => {
|
|
4923
|
-
if (this.status.type === "resolved") {
|
|
4924
|
-
resolve3(this.status.value);
|
|
4925
|
-
} else if (this.status.type === "rejected") {
|
|
4926
|
-
reject(this.status.error);
|
|
4927
|
-
}
|
|
4928
|
-
this._resolve = resolve3;
|
|
4929
|
-
this._reject = reject;
|
|
4930
|
-
});
|
|
4931
|
-
return this._promise;
|
|
4932
|
-
}
|
|
4933
|
-
resolve(value) {
|
|
4934
|
-
var _a15;
|
|
4935
|
-
this.status = { type: "resolved", value };
|
|
4936
|
-
if (this._promise) {
|
|
4937
|
-
(_a15 = this._resolve) == null ? void 0 : _a15.call(this, value);
|
|
4938
|
-
}
|
|
4939
|
-
}
|
|
4940
|
-
reject(error) {
|
|
4941
|
-
var _a15;
|
|
4942
|
-
this.status = { type: "rejected", error };
|
|
4943
|
-
if (this._promise) {
|
|
4944
|
-
(_a15 = this._reject) == null ? void 0 : _a15.call(this, error);
|
|
4945
|
-
}
|
|
4946
|
-
}
|
|
4947
|
-
};
|
|
4948
|
-
|
|
4949
5218
|
// src/util/now.ts
|
|
4950
5219
|
function now() {
|
|
4951
|
-
var
|
|
4952
|
-
return (_b = (
|
|
5220
|
+
var _a14, _b;
|
|
5221
|
+
return (_b = (_a14 = globalThis == null ? void 0 : globalThis.performance) == null ? void 0 : _a14.now()) != null ? _b : Date.now();
|
|
4953
5222
|
}
|
|
4954
5223
|
|
|
4955
5224
|
// src/generate-text/run-tools-transformation.ts
|
|
@@ -5020,8 +5289,9 @@ function runToolsTransformation({
|
|
|
5020
5289
|
case "finish": {
|
|
5021
5290
|
finishChunk = {
|
|
5022
5291
|
type: "finish",
|
|
5023
|
-
finishReason: chunk.finishReason,
|
|
5024
|
-
|
|
5292
|
+
finishReason: chunk.finishReason.unified,
|
|
5293
|
+
rawFinishReason: chunk.finishReason.raw,
|
|
5294
|
+
usage: asLanguageModelUsage(chunk.usage),
|
|
5025
5295
|
providerMetadata: chunk.providerMetadata
|
|
5026
5296
|
};
|
|
5027
5297
|
break;
|
|
@@ -5125,6 +5395,9 @@ function runToolsTransformation({
|
|
|
5125
5395
|
}
|
|
5126
5396
|
break;
|
|
5127
5397
|
}
|
|
5398
|
+
case "tool-approval-request": {
|
|
5399
|
+
break;
|
|
5400
|
+
}
|
|
5128
5401
|
default: {
|
|
5129
5402
|
const _exhaustiveCheck = chunkType;
|
|
5130
5403
|
throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`);
|
|
@@ -5260,7 +5533,7 @@ function createOutputTransformStream(output) {
|
|
|
5260
5533
|
}
|
|
5261
5534
|
return new TransformStream({
|
|
5262
5535
|
async transform(chunk, controller) {
|
|
5263
|
-
var
|
|
5536
|
+
var _a14;
|
|
5264
5537
|
if (chunk.type === "finish-step" && textChunk.length > 0) {
|
|
5265
5538
|
publishTextChunk({ controller });
|
|
5266
5539
|
}
|
|
@@ -5287,7 +5560,7 @@ function createOutputTransformStream(output) {
|
|
|
5287
5560
|
}
|
|
5288
5561
|
text2 += chunk.text;
|
|
5289
5562
|
textChunk += chunk.text;
|
|
5290
|
-
textProviderMetadata = (
|
|
5563
|
+
textProviderMetadata = (_a14 = chunk.providerMetadata) != null ? _a14 : textProviderMetadata;
|
|
5291
5564
|
const result = await output.parsePartialOutput({ text: text2 });
|
|
5292
5565
|
if (result !== void 0) {
|
|
5293
5566
|
const currentJson = JSON.stringify(result.partial);
|
|
@@ -5333,6 +5606,7 @@ var DefaultStreamTextResult = class {
|
|
|
5333
5606
|
}) {
|
|
5334
5607
|
this._totalUsage = new DelayedPromise();
|
|
5335
5608
|
this._finishReason = new DelayedPromise();
|
|
5609
|
+
this._rawFinishReason = new DelayedPromise();
|
|
5336
5610
|
this._steps = new DelayedPromise();
|
|
5337
5611
|
this.outputSpecification = output;
|
|
5338
5612
|
this.includeRawChunks = includeRawChunks;
|
|
@@ -5341,16 +5615,18 @@ var DefaultStreamTextResult = class {
|
|
|
5341
5615
|
let recordedContent = [];
|
|
5342
5616
|
const recordedResponseMessages = [];
|
|
5343
5617
|
let recordedFinishReason = void 0;
|
|
5618
|
+
let recordedRawFinishReason = void 0;
|
|
5344
5619
|
let recordedTotalUsage = void 0;
|
|
5345
5620
|
let recordedRequest = {};
|
|
5346
5621
|
let recordedWarnings = [];
|
|
5347
5622
|
const recordedSteps = [];
|
|
5623
|
+
const pendingDeferredToolCalls = /* @__PURE__ */ new Map();
|
|
5348
5624
|
let rootSpan;
|
|
5349
5625
|
let activeTextContent = {};
|
|
5350
5626
|
let activeReasoningContent = {};
|
|
5351
5627
|
const eventProcessor = new TransformStream({
|
|
5352
5628
|
async transform(chunk, controller) {
|
|
5353
|
-
var
|
|
5629
|
+
var _a14, _b, _c, _d;
|
|
5354
5630
|
controller.enqueue(chunk);
|
|
5355
5631
|
const { part } = chunk;
|
|
5356
5632
|
if (part.type === "text-delta" || part.type === "reasoning-delta" || part.type === "source" || part.type === "tool-call" || part.type === "tool-result" || part.type === "tool-input-start" || part.type === "tool-input-delta" || part.type === "raw") {
|
|
@@ -5380,7 +5656,7 @@ var DefaultStreamTextResult = class {
|
|
|
5380
5656
|
return;
|
|
5381
5657
|
}
|
|
5382
5658
|
activeText.text += part.text;
|
|
5383
|
-
activeText.providerMetadata = (
|
|
5659
|
+
activeText.providerMetadata = (_a14 = part.providerMetadata) != null ? _a14 : activeText.providerMetadata;
|
|
5384
5660
|
}
|
|
5385
5661
|
if (part.type === "text-end") {
|
|
5386
5662
|
const activeText = activeTextContent[part.id];
|
|
@@ -5461,13 +5737,14 @@ var DefaultStreamTextResult = class {
|
|
|
5461
5737
|
recordedWarnings = part.warnings;
|
|
5462
5738
|
}
|
|
5463
5739
|
if (part.type === "finish-step") {
|
|
5464
|
-
const stepMessages = toResponseMessages({
|
|
5740
|
+
const stepMessages = await toResponseMessages({
|
|
5465
5741
|
content: recordedContent,
|
|
5466
5742
|
tools
|
|
5467
5743
|
});
|
|
5468
5744
|
const currentStepResult = new DefaultStepResult({
|
|
5469
5745
|
content: recordedContent,
|
|
5470
5746
|
finishReason: part.finishReason,
|
|
5747
|
+
rawFinishReason: part.rawFinishReason,
|
|
5471
5748
|
usage: part.usage,
|
|
5472
5749
|
warnings: recordedWarnings,
|
|
5473
5750
|
request: recordedRequest,
|
|
@@ -5490,6 +5767,7 @@ var DefaultStreamTextResult = class {
|
|
|
5490
5767
|
if (part.type === "finish") {
|
|
5491
5768
|
recordedTotalUsage = part.totalUsage;
|
|
5492
5769
|
recordedFinishReason = part.finishReason;
|
|
5770
|
+
recordedRawFinishReason = part.rawFinishReason;
|
|
5493
5771
|
}
|
|
5494
5772
|
},
|
|
5495
5773
|
async flush(controller) {
|
|
@@ -5499,22 +5777,21 @@ var DefaultStreamTextResult = class {
|
|
|
5499
5777
|
message: "No output generated. Check the stream for errors."
|
|
5500
5778
|
});
|
|
5501
5779
|
self._finishReason.reject(error);
|
|
5780
|
+
self._rawFinishReason.reject(error);
|
|
5502
5781
|
self._totalUsage.reject(error);
|
|
5503
5782
|
self._steps.reject(error);
|
|
5504
5783
|
return;
|
|
5505
5784
|
}
|
|
5506
|
-
const finishReason = recordedFinishReason != null ? recordedFinishReason : "
|
|
5507
|
-
const totalUsage = recordedTotalUsage != null ? recordedTotalUsage :
|
|
5508
|
-
inputTokens: void 0,
|
|
5509
|
-
outputTokens: void 0,
|
|
5510
|
-
totalTokens: void 0
|
|
5511
|
-
};
|
|
5785
|
+
const finishReason = recordedFinishReason != null ? recordedFinishReason : "other";
|
|
5786
|
+
const totalUsage = recordedTotalUsage != null ? recordedTotalUsage : createNullLanguageModelUsage();
|
|
5512
5787
|
self._finishReason.resolve(finishReason);
|
|
5788
|
+
self._rawFinishReason.resolve(recordedRawFinishReason);
|
|
5513
5789
|
self._totalUsage.resolve(totalUsage);
|
|
5514
5790
|
self._steps.resolve(recordedSteps);
|
|
5515
5791
|
const finalStep = recordedSteps[recordedSteps.length - 1];
|
|
5516
5792
|
await (onFinish == null ? void 0 : onFinish({
|
|
5517
|
-
finishReason,
|
|
5793
|
+
finishReason: finalStep.finishReason,
|
|
5794
|
+
rawFinishReason: finalStep.rawFinishReason,
|
|
5518
5795
|
totalUsage,
|
|
5519
5796
|
usage: finalStep.usage,
|
|
5520
5797
|
content: finalStep.content,
|
|
@@ -5533,7 +5810,8 @@ var DefaultStreamTextResult = class {
|
|
|
5533
5810
|
response: finalStep.response,
|
|
5534
5811
|
warnings: finalStep.warnings,
|
|
5535
5812
|
providerMetadata: finalStep.providerMetadata,
|
|
5536
|
-
steps: recordedSteps
|
|
5813
|
+
steps: recordedSteps,
|
|
5814
|
+
experimental_context
|
|
5537
5815
|
}));
|
|
5538
5816
|
rootSpan.setAttributes(
|
|
5539
5817
|
await selectTelemetryAttributes({
|
|
@@ -5543,8 +5821,8 @@ var DefaultStreamTextResult = class {
|
|
|
5543
5821
|
"ai.response.text": { output: () => finalStep.text },
|
|
5544
5822
|
"ai.response.toolCalls": {
|
|
5545
5823
|
output: () => {
|
|
5546
|
-
var
|
|
5547
|
-
return ((
|
|
5824
|
+
var _a14;
|
|
5825
|
+
return ((_a14 = finalStep.toolCalls) == null ? void 0 : _a14.length) ? JSON.stringify(finalStep.toolCalls) : void 0;
|
|
5548
5826
|
}
|
|
5549
5827
|
},
|
|
5550
5828
|
"ai.response.providerMetadata": JSON.stringify(
|
|
@@ -5688,31 +5966,35 @@ var DefaultStreamTextResult = class {
|
|
|
5688
5966
|
}
|
|
5689
5967
|
})
|
|
5690
5968
|
);
|
|
5969
|
+
const content = [];
|
|
5970
|
+
for (const output2 of toolOutputs) {
|
|
5971
|
+
content.push({
|
|
5972
|
+
type: "tool-result",
|
|
5973
|
+
toolCallId: output2.toolCallId,
|
|
5974
|
+
toolName: output2.toolName,
|
|
5975
|
+
output: await createToolModelOutput({
|
|
5976
|
+
toolCallId: output2.toolCallId,
|
|
5977
|
+
input: output2.input,
|
|
5978
|
+
tool: tools == null ? void 0 : tools[output2.toolName],
|
|
5979
|
+
output: output2.type === "tool-result" ? output2.output : output2.error,
|
|
5980
|
+
errorMode: output2.type === "tool-error" ? "json" : "none"
|
|
5981
|
+
})
|
|
5982
|
+
});
|
|
5983
|
+
}
|
|
5984
|
+
for (const toolApproval of deniedToolApprovals) {
|
|
5985
|
+
content.push({
|
|
5986
|
+
type: "tool-result",
|
|
5987
|
+
toolCallId: toolApproval.toolCall.toolCallId,
|
|
5988
|
+
toolName: toolApproval.toolCall.toolName,
|
|
5989
|
+
output: {
|
|
5990
|
+
type: "execution-denied",
|
|
5991
|
+
reason: toolApproval.approvalResponse.reason
|
|
5992
|
+
}
|
|
5993
|
+
});
|
|
5994
|
+
}
|
|
5691
5995
|
initialResponseMessages.push({
|
|
5692
5996
|
role: "tool",
|
|
5693
|
-
content
|
|
5694
|
-
// add regular tool results for approved tool calls:
|
|
5695
|
-
...toolOutputs.map((output2) => ({
|
|
5696
|
-
type: "tool-result",
|
|
5697
|
-
toolCallId: output2.toolCallId,
|
|
5698
|
-
toolName: output2.toolName,
|
|
5699
|
-
output: createToolModelOutput({
|
|
5700
|
-
tool: tools == null ? void 0 : tools[output2.toolName],
|
|
5701
|
-
output: output2.type === "tool-result" ? output2.output : output2.error,
|
|
5702
|
-
errorMode: output2.type === "tool-error" ? "json" : "none"
|
|
5703
|
-
})
|
|
5704
|
-
})),
|
|
5705
|
-
// add execution denied tool results for denied tool approvals:
|
|
5706
|
-
...deniedToolApprovals.map((toolApproval) => ({
|
|
5707
|
-
type: "tool-result",
|
|
5708
|
-
toolCallId: toolApproval.toolCall.toolCallId,
|
|
5709
|
-
toolName: toolApproval.toolCall.toolName,
|
|
5710
|
-
output: {
|
|
5711
|
-
type: "execution-denied",
|
|
5712
|
-
reason: toolApproval.approvalResponse.reason
|
|
5713
|
-
}
|
|
5714
|
-
}))
|
|
5715
|
-
]
|
|
5997
|
+
content
|
|
5716
5998
|
});
|
|
5717
5999
|
} finally {
|
|
5718
6000
|
toolExecutionStepStreamController == null ? void 0 : toolExecutionStepStreamController.close();
|
|
@@ -5724,7 +6006,7 @@ var DefaultStreamTextResult = class {
|
|
|
5724
6006
|
responseMessages,
|
|
5725
6007
|
usage
|
|
5726
6008
|
}) {
|
|
5727
|
-
var
|
|
6009
|
+
var _a14, _b, _c, _d, _e, _f;
|
|
5728
6010
|
const includeRawChunks2 = self.includeRawChunks;
|
|
5729
6011
|
stepFinish = new DelayedPromise();
|
|
5730
6012
|
const stepInputMessages = [...initialMessages, ...responseMessages];
|
|
@@ -5732,10 +6014,11 @@ var DefaultStreamTextResult = class {
|
|
|
5732
6014
|
model,
|
|
5733
6015
|
steps: recordedSteps,
|
|
5734
6016
|
stepNumber: recordedSteps.length,
|
|
5735
|
-
messages: stepInputMessages
|
|
6017
|
+
messages: stepInputMessages,
|
|
6018
|
+
experimental_context
|
|
5736
6019
|
}));
|
|
5737
6020
|
const stepModel = resolveLanguageModel(
|
|
5738
|
-
(
|
|
6021
|
+
(_a14 = prepareStepResult == null ? void 0 : prepareStepResult.model) != null ? _a14 : model
|
|
5739
6022
|
);
|
|
5740
6023
|
const promptMessages = await convertToLanguageModelPrompt({
|
|
5741
6024
|
prompt: {
|
|
@@ -5750,6 +6033,11 @@ var DefaultStreamTextResult = class {
|
|
|
5750
6033
|
toolChoice: (_d = prepareStepResult == null ? void 0 : prepareStepResult.toolChoice) != null ? _d : toolChoice,
|
|
5751
6034
|
activeTools: (_e = prepareStepResult == null ? void 0 : prepareStepResult.activeTools) != null ? _e : activeTools
|
|
5752
6035
|
});
|
|
6036
|
+
experimental_context = (_f = prepareStepResult == null ? void 0 : prepareStepResult.experimental_context) != null ? _f : experimental_context;
|
|
6037
|
+
const stepProviderOptions = mergeObjects(
|
|
6038
|
+
providerOptions,
|
|
6039
|
+
prepareStepResult == null ? void 0 : prepareStepResult.providerOptions
|
|
6040
|
+
);
|
|
5753
6041
|
const {
|
|
5754
6042
|
result: { stream: stream2, response, request },
|
|
5755
6043
|
doStreamSpan,
|
|
@@ -5803,7 +6091,7 @@ var DefaultStreamTextResult = class {
|
|
|
5803
6091
|
toolChoice: stepToolChoice,
|
|
5804
6092
|
responseFormat: await (output == null ? void 0 : output.responseFormat),
|
|
5805
6093
|
prompt: promptMessages,
|
|
5806
|
-
providerOptions,
|
|
6094
|
+
providerOptions: stepProviderOptions,
|
|
5807
6095
|
abortSignal,
|
|
5808
6096
|
headers,
|
|
5809
6097
|
includeRawChunks: includeRawChunks2
|
|
@@ -5828,12 +6116,9 @@ var DefaultStreamTextResult = class {
|
|
|
5828
6116
|
const stepToolOutputs = [];
|
|
5829
6117
|
let warnings;
|
|
5830
6118
|
const activeToolCallToolNames = {};
|
|
5831
|
-
let stepFinishReason = "
|
|
5832
|
-
let
|
|
5833
|
-
|
|
5834
|
-
outputTokens: void 0,
|
|
5835
|
-
totalTokens: void 0
|
|
5836
|
-
};
|
|
6119
|
+
let stepFinishReason = "other";
|
|
6120
|
+
let stepRawFinishReason = void 0;
|
|
6121
|
+
let stepUsage = createNullLanguageModelUsage();
|
|
5837
6122
|
let stepProviderMetadata;
|
|
5838
6123
|
let stepFirstChunk = true;
|
|
5839
6124
|
let stepResponse = {
|
|
@@ -5846,7 +6131,7 @@ var DefaultStreamTextResult = class {
|
|
|
5846
6131
|
streamWithToolResults.pipeThrough(
|
|
5847
6132
|
new TransformStream({
|
|
5848
6133
|
async transform(chunk, controller) {
|
|
5849
|
-
var
|
|
6134
|
+
var _a15, _b2, _c2, _d2, _e2;
|
|
5850
6135
|
if (chunk.type === "stream-start") {
|
|
5851
6136
|
warnings = chunk.warnings;
|
|
5852
6137
|
return;
|
|
@@ -5919,7 +6204,7 @@ var DefaultStreamTextResult = class {
|
|
|
5919
6204
|
}
|
|
5920
6205
|
case "response-metadata": {
|
|
5921
6206
|
stepResponse = {
|
|
5922
|
-
id: (
|
|
6207
|
+
id: (_a15 = chunk.id) != null ? _a15 : stepResponse.id,
|
|
5923
6208
|
timestamp: (_b2 = chunk.timestamp) != null ? _b2 : stepResponse.timestamp,
|
|
5924
6209
|
modelId: (_c2 = chunk.modelId) != null ? _c2 : stepResponse.modelId
|
|
5925
6210
|
};
|
|
@@ -5928,6 +6213,7 @@ var DefaultStreamTextResult = class {
|
|
|
5928
6213
|
case "finish": {
|
|
5929
6214
|
stepUsage = chunk.usage;
|
|
5930
6215
|
stepFinishReason = chunk.finishReason;
|
|
6216
|
+
stepRawFinishReason = chunk.rawFinishReason;
|
|
5931
6217
|
stepProviderMetadata = chunk.providerMetadata;
|
|
5932
6218
|
const msToFinish = now2() - startTimestampMs;
|
|
5933
6219
|
doStreamSpan.addEvent("ai.stream.finish");
|
|
@@ -6040,6 +6326,7 @@ var DefaultStreamTextResult = class {
|
|
|
6040
6326
|
controller.enqueue({
|
|
6041
6327
|
type: "finish-step",
|
|
6042
6328
|
finishReason: stepFinishReason,
|
|
6329
|
+
rawFinishReason: stepRawFinishReason,
|
|
6043
6330
|
usage: stepUsage,
|
|
6044
6331
|
providerMetadata: stepProviderMetadata,
|
|
6045
6332
|
response: {
|
|
@@ -6055,14 +6342,38 @@ var DefaultStreamTextResult = class {
|
|
|
6055
6342
|
const clientToolOutputs = stepToolOutputs.filter(
|
|
6056
6343
|
(toolOutput) => toolOutput.providerExecuted !== true
|
|
6057
6344
|
);
|
|
6058
|
-
|
|
6059
|
-
|
|
6060
|
-
|
|
6061
|
-
|
|
6062
|
-
|
|
6063
|
-
|
|
6345
|
+
for (const toolCall of stepToolCalls) {
|
|
6346
|
+
if (toolCall.providerExecuted !== true)
|
|
6347
|
+
continue;
|
|
6348
|
+
const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
|
|
6349
|
+
if ((tool2 == null ? void 0 : tool2.type) === "provider" && tool2.supportsDeferredResults) {
|
|
6350
|
+
const hasResultInStep = stepToolOutputs.some(
|
|
6351
|
+
(output2) => output2.type === "tool-result" && output2.toolCallId === toolCall.toolCallId
|
|
6352
|
+
);
|
|
6353
|
+
if (!hasResultInStep) {
|
|
6354
|
+
pendingDeferredToolCalls.set(toolCall.toolCallId, {
|
|
6355
|
+
toolName: toolCall.toolName
|
|
6356
|
+
});
|
|
6357
|
+
}
|
|
6358
|
+
}
|
|
6359
|
+
}
|
|
6360
|
+
for (const output2 of stepToolOutputs) {
|
|
6361
|
+
if (output2.type === "tool-result") {
|
|
6362
|
+
pendingDeferredToolCalls.delete(output2.toolCallId);
|
|
6363
|
+
}
|
|
6364
|
+
}
|
|
6365
|
+
if (
|
|
6366
|
+
// Continue if:
|
|
6367
|
+
// 1. There are client tool calls that have all been executed, OR
|
|
6368
|
+
// 2. There are pending deferred results from provider-executed tools
|
|
6369
|
+
(clientToolCalls.length > 0 && clientToolOutputs.length === clientToolCalls.length || pendingDeferredToolCalls.size > 0) && // continue until a stop condition is met:
|
|
6370
|
+
!await isStopConditionMet({
|
|
6371
|
+
stopConditions,
|
|
6372
|
+
steps: recordedSteps
|
|
6373
|
+
})
|
|
6374
|
+
) {
|
|
6064
6375
|
responseMessages.push(
|
|
6065
|
-
...toResponseMessages({
|
|
6376
|
+
...await toResponseMessages({
|
|
6066
6377
|
content: (
|
|
6067
6378
|
// use transformed content to create the messages for the next step:
|
|
6068
6379
|
recordedSteps[recordedSteps.length - 1].content
|
|
@@ -6087,6 +6398,7 @@ var DefaultStreamTextResult = class {
|
|
|
6087
6398
|
controller.enqueue({
|
|
6088
6399
|
type: "finish",
|
|
6089
6400
|
finishReason: stepFinishReason,
|
|
6401
|
+
rawFinishReason: stepRawFinishReason,
|
|
6090
6402
|
totalUsage: combinedUsage
|
|
6091
6403
|
});
|
|
6092
6404
|
self.closeStream();
|
|
@@ -6099,11 +6411,7 @@ var DefaultStreamTextResult = class {
|
|
|
6099
6411
|
await streamStep({
|
|
6100
6412
|
currentStep: 0,
|
|
6101
6413
|
responseMessages: initialResponseMessages,
|
|
6102
|
-
usage:
|
|
6103
|
-
inputTokens: void 0,
|
|
6104
|
-
outputTokens: void 0,
|
|
6105
|
-
totalTokens: void 0
|
|
6106
|
-
}
|
|
6414
|
+
usage: createNullLanguageModelUsage()
|
|
6107
6415
|
});
|
|
6108
6416
|
}
|
|
6109
6417
|
}).catch((error) => {
|
|
@@ -6184,6 +6492,10 @@ var DefaultStreamTextResult = class {
|
|
|
6184
6492
|
this.consumeStream();
|
|
6185
6493
|
return this._finishReason.promise;
|
|
6186
6494
|
}
|
|
6495
|
+
get rawFinishReason() {
|
|
6496
|
+
this.consumeStream();
|
|
6497
|
+
return this._rawFinishReason.promise;
|
|
6498
|
+
}
|
|
6187
6499
|
/**
|
|
6188
6500
|
Split out a new stream from the original stream.
|
|
6189
6501
|
The original stream is replaced to allow for further splitting,
|
|
@@ -6222,14 +6534,14 @@ var DefaultStreamTextResult = class {
|
|
|
6222
6534
|
);
|
|
6223
6535
|
}
|
|
6224
6536
|
async consumeStream(options) {
|
|
6225
|
-
var
|
|
6537
|
+
var _a14;
|
|
6226
6538
|
try {
|
|
6227
6539
|
await consumeStream({
|
|
6228
6540
|
stream: this.fullStream,
|
|
6229
6541
|
onError: options == null ? void 0 : options.onError
|
|
6230
6542
|
});
|
|
6231
6543
|
} catch (error) {
|
|
6232
|
-
(
|
|
6544
|
+
(_a14 = options == null ? void 0 : options.onError) == null ? void 0 : _a14.call(options, error);
|
|
6233
6545
|
}
|
|
6234
6546
|
}
|
|
6235
6547
|
get experimental_partialOutputStream() {
|
|
@@ -6250,8 +6562,8 @@ var DefaultStreamTextResult = class {
|
|
|
6250
6562
|
}
|
|
6251
6563
|
get output() {
|
|
6252
6564
|
return this.finalStep.then((step) => {
|
|
6253
|
-
var
|
|
6254
|
-
const output = (
|
|
6565
|
+
var _a14;
|
|
6566
|
+
const output = (_a14 = this.outputSpecification) != null ? _a14 : text();
|
|
6255
6567
|
return output.parseCompleteOutput(
|
|
6256
6568
|
{ text: step.text },
|
|
6257
6569
|
{
|
|
@@ -6278,8 +6590,8 @@ var DefaultStreamTextResult = class {
|
|
|
6278
6590
|
responseMessageId: generateMessageId
|
|
6279
6591
|
}) : void 0;
|
|
6280
6592
|
const isDynamic = (part) => {
|
|
6281
|
-
var
|
|
6282
|
-
const tool2 = (
|
|
6593
|
+
var _a14;
|
|
6594
|
+
const tool2 = (_a14 = this.tools) == null ? void 0 : _a14[part.toolName];
|
|
6283
6595
|
if (tool2 == null) {
|
|
6284
6596
|
return part.dynamic;
|
|
6285
6597
|
}
|
|
@@ -6617,10 +6929,10 @@ var ToolLoopAgent = class {
|
|
|
6617
6929
|
return this.settings.tools;
|
|
6618
6930
|
}
|
|
6619
6931
|
async prepareCall(options) {
|
|
6620
|
-
var
|
|
6932
|
+
var _a14, _b, _c, _d;
|
|
6621
6933
|
const baseCallArgs = {
|
|
6622
6934
|
...this.settings,
|
|
6623
|
-
stopWhen: (
|
|
6935
|
+
stopWhen: (_a14 = this.settings.stopWhen) != null ? _a14 : stepCountIs(20),
|
|
6624
6936
|
...options
|
|
6625
6937
|
};
|
|
6626
6938
|
const preparedCallArgs = (_d = await ((_c = (_b = this.settings).prepareCall) == null ? void 0 : _c.call(_b, baseCallArgs))) != null ? _d : baseCallArgs;
|
|
@@ -6648,11 +6960,13 @@ var ToolLoopAgent = class {
|
|
|
6648
6960
|
*/
|
|
6649
6961
|
async stream({
|
|
6650
6962
|
abortSignal,
|
|
6963
|
+
experimental_transform,
|
|
6651
6964
|
...options
|
|
6652
6965
|
}) {
|
|
6653
6966
|
return streamText({
|
|
6654
6967
|
...await this.prepareCall(options),
|
|
6655
|
-
abortSignal
|
|
6968
|
+
abortSignal,
|
|
6969
|
+
experimental_transform
|
|
6656
6970
|
});
|
|
6657
6971
|
}
|
|
6658
6972
|
};
|
|
@@ -6753,7 +7067,7 @@ function readUIMessageStream({
|
|
|
6753
7067
|
onError,
|
|
6754
7068
|
terminateOnError = false
|
|
6755
7069
|
}) {
|
|
6756
|
-
var
|
|
7070
|
+
var _a14;
|
|
6757
7071
|
let controller;
|
|
6758
7072
|
let hasErrored = false;
|
|
6759
7073
|
const outputStream = new ReadableStream({
|
|
@@ -6762,7 +7076,7 @@ function readUIMessageStream({
|
|
|
6762
7076
|
}
|
|
6763
7077
|
});
|
|
6764
7078
|
const state = createStreamingUIMessageState({
|
|
6765
|
-
messageId: (
|
|
7079
|
+
messageId: (_a14 = message == null ? void 0 : message.id) != null ? _a14 : "",
|
|
6766
7080
|
lastMessage: message
|
|
6767
7081
|
});
|
|
6768
7082
|
const handleError = (error) => {
|
|
@@ -6798,13 +7112,13 @@ function readUIMessageStream({
|
|
|
6798
7112
|
import {
|
|
6799
7113
|
isNonNullable
|
|
6800
7114
|
} from "@ai-sdk/provider-utils";
|
|
6801
|
-
function convertToModelMessages(messages, options) {
|
|
7115
|
+
async function convertToModelMessages(messages, options) {
|
|
6802
7116
|
const modelMessages = [];
|
|
6803
7117
|
if (options == null ? void 0 : options.ignoreIncompleteToolCalls) {
|
|
6804
7118
|
messages = messages.map((message) => ({
|
|
6805
7119
|
...message,
|
|
6806
7120
|
parts: message.parts.filter(
|
|
6807
|
-
(part) => !
|
|
7121
|
+
(part) => !isToolUIPart(part) || part.state !== "input-streaming" && part.state !== "input-available"
|
|
6808
7122
|
)
|
|
6809
7123
|
}));
|
|
6810
7124
|
}
|
|
@@ -6831,7 +7145,7 @@ function convertToModelMessages(messages, options) {
|
|
|
6831
7145
|
modelMessages.push({
|
|
6832
7146
|
role: "user",
|
|
6833
7147
|
content: message.parts.map((part) => {
|
|
6834
|
-
var
|
|
7148
|
+
var _a14;
|
|
6835
7149
|
if (isTextUIPart(part)) {
|
|
6836
7150
|
return {
|
|
6837
7151
|
type: "text",
|
|
@@ -6849,7 +7163,7 @@ function convertToModelMessages(messages, options) {
|
|
|
6849
7163
|
};
|
|
6850
7164
|
}
|
|
6851
7165
|
if (isDataUIPart(part)) {
|
|
6852
|
-
return (
|
|
7166
|
+
return (_a14 = options == null ? void 0 : options.convertDataPart) == null ? void 0 : _a14.call(
|
|
6853
7167
|
options,
|
|
6854
7168
|
part
|
|
6855
7169
|
);
|
|
@@ -6860,8 +7174,9 @@ function convertToModelMessages(messages, options) {
|
|
|
6860
7174
|
}
|
|
6861
7175
|
case "assistant": {
|
|
6862
7176
|
if (message.parts != null) {
|
|
6863
|
-
let
|
|
6864
|
-
|
|
7177
|
+
let block = [];
|
|
7178
|
+
async function processBlock() {
|
|
7179
|
+
var _a14, _b, _c, _d, _e, _f;
|
|
6865
7180
|
if (block.length === 0) {
|
|
6866
7181
|
return;
|
|
6867
7182
|
}
|
|
@@ -6886,14 +7201,14 @@ function convertToModelMessages(messages, options) {
|
|
|
6886
7201
|
text: part.text,
|
|
6887
7202
|
providerOptions: part.providerMetadata
|
|
6888
7203
|
});
|
|
6889
|
-
} else if (
|
|
6890
|
-
const toolName =
|
|
7204
|
+
} else if (isToolUIPart(part)) {
|
|
7205
|
+
const toolName = getToolName(part);
|
|
6891
7206
|
if (part.state !== "input-streaming") {
|
|
6892
7207
|
content.push({
|
|
6893
7208
|
type: "tool-call",
|
|
6894
7209
|
toolCallId: part.toolCallId,
|
|
6895
7210
|
toolName,
|
|
6896
|
-
input: part.state === "output-error" ? (
|
|
7211
|
+
input: part.state === "output-error" ? (_a14 = part.input) != null ? _a14 : "rawInput" in part ? part.rawInput : void 0 : part.input,
|
|
6897
7212
|
providerExecuted: part.providerExecuted,
|
|
6898
7213
|
...part.callProviderMetadata != null ? { providerOptions: part.callProviderMetadata } : {}
|
|
6899
7214
|
});
|
|
@@ -6909,11 +7224,14 @@ function convertToModelMessages(messages, options) {
|
|
|
6909
7224
|
type: "tool-result",
|
|
6910
7225
|
toolCallId: part.toolCallId,
|
|
6911
7226
|
toolName,
|
|
6912
|
-
output: createToolModelOutput({
|
|
7227
|
+
output: await createToolModelOutput({
|
|
7228
|
+
toolCallId: part.toolCallId,
|
|
7229
|
+
input: part.input,
|
|
6913
7230
|
output: part.state === "output-error" ? part.errorText : part.output,
|
|
6914
7231
|
tool: (_b = options == null ? void 0 : options.tools) == null ? void 0 : _b[toolName],
|
|
6915
7232
|
errorMode: part.state === "output-error" ? "json" : "none"
|
|
6916
|
-
})
|
|
7233
|
+
}),
|
|
7234
|
+
...part.callProviderMetadata != null ? { providerOptions: part.callProviderMetadata } : {}
|
|
6917
7235
|
});
|
|
6918
7236
|
}
|
|
6919
7237
|
}
|
|
@@ -6935,69 +7253,70 @@ function convertToModelMessages(messages, options) {
|
|
|
6935
7253
|
content
|
|
6936
7254
|
});
|
|
6937
7255
|
const toolParts = block.filter(
|
|
6938
|
-
(part) =>
|
|
7256
|
+
(part) => isToolUIPart(part) && part.providerExecuted !== true
|
|
6939
7257
|
);
|
|
6940
7258
|
if (toolParts.length > 0) {
|
|
6941
|
-
|
|
6942
|
-
|
|
6943
|
-
|
|
6944
|
-
(toolPart)
|
|
6945
|
-
|
|
6946
|
-
|
|
6947
|
-
|
|
6948
|
-
|
|
6949
|
-
|
|
6950
|
-
|
|
6951
|
-
|
|
6952
|
-
|
|
7259
|
+
{
|
|
7260
|
+
const content2 = [];
|
|
7261
|
+
for (const toolPart of toolParts) {
|
|
7262
|
+
if (((_d = toolPart.approval) == null ? void 0 : _d.approved) != null) {
|
|
7263
|
+
content2.push({
|
|
7264
|
+
type: "tool-approval-response",
|
|
7265
|
+
approvalId: toolPart.approval.id,
|
|
7266
|
+
approved: toolPart.approval.approved,
|
|
7267
|
+
reason: toolPart.approval.reason
|
|
7268
|
+
});
|
|
7269
|
+
}
|
|
7270
|
+
switch (toolPart.state) {
|
|
7271
|
+
case "output-denied": {
|
|
7272
|
+
content2.push({
|
|
7273
|
+
type: "tool-result",
|
|
7274
|
+
toolCallId: toolPart.toolCallId,
|
|
7275
|
+
toolName: getToolName(toolPart),
|
|
7276
|
+
output: {
|
|
7277
|
+
type: "error-text",
|
|
7278
|
+
value: (_e = toolPart.approval.reason) != null ? _e : "Tool execution denied."
|
|
7279
|
+
},
|
|
7280
|
+
...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
|
|
6953
7281
|
});
|
|
7282
|
+
break;
|
|
6954
7283
|
}
|
|
6955
|
-
|
|
6956
|
-
|
|
6957
|
-
|
|
6958
|
-
|
|
6959
|
-
|
|
6960
|
-
|
|
6961
|
-
|
|
6962
|
-
|
|
6963
|
-
value: (_b2 = toolPart.approval.reason) != null ? _b2 : "Tool execution denied."
|
|
6964
|
-
}
|
|
6965
|
-
});
|
|
6966
|
-
break;
|
|
6967
|
-
}
|
|
6968
|
-
case "output-error":
|
|
6969
|
-
case "output-available": {
|
|
6970
|
-
const toolName = getToolOrDynamicToolName(toolPart);
|
|
6971
|
-
outputs.push({
|
|
6972
|
-
type: "tool-result",
|
|
7284
|
+
case "output-error":
|
|
7285
|
+
case "output-available": {
|
|
7286
|
+
const toolName = getToolName(toolPart);
|
|
7287
|
+
content2.push({
|
|
7288
|
+
type: "tool-result",
|
|
7289
|
+
toolCallId: toolPart.toolCallId,
|
|
7290
|
+
toolName,
|
|
7291
|
+
output: await createToolModelOutput({
|
|
6973
7292
|
toolCallId: toolPart.toolCallId,
|
|
6974
|
-
|
|
6975
|
-
output:
|
|
6976
|
-
|
|
6977
|
-
|
|
6978
|
-
|
|
6979
|
-
|
|
6980
|
-
|
|
6981
|
-
|
|
6982
|
-
}
|
|
7293
|
+
input: toolPart.input,
|
|
7294
|
+
output: toolPart.state === "output-error" ? toolPart.errorText : toolPart.output,
|
|
7295
|
+
tool: (_f = options == null ? void 0 : options.tools) == null ? void 0 : _f[toolName],
|
|
7296
|
+
errorMode: toolPart.state === "output-error" ? "text" : "none"
|
|
7297
|
+
}),
|
|
7298
|
+
...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
|
|
7299
|
+
});
|
|
7300
|
+
break;
|
|
6983
7301
|
}
|
|
6984
|
-
return outputs;
|
|
6985
7302
|
}
|
|
6986
|
-
|
|
6987
|
-
|
|
7303
|
+
}
|
|
7304
|
+
modelMessages.push({
|
|
7305
|
+
role: "tool",
|
|
7306
|
+
content: content2
|
|
7307
|
+
});
|
|
7308
|
+
}
|
|
6988
7309
|
}
|
|
6989
7310
|
block = [];
|
|
6990
|
-
}
|
|
6991
|
-
var processBlock = processBlock2;
|
|
6992
|
-
let block = [];
|
|
7311
|
+
}
|
|
6993
7312
|
for (const part of message.parts) {
|
|
6994
|
-
if (isTextUIPart(part) || isReasoningUIPart(part) || isFileUIPart(part) ||
|
|
7313
|
+
if (isTextUIPart(part) || isReasoningUIPart(part) || isFileUIPart(part) || isToolUIPart(part) || isDataUIPart(part)) {
|
|
6995
7314
|
block.push(part);
|
|
6996
7315
|
} else if (part.type === "step-start") {
|
|
6997
|
-
|
|
7316
|
+
await processBlock();
|
|
6998
7317
|
}
|
|
6999
7318
|
}
|
|
7000
|
-
|
|
7319
|
+
await processBlock();
|
|
7001
7320
|
break;
|
|
7002
7321
|
}
|
|
7003
7322
|
break;
|
|
@@ -7013,7 +7332,6 @@ function convertToModelMessages(messages, options) {
|
|
|
7013
7332
|
}
|
|
7014
7333
|
return modelMessages;
|
|
7015
7334
|
}
|
|
7016
|
-
var convertToCoreMessages = convertToModelMessages;
|
|
7017
7335
|
|
|
7018
7336
|
// src/ui/validate-ui-messages.ts
|
|
7019
7337
|
import { TypeValidationError as TypeValidationError3 } from "@ai-sdk/provider";
|
|
@@ -7398,20 +7716,24 @@ async function validateUIMessages({
|
|
|
7398
7716
|
// src/agent/create-agent-ui-stream.ts
|
|
7399
7717
|
async function createAgentUIStream({
|
|
7400
7718
|
agent,
|
|
7401
|
-
|
|
7719
|
+
uiMessages,
|
|
7402
7720
|
options,
|
|
7721
|
+
abortSignal,
|
|
7722
|
+
experimental_transform,
|
|
7403
7723
|
...uiMessageStreamOptions
|
|
7404
7724
|
}) {
|
|
7405
7725
|
const validatedMessages = await validateUIMessages({
|
|
7406
|
-
messages,
|
|
7726
|
+
messages: uiMessages,
|
|
7407
7727
|
tools: agent.tools
|
|
7408
7728
|
});
|
|
7409
|
-
const modelMessages = convertToModelMessages(validatedMessages, {
|
|
7729
|
+
const modelMessages = await convertToModelMessages(validatedMessages, {
|
|
7410
7730
|
tools: agent.tools
|
|
7411
7731
|
});
|
|
7412
7732
|
const result = await agent.stream({
|
|
7413
7733
|
prompt: modelMessages,
|
|
7414
|
-
options
|
|
7734
|
+
options,
|
|
7735
|
+
abortSignal,
|
|
7736
|
+
experimental_transform
|
|
7415
7737
|
});
|
|
7416
7738
|
return result.toUIMessageStream(uiMessageStreamOptions);
|
|
7417
7739
|
}
|
|
@@ -7491,7 +7813,7 @@ async function embed({
|
|
|
7491
7813
|
}),
|
|
7492
7814
|
tracer,
|
|
7493
7815
|
fn: async (span) => {
|
|
7494
|
-
const { embedding, usage, response, providerMetadata } = await retry(
|
|
7816
|
+
const { embedding, usage, warnings, response, providerMetadata } = await retry(
|
|
7495
7817
|
() => (
|
|
7496
7818
|
// nested spans to align with the embedMany telemetry data:
|
|
7497
7819
|
recordSpan({
|
|
@@ -7510,7 +7832,7 @@ async function embed({
|
|
|
7510
7832
|
}),
|
|
7511
7833
|
tracer,
|
|
7512
7834
|
fn: async (doEmbedSpan) => {
|
|
7513
|
-
var
|
|
7835
|
+
var _a14;
|
|
7514
7836
|
const modelResponse = await model.doEmbed({
|
|
7515
7837
|
values: [value],
|
|
7516
7838
|
abortSignal,
|
|
@@ -7518,7 +7840,7 @@ async function embed({
|
|
|
7518
7840
|
providerOptions
|
|
7519
7841
|
});
|
|
7520
7842
|
const embedding2 = modelResponse.embeddings[0];
|
|
7521
|
-
const usage2 = (
|
|
7843
|
+
const usage2 = (_a14 = modelResponse.usage) != null ? _a14 : { tokens: NaN };
|
|
7522
7844
|
doEmbedSpan.setAttributes(
|
|
7523
7845
|
await selectTelemetryAttributes({
|
|
7524
7846
|
telemetry,
|
|
@@ -7535,6 +7857,7 @@ async function embed({
|
|
|
7535
7857
|
return {
|
|
7536
7858
|
embedding: embedding2,
|
|
7537
7859
|
usage: usage2,
|
|
7860
|
+
warnings: modelResponse.warnings,
|
|
7538
7861
|
providerMetadata: modelResponse.providerMetadata,
|
|
7539
7862
|
response: modelResponse.response
|
|
7540
7863
|
};
|
|
@@ -7551,10 +7874,12 @@ async function embed({
|
|
|
7551
7874
|
}
|
|
7552
7875
|
})
|
|
7553
7876
|
);
|
|
7877
|
+
logWarnings({ warnings, provider: model.provider, model: model.modelId });
|
|
7554
7878
|
return new DefaultEmbedResult({
|
|
7555
7879
|
value,
|
|
7556
7880
|
embedding,
|
|
7557
7881
|
usage,
|
|
7882
|
+
warnings,
|
|
7558
7883
|
providerMetadata,
|
|
7559
7884
|
response
|
|
7560
7885
|
});
|
|
@@ -7566,6 +7891,7 @@ var DefaultEmbedResult = class {
|
|
|
7566
7891
|
this.value = options.value;
|
|
7567
7892
|
this.embedding = options.embedding;
|
|
7568
7893
|
this.usage = options.usage;
|
|
7894
|
+
this.warnings = options.warnings;
|
|
7569
7895
|
this.providerMetadata = options.providerMetadata;
|
|
7570
7896
|
this.response = options.response;
|
|
7571
7897
|
}
|
|
@@ -7628,64 +7954,63 @@ async function embedMany({
|
|
|
7628
7954
|
}),
|
|
7629
7955
|
tracer,
|
|
7630
7956
|
fn: async (span) => {
|
|
7631
|
-
var
|
|
7957
|
+
var _a14;
|
|
7632
7958
|
const [maxEmbeddingsPerCall, supportsParallelCalls] = await Promise.all([
|
|
7633
7959
|
model.maxEmbeddingsPerCall,
|
|
7634
7960
|
model.supportsParallelCalls
|
|
7635
7961
|
]);
|
|
7636
7962
|
if (maxEmbeddingsPerCall == null || maxEmbeddingsPerCall === Infinity) {
|
|
7637
|
-
const { embeddings: embeddings2, usage, response, providerMetadata: providerMetadata2 } = await retry(
|
|
7638
|
-
(
|
|
7639
|
-
|
|
7640
|
-
|
|
7641
|
-
|
|
7642
|
-
|
|
7643
|
-
|
|
7644
|
-
|
|
7645
|
-
|
|
7646
|
-
|
|
7647
|
-
|
|
7648
|
-
|
|
7649
|
-
|
|
7650
|
-
|
|
7651
|
-
input: () => values.map((value) => JSON.stringify(value))
|
|
7652
|
-
}
|
|
7963
|
+
const { embeddings: embeddings2, usage, warnings: warnings2, response, providerMetadata: providerMetadata2 } = await retry(() => {
|
|
7964
|
+
return recordSpan({
|
|
7965
|
+
name: "ai.embedMany.doEmbed",
|
|
7966
|
+
attributes: selectTelemetryAttributes({
|
|
7967
|
+
telemetry,
|
|
7968
|
+
attributes: {
|
|
7969
|
+
...assembleOperationName({
|
|
7970
|
+
operationId: "ai.embedMany.doEmbed",
|
|
7971
|
+
telemetry
|
|
7972
|
+
}),
|
|
7973
|
+
...baseTelemetryAttributes,
|
|
7974
|
+
// specific settings that only make sense on the outer level:
|
|
7975
|
+
"ai.values": {
|
|
7976
|
+
input: () => values.map((value) => JSON.stringify(value))
|
|
7653
7977
|
}
|
|
7654
|
-
}),
|
|
7655
|
-
tracer,
|
|
7656
|
-
fn: async (doEmbedSpan) => {
|
|
7657
|
-
var _a16;
|
|
7658
|
-
const modelResponse = await model.doEmbed({
|
|
7659
|
-
values,
|
|
7660
|
-
abortSignal,
|
|
7661
|
-
headers: headersWithUserAgent,
|
|
7662
|
-
providerOptions
|
|
7663
|
-
});
|
|
7664
|
-
const embeddings3 = modelResponse.embeddings;
|
|
7665
|
-
const usage2 = (_a16 = modelResponse.usage) != null ? _a16 : { tokens: NaN };
|
|
7666
|
-
doEmbedSpan.setAttributes(
|
|
7667
|
-
await selectTelemetryAttributes({
|
|
7668
|
-
telemetry,
|
|
7669
|
-
attributes: {
|
|
7670
|
-
"ai.embeddings": {
|
|
7671
|
-
output: () => embeddings3.map(
|
|
7672
|
-
(embedding) => JSON.stringify(embedding)
|
|
7673
|
-
)
|
|
7674
|
-
},
|
|
7675
|
-
"ai.usage.tokens": usage2.tokens
|
|
7676
|
-
}
|
|
7677
|
-
})
|
|
7678
|
-
);
|
|
7679
|
-
return {
|
|
7680
|
-
embeddings: embeddings3,
|
|
7681
|
-
usage: usage2,
|
|
7682
|
-
providerMetadata: modelResponse.providerMetadata,
|
|
7683
|
-
response: modelResponse.response
|
|
7684
|
-
};
|
|
7685
7978
|
}
|
|
7686
|
-
})
|
|
7687
|
-
|
|
7688
|
-
|
|
7979
|
+
}),
|
|
7980
|
+
tracer,
|
|
7981
|
+
fn: async (doEmbedSpan) => {
|
|
7982
|
+
var _a15;
|
|
7983
|
+
const modelResponse = await model.doEmbed({
|
|
7984
|
+
values,
|
|
7985
|
+
abortSignal,
|
|
7986
|
+
headers: headersWithUserAgent,
|
|
7987
|
+
providerOptions
|
|
7988
|
+
});
|
|
7989
|
+
const embeddings3 = modelResponse.embeddings;
|
|
7990
|
+
const usage2 = (_a15 = modelResponse.usage) != null ? _a15 : { tokens: NaN };
|
|
7991
|
+
doEmbedSpan.setAttributes(
|
|
7992
|
+
await selectTelemetryAttributes({
|
|
7993
|
+
telemetry,
|
|
7994
|
+
attributes: {
|
|
7995
|
+
"ai.embeddings": {
|
|
7996
|
+
output: () => embeddings3.map(
|
|
7997
|
+
(embedding) => JSON.stringify(embedding)
|
|
7998
|
+
)
|
|
7999
|
+
},
|
|
8000
|
+
"ai.usage.tokens": usage2.tokens
|
|
8001
|
+
}
|
|
8002
|
+
})
|
|
8003
|
+
);
|
|
8004
|
+
return {
|
|
8005
|
+
embeddings: embeddings3,
|
|
8006
|
+
usage: usage2,
|
|
8007
|
+
warnings: modelResponse.warnings,
|
|
8008
|
+
providerMetadata: modelResponse.providerMetadata,
|
|
8009
|
+
response: modelResponse.response
|
|
8010
|
+
};
|
|
8011
|
+
}
|
|
8012
|
+
});
|
|
8013
|
+
});
|
|
7689
8014
|
span.setAttributes(
|
|
7690
8015
|
await selectTelemetryAttributes({
|
|
7691
8016
|
telemetry,
|
|
@@ -7697,16 +8022,23 @@ async function embedMany({
|
|
|
7697
8022
|
}
|
|
7698
8023
|
})
|
|
7699
8024
|
);
|
|
8025
|
+
logWarnings({
|
|
8026
|
+
warnings: warnings2,
|
|
8027
|
+
provider: model.provider,
|
|
8028
|
+
model: model.modelId
|
|
8029
|
+
});
|
|
7700
8030
|
return new DefaultEmbedManyResult({
|
|
7701
8031
|
values,
|
|
7702
8032
|
embeddings: embeddings2,
|
|
7703
8033
|
usage,
|
|
8034
|
+
warnings: warnings2,
|
|
7704
8035
|
providerMetadata: providerMetadata2,
|
|
7705
8036
|
responses: [response]
|
|
7706
8037
|
});
|
|
7707
8038
|
}
|
|
7708
8039
|
const valueChunks = splitArray(values, maxEmbeddingsPerCall);
|
|
7709
8040
|
const embeddings = [];
|
|
8041
|
+
const warnings = [];
|
|
7710
8042
|
const responses = [];
|
|
7711
8043
|
let tokens = 0;
|
|
7712
8044
|
let providerMetadata;
|
|
@@ -7736,7 +8068,7 @@ async function embedMany({
|
|
|
7736
8068
|
}),
|
|
7737
8069
|
tracer,
|
|
7738
8070
|
fn: async (doEmbedSpan) => {
|
|
7739
|
-
var
|
|
8071
|
+
var _a15;
|
|
7740
8072
|
const modelResponse = await model.doEmbed({
|
|
7741
8073
|
values: chunk,
|
|
7742
8074
|
abortSignal,
|
|
@@ -7744,7 +8076,7 @@ async function embedMany({
|
|
|
7744
8076
|
providerOptions
|
|
7745
8077
|
});
|
|
7746
8078
|
const embeddings2 = modelResponse.embeddings;
|
|
7747
|
-
const usage = (
|
|
8079
|
+
const usage = (_a15 = modelResponse.usage) != null ? _a15 : { tokens: NaN };
|
|
7748
8080
|
doEmbedSpan.setAttributes(
|
|
7749
8081
|
await selectTelemetryAttributes({
|
|
7750
8082
|
telemetry,
|
|
@@ -7761,6 +8093,7 @@ async function embedMany({
|
|
|
7761
8093
|
return {
|
|
7762
8094
|
embeddings: embeddings2,
|
|
7763
8095
|
usage,
|
|
8096
|
+
warnings: modelResponse.warnings,
|
|
7764
8097
|
providerMetadata: modelResponse.providerMetadata,
|
|
7765
8098
|
response: modelResponse.response
|
|
7766
8099
|
};
|
|
@@ -7771,6 +8104,7 @@ async function embedMany({
|
|
|
7771
8104
|
);
|
|
7772
8105
|
for (const result of results) {
|
|
7773
8106
|
embeddings.push(...result.embeddings);
|
|
8107
|
+
warnings.push(...result.warnings);
|
|
7774
8108
|
responses.push(result.response);
|
|
7775
8109
|
tokens += result.usage.tokens;
|
|
7776
8110
|
if (result.providerMetadata) {
|
|
@@ -7781,7 +8115,7 @@ async function embedMany({
|
|
|
7781
8115
|
result.providerMetadata
|
|
7782
8116
|
)) {
|
|
7783
8117
|
providerMetadata[providerName] = {
|
|
7784
|
-
...(
|
|
8118
|
+
...(_a14 = providerMetadata[providerName]) != null ? _a14 : {},
|
|
7785
8119
|
...metadata
|
|
7786
8120
|
};
|
|
7787
8121
|
}
|
|
@@ -7800,10 +8134,16 @@ async function embedMany({
|
|
|
7800
8134
|
}
|
|
7801
8135
|
})
|
|
7802
8136
|
);
|
|
8137
|
+
logWarnings({
|
|
8138
|
+
warnings,
|
|
8139
|
+
provider: model.provider,
|
|
8140
|
+
model: model.modelId
|
|
8141
|
+
});
|
|
7803
8142
|
return new DefaultEmbedManyResult({
|
|
7804
8143
|
values,
|
|
7805
8144
|
embeddings,
|
|
7806
8145
|
usage: { tokens },
|
|
8146
|
+
warnings,
|
|
7807
8147
|
providerMetadata,
|
|
7808
8148
|
responses
|
|
7809
8149
|
});
|
|
@@ -7815,16 +8155,20 @@ var DefaultEmbedManyResult = class {
|
|
|
7815
8155
|
this.values = options.values;
|
|
7816
8156
|
this.embeddings = options.embeddings;
|
|
7817
8157
|
this.usage = options.usage;
|
|
8158
|
+
this.warnings = options.warnings;
|
|
7818
8159
|
this.providerMetadata = options.providerMetadata;
|
|
7819
8160
|
this.responses = options.responses;
|
|
7820
8161
|
}
|
|
7821
8162
|
};
|
|
7822
8163
|
|
|
7823
8164
|
// src/generate-image/generate-image.ts
|
|
7824
|
-
import {
|
|
8165
|
+
import {
|
|
8166
|
+
convertBase64ToUint8Array as convertBase64ToUint8Array4,
|
|
8167
|
+
withUserAgentSuffix as withUserAgentSuffix5
|
|
8168
|
+
} from "@ai-sdk/provider-utils";
|
|
7825
8169
|
async function generateImage({
|
|
7826
|
-
model,
|
|
7827
|
-
prompt,
|
|
8170
|
+
model: modelArg,
|
|
8171
|
+
prompt: promptArg,
|
|
7828
8172
|
n = 1,
|
|
7829
8173
|
maxImagesPerCall,
|
|
7830
8174
|
size,
|
|
@@ -7835,14 +8179,8 @@ async function generateImage({
|
|
|
7835
8179
|
abortSignal,
|
|
7836
8180
|
headers
|
|
7837
8181
|
}) {
|
|
7838
|
-
var
|
|
7839
|
-
|
|
7840
|
-
throw new UnsupportedModelVersionError({
|
|
7841
|
-
version: model.specificationVersion,
|
|
7842
|
-
provider: model.provider,
|
|
7843
|
-
modelId: model.modelId
|
|
7844
|
-
});
|
|
7845
|
-
}
|
|
8182
|
+
var _a14, _b;
|
|
8183
|
+
const model = resolveImageModel(modelArg);
|
|
7846
8184
|
const headersWithUserAgent = withUserAgentSuffix5(
|
|
7847
8185
|
headers != null ? headers : {},
|
|
7848
8186
|
`ai/${VERSION}`
|
|
@@ -7851,7 +8189,7 @@ async function generateImage({
|
|
|
7851
8189
|
maxRetries: maxRetriesArg,
|
|
7852
8190
|
abortSignal
|
|
7853
8191
|
});
|
|
7854
|
-
const maxImagesPerCallWithDefault = (
|
|
8192
|
+
const maxImagesPerCallWithDefault = (_a14 = maxImagesPerCall != null ? maxImagesPerCall : await invokeModelMaxImagesPerCall(model)) != null ? _a14 : 1;
|
|
7855
8193
|
const callCount = Math.ceil(n / maxImagesPerCallWithDefault);
|
|
7856
8194
|
const callImageCounts = Array.from({ length: callCount }, (_, i) => {
|
|
7857
8195
|
if (i < callCount - 1) {
|
|
@@ -7862,9 +8200,12 @@ async function generateImage({
|
|
|
7862
8200
|
});
|
|
7863
8201
|
const results = await Promise.all(
|
|
7864
8202
|
callImageCounts.map(
|
|
7865
|
-
async (callImageCount) => retry(
|
|
7866
|
-
|
|
8203
|
+
async (callImageCount) => retry(() => {
|
|
8204
|
+
const { prompt, files, mask } = normalizePrompt(promptArg);
|
|
8205
|
+
return model.doGenerate({
|
|
7867
8206
|
prompt,
|
|
8207
|
+
files,
|
|
8208
|
+
mask,
|
|
7868
8209
|
n: callImageCount,
|
|
7869
8210
|
abortSignal,
|
|
7870
8211
|
headers: headersWithUserAgent,
|
|
@@ -7872,8 +8213,8 @@ async function generateImage({
|
|
|
7872
8213
|
aspectRatio,
|
|
7873
8214
|
seed,
|
|
7874
8215
|
providerOptions: providerOptions != null ? providerOptions : {}
|
|
7875
|
-
})
|
|
7876
|
-
)
|
|
8216
|
+
});
|
|
8217
|
+
})
|
|
7877
8218
|
)
|
|
7878
8219
|
);
|
|
7879
8220
|
const images = [];
|
|
@@ -7889,13 +8230,13 @@ async function generateImage({
|
|
|
7889
8230
|
images.push(
|
|
7890
8231
|
...result.images.map(
|
|
7891
8232
|
(image) => {
|
|
7892
|
-
var
|
|
8233
|
+
var _a15;
|
|
7893
8234
|
return new DefaultGeneratedFile({
|
|
7894
8235
|
data: image,
|
|
7895
|
-
mediaType: (
|
|
8236
|
+
mediaType: (_a15 = detectMediaType({
|
|
7896
8237
|
data: image,
|
|
7897
8238
|
signatures: imageMediaTypeSignatures
|
|
7898
|
-
})) != null ?
|
|
8239
|
+
})) != null ? _a15 : "image/png"
|
|
7899
8240
|
});
|
|
7900
8241
|
}
|
|
7901
8242
|
)
|
|
@@ -7906,10 +8247,26 @@ async function generateImage({
|
|
|
7906
8247
|
}
|
|
7907
8248
|
if (result.providerMetadata) {
|
|
7908
8249
|
for (const [providerName, metadata] of Object.entries(result.providerMetadata)) {
|
|
7909
|
-
(
|
|
7910
|
-
|
|
7911
|
-
|
|
7912
|
-
|
|
8250
|
+
if (providerName === "gateway") {
|
|
8251
|
+
const currentEntry = providerMetadata[providerName];
|
|
8252
|
+
if (currentEntry != null && typeof currentEntry === "object") {
|
|
8253
|
+
providerMetadata[providerName] = {
|
|
8254
|
+
...currentEntry,
|
|
8255
|
+
...metadata
|
|
8256
|
+
};
|
|
8257
|
+
} else {
|
|
8258
|
+
providerMetadata[providerName] = metadata;
|
|
8259
|
+
}
|
|
8260
|
+
const imagesValue = providerMetadata[providerName].images;
|
|
8261
|
+
if (Array.isArray(imagesValue) && imagesValue.length === 0) {
|
|
8262
|
+
delete providerMetadata[providerName].images;
|
|
8263
|
+
}
|
|
8264
|
+
} else {
|
|
8265
|
+
(_b = providerMetadata[providerName]) != null ? _b : providerMetadata[providerName] = { images: [] };
|
|
8266
|
+
providerMetadata[providerName].images.push(
|
|
8267
|
+
...result.providerMetadata[providerName].images
|
|
8268
|
+
);
|
|
8269
|
+
}
|
|
7913
8270
|
}
|
|
7914
8271
|
}
|
|
7915
8272
|
responses.push(result.response);
|
|
@@ -7947,6 +8304,50 @@ async function invokeModelMaxImagesPerCall(model) {
|
|
|
7947
8304
|
modelId: model.modelId
|
|
7948
8305
|
});
|
|
7949
8306
|
}
|
|
8307
|
+
function normalizePrompt(prompt) {
|
|
8308
|
+
if (typeof prompt === "string") {
|
|
8309
|
+
return { prompt, files: void 0, mask: void 0 };
|
|
8310
|
+
}
|
|
8311
|
+
return {
|
|
8312
|
+
prompt: prompt.text,
|
|
8313
|
+
files: prompt.images.map(toImageModelV3File),
|
|
8314
|
+
mask: prompt.mask ? toImageModelV3File(prompt.mask) : void 0
|
|
8315
|
+
};
|
|
8316
|
+
}
|
|
8317
|
+
function toImageModelV3File(dataContent) {
|
|
8318
|
+
if (typeof dataContent === "string" && dataContent.startsWith("http")) {
|
|
8319
|
+
return {
|
|
8320
|
+
type: "url",
|
|
8321
|
+
url: dataContent
|
|
8322
|
+
};
|
|
8323
|
+
}
|
|
8324
|
+
if (typeof dataContent === "string" && dataContent.startsWith("data:")) {
|
|
8325
|
+
const { mediaType: dataUrlMediaType, base64Content } = splitDataUrl(dataContent);
|
|
8326
|
+
if (base64Content != null) {
|
|
8327
|
+
const uint8Data2 = convertBase64ToUint8Array4(base64Content);
|
|
8328
|
+
return {
|
|
8329
|
+
type: "file",
|
|
8330
|
+
data: uint8Data2,
|
|
8331
|
+
mediaType: dataUrlMediaType || detectMediaType({
|
|
8332
|
+
data: uint8Data2,
|
|
8333
|
+
signatures: imageMediaTypeSignatures
|
|
8334
|
+
}) || "image/png"
|
|
8335
|
+
};
|
|
8336
|
+
}
|
|
8337
|
+
}
|
|
8338
|
+
const uint8Data = convertDataContentToUint8Array(dataContent);
|
|
8339
|
+
return {
|
|
8340
|
+
type: "file",
|
|
8341
|
+
data: uint8Data,
|
|
8342
|
+
mediaType: detectMediaType({
|
|
8343
|
+
data: uint8Data,
|
|
8344
|
+
signatures: imageMediaTypeSignatures
|
|
8345
|
+
}) || "image/png"
|
|
8346
|
+
};
|
|
8347
|
+
}
|
|
8348
|
+
|
|
8349
|
+
// src/generate-image/index.ts
|
|
8350
|
+
var experimental_generateImage = generateImage;
|
|
7950
8351
|
|
|
7951
8352
|
// src/generate-object/generate-object.ts
|
|
7952
8353
|
import {
|
|
@@ -8043,7 +8444,7 @@ var arrayOutputStrategy = (schema) => {
|
|
|
8043
8444
|
isFirstDelta,
|
|
8044
8445
|
isFinalDelta
|
|
8045
8446
|
}) {
|
|
8046
|
-
var
|
|
8447
|
+
var _a14;
|
|
8047
8448
|
if (!isJSONObject(value) || !isJSONArray(value.elements)) {
|
|
8048
8449
|
return {
|
|
8049
8450
|
success: false,
|
|
@@ -8066,7 +8467,7 @@ var arrayOutputStrategy = (schema) => {
|
|
|
8066
8467
|
}
|
|
8067
8468
|
resultArray.push(result.value);
|
|
8068
8469
|
}
|
|
8069
|
-
const publishedElementCount = (
|
|
8470
|
+
const publishedElementCount = (_a14 = latestObject == null ? void 0 : latestObject.length) != null ? _a14 : 0;
|
|
8070
8471
|
let textDelta = "";
|
|
8071
8472
|
if (isFirstDelta) {
|
|
8072
8473
|
textDelta += "[";
|
|
@@ -8486,7 +8887,7 @@ async function generateObject(options) {
|
|
|
8486
8887
|
}),
|
|
8487
8888
|
tracer,
|
|
8488
8889
|
fn: async (span) => {
|
|
8489
|
-
var
|
|
8890
|
+
var _a14;
|
|
8490
8891
|
let result;
|
|
8491
8892
|
let finishReason;
|
|
8492
8893
|
let usage;
|
|
@@ -8532,7 +8933,7 @@ async function generateObject(options) {
|
|
|
8532
8933
|
}),
|
|
8533
8934
|
tracer,
|
|
8534
8935
|
fn: async (span2) => {
|
|
8535
|
-
var
|
|
8936
|
+
var _a15, _b, _c, _d, _e, _f, _g, _h;
|
|
8536
8937
|
const result2 = await model.doGenerate({
|
|
8537
8938
|
responseFormat: {
|
|
8538
8939
|
type: "json",
|
|
@@ -8547,7 +8948,7 @@ async function generateObject(options) {
|
|
|
8547
8948
|
headers: headersWithUserAgent
|
|
8548
8949
|
});
|
|
8549
8950
|
const responseData = {
|
|
8550
|
-
id: (_b = (
|
|
8951
|
+
id: (_b = (_a15 = result2.response) == null ? void 0 : _a15.id) != null ? _b : generateId2(),
|
|
8551
8952
|
timestamp: (_d = (_c = result2.response) == null ? void 0 : _c.timestamp) != null ? _d : currentDate(),
|
|
8552
8953
|
modelId: (_f = (_e = result2.response) == null ? void 0 : _e.modelId) != null ? _f : model.modelId,
|
|
8553
8954
|
headers: (_g = result2.response) == null ? void 0 : _g.headers,
|
|
@@ -8559,15 +8960,15 @@ async function generateObject(options) {
|
|
|
8559
8960
|
throw new NoObjectGeneratedError({
|
|
8560
8961
|
message: "No object generated: the model did not return a response.",
|
|
8561
8962
|
response: responseData,
|
|
8562
|
-
usage: result2.usage,
|
|
8563
|
-
finishReason: result2.finishReason
|
|
8963
|
+
usage: asLanguageModelUsage(result2.usage),
|
|
8964
|
+
finishReason: result2.finishReason.unified
|
|
8564
8965
|
});
|
|
8565
8966
|
}
|
|
8566
8967
|
span2.setAttributes(
|
|
8567
8968
|
await selectTelemetryAttributes({
|
|
8568
8969
|
telemetry,
|
|
8569
8970
|
attributes: {
|
|
8570
|
-
"ai.response.finishReason": result2.finishReason,
|
|
8971
|
+
"ai.response.finishReason": result2.finishReason.unified,
|
|
8571
8972
|
"ai.response.object": { output: () => text2 },
|
|
8572
8973
|
"ai.response.id": responseData.id,
|
|
8573
8974
|
"ai.response.model": responseData.modelId,
|
|
@@ -8576,14 +8977,16 @@ async function generateObject(options) {
|
|
|
8576
8977
|
result2.providerMetadata
|
|
8577
8978
|
),
|
|
8578
8979
|
// TODO rename telemetry attributes to inputTokens and outputTokens
|
|
8579
|
-
"ai.usage.promptTokens": result2.usage.inputTokens,
|
|
8580
|
-
"ai.usage.completionTokens": result2.usage.outputTokens,
|
|
8980
|
+
"ai.usage.promptTokens": result2.usage.inputTokens.total,
|
|
8981
|
+
"ai.usage.completionTokens": result2.usage.outputTokens.total,
|
|
8581
8982
|
// standardized gen-ai llm span attributes:
|
|
8582
|
-
"gen_ai.response.finish_reasons": [
|
|
8983
|
+
"gen_ai.response.finish_reasons": [
|
|
8984
|
+
result2.finishReason.unified
|
|
8985
|
+
],
|
|
8583
8986
|
"gen_ai.response.id": responseData.id,
|
|
8584
8987
|
"gen_ai.response.model": responseData.modelId,
|
|
8585
|
-
"gen_ai.usage.input_tokens": result2.usage.inputTokens,
|
|
8586
|
-
"gen_ai.usage.output_tokens": result2.usage.outputTokens
|
|
8988
|
+
"gen_ai.usage.input_tokens": result2.usage.inputTokens.total,
|
|
8989
|
+
"gen_ai.usage.output_tokens": result2.usage.outputTokens.total
|
|
8587
8990
|
}
|
|
8588
8991
|
})
|
|
8589
8992
|
);
|
|
@@ -8597,11 +9000,11 @@ async function generateObject(options) {
|
|
|
8597
9000
|
})
|
|
8598
9001
|
);
|
|
8599
9002
|
result = generateResult.objectText;
|
|
8600
|
-
finishReason = generateResult.finishReason;
|
|
8601
|
-
usage = generateResult.usage;
|
|
9003
|
+
finishReason = generateResult.finishReason.unified;
|
|
9004
|
+
usage = asLanguageModelUsage(generateResult.usage);
|
|
8602
9005
|
warnings = generateResult.warnings;
|
|
8603
9006
|
resultProviderMetadata = generateResult.providerMetadata;
|
|
8604
|
-
request = (
|
|
9007
|
+
request = (_a14 = generateResult.request) != null ? _a14 : {};
|
|
8605
9008
|
response = generateResult.responseData;
|
|
8606
9009
|
reasoning = generateResult.reasoning;
|
|
8607
9010
|
logWarnings({
|
|
@@ -8664,9 +9067,9 @@ var DefaultGenerateObjectResult = class {
|
|
|
8664
9067
|
this.reasoning = options.reasoning;
|
|
8665
9068
|
}
|
|
8666
9069
|
toJsonResponse(init) {
|
|
8667
|
-
var
|
|
9070
|
+
var _a14;
|
|
8668
9071
|
return new Response(JSON.stringify(this.object), {
|
|
8669
|
-
status: (
|
|
9072
|
+
status: (_a14 = init == null ? void 0 : init.status) != null ? _a14 : 200,
|
|
8670
9073
|
headers: prepareHeaders(init == null ? void 0 : init.headers, {
|
|
8671
9074
|
"content-type": "application/json; charset=utf-8"
|
|
8672
9075
|
})
|
|
@@ -8676,7 +9079,8 @@ var DefaultGenerateObjectResult = class {
|
|
|
8676
9079
|
|
|
8677
9080
|
// src/generate-object/stream-object.ts
|
|
8678
9081
|
import {
|
|
8679
|
-
createIdGenerator as createIdGenerator4
|
|
9082
|
+
createIdGenerator as createIdGenerator4,
|
|
9083
|
+
DelayedPromise as DelayedPromise2
|
|
8680
9084
|
} from "@ai-sdk/provider-utils";
|
|
8681
9085
|
|
|
8682
9086
|
// src/util/cosine-similarity.ts
|
|
@@ -8794,8 +9198,8 @@ function simulateReadableStream({
|
|
|
8794
9198
|
chunkDelayInMs = 0,
|
|
8795
9199
|
_internal
|
|
8796
9200
|
}) {
|
|
8797
|
-
var
|
|
8798
|
-
const delay2 = (
|
|
9201
|
+
var _a14;
|
|
9202
|
+
const delay2 = (_a14 = _internal == null ? void 0 : _internal.delay) != null ? _a14 : delayFunction;
|
|
8799
9203
|
let index = 0;
|
|
8800
9204
|
return new ReadableStream({
|
|
8801
9205
|
async pull(controller) {
|
|
@@ -8900,13 +9304,13 @@ var DefaultStreamObjectResult = class {
|
|
|
8900
9304
|
currentDate,
|
|
8901
9305
|
now: now2
|
|
8902
9306
|
}) {
|
|
8903
|
-
this._object = new
|
|
8904
|
-
this._usage = new
|
|
8905
|
-
this._providerMetadata = new
|
|
8906
|
-
this._warnings = new
|
|
8907
|
-
this._request = new
|
|
8908
|
-
this._response = new
|
|
8909
|
-
this._finishReason = new
|
|
9307
|
+
this._object = new DelayedPromise2();
|
|
9308
|
+
this._usage = new DelayedPromise2();
|
|
9309
|
+
this._providerMetadata = new DelayedPromise2();
|
|
9310
|
+
this._warnings = new DelayedPromise2();
|
|
9311
|
+
this._request = new DelayedPromise2();
|
|
9312
|
+
this._response = new DelayedPromise2();
|
|
9313
|
+
this._finishReason = new DelayedPromise2();
|
|
8910
9314
|
const model = resolveLanguageModel(modelArg);
|
|
8911
9315
|
const { maxRetries, retry } = prepareRetries({
|
|
8912
9316
|
maxRetries: maxRetriesArg,
|
|
@@ -9034,11 +9438,7 @@ var DefaultStreamObjectResult = class {
|
|
|
9034
9438
|
);
|
|
9035
9439
|
self._request.resolve(request != null ? request : {});
|
|
9036
9440
|
let warnings;
|
|
9037
|
-
let usage =
|
|
9038
|
-
inputTokens: void 0,
|
|
9039
|
-
outputTokens: void 0,
|
|
9040
|
-
totalTokens: void 0
|
|
9041
|
-
};
|
|
9441
|
+
let usage = createNullLanguageModelUsage();
|
|
9042
9442
|
let finishReason;
|
|
9043
9443
|
let providerMetadata;
|
|
9044
9444
|
let object2;
|
|
@@ -9057,7 +9457,7 @@ var DefaultStreamObjectResult = class {
|
|
|
9057
9457
|
const transformedStream = stream.pipeThrough(new TransformStream(transformer)).pipeThrough(
|
|
9058
9458
|
new TransformStream({
|
|
9059
9459
|
async transform(chunk, controller) {
|
|
9060
|
-
var
|
|
9460
|
+
var _a14, _b, _c;
|
|
9061
9461
|
if (typeof chunk === "object" && chunk.type === "stream-start") {
|
|
9062
9462
|
warnings = chunk.warnings;
|
|
9063
9463
|
return;
|
|
@@ -9107,7 +9507,7 @@ var DefaultStreamObjectResult = class {
|
|
|
9107
9507
|
switch (chunk.type) {
|
|
9108
9508
|
case "response-metadata": {
|
|
9109
9509
|
fullResponse = {
|
|
9110
|
-
id: (
|
|
9510
|
+
id: (_a14 = chunk.id) != null ? _a14 : fullResponse.id,
|
|
9111
9511
|
timestamp: (_b = chunk.timestamp) != null ? _b : fullResponse.timestamp,
|
|
9112
9512
|
modelId: (_c = chunk.modelId) != null ? _c : fullResponse.modelId
|
|
9113
9513
|
};
|
|
@@ -9117,11 +9517,12 @@ var DefaultStreamObjectResult = class {
|
|
|
9117
9517
|
if (textDelta !== "") {
|
|
9118
9518
|
controller.enqueue({ type: "text-delta", textDelta });
|
|
9119
9519
|
}
|
|
9120
|
-
finishReason = chunk.finishReason;
|
|
9121
|
-
usage = chunk.usage;
|
|
9520
|
+
finishReason = chunk.finishReason.unified;
|
|
9521
|
+
usage = asLanguageModelUsage(chunk.usage);
|
|
9122
9522
|
providerMetadata = chunk.providerMetadata;
|
|
9123
9523
|
controller.enqueue({
|
|
9124
9524
|
...chunk,
|
|
9525
|
+
finishReason: chunk.finishReason.unified,
|
|
9125
9526
|
usage,
|
|
9126
9527
|
response: fullResponse
|
|
9127
9528
|
});
|
|
@@ -9137,7 +9538,7 @@ var DefaultStreamObjectResult = class {
|
|
|
9137
9538
|
...fullResponse,
|
|
9138
9539
|
headers: response == null ? void 0 : response.headers
|
|
9139
9540
|
});
|
|
9140
|
-
self._finishReason.resolve(finishReason != null ? finishReason : "
|
|
9541
|
+
self._finishReason.resolve(finishReason != null ? finishReason : "other");
|
|
9141
9542
|
try {
|
|
9142
9543
|
object2 = await parseAndValidateObjectResultWithRepair(
|
|
9143
9544
|
accumulatedText,
|
|
@@ -9378,7 +9779,7 @@ async function generateSpeech({
|
|
|
9378
9779
|
abortSignal,
|
|
9379
9780
|
headers
|
|
9380
9781
|
}) {
|
|
9381
|
-
var
|
|
9782
|
+
var _a14;
|
|
9382
9783
|
const resolvedModel = resolveSpeechModel(model);
|
|
9383
9784
|
if (!resolvedModel) {
|
|
9384
9785
|
throw new Error("Model could not be resolved");
|
|
@@ -9415,10 +9816,10 @@ async function generateSpeech({
|
|
|
9415
9816
|
return new DefaultSpeechResult({
|
|
9416
9817
|
audio: new DefaultGeneratedAudioFile({
|
|
9417
9818
|
data: result.audio,
|
|
9418
|
-
mediaType: (
|
|
9819
|
+
mediaType: (_a14 = detectMediaType({
|
|
9419
9820
|
data: result.audio,
|
|
9420
9821
|
signatures: audioMediaTypeSignatures
|
|
9421
|
-
})) != null ?
|
|
9822
|
+
})) != null ? _a14 : "audio/mp3"
|
|
9422
9823
|
}),
|
|
9423
9824
|
warnings: result.warnings,
|
|
9424
9825
|
responses: [result.response],
|
|
@@ -9427,11 +9828,11 @@ async function generateSpeech({
|
|
|
9427
9828
|
}
|
|
9428
9829
|
var DefaultSpeechResult = class {
|
|
9429
9830
|
constructor(options) {
|
|
9430
|
-
var
|
|
9831
|
+
var _a14;
|
|
9431
9832
|
this.audio = options.audio;
|
|
9432
9833
|
this.warnings = options.warnings;
|
|
9433
9834
|
this.responses = options.responses;
|
|
9434
|
-
this.providerMetadata = (
|
|
9835
|
+
this.providerMetadata = (_a14 = options.providerMetadata) != null ? _a14 : {};
|
|
9435
9836
|
}
|
|
9436
9837
|
};
|
|
9437
9838
|
|
|
@@ -9469,7 +9870,7 @@ function pruneMessages({
|
|
|
9469
9870
|
const keptToolCallIds = /* @__PURE__ */ new Set();
|
|
9470
9871
|
const keptApprovalIds = /* @__PURE__ */ new Set();
|
|
9471
9872
|
if (keepLastMessagesCount != null) {
|
|
9472
|
-
for (const message of messages.slice(
|
|
9873
|
+
for (const message of messages.slice(-keepLastMessagesCount)) {
|
|
9473
9874
|
if ((message.role === "assistant" || message.role === "tool") && typeof message.content !== "string") {
|
|
9474
9875
|
for (const part of message.content) {
|
|
9475
9876
|
if (part.type === "tool-call" || part.type === "tool-result") {
|
|
@@ -9596,10 +9997,7 @@ function defaultEmbeddingSettingsMiddleware({
|
|
|
9596
9997
|
return {
|
|
9597
9998
|
specificationVersion: "v3",
|
|
9598
9999
|
transformParams: async ({ params }) => {
|
|
9599
|
-
return mergeObjects(
|
|
9600
|
-
settings,
|
|
9601
|
-
params
|
|
9602
|
-
);
|
|
10000
|
+
return mergeObjects(settings, params);
|
|
9603
10001
|
}
|
|
9604
10002
|
};
|
|
9605
10003
|
}
|
|
@@ -9854,6 +10252,47 @@ function simulateStreamingMiddleware() {
|
|
|
9854
10252
|
};
|
|
9855
10253
|
}
|
|
9856
10254
|
|
|
10255
|
+
// src/middleware/add-tool-input-examples-middleware.ts
|
|
10256
|
+
function defaultFormatExample(example) {
|
|
10257
|
+
return JSON.stringify(example.input);
|
|
10258
|
+
}
|
|
10259
|
+
function addToolInputExamplesMiddleware({
|
|
10260
|
+
prefix = "Input Examples:",
|
|
10261
|
+
format = defaultFormatExample,
|
|
10262
|
+
remove = true
|
|
10263
|
+
} = {}) {
|
|
10264
|
+
return {
|
|
10265
|
+
specificationVersion: "v3",
|
|
10266
|
+
transformParams: async ({ params }) => {
|
|
10267
|
+
var _a14;
|
|
10268
|
+
if (!((_a14 = params.tools) == null ? void 0 : _a14.length)) {
|
|
10269
|
+
return params;
|
|
10270
|
+
}
|
|
10271
|
+
const transformedTools = params.tools.map((tool2) => {
|
|
10272
|
+
var _a15;
|
|
10273
|
+
if (tool2.type !== "function" || !((_a15 = tool2.inputExamples) == null ? void 0 : _a15.length)) {
|
|
10274
|
+
return tool2;
|
|
10275
|
+
}
|
|
10276
|
+
const formattedExamples = tool2.inputExamples.map((example, index) => format(example, index)).join("\n");
|
|
10277
|
+
const examplesSection = `${prefix}
|
|
10278
|
+
${formattedExamples}`;
|
|
10279
|
+
const toolDescription = tool2.description ? `${tool2.description}
|
|
10280
|
+
|
|
10281
|
+
${examplesSection}` : examplesSection;
|
|
10282
|
+
return {
|
|
10283
|
+
...tool2,
|
|
10284
|
+
description: toolDescription,
|
|
10285
|
+
inputExamples: remove ? void 0 : tool2.inputExamples
|
|
10286
|
+
};
|
|
10287
|
+
});
|
|
10288
|
+
return {
|
|
10289
|
+
...params,
|
|
10290
|
+
tools: transformedTools
|
|
10291
|
+
};
|
|
10292
|
+
}
|
|
10293
|
+
};
|
|
10294
|
+
}
|
|
10295
|
+
|
|
9857
10296
|
// src/middleware/wrap-language-model.ts
|
|
9858
10297
|
var wrapLanguageModel = ({
|
|
9859
10298
|
model,
|
|
@@ -9878,7 +10317,7 @@ var doWrap = ({
|
|
|
9878
10317
|
modelId,
|
|
9879
10318
|
providerId
|
|
9880
10319
|
}) => {
|
|
9881
|
-
var
|
|
10320
|
+
var _a14, _b, _c;
|
|
9882
10321
|
async function doTransform({
|
|
9883
10322
|
params,
|
|
9884
10323
|
type
|
|
@@ -9887,7 +10326,7 @@ var doWrap = ({
|
|
|
9887
10326
|
}
|
|
9888
10327
|
return {
|
|
9889
10328
|
specificationVersion: "v3",
|
|
9890
|
-
provider: (
|
|
10329
|
+
provider: (_a14 = providerId != null ? providerId : overrideProvider == null ? void 0 : overrideProvider({ model })) != null ? _a14 : model.provider,
|
|
9891
10330
|
modelId: (_b = modelId != null ? modelId : overrideModelId == null ? void 0 : overrideModelId({ model })) != null ? _b : model.modelId,
|
|
9892
10331
|
supportedUrls: (_c = overrideSupportedUrls == null ? void 0 : overrideSupportedUrls({ model })) != null ? _c : model.supportedUrls,
|
|
9893
10332
|
async doGenerate(params) {
|
|
@@ -9934,7 +10373,7 @@ var doWrap2 = ({
|
|
|
9934
10373
|
modelId,
|
|
9935
10374
|
providerId
|
|
9936
10375
|
}) => {
|
|
9937
|
-
var
|
|
10376
|
+
var _a14, _b, _c, _d;
|
|
9938
10377
|
async function doTransform({
|
|
9939
10378
|
params
|
|
9940
10379
|
}) {
|
|
@@ -9942,7 +10381,7 @@ var doWrap2 = ({
|
|
|
9942
10381
|
}
|
|
9943
10382
|
return {
|
|
9944
10383
|
specificationVersion: "v3",
|
|
9945
|
-
provider: (
|
|
10384
|
+
provider: (_a14 = providerId != null ? providerId : overrideProvider == null ? void 0 : overrideProvider({ model })) != null ? _a14 : model.provider,
|
|
9946
10385
|
modelId: (_b = modelId != null ? modelId : overrideModelId == null ? void 0 : overrideModelId({ model })) != null ? _b : model.modelId,
|
|
9947
10386
|
maxEmbeddingsPerCall: (_c = overrideMaxEmbeddingsPerCall == null ? void 0 : overrideMaxEmbeddingsPerCall({ model })) != null ? _c : model.maxEmbeddingsPerCall,
|
|
9948
10387
|
supportsParallelCalls: (_d = overrideSupportsParallelCalls == null ? void 0 : overrideSupportsParallelCalls({ model })) != null ? _d : model.supportsParallelCalls,
|
|
@@ -9958,32 +10397,19 @@ var doWrap2 = ({
|
|
|
9958
10397
|
};
|
|
9959
10398
|
};
|
|
9960
10399
|
|
|
9961
|
-
// src/model/as-image-model-v3.ts
|
|
9962
|
-
function asImageModelV3(model) {
|
|
9963
|
-
if (model.specificationVersion === "v3") {
|
|
9964
|
-
return model;
|
|
9965
|
-
}
|
|
9966
|
-
return new Proxy(model, {
|
|
9967
|
-
get(target, prop) {
|
|
9968
|
-
if (prop === "specificationVersion")
|
|
9969
|
-
return "v3";
|
|
9970
|
-
return target[prop];
|
|
9971
|
-
}
|
|
9972
|
-
});
|
|
9973
|
-
}
|
|
9974
|
-
|
|
9975
10400
|
// src/model/as-provider-v3.ts
|
|
9976
10401
|
function asProviderV3(provider) {
|
|
9977
10402
|
if ("specificationVersion" in provider && provider.specificationVersion === "v3") {
|
|
9978
10403
|
return provider;
|
|
9979
10404
|
}
|
|
10405
|
+
const v2Provider = provider;
|
|
9980
10406
|
return {
|
|
9981
10407
|
specificationVersion: "v3",
|
|
9982
|
-
languageModel: (modelId) => asLanguageModelV3(
|
|
9983
|
-
|
|
9984
|
-
imageModel: (modelId) => asImageModelV3(
|
|
9985
|
-
transcriptionModel:
|
|
9986
|
-
speechModel:
|
|
10408
|
+
languageModel: (modelId) => asLanguageModelV3(v2Provider.languageModel(modelId)),
|
|
10409
|
+
embeddingModel: (modelId) => asEmbeddingModelV3(v2Provider.textEmbeddingModel(modelId)),
|
|
10410
|
+
imageModel: (modelId) => asImageModelV3(v2Provider.imageModel(modelId)),
|
|
10411
|
+
transcriptionModel: v2Provider.transcriptionModel ? (modelId) => asTranscriptionModelV3(v2Provider.transcriptionModel(modelId)) : void 0,
|
|
10412
|
+
speechModel: v2Provider.speechModel ? (modelId) => asSpeechModelV3(v2Provider.speechModel(modelId)) : void 0,
|
|
9987
10413
|
rerankingModel: void 0
|
|
9988
10414
|
// v2 providers don't have reranking models
|
|
9989
10415
|
};
|
|
@@ -10001,7 +10427,7 @@ function wrapProvider({
|
|
|
10001
10427
|
model: providerV3.languageModel(modelId),
|
|
10002
10428
|
middleware: languageModelMiddleware
|
|
10003
10429
|
}),
|
|
10004
|
-
|
|
10430
|
+
embeddingModel: providerV3.embeddingModel,
|
|
10005
10431
|
imageModel: providerV3.imageModel,
|
|
10006
10432
|
transcriptionModel: providerV3.transcriptionModel,
|
|
10007
10433
|
speechModel: providerV3.speechModel,
|
|
@@ -10015,7 +10441,7 @@ import {
|
|
|
10015
10441
|
} from "@ai-sdk/provider";
|
|
10016
10442
|
function customProvider({
|
|
10017
10443
|
languageModels,
|
|
10018
|
-
|
|
10444
|
+
embeddingModels,
|
|
10019
10445
|
imageModels,
|
|
10020
10446
|
transcriptionModels,
|
|
10021
10447
|
speechModels,
|
|
@@ -10034,14 +10460,14 @@ function customProvider({
|
|
|
10034
10460
|
}
|
|
10035
10461
|
throw new NoSuchModelError2({ modelId, modelType: "languageModel" });
|
|
10036
10462
|
},
|
|
10037
|
-
|
|
10038
|
-
if (
|
|
10039
|
-
return
|
|
10463
|
+
embeddingModel(modelId) {
|
|
10464
|
+
if (embeddingModels != null && modelId in embeddingModels) {
|
|
10465
|
+
return embeddingModels[modelId];
|
|
10040
10466
|
}
|
|
10041
10467
|
if (fallbackProvider) {
|
|
10042
|
-
return fallbackProvider.
|
|
10468
|
+
return fallbackProvider.embeddingModel(modelId);
|
|
10043
10469
|
}
|
|
10044
|
-
throw new NoSuchModelError2({ modelId, modelType: "
|
|
10470
|
+
throw new NoSuchModelError2({ modelId, modelType: "embeddingModel" });
|
|
10045
10471
|
},
|
|
10046
10472
|
imageModel(modelId) {
|
|
10047
10473
|
if (imageModels != null && modelId in imageModels) {
|
|
@@ -10084,11 +10510,11 @@ function customProvider({
|
|
|
10084
10510
|
var experimental_customProvider = customProvider;
|
|
10085
10511
|
|
|
10086
10512
|
// src/registry/no-such-provider-error.ts
|
|
10087
|
-
import { AISDKError as
|
|
10088
|
-
var
|
|
10089
|
-
var
|
|
10090
|
-
var
|
|
10091
|
-
var
|
|
10513
|
+
import { AISDKError as AISDKError18, NoSuchModelError as NoSuchModelError3 } from "@ai-sdk/provider";
|
|
10514
|
+
var name13 = "AI_NoSuchProviderError";
|
|
10515
|
+
var marker13 = `vercel.ai.error.${name13}`;
|
|
10516
|
+
var symbol13 = Symbol.for(marker13);
|
|
10517
|
+
var _a13;
|
|
10092
10518
|
var NoSuchProviderError = class extends NoSuchModelError3 {
|
|
10093
10519
|
constructor({
|
|
10094
10520
|
modelId,
|
|
@@ -10097,16 +10523,16 @@ var NoSuchProviderError = class extends NoSuchModelError3 {
|
|
|
10097
10523
|
availableProviders,
|
|
10098
10524
|
message = `No such provider: ${providerId} (available providers: ${availableProviders.join()})`
|
|
10099
10525
|
}) {
|
|
10100
|
-
super({ errorName:
|
|
10101
|
-
this[
|
|
10526
|
+
super({ errorName: name13, modelId, modelType, message });
|
|
10527
|
+
this[_a13] = true;
|
|
10102
10528
|
this.providerId = providerId;
|
|
10103
10529
|
this.availableProviders = availableProviders;
|
|
10104
10530
|
}
|
|
10105
10531
|
static isInstance(error) {
|
|
10106
|
-
return
|
|
10532
|
+
return AISDKError18.hasMarker(error, marker13);
|
|
10107
10533
|
}
|
|
10108
10534
|
};
|
|
10109
|
-
|
|
10535
|
+
_a13 = symbol13;
|
|
10110
10536
|
|
|
10111
10537
|
// src/registry/provider-registry.ts
|
|
10112
10538
|
import {
|
|
@@ -10165,10 +10591,10 @@ var DefaultProviderRegistry = class {
|
|
|
10165
10591
|
return [id.slice(0, index), id.slice(index + this.separator.length)];
|
|
10166
10592
|
}
|
|
10167
10593
|
languageModel(id) {
|
|
10168
|
-
var
|
|
10594
|
+
var _a14, _b;
|
|
10169
10595
|
const [providerId, modelId] = this.splitId(id, "languageModel");
|
|
10170
|
-
let model = (_b = (
|
|
10171
|
-
|
|
10596
|
+
let model = (_b = (_a14 = this.getProvider(providerId, "languageModel")).languageModel) == null ? void 0 : _b.call(
|
|
10597
|
+
_a14,
|
|
10172
10598
|
modelId
|
|
10173
10599
|
);
|
|
10174
10600
|
if (model == null) {
|
|
@@ -10182,34 +10608,34 @@ var DefaultProviderRegistry = class {
|
|
|
10182
10608
|
}
|
|
10183
10609
|
return model;
|
|
10184
10610
|
}
|
|
10185
|
-
|
|
10186
|
-
var
|
|
10187
|
-
const [providerId, modelId] = this.splitId(id, "
|
|
10188
|
-
const provider = this.getProvider(providerId, "
|
|
10189
|
-
const model = (
|
|
10611
|
+
embeddingModel(id) {
|
|
10612
|
+
var _a14;
|
|
10613
|
+
const [providerId, modelId] = this.splitId(id, "embeddingModel");
|
|
10614
|
+
const provider = this.getProvider(providerId, "embeddingModel");
|
|
10615
|
+
const model = (_a14 = provider.embeddingModel) == null ? void 0 : _a14.call(provider, modelId);
|
|
10190
10616
|
if (model == null) {
|
|
10191
10617
|
throw new NoSuchModelError4({
|
|
10192
10618
|
modelId: id,
|
|
10193
|
-
modelType: "
|
|
10619
|
+
modelType: "embeddingModel"
|
|
10194
10620
|
});
|
|
10195
10621
|
}
|
|
10196
10622
|
return model;
|
|
10197
10623
|
}
|
|
10198
10624
|
imageModel(id) {
|
|
10199
|
-
var
|
|
10625
|
+
var _a14;
|
|
10200
10626
|
const [providerId, modelId] = this.splitId(id, "imageModel");
|
|
10201
10627
|
const provider = this.getProvider(providerId, "imageModel");
|
|
10202
|
-
const model = (
|
|
10628
|
+
const model = (_a14 = provider.imageModel) == null ? void 0 : _a14.call(provider, modelId);
|
|
10203
10629
|
if (model == null) {
|
|
10204
10630
|
throw new NoSuchModelError4({ modelId: id, modelType: "imageModel" });
|
|
10205
10631
|
}
|
|
10206
10632
|
return model;
|
|
10207
10633
|
}
|
|
10208
10634
|
transcriptionModel(id) {
|
|
10209
|
-
var
|
|
10635
|
+
var _a14;
|
|
10210
10636
|
const [providerId, modelId] = this.splitId(id, "transcriptionModel");
|
|
10211
10637
|
const provider = this.getProvider(providerId, "transcriptionModel");
|
|
10212
|
-
const model = (
|
|
10638
|
+
const model = (_a14 = provider.transcriptionModel) == null ? void 0 : _a14.call(provider, modelId);
|
|
10213
10639
|
if (model == null) {
|
|
10214
10640
|
throw new NoSuchModelError4({
|
|
10215
10641
|
modelId: id,
|
|
@@ -10219,20 +10645,20 @@ var DefaultProviderRegistry = class {
|
|
|
10219
10645
|
return model;
|
|
10220
10646
|
}
|
|
10221
10647
|
speechModel(id) {
|
|
10222
|
-
var
|
|
10648
|
+
var _a14;
|
|
10223
10649
|
const [providerId, modelId] = this.splitId(id, "speechModel");
|
|
10224
10650
|
const provider = this.getProvider(providerId, "speechModel");
|
|
10225
|
-
const model = (
|
|
10651
|
+
const model = (_a14 = provider.speechModel) == null ? void 0 : _a14.call(provider, modelId);
|
|
10226
10652
|
if (model == null) {
|
|
10227
10653
|
throw new NoSuchModelError4({ modelId: id, modelType: "speechModel" });
|
|
10228
10654
|
}
|
|
10229
10655
|
return model;
|
|
10230
10656
|
}
|
|
10231
10657
|
rerankingModel(id) {
|
|
10232
|
-
var
|
|
10658
|
+
var _a14;
|
|
10233
10659
|
const [providerId, modelId] = this.splitId(id, "rerankingModel");
|
|
10234
10660
|
const provider = this.getProvider(providerId, "rerankingModel");
|
|
10235
|
-
const model = (
|
|
10661
|
+
const model = (_a14 = provider.rerankingModel) == null ? void 0 : _a14.call(provider, modelId);
|
|
10236
10662
|
if (model == null) {
|
|
10237
10663
|
throw new NoSuchModelError4({ modelId: id, modelType: "rerankingModel" });
|
|
10238
10664
|
}
|
|
@@ -10289,7 +10715,7 @@ async function rerank({
|
|
|
10289
10715
|
}),
|
|
10290
10716
|
tracer,
|
|
10291
10717
|
fn: async () => {
|
|
10292
|
-
var
|
|
10718
|
+
var _a14, _b;
|
|
10293
10719
|
const { ranking, response, providerMetadata, warnings } = await retry(
|
|
10294
10720
|
() => recordSpan({
|
|
10295
10721
|
name: "ai.rerank.doRerank",
|
|
@@ -10353,7 +10779,7 @@ async function rerank({
|
|
|
10353
10779
|
providerMetadata,
|
|
10354
10780
|
response: {
|
|
10355
10781
|
id: response == null ? void 0 : response.id,
|
|
10356
|
-
timestamp: (
|
|
10782
|
+
timestamp: (_a14 = response == null ? void 0 : response.timestamp) != null ? _a14 : /* @__PURE__ */ new Date(),
|
|
10357
10783
|
modelId: (_b = response == null ? void 0 : response.modelId) != null ? _b : model.modelId,
|
|
10358
10784
|
headers: response == null ? void 0 : response.headers,
|
|
10359
10785
|
body: response == null ? void 0 : response.body
|
|
@@ -10378,8 +10804,8 @@ var DefaultRerankResult = class {
|
|
|
10378
10804
|
import { withUserAgentSuffix as withUserAgentSuffix8 } from "@ai-sdk/provider-utils";
|
|
10379
10805
|
|
|
10380
10806
|
// src/error/no-transcript-generated-error.ts
|
|
10381
|
-
import { AISDKError as
|
|
10382
|
-
var NoTranscriptGeneratedError = class extends
|
|
10807
|
+
import { AISDKError as AISDKError19 } from "@ai-sdk/provider";
|
|
10808
|
+
var NoTranscriptGeneratedError = class extends AISDKError19 {
|
|
10383
10809
|
constructor(options) {
|
|
10384
10810
|
super({
|
|
10385
10811
|
name: "AI_NoTranscriptGeneratedError",
|
|
@@ -10413,16 +10839,16 @@ async function transcribe({
|
|
|
10413
10839
|
const audioData = audio instanceof URL ? (await download({ url: audio })).data : convertDataContentToUint8Array(audio);
|
|
10414
10840
|
const result = await retry(
|
|
10415
10841
|
() => {
|
|
10416
|
-
var
|
|
10842
|
+
var _a14;
|
|
10417
10843
|
return resolvedModel.doGenerate({
|
|
10418
10844
|
audio: audioData,
|
|
10419
10845
|
abortSignal,
|
|
10420
10846
|
headers: headersWithUserAgent,
|
|
10421
10847
|
providerOptions,
|
|
10422
|
-
mediaType: (
|
|
10848
|
+
mediaType: (_a14 = detectMediaType({
|
|
10423
10849
|
data: audioData,
|
|
10424
10850
|
signatures: audioMediaTypeSignatures
|
|
10425
|
-
})) != null ?
|
|
10851
|
+
})) != null ? _a14 : "audio/wav"
|
|
10426
10852
|
});
|
|
10427
10853
|
}
|
|
10428
10854
|
);
|
|
@@ -10446,14 +10872,14 @@ async function transcribe({
|
|
|
10446
10872
|
}
|
|
10447
10873
|
var DefaultTranscriptionResult = class {
|
|
10448
10874
|
constructor(options) {
|
|
10449
|
-
var
|
|
10875
|
+
var _a14;
|
|
10450
10876
|
this.text = options.text;
|
|
10451
10877
|
this.segments = options.segments;
|
|
10452
10878
|
this.language = options.language;
|
|
10453
10879
|
this.durationInSeconds = options.durationInSeconds;
|
|
10454
10880
|
this.warnings = options.warnings;
|
|
10455
10881
|
this.responses = options.responses;
|
|
10456
|
-
this.providerMetadata = (
|
|
10882
|
+
this.providerMetadata = (_a14 = options.providerMetadata) != null ? _a14 : {};
|
|
10457
10883
|
}
|
|
10458
10884
|
};
|
|
10459
10885
|
|
|
@@ -10496,7 +10922,7 @@ async function callCompletionApi({
|
|
|
10496
10922
|
onError,
|
|
10497
10923
|
fetch: fetch2 = getOriginalFetch()
|
|
10498
10924
|
}) {
|
|
10499
|
-
var
|
|
10925
|
+
var _a14;
|
|
10500
10926
|
try {
|
|
10501
10927
|
setLoading(true);
|
|
10502
10928
|
setError(void 0);
|
|
@@ -10524,7 +10950,7 @@ async function callCompletionApi({
|
|
|
10524
10950
|
});
|
|
10525
10951
|
if (!response.ok) {
|
|
10526
10952
|
throw new Error(
|
|
10527
|
-
(
|
|
10953
|
+
(_a14 = await response.text()) != null ? _a14 : "Failed to fetch the chat response."
|
|
10528
10954
|
);
|
|
10529
10955
|
}
|
|
10530
10956
|
if (!response.body) {
|
|
@@ -10610,12 +11036,12 @@ async function convertFileListToFileUIParts(files) {
|
|
|
10610
11036
|
}
|
|
10611
11037
|
return Promise.all(
|
|
10612
11038
|
Array.from(files).map(async (file) => {
|
|
10613
|
-
const { name:
|
|
11039
|
+
const { name: name14, type } = file;
|
|
10614
11040
|
const dataUrl = await new Promise((resolve3, reject) => {
|
|
10615
11041
|
const reader = new FileReader();
|
|
10616
11042
|
reader.onload = (readerEvent) => {
|
|
10617
|
-
var
|
|
10618
|
-
resolve3((
|
|
11043
|
+
var _a14;
|
|
11044
|
+
resolve3((_a14 = readerEvent.target) == null ? void 0 : _a14.result);
|
|
10619
11045
|
};
|
|
10620
11046
|
reader.onerror = (error) => reject(error);
|
|
10621
11047
|
reader.readAsDataURL(file);
|
|
@@ -10623,7 +11049,7 @@ async function convertFileListToFileUIParts(files) {
|
|
|
10623
11049
|
return {
|
|
10624
11050
|
type: "file",
|
|
10625
11051
|
mediaType: type,
|
|
10626
|
-
filename:
|
|
11052
|
+
filename: name14,
|
|
10627
11053
|
url: dataUrl
|
|
10628
11054
|
};
|
|
10629
11055
|
})
|
|
@@ -10662,7 +11088,7 @@ var HttpChatTransport = class {
|
|
|
10662
11088
|
abortSignal,
|
|
10663
11089
|
...options
|
|
10664
11090
|
}) {
|
|
10665
|
-
var
|
|
11091
|
+
var _a14, _b, _c, _d, _e;
|
|
10666
11092
|
const resolvedBody = await resolve2(this.body);
|
|
10667
11093
|
const resolvedHeaders = await resolve2(this.headers);
|
|
10668
11094
|
const resolvedCredentials = await resolve2(this.credentials);
|
|
@@ -10670,7 +11096,7 @@ var HttpChatTransport = class {
|
|
|
10670
11096
|
...normalizeHeaders(resolvedHeaders),
|
|
10671
11097
|
...normalizeHeaders(options.headers)
|
|
10672
11098
|
};
|
|
10673
|
-
const preparedRequest = await ((
|
|
11099
|
+
const preparedRequest = await ((_a14 = this.prepareSendMessagesRequest) == null ? void 0 : _a14.call(this, {
|
|
10674
11100
|
api: this.api,
|
|
10675
11101
|
id: options.chatId,
|
|
10676
11102
|
messages: options.messages,
|
|
@@ -10718,7 +11144,7 @@ var HttpChatTransport = class {
|
|
|
10718
11144
|
return this.processResponseStream(response.body);
|
|
10719
11145
|
}
|
|
10720
11146
|
async reconnectToStream(options) {
|
|
10721
|
-
var
|
|
11147
|
+
var _a14, _b, _c, _d, _e;
|
|
10722
11148
|
const resolvedBody = await resolve2(this.body);
|
|
10723
11149
|
const resolvedHeaders = await resolve2(this.headers);
|
|
10724
11150
|
const resolvedCredentials = await resolve2(this.credentials);
|
|
@@ -10726,7 +11152,7 @@ var HttpChatTransport = class {
|
|
|
10726
11152
|
...normalizeHeaders(resolvedHeaders),
|
|
10727
11153
|
...normalizeHeaders(options.headers)
|
|
10728
11154
|
};
|
|
10729
|
-
const preparedRequest = await ((
|
|
11155
|
+
const preparedRequest = await ((_a14 = this.prepareReconnectToStreamRequest) == null ? void 0 : _a14.call(this, {
|
|
10730
11156
|
api: this.api,
|
|
10731
11157
|
id: options.chatId,
|
|
10732
11158
|
body: { ...resolvedBody, ...options.body },
|
|
@@ -10808,11 +11234,11 @@ var AbstractChat = class {
|
|
|
10808
11234
|
* If a messageId is provided, the message will be replaced.
|
|
10809
11235
|
*/
|
|
10810
11236
|
this.sendMessage = async (message, options) => {
|
|
10811
|
-
var
|
|
11237
|
+
var _a14, _b, _c, _d;
|
|
10812
11238
|
if (message == null) {
|
|
10813
11239
|
await this.makeRequest({
|
|
10814
11240
|
trigger: "submit-message",
|
|
10815
|
-
messageId: (
|
|
11241
|
+
messageId: (_a14 = this.lastMessage) == null ? void 0 : _a14.id,
|
|
10816
11242
|
...options
|
|
10817
11243
|
});
|
|
10818
11244
|
return;
|
|
@@ -10905,10 +11331,10 @@ var AbstractChat = class {
|
|
|
10905
11331
|
approved,
|
|
10906
11332
|
reason
|
|
10907
11333
|
}) => this.jobExecutor.run(async () => {
|
|
10908
|
-
var
|
|
11334
|
+
var _a14, _b;
|
|
10909
11335
|
const messages = this.state.messages;
|
|
10910
11336
|
const lastMessage = messages[messages.length - 1];
|
|
10911
|
-
const updatePart = (part) =>
|
|
11337
|
+
const updatePart = (part) => isToolUIPart(part) && part.state === "approval-requested" && part.approval.id === id ? {
|
|
10912
11338
|
...part,
|
|
10913
11339
|
state: "approval-responded",
|
|
10914
11340
|
approval: { id, approved, reason }
|
|
@@ -10920,7 +11346,7 @@ var AbstractChat = class {
|
|
|
10920
11346
|
if (this.activeResponse) {
|
|
10921
11347
|
this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(updatePart);
|
|
10922
11348
|
}
|
|
10923
|
-
if (this.status !== "streaming" && this.status !== "submitted" && ((
|
|
11349
|
+
if (this.status !== "streaming" && this.status !== "submitted" && ((_a14 = this.sendAutomaticallyWhen) == null ? void 0 : _a14.call(this, { messages: this.state.messages }))) {
|
|
10924
11350
|
this.makeRequest({
|
|
10925
11351
|
trigger: "submit-message",
|
|
10926
11352
|
messageId: (_b = this.lastMessage) == null ? void 0 : _b.id
|
|
@@ -10934,10 +11360,10 @@ var AbstractChat = class {
|
|
|
10934
11360
|
output,
|
|
10935
11361
|
errorText
|
|
10936
11362
|
}) => this.jobExecutor.run(async () => {
|
|
10937
|
-
var
|
|
11363
|
+
var _a14, _b;
|
|
10938
11364
|
const messages = this.state.messages;
|
|
10939
11365
|
const lastMessage = messages[messages.length - 1];
|
|
10940
|
-
const updatePart = (part) =>
|
|
11366
|
+
const updatePart = (part) => isToolUIPart(part) && part.toolCallId === toolCallId ? { ...part, state, output, errorText } : part;
|
|
10941
11367
|
this.state.replaceMessage(messages.length - 1, {
|
|
10942
11368
|
...lastMessage,
|
|
10943
11369
|
parts: lastMessage.parts.map(updatePart)
|
|
@@ -10945,7 +11371,7 @@ var AbstractChat = class {
|
|
|
10945
11371
|
if (this.activeResponse) {
|
|
10946
11372
|
this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(updatePart);
|
|
10947
11373
|
}
|
|
10948
|
-
if (this.status !== "streaming" && this.status !== "submitted" && ((
|
|
11374
|
+
if (this.status !== "streaming" && this.status !== "submitted" && ((_a14 = this.sendAutomaticallyWhen) == null ? void 0 : _a14.call(this, { messages: this.state.messages }))) {
|
|
10949
11375
|
this.makeRequest({
|
|
10950
11376
|
trigger: "submit-message",
|
|
10951
11377
|
messageId: (_b = this.lastMessage) == null ? void 0 : _b.id
|
|
@@ -10958,10 +11384,10 @@ var AbstractChat = class {
|
|
|
10958
11384
|
* Abort the current request immediately, keep the generated tokens if any.
|
|
10959
11385
|
*/
|
|
10960
11386
|
this.stop = async () => {
|
|
10961
|
-
var
|
|
11387
|
+
var _a14;
|
|
10962
11388
|
if (this.status !== "streaming" && this.status !== "submitted")
|
|
10963
11389
|
return;
|
|
10964
|
-
if ((
|
|
11390
|
+
if ((_a14 = this.activeResponse) == null ? void 0 : _a14.abortController) {
|
|
10965
11391
|
this.activeResponse.abortController.abort();
|
|
10966
11392
|
}
|
|
10967
11393
|
};
|
|
@@ -11016,7 +11442,7 @@ var AbstractChat = class {
|
|
|
11016
11442
|
body,
|
|
11017
11443
|
messageId
|
|
11018
11444
|
}) {
|
|
11019
|
-
var
|
|
11445
|
+
var _a14, _b, _c, _d;
|
|
11020
11446
|
this.setStatus({ status: "submitted", error: void 0 });
|
|
11021
11447
|
const lastMessage = this.lastMessage;
|
|
11022
11448
|
let isAbort = false;
|
|
@@ -11065,9 +11491,9 @@ var AbstractChat = class {
|
|
|
11065
11491
|
() => job({
|
|
11066
11492
|
state: activeResponse.state,
|
|
11067
11493
|
write: () => {
|
|
11068
|
-
var
|
|
11494
|
+
var _a15;
|
|
11069
11495
|
this.setStatus({ status: "streaming" });
|
|
11070
|
-
const replaceLastMessage = activeResponse.state.message.id === ((
|
|
11496
|
+
const replaceLastMessage = activeResponse.state.message.id === ((_a15 = this.lastMessage) == null ? void 0 : _a15.id);
|
|
11071
11497
|
if (replaceLastMessage) {
|
|
11072
11498
|
this.state.replaceMessage(
|
|
11073
11499
|
this.state.messages.length - 1,
|
|
@@ -11119,7 +11545,7 @@ var AbstractChat = class {
|
|
|
11119
11545
|
isAbort,
|
|
11120
11546
|
isDisconnect,
|
|
11121
11547
|
isError,
|
|
11122
|
-
finishReason: (
|
|
11548
|
+
finishReason: (_a14 = this.activeResponse) == null ? void 0 : _a14.state.finishReason
|
|
11123
11549
|
});
|
|
11124
11550
|
} catch (err) {
|
|
11125
11551
|
console.error(err);
|
|
@@ -11152,7 +11578,7 @@ function lastAssistantMessageIsCompleteWithApprovalResponses({
|
|
|
11152
11578
|
const lastStepStartIndex = message.parts.reduce((lastIndex, part, index) => {
|
|
11153
11579
|
return part.type === "step-start" ? index : lastIndex;
|
|
11154
11580
|
}, -1);
|
|
11155
|
-
const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(
|
|
11581
|
+
const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(isToolUIPart).filter((part) => !part.providerExecuted);
|
|
11156
11582
|
return (
|
|
11157
11583
|
// has at least one tool approval response
|
|
11158
11584
|
lastStepToolInvocations.filter((part) => part.state === "approval-responded").length > 0 && // all tool approvals must have a response
|
|
@@ -11176,7 +11602,7 @@ function lastAssistantMessageIsCompleteWithToolCalls({
|
|
|
11176
11602
|
const lastStepStartIndex = message.parts.reduce((lastIndex, part, index) => {
|
|
11177
11603
|
return part.type === "step-start" ? index : lastIndex;
|
|
11178
11604
|
}, -1);
|
|
11179
|
-
const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(
|
|
11605
|
+
const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(isToolUIPart).filter((part) => !part.providerExecuted);
|
|
11180
11606
|
return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every(
|
|
11181
11607
|
(part) => part.state === "output-available" || part.state === "output-error"
|
|
11182
11608
|
);
|
|
@@ -11217,7 +11643,7 @@ var TextStreamChatTransport = class extends HttpChatTransport {
|
|
|
11217
11643
|
}
|
|
11218
11644
|
};
|
|
11219
11645
|
export {
|
|
11220
|
-
|
|
11646
|
+
AISDKError15 as AISDKError,
|
|
11221
11647
|
APICallError,
|
|
11222
11648
|
AbstractChat,
|
|
11223
11649
|
DefaultChatTransport,
|
|
@@ -11256,18 +11682,13 @@ export {
|
|
|
11256
11682
|
UI_MESSAGE_STREAM_HEADERS,
|
|
11257
11683
|
UnsupportedFunctionalityError,
|
|
11258
11684
|
UnsupportedModelVersionError,
|
|
11685
|
+
addToolInputExamplesMiddleware,
|
|
11259
11686
|
asSchema5 as asSchema,
|
|
11260
11687
|
assistantModelMessageSchema,
|
|
11261
11688
|
callCompletionApi,
|
|
11262
11689
|
consumeStream,
|
|
11263
11690
|
convertFileListToFileUIParts,
|
|
11264
|
-
convertToCoreMessages,
|
|
11265
11691
|
convertToModelMessages,
|
|
11266
|
-
coreAssistantMessageSchema,
|
|
11267
|
-
coreMessageSchema,
|
|
11268
|
-
coreSystemMessageSchema,
|
|
11269
|
-
coreToolMessageSchema,
|
|
11270
|
-
coreUserMessageSchema,
|
|
11271
11692
|
cosineSimilarity,
|
|
11272
11693
|
createAgentUIStream,
|
|
11273
11694
|
createAgentUIStreamResponse,
|
|
@@ -11285,14 +11706,16 @@ export {
|
|
|
11285
11706
|
embedMany,
|
|
11286
11707
|
experimental_createProviderRegistry,
|
|
11287
11708
|
experimental_customProvider,
|
|
11288
|
-
|
|
11709
|
+
experimental_generateImage,
|
|
11289
11710
|
generateSpeech as experimental_generateSpeech,
|
|
11290
11711
|
transcribe as experimental_transcribe,
|
|
11291
11712
|
extractReasoningMiddleware,
|
|
11292
11713
|
gateway2 as gateway,
|
|
11293
11714
|
generateId,
|
|
11715
|
+
generateImage,
|
|
11294
11716
|
generateObject,
|
|
11295
11717
|
generateText,
|
|
11718
|
+
getStaticToolName,
|
|
11296
11719
|
getTextFromDataUrl,
|
|
11297
11720
|
getToolName,
|
|
11298
11721
|
getToolOrDynamicToolName,
|
|
@@ -11301,6 +11724,7 @@ export {
|
|
|
11301
11724
|
isDeepEqualData,
|
|
11302
11725
|
isFileUIPart,
|
|
11303
11726
|
isReasoningUIPart,
|
|
11727
|
+
isStaticToolUIPart,
|
|
11304
11728
|
isTextUIPart,
|
|
11305
11729
|
isToolOrDynamicToolUIPart,
|
|
11306
11730
|
isToolUIPart,
|