ai 3.0.4 → 3.0.5

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.
@@ -0,0 +1,347 @@
1
+ import { Resource, Accessor, Setter } from 'solid-js';
2
+
3
+ interface FunctionCall {
4
+ /**
5
+ * The arguments to call the function with, as generated by the model in JSON
6
+ * format. Note that the model does not always generate valid JSON, and may
7
+ * hallucinate parameters not defined by your function schema. Validate the
8
+ * arguments in your code before calling your function.
9
+ */
10
+ arguments?: string;
11
+ /**
12
+ * The name of the function to call.
13
+ */
14
+ name?: string;
15
+ }
16
+ /**
17
+ * The tool calls generated by the model, such as function calls.
18
+ */
19
+ interface ToolCall {
20
+ id: string;
21
+ type: string;
22
+ function: {
23
+ name: string;
24
+ arguments: string;
25
+ };
26
+ }
27
+ /**
28
+ * Controls which (if any) function is called by the model.
29
+ * - none means the model will not call a function and instead generates a message.
30
+ * - auto means the model can pick between generating a message or calling a function.
31
+ * - Specifying a particular function via {"type: "function", "function": {"name": "my_function"}} forces the model to call that function.
32
+ * none is the default when no functions are present. auto is the default if functions are present.
33
+ */
34
+ type ToolChoice = 'none' | 'auto' | {
35
+ type: 'function';
36
+ function: {
37
+ name: string;
38
+ };
39
+ };
40
+ /**
41
+ * A list of tools the model may call. Currently, only functions are supported as a tool.
42
+ * Use this to provide a list of functions the model may generate JSON inputs for.
43
+ */
44
+ interface Tool {
45
+ type: 'function';
46
+ function: Function;
47
+ }
48
+ interface Function {
49
+ /**
50
+ * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
51
+ * underscores and dashes, with a maximum length of 64.
52
+ */
53
+ name: string;
54
+ /**
55
+ * The parameters the functions accepts, described as a JSON Schema object. See the
56
+ * [guide](/docs/guides/gpt/function-calling) for examples, and the
57
+ * [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for
58
+ * documentation about the format.
59
+ *
60
+ * To describe a function that accepts no parameters, provide the value
61
+ * `{"type": "object", "properties": {}}`.
62
+ */
63
+ parameters: Record<string, unknown>;
64
+ /**
65
+ * A description of what the function does, used by the model to choose when and
66
+ * how to call the function.
67
+ */
68
+ description?: string;
69
+ }
70
+ type IdGenerator = () => string;
71
+ /**
72
+ * Shared types between the API and UI packages.
73
+ */
74
+ interface Message {
75
+ id: string;
76
+ tool_call_id?: string;
77
+ createdAt?: Date;
78
+ content: string;
79
+ ui?: string | JSX.Element | JSX.Element[] | null | undefined;
80
+ role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
81
+ /**
82
+ * If the message has a role of `function`, the `name` field is the name of the function.
83
+ * Otherwise, the name field should not be set.
84
+ */
85
+ name?: string;
86
+ /**
87
+ * If the assistant role makes a function call, the `function_call` field
88
+ * contains the function call name and arguments. Otherwise, the field should
89
+ * not be set. (Deprecated and replaced by tool_calls.)
90
+ */
91
+ function_call?: string | FunctionCall;
92
+ data?: JSONValue;
93
+ /**
94
+ * If the assistant role makes a tool call, the `tool_calls` field contains
95
+ * the tool call name and arguments. Otherwise, the field should not be set.
96
+ */
97
+ tool_calls?: string | ToolCall[];
98
+ /**
99
+ * Additional message-specific information added on the server via StreamData
100
+ */
101
+ annotations?: JSONValue[] | undefined;
102
+ }
103
+ type CreateMessage = Omit<Message, 'id'> & {
104
+ id?: Message['id'];
105
+ };
106
+ type ChatRequest = {
107
+ messages: Message[];
108
+ options?: RequestOptions;
109
+ functions?: Array<Function>;
110
+ function_call?: FunctionCall;
111
+ data?: Record<string, string>;
112
+ tools?: Array<Tool>;
113
+ tool_choice?: ToolChoice;
114
+ };
115
+ type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
116
+ type ToolCallHandler = (chatMessages: Message[], toolCalls: ToolCall[]) => Promise<ChatRequest | void>;
117
+ type RequestOptions = {
118
+ headers?: Record<string, string> | Headers;
119
+ body?: object;
120
+ };
121
+ type ChatRequestOptions = {
122
+ options?: RequestOptions;
123
+ functions?: Array<Function>;
124
+ function_call?: FunctionCall;
125
+ tools?: Array<Tool>;
126
+ tool_choice?: ToolChoice;
127
+ data?: Record<string, string>;
128
+ };
129
+ type UseChatOptions = {
130
+ /**
131
+ * The API endpoint that accepts a `{ messages: Message[] }` object and returns
132
+ * a stream of tokens of the AI chat response. Defaults to `/api/chat`.
133
+ */
134
+ api?: string;
135
+ /**
136
+ * A unique identifier for the chat. If not provided, a random one will be
137
+ * generated. When provided, the `useChat` hook with the same `id` will
138
+ * have shared states across components.
139
+ */
140
+ id?: string;
141
+ /**
142
+ * Initial messages of the chat. Useful to load an existing chat history.
143
+ */
144
+ initialMessages?: Message[];
145
+ /**
146
+ * Initial input of the chat.
147
+ */
148
+ initialInput?: string;
149
+ /**
150
+ * Callback function to be called when a function call is received.
151
+ * If the function returns a `ChatRequest` object, the request will be sent
152
+ * automatically to the API and will be used to update the chat.
153
+ */
154
+ experimental_onFunctionCall?: FunctionCallHandler;
155
+ /**
156
+ * Callback function to be called when a tool call is received.
157
+ * If the function returns a `ChatRequest` object, the request will be sent
158
+ * automatically to the API and will be used to update the chat.
159
+ */
160
+ experimental_onToolCall?: ToolCallHandler;
161
+ /**
162
+ * Callback function to be called when the API response is received.
163
+ */
164
+ onResponse?: (response: Response) => void | Promise<void>;
165
+ /**
166
+ * Callback function to be called when the chat is finished streaming.
167
+ */
168
+ onFinish?: (message: Message) => void;
169
+ /**
170
+ * Callback function to be called when an error is encountered.
171
+ */
172
+ onError?: (error: Error) => void;
173
+ /**
174
+ * A way to provide a function that is going to be used for ids for messages.
175
+ * If not provided nanoid is used by default.
176
+ */
177
+ generateId?: IdGenerator;
178
+ /**
179
+ * The credentials mode to be used for the fetch request.
180
+ * Possible values are: 'omit', 'same-origin', 'include'.
181
+ * Defaults to 'same-origin'.
182
+ */
183
+ credentials?: RequestCredentials;
184
+ /**
185
+ * HTTP headers to be sent with the API request.
186
+ */
187
+ headers?: Record<string, string> | Headers;
188
+ /**
189
+ * Extra body object to be sent with the API request.
190
+ * @example
191
+ * Send a `sessionId` to the API along with the messages.
192
+ * ```js
193
+ * useChat({
194
+ * body: {
195
+ * sessionId: '123',
196
+ * }
197
+ * })
198
+ * ```
199
+ */
200
+ body?: object;
201
+ /**
202
+ * Whether to send extra message fields such as `message.id` and `message.createdAt` to the API.
203
+ * Defaults to `false`. When set to `true`, the API endpoint might need to
204
+ * handle the extra fields before forwarding the request to the AI service.
205
+ */
206
+ sendExtraMessageFields?: boolean;
207
+ };
208
+ type UseCompletionOptions = {
209
+ /**
210
+ * The API endpoint that accepts a `{ prompt: string }` object and returns
211
+ * a stream of tokens of the AI completion response. Defaults to `/api/completion`.
212
+ */
213
+ api?: string;
214
+ /**
215
+ * An unique identifier for the chat. If not provided, a random one will be
216
+ * generated. When provided, the `useChat` hook with the same `id` will
217
+ * have shared states across components.
218
+ */
219
+ id?: string;
220
+ /**
221
+ * Initial prompt input of the completion.
222
+ */
223
+ initialInput?: string;
224
+ /**
225
+ * Initial completion result. Useful to load an existing history.
226
+ */
227
+ initialCompletion?: string;
228
+ /**
229
+ * Callback function to be called when the API response is received.
230
+ */
231
+ onResponse?: (response: Response) => void | Promise<void>;
232
+ /**
233
+ * Callback function to be called when the completion is finished streaming.
234
+ */
235
+ onFinish?: (prompt: string, completion: string) => void;
236
+ /**
237
+ * Callback function to be called when an error is encountered.
238
+ */
239
+ onError?: (error: Error) => void;
240
+ /**
241
+ * The credentials mode to be used for the fetch request.
242
+ * Possible values are: 'omit', 'same-origin', 'include'.
243
+ * Defaults to 'same-origin'.
244
+ */
245
+ credentials?: RequestCredentials;
246
+ /**
247
+ * HTTP headers to be sent with the API request.
248
+ */
249
+ headers?: Record<string, string> | Headers;
250
+ /**
251
+ * Extra body object to be sent with the API request.
252
+ * @example
253
+ * Send a `sessionId` to the API along with the prompt.
254
+ * ```js
255
+ * useChat({
256
+ * body: {
257
+ * sessionId: '123',
258
+ * }
259
+ * })
260
+ * ```
261
+ */
262
+ body?: object;
263
+ };
264
+ type JSONValue = null | string | number | boolean | {
265
+ [x: string]: JSONValue;
266
+ } | Array<JSONValue>;
267
+
268
+ type UseChatHelpers = {
269
+ /** Current messages in the chat */
270
+ messages: Resource<Message[]>;
271
+ /** The error object of the API request */
272
+ error: Accessor<undefined | Error>;
273
+ /**
274
+ * Append a user message to the chat list. This triggers the API call to fetch
275
+ * the assistant's response.
276
+ * @param message The message to append
277
+ * @param options Additional options to pass to the API call
278
+ */
279
+ append: (message: Message | CreateMessage, chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
280
+ /**
281
+ * Reload the last AI chat response for the given chat history. If the last
282
+ * message isn't from the assistant, it will request the API to generate a
283
+ * new response.
284
+ */
285
+ reload: (chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
286
+ /**
287
+ * Abort the current request immediately, keep the generated tokens if any.
288
+ */
289
+ stop: () => void;
290
+ /**
291
+ * Update the `messages` state locally. This is useful when you want to
292
+ * edit the messages on the client, and then trigger the `reload` method
293
+ * manually to regenerate the AI response.
294
+ */
295
+ setMessages: (messages: Message[]) => void;
296
+ /** The current value of the input */
297
+ input: Accessor<string>;
298
+ /** Signal setter to update the input value */
299
+ setInput: Setter<string>;
300
+ /** Form submission handler to automatically reset input and append a user message */
301
+ handleSubmit: (e: any, chatRequestOptions?: ChatRequestOptions) => void;
302
+ /** Whether the API request is in progress */
303
+ isLoading: Accessor<boolean>;
304
+ /** Additional data added on the server via StreamData */
305
+ data: Accessor<JSONValue[] | undefined>;
306
+ };
307
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: UseChatOptions): UseChatHelpers;
308
+
309
+ type UseCompletionHelpers = {
310
+ /** The current completion result */
311
+ completion: Resource<string>;
312
+ /** The error object of the API request */
313
+ error: Accessor<undefined | Error>;
314
+ /**
315
+ * Send a new prompt to the API endpoint and update the completion state.
316
+ */
317
+ complete: (prompt: string, options?: RequestOptions) => Promise<string | null | undefined>;
318
+ /**
319
+ * Abort the current API request but keep the generated tokens.
320
+ */
321
+ stop: () => void;
322
+ /**
323
+ * Update the `completion` state locally.
324
+ */
325
+ setCompletion: (completion: string) => void;
326
+ /** The current value of the input */
327
+ input: Accessor<string>;
328
+ /** Signal Setter to update the input value */
329
+ setInput: Setter<string>;
330
+ /**
331
+ * Form submission handler to automatically reset input and append a user message
332
+ * @example
333
+ * ```jsx
334
+ * <form onSubmit={handleSubmit}>
335
+ * <input value={input()} />
336
+ * </form>
337
+ * ```
338
+ */
339
+ handleSubmit: (e: any) => void;
340
+ /** Whether the API request is in progress */
341
+ isLoading: Accessor<boolean>;
342
+ /** Additional data added on the server via StreamData */
343
+ data: Accessor<JSONValue[] | undefined>;
344
+ };
345
+ declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
346
+
347
+ export { CreateMessage, Message, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useChat, useCompletion };