import type { AttributeValue } from '@opentelemetry/api'; import type { MultiAgentInput, MultiAgentInvokeOptions } from './multiagent.js'; import { HookableEvent } from '../hooks/events.js'; import type { HookCallback, HookableEventConstructor, HookCleanup } from '../hooks/types.js'; import type { MultiAgentPlugin } from './plugins.js'; import type { SessionManager } from '../session/session-manager.js'; import type { NodeDefinition } from './nodes.js'; import { Node } from './nodes.js'; import { MultiAgentResult } from './state.js'; import type { MultiAgent } from './multiagent.js'; import type { MultiAgentStreamEvent } from './events.js'; import type { EdgeDefinition } from './edge.js'; import { Edge } from './edge.js'; /** * Runtime configuration for graph execution. */ export interface GraphConfig { /** Max nodes executing in parallel. Defaults to `Infinity` (no limit). */ maxConcurrency?: number; /** Max total steps (prevents infinite loops in cyclic graphs). Defaults to `Infinity` (no limit). */ maxSteps?: number; /** * Wall-clock ceiling for the entire graph invocation, in milliseconds. Defaults to `Infinity` * (no limit). * * Does not propagate into nested orchestrators wrapped via `MultiAgentNode` — a nested * `Swarm`/`Graph` runs to completion under its own timeout config; the parent graph's * timeout only fires once the nested node returns. */ timeout?: number; /** * Fallback per-node wall-clock ceiling in milliseconds. Applied to any `AgentNode` that * doesn't set its own `timeout`. Defaults to `Infinity` (no limit). * * Does not apply to `MultiAgentNode`. Set `timeout`/`nodeTimeout` on the nested * orchestrator to bound it. * * Enforced via `AbortSignal` — cancellation is cooperative, so a tool that neither polls * its cancel signal nor forwards it to a cancellable API can run past this deadline. */ nodeTimeout?: number; } /** * Options for creating a Graph instance. */ export interface GraphOptions extends GraphConfig { /** Unique identifier for this graph. Defaults to `'graph'`. */ id?: string; /** Node definitions to construct the graph from. */ nodes: NodeDefinition[]; /** Edge definitions describing connections between nodes. */ edges: EdgeDefinition[]; /** Explicit source node IDs. If omitted, auto-detected from nodes with no incoming edges. */ sources?: string[]; /** Session manager for saving and restoring graph sessions. */ sessionManager?: SessionManager; /** Plugins for event-driven extensibility. */ plugins?: MultiAgentPlugin[]; /** Custom trace attributes to include on all spans. */ traceAttributes?: Record; } /** * Directed graph orchestration pattern. * * Agents execute as nodes in a dependency graph, with edges defining execution order * and optional conditions controlling routing. Source nodes (those with no incoming edges) * run first, and downstream nodes execute once all their dependencies complete. Parallel * execution is supported up to a configurable concurrency limit. * * Key design choices vs the Python SDK: * - Construction uses a declarative options object rather than a mutable GraphBuilder. * Nodes and edges are passed directly to the constructor. * - Dependency resolution uses AND semantics: a node runs only when all incoming edges * are satisfied. Python uses OR semantics, firing a node when any single incoming * edge from the completed batch is satisfied. * - Nodes are launched individually as they become ready (up to maxConcurrency). Python * executes in discrete batches, waiting for the entire batch to complete before * scheduling the next set of nodes. * - Agent nodes are stateless by default (snapshot/restore on each execution). Python * accumulates agent state across executions unless `reset_on_revisit` is enabled. * - Node failures produce a FAILED result, allowing parallel paths to continue. * MultiAgent-level limits (maxSteps) throw exceptions. Python does the inverse: * node failures throw exceptions (fail-fast), while limit violations return a * FAILED result. * * @example * ```typescript * const graph = new Graph({ * nodes: [researcher, writer], * edges: [['researcher', 'writer']], * }) * * const result = await graph.invoke('Explain quantum computing') * ``` */ export declare class Graph implements MultiAgent { readonly id: string; readonly nodes: ReadonlyMap; readonly edges: readonly Edge[]; readonly config: Required; private readonly _pluginRegistry; private readonly _hookRegistry; private readonly _sources; private readonly _tracer; readonly sessionManager?: SessionManager | undefined; private _initialized; /** * State retained across invocations when a run ends INTERRUPTED. Lets * `graph.invoke(responses)` resume on the same instance without requiring a * SessionManager, mirroring single-agent ergonomics. Cleared when a run * terminates in any non-INTERRUPTED state. */ private _pendingInterruptState?; constructor(options: GraphOptions); /** * Initialize the graph. Invokes the {@link MultiAgentInitializedEvent} callback. * Called automatically on first invocation. */ initialize(): Promise; /** * Invoke graph and return final result (consumes stream). * * @param input - The input to pass to entry point nodes * @param options - Optional per-invocation options (e.g., {@link InvocationState}) * @returns Promise resolving to the final MultiAgentResult */ invoke(input: MultiAgentInput, options?: MultiAgentInvokeOptions): Promise; /** * Register a hook callback for a specific graph event type. * * @param eventType - The event class constructor to register the callback for * @param callback - The callback function to invoke when the event occurs * @returns Cleanup function that removes the callback when invoked */ addHook(eventType: HookableEventConstructor, callback: HookCallback): HookCleanup; /** * Stream graph execution, yielding events as nodes execute. * Invokes hook callbacks for each event before yielding. * * @param input - The input to pass to entry nodes * @param options - Optional per-invocation options (e.g., {@link InvocationState}) * @returns Async generator yielding streaming events and returning a MultiAgentResult */ stream(input: MultiAgentInput, options?: MultiAgentInvokeOptions): AsyncGenerator; private _stream; /** * Invokes hook callbacks on an event, then yields it. */ private _emit; /** * Fires `BeforeNodeCallEvent` and handles hook-raised interrupts or cancels inline. * Returns a synthetic NodeResult (INTERRUPTED or CANCELLED) when a hook gates the * node, in which case the caller skips `_streamNode` and surfaces the result directly. * Returns `undefined` when no hook gated the node and execution should proceed. * * Owns the `nodeSpan` on gated paths — `_streamNode` owns it on the ungated path. * Yields the `NodeResultEvent` + `AfterNodeCallEvent` lifecycle pair on gated paths * so observers see the same event sequence regardless of how the node terminated. */ private _runBeforeNodeCall; /** * Runs a node whose `BeforeNodeCallEvent` already fired without a hook gating it * (interrupt or cancel are handled by `_runBeforeNodeCall` before this coroutine * is spawned). Takes ownership of the already-started `nodeSpan` and ends it. */ private _streamNode; private _validateConfig; private _validateSources; private _resolveNodes; private _resolveEdges; private _resolveSources; /** * Identifies terminus nodes and returns their combined content. * A terminus node is where an execution path ended: completed with no * downstream progress, or failed/cancelled. */ private _resolveContent; /** * Chooses the input for a node about to be scheduled, handling the three resume cases: * routed orchestrator-hook responses (forward leftovers to the agent), routed responses * fully consumed by the hook (replay the original invocation input), and fresh runs * (dependency-merged). Falls back to an empty input with a warning if a custom * SessionManager dropped `_pendingInput`. */ private _resolveInputForScheduling; /** * Builds the input for a node by combining the original task with dependency outputs. * * Only called for non-resume executions: the caller routes resume responses directly * to interrupted nodes without going through dependency resolution, so this helper * never sees `InterruptResponseContent[]`. */ private _resolveNodeInput; /** * Finds nodes that should execute on resume from a restored {@link MultiAgentState}. * * Any node that did not complete is a candidate for re-execution, provided its * dependencies are all COMPLETED and edge conditions are satisfied. This covers: * - PENDING nodes that never started * - EXECUTING/FAILED/CANCELLED nodes from the previous run * - Source nodes (no incoming edges) that are not COMPLETED * * Works for all node types including {@link AgentNode} and {@link MultiAgentNode} * (subgraphs/swarms). A `MultiAgentNode` that didn't complete will be re-executed * from scratch — its inner orchestrator manages its own state independently. * * @returns Array of ready nodes, or `undefined` if state was not restored (fresh start) */ private _findResumeTargets; /** * Checks whether all incoming edges have completed sources with satisfied conditions. */ private _allDependenciesSatisfied; private _checkSteps; /** * Finds downstream nodes that are ready to execute after a node completes. * A target is ready when all its incoming edge sources are COMPLETED and all edge handlers return true. * * @param node - The node that just completed execution. * @param state - Current multi-agent execution state. * @param streams - Map of node IDs to their in-flight execution promises. * @param targets - Nodes already queued for execution. * @returns Nodes that are ready to execute. */ private _findReady; } //# sourceMappingURL=graph.d.ts.map