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
package/vue/dist/index.js CHANGED
@@ -139,9 +139,9 @@ var toolCallStreamPart = {
139
139
  code: "7",
140
140
  name: "tool_calls",
141
141
  parse: (value) => {
142
- 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) => {
143
- 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";
144
- })) {
142
+ 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(
143
+ (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"
144
+ )) {
145
145
  throw new Error(
146
146
  '"tool_calls" parts expect an object with a ToolCallPayload.'
147
147
  );
@@ -254,27 +254,12 @@ async function* readDataStream(reader, {
254
254
  }
255
255
  }
256
256
 
257
- // shared/utils.ts
257
+ // shared/generate-id.ts
258
258
  var import_non_secure = require("nanoid/non-secure");
259
- var nanoid = (0, import_non_secure.customAlphabet)(
259
+ var generateId = (0, import_non_secure.customAlphabet)(
260
260
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
261
261
  7
262
262
  );
263
- function createChunkDecoder(complex) {
264
- const decoder = new TextDecoder();
265
- if (!complex) {
266
- return function(chunk) {
267
- if (!chunk)
268
- return "";
269
- return decoder.decode(chunk, { stream: true });
270
- };
271
- }
272
- return function(chunk) {
273
- const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
274
- return decoded.map(parseStreamPart).filter(Boolean);
275
- };
276
- }
277
- var COMPLEX_HEADER = "X-Experimental-Stream-Data";
278
263
 
279
264
  // shared/parse-complex-response.ts
280
265
  function assignAnnotationsToMessage(message, annotations) {
@@ -287,7 +272,7 @@ async function parseComplexResponse({
287
272
  abortControllerRef,
288
273
  update,
289
274
  onFinish,
290
- generateId = nanoid,
275
+ generateId: generateId2 = generateId,
291
276
  getCurrentDate = () => /* @__PURE__ */ new Date()
292
277
  }) {
293
278
  const createdAt = getCurrentDate();
@@ -306,7 +291,7 @@ async function parseComplexResponse({
306
291
  };
307
292
  } else {
308
293
  prefixMap["text"] = {
309
- id: generateId(),
294
+ id: generateId2(),
310
295
  role: "assistant",
311
296
  content: value,
312
297
  createdAt
@@ -316,7 +301,7 @@ async function parseComplexResponse({
316
301
  let functionCallMessage = null;
317
302
  if (type === "function_call") {
318
303
  prefixMap["function_call"] = {
319
- id: generateId(),
304
+ id: generateId2(),
320
305
  role: "assistant",
321
306
  content: "",
322
307
  function_call: value.function_call,
@@ -328,7 +313,7 @@ async function parseComplexResponse({
328
313
  let toolCallMessage = null;
329
314
  if (type === "tool_calls") {
330
315
  prefixMap["tool_calls"] = {
331
- id: generateId(),
316
+ id: generateId2(),
332
317
  role: "assistant",
333
318
  content: "",
334
319
  tool_calls: value.tool_calls,
@@ -387,20 +372,36 @@ async function parseComplexResponse({
387
372
  };
388
373
  }
389
374
 
375
+ // shared/utils.ts
376
+ function createChunkDecoder(complex) {
377
+ const decoder = new TextDecoder();
378
+ if (!complex) {
379
+ return function(chunk) {
380
+ if (!chunk)
381
+ return "";
382
+ return decoder.decode(chunk, { stream: true });
383
+ };
384
+ }
385
+ return function(chunk) {
386
+ const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
387
+ return decoded.map(parseStreamPart).filter(Boolean);
388
+ };
389
+ }
390
+
390
391
  // shared/call-chat-api.ts
391
392
  async function callChatApi({
392
393
  api,
393
394
  messages,
394
395
  body,
396
+ streamMode = "stream-data",
395
397
  credentials,
396
398
  headers,
397
399
  abortController,
398
- appendMessage,
399
400
  restoreMessagesOnFailure,
400
401
  onResponse,
401
402
  onUpdate,
402
403
  onFinish,
403
- generateId
404
+ generateId: generateId2
404
405
  }) {
405
406
  var _a;
406
407
  const response = await fetch(api, {
@@ -436,63 +437,51 @@ async function callChatApi({
436
437
  throw new Error("The response body is empty.");
437
438
  }
438
439
  const reader = response.body.getReader();
439
- const isComplexMode = response.headers.get(COMPLEX_HEADER) === "true";
440
- if (isComplexMode) {
441
- return await parseComplexResponse({
442
- reader,
443
- abortControllerRef: abortController != null ? { current: abortController() } : void 0,
444
- update: onUpdate,
445
- onFinish(prefixMap) {
446
- if (onFinish && prefixMap.text != null) {
447
- onFinish(prefixMap.text);
440
+ switch (streamMode) {
441
+ case "text": {
442
+ const decoder = createChunkDecoder();
443
+ const resultMessage = {
444
+ id: generateId2(),
445
+ createdAt: /* @__PURE__ */ new Date(),
446
+ role: "assistant",
447
+ content: ""
448
+ };
449
+ while (true) {
450
+ const { done, value } = await reader.read();
451
+ if (done) {
452
+ break;
453
+ }
454
+ resultMessage.content += decoder(value);
455
+ resultMessage.id = generateId2();
456
+ onUpdate([{ ...resultMessage }], []);
457
+ if ((abortController == null ? void 0 : abortController()) === null) {
458
+ reader.cancel();
459
+ break;
448
460
  }
449
- },
450
- generateId
451
- });
452
- } else {
453
- const createdAt = /* @__PURE__ */ new Date();
454
- const decode = createChunkDecoder(false);
455
- let streamedResponse = "";
456
- const replyId = generateId();
457
- let responseMessage = {
458
- id: replyId,
459
- createdAt,
460
- content: "",
461
- role: "assistant"
462
- };
463
- while (true) {
464
- const { done, value } = await reader.read();
465
- if (done) {
466
- break;
467
- }
468
- streamedResponse += decode(value);
469
- if (streamedResponse.startsWith('{"function_call":')) {
470
- responseMessage["function_call"] = streamedResponse;
471
- } else if (streamedResponse.startsWith('{"tool_calls":')) {
472
- responseMessage["tool_calls"] = streamedResponse;
473
- } else {
474
- responseMessage["content"] = streamedResponse;
475
- }
476
- appendMessage({ ...responseMessage });
477
- if ((abortController == null ? void 0 : abortController()) === null) {
478
- reader.cancel();
479
- break;
480
461
  }
462
+ onFinish == null ? void 0 : onFinish(resultMessage);
463
+ return {
464
+ messages: [resultMessage],
465
+ data: []
466
+ };
481
467
  }
482
- if (streamedResponse.startsWith('{"function_call":')) {
483
- const parsedFunctionCall = JSON.parse(streamedResponse).function_call;
484
- responseMessage["function_call"] = parsedFunctionCall;
485
- appendMessage({ ...responseMessage });
486
- }
487
- if (streamedResponse.startsWith('{"tool_calls":')) {
488
- const parsedToolCalls = JSON.parse(streamedResponse).tool_calls;
489
- responseMessage["tool_calls"] = parsedToolCalls;
490
- appendMessage({ ...responseMessage });
468
+ case "stream-data": {
469
+ return await parseComplexResponse({
470
+ reader,
471
+ abortControllerRef: abortController != null ? { current: abortController() } : void 0,
472
+ update: onUpdate,
473
+ onFinish(prefixMap) {
474
+ if (onFinish && prefixMap.text != null) {
475
+ onFinish(prefixMap.text);
476
+ }
477
+ },
478
+ generateId: generateId2
479
+ });
491
480
  }
492
- if (onFinish) {
493
- onFinish(responseMessage);
481
+ default: {
482
+ const exhaustiveCheck = streamMode;
483
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
494
484
  }
495
- return responseMessage;
496
485
  }
497
486
  }
498
487
 
@@ -623,13 +612,14 @@ function useChat({
623
612
  initialInput = "",
624
613
  sendExtraMessageFields,
625
614
  experimental_onFunctionCall,
615
+ streamMode,
626
616
  onResponse,
627
617
  onFinish,
628
618
  onError,
629
619
  credentials,
630
620
  headers,
631
621
  body,
632
- generateId = nanoid
622
+ generateId: generateId2 = generateId
633
623
  } = {}) {
634
624
  var _a, _b;
635
625
  const chatId = id || `chat-${uniqueId++}`;
@@ -686,6 +676,7 @@ function useChat({
686
676
  // Use unref to unwrap the ref value
687
677
  ...options == null ? void 0 : options.body
688
678
  },
679
+ streamMode,
689
680
  headers: {
690
681
  ...headers,
691
682
  ...options == null ? void 0 : options.headers
@@ -701,13 +692,10 @@ function useChat({
701
692
  mutate([...chatRequest.messages, message]);
702
693
  onFinish == null ? void 0 : onFinish(message);
703
694
  },
704
- appendMessage(message) {
705
- mutate([...chatRequest.messages, message]);
706
- },
707
695
  restoreMessagesOnFailure() {
708
696
  mutate(previousMessages);
709
697
  },
710
- generateId
698
+ generateId: generateId2
711
699
  });
712
700
  },
713
701
  experimental_onFunctionCall,
@@ -732,7 +720,7 @@ function useChat({
732
720
  }
733
721
  const append = async (message, options) => {
734
722
  if (!message.id) {
735
- message.id = generateId();
723
+ message.id = generateId2();
736
724
  }
737
725
  return triggerRequest(messages.value.concat(message), options);
738
726
  };
@@ -795,6 +783,7 @@ async function callCompletionApi({
795
783
  credentials,
796
784
  headers,
797
785
  body,
786
+ streamMode = "stream-data",
798
787
  setCompletion,
799
788
  setLoading,
800
789
  setError,
@@ -842,36 +831,44 @@ async function callCompletionApi({
842
831
  }
843
832
  let result = "";
844
833
  const reader = res.body.getReader();
845
- const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
846
- if (isComplexMode) {
847
- for await (const { type, value } of readDataStream(reader, {
848
- isAborted: () => abortController === null
849
- })) {
850
- switch (type) {
851
- case "text": {
852
- result += value;
853
- setCompletion(result);
834
+ switch (streamMode) {
835
+ case "text": {
836
+ const decoder = createChunkDecoder();
837
+ while (true) {
838
+ const { done, value } = await reader.read();
839
+ if (done) {
854
840
  break;
855
841
  }
856
- case "data": {
857
- onData == null ? void 0 : onData(value);
842
+ result += decoder(value);
843
+ setCompletion(result);
844
+ if (abortController === null) {
845
+ reader.cancel();
858
846
  break;
859
847
  }
860
848
  }
849
+ break;
861
850
  }
862
- } else {
863
- const decoder = createChunkDecoder();
864
- while (true) {
865
- const { done, value } = await reader.read();
866
- if (done) {
867
- break;
868
- }
869
- result += decoder(value);
870
- setCompletion(result);
871
- if (abortController === null) {
872
- reader.cancel();
873
- break;
851
+ case "stream-data": {
852
+ for await (const { type, value } of readDataStream(reader, {
853
+ isAborted: () => abortController === null
854
+ })) {
855
+ switch (type) {
856
+ case "text": {
857
+ result += value;
858
+ setCompletion(result);
859
+ break;
860
+ }
861
+ case "data": {
862
+ onData == null ? void 0 : onData(value);
863
+ break;
864
+ }
865
+ }
874
866
  }
867
+ break;
868
+ }
869
+ default: {
870
+ const exhaustiveCheck = streamMode;
871
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
875
872
  }
876
873
  }
877
874
  if (onFinish) {
@@ -907,6 +904,7 @@ function useCompletion({
907
904
  credentials,
908
905
  headers,
909
906
  body,
907
+ streamMode,
910
908
  onResponse,
911
909
  onFinish,
912
910
  onError
@@ -947,6 +945,7 @@ function useCompletion({
947
945
  ...(0, import_vue2.unref)(body),
948
946
  ...options == null ? void 0 : options.body
949
947
  },
948
+ streamMode,
950
949
  setCompletion: mutate,
951
950
  setLoading: (loading) => mutateLoading(() => loading),
952
951
  setError: (err) => {