ai 3.0.21 → 3.0.23
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 +42 -1
- package/dist/index.d.ts +42 -1
- package/dist/index.js +104 -177
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +65 -138
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -33
- package/react/dist/index.d.mts +6 -2
- package/react/dist/index.d.ts +6 -2
- package/react/dist/index.js +107 -24
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +107 -24
- package/react/dist/index.mjs.map +1 -1
- package/rsc/dist/rsc-server.mjs +3 -3
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/solid/dist/index.d.mts +6 -2
- package/solid/dist/index.d.ts +6 -2
- package/solid/dist/index.js +105 -23
- package/solid/dist/index.js.map +1 -1
- package/solid/dist/index.mjs +105 -23
- package/solid/dist/index.mjs.map +1 -1
- package/svelte/dist/index.d.mts +6 -2
- package/svelte/dist/index.d.ts +6 -2
- package/svelte/dist/index.js +107 -24
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +107 -24
- package/svelte/dist/index.mjs.map +1 -1
- package/vue/dist/index.d.mts +6 -2
- package/vue/dist/index.d.ts +6 -2
- package/vue/dist/index.js +105 -23
- package/vue/dist/index.js.map +1 -1
- package/vue/dist/index.mjs +105 -23
- package/vue/dist/index.mjs.map +1 -1
- package/anthropic/dist/index.d.mts +0 -51
- package/anthropic/dist/index.d.ts +0 -51
- package/anthropic/dist/index.js +0 -792
- package/anthropic/dist/index.js.map +0 -1
- package/anthropic/dist/index.mjs +0 -760
- package/anthropic/dist/index.mjs.map +0 -1
- package/google/dist/index.d.mts +0 -47
- package/google/dist/index.d.ts +0 -47
- package/google/dist/index.js +0 -796
- package/google/dist/index.js.map +0 -1
- package/google/dist/index.mjs +0 -764
- package/google/dist/index.mjs.map +0 -1
- package/mistral/dist/index.d.mts +0 -52
- package/mistral/dist/index.d.ts +0 -52
- package/mistral/dist/index.js +0 -763
- package/mistral/dist/index.js.map +0 -1
- package/mistral/dist/index.mjs +0 -731
- package/mistral/dist/index.mjs.map +0 -1
- package/openai/dist/index.d.mts +0 -116
- package/openai/dist/index.d.ts +0 -116
- package/openai/dist/index.js +0 -1143
- package/openai/dist/index.js.map +0 -1
- package/openai/dist/index.mjs +0 -1115
- package/openai/dist/index.mjs.map +0 -1
package/svelte/dist/index.mjs
CHANGED
@@ -598,9 +598,9 @@ var toolCallStreamPart = {
|
|
598
598
|
code: "7",
|
599
599
|
name: "tool_calls",
|
600
600
|
parse: (value) => {
|
601
|
-
if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some(
|
602
|
-
tc == null || typeof tc !== "object" || !("id" in tc) || typeof tc.id !== "string" || !("type" in tc) || typeof tc.type !== "string" || !("function" in tc) || tc.function == null || typeof tc.function !== "object" || !("arguments" in tc.function) || typeof tc.function.name !== "string" || typeof tc.function.arguments !== "string"
|
603
|
-
|
601
|
+
if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some(
|
602
|
+
(tc) => tc == null || typeof tc !== "object" || !("id" in tc) || typeof tc.id !== "string" || !("type" in tc) || typeof tc.type !== "string" || !("function" in tc) || tc.function == null || typeof tc.function !== "object" || !("arguments" in tc.function) || typeof tc.function.name !== "string" || typeof tc.function.arguments !== "string"
|
603
|
+
)) {
|
604
604
|
throw new Error(
|
605
605
|
'"tool_calls" parts expect an object with a ToolCallPayload.'
|
606
606
|
);
|
@@ -831,11 +831,28 @@ async function parseComplexResponse({
|
|
831
831
|
};
|
832
832
|
}
|
833
833
|
|
834
|
+
// shared/utils.ts
|
835
|
+
function createChunkDecoder(complex) {
|
836
|
+
const decoder = new TextDecoder();
|
837
|
+
if (!complex) {
|
838
|
+
return function(chunk) {
|
839
|
+
if (!chunk)
|
840
|
+
return "";
|
841
|
+
return decoder.decode(chunk, { stream: true });
|
842
|
+
};
|
843
|
+
}
|
844
|
+
return function(chunk) {
|
845
|
+
const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
|
846
|
+
return decoded.map(parseStreamPart).filter(Boolean);
|
847
|
+
};
|
848
|
+
}
|
849
|
+
|
834
850
|
// shared/call-chat-api.ts
|
835
851
|
async function callChatApi({
|
836
852
|
api,
|
837
853
|
messages,
|
838
854
|
body,
|
855
|
+
streamMode = "stream-data",
|
839
856
|
credentials,
|
840
857
|
headers,
|
841
858
|
abortController,
|
@@ -879,17 +896,52 @@ async function callChatApi({
|
|
879
896
|
throw new Error("The response body is empty.");
|
880
897
|
}
|
881
898
|
const reader = response.body.getReader();
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
899
|
+
switch (streamMode) {
|
900
|
+
case "text": {
|
901
|
+
const decoder = createChunkDecoder();
|
902
|
+
const resultMessage = {
|
903
|
+
id: generateId2(),
|
904
|
+
createdAt: /* @__PURE__ */ new Date(),
|
905
|
+
role: "assistant",
|
906
|
+
content: ""
|
907
|
+
};
|
908
|
+
while (true) {
|
909
|
+
const { done, value } = await reader.read();
|
910
|
+
if (done) {
|
911
|
+
break;
|
912
|
+
}
|
913
|
+
resultMessage.content += decoder(value);
|
914
|
+
resultMessage.id = generateId2();
|
915
|
+
onUpdate([{ ...resultMessage }], []);
|
916
|
+
if ((abortController == null ? void 0 : abortController()) === null) {
|
917
|
+
reader.cancel();
|
918
|
+
break;
|
919
|
+
}
|
889
920
|
}
|
890
|
-
|
891
|
-
|
892
|
-
|
921
|
+
onFinish == null ? void 0 : onFinish(resultMessage);
|
922
|
+
return {
|
923
|
+
messages: [resultMessage],
|
924
|
+
data: []
|
925
|
+
};
|
926
|
+
}
|
927
|
+
case "stream-data": {
|
928
|
+
return await parseComplexResponse({
|
929
|
+
reader,
|
930
|
+
abortControllerRef: abortController != null ? { current: abortController() } : void 0,
|
931
|
+
update: onUpdate,
|
932
|
+
onFinish(prefixMap) {
|
933
|
+
if (onFinish && prefixMap.text != null) {
|
934
|
+
onFinish(prefixMap.text);
|
935
|
+
}
|
936
|
+
},
|
937
|
+
generateId: generateId2
|
938
|
+
});
|
939
|
+
}
|
940
|
+
default: {
|
941
|
+
const exhaustiveCheck = streamMode;
|
942
|
+
throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
|
943
|
+
}
|
944
|
+
}
|
893
945
|
}
|
894
946
|
|
895
947
|
// shared/process-chat-stream.ts
|
@@ -1009,7 +1061,7 @@ async function processChatStream({
|
|
1009
1061
|
}
|
1010
1062
|
|
1011
1063
|
// svelte/use-chat.ts
|
1012
|
-
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId2, onFinish, onResponse, sendExtraMessageFields) => {
|
1064
|
+
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId2, streamMode, onFinish, onResponse, sendExtraMessageFields) => {
|
1013
1065
|
var _a, _b;
|
1014
1066
|
mutate(chatRequest.messages);
|
1015
1067
|
const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
|
@@ -1045,6 +1097,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
|
|
1045
1097
|
tool_choice: chatRequest.tool_choice
|
1046
1098
|
}
|
1047
1099
|
},
|
1100
|
+
streamMode,
|
1048
1101
|
credentials: extraMetadata.credentials,
|
1049
1102
|
headers: {
|
1050
1103
|
...extraMetadata.headers,
|
@@ -1073,6 +1126,7 @@ function useChat({
|
|
1073
1126
|
sendExtraMessageFields,
|
1074
1127
|
experimental_onFunctionCall,
|
1075
1128
|
experimental_onToolCall,
|
1129
|
+
streamMode,
|
1076
1130
|
onResponse,
|
1077
1131
|
onFinish,
|
1078
1132
|
onError,
|
@@ -1124,6 +1178,7 @@ function useChat({
|
|
1124
1178
|
get(messages),
|
1125
1179
|
abortController,
|
1126
1180
|
generateId2,
|
1181
|
+
streamMode,
|
1127
1182
|
onFinish,
|
1128
1183
|
onResponse,
|
1129
1184
|
sendExtraMessageFields
|
@@ -1257,6 +1312,7 @@ async function callCompletionApi({
|
|
1257
1312
|
credentials,
|
1258
1313
|
headers,
|
1259
1314
|
body,
|
1315
|
+
streamMode = "stream-data",
|
1260
1316
|
setCompletion,
|
1261
1317
|
setLoading,
|
1262
1318
|
setError,
|
@@ -1304,19 +1360,44 @@ async function callCompletionApi({
|
|
1304
1360
|
}
|
1305
1361
|
let result = "";
|
1306
1362
|
const reader = res.body.getReader();
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1363
|
+
switch (streamMode) {
|
1364
|
+
case "text": {
|
1365
|
+
const decoder = createChunkDecoder();
|
1366
|
+
while (true) {
|
1367
|
+
const { done, value } = await reader.read();
|
1368
|
+
if (done) {
|
1369
|
+
break;
|
1370
|
+
}
|
1371
|
+
result += decoder(value);
|
1313
1372
|
setCompletion(result);
|
1314
|
-
|
1373
|
+
if (abortController === null) {
|
1374
|
+
reader.cancel();
|
1375
|
+
break;
|
1376
|
+
}
|
1315
1377
|
}
|
1316
|
-
|
1317
|
-
|
1318
|
-
|
1378
|
+
break;
|
1379
|
+
}
|
1380
|
+
case "stream-data": {
|
1381
|
+
for await (const { type, value } of readDataStream(reader, {
|
1382
|
+
isAborted: () => abortController === null
|
1383
|
+
})) {
|
1384
|
+
switch (type) {
|
1385
|
+
case "text": {
|
1386
|
+
result += value;
|
1387
|
+
setCompletion(result);
|
1388
|
+
break;
|
1389
|
+
}
|
1390
|
+
case "data": {
|
1391
|
+
onData == null ? void 0 : onData(value);
|
1392
|
+
break;
|
1393
|
+
}
|
1394
|
+
}
|
1319
1395
|
}
|
1396
|
+
break;
|
1397
|
+
}
|
1398
|
+
default: {
|
1399
|
+
const exhaustiveCheck = streamMode;
|
1400
|
+
throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
|
1320
1401
|
}
|
1321
1402
|
}
|
1322
1403
|
if (onFinish) {
|
@@ -1351,6 +1432,7 @@ function useCompletion({
|
|
1351
1432
|
credentials,
|
1352
1433
|
headers,
|
1353
1434
|
body,
|
1435
|
+
streamMode,
|
1354
1436
|
onResponse,
|
1355
1437
|
onFinish,
|
1356
1438
|
onError
|
@@ -1389,6 +1471,7 @@ function useCompletion({
|
|
1389
1471
|
...body,
|
1390
1472
|
...options == null ? void 0 : options.body
|
1391
1473
|
},
|
1474
|
+
streamMode,
|
1392
1475
|
setCompletion: mutate,
|
1393
1476
|
setLoading: (loadingState) => loading.set(loadingState),
|
1394
1477
|
setError: (err) => error.set(err),
|