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