const STREAM_MODES = new Set([ "values", "updates", "checkpoints", "tasks", "debug", "messages", "custom", "events", "tools", "lifecycle", ]); const SUBAGENT_SEGMENT = "tools:"; /** * Which events a run stream that asked for `namespace` keeps. A conversation * is a `|`-joined namespace (`""` is the root graph): the graph output at or * below it, except below a subagent it starts. A subgraph's lifecycle event * shows in every conversation above it. Events about the whole run always * pass. This is the SDK's `inNamespaces`, copied since the SDK is only an * optional peer of this package, except that a `messages` event also passes * by its own namespace, as SDKs before 1.18 read it. Clients filter these * streams again by their own SDK's rule, so the server must keep every event * either rule keeps. */ export function inNamespaces( namespaces: readonly string[] | undefined ): ((part: { event: string; data: unknown }) => boolean) | undefined { if (namespaces == null || namespaces.length === 0) return undefined; return ({ event, data }) => { // A too_large gap stands in for the event it replaced. const name = event === "gap" ? (data as { mode?: string } | null)?.mode || event : event; const bar = name.indexOf("|"); const base = bar === -1 ? name : name.slice(0, bar); const named = bar === -1 ? "" : name.slice(bar + 1); if (!isGraphOutput(base)) return true; const candidates = [named]; if (base === "messages" && Array.isArray(data)) { const eventNamespace = named ? named.split("|") : undefined; candidates.push(messageNamespace(eventNamespace, data[1]).join("|")); } return candidates.some((namespace) => namespaces.some((conversation) => { const rest = below(namespace, conversation); if (rest === undefined) return false; return base === "lifecycle" ? rest !== "" : !entersSubagent(rest); }) ); }; } /** * The namespace of the graph a `messages` event's message came from: the * event's own namespace, except where old JS cores name a node's messages * with the node's own task. Their event namespace equals the message's * `langgraph_checkpoint_ns`, which ends with that task, so the graph is the * namespace without it and the tools node's own `ToolMessage` * (`tools:`) is the root graph's. An event with no namespace is read * the same way from `langgraph_checkpoint_ns`, or from the `checkpoint_ns` * old JS cores send. deepagents puts the `task` call on a subagent's * messages, which keep their last segment, since a subagent can write from * the tool itself. */ function messageNamespace( eventNamespace: string[] | undefined, metadata: Record | undefined ): string[] { const named = metadata?.langgraph_checkpoint_ns; if (eventNamespace?.length && named !== eventNamespace.join("|")) { return eventNamespace; } const namespace = typeof named === "string" && named !== "" ? named : metadata?.checkpoint_ns; if (typeof namespace !== "string" || namespace === "") { return eventNamespace ?? []; } const segments = namespace.split("|"); return typeof metadata?.lc_subagent_tool_call_id === "string" ? segments : segments.slice(0, -1); } function isGraphOutput(base: string): boolean { // Protocol-v2 runs name a custom channel's events `custom:`, and // legacy `messages/*` events carry no namespace, so they count as root. return STREAM_MODES.has(base.split(":")[0]) || base.startsWith("messages/"); } /** The segments of `namespace` below `prefix`, or `undefined` if it isn't there. */ function below(namespace: string, prefix: string): string | undefined { if (prefix === "") return namespace; if (namespace === prefix) return ""; if (namespace.startsWith(prefix) && namespace[prefix.length] === "|") { return namespace.slice(prefix.length + 1); } return undefined; } function entersSubagent(rest: string): boolean { return ( rest.startsWith(SUBAGENT_SEGMENT) || rest.includes(`|${SUBAGENT_SEGMENT}`) ); }