import React, { createContext, useContext, ReactNode } from "react"; import { useStream } from "@langchain/langgraph-sdk/react"; import { type Message } from "@langchain/langgraph-sdk"; import type { UIMessage, RemoveUIMessage, } from "@langchain/langgraph-sdk/react-ui/types"; import { useQueryParam, StringParam } from "use-query-params"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { LangGraphLogoSVG } from "@/components/icons/langgraph"; import { Label } from "@/components/ui/label"; import { ArrowRight } from "lucide-react"; 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); const StreamSession = ({ children, apiUrl, assistantId, }: { children: ReactNode; apiUrl: string; assistantId: string; }) => { const [threadId, setThreadId] = useQueryParam("threadId", StringParam); const streamValue = useTypedStream({ apiUrl, assistantId, threadId: threadId ?? null, onThreadId: setThreadId, }); return ( {children} ); }; export const StreamProvider: React.FC<{ children: ReactNode }> = ({ children, }) => { const [apiUrl, setApiUrl] = useQueryParam("apiUrl", StringParam); const [assistantId, setAssistantId] = useQueryParam( "assistantId", StringParam, ); if (!apiUrl || !assistantId) { return (

LangGraph Chat

Welcome to LangGraph Chat! Before you get started, you need to enter the URL of the deployment and the assistant / graph ID.

{ e.preventDefault(); const form = e.target as HTMLFormElement; const formData = new FormData(form); const apiUrl = formData.get("apiUrl") as string; const assistantId = formData.get("assistantId") as string; setApiUrl(apiUrl); setAssistantId(assistantId); console.log({ apiUrl, assistantId }); form.reset(); }} className="flex flex-col gap-6 p-6 bg-muted/50" >

This is the URL of your LangGraph deployment. Can be a local, or production deployment.

setApiUrl(e.target.value)} />

This is the ID of the graph (can be the graph name), or assistant to fetch threads from, and invoke when actions are taken.

setAssistantId(e.target.value)} />
); } 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;