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
@@ -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((tc) => {
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
  );
@@ -713,27 +713,12 @@ async function* readDataStream(reader, {
713
713
  }
714
714
  }
715
715
 
716
- // shared/utils.ts
716
+ // shared/generate-id.ts
717
717
  import { customAlphabet } from "nanoid/non-secure";
718
- var nanoid = customAlphabet(
718
+ var generateId = customAlphabet(
719
719
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
720
720
  7
721
721
  );
722
- function createChunkDecoder(complex) {
723
- const decoder = new TextDecoder();
724
- if (!complex) {
725
- return function(chunk) {
726
- if (!chunk)
727
- return "";
728
- return decoder.decode(chunk, { stream: true });
729
- };
730
- }
731
- return function(chunk) {
732
- const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
733
- return decoded.map(parseStreamPart).filter(Boolean);
734
- };
735
- }
736
- var COMPLEX_HEADER = "X-Experimental-Stream-Data";
737
722
 
738
723
  // shared/parse-complex-response.ts
739
724
  function assignAnnotationsToMessage(message, annotations) {
@@ -746,7 +731,7 @@ async function parseComplexResponse({
746
731
  abortControllerRef,
747
732
  update,
748
733
  onFinish,
749
- generateId = nanoid,
734
+ generateId: generateId2 = generateId,
750
735
  getCurrentDate = () => /* @__PURE__ */ new Date()
751
736
  }) {
752
737
  const createdAt = getCurrentDate();
@@ -765,7 +750,7 @@ async function parseComplexResponse({
765
750
  };
766
751
  } else {
767
752
  prefixMap["text"] = {
768
- id: generateId(),
753
+ id: generateId2(),
769
754
  role: "assistant",
770
755
  content: value,
771
756
  createdAt
@@ -775,7 +760,7 @@ async function parseComplexResponse({
775
760
  let functionCallMessage = null;
776
761
  if (type === "function_call") {
777
762
  prefixMap["function_call"] = {
778
- id: generateId(),
763
+ id: generateId2(),
779
764
  role: "assistant",
780
765
  content: "",
781
766
  function_call: value.function_call,
@@ -787,7 +772,7 @@ async function parseComplexResponse({
787
772
  let toolCallMessage = null;
788
773
  if (type === "tool_calls") {
789
774
  prefixMap["tool_calls"] = {
790
- id: generateId(),
775
+ id: generateId2(),
791
776
  role: "assistant",
792
777
  content: "",
793
778
  tool_calls: value.tool_calls,
@@ -846,20 +831,36 @@ async function parseComplexResponse({
846
831
  };
847
832
  }
848
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
+
849
850
  // shared/call-chat-api.ts
850
851
  async function callChatApi({
851
852
  api,
852
853
  messages,
853
854
  body,
855
+ streamMode = "stream-data",
854
856
  credentials,
855
857
  headers,
856
858
  abortController,
857
- appendMessage,
858
859
  restoreMessagesOnFailure,
859
860
  onResponse,
860
861
  onUpdate,
861
862
  onFinish,
862
- generateId
863
+ generateId: generateId2
863
864
  }) {
864
865
  var _a;
865
866
  const response = await fetch(api, {
@@ -895,63 +896,51 @@ async function callChatApi({
895
896
  throw new Error("The response body is empty.");
896
897
  }
897
898
  const reader = response.body.getReader();
898
- const isComplexMode = response.headers.get(COMPLEX_HEADER) === "true";
899
- if (isComplexMode) {
900
- return await parseComplexResponse({
901
- reader,
902
- abortControllerRef: abortController != null ? { current: abortController() } : void 0,
903
- update: onUpdate,
904
- onFinish(prefixMap) {
905
- if (onFinish && prefixMap.text != null) {
906
- onFinish(prefixMap.text);
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;
907
919
  }
908
- },
909
- generateId
910
- });
911
- } else {
912
- const createdAt = /* @__PURE__ */ new Date();
913
- const decode = createChunkDecoder(false);
914
- let streamedResponse = "";
915
- const replyId = generateId();
916
- let responseMessage = {
917
- id: replyId,
918
- createdAt,
919
- content: "",
920
- role: "assistant"
921
- };
922
- while (true) {
923
- const { done, value } = await reader.read();
924
- if (done) {
925
- break;
926
- }
927
- streamedResponse += decode(value);
928
- if (streamedResponse.startsWith('{"function_call":')) {
929
- responseMessage["function_call"] = streamedResponse;
930
- } else if (streamedResponse.startsWith('{"tool_calls":')) {
931
- responseMessage["tool_calls"] = streamedResponse;
932
- } else {
933
- responseMessage["content"] = streamedResponse;
934
- }
935
- appendMessage({ ...responseMessage });
936
- if ((abortController == null ? void 0 : abortController()) === null) {
937
- reader.cancel();
938
- break;
939
920
  }
921
+ onFinish == null ? void 0 : onFinish(resultMessage);
922
+ return {
923
+ messages: [resultMessage],
924
+ data: []
925
+ };
940
926
  }
941
- if (streamedResponse.startsWith('{"function_call":')) {
942
- const parsedFunctionCall = JSON.parse(streamedResponse).function_call;
943
- responseMessage["function_call"] = parsedFunctionCall;
944
- appendMessage({ ...responseMessage });
945
- }
946
- if (streamedResponse.startsWith('{"tool_calls":')) {
947
- const parsedToolCalls = JSON.parse(streamedResponse).tool_calls;
948
- responseMessage["tool_calls"] = parsedToolCalls;
949
- appendMessage({ ...responseMessage });
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
+ });
950
939
  }
951
- if (onFinish) {
952
- onFinish(responseMessage);
940
+ default: {
941
+ const exhaustiveCheck = streamMode;
942
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
953
943
  }
954
- return responseMessage;
955
944
  }
956
945
  }
957
946
 
@@ -1072,7 +1061,7 @@ async function processChatStream({
1072
1061
  }
1073
1062
 
1074
1063
  // svelte/use-chat.ts
1075
- var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId, onFinish, onResponse, sendExtraMessageFields) => {
1064
+ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadata, previousMessages, abortControllerRef, generateId2, streamMode, onFinish, onResponse, sendExtraMessageFields) => {
1076
1065
  var _a, _b;
1077
1066
  mutate(chatRequest.messages);
1078
1067
  const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
@@ -1108,15 +1097,13 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
1108
1097
  tool_choice: chatRequest.tool_choice
1109
1098
  }
1110
1099
  },
1100
+ streamMode,
1111
1101
  credentials: extraMetadata.credentials,
1112
1102
  headers: {
1113
1103
  ...extraMetadata.headers,
1114
1104
  ...(_b = chatRequest.options) == null ? void 0 : _b.headers
1115
1105
  },
1116
1106
  abortController: () => abortControllerRef,
1117
- appendMessage(message) {
1118
- mutate([...chatRequest.messages, message]);
1119
- },
1120
1107
  restoreMessagesOnFailure() {
1121
1108
  mutate(previousMessages);
1122
1109
  },
@@ -1126,7 +1113,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
1126
1113
  mutateStreamData([...existingData || [], ...data || []]);
1127
1114
  },
1128
1115
  onFinish,
1129
- generateId
1116
+ generateId: generateId2
1130
1117
  });
1131
1118
  };
1132
1119
  var uniqueId = 0;
@@ -1139,13 +1126,14 @@ function useChat({
1139
1126
  sendExtraMessageFields,
1140
1127
  experimental_onFunctionCall,
1141
1128
  experimental_onToolCall,
1129
+ streamMode,
1142
1130
  onResponse,
1143
1131
  onFinish,
1144
1132
  onError,
1145
1133
  credentials,
1146
1134
  headers,
1147
1135
  body,
1148
- generateId = nanoid
1136
+ generateId: generateId2 = generateId
1149
1137
  } = {}) {
1150
1138
  const chatId = id || `chat-${uniqueId++}`;
1151
1139
  const key = `${api}|${chatId}`;
@@ -1189,7 +1177,8 @@ function useChat({
1189
1177
  extraMetadata,
1190
1178
  get(messages),
1191
1179
  abortController,
1192
- generateId,
1180
+ generateId2,
1181
+ streamMode,
1193
1182
  onFinish,
1194
1183
  onResponse,
1195
1184
  sendExtraMessageFields
@@ -1224,7 +1213,7 @@ function useChat({
1224
1213
  tool_choice
1225
1214
  } = {}) => {
1226
1215
  if (!message.id) {
1227
- message.id = generateId();
1216
+ message.id = generateId2();
1228
1217
  }
1229
1218
  const chatRequest = {
1230
1219
  messages: get(messages).concat(message),
@@ -1323,6 +1312,7 @@ async function callCompletionApi({
1323
1312
  credentials,
1324
1313
  headers,
1325
1314
  body,
1315
+ streamMode = "stream-data",
1326
1316
  setCompletion,
1327
1317
  setLoading,
1328
1318
  setError,
@@ -1370,36 +1360,44 @@ async function callCompletionApi({
1370
1360
  }
1371
1361
  let result = "";
1372
1362
  const reader = res.body.getReader();
1373
- const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
1374
- if (isComplexMode) {
1375
- for await (const { type, value } of readDataStream(reader, {
1376
- isAborted: () => abortController === null
1377
- })) {
1378
- switch (type) {
1379
- case "text": {
1380
- result += value;
1381
- setCompletion(result);
1363
+ switch (streamMode) {
1364
+ case "text": {
1365
+ const decoder = createChunkDecoder();
1366
+ while (true) {
1367
+ const { done, value } = await reader.read();
1368
+ if (done) {
1382
1369
  break;
1383
1370
  }
1384
- case "data": {
1385
- onData == null ? void 0 : onData(value);
1371
+ result += decoder(value);
1372
+ setCompletion(result);
1373
+ if (abortController === null) {
1374
+ reader.cancel();
1386
1375
  break;
1387
1376
  }
1388
1377
  }
1378
+ break;
1389
1379
  }
1390
- } else {
1391
- const decoder = createChunkDecoder();
1392
- while (true) {
1393
- const { done, value } = await reader.read();
1394
- if (done) {
1395
- break;
1396
- }
1397
- result += decoder(value);
1398
- setCompletion(result);
1399
- if (abortController === null) {
1400
- reader.cancel();
1401
- break;
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
+ }
1402
1395
  }
1396
+ break;
1397
+ }
1398
+ default: {
1399
+ const exhaustiveCheck = streamMode;
1400
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
1403
1401
  }
1404
1402
  }
1405
1403
  if (onFinish) {
@@ -1434,6 +1432,7 @@ function useCompletion({
1434
1432
  credentials,
1435
1433
  headers,
1436
1434
  body,
1435
+ streamMode,
1437
1436
  onResponse,
1438
1437
  onFinish,
1439
1438
  onError
@@ -1472,6 +1471,7 @@ function useCompletion({
1472
1471
  ...body,
1473
1472
  ...options == null ? void 0 : options.body
1474
1473
  },
1474
+ streamMode,
1475
1475
  setCompletion: mutate,
1476
1476
  setLoading: (loadingState) => loading.set(loadingState),
1477
1477
  setError: (err) => error.set(err),