ai 5.1.0-beta.8 → 6.0.0-beta.29
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 +181 -0
- package/README.md +128 -44
- package/dist/index.d.mts +559 -388
- package/dist/index.d.ts +559 -388
- package/dist/index.js +985 -491
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +850 -362
- 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 +42 -14
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +42 -14
- package/dist/internal/index.mjs.map +1 -1
- package/dist/test/index.d.mts +51 -51
- package/dist/test/index.d.ts +51 -51
- package/dist/test/index.js +25 -25
- package/dist/test/index.js.map +1 -1
- package/dist/test/index.mjs +20 -20
- package/dist/test/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.mjs
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
asSchema as asSchema5,
|
|
11
11
|
createIdGenerator as createIdGenerator5,
|
|
12
12
|
dynamicTool as dynamicTool2,
|
|
13
|
-
generateId
|
|
13
|
+
generateId,
|
|
14
14
|
jsonSchema as jsonSchema2,
|
|
15
15
|
parseJsonEventStream as parseJsonEventStream3,
|
|
16
16
|
tool as tool2,
|
|
@@ -20,7 +20,6 @@ import {
|
|
|
20
20
|
// src/generate-text/generate-text.ts
|
|
21
21
|
import {
|
|
22
22
|
createIdGenerator,
|
|
23
|
-
executeTool,
|
|
24
23
|
getErrorMessage as getErrorMessage5,
|
|
25
24
|
withUserAgentSuffix as withUserAgentSuffix2
|
|
26
25
|
} from "@ai-sdk/provider-utils";
|
|
@@ -98,7 +97,7 @@ import { gateway } from "@ai-sdk/gateway";
|
|
|
98
97
|
|
|
99
98
|
// src/error/index.ts
|
|
100
99
|
import {
|
|
101
|
-
AISDKError as
|
|
100
|
+
AISDKError as AISDKError18,
|
|
102
101
|
APICallError,
|
|
103
102
|
EmptyResponseBodyError,
|
|
104
103
|
InvalidPromptError,
|
|
@@ -275,13 +274,25 @@ var NoOutputGeneratedError = class extends AISDKError8 {
|
|
|
275
274
|
};
|
|
276
275
|
_a8 = symbol8;
|
|
277
276
|
|
|
278
|
-
// src/error/no-
|
|
277
|
+
// src/error/no-speech-generated-error.ts
|
|
279
278
|
import { AISDKError as AISDKError9 } from "@ai-sdk/provider";
|
|
279
|
+
var NoSpeechGeneratedError = class extends AISDKError9 {
|
|
280
|
+
constructor(options) {
|
|
281
|
+
super({
|
|
282
|
+
name: "AI_NoSpeechGeneratedError",
|
|
283
|
+
message: "No speech audio generated."
|
|
284
|
+
});
|
|
285
|
+
this.responses = options.responses;
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
// src/error/no-such-tool-error.ts
|
|
290
|
+
import { AISDKError as AISDKError10 } from "@ai-sdk/provider";
|
|
280
291
|
var name9 = "AI_NoSuchToolError";
|
|
281
292
|
var marker9 = `vercel.ai.error.${name9}`;
|
|
282
293
|
var symbol9 = Symbol.for(marker9);
|
|
283
294
|
var _a9;
|
|
284
|
-
var NoSuchToolError = class extends
|
|
295
|
+
var NoSuchToolError = class extends AISDKError10 {
|
|
285
296
|
constructor({
|
|
286
297
|
toolName,
|
|
287
298
|
availableTools = void 0,
|
|
@@ -293,18 +304,18 @@ var NoSuchToolError = class extends AISDKError9 {
|
|
|
293
304
|
this.availableTools = availableTools;
|
|
294
305
|
}
|
|
295
306
|
static isInstance(error) {
|
|
296
|
-
return
|
|
307
|
+
return AISDKError10.hasMarker(error, marker9);
|
|
297
308
|
}
|
|
298
309
|
};
|
|
299
310
|
_a9 = symbol9;
|
|
300
311
|
|
|
301
312
|
// src/error/tool-call-repair-error.ts
|
|
302
|
-
import { AISDKError as
|
|
313
|
+
import { AISDKError as AISDKError11, getErrorMessage as getErrorMessage2 } from "@ai-sdk/provider";
|
|
303
314
|
var name10 = "AI_ToolCallRepairError";
|
|
304
315
|
var marker10 = `vercel.ai.error.${name10}`;
|
|
305
316
|
var symbol10 = Symbol.for(marker10);
|
|
306
317
|
var _a10;
|
|
307
|
-
var ToolCallRepairError = class extends
|
|
318
|
+
var ToolCallRepairError = class extends AISDKError11 {
|
|
308
319
|
constructor({
|
|
309
320
|
cause,
|
|
310
321
|
originalError,
|
|
@@ -315,14 +326,14 @@ var ToolCallRepairError = class extends AISDKError10 {
|
|
|
315
326
|
this.originalError = originalError;
|
|
316
327
|
}
|
|
317
328
|
static isInstance(error) {
|
|
318
|
-
return
|
|
329
|
+
return AISDKError11.hasMarker(error, marker10);
|
|
319
330
|
}
|
|
320
331
|
};
|
|
321
332
|
_a10 = symbol10;
|
|
322
333
|
|
|
323
334
|
// src/error/unsupported-model-version-error.ts
|
|
324
|
-
import { AISDKError as
|
|
325
|
-
var UnsupportedModelVersionError = class extends
|
|
335
|
+
import { AISDKError as AISDKError12 } from "@ai-sdk/provider";
|
|
336
|
+
var UnsupportedModelVersionError = class extends AISDKError12 {
|
|
326
337
|
constructor(options) {
|
|
327
338
|
super({
|
|
328
339
|
name: "AI_UnsupportedModelVersionError",
|
|
@@ -335,12 +346,12 @@ var UnsupportedModelVersionError = class extends AISDKError11 {
|
|
|
335
346
|
};
|
|
336
347
|
|
|
337
348
|
// src/prompt/invalid-data-content-error.ts
|
|
338
|
-
import { AISDKError as
|
|
349
|
+
import { AISDKError as AISDKError13 } from "@ai-sdk/provider";
|
|
339
350
|
var name11 = "AI_InvalidDataContentError";
|
|
340
351
|
var marker11 = `vercel.ai.error.${name11}`;
|
|
341
352
|
var symbol11 = Symbol.for(marker11);
|
|
342
353
|
var _a11;
|
|
343
|
-
var InvalidDataContentError = class extends
|
|
354
|
+
var InvalidDataContentError = class extends AISDKError13 {
|
|
344
355
|
constructor({
|
|
345
356
|
content,
|
|
346
357
|
cause,
|
|
@@ -351,18 +362,18 @@ var InvalidDataContentError = class extends AISDKError12 {
|
|
|
351
362
|
this.content = content;
|
|
352
363
|
}
|
|
353
364
|
static isInstance(error) {
|
|
354
|
-
return
|
|
365
|
+
return AISDKError13.hasMarker(error, marker11);
|
|
355
366
|
}
|
|
356
367
|
};
|
|
357
368
|
_a11 = symbol11;
|
|
358
369
|
|
|
359
370
|
// src/prompt/invalid-message-role-error.ts
|
|
360
|
-
import { AISDKError as
|
|
371
|
+
import { AISDKError as AISDKError14 } from "@ai-sdk/provider";
|
|
361
372
|
var name12 = "AI_InvalidMessageRoleError";
|
|
362
373
|
var marker12 = `vercel.ai.error.${name12}`;
|
|
363
374
|
var symbol12 = Symbol.for(marker12);
|
|
364
375
|
var _a12;
|
|
365
|
-
var InvalidMessageRoleError = class extends
|
|
376
|
+
var InvalidMessageRoleError = class extends AISDKError14 {
|
|
366
377
|
constructor({
|
|
367
378
|
role,
|
|
368
379
|
message = `Invalid message role: '${role}'. Must be one of: "system", "user", "assistant", "tool".`
|
|
@@ -372,18 +383,18 @@ var InvalidMessageRoleError = class extends AISDKError13 {
|
|
|
372
383
|
this.role = role;
|
|
373
384
|
}
|
|
374
385
|
static isInstance(error) {
|
|
375
|
-
return
|
|
386
|
+
return AISDKError14.hasMarker(error, marker12);
|
|
376
387
|
}
|
|
377
388
|
};
|
|
378
389
|
_a12 = symbol12;
|
|
379
390
|
|
|
380
391
|
// src/prompt/message-conversion-error.ts
|
|
381
|
-
import { AISDKError as
|
|
392
|
+
import { AISDKError as AISDKError15 } from "@ai-sdk/provider";
|
|
382
393
|
var name13 = "AI_MessageConversionError";
|
|
383
394
|
var marker13 = `vercel.ai.error.${name13}`;
|
|
384
395
|
var symbol13 = Symbol.for(marker13);
|
|
385
396
|
var _a13;
|
|
386
|
-
var MessageConversionError = class extends
|
|
397
|
+
var MessageConversionError = class extends AISDKError15 {
|
|
387
398
|
constructor({
|
|
388
399
|
originalMessage,
|
|
389
400
|
message
|
|
@@ -393,18 +404,18 @@ var MessageConversionError = class extends AISDKError14 {
|
|
|
393
404
|
this.originalMessage = originalMessage;
|
|
394
405
|
}
|
|
395
406
|
static isInstance(error) {
|
|
396
|
-
return
|
|
407
|
+
return AISDKError15.hasMarker(error, marker13);
|
|
397
408
|
}
|
|
398
409
|
};
|
|
399
410
|
_a13 = symbol13;
|
|
400
411
|
|
|
401
412
|
// src/util/download/download-error.ts
|
|
402
|
-
import { AISDKError as
|
|
413
|
+
import { AISDKError as AISDKError16 } from "@ai-sdk/provider";
|
|
403
414
|
var name14 = "AI_DownloadError";
|
|
404
415
|
var marker14 = `vercel.ai.error.${name14}`;
|
|
405
416
|
var symbol14 = Symbol.for(marker14);
|
|
406
417
|
var _a14;
|
|
407
|
-
var DownloadError = class extends
|
|
418
|
+
var DownloadError = class extends AISDKError16 {
|
|
408
419
|
constructor({
|
|
409
420
|
url,
|
|
410
421
|
statusCode,
|
|
@@ -419,18 +430,18 @@ var DownloadError = class extends AISDKError15 {
|
|
|
419
430
|
this.statusText = statusText;
|
|
420
431
|
}
|
|
421
432
|
static isInstance(error) {
|
|
422
|
-
return
|
|
433
|
+
return AISDKError16.hasMarker(error, marker14);
|
|
423
434
|
}
|
|
424
435
|
};
|
|
425
436
|
_a14 = symbol14;
|
|
426
437
|
|
|
427
438
|
// src/util/retry-error.ts
|
|
428
|
-
import { AISDKError as
|
|
439
|
+
import { AISDKError as AISDKError17 } from "@ai-sdk/provider";
|
|
429
440
|
var name15 = "AI_RetryError";
|
|
430
441
|
var marker15 = `vercel.ai.error.${name15}`;
|
|
431
442
|
var symbol15 = Symbol.for(marker15);
|
|
432
443
|
var _a15;
|
|
433
|
-
var RetryError = class extends
|
|
444
|
+
var RetryError = class extends AISDKError17 {
|
|
434
445
|
constructor({
|
|
435
446
|
message,
|
|
436
447
|
reason,
|
|
@@ -443,34 +454,60 @@ var RetryError = class extends AISDKError16 {
|
|
|
443
454
|
this.lastError = errors[errors.length - 1];
|
|
444
455
|
}
|
|
445
456
|
static isInstance(error) {
|
|
446
|
-
return
|
|
457
|
+
return AISDKError17.hasMarker(error, marker15);
|
|
447
458
|
}
|
|
448
459
|
};
|
|
449
460
|
_a15 = symbol15;
|
|
450
461
|
|
|
451
462
|
// src/model/resolve-model.ts
|
|
463
|
+
function transformToV3LanguageModel(model) {
|
|
464
|
+
return new Proxy(model, {
|
|
465
|
+
get(target, prop) {
|
|
466
|
+
if (prop === "specificationVersion")
|
|
467
|
+
return "v3";
|
|
468
|
+
return target[prop];
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
function transformToV3EmbeddingModel(model) {
|
|
473
|
+
return new Proxy(model, {
|
|
474
|
+
get(target, prop) {
|
|
475
|
+
if (prop === "specificationVersion")
|
|
476
|
+
return "v3";
|
|
477
|
+
return target[prop];
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
}
|
|
452
481
|
function resolveLanguageModel(model) {
|
|
453
482
|
if (typeof model !== "string") {
|
|
454
|
-
if (model.specificationVersion !== "v2") {
|
|
483
|
+
if (model.specificationVersion !== "v3" && model.specificationVersion !== "v2") {
|
|
484
|
+
const unsupportedModel = model;
|
|
455
485
|
throw new UnsupportedModelVersionError({
|
|
456
|
-
version:
|
|
457
|
-
provider:
|
|
458
|
-
modelId:
|
|
486
|
+
version: unsupportedModel.specificationVersion,
|
|
487
|
+
provider: unsupportedModel.provider,
|
|
488
|
+
modelId: unsupportedModel.modelId
|
|
459
489
|
});
|
|
460
490
|
}
|
|
491
|
+
if (model.specificationVersion === "v2") {
|
|
492
|
+
return transformToV3LanguageModel(model);
|
|
493
|
+
}
|
|
461
494
|
return model;
|
|
462
495
|
}
|
|
463
496
|
return getGlobalProvider().languageModel(model);
|
|
464
497
|
}
|
|
465
498
|
function resolveEmbeddingModel(model) {
|
|
466
499
|
if (typeof model !== "string") {
|
|
467
|
-
if (model.specificationVersion !== "v3") {
|
|
500
|
+
if (model.specificationVersion !== "v3" && model.specificationVersion !== "v2") {
|
|
501
|
+
const unsupportedModel = model;
|
|
468
502
|
throw new UnsupportedModelVersionError({
|
|
469
|
-
version:
|
|
470
|
-
provider:
|
|
471
|
-
modelId:
|
|
503
|
+
version: unsupportedModel.specificationVersion,
|
|
504
|
+
provider: unsupportedModel.provider,
|
|
505
|
+
modelId: unsupportedModel.modelId
|
|
472
506
|
});
|
|
473
507
|
}
|
|
508
|
+
if (model.specificationVersion === "v2") {
|
|
509
|
+
return transformToV3EmbeddingModel(model);
|
|
510
|
+
}
|
|
474
511
|
return model;
|
|
475
512
|
}
|
|
476
513
|
return getGlobalProvider().textEmbeddingModel(
|
|
@@ -679,7 +716,7 @@ import {
|
|
|
679
716
|
} from "@ai-sdk/provider-utils";
|
|
680
717
|
|
|
681
718
|
// src/version.ts
|
|
682
|
-
var VERSION = true ? "
|
|
719
|
+
var VERSION = true ? "6.0.0-beta.29" : "0.0.0-test";
|
|
683
720
|
|
|
684
721
|
// src/util/download/download.ts
|
|
685
722
|
var download = async ({ url }) => {
|
|
@@ -720,7 +757,7 @@ var createDefaultDownloadFunction = (download2 = download) => (requestedDownload
|
|
|
720
757
|
);
|
|
721
758
|
|
|
722
759
|
// src/prompt/data-content.ts
|
|
723
|
-
import { AISDKError as
|
|
760
|
+
import { AISDKError as AISDKError19 } from "@ai-sdk/provider";
|
|
724
761
|
import {
|
|
725
762
|
convertBase64ToUint8Array as convertBase64ToUint8Array2,
|
|
726
763
|
convertUint8ArrayToBase64
|
|
@@ -757,7 +794,7 @@ var dataContentSchema = z.union([
|
|
|
757
794
|
{ message: "Must be a Buffer" }
|
|
758
795
|
)
|
|
759
796
|
]);
|
|
760
|
-
function
|
|
797
|
+
function convertToLanguageModelV3DataContent(content) {
|
|
761
798
|
if (content instanceof Uint8Array) {
|
|
762
799
|
return { data: content, mediaType: void 0 };
|
|
763
800
|
}
|
|
@@ -775,7 +812,7 @@ function convertToLanguageModelV2DataContent(content) {
|
|
|
775
812
|
content.toString()
|
|
776
813
|
);
|
|
777
814
|
if (dataUrlMediaType == null || base64Content == null) {
|
|
778
|
-
throw new
|
|
815
|
+
throw new AISDKError19({
|
|
779
816
|
name: "InvalidDataContentError",
|
|
780
817
|
message: `Invalid data URL format in content ${content.toString()}`
|
|
781
818
|
});
|
|
@@ -825,12 +862,26 @@ async function convertToLanguageModelPrompt({
|
|
|
825
862
|
download2,
|
|
826
863
|
supportedUrls
|
|
827
864
|
);
|
|
828
|
-
|
|
865
|
+
const messages = [
|
|
829
866
|
...prompt.system != null ? [{ role: "system", content: prompt.system }] : [],
|
|
830
867
|
...prompt.messages.map(
|
|
831
868
|
(message) => convertToLanguageModelMessage({ message, downloadedAssets })
|
|
832
869
|
)
|
|
833
870
|
];
|
|
871
|
+
const combinedMessages = [];
|
|
872
|
+
for (const message of messages) {
|
|
873
|
+
if (message.role !== "tool") {
|
|
874
|
+
combinedMessages.push(message);
|
|
875
|
+
continue;
|
|
876
|
+
}
|
|
877
|
+
const lastCombinedMessage = combinedMessages.at(-1);
|
|
878
|
+
if ((lastCombinedMessage == null ? void 0 : lastCombinedMessage.role) === "tool") {
|
|
879
|
+
lastCombinedMessage.content.push(...message.content);
|
|
880
|
+
} else {
|
|
881
|
+
combinedMessages.push(message);
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
return combinedMessages;
|
|
834
885
|
}
|
|
835
886
|
function convertToLanguageModelMessage({
|
|
836
887
|
message,
|
|
@@ -872,11 +923,13 @@ function convertToLanguageModelMessage({
|
|
|
872
923
|
content: message.content.filter(
|
|
873
924
|
// remove empty text parts (no text, and no provider options):
|
|
874
925
|
(part) => part.type !== "text" || part.text !== "" || part.providerOptions != null
|
|
926
|
+
).filter(
|
|
927
|
+
(part) => part.type !== "tool-approval-request"
|
|
875
928
|
).map((part) => {
|
|
876
929
|
const providerOptions = part.providerOptions;
|
|
877
930
|
switch (part.type) {
|
|
878
931
|
case "file": {
|
|
879
|
-
const { data, mediaType } =
|
|
932
|
+
const { data, mediaType } = convertToLanguageModelV3DataContent(
|
|
880
933
|
part.data
|
|
881
934
|
);
|
|
882
935
|
return {
|
|
@@ -928,7 +981,7 @@ function convertToLanguageModelMessage({
|
|
|
928
981
|
case "tool": {
|
|
929
982
|
return {
|
|
930
983
|
role: "tool",
|
|
931
|
-
content: message.content.map((part) => ({
|
|
984
|
+
content: message.content.filter((part) => part.type !== "tool-approval-response").map((part) => ({
|
|
932
985
|
type: "tool-result",
|
|
933
986
|
toolCallId: part.toolCallId,
|
|
934
987
|
toolName: part.toolName,
|
|
@@ -972,12 +1025,12 @@ async function downloadAssets(messages, download2, supportedUrls) {
|
|
|
972
1025
|
}));
|
|
973
1026
|
const downloadedFiles = await download2(plannedDownloads);
|
|
974
1027
|
return Object.fromEntries(
|
|
975
|
-
downloadedFiles.
|
|
976
|
-
(
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
1028
|
+
downloadedFiles.map(
|
|
1029
|
+
(file, index) => file == null ? null : [
|
|
1030
|
+
plannedDownloads[index].url.toString(),
|
|
1031
|
+
{ data: file.data, mediaType: file.mediaType }
|
|
1032
|
+
]
|
|
1033
|
+
).filter((file) => file != null)
|
|
981
1034
|
);
|
|
982
1035
|
}
|
|
983
1036
|
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
|
@@ -1001,7 +1054,7 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
|
|
|
1001
1054
|
default:
|
|
1002
1055
|
throw new Error(`Unsupported part type: ${type}`);
|
|
1003
1056
|
}
|
|
1004
|
-
const { data: convertedData, mediaType: convertedMediaType } =
|
|
1057
|
+
const { data: convertedData, mediaType: convertedMediaType } = convertToLanguageModelV3DataContent(originalData);
|
|
1005
1058
|
let mediaType = convertedMediaType != null ? convertedMediaType : part.mediaType;
|
|
1006
1059
|
let data = convertedData;
|
|
1007
1060
|
if (data instanceof URL) {
|
|
@@ -1040,6 +1093,29 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
|
|
|
1040
1093
|
}
|
|
1041
1094
|
}
|
|
1042
1095
|
|
|
1096
|
+
// src/prompt/create-tool-model-output.ts
|
|
1097
|
+
import {
|
|
1098
|
+
getErrorMessage as getErrorMessage3
|
|
1099
|
+
} from "@ai-sdk/provider";
|
|
1100
|
+
function createToolModelOutput({
|
|
1101
|
+
output,
|
|
1102
|
+
tool: tool3,
|
|
1103
|
+
errorMode
|
|
1104
|
+
}) {
|
|
1105
|
+
if (errorMode === "text") {
|
|
1106
|
+
return { type: "error-text", value: getErrorMessage3(output) };
|
|
1107
|
+
} else if (errorMode === "json") {
|
|
1108
|
+
return { type: "error-json", value: toJSONValue(output) };
|
|
1109
|
+
}
|
|
1110
|
+
if (tool3 == null ? void 0 : tool3.toModelOutput) {
|
|
1111
|
+
return tool3.toModelOutput(output);
|
|
1112
|
+
}
|
|
1113
|
+
return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: toJSONValue(output) };
|
|
1114
|
+
}
|
|
1115
|
+
function toJSONValue(value) {
|
|
1116
|
+
return value === void 0 ? null : value;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1043
1119
|
// src/prompt/prepare-call-settings.ts
|
|
1044
1120
|
function prepareCallSettings({
|
|
1045
1121
|
maxOutputTokens,
|
|
@@ -1291,6 +1367,17 @@ var toolResultPartSchema = z4.object({
|
|
|
1291
1367
|
output: outputSchema,
|
|
1292
1368
|
providerOptions: providerMetadataSchema.optional()
|
|
1293
1369
|
});
|
|
1370
|
+
var toolApprovalRequestSchema = z4.object({
|
|
1371
|
+
type: z4.literal("tool-approval-request"),
|
|
1372
|
+
approvalId: z4.string(),
|
|
1373
|
+
toolCallId: z4.string()
|
|
1374
|
+
});
|
|
1375
|
+
var toolApprovalResponseSchema = z4.object({
|
|
1376
|
+
type: z4.literal("tool-approval-response"),
|
|
1377
|
+
approvalId: z4.string(),
|
|
1378
|
+
approved: z4.boolean(),
|
|
1379
|
+
reason: z4.string().optional()
|
|
1380
|
+
});
|
|
1294
1381
|
|
|
1295
1382
|
// src/prompt/message.ts
|
|
1296
1383
|
var systemModelMessageSchema = z5.object(
|
|
@@ -1320,7 +1407,8 @@ var assistantModelMessageSchema = z5.object({
|
|
|
1320
1407
|
filePartSchema,
|
|
1321
1408
|
reasoningPartSchema,
|
|
1322
1409
|
toolCallPartSchema,
|
|
1323
|
-
toolResultPartSchema
|
|
1410
|
+
toolResultPartSchema,
|
|
1411
|
+
toolApprovalRequestSchema
|
|
1324
1412
|
])
|
|
1325
1413
|
)
|
|
1326
1414
|
]),
|
|
@@ -1329,7 +1417,7 @@ var assistantModelMessageSchema = z5.object({
|
|
|
1329
1417
|
var coreAssistantMessageSchema = assistantModelMessageSchema;
|
|
1330
1418
|
var toolModelMessageSchema = z5.object({
|
|
1331
1419
|
role: z5.literal("tool"),
|
|
1332
|
-
content: z5.array(toolResultPartSchema),
|
|
1420
|
+
content: z5.array(z5.union([toolResultPartSchema, toolApprovalResponseSchema])),
|
|
1333
1421
|
providerOptions: providerMetadataSchema.optional()
|
|
1334
1422
|
});
|
|
1335
1423
|
var coreToolMessageSchema = toolModelMessageSchema;
|
|
@@ -1402,10 +1490,10 @@ import {
|
|
|
1402
1490
|
GatewayAuthenticationError,
|
|
1403
1491
|
GatewayModelNotFoundError
|
|
1404
1492
|
} from "@ai-sdk/gateway";
|
|
1405
|
-
import { AISDKError as
|
|
1493
|
+
import { AISDKError as AISDKError20 } from "@ai-sdk/provider";
|
|
1406
1494
|
function wrapGatewayError(error) {
|
|
1407
1495
|
if (GatewayAuthenticationError.isInstance(error) || GatewayModelNotFoundError.isInstance(error)) {
|
|
1408
|
-
return new
|
|
1496
|
+
return new AISDKError20({
|
|
1409
1497
|
name: "GatewayError",
|
|
1410
1498
|
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.",
|
|
1411
1499
|
cause: error
|
|
@@ -1652,7 +1740,7 @@ function asArray(value) {
|
|
|
1652
1740
|
|
|
1653
1741
|
// src/util/retry-with-exponential-backoff.ts
|
|
1654
1742
|
import { APICallError as APICallError2 } from "@ai-sdk/provider";
|
|
1655
|
-
import { delay, getErrorMessage as
|
|
1743
|
+
import { delay, getErrorMessage as getErrorMessage4, isAbortError } from "@ai-sdk/provider-utils";
|
|
1656
1744
|
function getRetryDelayInMs({
|
|
1657
1745
|
error,
|
|
1658
1746
|
exponentialBackoffDelay
|
|
@@ -1708,7 +1796,7 @@ async function _retryWithExponentialBackoff(f, {
|
|
|
1708
1796
|
if (maxRetries === 0) {
|
|
1709
1797
|
throw error;
|
|
1710
1798
|
}
|
|
1711
|
-
const errorMessage =
|
|
1799
|
+
const errorMessage = getErrorMessage4(error);
|
|
1712
1800
|
const newErrors = [...errors, error];
|
|
1713
1801
|
const tryNumber = newErrors.length;
|
|
1714
1802
|
if (tryNumber > maxRetries) {
|
|
@@ -1779,6 +1867,164 @@ function prepareRetries({
|
|
|
1779
1867
|
};
|
|
1780
1868
|
}
|
|
1781
1869
|
|
|
1870
|
+
// src/generate-text/collect-tool-approvals.ts
|
|
1871
|
+
function collectToolApprovals({
|
|
1872
|
+
messages
|
|
1873
|
+
}) {
|
|
1874
|
+
const lastMessage = messages.at(-1);
|
|
1875
|
+
if ((lastMessage == null ? void 0 : lastMessage.role) != "tool") {
|
|
1876
|
+
return {
|
|
1877
|
+
approvedToolApprovals: [],
|
|
1878
|
+
deniedToolApprovals: []
|
|
1879
|
+
};
|
|
1880
|
+
}
|
|
1881
|
+
const toolCallsByToolCallId = {};
|
|
1882
|
+
for (const message of messages) {
|
|
1883
|
+
if (message.role === "assistant" && typeof message.content !== "string") {
|
|
1884
|
+
const content = message.content;
|
|
1885
|
+
for (const part of content) {
|
|
1886
|
+
if (part.type === "tool-call") {
|
|
1887
|
+
toolCallsByToolCallId[part.toolCallId] = part;
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
const toolApprovalRequestsByApprovalId = {};
|
|
1893
|
+
for (const message of messages) {
|
|
1894
|
+
if (message.role === "assistant" && typeof message.content !== "string") {
|
|
1895
|
+
const content = message.content;
|
|
1896
|
+
for (const part of content) {
|
|
1897
|
+
if (part.type === "tool-approval-request") {
|
|
1898
|
+
toolApprovalRequestsByApprovalId[part.approvalId] = part;
|
|
1899
|
+
}
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
const toolResults = {};
|
|
1904
|
+
for (const part of lastMessage.content) {
|
|
1905
|
+
if (part.type === "tool-result") {
|
|
1906
|
+
toolResults[part.toolCallId] = part;
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
const approvedToolApprovals = [];
|
|
1910
|
+
const deniedToolApprovals = [];
|
|
1911
|
+
const approvalResponses = lastMessage.content.filter(
|
|
1912
|
+
(part) => part.type === "tool-approval-response"
|
|
1913
|
+
);
|
|
1914
|
+
for (const approvalResponse of approvalResponses) {
|
|
1915
|
+
const approvalRequest = toolApprovalRequestsByApprovalId[approvalResponse.approvalId];
|
|
1916
|
+
if (toolResults[approvalRequest.toolCallId] != null) {
|
|
1917
|
+
continue;
|
|
1918
|
+
}
|
|
1919
|
+
const approval = {
|
|
1920
|
+
approvalRequest,
|
|
1921
|
+
approvalResponse,
|
|
1922
|
+
toolCall: toolCallsByToolCallId[approvalRequest.toolCallId]
|
|
1923
|
+
};
|
|
1924
|
+
if (approvalResponse.approved) {
|
|
1925
|
+
approvedToolApprovals.push(approval);
|
|
1926
|
+
} else {
|
|
1927
|
+
deniedToolApprovals.push(approval);
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
return { approvedToolApprovals, deniedToolApprovals };
|
|
1931
|
+
}
|
|
1932
|
+
|
|
1933
|
+
// src/generate-text/execute-tool-call.ts
|
|
1934
|
+
import { executeTool } from "@ai-sdk/provider-utils";
|
|
1935
|
+
async function executeToolCall({
|
|
1936
|
+
toolCall,
|
|
1937
|
+
tools,
|
|
1938
|
+
tracer,
|
|
1939
|
+
telemetry,
|
|
1940
|
+
messages,
|
|
1941
|
+
abortSignal,
|
|
1942
|
+
experimental_context,
|
|
1943
|
+
onPreliminaryToolResult
|
|
1944
|
+
}) {
|
|
1945
|
+
const { toolName, toolCallId, input } = toolCall;
|
|
1946
|
+
const tool3 = tools == null ? void 0 : tools[toolName];
|
|
1947
|
+
if ((tool3 == null ? void 0 : tool3.execute) == null) {
|
|
1948
|
+
return void 0;
|
|
1949
|
+
}
|
|
1950
|
+
return recordSpan({
|
|
1951
|
+
name: "ai.toolCall",
|
|
1952
|
+
attributes: selectTelemetryAttributes({
|
|
1953
|
+
telemetry,
|
|
1954
|
+
attributes: {
|
|
1955
|
+
...assembleOperationName({
|
|
1956
|
+
operationId: "ai.toolCall",
|
|
1957
|
+
telemetry
|
|
1958
|
+
}),
|
|
1959
|
+
"ai.toolCall.name": toolName,
|
|
1960
|
+
"ai.toolCall.id": toolCallId,
|
|
1961
|
+
"ai.toolCall.args": {
|
|
1962
|
+
output: () => JSON.stringify(input)
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
}),
|
|
1966
|
+
tracer,
|
|
1967
|
+
fn: async (span) => {
|
|
1968
|
+
let output;
|
|
1969
|
+
try {
|
|
1970
|
+
const stream = executeTool({
|
|
1971
|
+
execute: tool3.execute.bind(tool3),
|
|
1972
|
+
input,
|
|
1973
|
+
options: {
|
|
1974
|
+
toolCallId,
|
|
1975
|
+
messages,
|
|
1976
|
+
abortSignal,
|
|
1977
|
+
experimental_context
|
|
1978
|
+
}
|
|
1979
|
+
});
|
|
1980
|
+
for await (const part of stream) {
|
|
1981
|
+
if (part.type === "preliminary") {
|
|
1982
|
+
onPreliminaryToolResult == null ? void 0 : onPreliminaryToolResult({
|
|
1983
|
+
...toolCall,
|
|
1984
|
+
type: "tool-result",
|
|
1985
|
+
output: part.output,
|
|
1986
|
+
preliminary: true
|
|
1987
|
+
});
|
|
1988
|
+
} else {
|
|
1989
|
+
output = part.output;
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
} catch (error) {
|
|
1993
|
+
recordErrorOnSpan(span, error);
|
|
1994
|
+
return {
|
|
1995
|
+
type: "tool-error",
|
|
1996
|
+
toolCallId,
|
|
1997
|
+
toolName,
|
|
1998
|
+
input,
|
|
1999
|
+
error,
|
|
2000
|
+
dynamic: tool3.type === "dynamic"
|
|
2001
|
+
};
|
|
2002
|
+
}
|
|
2003
|
+
try {
|
|
2004
|
+
span.setAttributes(
|
|
2005
|
+
selectTelemetryAttributes({
|
|
2006
|
+
telemetry,
|
|
2007
|
+
attributes: {
|
|
2008
|
+
"ai.toolCall.result": {
|
|
2009
|
+
output: () => JSON.stringify(output)
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
})
|
|
2013
|
+
);
|
|
2014
|
+
} catch (ignored) {
|
|
2015
|
+
}
|
|
2016
|
+
return {
|
|
2017
|
+
type: "tool-result",
|
|
2018
|
+
toolCallId,
|
|
2019
|
+
toolName,
|
|
2020
|
+
input,
|
|
2021
|
+
output,
|
|
2022
|
+
dynamic: tool3.type === "dynamic"
|
|
2023
|
+
};
|
|
2024
|
+
}
|
|
2025
|
+
});
|
|
2026
|
+
}
|
|
2027
|
+
|
|
1782
2028
|
// src/generate-text/extract-text-content.ts
|
|
1783
2029
|
function extractTextContent(content) {
|
|
1784
2030
|
const parts = content.filter(
|
|
@@ -1827,6 +2073,26 @@ var DefaultGeneratedFileWithType = class extends DefaultGeneratedFile {
|
|
|
1827
2073
|
}
|
|
1828
2074
|
};
|
|
1829
2075
|
|
|
2076
|
+
// src/generate-text/is-approval-needed.ts
|
|
2077
|
+
async function isApprovalNeeded({
|
|
2078
|
+
tool: tool3,
|
|
2079
|
+
toolCall,
|
|
2080
|
+
messages,
|
|
2081
|
+
experimental_context
|
|
2082
|
+
}) {
|
|
2083
|
+
if (tool3.needsApproval == null) {
|
|
2084
|
+
return false;
|
|
2085
|
+
}
|
|
2086
|
+
if (typeof tool3.needsApproval === "boolean") {
|
|
2087
|
+
return tool3.needsApproval;
|
|
2088
|
+
}
|
|
2089
|
+
return await tool3.needsApproval(toolCall.input, {
|
|
2090
|
+
toolCallId: toolCall.toolCallId,
|
|
2091
|
+
messages,
|
|
2092
|
+
experimental_context
|
|
2093
|
+
});
|
|
2094
|
+
}
|
|
2095
|
+
|
|
1830
2096
|
// src/generate-text/parse-tool-call.ts
|
|
1831
2097
|
import {
|
|
1832
2098
|
asSchema as asSchema2,
|
|
@@ -2008,29 +2274,6 @@ async function isStopConditionMet({
|
|
|
2008
2274
|
return (await Promise.all(stopConditions.map((condition) => condition({ steps })))).some((result) => result);
|
|
2009
2275
|
}
|
|
2010
2276
|
|
|
2011
|
-
// src/prompt/create-tool-model-output.ts
|
|
2012
|
-
import {
|
|
2013
|
-
getErrorMessage as getErrorMessage4
|
|
2014
|
-
} from "@ai-sdk/provider";
|
|
2015
|
-
function createToolModelOutput({
|
|
2016
|
-
output,
|
|
2017
|
-
tool: tool3,
|
|
2018
|
-
errorMode
|
|
2019
|
-
}) {
|
|
2020
|
-
if (errorMode === "text") {
|
|
2021
|
-
return { type: "error-text", value: getErrorMessage4(output) };
|
|
2022
|
-
} else if (errorMode === "json") {
|
|
2023
|
-
return { type: "error-json", value: toJSONValue(output) };
|
|
2024
|
-
}
|
|
2025
|
-
if (tool3 == null ? void 0 : tool3.toModelOutput) {
|
|
2026
|
-
return tool3.toModelOutput(output);
|
|
2027
|
-
}
|
|
2028
|
-
return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: toJSONValue(output) };
|
|
2029
|
-
}
|
|
2030
|
-
function toJSONValue(value) {
|
|
2031
|
-
return value === void 0 ? null : value;
|
|
2032
|
-
}
|
|
2033
|
-
|
|
2034
2277
|
// src/generate-text/to-response-messages.ts
|
|
2035
2278
|
function toResponseMessages({
|
|
2036
2279
|
content: inputContent,
|
|
@@ -2094,6 +2337,12 @@ function toResponseMessages({
|
|
|
2094
2337
|
}),
|
|
2095
2338
|
providerOptions: part.providerMetadata
|
|
2096
2339
|
};
|
|
2340
|
+
case "tool-approval-request":
|
|
2341
|
+
return {
|
|
2342
|
+
type: "tool-approval-request",
|
|
2343
|
+
approvalId: part.approvalId,
|
|
2344
|
+
toolCallId: part.toolCall.toolCallId
|
|
2345
|
+
};
|
|
2097
2346
|
}
|
|
2098
2347
|
});
|
|
2099
2348
|
if (content.length > 0) {
|
|
@@ -2148,10 +2397,11 @@ async function generateText({
|
|
|
2148
2397
|
experimental_download: download2,
|
|
2149
2398
|
experimental_context,
|
|
2150
2399
|
_internal: {
|
|
2151
|
-
generateId:
|
|
2400
|
+
generateId: generateId2 = originalGenerateId,
|
|
2152
2401
|
currentDate = () => /* @__PURE__ */ new Date()
|
|
2153
2402
|
} = {},
|
|
2154
2403
|
onStepFinish,
|
|
2404
|
+
onFinish,
|
|
2155
2405
|
...settings
|
|
2156
2406
|
}) {
|
|
2157
2407
|
const model = resolveLanguageModel(modelArg);
|
|
@@ -2200,17 +2450,55 @@ async function generateText({
|
|
|
2200
2450
|
tracer,
|
|
2201
2451
|
fn: async (span) => {
|
|
2202
2452
|
var _a17, _b, _c, _d, _e, _f, _g;
|
|
2453
|
+
const initialMessages = initialPrompt.messages;
|
|
2454
|
+
const responseMessages = [];
|
|
2455
|
+
const { approvedToolApprovals, deniedToolApprovals } = collectToolApprovals({ messages: initialMessages });
|
|
2456
|
+
if (deniedToolApprovals.length > 0 || approvedToolApprovals.length > 0) {
|
|
2457
|
+
const toolOutputs = await executeTools({
|
|
2458
|
+
toolCalls: approvedToolApprovals.map(
|
|
2459
|
+
(toolApproval) => toolApproval.toolCall
|
|
2460
|
+
),
|
|
2461
|
+
tools,
|
|
2462
|
+
tracer,
|
|
2463
|
+
telemetry,
|
|
2464
|
+
messages: initialMessages,
|
|
2465
|
+
abortSignal,
|
|
2466
|
+
experimental_context
|
|
2467
|
+
});
|
|
2468
|
+
responseMessages.push({
|
|
2469
|
+
role: "tool",
|
|
2470
|
+
content: [
|
|
2471
|
+
// add regular tool results for approved tool calls:
|
|
2472
|
+
...toolOutputs.map((output2) => ({
|
|
2473
|
+
type: "tool-result",
|
|
2474
|
+
toolCallId: output2.toolCallId,
|
|
2475
|
+
toolName: output2.toolName,
|
|
2476
|
+
output: createToolModelOutput({
|
|
2477
|
+
tool: tools == null ? void 0 : tools[output2.toolName],
|
|
2478
|
+
output: output2.type === "tool-result" ? output2.output : output2.error,
|
|
2479
|
+
errorMode: output2.type === "tool-error" ? "json" : "none"
|
|
2480
|
+
})
|
|
2481
|
+
})),
|
|
2482
|
+
// add execution denied tool results for denied tool approvals:
|
|
2483
|
+
...deniedToolApprovals.map((toolApproval) => ({
|
|
2484
|
+
type: "tool-result",
|
|
2485
|
+
toolCallId: toolApproval.toolCall.toolCallId,
|
|
2486
|
+
toolName: toolApproval.toolCall.toolName,
|
|
2487
|
+
output: {
|
|
2488
|
+
type: "execution-denied",
|
|
2489
|
+
reason: toolApproval.approvalResponse.reason
|
|
2490
|
+
}
|
|
2491
|
+
}))
|
|
2492
|
+
]
|
|
2493
|
+
});
|
|
2494
|
+
}
|
|
2203
2495
|
const callSettings2 = prepareCallSettings(settings);
|
|
2204
2496
|
let currentModelResponse;
|
|
2205
2497
|
let clientToolCalls = [];
|
|
2206
2498
|
let clientToolOutputs = [];
|
|
2207
|
-
const responseMessages = [];
|
|
2208
2499
|
const steps = [];
|
|
2209
2500
|
do {
|
|
2210
|
-
const stepInputMessages = [
|
|
2211
|
-
...initialPrompt.messages,
|
|
2212
|
-
...responseMessages
|
|
2213
|
-
];
|
|
2501
|
+
const stepInputMessages = [...initialMessages, ...responseMessages];
|
|
2214
2502
|
const prepareStepResult = await (prepareStep == null ? void 0 : prepareStep({
|
|
2215
2503
|
model,
|
|
2216
2504
|
steps,
|
|
@@ -2286,7 +2574,7 @@ async function generateText({
|
|
|
2286
2574
|
headers: headersWithUserAgent
|
|
2287
2575
|
});
|
|
2288
2576
|
const responseData = {
|
|
2289
|
-
id: (_b2 = (_a19 = result.response) == null ? void 0 : _a19.id) != null ? _b2 :
|
|
2577
|
+
id: (_b2 = (_a19 = result.response) == null ? void 0 : _a19.id) != null ? _b2 : generateId2(),
|
|
2290
2578
|
timestamp: (_d2 = (_c2 = result.response) == null ? void 0 : _c2.timestamp) != null ? _d2 : currentDate(),
|
|
2291
2579
|
modelId: (_f2 = (_e2 = result.response) == null ? void 0 : _e2.modelId) != null ? _f2 : stepModel.modelId,
|
|
2292
2580
|
headers: (_g2 = result.response) == null ? void 0 : _g2.headers,
|
|
@@ -2342,6 +2630,7 @@ async function generateText({
|
|
|
2342
2630
|
})
|
|
2343
2631
|
)
|
|
2344
2632
|
);
|
|
2633
|
+
const toolApprovalRequests = {};
|
|
2345
2634
|
for (const toolCall of stepToolCalls) {
|
|
2346
2635
|
if (toolCall.invalid) {
|
|
2347
2636
|
continue;
|
|
@@ -2356,6 +2645,18 @@ async function generateText({
|
|
|
2356
2645
|
experimental_context
|
|
2357
2646
|
});
|
|
2358
2647
|
}
|
|
2648
|
+
if (await isApprovalNeeded({
|
|
2649
|
+
tool: tool3,
|
|
2650
|
+
toolCall,
|
|
2651
|
+
messages: stepInputMessages,
|
|
2652
|
+
experimental_context
|
|
2653
|
+
})) {
|
|
2654
|
+
toolApprovalRequests[toolCall.toolCallId] = {
|
|
2655
|
+
type: "tool-approval-request",
|
|
2656
|
+
approvalId: generateId2(),
|
|
2657
|
+
toolCall
|
|
2658
|
+
};
|
|
2659
|
+
}
|
|
2359
2660
|
}
|
|
2360
2661
|
const invalidToolCalls = stepToolCalls.filter(
|
|
2361
2662
|
(toolCall) => toolCall.invalid && toolCall.dynamic
|
|
@@ -2378,7 +2679,7 @@ async function generateText({
|
|
|
2378
2679
|
clientToolOutputs.push(
|
|
2379
2680
|
...await executeTools({
|
|
2380
2681
|
toolCalls: clientToolCalls.filter(
|
|
2381
|
-
(toolCall) => !toolCall.invalid
|
|
2682
|
+
(toolCall) => !toolCall.invalid && toolApprovalRequests[toolCall.toolCallId] == null
|
|
2382
2683
|
),
|
|
2383
2684
|
tools,
|
|
2384
2685
|
tracer,
|
|
@@ -2392,7 +2693,8 @@ async function generateText({
|
|
|
2392
2693
|
const stepContent = asContent({
|
|
2393
2694
|
content: currentModelResponse.content,
|
|
2394
2695
|
toolCalls: stepToolCalls,
|
|
2395
|
-
toolOutputs: clientToolOutputs
|
|
2696
|
+
toolOutputs: clientToolOutputs,
|
|
2697
|
+
toolApprovalRequests: Object.values(toolApprovalRequests)
|
|
2396
2698
|
});
|
|
2397
2699
|
responseMessages.push(
|
|
2398
2700
|
...toResponseMessages({
|
|
@@ -2446,8 +2748,43 @@ async function generateText({
|
|
|
2446
2748
|
})
|
|
2447
2749
|
);
|
|
2448
2750
|
const lastStep = steps[steps.length - 1];
|
|
2751
|
+
const totalUsage = steps.reduce(
|
|
2752
|
+
(totalUsage2, step) => {
|
|
2753
|
+
return addLanguageModelUsage(totalUsage2, step.usage);
|
|
2754
|
+
},
|
|
2755
|
+
{
|
|
2756
|
+
inputTokens: void 0,
|
|
2757
|
+
outputTokens: void 0,
|
|
2758
|
+
totalTokens: void 0,
|
|
2759
|
+
reasoningTokens: void 0,
|
|
2760
|
+
cachedInputTokens: void 0
|
|
2761
|
+
}
|
|
2762
|
+
);
|
|
2763
|
+
await (onFinish == null ? void 0 : onFinish({
|
|
2764
|
+
finishReason: lastStep.finishReason,
|
|
2765
|
+
usage: lastStep.usage,
|
|
2766
|
+
content: lastStep.content,
|
|
2767
|
+
text: lastStep.text,
|
|
2768
|
+
reasoningText: lastStep.reasoningText,
|
|
2769
|
+
reasoning: lastStep.reasoning,
|
|
2770
|
+
files: lastStep.files,
|
|
2771
|
+
sources: lastStep.sources,
|
|
2772
|
+
toolCalls: lastStep.toolCalls,
|
|
2773
|
+
staticToolCalls: lastStep.staticToolCalls,
|
|
2774
|
+
dynamicToolCalls: lastStep.dynamicToolCalls,
|
|
2775
|
+
toolResults: lastStep.toolResults,
|
|
2776
|
+
staticToolResults: lastStep.staticToolResults,
|
|
2777
|
+
dynamicToolResults: lastStep.dynamicToolResults,
|
|
2778
|
+
request: lastStep.request,
|
|
2779
|
+
response: lastStep.response,
|
|
2780
|
+
warnings: lastStep.warnings,
|
|
2781
|
+
providerMetadata: lastStep.providerMetadata,
|
|
2782
|
+
steps,
|
|
2783
|
+
totalUsage
|
|
2784
|
+
}));
|
|
2449
2785
|
return new DefaultGenerateTextResult({
|
|
2450
2786
|
steps,
|
|
2787
|
+
totalUsage,
|
|
2451
2788
|
resolvedOutput: await (output == null ? void 0 : output.parseOutput(
|
|
2452
2789
|
{ text: lastStep.text },
|
|
2453
2790
|
{
|
|
@@ -2473,81 +2810,17 @@ async function executeTools({
|
|
|
2473
2810
|
experimental_context
|
|
2474
2811
|
}) {
|
|
2475
2812
|
const toolOutputs = await Promise.all(
|
|
2476
|
-
toolCalls.map(
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
}
|
|
2481
|
-
return recordSpan({
|
|
2482
|
-
name: "ai.toolCall",
|
|
2483
|
-
attributes: selectTelemetryAttributes({
|
|
2484
|
-
telemetry,
|
|
2485
|
-
attributes: {
|
|
2486
|
-
...assembleOperationName({
|
|
2487
|
-
operationId: "ai.toolCall",
|
|
2488
|
-
telemetry
|
|
2489
|
-
}),
|
|
2490
|
-
"ai.toolCall.name": toolName,
|
|
2491
|
-
"ai.toolCall.id": toolCallId,
|
|
2492
|
-
"ai.toolCall.args": {
|
|
2493
|
-
output: () => JSON.stringify(input)
|
|
2494
|
-
}
|
|
2495
|
-
}
|
|
2496
|
-
}),
|
|
2813
|
+
toolCalls.map(
|
|
2814
|
+
async (toolCall) => executeToolCall({
|
|
2815
|
+
toolCall,
|
|
2816
|
+
tools,
|
|
2497
2817
|
tracer,
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
toolCallId,
|
|
2505
|
-
messages,
|
|
2506
|
-
abortSignal,
|
|
2507
|
-
experimental_context
|
|
2508
|
-
}
|
|
2509
|
-
});
|
|
2510
|
-
let output;
|
|
2511
|
-
for await (const part of stream) {
|
|
2512
|
-
if (part.type === "final") {
|
|
2513
|
-
output = part.output;
|
|
2514
|
-
}
|
|
2515
|
-
}
|
|
2516
|
-
try {
|
|
2517
|
-
span.setAttributes(
|
|
2518
|
-
selectTelemetryAttributes({
|
|
2519
|
-
telemetry,
|
|
2520
|
-
attributes: {
|
|
2521
|
-
"ai.toolCall.result": {
|
|
2522
|
-
output: () => JSON.stringify(output)
|
|
2523
|
-
}
|
|
2524
|
-
}
|
|
2525
|
-
})
|
|
2526
|
-
);
|
|
2527
|
-
} catch (ignored) {
|
|
2528
|
-
}
|
|
2529
|
-
return {
|
|
2530
|
-
type: "tool-result",
|
|
2531
|
-
toolCallId,
|
|
2532
|
-
toolName,
|
|
2533
|
-
input,
|
|
2534
|
-
output,
|
|
2535
|
-
dynamic: tool3.type === "dynamic"
|
|
2536
|
-
};
|
|
2537
|
-
} catch (error) {
|
|
2538
|
-
recordErrorOnSpan(span, error);
|
|
2539
|
-
return {
|
|
2540
|
-
type: "tool-error",
|
|
2541
|
-
toolCallId,
|
|
2542
|
-
toolName,
|
|
2543
|
-
input,
|
|
2544
|
-
error,
|
|
2545
|
-
dynamic: tool3.type === "dynamic"
|
|
2546
|
-
};
|
|
2547
|
-
}
|
|
2548
|
-
}
|
|
2549
|
-
});
|
|
2550
|
-
})
|
|
2818
|
+
telemetry,
|
|
2819
|
+
messages,
|
|
2820
|
+
abortSignal,
|
|
2821
|
+
experimental_context
|
|
2822
|
+
})
|
|
2823
|
+
)
|
|
2551
2824
|
);
|
|
2552
2825
|
return toolOutputs.filter(
|
|
2553
2826
|
(output) => output != null
|
|
@@ -2557,6 +2830,7 @@ var DefaultGenerateTextResult = class {
|
|
|
2557
2830
|
constructor(options) {
|
|
2558
2831
|
this.steps = options.steps;
|
|
2559
2832
|
this.resolvedOutput = options.resolvedOutput;
|
|
2833
|
+
this.totalUsage = options.totalUsage;
|
|
2560
2834
|
}
|
|
2561
2835
|
get finalStep() {
|
|
2562
2836
|
return this.steps[this.steps.length - 1];
|
|
@@ -2615,20 +2889,6 @@ var DefaultGenerateTextResult = class {
|
|
|
2615
2889
|
get usage() {
|
|
2616
2890
|
return this.finalStep.usage;
|
|
2617
2891
|
}
|
|
2618
|
-
get totalUsage() {
|
|
2619
|
-
return this.steps.reduce(
|
|
2620
|
-
(totalUsage, step) => {
|
|
2621
|
-
return addLanguageModelUsage(totalUsage, step.usage);
|
|
2622
|
-
},
|
|
2623
|
-
{
|
|
2624
|
-
inputTokens: void 0,
|
|
2625
|
-
outputTokens: void 0,
|
|
2626
|
-
totalTokens: void 0,
|
|
2627
|
-
reasoningTokens: void 0,
|
|
2628
|
-
cachedInputTokens: void 0
|
|
2629
|
-
}
|
|
2630
|
-
);
|
|
2631
|
-
}
|
|
2632
2892
|
get experimental_output() {
|
|
2633
2893
|
if (this.resolvedOutput == null) {
|
|
2634
2894
|
throw new NoOutputSpecifiedError();
|
|
@@ -2652,7 +2912,8 @@ function asToolCalls(content) {
|
|
|
2652
2912
|
function asContent({
|
|
2653
2913
|
content,
|
|
2654
2914
|
toolCalls,
|
|
2655
|
-
toolOutputs
|
|
2915
|
+
toolOutputs,
|
|
2916
|
+
toolApprovalRequests
|
|
2656
2917
|
}) {
|
|
2657
2918
|
return [
|
|
2658
2919
|
...content.map((part) => {
|
|
@@ -2702,7 +2963,8 @@ function asContent({
|
|
|
2702
2963
|
}
|
|
2703
2964
|
}
|
|
2704
2965
|
}),
|
|
2705
|
-
...toolOutputs
|
|
2966
|
+
...toolOutputs,
|
|
2967
|
+
...toolApprovalRequests
|
|
2706
2968
|
];
|
|
2707
2969
|
}
|
|
2708
2970
|
|
|
@@ -2908,6 +3170,11 @@ var uiMessageChunkSchema = z7.union([
|
|
|
2908
3170
|
dynamic: z7.boolean().optional(),
|
|
2909
3171
|
errorText: z7.string()
|
|
2910
3172
|
}),
|
|
3173
|
+
z7.strictObject({
|
|
3174
|
+
type: z7.literal("tool-approval-request"),
|
|
3175
|
+
approvalId: z7.string(),
|
|
3176
|
+
toolCallId: z7.string()
|
|
3177
|
+
}),
|
|
2911
3178
|
z7.strictObject({
|
|
2912
3179
|
type: z7.literal("tool-output-available"),
|
|
2913
3180
|
toolCallId: z7.string(),
|
|
@@ -2923,6 +3190,10 @@ var uiMessageChunkSchema = z7.union([
|
|
|
2923
3190
|
providerExecuted: z7.boolean().optional(),
|
|
2924
3191
|
dynamic: z7.boolean().optional()
|
|
2925
3192
|
}),
|
|
3193
|
+
z7.strictObject({
|
|
3194
|
+
type: z7.literal("tool-output-denied"),
|
|
3195
|
+
toolCallId: z7.string()
|
|
3196
|
+
}),
|
|
2926
3197
|
z7.strictObject({
|
|
2927
3198
|
type: z7.literal("reasoning-start"),
|
|
2928
3199
|
id: z7.string(),
|
|
@@ -3707,6 +3978,21 @@ function processUIMessageStream({
|
|
|
3707
3978
|
write();
|
|
3708
3979
|
break;
|
|
3709
3980
|
}
|
|
3981
|
+
case "tool-approval-request": {
|
|
3982
|
+
const toolInvocation = getToolInvocation(chunk.toolCallId);
|
|
3983
|
+
toolInvocation.state = "approval-requested";
|
|
3984
|
+
toolInvocation.approval = {
|
|
3985
|
+
id: chunk.approvalId
|
|
3986
|
+
};
|
|
3987
|
+
write();
|
|
3988
|
+
break;
|
|
3989
|
+
}
|
|
3990
|
+
case "tool-output-denied": {
|
|
3991
|
+
const toolInvocation = getToolInvocation(chunk.toolCallId);
|
|
3992
|
+
toolInvocation.state = "output-denied";
|
|
3993
|
+
write();
|
|
3994
|
+
break;
|
|
3995
|
+
}
|
|
3710
3996
|
case "tool-output-available": {
|
|
3711
3997
|
if (chunk.dynamic) {
|
|
3712
3998
|
const toolInvocation = getDynamicToolInvocation(
|
|
@@ -4058,10 +4344,10 @@ function createStitchableStream() {
|
|
|
4058
4344
|
const { value, done } = await innerStreamReaders[0].read();
|
|
4059
4345
|
if (done) {
|
|
4060
4346
|
innerStreamReaders.shift();
|
|
4061
|
-
if (innerStreamReaders.length
|
|
4062
|
-
await processPull();
|
|
4063
|
-
} else if (isClosed) {
|
|
4347
|
+
if (innerStreamReaders.length === 0 && isClosed) {
|
|
4064
4348
|
controller == null ? void 0 : controller.close();
|
|
4349
|
+
} else {
|
|
4350
|
+
await processPull();
|
|
4065
4351
|
}
|
|
4066
4352
|
} else {
|
|
4067
4353
|
controller == null ? void 0 : controller.enqueue(value);
|
|
@@ -4158,8 +4444,6 @@ function now() {
|
|
|
4158
4444
|
|
|
4159
4445
|
// src/generate-text/run-tools-transformation.ts
|
|
4160
4446
|
import {
|
|
4161
|
-
executeTool as executeTool2,
|
|
4162
|
-
generateId,
|
|
4163
4447
|
getErrorMessage as getErrorMessage6
|
|
4164
4448
|
} from "@ai-sdk/provider-utils";
|
|
4165
4449
|
function runToolsTransformation({
|
|
@@ -4171,7 +4455,8 @@ function runToolsTransformation({
|
|
|
4171
4455
|
messages,
|
|
4172
4456
|
abortSignal,
|
|
4173
4457
|
repairToolCall,
|
|
4174
|
-
experimental_context
|
|
4458
|
+
experimental_context,
|
|
4459
|
+
generateId: generateId2
|
|
4175
4460
|
}) {
|
|
4176
4461
|
let toolResultsStreamController = null;
|
|
4177
4462
|
const toolResultsStream = new ReadableStream({
|
|
@@ -4253,7 +4538,6 @@ function runToolsTransformation({
|
|
|
4253
4538
|
break;
|
|
4254
4539
|
}
|
|
4255
4540
|
const tool3 = tools[toolCall.toolName];
|
|
4256
|
-
toolInputs.set(toolCall.toolCallId, toolCall.input);
|
|
4257
4541
|
if (tool3.onInputAvailable != null) {
|
|
4258
4542
|
await tool3.onInputAvailable({
|
|
4259
4543
|
input: toolCall.input,
|
|
@@ -4263,79 +4547,38 @@ function runToolsTransformation({
|
|
|
4263
4547
|
experimental_context
|
|
4264
4548
|
});
|
|
4265
4549
|
}
|
|
4550
|
+
if (await isApprovalNeeded({
|
|
4551
|
+
tool: tool3,
|
|
4552
|
+
toolCall,
|
|
4553
|
+
messages,
|
|
4554
|
+
experimental_context
|
|
4555
|
+
})) {
|
|
4556
|
+
toolResultsStreamController.enqueue({
|
|
4557
|
+
type: "tool-approval-request",
|
|
4558
|
+
approvalId: generateId2(),
|
|
4559
|
+
toolCall
|
|
4560
|
+
});
|
|
4561
|
+
break;
|
|
4562
|
+
}
|
|
4563
|
+
toolInputs.set(toolCall.toolCallId, toolCall.input);
|
|
4266
4564
|
if (tool3.execute != null && toolCall.providerExecuted !== true) {
|
|
4267
|
-
const toolExecutionId =
|
|
4565
|
+
const toolExecutionId = generateId2();
|
|
4268
4566
|
outstandingToolResults.add(toolExecutionId);
|
|
4269
|
-
|
|
4270
|
-
|
|
4271
|
-
|
|
4272
|
-
telemetry,
|
|
4273
|
-
attributes: {
|
|
4274
|
-
...assembleOperationName({
|
|
4275
|
-
operationId: "ai.toolCall",
|
|
4276
|
-
telemetry
|
|
4277
|
-
}),
|
|
4278
|
-
"ai.toolCall.name": toolCall.toolName,
|
|
4279
|
-
"ai.toolCall.id": toolCall.toolCallId,
|
|
4280
|
-
"ai.toolCall.args": {
|
|
4281
|
-
output: () => JSON.stringify(toolCall.input)
|
|
4282
|
-
}
|
|
4283
|
-
}
|
|
4284
|
-
}),
|
|
4567
|
+
executeToolCall({
|
|
4568
|
+
toolCall,
|
|
4569
|
+
tools,
|
|
4285
4570
|
tracer,
|
|
4286
|
-
|
|
4287
|
-
|
|
4288
|
-
|
|
4289
|
-
|
|
4290
|
-
|
|
4291
|
-
|
|
4292
|
-
options: {
|
|
4293
|
-
toolCallId: toolCall.toolCallId,
|
|
4294
|
-
messages,
|
|
4295
|
-
abortSignal,
|
|
4296
|
-
experimental_context
|
|
4297
|
-
}
|
|
4298
|
-
});
|
|
4299
|
-
for await (const part of stream) {
|
|
4300
|
-
toolResultsStreamController.enqueue({
|
|
4301
|
-
...toolCall,
|
|
4302
|
-
type: "tool-result",
|
|
4303
|
-
output: part.output,
|
|
4304
|
-
...part.type === "preliminary" && {
|
|
4305
|
-
preliminary: true
|
|
4306
|
-
}
|
|
4307
|
-
});
|
|
4308
|
-
if (part.type === "final") {
|
|
4309
|
-
output = part.output;
|
|
4310
|
-
}
|
|
4311
|
-
}
|
|
4312
|
-
} catch (error) {
|
|
4313
|
-
recordErrorOnSpan(span, error);
|
|
4314
|
-
toolResultsStreamController.enqueue({
|
|
4315
|
-
...toolCall,
|
|
4316
|
-
type: "tool-error",
|
|
4317
|
-
error
|
|
4318
|
-
});
|
|
4319
|
-
outstandingToolResults.delete(toolExecutionId);
|
|
4320
|
-
attemptClose();
|
|
4321
|
-
return;
|
|
4322
|
-
}
|
|
4323
|
-
outstandingToolResults.delete(toolExecutionId);
|
|
4324
|
-
attemptClose();
|
|
4325
|
-
try {
|
|
4326
|
-
span.setAttributes(
|
|
4327
|
-
selectTelemetryAttributes({
|
|
4328
|
-
telemetry,
|
|
4329
|
-
attributes: {
|
|
4330
|
-
"ai.toolCall.result": {
|
|
4331
|
-
output: () => JSON.stringify(output)
|
|
4332
|
-
}
|
|
4333
|
-
}
|
|
4334
|
-
})
|
|
4335
|
-
);
|
|
4336
|
-
} catch (ignored) {
|
|
4337
|
-
}
|
|
4571
|
+
telemetry,
|
|
4572
|
+
messages,
|
|
4573
|
+
abortSignal,
|
|
4574
|
+
experimental_context,
|
|
4575
|
+
onPreliminaryToolResult: (result) => {
|
|
4576
|
+
toolResultsStreamController.enqueue(result);
|
|
4338
4577
|
}
|
|
4578
|
+
}).then((result) => {
|
|
4579
|
+
toolResultsStreamController.enqueue(result);
|
|
4580
|
+
outstandingToolResults.delete(toolExecutionId);
|
|
4581
|
+
attemptClose();
|
|
4339
4582
|
});
|
|
4340
4583
|
}
|
|
4341
4584
|
} catch (error) {
|
|
@@ -4440,7 +4683,7 @@ function streamText({
|
|
|
4440
4683
|
experimental_context,
|
|
4441
4684
|
_internal: {
|
|
4442
4685
|
now: now2 = now,
|
|
4443
|
-
generateId:
|
|
4686
|
+
generateId: generateId2 = originalGenerateId2,
|
|
4444
4687
|
currentDate = () => /* @__PURE__ */ new Date()
|
|
4445
4688
|
} = {},
|
|
4446
4689
|
...settings
|
|
@@ -4472,7 +4715,7 @@ function streamText({
|
|
|
4472
4715
|
onStepFinish,
|
|
4473
4716
|
now: now2,
|
|
4474
4717
|
currentDate,
|
|
4475
|
-
generateId:
|
|
4718
|
+
generateId: generateId2,
|
|
4476
4719
|
experimental_context,
|
|
4477
4720
|
download: download2
|
|
4478
4721
|
});
|
|
@@ -4565,7 +4808,7 @@ var DefaultStreamTextResult = class {
|
|
|
4565
4808
|
includeRawChunks,
|
|
4566
4809
|
now: now2,
|
|
4567
4810
|
currentDate,
|
|
4568
|
-
generateId:
|
|
4811
|
+
generateId: generateId2,
|
|
4569
4812
|
onChunk,
|
|
4570
4813
|
onError,
|
|
4571
4814
|
onFinish,
|
|
@@ -4593,7 +4836,7 @@ var DefaultStreamTextResult = class {
|
|
|
4593
4836
|
let activeReasoningContent = {};
|
|
4594
4837
|
const eventProcessor = new TransformStream({
|
|
4595
4838
|
async transform(chunk, controller) {
|
|
4596
|
-
var _a17, _b, _c;
|
|
4839
|
+
var _a17, _b, _c, _d;
|
|
4597
4840
|
controller.enqueue(chunk);
|
|
4598
4841
|
const { part } = chunk;
|
|
4599
4842
|
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") {
|
|
@@ -4626,6 +4869,18 @@ var DefaultStreamTextResult = class {
|
|
|
4626
4869
|
activeText.providerMetadata = (_a17 = part.providerMetadata) != null ? _a17 : activeText.providerMetadata;
|
|
4627
4870
|
}
|
|
4628
4871
|
if (part.type === "text-end") {
|
|
4872
|
+
const activeText = activeTextContent[part.id];
|
|
4873
|
+
if (activeText == null) {
|
|
4874
|
+
controller.enqueue({
|
|
4875
|
+
part: {
|
|
4876
|
+
type: "error",
|
|
4877
|
+
error: `text part ${part.id} not found`
|
|
4878
|
+
},
|
|
4879
|
+
partialOutput: void 0
|
|
4880
|
+
});
|
|
4881
|
+
return;
|
|
4882
|
+
}
|
|
4883
|
+
activeText.providerMetadata = (_b = part.providerMetadata) != null ? _b : activeText.providerMetadata;
|
|
4629
4884
|
delete activeTextContent[part.id];
|
|
4630
4885
|
}
|
|
4631
4886
|
if (part.type === "reasoning-start") {
|
|
@@ -4649,7 +4904,7 @@ var DefaultStreamTextResult = class {
|
|
|
4649
4904
|
return;
|
|
4650
4905
|
}
|
|
4651
4906
|
activeReasoning.text += part.text;
|
|
4652
|
-
activeReasoning.providerMetadata = (
|
|
4907
|
+
activeReasoning.providerMetadata = (_c = part.providerMetadata) != null ? _c : activeReasoning.providerMetadata;
|
|
4653
4908
|
}
|
|
4654
4909
|
if (part.type === "reasoning-end") {
|
|
4655
4910
|
const activeReasoning = activeReasoningContent[part.id];
|
|
@@ -4663,7 +4918,7 @@ var DefaultStreamTextResult = class {
|
|
|
4663
4918
|
});
|
|
4664
4919
|
return;
|
|
4665
4920
|
}
|
|
4666
|
-
activeReasoning.providerMetadata = (
|
|
4921
|
+
activeReasoning.providerMetadata = (_d = part.providerMetadata) != null ? _d : activeReasoning.providerMetadata;
|
|
4667
4922
|
delete activeReasoningContent[part.id];
|
|
4668
4923
|
}
|
|
4669
4924
|
if (part.type === "file") {
|
|
@@ -4678,10 +4933,16 @@ var DefaultStreamTextResult = class {
|
|
|
4678
4933
|
if (part.type === "tool-result" && !part.preliminary) {
|
|
4679
4934
|
recordedContent.push(part);
|
|
4680
4935
|
}
|
|
4936
|
+
if (part.type === "tool-approval-request") {
|
|
4937
|
+
recordedContent.push(part);
|
|
4938
|
+
}
|
|
4681
4939
|
if (part.type === "tool-error") {
|
|
4682
4940
|
recordedContent.push(part);
|
|
4683
4941
|
}
|
|
4684
4942
|
if (part.type === "start-step") {
|
|
4943
|
+
recordedContent = [];
|
|
4944
|
+
activeReasoningContent = {};
|
|
4945
|
+
activeTextContent = {};
|
|
4685
4946
|
recordedRequest = part.request;
|
|
4686
4947
|
recordedWarnings = part.warnings;
|
|
4687
4948
|
}
|
|
@@ -4705,9 +4966,6 @@ var DefaultStreamTextResult = class {
|
|
|
4705
4966
|
await (onStepFinish == null ? void 0 : onStepFinish(currentStepResult));
|
|
4706
4967
|
logWarnings(recordedWarnings);
|
|
4707
4968
|
recordedSteps.push(currentStepResult);
|
|
4708
|
-
recordedContent = [];
|
|
4709
|
-
activeReasoningContent = {};
|
|
4710
|
-
activeTextContent = {};
|
|
4711
4969
|
recordedResponseMessages.push(...stepMessages);
|
|
4712
4970
|
stepFinish.resolve();
|
|
4713
4971
|
}
|
|
@@ -4867,6 +5125,82 @@ var DefaultStreamTextResult = class {
|
|
|
4867
5125
|
endWhenDone: false,
|
|
4868
5126
|
fn: async (rootSpanArg) => {
|
|
4869
5127
|
rootSpan = rootSpanArg;
|
|
5128
|
+
const initialPrompt = await standardizePrompt({
|
|
5129
|
+
system,
|
|
5130
|
+
prompt,
|
|
5131
|
+
messages
|
|
5132
|
+
});
|
|
5133
|
+
const initialMessages = initialPrompt.messages;
|
|
5134
|
+
const initialResponseMessages = [];
|
|
5135
|
+
const { approvedToolApprovals, deniedToolApprovals } = collectToolApprovals({ messages: initialMessages });
|
|
5136
|
+
if (deniedToolApprovals.length > 0 || approvedToolApprovals.length > 0) {
|
|
5137
|
+
let toolExecutionStepStreamController;
|
|
5138
|
+
const toolExecutionStepStream = new ReadableStream({
|
|
5139
|
+
start(controller) {
|
|
5140
|
+
toolExecutionStepStreamController = controller;
|
|
5141
|
+
}
|
|
5142
|
+
});
|
|
5143
|
+
self.addStream(toolExecutionStepStream);
|
|
5144
|
+
try {
|
|
5145
|
+
for (const toolApproval of deniedToolApprovals) {
|
|
5146
|
+
toolExecutionStepStreamController == null ? void 0 : toolExecutionStepStreamController.enqueue({
|
|
5147
|
+
type: "tool-output-denied",
|
|
5148
|
+
toolCallId: toolApproval.toolCall.toolCallId,
|
|
5149
|
+
toolName: toolApproval.toolCall.toolName
|
|
5150
|
+
});
|
|
5151
|
+
}
|
|
5152
|
+
const toolOutputs = [];
|
|
5153
|
+
await Promise.all(
|
|
5154
|
+
approvedToolApprovals.map(async (toolApproval) => {
|
|
5155
|
+
const result = await executeToolCall({
|
|
5156
|
+
toolCall: toolApproval.toolCall,
|
|
5157
|
+
tools,
|
|
5158
|
+
tracer,
|
|
5159
|
+
telemetry,
|
|
5160
|
+
messages: initialMessages,
|
|
5161
|
+
abortSignal,
|
|
5162
|
+
experimental_context,
|
|
5163
|
+
onPreliminaryToolResult: (result2) => {
|
|
5164
|
+
toolExecutionStepStreamController == null ? void 0 : toolExecutionStepStreamController.enqueue(result2);
|
|
5165
|
+
}
|
|
5166
|
+
});
|
|
5167
|
+
if (result != null) {
|
|
5168
|
+
toolExecutionStepStreamController == null ? void 0 : toolExecutionStepStreamController.enqueue(result);
|
|
5169
|
+
toolOutputs.push(result);
|
|
5170
|
+
}
|
|
5171
|
+
})
|
|
5172
|
+
);
|
|
5173
|
+
initialResponseMessages.push({
|
|
5174
|
+
role: "tool",
|
|
5175
|
+
content: [
|
|
5176
|
+
// add regular tool results for approved tool calls:
|
|
5177
|
+
...toolOutputs.map((output2) => ({
|
|
5178
|
+
type: "tool-result",
|
|
5179
|
+
toolCallId: output2.toolCallId,
|
|
5180
|
+
toolName: output2.toolName,
|
|
5181
|
+
output: createToolModelOutput({
|
|
5182
|
+
tool: tools == null ? void 0 : tools[output2.toolName],
|
|
5183
|
+
output: output2.type === "tool-result" ? output2.output : output2.error,
|
|
5184
|
+
errorMode: output2.type === "tool-error" ? "json" : "none"
|
|
5185
|
+
})
|
|
5186
|
+
})),
|
|
5187
|
+
// add execution denied tool results for denied tool approvals:
|
|
5188
|
+
...deniedToolApprovals.map((toolApproval) => ({
|
|
5189
|
+
type: "tool-result",
|
|
5190
|
+
toolCallId: toolApproval.toolCall.toolCallId,
|
|
5191
|
+
toolName: toolApproval.toolCall.toolName,
|
|
5192
|
+
output: {
|
|
5193
|
+
type: "execution-denied",
|
|
5194
|
+
reason: toolApproval.approvalResponse.reason
|
|
5195
|
+
}
|
|
5196
|
+
}))
|
|
5197
|
+
]
|
|
5198
|
+
});
|
|
5199
|
+
} finally {
|
|
5200
|
+
toolExecutionStepStreamController == null ? void 0 : toolExecutionStepStreamController.close();
|
|
5201
|
+
}
|
|
5202
|
+
}
|
|
5203
|
+
recordedResponseMessages.push(...initialResponseMessages);
|
|
4870
5204
|
async function streamStep({
|
|
4871
5205
|
currentStep,
|
|
4872
5206
|
responseMessages,
|
|
@@ -4875,15 +5209,7 @@ var DefaultStreamTextResult = class {
|
|
|
4875
5209
|
var _a17, _b, _c, _d, _e;
|
|
4876
5210
|
const includeRawChunks2 = self.includeRawChunks;
|
|
4877
5211
|
stepFinish = new DelayedPromise();
|
|
4878
|
-
const
|
|
4879
|
-
system,
|
|
4880
|
-
prompt,
|
|
4881
|
-
messages
|
|
4882
|
-
});
|
|
4883
|
-
const stepInputMessages = [
|
|
4884
|
-
...initialPrompt.messages,
|
|
4885
|
-
...responseMessages
|
|
4886
|
-
];
|
|
5212
|
+
const stepInputMessages = [...initialMessages, ...responseMessages];
|
|
4887
5213
|
const prepareStepResult = await (prepareStep == null ? void 0 : prepareStep({
|
|
4888
5214
|
model,
|
|
4889
5215
|
steps: recordedSteps,
|
|
@@ -4949,24 +5275,22 @@ var DefaultStreamTextResult = class {
|
|
|
4949
5275
|
}),
|
|
4950
5276
|
tracer,
|
|
4951
5277
|
endWhenDone: false,
|
|
4952
|
-
fn: async (doStreamSpan2) => {
|
|
4953
|
-
|
|
4954
|
-
|
|
4955
|
-
|
|
4956
|
-
|
|
4957
|
-
|
|
4958
|
-
|
|
4959
|
-
|
|
4960
|
-
|
|
4961
|
-
|
|
4962
|
-
|
|
4963
|
-
|
|
4964
|
-
|
|
4965
|
-
|
|
4966
|
-
|
|
4967
|
-
|
|
4968
|
-
};
|
|
4969
|
-
}
|
|
5278
|
+
fn: async (doStreamSpan2) => ({
|
|
5279
|
+
startTimestampMs: now2(),
|
|
5280
|
+
// get before the call
|
|
5281
|
+
doStreamSpan: doStreamSpan2,
|
|
5282
|
+
result: await stepModel.doStream({
|
|
5283
|
+
...callSettings,
|
|
5284
|
+
tools: stepTools,
|
|
5285
|
+
toolChoice: stepToolChoice,
|
|
5286
|
+
responseFormat: output == null ? void 0 : output.responseFormat,
|
|
5287
|
+
prompt: promptMessages,
|
|
5288
|
+
providerOptions,
|
|
5289
|
+
abortSignal,
|
|
5290
|
+
headers,
|
|
5291
|
+
includeRawChunks: includeRawChunks2
|
|
5292
|
+
})
|
|
5293
|
+
})
|
|
4970
5294
|
})
|
|
4971
5295
|
);
|
|
4972
5296
|
const streamWithToolResults = runToolsTransformation({
|
|
@@ -4978,7 +5302,8 @@ var DefaultStreamTextResult = class {
|
|
|
4978
5302
|
messages: stepInputMessages,
|
|
4979
5303
|
repairToolCall,
|
|
4980
5304
|
abortSignal,
|
|
4981
|
-
experimental_context
|
|
5305
|
+
experimental_context,
|
|
5306
|
+
generateId: generateId2
|
|
4982
5307
|
});
|
|
4983
5308
|
const stepRequest = request != null ? request : {};
|
|
4984
5309
|
const stepToolCalls = [];
|
|
@@ -4994,7 +5319,7 @@ var DefaultStreamTextResult = class {
|
|
|
4994
5319
|
let stepProviderMetadata;
|
|
4995
5320
|
let stepFirstChunk = true;
|
|
4996
5321
|
let stepResponse = {
|
|
4997
|
-
id:
|
|
5322
|
+
id: generateId2(),
|
|
4998
5323
|
timestamp: currentDate(),
|
|
4999
5324
|
modelId: model.modelId
|
|
5000
5325
|
};
|
|
@@ -5025,6 +5350,7 @@ var DefaultStreamTextResult = class {
|
|
|
5025
5350
|
}
|
|
5026
5351
|
const chunkType = chunk.type;
|
|
5027
5352
|
switch (chunkType) {
|
|
5353
|
+
case "tool-approval-request":
|
|
5028
5354
|
case "text-start":
|
|
5029
5355
|
case "text-end": {
|
|
5030
5356
|
controller.enqueue(chunk);
|
|
@@ -5253,7 +5579,7 @@ var DefaultStreamTextResult = class {
|
|
|
5253
5579
|
}
|
|
5254
5580
|
await streamStep({
|
|
5255
5581
|
currentStep: 0,
|
|
5256
|
-
responseMessages:
|
|
5582
|
+
responseMessages: initialResponseMessages,
|
|
5257
5583
|
usage: {
|
|
5258
5584
|
inputTokens: void 0,
|
|
5259
5585
|
outputTokens: void 0,
|
|
@@ -5560,6 +5886,14 @@ var DefaultStreamTextResult = class {
|
|
|
5560
5886
|
}
|
|
5561
5887
|
break;
|
|
5562
5888
|
}
|
|
5889
|
+
case "tool-approval-request": {
|
|
5890
|
+
controller.enqueue({
|
|
5891
|
+
type: "tool-approval-request",
|
|
5892
|
+
approvalId: part.approvalId,
|
|
5893
|
+
toolCallId: part.toolCall.toolCallId
|
|
5894
|
+
});
|
|
5895
|
+
break;
|
|
5896
|
+
}
|
|
5563
5897
|
case "tool-result": {
|
|
5564
5898
|
const dynamic = isDynamic(part.toolCallId);
|
|
5565
5899
|
controller.enqueue({
|
|
@@ -5583,6 +5917,13 @@ var DefaultStreamTextResult = class {
|
|
|
5583
5917
|
});
|
|
5584
5918
|
break;
|
|
5585
5919
|
}
|
|
5920
|
+
case "tool-output-denied": {
|
|
5921
|
+
controller.enqueue({
|
|
5922
|
+
type: "tool-output-denied",
|
|
5923
|
+
toolCallId: part.toolCallId
|
|
5924
|
+
});
|
|
5925
|
+
break;
|
|
5926
|
+
}
|
|
5586
5927
|
case "error": {
|
|
5587
5928
|
controller.enqueue({
|
|
5588
5929
|
type: "error",
|
|
@@ -5827,6 +6168,13 @@ function convertToModelMessages(messages, options) {
|
|
|
5827
6168
|
providerExecuted: part.providerExecuted,
|
|
5828
6169
|
...part.callProviderMetadata != null ? { providerOptions: part.callProviderMetadata } : {}
|
|
5829
6170
|
});
|
|
6171
|
+
if (part.approval != null) {
|
|
6172
|
+
content.push({
|
|
6173
|
+
type: "tool-approval-request",
|
|
6174
|
+
approvalId: part.approval.id,
|
|
6175
|
+
toolCallId: part.toolCallId
|
|
6176
|
+
});
|
|
6177
|
+
}
|
|
5830
6178
|
if (part.providerExecuted === true && (part.state === "output-available" || part.state === "output-error")) {
|
|
5831
6179
|
content.push({
|
|
5832
6180
|
type: "tool-result",
|
|
@@ -5855,29 +6203,49 @@ function convertToModelMessages(messages, options) {
|
|
|
5855
6203
|
if (toolParts.length > 0) {
|
|
5856
6204
|
modelMessages.push({
|
|
5857
6205
|
role: "tool",
|
|
5858
|
-
content: toolParts.
|
|
5859
|
-
|
|
5860
|
-
|
|
5861
|
-
|
|
5862
|
-
|
|
5863
|
-
|
|
5864
|
-
|
|
5865
|
-
|
|
5866
|
-
|
|
5867
|
-
|
|
5868
|
-
|
|
5869
|
-
output: toolPart.state === "output-error" ? toolPart.errorText : toolPart.output,
|
|
5870
|
-
tool: (_a18 = options == null ? void 0 : options.tools) == null ? void 0 : _a18[toolName],
|
|
5871
|
-
errorMode: toolPart.state === "output-error" ? "text" : "none"
|
|
5872
|
-
})
|
|
5873
|
-
};
|
|
6206
|
+
content: toolParts.flatMap(
|
|
6207
|
+
(toolPart) => {
|
|
6208
|
+
var _a18, _b2, _c;
|
|
6209
|
+
const outputs = [];
|
|
6210
|
+
if (toolPart.type !== "dynamic-tool" && ((_a18 = toolPart.approval) == null ? void 0 : _a18.approved) != null) {
|
|
6211
|
+
outputs.push({
|
|
6212
|
+
type: "tool-approval-response",
|
|
6213
|
+
approvalId: toolPart.approval.id,
|
|
6214
|
+
approved: toolPart.approval.approved,
|
|
6215
|
+
reason: toolPart.approval.reason
|
|
6216
|
+
});
|
|
5874
6217
|
}
|
|
5875
|
-
|
|
5876
|
-
|
|
6218
|
+
switch (toolPart.state) {
|
|
6219
|
+
case "output-denied": {
|
|
6220
|
+
outputs.push({
|
|
6221
|
+
type: "tool-result",
|
|
6222
|
+
toolCallId: toolPart.toolCallId,
|
|
6223
|
+
toolName: getToolName(toolPart),
|
|
6224
|
+
output: {
|
|
6225
|
+
type: "error-text",
|
|
6226
|
+
value: (_b2 = toolPart.approval.reason) != null ? _b2 : "Tool execution denied."
|
|
6227
|
+
}
|
|
6228
|
+
});
|
|
6229
|
+
break;
|
|
6230
|
+
}
|
|
6231
|
+
case "output-error":
|
|
6232
|
+
case "output-available": {
|
|
6233
|
+
const toolName = toolPart.type === "dynamic-tool" ? toolPart.toolName : getToolName(toolPart);
|
|
6234
|
+
outputs.push({
|
|
6235
|
+
type: "tool-result",
|
|
6236
|
+
toolCallId: toolPart.toolCallId,
|
|
6237
|
+
toolName,
|
|
6238
|
+
output: createToolModelOutput({
|
|
6239
|
+
output: toolPart.state === "output-error" ? toolPart.errorText : toolPart.output,
|
|
6240
|
+
tool: (_c = options == null ? void 0 : options.tools) == null ? void 0 : _c[toolName],
|
|
6241
|
+
errorMode: toolPart.state === "output-error" ? "text" : "none"
|
|
6242
|
+
})
|
|
6243
|
+
});
|
|
6244
|
+
break;
|
|
6245
|
+
}
|
|
5877
6246
|
}
|
|
6247
|
+
return outputs;
|
|
5878
6248
|
}
|
|
5879
|
-
}).filter(
|
|
5880
|
-
(output) => output != null
|
|
5881
6249
|
)
|
|
5882
6250
|
});
|
|
5883
6251
|
}
|
|
@@ -5915,14 +6283,39 @@ var Agent = class {
|
|
|
5915
6283
|
constructor(settings) {
|
|
5916
6284
|
this.settings = settings;
|
|
5917
6285
|
}
|
|
6286
|
+
/**
|
|
6287
|
+
* The name of the agent.
|
|
6288
|
+
*/
|
|
6289
|
+
get name() {
|
|
6290
|
+
return this.settings.name;
|
|
6291
|
+
}
|
|
6292
|
+
/**
|
|
6293
|
+
* The tools that the agent can use.
|
|
6294
|
+
*/
|
|
5918
6295
|
get tools() {
|
|
5919
6296
|
return this.settings.tools;
|
|
5920
6297
|
}
|
|
6298
|
+
/**
|
|
6299
|
+
* Generates an output from the agent (non-streaming).
|
|
6300
|
+
*/
|
|
5921
6301
|
async generate(options) {
|
|
5922
|
-
|
|
6302
|
+
var _a17;
|
|
6303
|
+
return generateText({
|
|
6304
|
+
...this.settings,
|
|
6305
|
+
stopWhen: (_a17 = this.settings.stopWhen) != null ? _a17 : stepCountIs(20),
|
|
6306
|
+
...options
|
|
6307
|
+
});
|
|
5923
6308
|
}
|
|
6309
|
+
/**
|
|
6310
|
+
* Streams an output from the agent (streaming).
|
|
6311
|
+
*/
|
|
5924
6312
|
stream(options) {
|
|
5925
|
-
|
|
6313
|
+
var _a17;
|
|
6314
|
+
return streamText({
|
|
6315
|
+
...this.settings,
|
|
6316
|
+
stopWhen: (_a17 = this.settings.stopWhen) != null ? _a17 : stepCountIs(20),
|
|
6317
|
+
...options
|
|
6318
|
+
});
|
|
5926
6319
|
}
|
|
5927
6320
|
/**
|
|
5928
6321
|
* Creates a response object that streams UI messages to the client.
|
|
@@ -6318,7 +6711,7 @@ async function generateImage({
|
|
|
6318
6711
|
headers
|
|
6319
6712
|
}) {
|
|
6320
6713
|
var _a17, _b;
|
|
6321
|
-
if (model.specificationVersion !== "
|
|
6714
|
+
if (model.specificationVersion !== "v3") {
|
|
6322
6715
|
throw new UnsupportedModelVersionError({
|
|
6323
6716
|
version: model.specificationVersion,
|
|
6324
6717
|
provider: model.provider,
|
|
@@ -6892,7 +7285,7 @@ async function generateObject(options) {
|
|
|
6892
7285
|
experimental_download: download2,
|
|
6893
7286
|
providerOptions,
|
|
6894
7287
|
_internal: {
|
|
6895
|
-
generateId:
|
|
7288
|
+
generateId: generateId2 = originalGenerateId3,
|
|
6896
7289
|
currentDate = () => /* @__PURE__ */ new Date()
|
|
6897
7290
|
} = {},
|
|
6898
7291
|
...settings
|
|
@@ -7016,7 +7409,7 @@ async function generateObject(options) {
|
|
|
7016
7409
|
headers: headersWithUserAgent
|
|
7017
7410
|
});
|
|
7018
7411
|
const responseData = {
|
|
7019
|
-
id: (_b = (_a18 = result2.response) == null ? void 0 : _a18.id) != null ? _b :
|
|
7412
|
+
id: (_b = (_a18 = result2.response) == null ? void 0 : _a18.id) != null ? _b : generateId2(),
|
|
7020
7413
|
timestamp: (_d = (_c = result2.response) == null ? void 0 : _c.timestamp) != null ? _d : currentDate(),
|
|
7021
7414
|
modelId: (_f = (_e = result2.response) == null ? void 0 : _e.modelId) != null ? _f : model.modelId,
|
|
7022
7415
|
headers: (_g = result2.response) == null ? void 0 : _g.headers,
|
|
@@ -7295,7 +7688,7 @@ function streamObject(options) {
|
|
|
7295
7688
|
},
|
|
7296
7689
|
onFinish,
|
|
7297
7690
|
_internal: {
|
|
7298
|
-
generateId:
|
|
7691
|
+
generateId: generateId2 = originalGenerateId4,
|
|
7299
7692
|
currentDate = () => /* @__PURE__ */ new Date(),
|
|
7300
7693
|
now: now2 = now
|
|
7301
7694
|
} = {},
|
|
@@ -7337,7 +7730,7 @@ function streamObject(options) {
|
|
|
7337
7730
|
onError,
|
|
7338
7731
|
onFinish,
|
|
7339
7732
|
download: download2,
|
|
7340
|
-
generateId:
|
|
7733
|
+
generateId: generateId2,
|
|
7341
7734
|
currentDate,
|
|
7342
7735
|
now: now2
|
|
7343
7736
|
});
|
|
@@ -7361,7 +7754,7 @@ var DefaultStreamObjectResult = class {
|
|
|
7361
7754
|
onError,
|
|
7362
7755
|
onFinish,
|
|
7363
7756
|
download: download2,
|
|
7364
|
-
generateId:
|
|
7757
|
+
generateId: generateId2,
|
|
7365
7758
|
currentDate,
|
|
7366
7759
|
now: now2
|
|
7367
7760
|
}) {
|
|
@@ -7509,7 +7902,7 @@ var DefaultStreamObjectResult = class {
|
|
|
7509
7902
|
let accumulatedText = "";
|
|
7510
7903
|
let textDelta = "";
|
|
7511
7904
|
let fullResponse = {
|
|
7512
|
-
id:
|
|
7905
|
+
id: generateId2(),
|
|
7513
7906
|
timestamp: currentDate(),
|
|
7514
7907
|
modelId: model.modelId
|
|
7515
7908
|
};
|
|
@@ -7798,18 +8191,6 @@ var DefaultStreamObjectResult = class {
|
|
|
7798
8191
|
// src/generate-speech/generate-speech.ts
|
|
7799
8192
|
import { withUserAgentSuffix as withUserAgentSuffix7 } from "@ai-sdk/provider-utils";
|
|
7800
8193
|
|
|
7801
|
-
// src/error/no-speech-generated-error.ts
|
|
7802
|
-
import { AISDKError as AISDKError20 } from "@ai-sdk/provider";
|
|
7803
|
-
var NoSpeechGeneratedError = class extends AISDKError20 {
|
|
7804
|
-
constructor(options) {
|
|
7805
|
-
super({
|
|
7806
|
-
name: "AI_NoSpeechGeneratedError",
|
|
7807
|
-
message: "No speech audio generated."
|
|
7808
|
-
});
|
|
7809
|
-
this.responses = options.responses;
|
|
7810
|
-
}
|
|
7811
|
-
};
|
|
7812
|
-
|
|
7813
8194
|
// src/generate-speech/generated-audio-file.ts
|
|
7814
8195
|
var DefaultGeneratedAudioFile = class extends DefaultGeneratedFile {
|
|
7815
8196
|
constructor({
|
|
@@ -7850,7 +8231,7 @@ async function generateSpeech({
|
|
|
7850
8231
|
headers
|
|
7851
8232
|
}) {
|
|
7852
8233
|
var _a17;
|
|
7853
|
-
if (model.specificationVersion !== "
|
|
8234
|
+
if (model.specificationVersion !== "v3") {
|
|
7854
8235
|
throw new UnsupportedModelVersionError({
|
|
7855
8236
|
version: model.specificationVersion,
|
|
7856
8237
|
provider: model.provider,
|
|
@@ -8065,7 +8446,7 @@ function defaultSettingsMiddleware({
|
|
|
8065
8446
|
settings
|
|
8066
8447
|
}) {
|
|
8067
8448
|
return {
|
|
8068
|
-
middlewareVersion: "
|
|
8449
|
+
middlewareVersion: "v3",
|
|
8069
8450
|
transformParams: async ({ params }) => {
|
|
8070
8451
|
return mergeObjects(settings, params);
|
|
8071
8452
|
}
|
|
@@ -8099,7 +8480,7 @@ function extractReasoningMiddleware({
|
|
|
8099
8480
|
const openingTag = `<${tagName}>`;
|
|
8100
8481
|
const closingTag = `</${tagName}>`;
|
|
8101
8482
|
return {
|
|
8102
|
-
middlewareVersion: "
|
|
8483
|
+
middlewareVersion: "v3",
|
|
8103
8484
|
wrapGenerate: async ({ doGenerate }) => {
|
|
8104
8485
|
const { content, ...rest } = await doGenerate();
|
|
8105
8486
|
const transformedContent = [];
|
|
@@ -8245,7 +8626,7 @@ function extractReasoningMiddleware({
|
|
|
8245
8626
|
// src/middleware/simulate-streaming-middleware.ts
|
|
8246
8627
|
function simulateStreamingMiddleware() {
|
|
8247
8628
|
return {
|
|
8248
|
-
middlewareVersion: "
|
|
8629
|
+
middlewareVersion: "v3",
|
|
8249
8630
|
wrapStream: async ({ doGenerate }) => {
|
|
8250
8631
|
const result = await doGenerate();
|
|
8251
8632
|
let id = 0;
|
|
@@ -8342,7 +8723,7 @@ var doWrap = ({
|
|
|
8342
8723
|
return transformParams ? await transformParams({ params, type, model }) : params;
|
|
8343
8724
|
}
|
|
8344
8725
|
return {
|
|
8345
|
-
specificationVersion: "
|
|
8726
|
+
specificationVersion: "v3",
|
|
8346
8727
|
provider: (_a17 = providerId != null ? providerId : overrideProvider == null ? void 0 : overrideProvider({ model })) != null ? _a17 : model.provider,
|
|
8347
8728
|
modelId: (_b = modelId != null ? modelId : overrideModelId == null ? void 0 : overrideModelId({ model })) != null ? _b : model.modelId,
|
|
8348
8729
|
supportedUrls: (_c = overrideSupportedUrls == null ? void 0 : overrideSupportedUrls({ model })) != null ? _c : model.supportedUrls,
|
|
@@ -9204,7 +9585,7 @@ async function transcribe({
|
|
|
9204
9585
|
abortSignal,
|
|
9205
9586
|
headers
|
|
9206
9587
|
}) {
|
|
9207
|
-
if (model.specificationVersion !== "
|
|
9588
|
+
if (model.specificationVersion !== "v3") {
|
|
9208
9589
|
throw new UnsupportedModelVersionError({
|
|
9209
9590
|
version: model.specificationVersion,
|
|
9210
9591
|
provider: model.provider,
|
|
@@ -9583,8 +9964,8 @@ var DefaultChatTransport = class extends HttpChatTransport {
|
|
|
9583
9964
|
// src/ui/chat.ts
|
|
9584
9965
|
var AbstractChat = class {
|
|
9585
9966
|
constructor({
|
|
9586
|
-
generateId:
|
|
9587
|
-
id =
|
|
9967
|
+
generateId: generateId2 = generateIdFunc,
|
|
9968
|
+
id = generateId2(),
|
|
9588
9969
|
transport = new DefaultChatTransport(),
|
|
9589
9970
|
messageMetadataSchema,
|
|
9590
9971
|
dataPartSchemas,
|
|
@@ -9696,29 +10077,50 @@ var AbstractChat = class {
|
|
|
9696
10077
|
this.setStatus({ status: "ready" });
|
|
9697
10078
|
}
|
|
9698
10079
|
};
|
|
10080
|
+
this.addToolApprovalResponse = async ({
|
|
10081
|
+
id,
|
|
10082
|
+
approved,
|
|
10083
|
+
reason
|
|
10084
|
+
}) => this.jobExecutor.run(async () => {
|
|
10085
|
+
var _a17, _b;
|
|
10086
|
+
const messages = this.state.messages;
|
|
10087
|
+
const lastMessage = messages[messages.length - 1];
|
|
10088
|
+
const updatePart = (part) => isToolUIPart(part) && part.state === "approval-requested" && part.approval.id === id ? {
|
|
10089
|
+
...part,
|
|
10090
|
+
state: "approval-responded",
|
|
10091
|
+
approval: { id, approved, reason }
|
|
10092
|
+
} : part;
|
|
10093
|
+
this.state.replaceMessage(messages.length - 1, {
|
|
10094
|
+
...lastMessage,
|
|
10095
|
+
parts: lastMessage.parts.map(updatePart)
|
|
10096
|
+
});
|
|
10097
|
+
if (this.activeResponse) {
|
|
10098
|
+
this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(updatePart);
|
|
10099
|
+
}
|
|
10100
|
+
if (this.status !== "streaming" && this.status !== "submitted" && ((_a17 = this.sendAutomaticallyWhen) == null ? void 0 : _a17.call(this, { messages: this.state.messages }))) {
|
|
10101
|
+
this.makeRequest({
|
|
10102
|
+
trigger: "submit-message",
|
|
10103
|
+
messageId: (_b = this.lastMessage) == null ? void 0 : _b.id
|
|
10104
|
+
});
|
|
10105
|
+
}
|
|
10106
|
+
});
|
|
9699
10107
|
this.addToolResult = async ({
|
|
10108
|
+
state = "output-available",
|
|
9700
10109
|
tool: tool3,
|
|
9701
10110
|
toolCallId,
|
|
9702
|
-
output
|
|
10111
|
+
output,
|
|
10112
|
+
errorText
|
|
9703
10113
|
}) => this.jobExecutor.run(async () => {
|
|
9704
10114
|
var _a17, _b;
|
|
9705
10115
|
const messages = this.state.messages;
|
|
9706
10116
|
const lastMessage = messages[messages.length - 1];
|
|
10117
|
+
const updatePart = (part) => isToolOrDynamicToolUIPart(part) && part.toolCallId === toolCallId ? { ...part, state, output, errorText } : part;
|
|
9707
10118
|
this.state.replaceMessage(messages.length - 1, {
|
|
9708
10119
|
...lastMessage,
|
|
9709
|
-
parts: lastMessage.parts.map(
|
|
9710
|
-
(part) => isToolOrDynamicToolUIPart(part) && part.toolCallId === toolCallId ? { ...part, state: "output-available", output } : part
|
|
9711
|
-
)
|
|
10120
|
+
parts: lastMessage.parts.map(updatePart)
|
|
9712
10121
|
});
|
|
9713
10122
|
if (this.activeResponse) {
|
|
9714
|
-
this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(
|
|
9715
|
-
(part) => isToolOrDynamicToolUIPart(part) && part.toolCallId === toolCallId ? {
|
|
9716
|
-
...part,
|
|
9717
|
-
state: "output-available",
|
|
9718
|
-
output,
|
|
9719
|
-
errorText: void 0
|
|
9720
|
-
} : part
|
|
9721
|
-
);
|
|
10123
|
+
this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(updatePart);
|
|
9722
10124
|
}
|
|
9723
10125
|
if (this.status !== "streaming" && this.status !== "submitted" && ((_a17 = this.sendAutomaticallyWhen) == null ? void 0 : _a17.call(this, { messages: this.state.messages }))) {
|
|
9724
10126
|
this.makeRequest({
|
|
@@ -9740,7 +10142,7 @@ var AbstractChat = class {
|
|
|
9740
10142
|
};
|
|
9741
10143
|
this.id = id;
|
|
9742
10144
|
this.transport = transport;
|
|
9743
|
-
this.generateId =
|
|
10145
|
+
this.generateId = generateId2;
|
|
9744
10146
|
this.messageMetadataSchema = messageMetadataSchema;
|
|
9745
10147
|
this.dataPartSchemas = dataPartSchemas;
|
|
9746
10148
|
this.state = state;
|
|
@@ -9910,6 +10312,30 @@ var AbstractChat = class {
|
|
|
9910
10312
|
}
|
|
9911
10313
|
};
|
|
9912
10314
|
|
|
10315
|
+
// src/ui/last-assistant-message-is-complete-with-approval-responses.ts
|
|
10316
|
+
function lastAssistantMessageIsCompleteWithApprovalResponses({
|
|
10317
|
+
messages
|
|
10318
|
+
}) {
|
|
10319
|
+
const message = messages[messages.length - 1];
|
|
10320
|
+
if (!message) {
|
|
10321
|
+
return false;
|
|
10322
|
+
}
|
|
10323
|
+
if (message.role !== "assistant") {
|
|
10324
|
+
return false;
|
|
10325
|
+
}
|
|
10326
|
+
const lastStepStartIndex = message.parts.reduce((lastIndex, part, index) => {
|
|
10327
|
+
return part.type === "step-start" ? index : lastIndex;
|
|
10328
|
+
}, -1);
|
|
10329
|
+
const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(isToolOrDynamicToolUIPart);
|
|
10330
|
+
return (
|
|
10331
|
+
// has at least one tool approval response
|
|
10332
|
+
lastStepToolInvocations.filter((part) => part.state === "approval-responded").length > 0 && // all tool approvals must have a response
|
|
10333
|
+
lastStepToolInvocations.every(
|
|
10334
|
+
(part) => part.state === "output-available" || part.state === "output-error" || part.state === "approval-responded"
|
|
10335
|
+
)
|
|
10336
|
+
);
|
|
10337
|
+
}
|
|
10338
|
+
|
|
9913
10339
|
// src/ui/last-assistant-message-is-complete-with-tool-calls.ts
|
|
9914
10340
|
function lastAssistantMessageIsCompleteWithToolCalls({
|
|
9915
10341
|
messages
|
|
@@ -9925,7 +10351,9 @@ function lastAssistantMessageIsCompleteWithToolCalls({
|
|
|
9925
10351
|
return part.type === "step-start" ? index : lastIndex;
|
|
9926
10352
|
}, -1);
|
|
9927
10353
|
const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter(isToolOrDynamicToolUIPart);
|
|
9928
|
-
return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every(
|
|
10354
|
+
return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every(
|
|
10355
|
+
(part) => part.state === "output-available" || part.state === "output-error"
|
|
10356
|
+
);
|
|
9929
10357
|
}
|
|
9930
10358
|
|
|
9931
10359
|
// src/ui/transform-text-to-ui-message-stream.ts
|
|
@@ -10061,7 +10489,8 @@ var toolUIPartSchemas = [
|
|
|
10061
10489
|
providerExecuted: z10.boolean().optional(),
|
|
10062
10490
|
input: z10.unknown().optional(),
|
|
10063
10491
|
output: z10.never().optional(),
|
|
10064
|
-
errorText: z10.never().optional()
|
|
10492
|
+
errorText: z10.never().optional(),
|
|
10493
|
+
approval: z10.never().optional()
|
|
10065
10494
|
}),
|
|
10066
10495
|
z10.object({
|
|
10067
10496
|
type: z10.string().startsWith("tool-"),
|
|
@@ -10071,7 +10500,38 @@ var toolUIPartSchemas = [
|
|
|
10071
10500
|
input: z10.unknown(),
|
|
10072
10501
|
output: z10.never().optional(),
|
|
10073
10502
|
errorText: z10.never().optional(),
|
|
10074
|
-
callProviderMetadata: providerMetadataSchema.optional()
|
|
10503
|
+
callProviderMetadata: providerMetadataSchema.optional(),
|
|
10504
|
+
approval: z10.never().optional()
|
|
10505
|
+
}),
|
|
10506
|
+
z10.object({
|
|
10507
|
+
type: z10.string().startsWith("tool-"),
|
|
10508
|
+
toolCallId: z10.string(),
|
|
10509
|
+
state: z10.literal("approval-requested"),
|
|
10510
|
+
input: z10.unknown(),
|
|
10511
|
+
providerExecuted: z10.boolean().optional(),
|
|
10512
|
+
output: z10.never().optional(),
|
|
10513
|
+
errorText: z10.never().optional(),
|
|
10514
|
+
callProviderMetadata: providerMetadataSchema.optional(),
|
|
10515
|
+
approval: z10.object({
|
|
10516
|
+
id: z10.string(),
|
|
10517
|
+
approved: z10.never().optional(),
|
|
10518
|
+
reason: z10.never().optional()
|
|
10519
|
+
})
|
|
10520
|
+
}),
|
|
10521
|
+
z10.object({
|
|
10522
|
+
type: z10.string().startsWith("tool-"),
|
|
10523
|
+
toolCallId: z10.string(),
|
|
10524
|
+
state: z10.literal("approval-responded"),
|
|
10525
|
+
input: z10.unknown(),
|
|
10526
|
+
providerExecuted: z10.boolean().optional(),
|
|
10527
|
+
output: z10.never().optional(),
|
|
10528
|
+
errorText: z10.never().optional(),
|
|
10529
|
+
callProviderMetadata: providerMetadataSchema.optional(),
|
|
10530
|
+
approval: z10.object({
|
|
10531
|
+
id: z10.string(),
|
|
10532
|
+
approved: z10.boolean(),
|
|
10533
|
+
reason: z10.string().optional()
|
|
10534
|
+
})
|
|
10075
10535
|
}),
|
|
10076
10536
|
z10.object({
|
|
10077
10537
|
type: z10.string().startsWith("tool-"),
|
|
@@ -10082,7 +10542,12 @@ var toolUIPartSchemas = [
|
|
|
10082
10542
|
output: z10.unknown(),
|
|
10083
10543
|
errorText: z10.never().optional(),
|
|
10084
10544
|
callProviderMetadata: providerMetadataSchema.optional(),
|
|
10085
|
-
preliminary: z10.boolean().optional()
|
|
10545
|
+
preliminary: z10.boolean().optional(),
|
|
10546
|
+
approval: z10.object({
|
|
10547
|
+
id: z10.string(),
|
|
10548
|
+
approved: z10.literal(true),
|
|
10549
|
+
reason: z10.string().optional()
|
|
10550
|
+
}).optional()
|
|
10086
10551
|
}),
|
|
10087
10552
|
z10.object({
|
|
10088
10553
|
type: z10.string().startsWith("tool-"),
|
|
@@ -10092,7 +10557,27 @@ var toolUIPartSchemas = [
|
|
|
10092
10557
|
input: z10.unknown(),
|
|
10093
10558
|
output: z10.never().optional(),
|
|
10094
10559
|
errorText: z10.string(),
|
|
10095
|
-
callProviderMetadata: providerMetadataSchema.optional()
|
|
10560
|
+
callProviderMetadata: providerMetadataSchema.optional(),
|
|
10561
|
+
approval: z10.object({
|
|
10562
|
+
id: z10.string(),
|
|
10563
|
+
approved: z10.literal(true),
|
|
10564
|
+
reason: z10.string().optional()
|
|
10565
|
+
}).optional()
|
|
10566
|
+
}),
|
|
10567
|
+
z10.object({
|
|
10568
|
+
type: z10.string().startsWith("tool-"),
|
|
10569
|
+
toolCallId: z10.string(),
|
|
10570
|
+
state: z10.literal("output-denied"),
|
|
10571
|
+
providerExecuted: z10.boolean().optional(),
|
|
10572
|
+
input: z10.unknown(),
|
|
10573
|
+
output: z10.never().optional(),
|
|
10574
|
+
errorText: z10.never().optional(),
|
|
10575
|
+
callProviderMetadata: providerMetadataSchema.optional(),
|
|
10576
|
+
approval: z10.object({
|
|
10577
|
+
id: z10.string(),
|
|
10578
|
+
approved: z10.literal(false),
|
|
10579
|
+
reason: z10.string().optional()
|
|
10580
|
+
})
|
|
10096
10581
|
})
|
|
10097
10582
|
];
|
|
10098
10583
|
var uiMessageSchema = z10.object({
|
|
@@ -10237,7 +10722,7 @@ function createUIMessageStream({
|
|
|
10237
10722
|
onError = getErrorMessage8,
|
|
10238
10723
|
originalMessages,
|
|
10239
10724
|
onFinish,
|
|
10240
|
-
generateId:
|
|
10725
|
+
generateId: generateId2 = generateIdFunc2
|
|
10241
10726
|
}) {
|
|
10242
10727
|
let controller;
|
|
10243
10728
|
const ongoingStreamPromises = [];
|
|
@@ -10309,7 +10794,7 @@ function createUIMessageStream({
|
|
|
10309
10794
|
});
|
|
10310
10795
|
return handleUIMessageStreamFinish({
|
|
10311
10796
|
stream,
|
|
10312
|
-
messageId:
|
|
10797
|
+
messageId: generateId2(),
|
|
10313
10798
|
originalMessages,
|
|
10314
10799
|
onFinish,
|
|
10315
10800
|
onError
|
|
@@ -10364,9 +10849,10 @@ function readUIMessageStream({
|
|
|
10364
10849
|
return createAsyncIterableStream(outputStream);
|
|
10365
10850
|
}
|
|
10366
10851
|
export {
|
|
10367
|
-
|
|
10852
|
+
AISDKError18 as AISDKError,
|
|
10368
10853
|
APICallError,
|
|
10369
10854
|
AbstractChat,
|
|
10855
|
+
Agent,
|
|
10370
10856
|
DefaultChatTransport,
|
|
10371
10857
|
DownloadError,
|
|
10372
10858
|
EmptyResponseBodyError,
|
|
@@ -10389,6 +10875,7 @@ export {
|
|
|
10389
10875
|
NoObjectGeneratedError,
|
|
10390
10876
|
NoOutputGeneratedError,
|
|
10391
10877
|
NoOutputSpecifiedError,
|
|
10878
|
+
NoSpeechGeneratedError,
|
|
10392
10879
|
NoSuchModelError,
|
|
10393
10880
|
NoSuchProviderError,
|
|
10394
10881
|
NoSuchToolError,
|
|
@@ -10434,7 +10921,7 @@ export {
|
|
|
10434
10921
|
transcribe as experimental_transcribe,
|
|
10435
10922
|
extractReasoningMiddleware,
|
|
10436
10923
|
gateway2 as gateway,
|
|
10437
|
-
|
|
10924
|
+
generateId,
|
|
10438
10925
|
generateObject,
|
|
10439
10926
|
generateText,
|
|
10440
10927
|
getTextFromDataUrl,
|
|
@@ -10445,6 +10932,7 @@ export {
|
|
|
10445
10932
|
isToolOrDynamicToolUIPart,
|
|
10446
10933
|
isToolUIPart,
|
|
10447
10934
|
jsonSchema2 as jsonSchema,
|
|
10935
|
+
lastAssistantMessageIsCompleteWithApprovalResponses,
|
|
10448
10936
|
lastAssistantMessageIsCompleteWithToolCalls,
|
|
10449
10937
|
modelMessageSchema,
|
|
10450
10938
|
parseJsonEventStream3 as parseJsonEventStream,
|