ai 3.1.0-canary.3 → 3.1.0

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 (70) hide show
  1. package/README.md +1 -1
  2. package/dist/index.d.mts +982 -24
  3. package/dist/index.d.ts +982 -24
  4. package/dist/index.js +1748 -175
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +1723 -174
  7. package/dist/index.mjs.map +1 -1
  8. package/package.json +14 -31
  9. package/prompts/dist/index.d.mts +13 -1
  10. package/prompts/dist/index.d.ts +13 -1
  11. package/prompts/dist/index.js +13 -0
  12. package/prompts/dist/index.js.map +1 -1
  13. package/prompts/dist/index.mjs +12 -0
  14. package/prompts/dist/index.mjs.map +1 -1
  15. package/react/dist/index.d.mts +27 -6
  16. package/react/dist/index.d.ts +31 -8
  17. package/react/dist/index.js +155 -141
  18. package/react/dist/index.js.map +1 -1
  19. package/react/dist/index.mjs +154 -141
  20. package/react/dist/index.mjs.map +1 -1
  21. package/react/dist/index.server.d.mts +4 -2
  22. package/react/dist/index.server.d.ts +4 -2
  23. package/react/dist/index.server.js.map +1 -1
  24. package/react/dist/index.server.mjs.map +1 -1
  25. package/rsc/dist/index.d.ts +385 -20
  26. package/rsc/dist/rsc-client.d.mts +1 -1
  27. package/rsc/dist/rsc-client.mjs +2 -0
  28. package/rsc/dist/rsc-client.mjs.map +1 -1
  29. package/rsc/dist/rsc-server.d.mts +367 -20
  30. package/rsc/dist/rsc-server.mjs +676 -35
  31. package/rsc/dist/rsc-server.mjs.map +1 -1
  32. package/rsc/dist/rsc-shared.d.mts +24 -9
  33. package/rsc/dist/rsc-shared.mjs +98 -4
  34. package/rsc/dist/rsc-shared.mjs.map +1 -1
  35. package/solid/dist/index.d.mts +7 -3
  36. package/solid/dist/index.d.ts +7 -3
  37. package/solid/dist/index.js +106 -107
  38. package/solid/dist/index.js.map +1 -1
  39. package/solid/dist/index.mjs +106 -107
  40. package/solid/dist/index.mjs.map +1 -1
  41. package/svelte/dist/index.d.mts +7 -3
  42. package/svelte/dist/index.d.ts +7 -3
  43. package/svelte/dist/index.js +109 -109
  44. package/svelte/dist/index.js.map +1 -1
  45. package/svelte/dist/index.mjs +109 -109
  46. package/svelte/dist/index.mjs.map +1 -1
  47. package/vue/dist/index.d.mts +7 -3
  48. package/vue/dist/index.d.ts +7 -3
  49. package/vue/dist/index.js +106 -107
  50. package/vue/dist/index.js.map +1 -1
  51. package/vue/dist/index.mjs +106 -107
  52. package/vue/dist/index.mjs.map +1 -1
  53. package/ai-model-specification/dist/index.d.mts +0 -606
  54. package/ai-model-specification/dist/index.d.ts +0 -606
  55. package/ai-model-specification/dist/index.js +0 -617
  56. package/ai-model-specification/dist/index.js.map +0 -1
  57. package/ai-model-specification/dist/index.mjs +0 -560
  58. package/ai-model-specification/dist/index.mjs.map +0 -1
  59. package/core/dist/index.d.mts +0 -590
  60. package/core/dist/index.d.ts +0 -590
  61. package/core/dist/index.js +0 -1528
  62. package/core/dist/index.js.map +0 -1
  63. package/core/dist/index.mjs +0 -1481
  64. package/core/dist/index.mjs.map +0 -1
  65. package/provider/dist/index.d.mts +0 -429
  66. package/provider/dist/index.d.ts +0 -429
  67. package/provider/dist/index.js +0 -1194
  68. package/provider/dist/index.js.map +0 -1
  69. package/provider/dist/index.mjs +0 -1158
  70. package/provider/dist/index.mjs.map +0 -1
@@ -104,9 +104,9 @@ var toolCallStreamPart = {
104
104
  code: "7",
105
105
  name: "tool_calls",
106
106
  parse: (value) => {
107
- 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) => {
108
- 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";
109
- })) {
107
+ 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(
108
+ (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"
109
+ )) {
110
110
  throw new Error(
111
111
  '"tool_calls" parts expect an object with a ToolCallPayload.'
112
112
  );
@@ -219,27 +219,12 @@ async function* readDataStream(reader, {
219
219
  }
220
220
  }
221
221
 
222
- // shared/utils.ts
222
+ // shared/generate-id.ts
223
223
  import { customAlphabet } from "nanoid/non-secure";
224
- var nanoid = customAlphabet(
224
+ var generateId = customAlphabet(
225
225
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
226
226
  7
227
227
  );
228
- function createChunkDecoder(complex) {
229
- const decoder = new TextDecoder();
230
- if (!complex) {
231
- return function(chunk) {
232
- if (!chunk)
233
- return "";
234
- return decoder.decode(chunk, { stream: true });
235
- };
236
- }
237
- return function(chunk) {
238
- const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
239
- return decoded.map(parseStreamPart).filter(Boolean);
240
- };
241
- }
242
- var COMPLEX_HEADER = "X-Experimental-Stream-Data";
243
228
 
244
229
  // shared/parse-complex-response.ts
245
230
  function assignAnnotationsToMessage(message, annotations) {
@@ -252,7 +237,7 @@ async function parseComplexResponse({
252
237
  abortControllerRef,
253
238
  update,
254
239
  onFinish,
255
- generateId = nanoid,
240
+ generateId: generateId2 = generateId,
256
241
  getCurrentDate = () => /* @__PURE__ */ new Date()
257
242
  }) {
258
243
  const createdAt = getCurrentDate();
@@ -271,7 +256,7 @@ async function parseComplexResponse({
271
256
  };
272
257
  } else {
273
258
  prefixMap["text"] = {
274
- id: generateId(),
259
+ id: generateId2(),
275
260
  role: "assistant",
276
261
  content: value,
277
262
  createdAt
@@ -281,7 +266,7 @@ async function parseComplexResponse({
281
266
  let functionCallMessage = null;
282
267
  if (type === "function_call") {
283
268
  prefixMap["function_call"] = {
284
- id: generateId(),
269
+ id: generateId2(),
285
270
  role: "assistant",
286
271
  content: "",
287
272
  function_call: value.function_call,
@@ -293,7 +278,7 @@ async function parseComplexResponse({
293
278
  let toolCallMessage = null;
294
279
  if (type === "tool_calls") {
295
280
  prefixMap["tool_calls"] = {
296
- id: generateId(),
281
+ id: generateId2(),
297
282
  role: "assistant",
298
283
  content: "",
299
284
  tool_calls: value.tool_calls,
@@ -352,20 +337,36 @@ async function parseComplexResponse({
352
337
  };
353
338
  }
354
339
 
340
+ // shared/utils.ts
341
+ function createChunkDecoder(complex) {
342
+ const decoder = new TextDecoder();
343
+ if (!complex) {
344
+ return function(chunk) {
345
+ if (!chunk)
346
+ return "";
347
+ return decoder.decode(chunk, { stream: true });
348
+ };
349
+ }
350
+ return function(chunk) {
351
+ const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
352
+ return decoded.map(parseStreamPart).filter(Boolean);
353
+ };
354
+ }
355
+
355
356
  // shared/call-chat-api.ts
356
357
  async function callChatApi({
357
358
  api,
358
359
  messages,
359
360
  body,
361
+ streamMode = "stream-data",
360
362
  credentials,
361
363
  headers,
362
364
  abortController,
363
- appendMessage,
364
365
  restoreMessagesOnFailure,
365
366
  onResponse,
366
367
  onUpdate,
367
368
  onFinish,
368
- generateId
369
+ generateId: generateId2
369
370
  }) {
370
371
  var _a;
371
372
  const response = await fetch(api, {
@@ -401,63 +402,51 @@ async function callChatApi({
401
402
  throw new Error("The response body is empty.");
402
403
  }
403
404
  const reader = response.body.getReader();
404
- const isComplexMode = response.headers.get(COMPLEX_HEADER) === "true";
405
- if (isComplexMode) {
406
- return await parseComplexResponse({
407
- reader,
408
- abortControllerRef: abortController != null ? { current: abortController() } : void 0,
409
- update: onUpdate,
410
- onFinish(prefixMap) {
411
- if (onFinish && prefixMap.text != null) {
412
- onFinish(prefixMap.text);
405
+ switch (streamMode) {
406
+ case "text": {
407
+ const decoder = createChunkDecoder();
408
+ const resultMessage = {
409
+ id: generateId2(),
410
+ createdAt: /* @__PURE__ */ new Date(),
411
+ role: "assistant",
412
+ content: ""
413
+ };
414
+ while (true) {
415
+ const { done, value } = await reader.read();
416
+ if (done) {
417
+ break;
418
+ }
419
+ resultMessage.content += decoder(value);
420
+ resultMessage.id = generateId2();
421
+ onUpdate([{ ...resultMessage }], []);
422
+ if ((abortController == null ? void 0 : abortController()) === null) {
423
+ reader.cancel();
424
+ break;
413
425
  }
414
- },
415
- generateId
416
- });
417
- } else {
418
- const createdAt = /* @__PURE__ */ new Date();
419
- const decode = createChunkDecoder(false);
420
- let streamedResponse = "";
421
- const replyId = generateId();
422
- let responseMessage = {
423
- id: replyId,
424
- createdAt,
425
- content: "",
426
- role: "assistant"
427
- };
428
- while (true) {
429
- const { done, value } = await reader.read();
430
- if (done) {
431
- break;
432
- }
433
- streamedResponse += decode(value);
434
- if (streamedResponse.startsWith('{"function_call":')) {
435
- responseMessage["function_call"] = streamedResponse;
436
- } else if (streamedResponse.startsWith('{"tool_calls":')) {
437
- responseMessage["tool_calls"] = streamedResponse;
438
- } else {
439
- responseMessage["content"] = streamedResponse;
440
- }
441
- appendMessage({ ...responseMessage });
442
- if ((abortController == null ? void 0 : abortController()) === null) {
443
- reader.cancel();
444
- break;
445
426
  }
427
+ onFinish == null ? void 0 : onFinish(resultMessage);
428
+ return {
429
+ messages: [resultMessage],
430
+ data: []
431
+ };
446
432
  }
447
- if (streamedResponse.startsWith('{"function_call":')) {
448
- const parsedFunctionCall = JSON.parse(streamedResponse).function_call;
449
- responseMessage["function_call"] = parsedFunctionCall;
450
- appendMessage({ ...responseMessage });
451
- }
452
- if (streamedResponse.startsWith('{"tool_calls":')) {
453
- const parsedToolCalls = JSON.parse(streamedResponse).tool_calls;
454
- responseMessage["tool_calls"] = parsedToolCalls;
455
- appendMessage({ ...responseMessage });
433
+ case "stream-data": {
434
+ return await parseComplexResponse({
435
+ reader,
436
+ abortControllerRef: abortController != null ? { current: abortController() } : void 0,
437
+ update: onUpdate,
438
+ onFinish(prefixMap) {
439
+ if (onFinish && prefixMap.text != null) {
440
+ onFinish(prefixMap.text);
441
+ }
442
+ },
443
+ generateId: generateId2
444
+ });
456
445
  }
457
- if (onFinish) {
458
- onFinish(responseMessage);
446
+ default: {
447
+ const exhaustiveCheck = streamMode;
448
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
459
449
  }
460
- return responseMessage;
461
450
  }
462
451
  }
463
452
 
@@ -578,7 +567,7 @@ async function processChatStream({
578
567
  }
579
568
 
580
569
  // react/use-chat.ts
581
- var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId, onFinish, onResponse, sendExtraMessageFields) => {
570
+ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId2, streamMode, onFinish, onResponse, sendExtraMessageFields) => {
582
571
  var _a, _b;
583
572
  const previousMessages = messagesRef.current;
584
573
  mutate(chatRequest.messages, false);
@@ -597,7 +586,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
597
586
  })
598
587
  );
599
588
  if (typeof api !== "string") {
600
- const replyId = generateId();
589
+ const replyId = generateId2();
601
590
  const createdAt = /* @__PURE__ */ new Date();
602
591
  let responseMessage = {
603
592
  id: replyId,
@@ -649,15 +638,13 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
649
638
  tool_choice: chatRequest.tool_choice
650
639
  }
651
640
  },
641
+ streamMode,
652
642
  credentials: extraMetadataRef.current.credentials,
653
643
  headers: {
654
644
  ...extraMetadataRef.current.headers,
655
645
  ...(_b = chatRequest.options) == null ? void 0 : _b.headers
656
646
  },
657
647
  abortController: () => abortControllerRef.current,
658
- appendMessage(message) {
659
- mutate([...chatRequest.messages, message], false);
660
- },
661
648
  restoreMessagesOnFailure() {
662
649
  mutate(previousMessages, false);
663
650
  },
@@ -667,7 +654,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
667
654
  mutateStreamData([...existingData || [], ...data || []], false);
668
655
  },
669
656
  onFinish,
670
- generateId
657
+ generateId: generateId2
671
658
  });
672
659
  };
673
660
  function useChat({
@@ -678,13 +665,14 @@ function useChat({
678
665
  sendExtraMessageFields,
679
666
  experimental_onFunctionCall,
680
667
  experimental_onToolCall,
668
+ streamMode,
681
669
  onResponse,
682
670
  onFinish,
683
671
  onError,
684
672
  credentials,
685
673
  headers,
686
674
  body,
687
- generateId = nanoid
675
+ generateId: generateId2 = generateId
688
676
  } = {}) {
689
677
  const hookId = useId();
690
678
  const idKey = id != null ? id : hookId;
@@ -735,7 +723,8 @@ function useChat({
735
723
  extraMetadataRef,
736
724
  messagesRef,
737
725
  abortControllerRef,
738
- generateId,
726
+ generateId2,
727
+ streamMode,
739
728
  onFinish,
740
729
  onResponse,
741
730
  sendExtraMessageFields
@@ -777,7 +766,7 @@ function useChat({
777
766
  experimental_onToolCall,
778
767
  messagesRef,
779
768
  abortControllerRef,
780
- generateId
769
+ generateId2
781
770
  ]
782
771
  );
783
772
  const append = useCallback(
@@ -790,7 +779,7 @@ function useChat({
790
779
  data
791
780
  } = {}) => {
792
781
  if (!message.id) {
793
- message.id = generateId();
782
+ message.id = generateId2();
794
783
  }
795
784
  const chatRequest = {
796
785
  messages: messagesRef.current.concat(message),
@@ -803,7 +792,7 @@ function useChat({
803
792
  };
804
793
  return triggerRequest(chatRequest);
805
794
  },
806
- [triggerRequest, generateId]
795
+ [triggerRequest, generateId2]
807
796
  );
808
797
  const reload = useCallback(
809
798
  async ({
@@ -906,6 +895,7 @@ async function callCompletionApi({
906
895
  credentials,
907
896
  headers,
908
897
  body,
898
+ streamMode = "stream-data",
909
899
  setCompletion,
910
900
  setLoading,
911
901
  setError,
@@ -953,36 +943,44 @@ async function callCompletionApi({
953
943
  }
954
944
  let result = "";
955
945
  const reader = res.body.getReader();
956
- const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
957
- if (isComplexMode) {
958
- for await (const { type, value } of readDataStream(reader, {
959
- isAborted: () => abortController === null
960
- })) {
961
- switch (type) {
962
- case "text": {
963
- result += value;
964
- setCompletion(result);
946
+ switch (streamMode) {
947
+ case "text": {
948
+ const decoder = createChunkDecoder();
949
+ while (true) {
950
+ const { done, value } = await reader.read();
951
+ if (done) {
965
952
  break;
966
953
  }
967
- case "data": {
968
- onData == null ? void 0 : onData(value);
954
+ result += decoder(value);
955
+ setCompletion(result);
956
+ if (abortController === null) {
957
+ reader.cancel();
969
958
  break;
970
959
  }
971
960
  }
961
+ break;
972
962
  }
973
- } else {
974
- const decoder = createChunkDecoder();
975
- while (true) {
976
- const { done, value } = await reader.read();
977
- if (done) {
978
- break;
979
- }
980
- result += decoder(value);
981
- setCompletion(result);
982
- if (abortController === null) {
983
- reader.cancel();
984
- break;
963
+ case "stream-data": {
964
+ for await (const { type, value } of readDataStream(reader, {
965
+ isAborted: () => abortController === null
966
+ })) {
967
+ switch (type) {
968
+ case "text": {
969
+ result += value;
970
+ setCompletion(result);
971
+ break;
972
+ }
973
+ case "data": {
974
+ onData == null ? void 0 : onData(value);
975
+ break;
976
+ }
977
+ }
985
978
  }
979
+ break;
980
+ }
981
+ default: {
982
+ const exhaustiveCheck = streamMode;
983
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
986
984
  }
987
985
  }
988
986
  if (onFinish) {
@@ -1015,6 +1013,7 @@ function useCompletion({
1015
1013
  credentials,
1016
1014
  headers,
1017
1015
  body,
1016
+ streamMode,
1018
1017
  onResponse,
1019
1018
  onFinish,
1020
1019
  onError
@@ -1054,6 +1053,7 @@ function useCompletion({
1054
1053
  ...extraMetadataRef.current.body,
1055
1054
  ...options == null ? void 0 : options.body
1056
1055
  },
1056
+ streamMode,
1057
1057
  setCompletion: (completion2) => mutate(completion2, false),
1058
1058
  setLoading: mutateLoading,
1059
1059
  setError,
@@ -1127,8 +1127,7 @@ function useCompletion({
1127
1127
 
1128
1128
  // react/use-assistant.ts
1129
1129
  import { useState as useState3 } from "react";
1130
- import { nanoid as nanoid2 } from "nanoid";
1131
- function experimental_useAssistant({
1130
+ function useAssistant({
1132
1131
  api,
1133
1132
  threadId: threadIdParam,
1134
1133
  credentials,
@@ -1144,35 +1143,37 @@ function experimental_useAssistant({
1144
1143
  const handleInputChange = (event) => {
1145
1144
  setInput(event.target.value);
1146
1145
  };
1147
- const submitMessage = async (event, requestOptions) => {
1148
- var _a, _b;
1149
- (_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
1150
- if (input === "") {
1151
- return;
1152
- }
1146
+ const append = async (message, requestOptions) => {
1147
+ var _a;
1153
1148
  setStatus("in_progress");
1154
- setMessages((messages2) => [
1155
- ...messages2,
1156
- { id: "", role: "user", content: input }
1157
- ]);
1158
- setInput("");
1159
- const result = await fetch(api, {
1160
- method: "POST",
1161
- credentials,
1162
- headers: { "Content-Type": "application/json", ...headers },
1163
- body: JSON.stringify({
1164
- ...body,
1165
- // always use user-provided threadId when available:
1166
- threadId: (_b = threadIdParam != null ? threadIdParam : threadId) != null ? _b : null,
1167
- message: input,
1168
- // optional request data:
1169
- data: requestOptions == null ? void 0 : requestOptions.data
1170
- })
1149
+ setMessages((messages2) => {
1150
+ var _a2;
1151
+ return [
1152
+ ...messages2,
1153
+ {
1154
+ ...message,
1155
+ id: (_a2 = message.id) != null ? _a2 : generateId()
1156
+ }
1157
+ ];
1171
1158
  });
1172
- if (result.body == null) {
1173
- throw new Error("The response body is empty.");
1174
- }
1159
+ setInput("");
1175
1160
  try {
1161
+ const result = await fetch(api, {
1162
+ method: "POST",
1163
+ credentials,
1164
+ headers: { "Content-Type": "application/json", ...headers },
1165
+ body: JSON.stringify({
1166
+ ...body,
1167
+ // always use user-provided threadId when available:
1168
+ threadId: (_a = threadIdParam != null ? threadIdParam : threadId) != null ? _a : null,
1169
+ message: message.content,
1170
+ // optional request data:
1171
+ data: requestOptions == null ? void 0 : requestOptions.data
1172
+ })
1173
+ });
1174
+ if (result.body == null) {
1175
+ throw new Error("The response body is empty.");
1176
+ }
1176
1177
  for await (const { type, value } of readDataStream(
1177
1178
  result.body.getReader()
1178
1179
  )) {
@@ -1208,7 +1209,7 @@ function experimental_useAssistant({
1208
1209
  return [
1209
1210
  ...messages2,
1210
1211
  {
1211
- id: (_a2 = value.id) != null ? _a2 : nanoid2(),
1212
+ id: (_a2 = value.id) != null ? _a2 : generateId(),
1212
1213
  role: "data",
1213
1214
  content: "",
1214
1215
  data: value.data
@@ -1241,8 +1242,18 @@ function experimental_useAssistant({
1241
1242
  }
1242
1243
  setStatus("awaiting_message");
1243
1244
  };
1245
+ const submitMessage = async (event, requestOptions) => {
1246
+ var _a;
1247
+ (_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
1248
+ if (input === "") {
1249
+ return;
1250
+ }
1251
+ append({ role: "user", content: input }, requestOptions);
1252
+ };
1244
1253
  return {
1254
+ append,
1245
1255
  messages,
1256
+ setMessages,
1246
1257
  threadId,
1247
1258
  input,
1248
1259
  setInput,
@@ -1252,8 +1263,10 @@ function experimental_useAssistant({
1252
1263
  error
1253
1264
  };
1254
1265
  }
1266
+ var experimental_useAssistant = useAssistant;
1255
1267
  export {
1256
1268
  experimental_useAssistant,
1269
+ useAssistant,
1257
1270
  useChat,
1258
1271
  useCompletion
1259
1272
  };