ai 0.0.0-e27b4ed4-20240419203611

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 (56) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +37 -0
  3. package/dist/index.d.mts +1770 -0
  4. package/dist/index.d.ts +1770 -0
  5. package/dist/index.js +2958 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/index.mjs +2887 -0
  8. package/dist/index.mjs.map +1 -0
  9. package/package.json +174 -0
  10. package/prompts/dist/index.d.mts +267 -0
  11. package/prompts/dist/index.d.ts +267 -0
  12. package/prompts/dist/index.js +178 -0
  13. package/prompts/dist/index.js.map +1 -0
  14. package/prompts/dist/index.mjs +146 -0
  15. package/prompts/dist/index.mjs.map +1 -0
  16. package/react/dist/index.d.mts +487 -0
  17. package/react/dist/index.d.ts +504 -0
  18. package/react/dist/index.js +1310 -0
  19. package/react/dist/index.js.map +1 -0
  20. package/react/dist/index.mjs +1271 -0
  21. package/react/dist/index.mjs.map +1 -0
  22. package/react/dist/index.server.d.mts +17 -0
  23. package/react/dist/index.server.d.ts +17 -0
  24. package/react/dist/index.server.js +50 -0
  25. package/react/dist/index.server.js.map +1 -0
  26. package/react/dist/index.server.mjs +23 -0
  27. package/react/dist/index.server.mjs.map +1 -0
  28. package/rsc/dist/index.d.ts +289 -0
  29. package/rsc/dist/index.mjs +18 -0
  30. package/rsc/dist/rsc-client.d.mts +1 -0
  31. package/rsc/dist/rsc-client.mjs +18 -0
  32. package/rsc/dist/rsc-client.mjs.map +1 -0
  33. package/rsc/dist/rsc-server.d.mts +225 -0
  34. package/rsc/dist/rsc-server.mjs +1246 -0
  35. package/rsc/dist/rsc-server.mjs.map +1 -0
  36. package/rsc/dist/rsc-shared.d.mts +94 -0
  37. package/rsc/dist/rsc-shared.mjs +346 -0
  38. package/rsc/dist/rsc-shared.mjs.map +1 -0
  39. package/solid/dist/index.d.mts +351 -0
  40. package/solid/dist/index.d.ts +351 -0
  41. package/solid/dist/index.js +1002 -0
  42. package/solid/dist/index.js.map +1 -0
  43. package/solid/dist/index.mjs +974 -0
  44. package/solid/dist/index.mjs.map +1 -0
  45. package/svelte/dist/index.d.mts +348 -0
  46. package/svelte/dist/index.d.ts +348 -0
  47. package/svelte/dist/index.js +1556 -0
  48. package/svelte/dist/index.js.map +1 -0
  49. package/svelte/dist/index.mjs +1528 -0
  50. package/svelte/dist/index.mjs.map +1 -0
  51. package/vue/dist/index.d.mts +345 -0
  52. package/vue/dist/index.d.ts +345 -0
  53. package/vue/dist/index.js +1002 -0
  54. package/vue/dist/index.js.map +1 -0
  55. package/vue/dist/index.mjs +964 -0
  56. package/vue/dist/index.mjs.map +1 -0
@@ -0,0 +1,18 @@
1
+ // rsc/rsc-client.ts
2
+ import {
3
+ readStreamableValue,
4
+ useStreamableValue,
5
+ useUIState,
6
+ useAIState,
7
+ useActions,
8
+ useSyncUIState
9
+ } from "./rsc-shared.mjs";
10
+ export {
11
+ readStreamableValue,
12
+ useAIState,
13
+ useActions,
14
+ useStreamableValue,
15
+ useSyncUIState,
16
+ useUIState
17
+ };
18
+ //# sourceMappingURL=rsc-client.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../rsc-client.ts"],"sourcesContent":["export {\n readStreamableValue,\n useStreamableValue,\n useUIState,\n useAIState,\n useActions,\n useSyncUIState,\n} from './rsc-shared.mjs';\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;","names":[]}
@@ -0,0 +1,225 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+ import OpenAI from 'openai';
4
+ import { z } from 'zod';
5
+
6
+ type AIAction<T = any, R = any> = (...args: T[]) => Promise<R>;
7
+ type AIActions<T = any, R = any> = Record<string, AIAction<T, R>>;
8
+ type AIProviderProps<AIState = any, UIState = any, Actions = any> = {
9
+ children: React.ReactNode;
10
+ initialAIState?: AIState;
11
+ initialUIState?: UIState;
12
+ /** $ActionTypes is only added for type inference and is never used at runtime **/
13
+ $ActionTypes?: Actions;
14
+ };
15
+ type AIProvider<AIState = any, UIState = any, Actions = any> = (props: AIProviderProps<AIState, UIState, Actions>) => Promise<React.ReactElement>;
16
+ type InferAIState<T, Fallback> = T extends AIProvider<infer AIState, any, any> ? AIState : Fallback;
17
+ type OnSetAIState<S> = ({ key, state, done, }: {
18
+ key: string | number | symbol | undefined;
19
+ state: S;
20
+ done: boolean;
21
+ }) => void | Promise<void>;
22
+ type OnGetUIState<S> = AIAction<void, S | undefined>;
23
+ type ValueOrUpdater<T> = T | ((current: T) => T);
24
+ type MutableAIState<AIState> = {
25
+ get: () => AIState;
26
+ update: (newState: ValueOrUpdater<AIState>) => void;
27
+ done: ((newState: AIState) => void) | (() => void);
28
+ };
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` or `useStreamableValue` APIs.
32
+ */
33
+ type StreamableValue<T = any, E = any> = {};
34
+
35
+ /**
36
+ * Get the current AI state.
37
+ * If `key` is provided, it will return the value of the specified key in the
38
+ * AI state, if it's an object. If it's not an object, it will throw an error.
39
+ *
40
+ * @example const state = getAIState() // Get the entire AI state
41
+ * @example const field = getAIState('key') // Get the value of the key
42
+ */
43
+ declare function getAIState<AI extends AIProvider = any>(): InferAIState<AI, any>;
44
+ declare function getAIState<AI extends AIProvider = any>(key: keyof InferAIState<AI, any>): InferAIState<AI, any>[typeof key];
45
+ /**
46
+ * Get the mutable AI state. Note that you must call `.close()` when finishing
47
+ * updating the AI state.
48
+ *
49
+ * @example
50
+ * ```tsx
51
+ * const state = getMutableAIState()
52
+ * state.update({ ...state.get(), key: 'value' })
53
+ * state.update((currentState) => ({ ...currentState, key: 'value' }))
54
+ * state.done()
55
+ * ```
56
+ *
57
+ * @example
58
+ * ```tsx
59
+ * const state = getMutableAIState()
60
+ * state.done({ ...state.get(), key: 'value' }) // Done with a new state
61
+ * ```
62
+ */
63
+ declare function getMutableAIState<AI extends AIProvider = any>(): MutableAIState<InferAIState<AI, any>>;
64
+ declare function getMutableAIState<AI extends AIProvider = any>(key: keyof InferAIState<AI, any>): MutableAIState<InferAIState<AI, any>[typeof key]>;
65
+
66
+ /**
67
+ * Create a piece of changable UI that can be streamed to the client.
68
+ * On the client side, it can be rendered as a normal React node.
69
+ */
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
+ 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
+ */
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
+ */
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
+ */
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
+ */
107
+ done(...args: [] | [React.ReactNode]): void;
108
+ };
109
+ /**
110
+ * Create a wrapped, changable value that can be streamed to the client.
111
+ * On the client side, the value can be accessed via the readStreamableValue() API.
112
+ */
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` or `useStreamableValue` APIs.
118
+ */
119
+ readonly value: StreamableValue<T, E>;
120
+ /**
121
+ * This method updates the current value with a new one.
122
+ */
123
+ update(value: T): void;
124
+ error(error: any): void;
125
+ done(...args: [
126
+ ] | [T]): void;
127
+ };
128
+ type Streamable = ReactNode | Promise<ReactNode>;
129
+ type Renderer<T> = (props: T) => Streamable | Generator<Streamable, Streamable, void> | AsyncGenerator<Streamable, Streamable, void>;
130
+ /**
131
+ * `render` is a helper function to create a streamable UI from some LLMs.
132
+ * Currently, it only supports OpenAI's GPT models with Function Calling and Assistants Tools.
133
+ */
134
+ declare function render<TS extends {
135
+ [name: string]: z.Schema;
136
+ } = {}, FS extends {
137
+ [name: string]: z.Schema;
138
+ } = {}>(options: {
139
+ /**
140
+ * The model name to use. Must be OpenAI SDK compatible. Tools and Functions are only supported
141
+ * GPT models (3.5/4), OpenAI Assistants, Mistral small and large, and Fireworks firefunction-v1.
142
+ *
143
+ * @example "gpt-3.5-turbo"
144
+ */
145
+ model: string;
146
+ /**
147
+ * The provider instance to use. Currently the only provider available is OpenAI.
148
+ * This needs to match the model name.
149
+ */
150
+ provider: OpenAI;
151
+ messages: Parameters<typeof OpenAI.prototype.chat.completions.create>[0]['messages'];
152
+ text?: Renderer<{
153
+ /**
154
+ * The full text content from the model so far.
155
+ */
156
+ content: string;
157
+ /**
158
+ * The new appended text content from the model since the last `text` call.
159
+ */
160
+ delta: string;
161
+ /**
162
+ * Whether the model is done generating text.
163
+ * If `true`, the `content` will be the final output and this call will be the last.
164
+ */
165
+ done: boolean;
166
+ }>;
167
+ tools?: {
168
+ [name in keyof TS]: {
169
+ description?: string;
170
+ parameters: TS[name];
171
+ render: Renderer<z.infer<TS[name]>>;
172
+ };
173
+ };
174
+ functions?: {
175
+ [name in keyof FS]: {
176
+ description?: string;
177
+ parameters: FS[name];
178
+ render: Renderer<z.infer<FS[name]>>;
179
+ };
180
+ };
181
+ initial?: ReactNode;
182
+ temperature?: number;
183
+ }): ReactNode;
184
+
185
+ declare function createAI<AIState = any, UIState = any, Actions extends AIActions = {}>({ actions, initialAIState, initialUIState, onSetAIState, onGetUIState, }: {
186
+ actions: Actions;
187
+ initialAIState?: AIState;
188
+ initialUIState?: UIState;
189
+ /**
190
+ * This function is called whenever the AI state is updated by an Action.
191
+ * You can use this to persist the AI state to a database, or to send it to a
192
+ * logging service.
193
+ */
194
+ onSetAIState?: OnSetAIState<AIState>;
195
+ /**
196
+ * This function is used to retrieve the UI state based on the AI state.
197
+ * For example, to render the initial UI state based on a given AI state, or
198
+ * to sync the UI state when the application is already loaded.
199
+ *
200
+ * If returning `undefined`, the client side UI state will not be updated.
201
+ *
202
+ * This function must be annotated with the `"use server"` directive.
203
+ *
204
+ * @example
205
+ * ```tsx
206
+ * onGetUIState: async () => {
207
+ * 'use server';
208
+ *
209
+ * const currentAIState = getAIState();
210
+ * const externalAIState = await loadAIStateFromDatabase();
211
+ *
212
+ * if (currentAIState === externalAIState) return undefined;
213
+ *
214
+ * // Update current AI state and return the new UI state
215
+ * const state = getMutableAIState()
216
+ * state.done(externalAIState)
217
+ *
218
+ * return <div>...</div>;
219
+ * }
220
+ * ```
221
+ */
222
+ onGetUIState?: OnGetUIState<UIState>;
223
+ }): AIProvider<AIState, UIState, Actions>;
224
+
225
+ export { createAI, createStreamableUI, createStreamableValue, getAIState, getMutableAIState, render };