Open History to see the last commit
1import { useChat as useUnboundChat } from './use-chat'
2import type { AnyClientTool, InterruptDefinition } from '@tanstack/ai/client'
3import type { InferredClientContext } from '@tanstack/ai-client'
4import type { UseChatOptions } from './types'
5
6type ChatHookOverrides<
7 TTools extends ReadonlyArray<AnyClientTool>,
8 TContext,
9 TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>>,
10> = {
11 threadId?: string
12 live?: boolean
13 forwardedProps?: Record<string, any>
14 body?: Record<string, any>
15 initialMessages?: UseChatOptions<
16 TTools,
17 TContext,
18 TInterrupts
19 >['initialMessages']
20}
21
22/**
23 * Bind chat options once at module scope. The returned `useChat` hook
24 * creates a chat instance from those options.
25 *
26 * Pass per-call overrides for instance keys such as `threadId`,
27 * `initialMessages`, `live`, and `forwardedProps`. Do not change `tools`,
28 * `interrupts`, or `outputSchema` here. Those stay on the factory options.
29 */
30export function createChatHook<
31 const TTools extends ReadonlyArray<AnyClientTool> = any,
32 TContext = InferredClientContext<TTools>,
33 const TInterrupts extends ReadonlyArray<
34 InterruptDefinition<any, any, any, any>
35 > = readonly [],
36>(options: UseChatOptions<TTools, TContext, TInterrupts>) {
37 function useChat(
38 overrides?: ChatHookOverrides<TTools, TContext, TInterrupts>,
39 ) {
40 if (!overrides) {
41 return useUnboundChat(options)
42 }
43 return useUnboundChat({ ...options, ...overrides })
44 }
45
46 return { useChat }
47}