ai 2.1.23 → 2.1.24

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.1.23",
3
+ "version": "2.1.24",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -11,6 +11,7 @@
11
11
  "react/dist/**/*",
12
12
  "svelte/dist/**/*",
13
13
  "vue/dist/**/*",
14
+ "solid/dist/**/*",
14
15
  "prompts/dist/**/*"
15
16
  ],
16
17
  "exports": {
@@ -45,6 +46,12 @@
45
46
  "import": "./vue/dist/index.mjs",
46
47
  "module": "./vue/dist/index.mjs",
47
48
  "require": "./vue/dist/index.js"
49
+ },
50
+ "./solid": {
51
+ "types": "./solid/dist/index.d.ts",
52
+ "import": "./solid/dist/index.mjs",
53
+ "module": "./solid/dist/index.mjs",
54
+ "require": "./solid/dist/index.js"
48
55
  }
49
56
  },
50
57
  "jest": {
@@ -53,10 +60,12 @@
53
60
  },
54
61
  "dependencies": {
55
62
  "eventsource-parser": "1.0.0",
56
- "swr": "2.1.5",
57
- "nanoid": "^3.3.6",
58
- "sswr": "^1.10.0",
59
- "swrv": "1.0.3"
63
+ "nanoid": "3.3.6",
64
+ "sswr": "2.0.0",
65
+ "swr": "2.2.0",
66
+ "swrv": "1.0.4",
67
+ "solid-swr-store": "0.10.7",
68
+ "swr-store": "0.10.6"
60
69
  },
61
70
  "devDependencies": {
62
71
  "@edge-runtime/jest-environment": "1.1.0-beta.31",
@@ -75,6 +84,7 @@
75
84
  },
76
85
  "peerDependencies": {
77
86
  "react": "^18.2.0",
87
+ "solid-js": "^1.7.7",
78
88
  "svelte": "^3.0.0 || ^4.0.0",
79
89
  "vue": "^3.3.4"
80
90
  },
@@ -87,6 +97,9 @@
87
97
  },
88
98
  "vue": {
89
99
  "optional": true
100
+ },
101
+ "solid-js": {
102
+ "optional": true
90
103
  }
91
104
  },
92
105
  "engines": {
@@ -112,7 +125,7 @@
112
125
  ],
113
126
  "scripts": {
114
127
  "build": "tsup && cat react/dist/index.server.d.ts >> react/dist/index.d.ts",
115
- "clean": "rm -rf dist && rm -rf react/dist && rm -rf svelte/dist && rm -rf vue/dist",
128
+ "clean": "rm -rf dist && rm -rf react/dist && rm -rf svelte/dist && rm -rf vue/dist && rm -rf solid/dist",
116
129
  "dev": "tsup --watch",
117
130
  "lint": "eslint \"./**/*.ts*\"",
118
131
  "type-check": "tsc --noEmit",
@@ -0,0 +1,239 @@
1
+ import { Resource, Accessor, Setter } from 'solid-js';
2
+ import { ChatCompletionRequestMessageFunctionCall, CreateChatCompletionRequestFunctionCall } from 'openai-edge';
3
+ import { ChatCompletionFunctions } from 'openai-edge/types/api';
4
+
5
+ /**
6
+ * Shared types between the API and UI packages.
7
+ */
8
+ type Message = {
9
+ id: string;
10
+ createdAt?: Date;
11
+ content: string;
12
+ role: 'system' | 'user' | 'assistant' | 'function';
13
+ /**
14
+ * If the message has a role of `function`, the `name` field is the name of the function.
15
+ * Otherwise, the name field should not be set.
16
+ */
17
+ name?: string;
18
+ /**
19
+ * If the assistant role makes a function call, the `function_call` field
20
+ * contains the function call name and arguments. Otherwise, the field should
21
+ * not be set.
22
+ */
23
+ function_call?: string | ChatCompletionRequestMessageFunctionCall;
24
+ };
25
+ type CreateMessage = Omit<Message, 'id'> & {
26
+ id?: Message['id'];
27
+ };
28
+ type ChatRequest = {
29
+ messages: Message[];
30
+ options?: RequestOptions;
31
+ functions?: Array<ChatCompletionFunctions>;
32
+ function_call?: CreateChatCompletionRequestFunctionCall;
33
+ };
34
+ type FunctionCallHandler = (chatMessages: Message[], functionCall: ChatCompletionRequestMessageFunctionCall) => Promise<ChatRequest | void>;
35
+ type RequestOptions = {
36
+ headers?: Record<string, string> | Headers;
37
+ body?: object;
38
+ };
39
+ type UseChatOptions = {
40
+ /**
41
+ * The API endpoint that accepts a `{ messages: Message[] }` object and returns
42
+ * a stream of tokens of the AI chat response. Defaults to `/api/chat`.
43
+ */
44
+ api?: string;
45
+ /**
46
+ * A unique identifier for the chat. If not provided, a random one will be
47
+ * generated. When provided, the `useChat` hook with the same `id` will
48
+ * have shared states across components.
49
+ */
50
+ id?: string;
51
+ /**
52
+ * Initial messages of the chat. Useful to load an existing chat history.
53
+ */
54
+ initialMessages?: Message[];
55
+ /**
56
+ * Initial input of the chat.
57
+ */
58
+ initialInput?: string;
59
+ /**
60
+ * Callback function to be called when a function call is received.
61
+ * If the function returns a `ChatRequest` object, the request will be sent
62
+ * automatically to the API and will be used to update the chat.
63
+ */
64
+ experimental_onFunctionCall?: FunctionCallHandler;
65
+ /**
66
+ * Callback function to be called when the API response is received.
67
+ */
68
+ onResponse?: (response: Response) => void | Promise<void>;
69
+ /**
70
+ * Callback function to be called when the chat is finished streaming.
71
+ */
72
+ onFinish?: (message: Message) => void;
73
+ /**
74
+ * Callback function to be called when an error is encountered.
75
+ */
76
+ onError?: (error: Error) => void;
77
+ /**
78
+ * The credentials mode to be used for the fetch request.
79
+ * Possible values are: 'omit', 'same-origin', 'include'.
80
+ * Defaults to 'same-origin'.
81
+ */
82
+ credentials?: RequestCredentials;
83
+ /**
84
+ * HTTP headers to be sent with the API request.
85
+ */
86
+ headers?: Record<string, string> | Headers;
87
+ /**
88
+ * Extra body object to be sent with the API request.
89
+ * @example
90
+ * Send a `sessionId` to the API along with the messages.
91
+ * ```js
92
+ * useChat({
93
+ * body: {
94
+ * sessionId: '123',
95
+ * }
96
+ * })
97
+ * ```
98
+ */
99
+ body?: object;
100
+ /**
101
+ * Whether to send extra message fields such as `message.id` and `message.createdAt` to the API.
102
+ * Defaults to `false`. When set to `true`, the API endpoint might need to
103
+ * handle the extra fields before forwarding the request to the AI service.
104
+ */
105
+ sendExtraMessageFields?: boolean;
106
+ };
107
+ type UseCompletionOptions = {
108
+ /**
109
+ * The API endpoint that accepts a `{ prompt: string }` object and returns
110
+ * a stream of tokens of the AI completion response. Defaults to `/api/completion`.
111
+ */
112
+ api?: string;
113
+ /**
114
+ * An unique identifier for the chat. If not provided, a random one will be
115
+ * generated. When provided, the `useChat` hook with the same `id` will
116
+ * have shared states across components.
117
+ */
118
+ id?: string;
119
+ /**
120
+ * Initial prompt input of the completion.
121
+ */
122
+ initialInput?: string;
123
+ /**
124
+ * Initial completion result. Useful to load an existing history.
125
+ */
126
+ initialCompletion?: string;
127
+ /**
128
+ * Callback function to be called when the API response is received.
129
+ */
130
+ onResponse?: (response: Response) => void | Promise<void>;
131
+ /**
132
+ * Callback function to be called when the completion is finished streaming.
133
+ */
134
+ onFinish?: (prompt: string, completion: string) => void;
135
+ /**
136
+ * Callback function to be called when an error is encountered.
137
+ */
138
+ onError?: (error: Error) => void;
139
+ /**
140
+ * The credentials mode to be used for the fetch request.
141
+ * Possible values are: 'omit', 'same-origin', 'include'.
142
+ * Defaults to 'same-origin'.
143
+ */
144
+ credentials?: RequestCredentials;
145
+ /**
146
+ * HTTP headers to be sent with the API request.
147
+ */
148
+ headers?: Record<string, string> | Headers;
149
+ /**
150
+ * Extra body object to be sent with the API request.
151
+ * @example
152
+ * Send a `sessionId` to the API along with the prompt.
153
+ * ```js
154
+ * useChat({
155
+ * body: {
156
+ * sessionId: '123',
157
+ * }
158
+ * })
159
+ * ```
160
+ */
161
+ body?: object;
162
+ };
163
+
164
+ type UseChatHelpers = {
165
+ /** Current messages in the chat */
166
+ messages: Resource<Message[]>;
167
+ /** The error object of the API request */
168
+ error: Accessor<undefined | Error>;
169
+ /**
170
+ * Append a user message to the chat list. This triggers the API call to fetch
171
+ * the assistant's response.
172
+ * @param message The message to append
173
+ * @param options Additional options to pass to the API call
174
+ */
175
+ append: (message: Message | CreateMessage, options?: RequestOptions) => Promise<string | null | undefined>;
176
+ /**
177
+ * Reload the last AI chat response for the given chat history. If the last
178
+ * message isn't from the assistant, it will request the API to generate a
179
+ * new response.
180
+ */
181
+ reload: (options?: RequestOptions) => Promise<string | null | undefined>;
182
+ /**
183
+ * Abort the current request immediately, keep the generated tokens if any.
184
+ */
185
+ stop: () => void;
186
+ /**
187
+ * Update the `messages` state locally. This is useful when you want to
188
+ * edit the messages on the client, and then trigger the `reload` method
189
+ * manually to regenerate the AI response.
190
+ */
191
+ setMessages: (messages: Message[]) => void;
192
+ /** The current value of the input */
193
+ input: Accessor<string>;
194
+ /** Signal setter to update the input value */
195
+ setInput: Setter<string>;
196
+ /** Form submission handler to automattically reset input and append a user message */
197
+ handleSubmit: (e: any) => void;
198
+ /** Whether the API request is in progress */
199
+ isLoading: Accessor<boolean>;
200
+ };
201
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, onResponse, onFinish, onError, credentials, headers, body }?: UseChatOptions): UseChatHelpers;
202
+
203
+ type UseCompletionHelpers = {
204
+ /** The current completion result */
205
+ completion: Resource<string>;
206
+ /** The error object of the API request */
207
+ error: Accessor<undefined | Error>;
208
+ /**
209
+ * Send a new prompt to the API endpoint and update the completion state.
210
+ */
211
+ complete: (prompt: string, options?: RequestOptions) => Promise<string | null | undefined>;
212
+ /**
213
+ * Abort the current API request but keep the generated tokens.
214
+ */
215
+ stop: () => void;
216
+ /**
217
+ * Update the `completion` state locally.
218
+ */
219
+ setCompletion: (completion: string) => void;
220
+ /** The current value of the input */
221
+ input: Accessor<string>;
222
+ /** Signal Setter to update the input value */
223
+ setInput: Setter<string>;
224
+ /**
225
+ * Form submission handler to automattically reset input and append a user message
226
+ * @example
227
+ * ```jsx
228
+ * <form onSubmit={handleSubmit}>
229
+ * <input value={input()} />
230
+ * </form>
231
+ * ```
232
+ */
233
+ handleSubmit: (e: any) => void;
234
+ /** Whether the API request is in progress */
235
+ isLoading: Accessor<boolean>;
236
+ };
237
+ declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError }?: UseCompletionOptions): UseCompletionHelpers;
238
+
239
+ export { CreateMessage, Message, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useChat, useCompletion };