ai 3.0.22 → 3.0.24

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.
@@ -858,11 +858,28 @@ async function parseComplexResponse({
858
858
  };
859
859
  }
860
860
 
861
+ // shared/utils.ts
862
+ function createChunkDecoder(complex) {
863
+ const decoder = new TextDecoder();
864
+ if (!complex) {
865
+ return function(chunk) {
866
+ if (!chunk)
867
+ return "";
868
+ return decoder.decode(chunk, { stream: true });
869
+ };
870
+ }
871
+ return function(chunk) {
872
+ const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
873
+ return decoded.map(parseStreamPart).filter(Boolean);
874
+ };
875
+ }
876
+
861
877
  // shared/call-chat-api.ts
862
878
  async function callChatApi({
863
879
  api,
864
880
  messages,
865
881
  body,
882
+ streamMode = "stream-data",
866
883
  credentials,
867
884
  headers,
868
885
  abortController,
@@ -906,17 +923,52 @@ async function callChatApi({
906
923
  throw new Error("The response body is empty.");
907
924
  }
908
925
  const reader = response.body.getReader();
909
- return await parseComplexResponse({
910
- reader,
911
- abortControllerRef: abortController != null ? { current: abortController() } : void 0,
912
- update: onUpdate,
913
- onFinish(prefixMap) {
914
- if (onFinish && prefixMap.text != null) {
915
- onFinish(prefixMap.text);
926
+ switch (streamMode) {
927
+ case "text": {
928
+ const decoder = createChunkDecoder();
929
+ const resultMessage = {
930
+ id: generateId2(),
931
+ createdAt: /* @__PURE__ */ new Date(),
932
+ role: "assistant",
933
+ content: ""
934
+ };
935
+ while (true) {
936
+ const { done, value } = await reader.read();
937
+ if (done) {
938
+ break;
939
+ }
940
+ resultMessage.content += decoder(value);
941
+ resultMessage.id = generateId2();
942
+ onUpdate([{ ...resultMessage }], []);
943
+ if ((abortController == null ? void 0 : abortController()) === null) {
944
+ reader.cancel();
945
+ break;
946
+ }
916
947
  }
917
- },
918
- generateId: generateId2
919
- });
948
+ onFinish == null ? void 0 : onFinish(resultMessage);
949
+ return {
950
+ messages: [resultMessage],
951
+ data: []
952
+ };
953
+ }
954
+ case "stream-data": {
955
+ return await parseComplexResponse({
956
+ reader,
957
+ abortControllerRef: abortController != null ? { current: abortController() } : void 0,
958
+ update: onUpdate,
959
+ onFinish(prefixMap) {
960
+ if (onFinish && prefixMap.text != null) {
961
+ onFinish(prefixMap.text);
962
+ }
963
+ },
964
+ generateId: generateId2
965
+ });
966
+ }
967
+ default: {
968
+ const exhaustiveCheck = streamMode;
969
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
970
+ }
971
+ }
920
972
  }
921
973
 
922
974
  // shared/process-chat-stream.ts
@@ -1036,7 +1088,7 @@ async function processChatStream({
1036
1088
  }
1037
1089
 
1038
1090
  // svelte/use-chat.ts
1039
- var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId2, onFinish, onResponse, sendExtraMessageFields) => {
1091
+ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId2, streamMode, onFinish, onResponse, sendExtraMessageFields) => {
1040
1092
  var _a, _b;
1041
1093
  mutate(chatRequest.messages);
1042
1094
  const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
@@ -1072,6 +1124,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
1072
1124
  tool_choice: chatRequest.tool_choice
1073
1125
  }
1074
1126
  },
1127
+ streamMode,
1075
1128
  credentials: extraMetadata.credentials,
1076
1129
  headers: {
1077
1130
  ...extraMetadata.headers,
@@ -1100,6 +1153,7 @@ function useChat({
1100
1153
  sendExtraMessageFields,
1101
1154
  experimental_onFunctionCall,
1102
1155
  experimental_onToolCall,
1156
+ streamMode,
1103
1157
  onResponse,
1104
1158
  onFinish,
1105
1159
  onError,
@@ -1151,6 +1205,7 @@ function useChat({
1151
1205
  (0, import_store.get)(messages),
1152
1206
  abortController,
1153
1207
  generateId2,
1208
+ streamMode,
1154
1209
  onFinish,
1155
1210
  onResponse,
1156
1211
  sendExtraMessageFields
@@ -1284,6 +1339,7 @@ async function callCompletionApi({
1284
1339
  credentials,
1285
1340
  headers,
1286
1341
  body,
1342
+ streamMode = "stream-data",
1287
1343
  setCompletion,
1288
1344
  setLoading,
1289
1345
  setError,
@@ -1331,19 +1387,44 @@ async function callCompletionApi({
1331
1387
  }
1332
1388
  let result = "";
1333
1389
  const reader = res.body.getReader();
1334
- for await (const { type, value } of readDataStream(reader, {
1335
- isAborted: () => abortController === null
1336
- })) {
1337
- switch (type) {
1338
- case "text": {
1339
- result += value;
1390
+ switch (streamMode) {
1391
+ case "text": {
1392
+ const decoder = createChunkDecoder();
1393
+ while (true) {
1394
+ const { done, value } = await reader.read();
1395
+ if (done) {
1396
+ break;
1397
+ }
1398
+ result += decoder(value);
1340
1399
  setCompletion(result);
1341
- break;
1400
+ if (abortController === null) {
1401
+ reader.cancel();
1402
+ break;
1403
+ }
1342
1404
  }
1343
- case "data": {
1344
- onData == null ? void 0 : onData(value);
1345
- break;
1405
+ break;
1406
+ }
1407
+ case "stream-data": {
1408
+ for await (const { type, value } of readDataStream(reader, {
1409
+ isAborted: () => abortController === null
1410
+ })) {
1411
+ switch (type) {
1412
+ case "text": {
1413
+ result += value;
1414
+ setCompletion(result);
1415
+ break;
1416
+ }
1417
+ case "data": {
1418
+ onData == null ? void 0 : onData(value);
1419
+ break;
1420
+ }
1421
+ }
1346
1422
  }
1423
+ break;
1424
+ }
1425
+ default: {
1426
+ const exhaustiveCheck = streamMode;
1427
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
1347
1428
  }
1348
1429
  }
1349
1430
  if (onFinish) {
@@ -1378,6 +1459,7 @@ function useCompletion({
1378
1459
  credentials,
1379
1460
  headers,
1380
1461
  body,
1462
+ streamMode,
1381
1463
  onResponse,
1382
1464
  onFinish,
1383
1465
  onError
@@ -1416,6 +1498,7 @@ function useCompletion({
1416
1498
  ...body,
1417
1499
  ...options == null ? void 0 : options.body
1418
1500
  },
1501
+ streamMode,
1419
1502
  setCompletion: mutate,
1420
1503
  setLoading: (loadingState) => loading.set(loadingState),
1421
1504
  setError: (err) => error.set(err),