ai 3.0.21 → 3.0.23

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 (57) hide show
  1. package/dist/index.d.mts +42 -1
  2. package/dist/index.d.ts +42 -1
  3. package/dist/index.js +104 -177
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +65 -138
  6. package/dist/index.mjs.map +1 -1
  7. package/package.json +4 -33
  8. package/react/dist/index.d.mts +6 -2
  9. package/react/dist/index.d.ts +6 -2
  10. package/react/dist/index.js +107 -24
  11. package/react/dist/index.js.map +1 -1
  12. package/react/dist/index.mjs +107 -24
  13. package/react/dist/index.mjs.map +1 -1
  14. package/rsc/dist/rsc-server.mjs +3 -3
  15. package/rsc/dist/rsc-server.mjs.map +1 -1
  16. package/solid/dist/index.d.mts +6 -2
  17. package/solid/dist/index.d.ts +6 -2
  18. package/solid/dist/index.js +105 -23
  19. package/solid/dist/index.js.map +1 -1
  20. package/solid/dist/index.mjs +105 -23
  21. package/solid/dist/index.mjs.map +1 -1
  22. package/svelte/dist/index.d.mts +6 -2
  23. package/svelte/dist/index.d.ts +6 -2
  24. package/svelte/dist/index.js +107 -24
  25. package/svelte/dist/index.js.map +1 -1
  26. package/svelte/dist/index.mjs +107 -24
  27. package/svelte/dist/index.mjs.map +1 -1
  28. package/vue/dist/index.d.mts +6 -2
  29. package/vue/dist/index.d.ts +6 -2
  30. package/vue/dist/index.js +105 -23
  31. package/vue/dist/index.js.map +1 -1
  32. package/vue/dist/index.mjs +105 -23
  33. package/vue/dist/index.mjs.map +1 -1
  34. package/anthropic/dist/index.d.mts +0 -51
  35. package/anthropic/dist/index.d.ts +0 -51
  36. package/anthropic/dist/index.js +0 -792
  37. package/anthropic/dist/index.js.map +0 -1
  38. package/anthropic/dist/index.mjs +0 -760
  39. package/anthropic/dist/index.mjs.map +0 -1
  40. package/google/dist/index.d.mts +0 -47
  41. package/google/dist/index.d.ts +0 -47
  42. package/google/dist/index.js +0 -796
  43. package/google/dist/index.js.map +0 -1
  44. package/google/dist/index.mjs +0 -764
  45. package/google/dist/index.mjs.map +0 -1
  46. package/mistral/dist/index.d.mts +0 -52
  47. package/mistral/dist/index.d.ts +0 -52
  48. package/mistral/dist/index.js +0 -763
  49. package/mistral/dist/index.js.map +0 -1
  50. package/mistral/dist/index.mjs +0 -731
  51. package/mistral/dist/index.mjs.map +0 -1
  52. package/openai/dist/index.d.mts +0 -116
  53. package/openai/dist/index.d.ts +0 -116
  54. package/openai/dist/index.js +0 -1143
  55. package/openai/dist/index.js.map +0 -1
  56. package/openai/dist/index.mjs +0 -1115
  57. package/openai/dist/index.mjs.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "3.0.21",
3
+ "version": "3.0.23",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -15,8 +15,6 @@
15
15
  "prompts/dist/**/*",
16
16
  "rsc/dist/**/*",
17
17
  "google/dist/**/*",
18
- "openai/dist/**/*",
19
- "anthropic/dist/**/*",
20
18
  "mistral/dist/**/*"
21
19
  ],
22
20
  "exports": {
@@ -26,30 +24,6 @@
26
24
  "import": "./dist/index.mjs",
27
25
  "require": "./dist/index.js"
28
26
  },
29
- "./anthropic": {
30
- "types": "./anthropic/dist/index.d.ts",
31
- "import": "./anthropic/dist/index.mjs",
32
- "module": "./anthropic/dist/index.mjs",
33
- "require": "./anthropic/dist/index.js"
34
- },
35
- "./google": {
36
- "types": "./google/dist/index.d.ts",
37
- "import": "./google/dist/index.mjs",
38
- "module": "./google/dist/index.mjs",
39
- "require": "./google/dist/index.js"
40
- },
41
- "./openai": {
42
- "types": "./openai/dist/index.d.ts",
43
- "import": "./openai/dist/index.mjs",
44
- "module": "./openai/dist/index.mjs",
45
- "require": "./openai/dist/index.js"
46
- },
47
- "./mistral": {
48
- "types": "./mistral/dist/index.d.ts",
49
- "import": "./mistral/dist/index.mjs",
50
- "module": "./mistral/dist/index.mjs",
51
- "require": "./mistral/dist/index.js"
52
- },
53
27
  "./rsc": {
54
28
  "types": "./rsc/dist/index.d.ts",
55
29
  "react-server": "./rsc/dist/rsc-server.mjs",
@@ -84,6 +58,7 @@
84
58
  },
85
59
  "dependencies": {
86
60
  "@ai-sdk/provider": "0.0.0",
61
+ "@ai-sdk/provider-utils": "0.0.1",
87
62
  "secure-json-parse": "2.7.0",
88
63
  "eventsource-parser": "1.1.2",
89
64
  "jsondiffpatch": "0.6.0",
@@ -155,7 +130,7 @@
155
130
  }
156
131
  },
157
132
  "engines": {
158
- "node": ">=14.6"
133
+ "node": ">=18"
159
134
  },
160
135
  "publishConfig": {
161
136
  "access": "public"
@@ -177,7 +152,7 @@
177
152
  ],
178
153
  "scripts": {
179
154
  "build": "tsup && cat react/dist/index.server.d.ts >> react/dist/index.d.ts",
180
- "clean": "rm -rf dist && rm -rf core/dist && rm -rf google/dist && rm -rf anthropic/dist && rm -rf openai/dist && rm -rf mistral/dist && rm -rf react/dist && rm -rf svelte/dist && rm -rf vue/dist && rm -rf solid/dist && rm -rf rsc/dist",
155
+ "clean": "rm -rf dist && rm -rf react/dist && rm -rf svelte/dist && rm -rf vue/dist && rm -rf solid/dist && rm -rf rsc/dist",
181
156
  "dev": "tsup --watch",
182
157
  "lint": "eslint \"./**/*.ts*\"",
183
158
  "type-check": "tsc --noEmit",
@@ -186,10 +161,6 @@
186
161
  "test:edge": "vitest --config vitest.edge.config.js --run --threads=false",
187
162
  "test:node": "vitest --config vitest.node.config.js --run --threads=false",
188
163
  "test:node:core": "pnpm vitest --config vitest.node.config.js --run --threads=false ./core/",
189
- "test:node:anthropic": "pnpm vitest --config vitest.node.config.js --run --threads=false ./anthropic/",
190
- "test:node:google": "pnpm vitest --config vitest.node.config.js --run --threads=false ./google/",
191
- "test:node:openai": "pnpm vitest --config vitest.node.config.js --run --threads=false ./openai/",
192
- "test:node:mistral": "pnpm vitest --config vitest.node.config.js --run --threads=false ./mistral/",
193
164
  "test:ui": "pnpm test:ui:react && pnpm test:ui:vue && pnpm test:ui:solid",
194
165
  "test:ui:react": "vitest --config vitest.ui.react.config.js --run",
195
166
  "test:ui:solid": "vitest --config vitest.ui.solid.config.js --run",
@@ -202,6 +202,8 @@ type UseChatOptions = {
202
202
  * handle the extra fields before forwarding the request to the AI service.
203
203
  */
204
204
  sendExtraMessageFields?: boolean;
205
+ /** Stream mode (default to "stream-data") */
206
+ streamMode?: 'stream-data' | 'text';
205
207
  };
206
208
  type UseCompletionOptions = {
207
209
  /**
@@ -258,6 +260,8 @@ type UseCompletionOptions = {
258
260
  * ```
259
261
  */
260
262
  body?: object;
263
+ /** Stream mode (default to "stream-data") */
264
+ streamMode?: 'stream-data' | 'text';
261
265
  };
262
266
  type JSONValue = null | string | number | boolean | {
263
267
  [x: string]: JSONValue;
@@ -352,7 +356,7 @@ type StreamingReactResponseAction = (payload: {
352
356
  messages: Message[];
353
357
  data?: Record<string, string>;
354
358
  }) => Promise<experimental_StreamingReactResponse>;
355
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
359
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
356
360
  api?: string | StreamingReactResponseAction;
357
361
  key?: string;
358
362
  }): UseChatHelpers;
@@ -401,7 +405,7 @@ type UseCompletionHelpers = {
401
405
  /** Additional data added on the server via StreamData */
402
406
  data?: JSONValue[];
403
407
  };
404
- declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
408
+ declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
405
409
 
406
410
  type AssistantStatus = 'in_progress' | 'awaiting_message';
407
411
  type UseAssistantHelpers = {
@@ -202,6 +202,8 @@ type UseChatOptions = {
202
202
  * handle the extra fields before forwarding the request to the AI service.
203
203
  */
204
204
  sendExtraMessageFields?: boolean;
205
+ /** Stream mode (default to "stream-data") */
206
+ streamMode?: 'stream-data' | 'text';
205
207
  };
206
208
  type UseCompletionOptions = {
207
209
  /**
@@ -258,6 +260,8 @@ type UseCompletionOptions = {
258
260
  * ```
259
261
  */
260
262
  body?: object;
263
+ /** Stream mode (default to "stream-data") */
264
+ streamMode?: 'stream-data' | 'text';
261
265
  };
262
266
  type JSONValue = null | string | number | boolean | {
263
267
  [x: string]: JSONValue;
@@ -352,7 +356,7 @@ type StreamingReactResponseAction = (payload: {
352
356
  messages: Message[];
353
357
  data?: Record<string, string>;
354
358
  }) => Promise<experimental_StreamingReactResponse>;
355
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
359
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
356
360
  api?: string | StreamingReactResponseAction;
357
361
  key?: string;
358
362
  }): UseChatHelpers;
@@ -401,7 +405,7 @@ type UseCompletionHelpers = {
401
405
  /** Additional data added on the server via StreamData */
402
406
  data?: JSONValue[];
403
407
  };
404
- declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
408
+ declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
405
409
 
406
410
  type AssistantStatus = 'in_progress' | 'awaiting_message';
407
411
  type UseAssistantHelpers = {
@@ -142,9 +142,9 @@ var toolCallStreamPart = {
142
142
  code: "7",
143
143
  name: "tool_calls",
144
144
  parse: (value) => {
145
- 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) => {
146
- 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";
147
- })) {
145
+ 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(
146
+ (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"
147
+ )) {
148
148
  throw new Error(
149
149
  '"tool_calls" parts expect an object with a ToolCallPayload.'
150
150
  );
@@ -375,11 +375,28 @@ async function parseComplexResponse({
375
375
  };
376
376
  }
377
377
 
378
+ // shared/utils.ts
379
+ function createChunkDecoder(complex) {
380
+ const decoder = new TextDecoder();
381
+ if (!complex) {
382
+ return function(chunk) {
383
+ if (!chunk)
384
+ return "";
385
+ return decoder.decode(chunk, { stream: true });
386
+ };
387
+ }
388
+ return function(chunk) {
389
+ const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
390
+ return decoded.map(parseStreamPart).filter(Boolean);
391
+ };
392
+ }
393
+
378
394
  // shared/call-chat-api.ts
379
395
  async function callChatApi({
380
396
  api,
381
397
  messages,
382
398
  body,
399
+ streamMode = "stream-data",
383
400
  credentials,
384
401
  headers,
385
402
  abortController,
@@ -423,17 +440,52 @@ async function callChatApi({
423
440
  throw new Error("The response body is empty.");
424
441
  }
425
442
  const reader = response.body.getReader();
426
- return await parseComplexResponse({
427
- reader,
428
- abortControllerRef: abortController != null ? { current: abortController() } : void 0,
429
- update: onUpdate,
430
- onFinish(prefixMap) {
431
- if (onFinish && prefixMap.text != null) {
432
- onFinish(prefixMap.text);
443
+ switch (streamMode) {
444
+ case "text": {
445
+ const decoder = createChunkDecoder();
446
+ const resultMessage = {
447
+ id: generateId2(),
448
+ createdAt: /* @__PURE__ */ new Date(),
449
+ role: "assistant",
450
+ content: ""
451
+ };
452
+ while (true) {
453
+ const { done, value } = await reader.read();
454
+ if (done) {
455
+ break;
456
+ }
457
+ resultMessage.content += decoder(value);
458
+ resultMessage.id = generateId2();
459
+ onUpdate([{ ...resultMessage }], []);
460
+ if ((abortController == null ? void 0 : abortController()) === null) {
461
+ reader.cancel();
462
+ break;
463
+ }
433
464
  }
434
- },
435
- generateId: generateId2
436
- });
465
+ onFinish == null ? void 0 : onFinish(resultMessage);
466
+ return {
467
+ messages: [resultMessage],
468
+ data: []
469
+ };
470
+ }
471
+ case "stream-data": {
472
+ return await parseComplexResponse({
473
+ reader,
474
+ abortControllerRef: abortController != null ? { current: abortController() } : void 0,
475
+ update: onUpdate,
476
+ onFinish(prefixMap) {
477
+ if (onFinish && prefixMap.text != null) {
478
+ onFinish(prefixMap.text);
479
+ }
480
+ },
481
+ generateId: generateId2
482
+ });
483
+ }
484
+ default: {
485
+ const exhaustiveCheck = streamMode;
486
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
487
+ }
488
+ }
437
489
  }
438
490
 
439
491
  // shared/process-chat-stream.ts
@@ -553,7 +605,7 @@ async function processChatStream({
553
605
  }
554
606
 
555
607
  // react/use-chat.ts
556
- var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId2, onFinish, onResponse, sendExtraMessageFields) => {
608
+ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId2, streamMode, onFinish, onResponse, sendExtraMessageFields) => {
557
609
  var _a, _b;
558
610
  const previousMessages = messagesRef.current;
559
611
  mutate(chatRequest.messages, false);
@@ -624,6 +676,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
624
676
  tool_choice: chatRequest.tool_choice
625
677
  }
626
678
  },
679
+ streamMode,
627
680
  credentials: extraMetadataRef.current.credentials,
628
681
  headers: {
629
682
  ...extraMetadataRef.current.headers,
@@ -650,6 +703,7 @@ function useChat({
650
703
  sendExtraMessageFields,
651
704
  experimental_onFunctionCall,
652
705
  experimental_onToolCall,
706
+ streamMode,
653
707
  onResponse,
654
708
  onFinish,
655
709
  onError,
@@ -708,6 +762,7 @@ function useChat({
708
762
  messagesRef,
709
763
  abortControllerRef,
710
764
  generateId2,
765
+ streamMode,
711
766
  onFinish,
712
767
  onResponse,
713
768
  sendExtraMessageFields
@@ -878,6 +933,7 @@ async function callCompletionApi({
878
933
  credentials,
879
934
  headers,
880
935
  body,
936
+ streamMode = "stream-data",
881
937
  setCompletion,
882
938
  setLoading,
883
939
  setError,
@@ -925,19 +981,44 @@ async function callCompletionApi({
925
981
  }
926
982
  let result = "";
927
983
  const reader = res.body.getReader();
928
- for await (const { type, value } of readDataStream(reader, {
929
- isAborted: () => abortController === null
930
- })) {
931
- switch (type) {
932
- case "text": {
933
- result += value;
984
+ switch (streamMode) {
985
+ case "text": {
986
+ const decoder = createChunkDecoder();
987
+ while (true) {
988
+ const { done, value } = await reader.read();
989
+ if (done) {
990
+ break;
991
+ }
992
+ result += decoder(value);
934
993
  setCompletion(result);
935
- break;
994
+ if (abortController === null) {
995
+ reader.cancel();
996
+ break;
997
+ }
936
998
  }
937
- case "data": {
938
- onData == null ? void 0 : onData(value);
939
- break;
999
+ break;
1000
+ }
1001
+ case "stream-data": {
1002
+ for await (const { type, value } of readDataStream(reader, {
1003
+ isAborted: () => abortController === null
1004
+ })) {
1005
+ switch (type) {
1006
+ case "text": {
1007
+ result += value;
1008
+ setCompletion(result);
1009
+ break;
1010
+ }
1011
+ case "data": {
1012
+ onData == null ? void 0 : onData(value);
1013
+ break;
1014
+ }
1015
+ }
940
1016
  }
1017
+ break;
1018
+ }
1019
+ default: {
1020
+ const exhaustiveCheck = streamMode;
1021
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
941
1022
  }
942
1023
  }
943
1024
  if (onFinish) {
@@ -970,6 +1051,7 @@ function useCompletion({
970
1051
  credentials,
971
1052
  headers,
972
1053
  body,
1054
+ streamMode,
973
1055
  onResponse,
974
1056
  onFinish,
975
1057
  onError
@@ -1009,6 +1091,7 @@ function useCompletion({
1009
1091
  ...extraMetadataRef.current.body,
1010
1092
  ...options == null ? void 0 : options.body
1011
1093
  },
1094
+ streamMode,
1012
1095
  setCompletion: (completion2) => mutate(completion2, false),
1013
1096
  setLoading: mutateLoading,
1014
1097
  setError,