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
@@ -102,9 +102,9 @@ var toolCallStreamPart = {
102
102
  code: "7",
103
103
  name: "tool_calls",
104
104
  parse: (value) => {
105
- 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) => {
106
- 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";
107
- })) {
105
+ 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(
106
+ (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"
107
+ )) {
108
108
  throw new Error(
109
109
  '"tool_calls" parts expect an object with a ToolCallPayload.'
110
110
  );
@@ -217,27 +217,12 @@ async function* readDataStream(reader, {
217
217
  }
218
218
  }
219
219
 
220
- // shared/utils.ts
220
+ // shared/generate-id.ts
221
221
  import { customAlphabet } from "nanoid/non-secure";
222
- var nanoid = customAlphabet(
222
+ var generateId = customAlphabet(
223
223
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
224
224
  7
225
225
  );
226
- function createChunkDecoder(complex) {
227
- const decoder = new TextDecoder();
228
- if (!complex) {
229
- return function(chunk) {
230
- if (!chunk)
231
- return "";
232
- return decoder.decode(chunk, { stream: true });
233
- };
234
- }
235
- return function(chunk) {
236
- const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
237
- return decoded.map(parseStreamPart).filter(Boolean);
238
- };
239
- }
240
- var COMPLEX_HEADER = "X-Experimental-Stream-Data";
241
226
 
242
227
  // shared/parse-complex-response.ts
243
228
  function assignAnnotationsToMessage(message, annotations) {
@@ -250,7 +235,7 @@ async function parseComplexResponse({
250
235
  abortControllerRef,
251
236
  update,
252
237
  onFinish,
253
- generateId = nanoid,
238
+ generateId: generateId2 = generateId,
254
239
  getCurrentDate = () => /* @__PURE__ */ new Date()
255
240
  }) {
256
241
  const createdAt = getCurrentDate();
@@ -269,7 +254,7 @@ async function parseComplexResponse({
269
254
  };
270
255
  } else {
271
256
  prefixMap["text"] = {
272
- id: generateId(),
257
+ id: generateId2(),
273
258
  role: "assistant",
274
259
  content: value,
275
260
  createdAt
@@ -279,7 +264,7 @@ async function parseComplexResponse({
279
264
  let functionCallMessage = null;
280
265
  if (type === "function_call") {
281
266
  prefixMap["function_call"] = {
282
- id: generateId(),
267
+ id: generateId2(),
283
268
  role: "assistant",
284
269
  content: "",
285
270
  function_call: value.function_call,
@@ -291,7 +276,7 @@ async function parseComplexResponse({
291
276
  let toolCallMessage = null;
292
277
  if (type === "tool_calls") {
293
278
  prefixMap["tool_calls"] = {
294
- id: generateId(),
279
+ id: generateId2(),
295
280
  role: "assistant",
296
281
  content: "",
297
282
  tool_calls: value.tool_calls,
@@ -350,20 +335,36 @@ async function parseComplexResponse({
350
335
  };
351
336
  }
352
337
 
338
+ // shared/utils.ts
339
+ function createChunkDecoder(complex) {
340
+ const decoder = new TextDecoder();
341
+ if (!complex) {
342
+ return function(chunk) {
343
+ if (!chunk)
344
+ return "";
345
+ return decoder.decode(chunk, { stream: true });
346
+ };
347
+ }
348
+ return function(chunk) {
349
+ const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
350
+ return decoded.map(parseStreamPart).filter(Boolean);
351
+ };
352
+ }
353
+
353
354
  // shared/call-chat-api.ts
354
355
  async function callChatApi({
355
356
  api,
356
357
  messages,
357
358
  body,
359
+ streamMode = "stream-data",
358
360
  credentials,
359
361
  headers,
360
362
  abortController,
361
- appendMessage,
362
363
  restoreMessagesOnFailure,
363
364
  onResponse,
364
365
  onUpdate,
365
366
  onFinish,
366
- generateId
367
+ generateId: generateId2
367
368
  }) {
368
369
  var _a;
369
370
  const response = await fetch(api, {
@@ -399,63 +400,51 @@ async function callChatApi({
399
400
  throw new Error("The response body is empty.");
400
401
  }
401
402
  const reader = response.body.getReader();
402
- const isComplexMode = response.headers.get(COMPLEX_HEADER) === "true";
403
- if (isComplexMode) {
404
- return await parseComplexResponse({
405
- reader,
406
- abortControllerRef: abortController != null ? { current: abortController() } : void 0,
407
- update: onUpdate,
408
- onFinish(prefixMap) {
409
- if (onFinish && prefixMap.text != null) {
410
- onFinish(prefixMap.text);
403
+ switch (streamMode) {
404
+ case "text": {
405
+ const decoder = createChunkDecoder();
406
+ const resultMessage = {
407
+ id: generateId2(),
408
+ createdAt: /* @__PURE__ */ new Date(),
409
+ role: "assistant",
410
+ content: ""
411
+ };
412
+ while (true) {
413
+ const { done, value } = await reader.read();
414
+ if (done) {
415
+ break;
416
+ }
417
+ resultMessage.content += decoder(value);
418
+ resultMessage.id = generateId2();
419
+ onUpdate([{ ...resultMessage }], []);
420
+ if ((abortController == null ? void 0 : abortController()) === null) {
421
+ reader.cancel();
422
+ break;
411
423
  }
412
- },
413
- generateId
414
- });
415
- } else {
416
- const createdAt = /* @__PURE__ */ new Date();
417
- const decode = createChunkDecoder(false);
418
- let streamedResponse = "";
419
- const replyId = generateId();
420
- let responseMessage = {
421
- id: replyId,
422
- createdAt,
423
- content: "",
424
- role: "assistant"
425
- };
426
- while (true) {
427
- const { done, value } = await reader.read();
428
- if (done) {
429
- break;
430
- }
431
- streamedResponse += decode(value);
432
- if (streamedResponse.startsWith('{"function_call":')) {
433
- responseMessage["function_call"] = streamedResponse;
434
- } else if (streamedResponse.startsWith('{"tool_calls":')) {
435
- responseMessage["tool_calls"] = streamedResponse;
436
- } else {
437
- responseMessage["content"] = streamedResponse;
438
- }
439
- appendMessage({ ...responseMessage });
440
- if ((abortController == null ? void 0 : abortController()) === null) {
441
- reader.cancel();
442
- break;
443
424
  }
425
+ onFinish == null ? void 0 : onFinish(resultMessage);
426
+ return {
427
+ messages: [resultMessage],
428
+ data: []
429
+ };
444
430
  }
445
- if (streamedResponse.startsWith('{"function_call":')) {
446
- const parsedFunctionCall = JSON.parse(streamedResponse).function_call;
447
- responseMessage["function_call"] = parsedFunctionCall;
448
- appendMessage({ ...responseMessage });
449
- }
450
- if (streamedResponse.startsWith('{"tool_calls":')) {
451
- const parsedToolCalls = JSON.parse(streamedResponse).tool_calls;
452
- responseMessage["tool_calls"] = parsedToolCalls;
453
- appendMessage({ ...responseMessage });
431
+ case "stream-data": {
432
+ return await parseComplexResponse({
433
+ reader,
434
+ abortControllerRef: abortController != null ? { current: abortController() } : void 0,
435
+ update: onUpdate,
436
+ onFinish(prefixMap) {
437
+ if (onFinish && prefixMap.text != null) {
438
+ onFinish(prefixMap.text);
439
+ }
440
+ },
441
+ generateId: generateId2
442
+ });
454
443
  }
455
- if (onFinish) {
456
- onFinish(responseMessage);
444
+ default: {
445
+ const exhaustiveCheck = streamMode;
446
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
457
447
  }
458
- return responseMessage;
459
448
  }
460
449
  }
461
450
 
@@ -586,13 +575,14 @@ function useChat({
586
575
  initialInput = "",
587
576
  sendExtraMessageFields,
588
577
  experimental_onFunctionCall,
578
+ streamMode,
589
579
  onResponse,
590
580
  onFinish,
591
581
  onError,
592
582
  credentials,
593
583
  headers,
594
584
  body,
595
- generateId = nanoid
585
+ generateId: generateId2 = generateId
596
586
  } = {}) {
597
587
  var _a, _b;
598
588
  const chatId = id || `chat-${uniqueId++}`;
@@ -649,6 +639,7 @@ function useChat({
649
639
  // Use unref to unwrap the ref value
650
640
  ...options == null ? void 0 : options.body
651
641
  },
642
+ streamMode,
652
643
  headers: {
653
644
  ...headers,
654
645
  ...options == null ? void 0 : options.headers
@@ -664,13 +655,10 @@ function useChat({
664
655
  mutate([...chatRequest.messages, message]);
665
656
  onFinish == null ? void 0 : onFinish(message);
666
657
  },
667
- appendMessage(message) {
668
- mutate([...chatRequest.messages, message]);
669
- },
670
658
  restoreMessagesOnFailure() {
671
659
  mutate(previousMessages);
672
660
  },
673
- generateId
661
+ generateId: generateId2
674
662
  });
675
663
  },
676
664
  experimental_onFunctionCall,
@@ -695,7 +683,7 @@ function useChat({
695
683
  }
696
684
  const append = async (message, options) => {
697
685
  if (!message.id) {
698
- message.id = generateId();
686
+ message.id = generateId2();
699
687
  }
700
688
  return triggerRequest(messages.value.concat(message), options);
701
689
  };
@@ -758,6 +746,7 @@ async function callCompletionApi({
758
746
  credentials,
759
747
  headers,
760
748
  body,
749
+ streamMode = "stream-data",
761
750
  setCompletion,
762
751
  setLoading,
763
752
  setError,
@@ -805,36 +794,44 @@ async function callCompletionApi({
805
794
  }
806
795
  let result = "";
807
796
  const reader = res.body.getReader();
808
- const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
809
- if (isComplexMode) {
810
- for await (const { type, value } of readDataStream(reader, {
811
- isAborted: () => abortController === null
812
- })) {
813
- switch (type) {
814
- case "text": {
815
- result += value;
816
- setCompletion(result);
797
+ switch (streamMode) {
798
+ case "text": {
799
+ const decoder = createChunkDecoder();
800
+ while (true) {
801
+ const { done, value } = await reader.read();
802
+ if (done) {
817
803
  break;
818
804
  }
819
- case "data": {
820
- onData == null ? void 0 : onData(value);
805
+ result += decoder(value);
806
+ setCompletion(result);
807
+ if (abortController === null) {
808
+ reader.cancel();
821
809
  break;
822
810
  }
823
811
  }
812
+ break;
824
813
  }
825
- } else {
826
- const decoder = createChunkDecoder();
827
- while (true) {
828
- const { done, value } = await reader.read();
829
- if (done) {
830
- break;
831
- }
832
- result += decoder(value);
833
- setCompletion(result);
834
- if (abortController === null) {
835
- reader.cancel();
836
- break;
814
+ case "stream-data": {
815
+ for await (const { type, value } of readDataStream(reader, {
816
+ isAborted: () => abortController === null
817
+ })) {
818
+ switch (type) {
819
+ case "text": {
820
+ result += value;
821
+ setCompletion(result);
822
+ break;
823
+ }
824
+ case "data": {
825
+ onData == null ? void 0 : onData(value);
826
+ break;
827
+ }
828
+ }
837
829
  }
830
+ break;
831
+ }
832
+ default: {
833
+ const exhaustiveCheck = streamMode;
834
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
838
835
  }
839
836
  }
840
837
  if (onFinish) {
@@ -870,6 +867,7 @@ function useCompletion({
870
867
  credentials,
871
868
  headers,
872
869
  body,
870
+ streamMode,
873
871
  onResponse,
874
872
  onFinish,
875
873
  onError
@@ -910,6 +908,7 @@ function useCompletion({
910
908
  ...unref2(body),
911
909
  ...options == null ? void 0 : options.body
912
910
  },
911
+ streamMode,
913
912
  setCompletion: mutate,
914
913
  setLoading: (loading) => mutateLoading(() => loading),
915
914
  setError: (err) => {