ai 3.1.0-canary.4 → 3.1.1

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.
Files changed (69) hide show
  1. package/dist/index.d.mts +982 -24
  2. package/dist/index.d.ts +982 -24
  3. package/dist/index.js +1748 -175
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +1723 -174
  6. package/dist/index.mjs.map +1 -1
  7. package/package.json +11 -28
  8. package/prompts/dist/index.d.mts +13 -1
  9. package/prompts/dist/index.d.ts +13 -1
  10. package/prompts/dist/index.js +13 -0
  11. package/prompts/dist/index.js.map +1 -1
  12. package/prompts/dist/index.mjs +12 -0
  13. package/prompts/dist/index.mjs.map +1 -1
  14. package/react/dist/index.d.mts +23 -6
  15. package/react/dist/index.d.ts +27 -8
  16. package/react/dist/index.js +154 -141
  17. package/react/dist/index.js.map +1 -1
  18. package/react/dist/index.mjs +153 -141
  19. package/react/dist/index.mjs.map +1 -1
  20. package/react/dist/index.server.d.mts +4 -2
  21. package/react/dist/index.server.d.ts +4 -2
  22. package/react/dist/index.server.js.map +1 -1
  23. package/react/dist/index.server.mjs.map +1 -1
  24. package/rsc/dist/index.d.ts +388 -21
  25. package/rsc/dist/rsc-client.d.mts +1 -1
  26. package/rsc/dist/rsc-client.mjs +2 -0
  27. package/rsc/dist/rsc-client.mjs.map +1 -1
  28. package/rsc/dist/rsc-server.d.mts +370 -21
  29. package/rsc/dist/rsc-server.mjs +677 -36
  30. package/rsc/dist/rsc-server.mjs.map +1 -1
  31. package/rsc/dist/rsc-shared.d.mts +24 -9
  32. package/rsc/dist/rsc-shared.mjs +98 -4
  33. package/rsc/dist/rsc-shared.mjs.map +1 -1
  34. package/solid/dist/index.d.mts +7 -3
  35. package/solid/dist/index.d.ts +7 -3
  36. package/solid/dist/index.js +106 -107
  37. package/solid/dist/index.js.map +1 -1
  38. package/solid/dist/index.mjs +106 -107
  39. package/solid/dist/index.mjs.map +1 -1
  40. package/svelte/dist/index.d.mts +7 -3
  41. package/svelte/dist/index.d.ts +7 -3
  42. package/svelte/dist/index.js +109 -109
  43. package/svelte/dist/index.js.map +1 -1
  44. package/svelte/dist/index.mjs +109 -109
  45. package/svelte/dist/index.mjs.map +1 -1
  46. package/vue/dist/index.d.mts +7 -3
  47. package/vue/dist/index.d.ts +7 -3
  48. package/vue/dist/index.js +106 -107
  49. package/vue/dist/index.js.map +1 -1
  50. package/vue/dist/index.mjs +106 -107
  51. package/vue/dist/index.mjs.map +1 -1
  52. package/ai-model-specification/dist/index.d.mts +0 -665
  53. package/ai-model-specification/dist/index.d.ts +0 -665
  54. package/ai-model-specification/dist/index.js +0 -716
  55. package/ai-model-specification/dist/index.js.map +0 -1
  56. package/ai-model-specification/dist/index.mjs +0 -656
  57. package/ai-model-specification/dist/index.mjs.map +0 -1
  58. package/core/dist/index.d.mts +0 -626
  59. package/core/dist/index.d.ts +0 -626
  60. package/core/dist/index.js +0 -1918
  61. package/core/dist/index.js.map +0 -1
  62. package/core/dist/index.mjs +0 -1873
  63. package/core/dist/index.mjs.map +0 -1
  64. package/openai/dist/index.d.mts +0 -429
  65. package/openai/dist/index.d.ts +0 -429
  66. package/openai/dist/index.js +0 -1231
  67. package/openai/dist/index.js.map +0 -1
  68. package/openai/dist/index.mjs +0 -1195
  69. package/openai/dist/index.mjs.map +0 -1
@@ -625,9 +625,9 @@ var toolCallStreamPart = {
625
625
  code: "7",
626
626
  name: "tool_calls",
627
627
  parse: (value) => {
628
- 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((tc) => {
629
- 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";
630
- })) {
628
+ 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(
629
+ (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"
630
+ )) {
631
631
  throw new Error(
632
632
  '"tool_calls" parts expect an object with a ToolCallPayload.'
633
633
  );
@@ -740,27 +740,12 @@ async function* readDataStream(reader, {
740
740
  }
741
741
  }
742
742
 
743
- // shared/utils.ts
743
+ // shared/generate-id.ts
744
744
  var import_non_secure = require("nanoid/non-secure");
745
- var nanoid = (0, import_non_secure.customAlphabet)(
745
+ var generateId = (0, import_non_secure.customAlphabet)(
746
746
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
747
747
  7
748
748
  );
749
- function createChunkDecoder(complex) {
750
- const decoder = new TextDecoder();
751
- if (!complex) {
752
- return function(chunk) {
753
- if (!chunk)
754
- return "";
755
- return decoder.decode(chunk, { stream: true });
756
- };
757
- }
758
- return function(chunk) {
759
- const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
760
- return decoded.map(parseStreamPart).filter(Boolean);
761
- };
762
- }
763
- var COMPLEX_HEADER = "X-Experimental-Stream-Data";
764
749
 
765
750
  // shared/parse-complex-response.ts
766
751
  function assignAnnotationsToMessage(message, annotations) {
@@ -773,7 +758,7 @@ async function parseComplexResponse({
773
758
  abortControllerRef,
774
759
  update,
775
760
  onFinish,
776
- generateId = nanoid,
761
+ generateId: generateId2 = generateId,
777
762
  getCurrentDate = () => /* @__PURE__ */ new Date()
778
763
  }) {
779
764
  const createdAt = getCurrentDate();
@@ -792,7 +777,7 @@ async function parseComplexResponse({
792
777
  };
793
778
  } else {
794
779
  prefixMap["text"] = {
795
- id: generateId(),
780
+ id: generateId2(),
796
781
  role: "assistant",
797
782
  content: value,
798
783
  createdAt
@@ -802,7 +787,7 @@ async function parseComplexResponse({
802
787
  let functionCallMessage = null;
803
788
  if (type === "function_call") {
804
789
  prefixMap["function_call"] = {
805
- id: generateId(),
790
+ id: generateId2(),
806
791
  role: "assistant",
807
792
  content: "",
808
793
  function_call: value.function_call,
@@ -814,7 +799,7 @@ async function parseComplexResponse({
814
799
  let toolCallMessage = null;
815
800
  if (type === "tool_calls") {
816
801
  prefixMap["tool_calls"] = {
817
- id: generateId(),
802
+ id: generateId2(),
818
803
  role: "assistant",
819
804
  content: "",
820
805
  tool_calls: value.tool_calls,
@@ -873,20 +858,36 @@ async function parseComplexResponse({
873
858
  };
874
859
  }
875
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
+
876
877
  // shared/call-chat-api.ts
877
878
  async function callChatApi({
878
879
  api,
879
880
  messages,
880
881
  body,
882
+ streamMode = "stream-data",
881
883
  credentials,
882
884
  headers,
883
885
  abortController,
884
- appendMessage,
885
886
  restoreMessagesOnFailure,
886
887
  onResponse,
887
888
  onUpdate,
888
889
  onFinish,
889
- generateId
890
+ generateId: generateId2
890
891
  }) {
891
892
  var _a;
892
893
  const response = await fetch(api, {
@@ -922,63 +923,51 @@ async function callChatApi({
922
923
  throw new Error("The response body is empty.");
923
924
  }
924
925
  const reader = response.body.getReader();
925
- const isComplexMode = response.headers.get(COMPLEX_HEADER) === "true";
926
- if (isComplexMode) {
927
- return await parseComplexResponse({
928
- reader,
929
- abortControllerRef: abortController != null ? { current: abortController() } : void 0,
930
- update: onUpdate,
931
- onFinish(prefixMap) {
932
- if (onFinish && prefixMap.text != null) {
933
- 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;
934
946
  }
935
- },
936
- generateId
937
- });
938
- } else {
939
- const createdAt = /* @__PURE__ */ new Date();
940
- const decode = createChunkDecoder(false);
941
- let streamedResponse = "";
942
- const replyId = generateId();
943
- let responseMessage = {
944
- id: replyId,
945
- createdAt,
946
- content: "",
947
- role: "assistant"
948
- };
949
- while (true) {
950
- const { done, value } = await reader.read();
951
- if (done) {
952
- break;
953
- }
954
- streamedResponse += decode(value);
955
- if (streamedResponse.startsWith('{"function_call":')) {
956
- responseMessage["function_call"] = streamedResponse;
957
- } else if (streamedResponse.startsWith('{"tool_calls":')) {
958
- responseMessage["tool_calls"] = streamedResponse;
959
- } else {
960
- responseMessage["content"] = streamedResponse;
961
- }
962
- appendMessage({ ...responseMessage });
963
- if ((abortController == null ? void 0 : abortController()) === null) {
964
- reader.cancel();
965
- break;
966
947
  }
948
+ onFinish == null ? void 0 : onFinish(resultMessage);
949
+ return {
950
+ messages: [resultMessage],
951
+ data: []
952
+ };
967
953
  }
968
- if (streamedResponse.startsWith('{"function_call":')) {
969
- const parsedFunctionCall = JSON.parse(streamedResponse).function_call;
970
- responseMessage["function_call"] = parsedFunctionCall;
971
- appendMessage({ ...responseMessage });
972
- }
973
- if (streamedResponse.startsWith('{"tool_calls":')) {
974
- const parsedToolCalls = JSON.parse(streamedResponse).tool_calls;
975
- responseMessage["tool_calls"] = parsedToolCalls;
976
- appendMessage({ ...responseMessage });
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
+ });
977
966
  }
978
- if (onFinish) {
979
- onFinish(responseMessage);
967
+ default: {
968
+ const exhaustiveCheck = streamMode;
969
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
980
970
  }
981
- return responseMessage;
982
971
  }
983
972
  }
984
973
 
@@ -1099,7 +1088,7 @@ async function processChatStream({
1099
1088
  }
1100
1089
 
1101
1090
  // svelte/use-chat.ts
1102
- var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId, onFinish, onResponse, sendExtraMessageFields) => {
1091
+ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId2, streamMode, onFinish, onResponse, sendExtraMessageFields) => {
1103
1092
  var _a, _b;
1104
1093
  mutate(chatRequest.messages);
1105
1094
  const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
@@ -1135,15 +1124,13 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
1135
1124
  tool_choice: chatRequest.tool_choice
1136
1125
  }
1137
1126
  },
1127
+ streamMode,
1138
1128
  credentials: extraMetadata.credentials,
1139
1129
  headers: {
1140
1130
  ...extraMetadata.headers,
1141
1131
  ...(_b = chatRequest.options) == null ? void 0 : _b.headers
1142
1132
  },
1143
1133
  abortController: () => abortControllerRef,
1144
- appendMessage(message) {
1145
- mutate([...chatRequest.messages, message]);
1146
- },
1147
1134
  restoreMessagesOnFailure() {
1148
1135
  mutate(previousMessages);
1149
1136
  },
@@ -1153,7 +1140,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
1153
1140
  mutateStreamData([...existingData || [], ...data || []]);
1154
1141
  },
1155
1142
  onFinish,
1156
- generateId
1143
+ generateId: generateId2
1157
1144
  });
1158
1145
  };
1159
1146
  var uniqueId = 0;
@@ -1166,13 +1153,14 @@ function useChat({
1166
1153
  sendExtraMessageFields,
1167
1154
  experimental_onFunctionCall,
1168
1155
  experimental_onToolCall,
1156
+ streamMode,
1169
1157
  onResponse,
1170
1158
  onFinish,
1171
1159
  onError,
1172
1160
  credentials,
1173
1161
  headers,
1174
1162
  body,
1175
- generateId = nanoid
1163
+ generateId: generateId2 = generateId
1176
1164
  } = {}) {
1177
1165
  const chatId = id || `chat-${uniqueId++}`;
1178
1166
  const key = `${api}|${chatId}`;
@@ -1216,7 +1204,8 @@ function useChat({
1216
1204
  extraMetadata,
1217
1205
  (0, import_store.get)(messages),
1218
1206
  abortController,
1219
- generateId,
1207
+ generateId2,
1208
+ streamMode,
1220
1209
  onFinish,
1221
1210
  onResponse,
1222
1211
  sendExtraMessageFields
@@ -1251,7 +1240,7 @@ function useChat({
1251
1240
  tool_choice
1252
1241
  } = {}) => {
1253
1242
  if (!message.id) {
1254
- message.id = generateId();
1243
+ message.id = generateId2();
1255
1244
  }
1256
1245
  const chatRequest = {
1257
1246
  messages: (0, import_store.get)(messages).concat(message),
@@ -1350,6 +1339,7 @@ async function callCompletionApi({
1350
1339
  credentials,
1351
1340
  headers,
1352
1341
  body,
1342
+ streamMode = "stream-data",
1353
1343
  setCompletion,
1354
1344
  setLoading,
1355
1345
  setError,
@@ -1397,36 +1387,44 @@ async function callCompletionApi({
1397
1387
  }
1398
1388
  let result = "";
1399
1389
  const reader = res.body.getReader();
1400
- const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
1401
- if (isComplexMode) {
1402
- for await (const { type, value } of readDataStream(reader, {
1403
- isAborted: () => abortController === null
1404
- })) {
1405
- switch (type) {
1406
- case "text": {
1407
- result += value;
1408
- setCompletion(result);
1390
+ switch (streamMode) {
1391
+ case "text": {
1392
+ const decoder = createChunkDecoder();
1393
+ while (true) {
1394
+ const { done, value } = await reader.read();
1395
+ if (done) {
1409
1396
  break;
1410
1397
  }
1411
- case "data": {
1412
- onData == null ? void 0 : onData(value);
1398
+ result += decoder(value);
1399
+ setCompletion(result);
1400
+ if (abortController === null) {
1401
+ reader.cancel();
1413
1402
  break;
1414
1403
  }
1415
1404
  }
1405
+ break;
1416
1406
  }
1417
- } else {
1418
- const decoder = createChunkDecoder();
1419
- while (true) {
1420
- const { done, value } = await reader.read();
1421
- if (done) {
1422
- break;
1423
- }
1424
- result += decoder(value);
1425
- setCompletion(result);
1426
- if (abortController === null) {
1427
- reader.cancel();
1428
- break;
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
+ }
1429
1422
  }
1423
+ break;
1424
+ }
1425
+ default: {
1426
+ const exhaustiveCheck = streamMode;
1427
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
1430
1428
  }
1431
1429
  }
1432
1430
  if (onFinish) {
@@ -1461,6 +1459,7 @@ function useCompletion({
1461
1459
  credentials,
1462
1460
  headers,
1463
1461
  body,
1462
+ streamMode,
1464
1463
  onResponse,
1465
1464
  onFinish,
1466
1465
  onError
@@ -1499,6 +1498,7 @@ function useCompletion({
1499
1498
  ...body,
1500
1499
  ...options == null ? void 0 : options.body
1501
1500
  },
1501
+ streamMode,
1502
1502
  setCompletion: mutate,
1503
1503
  setLoading: (loadingState) => loading.set(loadingState),
1504
1504
  setError: (err) => error.set(err),