import * as jotai_vanilla from 'jotai/vanilla'; import { Getter, Setter, PrimitiveAtom } from 'jotai/vanilla'; import { FetchFunction, ToolCall } from '@ai-sdk/provider-utils'; import { UseChatOptions, Message, JSONValue, CreateMessage, ChatRequestOptions } from '@ai-sdk/ui-utils'; import { FormEvent } from 'react'; import { LanguageModelV1FinishReason } from '@ai-sdk/provider'; import { LanguageModelUsage } from 'ai'; declare function chatAtoms(chatOptions?: Omit & { onFinish?: (get: Getter, set: Setter, message: Message) => void; onResponse?: (get: Getter, set: Setter, response: Response) => void | Promise; onError?: (get: Getter, set: Setter, error: Error) => void; maxToolRoundtrips?: number; /** * 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?: (get: Getter, set: Setter, options: { messages: Message[]; requestData?: JSONValue; requestBody?: object; }) => Record; experimental_onFunctionCall?: (get: Getter, set: Setter, ...args: Parameters['onToolCall']>) => ReturnType['onToolCall']>; initialMessages?: Message[] | Promise | (() => Message[] | Promise); }): { messagesAtom: jotai_vanilla.WritableAtom, [messages: Message[]], Promise>; dataAtom: jotai_vanilla.Atom; isLoadingAtom: jotai_vanilla.Atom; isPendingAtom: jotai_vanilla.Atom>; inputAtom: jotai_vanilla.WritableAtom; appendAtom: jotai_vanilla.WritableAtom; } | undefined], Promise>; submitAtom: jotai_vanilla.WritableAtom, options?: ChatRequestOptions | undefined, metadata?: Object | undefined], Promise | undefined>; reloadAtom: jotai_vanilla.WritableAtom> & { init: null; }; stopAtom: jotai_vanilla.WritableAtom & { init: null; }; }; type ExtraMetadata = { /** * The credentials mode to be used for the fetch request. * Possible values are: 'omit', 'same-origin', 'include'. * Defaults to 'same-origin'. */ credentials?: RequestCredentials; /** * HTTP headers to be sent with the API request. */ headers?: HeadersInit; /** * Extra body object to be sent with the API request. * @example * Send a `sessionId` to the API along with the messages. * ```js * useChat({ * body: { * sessionId: '123', * } * }) * ``` */ body?: Record; }; 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 MakeChatAtomsOptions = { messagesAtom: PrimitiveAtom; /** * 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; /** * Custom fetch implementation. You can use it as a middleware to intercept requests, * or to provide a custom fetch implementation for e.g. testing. */ fetch?: FetchFunction; /** *Streaming protocol that is used. Defaults to `data`. */ streamProtocol?: 'data' | 'text'; generateId?: () => string; /** * 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[]; requestData?: JSONValue; requestBody?: object; }) => Record; /** * Whether to send extra message fields such as `message.id` and `message.createdAt` to the API. * Defaults to `false`. When set to `true`, the API endpoint might need to * handle the extra fields before forwarding the request to the AI service. */ sendExtraMessageFields?: 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; } & ExtraMetadata & Handlers; declare function makeChatAtoms(opts: MakeChatAtomsOptions): { stopAtom: jotai_vanilla.WritableAtom & { init: null; }; appendAtom: jotai_vanilla.WritableAtom> & { init: null; }; reloadAtom: jotai_vanilla.WritableAtom> & { init: null; }; dataAtom: PrimitiveAtom & { init: JSONValue[] | undefined; }; isLoadingAtom: jotai_vanilla.Atom; errorAtom: jotai_vanilla.Atom; onResponseAtom: PrimitiveAtom<((response: Response) => void) | undefined> & { init: ((response: Response) => void) | undefined; }; onFinishAtom: PrimitiveAtom<((message: Message, options: { usage: LanguageModelUsage; finishReason: LanguageModelV1FinishReason; }) => void) | undefined> & { init: ((message: Message, options: { usage: LanguageModelUsage; finishReason: LanguageModelV1FinishReason; }) => void) | undefined; }; onToolCallAtom: PrimitiveAtom<(({ toolCall }: { toolCall: ToolCall; }) => void) | undefined> & { init: (({ toolCall }: { toolCall: ToolCall; }) => void) | undefined; }; onErrorAtom: PrimitiveAtom<((error: Error) => void) | undefined> & { init: ((error: Error) => void) | undefined; }; }; export { chatAtoms, makeChatAtoms };