import { createContext, useContext } from 'solid-js' import type { Context } from 'solid-js' import type { ChatUIInterrupt, ChatUISelectedPart, } from '@tanstack/ai-client/ui' import type { UseChatReturn } from '../types' export interface ChatUIContexts { chatContext: Context | undefined> partContext: Context interruptContext: Context useChatContext: () => UseChatReturn } /** * Create a fresh set of chat UI contexts. This matches Form * `createFormHookContexts` and Table `createTableHookContexts`. * * Most apps can skip this. `createChatHook` uses shared module contexts by * default. Call this when a widget file cannot import the `createChatHook` * result, or when nested chats need isolated providers. * * Prefer `useChatContext` from `createChatHook` when you can import that * result. Part and interrupt widgets take `part` / `interrupt` as props. */ export function createChatHookContexts(): ChatUIContexts { const chatContext = createContext | undefined>( undefined, ) const partContext = createContext(undefined) const interruptContext = createContext(undefined) function useChatContext() { const chat = useContext(chatContext) if (!chat) { throw new Error( '`useChatContext` must be used within `UI.Provider` or `UI.Chat`.', ) } return chat } return { chatContext, partContext, interruptContext, useChatContext, } } export const defaultChatUIContexts = createChatHookContexts()