import { useChat as useUnboundChat } from './use-chat' import type { AnyClientTool, InterruptDefinition, SchemaInput, } from '@tanstack/ai' import type { InferredClientContext } from '@tanstack/ai-client' import type { UseChatOptions } from './types' type ChatHookOverrides< TTools extends ReadonlyArray, TSchema extends SchemaInput | undefined, TContext, TInterrupts extends ReadonlyArray>, > = { threadId?: string live?: boolean forwardedProps?: Record body?: Record initialMessages?: UseChatOptions< TTools, TSchema, TContext, TInterrupts >['initialMessages'] } /** * Bind chat options once at module scope. The returned `useChat` primitive * creates a chat instance from those options. * * Pass per-call overrides for instance keys such as `threadId`, * `initialMessages`, `live`, and `forwardedProps`. Do not change `tools`, * `interrupts`, or `outputSchema` here. Those stay on the factory options. * * Rename the primitive at the call site if you already import `useChat` from * `@tanstack/ai-solid`: `const { useChat: useAppChat } = createChatHook(chatOptions)`. * * @example * ```tsx * const { useChat } = createChatHook(chatOptions) * * function Support() { * const chat = useChat({ threadId: 'support-1' }) * return * } * ``` */ export function createChatHook< const TTools extends ReadonlyArray = any, TSchema extends SchemaInput | undefined = undefined, TContext = InferredClientContext, const TInterrupts extends ReadonlyArray< InterruptDefinition > = readonly [], >(options: UseChatOptions) { function useChat( overrides?: ChatHookOverrides, ) { if (!overrides) { return useUnboundChat(options) } return useUnboundChat({ ...options, ...overrides }) } return { useChat } }