import React, { createContext, useContext, ReactNode } from "react"; import { useStream } from "@langchain/langgraph-sdk/react"; import { Client, type Message } from "@langchain/langgraph-sdk"; import type { UIMessage, RemoveUIMessage, } from "@langchain/langgraph-sdk/react-ui/types"; import { useQueryParam, StringParam } from 'use-query-params'; const useTypedStream = useStream< { messages: Message[]; ui: UIMessage[] }, { UpdateType: { messages?: Message[] | Message | string; ui?: (UIMessage | RemoveUIMessage)[] | UIMessage | RemoveUIMessage; }; CustomUpdateType: UIMessage | RemoveUIMessage; } >; type StreamContextType = ReturnType; const StreamContext = createContext(undefined); export const StreamProvider: React.FC<{ children: ReactNode }> = ({ children, }) => { const [threadId, setThreadId] = useQueryParam('threadId', StringParam); const streamValue = useTypedStream({ apiUrl: "http://localhost:2024", assistantId: "agent", threadId: threadId || "", onThreadId: setThreadId, }); return ( {children} ); }; // Create a custom hook to use the context export const useStreamContext = (): StreamContextType => { const context = useContext(StreamContext); if (context === undefined) { throw new Error("useStreamContext must be used within a StreamProvider"); } return context; }; export default StreamContext;