ai 3.1.0-canary.4 → 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 (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 +385 -20
  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 +367 -20
  29. package/rsc/dist/rsc-server.mjs +676 -35
  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
@@ -103,9 +103,9 @@ var toolCallStreamPart = {
103
103
  code: "7",
104
104
  name: "tool_calls",
105
105
  parse: (value) => {
106
- 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) => {
107
- 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";
108
- })) {
106
+ 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(
107
+ (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"
108
+ )) {
109
109
  throw new Error(
110
110
  '"tool_calls" parts expect an object with a ToolCallPayload.'
111
111
  );
@@ -218,27 +218,12 @@ async function* readDataStream(reader, {
218
218
  }
219
219
  }
220
220
 
221
- // shared/utils.ts
221
+ // shared/generate-id.ts
222
222
  import { customAlphabet } from "nanoid/non-secure";
223
- var nanoid = customAlphabet(
223
+ var generateId = customAlphabet(
224
224
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
225
225
  7
226
226
  );
227
- function createChunkDecoder(complex) {
228
- const decoder = new TextDecoder();
229
- if (!complex) {
230
- return function(chunk) {
231
- if (!chunk)
232
- return "";
233
- return decoder.decode(chunk, { stream: true });
234
- };
235
- }
236
- return function(chunk) {
237
- const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
238
- return decoded.map(parseStreamPart).filter(Boolean);
239
- };
240
- }
241
- var COMPLEX_HEADER = "X-Experimental-Stream-Data";
242
227
 
243
228
  // shared/parse-complex-response.ts
244
229
  function assignAnnotationsToMessage(message, annotations) {
@@ -251,7 +236,7 @@ async function parseComplexResponse({
251
236
  abortControllerRef,
252
237
  update,
253
238
  onFinish,
254
- generateId = nanoid,
239
+ generateId: generateId2 = generateId,
255
240
  getCurrentDate = () => /* @__PURE__ */ new Date()
256
241
  }) {
257
242
  const createdAt = getCurrentDate();
@@ -270,7 +255,7 @@ async function parseComplexResponse({
270
255
  };
271
256
  } else {
272
257
  prefixMap["text"] = {
273
- id: generateId(),
258
+ id: generateId2(),
274
259
  role: "assistant",
275
260
  content: value,
276
261
  createdAt
@@ -280,7 +265,7 @@ async function parseComplexResponse({
280
265
  let functionCallMessage = null;
281
266
  if (type === "function_call") {
282
267
  prefixMap["function_call"] = {
283
- id: generateId(),
268
+ id: generateId2(),
284
269
  role: "assistant",
285
270
  content: "",
286
271
  function_call: value.function_call,
@@ -292,7 +277,7 @@ async function parseComplexResponse({
292
277
  let toolCallMessage = null;
293
278
  if (type === "tool_calls") {
294
279
  prefixMap["tool_calls"] = {
295
- id: generateId(),
280
+ id: generateId2(),
296
281
  role: "assistant",
297
282
  content: "",
298
283
  tool_calls: value.tool_calls,
@@ -351,20 +336,36 @@ async function parseComplexResponse({
351
336
  };
352
337
  }
353
338
 
339
+ // shared/utils.ts
340
+ function createChunkDecoder(complex) {
341
+ const decoder = new TextDecoder();
342
+ if (!complex) {
343
+ return function(chunk) {
344
+ if (!chunk)
345
+ return "";
346
+ return decoder.decode(chunk, { stream: true });
347
+ };
348
+ }
349
+ return function(chunk) {
350
+ const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
351
+ return decoded.map(parseStreamPart).filter(Boolean);
352
+ };
353
+ }
354
+
354
355
  // shared/call-chat-api.ts
355
356
  async function callChatApi({
356
357
  api,
357
358
  messages,
358
359
  body,
360
+ streamMode = "stream-data",
359
361
  credentials,
360
362
  headers,
361
363
  abortController,
362
- appendMessage,
363
364
  restoreMessagesOnFailure,
364
365
  onResponse,
365
366
  onUpdate,
366
367
  onFinish,
367
- generateId
368
+ generateId: generateId2
368
369
  }) {
369
370
  var _a;
370
371
  const response = await fetch(api, {
@@ -400,63 +401,51 @@ async function callChatApi({
400
401
  throw new Error("The response body is empty.");
401
402
  }
402
403
  const reader = response.body.getReader();
403
- const isComplexMode = response.headers.get(COMPLEX_HEADER) === "true";
404
- if (isComplexMode) {
405
- return await parseComplexResponse({
406
- reader,
407
- abortControllerRef: abortController != null ? { current: abortController() } : void 0,
408
- update: onUpdate,
409
- onFinish(prefixMap) {
410
- if (onFinish && prefixMap.text != null) {
411
- onFinish(prefixMap.text);
404
+ switch (streamMode) {
405
+ case "text": {
406
+ const decoder = createChunkDecoder();
407
+ const resultMessage = {
408
+ id: generateId2(),
409
+ createdAt: /* @__PURE__ */ new Date(),
410
+ role: "assistant",
411
+ content: ""
412
+ };
413
+ while (true) {
414
+ const { done, value } = await reader.read();
415
+ if (done) {
416
+ break;
417
+ }
418
+ resultMessage.content += decoder(value);
419
+ resultMessage.id = generateId2();
420
+ onUpdate([{ ...resultMessage }], []);
421
+ if ((abortController == null ? void 0 : abortController()) === null) {
422
+ reader.cancel();
423
+ break;
412
424
  }
413
- },
414
- generateId
415
- });
416
- } else {
417
- const createdAt = /* @__PURE__ */ new Date();
418
- const decode = createChunkDecoder(false);
419
- let streamedResponse = "";
420
- const replyId = generateId();
421
- let responseMessage = {
422
- id: replyId,
423
- createdAt,
424
- content: "",
425
- role: "assistant"
426
- };
427
- while (true) {
428
- const { done, value } = await reader.read();
429
- if (done) {
430
- break;
431
- }
432
- streamedResponse += decode(value);
433
- if (streamedResponse.startsWith('{"function_call":')) {
434
- responseMessage["function_call"] = streamedResponse;
435
- } else if (streamedResponse.startsWith('{"tool_calls":')) {
436
- responseMessage["tool_calls"] = streamedResponse;
437
- } else {
438
- responseMessage["content"] = streamedResponse;
439
- }
440
- appendMessage({ ...responseMessage });
441
- if ((abortController == null ? void 0 : abortController()) === null) {
442
- reader.cancel();
443
- break;
444
425
  }
426
+ onFinish == null ? void 0 : onFinish(resultMessage);
427
+ return {
428
+ messages: [resultMessage],
429
+ data: []
430
+ };
445
431
  }
446
- if (streamedResponse.startsWith('{"function_call":')) {
447
- const parsedFunctionCall = JSON.parse(streamedResponse).function_call;
448
- responseMessage["function_call"] = parsedFunctionCall;
449
- appendMessage({ ...responseMessage });
450
- }
451
- if (streamedResponse.startsWith('{"tool_calls":')) {
452
- const parsedToolCalls = JSON.parse(streamedResponse).tool_calls;
453
- responseMessage["tool_calls"] = parsedToolCalls;
454
- appendMessage({ ...responseMessage });
432
+ case "stream-data": {
433
+ return await parseComplexResponse({
434
+ reader,
435
+ abortControllerRef: abortController != null ? { current: abortController() } : void 0,
436
+ update: onUpdate,
437
+ onFinish(prefixMap) {
438
+ if (onFinish && prefixMap.text != null) {
439
+ onFinish(prefixMap.text);
440
+ }
441
+ },
442
+ generateId: generateId2
443
+ });
455
444
  }
456
- if (onFinish) {
457
- onFinish(responseMessage);
445
+ default: {
446
+ const exhaustiveCheck = streamMode;
447
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
458
448
  }
459
- return responseMessage;
460
449
  }
461
450
  }
462
451
 
@@ -598,7 +587,8 @@ function useChat({
598
587
  credentials,
599
588
  headers,
600
589
  body,
601
- generateId = nanoid
590
+ streamMode,
591
+ generateId: generateId2 = generateId
602
592
  } = {}) {
603
593
  const chatId = id || `chat-${uniqueId++}`;
604
594
  const key = `${api}|${chatId}`;
@@ -654,6 +644,7 @@ function useChat({
654
644
  ...body,
655
645
  ...options == null ? void 0 : options.body
656
646
  },
647
+ streamMode,
657
648
  headers: {
658
649
  ...headers,
659
650
  ...options == null ? void 0 : options.headers
@@ -666,15 +657,12 @@ function useChat({
666
657
  setStreamData([...existingData, ...data2 != null ? data2 : []]);
667
658
  },
668
659
  onFinish,
669
- appendMessage(message) {
670
- mutate([...chatRequest.messages, message]);
671
- },
672
660
  restoreMessagesOnFailure() {
673
661
  if (previousMessages.status === "success") {
674
662
  mutate(previousMessages.data);
675
663
  }
676
664
  },
677
- generateId
665
+ generateId: generateId2
678
666
  });
679
667
  },
680
668
  experimental_onFunctionCall,
@@ -700,7 +688,7 @@ function useChat({
700
688
  const append = async (message, options) => {
701
689
  var _a;
702
690
  if (!message.id) {
703
- message.id = generateId();
691
+ message.id = generateId2();
704
692
  }
705
693
  return triggerRequest(
706
694
  ((_a = messages()) != null ? _a : []).concat(message),
@@ -769,6 +757,7 @@ async function callCompletionApi({
769
757
  credentials,
770
758
  headers,
771
759
  body,
760
+ streamMode = "stream-data",
772
761
  setCompletion,
773
762
  setLoading,
774
763
  setError,
@@ -816,36 +805,44 @@ async function callCompletionApi({
816
805
  }
817
806
  let result = "";
818
807
  const reader = res.body.getReader();
819
- const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
820
- if (isComplexMode) {
821
- for await (const { type, value } of readDataStream(reader, {
822
- isAborted: () => abortController === null
823
- })) {
824
- switch (type) {
825
- case "text": {
826
- result += value;
827
- setCompletion(result);
808
+ switch (streamMode) {
809
+ case "text": {
810
+ const decoder = createChunkDecoder();
811
+ while (true) {
812
+ const { done, value } = await reader.read();
813
+ if (done) {
828
814
  break;
829
815
  }
830
- case "data": {
831
- onData == null ? void 0 : onData(value);
816
+ result += decoder(value);
817
+ setCompletion(result);
818
+ if (abortController === null) {
819
+ reader.cancel();
832
820
  break;
833
821
  }
834
822
  }
823
+ break;
835
824
  }
836
- } else {
837
- const decoder = createChunkDecoder();
838
- while (true) {
839
- const { done, value } = await reader.read();
840
- if (done) {
841
- break;
842
- }
843
- result += decoder(value);
844
- setCompletion(result);
845
- if (abortController === null) {
846
- reader.cancel();
847
- break;
825
+ case "stream-data": {
826
+ for await (const { type, value } of readDataStream(reader, {
827
+ isAborted: () => abortController === null
828
+ })) {
829
+ switch (type) {
830
+ case "text": {
831
+ result += value;
832
+ setCompletion(result);
833
+ break;
834
+ }
835
+ case "data": {
836
+ onData == null ? void 0 : onData(value);
837
+ break;
838
+ }
839
+ }
848
840
  }
841
+ break;
842
+ }
843
+ default: {
844
+ const exhaustiveCheck = streamMode;
845
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
849
846
  }
850
847
  }
851
848
  if (onFinish) {
@@ -886,6 +883,7 @@ function useCompletion({
886
883
  credentials,
887
884
  headers,
888
885
  body,
886
+ streamMode,
889
887
  onResponse,
890
888
  onFinish,
891
889
  onError
@@ -924,6 +922,7 @@ function useCompletion({
924
922
  ...body,
925
923
  ...options == null ? void 0 : options.body
926
924
  },
925
+ streamMode,
927
926
  setCompletion: mutate,
928
927
  setLoading: setIsLoading,
929
928
  setError,