ai 0.0.0-85f9a635-20240518005312

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 +2287 -0
  4. package/dist/index.d.ts +2287 -0
  5. package/dist/index.js +3531 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/index.mjs +3454 -0
  8. package/dist/index.mjs.map +1 -0
  9. package/package.json +178 -0
  10. package/prompts/dist/index.d.mts +324 -0
  11. package/prompts/dist/index.d.ts +324 -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 +565 -0
  17. package/react/dist/index.d.ts +582 -0
  18. package/react/dist/index.js +1430 -0
  19. package/react/dist/index.js.map +1 -0
  20. package/react/dist/index.mjs +1391 -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 +580 -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 +516 -0
  34. package/rsc/dist/rsc-server.mjs +1900 -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 +408 -0
  40. package/solid/dist/index.d.ts +408 -0
  41. package/solid/dist/index.js +1072 -0
  42. package/solid/dist/index.js.map +1 -0
  43. package/solid/dist/index.mjs +1044 -0
  44. package/solid/dist/index.mjs.map +1 -0
  45. package/svelte/dist/index.d.mts +484 -0
  46. package/svelte/dist/index.d.ts +484 -0
  47. package/svelte/dist/index.js +1778 -0
  48. package/svelte/dist/index.js.map +1 -0
  49. package/svelte/dist/index.mjs +1749 -0
  50. package/svelte/dist/index.mjs.map +1 -0
  51. package/vue/dist/index.d.mts +402 -0
  52. package/vue/dist/index.d.ts +402 -0
  53. package/vue/dist/index.js +1072 -0
  54. package/vue/dist/index.js.map +1 -0
  55. package/vue/dist/index.mjs +1034 -0
  56. package/vue/dist/index.mjs.map +1 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../anthropic.ts","../huggingface.ts","../openai.tsx"],"sourcesContent":["import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for Anthropic models.\n * Does not support `function` messages.\n * @see https://docs.anthropic.com/claude/reference/getting-started-with-the-api\n */\nexport function experimental_buildAnthropicPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages.map(({ content, role }) => {\n if (role === 'user') {\n return `\\n\\nHuman: ${content}`;\n } else {\n return `\\n\\nAssistant: ${content}`;\n }\n }) + '\\n\\nAssistant:'\n );\n}\n\n/**\n * A prompt constructor for Anthropic V3 models which require Messages API.\n * Does not support message with image content\n * @see https://docs.anthropic.com/claude/reference/messages_post\n */\nexport function experimental_buildAnthropicMessages(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return messages.map(({ content, role }) => {\n if (!['assistant', 'user'].includes(role)) {\n throw new Error(`Cannot use ${role} on Anthropic V3 Messages API`);\n }\n return {\n role,\n content: [{ type: 'text', text: content }],\n };\n });\n}\n","import { Message } from '../shared/types';\n\n/**\n * A prompt constructor for the HuggingFace StarChat Beta model.\n * Does not support `function` messages.\n * @see https://huggingface.co/HuggingFaceH4/starchat-beta\n */\nexport function experimental_buildStarChatBetaPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|user|>\\n${content}<|end|>\\n`;\n } else if (role === 'assistant') {\n return `<|assistant|>\\n${content}<|end|>\\n`;\n } else if (role === 'system') {\n return `<|system|>\\n${content}<|end|>\\n`;\n } else if (role === 'function') {\n throw new Error('StarChat Beta does not support function calls.');\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace OpenAssistant models.\n * Does not support `function` or `system` messages.\n * @see https://huggingface.co/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5\n */\nexport function experimental_buildOpenAssistantPrompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n return (\n messages\n .map(({ content, role }) => {\n if (role === 'user') {\n return `<|prompter|>${content}<|endoftext|>`;\n } else if (role === 'function') {\n throw new Error('OpenAssistant does not support function calls.');\n } else if (role === 'system') {\n throw new Error('OpenAssistant does not support system messages.');\n } else {\n return `<|assistant|>${content}<|endoftext|>`;\n }\n })\n .join('') + '<|assistant|>'\n );\n}\n\n/**\n * A prompt constructor for HuggingFace LLama 2 chat models.\n * Does not support `function` messages.\n * @see https://huggingface.co/meta-llama/Llama-2-70b-chat-hf and https://huggingface.co/blog/llama2#how-to-prompt-llama-2\n */\nexport function experimental_buildLlama2Prompt(\n messages: Pick<Message, 'content' | 'role'>[],\n) {\n const startPrompt = `<s>[INST] `;\n const endPrompt = ` [/INST]`;\n const conversation = messages.map(({ content, role }, index) => {\n if (role === 'user') {\n return content.trim();\n } else if (role === 'assistant') {\n return ` [/INST] ${content}</s><s>[INST] `;\n } else if (role === 'function') {\n throw new Error('Llama 2 does not support function calls.');\n } else if (role === 'system' && index === 0) {\n return `<<SYS>>\\n${content}\\n<</SYS>>\\n\\n`;\n } else {\n throw new Error(`Invalid message role: ${role}`);\n }\n });\n\n return startPrompt + conversation.join('') + endPrompt;\n}\n","import { Message } from '../shared/types';\n\nexport function experimental_buildOpenAIMessages(\n messages: Message[],\n): ChatCompletionMessageParam[] {\n return messages.map(message => {\n switch (message.role) {\n case 'system':\n case 'user':\n return {\n role: message.role,\n content: message.content,\n } satisfies ChatCompletionMessageParam;\n\n case 'assistant': {\n const function_call = message.function_call;\n\n if (\n function_call !== undefined &&\n (typeof function_call === 'string' ||\n function_call.arguments === undefined ||\n function_call.name === undefined)\n ) {\n throw new Error(\n 'Invalid function call in message. Expected a function call object',\n );\n }\n\n return {\n role: message.role,\n content: message.content,\n function_call:\n function_call === undefined\n ? undefined\n : {\n name: function_call.name!,\n arguments: function_call.arguments!,\n },\n } satisfies ChatCompletionMessageParam;\n }\n\n case 'function': {\n if (message.name === undefined) {\n throw new Error('Invalid function call in message. Expected a name');\n }\n\n return {\n role: message.role,\n content: message.content,\n name: message.name,\n } satisfies ChatCompletionMessageParam;\n }\n\n case 'data': {\n throw \"unsupported message role 'data'\";\n }\n\n case 'tool': {\n if (message.name === undefined) {\n throw new Error('Invalid tool message. Expected a name');\n }\n\n if (message.tool_call_id === undefined) {\n throw new Error('Invalid tool message. Expected a tool_call_id');\n }\n\n return {\n role: message.role,\n content: message.content,\n tool_call_id: message.tool_call_id,\n } satisfies ChatCompletionMessageParam;\n }\n }\n });\n}\n\n// copy of open ai messages (so we don't have a dependency on the openai package)\nexport type ChatCompletionMessageParam =\n | ChatCompletionSystemMessageParam\n | ChatCompletionUserMessageParam\n | ChatCompletionAssistantMessageParam\n | ChatCompletionToolMessageParam\n | ChatCompletionFunctionMessageParam;\n\nexport interface ChatCompletionSystemMessageParam {\n /**\n * The contents of the system message.\n */\n content: string;\n\n /**\n * The role of the messages author, in this case `system`.\n */\n role: 'system';\n\n /**\n * An optional name for the participant. Provides the model information to\n * differentiate between participants of the same role.\n */\n name?: string;\n}\n\nexport interface ChatCompletionUserMessageParam {\n /**\n * The contents of the user message.\n */\n content: string | Array<ChatCompletionContentPart>;\n\n /**\n * The role of the messages author, in this case `user`.\n */\n role: 'user';\n\n /**\n * An optional name for the participant. Provides the model information to\n * differentiate between participants of the same role.\n */\n name?: string;\n}\n\nexport type ChatCompletionContentPart =\n | ChatCompletionContentPartText\n | ChatCompletionContentPartImage;\n\nexport interface ChatCompletionContentPartText {\n /**\n * The text content.\n */\n text: string;\n\n /**\n * The type of the content part.\n */\n type: 'text';\n}\n\nexport interface ChatCompletionContentPartImage {\n image_url: ChatCompletionContentPartImage.ImageURL;\n\n /**\n * The type of the content part.\n */\n type: 'image_url';\n}\n\nexport namespace ChatCompletionContentPartImage {\n export interface ImageURL {\n /**\n * Either a URL of the image or the base64 encoded image data.\n */\n url: string;\n\n /**\n * Specifies the detail level of the image. Learn more in the\n * [Vision guide](https://platform.openai.com/docs/guides/vision/low-or-high-fidelity-image-understanding).\n */\n detail?: 'auto' | 'low' | 'high';\n }\n}\n\nexport interface ChatCompletionAssistantMessageParam {\n /**\n * The role of the messages author, in this case `assistant`.\n */\n role: 'assistant';\n\n /**\n * The contents of the assistant message. Required unless `tool_calls` or\n * `function_call` is specified.\n */\n content?: string | null;\n\n /**\n * @deprecated: Deprecated and replaced by `tool_calls`. The name and arguments of\n * a function that should be called, as generated by the model.\n */\n function_call?: ChatCompletionAssistantMessageParam.FunctionCall;\n\n /**\n * An optional name for the participant. Provides the model information to\n * differentiate between participants of the same role.\n */\n name?: string;\n\n /**\n * The tool calls generated by the model, such as function calls.\n */\n tool_calls?: Array<ChatCompletionMessageToolCall>;\n}\n\nexport namespace ChatCompletionAssistantMessageParam {\n /**\n * @deprecated: Deprecated and replaced by `tool_calls`. The name and arguments of\n * a function that should be called, as generated by the model.\n */\n export interface FunctionCall {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n }\n}\n\nexport interface ChatCompletionMessageToolCall {\n /**\n * The ID of the tool call.\n */\n id: string;\n\n /**\n * The function that the model called.\n */\n function: ChatCompletionMessageToolCall.Function;\n\n /**\n * The type of the tool. Currently, only `function` is supported.\n */\n type: 'function';\n}\n\nexport namespace ChatCompletionMessageToolCall {\n /**\n * The function that the model called.\n */\n export interface Function {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n }\n}\n\nexport interface ChatCompletionToolMessageParam {\n /**\n * The contents of the tool message.\n */\n content: string;\n\n /**\n * The role of the messages author, in this case `tool`.\n */\n role: 'tool';\n\n /**\n * Tool call that this message is responding to.\n */\n tool_call_id: string;\n}\n\nexport interface ChatCompletionFunctionMessageParam {\n /**\n * The return value from the function call, to return to the model.\n */\n content: string | null;\n\n /**\n * The name of the function to call.\n */\n name: string;\n\n /**\n * The role of the messages author, in this case `function`.\n */\n role: 'function';\n}\n"],"mappings":"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","names":[]}
@@ -0,0 +1,565 @@
1
+ /**
2
+ Typed tool call that is returned by generateText and streamText.
3
+ It contains the tool call ID, the tool name, and the tool arguments.
4
+ */
5
+ interface ToolCall$1<NAME extends string, ARGS> {
6
+ /**
7
+ ID of the tool call. This ID is used to match the tool call with the tool result.
8
+ */
9
+ toolCallId: string;
10
+ /**
11
+ Name of the tool that is being called.
12
+ */
13
+ toolName: NAME;
14
+ /**
15
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
16
+ */
17
+ args: ARGS;
18
+ }
19
+
20
+ /**
21
+ Typed tool result that is returned by generateText and streamText.
22
+ It contains the tool call ID, the tool name, the tool arguments, and the tool result.
23
+ */
24
+ interface ToolResult<NAME extends string, ARGS, RESULT> {
25
+ /**
26
+ ID of the tool call. This ID is used to match the tool call with the tool result.
27
+ */
28
+ toolCallId: string;
29
+ /**
30
+ Name of the tool that was called.
31
+ */
32
+ toolName: NAME;
33
+ /**
34
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
35
+ */
36
+ args: ARGS;
37
+ /**
38
+ Result of the tool call. This is the result of the tool's execution.
39
+ */
40
+ result: RESULT;
41
+ }
42
+
43
+ type AssistantStatus = 'in_progress' | 'awaiting_message';
44
+ type UseAssistantOptions = {
45
+ /**
46
+ * The API endpoint that accepts a `{ threadId: string | null; message: string; }` object and returns an `AssistantResponse` stream.
47
+ * The threadId refers to an existing thread with messages (or is `null` to create a new thread).
48
+ * The message is the next message that should be appended to the thread and sent to the assistant.
49
+ */
50
+ api: string;
51
+ /**
52
+ * An optional string that represents the ID of an existing thread.
53
+ * If not provided, a new thread will be created.
54
+ */
55
+ threadId?: string;
56
+ /**
57
+ * An optional literal that sets the mode of credentials to be used on the request.
58
+ * Defaults to "same-origin".
59
+ */
60
+ credentials?: RequestCredentials;
61
+ /**
62
+ * An optional object of headers to be passed to the API endpoint.
63
+ */
64
+ headers?: Record<string, string> | Headers;
65
+ /**
66
+ * An optional, additional body object to be passed to the API endpoint.
67
+ */
68
+ body?: object;
69
+ /**
70
+ * An optional callback that will be called when the assistant encounters an error.
71
+ */
72
+ onError?: (error: Error) => void;
73
+ };
74
+
75
+ interface FunctionCall {
76
+ /**
77
+ * The arguments to call the function with, as generated by the model in JSON
78
+ * format. Note that the model does not always generate valid JSON, and may
79
+ * hallucinate parameters not defined by your function schema. Validate the
80
+ * arguments in your code before calling your function.
81
+ */
82
+ arguments?: string;
83
+ /**
84
+ * The name of the function to call.
85
+ */
86
+ name?: string;
87
+ }
88
+ /**
89
+ * The tool calls generated by the model, such as function calls.
90
+ */
91
+ interface ToolCall {
92
+ id: string;
93
+ type: string;
94
+ function: {
95
+ name: string;
96
+ arguments: string;
97
+ };
98
+ }
99
+ /**
100
+ * Controls which (if any) function is called by the model.
101
+ * - none means the model will not call a function and instead generates a message.
102
+ * - auto means the model can pick between generating a message or calling a function.
103
+ * - Specifying a particular function via {"type: "function", "function": {"name": "my_function"}} forces the model to call that function.
104
+ * none is the default when no functions are present. auto is the default if functions are present.
105
+ */
106
+ type ToolChoice = 'none' | 'auto' | {
107
+ type: 'function';
108
+ function: {
109
+ name: string;
110
+ };
111
+ };
112
+ /**
113
+ * A list of tools the model may call. Currently, only functions are supported as a tool.
114
+ * Use this to provide a list of functions the model may generate JSON inputs for.
115
+ */
116
+ interface Tool {
117
+ type: 'function';
118
+ function: Function;
119
+ }
120
+ interface Function {
121
+ /**
122
+ * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
123
+ * underscores and dashes, with a maximum length of 64.
124
+ */
125
+ name: string;
126
+ /**
127
+ * The parameters the functions accepts, described as a JSON Schema object. See the
128
+ * [guide](/docs/guides/gpt/function-calling) for examples, and the
129
+ * [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for
130
+ * documentation about the format.
131
+ *
132
+ * To describe a function that accepts no parameters, provide the value
133
+ * `{"type": "object", "properties": {}}`.
134
+ */
135
+ parameters: Record<string, unknown>;
136
+ /**
137
+ * A description of what the function does, used by the model to choose when and
138
+ * how to call the function.
139
+ */
140
+ description?: string;
141
+ }
142
+ type IdGenerator = () => string;
143
+ /**
144
+ Tool invocations are either tool calls or tool results. For each assistant tool call,
145
+ there is one tool invocation. While the call is in progress, the invocation is a tool call.
146
+ Once the call is complete, the invocation is a tool result.
147
+ */
148
+ type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
149
+ /**
150
+ * Shared types between the API and UI packages.
151
+ */
152
+ interface Message {
153
+ id: string;
154
+ tool_call_id?: string;
155
+ createdAt?: Date;
156
+ content: string;
157
+ /**
158
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
159
+ */
160
+ ui?: string | JSX.Element | JSX.Element[] | null | undefined;
161
+ role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
162
+ /**
163
+ *
164
+ * If the message has a role of `function`, the `name` field is the name of the function.
165
+ * Otherwise, the name field should not be set.
166
+ */
167
+ name?: string;
168
+ /**
169
+ * If the assistant role makes a function call, the `function_call` field
170
+ * contains the function call name and arguments. Otherwise, the field should
171
+ * not be set. (Deprecated and replaced by tool_calls.)
172
+ */
173
+ function_call?: string | FunctionCall;
174
+ data?: JSONValue;
175
+ /**
176
+ * If the assistant role makes a tool call, the `tool_calls` field contains
177
+ * the tool call name and arguments. Otherwise, the field should not be set.
178
+ */
179
+ tool_calls?: string | ToolCall[];
180
+ /**
181
+ * Additional message-specific information added on the server via StreamData
182
+ */
183
+ annotations?: JSONValue[] | undefined;
184
+ /**
185
+ Tool invocations (that can be tool calls or tool results, depending on whether or not the invocation has finished)
186
+ that the assistant made as part of this message.
187
+ */
188
+ toolInvocations?: Array<ToolInvocation>;
189
+ }
190
+ type CreateMessage = Omit<Message, 'id'> & {
191
+ id?: Message['id'];
192
+ };
193
+ type ChatRequest = {
194
+ messages: Message[];
195
+ options?: RequestOptions;
196
+ functions?: Array<Function>;
197
+ function_call?: FunctionCall;
198
+ data?: Record<string, string>;
199
+ tools?: Array<Tool>;
200
+ tool_choice?: ToolChoice;
201
+ };
202
+ type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
203
+ type ToolCallHandler = (chatMessages: Message[], toolCalls: ToolCall[]) => Promise<ChatRequest | void>;
204
+ type RequestOptions = {
205
+ headers?: Record<string, string> | Headers;
206
+ body?: object;
207
+ };
208
+ type ChatRequestOptions = {
209
+ options?: RequestOptions;
210
+ functions?: Array<Function>;
211
+ function_call?: FunctionCall;
212
+ tools?: Array<Tool>;
213
+ tool_choice?: ToolChoice;
214
+ data?: Record<string, string>;
215
+ };
216
+ type UseChatOptions = {
217
+ /**
218
+ * The API endpoint that accepts a `{ messages: Message[] }` object and returns
219
+ * a stream of tokens of the AI chat response. Defaults to `/api/chat`.
220
+ */
221
+ api?: string;
222
+ /**
223
+ * A unique identifier for the chat. If not provided, a random one will be
224
+ * generated. When provided, the `useChat` hook with the same `id` will
225
+ * have shared states across components.
226
+ */
227
+ id?: string;
228
+ /**
229
+ * Initial messages of the chat. Useful to load an existing chat history.
230
+ */
231
+ initialMessages?: Message[];
232
+ /**
233
+ * Initial input of the chat.
234
+ */
235
+ initialInput?: string;
236
+ /**
237
+ * Callback function to be called when a function call is received.
238
+ * If the function returns a `ChatRequest` object, the request will be sent
239
+ * automatically to the API and will be used to update the chat.
240
+ */
241
+ experimental_onFunctionCall?: FunctionCallHandler;
242
+ /**
243
+ * Callback function to be called when a tool call is received.
244
+ * If the function returns a `ChatRequest` object, the request will be sent
245
+ * automatically to the API and will be used to update the chat.
246
+ */
247
+ experimental_onToolCall?: ToolCallHandler;
248
+ /**
249
+ * Callback function to be called when the API response is received.
250
+ */
251
+ onResponse?: (response: Response) => void | Promise<void>;
252
+ /**
253
+ * Callback function to be called when the chat is finished streaming.
254
+ */
255
+ onFinish?: (message: Message) => void;
256
+ /**
257
+ * Callback function to be called when an error is encountered.
258
+ */
259
+ onError?: (error: Error) => void;
260
+ /**
261
+ * A way to provide a function that is going to be used for ids for messages.
262
+ * If not provided nanoid is used by default.
263
+ */
264
+ generateId?: IdGenerator;
265
+ /**
266
+ * The credentials mode to be used for the fetch request.
267
+ * Possible values are: 'omit', 'same-origin', 'include'.
268
+ * Defaults to 'same-origin'.
269
+ */
270
+ credentials?: RequestCredentials;
271
+ /**
272
+ * HTTP headers to be sent with the API request.
273
+ */
274
+ headers?: Record<string, string> | Headers;
275
+ /**
276
+ * Extra body object to be sent with the API request.
277
+ * @example
278
+ * Send a `sessionId` to the API along with the messages.
279
+ * ```js
280
+ * useChat({
281
+ * body: {
282
+ * sessionId: '123',
283
+ * }
284
+ * })
285
+ * ```
286
+ */
287
+ body?: object;
288
+ /**
289
+ * Whether to send extra message fields such as `message.id` and `message.createdAt` to the API.
290
+ * Defaults to `false`. When set to `true`, the API endpoint might need to
291
+ * handle the extra fields before forwarding the request to the AI service.
292
+ */
293
+ sendExtraMessageFields?: boolean;
294
+ /** Stream mode (default to "stream-data") */
295
+ streamMode?: 'stream-data' | 'text';
296
+ };
297
+ type UseCompletionOptions = {
298
+ /**
299
+ * The API endpoint that accepts a `{ prompt: string }` object and returns
300
+ * a stream of tokens of the AI completion response. Defaults to `/api/completion`.
301
+ */
302
+ api?: string;
303
+ /**
304
+ * An unique identifier for the chat. If not provided, a random one will be
305
+ * generated. When provided, the `useChat` hook with the same `id` will
306
+ * have shared states across components.
307
+ */
308
+ id?: string;
309
+ /**
310
+ * Initial prompt input of the completion.
311
+ */
312
+ initialInput?: string;
313
+ /**
314
+ * Initial completion result. Useful to load an existing history.
315
+ */
316
+ initialCompletion?: string;
317
+ /**
318
+ * Callback function to be called when the API response is received.
319
+ */
320
+ onResponse?: (response: Response) => void | Promise<void>;
321
+ /**
322
+ * Callback function to be called when the completion is finished streaming.
323
+ */
324
+ onFinish?: (prompt: string, completion: string) => void;
325
+ /**
326
+ * Callback function to be called when an error is encountered.
327
+ */
328
+ onError?: (error: Error) => void;
329
+ /**
330
+ * The credentials mode to be used for the fetch request.
331
+ * Possible values are: 'omit', 'same-origin', 'include'.
332
+ * Defaults to 'same-origin'.
333
+ */
334
+ credentials?: RequestCredentials;
335
+ /**
336
+ * HTTP headers to be sent with the API request.
337
+ */
338
+ headers?: Record<string, string> | Headers;
339
+ /**
340
+ * Extra body object to be sent with the API request.
341
+ * @example
342
+ * Send a `sessionId` to the API along with the prompt.
343
+ * ```js
344
+ * useChat({
345
+ * body: {
346
+ * sessionId: '123',
347
+ * }
348
+ * })
349
+ * ```
350
+ */
351
+ body?: object;
352
+ /** Stream mode (default to "stream-data") */
353
+ streamMode?: 'stream-data' | 'text';
354
+ };
355
+ type JSONValue = null | string | number | boolean | {
356
+ [x: string]: JSONValue;
357
+ } | Array<JSONValue>;
358
+
359
+ /**
360
+ * A stream wrapper to send custom JSON-encoded data back to the client.
361
+ */
362
+ declare class StreamData {
363
+ private encoder;
364
+ private controller;
365
+ stream: ReadableStream<Uint8Array>;
366
+ private isClosed;
367
+ private warningTimeout;
368
+ constructor();
369
+ close(): Promise<void>;
370
+ append(value: JSONValue): void;
371
+ appendMessageAnnotation(value: JSONValue): void;
372
+ }
373
+
374
+ /**
375
+ * This is a naive implementation of the streaming React response API.
376
+ * Currently, it can carry the original raw content, data payload and a special
377
+ * UI payload and stream them via "rows" (nested promises).
378
+ * It must be used inside Server Actions so Flight can encode the React elements.
379
+ *
380
+ * It is naive as unlike the StreamingTextResponse, it does not send the diff
381
+ * between the rows, but flushing the full payload on each row.
382
+ */
383
+
384
+ type UINode = string | JSX.Element | JSX.Element[] | null | undefined;
385
+ /**
386
+ A utility class for streaming React responses.
387
+
388
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
389
+ */
390
+ declare class experimental_StreamingReactResponse {
391
+ constructor(res: ReadableStream, options?: {
392
+ ui?: (message: {
393
+ content: string;
394
+ data?: JSONValue[];
395
+ }) => UINode | Promise<UINode>;
396
+ data?: StreamData;
397
+ generateId?: IdGenerator;
398
+ });
399
+ }
400
+
401
+ type UseChatHelpers = {
402
+ /** Current messages in the chat */
403
+ messages: Message[];
404
+ /** The error object of the API request */
405
+ error: undefined | Error;
406
+ /**
407
+ * Append a user message to the chat list. This triggers the API call to fetch
408
+ * the assistant's response.
409
+ * @param message The message to append
410
+ * @param options Additional options to pass to the API call
411
+ */
412
+ append: (message: Message | CreateMessage, chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
413
+ /**
414
+ * Reload the last AI chat response for the given chat history. If the last
415
+ * message isn't from the assistant, it will request the API to generate a
416
+ * new response.
417
+ */
418
+ reload: (chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
419
+ /**
420
+ * Abort the current request immediately, keep the generated tokens if any.
421
+ */
422
+ stop: () => void;
423
+ /**
424
+ * Update the `messages` state locally. This is useful when you want to
425
+ * edit the messages on the client, and then trigger the `reload` method
426
+ * manually to regenerate the AI response.
427
+ */
428
+ setMessages: (messages: Message[]) => void;
429
+ /** The current value of the input */
430
+ input: string;
431
+ /** setState-powered method to update the input value */
432
+ setInput: React.Dispatch<React.SetStateAction<string>>;
433
+ /** An input/textarea-ready onChange handler to control the value of the input */
434
+ handleInputChange: (e: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
435
+ /** Form submission handler to automatically reset input and append a user message */
436
+ handleSubmit: (e: React.FormEvent<HTMLFormElement>, chatRequestOptions?: ChatRequestOptions) => void;
437
+ metadata?: Object;
438
+ /** Whether the API request is in progress */
439
+ isLoading: boolean;
440
+ /** Additional data added on the server via StreamData */
441
+ data?: JSONValue[];
442
+ };
443
+ /**
444
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
445
+ */
446
+ type StreamingReactResponseAction = (payload: {
447
+ messages: Message[];
448
+ data?: Record<string, string>;
449
+ }) => Promise<experimental_StreamingReactResponse>;
450
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
451
+ api?: string | StreamingReactResponseAction;
452
+ key?: string;
453
+ }): UseChatHelpers & {
454
+ experimental_addToolResult: ({ toolCallId, result, }: {
455
+ toolCallId: string;
456
+ result: any;
457
+ }) => void;
458
+ };
459
+
460
+ type UseCompletionHelpers = {
461
+ /** The current completion result */
462
+ completion: string;
463
+ /**
464
+ * Send a new prompt to the API endpoint and update the completion state.
465
+ */
466
+ complete: (prompt: string, options?: RequestOptions) => Promise<string | null | undefined>;
467
+ /** The error object of the API request */
468
+ error: undefined | Error;
469
+ /**
470
+ * Abort the current API request but keep the generated tokens.
471
+ */
472
+ stop: () => void;
473
+ /**
474
+ * Update the `completion` state locally.
475
+ */
476
+ setCompletion: (completion: string) => void;
477
+ /** The current value of the input */
478
+ input: string;
479
+ /** setState-powered method to update the input value */
480
+ setInput: React.Dispatch<React.SetStateAction<string>>;
481
+ /**
482
+ * An input/textarea-ready onChange handler to control the value of the input
483
+ * @example
484
+ * ```jsx
485
+ * <input onChange={handleInputChange} value={input} />
486
+ * ```
487
+ */
488
+ handleInputChange: (e: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
489
+ /**
490
+ * Form submission handler to automatically reset input and append a user message
491
+ * @example
492
+ * ```jsx
493
+ * <form onSubmit={handleSubmit}>
494
+ * <input onChange={handleInputChange} value={input} />
495
+ * </form>
496
+ * ```
497
+ */
498
+ handleSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
499
+ /** Whether the API request is in progress */
500
+ isLoading: boolean;
501
+ /** Additional data added on the server via StreamData */
502
+ data?: JSONValue[];
503
+ };
504
+ declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
505
+
506
+ type UseAssistantHelpers = {
507
+ /**
508
+ * The current array of chat messages.
509
+ */
510
+ messages: Message[];
511
+ /**
512
+ * Update the message store with a new array of messages.
513
+ */
514
+ setMessages: React.Dispatch<React.SetStateAction<Message[]>>;
515
+ /**
516
+ * The current thread ID.
517
+ */
518
+ threadId: string | undefined;
519
+ /**
520
+ * The current value of the input field.
521
+ */
522
+ input: string;
523
+ /**
524
+ * Append a user message to the chat list. This triggers the API call to fetch
525
+ * the assistant's response.
526
+ * @param message The message to append
527
+ * @param requestOptions Additional options to pass to the API call
528
+ */
529
+ append: (message: Message | CreateMessage, requestOptions?: {
530
+ data?: Record<string, string>;
531
+ }) => Promise<void>;
532
+ /**
533
+ Abort the current request immediately, keep the generated tokens if any.
534
+ */
535
+ stop: () => void;
536
+ /**
537
+ * setState-powered method to update the input value.
538
+ */
539
+ setInput: React.Dispatch<React.SetStateAction<string>>;
540
+ /**
541
+ * Handler for the `onChange` event of the input field to control the input's value.
542
+ */
543
+ handleInputChange: (event: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
544
+ /**
545
+ * Form submission handler that automatically resets the input field and appends a user message.
546
+ */
547
+ submitMessage: (event?: React.FormEvent<HTMLFormElement>, requestOptions?: {
548
+ data?: Record<string, string>;
549
+ }) => Promise<void>;
550
+ /**
551
+ * The current status of the assistant. This can be used to show a loading indicator.
552
+ */
553
+ status: AssistantStatus;
554
+ /**
555
+ * The error thrown during the assistant message processing, if any.
556
+ */
557
+ error: undefined | unknown;
558
+ };
559
+ declare function useAssistant({ api, threadId: threadIdParam, credentials, headers, body, onError, }: UseAssistantOptions): UseAssistantHelpers;
560
+ /**
561
+ @deprecated Use `useAssistant` instead.
562
+ */
563
+ declare const experimental_useAssistant: typeof useAssistant;
564
+
565
+ export { CreateMessage, Message, UseAssistantHelpers, UseChatHelpers, UseChatOptions, UseCompletionHelpers, UseCompletionOptions, experimental_useAssistant, useAssistant, useChat, useCompletion };