/** * Stream Context Provider * * Manages streaming state using React Context and useReducer. * Provides state and dispatch to child components via separate contexts * following the split-context pattern for optimal re-render performance. */ import React from "react"; import type { InitialInputMessage } from "../types/message.js"; import type { TamboThread } from "@tambo-ai/client"; import { type StreamAction, type StreamState } from "@tambo-ai/client"; /** * Thread management functions exposed by the stream context. */ export interface ThreadManagement { /** * Initialize a new thread in the stream context. * Use this before sending messages to a new thread. * @param threadId - The thread ID to initialize * @param initialThread - Optional initial thread data */ initThread: (threadId: string, initialThread?: Partial) => void; /** * Switch the current active thread. * Does not fetch thread data - use useTamboThread for that. * @param threadId - The thread ID to switch to */ switchThread: (threadId: string) => void; /** * Start a new thread (generates a temporary ID). * The actual thread ID will be assigned when the first message is sent. * @returns The temporary thread ID */ startNewThread: () => string; } /** * Props for TamboStreamProvider */ export interface TamboStreamProviderProps { children: React.ReactNode; /** * Initial messages to populate the placeholder thread with. * These render in the UI before any API call is made. */ initialMessages?: InitialInputMessage[]; /** * Optional override for stream state (primarily for tests). * If provided, you must also provide `dispatch`. */ state?: StreamState; /** * Optional override for stream dispatch (primarily for tests). * If provided, you must also provide `state`. */ dispatch?: React.Dispatch; /** * Optional override for thread management functions (primarily for tests). */ threadManagement?: ThreadManagement; } /** * Provider component for stream state management. * * Uses useReducer with streamReducer to accumulate AG-UI events into * thread state. Provides state, dispatch, and thread management via separate contexts. * * Thread management is done programmatically via the hooks: * - startNewThread() - Start a new conversation * - switchThread(threadId) - Switch to an existing thread * - initThread(threadId) - Initialize a thread for receiving events * @returns JSX element wrapping children with stream contexts * @example * ```tsx * * * * ``` */ export declare function TamboStreamProvider(props: TamboStreamProviderProps): React.JSX.Element; /** * Hook to access stream state. * * Must be used within TamboStreamProvider. * @returns Current stream state * @throws {Error} if used outside TamboStreamProvider * @example * ```tsx * function ChatMessages() { * const { thread, streaming } = useStreamState(); * * return ( *
* {thread.messages.map(msg => )} * {streaming.status === 'streaming' && } *
* ); * } * ``` */ export declare function useStreamState(): StreamState; /** * Hook to access stream dispatch function. * * Must be used within TamboStreamProvider. * @returns Dispatch function for sending events to reducer * @throws {Error} if used outside TamboStreamProvider * @example * ```tsx * function StreamHandler() { * const dispatch = useStreamDispatch(); * * useEffect(() => { * async function handleStream() { * for await (const event of streamEvents) { * dispatch({ type: 'EVENT', event }); * } * } * handleStream(); * }, [dispatch]); * * return null; * } * ``` */ export declare function useStreamDispatch(): React.Dispatch; /** * Hook to access thread management functions. * * Must be used within TamboStreamProvider. * @returns Thread management functions * @throws {Error} if used outside TamboStreamProvider * @example * ```tsx * function ThreadSwitcher() { * const { switchThread, startNewThread } = useThreadManagement(); * * return ( *
* * *
* ); * } * ``` */ export declare function useThreadManagement(): ThreadManagement; //# sourceMappingURL=tambo-v1-stream-context.d.ts.map