import { LanguageModelUsage, IdGenerator, JSONValue, ChatRequestOptions } from 'ai'; import { LanguageModelV1FinishReason } from '@ai-sdk/provider'; import { ToolCall } from '@ai-sdk/provider-utils'; import { Message } from '@ai-sdk/ui-utils'; import { Message as Message$1, CreateMessage } from '@ai-sdk/react'; type Handlers = { /** * Optional callback function that is invoked when a tool call is received. * Intended for automatic client-side tool execution. * * You can optionally return a result for the tool call, * either synchronously or asynchronously. */ onToolCall?: ({ toolCall }: { toolCall: ToolCall; }) => void; /** * Callback function to be called when the API response is received. */ onResponse?: (response: Response) => void; /** * Optional callback function that is called when the assistant message is finished streaming. * * @param message The message that was streamed. * @param options.usage The token usage of the message. * @param options.finishReason The finish reason of the message. */ onFinish?: (message: Message, options: { usage: LanguageModelUsage; finishReason: LanguageModelV1FinishReason; }) => void; /** * Callback function to be called when an error is encountered. */ onError?: (error: Error) => void; }; type UseChatOptions = { /** * The API endpoint that accepts a `{ messages: Message[] }` object and returns * a stream of tokens of the AI chat response. Defaults to `/api/chat`. */ api?: string; /** * A unique identifier for the chat. If not provided, a random one will be * generated. When provided, the `useChat` hook with the same `id` will * have shared states across components. */ id?: string; /** * Initial messages of the chat. Useful to load an existing chat history. */ initialMessages?: Message$1[]; /** * Initial input of the chat. */ initialInput?: string; /** * Keeps the last message when an error happens. This will be the default behavior * starting with the next major release. * The flag was introduced for backwards compatibility and currently defaults to `false`. * Please enable it and update your error handling/resubmit behavior. */ keepLastMessageOnError?: boolean; /** * Maximum number of sequential LLM calls (steps), e.g. when you use tool calls. Must be at least 1. * * A maximum number is required to prevent infinite loops in the case of misconfigured tools. * * By default, it's set to 1, which means that only a single LLM call is made. */ maxSteps?: number; /** * A way to provide a function that is going to be used for ids for messages. * If not provided the default AI SDK `generateId` is used. */ generateId?: IdGenerator; /** * Experimental (React only). When a function is provided, it will be used * to prepare the request body for the chat API. This can be useful for * customizing the request body based on the messages and data in the chat. * * @param messages The current messages in the chat. * @param requestData The data object passed in the chat request. * @param requestBody The request body object passed in the chat request. */ experimental_prepareRequestBody?: (options: { messages: Message$1[]; requestData?: JSONValue; requestBody?: object; }) => JSONValue; } & Handlers; type UseChatActions = { /** * Append a user message to the chat list. This triggers the API call to fetch * the assistant's response. * @param message The message to append * @param options Additional options to pass to the API call */ append: (message: Message$1 | CreateMessage, options?: ChatRequestOptions) => void; /** * Reload the last AI chat response for the given chat history. If the last * message isn't from the assistant, it will request the API to generate a * new response. */ reload: (options?: ChatRequestOptions) => void; /** * Abort the current request immediately, keep the generated tokens if any. */ stop: () => void; }; type UseChatReturn = { /** Current messages in the chat */ messages: Message$1[]; /** * Update the `messages` state locally. This is useful when you want to * edit the messages on the client, and then trigger the `reload` method * manually to regenerate the AI response. */ setMessages: (messages: Message$1[] | ((messages: Message$1[]) => Message$1[])) => void; /** The current value of the input */ input: string; /** setState-powered method to update the input value */ setInput: React.Dispatch>; /** An input/textarea-ready onChange handler to control the value of the input */ /** Form submission handler to automatically reset input and append a user message */ handleSubmit: (event?: { preventDefault?: () => void; }, chatRequestOptions?: ChatRequestOptions) => void; metadata?: object; /** Additional data added on the server via StreamData. */ data?: JSONValue[]; /** * Set the data of the chat. You can use this to transform or clear the chat data. * * TODO: add to docs, the signature of function is not the same with upstream (vercel/ai) */ setData: (data?: JSONValue[]) => void; /** Whether the API request is in progress */ isLoading: boolean; /** The error object of the API request */ error?: Error; } & UseChatActions; declare const useChat: (opts: UseChatOptions) => UseChatReturn; export { useChat };