ai 3.2.0 → 3.2.2
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/dist/index.d.mts +51 -23
- package/dist/index.d.ts +51 -23
- package/dist/index.js +156 -414
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +130 -385
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -8
- package/rsc/dist/index.d.ts +68 -3
- package/rsc/dist/rsc-server.d.mts +68 -3
- package/rsc/dist/rsc-server.mjs +17 -0
- package/rsc/dist/rsc-server.mjs.map +1 -1
package/dist/index.js
CHANGED
@@ -92,14 +92,14 @@ __export(streams_exports, {
|
|
92
92
|
experimental_generateText: () => experimental_generateText,
|
93
93
|
experimental_streamObject: () => experimental_streamObject,
|
94
94
|
experimental_streamText: () => experimental_streamText,
|
95
|
-
formatStreamPart: () =>
|
95
|
+
formatStreamPart: () => import_ui_utils6.formatStreamPart,
|
96
96
|
generateId: () => generateId2,
|
97
97
|
generateObject: () => generateObject,
|
98
98
|
generateText: () => generateText,
|
99
99
|
nanoid: () => nanoid,
|
100
|
-
parseComplexResponse: () =>
|
101
|
-
parseStreamPart: () =>
|
102
|
-
readDataStream: () =>
|
100
|
+
parseComplexResponse: () => import_ui_utils6.parseComplexResponse,
|
101
|
+
parseStreamPart: () => import_ui_utils6.parseStreamPart,
|
102
|
+
readDataStream: () => import_ui_utils6.readDataStream,
|
103
103
|
readableFromAsyncIterable: () => readableFromAsyncIterable,
|
104
104
|
streamObject: () => streamObject,
|
105
105
|
streamText: () => streamText,
|
@@ -108,7 +108,7 @@ __export(streams_exports, {
|
|
108
108
|
trimStartOfStreamHelper: () => trimStartOfStreamHelper
|
109
109
|
});
|
110
110
|
module.exports = __toCommonJS(streams_exports);
|
111
|
-
var
|
111
|
+
var import_ui_utils6 = require("@ai-sdk/ui-utils");
|
112
112
|
var import_provider_utils7 = require("@ai-sdk/provider-utils");
|
113
113
|
|
114
114
|
// core/util/retry-with-exponential-backoff.ts
|
@@ -623,6 +623,16 @@ function injectJsonSchemaIntoSystem({
|
|
623
623
|
].filter((line) => line != null).join("\n");
|
624
624
|
}
|
625
625
|
|
626
|
+
// core/util/prepare-response-headers.ts
|
627
|
+
function prepareResponseHeaders(init, { contentType }) {
|
628
|
+
var _a;
|
629
|
+
const headers = new Headers((_a = init == null ? void 0 : init.headers) != null ? _a : {});
|
630
|
+
if (!headers.has("Content-Type")) {
|
631
|
+
headers.set("Content-Type", contentType);
|
632
|
+
}
|
633
|
+
return headers;
|
634
|
+
}
|
635
|
+
|
626
636
|
// core/generate-object/generate-object.ts
|
627
637
|
async function generateObject({
|
628
638
|
model,
|
@@ -765,9 +775,26 @@ var GenerateObjectResult = class {
|
|
765
775
|
this.rawResponse = options.rawResponse;
|
766
776
|
this.logprobs = options.logprobs;
|
767
777
|
}
|
778
|
+
/**
|
779
|
+
Converts the object to a JSON response.
|
780
|
+
The response will have a status code of 200 and a content type of `application/json; charset=utf-8`.
|
781
|
+
*/
|
782
|
+
toJsonResponse(init) {
|
783
|
+
var _a;
|
784
|
+
return new Response(JSON.stringify(this.object), {
|
785
|
+
status: (_a = init == null ? void 0 : init.status) != null ? _a : 200,
|
786
|
+
headers: prepareResponseHeaders(init, {
|
787
|
+
contentType: "application/json; charset=utf-8"
|
788
|
+
})
|
789
|
+
});
|
790
|
+
}
|
768
791
|
};
|
769
792
|
var experimental_generateObject = generateObject;
|
770
793
|
|
794
|
+
// core/generate-object/stream-object.ts
|
795
|
+
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
796
|
+
var import_ui_utils = require("@ai-sdk/ui-utils");
|
797
|
+
|
771
798
|
// core/util/async-iterable-stream.ts
|
772
799
|
function createAsyncIterableStream(source, transformer) {
|
773
800
|
const transformedStream = source.pipeThrough(
|
@@ -785,381 +812,7 @@ function createAsyncIterableStream(source, transformer) {
|
|
785
812
|
return transformedStream;
|
786
813
|
}
|
787
814
|
|
788
|
-
// core/util/is-deep-equal-data.ts
|
789
|
-
function isDeepEqualData(obj1, obj2) {
|
790
|
-
if (obj1 === obj2)
|
791
|
-
return true;
|
792
|
-
if (obj1 == null || obj2 == null)
|
793
|
-
return false;
|
794
|
-
if (typeof obj1 !== "object" && typeof obj2 !== "object")
|
795
|
-
return obj1 === obj2;
|
796
|
-
if (obj1.constructor !== obj2.constructor)
|
797
|
-
return false;
|
798
|
-
if (obj1 instanceof Date && obj2 instanceof Date) {
|
799
|
-
return obj1.getTime() === obj2.getTime();
|
800
|
-
}
|
801
|
-
if (Array.isArray(obj1)) {
|
802
|
-
if (obj1.length !== obj2.length)
|
803
|
-
return false;
|
804
|
-
for (let i = 0; i < obj1.length; i++) {
|
805
|
-
if (!isDeepEqualData(obj1[i], obj2[i]))
|
806
|
-
return false;
|
807
|
-
}
|
808
|
-
return true;
|
809
|
-
}
|
810
|
-
const keys1 = Object.keys(obj1);
|
811
|
-
const keys2 = Object.keys(obj2);
|
812
|
-
if (keys1.length !== keys2.length)
|
813
|
-
return false;
|
814
|
-
for (const key of keys1) {
|
815
|
-
if (!keys2.includes(key))
|
816
|
-
return false;
|
817
|
-
if (!isDeepEqualData(obj1[key], obj2[key]))
|
818
|
-
return false;
|
819
|
-
}
|
820
|
-
return true;
|
821
|
-
}
|
822
|
-
|
823
|
-
// core/util/parse-partial-json.ts
|
824
|
-
var import_secure_json_parse = __toESM(require("secure-json-parse"));
|
825
|
-
|
826
|
-
// core/util/fix-json.ts
|
827
|
-
function fixJson(input) {
|
828
|
-
const stack = ["ROOT"];
|
829
|
-
let lastValidIndex = -1;
|
830
|
-
let literalStart = null;
|
831
|
-
function processValueStart(char, i, swapState) {
|
832
|
-
{
|
833
|
-
switch (char) {
|
834
|
-
case '"': {
|
835
|
-
lastValidIndex = i;
|
836
|
-
stack.pop();
|
837
|
-
stack.push(swapState);
|
838
|
-
stack.push("INSIDE_STRING");
|
839
|
-
break;
|
840
|
-
}
|
841
|
-
case "f":
|
842
|
-
case "t":
|
843
|
-
case "n": {
|
844
|
-
lastValidIndex = i;
|
845
|
-
literalStart = i;
|
846
|
-
stack.pop();
|
847
|
-
stack.push(swapState);
|
848
|
-
stack.push("INSIDE_LITERAL");
|
849
|
-
break;
|
850
|
-
}
|
851
|
-
case "-": {
|
852
|
-
stack.pop();
|
853
|
-
stack.push(swapState);
|
854
|
-
stack.push("INSIDE_NUMBER");
|
855
|
-
break;
|
856
|
-
}
|
857
|
-
case "0":
|
858
|
-
case "1":
|
859
|
-
case "2":
|
860
|
-
case "3":
|
861
|
-
case "4":
|
862
|
-
case "5":
|
863
|
-
case "6":
|
864
|
-
case "7":
|
865
|
-
case "8":
|
866
|
-
case "9": {
|
867
|
-
lastValidIndex = i;
|
868
|
-
stack.pop();
|
869
|
-
stack.push(swapState);
|
870
|
-
stack.push("INSIDE_NUMBER");
|
871
|
-
break;
|
872
|
-
}
|
873
|
-
case "{": {
|
874
|
-
lastValidIndex = i;
|
875
|
-
stack.pop();
|
876
|
-
stack.push(swapState);
|
877
|
-
stack.push("INSIDE_OBJECT_START");
|
878
|
-
break;
|
879
|
-
}
|
880
|
-
case "[": {
|
881
|
-
lastValidIndex = i;
|
882
|
-
stack.pop();
|
883
|
-
stack.push(swapState);
|
884
|
-
stack.push("INSIDE_ARRAY_START");
|
885
|
-
break;
|
886
|
-
}
|
887
|
-
}
|
888
|
-
}
|
889
|
-
}
|
890
|
-
function processAfterObjectValue(char, i) {
|
891
|
-
switch (char) {
|
892
|
-
case ",": {
|
893
|
-
stack.pop();
|
894
|
-
stack.push("INSIDE_OBJECT_AFTER_COMMA");
|
895
|
-
break;
|
896
|
-
}
|
897
|
-
case "}": {
|
898
|
-
lastValidIndex = i;
|
899
|
-
stack.pop();
|
900
|
-
break;
|
901
|
-
}
|
902
|
-
}
|
903
|
-
}
|
904
|
-
function processAfterArrayValue(char, i) {
|
905
|
-
switch (char) {
|
906
|
-
case ",": {
|
907
|
-
stack.pop();
|
908
|
-
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
909
|
-
break;
|
910
|
-
}
|
911
|
-
case "]": {
|
912
|
-
lastValidIndex = i;
|
913
|
-
stack.pop();
|
914
|
-
break;
|
915
|
-
}
|
916
|
-
}
|
917
|
-
}
|
918
|
-
for (let i = 0; i < input.length; i++) {
|
919
|
-
const char = input[i];
|
920
|
-
const currentState = stack[stack.length - 1];
|
921
|
-
switch (currentState) {
|
922
|
-
case "ROOT":
|
923
|
-
processValueStart(char, i, "FINISH");
|
924
|
-
break;
|
925
|
-
case "INSIDE_OBJECT_START": {
|
926
|
-
switch (char) {
|
927
|
-
case '"': {
|
928
|
-
stack.pop();
|
929
|
-
stack.push("INSIDE_OBJECT_KEY");
|
930
|
-
break;
|
931
|
-
}
|
932
|
-
case "}": {
|
933
|
-
lastValidIndex = i;
|
934
|
-
stack.pop();
|
935
|
-
break;
|
936
|
-
}
|
937
|
-
}
|
938
|
-
break;
|
939
|
-
}
|
940
|
-
case "INSIDE_OBJECT_AFTER_COMMA": {
|
941
|
-
switch (char) {
|
942
|
-
case '"': {
|
943
|
-
stack.pop();
|
944
|
-
stack.push("INSIDE_OBJECT_KEY");
|
945
|
-
break;
|
946
|
-
}
|
947
|
-
}
|
948
|
-
break;
|
949
|
-
}
|
950
|
-
case "INSIDE_OBJECT_KEY": {
|
951
|
-
switch (char) {
|
952
|
-
case '"': {
|
953
|
-
stack.pop();
|
954
|
-
stack.push("INSIDE_OBJECT_AFTER_KEY");
|
955
|
-
break;
|
956
|
-
}
|
957
|
-
}
|
958
|
-
break;
|
959
|
-
}
|
960
|
-
case "INSIDE_OBJECT_AFTER_KEY": {
|
961
|
-
switch (char) {
|
962
|
-
case ":": {
|
963
|
-
stack.pop();
|
964
|
-
stack.push("INSIDE_OBJECT_BEFORE_VALUE");
|
965
|
-
break;
|
966
|
-
}
|
967
|
-
}
|
968
|
-
break;
|
969
|
-
}
|
970
|
-
case "INSIDE_OBJECT_BEFORE_VALUE": {
|
971
|
-
processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
|
972
|
-
break;
|
973
|
-
}
|
974
|
-
case "INSIDE_OBJECT_AFTER_VALUE": {
|
975
|
-
processAfterObjectValue(char, i);
|
976
|
-
break;
|
977
|
-
}
|
978
|
-
case "INSIDE_STRING": {
|
979
|
-
switch (char) {
|
980
|
-
case '"': {
|
981
|
-
stack.pop();
|
982
|
-
lastValidIndex = i;
|
983
|
-
break;
|
984
|
-
}
|
985
|
-
case "\\": {
|
986
|
-
stack.push("INSIDE_STRING_ESCAPE");
|
987
|
-
break;
|
988
|
-
}
|
989
|
-
default: {
|
990
|
-
lastValidIndex = i;
|
991
|
-
}
|
992
|
-
}
|
993
|
-
break;
|
994
|
-
}
|
995
|
-
case "INSIDE_ARRAY_START": {
|
996
|
-
switch (char) {
|
997
|
-
case "]": {
|
998
|
-
lastValidIndex = i;
|
999
|
-
stack.pop();
|
1000
|
-
break;
|
1001
|
-
}
|
1002
|
-
default: {
|
1003
|
-
lastValidIndex = i;
|
1004
|
-
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
1005
|
-
break;
|
1006
|
-
}
|
1007
|
-
}
|
1008
|
-
break;
|
1009
|
-
}
|
1010
|
-
case "INSIDE_ARRAY_AFTER_VALUE": {
|
1011
|
-
switch (char) {
|
1012
|
-
case ",": {
|
1013
|
-
stack.pop();
|
1014
|
-
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
1015
|
-
break;
|
1016
|
-
}
|
1017
|
-
case "]": {
|
1018
|
-
lastValidIndex = i;
|
1019
|
-
stack.pop();
|
1020
|
-
break;
|
1021
|
-
}
|
1022
|
-
default: {
|
1023
|
-
lastValidIndex = i;
|
1024
|
-
break;
|
1025
|
-
}
|
1026
|
-
}
|
1027
|
-
break;
|
1028
|
-
}
|
1029
|
-
case "INSIDE_ARRAY_AFTER_COMMA": {
|
1030
|
-
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
1031
|
-
break;
|
1032
|
-
}
|
1033
|
-
case "INSIDE_STRING_ESCAPE": {
|
1034
|
-
stack.pop();
|
1035
|
-
lastValidIndex = i;
|
1036
|
-
break;
|
1037
|
-
}
|
1038
|
-
case "INSIDE_NUMBER": {
|
1039
|
-
switch (char) {
|
1040
|
-
case "0":
|
1041
|
-
case "1":
|
1042
|
-
case "2":
|
1043
|
-
case "3":
|
1044
|
-
case "4":
|
1045
|
-
case "5":
|
1046
|
-
case "6":
|
1047
|
-
case "7":
|
1048
|
-
case "8":
|
1049
|
-
case "9": {
|
1050
|
-
lastValidIndex = i;
|
1051
|
-
break;
|
1052
|
-
}
|
1053
|
-
case "e":
|
1054
|
-
case "E":
|
1055
|
-
case "-":
|
1056
|
-
case ".": {
|
1057
|
-
break;
|
1058
|
-
}
|
1059
|
-
case ",": {
|
1060
|
-
stack.pop();
|
1061
|
-
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
1062
|
-
processAfterArrayValue(char, i);
|
1063
|
-
}
|
1064
|
-
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
1065
|
-
processAfterObjectValue(char, i);
|
1066
|
-
}
|
1067
|
-
break;
|
1068
|
-
}
|
1069
|
-
case "}": {
|
1070
|
-
stack.pop();
|
1071
|
-
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
1072
|
-
processAfterObjectValue(char, i);
|
1073
|
-
}
|
1074
|
-
break;
|
1075
|
-
}
|
1076
|
-
case "]": {
|
1077
|
-
stack.pop();
|
1078
|
-
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
1079
|
-
processAfterArrayValue(char, i);
|
1080
|
-
}
|
1081
|
-
break;
|
1082
|
-
}
|
1083
|
-
default: {
|
1084
|
-
stack.pop();
|
1085
|
-
break;
|
1086
|
-
}
|
1087
|
-
}
|
1088
|
-
break;
|
1089
|
-
}
|
1090
|
-
case "INSIDE_LITERAL": {
|
1091
|
-
const partialLiteral = input.substring(literalStart, i + 1);
|
1092
|
-
if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
|
1093
|
-
stack.pop();
|
1094
|
-
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
1095
|
-
processAfterObjectValue(char, i);
|
1096
|
-
} else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
1097
|
-
processAfterArrayValue(char, i);
|
1098
|
-
}
|
1099
|
-
} else {
|
1100
|
-
lastValidIndex = i;
|
1101
|
-
}
|
1102
|
-
break;
|
1103
|
-
}
|
1104
|
-
}
|
1105
|
-
}
|
1106
|
-
let result = input.slice(0, lastValidIndex + 1);
|
1107
|
-
for (let i = stack.length - 1; i >= 0; i--) {
|
1108
|
-
const state = stack[i];
|
1109
|
-
switch (state) {
|
1110
|
-
case "INSIDE_STRING": {
|
1111
|
-
result += '"';
|
1112
|
-
break;
|
1113
|
-
}
|
1114
|
-
case "INSIDE_OBJECT_KEY":
|
1115
|
-
case "INSIDE_OBJECT_AFTER_KEY":
|
1116
|
-
case "INSIDE_OBJECT_AFTER_COMMA":
|
1117
|
-
case "INSIDE_OBJECT_START":
|
1118
|
-
case "INSIDE_OBJECT_BEFORE_VALUE":
|
1119
|
-
case "INSIDE_OBJECT_AFTER_VALUE": {
|
1120
|
-
result += "}";
|
1121
|
-
break;
|
1122
|
-
}
|
1123
|
-
case "INSIDE_ARRAY_START":
|
1124
|
-
case "INSIDE_ARRAY_AFTER_COMMA":
|
1125
|
-
case "INSIDE_ARRAY_AFTER_VALUE": {
|
1126
|
-
result += "]";
|
1127
|
-
break;
|
1128
|
-
}
|
1129
|
-
case "INSIDE_LITERAL": {
|
1130
|
-
const partialLiteral = input.substring(literalStart, input.length);
|
1131
|
-
if ("true".startsWith(partialLiteral)) {
|
1132
|
-
result += "true".slice(partialLiteral.length);
|
1133
|
-
} else if ("false".startsWith(partialLiteral)) {
|
1134
|
-
result += "false".slice(partialLiteral.length);
|
1135
|
-
} else if ("null".startsWith(partialLiteral)) {
|
1136
|
-
result += "null".slice(partialLiteral.length);
|
1137
|
-
}
|
1138
|
-
}
|
1139
|
-
}
|
1140
|
-
}
|
1141
|
-
return result;
|
1142
|
-
}
|
1143
|
-
|
1144
|
-
// core/util/parse-partial-json.ts
|
1145
|
-
function parsePartialJson(jsonText) {
|
1146
|
-
if (jsonText == null) {
|
1147
|
-
return void 0;
|
1148
|
-
}
|
1149
|
-
try {
|
1150
|
-
return import_secure_json_parse.default.parse(jsonText);
|
1151
|
-
} catch (ignored) {
|
1152
|
-
try {
|
1153
|
-
const fixedJsonText = fixJson(jsonText);
|
1154
|
-
return import_secure_json_parse.default.parse(fixedJsonText);
|
1155
|
-
} catch (ignored2) {
|
1156
|
-
}
|
1157
|
-
}
|
1158
|
-
return void 0;
|
1159
|
-
}
|
1160
|
-
|
1161
815
|
// core/generate-object/stream-object.ts
|
1162
|
-
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
1163
816
|
async function streamObject({
|
1164
817
|
model,
|
1165
818
|
schema,
|
@@ -1313,23 +966,39 @@ var StreamObjectResult = class {
|
|
1313
966
|
let object;
|
1314
967
|
let error;
|
1315
968
|
let accumulatedText = "";
|
969
|
+
let delta = "";
|
1316
970
|
let latestObject = void 0;
|
1317
971
|
this.originalStream = stream.pipeThrough(
|
1318
972
|
new TransformStream({
|
1319
973
|
async transform(chunk, controller) {
|
1320
974
|
if (typeof chunk === "string") {
|
1321
975
|
accumulatedText += chunk;
|
1322
|
-
|
976
|
+
delta += chunk;
|
977
|
+
const currentObject = (0, import_ui_utils.parsePartialJson)(
|
1323
978
|
accumulatedText
|
1324
979
|
);
|
1325
|
-
if (!isDeepEqualData(latestObject, currentObject)) {
|
980
|
+
if (!(0, import_ui_utils.isDeepEqualData)(latestObject, currentObject)) {
|
1326
981
|
latestObject = currentObject;
|
1327
|
-
controller.enqueue({
|
982
|
+
controller.enqueue({
|
983
|
+
type: "object",
|
984
|
+
object: currentObject
|
985
|
+
});
|
986
|
+
controller.enqueue({
|
987
|
+
type: "text-delta",
|
988
|
+
textDelta: delta
|
989
|
+
});
|
990
|
+
delta = "";
|
1328
991
|
}
|
1329
992
|
return;
|
1330
993
|
}
|
1331
994
|
switch (chunk.type) {
|
1332
995
|
case "finish": {
|
996
|
+
if (delta !== "") {
|
997
|
+
controller.enqueue({
|
998
|
+
type: "text-delta",
|
999
|
+
textDelta: delta
|
1000
|
+
});
|
1001
|
+
}
|
1333
1002
|
usage = calculateTokenUsage(chunk.usage);
|
1334
1003
|
controller.enqueue({ ...chunk, usage });
|
1335
1004
|
resolveUsage(usage);
|
@@ -1373,6 +1042,12 @@ var StreamObjectResult = class {
|
|
1373
1042
|
})
|
1374
1043
|
);
|
1375
1044
|
}
|
1045
|
+
/**
|
1046
|
+
Stream of partial objects. It gets more complete as the stream progresses.
|
1047
|
+
|
1048
|
+
Note that the partial object is not validated.
|
1049
|
+
If you want to be certain that the actual content matches your schema, you need to implement your own validation for partial results.
|
1050
|
+
*/
|
1376
1051
|
get partialObjectStream() {
|
1377
1052
|
return createAsyncIterableStream(this.originalStream, {
|
1378
1053
|
transform(chunk, controller) {
|
@@ -1380,6 +1055,32 @@ var StreamObjectResult = class {
|
|
1380
1055
|
case "object":
|
1381
1056
|
controller.enqueue(chunk.object);
|
1382
1057
|
break;
|
1058
|
+
case "text-delta":
|
1059
|
+
case "finish":
|
1060
|
+
break;
|
1061
|
+
case "error":
|
1062
|
+
controller.error(chunk.error);
|
1063
|
+
break;
|
1064
|
+
default: {
|
1065
|
+
const _exhaustiveCheck = chunk;
|
1066
|
+
throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
|
1067
|
+
}
|
1068
|
+
}
|
1069
|
+
}
|
1070
|
+
});
|
1071
|
+
}
|
1072
|
+
/**
|
1073
|
+
Text stream of the JSON representation of the generated object. It contains text chunks.
|
1074
|
+
When the stream is finished, the object is valid JSON that can be parsed.
|
1075
|
+
*/
|
1076
|
+
get textStream() {
|
1077
|
+
return createAsyncIterableStream(this.originalStream, {
|
1078
|
+
transform(chunk, controller) {
|
1079
|
+
switch (chunk.type) {
|
1080
|
+
case "text-delta":
|
1081
|
+
controller.enqueue(chunk.textDelta);
|
1082
|
+
break;
|
1083
|
+
case "object":
|
1383
1084
|
case "finish":
|
1384
1085
|
break;
|
1385
1086
|
case "error":
|
@@ -1393,6 +1094,9 @@ var StreamObjectResult = class {
|
|
1393
1094
|
}
|
1394
1095
|
});
|
1395
1096
|
}
|
1097
|
+
/**
|
1098
|
+
Stream of different types of events, including partial objects, errors, and finish events.
|
1099
|
+
*/
|
1396
1100
|
get fullStream() {
|
1397
1101
|
return createAsyncIterableStream(this.originalStream, {
|
1398
1102
|
transform(chunk, controller) {
|
@@ -1400,6 +1104,54 @@ var StreamObjectResult = class {
|
|
1400
1104
|
}
|
1401
1105
|
});
|
1402
1106
|
}
|
1107
|
+
/**
|
1108
|
+
Writes text delta output to a Node.js response-like object.
|
1109
|
+
It sets a `Content-Type` header to `text/plain; charset=utf-8` and
|
1110
|
+
writes each text delta as a separate chunk.
|
1111
|
+
|
1112
|
+
@param response A Node.js response-like object (ServerResponse).
|
1113
|
+
@param init Optional headers and status code.
|
1114
|
+
*/
|
1115
|
+
pipeTextStreamToResponse(response, init) {
|
1116
|
+
var _a;
|
1117
|
+
response.writeHead((_a = init == null ? void 0 : init.status) != null ? _a : 200, {
|
1118
|
+
"Content-Type": "text/plain; charset=utf-8",
|
1119
|
+
...init == null ? void 0 : init.headers
|
1120
|
+
});
|
1121
|
+
const reader = this.textStream.pipeThrough(new TextEncoderStream()).getReader();
|
1122
|
+
const read = async () => {
|
1123
|
+
try {
|
1124
|
+
while (true) {
|
1125
|
+
const { done, value } = await reader.read();
|
1126
|
+
if (done)
|
1127
|
+
break;
|
1128
|
+
response.write(value);
|
1129
|
+
}
|
1130
|
+
} catch (error) {
|
1131
|
+
throw error;
|
1132
|
+
} finally {
|
1133
|
+
response.end();
|
1134
|
+
}
|
1135
|
+
};
|
1136
|
+
read();
|
1137
|
+
}
|
1138
|
+
/**
|
1139
|
+
Creates a simple text stream response.
|
1140
|
+
The response has a `Content-Type` header set to `text/plain; charset=utf-8`.
|
1141
|
+
Each text delta is encoded as UTF-8 and sent as a separate chunk.
|
1142
|
+
Non-text-delta events are ignored.
|
1143
|
+
|
1144
|
+
@param init Optional headers and status code.
|
1145
|
+
*/
|
1146
|
+
toTextStreamResponse(init) {
|
1147
|
+
var _a;
|
1148
|
+
return new Response(this.textStream.pipeThrough(new TextEncoderStream()), {
|
1149
|
+
status: (_a = init == null ? void 0 : init.status) != null ? _a : 200,
|
1150
|
+
headers: prepareResponseHeaders(init, {
|
1151
|
+
contentType: "text/plain; charset=utf-8"
|
1152
|
+
})
|
1153
|
+
});
|
1154
|
+
}
|
1403
1155
|
};
|
1404
1156
|
var experimental_streamObject = streamObject;
|
1405
1157
|
|
@@ -1601,19 +1353,9 @@ function toResponseMessages({
|
|
1601
1353
|
}
|
1602
1354
|
var experimental_generateText = generateText;
|
1603
1355
|
|
1604
|
-
// core/util/prepare-response-headers.ts
|
1605
|
-
function prepareResponseHeaders(init, { contentType }) {
|
1606
|
-
var _a;
|
1607
|
-
const headers = new Headers((_a = init == null ? void 0 : init.headers) != null ? _a : {});
|
1608
|
-
if (!headers.has("Content-Type")) {
|
1609
|
-
headers.set("Content-Type", contentType);
|
1610
|
-
}
|
1611
|
-
return headers;
|
1612
|
-
}
|
1613
|
-
|
1614
1356
|
// core/generate-text/run-tools-transformation.ts
|
1615
1357
|
var import_provider7 = require("@ai-sdk/provider");
|
1616
|
-
var
|
1358
|
+
var import_ui_utils2 = require("@ai-sdk/ui-utils");
|
1617
1359
|
function runToolsTransformation({
|
1618
1360
|
tools,
|
1619
1361
|
generatorStream
|
@@ -1662,7 +1404,7 @@ function runToolsTransformation({
|
|
1662
1404
|
});
|
1663
1405
|
controller.enqueue(toolCall);
|
1664
1406
|
if (tool2.execute != null) {
|
1665
|
-
const toolExecutionId = (0,
|
1407
|
+
const toolExecutionId = (0, import_ui_utils2.generateId)();
|
1666
1408
|
outstandingToolCalls.add(toolExecutionId);
|
1667
1409
|
tool2.execute(toolCall.args).then(
|
1668
1410
|
(result) => {
|
@@ -1960,11 +1702,11 @@ var StreamTextResult = class {
|
|
1960
1702
|
transform: async (chunk, controller) => {
|
1961
1703
|
switch (chunk.type) {
|
1962
1704
|
case "text-delta":
|
1963
|
-
controller.enqueue((0,
|
1705
|
+
controller.enqueue((0, import_ui_utils6.formatStreamPart)("text", chunk.textDelta));
|
1964
1706
|
break;
|
1965
1707
|
case "tool-call":
|
1966
1708
|
controller.enqueue(
|
1967
|
-
(0,
|
1709
|
+
(0, import_ui_utils6.formatStreamPart)("tool_call", {
|
1968
1710
|
toolCallId: chunk.toolCallId,
|
1969
1711
|
toolName: chunk.toolName,
|
1970
1712
|
args: chunk.args
|
@@ -1973,7 +1715,7 @@ var StreamTextResult = class {
|
|
1973
1715
|
break;
|
1974
1716
|
case "tool-result":
|
1975
1717
|
controller.enqueue(
|
1976
|
-
(0,
|
1718
|
+
(0, import_ui_utils6.formatStreamPart)("tool_result", {
|
1977
1719
|
toolCallId: chunk.toolCallId,
|
1978
1720
|
toolName: chunk.toolName,
|
1979
1721
|
args: chunk.args,
|
@@ -1983,7 +1725,7 @@ var StreamTextResult = class {
|
|
1983
1725
|
break;
|
1984
1726
|
case "error":
|
1985
1727
|
controller.enqueue(
|
1986
|
-
(0,
|
1728
|
+
(0, import_ui_utils6.formatStreamPart)("error", JSON.stringify(chunk.error))
|
1987
1729
|
);
|
1988
1730
|
break;
|
1989
1731
|
}
|
@@ -2412,7 +2154,7 @@ function readableFromAsyncIterable(iterable) {
|
|
2412
2154
|
}
|
2413
2155
|
|
2414
2156
|
// streams/stream-data.ts
|
2415
|
-
var
|
2157
|
+
var import_ui_utils3 = require("@ai-sdk/ui-utils");
|
2416
2158
|
var StreamData = class {
|
2417
2159
|
constructor() {
|
2418
2160
|
this.encoder = new TextEncoder();
|
@@ -2459,7 +2201,7 @@ var StreamData = class {
|
|
2459
2201
|
throw new Error("Stream controller is not initialized.");
|
2460
2202
|
}
|
2461
2203
|
this.controller.enqueue(
|
2462
|
-
this.encoder.encode((0,
|
2204
|
+
this.encoder.encode((0, import_ui_utils3.formatStreamPart)("data", [value]))
|
2463
2205
|
);
|
2464
2206
|
}
|
2465
2207
|
appendMessageAnnotation(value) {
|
@@ -2470,7 +2212,7 @@ var StreamData = class {
|
|
2470
2212
|
throw new Error("Stream controller is not initialized.");
|
2471
2213
|
}
|
2472
2214
|
this.controller.enqueue(
|
2473
|
-
this.encoder.encode((0,
|
2215
|
+
this.encoder.encode((0, import_ui_utils3.formatStreamPart)("message_annotations", [value]))
|
2474
2216
|
);
|
2475
2217
|
}
|
2476
2218
|
};
|
@@ -2480,7 +2222,7 @@ function createStreamDataTransformer() {
|
|
2480
2222
|
return new TransformStream({
|
2481
2223
|
transform: async (chunk, controller) => {
|
2482
2224
|
const message = decoder.decode(chunk);
|
2483
|
-
controller.enqueue(encoder.encode((0,
|
2225
|
+
controller.enqueue(encoder.encode((0, import_ui_utils3.formatStreamPart)("text", message)));
|
2484
2226
|
}
|
2485
2227
|
});
|
2486
2228
|
}
|
@@ -2534,7 +2276,7 @@ function AnthropicStream(res, cb) {
|
|
2534
2276
|
}
|
2535
2277
|
|
2536
2278
|
// streams/assistant-response.ts
|
2537
|
-
var
|
2279
|
+
var import_ui_utils4 = require("@ai-sdk/ui-utils");
|
2538
2280
|
function AssistantResponse({ threadId, messageId }, process2) {
|
2539
2281
|
const stream = new ReadableStream({
|
2540
2282
|
async start(controller) {
|
@@ -2542,17 +2284,17 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2542
2284
|
const textEncoder = new TextEncoder();
|
2543
2285
|
const sendMessage = (message) => {
|
2544
2286
|
controller.enqueue(
|
2545
|
-
textEncoder.encode((0,
|
2287
|
+
textEncoder.encode((0, import_ui_utils4.formatStreamPart)("assistant_message", message))
|
2546
2288
|
);
|
2547
2289
|
};
|
2548
2290
|
const sendDataMessage = (message) => {
|
2549
2291
|
controller.enqueue(
|
2550
|
-
textEncoder.encode((0,
|
2292
|
+
textEncoder.encode((0, import_ui_utils4.formatStreamPart)("data_message", message))
|
2551
2293
|
);
|
2552
2294
|
};
|
2553
2295
|
const sendError = (errorMessage) => {
|
2554
2296
|
controller.enqueue(
|
2555
|
-
textEncoder.encode((0,
|
2297
|
+
textEncoder.encode((0, import_ui_utils4.formatStreamPart)("error", errorMessage))
|
2556
2298
|
);
|
2557
2299
|
};
|
2558
2300
|
const forwardStream = async (stream2) => {
|
@@ -2563,7 +2305,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2563
2305
|
case "thread.message.created": {
|
2564
2306
|
controller.enqueue(
|
2565
2307
|
textEncoder.encode(
|
2566
|
-
(0,
|
2308
|
+
(0, import_ui_utils4.formatStreamPart)("assistant_message", {
|
2567
2309
|
id: value.data.id,
|
2568
2310
|
role: "assistant",
|
2569
2311
|
content: [{ type: "text", text: { value: "" } }]
|
@@ -2577,7 +2319,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2577
2319
|
if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
|
2578
2320
|
controller.enqueue(
|
2579
2321
|
textEncoder.encode(
|
2580
|
-
(0,
|
2322
|
+
(0, import_ui_utils4.formatStreamPart)("text", content.text.value)
|
2581
2323
|
)
|
2582
2324
|
);
|
2583
2325
|
}
|
@@ -2594,7 +2336,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2594
2336
|
};
|
2595
2337
|
controller.enqueue(
|
2596
2338
|
textEncoder.encode(
|
2597
|
-
(0,
|
2339
|
+
(0, import_ui_utils4.formatStreamPart)("assistant_control_data", {
|
2598
2340
|
threadId,
|
2599
2341
|
messageId
|
2600
2342
|
})
|
@@ -2909,7 +2651,7 @@ function MistralStream(response, callbacks) {
|
|
2909
2651
|
}
|
2910
2652
|
|
2911
2653
|
// streams/openai-stream.ts
|
2912
|
-
var
|
2654
|
+
var import_ui_utils5 = require("@ai-sdk/ui-utils");
|
2913
2655
|
function parseOpenAIStream() {
|
2914
2656
|
const extract = chunkToText();
|
2915
2657
|
return (data) => extract(JSON.parse(data));
|
@@ -3060,7 +2802,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
3060
2802
|
let aggregatedFinalCompletionResponse = "";
|
3061
2803
|
let isFunctionStreamingIn = false;
|
3062
2804
|
let functionCallMessages = callbacks[__internal__OpenAIFnMessagesSymbol] || [];
|
3063
|
-
const decode = (0,
|
2805
|
+
const decode = (0, import_ui_utils5.createChunkDecoder)();
|
3064
2806
|
return new TransformStream({
|
3065
2807
|
async transform(chunk, controller) {
|
3066
2808
|
const message = decode(chunk);
|
@@ -3074,7 +2816,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
3074
2816
|
}
|
3075
2817
|
if (!isFunctionStreamingIn) {
|
3076
2818
|
controller.enqueue(
|
3077
|
-
textEncoder.encode((0,
|
2819
|
+
textEncoder.encode((0, import_ui_utils5.formatStreamPart)("text", message))
|
3078
2820
|
);
|
3079
2821
|
return;
|
3080
2822
|
} else {
|
@@ -3185,7 +2927,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
3185
2927
|
if (!functionResponse) {
|
3186
2928
|
controller.enqueue(
|
3187
2929
|
textEncoder.encode(
|
3188
|
-
(0,
|
2930
|
+
(0, import_ui_utils5.formatStreamPart)(
|
3189
2931
|
payload.function_call ? "function_call" : "tool_calls",
|
3190
2932
|
// parse to prevent double-encoding:
|
3191
2933
|
JSON.parse(aggregatedResponse)
|
@@ -3195,7 +2937,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
3195
2937
|
return;
|
3196
2938
|
} else if (typeof functionResponse === "string") {
|
3197
2939
|
controller.enqueue(
|
3198
|
-
textEncoder.encode((0,
|
2940
|
+
textEncoder.encode((0, import_ui_utils5.formatStreamPart)("text", functionResponse))
|
3199
2941
|
);
|
3200
2942
|
aggregatedFinalCompletionResponse = functionResponse;
|
3201
2943
|
return;
|