{"version":3,"file":"context.cjs","names":["useStream"],"sources":["../src/context.tsx"],"sourcesContent":["/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */\n\n\"use client\";\n\nimport { createContext, useContext, type ReactNode } from \"react\";\nimport {\n  useStream,\n  type UseStreamOptions,\n  type UseStreamReturn,\n  type AgentServerOptions,\n  type CustomAdapterOptions,\n} from \"./use-stream.js\";\nimport type { InferStateType } from \"@langchain/langgraph-sdk/stream\";\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst StreamContext = createContext<any | null>(null);\n\n/**\n * Props for {@link StreamProvider} when talking to the default\n * LangGraph Platform agent server. Mirrors {@link AgentServerOptions}\n * plus `children`.\n */\nexport type StreamProviderProps<T = Record<string, unknown>> =\n  AgentServerOptions<InferStateType<T>> & {\n    children: ReactNode;\n  };\n\n/**\n * Props for {@link StreamProvider} when wiring a custom\n * {@link AgentServerAdapter}. Mirrors {@link CustomAdapterOptions}\n * plus `children`.\n */\nexport type StreamProviderCustomProps<T = Record<string, unknown>> =\n  CustomAdapterOptions<InferStateType<T>> & {\n    children: ReactNode;\n  };\n\n/**\n * Provides a shared {@link useStream} instance to all descendants via\n * React Context.\n *\n * Use `StreamProvider` when multiple components in a subtree need\n * access to the same stream state (messages, loading status, errors,\n * interrupts, …) without prop drilling.\n *\n * @example\n * ```tsx\n * import { StreamProvider, useStreamContext } from \"@langchain/react\";\n *\n * function App() {\n *   return (\n *     <StreamProvider assistantId=\"agent\" apiUrl=\"http://localhost:2024\">\n *       <ChatHeader />\n *       <MessageList />\n *       <MessageInput />\n *     </StreamProvider>\n *   );\n * }\n *\n * function ChatHeader() {\n *   const { isLoading, error } = useStreamContext();\n *   return (\n *     <header>\n *       {isLoading && <span>Thinking...</span>}\n *       {error && <span>Error</span>}\n *     </header>\n *   );\n * }\n * ```\n *\n * Multiple providers can be nested for multi-agent scenarios:\n *\n * @example\n * ```tsx\n * <StreamProvider assistantId=\"researcher\" apiUrl=\"http://localhost:2024\">\n *   <ResearchPanel />\n * </StreamProvider>\n * <StreamProvider assistantId=\"writer\" apiUrl=\"http://localhost:2024\">\n *   <WriterPanel />\n * </StreamProvider>\n * ```\n */\nexport function StreamProvider<T = Record<string, unknown>>(\n  props: StreamProviderProps<T> | StreamProviderCustomProps<T>\n): ReactNode {\n  const { children, ...options } = props;\n  const stream = useStream<T>(options as UseStreamOptions<InferStateType<T>>);\n\n  return (\n    <StreamContext.Provider value={stream}>{children}</StreamContext.Provider>\n  );\n}\n\n/**\n * Accesses the shared stream instance from the nearest\n * {@link StreamProvider}. Throws if called outside of one.\n *\n * @example\n * ```tsx\n * function MessageList() {\n *   const { messages } = useStreamContext();\n *   return messages.map((m, i) => <div key={m.id ?? i}>{String(m.content)}</div>);\n * }\n * ```\n *\n * @example With a type parameter for full type safety:\n * ```tsx\n * import type { agent } from \"./agent\";\n *\n * function Chat() {\n *   const { toolCalls } = useStreamContext<typeof agent>();\n * }\n * ```\n */\nexport function useStreamContext<\n  T = Record<string, unknown>,\n>(): UseStreamReturn<T> {\n  const context = useContext(StreamContext);\n  if (context === null) {\n    throw new Error(\n      \"useStreamContext must be used within a <StreamProvider>. \" +\n        \"Wrap your component tree with <StreamProvider> or use useStream() directly.\"\n    );\n  }\n  return context as UseStreamReturn<T>;\n}\n"],"mappings":";;;;;AAeA,MAAM,iBAAA,GAAA,MAAA,eAA0C,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmErD,SAAgB,eACd,OACW;CACX,MAAM,EAAE,UAAU,GAAG,YAAY;CACjC,MAAM,SAASA,mBAAAA,UAAa,QAA+C;AAE3E,QACE,iBAAA,GAAA,kBAAA,KAAC,cAAc,UAAf;EAAwB,OAAO;EAAS;EAAkC,CAAA;;;;;;;;;;;;;;;;;;;;;;;AAyB9E,SAAgB,mBAEQ;CACtB,MAAM,WAAA,GAAA,MAAA,YAAqB,cAAc;AACzC,KAAI,YAAY,KACd,OAAM,IAAI,MACR,uIAED;AAEH,QAAO"}