import { useCallback, useMemo, useState } from "react"; import { useMessages, useStream, type AnyStream, type SubgraphDiscoverySnapshot, } from "@langchain/react"; import { HumanMessage } from "langchain"; import type { agent as nestedStateGraphType } from "../agents/nested-stategraph"; import { API_URL, type Transport } from "../api"; import { Composer } from "../components/Composer"; import { JsonPanel } from "../components/JsonPanel"; import { MessageFeed } from "../components/MessageFeed"; import { RecentEvents } from "../components/RecentEvents"; import { ViewShell } from "../components/ViewShell"; import { formatNamespace, getSubagentPreview } from "../utils"; import { useEventTrace } from "./shared"; const ASSISTANT_ID = "nested-stategraph"; const SUGGESTIONS = [ "Give me a briefing on the risks of rolling out the new streaming SDK.", "Research the current state of ephemeral dev agents and rank the risks.", ]; export function NestedStateGraphView({ transport }: { transport: Transport }) { const [threadId, setThreadId] = useState(null); const stream = useStream({ assistantId: ASSISTANT_ID, apiUrl: API_URL, transport, threadId, onThreadId: setThreadId, }); const eventTrace = useEventTrace(stream); const handleSubmit = useCallback( (content: string) => { void stream.submit({ messages: [new HumanMessage(content)], }); }, [stream] ); const subgraphs = useMemo( () => Array.from(stream.subgraphs.values()), [stream.subgraphs] ); return ( An orchestrator graph dispatches two compiled subgraphs — a research loop and a risk analyst — before handing off to a writer. Parent state only carries the root conversation plus distilled{" "} researchBrief / riskReport artifacts, so the main feed stays clean while each subgraph streams its full tool-calling chatter in its own namespace. The sidebar lazily mounts a per-subgraph useMessages subscription only when its card renders. } error={stream.error} >
{SUGGESTIONS.map((suggestion) => ( ))}

Root Conversation

{stream.isLoading ? "Streaming..." : "Idle"}
); } /** * Mount = open a `messages` subscription for the subgraph's namespace. * Unmount = ref-count drops to zero and the subscription closes. Two * cards viewing the same subgraph share a single server subscription. */ function SubgraphCard({ stream, subgraph, }: { stream: AnyStream; subgraph: SubgraphDiscoverySnapshot; }) { const messages = useMessages(stream, subgraph); const preview = getSubagentPreview(messages); const nodeLabel = subgraph.namespace[0] ?? "subgraph"; return (
{nodeLabel} {subgraph.status}
Namespace: {formatNamespace([...subgraph.namespace])}
{messages.length} streamed message{messages.length === 1 ? "" : "s"}
{preview ?
{preview}
: null}
); }