ai 4.0.17 → 4.0.19
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 +13 -0
- package/dist/index.d.mts +44 -15
- package/dist/index.d.ts +44 -15
- package/dist/index.js +369 -203
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +363 -197
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/rsc/dist/rsc-server.mjs.map +1 -1
package/dist/index.mjs
CHANGED
@@ -837,13 +837,39 @@ var DefaultGenerateImageResult = class {
|
|
837
837
|
// core/generate-object/generate-object.ts
|
838
838
|
import { createIdGenerator, safeParseJSON } from "@ai-sdk/provider-utils";
|
839
839
|
|
840
|
-
//
|
840
|
+
// errors/no-object-generated-error.ts
|
841
841
|
import { AISDKError as AISDKError3 } from "@ai-sdk/provider";
|
842
|
-
var name3 = "
|
842
|
+
var name3 = "AI_NoObjectGeneratedError";
|
843
843
|
var marker3 = `vercel.ai.error.${name3}`;
|
844
844
|
var symbol3 = Symbol.for(marker3);
|
845
845
|
var _a3;
|
846
|
-
var
|
846
|
+
var NoObjectGeneratedError = class extends AISDKError3 {
|
847
|
+
constructor({
|
848
|
+
message = "No object generated.",
|
849
|
+
cause,
|
850
|
+
text: text2,
|
851
|
+
response,
|
852
|
+
usage
|
853
|
+
}) {
|
854
|
+
super({ name: name3, message, cause });
|
855
|
+
this[_a3] = true;
|
856
|
+
this.text = text2;
|
857
|
+
this.response = response;
|
858
|
+
this.usage = usage;
|
859
|
+
}
|
860
|
+
static isInstance(error) {
|
861
|
+
return AISDKError3.hasMarker(error, marker3);
|
862
|
+
}
|
863
|
+
};
|
864
|
+
_a3 = symbol3;
|
865
|
+
|
866
|
+
// util/download-error.ts
|
867
|
+
import { AISDKError as AISDKError4 } from "@ai-sdk/provider";
|
868
|
+
var name4 = "AI_DownloadError";
|
869
|
+
var marker4 = `vercel.ai.error.${name4}`;
|
870
|
+
var symbol4 = Symbol.for(marker4);
|
871
|
+
var _a4;
|
872
|
+
var DownloadError = class extends AISDKError4 {
|
847
873
|
constructor({
|
848
874
|
url,
|
849
875
|
statusCode,
|
@@ -851,17 +877,17 @@ var DownloadError = class extends AISDKError3 {
|
|
851
877
|
cause,
|
852
878
|
message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}`
|
853
879
|
}) {
|
854
|
-
super({ name:
|
855
|
-
this[
|
880
|
+
super({ name: name4, message, cause });
|
881
|
+
this[_a4] = true;
|
856
882
|
this.url = url;
|
857
883
|
this.statusCode = statusCode;
|
858
884
|
this.statusText = statusText;
|
859
885
|
}
|
860
886
|
static isInstance(error) {
|
861
|
-
return
|
887
|
+
return AISDKError4.hasMarker(error, marker4);
|
862
888
|
}
|
863
889
|
};
|
864
|
-
|
890
|
+
_a4 = symbol4;
|
865
891
|
|
866
892
|
// util/download.ts
|
867
893
|
async function download({
|
@@ -914,26 +940,26 @@ import {
|
|
914
940
|
} from "@ai-sdk/provider-utils";
|
915
941
|
|
916
942
|
// core/prompt/invalid-data-content-error.ts
|
917
|
-
import { AISDKError as
|
918
|
-
var
|
919
|
-
var
|
920
|
-
var
|
921
|
-
var
|
922
|
-
var InvalidDataContentError = class extends
|
943
|
+
import { AISDKError as AISDKError5 } from "@ai-sdk/provider";
|
944
|
+
var name5 = "AI_InvalidDataContentError";
|
945
|
+
var marker5 = `vercel.ai.error.${name5}`;
|
946
|
+
var symbol5 = Symbol.for(marker5);
|
947
|
+
var _a5;
|
948
|
+
var InvalidDataContentError = class extends AISDKError5 {
|
923
949
|
constructor({
|
924
950
|
content,
|
925
951
|
cause,
|
926
952
|
message = `Invalid data content. Expected a base64 string, Uint8Array, ArrayBuffer, or Buffer, but got ${typeof content}.`
|
927
953
|
}) {
|
928
|
-
super({ name:
|
929
|
-
this[
|
954
|
+
super({ name: name5, message, cause });
|
955
|
+
this[_a5] = true;
|
930
956
|
this.content = content;
|
931
957
|
}
|
932
958
|
static isInstance(error) {
|
933
|
-
return
|
959
|
+
return AISDKError5.hasMarker(error, marker5);
|
934
960
|
}
|
935
961
|
};
|
936
|
-
|
962
|
+
_a5 = symbol5;
|
937
963
|
|
938
964
|
// core/prompt/data-content.ts
|
939
965
|
import { z } from "zod";
|
@@ -988,25 +1014,25 @@ function convertUint8ArrayToText(uint8Array) {
|
|
988
1014
|
}
|
989
1015
|
|
990
1016
|
// core/prompt/invalid-message-role-error.ts
|
991
|
-
import { AISDKError as
|
992
|
-
var
|
993
|
-
var
|
994
|
-
var
|
995
|
-
var
|
996
|
-
var InvalidMessageRoleError = class extends
|
1017
|
+
import { AISDKError as AISDKError6 } from "@ai-sdk/provider";
|
1018
|
+
var name6 = "AI_InvalidMessageRoleError";
|
1019
|
+
var marker6 = `vercel.ai.error.${name6}`;
|
1020
|
+
var symbol6 = Symbol.for(marker6);
|
1021
|
+
var _a6;
|
1022
|
+
var InvalidMessageRoleError = class extends AISDKError6 {
|
997
1023
|
constructor({
|
998
1024
|
role,
|
999
1025
|
message = `Invalid message role: '${role}'. Must be one of: "system", "user", "assistant", "tool".`
|
1000
1026
|
}) {
|
1001
|
-
super({ name:
|
1002
|
-
this[
|
1027
|
+
super({ name: name6, message });
|
1028
|
+
this[_a6] = true;
|
1003
1029
|
this.role = role;
|
1004
1030
|
}
|
1005
1031
|
static isInstance(error) {
|
1006
|
-
return
|
1032
|
+
return AISDKError6.hasMarker(error, marker6);
|
1007
1033
|
}
|
1008
1034
|
};
|
1009
|
-
|
1035
|
+
_a6 = symbol6;
|
1010
1036
|
|
1011
1037
|
// core/prompt/split-data-url.ts
|
1012
1038
|
function splitDataUrl(dataUrl) {
|
@@ -1529,25 +1555,25 @@ function attachmentsToParts(attachments) {
|
|
1529
1555
|
}
|
1530
1556
|
|
1531
1557
|
// core/prompt/message-conversion-error.ts
|
1532
|
-
import { AISDKError as
|
1533
|
-
var
|
1534
|
-
var
|
1535
|
-
var
|
1536
|
-
var
|
1537
|
-
var MessageConversionError = class extends
|
1558
|
+
import { AISDKError as AISDKError7 } from "@ai-sdk/provider";
|
1559
|
+
var name7 = "AI_MessageConversionError";
|
1560
|
+
var marker7 = `vercel.ai.error.${name7}`;
|
1561
|
+
var symbol7 = Symbol.for(marker7);
|
1562
|
+
var _a7;
|
1563
|
+
var MessageConversionError = class extends AISDKError7 {
|
1538
1564
|
constructor({
|
1539
1565
|
originalMessage,
|
1540
1566
|
message
|
1541
1567
|
}) {
|
1542
|
-
super({ name:
|
1543
|
-
this[
|
1568
|
+
super({ name: name7, message });
|
1569
|
+
this[_a7] = true;
|
1544
1570
|
this.originalMessage = originalMessage;
|
1545
1571
|
}
|
1546
1572
|
static isInstance(error) {
|
1547
|
-
return
|
1573
|
+
return AISDKError7.hasMarker(error, marker7);
|
1548
1574
|
}
|
1549
1575
|
};
|
1550
|
-
|
1576
|
+
_a7 = symbol7;
|
1551
1577
|
|
1552
1578
|
// core/prompt/convert-to-core-messages.ts
|
1553
1579
|
function convertToCoreMessages(messages, options) {
|
@@ -1718,6 +1744,13 @@ function calculateLanguageModelUsage({
|
|
1718
1744
|
totalTokens: promptTokens + completionTokens
|
1719
1745
|
};
|
1720
1746
|
}
|
1747
|
+
function addLanguageModelUsage(usage1, usage2) {
|
1748
|
+
return {
|
1749
|
+
promptTokens: usage1.promptTokens + usage2.promptTokens,
|
1750
|
+
completionTokens: usage1.completionTokens + usage2.completionTokens,
|
1751
|
+
totalTokens: usage1.totalTokens + usage2.totalTokens
|
1752
|
+
};
|
1753
|
+
}
|
1721
1754
|
|
1722
1755
|
// core/generate-object/inject-json-instruction.ts
|
1723
1756
|
var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
|
@@ -1739,24 +1772,6 @@ function injectJsonInstruction({
|
|
1739
1772
|
].filter((line) => line != null).join("\n");
|
1740
1773
|
}
|
1741
1774
|
|
1742
|
-
// core/generate-object/no-object-generated-error.ts
|
1743
|
-
import { AISDKError as AISDKError7 } from "@ai-sdk/provider";
|
1744
|
-
var name7 = "AI_NoObjectGeneratedError";
|
1745
|
-
var marker7 = `vercel.ai.error.${name7}`;
|
1746
|
-
var symbol7 = Symbol.for(marker7);
|
1747
|
-
var _a7;
|
1748
|
-
var NoObjectGeneratedError = class extends AISDKError7 {
|
1749
|
-
// used in isInstance
|
1750
|
-
constructor({ message = "No object generated." } = {}) {
|
1751
|
-
super({ name: name7, message });
|
1752
|
-
this[_a7] = true;
|
1753
|
-
}
|
1754
|
-
static isInstance(error) {
|
1755
|
-
return AISDKError7.hasMarker(error, marker7);
|
1756
|
-
}
|
1757
|
-
};
|
1758
|
-
_a7 = symbol7;
|
1759
|
-
|
1760
1775
|
// core/generate-object/output-strategy.ts
|
1761
1776
|
import {
|
1762
1777
|
isJSONArray,
|
@@ -1791,8 +1806,16 @@ var noSchemaOutputStrategy = {
|
|
1791
1806
|
validatePartialResult({ value, textDelta }) {
|
1792
1807
|
return { success: true, value: { partial: value, textDelta } };
|
1793
1808
|
},
|
1794
|
-
validateFinalResult(value) {
|
1795
|
-
return value === void 0 ? {
|
1809
|
+
validateFinalResult(value, context) {
|
1810
|
+
return value === void 0 ? {
|
1811
|
+
success: false,
|
1812
|
+
error: new NoObjectGeneratedError({
|
1813
|
+
message: "No object generated: response did not match schema.",
|
1814
|
+
text: context.text,
|
1815
|
+
response: context.response,
|
1816
|
+
usage: context.usage
|
1817
|
+
})
|
1818
|
+
} : { success: true, value };
|
1796
1819
|
},
|
1797
1820
|
createElementStream() {
|
1798
1821
|
throw new UnsupportedFunctionalityError({
|
@@ -2271,14 +2294,18 @@ async function generateObject({
|
|
2271
2294
|
abortSignal,
|
2272
2295
|
headers
|
2273
2296
|
});
|
2274
|
-
if (result2.text === void 0) {
|
2275
|
-
throw new NoObjectGeneratedError();
|
2276
|
-
}
|
2277
2297
|
const responseData = {
|
2278
2298
|
id: (_b2 = (_a14 = result2.response) == null ? void 0 : _a14.id) != null ? _b2 : generateId3(),
|
2279
2299
|
timestamp: (_d = (_c = result2.response) == null ? void 0 : _c.timestamp) != null ? _d : currentDate(),
|
2280
2300
|
modelId: (_f = (_e = result2.response) == null ? void 0 : _e.modelId) != null ? _f : model.modelId
|
2281
2301
|
};
|
2302
|
+
if (result2.text === void 0) {
|
2303
|
+
throw new NoObjectGeneratedError({
|
2304
|
+
message: "No object generated: the model did not return a response.",
|
2305
|
+
response: responseData,
|
2306
|
+
usage: calculateLanguageModelUsage(result2.usage)
|
2307
|
+
});
|
2308
|
+
}
|
2282
2309
|
span2.setAttributes(
|
2283
2310
|
selectTelemetryAttributes({
|
2284
2311
|
telemetry,
|
@@ -2375,14 +2402,18 @@ async function generateObject({
|
|
2375
2402
|
headers
|
2376
2403
|
});
|
2377
2404
|
const objectText = (_b2 = (_a14 = result2.toolCalls) == null ? void 0 : _a14[0]) == null ? void 0 : _b2.args;
|
2378
|
-
if (objectText === void 0) {
|
2379
|
-
throw new NoObjectGeneratedError();
|
2380
|
-
}
|
2381
2405
|
const responseData = {
|
2382
2406
|
id: (_d = (_c = result2.response) == null ? void 0 : _c.id) != null ? _d : generateId3(),
|
2383
2407
|
timestamp: (_f = (_e = result2.response) == null ? void 0 : _e.timestamp) != null ? _f : currentDate(),
|
2384
2408
|
modelId: (_h = (_g = result2.response) == null ? void 0 : _g.modelId) != null ? _h : model.modelId
|
2385
2409
|
};
|
2410
|
+
if (objectText === void 0) {
|
2411
|
+
throw new NoObjectGeneratedError({
|
2412
|
+
message: "No object generated: the tool was not called.",
|
2413
|
+
response: responseData,
|
2414
|
+
usage: calculateLanguageModelUsage(result2.usage)
|
2415
|
+
});
|
2416
|
+
}
|
2386
2417
|
span2.setAttributes(
|
2387
2418
|
selectTelemetryAttributes({
|
2388
2419
|
telemetry,
|
@@ -2430,13 +2461,30 @@ async function generateObject({
|
|
2430
2461
|
}
|
2431
2462
|
const parseResult = safeParseJSON({ text: result });
|
2432
2463
|
if (!parseResult.success) {
|
2433
|
-
throw
|
2464
|
+
throw new NoObjectGeneratedError({
|
2465
|
+
message: "No object generated: could not parse the response.",
|
2466
|
+
cause: parseResult.error,
|
2467
|
+
text: result,
|
2468
|
+
response,
|
2469
|
+
usage: calculateLanguageModelUsage(usage)
|
2470
|
+
});
|
2434
2471
|
}
|
2435
2472
|
const validationResult = outputStrategy.validateFinalResult(
|
2436
|
-
parseResult.value
|
2473
|
+
parseResult.value,
|
2474
|
+
{
|
2475
|
+
text: result,
|
2476
|
+
response,
|
2477
|
+
usage: calculateLanguageModelUsage(usage)
|
2478
|
+
}
|
2437
2479
|
);
|
2438
2480
|
if (!validationResult.success) {
|
2439
|
-
throw
|
2481
|
+
throw new NoObjectGeneratedError({
|
2482
|
+
message: "No object generated: response did not match schema.",
|
2483
|
+
cause: validationResult.error,
|
2484
|
+
text: result,
|
2485
|
+
response,
|
2486
|
+
usage: calculateLanguageModelUsage(usage)
|
2487
|
+
});
|
2440
2488
|
}
|
2441
2489
|
span.setAttributes(
|
2442
2490
|
selectTelemetryAttributes({
|
@@ -2974,12 +3022,25 @@ var DefaultStreamObjectResult = class {
|
|
2974
3022
|
...response,
|
2975
3023
|
headers: rawResponse == null ? void 0 : rawResponse.headers
|
2976
3024
|
});
|
2977
|
-
const validationResult = outputStrategy.validateFinalResult(
|
3025
|
+
const validationResult = outputStrategy.validateFinalResult(
|
3026
|
+
latestObjectJson,
|
3027
|
+
{
|
3028
|
+
text: accumulatedText,
|
3029
|
+
response,
|
3030
|
+
usage
|
3031
|
+
}
|
3032
|
+
);
|
2978
3033
|
if (validationResult.success) {
|
2979
3034
|
object2 = validationResult.value;
|
2980
3035
|
self.objectPromise.resolve(object2);
|
2981
3036
|
} else {
|
2982
|
-
error =
|
3037
|
+
error = new NoObjectGeneratedError({
|
3038
|
+
message: "No object generated: response did not match schema.",
|
3039
|
+
cause: validationResult.error,
|
3040
|
+
text: accumulatedText,
|
3041
|
+
response,
|
3042
|
+
usage
|
3043
|
+
});
|
2983
3044
|
self.objectPromise.reject(error);
|
2984
3045
|
}
|
2985
3046
|
break;
|
@@ -3528,7 +3589,7 @@ async function generateText({
|
|
3528
3589
|
const responseMessages = [];
|
3529
3590
|
let text2 = "";
|
3530
3591
|
const steps = [];
|
3531
|
-
|
3592
|
+
let usage = {
|
3532
3593
|
completionTokens: 0,
|
3533
3594
|
promptTokens: 0,
|
3534
3595
|
totalTokens: 0
|
@@ -3655,9 +3716,7 @@ async function generateText({
|
|
3655
3716
|
const currentUsage = calculateLanguageModelUsage(
|
3656
3717
|
currentModelResponse.usage
|
3657
3718
|
);
|
3658
|
-
usage
|
3659
|
-
usage.promptTokens += currentUsage.promptTokens;
|
3660
|
-
usage.totalTokens += currentUsage.totalTokens;
|
3719
|
+
usage = addLanguageModelUsage(usage, currentUsage);
|
3661
3720
|
let nextStepType = "done";
|
3662
3721
|
if (++stepCount < maxSteps) {
|
3663
3722
|
if (continueSteps && currentModelResponse.finishReason === "length" && // only use continue when there are no tool calls:
|
@@ -3737,7 +3796,13 @@ async function generateText({
|
|
3737
3796
|
);
|
3738
3797
|
return new DefaultGenerateTextResult({
|
3739
3798
|
text: text2,
|
3740
|
-
output: output == null ? void 0 : output.parseOutput(
|
3799
|
+
output: output == null ? void 0 : output.parseOutput(
|
3800
|
+
{ text: text2 },
|
3801
|
+
{
|
3802
|
+
response: currentModelResponse.response,
|
3803
|
+
usage
|
3804
|
+
}
|
3805
|
+
),
|
3741
3806
|
toolCalls: currentToolCalls,
|
3742
3807
|
toolResults: currentToolResults,
|
3743
3808
|
finishReason: currentModelResponse.finishReason,
|
@@ -3852,7 +3917,7 @@ __export(output_exports, {
|
|
3852
3917
|
object: () => object,
|
3853
3918
|
text: () => text
|
3854
3919
|
});
|
3855
|
-
import {
|
3920
|
+
import { safeParseJSON as safeParseJSON3, safeValidateTypes as safeValidateTypes4 } from "@ai-sdk/provider-utils";
|
3856
3921
|
import { asSchema as asSchema4 } from "@ai-sdk/ui-utils";
|
3857
3922
|
var text = () => ({
|
3858
3923
|
type: "text",
|
@@ -3880,8 +3945,31 @@ var object = ({
|
|
3880
3945
|
schema: schema.jsonSchema
|
3881
3946
|
});
|
3882
3947
|
},
|
3883
|
-
parseOutput({ text: text2 }) {
|
3884
|
-
|
3948
|
+
parseOutput({ text: text2 }, context) {
|
3949
|
+
const parseResult = safeParseJSON3({ text: text2 });
|
3950
|
+
if (!parseResult.success) {
|
3951
|
+
throw new NoObjectGeneratedError({
|
3952
|
+
message: "No object generated: could not parse the response.",
|
3953
|
+
cause: parseResult.error,
|
3954
|
+
text: text2,
|
3955
|
+
response: context.response,
|
3956
|
+
usage: context.usage
|
3957
|
+
});
|
3958
|
+
}
|
3959
|
+
const validationResult = safeValidateTypes4({
|
3960
|
+
value: parseResult.value,
|
3961
|
+
schema
|
3962
|
+
});
|
3963
|
+
if (!validationResult.success) {
|
3964
|
+
throw new NoObjectGeneratedError({
|
3965
|
+
message: "No object generated: response did not match schema.",
|
3966
|
+
cause: validationResult.error,
|
3967
|
+
text: text2,
|
3968
|
+
response: context.response,
|
3969
|
+
usage: context.usage
|
3970
|
+
});
|
3971
|
+
}
|
3972
|
+
return validationResult.value;
|
3885
3973
|
}
|
3886
3974
|
};
|
3887
3975
|
};
|
@@ -4286,10 +4374,168 @@ var DefaultStreamTextResult = class {
|
|
4286
4374
|
message: "maxSteps must be at least 1"
|
4287
4375
|
});
|
4288
4376
|
}
|
4377
|
+
let recordedStepText = "";
|
4378
|
+
let recordedContinuationText = "";
|
4379
|
+
let recordedFullText = "";
|
4380
|
+
let recordedRequest = void 0;
|
4381
|
+
const recordedResponse = {
|
4382
|
+
id: generateId3(),
|
4383
|
+
timestamp: currentDate(),
|
4384
|
+
modelId: model.modelId,
|
4385
|
+
messages: []
|
4386
|
+
};
|
4387
|
+
let recordedToolCalls = [];
|
4388
|
+
let recordedToolResults = [];
|
4389
|
+
let recordedFinishReason = void 0;
|
4390
|
+
let recordedUsage = void 0;
|
4391
|
+
let recordedProviderMetadata = void 0;
|
4392
|
+
let stepType = "initial";
|
4393
|
+
const recordedSteps = [];
|
4394
|
+
let rootSpan;
|
4395
|
+
const eventProcessor = new TransformStream({
|
4396
|
+
async transform(chunk, controller) {
|
4397
|
+
controller.enqueue(chunk);
|
4398
|
+
if (chunk.type === "text-delta" || chunk.type === "tool-call" || chunk.type === "tool-result" || chunk.type === "tool-call-streaming-start" || chunk.type === "tool-call-delta") {
|
4399
|
+
await (onChunk == null ? void 0 : onChunk({ chunk }));
|
4400
|
+
}
|
4401
|
+
if (chunk.type === "text-delta") {
|
4402
|
+
recordedStepText += chunk.textDelta;
|
4403
|
+
recordedContinuationText += chunk.textDelta;
|
4404
|
+
recordedFullText += chunk.textDelta;
|
4405
|
+
}
|
4406
|
+
if (chunk.type === "tool-call") {
|
4407
|
+
recordedToolCalls.push(chunk);
|
4408
|
+
}
|
4409
|
+
if (chunk.type === "tool-result") {
|
4410
|
+
recordedToolResults.push(chunk);
|
4411
|
+
}
|
4412
|
+
if (chunk.type === "step-finish") {
|
4413
|
+
const stepMessages = toResponseMessages({
|
4414
|
+
text: recordedContinuationText,
|
4415
|
+
tools: tools != null ? tools : {},
|
4416
|
+
toolCalls: recordedToolCalls,
|
4417
|
+
toolResults: recordedToolResults
|
4418
|
+
});
|
4419
|
+
const currentStep = recordedSteps.length;
|
4420
|
+
let nextStepType = "done";
|
4421
|
+
if (currentStep + 1 < maxSteps) {
|
4422
|
+
if (continueSteps && chunk.finishReason === "length" && // only use continue when there are no tool calls:
|
4423
|
+
recordedToolCalls.length === 0) {
|
4424
|
+
nextStepType = "continue";
|
4425
|
+
} else if (
|
4426
|
+
// there are tool calls:
|
4427
|
+
recordedToolCalls.length > 0 && // all current tool calls have results:
|
4428
|
+
recordedToolResults.length === recordedToolCalls.length
|
4429
|
+
) {
|
4430
|
+
nextStepType = "tool-result";
|
4431
|
+
}
|
4432
|
+
}
|
4433
|
+
const currentStepResult = {
|
4434
|
+
stepType,
|
4435
|
+
text: recordedStepText,
|
4436
|
+
toolCalls: recordedToolCalls,
|
4437
|
+
toolResults: recordedToolResults,
|
4438
|
+
finishReason: chunk.finishReason,
|
4439
|
+
usage: chunk.usage,
|
4440
|
+
warnings: chunk.warnings,
|
4441
|
+
logprobs: chunk.logprobs,
|
4442
|
+
request: chunk.request,
|
4443
|
+
response: {
|
4444
|
+
...chunk.response,
|
4445
|
+
messages: [...recordedResponse.messages, ...stepMessages]
|
4446
|
+
},
|
4447
|
+
experimental_providerMetadata: chunk.experimental_providerMetadata,
|
4448
|
+
isContinued: chunk.isContinued
|
4449
|
+
};
|
4450
|
+
await (onStepFinish == null ? void 0 : onStepFinish(currentStepResult));
|
4451
|
+
recordedSteps.push(currentStepResult);
|
4452
|
+
recordedToolCalls = [];
|
4453
|
+
recordedToolResults = [];
|
4454
|
+
recordedStepText = "";
|
4455
|
+
recordedRequest = chunk.request;
|
4456
|
+
if (nextStepType !== "done") {
|
4457
|
+
stepType = nextStepType;
|
4458
|
+
}
|
4459
|
+
if (nextStepType !== "continue") {
|
4460
|
+
recordedResponse.messages.push(...stepMessages);
|
4461
|
+
recordedContinuationText = "";
|
4462
|
+
}
|
4463
|
+
}
|
4464
|
+
if (chunk.type === "finish") {
|
4465
|
+
recordedResponse.id = chunk.response.id;
|
4466
|
+
recordedResponse.timestamp = chunk.response.timestamp;
|
4467
|
+
recordedResponse.modelId = chunk.response.modelId;
|
4468
|
+
recordedResponse.headers = chunk.response.headers;
|
4469
|
+
recordedUsage = chunk.usage;
|
4470
|
+
recordedFinishReason = chunk.finishReason;
|
4471
|
+
recordedProviderMetadata = chunk.experimental_providerMetadata;
|
4472
|
+
}
|
4473
|
+
},
|
4474
|
+
async flush(controller) {
|
4475
|
+
var _a13;
|
4476
|
+
try {
|
4477
|
+
const lastStep = recordedSteps[recordedSteps.length - 1];
|
4478
|
+
if (lastStep) {
|
4479
|
+
self.warningsPromise.resolve(lastStep.warnings);
|
4480
|
+
self.requestPromise.resolve(lastStep.request);
|
4481
|
+
self.responsePromise.resolve(lastStep.response);
|
4482
|
+
self.toolCallsPromise.resolve(lastStep.toolCalls);
|
4483
|
+
self.toolResultsPromise.resolve(lastStep.toolResults);
|
4484
|
+
self.providerMetadataPromise.resolve(
|
4485
|
+
lastStep.experimental_providerMetadata
|
4486
|
+
);
|
4487
|
+
}
|
4488
|
+
const finishReason = recordedFinishReason != null ? recordedFinishReason : "unknown";
|
4489
|
+
const usage = recordedUsage != null ? recordedUsage : {
|
4490
|
+
completionTokens: NaN,
|
4491
|
+
promptTokens: NaN,
|
4492
|
+
totalTokens: NaN
|
4493
|
+
};
|
4494
|
+
self.finishReasonPromise.resolve(finishReason);
|
4495
|
+
self.usagePromise.resolve(usage);
|
4496
|
+
self.textPromise.resolve(recordedFullText);
|
4497
|
+
self.stepsPromise.resolve(recordedSteps);
|
4498
|
+
await (onFinish == null ? void 0 : onFinish({
|
4499
|
+
finishReason,
|
4500
|
+
logprobs: void 0,
|
4501
|
+
usage,
|
4502
|
+
text: recordedFullText,
|
4503
|
+
toolCalls: lastStep.toolCalls,
|
4504
|
+
toolResults: lastStep.toolResults,
|
4505
|
+
request: (_a13 = lastStep.request) != null ? _a13 : {},
|
4506
|
+
response: lastStep.response,
|
4507
|
+
warnings: lastStep.warnings,
|
4508
|
+
experimental_providerMetadata: lastStep.experimental_providerMetadata,
|
4509
|
+
steps: recordedSteps
|
4510
|
+
}));
|
4511
|
+
rootSpan.setAttributes(
|
4512
|
+
selectTelemetryAttributes({
|
4513
|
+
telemetry,
|
4514
|
+
attributes: {
|
4515
|
+
"ai.response.finishReason": finishReason,
|
4516
|
+
"ai.response.text": { output: () => recordedFullText },
|
4517
|
+
"ai.response.toolCalls": {
|
4518
|
+
output: () => {
|
4519
|
+
var _a14;
|
4520
|
+
return ((_a14 = lastStep.toolCalls) == null ? void 0 : _a14.length) ? JSON.stringify(lastStep.toolCalls) : void 0;
|
4521
|
+
}
|
4522
|
+
},
|
4523
|
+
"ai.usage.promptTokens": usage.promptTokens,
|
4524
|
+
"ai.usage.completionTokens": usage.completionTokens
|
4525
|
+
}
|
4526
|
+
})
|
4527
|
+
);
|
4528
|
+
} catch (error) {
|
4529
|
+
controller.error(error);
|
4530
|
+
} finally {
|
4531
|
+
rootSpan.end();
|
4532
|
+
}
|
4533
|
+
}
|
4534
|
+
});
|
4289
4535
|
const stitchableStream = createStitchableStream();
|
4290
4536
|
this.addStream = stitchableStream.addStream;
|
4291
4537
|
this.closeStream = stitchableStream.close;
|
4292
|
-
this.baseStream = transform ? stitchableStream.stream.pipeThrough(transform) : stitchableStream.stream;
|
4538
|
+
this.baseStream = (transform ? stitchableStream.stream.pipeThrough(transform) : stitchableStream.stream).pipeThrough(eventProcessor);
|
4293
4539
|
const { maxRetries, retry } = prepareRetries({
|
4294
4540
|
maxRetries: maxRetriesArg
|
4295
4541
|
});
|
@@ -4321,13 +4567,13 @@ var DefaultStreamTextResult = class {
|
|
4321
4567
|
}),
|
4322
4568
|
tracer,
|
4323
4569
|
endWhenDone: false,
|
4324
|
-
fn: async (
|
4325
|
-
|
4570
|
+
fn: async (rootSpanArg) => {
|
4571
|
+
rootSpan = rootSpanArg;
|
4326
4572
|
async function streamStep({
|
4327
4573
|
currentStep,
|
4328
4574
|
responseMessages,
|
4329
4575
|
usage,
|
4330
|
-
stepType,
|
4576
|
+
stepType: stepType2,
|
4331
4577
|
previousStepText,
|
4332
4578
|
hasLeadingWhitespace
|
4333
4579
|
}) {
|
@@ -4433,7 +4679,7 @@ var DefaultStreamTextResult = class {
|
|
4433
4679
|
let stepProviderMetadata;
|
4434
4680
|
let stepFirstChunk = true;
|
4435
4681
|
let stepText = "";
|
4436
|
-
let fullStepText =
|
4682
|
+
let fullStepText = stepType2 === "continue" ? previousStepText : "";
|
4437
4683
|
let stepLogProbs;
|
4438
4684
|
let stepResponse = {
|
4439
4685
|
id: generateId3(),
|
@@ -4453,7 +4699,6 @@ var DefaultStreamTextResult = class {
|
|
4453
4699
|
fullStepText += chunk.textDelta;
|
4454
4700
|
chunkTextPublished = true;
|
4455
4701
|
hasWhitespaceSuffix = chunk.textDelta.trimEnd() !== chunk.textDelta;
|
4456
|
-
await (onChunk == null ? void 0 : onChunk({ chunk }));
|
4457
4702
|
}
|
4458
4703
|
self.addStream(
|
4459
4704
|
transformedStream.pipeThrough(
|
@@ -4502,13 +4747,11 @@ var DefaultStreamTextResult = class {
|
|
4502
4747
|
case "tool-call": {
|
4503
4748
|
controller.enqueue(chunk);
|
4504
4749
|
stepToolCalls.push(chunk);
|
4505
|
-
await (onChunk == null ? void 0 : onChunk({ chunk }));
|
4506
4750
|
break;
|
4507
4751
|
}
|
4508
4752
|
case "tool-result": {
|
4509
4753
|
controller.enqueue(chunk);
|
4510
4754
|
stepToolResults.push(chunk);
|
4511
|
-
await (onChunk == null ? void 0 : onChunk({ chunk }));
|
4512
4755
|
break;
|
4513
4756
|
}
|
4514
4757
|
case "response-metadata": {
|
@@ -4535,7 +4778,6 @@ var DefaultStreamTextResult = class {
|
|
4535
4778
|
case "tool-call-streaming-start":
|
4536
4779
|
case "tool-call-delta": {
|
4537
4780
|
controller.enqueue(chunk);
|
4538
|
-
await (onChunk == null ? void 0 : onChunk({ chunk }));
|
4539
4781
|
break;
|
4540
4782
|
}
|
4541
4783
|
case "error": {
|
@@ -4566,7 +4808,7 @@ var DefaultStreamTextResult = class {
|
|
4566
4808
|
}
|
4567
4809
|
}
|
4568
4810
|
if (continueSteps && chunkBuffer.length > 0 && (nextStepType !== "continue" || // when the next step is a regular step, publish the buffer
|
4569
|
-
|
4811
|
+
stepType2 === "continue" && !chunkTextPublished)) {
|
4570
4812
|
await publishTextChunk({
|
4571
4813
|
controller,
|
4572
4814
|
chunk: {
|
@@ -4610,69 +4852,16 @@ var DefaultStreamTextResult = class {
|
|
4610
4852
|
usage: stepUsage,
|
4611
4853
|
experimental_providerMetadata: stepProviderMetadata,
|
4612
4854
|
logprobs: stepLogProbs,
|
4613
|
-
response: {
|
4614
|
-
...stepResponse
|
4615
|
-
},
|
4616
|
-
isContinued: nextStepType === "continue"
|
4617
|
-
});
|
4618
|
-
if (stepType === "continue") {
|
4619
|
-
const lastMessage = responseMessages[responseMessages.length - 1];
|
4620
|
-
if (typeof lastMessage.content === "string") {
|
4621
|
-
lastMessage.content += stepText;
|
4622
|
-
} else {
|
4623
|
-
lastMessage.content.push({
|
4624
|
-
text: stepText,
|
4625
|
-
type: "text"
|
4626
|
-
});
|
4627
|
-
}
|
4628
|
-
} else {
|
4629
|
-
responseMessages.push(
|
4630
|
-
...toResponseMessages({
|
4631
|
-
text: stepText,
|
4632
|
-
tools: tools != null ? tools : {},
|
4633
|
-
toolCalls: stepToolCalls,
|
4634
|
-
toolResults: stepToolResults
|
4635
|
-
})
|
4636
|
-
);
|
4637
|
-
}
|
4638
|
-
const currentStepResult = {
|
4639
|
-
stepType,
|
4640
|
-
text: stepText,
|
4641
|
-
toolCalls: stepToolCalls,
|
4642
|
-
toolResults: stepToolResults,
|
4643
|
-
finishReason: stepFinishReason,
|
4644
|
-
usage: stepUsage,
|
4645
|
-
warnings,
|
4646
|
-
logprobs: stepLogProbs,
|
4647
4855
|
request: stepRequest,
|
4648
4856
|
response: {
|
4649
4857
|
...stepResponse,
|
4650
|
-
headers: rawResponse == null ? void 0 : rawResponse.headers
|
4651
|
-
// deep clone msgs to avoid mutating past messages in multi-step:
|
4652
|
-
messages: JSON.parse(JSON.stringify(responseMessages))
|
4858
|
+
headers: rawResponse == null ? void 0 : rawResponse.headers
|
4653
4859
|
},
|
4654
|
-
|
4860
|
+
warnings,
|
4655
4861
|
isContinued: nextStepType === "continue"
|
4656
|
-
};
|
4657
|
-
|
4658
|
-
|
4659
|
-
const combinedUsage = {
|
4660
|
-
promptTokens: usage.promptTokens + stepUsage.promptTokens,
|
4661
|
-
completionTokens: usage.completionTokens + stepUsage.completionTokens,
|
4662
|
-
totalTokens: usage.totalTokens + stepUsage.totalTokens
|
4663
|
-
};
|
4664
|
-
if (nextStepType !== "done") {
|
4665
|
-
await streamStep({
|
4666
|
-
currentStep: currentStep + 1,
|
4667
|
-
responseMessages,
|
4668
|
-
usage: combinedUsage,
|
4669
|
-
stepType: nextStepType,
|
4670
|
-
previousStepText: fullStepText,
|
4671
|
-
hasLeadingWhitespace: hasWhitespaceSuffix
|
4672
|
-
});
|
4673
|
-
return;
|
4674
|
-
}
|
4675
|
-
try {
|
4862
|
+
});
|
4863
|
+
const combinedUsage = addLanguageModelUsage(usage, stepUsage);
|
4864
|
+
if (nextStepType === "done") {
|
4676
4865
|
controller.enqueue({
|
4677
4866
|
type: "finish",
|
4678
4867
|
finishReason: stepFinishReason,
|
@@ -4680,63 +4869,40 @@ var DefaultStreamTextResult = class {
|
|
4680
4869
|
experimental_providerMetadata: stepProviderMetadata,
|
4681
4870
|
logprobs: stepLogProbs,
|
4682
4871
|
response: {
|
4683
|
-
...stepResponse
|
4872
|
+
...stepResponse,
|
4873
|
+
headers: rawResponse == null ? void 0 : rawResponse.headers
|
4684
4874
|
}
|
4685
4875
|
});
|
4686
4876
|
self.closeStream();
|
4687
|
-
|
4688
|
-
|
4689
|
-
|
4690
|
-
|
4691
|
-
|
4692
|
-
|
4693
|
-
|
4694
|
-
|
4695
|
-
|
4696
|
-
|
4697
|
-
|
4698
|
-
|
4699
|
-
|
4700
|
-
|
4701
|
-
|
4702
|
-
|
4703
|
-
|
4704
|
-
|
4705
|
-
|
4706
|
-
|
4707
|
-
|
4708
|
-
|
4709
|
-
|
4710
|
-
|
4711
|
-
messages: responseMessages
|
4712
|
-
});
|
4713
|
-
self.stepsPromise.resolve(stepResults);
|
4714
|
-
self.warningsPromise.resolve(warnings != null ? warnings : []);
|
4715
|
-
await (onFinish == null ? void 0 : onFinish({
|
4716
|
-
finishReason: stepFinishReason,
|
4717
|
-
logprobs: stepLogProbs,
|
4877
|
+
} else {
|
4878
|
+
if (stepType2 === "continue") {
|
4879
|
+
const lastMessage = responseMessages[responseMessages.length - 1];
|
4880
|
+
if (typeof lastMessage.content === "string") {
|
4881
|
+
lastMessage.content += stepText;
|
4882
|
+
} else {
|
4883
|
+
lastMessage.content.push({
|
4884
|
+
text: stepText,
|
4885
|
+
type: "text"
|
4886
|
+
});
|
4887
|
+
}
|
4888
|
+
} else {
|
4889
|
+
responseMessages.push(
|
4890
|
+
...toResponseMessages({
|
4891
|
+
text: stepText,
|
4892
|
+
tools: tools != null ? tools : {},
|
4893
|
+
toolCalls: stepToolCalls,
|
4894
|
+
toolResults: stepToolResults
|
4895
|
+
})
|
4896
|
+
);
|
4897
|
+
}
|
4898
|
+
await streamStep({
|
4899
|
+
currentStep: currentStep + 1,
|
4900
|
+
responseMessages,
|
4718
4901
|
usage: combinedUsage,
|
4719
|
-
|
4720
|
-
|
4721
|
-
|
4722
|
-
|
4723
|
-
// optional as well. Therefore we need to cast the toolResults to any.
|
4724
|
-
// The type exposed to the users will be correctly inferred.
|
4725
|
-
toolResults: stepToolResults,
|
4726
|
-
request: stepRequest,
|
4727
|
-
response: {
|
4728
|
-
...stepResponse,
|
4729
|
-
headers: rawResponse == null ? void 0 : rawResponse.headers,
|
4730
|
-
messages: responseMessages
|
4731
|
-
},
|
4732
|
-
warnings,
|
4733
|
-
experimental_providerMetadata: stepProviderMetadata,
|
4734
|
-
steps: stepResults
|
4735
|
-
}));
|
4736
|
-
} catch (error) {
|
4737
|
-
controller.error(error);
|
4738
|
-
} finally {
|
4739
|
-
rootSpan.end();
|
4902
|
+
stepType: nextStepType,
|
4903
|
+
previousStepText: fullStepText,
|
4904
|
+
hasLeadingWhitespace: hasWhitespaceSuffix
|
4905
|
+
});
|
4740
4906
|
}
|
4741
4907
|
}
|
4742
4908
|
})
|