ai 2.2.37 → 3.0.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "2.2.37",
3
+ "version": "3.0.0",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -12,7 +12,8 @@
12
12
  "svelte/dist/**/*",
13
13
  "vue/dist/**/*",
14
14
  "solid/dist/**/*",
15
- "prompts/dist/**/*"
15
+ "prompts/dist/**/*",
16
+ "rsc/dist/**/*"
16
17
  ],
17
18
  "exports": {
18
19
  "./package.json": "./package.json",
@@ -22,6 +23,12 @@
22
23
  "module": "./dist/index.mjs",
23
24
  "require": "./dist/index.js"
24
25
  },
26
+ "./rsc": {
27
+ "types": "./rsc/dist/rsc-types.d.ts",
28
+ "react-server": "./rsc/dist/rsc-server.mjs",
29
+ "import": "./rsc/dist/rsc-client.mjs",
30
+ "module": "./rsc/dist/rsc-client.mjs"
31
+ },
25
32
  "./prompts": {
26
33
  "types": "./prompts/dist/index.d.ts",
27
34
  "import": "./prompts/dist/index.mjs",
@@ -56,12 +63,14 @@
56
63
  },
57
64
  "dependencies": {
58
65
  "eventsource-parser": "1.0.0",
66
+ "jsondiffpatch": "^0.6.0",
59
67
  "nanoid": "3.3.6",
60
68
  "solid-swr-store": "0.10.7",
61
69
  "sswr": "2.0.0",
62
70
  "swr": "2.2.0",
63
71
  "swr-store": "0.10.6",
64
- "swrv": "1.0.4"
72
+ "swrv": "1.0.4",
73
+ "zod-to-json-schema": "^3.22.4"
65
74
  },
66
75
  "devDependencies": {
67
76
  "@anthropic-ai/sdk": "0.12.0",
@@ -91,14 +100,15 @@
91
100
  "tsup": "^6.7.0",
92
101
  "typescript": "5.1.3",
93
102
  "vite-plugin-solid": "2.7.2",
94
- "@vercel/ai-tsconfig": "0.0.0",
95
- "eslint-config-vercel-ai": "0.0.0"
103
+ "eslint-config-vercel-ai": "0.0.0",
104
+ "@vercel/ai-tsconfig": "0.0.0"
96
105
  },
97
106
  "peerDependencies": {
98
107
  "react": "^18.2.0",
99
108
  "solid-js": "^1.7.7",
100
109
  "svelte": "^3.0.0 || ^4.0.0",
101
- "vue": "^3.3.4"
110
+ "vue": "^3.3.4",
111
+ "zod": "^3.0.0"
102
112
  },
103
113
  "peerDependenciesMeta": {
104
114
  "react": {
@@ -112,6 +122,9 @@
112
122
  },
113
123
  "solid-js": {
114
124
  "optional": true
125
+ },
126
+ "zod": {
127
+ "optional": true
115
128
  }
116
129
  },
117
130
  "engines": {
@@ -137,7 +150,7 @@
137
150
  ],
138
151
  "scripts": {
139
152
  "build": "tsup && cat react/dist/index.server.d.ts >> react/dist/index.d.ts",
140
- "clean": "rm -rf dist && rm -rf react/dist && rm -rf svelte/dist && rm -rf vue/dist && rm -rf solid/dist",
153
+ "clean": "rm -rf dist && rm -rf react/dist && rm -rf svelte/dist && rm -rf vue/dist && rm -rf solid/dist && rm -rf rsc/dist",
141
154
  "dev": "tsup --watch",
142
155
  "lint": "eslint \"./**/*.ts*\"",
143
156
  "type-check": "tsc --noEmit",
@@ -0,0 +1 @@
1
+ export { useAIState, useActions, useStreamableValue, useSyncUIState, useUIState } from './rsc-shared';
@@ -0,0 +1,16 @@
1
+ // rsc/rsc-client.ts
2
+ import {
3
+ useStreamableValue,
4
+ useUIState,
5
+ useAIState,
6
+ useActions,
7
+ useSyncUIState
8
+ } from "./rsc-shared";
9
+ export {
10
+ useAIState,
11
+ useActions,
12
+ useStreamableValue,
13
+ useSyncUIState,
14
+ useUIState
15
+ };
16
+ //# sourceMappingURL=rsc-client.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../rsc-client.ts"],"sourcesContent":["export {\n useStreamableValue,\n useUIState,\n useAIState,\n useActions,\n useSyncUIState,\n} from './rsc-shared';\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;","names":[]}
@@ -0,0 +1,143 @@
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
+ import './rsc-shared';
6
+
7
+ type AIAction<T = any, R = any> = (...args: T[]) => Promise<R>;
8
+ type AIActions<T = any, R = any> = Record<string, AIAction<T, R>>;
9
+ type AIProviderProps<AIState = any, UIState = any> = {
10
+ children: React.ReactNode;
11
+ initialAIState?: AIState;
12
+ initialUIState?: UIState;
13
+ };
14
+ type AIProvider<AIState = any, UIState = any, Actions = any> = (props: AIProviderProps<AIState, UIState>) => Promise<React.ReactElement>;
15
+ type InferAIState<T, Fallback> = T extends AIProvider<infer AIState, any, any> ? AIState : Fallback;
16
+ type OnSetAIState<S> = ({ key, state, done, }: {
17
+ key: string | number | symbol | undefined;
18
+ state: S;
19
+ done: boolean;
20
+ }) => void | Promise<void>;
21
+ type OnGetUIState<S> = AIAction<void, S | undefined>;
22
+ type ValueOrUpdater<T> = T | ((current: T) => T);
23
+ type MutableAIState<AIState> = {
24
+ get: () => AIState;
25
+ update: (newState: ValueOrUpdater<AIState>) => void;
26
+ done: ((newState: AIState) => void) | (() => void);
27
+ };
28
+
29
+ /**
30
+ * Get the current AI state.
31
+ * If `key` is provided, it will return the value of the specified key in the
32
+ * AI state, if it's an object. If it's not an object, it will throw an error.
33
+ *
34
+ * @example const state = getAIState() // Get the entire AI state
35
+ * @example const field = getAIState('key') // Get the value of the key
36
+ */
37
+ declare function getAIState<AI extends AIProvider = any>(): InferAIState<AI, any>;
38
+ declare function getAIState<AI extends AIProvider = any>(key: keyof InferAIState<AI, any>): InferAIState<AI, any>[typeof key];
39
+ /**
40
+ * Get the mutable AI state. Note that you must call `.close()` when finishing
41
+ * updating the AI state.
42
+ *
43
+ * @example
44
+ * ```tsx
45
+ * const state = getMutableAIState()
46
+ * state.update({ ...state.get(), key: 'value' })
47
+ * state.update((currentState) => ({ ...currentState, key: 'value' }))
48
+ * state.done()
49
+ * ```
50
+ *
51
+ * @example
52
+ * ```tsx
53
+ * const state = getMutableAIState()
54
+ * state.done({ ...state.get(), key: 'value' }) // Done with a new state
55
+ * ```
56
+ */
57
+ declare function getMutableAIState<AI extends AIProvider = any>(): MutableAIState<InferAIState<AI, any>>;
58
+ declare function getMutableAIState<AI extends AIProvider = any>(key: keyof InferAIState<AI, any>): MutableAIState<InferAIState<AI, any>[typeof key]>;
59
+
60
+ /**
61
+ * Create a piece of changable UI that can be streamed to the client.
62
+ * On the client side, it can be rendered as a normal React node.
63
+ */
64
+ declare function createStreamableUI(initialValue?: React.ReactNode): {
65
+ value: react_jsx_runtime.JSX.Element;
66
+ update(value: React.ReactNode): void;
67
+ append(value: React.ReactNode): void;
68
+ error(error: any): void;
69
+ done(...args: any): void;
70
+ };
71
+ /**
72
+ * Create a wrapped, changable value that can be streamed to the client.
73
+ * On the client side, the value can be accessed via the useStreamableValue() hook.
74
+ */
75
+ declare function createStreamableValue<T = any>(initialValue?: T): {
76
+ value: {
77
+ type: symbol;
78
+ curr: T | undefined;
79
+ next: Promise<unknown>;
80
+ } | {
81
+ curr: T | undefined;
82
+ next: Promise<unknown>;
83
+ type?: undefined;
84
+ };
85
+ update(value: T): void;
86
+ error(error: any): void;
87
+ done(...args: any): void;
88
+ };
89
+ type Streamable = ReactNode | Promise<ReactNode>;
90
+ type Renderer<T> = (props: T) => Streamable | Generator<Streamable, Streamable, void> | AsyncGenerator<Streamable, Streamable, void>;
91
+ /**
92
+ * `render` is a helper function to create a streamable UI from some LLMs.
93
+ * Currently, it only supports OpenAI's GPT models with Function Calling and Assistants Tools.
94
+ */
95
+ declare function render<TS extends {
96
+ [name: string]: z.Schema;
97
+ } = {}, FS extends {
98
+ [name: string]: z.Schema;
99
+ } = {}>(options: {
100
+ /**
101
+ * The model name to use. Currently the only models available are OpenAI's
102
+ * GPT models (3.5/4) with Function Calling and Assistants Tools.
103
+ *
104
+ * @example "gpt-3.5-turbo"
105
+ */
106
+ model: `gpt-${string}`;
107
+ /**
108
+ * The provider instance to use. Currently the only provider available is OpenAI.
109
+ * This needs to match the model name.
110
+ */
111
+ provider: OpenAI;
112
+ messages: Parameters<typeof OpenAI.prototype.chat.completions.create>[0]['messages'];
113
+ text?: Renderer<{
114
+ content: string;
115
+ done: boolean;
116
+ }>;
117
+ tools?: {
118
+ [name in keyof TS]: {
119
+ description?: string;
120
+ parameters: TS[name];
121
+ render: Renderer<z.infer<TS[name]>>;
122
+ };
123
+ };
124
+ functions?: {
125
+ [name in keyof FS]: {
126
+ description?: string;
127
+ parameters: FS[name];
128
+ render: Renderer<z.infer<FS[name]>>;
129
+ };
130
+ };
131
+ initial?: ReactNode;
132
+ temperature?: number;
133
+ }): ReactNode;
134
+
135
+ declare function createAI<AIState = any, UIState = any, Actions extends AIActions = {}>({ actions, initialAIState, initialUIState, unstable_onSetAIState: onSetAIState, unstable_onGetUIState: onGetUIState, }: {
136
+ actions: Actions;
137
+ initialAIState?: AIState;
138
+ initialUIState?: UIState;
139
+ unstable_onSetAIState?: OnSetAIState<AIState>;
140
+ unstable_onGetUIState?: OnGetUIState<UIState>;
141
+ }): AIProvider<AIState, UIState, Actions>;
142
+
143
+ export { createAI, createStreamableUI, createStreamableValue, getAIState, getMutableAIState, render };