ai 5.1.0-beta.9 → 6.0.0-beta.30
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 +174 -0
- package/README.md +128 -44
- package/dist/index.d.mts +544 -373
- package/dist/index.d.ts +544 -373
- package/dist/index.js +990 -496
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +843 -355
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +4 -4
- package/dist/internal/index.d.ts +4 -4
- package/dist/internal/index.js +36 -8
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +36 -8
- package/dist/internal/index.mjs.map +1 -1
- package/dist/test/index.d.mts +34 -34
- package/dist/test/index.d.ts +34 -34
- package/dist/test/index.js +15 -15
- package/dist/test/index.js.map +1 -1
- package/dist/test/index.mjs +12 -12
- package/dist/test/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -20,45 +20,47 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
-
AISDKError: () =>
|
|
24
|
-
APICallError: () =>
|
|
23
|
+
AISDKError: () => import_provider18.AISDKError,
|
|
24
|
+
APICallError: () => import_provider18.APICallError,
|
|
25
25
|
AbstractChat: () => AbstractChat,
|
|
26
|
+
Agent: () => Agent,
|
|
26
27
|
DefaultChatTransport: () => DefaultChatTransport,
|
|
27
28
|
DownloadError: () => DownloadError,
|
|
28
|
-
EmptyResponseBodyError: () =>
|
|
29
|
+
EmptyResponseBodyError: () => import_provider18.EmptyResponseBodyError,
|
|
29
30
|
Experimental_Agent: () => Agent,
|
|
30
31
|
HttpChatTransport: () => HttpChatTransport,
|
|
31
32
|
InvalidArgumentError: () => InvalidArgumentError,
|
|
32
33
|
InvalidDataContentError: () => InvalidDataContentError,
|
|
33
34
|
InvalidMessageRoleError: () => InvalidMessageRoleError,
|
|
34
|
-
InvalidPromptError: () =>
|
|
35
|
-
InvalidResponseDataError: () =>
|
|
35
|
+
InvalidPromptError: () => import_provider18.InvalidPromptError,
|
|
36
|
+
InvalidResponseDataError: () => import_provider18.InvalidResponseDataError,
|
|
36
37
|
InvalidStreamPartError: () => InvalidStreamPartError,
|
|
37
38
|
InvalidToolInputError: () => InvalidToolInputError,
|
|
38
|
-
JSONParseError: () =>
|
|
39
|
+
JSONParseError: () => import_provider18.JSONParseError,
|
|
39
40
|
JsonToSseTransformStream: () => JsonToSseTransformStream,
|
|
40
|
-
LoadAPIKeyError: () =>
|
|
41
|
+
LoadAPIKeyError: () => import_provider18.LoadAPIKeyError,
|
|
41
42
|
MCPClientError: () => MCPClientError,
|
|
42
43
|
MessageConversionError: () => MessageConversionError,
|
|
43
|
-
NoContentGeneratedError: () =>
|
|
44
|
+
NoContentGeneratedError: () => import_provider18.NoContentGeneratedError,
|
|
44
45
|
NoImageGeneratedError: () => NoImageGeneratedError,
|
|
45
46
|
NoObjectGeneratedError: () => NoObjectGeneratedError,
|
|
46
47
|
NoOutputGeneratedError: () => NoOutputGeneratedError,
|
|
47
48
|
NoOutputSpecifiedError: () => NoOutputSpecifiedError,
|
|
48
|
-
|
|
49
|
+
NoSpeechGeneratedError: () => NoSpeechGeneratedError,
|
|
50
|
+
NoSuchModelError: () => import_provider18.NoSuchModelError,
|
|
49
51
|
NoSuchProviderError: () => NoSuchProviderError,
|
|
50
52
|
NoSuchToolError: () => NoSuchToolError,
|
|
51
53
|
Output: () => output_exports,
|
|
52
54
|
RetryError: () => RetryError,
|
|
53
55
|
SerialJobExecutor: () => SerialJobExecutor,
|
|
54
56
|
TextStreamChatTransport: () => TextStreamChatTransport,
|
|
55
|
-
TooManyEmbeddingValuesForCallError: () =>
|
|
57
|
+
TooManyEmbeddingValuesForCallError: () => import_provider18.TooManyEmbeddingValuesForCallError,
|
|
56
58
|
ToolCallRepairError: () => ToolCallRepairError,
|
|
57
|
-
TypeValidationError: () =>
|
|
59
|
+
TypeValidationError: () => import_provider18.TypeValidationError,
|
|
58
60
|
UI_MESSAGE_STREAM_HEADERS: () => UI_MESSAGE_STREAM_HEADERS,
|
|
59
|
-
UnsupportedFunctionalityError: () =>
|
|
61
|
+
UnsupportedFunctionalityError: () => import_provider18.UnsupportedFunctionalityError,
|
|
60
62
|
UnsupportedModelVersionError: () => UnsupportedModelVersionError,
|
|
61
|
-
asSchema: () =>
|
|
63
|
+
asSchema: () => import_provider_utils36.asSchema,
|
|
62
64
|
assistantModelMessageSchema: () => assistantModelMessageSchema,
|
|
63
65
|
callCompletionApi: () => callCompletionApi,
|
|
64
66
|
consumeStream: () => consumeStream,
|
|
@@ -72,14 +74,14 @@ __export(src_exports, {
|
|
|
72
74
|
coreUserMessageSchema: () => coreUserMessageSchema,
|
|
73
75
|
cosineSimilarity: () => cosineSimilarity,
|
|
74
76
|
createGateway: () => import_gateway3.createGateway,
|
|
75
|
-
createIdGenerator: () =>
|
|
77
|
+
createIdGenerator: () => import_provider_utils36.createIdGenerator,
|
|
76
78
|
createProviderRegistry: () => createProviderRegistry,
|
|
77
79
|
createTextStreamResponse: () => createTextStreamResponse,
|
|
78
80
|
createUIMessageStream: () => createUIMessageStream,
|
|
79
81
|
createUIMessageStreamResponse: () => createUIMessageStreamResponse,
|
|
80
82
|
customProvider: () => customProvider,
|
|
81
83
|
defaultSettingsMiddleware: () => defaultSettingsMiddleware,
|
|
82
|
-
dynamicTool: () =>
|
|
84
|
+
dynamicTool: () => import_provider_utils36.dynamicTool,
|
|
83
85
|
embed: () => embed,
|
|
84
86
|
embedMany: () => embedMany,
|
|
85
87
|
experimental_createMCPClient: () => createMCPClient,
|
|
@@ -90,7 +92,7 @@ __export(src_exports, {
|
|
|
90
92
|
experimental_transcribe: () => transcribe,
|
|
91
93
|
extractReasoningMiddleware: () => extractReasoningMiddleware,
|
|
92
94
|
gateway: () => import_gateway3.gateway,
|
|
93
|
-
generateId: () =>
|
|
95
|
+
generateId: () => import_provider_utils36.generateId,
|
|
94
96
|
generateObject: () => generateObject,
|
|
95
97
|
generateText: () => generateText,
|
|
96
98
|
getTextFromDataUrl: () => getTextFromDataUrl,
|
|
@@ -100,10 +102,11 @@ __export(src_exports, {
|
|
|
100
102
|
isDeepEqualData: () => isDeepEqualData,
|
|
101
103
|
isToolOrDynamicToolUIPart: () => isToolOrDynamicToolUIPart,
|
|
102
104
|
isToolUIPart: () => isToolUIPart,
|
|
103
|
-
jsonSchema: () =>
|
|
105
|
+
jsonSchema: () => import_provider_utils36.jsonSchema,
|
|
106
|
+
lastAssistantMessageIsCompleteWithApprovalResponses: () => lastAssistantMessageIsCompleteWithApprovalResponses,
|
|
104
107
|
lastAssistantMessageIsCompleteWithToolCalls: () => lastAssistantMessageIsCompleteWithToolCalls,
|
|
105
108
|
modelMessageSchema: () => modelMessageSchema,
|
|
106
|
-
parseJsonEventStream: () =>
|
|
109
|
+
parseJsonEventStream: () => import_provider_utils36.parseJsonEventStream,
|
|
107
110
|
parsePartialJson: () => parsePartialJson,
|
|
108
111
|
pipeTextStreamToResponse: () => pipeTextStreamToResponse,
|
|
109
112
|
pipeUIMessageStreamToResponse: () => pipeUIMessageStreamToResponse,
|
|
@@ -116,21 +119,21 @@ __export(src_exports, {
|
|
|
116
119
|
streamObject: () => streamObject,
|
|
117
120
|
streamText: () => streamText,
|
|
118
121
|
systemModelMessageSchema: () => systemModelMessageSchema,
|
|
119
|
-
tool: () =>
|
|
122
|
+
tool: () => import_provider_utils36.tool,
|
|
120
123
|
toolModelMessageSchema: () => toolModelMessageSchema,
|
|
121
124
|
uiMessageChunkSchema: () => uiMessageChunkSchema,
|
|
122
125
|
userModelMessageSchema: () => userModelMessageSchema,
|
|
123
126
|
validateUIMessages: () => validateUIMessages,
|
|
124
127
|
wrapLanguageModel: () => wrapLanguageModel,
|
|
125
128
|
wrapProvider: () => wrapProvider,
|
|
126
|
-
zodSchema: () =>
|
|
129
|
+
zodSchema: () => import_provider_utils36.zodSchema
|
|
127
130
|
});
|
|
128
131
|
module.exports = __toCommonJS(src_exports);
|
|
129
132
|
var import_gateway3 = require("@ai-sdk/gateway");
|
|
130
|
-
var
|
|
133
|
+
var import_provider_utils36 = require("@ai-sdk/provider-utils");
|
|
131
134
|
|
|
132
135
|
// src/generate-text/generate-text.ts
|
|
133
|
-
var
|
|
136
|
+
var import_provider_utils11 = require("@ai-sdk/provider-utils");
|
|
134
137
|
|
|
135
138
|
// src/error/no-output-specified-error.ts
|
|
136
139
|
var import_provider = require("@ai-sdk/provider");
|
|
@@ -204,7 +207,7 @@ var logWarnings = (warnings) => {
|
|
|
204
207
|
var import_gateway = require("@ai-sdk/gateway");
|
|
205
208
|
|
|
206
209
|
// src/error/index.ts
|
|
207
|
-
var
|
|
210
|
+
var import_provider18 = require("@ai-sdk/provider");
|
|
208
211
|
|
|
209
212
|
// src/error/invalid-argument-error.ts
|
|
210
213
|
var import_provider2 = require("@ai-sdk/provider");
|
|
@@ -369,13 +372,25 @@ var NoOutputGeneratedError = class extends import_provider8.AISDKError {
|
|
|
369
372
|
};
|
|
370
373
|
_a8 = symbol8;
|
|
371
374
|
|
|
372
|
-
// src/error/no-
|
|
375
|
+
// src/error/no-speech-generated-error.ts
|
|
373
376
|
var import_provider9 = require("@ai-sdk/provider");
|
|
377
|
+
var NoSpeechGeneratedError = class extends import_provider9.AISDKError {
|
|
378
|
+
constructor(options) {
|
|
379
|
+
super({
|
|
380
|
+
name: "AI_NoSpeechGeneratedError",
|
|
381
|
+
message: "No speech audio generated."
|
|
382
|
+
});
|
|
383
|
+
this.responses = options.responses;
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
// src/error/no-such-tool-error.ts
|
|
388
|
+
var import_provider10 = require("@ai-sdk/provider");
|
|
374
389
|
var name9 = "AI_NoSuchToolError";
|
|
375
390
|
var marker9 = `vercel.ai.error.${name9}`;
|
|
376
391
|
var symbol9 = Symbol.for(marker9);
|
|
377
392
|
var _a9;
|
|
378
|
-
var NoSuchToolError = class extends
|
|
393
|
+
var NoSuchToolError = class extends import_provider10.AISDKError {
|
|
379
394
|
constructor({
|
|
380
395
|
toolName,
|
|
381
396
|
availableTools = void 0,
|
|
@@ -387,36 +402,36 @@ var NoSuchToolError = class extends import_provider9.AISDKError {
|
|
|
387
402
|
this.availableTools = availableTools;
|
|
388
403
|
}
|
|
389
404
|
static isInstance(error) {
|
|
390
|
-
return
|
|
405
|
+
return import_provider10.AISDKError.hasMarker(error, marker9);
|
|
391
406
|
}
|
|
392
407
|
};
|
|
393
408
|
_a9 = symbol9;
|
|
394
409
|
|
|
395
410
|
// src/error/tool-call-repair-error.ts
|
|
396
|
-
var
|
|
411
|
+
var import_provider11 = require("@ai-sdk/provider");
|
|
397
412
|
var name10 = "AI_ToolCallRepairError";
|
|
398
413
|
var marker10 = `vercel.ai.error.${name10}`;
|
|
399
414
|
var symbol10 = Symbol.for(marker10);
|
|
400
415
|
var _a10;
|
|
401
|
-
var ToolCallRepairError = class extends
|
|
416
|
+
var ToolCallRepairError = class extends import_provider11.AISDKError {
|
|
402
417
|
constructor({
|
|
403
418
|
cause,
|
|
404
419
|
originalError,
|
|
405
|
-
message = `Error repairing tool call: ${(0,
|
|
420
|
+
message = `Error repairing tool call: ${(0, import_provider11.getErrorMessage)(cause)}`
|
|
406
421
|
}) {
|
|
407
422
|
super({ name: name10, message, cause });
|
|
408
423
|
this[_a10] = true;
|
|
409
424
|
this.originalError = originalError;
|
|
410
425
|
}
|
|
411
426
|
static isInstance(error) {
|
|
412
|
-
return
|
|
427
|
+
return import_provider11.AISDKError.hasMarker(error, marker10);
|
|
413
428
|
}
|
|
414
429
|
};
|
|
415
430
|
_a10 = symbol10;
|
|
416
431
|
|
|
417
432
|
// src/error/unsupported-model-version-error.ts
|
|
418
|
-
var
|
|
419
|
-
var UnsupportedModelVersionError = class extends
|
|
433
|
+
var import_provider12 = require("@ai-sdk/provider");
|
|
434
|
+
var UnsupportedModelVersionError = class extends import_provider12.AISDKError {
|
|
420
435
|
constructor(options) {
|
|
421
436
|
super({
|
|
422
437
|
name: "AI_UnsupportedModelVersionError",
|
|
@@ -429,12 +444,12 @@ var UnsupportedModelVersionError = class extends import_provider11.AISDKError {
|
|
|
429
444
|
};
|
|
430
445
|
|
|
431
446
|
// src/prompt/invalid-data-content-error.ts
|
|
432
|
-
var
|
|
447
|
+
var import_provider13 = require("@ai-sdk/provider");
|
|
433
448
|
var name11 = "AI_InvalidDataContentError";
|
|
434
449
|
var marker11 = `vercel.ai.error.${name11}`;
|
|
435
450
|
var symbol11 = Symbol.for(marker11);
|
|
436
451
|
var _a11;
|
|
437
|
-
var InvalidDataContentError = class extends
|
|
452
|
+
var InvalidDataContentError = class extends import_provider13.AISDKError {
|
|
438
453
|
constructor({
|
|
439
454
|
content,
|
|
440
455
|
cause,
|
|
@@ -445,18 +460,18 @@ var InvalidDataContentError = class extends import_provider12.AISDKError {
|
|
|
445
460
|
this.content = content;
|
|
446
461
|
}
|
|
447
462
|
static isInstance(error) {
|
|
448
|
-
return
|
|
463
|
+
return import_provider13.AISDKError.hasMarker(error, marker11);
|
|
449
464
|
}
|
|
450
465
|
};
|
|
451
466
|
_a11 = symbol11;
|
|
452
467
|
|
|
453
468
|
// src/prompt/invalid-message-role-error.ts
|
|
454
|
-
var
|
|
469
|
+
var import_provider14 = require("@ai-sdk/provider");
|
|
455
470
|
var name12 = "AI_InvalidMessageRoleError";
|
|
456
471
|
var marker12 = `vercel.ai.error.${name12}`;
|
|
457
472
|
var symbol12 = Symbol.for(marker12);
|
|
458
473
|
var _a12;
|
|
459
|
-
var InvalidMessageRoleError = class extends
|
|
474
|
+
var InvalidMessageRoleError = class extends import_provider14.AISDKError {
|
|
460
475
|
constructor({
|
|
461
476
|
role,
|
|
462
477
|
message = `Invalid message role: '${role}'. Must be one of: "system", "user", "assistant", "tool".`
|
|
@@ -466,18 +481,18 @@ var InvalidMessageRoleError = class extends import_provider13.AISDKError {
|
|
|
466
481
|
this.role = role;
|
|
467
482
|
}
|
|
468
483
|
static isInstance(error) {
|
|
469
|
-
return
|
|
484
|
+
return import_provider14.AISDKError.hasMarker(error, marker12);
|
|
470
485
|
}
|
|
471
486
|
};
|
|
472
487
|
_a12 = symbol12;
|
|
473
488
|
|
|
474
489
|
// src/prompt/message-conversion-error.ts
|
|
475
|
-
var
|
|
490
|
+
var import_provider15 = require("@ai-sdk/provider");
|
|
476
491
|
var name13 = "AI_MessageConversionError";
|
|
477
492
|
var marker13 = `vercel.ai.error.${name13}`;
|
|
478
493
|
var symbol13 = Symbol.for(marker13);
|
|
479
494
|
var _a13;
|
|
480
|
-
var MessageConversionError = class extends
|
|
495
|
+
var MessageConversionError = class extends import_provider15.AISDKError {
|
|
481
496
|
constructor({
|
|
482
497
|
originalMessage,
|
|
483
498
|
message
|
|
@@ -487,18 +502,18 @@ var MessageConversionError = class extends import_provider14.AISDKError {
|
|
|
487
502
|
this.originalMessage = originalMessage;
|
|
488
503
|
}
|
|
489
504
|
static isInstance(error) {
|
|
490
|
-
return
|
|
505
|
+
return import_provider15.AISDKError.hasMarker(error, marker13);
|
|
491
506
|
}
|
|
492
507
|
};
|
|
493
508
|
_a13 = symbol13;
|
|
494
509
|
|
|
495
510
|
// src/util/download/download-error.ts
|
|
496
|
-
var
|
|
511
|
+
var import_provider16 = require("@ai-sdk/provider");
|
|
497
512
|
var name14 = "AI_DownloadError";
|
|
498
513
|
var marker14 = `vercel.ai.error.${name14}`;
|
|
499
514
|
var symbol14 = Symbol.for(marker14);
|
|
500
515
|
var _a14;
|
|
501
|
-
var DownloadError = class extends
|
|
516
|
+
var DownloadError = class extends import_provider16.AISDKError {
|
|
502
517
|
constructor({
|
|
503
518
|
url,
|
|
504
519
|
statusCode,
|
|
@@ -513,18 +528,18 @@ var DownloadError = class extends import_provider15.AISDKError {
|
|
|
513
528
|
this.statusText = statusText;
|
|
514
529
|
}
|
|
515
530
|
static isInstance(error) {
|
|
516
|
-
return
|
|
531
|
+
return import_provider16.AISDKError.hasMarker(error, marker14);
|
|
517
532
|
}
|
|
518
533
|
};
|
|
519
534
|
_a14 = symbol14;
|
|
520
535
|
|
|
521
536
|
// src/util/retry-error.ts
|
|
522
|
-
var
|
|
537
|
+
var import_provider17 = require("@ai-sdk/provider");
|
|
523
538
|
var name15 = "AI_RetryError";
|
|
524
539
|
var marker15 = `vercel.ai.error.${name15}`;
|
|
525
540
|
var symbol15 = Symbol.for(marker15);
|
|
526
541
|
var _a15;
|
|
527
|
-
var RetryError = class extends
|
|
542
|
+
var RetryError = class extends import_provider17.AISDKError {
|
|
528
543
|
constructor({
|
|
529
544
|
message,
|
|
530
545
|
reason,
|
|
@@ -537,34 +552,60 @@ var RetryError = class extends import_provider16.AISDKError {
|
|
|
537
552
|
this.lastError = errors[errors.length - 1];
|
|
538
553
|
}
|
|
539
554
|
static isInstance(error) {
|
|
540
|
-
return
|
|
555
|
+
return import_provider17.AISDKError.hasMarker(error, marker15);
|
|
541
556
|
}
|
|
542
557
|
};
|
|
543
558
|
_a15 = symbol15;
|
|
544
559
|
|
|
545
560
|
// src/model/resolve-model.ts
|
|
561
|
+
function transformToV3LanguageModel(model) {
|
|
562
|
+
return new Proxy(model, {
|
|
563
|
+
get(target, prop) {
|
|
564
|
+
if (prop === "specificationVersion")
|
|
565
|
+
return "v3";
|
|
566
|
+
return target[prop];
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
function transformToV3EmbeddingModel(model) {
|
|
571
|
+
return new Proxy(model, {
|
|
572
|
+
get(target, prop) {
|
|
573
|
+
if (prop === "specificationVersion")
|
|
574
|
+
return "v3";
|
|
575
|
+
return target[prop];
|
|
576
|
+
}
|
|
577
|
+
});
|
|
578
|
+
}
|
|
546
579
|
function resolveLanguageModel(model) {
|
|
547
580
|
if (typeof model !== "string") {
|
|
548
|
-
if (model.specificationVersion !== "v2") {
|
|
581
|
+
if (model.specificationVersion !== "v3" && model.specificationVersion !== "v2") {
|
|
582
|
+
const unsupportedModel = model;
|
|
549
583
|
throw new UnsupportedModelVersionError({
|
|
550
|
-
version:
|
|
551
|
-
provider:
|
|
552
|
-
modelId:
|
|
584
|
+
version: unsupportedModel.specificationVersion,
|
|
585
|
+
provider: unsupportedModel.provider,
|
|
586
|
+
modelId: unsupportedModel.modelId
|
|
553
587
|
});
|
|
554
588
|
}
|
|
589
|
+
if (model.specificationVersion === "v2") {
|
|
590
|
+
return transformToV3LanguageModel(model);
|
|
591
|
+
}
|
|
555
592
|
return model;
|
|
556
593
|
}
|
|
557
594
|
return getGlobalProvider().languageModel(model);
|
|
558
595
|
}
|
|
559
596
|
function resolveEmbeddingModel(model) {
|
|
560
597
|
if (typeof model !== "string") {
|
|
561
|
-
if (model.specificationVersion !== "v3") {
|
|
598
|
+
if (model.specificationVersion !== "v3" && model.specificationVersion !== "v2") {
|
|
599
|
+
const unsupportedModel = model;
|
|
562
600
|
throw new UnsupportedModelVersionError({
|
|
563
|
-
version:
|
|
564
|
-
provider:
|
|
565
|
-
modelId:
|
|
601
|
+
version: unsupportedModel.specificationVersion,
|
|
602
|
+
provider: unsupportedModel.provider,
|
|
603
|
+
modelId: unsupportedModel.modelId
|
|
566
604
|
});
|
|
567
605
|
}
|
|
606
|
+
if (model.specificationVersion === "v2") {
|
|
607
|
+
return transformToV3EmbeddingModel(model);
|
|
608
|
+
}
|
|
568
609
|
return model;
|
|
569
610
|
}
|
|
570
611
|
return getGlobalProvider().textEmbeddingModel(
|
|
@@ -768,7 +809,7 @@ function detectMediaType({
|
|
|
768
809
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
769
810
|
|
|
770
811
|
// src/version.ts
|
|
771
|
-
var VERSION = true ? "
|
|
812
|
+
var VERSION = true ? "6.0.0-beta.30" : "0.0.0-test";
|
|
772
813
|
|
|
773
814
|
// src/util/download/download.ts
|
|
774
815
|
var download = async ({ url }) => {
|
|
@@ -809,7 +850,7 @@ var createDefaultDownloadFunction = (download2 = download) => (requestedDownload
|
|
|
809
850
|
);
|
|
810
851
|
|
|
811
852
|
// src/prompt/data-content.ts
|
|
812
|
-
var
|
|
853
|
+
var import_provider19 = require("@ai-sdk/provider");
|
|
813
854
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
814
855
|
var import_v4 = require("zod/v4");
|
|
815
856
|
|
|
@@ -843,7 +884,7 @@ var dataContentSchema = import_v4.z.union([
|
|
|
843
884
|
{ message: "Must be a Buffer" }
|
|
844
885
|
)
|
|
845
886
|
]);
|
|
846
|
-
function
|
|
887
|
+
function convertToLanguageModelV3DataContent(content) {
|
|
847
888
|
if (content instanceof Uint8Array) {
|
|
848
889
|
return { data: content, mediaType: void 0 };
|
|
849
890
|
}
|
|
@@ -861,7 +902,7 @@ function convertToLanguageModelV2DataContent(content) {
|
|
|
861
902
|
content.toString()
|
|
862
903
|
);
|
|
863
904
|
if (dataUrlMediaType == null || base64Content == null) {
|
|
864
|
-
throw new
|
|
905
|
+
throw new import_provider19.AISDKError({
|
|
865
906
|
name: "InvalidDataContentError",
|
|
866
907
|
message: `Invalid data URL format in content ${content.toString()}`
|
|
867
908
|
});
|
|
@@ -911,12 +952,26 @@ async function convertToLanguageModelPrompt({
|
|
|
911
952
|
download2,
|
|
912
953
|
supportedUrls
|
|
913
954
|
);
|
|
914
|
-
|
|
955
|
+
const messages = [
|
|
915
956
|
...prompt.system != null ? [{ role: "system", content: prompt.system }] : [],
|
|
916
957
|
...prompt.messages.map(
|
|
917
958
|
(message) => convertToLanguageModelMessage({ message, downloadedAssets })
|
|
918
959
|
)
|
|
919
960
|
];
|
|
961
|
+
const combinedMessages = [];
|
|
962
|
+
for (const message of messages) {
|
|
963
|
+
if (message.role !== "tool") {
|
|
964
|
+
combinedMessages.push(message);
|
|
965
|
+
continue;
|
|
966
|
+
}
|
|
967
|
+
const lastCombinedMessage = combinedMessages.at(-1);
|
|
968
|
+
if ((lastCombinedMessage == null ? void 0 : lastCombinedMessage.role) === "tool") {
|
|
969
|
+
lastCombinedMessage.content.push(...message.content);
|
|
970
|
+
} else {
|
|
971
|
+
combinedMessages.push(message);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
return combinedMessages;
|
|
920
975
|
}
|
|
921
976
|
function convertToLanguageModelMessage({
|
|
922
977
|
message,
|
|
@@ -958,11 +1013,13 @@ function convertToLanguageModelMessage({
|
|
|
958
1013
|
content: message.content.filter(
|
|
959
1014
|
// remove empty text parts (no text, and no provider options):
|
|
960
1015
|
(part) => part.type !== "text" || part.text !== "" || part.providerOptions != null
|
|
1016
|
+
).filter(
|
|
1017
|
+
(part) => part.type !== "tool-approval-request"
|
|
961
1018
|
).map((part) => {
|
|
962
1019
|
const providerOptions = part.providerOptions;
|
|
963
1020
|
switch (part.type) {
|
|
964
1021
|
case "file": {
|
|
965
|
-
const { data, mediaType } =
|
|
1022
|
+
const { data, mediaType } = convertToLanguageModelV3DataContent(
|
|
966
1023
|
part.data
|
|
967
1024
|
);
|
|
968
1025
|
return {
|
|
@@ -1014,7 +1071,7 @@ function convertToLanguageModelMessage({
|
|
|
1014
1071
|
case "tool": {
|
|
1015
1072
|
return {
|
|
1016
1073
|
role: "tool",
|
|
1017
|
-
content: message.content.map((part) => ({
|
|
1074
|
+
content: message.content.filter((part) => part.type !== "tool-approval-response").map((part) => ({
|
|
1018
1075
|
type: "tool-result",
|
|
1019
1076
|
toolCallId: part.toolCallId,
|
|
1020
1077
|
toolName: part.toolName,
|
|
@@ -1087,7 +1144,7 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
|
|
|
1087
1144
|
default:
|
|
1088
1145
|
throw new Error(`Unsupported part type: ${type}`);
|
|
1089
1146
|
}
|
|
1090
|
-
const { data: convertedData, mediaType: convertedMediaType } =
|
|
1147
|
+
const { data: convertedData, mediaType: convertedMediaType } = convertToLanguageModelV3DataContent(originalData);
|
|
1091
1148
|
let mediaType = convertedMediaType != null ? convertedMediaType : part.mediaType;
|
|
1092
1149
|
let data = convertedData;
|
|
1093
1150
|
if (data instanceof URL) {
|
|
@@ -1126,6 +1183,27 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
|
|
|
1126
1183
|
}
|
|
1127
1184
|
}
|
|
1128
1185
|
|
|
1186
|
+
// src/prompt/create-tool-model-output.ts
|
|
1187
|
+
var import_provider20 = require("@ai-sdk/provider");
|
|
1188
|
+
function createToolModelOutput({
|
|
1189
|
+
output,
|
|
1190
|
+
tool: tool3,
|
|
1191
|
+
errorMode
|
|
1192
|
+
}) {
|
|
1193
|
+
if (errorMode === "text") {
|
|
1194
|
+
return { type: "error-text", value: (0, import_provider20.getErrorMessage)(output) };
|
|
1195
|
+
} else if (errorMode === "json") {
|
|
1196
|
+
return { type: "error-json", value: toJSONValue(output) };
|
|
1197
|
+
}
|
|
1198
|
+
if (tool3 == null ? void 0 : tool3.toModelOutput) {
|
|
1199
|
+
return tool3.toModelOutput(output);
|
|
1200
|
+
}
|
|
1201
|
+
return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: toJSONValue(output) };
|
|
1202
|
+
}
|
|
1203
|
+
function toJSONValue(value) {
|
|
1204
|
+
return value === void 0 ? null : value;
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1129
1207
|
// src/prompt/prepare-call-settings.ts
|
|
1130
1208
|
function prepareCallSettings({
|
|
1131
1209
|
maxOutputTokens,
|
|
@@ -1274,7 +1352,7 @@ function prepareToolsAndToolChoice({
|
|
|
1274
1352
|
}
|
|
1275
1353
|
|
|
1276
1354
|
// src/prompt/standardize-prompt.ts
|
|
1277
|
-
var
|
|
1355
|
+
var import_provider21 = require("@ai-sdk/provider");
|
|
1278
1356
|
var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
|
1279
1357
|
var import_v46 = require("zod/v4");
|
|
1280
1358
|
|
|
@@ -1377,6 +1455,17 @@ var toolResultPartSchema = import_v44.z.object({
|
|
|
1377
1455
|
output: outputSchema,
|
|
1378
1456
|
providerOptions: providerMetadataSchema.optional()
|
|
1379
1457
|
});
|
|
1458
|
+
var toolApprovalRequestSchema = import_v44.z.object({
|
|
1459
|
+
type: import_v44.z.literal("tool-approval-request"),
|
|
1460
|
+
approvalId: import_v44.z.string(),
|
|
1461
|
+
toolCallId: import_v44.z.string()
|
|
1462
|
+
});
|
|
1463
|
+
var toolApprovalResponseSchema = import_v44.z.object({
|
|
1464
|
+
type: import_v44.z.literal("tool-approval-response"),
|
|
1465
|
+
approvalId: import_v44.z.string(),
|
|
1466
|
+
approved: import_v44.z.boolean(),
|
|
1467
|
+
reason: import_v44.z.string().optional()
|
|
1468
|
+
});
|
|
1380
1469
|
|
|
1381
1470
|
// src/prompt/message.ts
|
|
1382
1471
|
var systemModelMessageSchema = import_v45.z.object(
|
|
@@ -1406,7 +1495,8 @@ var assistantModelMessageSchema = import_v45.z.object({
|
|
|
1406
1495
|
filePartSchema,
|
|
1407
1496
|
reasoningPartSchema,
|
|
1408
1497
|
toolCallPartSchema,
|
|
1409
|
-
toolResultPartSchema
|
|
1498
|
+
toolResultPartSchema,
|
|
1499
|
+
toolApprovalRequestSchema
|
|
1410
1500
|
])
|
|
1411
1501
|
)
|
|
1412
1502
|
]),
|
|
@@ -1415,7 +1505,7 @@ var assistantModelMessageSchema = import_v45.z.object({
|
|
|
1415
1505
|
var coreAssistantMessageSchema = assistantModelMessageSchema;
|
|
1416
1506
|
var toolModelMessageSchema = import_v45.z.object({
|
|
1417
1507
|
role: import_v45.z.literal("tool"),
|
|
1418
|
-
content: import_v45.z.array(toolResultPartSchema),
|
|
1508
|
+
content: import_v45.z.array(import_v45.z.union([toolResultPartSchema, toolApprovalResponseSchema])),
|
|
1419
1509
|
providerOptions: providerMetadataSchema.optional()
|
|
1420
1510
|
});
|
|
1421
1511
|
var coreToolMessageSchema = toolModelMessageSchema;
|
|
@@ -1430,19 +1520,19 @@ var coreMessageSchema = modelMessageSchema;
|
|
|
1430
1520
|
// src/prompt/standardize-prompt.ts
|
|
1431
1521
|
async function standardizePrompt(prompt) {
|
|
1432
1522
|
if (prompt.prompt == null && prompt.messages == null) {
|
|
1433
|
-
throw new
|
|
1523
|
+
throw new import_provider21.InvalidPromptError({
|
|
1434
1524
|
prompt,
|
|
1435
1525
|
message: "prompt or messages must be defined"
|
|
1436
1526
|
});
|
|
1437
1527
|
}
|
|
1438
1528
|
if (prompt.prompt != null && prompt.messages != null) {
|
|
1439
|
-
throw new
|
|
1529
|
+
throw new import_provider21.InvalidPromptError({
|
|
1440
1530
|
prompt,
|
|
1441
1531
|
message: "prompt and messages cannot be defined at the same time"
|
|
1442
1532
|
});
|
|
1443
1533
|
}
|
|
1444
1534
|
if (prompt.system != null && typeof prompt.system !== "string") {
|
|
1445
|
-
throw new
|
|
1535
|
+
throw new import_provider21.InvalidPromptError({
|
|
1446
1536
|
prompt,
|
|
1447
1537
|
message: "system must be a string"
|
|
1448
1538
|
});
|
|
@@ -1455,13 +1545,13 @@ async function standardizePrompt(prompt) {
|
|
|
1455
1545
|
} else if (prompt.messages != null) {
|
|
1456
1546
|
messages = prompt.messages;
|
|
1457
1547
|
} else {
|
|
1458
|
-
throw new
|
|
1548
|
+
throw new import_provider21.InvalidPromptError({
|
|
1459
1549
|
prompt,
|
|
1460
1550
|
message: "prompt or messages must be defined"
|
|
1461
1551
|
});
|
|
1462
1552
|
}
|
|
1463
1553
|
if (messages.length === 0) {
|
|
1464
|
-
throw new
|
|
1554
|
+
throw new import_provider21.InvalidPromptError({
|
|
1465
1555
|
prompt,
|
|
1466
1556
|
message: "messages must not be empty"
|
|
1467
1557
|
});
|
|
@@ -1471,7 +1561,7 @@ async function standardizePrompt(prompt) {
|
|
|
1471
1561
|
schema: import_v46.z.array(modelMessageSchema)
|
|
1472
1562
|
});
|
|
1473
1563
|
if (!validationResult.success) {
|
|
1474
|
-
throw new
|
|
1564
|
+
throw new import_provider21.InvalidPromptError({
|
|
1475
1565
|
prompt,
|
|
1476
1566
|
message: "The messages must be a ModelMessage[]. If you have passed a UIMessage[], you can use convertToModelMessages to convert them.",
|
|
1477
1567
|
cause: validationResult.error
|
|
@@ -1485,10 +1575,10 @@ async function standardizePrompt(prompt) {
|
|
|
1485
1575
|
|
|
1486
1576
|
// src/prompt/wrap-gateway-error.ts
|
|
1487
1577
|
var import_gateway2 = require("@ai-sdk/gateway");
|
|
1488
|
-
var
|
|
1578
|
+
var import_provider22 = require("@ai-sdk/provider");
|
|
1489
1579
|
function wrapGatewayError(error) {
|
|
1490
1580
|
if (import_gateway2.GatewayAuthenticationError.isInstance(error) || import_gateway2.GatewayModelNotFoundError.isInstance(error)) {
|
|
1491
|
-
return new
|
|
1581
|
+
return new import_provider22.AISDKError({
|
|
1492
1582
|
name: "GatewayError",
|
|
1493
1583
|
message: "Vercel AI Gateway access failed. If you want to use AI SDK providers directly, use the providers, e.g. @ai-sdk/openai, or register a different global default provider.",
|
|
1494
1584
|
cause: error
|
|
@@ -1734,7 +1824,7 @@ function asArray(value) {
|
|
|
1734
1824
|
}
|
|
1735
1825
|
|
|
1736
1826
|
// src/util/retry-with-exponential-backoff.ts
|
|
1737
|
-
var
|
|
1827
|
+
var import_provider23 = require("@ai-sdk/provider");
|
|
1738
1828
|
var import_provider_utils7 = require("@ai-sdk/provider-utils");
|
|
1739
1829
|
function getRetryDelayInMs({
|
|
1740
1830
|
error,
|
|
@@ -1801,7 +1891,7 @@ async function _retryWithExponentialBackoff(f, {
|
|
|
1801
1891
|
errors: newErrors
|
|
1802
1892
|
});
|
|
1803
1893
|
}
|
|
1804
|
-
if (error instanceof Error &&
|
|
1894
|
+
if (error instanceof Error && import_provider23.APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
|
|
1805
1895
|
await (0, import_provider_utils7.delay)(
|
|
1806
1896
|
getRetryDelayInMs({
|
|
1807
1897
|
error,
|
|
@@ -1862,6 +1952,164 @@ function prepareRetries({
|
|
|
1862
1952
|
};
|
|
1863
1953
|
}
|
|
1864
1954
|
|
|
1955
|
+
// src/generate-text/collect-tool-approvals.ts
|
|
1956
|
+
function collectToolApprovals({
|
|
1957
|
+
messages
|
|
1958
|
+
}) {
|
|
1959
|
+
const lastMessage = messages.at(-1);
|
|
1960
|
+
if ((lastMessage == null ? void 0 : lastMessage.role) != "tool") {
|
|
1961
|
+
return {
|
|
1962
|
+
approvedToolApprovals: [],
|
|
1963
|
+
deniedToolApprovals: []
|
|
1964
|
+
};
|
|
1965
|
+
}
|
|
1966
|
+
const toolCallsByToolCallId = {};
|
|
1967
|
+
for (const message of messages) {
|
|
1968
|
+
if (message.role === "assistant" && typeof message.content !== "string") {
|
|
1969
|
+
const content = message.content;
|
|
1970
|
+
for (const part of content) {
|
|
1971
|
+
if (part.type === "tool-call") {
|
|
1972
|
+
toolCallsByToolCallId[part.toolCallId] = part;
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
}
|
|
1976
|
+
}
|
|
1977
|
+
const toolApprovalRequestsByApprovalId = {};
|
|
1978
|
+
for (const message of messages) {
|
|
1979
|
+
if (message.role === "assistant" && typeof message.content !== "string") {
|
|
1980
|
+
const content = message.content;
|
|
1981
|
+
for (const part of content) {
|
|
1982
|
+
if (part.type === "tool-approval-request") {
|
|
1983
|
+
toolApprovalRequestsByApprovalId[part.approvalId] = part;
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
const toolResults = {};
|
|
1989
|
+
for (const part of lastMessage.content) {
|
|
1990
|
+
if (part.type === "tool-result") {
|
|
1991
|
+
toolResults[part.toolCallId] = part;
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
const approvedToolApprovals = [];
|
|
1995
|
+
const deniedToolApprovals = [];
|
|
1996
|
+
const approvalResponses = lastMessage.content.filter(
|
|
1997
|
+
(part) => part.type === "tool-approval-response"
|
|
1998
|
+
);
|
|
1999
|
+
for (const approvalResponse of approvalResponses) {
|
|
2000
|
+
const approvalRequest = toolApprovalRequestsByApprovalId[approvalResponse.approvalId];
|
|
2001
|
+
if (toolResults[approvalRequest.toolCallId] != null) {
|
|
2002
|
+
continue;
|
|
2003
|
+
}
|
|
2004
|
+
const approval = {
|
|
2005
|
+
approvalRequest,
|
|
2006
|
+
approvalResponse,
|
|
2007
|
+
toolCall: toolCallsByToolCallId[approvalRequest.toolCallId]
|
|
2008
|
+
};
|
|
2009
|
+
if (approvalResponse.approved) {
|
|
2010
|
+
approvedToolApprovals.push(approval);
|
|
2011
|
+
} else {
|
|
2012
|
+
deniedToolApprovals.push(approval);
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2015
|
+
return { approvedToolApprovals, deniedToolApprovals };
|
|
2016
|
+
}
|
|
2017
|
+
|
|
2018
|
+
// src/generate-text/execute-tool-call.ts
|
|
2019
|
+
var import_provider_utils8 = require("@ai-sdk/provider-utils");
|
|
2020
|
+
async function executeToolCall({
|
|
2021
|
+
toolCall,
|
|
2022
|
+
tools,
|
|
2023
|
+
tracer,
|
|
2024
|
+
telemetry,
|
|
2025
|
+
messages,
|
|
2026
|
+
abortSignal,
|
|
2027
|
+
experimental_context,
|
|
2028
|
+
onPreliminaryToolResult
|
|
2029
|
+
}) {
|
|
2030
|
+
const { toolName, toolCallId, input } = toolCall;
|
|
2031
|
+
const tool3 = tools == null ? void 0 : tools[toolName];
|
|
2032
|
+
if ((tool3 == null ? void 0 : tool3.execute) == null) {
|
|
2033
|
+
return void 0;
|
|
2034
|
+
}
|
|
2035
|
+
return recordSpan({
|
|
2036
|
+
name: "ai.toolCall",
|
|
2037
|
+
attributes: selectTelemetryAttributes({
|
|
2038
|
+
telemetry,
|
|
2039
|
+
attributes: {
|
|
2040
|
+
...assembleOperationName({
|
|
2041
|
+
operationId: "ai.toolCall",
|
|
2042
|
+
telemetry
|
|
2043
|
+
}),
|
|
2044
|
+
"ai.toolCall.name": toolName,
|
|
2045
|
+
"ai.toolCall.id": toolCallId,
|
|
2046
|
+
"ai.toolCall.args": {
|
|
2047
|
+
output: () => JSON.stringify(input)
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
}),
|
|
2051
|
+
tracer,
|
|
2052
|
+
fn: async (span) => {
|
|
2053
|
+
let output;
|
|
2054
|
+
try {
|
|
2055
|
+
const stream = (0, import_provider_utils8.executeTool)({
|
|
2056
|
+
execute: tool3.execute.bind(tool3),
|
|
2057
|
+
input,
|
|
2058
|
+
options: {
|
|
2059
|
+
toolCallId,
|
|
2060
|
+
messages,
|
|
2061
|
+
abortSignal,
|
|
2062
|
+
experimental_context
|
|
2063
|
+
}
|
|
2064
|
+
});
|
|
2065
|
+
for await (const part of stream) {
|
|
2066
|
+
if (part.type === "preliminary") {
|
|
2067
|
+
onPreliminaryToolResult == null ? void 0 : onPreliminaryToolResult({
|
|
2068
|
+
...toolCall,
|
|
2069
|
+
type: "tool-result",
|
|
2070
|
+
output: part.output,
|
|
2071
|
+
preliminary: true
|
|
2072
|
+
});
|
|
2073
|
+
} else {
|
|
2074
|
+
output = part.output;
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
} catch (error) {
|
|
2078
|
+
recordErrorOnSpan(span, error);
|
|
2079
|
+
return {
|
|
2080
|
+
type: "tool-error",
|
|
2081
|
+
toolCallId,
|
|
2082
|
+
toolName,
|
|
2083
|
+
input,
|
|
2084
|
+
error,
|
|
2085
|
+
dynamic: tool3.type === "dynamic"
|
|
2086
|
+
};
|
|
2087
|
+
}
|
|
2088
|
+
try {
|
|
2089
|
+
span.setAttributes(
|
|
2090
|
+
selectTelemetryAttributes({
|
|
2091
|
+
telemetry,
|
|
2092
|
+
attributes: {
|
|
2093
|
+
"ai.toolCall.result": {
|
|
2094
|
+
output: () => JSON.stringify(output)
|
|
2095
|
+
}
|
|
2096
|
+
}
|
|
2097
|
+
})
|
|
2098
|
+
);
|
|
2099
|
+
} catch (ignored) {
|
|
2100
|
+
}
|
|
2101
|
+
return {
|
|
2102
|
+
type: "tool-result",
|
|
2103
|
+
toolCallId,
|
|
2104
|
+
toolName,
|
|
2105
|
+
input,
|
|
2106
|
+
output,
|
|
2107
|
+
dynamic: tool3.type === "dynamic"
|
|
2108
|
+
};
|
|
2109
|
+
}
|
|
2110
|
+
});
|
|
2111
|
+
}
|
|
2112
|
+
|
|
1865
2113
|
// src/generate-text/extract-text-content.ts
|
|
1866
2114
|
function extractTextContent(content) {
|
|
1867
2115
|
const parts = content.filter(
|
|
@@ -1874,7 +2122,7 @@ function extractTextContent(content) {
|
|
|
1874
2122
|
}
|
|
1875
2123
|
|
|
1876
2124
|
// src/generate-text/generated-file.ts
|
|
1877
|
-
var
|
|
2125
|
+
var import_provider_utils9 = require("@ai-sdk/provider-utils");
|
|
1878
2126
|
var DefaultGeneratedFile = class {
|
|
1879
2127
|
constructor({
|
|
1880
2128
|
data,
|
|
@@ -1888,14 +2136,14 @@ var DefaultGeneratedFile = class {
|
|
|
1888
2136
|
// lazy conversion with caching to avoid unnecessary conversion overhead:
|
|
1889
2137
|
get base64() {
|
|
1890
2138
|
if (this.base64Data == null) {
|
|
1891
|
-
this.base64Data = (0,
|
|
2139
|
+
this.base64Data = (0, import_provider_utils9.convertUint8ArrayToBase64)(this.uint8ArrayData);
|
|
1892
2140
|
}
|
|
1893
2141
|
return this.base64Data;
|
|
1894
2142
|
}
|
|
1895
2143
|
// lazy conversion with caching to avoid unnecessary conversion overhead:
|
|
1896
2144
|
get uint8Array() {
|
|
1897
2145
|
if (this.uint8ArrayData == null) {
|
|
1898
|
-
this.uint8ArrayData = (0,
|
|
2146
|
+
this.uint8ArrayData = (0, import_provider_utils9.convertBase64ToUint8Array)(this.base64Data);
|
|
1899
2147
|
}
|
|
1900
2148
|
return this.uint8ArrayData;
|
|
1901
2149
|
}
|
|
@@ -1907,8 +2155,28 @@ var DefaultGeneratedFileWithType = class extends DefaultGeneratedFile {
|
|
|
1907
2155
|
}
|
|
1908
2156
|
};
|
|
1909
2157
|
|
|
2158
|
+
// src/generate-text/is-approval-needed.ts
|
|
2159
|
+
async function isApprovalNeeded({
|
|
2160
|
+
tool: tool3,
|
|
2161
|
+
toolCall,
|
|
2162
|
+
messages,
|
|
2163
|
+
experimental_context
|
|
2164
|
+
}) {
|
|
2165
|
+
if (tool3.needsApproval == null) {
|
|
2166
|
+
return false;
|
|
2167
|
+
}
|
|
2168
|
+
if (typeof tool3.needsApproval === "boolean") {
|
|
2169
|
+
return tool3.needsApproval;
|
|
2170
|
+
}
|
|
2171
|
+
return await tool3.needsApproval(toolCall.input, {
|
|
2172
|
+
toolCallId: toolCall.toolCallId,
|
|
2173
|
+
messages,
|
|
2174
|
+
experimental_context
|
|
2175
|
+
});
|
|
2176
|
+
}
|
|
2177
|
+
|
|
1910
2178
|
// src/generate-text/parse-tool-call.ts
|
|
1911
|
-
var
|
|
2179
|
+
var import_provider_utils10 = require("@ai-sdk/provider-utils");
|
|
1912
2180
|
async function parseToolCall({
|
|
1913
2181
|
toolCall,
|
|
1914
2182
|
tools,
|
|
@@ -1933,7 +2201,7 @@ async function parseToolCall({
|
|
|
1933
2201
|
tools,
|
|
1934
2202
|
inputSchema: ({ toolName }) => {
|
|
1935
2203
|
const { inputSchema } = tools[toolName];
|
|
1936
|
-
return (0,
|
|
2204
|
+
return (0, import_provider_utils10.asSchema)(inputSchema).jsonSchema;
|
|
1937
2205
|
},
|
|
1938
2206
|
system,
|
|
1939
2207
|
messages,
|
|
@@ -1951,7 +2219,7 @@ async function parseToolCall({
|
|
|
1951
2219
|
return await doParseToolCall({ toolCall: repairedToolCall, tools });
|
|
1952
2220
|
}
|
|
1953
2221
|
} catch (error) {
|
|
1954
|
-
const parsedInput = await (0,
|
|
2222
|
+
const parsedInput = await (0, import_provider_utils10.safeParseJSON)({ text: toolCall.input });
|
|
1955
2223
|
const input = parsedInput.success ? parsedInput.value : toolCall.input;
|
|
1956
2224
|
return {
|
|
1957
2225
|
type: "tool-call",
|
|
@@ -1976,8 +2244,8 @@ async function doParseToolCall({
|
|
|
1976
2244
|
availableTools: Object.keys(tools)
|
|
1977
2245
|
});
|
|
1978
2246
|
}
|
|
1979
|
-
const schema = (0,
|
|
1980
|
-
const parseResult = toolCall.input.trim() === "" ? await (0,
|
|
2247
|
+
const schema = (0, import_provider_utils10.asSchema)(tool3.inputSchema);
|
|
2248
|
+
const parseResult = toolCall.input.trim() === "" ? await (0, import_provider_utils10.safeValidateTypes)({ value: {}, schema }) : await (0, import_provider_utils10.safeParseJSON)({ text: toolCall.input, schema });
|
|
1981
2249
|
if (parseResult.success === false) {
|
|
1982
2250
|
throw new InvalidToolInputError({
|
|
1983
2251
|
toolName,
|
|
@@ -2084,27 +2352,6 @@ async function isStopConditionMet({
|
|
|
2084
2352
|
return (await Promise.all(stopConditions.map((condition) => condition({ steps })))).some((result) => result);
|
|
2085
2353
|
}
|
|
2086
2354
|
|
|
2087
|
-
// src/prompt/create-tool-model-output.ts
|
|
2088
|
-
var import_provider22 = require("@ai-sdk/provider");
|
|
2089
|
-
function createToolModelOutput({
|
|
2090
|
-
output,
|
|
2091
|
-
tool: tool3,
|
|
2092
|
-
errorMode
|
|
2093
|
-
}) {
|
|
2094
|
-
if (errorMode === "text") {
|
|
2095
|
-
return { type: "error-text", value: (0, import_provider22.getErrorMessage)(output) };
|
|
2096
|
-
} else if (errorMode === "json") {
|
|
2097
|
-
return { type: "error-json", value: toJSONValue(output) };
|
|
2098
|
-
}
|
|
2099
|
-
if (tool3 == null ? void 0 : tool3.toModelOutput) {
|
|
2100
|
-
return tool3.toModelOutput(output);
|
|
2101
|
-
}
|
|
2102
|
-
return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: toJSONValue(output) };
|
|
2103
|
-
}
|
|
2104
|
-
function toJSONValue(value) {
|
|
2105
|
-
return value === void 0 ? null : value;
|
|
2106
|
-
}
|
|
2107
|
-
|
|
2108
2355
|
// src/generate-text/to-response-messages.ts
|
|
2109
2356
|
function toResponseMessages({
|
|
2110
2357
|
content: inputContent,
|
|
@@ -2168,6 +2415,12 @@ function toResponseMessages({
|
|
|
2168
2415
|
}),
|
|
2169
2416
|
providerOptions: part.providerMetadata
|
|
2170
2417
|
};
|
|
2418
|
+
case "tool-approval-request":
|
|
2419
|
+
return {
|
|
2420
|
+
type: "tool-approval-request",
|
|
2421
|
+
approvalId: part.approvalId,
|
|
2422
|
+
toolCallId: part.toolCall.toolCallId
|
|
2423
|
+
};
|
|
2171
2424
|
}
|
|
2172
2425
|
});
|
|
2173
2426
|
if (content.length > 0) {
|
|
@@ -2196,7 +2449,7 @@ function toResponseMessages({
|
|
|
2196
2449
|
}
|
|
2197
2450
|
|
|
2198
2451
|
// src/generate-text/generate-text.ts
|
|
2199
|
-
var originalGenerateId = (0,
|
|
2452
|
+
var originalGenerateId = (0, import_provider_utils11.createIdGenerator)({
|
|
2200
2453
|
prefix: "aitxt",
|
|
2201
2454
|
size: 24
|
|
2202
2455
|
});
|
|
@@ -2222,10 +2475,11 @@ async function generateText({
|
|
|
2222
2475
|
experimental_download: download2,
|
|
2223
2476
|
experimental_context,
|
|
2224
2477
|
_internal: {
|
|
2225
|
-
generateId:
|
|
2478
|
+
generateId: generateId2 = originalGenerateId,
|
|
2226
2479
|
currentDate = () => /* @__PURE__ */ new Date()
|
|
2227
2480
|
} = {},
|
|
2228
2481
|
onStepFinish,
|
|
2482
|
+
onFinish,
|
|
2229
2483
|
...settings
|
|
2230
2484
|
}) {
|
|
2231
2485
|
const model = resolveLanguageModel(modelArg);
|
|
@@ -2235,7 +2489,7 @@ async function generateText({
|
|
|
2235
2489
|
abortSignal
|
|
2236
2490
|
});
|
|
2237
2491
|
const callSettings = prepareCallSettings(settings);
|
|
2238
|
-
const headersWithUserAgent = (0,
|
|
2492
|
+
const headersWithUserAgent = (0, import_provider_utils11.withUserAgentSuffix)(
|
|
2239
2493
|
headers != null ? headers : {},
|
|
2240
2494
|
`ai/${VERSION}`
|
|
2241
2495
|
);
|
|
@@ -2274,17 +2528,55 @@ async function generateText({
|
|
|
2274
2528
|
tracer,
|
|
2275
2529
|
fn: async (span) => {
|
|
2276
2530
|
var _a17, _b, _c, _d, _e, _f, _g;
|
|
2531
|
+
const initialMessages = initialPrompt.messages;
|
|
2532
|
+
const responseMessages = [];
|
|
2533
|
+
const { approvedToolApprovals, deniedToolApprovals } = collectToolApprovals({ messages: initialMessages });
|
|
2534
|
+
if (deniedToolApprovals.length > 0 || approvedToolApprovals.length > 0) {
|
|
2535
|
+
const toolOutputs = await executeTools({
|
|
2536
|
+
toolCalls: approvedToolApprovals.map(
|
|
2537
|
+
(toolApproval) => toolApproval.toolCall
|
|
2538
|
+
),
|
|
2539
|
+
tools,
|
|
2540
|
+
tracer,
|
|
2541
|
+
telemetry,
|
|
2542
|
+
messages: initialMessages,
|
|
2543
|
+
abortSignal,
|
|
2544
|
+
experimental_context
|
|
2545
|
+
});
|
|
2546
|
+
responseMessages.push({
|
|
2547
|
+
role: "tool",
|
|
2548
|
+
content: [
|
|
2549
|
+
// add regular tool results for approved tool calls:
|
|
2550
|
+
...toolOutputs.map((output2) => ({
|
|
2551
|
+
type: "tool-result",
|
|
2552
|
+
toolCallId: output2.toolCallId,
|
|
2553
|
+
toolName: output2.toolName,
|
|
2554
|
+
output: createToolModelOutput({
|
|
2555
|
+
tool: tools == null ? void 0 : tools[output2.toolName],
|
|
2556
|
+
output: output2.type === "tool-result" ? output2.output : output2.error,
|
|
2557
|
+
errorMode: output2.type === "tool-error" ? "json" : "none"
|
|
2558
|
+
})
|
|
2559
|
+
})),
|
|
2560
|
+
// add execution denied tool results for denied tool approvals:
|
|
2561
|
+
...deniedToolApprovals.map((toolApproval) => ({
|
|
2562
|
+
type: "tool-result",
|
|
2563
|
+
toolCallId: toolApproval.toolCall.toolCallId,
|
|
2564
|
+
toolName: toolApproval.toolCall.toolName,
|
|
2565
|
+
output: {
|
|
2566
|
+
type: "execution-denied",
|
|
2567
|
+
reason: toolApproval.approvalResponse.reason
|
|
2568
|
+
}
|
|
2569
|
+
}))
|
|
2570
|
+
]
|
|
2571
|
+
});
|
|
2572
|
+
}
|
|
2277
2573
|
const callSettings2 = prepareCallSettings(settings);
|
|
2278
2574
|
let currentModelResponse;
|
|
2279
2575
|
let clientToolCalls = [];
|
|
2280
2576
|
let clientToolOutputs = [];
|
|
2281
|
-
const responseMessages = [];
|
|
2282
2577
|
const steps = [];
|
|
2283
2578
|
do {
|
|
2284
|
-
const stepInputMessages = [
|
|
2285
|
-
...initialPrompt.messages,
|
|
2286
|
-
...responseMessages
|
|
2287
|
-
];
|
|
2579
|
+
const stepInputMessages = [...initialMessages, ...responseMessages];
|
|
2288
2580
|
const prepareStepResult = await (prepareStep == null ? void 0 : prepareStep({
|
|
2289
2581
|
model,
|
|
2290
2582
|
steps,
|
|
@@ -2360,7 +2652,7 @@ async function generateText({
|
|
|
2360
2652
|
headers: headersWithUserAgent
|
|
2361
2653
|
});
|
|
2362
2654
|
const responseData = {
|
|
2363
|
-
id: (_b2 = (_a19 = result.response) == null ? void 0 : _a19.id) != null ? _b2 :
|
|
2655
|
+
id: (_b2 = (_a19 = result.response) == null ? void 0 : _a19.id) != null ? _b2 : generateId2(),
|
|
2364
2656
|
timestamp: (_d2 = (_c2 = result.response) == null ? void 0 : _c2.timestamp) != null ? _d2 : currentDate(),
|
|
2365
2657
|
modelId: (_f2 = (_e2 = result.response) == null ? void 0 : _e2.modelId) != null ? _f2 : stepModel.modelId,
|
|
2366
2658
|
headers: (_g2 = result.response) == null ? void 0 : _g2.headers,
|
|
@@ -2416,6 +2708,7 @@ async function generateText({
|
|
|
2416
2708
|
})
|
|
2417
2709
|
)
|
|
2418
2710
|
);
|
|
2711
|
+
const toolApprovalRequests = {};
|
|
2419
2712
|
for (const toolCall of stepToolCalls) {
|
|
2420
2713
|
if (toolCall.invalid) {
|
|
2421
2714
|
continue;
|
|
@@ -2430,6 +2723,18 @@ async function generateText({
|
|
|
2430
2723
|
experimental_context
|
|
2431
2724
|
});
|
|
2432
2725
|
}
|
|
2726
|
+
if (await isApprovalNeeded({
|
|
2727
|
+
tool: tool3,
|
|
2728
|
+
toolCall,
|
|
2729
|
+
messages: stepInputMessages,
|
|
2730
|
+
experimental_context
|
|
2731
|
+
})) {
|
|
2732
|
+
toolApprovalRequests[toolCall.toolCallId] = {
|
|
2733
|
+
type: "tool-approval-request",
|
|
2734
|
+
approvalId: generateId2(),
|
|
2735
|
+
toolCall
|
|
2736
|
+
};
|
|
2737
|
+
}
|
|
2433
2738
|
}
|
|
2434
2739
|
const invalidToolCalls = stepToolCalls.filter(
|
|
2435
2740
|
(toolCall) => toolCall.invalid && toolCall.dynamic
|
|
@@ -2441,7 +2746,7 @@ async function generateText({
|
|
|
2441
2746
|
toolCallId: toolCall.toolCallId,
|
|
2442
2747
|
toolName: toolCall.toolName,
|
|
2443
2748
|
input: toolCall.input,
|
|
2444
|
-
error: (0,
|
|
2749
|
+
error: (0, import_provider_utils11.getErrorMessage)(toolCall.error),
|
|
2445
2750
|
dynamic: true
|
|
2446
2751
|
});
|
|
2447
2752
|
}
|
|
@@ -2452,7 +2757,7 @@ async function generateText({
|
|
|
2452
2757
|
clientToolOutputs.push(
|
|
2453
2758
|
...await executeTools({
|
|
2454
2759
|
toolCalls: clientToolCalls.filter(
|
|
2455
|
-
(toolCall) => !toolCall.invalid
|
|
2760
|
+
(toolCall) => !toolCall.invalid && toolApprovalRequests[toolCall.toolCallId] == null
|
|
2456
2761
|
),
|
|
2457
2762
|
tools,
|
|
2458
2763
|
tracer,
|
|
@@ -2466,7 +2771,8 @@ async function generateText({
|
|
|
2466
2771
|
const stepContent = asContent({
|
|
2467
2772
|
content: currentModelResponse.content,
|
|
2468
2773
|
toolCalls: stepToolCalls,
|
|
2469
|
-
toolOutputs: clientToolOutputs
|
|
2774
|
+
toolOutputs: clientToolOutputs,
|
|
2775
|
+
toolApprovalRequests: Object.values(toolApprovalRequests)
|
|
2470
2776
|
});
|
|
2471
2777
|
responseMessages.push(
|
|
2472
2778
|
...toResponseMessages({
|
|
@@ -2520,8 +2826,43 @@ async function generateText({
|
|
|
2520
2826
|
})
|
|
2521
2827
|
);
|
|
2522
2828
|
const lastStep = steps[steps.length - 1];
|
|
2829
|
+
const totalUsage = steps.reduce(
|
|
2830
|
+
(totalUsage2, step) => {
|
|
2831
|
+
return addLanguageModelUsage(totalUsage2, step.usage);
|
|
2832
|
+
},
|
|
2833
|
+
{
|
|
2834
|
+
inputTokens: void 0,
|
|
2835
|
+
outputTokens: void 0,
|
|
2836
|
+
totalTokens: void 0,
|
|
2837
|
+
reasoningTokens: void 0,
|
|
2838
|
+
cachedInputTokens: void 0
|
|
2839
|
+
}
|
|
2840
|
+
);
|
|
2841
|
+
await (onFinish == null ? void 0 : onFinish({
|
|
2842
|
+
finishReason: lastStep.finishReason,
|
|
2843
|
+
usage: lastStep.usage,
|
|
2844
|
+
content: lastStep.content,
|
|
2845
|
+
text: lastStep.text,
|
|
2846
|
+
reasoningText: lastStep.reasoningText,
|
|
2847
|
+
reasoning: lastStep.reasoning,
|
|
2848
|
+
files: lastStep.files,
|
|
2849
|
+
sources: lastStep.sources,
|
|
2850
|
+
toolCalls: lastStep.toolCalls,
|
|
2851
|
+
staticToolCalls: lastStep.staticToolCalls,
|
|
2852
|
+
dynamicToolCalls: lastStep.dynamicToolCalls,
|
|
2853
|
+
toolResults: lastStep.toolResults,
|
|
2854
|
+
staticToolResults: lastStep.staticToolResults,
|
|
2855
|
+
dynamicToolResults: lastStep.dynamicToolResults,
|
|
2856
|
+
request: lastStep.request,
|
|
2857
|
+
response: lastStep.response,
|
|
2858
|
+
warnings: lastStep.warnings,
|
|
2859
|
+
providerMetadata: lastStep.providerMetadata,
|
|
2860
|
+
steps,
|
|
2861
|
+
totalUsage
|
|
2862
|
+
}));
|
|
2523
2863
|
return new DefaultGenerateTextResult({
|
|
2524
2864
|
steps,
|
|
2865
|
+
totalUsage,
|
|
2525
2866
|
resolvedOutput: await (output == null ? void 0 : output.parseOutput(
|
|
2526
2867
|
{ text: lastStep.text },
|
|
2527
2868
|
{
|
|
@@ -2547,81 +2888,17 @@ async function executeTools({
|
|
|
2547
2888
|
experimental_context
|
|
2548
2889
|
}) {
|
|
2549
2890
|
const toolOutputs = await Promise.all(
|
|
2550
|
-
toolCalls.map(
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
}
|
|
2555
|
-
return recordSpan({
|
|
2556
|
-
name: "ai.toolCall",
|
|
2557
|
-
attributes: selectTelemetryAttributes({
|
|
2558
|
-
telemetry,
|
|
2559
|
-
attributes: {
|
|
2560
|
-
...assembleOperationName({
|
|
2561
|
-
operationId: "ai.toolCall",
|
|
2562
|
-
telemetry
|
|
2563
|
-
}),
|
|
2564
|
-
"ai.toolCall.name": toolName,
|
|
2565
|
-
"ai.toolCall.id": toolCallId,
|
|
2566
|
-
"ai.toolCall.args": {
|
|
2567
|
-
output: () => JSON.stringify(input)
|
|
2568
|
-
}
|
|
2569
|
-
}
|
|
2570
|
-
}),
|
|
2891
|
+
toolCalls.map(
|
|
2892
|
+
async (toolCall) => executeToolCall({
|
|
2893
|
+
toolCall,
|
|
2894
|
+
tools,
|
|
2571
2895
|
tracer,
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
toolCallId,
|
|
2579
|
-
messages,
|
|
2580
|
-
abortSignal,
|
|
2581
|
-
experimental_context
|
|
2582
|
-
}
|
|
2583
|
-
});
|
|
2584
|
-
let output;
|
|
2585
|
-
for await (const part of stream) {
|
|
2586
|
-
if (part.type === "final") {
|
|
2587
|
-
output = part.output;
|
|
2588
|
-
}
|
|
2589
|
-
}
|
|
2590
|
-
try {
|
|
2591
|
-
span.setAttributes(
|
|
2592
|
-
selectTelemetryAttributes({
|
|
2593
|
-
telemetry,
|
|
2594
|
-
attributes: {
|
|
2595
|
-
"ai.toolCall.result": {
|
|
2596
|
-
output: () => JSON.stringify(output)
|
|
2597
|
-
}
|
|
2598
|
-
}
|
|
2599
|
-
})
|
|
2600
|
-
);
|
|
2601
|
-
} catch (ignored) {
|
|
2602
|
-
}
|
|
2603
|
-
return {
|
|
2604
|
-
type: "tool-result",
|
|
2605
|
-
toolCallId,
|
|
2606
|
-
toolName,
|
|
2607
|
-
input,
|
|
2608
|
-
output,
|
|
2609
|
-
dynamic: tool3.type === "dynamic"
|
|
2610
|
-
};
|
|
2611
|
-
} catch (error) {
|
|
2612
|
-
recordErrorOnSpan(span, error);
|
|
2613
|
-
return {
|
|
2614
|
-
type: "tool-error",
|
|
2615
|
-
toolCallId,
|
|
2616
|
-
toolName,
|
|
2617
|
-
input,
|
|
2618
|
-
error,
|
|
2619
|
-
dynamic: tool3.type === "dynamic"
|
|
2620
|
-
};
|
|
2621
|
-
}
|
|
2622
|
-
}
|
|
2623
|
-
});
|
|
2624
|
-
})
|
|
2896
|
+
telemetry,
|
|
2897
|
+
messages,
|
|
2898
|
+
abortSignal,
|
|
2899
|
+
experimental_context
|
|
2900
|
+
})
|
|
2901
|
+
)
|
|
2625
2902
|
);
|
|
2626
2903
|
return toolOutputs.filter(
|
|
2627
2904
|
(output) => output != null
|
|
@@ -2631,6 +2908,7 @@ var DefaultGenerateTextResult = class {
|
|
|
2631
2908
|
constructor(options) {
|
|
2632
2909
|
this.steps = options.steps;
|
|
2633
2910
|
this.resolvedOutput = options.resolvedOutput;
|
|
2911
|
+
this.totalUsage = options.totalUsage;
|
|
2634
2912
|
}
|
|
2635
2913
|
get finalStep() {
|
|
2636
2914
|
return this.steps[this.steps.length - 1];
|
|
@@ -2689,20 +2967,6 @@ var DefaultGenerateTextResult = class {
|
|
|
2689
2967
|
get usage() {
|
|
2690
2968
|
return this.finalStep.usage;
|
|
2691
2969
|
}
|
|
2692
|
-
get totalUsage() {
|
|
2693
|
-
return this.steps.reduce(
|
|
2694
|
-
(totalUsage, step) => {
|
|
2695
|
-
return addLanguageModelUsage(totalUsage, step.usage);
|
|
2696
|
-
},
|
|
2697
|
-
{
|
|
2698
|
-
inputTokens: void 0,
|
|
2699
|
-
outputTokens: void 0,
|
|
2700
|
-
totalTokens: void 0,
|
|
2701
|
-
reasoningTokens: void 0,
|
|
2702
|
-
cachedInputTokens: void 0
|
|
2703
|
-
}
|
|
2704
|
-
);
|
|
2705
|
-
}
|
|
2706
2970
|
get experimental_output() {
|
|
2707
2971
|
if (this.resolvedOutput == null) {
|
|
2708
2972
|
throw new NoOutputSpecifiedError();
|
|
@@ -2726,7 +2990,8 @@ function asToolCalls(content) {
|
|
|
2726
2990
|
function asContent({
|
|
2727
2991
|
content,
|
|
2728
2992
|
toolCalls,
|
|
2729
|
-
toolOutputs
|
|
2993
|
+
toolOutputs,
|
|
2994
|
+
toolApprovalRequests
|
|
2730
2995
|
}) {
|
|
2731
2996
|
return [
|
|
2732
2997
|
...content.map((part) => {
|
|
@@ -2776,13 +3041,14 @@ function asContent({
|
|
|
2776
3041
|
}
|
|
2777
3042
|
}
|
|
2778
3043
|
}),
|
|
2779
|
-
...toolOutputs
|
|
3044
|
+
...toolOutputs,
|
|
3045
|
+
...toolApprovalRequests
|
|
2780
3046
|
];
|
|
2781
3047
|
}
|
|
2782
3048
|
|
|
2783
3049
|
// src/generate-text/stream-text.ts
|
|
2784
|
-
var
|
|
2785
|
-
var
|
|
3050
|
+
var import_provider24 = require("@ai-sdk/provider");
|
|
3051
|
+
var import_provider_utils15 = require("@ai-sdk/provider-utils");
|
|
2786
3052
|
|
|
2787
3053
|
// src/util/prepare-headers.ts
|
|
2788
3054
|
function prepareHeaders(headers, defaultHeaders) {
|
|
@@ -2919,7 +3185,7 @@ function getResponseUIMessageId({
|
|
|
2919
3185
|
}
|
|
2920
3186
|
|
|
2921
3187
|
// src/ui/process-ui-message-stream.ts
|
|
2922
|
-
var
|
|
3188
|
+
var import_provider_utils13 = require("@ai-sdk/provider-utils");
|
|
2923
3189
|
|
|
2924
3190
|
// src/ui-message-stream/ui-message-chunks.ts
|
|
2925
3191
|
var import_v47 = require("zod/v4");
|
|
@@ -2975,6 +3241,11 @@ var uiMessageChunkSchema = import_v47.z.union([
|
|
|
2975
3241
|
dynamic: import_v47.z.boolean().optional(),
|
|
2976
3242
|
errorText: import_v47.z.string()
|
|
2977
3243
|
}),
|
|
3244
|
+
import_v47.z.strictObject({
|
|
3245
|
+
type: import_v47.z.literal("tool-approval-request"),
|
|
3246
|
+
approvalId: import_v47.z.string(),
|
|
3247
|
+
toolCallId: import_v47.z.string()
|
|
3248
|
+
}),
|
|
2978
3249
|
import_v47.z.strictObject({
|
|
2979
3250
|
type: import_v47.z.literal("tool-output-available"),
|
|
2980
3251
|
toolCallId: import_v47.z.string(),
|
|
@@ -2990,6 +3261,10 @@ var uiMessageChunkSchema = import_v47.z.union([
|
|
|
2990
3261
|
providerExecuted: import_v47.z.boolean().optional(),
|
|
2991
3262
|
dynamic: import_v47.z.boolean().optional()
|
|
2992
3263
|
}),
|
|
3264
|
+
import_v47.z.strictObject({
|
|
3265
|
+
type: import_v47.z.literal("tool-output-denied"),
|
|
3266
|
+
toolCallId: import_v47.z.string()
|
|
3267
|
+
}),
|
|
2993
3268
|
import_v47.z.strictObject({
|
|
2994
3269
|
type: import_v47.z.literal("reasoning-start"),
|
|
2995
3270
|
id: import_v47.z.string(),
|
|
@@ -3097,7 +3372,7 @@ function mergeObjects(base, overrides) {
|
|
|
3097
3372
|
}
|
|
3098
3373
|
|
|
3099
3374
|
// src/util/parse-partial-json.ts
|
|
3100
|
-
var
|
|
3375
|
+
var import_provider_utils12 = require("@ai-sdk/provider-utils");
|
|
3101
3376
|
|
|
3102
3377
|
// src/util/fix-json.ts
|
|
3103
3378
|
function fixJson(input) {
|
|
@@ -3422,11 +3697,11 @@ async function parsePartialJson(jsonText) {
|
|
|
3422
3697
|
if (jsonText === void 0) {
|
|
3423
3698
|
return { value: void 0, state: "undefined-input" };
|
|
3424
3699
|
}
|
|
3425
|
-
let result = await (0,
|
|
3700
|
+
let result = await (0, import_provider_utils12.safeParseJSON)({ text: jsonText });
|
|
3426
3701
|
if (result.success) {
|
|
3427
3702
|
return { value: result.value, state: "successful-parse" };
|
|
3428
3703
|
}
|
|
3429
|
-
result = await (0,
|
|
3704
|
+
result = await (0, import_provider_utils12.safeParseJSON)({ text: fixJson(jsonText) });
|
|
3430
3705
|
if (result.success) {
|
|
3431
3706
|
return { value: result.value, state: "repaired-parse" };
|
|
3432
3707
|
}
|
|
@@ -3576,7 +3851,7 @@ function processUIMessageStream({
|
|
|
3576
3851
|
if (metadata != null) {
|
|
3577
3852
|
const mergedMetadata = state.message.metadata != null ? mergeObjects(state.message.metadata, metadata) : metadata;
|
|
3578
3853
|
if (messageMetadataSchema != null) {
|
|
3579
|
-
await (0,
|
|
3854
|
+
await (0, import_provider_utils13.validateTypes)({
|
|
3580
3855
|
value: mergedMetadata,
|
|
3581
3856
|
schema: messageMetadataSchema
|
|
3582
3857
|
});
|
|
@@ -3774,6 +4049,21 @@ function processUIMessageStream({
|
|
|
3774
4049
|
write();
|
|
3775
4050
|
break;
|
|
3776
4051
|
}
|
|
4052
|
+
case "tool-approval-request": {
|
|
4053
|
+
const toolInvocation = getToolInvocation(chunk.toolCallId);
|
|
4054
|
+
toolInvocation.state = "approval-requested";
|
|
4055
|
+
toolInvocation.approval = {
|
|
4056
|
+
id: chunk.approvalId
|
|
4057
|
+
};
|
|
4058
|
+
write();
|
|
4059
|
+
break;
|
|
4060
|
+
}
|
|
4061
|
+
case "tool-output-denied": {
|
|
4062
|
+
const toolInvocation = getToolInvocation(chunk.toolCallId);
|
|
4063
|
+
toolInvocation.state = "output-denied";
|
|
4064
|
+
write();
|
|
4065
|
+
break;
|
|
4066
|
+
}
|
|
3777
4067
|
case "tool-output-available": {
|
|
3778
4068
|
if (chunk.dynamic) {
|
|
3779
4069
|
const toolInvocation = getDynamicToolInvocation(
|
|
@@ -3868,7 +4158,7 @@ function processUIMessageStream({
|
|
|
3868
4158
|
default: {
|
|
3869
4159
|
if (isDataUIMessageChunk(chunk)) {
|
|
3870
4160
|
if ((dataPartSchemas == null ? void 0 : dataPartSchemas[chunk.type]) != null) {
|
|
3871
|
-
await (0,
|
|
4161
|
+
await (0, import_provider_utils13.validateTypes)({
|
|
3872
4162
|
value: chunk.data,
|
|
3873
4163
|
schema: dataPartSchemas[chunk.type]
|
|
3874
4164
|
});
|
|
@@ -4125,10 +4415,10 @@ function createStitchableStream() {
|
|
|
4125
4415
|
const { value, done } = await innerStreamReaders[0].read();
|
|
4126
4416
|
if (done) {
|
|
4127
4417
|
innerStreamReaders.shift();
|
|
4128
|
-
if (innerStreamReaders.length
|
|
4129
|
-
await processPull();
|
|
4130
|
-
} else if (isClosed) {
|
|
4418
|
+
if (innerStreamReaders.length === 0 && isClosed) {
|
|
4131
4419
|
controller == null ? void 0 : controller.close();
|
|
4420
|
+
} else {
|
|
4421
|
+
await processPull();
|
|
4132
4422
|
}
|
|
4133
4423
|
} else {
|
|
4134
4424
|
controller == null ? void 0 : controller.enqueue(value);
|
|
@@ -4224,7 +4514,7 @@ function now() {
|
|
|
4224
4514
|
}
|
|
4225
4515
|
|
|
4226
4516
|
// src/generate-text/run-tools-transformation.ts
|
|
4227
|
-
var
|
|
4517
|
+
var import_provider_utils14 = require("@ai-sdk/provider-utils");
|
|
4228
4518
|
function runToolsTransformation({
|
|
4229
4519
|
tools,
|
|
4230
4520
|
generatorStream,
|
|
@@ -4234,7 +4524,8 @@ function runToolsTransformation({
|
|
|
4234
4524
|
messages,
|
|
4235
4525
|
abortSignal,
|
|
4236
4526
|
repairToolCall,
|
|
4237
|
-
experimental_context
|
|
4527
|
+
experimental_context,
|
|
4528
|
+
generateId: generateId2
|
|
4238
4529
|
}) {
|
|
4239
4530
|
let toolResultsStreamController = null;
|
|
4240
4531
|
const toolResultsStream = new ReadableStream({
|
|
@@ -4310,95 +4601,53 @@ function runToolsTransformation({
|
|
|
4310
4601
|
toolCallId: toolCall.toolCallId,
|
|
4311
4602
|
toolName: toolCall.toolName,
|
|
4312
4603
|
input: toolCall.input,
|
|
4313
|
-
error: (0,
|
|
4604
|
+
error: (0, import_provider_utils14.getErrorMessage)(toolCall.error),
|
|
4314
4605
|
dynamic: true
|
|
4315
4606
|
});
|
|
4316
4607
|
break;
|
|
4317
4608
|
}
|
|
4318
4609
|
const tool3 = tools[toolCall.toolName];
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
|
|
4325
|
-
|
|
4326
|
-
|
|
4327
|
-
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4335
|
-
|
|
4336
|
-
|
|
4337
|
-
|
|
4338
|
-
|
|
4339
|
-
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
|
|
4343
|
-
|
|
4344
|
-
|
|
4345
|
-
|
|
4346
|
-
|
|
4347
|
-
|
|
4348
|
-
tracer,
|
|
4349
|
-
|
|
4350
|
-
|
|
4351
|
-
|
|
4352
|
-
|
|
4353
|
-
|
|
4354
|
-
|
|
4355
|
-
options: {
|
|
4356
|
-
toolCallId: toolCall.toolCallId,
|
|
4357
|
-
messages,
|
|
4358
|
-
abortSignal,
|
|
4359
|
-
experimental_context
|
|
4360
|
-
}
|
|
4361
|
-
});
|
|
4362
|
-
for await (const part of stream) {
|
|
4363
|
-
toolResultsStreamController.enqueue({
|
|
4364
|
-
...toolCall,
|
|
4365
|
-
type: "tool-result",
|
|
4366
|
-
output: part.output,
|
|
4367
|
-
...part.type === "preliminary" && {
|
|
4368
|
-
preliminary: true
|
|
4369
|
-
}
|
|
4370
|
-
});
|
|
4371
|
-
if (part.type === "final") {
|
|
4372
|
-
output = part.output;
|
|
4373
|
-
}
|
|
4374
|
-
}
|
|
4375
|
-
} catch (error) {
|
|
4376
|
-
recordErrorOnSpan(span, error);
|
|
4377
|
-
toolResultsStreamController.enqueue({
|
|
4378
|
-
...toolCall,
|
|
4379
|
-
type: "tool-error",
|
|
4380
|
-
error
|
|
4381
|
-
});
|
|
4382
|
-
outstandingToolResults.delete(toolExecutionId);
|
|
4383
|
-
attemptClose();
|
|
4384
|
-
return;
|
|
4385
|
-
}
|
|
4386
|
-
outstandingToolResults.delete(toolExecutionId);
|
|
4387
|
-
attemptClose();
|
|
4388
|
-
try {
|
|
4389
|
-
span.setAttributes(
|
|
4390
|
-
selectTelemetryAttributes({
|
|
4391
|
-
telemetry,
|
|
4392
|
-
attributes: {
|
|
4393
|
-
"ai.toolCall.result": {
|
|
4394
|
-
output: () => JSON.stringify(output)
|
|
4395
|
-
}
|
|
4396
|
-
}
|
|
4397
|
-
})
|
|
4398
|
-
);
|
|
4399
|
-
} catch (ignored) {
|
|
4400
|
-
}
|
|
4610
|
+
if (tool3.onInputAvailable != null) {
|
|
4611
|
+
await tool3.onInputAvailable({
|
|
4612
|
+
input: toolCall.input,
|
|
4613
|
+
toolCallId: toolCall.toolCallId,
|
|
4614
|
+
messages,
|
|
4615
|
+
abortSignal,
|
|
4616
|
+
experimental_context
|
|
4617
|
+
});
|
|
4618
|
+
}
|
|
4619
|
+
if (await isApprovalNeeded({
|
|
4620
|
+
tool: tool3,
|
|
4621
|
+
toolCall,
|
|
4622
|
+
messages,
|
|
4623
|
+
experimental_context
|
|
4624
|
+
})) {
|
|
4625
|
+
toolResultsStreamController.enqueue({
|
|
4626
|
+
type: "tool-approval-request",
|
|
4627
|
+
approvalId: generateId2(),
|
|
4628
|
+
toolCall
|
|
4629
|
+
});
|
|
4630
|
+
break;
|
|
4631
|
+
}
|
|
4632
|
+
toolInputs.set(toolCall.toolCallId, toolCall.input);
|
|
4633
|
+
if (tool3.execute != null && toolCall.providerExecuted !== true) {
|
|
4634
|
+
const toolExecutionId = generateId2();
|
|
4635
|
+
outstandingToolResults.add(toolExecutionId);
|
|
4636
|
+
executeToolCall({
|
|
4637
|
+
toolCall,
|
|
4638
|
+
tools,
|
|
4639
|
+
tracer,
|
|
4640
|
+
telemetry,
|
|
4641
|
+
messages,
|
|
4642
|
+
abortSignal,
|
|
4643
|
+
experimental_context,
|
|
4644
|
+
onPreliminaryToolResult: (result) => {
|
|
4645
|
+
toolResultsStreamController.enqueue(result);
|
|
4401
4646
|
}
|
|
4647
|
+
}).then((result) => {
|
|
4648
|
+
toolResultsStreamController.enqueue(result);
|
|
4649
|
+
outstandingToolResults.delete(toolExecutionId);
|
|
4650
|
+
attemptClose();
|
|
4402
4651
|
});
|
|
4403
4652
|
}
|
|
4404
4653
|
} catch (error) {
|
|
@@ -4468,7 +4717,7 @@ function runToolsTransformation({
|
|
|
4468
4717
|
}
|
|
4469
4718
|
|
|
4470
4719
|
// src/generate-text/stream-text.ts
|
|
4471
|
-
var originalGenerateId2 = (0,
|
|
4720
|
+
var originalGenerateId2 = (0, import_provider_utils15.createIdGenerator)({
|
|
4472
4721
|
prefix: "aitxt",
|
|
4473
4722
|
size: 24
|
|
4474
4723
|
});
|
|
@@ -4503,7 +4752,7 @@ function streamText({
|
|
|
4503
4752
|
experimental_context,
|
|
4504
4753
|
_internal: {
|
|
4505
4754
|
now: now2 = now,
|
|
4506
|
-
generateId:
|
|
4755
|
+
generateId: generateId2 = originalGenerateId2,
|
|
4507
4756
|
currentDate = () => /* @__PURE__ */ new Date()
|
|
4508
4757
|
} = {},
|
|
4509
4758
|
...settings
|
|
@@ -4535,7 +4784,7 @@ function streamText({
|
|
|
4535
4784
|
onStepFinish,
|
|
4536
4785
|
now: now2,
|
|
4537
4786
|
currentDate,
|
|
4538
|
-
generateId:
|
|
4787
|
+
generateId: generateId2,
|
|
4539
4788
|
experimental_context,
|
|
4540
4789
|
download: download2
|
|
4541
4790
|
});
|
|
@@ -4628,7 +4877,7 @@ var DefaultStreamTextResult = class {
|
|
|
4628
4877
|
includeRawChunks,
|
|
4629
4878
|
now: now2,
|
|
4630
4879
|
currentDate,
|
|
4631
|
-
generateId:
|
|
4880
|
+
generateId: generateId2,
|
|
4632
4881
|
onChunk,
|
|
4633
4882
|
onError,
|
|
4634
4883
|
onFinish,
|
|
@@ -4656,7 +4905,7 @@ var DefaultStreamTextResult = class {
|
|
|
4656
4905
|
let activeReasoningContent = {};
|
|
4657
4906
|
const eventProcessor = new TransformStream({
|
|
4658
4907
|
async transform(chunk, controller) {
|
|
4659
|
-
var _a17, _b, _c;
|
|
4908
|
+
var _a17, _b, _c, _d;
|
|
4660
4909
|
controller.enqueue(chunk);
|
|
4661
4910
|
const { part } = chunk;
|
|
4662
4911
|
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") {
|
|
@@ -4689,6 +4938,18 @@ var DefaultStreamTextResult = class {
|
|
|
4689
4938
|
activeText.providerMetadata = (_a17 = part.providerMetadata) != null ? _a17 : activeText.providerMetadata;
|
|
4690
4939
|
}
|
|
4691
4940
|
if (part.type === "text-end") {
|
|
4941
|
+
const activeText = activeTextContent[part.id];
|
|
4942
|
+
if (activeText == null) {
|
|
4943
|
+
controller.enqueue({
|
|
4944
|
+
part: {
|
|
4945
|
+
type: "error",
|
|
4946
|
+
error: `text part ${part.id} not found`
|
|
4947
|
+
},
|
|
4948
|
+
partialOutput: void 0
|
|
4949
|
+
});
|
|
4950
|
+
return;
|
|
4951
|
+
}
|
|
4952
|
+
activeText.providerMetadata = (_b = part.providerMetadata) != null ? _b : activeText.providerMetadata;
|
|
4692
4953
|
delete activeTextContent[part.id];
|
|
4693
4954
|
}
|
|
4694
4955
|
if (part.type === "reasoning-start") {
|
|
@@ -4712,7 +4973,7 @@ var DefaultStreamTextResult = class {
|
|
|
4712
4973
|
return;
|
|
4713
4974
|
}
|
|
4714
4975
|
activeReasoning.text += part.text;
|
|
4715
|
-
activeReasoning.providerMetadata = (
|
|
4976
|
+
activeReasoning.providerMetadata = (_c = part.providerMetadata) != null ? _c : activeReasoning.providerMetadata;
|
|
4716
4977
|
}
|
|
4717
4978
|
if (part.type === "reasoning-end") {
|
|
4718
4979
|
const activeReasoning = activeReasoningContent[part.id];
|
|
@@ -4726,7 +4987,7 @@ var DefaultStreamTextResult = class {
|
|
|
4726
4987
|
});
|
|
4727
4988
|
return;
|
|
4728
4989
|
}
|
|
4729
|
-
activeReasoning.providerMetadata = (
|
|
4990
|
+
activeReasoning.providerMetadata = (_d = part.providerMetadata) != null ? _d : activeReasoning.providerMetadata;
|
|
4730
4991
|
delete activeReasoningContent[part.id];
|
|
4731
4992
|
}
|
|
4732
4993
|
if (part.type === "file") {
|
|
@@ -4741,10 +5002,16 @@ var DefaultStreamTextResult = class {
|
|
|
4741
5002
|
if (part.type === "tool-result" && !part.preliminary) {
|
|
4742
5003
|
recordedContent.push(part);
|
|
4743
5004
|
}
|
|
5005
|
+
if (part.type === "tool-approval-request") {
|
|
5006
|
+
recordedContent.push(part);
|
|
5007
|
+
}
|
|
4744
5008
|
if (part.type === "tool-error") {
|
|
4745
5009
|
recordedContent.push(part);
|
|
4746
5010
|
}
|
|
4747
5011
|
if (part.type === "start-step") {
|
|
5012
|
+
recordedContent = [];
|
|
5013
|
+
activeReasoningContent = {};
|
|
5014
|
+
activeTextContent = {};
|
|
4748
5015
|
recordedRequest = part.request;
|
|
4749
5016
|
recordedWarnings = part.warnings;
|
|
4750
5017
|
}
|
|
@@ -4768,9 +5035,6 @@ var DefaultStreamTextResult = class {
|
|
|
4768
5035
|
await (onStepFinish == null ? void 0 : onStepFinish(currentStepResult));
|
|
4769
5036
|
logWarnings(recordedWarnings);
|
|
4770
5037
|
recordedSteps.push(currentStepResult);
|
|
4771
|
-
recordedContent = [];
|
|
4772
|
-
activeReasoningContent = {};
|
|
4773
|
-
activeTextContent = {};
|
|
4774
5038
|
recordedResponseMessages.push(...stepMessages);
|
|
4775
5039
|
stepFinish.resolve();
|
|
4776
5040
|
}
|
|
@@ -4878,7 +5142,7 @@ var DefaultStreamTextResult = class {
|
|
|
4878
5142
|
}
|
|
4879
5143
|
controller.enqueue(value);
|
|
4880
5144
|
} catch (error) {
|
|
4881
|
-
if ((0,
|
|
5145
|
+
if ((0, import_provider_utils15.isAbortError)(error) && (abortSignal == null ? void 0 : abortSignal.aborted)) {
|
|
4882
5146
|
abort();
|
|
4883
5147
|
} else {
|
|
4884
5148
|
controller.error(error);
|
|
@@ -4930,6 +5194,82 @@ var DefaultStreamTextResult = class {
|
|
|
4930
5194
|
endWhenDone: false,
|
|
4931
5195
|
fn: async (rootSpanArg) => {
|
|
4932
5196
|
rootSpan = rootSpanArg;
|
|
5197
|
+
const initialPrompt = await standardizePrompt({
|
|
5198
|
+
system,
|
|
5199
|
+
prompt,
|
|
5200
|
+
messages
|
|
5201
|
+
});
|
|
5202
|
+
const initialMessages = initialPrompt.messages;
|
|
5203
|
+
const initialResponseMessages = [];
|
|
5204
|
+
const { approvedToolApprovals, deniedToolApprovals } = collectToolApprovals({ messages: initialMessages });
|
|
5205
|
+
if (deniedToolApprovals.length > 0 || approvedToolApprovals.length > 0) {
|
|
5206
|
+
let toolExecutionStepStreamController;
|
|
5207
|
+
const toolExecutionStepStream = new ReadableStream({
|
|
5208
|
+
start(controller) {
|
|
5209
|
+
toolExecutionStepStreamController = controller;
|
|
5210
|
+
}
|
|
5211
|
+
});
|
|
5212
|
+
self.addStream(toolExecutionStepStream);
|
|
5213
|
+
try {
|
|
5214
|
+
for (const toolApproval of deniedToolApprovals) {
|
|
5215
|
+
toolExecutionStepStreamController == null ? void 0 : toolExecutionStepStreamController.enqueue({
|
|
5216
|
+
type: "tool-output-denied",
|
|
5217
|
+
toolCallId: toolApproval.toolCall.toolCallId,
|
|
5218
|
+
toolName: toolApproval.toolCall.toolName
|
|
5219
|
+
});
|
|
5220
|
+
}
|
|
5221
|
+
const toolOutputs = [];
|
|
5222
|
+
await Promise.all(
|
|
5223
|
+
approvedToolApprovals.map(async (toolApproval) => {
|
|
5224
|
+
const result = await executeToolCall({
|
|
5225
|
+
toolCall: toolApproval.toolCall,
|
|
5226
|
+
tools,
|
|
5227
|
+
tracer,
|
|
5228
|
+
telemetry,
|
|
5229
|
+
messages: initialMessages,
|
|
5230
|
+
abortSignal,
|
|
5231
|
+
experimental_context,
|
|
5232
|
+
onPreliminaryToolResult: (result2) => {
|
|
5233
|
+
toolExecutionStepStreamController == null ? void 0 : toolExecutionStepStreamController.enqueue(result2);
|
|
5234
|
+
}
|
|
5235
|
+
});
|
|
5236
|
+
if (result != null) {
|
|
5237
|
+
toolExecutionStepStreamController == null ? void 0 : toolExecutionStepStreamController.enqueue(result);
|
|
5238
|
+
toolOutputs.push(result);
|
|
5239
|
+
}
|
|
5240
|
+
})
|
|
5241
|
+
);
|
|
5242
|
+
initialResponseMessages.push({
|
|
5243
|
+
role: "tool",
|
|
5244
|
+
content: [
|
|
5245
|
+
// add regular tool results for approved tool calls:
|
|
5246
|
+
...toolOutputs.map((output2) => ({
|
|
5247
|
+
type: "tool-result",
|
|
5248
|
+
toolCallId: output2.toolCallId,
|
|
5249
|
+
toolName: output2.toolName,
|
|
5250
|
+
output: createToolModelOutput({
|
|
5251
|
+
tool: tools == null ? void 0 : tools[output2.toolName],
|
|
5252
|
+
output: output2.type === "tool-result" ? output2.output : output2.error,
|
|
5253
|
+
errorMode: output2.type === "tool-error" ? "json" : "none"
|
|
5254
|
+
})
|
|
5255
|
+
})),
|
|
5256
|
+
// add execution denied tool results for denied tool approvals:
|
|
5257
|
+
...deniedToolApprovals.map((toolApproval) => ({
|
|
5258
|
+
type: "tool-result",
|
|
5259
|
+
toolCallId: toolApproval.toolCall.toolCallId,
|
|
5260
|
+
toolName: toolApproval.toolCall.toolName,
|
|
5261
|
+
output: {
|
|
5262
|
+
type: "execution-denied",
|
|
5263
|
+
reason: toolApproval.approvalResponse.reason
|
|
5264
|
+
}
|
|
5265
|
+
}))
|
|
5266
|
+
]
|
|
5267
|
+
});
|
|
5268
|
+
} finally {
|
|
5269
|
+
toolExecutionStepStreamController == null ? void 0 : toolExecutionStepStreamController.close();
|
|
5270
|
+
}
|
|
5271
|
+
}
|
|
5272
|
+
recordedResponseMessages.push(...initialResponseMessages);
|
|
4933
5273
|
async function streamStep({
|
|
4934
5274
|
currentStep,
|
|
4935
5275
|
responseMessages,
|
|
@@ -4938,15 +5278,7 @@ var DefaultStreamTextResult = class {
|
|
|
4938
5278
|
var _a17, _b, _c, _d, _e;
|
|
4939
5279
|
const includeRawChunks2 = self.includeRawChunks;
|
|
4940
5280
|
stepFinish = new DelayedPromise();
|
|
4941
|
-
const
|
|
4942
|
-
system,
|
|
4943
|
-
prompt,
|
|
4944
|
-
messages
|
|
4945
|
-
});
|
|
4946
|
-
const stepInputMessages = [
|
|
4947
|
-
...initialPrompt.messages,
|
|
4948
|
-
...responseMessages
|
|
4949
|
-
];
|
|
5281
|
+
const stepInputMessages = [...initialMessages, ...responseMessages];
|
|
4950
5282
|
const prepareStepResult = await (prepareStep == null ? void 0 : prepareStep({
|
|
4951
5283
|
model,
|
|
4952
5284
|
steps: recordedSteps,
|
|
@@ -5012,24 +5344,22 @@ var DefaultStreamTextResult = class {
|
|
|
5012
5344
|
}),
|
|
5013
5345
|
tracer,
|
|
5014
5346
|
endWhenDone: false,
|
|
5015
|
-
fn: async (doStreamSpan2) => {
|
|
5016
|
-
|
|
5017
|
-
|
|
5018
|
-
|
|
5019
|
-
|
|
5020
|
-
|
|
5021
|
-
|
|
5022
|
-
|
|
5023
|
-
|
|
5024
|
-
|
|
5025
|
-
|
|
5026
|
-
|
|
5027
|
-
|
|
5028
|
-
|
|
5029
|
-
|
|
5030
|
-
|
|
5031
|
-
};
|
|
5032
|
-
}
|
|
5347
|
+
fn: async (doStreamSpan2) => ({
|
|
5348
|
+
startTimestampMs: now2(),
|
|
5349
|
+
// get before the call
|
|
5350
|
+
doStreamSpan: doStreamSpan2,
|
|
5351
|
+
result: await stepModel.doStream({
|
|
5352
|
+
...callSettings,
|
|
5353
|
+
tools: stepTools,
|
|
5354
|
+
toolChoice: stepToolChoice,
|
|
5355
|
+
responseFormat: output == null ? void 0 : output.responseFormat,
|
|
5356
|
+
prompt: promptMessages,
|
|
5357
|
+
providerOptions,
|
|
5358
|
+
abortSignal,
|
|
5359
|
+
headers,
|
|
5360
|
+
includeRawChunks: includeRawChunks2
|
|
5361
|
+
})
|
|
5362
|
+
})
|
|
5033
5363
|
})
|
|
5034
5364
|
);
|
|
5035
5365
|
const streamWithToolResults = runToolsTransformation({
|
|
@@ -5041,7 +5371,8 @@ var DefaultStreamTextResult = class {
|
|
|
5041
5371
|
messages: stepInputMessages,
|
|
5042
5372
|
repairToolCall,
|
|
5043
5373
|
abortSignal,
|
|
5044
|
-
experimental_context
|
|
5374
|
+
experimental_context,
|
|
5375
|
+
generateId: generateId2
|
|
5045
5376
|
});
|
|
5046
5377
|
const stepRequest = request != null ? request : {};
|
|
5047
5378
|
const stepToolCalls = [];
|
|
@@ -5057,7 +5388,7 @@ var DefaultStreamTextResult = class {
|
|
|
5057
5388
|
let stepProviderMetadata;
|
|
5058
5389
|
let stepFirstChunk = true;
|
|
5059
5390
|
let stepResponse = {
|
|
5060
|
-
id:
|
|
5391
|
+
id: generateId2(),
|
|
5061
5392
|
timestamp: currentDate(),
|
|
5062
5393
|
modelId: model.modelId
|
|
5063
5394
|
};
|
|
@@ -5088,6 +5419,7 @@ var DefaultStreamTextResult = class {
|
|
|
5088
5419
|
}
|
|
5089
5420
|
const chunkType = chunk.type;
|
|
5090
5421
|
switch (chunkType) {
|
|
5422
|
+
case "tool-approval-request":
|
|
5091
5423
|
case "text-start":
|
|
5092
5424
|
case "text-end": {
|
|
5093
5425
|
controller.enqueue(chunk);
|
|
@@ -5316,7 +5648,7 @@ var DefaultStreamTextResult = class {
|
|
|
5316
5648
|
}
|
|
5317
5649
|
await streamStep({
|
|
5318
5650
|
currentStep: 0,
|
|
5319
|
-
responseMessages:
|
|
5651
|
+
responseMessages: initialResponseMessages,
|
|
5320
5652
|
usage: {
|
|
5321
5653
|
inputTokens: void 0,
|
|
5322
5654
|
outputTokens: void 0,
|
|
@@ -5475,7 +5807,7 @@ var DefaultStreamTextResult = class {
|
|
|
5475
5807
|
sendSources = false,
|
|
5476
5808
|
sendStart = true,
|
|
5477
5809
|
sendFinish = true,
|
|
5478
|
-
onError =
|
|
5810
|
+
onError = import_provider24.getErrorMessage
|
|
5479
5811
|
} = {}) {
|
|
5480
5812
|
const responseMessageId = generateMessageId != null ? getResponseUIMessageId({
|
|
5481
5813
|
originalMessages,
|
|
@@ -5623,6 +5955,14 @@ var DefaultStreamTextResult = class {
|
|
|
5623
5955
|
}
|
|
5624
5956
|
break;
|
|
5625
5957
|
}
|
|
5958
|
+
case "tool-approval-request": {
|
|
5959
|
+
controller.enqueue({
|
|
5960
|
+
type: "tool-approval-request",
|
|
5961
|
+
approvalId: part.approvalId,
|
|
5962
|
+
toolCallId: part.toolCall.toolCallId
|
|
5963
|
+
});
|
|
5964
|
+
break;
|
|
5965
|
+
}
|
|
5626
5966
|
case "tool-result": {
|
|
5627
5967
|
const dynamic = isDynamic(part.toolCallId);
|
|
5628
5968
|
controller.enqueue({
|
|
@@ -5646,6 +5986,13 @@ var DefaultStreamTextResult = class {
|
|
|
5646
5986
|
});
|
|
5647
5987
|
break;
|
|
5648
5988
|
}
|
|
5989
|
+
case "tool-output-denied": {
|
|
5990
|
+
controller.enqueue({
|
|
5991
|
+
type: "tool-output-denied",
|
|
5992
|
+
toolCallId: part.toolCallId
|
|
5993
|
+
});
|
|
5994
|
+
break;
|
|
5995
|
+
}
|
|
5649
5996
|
case "error": {
|
|
5650
5997
|
controller.enqueue({
|
|
5651
5998
|
type: "error",
|
|
@@ -5890,6 +6237,13 @@ function convertToModelMessages(messages, options) {
|
|
|
5890
6237
|
providerExecuted: part.providerExecuted,
|
|
5891
6238
|
...part.callProviderMetadata != null ? { providerOptions: part.callProviderMetadata } : {}
|
|
5892
6239
|
});
|
|
6240
|
+
if (part.approval != null) {
|
|
6241
|
+
content.push({
|
|
6242
|
+
type: "tool-approval-request",
|
|
6243
|
+
approvalId: part.approval.id,
|
|
6244
|
+
toolCallId: part.toolCallId
|
|
6245
|
+
});
|
|
6246
|
+
}
|
|
5893
6247
|
if (part.providerExecuted === true && (part.state === "output-available" || part.state === "output-error")) {
|
|
5894
6248
|
content.push({
|
|
5895
6249
|
type: "tool-result",
|
|
@@ -5918,29 +6272,49 @@ function convertToModelMessages(messages, options) {
|
|
|
5918
6272
|
if (toolParts.length > 0) {
|
|
5919
6273
|
modelMessages.push({
|
|
5920
6274
|
role: "tool",
|
|
5921
|
-
content: toolParts.
|
|
5922
|
-
|
|
5923
|
-
|
|
5924
|
-
|
|
5925
|
-
|
|
5926
|
-
|
|
5927
|
-
|
|
5928
|
-
|
|
5929
|
-
|
|
5930
|
-
|
|
5931
|
-
|
|
5932
|
-
output: toolPart.state === "output-error" ? toolPart.errorText : toolPart.output,
|
|
5933
|
-
tool: (_a18 = options == null ? void 0 : options.tools) == null ? void 0 : _a18[toolName],
|
|
5934
|
-
errorMode: toolPart.state === "output-error" ? "text" : "none"
|
|
5935
|
-
})
|
|
5936
|
-
};
|
|
6275
|
+
content: toolParts.flatMap(
|
|
6276
|
+
(toolPart) => {
|
|
6277
|
+
var _a18, _b2, _c;
|
|
6278
|
+
const outputs = [];
|
|
6279
|
+
if (toolPart.type !== "dynamic-tool" && ((_a18 = toolPart.approval) == null ? void 0 : _a18.approved) != null) {
|
|
6280
|
+
outputs.push({
|
|
6281
|
+
type: "tool-approval-response",
|
|
6282
|
+
approvalId: toolPart.approval.id,
|
|
6283
|
+
approved: toolPart.approval.approved,
|
|
6284
|
+
reason: toolPart.approval.reason
|
|
6285
|
+
});
|
|
5937
6286
|
}
|
|
5938
|
-
|
|
5939
|
-
|
|
6287
|
+
switch (toolPart.state) {
|
|
6288
|
+
case "output-denied": {
|
|
6289
|
+
outputs.push({
|
|
6290
|
+
type: "tool-result",
|
|
6291
|
+
toolCallId: toolPart.toolCallId,
|
|
6292
|
+
toolName: getToolName(toolPart),
|
|
6293
|
+
output: {
|
|
6294
|
+
type: "error-text",
|
|
6295
|
+
value: (_b2 = toolPart.approval.reason) != null ? _b2 : "Tool execution denied."
|
|
6296
|
+
}
|
|
6297
|
+
});
|
|
6298
|
+
break;
|
|
6299
|
+
}
|
|
6300
|
+
case "output-error":
|
|
6301
|
+
case "output-available": {
|
|
6302
|
+
const toolName = toolPart.type === "dynamic-tool" ? toolPart.toolName : getToolName(toolPart);
|
|
6303
|
+
outputs.push({
|
|
6304
|
+
type: "tool-result",
|
|
6305
|
+
toolCallId: toolPart.toolCallId,
|
|
6306
|
+
toolName,
|
|
6307
|
+
output: createToolModelOutput({
|
|
6308
|
+
output: toolPart.state === "output-error" ? toolPart.errorText : toolPart.output,
|
|
6309
|
+
tool: (_c = options == null ? void 0 : options.tools) == null ? void 0 : _c[toolName],
|
|
6310
|
+
errorMode: toolPart.state === "output-error" ? "text" : "none"
|
|
6311
|
+
})
|
|
6312
|
+
});
|
|
6313
|
+
break;
|
|
6314
|
+
}
|
|
5940
6315
|
}
|
|
6316
|
+
return outputs;
|
|
5941
6317
|
}
|
|
5942
|
-
}).filter(
|
|
5943
|
-
(output) => output != null
|
|
5944
6318
|
)
|
|
5945
6319
|
});
|
|
5946
6320
|
}
|
|
@@ -5978,14 +6352,39 @@ var Agent = class {
|
|
|
5978
6352
|
constructor(settings) {
|
|
5979
6353
|
this.settings = settings;
|
|
5980
6354
|
}
|
|
6355
|
+
/**
|
|
6356
|
+
* The name of the agent.
|
|
6357
|
+
*/
|
|
6358
|
+
get name() {
|
|
6359
|
+
return this.settings.name;
|
|
6360
|
+
}
|
|
6361
|
+
/**
|
|
6362
|
+
* The tools that the agent can use.
|
|
6363
|
+
*/
|
|
5981
6364
|
get tools() {
|
|
5982
6365
|
return this.settings.tools;
|
|
5983
6366
|
}
|
|
6367
|
+
/**
|
|
6368
|
+
* Generates an output from the agent (non-streaming).
|
|
6369
|
+
*/
|
|
5984
6370
|
async generate(options) {
|
|
5985
|
-
|
|
6371
|
+
var _a17;
|
|
6372
|
+
return generateText({
|
|
6373
|
+
...this.settings,
|
|
6374
|
+
stopWhen: (_a17 = this.settings.stopWhen) != null ? _a17 : stepCountIs(20),
|
|
6375
|
+
...options
|
|
6376
|
+
});
|
|
5986
6377
|
}
|
|
6378
|
+
/**
|
|
6379
|
+
* Streams an output from the agent (streaming).
|
|
6380
|
+
*/
|
|
5987
6381
|
stream(options) {
|
|
5988
|
-
|
|
6382
|
+
var _a17;
|
|
6383
|
+
return streamText({
|
|
6384
|
+
...this.settings,
|
|
6385
|
+
stopWhen: (_a17 = this.settings.stopWhen) != null ? _a17 : stepCountIs(20),
|
|
6386
|
+
...options
|
|
6387
|
+
});
|
|
5989
6388
|
}
|
|
5990
6389
|
/**
|
|
5991
6390
|
* Creates a response object that streams UI messages to the client.
|
|
@@ -5998,7 +6397,7 @@ var Agent = class {
|
|
|
5998
6397
|
};
|
|
5999
6398
|
|
|
6000
6399
|
// src/embed/embed.ts
|
|
6001
|
-
var
|
|
6400
|
+
var import_provider_utils16 = require("@ai-sdk/provider-utils");
|
|
6002
6401
|
async function embed({
|
|
6003
6402
|
model: modelArg,
|
|
6004
6403
|
value,
|
|
@@ -6013,7 +6412,7 @@ async function embed({
|
|
|
6013
6412
|
maxRetries: maxRetriesArg,
|
|
6014
6413
|
abortSignal
|
|
6015
6414
|
});
|
|
6016
|
-
const headersWithUserAgent = (0,
|
|
6415
|
+
const headersWithUserAgent = (0, import_provider_utils16.withUserAgentSuffix)(
|
|
6017
6416
|
headers != null ? headers : {},
|
|
6018
6417
|
`ai/${VERSION}`
|
|
6019
6418
|
);
|
|
@@ -6117,7 +6516,7 @@ var DefaultEmbedResult = class {
|
|
|
6117
6516
|
};
|
|
6118
6517
|
|
|
6119
6518
|
// src/embed/embed-many.ts
|
|
6120
|
-
var
|
|
6519
|
+
var import_provider_utils17 = require("@ai-sdk/provider-utils");
|
|
6121
6520
|
|
|
6122
6521
|
// src/util/split-array.ts
|
|
6123
6522
|
function splitArray(array, chunkSize) {
|
|
@@ -6147,7 +6546,7 @@ async function embedMany({
|
|
|
6147
6546
|
maxRetries: maxRetriesArg,
|
|
6148
6547
|
abortSignal
|
|
6149
6548
|
});
|
|
6150
|
-
const headersWithUserAgent = (0,
|
|
6549
|
+
const headersWithUserAgent = (0, import_provider_utils17.withUserAgentSuffix)(
|
|
6151
6550
|
headers != null ? headers : {},
|
|
6152
6551
|
`ai/${VERSION}`
|
|
6153
6552
|
);
|
|
@@ -6366,7 +6765,7 @@ var DefaultEmbedManyResult = class {
|
|
|
6366
6765
|
};
|
|
6367
6766
|
|
|
6368
6767
|
// src/generate-image/generate-image.ts
|
|
6369
|
-
var
|
|
6768
|
+
var import_provider_utils18 = require("@ai-sdk/provider-utils");
|
|
6370
6769
|
async function generateImage({
|
|
6371
6770
|
model,
|
|
6372
6771
|
prompt,
|
|
@@ -6388,7 +6787,7 @@ async function generateImage({
|
|
|
6388
6787
|
modelId: model.modelId
|
|
6389
6788
|
});
|
|
6390
6789
|
}
|
|
6391
|
-
const headersWithUserAgent = (0,
|
|
6790
|
+
const headersWithUserAgent = (0, import_provider_utils18.withUserAgentSuffix)(
|
|
6392
6791
|
headers != null ? headers : {},
|
|
6393
6792
|
`ai/${VERSION}`
|
|
6394
6793
|
);
|
|
@@ -6484,7 +6883,7 @@ async function invokeModelMaxImagesPerCall(model) {
|
|
|
6484
6883
|
}
|
|
6485
6884
|
|
|
6486
6885
|
// src/generate-object/generate-object.ts
|
|
6487
|
-
var
|
|
6886
|
+
var import_provider_utils21 = require("@ai-sdk/provider-utils");
|
|
6488
6887
|
|
|
6489
6888
|
// src/generate-text/extract-reasoning-content.ts
|
|
6490
6889
|
function extractReasoningContent(content) {
|
|
@@ -6495,8 +6894,8 @@ function extractReasoningContent(content) {
|
|
|
6495
6894
|
}
|
|
6496
6895
|
|
|
6497
6896
|
// src/generate-object/output-strategy.ts
|
|
6498
|
-
var
|
|
6499
|
-
var
|
|
6897
|
+
var import_provider25 = require("@ai-sdk/provider");
|
|
6898
|
+
var import_provider_utils19 = require("@ai-sdk/provider-utils");
|
|
6500
6899
|
var noSchemaOutputStrategy = {
|
|
6501
6900
|
type: "no-schema",
|
|
6502
6901
|
jsonSchema: void 0,
|
|
@@ -6516,7 +6915,7 @@ var noSchemaOutputStrategy = {
|
|
|
6516
6915
|
} : { success: true, value };
|
|
6517
6916
|
},
|
|
6518
6917
|
createElementStream() {
|
|
6519
|
-
throw new
|
|
6918
|
+
throw new import_provider25.UnsupportedFunctionalityError({
|
|
6520
6919
|
functionality: "element streams in no-schema mode"
|
|
6521
6920
|
});
|
|
6522
6921
|
}
|
|
@@ -6535,10 +6934,10 @@ var objectOutputStrategy = (schema) => ({
|
|
|
6535
6934
|
};
|
|
6536
6935
|
},
|
|
6537
6936
|
async validateFinalResult(value) {
|
|
6538
|
-
return (0,
|
|
6937
|
+
return (0, import_provider_utils19.safeValidateTypes)({ value, schema });
|
|
6539
6938
|
},
|
|
6540
6939
|
createElementStream() {
|
|
6541
|
-
throw new
|
|
6940
|
+
throw new import_provider25.UnsupportedFunctionalityError({
|
|
6542
6941
|
functionality: "element streams in object mode"
|
|
6543
6942
|
});
|
|
6544
6943
|
}
|
|
@@ -6566,10 +6965,10 @@ var arrayOutputStrategy = (schema) => {
|
|
|
6566
6965
|
isFinalDelta
|
|
6567
6966
|
}) {
|
|
6568
6967
|
var _a17;
|
|
6569
|
-
if (!(0,
|
|
6968
|
+
if (!(0, import_provider25.isJSONObject)(value) || !(0, import_provider25.isJSONArray)(value.elements)) {
|
|
6570
6969
|
return {
|
|
6571
6970
|
success: false,
|
|
6572
|
-
error: new
|
|
6971
|
+
error: new import_provider25.TypeValidationError({
|
|
6573
6972
|
value,
|
|
6574
6973
|
cause: "value must be an object that contains an array of elements"
|
|
6575
6974
|
})
|
|
@@ -6579,7 +6978,7 @@ var arrayOutputStrategy = (schema) => {
|
|
|
6579
6978
|
const resultArray = [];
|
|
6580
6979
|
for (let i = 0; i < inputArray.length; i++) {
|
|
6581
6980
|
const element = inputArray[i];
|
|
6582
|
-
const result = await (0,
|
|
6981
|
+
const result = await (0, import_provider_utils19.safeValidateTypes)({ value: element, schema });
|
|
6583
6982
|
if (i === inputArray.length - 1 && !isFinalDelta) {
|
|
6584
6983
|
continue;
|
|
6585
6984
|
}
|
|
@@ -6609,10 +7008,10 @@ var arrayOutputStrategy = (schema) => {
|
|
|
6609
7008
|
};
|
|
6610
7009
|
},
|
|
6611
7010
|
async validateFinalResult(value) {
|
|
6612
|
-
if (!(0,
|
|
7011
|
+
if (!(0, import_provider25.isJSONObject)(value) || !(0, import_provider25.isJSONArray)(value.elements)) {
|
|
6613
7012
|
return {
|
|
6614
7013
|
success: false,
|
|
6615
|
-
error: new
|
|
7014
|
+
error: new import_provider25.TypeValidationError({
|
|
6616
7015
|
value,
|
|
6617
7016
|
cause: "value must be an object that contains an array of elements"
|
|
6618
7017
|
})
|
|
@@ -6620,7 +7019,7 @@ var arrayOutputStrategy = (schema) => {
|
|
|
6620
7019
|
}
|
|
6621
7020
|
const inputArray = value.elements;
|
|
6622
7021
|
for (const element of inputArray) {
|
|
6623
|
-
const result = await (0,
|
|
7022
|
+
const result = await (0, import_provider_utils19.safeValidateTypes)({ value: element, schema });
|
|
6624
7023
|
if (!result.success) {
|
|
6625
7024
|
return result;
|
|
6626
7025
|
}
|
|
@@ -6675,10 +7074,10 @@ var enumOutputStrategy = (enumValues) => {
|
|
|
6675
7074
|
additionalProperties: false
|
|
6676
7075
|
},
|
|
6677
7076
|
async validateFinalResult(value) {
|
|
6678
|
-
if (!(0,
|
|
7077
|
+
if (!(0, import_provider25.isJSONObject)(value) || typeof value.result !== "string") {
|
|
6679
7078
|
return {
|
|
6680
7079
|
success: false,
|
|
6681
|
-
error: new
|
|
7080
|
+
error: new import_provider25.TypeValidationError({
|
|
6682
7081
|
value,
|
|
6683
7082
|
cause: 'value must be an object that contains a string in the "result" property.'
|
|
6684
7083
|
})
|
|
@@ -6687,17 +7086,17 @@ var enumOutputStrategy = (enumValues) => {
|
|
|
6687
7086
|
const result = value.result;
|
|
6688
7087
|
return enumValues.includes(result) ? { success: true, value: result } : {
|
|
6689
7088
|
success: false,
|
|
6690
|
-
error: new
|
|
7089
|
+
error: new import_provider25.TypeValidationError({
|
|
6691
7090
|
value,
|
|
6692
7091
|
cause: "value must be a string in the enum"
|
|
6693
7092
|
})
|
|
6694
7093
|
};
|
|
6695
7094
|
},
|
|
6696
7095
|
async validatePartialResult({ value, textDelta }) {
|
|
6697
|
-
if (!(0,
|
|
7096
|
+
if (!(0, import_provider25.isJSONObject)(value) || typeof value.result !== "string") {
|
|
6698
7097
|
return {
|
|
6699
7098
|
success: false,
|
|
6700
|
-
error: new
|
|
7099
|
+
error: new import_provider25.TypeValidationError({
|
|
6701
7100
|
value,
|
|
6702
7101
|
cause: 'value must be an object that contains a string in the "result" property.'
|
|
6703
7102
|
})
|
|
@@ -6710,7 +7109,7 @@ var enumOutputStrategy = (enumValues) => {
|
|
|
6710
7109
|
if (value.result.length === 0 || possibleEnumValues.length === 0) {
|
|
6711
7110
|
return {
|
|
6712
7111
|
success: false,
|
|
6713
|
-
error: new
|
|
7112
|
+
error: new import_provider25.TypeValidationError({
|
|
6714
7113
|
value,
|
|
6715
7114
|
cause: "value must be a string in the enum"
|
|
6716
7115
|
})
|
|
@@ -6725,7 +7124,7 @@ var enumOutputStrategy = (enumValues) => {
|
|
|
6725
7124
|
};
|
|
6726
7125
|
},
|
|
6727
7126
|
createElementStream() {
|
|
6728
|
-
throw new
|
|
7127
|
+
throw new import_provider25.UnsupportedFunctionalityError({
|
|
6729
7128
|
functionality: "element streams in enum mode"
|
|
6730
7129
|
});
|
|
6731
7130
|
}
|
|
@@ -6738,9 +7137,9 @@ function getOutputStrategy({
|
|
|
6738
7137
|
}) {
|
|
6739
7138
|
switch (output) {
|
|
6740
7139
|
case "object":
|
|
6741
|
-
return objectOutputStrategy((0,
|
|
7140
|
+
return objectOutputStrategy((0, import_provider_utils19.asSchema)(schema));
|
|
6742
7141
|
case "array":
|
|
6743
|
-
return arrayOutputStrategy((0,
|
|
7142
|
+
return arrayOutputStrategy((0, import_provider_utils19.asSchema)(schema));
|
|
6744
7143
|
case "enum":
|
|
6745
7144
|
return enumOutputStrategy(enumValues);
|
|
6746
7145
|
case "no-schema":
|
|
@@ -6753,10 +7152,10 @@ function getOutputStrategy({
|
|
|
6753
7152
|
}
|
|
6754
7153
|
|
|
6755
7154
|
// src/generate-object/parse-and-validate-object-result.ts
|
|
6756
|
-
var
|
|
6757
|
-
var
|
|
7155
|
+
var import_provider26 = require("@ai-sdk/provider");
|
|
7156
|
+
var import_provider_utils20 = require("@ai-sdk/provider-utils");
|
|
6758
7157
|
async function parseAndValidateObjectResult(result, outputStrategy, context) {
|
|
6759
|
-
const parseResult = await (0,
|
|
7158
|
+
const parseResult = await (0, import_provider_utils20.safeParseJSON)({ text: result });
|
|
6760
7159
|
if (!parseResult.success) {
|
|
6761
7160
|
throw new NoObjectGeneratedError({
|
|
6762
7161
|
message: "No object generated: could not parse the response.",
|
|
@@ -6791,7 +7190,7 @@ async function parseAndValidateObjectResultWithRepair(result, outputStrategy, re
|
|
|
6791
7190
|
try {
|
|
6792
7191
|
return await parseAndValidateObjectResult(result, outputStrategy, context);
|
|
6793
7192
|
} catch (error) {
|
|
6794
|
-
if (repairText != null && NoObjectGeneratedError.isInstance(error) && (
|
|
7193
|
+
if (repairText != null && NoObjectGeneratedError.isInstance(error) && (import_provider26.JSONParseError.isInstance(error.cause) || import_provider26.TypeValidationError.isInstance(error.cause))) {
|
|
6795
7194
|
const repairedText = await repairText({
|
|
6796
7195
|
text: result,
|
|
6797
7196
|
error: error.cause
|
|
@@ -6928,7 +7327,7 @@ function validateObjectGenerationInput({
|
|
|
6928
7327
|
}
|
|
6929
7328
|
|
|
6930
7329
|
// src/generate-object/generate-object.ts
|
|
6931
|
-
var originalGenerateId3 = (0,
|
|
7330
|
+
var originalGenerateId3 = (0, import_provider_utils21.createIdGenerator)({ prefix: "aiobj", size: 24 });
|
|
6932
7331
|
async function generateObject(options) {
|
|
6933
7332
|
const {
|
|
6934
7333
|
model: modelArg,
|
|
@@ -6944,7 +7343,7 @@ async function generateObject(options) {
|
|
|
6944
7343
|
experimental_download: download2,
|
|
6945
7344
|
providerOptions,
|
|
6946
7345
|
_internal: {
|
|
6947
|
-
generateId:
|
|
7346
|
+
generateId: generateId2 = originalGenerateId3,
|
|
6948
7347
|
currentDate = () => /* @__PURE__ */ new Date()
|
|
6949
7348
|
} = {},
|
|
6950
7349
|
...settings
|
|
@@ -6973,7 +7372,7 @@ async function generateObject(options) {
|
|
|
6973
7372
|
enumValues
|
|
6974
7373
|
});
|
|
6975
7374
|
const callSettings = prepareCallSettings(settings);
|
|
6976
|
-
const headersWithUserAgent = (0,
|
|
7375
|
+
const headersWithUserAgent = (0, import_provider_utils21.withUserAgentSuffix)(
|
|
6977
7376
|
headers != null ? headers : {},
|
|
6978
7377
|
`ai/${VERSION}`
|
|
6979
7378
|
);
|
|
@@ -7068,7 +7467,7 @@ async function generateObject(options) {
|
|
|
7068
7467
|
headers: headersWithUserAgent
|
|
7069
7468
|
});
|
|
7070
7469
|
const responseData = {
|
|
7071
|
-
id: (_b = (_a18 = result2.response) == null ? void 0 : _a18.id) != null ? _b :
|
|
7470
|
+
id: (_b = (_a18 = result2.response) == null ? void 0 : _a18.id) != null ? _b : generateId2(),
|
|
7072
7471
|
timestamp: (_d = (_c = result2.response) == null ? void 0 : _c.timestamp) != null ? _d : currentDate(),
|
|
7073
7472
|
modelId: (_f = (_e = result2.response) == null ? void 0 : _e.modelId) != null ? _f : model.modelId,
|
|
7074
7473
|
headers: (_g = result2.response) == null ? void 0 : _g.headers,
|
|
@@ -7192,7 +7591,7 @@ var DefaultGenerateObjectResult = class {
|
|
|
7192
7591
|
};
|
|
7193
7592
|
|
|
7194
7593
|
// src/generate-object/stream-object.ts
|
|
7195
|
-
var
|
|
7594
|
+
var import_provider_utils23 = require("@ai-sdk/provider-utils");
|
|
7196
7595
|
|
|
7197
7596
|
// src/util/cosine-similarity.ts
|
|
7198
7597
|
function cosineSimilarity(vector1, vector2) {
|
|
@@ -7302,7 +7701,7 @@ var SerialJobExecutor = class {
|
|
|
7302
7701
|
};
|
|
7303
7702
|
|
|
7304
7703
|
// src/util/simulate-readable-stream.ts
|
|
7305
|
-
var
|
|
7704
|
+
var import_provider_utils22 = require("@ai-sdk/provider-utils");
|
|
7306
7705
|
function simulateReadableStream({
|
|
7307
7706
|
chunks,
|
|
7308
7707
|
initialDelayInMs = 0,
|
|
@@ -7310,7 +7709,7 @@ function simulateReadableStream({
|
|
|
7310
7709
|
_internal
|
|
7311
7710
|
}) {
|
|
7312
7711
|
var _a17;
|
|
7313
|
-
const delay2 = (_a17 = _internal == null ? void 0 : _internal.delay) != null ? _a17 :
|
|
7712
|
+
const delay2 = (_a17 = _internal == null ? void 0 : _internal.delay) != null ? _a17 : import_provider_utils22.delay;
|
|
7314
7713
|
let index = 0;
|
|
7315
7714
|
return new ReadableStream({
|
|
7316
7715
|
async pull(controller) {
|
|
@@ -7325,7 +7724,7 @@ function simulateReadableStream({
|
|
|
7325
7724
|
}
|
|
7326
7725
|
|
|
7327
7726
|
// src/generate-object/stream-object.ts
|
|
7328
|
-
var originalGenerateId4 = (0,
|
|
7727
|
+
var originalGenerateId4 = (0, import_provider_utils23.createIdGenerator)({ prefix: "aiobj", size: 24 });
|
|
7329
7728
|
function streamObject(options) {
|
|
7330
7729
|
const {
|
|
7331
7730
|
model,
|
|
@@ -7345,7 +7744,7 @@ function streamObject(options) {
|
|
|
7345
7744
|
},
|
|
7346
7745
|
onFinish,
|
|
7347
7746
|
_internal: {
|
|
7348
|
-
generateId:
|
|
7747
|
+
generateId: generateId2 = originalGenerateId4,
|
|
7349
7748
|
currentDate = () => /* @__PURE__ */ new Date(),
|
|
7350
7749
|
now: now2 = now
|
|
7351
7750
|
} = {},
|
|
@@ -7387,7 +7786,7 @@ function streamObject(options) {
|
|
|
7387
7786
|
onError,
|
|
7388
7787
|
onFinish,
|
|
7389
7788
|
download: download2,
|
|
7390
|
-
generateId:
|
|
7789
|
+
generateId: generateId2,
|
|
7391
7790
|
currentDate,
|
|
7392
7791
|
now: now2
|
|
7393
7792
|
});
|
|
@@ -7411,7 +7810,7 @@ var DefaultStreamObjectResult = class {
|
|
|
7411
7810
|
onError,
|
|
7412
7811
|
onFinish,
|
|
7413
7812
|
download: download2,
|
|
7414
|
-
generateId:
|
|
7813
|
+
generateId: generateId2,
|
|
7415
7814
|
currentDate,
|
|
7416
7815
|
now: now2
|
|
7417
7816
|
}) {
|
|
@@ -7559,7 +7958,7 @@ var DefaultStreamObjectResult = class {
|
|
|
7559
7958
|
let accumulatedText = "";
|
|
7560
7959
|
let textDelta = "";
|
|
7561
7960
|
let fullResponse = {
|
|
7562
|
-
id:
|
|
7961
|
+
id: generateId2(),
|
|
7563
7962
|
timestamp: currentDate(),
|
|
7564
7963
|
modelId: model.modelId
|
|
7565
7964
|
};
|
|
@@ -7846,19 +8245,7 @@ var DefaultStreamObjectResult = class {
|
|
|
7846
8245
|
};
|
|
7847
8246
|
|
|
7848
8247
|
// src/generate-speech/generate-speech.ts
|
|
7849
|
-
var
|
|
7850
|
-
|
|
7851
|
-
// src/error/no-speech-generated-error.ts
|
|
7852
|
-
var import_provider26 = require("@ai-sdk/provider");
|
|
7853
|
-
var NoSpeechGeneratedError = class extends import_provider26.AISDKError {
|
|
7854
|
-
constructor(options) {
|
|
7855
|
-
super({
|
|
7856
|
-
name: "AI_NoSpeechGeneratedError",
|
|
7857
|
-
message: "No speech audio generated."
|
|
7858
|
-
});
|
|
7859
|
-
this.responses = options.responses;
|
|
7860
|
-
}
|
|
7861
|
-
};
|
|
8248
|
+
var import_provider_utils24 = require("@ai-sdk/provider-utils");
|
|
7862
8249
|
|
|
7863
8250
|
// src/generate-speech/generated-audio-file.ts
|
|
7864
8251
|
var DefaultGeneratedAudioFile = class extends DefaultGeneratedFile {
|
|
@@ -7900,14 +8287,14 @@ async function generateSpeech({
|
|
|
7900
8287
|
headers
|
|
7901
8288
|
}) {
|
|
7902
8289
|
var _a17;
|
|
7903
|
-
if (model.specificationVersion !== "
|
|
8290
|
+
if (model.specificationVersion !== "v3") {
|
|
7904
8291
|
throw new UnsupportedModelVersionError({
|
|
7905
8292
|
version: model.specificationVersion,
|
|
7906
8293
|
provider: model.provider,
|
|
7907
8294
|
modelId: model.modelId
|
|
7908
8295
|
});
|
|
7909
8296
|
}
|
|
7910
|
-
const headersWithUserAgent = (0,
|
|
8297
|
+
const headersWithUserAgent = (0, import_provider_utils24.withUserAgentSuffix)(
|
|
7911
8298
|
headers != null ? headers : {},
|
|
7912
8299
|
`ai/${VERSION}`
|
|
7913
8300
|
);
|
|
@@ -7961,7 +8348,7 @@ __export(output_exports, {
|
|
|
7961
8348
|
object: () => object,
|
|
7962
8349
|
text: () => text
|
|
7963
8350
|
});
|
|
7964
|
-
var
|
|
8351
|
+
var import_provider_utils25 = require("@ai-sdk/provider-utils");
|
|
7965
8352
|
var text = () => ({
|
|
7966
8353
|
type: "text",
|
|
7967
8354
|
responseFormat: { type: "text" },
|
|
@@ -7975,7 +8362,7 @@ var text = () => ({
|
|
|
7975
8362
|
var object = ({
|
|
7976
8363
|
schema: inputSchema
|
|
7977
8364
|
}) => {
|
|
7978
|
-
const schema = (0,
|
|
8365
|
+
const schema = (0, import_provider_utils25.asSchema)(inputSchema);
|
|
7979
8366
|
return {
|
|
7980
8367
|
type: "object",
|
|
7981
8368
|
responseFormat: {
|
|
@@ -8001,7 +8388,7 @@ var object = ({
|
|
|
8001
8388
|
}
|
|
8002
8389
|
},
|
|
8003
8390
|
async parseOutput({ text: text2 }, context) {
|
|
8004
|
-
const parseResult = await (0,
|
|
8391
|
+
const parseResult = await (0, import_provider_utils25.safeParseJSON)({ text: text2 });
|
|
8005
8392
|
if (!parseResult.success) {
|
|
8006
8393
|
throw new NoObjectGeneratedError({
|
|
8007
8394
|
message: "No object generated: could not parse the response.",
|
|
@@ -8012,7 +8399,7 @@ var object = ({
|
|
|
8012
8399
|
finishReason: context.finishReason
|
|
8013
8400
|
});
|
|
8014
8401
|
}
|
|
8015
|
-
const validationResult = await (0,
|
|
8402
|
+
const validationResult = await (0, import_provider_utils25.safeValidateTypes)({
|
|
8016
8403
|
value: parseResult.value,
|
|
8017
8404
|
schema
|
|
8018
8405
|
});
|
|
@@ -8032,7 +8419,7 @@ var object = ({
|
|
|
8032
8419
|
};
|
|
8033
8420
|
|
|
8034
8421
|
// src/generate-text/smooth-stream.ts
|
|
8035
|
-
var
|
|
8422
|
+
var import_provider_utils26 = require("@ai-sdk/provider-utils");
|
|
8036
8423
|
var import_provider27 = require("@ai-sdk/provider");
|
|
8037
8424
|
var CHUNKING_REGEXPS = {
|
|
8038
8425
|
word: /\S+\s+/m,
|
|
@@ -8041,7 +8428,7 @@ var CHUNKING_REGEXPS = {
|
|
|
8041
8428
|
function smoothStream({
|
|
8042
8429
|
delayInMs = 10,
|
|
8043
8430
|
chunking = "word",
|
|
8044
|
-
_internal: { delay: delay2 =
|
|
8431
|
+
_internal: { delay: delay2 = import_provider_utils26.delay } = {}
|
|
8045
8432
|
} = {}) {
|
|
8046
8433
|
let detectChunk;
|
|
8047
8434
|
if (typeof chunking === "function") {
|
|
@@ -8111,7 +8498,7 @@ function defaultSettingsMiddleware({
|
|
|
8111
8498
|
settings
|
|
8112
8499
|
}) {
|
|
8113
8500
|
return {
|
|
8114
|
-
middlewareVersion: "
|
|
8501
|
+
middlewareVersion: "v3",
|
|
8115
8502
|
transformParams: async ({ params }) => {
|
|
8116
8503
|
return mergeObjects(settings, params);
|
|
8117
8504
|
}
|
|
@@ -8145,7 +8532,7 @@ function extractReasoningMiddleware({
|
|
|
8145
8532
|
const openingTag = `<${tagName}>`;
|
|
8146
8533
|
const closingTag = `</${tagName}>`;
|
|
8147
8534
|
return {
|
|
8148
|
-
middlewareVersion: "
|
|
8535
|
+
middlewareVersion: "v3",
|
|
8149
8536
|
wrapGenerate: async ({ doGenerate }) => {
|
|
8150
8537
|
const { content, ...rest } = await doGenerate();
|
|
8151
8538
|
const transformedContent = [];
|
|
@@ -8291,7 +8678,7 @@ function extractReasoningMiddleware({
|
|
|
8291
8678
|
// src/middleware/simulate-streaming-middleware.ts
|
|
8292
8679
|
function simulateStreamingMiddleware() {
|
|
8293
8680
|
return {
|
|
8294
|
-
middlewareVersion: "
|
|
8681
|
+
middlewareVersion: "v3",
|
|
8295
8682
|
wrapStream: async ({ doGenerate }) => {
|
|
8296
8683
|
const result = await doGenerate();
|
|
8297
8684
|
let id = 0;
|
|
@@ -8388,7 +8775,7 @@ var doWrap = ({
|
|
|
8388
8775
|
return transformParams ? await transformParams({ params, type, model }) : params;
|
|
8389
8776
|
}
|
|
8390
8777
|
return {
|
|
8391
|
-
specificationVersion: "
|
|
8778
|
+
specificationVersion: "v3",
|
|
8392
8779
|
provider: (_a17 = providerId != null ? providerId : overrideProvider == null ? void 0 : overrideProvider({ model })) != null ? _a17 : model.provider,
|
|
8393
8780
|
modelId: (_b = modelId != null ? modelId : overrideModelId == null ? void 0 : overrideModelId({ model })) != null ? _b : model.modelId,
|
|
8394
8781
|
supportedUrls: (_c = overrideSupportedUrls == null ? void 0 : overrideSupportedUrls({ model })) != null ? _c : model.supportedUrls,
|
|
@@ -8640,10 +9027,10 @@ var DefaultProviderRegistry = class {
|
|
|
8640
9027
|
};
|
|
8641
9028
|
|
|
8642
9029
|
// src/tool/mcp/mcp-client.ts
|
|
8643
|
-
var
|
|
9030
|
+
var import_provider_utils28 = require("@ai-sdk/provider-utils");
|
|
8644
9031
|
|
|
8645
9032
|
// src/tool/mcp/mcp-sse-transport.ts
|
|
8646
|
-
var
|
|
9033
|
+
var import_provider_utils27 = require("@ai-sdk/provider-utils");
|
|
8647
9034
|
|
|
8648
9035
|
// src/tool/mcp/json-rpc-message.ts
|
|
8649
9036
|
var import_v49 = require("zod/v4");
|
|
@@ -8802,13 +9189,13 @@ var SseMCPTransport = class {
|
|
|
8802
9189
|
const establishConnection = async () => {
|
|
8803
9190
|
var _a17, _b, _c;
|
|
8804
9191
|
try {
|
|
8805
|
-
const headers = (0,
|
|
9192
|
+
const headers = (0, import_provider_utils27.withUserAgentSuffix)(
|
|
8806
9193
|
{
|
|
8807
9194
|
...this.headers,
|
|
8808
9195
|
Accept: "text/event-stream"
|
|
8809
9196
|
},
|
|
8810
9197
|
`ai-sdk/${VERSION}`,
|
|
8811
|
-
(0,
|
|
9198
|
+
(0, import_provider_utils27.getRuntimeEnvironmentUserAgent)()
|
|
8812
9199
|
);
|
|
8813
9200
|
const response = await fetch(this.url.href, {
|
|
8814
9201
|
headers,
|
|
@@ -8821,7 +9208,7 @@ var SseMCPTransport = class {
|
|
|
8821
9208
|
(_b = this.onerror) == null ? void 0 : _b.call(this, error);
|
|
8822
9209
|
return reject(error);
|
|
8823
9210
|
}
|
|
8824
|
-
const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new
|
|
9211
|
+
const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new import_provider_utils27.EventSourceParserStream());
|
|
8825
9212
|
const reader = stream.getReader();
|
|
8826
9213
|
const processEvents = async () => {
|
|
8827
9214
|
var _a18, _b2, _c2;
|
|
@@ -8900,13 +9287,13 @@ var SseMCPTransport = class {
|
|
|
8900
9287
|
});
|
|
8901
9288
|
}
|
|
8902
9289
|
try {
|
|
8903
|
-
const headers = (0,
|
|
9290
|
+
const headers = (0, import_provider_utils27.withUserAgentSuffix)(
|
|
8904
9291
|
{
|
|
8905
9292
|
...this.headers,
|
|
8906
9293
|
"Content-Type": "application/json"
|
|
8907
9294
|
},
|
|
8908
9295
|
`ai-sdk/${VERSION}`,
|
|
8909
|
-
(0,
|
|
9296
|
+
(0, import_provider_utils27.getRuntimeEnvironmentUserAgent)()
|
|
8910
9297
|
);
|
|
8911
9298
|
const init = {
|
|
8912
9299
|
method: "POST",
|
|
@@ -9157,15 +9544,15 @@ var DefaultMCPClient = class {
|
|
|
9157
9544
|
(_a18 = options == null ? void 0 : options.abortSignal) == null ? void 0 : _a18.throwIfAborted();
|
|
9158
9545
|
return self.callTool({ name: name17, args, options });
|
|
9159
9546
|
};
|
|
9160
|
-
const toolWithExecute = schemas === "automatic" ? (0,
|
|
9547
|
+
const toolWithExecute = schemas === "automatic" ? (0, import_provider_utils28.dynamicTool)({
|
|
9161
9548
|
description,
|
|
9162
|
-
inputSchema: (0,
|
|
9549
|
+
inputSchema: (0, import_provider_utils28.jsonSchema)({
|
|
9163
9550
|
...inputSchema,
|
|
9164
9551
|
properties: (_a17 = inputSchema.properties) != null ? _a17 : {},
|
|
9165
9552
|
additionalProperties: false
|
|
9166
9553
|
}),
|
|
9167
9554
|
execute
|
|
9168
|
-
}) : (0,
|
|
9555
|
+
}) : (0, import_provider_utils28.tool)({
|
|
9169
9556
|
description,
|
|
9170
9557
|
inputSchema: schemas[name17].inputSchema,
|
|
9171
9558
|
execute
|
|
@@ -9215,7 +9602,7 @@ var DefaultMCPClient = class {
|
|
|
9215
9602
|
};
|
|
9216
9603
|
|
|
9217
9604
|
// src/transcribe/transcribe.ts
|
|
9218
|
-
var
|
|
9605
|
+
var import_provider_utils29 = require("@ai-sdk/provider-utils");
|
|
9219
9606
|
|
|
9220
9607
|
// src/error/no-transcript-generated-error.ts
|
|
9221
9608
|
var import_provider31 = require("@ai-sdk/provider");
|
|
@@ -9238,7 +9625,7 @@ async function transcribe({
|
|
|
9238
9625
|
abortSignal,
|
|
9239
9626
|
headers
|
|
9240
9627
|
}) {
|
|
9241
|
-
if (model.specificationVersion !== "
|
|
9628
|
+
if (model.specificationVersion !== "v3") {
|
|
9242
9629
|
throw new UnsupportedModelVersionError({
|
|
9243
9630
|
version: model.specificationVersion,
|
|
9244
9631
|
provider: model.provider,
|
|
@@ -9249,7 +9636,7 @@ async function transcribe({
|
|
|
9249
9636
|
maxRetries: maxRetriesArg,
|
|
9250
9637
|
abortSignal
|
|
9251
9638
|
});
|
|
9252
|
-
const headersWithUserAgent = (0,
|
|
9639
|
+
const headersWithUserAgent = (0, import_provider_utils29.withUserAgentSuffix)(
|
|
9253
9640
|
headers != null ? headers : {},
|
|
9254
9641
|
`ai/${VERSION}`
|
|
9255
9642
|
);
|
|
@@ -9297,7 +9684,7 @@ var DefaultTranscriptionResult = class {
|
|
|
9297
9684
|
};
|
|
9298
9685
|
|
|
9299
9686
|
// src/ui/call-completion-api.ts
|
|
9300
|
-
var
|
|
9687
|
+
var import_provider_utils30 = require("@ai-sdk/provider-utils");
|
|
9301
9688
|
|
|
9302
9689
|
// src/ui/process-text-stream.ts
|
|
9303
9690
|
async function processTextStream({
|
|
@@ -9345,13 +9732,13 @@ async function callCompletionApi({
|
|
|
9345
9732
|
...body
|
|
9346
9733
|
}),
|
|
9347
9734
|
credentials,
|
|
9348
|
-
headers: (0,
|
|
9735
|
+
headers: (0, import_provider_utils30.withUserAgentSuffix)(
|
|
9349
9736
|
{
|
|
9350
9737
|
"Content-Type": "application/json",
|
|
9351
9738
|
...headers
|
|
9352
9739
|
},
|
|
9353
9740
|
`ai-sdk/${VERSION}`,
|
|
9354
|
-
(0,
|
|
9741
|
+
(0, import_provider_utils30.getRuntimeEnvironmentUserAgent)()
|
|
9355
9742
|
),
|
|
9356
9743
|
signal: abortController.signal
|
|
9357
9744
|
}).catch((err) => {
|
|
@@ -9379,7 +9766,7 @@ async function callCompletionApi({
|
|
|
9379
9766
|
}
|
|
9380
9767
|
case "data": {
|
|
9381
9768
|
await consumeStream({
|
|
9382
|
-
stream: (0,
|
|
9769
|
+
stream: (0, import_provider_utils30.parseJsonEventStream)({
|
|
9383
9770
|
stream: response.body,
|
|
9384
9771
|
schema: uiMessageChunkSchema
|
|
9385
9772
|
}).pipeThrough(
|
|
@@ -9431,7 +9818,7 @@ async function callCompletionApi({
|
|
|
9431
9818
|
}
|
|
9432
9819
|
|
|
9433
9820
|
// src/ui/chat.ts
|
|
9434
|
-
var
|
|
9821
|
+
var import_provider_utils33 = require("@ai-sdk/provider-utils");
|
|
9435
9822
|
|
|
9436
9823
|
// src/ui/convert-file-list-to-file-ui-parts.ts
|
|
9437
9824
|
async function convertFileListToFileUIParts(files) {
|
|
@@ -9464,10 +9851,10 @@ async function convertFileListToFileUIParts(files) {
|
|
|
9464
9851
|
}
|
|
9465
9852
|
|
|
9466
9853
|
// src/ui/default-chat-transport.ts
|
|
9467
|
-
var
|
|
9854
|
+
var import_provider_utils32 = require("@ai-sdk/provider-utils");
|
|
9468
9855
|
|
|
9469
9856
|
// src/ui/http-chat-transport.ts
|
|
9470
|
-
var
|
|
9857
|
+
var import_provider_utils31 = require("@ai-sdk/provider-utils");
|
|
9471
9858
|
var HttpChatTransport = class {
|
|
9472
9859
|
constructor({
|
|
9473
9860
|
api = "/api/chat",
|
|
@@ -9491,9 +9878,9 @@ var HttpChatTransport = class {
|
|
|
9491
9878
|
...options
|
|
9492
9879
|
}) {
|
|
9493
9880
|
var _a17, _b, _c, _d, _e;
|
|
9494
|
-
const resolvedBody = await (0,
|
|
9495
|
-
const resolvedHeaders = await (0,
|
|
9496
|
-
const resolvedCredentials = await (0,
|
|
9881
|
+
const resolvedBody = await (0, import_provider_utils31.resolve)(this.body);
|
|
9882
|
+
const resolvedHeaders = await (0, import_provider_utils31.resolve)(this.headers);
|
|
9883
|
+
const resolvedCredentials = await (0, import_provider_utils31.resolve)(this.credentials);
|
|
9497
9884
|
const preparedRequest = await ((_a17 = this.prepareSendMessagesRequest) == null ? void 0 : _a17.call(this, {
|
|
9498
9885
|
api: this.api,
|
|
9499
9886
|
id: options.chatId,
|
|
@@ -9519,13 +9906,13 @@ var HttpChatTransport = class {
|
|
|
9519
9906
|
const fetch2 = (_d = this.fetch) != null ? _d : globalThis.fetch;
|
|
9520
9907
|
const response = await fetch2(api, {
|
|
9521
9908
|
method: "POST",
|
|
9522
|
-
headers: (0,
|
|
9909
|
+
headers: (0, import_provider_utils31.withUserAgentSuffix)(
|
|
9523
9910
|
{
|
|
9524
9911
|
"Content-Type": "application/json",
|
|
9525
9912
|
...headers
|
|
9526
9913
|
},
|
|
9527
9914
|
`ai-sdk/${VERSION}`,
|
|
9528
|
-
(0,
|
|
9915
|
+
(0, import_provider_utils31.getRuntimeEnvironmentUserAgent)()
|
|
9529
9916
|
),
|
|
9530
9917
|
body: JSON.stringify(body),
|
|
9531
9918
|
credentials,
|
|
@@ -9543,9 +9930,9 @@ var HttpChatTransport = class {
|
|
|
9543
9930
|
}
|
|
9544
9931
|
async reconnectToStream(options) {
|
|
9545
9932
|
var _a17, _b, _c, _d, _e;
|
|
9546
|
-
const resolvedBody = await (0,
|
|
9547
|
-
const resolvedHeaders = await (0,
|
|
9548
|
-
const resolvedCredentials = await (0,
|
|
9933
|
+
const resolvedBody = await (0, import_provider_utils31.resolve)(this.body);
|
|
9934
|
+
const resolvedHeaders = await (0, import_provider_utils31.resolve)(this.headers);
|
|
9935
|
+
const resolvedCredentials = await (0, import_provider_utils31.resolve)(this.credentials);
|
|
9549
9936
|
const preparedRequest = await ((_a17 = this.prepareReconnectToStreamRequest) == null ? void 0 : _a17.call(this, {
|
|
9550
9937
|
api: this.api,
|
|
9551
9938
|
id: options.chatId,
|
|
@@ -9560,10 +9947,10 @@ var HttpChatTransport = class {
|
|
|
9560
9947
|
const fetch2 = (_d = this.fetch) != null ? _d : globalThis.fetch;
|
|
9561
9948
|
const response = await fetch2(api, {
|
|
9562
9949
|
method: "GET",
|
|
9563
|
-
headers: (0,
|
|
9950
|
+
headers: (0, import_provider_utils31.withUserAgentSuffix)(
|
|
9564
9951
|
headers,
|
|
9565
9952
|
`ai-sdk/${VERSION}`,
|
|
9566
|
-
(0,
|
|
9953
|
+
(0, import_provider_utils31.getRuntimeEnvironmentUserAgent)()
|
|
9567
9954
|
),
|
|
9568
9955
|
credentials
|
|
9569
9956
|
});
|
|
@@ -9588,7 +9975,7 @@ var DefaultChatTransport = class extends HttpChatTransport {
|
|
|
9588
9975
|
super(options);
|
|
9589
9976
|
}
|
|
9590
9977
|
processResponseStream(stream) {
|
|
9591
|
-
return (0,
|
|
9978
|
+
return (0, import_provider_utils32.parseJsonEventStream)({
|
|
9592
9979
|
stream,
|
|
9593
9980
|
schema: uiMessageChunkSchema
|
|
9594
9981
|
}).pipeThrough(
|
|
@@ -9607,8 +9994,8 @@ var DefaultChatTransport = class extends HttpChatTransport {
|
|
|
9607
9994
|
// src/ui/chat.ts
|
|
9608
9995
|
var AbstractChat = class {
|
|
9609
9996
|
constructor({
|
|
9610
|
-
generateId:
|
|
9611
|
-
id =
|
|
9997
|
+
generateId: generateId2 = import_provider_utils33.generateId,
|
|
9998
|
+
id = generateId2(),
|
|
9612
9999
|
transport = new DefaultChatTransport(),
|
|
9613
10000
|
messageMetadataSchema,
|
|
9614
10001
|
dataPartSchemas,
|
|
@@ -9720,29 +10107,50 @@ var AbstractChat = class {
|
|
|
9720
10107
|
this.setStatus({ status: "ready" });
|
|
9721
10108
|
}
|
|
9722
10109
|
};
|
|
10110
|
+
this.addToolApprovalResponse = async ({
|
|
10111
|
+
id,
|
|
10112
|
+
approved,
|
|
10113
|
+
reason
|
|
10114
|
+
}) => this.jobExecutor.run(async () => {
|
|
10115
|
+
var _a17, _b;
|
|
10116
|
+
const messages = this.state.messages;
|
|
10117
|
+
const lastMessage = messages[messages.length - 1];
|
|
10118
|
+
const updatePart = (part) => isToolUIPart(part) && part.state === "approval-requested" && part.approval.id === id ? {
|
|
10119
|
+
...part,
|
|
10120
|
+
state: "approval-responded",
|
|
10121
|
+
approval: { id, approved, reason }
|
|
10122
|
+
} : part;
|
|
10123
|
+
this.state.replaceMessage(messages.length - 1, {
|
|
10124
|
+
...lastMessage,
|
|
10125
|
+
parts: lastMessage.parts.map(updatePart)
|
|
10126
|
+
});
|
|
10127
|
+
if (this.activeResponse) {
|
|
10128
|
+
this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(updatePart);
|
|
10129
|
+
}
|
|
10130
|
+
if (this.status !== "streaming" && this.status !== "submitted" && ((_a17 = this.sendAutomaticallyWhen) == null ? void 0 : _a17.call(this, { messages: this.state.messages }))) {
|
|
10131
|
+
this.makeRequest({
|
|
10132
|
+
trigger: "submit-message",
|
|
10133
|
+
messageId: (_b = this.lastMessage) == null ? void 0 : _b.id
|
|
10134
|
+
});
|
|
10135
|
+
}
|
|
10136
|
+
});
|
|
9723
10137
|
this.addToolResult = async ({
|
|
10138
|
+
state = "output-available",
|
|
9724
10139
|
tool: tool3,
|
|
9725
10140
|
toolCallId,
|
|
9726
|
-
output
|
|
10141
|
+
output,
|
|
10142
|
+
errorText
|
|
9727
10143
|
}) => this.jobExecutor.run(async () => {
|
|
9728
10144
|
var _a17, _b;
|
|
9729
10145
|
const messages = this.state.messages;
|
|
9730
10146
|
const lastMessage = messages[messages.length - 1];
|
|
10147
|
+
const updatePart = (part) => isToolOrDynamicToolUIPart(part) && part.toolCallId === toolCallId ? { ...part, state, output, errorText } : part;
|
|
9731
10148
|
this.state.replaceMessage(messages.length - 1, {
|
|
9732
10149
|
...lastMessage,
|
|
9733
|
-
parts: lastMessage.parts.map(
|
|
9734
|
-
(part) => isToolOrDynamicToolUIPart(part) && part.toolCallId === toolCallId ? { ...part, state: "output-available", output } : part
|
|
9735
|
-
)
|
|
10150
|
+
parts: lastMessage.parts.map(updatePart)
|
|
9736
10151
|
});
|
|
9737
10152
|
if (this.activeResponse) {
|
|
9738
|
-
this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(
|
|
9739
|
-
(part) => isToolOrDynamicToolUIPart(part) && part.toolCallId === toolCallId ? {
|
|
9740
|
-
...part,
|
|
9741
|
-
state: "output-available",
|
|
9742
|
-
output,
|
|
9743
|
-
errorText: void 0
|
|
9744
|
-
} : part
|
|
9745
|
-
);
|
|
10153
|
+
this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(updatePart);
|
|
9746
10154
|
}
|
|
9747
10155
|
if (this.status !== "streaming" && this.status !== "submitted" && ((_a17 = this.sendAutomaticallyWhen) == null ? void 0 : _a17.call(this, { messages: this.state.messages }))) {
|
|
9748
10156
|
this.makeRequest({
|
|
@@ -9764,7 +10172,7 @@ var AbstractChat = class {
|
|
|
9764
10172
|
};
|
|
9765
10173
|
this.id = id;
|
|
9766
10174
|
this.transport = transport;
|
|
9767
|
-
this.generateId =
|
|
10175
|
+
this.generateId = generateId2;
|
|
9768
10176
|
this.messageMetadataSchema = messageMetadataSchema;
|
|
9769
10177
|
this.dataPartSchemas = dataPartSchemas;
|
|
9770
10178
|
this.state = state;
|
|
@@ -9934,6 +10342,30 @@ var AbstractChat = class {
|
|
|
9934
10342
|
}
|
|
9935
10343
|
};
|
|
9936
10344
|
|
|
10345
|
+
// src/ui/last-assistant-message-is-complete-with-approval-responses.ts
|
|
10346
|
+
function lastAssistantMessageIsCompleteWithApprovalResponses({
|
|
10347
|
+
messages
|
|
10348
|
+
}) {
|
|
10349
|
+
const message = messages[messages.length - 1];
|
|
10350
|
+
if (!message) {
|
|
10351
|
+
return false;
|
|
10352
|
+
}
|
|
10353
|
+
if (message.role !== "assistant") {
|
|
10354
|
+
return false;
|
|
10355
|
+
}
|
|
10356
|
+
const lastStepStartIndex = message.parts.reduce((lastIndex, part, index) => {
|
|
10357
|
+
return part.type === "step-start" ? index : lastIndex;
|
|
10358
|
+
}, -1);
|
|
10359
|
+
const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(isToolOrDynamicToolUIPart);
|
|
10360
|
+
return (
|
|
10361
|
+
// has at least one tool approval response
|
|
10362
|
+
lastStepToolInvocations.filter((part) => part.state === "approval-responded").length > 0 && // all tool approvals must have a response
|
|
10363
|
+
lastStepToolInvocations.every(
|
|
10364
|
+
(part) => part.state === "output-available" || part.state === "output-error" || part.state === "approval-responded"
|
|
10365
|
+
)
|
|
10366
|
+
);
|
|
10367
|
+
}
|
|
10368
|
+
|
|
9937
10369
|
// src/ui/last-assistant-message-is-complete-with-tool-calls.ts
|
|
9938
10370
|
function lastAssistantMessageIsCompleteWithToolCalls({
|
|
9939
10371
|
messages
|
|
@@ -9949,7 +10381,9 @@ function lastAssistantMessageIsCompleteWithToolCalls({
|
|
|
9949
10381
|
return part.type === "step-start" ? index : lastIndex;
|
|
9950
10382
|
}, -1);
|
|
9951
10383
|
const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(isToolOrDynamicToolUIPart);
|
|
9952
|
-
return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every(
|
|
10384
|
+
return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every(
|
|
10385
|
+
(part) => part.state === "output-available" || part.state === "output-error"
|
|
10386
|
+
);
|
|
9953
10387
|
}
|
|
9954
10388
|
|
|
9955
10389
|
// src/ui/transform-text-to-ui-message-stream.ts
|
|
@@ -9989,7 +10423,7 @@ var TextStreamChatTransport = class extends HttpChatTransport {
|
|
|
9989
10423
|
|
|
9990
10424
|
// src/ui/validate-ui-messages.ts
|
|
9991
10425
|
var import_provider32 = require("@ai-sdk/provider");
|
|
9992
|
-
var
|
|
10426
|
+
var import_provider_utils34 = require("@ai-sdk/provider-utils");
|
|
9993
10427
|
var import_v410 = require("zod/v4");
|
|
9994
10428
|
var textUIPartSchema = import_v410.z.object({
|
|
9995
10429
|
type: import_v410.z.literal("text"),
|
|
@@ -10083,7 +10517,8 @@ var toolUIPartSchemas = [
|
|
|
10083
10517
|
providerExecuted: import_v410.z.boolean().optional(),
|
|
10084
10518
|
input: import_v410.z.unknown().optional(),
|
|
10085
10519
|
output: import_v410.z.never().optional(),
|
|
10086
|
-
errorText: import_v410.z.never().optional()
|
|
10520
|
+
errorText: import_v410.z.never().optional(),
|
|
10521
|
+
approval: import_v410.z.never().optional()
|
|
10087
10522
|
}),
|
|
10088
10523
|
import_v410.z.object({
|
|
10089
10524
|
type: import_v410.z.string().startsWith("tool-"),
|
|
@@ -10093,7 +10528,38 @@ var toolUIPartSchemas = [
|
|
|
10093
10528
|
input: import_v410.z.unknown(),
|
|
10094
10529
|
output: import_v410.z.never().optional(),
|
|
10095
10530
|
errorText: import_v410.z.never().optional(),
|
|
10096
|
-
callProviderMetadata: providerMetadataSchema.optional()
|
|
10531
|
+
callProviderMetadata: providerMetadataSchema.optional(),
|
|
10532
|
+
approval: import_v410.z.never().optional()
|
|
10533
|
+
}),
|
|
10534
|
+
import_v410.z.object({
|
|
10535
|
+
type: import_v410.z.string().startsWith("tool-"),
|
|
10536
|
+
toolCallId: import_v410.z.string(),
|
|
10537
|
+
state: import_v410.z.literal("approval-requested"),
|
|
10538
|
+
input: import_v410.z.unknown(),
|
|
10539
|
+
providerExecuted: import_v410.z.boolean().optional(),
|
|
10540
|
+
output: import_v410.z.never().optional(),
|
|
10541
|
+
errorText: import_v410.z.never().optional(),
|
|
10542
|
+
callProviderMetadata: providerMetadataSchema.optional(),
|
|
10543
|
+
approval: import_v410.z.object({
|
|
10544
|
+
id: import_v410.z.string(),
|
|
10545
|
+
approved: import_v410.z.never().optional(),
|
|
10546
|
+
reason: import_v410.z.never().optional()
|
|
10547
|
+
})
|
|
10548
|
+
}),
|
|
10549
|
+
import_v410.z.object({
|
|
10550
|
+
type: import_v410.z.string().startsWith("tool-"),
|
|
10551
|
+
toolCallId: import_v410.z.string(),
|
|
10552
|
+
state: import_v410.z.literal("approval-responded"),
|
|
10553
|
+
input: import_v410.z.unknown(),
|
|
10554
|
+
providerExecuted: import_v410.z.boolean().optional(),
|
|
10555
|
+
output: import_v410.z.never().optional(),
|
|
10556
|
+
errorText: import_v410.z.never().optional(),
|
|
10557
|
+
callProviderMetadata: providerMetadataSchema.optional(),
|
|
10558
|
+
approval: import_v410.z.object({
|
|
10559
|
+
id: import_v410.z.string(),
|
|
10560
|
+
approved: import_v410.z.boolean(),
|
|
10561
|
+
reason: import_v410.z.string().optional()
|
|
10562
|
+
})
|
|
10097
10563
|
}),
|
|
10098
10564
|
import_v410.z.object({
|
|
10099
10565
|
type: import_v410.z.string().startsWith("tool-"),
|
|
@@ -10104,7 +10570,12 @@ var toolUIPartSchemas = [
|
|
|
10104
10570
|
output: import_v410.z.unknown(),
|
|
10105
10571
|
errorText: import_v410.z.never().optional(),
|
|
10106
10572
|
callProviderMetadata: providerMetadataSchema.optional(),
|
|
10107
|
-
preliminary: import_v410.z.boolean().optional()
|
|
10573
|
+
preliminary: import_v410.z.boolean().optional(),
|
|
10574
|
+
approval: import_v410.z.object({
|
|
10575
|
+
id: import_v410.z.string(),
|
|
10576
|
+
approved: import_v410.z.literal(true),
|
|
10577
|
+
reason: import_v410.z.string().optional()
|
|
10578
|
+
}).optional()
|
|
10108
10579
|
}),
|
|
10109
10580
|
import_v410.z.object({
|
|
10110
10581
|
type: import_v410.z.string().startsWith("tool-"),
|
|
@@ -10114,7 +10585,27 @@ var toolUIPartSchemas = [
|
|
|
10114
10585
|
input: import_v410.z.unknown(),
|
|
10115
10586
|
output: import_v410.z.never().optional(),
|
|
10116
10587
|
errorText: import_v410.z.string(),
|
|
10117
|
-
callProviderMetadata: providerMetadataSchema.optional()
|
|
10588
|
+
callProviderMetadata: providerMetadataSchema.optional(),
|
|
10589
|
+
approval: import_v410.z.object({
|
|
10590
|
+
id: import_v410.z.string(),
|
|
10591
|
+
approved: import_v410.z.literal(true),
|
|
10592
|
+
reason: import_v410.z.string().optional()
|
|
10593
|
+
}).optional()
|
|
10594
|
+
}),
|
|
10595
|
+
import_v410.z.object({
|
|
10596
|
+
type: import_v410.z.string().startsWith("tool-"),
|
|
10597
|
+
toolCallId: import_v410.z.string(),
|
|
10598
|
+
state: import_v410.z.literal("output-denied"),
|
|
10599
|
+
providerExecuted: import_v410.z.boolean().optional(),
|
|
10600
|
+
input: import_v410.z.unknown(),
|
|
10601
|
+
output: import_v410.z.never().optional(),
|
|
10602
|
+
errorText: import_v410.z.never().optional(),
|
|
10603
|
+
callProviderMetadata: providerMetadataSchema.optional(),
|
|
10604
|
+
approval: import_v410.z.object({
|
|
10605
|
+
id: import_v410.z.string(),
|
|
10606
|
+
approved: import_v410.z.literal(false),
|
|
10607
|
+
reason: import_v410.z.string().optional()
|
|
10608
|
+
})
|
|
10118
10609
|
})
|
|
10119
10610
|
];
|
|
10120
10611
|
var uiMessageSchema = import_v410.z.object({
|
|
@@ -10152,13 +10643,13 @@ async function safeValidateUIMessages({
|
|
|
10152
10643
|
})
|
|
10153
10644
|
};
|
|
10154
10645
|
}
|
|
10155
|
-
const validatedMessages = await (0,
|
|
10646
|
+
const validatedMessages = await (0, import_provider_utils34.validateTypes)({
|
|
10156
10647
|
value: messages,
|
|
10157
10648
|
schema: import_v410.z.array(uiMessageSchema)
|
|
10158
10649
|
});
|
|
10159
10650
|
if (metadataSchema) {
|
|
10160
10651
|
for (const message of validatedMessages) {
|
|
10161
|
-
await (0,
|
|
10652
|
+
await (0, import_provider_utils34.validateTypes)({
|
|
10162
10653
|
value: message.metadata,
|
|
10163
10654
|
schema: metadataSchema
|
|
10164
10655
|
});
|
|
@@ -10181,7 +10672,7 @@ async function safeValidateUIMessages({
|
|
|
10181
10672
|
})
|
|
10182
10673
|
};
|
|
10183
10674
|
}
|
|
10184
|
-
await (0,
|
|
10675
|
+
await (0, import_provider_utils34.validateTypes)({
|
|
10185
10676
|
value: dataPart.data,
|
|
10186
10677
|
schema: dataSchema
|
|
10187
10678
|
});
|
|
@@ -10206,13 +10697,13 @@ async function safeValidateUIMessages({
|
|
|
10206
10697
|
};
|
|
10207
10698
|
}
|
|
10208
10699
|
if (toolPart.state === "input-available" || toolPart.state === "output-available" || toolPart.state === "output-error") {
|
|
10209
|
-
await (0,
|
|
10700
|
+
await (0, import_provider_utils34.validateTypes)({
|
|
10210
10701
|
value: toolPart.input,
|
|
10211
10702
|
schema: tool3.inputSchema
|
|
10212
10703
|
});
|
|
10213
10704
|
}
|
|
10214
10705
|
if (toolPart.state === "output-available" && tool3.outputSchema) {
|
|
10215
|
-
await (0,
|
|
10706
|
+
await (0, import_provider_utils34.validateTypes)({
|
|
10216
10707
|
value: toolPart.output,
|
|
10217
10708
|
schema: tool3.outputSchema
|
|
10218
10709
|
});
|
|
@@ -10250,13 +10741,13 @@ async function validateUIMessages({
|
|
|
10250
10741
|
}
|
|
10251
10742
|
|
|
10252
10743
|
// src/ui-message-stream/create-ui-message-stream.ts
|
|
10253
|
-
var
|
|
10744
|
+
var import_provider_utils35 = require("@ai-sdk/provider-utils");
|
|
10254
10745
|
function createUIMessageStream({
|
|
10255
10746
|
execute,
|
|
10256
|
-
onError =
|
|
10747
|
+
onError = import_provider_utils35.getErrorMessage,
|
|
10257
10748
|
originalMessages,
|
|
10258
10749
|
onFinish,
|
|
10259
|
-
generateId:
|
|
10750
|
+
generateId: generateId2 = import_provider_utils35.generateId
|
|
10260
10751
|
}) {
|
|
10261
10752
|
let controller;
|
|
10262
10753
|
const ongoingStreamPromises = [];
|
|
@@ -10328,7 +10819,7 @@ function createUIMessageStream({
|
|
|
10328
10819
|
});
|
|
10329
10820
|
return handleUIMessageStreamFinish({
|
|
10330
10821
|
stream,
|
|
10331
|
-
messageId:
|
|
10822
|
+
messageId: generateId2(),
|
|
10332
10823
|
originalMessages,
|
|
10333
10824
|
onFinish,
|
|
10334
10825
|
onError
|
|
@@ -10387,6 +10878,7 @@ function readUIMessageStream({
|
|
|
10387
10878
|
AISDKError,
|
|
10388
10879
|
APICallError,
|
|
10389
10880
|
AbstractChat,
|
|
10881
|
+
Agent,
|
|
10390
10882
|
DefaultChatTransport,
|
|
10391
10883
|
DownloadError,
|
|
10392
10884
|
EmptyResponseBodyError,
|
|
@@ -10409,6 +10901,7 @@ function readUIMessageStream({
|
|
|
10409
10901
|
NoObjectGeneratedError,
|
|
10410
10902
|
NoOutputGeneratedError,
|
|
10411
10903
|
NoOutputSpecifiedError,
|
|
10904
|
+
NoSpeechGeneratedError,
|
|
10412
10905
|
NoSuchModelError,
|
|
10413
10906
|
NoSuchProviderError,
|
|
10414
10907
|
NoSuchToolError,
|
|
@@ -10465,6 +10958,7 @@ function readUIMessageStream({
|
|
|
10465
10958
|
isToolOrDynamicToolUIPart,
|
|
10466
10959
|
isToolUIPart,
|
|
10467
10960
|
jsonSchema,
|
|
10961
|
+
lastAssistantMessageIsCompleteWithApprovalResponses,
|
|
10468
10962
|
lastAssistantMessageIsCompleteWithToolCalls,
|
|
10469
10963
|
modelMessageSchema,
|
|
10470
10964
|
parseJsonEventStream,
|