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
@@ -130,9 +130,9 @@ var toolCallStreamPart = {
130
130
  code: "7",
131
131
  name: "tool_calls",
132
132
  parse: (value) => {
133
- 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) => {
134
- 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";
135
- })) {
133
+ 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(
134
+ (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"
135
+ )) {
136
136
  throw new Error(
137
137
  '"tool_calls" parts expect an object with a ToolCallPayload.'
138
138
  );
@@ -245,27 +245,12 @@ async function* readDataStream(reader, {
245
245
  }
246
246
  }
247
247
 
248
- // shared/utils.ts
248
+ // shared/generate-id.ts
249
249
  var import_non_secure = require("nanoid/non-secure");
250
- var nanoid = (0, import_non_secure.customAlphabet)(
250
+ var generateId = (0, import_non_secure.customAlphabet)(
251
251
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
252
252
  7
253
253
  );
254
- function createChunkDecoder(complex) {
255
- const decoder = new TextDecoder();
256
- if (!complex) {
257
- return function(chunk) {
258
- if (!chunk)
259
- return "";
260
- return decoder.decode(chunk, { stream: true });
261
- };
262
- }
263
- return function(chunk) {
264
- const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
265
- return decoded.map(parseStreamPart).filter(Boolean);
266
- };
267
- }
268
- var COMPLEX_HEADER = "X-Experimental-Stream-Data";
269
254
 
270
255
  // shared/parse-complex-response.ts
271
256
  function assignAnnotationsToMessage(message, annotations) {
@@ -278,7 +263,7 @@ async function parseComplexResponse({
278
263
  abortControllerRef,
279
264
  update,
280
265
  onFinish,
281
- generateId = nanoid,
266
+ generateId: generateId2 = generateId,
282
267
  getCurrentDate = () => /* @__PURE__ */ new Date()
283
268
  }) {
284
269
  const createdAt = getCurrentDate();
@@ -297,7 +282,7 @@ async function parseComplexResponse({
297
282
  };
298
283
  } else {
299
284
  prefixMap["text"] = {
300
- id: generateId(),
285
+ id: generateId2(),
301
286
  role: "assistant",
302
287
  content: value,
303
288
  createdAt
@@ -307,7 +292,7 @@ async function parseComplexResponse({
307
292
  let functionCallMessage = null;
308
293
  if (type === "function_call") {
309
294
  prefixMap["function_call"] = {
310
- id: generateId(),
295
+ id: generateId2(),
311
296
  role: "assistant",
312
297
  content: "",
313
298
  function_call: value.function_call,
@@ -319,7 +304,7 @@ async function parseComplexResponse({
319
304
  let toolCallMessage = null;
320
305
  if (type === "tool_calls") {
321
306
  prefixMap["tool_calls"] = {
322
- id: generateId(),
307
+ id: generateId2(),
323
308
  role: "assistant",
324
309
  content: "",
325
310
  tool_calls: value.tool_calls,
@@ -378,20 +363,36 @@ async function parseComplexResponse({
378
363
  };
379
364
  }
380
365
 
366
+ // shared/utils.ts
367
+ function createChunkDecoder(complex) {
368
+ const decoder = new TextDecoder();
369
+ if (!complex) {
370
+ return function(chunk) {
371
+ if (!chunk)
372
+ return "";
373
+ return decoder.decode(chunk, { stream: true });
374
+ };
375
+ }
376
+ return function(chunk) {
377
+ const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
378
+ return decoded.map(parseStreamPart).filter(Boolean);
379
+ };
380
+ }
381
+
381
382
  // shared/call-chat-api.ts
382
383
  async function callChatApi({
383
384
  api,
384
385
  messages,
385
386
  body,
387
+ streamMode = "stream-data",
386
388
  credentials,
387
389
  headers,
388
390
  abortController,
389
- appendMessage,
390
391
  restoreMessagesOnFailure,
391
392
  onResponse,
392
393
  onUpdate,
393
394
  onFinish,
394
- generateId
395
+ generateId: generateId2
395
396
  }) {
396
397
  var _a;
397
398
  const response = await fetch(api, {
@@ -427,63 +428,51 @@ async function callChatApi({
427
428
  throw new Error("The response body is empty.");
428
429
  }
429
430
  const reader = response.body.getReader();
430
- const isComplexMode = response.headers.get(COMPLEX_HEADER) === "true";
431
- if (isComplexMode) {
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);
431
+ switch (streamMode) {
432
+ case "text": {
433
+ const decoder = createChunkDecoder();
434
+ const resultMessage = {
435
+ id: generateId2(),
436
+ createdAt: /* @__PURE__ */ new Date(),
437
+ role: "assistant",
438
+ content: ""
439
+ };
440
+ while (true) {
441
+ const { done, value } = await reader.read();
442
+ if (done) {
443
+ break;
444
+ }
445
+ resultMessage.content += decoder(value);
446
+ resultMessage.id = generateId2();
447
+ onUpdate([{ ...resultMessage }], []);
448
+ if ((abortController == null ? void 0 : abortController()) === null) {
449
+ reader.cancel();
450
+ break;
439
451
  }
440
- },
441
- generateId
442
- });
443
- } else {
444
- const createdAt = /* @__PURE__ */ new Date();
445
- const decode = createChunkDecoder(false);
446
- let streamedResponse = "";
447
- const replyId = generateId();
448
- let responseMessage = {
449
- id: replyId,
450
- createdAt,
451
- content: "",
452
- role: "assistant"
453
- };
454
- while (true) {
455
- const { done, value } = await reader.read();
456
- if (done) {
457
- break;
458
- }
459
- streamedResponse += decode(value);
460
- if (streamedResponse.startsWith('{"function_call":')) {
461
- responseMessage["function_call"] = streamedResponse;
462
- } else if (streamedResponse.startsWith('{"tool_calls":')) {
463
- responseMessage["tool_calls"] = streamedResponse;
464
- } else {
465
- responseMessage["content"] = streamedResponse;
466
- }
467
- appendMessage({ ...responseMessage });
468
- if ((abortController == null ? void 0 : abortController()) === null) {
469
- reader.cancel();
470
- break;
471
452
  }
453
+ onFinish == null ? void 0 : onFinish(resultMessage);
454
+ return {
455
+ messages: [resultMessage],
456
+ data: []
457
+ };
472
458
  }
473
- if (streamedResponse.startsWith('{"function_call":')) {
474
- const parsedFunctionCall = JSON.parse(streamedResponse).function_call;
475
- responseMessage["function_call"] = parsedFunctionCall;
476
- appendMessage({ ...responseMessage });
477
- }
478
- if (streamedResponse.startsWith('{"tool_calls":')) {
479
- const parsedToolCalls = JSON.parse(streamedResponse).tool_calls;
480
- responseMessage["tool_calls"] = parsedToolCalls;
481
- appendMessage({ ...responseMessage });
459
+ case "stream-data": {
460
+ return await parseComplexResponse({
461
+ reader,
462
+ abortControllerRef: abortController != null ? { current: abortController() } : void 0,
463
+ update: onUpdate,
464
+ onFinish(prefixMap) {
465
+ if (onFinish && prefixMap.text != null) {
466
+ onFinish(prefixMap.text);
467
+ }
468
+ },
469
+ generateId: generateId2
470
+ });
482
471
  }
483
- if (onFinish) {
484
- onFinish(responseMessage);
472
+ default: {
473
+ const exhaustiveCheck = streamMode;
474
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
485
475
  }
486
- return responseMessage;
487
476
  }
488
477
  }
489
478
 
@@ -625,7 +614,8 @@ function useChat({
625
614
  credentials,
626
615
  headers,
627
616
  body,
628
- generateId = nanoid
617
+ streamMode,
618
+ generateId: generateId2 = generateId
629
619
  } = {}) {
630
620
  const chatId = id || `chat-${uniqueId++}`;
631
621
  const key = `${api}|${chatId}`;
@@ -681,6 +671,7 @@ function useChat({
681
671
  ...body,
682
672
  ...options == null ? void 0 : options.body
683
673
  },
674
+ streamMode,
684
675
  headers: {
685
676
  ...headers,
686
677
  ...options == null ? void 0 : options.headers
@@ -693,15 +684,12 @@ function useChat({
693
684
  setStreamData([...existingData, ...data2 != null ? data2 : []]);
694
685
  },
695
686
  onFinish,
696
- appendMessage(message) {
697
- mutate([...chatRequest.messages, message]);
698
- },
699
687
  restoreMessagesOnFailure() {
700
688
  if (previousMessages.status === "success") {
701
689
  mutate(previousMessages.data);
702
690
  }
703
691
  },
704
- generateId
692
+ generateId: generateId2
705
693
  });
706
694
  },
707
695
  experimental_onFunctionCall,
@@ -727,7 +715,7 @@ function useChat({
727
715
  const append = async (message, options) => {
728
716
  var _a;
729
717
  if (!message.id) {
730
- message.id = generateId();
718
+ message.id = generateId2();
731
719
  }
732
720
  return triggerRequest(
733
721
  ((_a = messages()) != null ? _a : []).concat(message),
@@ -796,6 +784,7 @@ async function callCompletionApi({
796
784
  credentials,
797
785
  headers,
798
786
  body,
787
+ streamMode = "stream-data",
799
788
  setCompletion,
800
789
  setLoading,
801
790
  setError,
@@ -843,36 +832,44 @@ async function callCompletionApi({
843
832
  }
844
833
  let result = "";
845
834
  const reader = res.body.getReader();
846
- const isComplexMode = res.headers.get(COMPLEX_HEADER) === "true";
847
- if (isComplexMode) {
848
- for await (const { type, value } of readDataStream(reader, {
849
- isAborted: () => abortController === null
850
- })) {
851
- switch (type) {
852
- case "text": {
853
- result += value;
854
- setCompletion(result);
835
+ switch (streamMode) {
836
+ case "text": {
837
+ const decoder = createChunkDecoder();
838
+ while (true) {
839
+ const { done, value } = await reader.read();
840
+ if (done) {
855
841
  break;
856
842
  }
857
- case "data": {
858
- onData == null ? void 0 : onData(value);
843
+ result += decoder(value);
844
+ setCompletion(result);
845
+ if (abortController === null) {
846
+ reader.cancel();
859
847
  break;
860
848
  }
861
849
  }
850
+ break;
862
851
  }
863
- } else {
864
- const decoder = createChunkDecoder();
865
- while (true) {
866
- const { done, value } = await reader.read();
867
- if (done) {
868
- break;
869
- }
870
- result += decoder(value);
871
- setCompletion(result);
872
- if (abortController === null) {
873
- reader.cancel();
874
- break;
852
+ case "stream-data": {
853
+ for await (const { type, value } of readDataStream(reader, {
854
+ isAborted: () => abortController === null
855
+ })) {
856
+ switch (type) {
857
+ case "text": {
858
+ result += value;
859
+ setCompletion(result);
860
+ break;
861
+ }
862
+ case "data": {
863
+ onData == null ? void 0 : onData(value);
864
+ break;
865
+ }
866
+ }
875
867
  }
868
+ break;
869
+ }
870
+ default: {
871
+ const exhaustiveCheck = streamMode;
872
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
876
873
  }
877
874
  }
878
875
  if (onFinish) {
@@ -913,6 +910,7 @@ function useCompletion({
913
910
  credentials,
914
911
  headers,
915
912
  body,
913
+ streamMode,
916
914
  onResponse,
917
915
  onFinish,
918
916
  onError
@@ -951,6 +949,7 @@ function useCompletion({
951
949
  ...body,
952
950
  ...options == null ? void 0 : options.body
953
951
  },
952
+ streamMode,
954
953
  setCompletion: mutate,
955
954
  setLoading: setIsLoading,
956
955
  setError,