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.
- package/dist/index.d.mts +998 -0
- package/dist/index.d.ts +15 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/prompts/dist/index.d.mts +242 -0
- package/prompts/dist/index.js.map +1 -1
- package/prompts/dist/index.mjs.map +1 -1
- package/react/dist/index.d.mts +475 -0
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs.map +1 -1
- package/react/dist/index.server.d.mts +15 -0
- package/rsc/dist/{rsc-types.d.ts → index.d.mts} +2 -2
- package/rsc/dist/index.mjs +1 -0
- package/rsc/dist/{rsc-client.d.ts → rsc-client.d.mts} +1 -1
- package/rsc/dist/rsc-client.mjs +1 -1
- package/rsc/dist/rsc-client.mjs.map +1 -1
- package/rsc/dist/{rsc-server.d.ts → rsc-server.d.mts} +1 -1
- package/rsc/dist/rsc-server.mjs +1 -1
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/rsc/dist/rsc-shared.mjs.map +1 -1
- package/solid/dist/index.d.mts +347 -0
- package/solid/dist/index.js.map +1 -1
- package/solid/dist/index.mjs.map +1 -1
- package/svelte/dist/index.d.mts +344 -0
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs.map +1 -1
- package/vue/dist/index.d.mts +341 -0
- package/vue/dist/index.js.map +1 -1
- package/vue/dist/index.mjs.map +1 -1
- package/rsc/dist/rsc-types.mjs +0 -1
- /package/rsc/dist/{rsc-types.mjs.map → index.mjs.map} +0 -0
- /package/rsc/dist/{rsc-shared.d.ts → rsc-shared.d.mts} +0 -0
@@ -0,0 +1,341 @@
|
|
1
|
+
import { Ref } from 'vue';
|
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: Ref<Message[]>;
|
271
|
+
/** The error object of the API request */
|
272
|
+
error: Ref<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
|
+
*/
|
277
|
+
append: (message: Message | CreateMessage, chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
|
278
|
+
/**
|
279
|
+
* Reload the last AI chat response for the given chat history. If the last
|
280
|
+
* message isn't from the assistant, it will request the API to generate a
|
281
|
+
* new response.
|
282
|
+
*/
|
283
|
+
reload: (chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
|
284
|
+
/**
|
285
|
+
* Abort the current request immediately, keep the generated tokens if any.
|
286
|
+
*/
|
287
|
+
stop: () => void;
|
288
|
+
/**
|
289
|
+
* Update the `messages` state locally. This is useful when you want to
|
290
|
+
* edit the messages on the client, and then trigger the `reload` method
|
291
|
+
* manually to regenerate the AI response.
|
292
|
+
*/
|
293
|
+
setMessages: (messages: Message[]) => void;
|
294
|
+
/** The current value of the input */
|
295
|
+
input: Ref<string>;
|
296
|
+
/** Form submission handler to automatically reset input and append a user message */
|
297
|
+
handleSubmit: (e: any, chatRequestOptions?: ChatRequestOptions) => void;
|
298
|
+
/** Whether the API request is in progress */
|
299
|
+
isLoading: Ref<boolean | undefined>;
|
300
|
+
/** Additional data added on the server via StreamData */
|
301
|
+
data: Ref<JSONValue[] | undefined>;
|
302
|
+
};
|
303
|
+
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: UseChatOptions): UseChatHelpers;
|
304
|
+
|
305
|
+
type UseCompletionHelpers = {
|
306
|
+
/** The current completion result */
|
307
|
+
completion: Ref<string>;
|
308
|
+
/** The error object of the API request */
|
309
|
+
error: Ref<undefined | Error>;
|
310
|
+
/**
|
311
|
+
* Send a new prompt to the API endpoint and update the completion state.
|
312
|
+
*/
|
313
|
+
complete: (prompt: string, options?: RequestOptions) => Promise<string | null | undefined>;
|
314
|
+
/**
|
315
|
+
* Abort the current API request but keep the generated tokens.
|
316
|
+
*/
|
317
|
+
stop: () => void;
|
318
|
+
/**
|
319
|
+
* Update the `completion` state locally.
|
320
|
+
*/
|
321
|
+
setCompletion: (completion: string) => void;
|
322
|
+
/** The current value of the input */
|
323
|
+
input: Ref<string>;
|
324
|
+
/**
|
325
|
+
* Form submission handler to automatically reset input and append a user message
|
326
|
+
* @example
|
327
|
+
* ```jsx
|
328
|
+
* <form @submit="handleSubmit">
|
329
|
+
* <input @change="handleInputChange" v-model="input" />
|
330
|
+
* </form>
|
331
|
+
* ```
|
332
|
+
*/
|
333
|
+
handleSubmit: (e: any) => void;
|
334
|
+
/** Whether the API request is in progress */
|
335
|
+
isLoading: Ref<boolean | undefined>;
|
336
|
+
/** Additional data added on the server via StreamData */
|
337
|
+
data: Ref<JSONValue[] | undefined>;
|
338
|
+
};
|
339
|
+
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
|
340
|
+
|
341
|
+
export { CreateMessage, Message, UseChatHelpers, UseChatOptions, UseCompletionHelpers, useChat, useCompletion };
|