ai 3.0.13 → 3.0.15

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 (54) hide show
  1. package/dist/index.d.mts +998 -2
  2. package/dist/index.d.ts +998 -2
  3. package/dist/index.js +1743 -15
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +1720 -15
  6. package/dist/index.mjs.map +1 -1
  7. package/mistral/dist/index.d.mts +371 -0
  8. package/mistral/dist/index.d.ts +371 -0
  9. package/mistral/dist/index.js +936 -0
  10. package/mistral/dist/index.js.map +1 -0
  11. package/mistral/dist/index.mjs +900 -0
  12. package/mistral/dist/index.mjs.map +1 -0
  13. package/openai/dist/index.d.mts +434 -0
  14. package/openai/dist/index.d.ts +434 -0
  15. package/openai/dist/index.js +1355 -0
  16. package/openai/dist/index.js.map +1 -0
  17. package/openai/dist/index.mjs +1319 -0
  18. package/openai/dist/index.mjs.map +1 -0
  19. package/package.json +32 -6
  20. package/prompts/dist/index.d.mts +13 -1
  21. package/prompts/dist/index.d.ts +13 -1
  22. package/prompts/dist/index.js +13 -0
  23. package/prompts/dist/index.js.map +1 -1
  24. package/prompts/dist/index.mjs +12 -0
  25. package/prompts/dist/index.mjs.map +1 -1
  26. package/react/dist/index.js +35 -34
  27. package/react/dist/index.js.map +1 -1
  28. package/react/dist/index.mjs +35 -34
  29. package/react/dist/index.mjs.map +1 -1
  30. package/rsc/dist/index.d.ts +45 -8
  31. package/rsc/dist/rsc-server.d.mts +45 -8
  32. package/rsc/dist/rsc-server.mjs +67 -13
  33. package/rsc/dist/rsc-server.mjs.map +1 -1
  34. package/rsc/dist/rsc-shared.d.mts +5 -8
  35. package/rsc/dist/rsc-shared.mjs +23 -2
  36. package/rsc/dist/rsc-shared.mjs.map +1 -1
  37. package/solid/dist/index.js +29 -27
  38. package/solid/dist/index.js.map +1 -1
  39. package/solid/dist/index.mjs +29 -27
  40. package/solid/dist/index.mjs.map +1 -1
  41. package/spec/dist/index.d.mts +708 -0
  42. package/spec/dist/index.d.ts +708 -0
  43. package/spec/dist/index.js +806 -0
  44. package/spec/dist/index.js.map +1 -0
  45. package/spec/dist/index.mjs +742 -0
  46. package/spec/dist/index.mjs.map +1 -0
  47. package/svelte/dist/index.js +31 -29
  48. package/svelte/dist/index.js.map +1 -1
  49. package/svelte/dist/index.mjs +31 -29
  50. package/svelte/dist/index.mjs.map +1 -1
  51. package/vue/dist/index.js +29 -27
  52. package/vue/dist/index.js.map +1 -1
  53. package/vue/dist/index.mjs +29 -27
  54. package/vue/dist/index.mjs.map +1 -1
@@ -3,8 +3,6 @@ import { ReactNode } from 'react';
3
3
  import OpenAI from 'openai';
4
4
  import { z } from 'zod';
5
5
 
6
- declare const STREAMABLE_VALUE_TYPE: unique symbol;
7
-
8
6
  type AIAction<T = any, R = any> = (...args: T[]) => Promise<R>;
9
7
  type AIActions<T = any, R = any> = Record<string, AIAction<T, R>>;
10
8
  type AIProviderProps<AIState = any, UIState = any, Actions = any> = {
@@ -30,12 +28,11 @@ type MutableAIState<AIState> = {
30
28
  update: (newState: ValueOrUpdater<AIState>) => void;
31
29
  done: ((newState: AIState) => void) | (() => void);
32
30
  };
33
- type StreamableValue<T = any, E = any> = {
34
- type?: typeof STREAMABLE_VALUE_TYPE;
35
- curr?: T;
36
- error?: E;
37
- next?: Promise<StreamableValue<T, E>>;
38
- };
31
+ /**
32
+ * StreamableValue is a value that can be streamed over the network via AI Actions.
33
+ * To read the streamed values, use the `readStreamableValue` API.
34
+ */
35
+ type StreamableValue<T = any, E = any> = {};
39
36
 
40
37
  /**
41
38
  * Get the current AI state.
@@ -73,10 +70,42 @@ declare function getMutableAIState<AI extends AIProvider = any>(key: keyof Infer
73
70
  * On the client side, it can be rendered as a normal React node.
74
71
  */
75
72
  declare function createStreamableUI(initialValue?: React.ReactNode): {
73
+ /**
74
+ * The value of the streamable UI. This can be returned from a Server Action and received by the client.
75
+ */
76
76
  value: react_jsx_runtime.JSX.Element;
77
+ /**
78
+ * This method updates the current UI node. It takes a new UI node and replaces the old one.
79
+ */
77
80
  update(value: React.ReactNode): void;
81
+ /**
82
+ * This method is used to append a new UI node to the end of the old one.
83
+ * Once appended a new UI node, the previous UI node cannot be updated anymore.
84
+ *
85
+ * @example
86
+ * ```jsx
87
+ * const ui = createStreamableUI(<div>hello</div>)
88
+ * ui.append(<div>world</div>)
89
+ *
90
+ * // The UI node will be:
91
+ * // <>
92
+ * // <div>hello</div>
93
+ * // <div>world</div>
94
+ * // </>
95
+ * ```
96
+ */
78
97
  append(value: React.ReactNode): void;
98
+ /**
99
+ * This method is used to signal that there is an error in the UI stream.
100
+ * It will be thrown on the client side and caught by the nearest error boundary component.
101
+ */
79
102
  error(error: any): void;
103
+ /**
104
+ * This method marks the UI node as finalized. You can either call it without any parameters or with a new UI node as the final state.
105
+ * Once called, the UI node cannot be updated or appended anymore.
106
+ *
107
+ * This method is always **required** to be called, otherwise the response will be stuck in a loading state.
108
+ */
80
109
  done(...args: [] | [React.ReactNode]): void;
81
110
  };
82
111
  /**
@@ -84,7 +113,15 @@ declare function createStreamableUI(initialValue?: React.ReactNode): {
84
113
  * On the client side, the value can be accessed via the readStreamableValue() API.
85
114
  */
86
115
  declare function createStreamableValue<T = any, E = any>(initialValue?: T): {
116
+ /**
117
+ * The value of the streamable. This can be returned from a Server Action and
118
+ * received by the client. To read the streamed values, use the
119
+ * `readStreamableValue` API.
120
+ */
87
121
  readonly value: StreamableValue<T, E>;
122
+ /**
123
+ * This method updates the current value with a new one.
124
+ */
88
125
  update(value: T): void;
89
126
  error(error: any): void;
90
127
  done(...args: [
@@ -3,8 +3,6 @@ import { ReactNode } from 'react';
3
3
  import OpenAI from 'openai';
4
4
  import { z } from 'zod';
5
5
 
6
- declare const STREAMABLE_VALUE_TYPE: unique symbol;
7
-
8
6
  type AIAction<T = any, R = any> = (...args: T[]) => Promise<R>;
9
7
  type AIActions<T = any, R = any> = Record<string, AIAction<T, R>>;
10
8
  type AIProviderProps<AIState = any, UIState = any, Actions = any> = {
@@ -28,12 +26,11 @@ type MutableAIState<AIState> = {
28
26
  update: (newState: ValueOrUpdater<AIState>) => void;
29
27
  done: ((newState: AIState) => void) | (() => void);
30
28
  };
31
- type StreamableValue<T = any, E = any> = {
32
- type?: typeof STREAMABLE_VALUE_TYPE;
33
- curr?: T;
34
- error?: E;
35
- next?: Promise<StreamableValue<T, E>>;
36
- };
29
+ /**
30
+ * StreamableValue is a value that can be streamed over the network via AI Actions.
31
+ * To read the streamed values, use the `readStreamableValue` API.
32
+ */
33
+ type StreamableValue<T = any, E = any> = {};
37
34
 
38
35
  /**
39
36
  * Get the current AI state.
@@ -71,10 +68,42 @@ declare function getMutableAIState<AI extends AIProvider = any>(key: keyof Infer
71
68
  * On the client side, it can be rendered as a normal React node.
72
69
  */
73
70
  declare function createStreamableUI(initialValue?: React.ReactNode): {
71
+ /**
72
+ * The value of the streamable UI. This can be returned from a Server Action and received by the client.
73
+ */
74
74
  value: react_jsx_runtime.JSX.Element;
75
+ /**
76
+ * This method updates the current UI node. It takes a new UI node and replaces the old one.
77
+ */
75
78
  update(value: React.ReactNode): void;
79
+ /**
80
+ * This method is used to append a new UI node to the end of the old one.
81
+ * Once appended a new UI node, the previous UI node cannot be updated anymore.
82
+ *
83
+ * @example
84
+ * ```jsx
85
+ * const ui = createStreamableUI(<div>hello</div>)
86
+ * ui.append(<div>world</div>)
87
+ *
88
+ * // The UI node will be:
89
+ * // <>
90
+ * // <div>hello</div>
91
+ * // <div>world</div>
92
+ * // </>
93
+ * ```
94
+ */
76
95
  append(value: React.ReactNode): void;
96
+ /**
97
+ * This method is used to signal that there is an error in the UI stream.
98
+ * It will be thrown on the client side and caught by the nearest error boundary component.
99
+ */
77
100
  error(error: any): void;
101
+ /**
102
+ * This method marks the UI node as finalized. You can either call it without any parameters or with a new UI node as the final state.
103
+ * Once called, the UI node cannot be updated or appended anymore.
104
+ *
105
+ * This method is always **required** to be called, otherwise the response will be stuck in a loading state.
106
+ */
78
107
  done(...args: [] | [React.ReactNode]): void;
79
108
  };
80
109
  /**
@@ -82,7 +111,15 @@ declare function createStreamableUI(initialValue?: React.ReactNode): {
82
111
  * On the client side, the value can be accessed via the readStreamableValue() API.
83
112
  */
84
113
  declare function createStreamableValue<T = any, E = any>(initialValue?: T): {
114
+ /**
115
+ * The value of the streamable. This can be returned from a Server Action and
116
+ * received by the client. To read the streamed values, use the
117
+ * `readStreamableValue` API.
118
+ */
85
119
  readonly value: StreamableValue<T, E>;
120
+ /**
121
+ * This method updates the current value with a new one.
122
+ */
86
123
  update(value: T): void;
87
124
  error(error: any): void;
88
125
  done(...args: [
@@ -177,9 +177,6 @@ function getMutableAIState(...args) {
177
177
  // rsc/streamable.tsx
178
178
  import zodToJsonSchema from "zod-to-json-schema";
179
179
 
180
- // shared/utils.ts
181
- import { customAlphabet } from "nanoid/non-secure";
182
-
183
180
  // shared/stream-parts.ts
184
181
  var textStreamPart = {
185
182
  code: "0",
@@ -361,10 +358,6 @@ function formatStreamPart(type, value) {
361
358
  }
362
359
 
363
360
  // shared/utils.ts
364
- var nanoid = customAlphabet(
365
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
366
- 7
367
- );
368
361
  function createChunkDecoder(complex) {
369
362
  const decoder = new TextDecoder();
370
363
  if (!complex) {
@@ -873,7 +866,13 @@ function createStreamableUI(initialValue) {
873
866
  }
874
867
  warnUnclosedStream();
875
868
  return {
869
+ /**
870
+ * The value of the streamable UI. This can be returned from a Server Action and received by the client.
871
+ */
876
872
  value: row,
873
+ /**
874
+ * This method updates the current UI node. It takes a new UI node and replaces the old one.
875
+ */
877
876
  update(value) {
878
877
  assertStream(".update()");
879
878
  if (value === currentValue) {
@@ -887,6 +886,22 @@ function createStreamableUI(initialValue) {
887
886
  reject = resolvable.reject;
888
887
  warnUnclosedStream();
889
888
  },
889
+ /**
890
+ * This method is used to append a new UI node to the end of the old one.
891
+ * Once appended a new UI node, the previous UI node cannot be updated anymore.
892
+ *
893
+ * @example
894
+ * ```jsx
895
+ * const ui = createStreamableUI(<div>hello</div>)
896
+ * ui.append(<div>world</div>)
897
+ *
898
+ * // The UI node will be:
899
+ * // <>
900
+ * // <div>hello</div>
901
+ * // <div>world</div>
902
+ * // </>
903
+ * ```
904
+ */
890
905
  append(value) {
891
906
  assertStream(".append()");
892
907
  const resolvable = createResolvablePromise();
@@ -896,6 +911,10 @@ function createStreamableUI(initialValue) {
896
911
  reject = resolvable.reject;
897
912
  warnUnclosedStream();
898
913
  },
914
+ /**
915
+ * This method is used to signal that there is an error in the UI stream.
916
+ * It will be thrown on the client side and caught by the nearest error boundary component.
917
+ */
899
918
  error(error) {
900
919
  assertStream(".error()");
901
920
  if (warningTimeout) {
@@ -904,6 +923,12 @@ function createStreamableUI(initialValue) {
904
923
  closed = true;
905
924
  reject(error);
906
925
  },
926
+ /**
927
+ * This method marks the UI node as finalized. You can either call it without any parameters or with a new UI node as the final state.
928
+ * Once called, the UI node cannot be updated or appended anymore.
929
+ *
930
+ * This method is always **required** to be called, otherwise the response will be stuck in a loading state.
931
+ */
907
932
  done(...args) {
908
933
  assertStream(".done()");
909
934
  if (warningTimeout) {
@@ -924,6 +949,7 @@ function createStreamableValue(initialValue) {
924
949
  let currentValue = initialValue;
925
950
  let currentError;
926
951
  let currentPromise = resolvable.promise;
952
+ let currentPatchValue;
927
953
  function assertStream(method) {
928
954
  if (closed) {
929
955
  throw new Error(method + ": Value stream is already closed.");
@@ -943,25 +969,53 @@ function createStreamableValue(initialValue) {
943
969
  }
944
970
  }
945
971
  warnUnclosedStream();
946
- function createWrapped(withType) {
947
- const init = currentError === void 0 ? { curr: currentValue } : { error: currentError };
972
+ function createWrapped(initialChunk) {
973
+ let init;
974
+ if (currentError !== void 0) {
975
+ init = { error: currentError };
976
+ } else {
977
+ if (currentPatchValue && !initialChunk) {
978
+ init = { diff: currentPatchValue };
979
+ } else {
980
+ init = { curr: currentValue };
981
+ }
982
+ }
948
983
  if (currentPromise) {
949
984
  init.next = currentPromise;
950
985
  }
951
- if (withType) {
986
+ if (initialChunk) {
952
987
  init.type = STREAMABLE_VALUE_TYPE;
953
988
  }
954
989
  return init;
955
990
  }
991
+ function updateValueStates(value) {
992
+ currentPatchValue = void 0;
993
+ if (typeof value === "string") {
994
+ if (typeof currentValue === "string") {
995
+ if (value.startsWith(currentValue)) {
996
+ currentPatchValue = [0, value.slice(currentValue.length)];
997
+ }
998
+ }
999
+ }
1000
+ currentValue = value;
1001
+ }
956
1002
  return {
1003
+ /**
1004
+ * The value of the streamable. This can be returned from a Server Action and
1005
+ * received by the client. To read the streamed values, use the
1006
+ * `readStreamableValue` API.
1007
+ */
957
1008
  get value() {
958
1009
  return createWrapped(true);
959
1010
  },
1011
+ /**
1012
+ * This method updates the current value with a new one.
1013
+ */
960
1014
  update(value) {
961
1015
  assertStream(".update()");
962
1016
  const resolvePrevious = resolvable.resolve;
963
1017
  resolvable = createResolvablePromise();
964
- currentValue = value;
1018
+ updateValueStates(value);
965
1019
  currentPromise = resolvable.promise;
966
1020
  resolvePrevious(createWrapped());
967
1021
  warnUnclosedStream();
@@ -984,8 +1038,8 @@ function createStreamableValue(initialValue) {
984
1038
  closed = true;
985
1039
  currentPromise = void 0;
986
1040
  if (args.length) {
987
- currentValue = args[0];
988
- resolvable.resolve({ curr: args[0] });
1041
+ updateValueStates(args[0]);
1042
+ resolvable.resolve(createWrapped());
989
1043
  return;
990
1044
  }
991
1045
  resolvable.resolve({});