graph

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package graph provides the StateGraph durable execution runtime.

Index

Constants

View Source
const (
	StartNode = "__start__"
	EndNode   = "__end__"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CompiledGraph

type CompiledGraph struct {
	ID      string
	Nodes   map[string]*Node
	AdjList map[string][]*Edge // from -> edges
	Entry   string
}

CompiledGraph is the immutable, validated graph ready for execution.

type Edge

type Edge struct {
	From      string
	To        string        // static target (mutually exclusive with Condition)
	Condition EdgeCondition // dynamic routing (mutually exclusive with To)
}

Edge represents a transition between nodes.

type EdgeCondition

type EdgeCondition func(state State) string

EdgeCondition decides which node to transition to based on state. Returns the target node ID.

type Message

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

Message records a message exchanged during the run.

type Node

type Node struct {
	ID string
	Fn NodeFunc
	// Interrupt, if true, causes the runner to checkpoint and pause before executing this node.
	Interrupt bool
}

Node represents a node in the state graph.

type NodeFunc

type NodeFunc func(ctx context.Context, state State) (State, error)

NodeFunc is the function signature for a graph node. It receives the current state and returns updated state.

type RunState

type RunState struct {
	RunID       string    `json:"run_id"`
	SessionID   string    `json:"session_id"`
	GraphID     string    `json:"graph_id"`
	CurrentNode string    `json:"current_node"`
	Status      RunStatus `json:"status"`
	State       State     `json:"state"`
	SeqNum      int64     `json:"seq_num"`
	StartedAt   time.Time `json:"started_at"`
	UpdatedAt   time.Time `json:"updated_at"`

	// Rich output (Agno-inspired)
	Messages   []Message        `json:"messages,omitempty"`
	ToolCalls  []ToolCallRecord `json:"tool_calls,omitempty"`
	TotalUsage UsageStats       `json:"total_usage"`
}

RunState captures the full runtime state for a single graph execution.

type RunStatus

type RunStatus string

RunStatus represents the status of a graph run.

const (
	RunStatusRunning   RunStatus = "running"
	RunStatusPaused    RunStatus = "paused"
	RunStatusCompleted RunStatus = "completed"
	RunStatusFailed    RunStatus = "failed"
)

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner executes a CompiledGraph with durable checkpointing.

func NewRunner

func NewRunner(g *CompiledGraph, store storage.Storage) *Runner

NewRunner creates a runner for the given compiled graph.

func (*Runner) Resume

func (r *Runner) Resume(ctx context.Context, sessionID string) (*RunState, error)

Resume continues execution from the latest checkpoint for the given session.

func (*Runner) ResumeFromCheckpoint

func (r *Runner) ResumeFromCheckpoint(ctx context.Context, checkpointID string) (*RunState, error)

ResumeFromCheckpoint resumes from a specific checkpoint (time-travel).

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, sessionID string, initial State) (*RunState, error)

Run starts a new execution of the graph with the given initial state.

func (*Runner) Stream

func (r *Runner) Stream() <-chan StreamEvent

Stream returns a channel of execution events for real-time observability.

func (*Runner) WithBroker

func (r *Runner) WithBroker(b *stream.Broker) *Runner

WithBroker attaches an SSE Broker so the runner publishes events to SSE subscribers.

func (*Runner) WithTracer

func (r *Runner) WithTracer(t *trace.Collector) *Runner

WithTracer attaches a trace.Collector for span-based execution tracing.

type State

type State map[string]any

State is the mutable state bag passed through graph execution.

type StateGraph

type StateGraph struct {
	// contains filtered or unexported fields
}

StateGraph is a builder for defining directed graphs with durable execution.

func New

func New(id string) *StateGraph

New creates a new StateGraph with the given ID.

func (*StateGraph) AddConditionalEdge

func (g *StateGraph) AddConditionalEdge(from string, condition EdgeCondition) *StateGraph

AddConditionalEdge adds a dynamic edge that routes based on state.

func (*StateGraph) AddEdge

func (g *StateGraph) AddEdge(from, to string) *StateGraph

AddEdge adds a static edge from one node to another.

func (*StateGraph) AddInterruptNode

func (g *StateGraph) AddInterruptNode(id string, fn NodeFunc) *StateGraph

AddInterruptNode registers a node that will pause execution for human approval.

func (*StateGraph) AddNode

func (g *StateGraph) AddNode(id string, fn NodeFunc) *StateGraph

AddNode registers a node with the given ID and handler function.

func (*StateGraph) Compile

func (g *StateGraph) Compile() (*CompiledGraph, error)

Compile validates the graph and returns a CompiledGraph.

func (*StateGraph) SetEntryPoint

func (g *StateGraph) SetEntryPoint(nodeID string) *StateGraph

SetEntryPoint sets the starting node of the graph.

func (*StateGraph) SetFinishPoint

func (g *StateGraph) SetFinishPoint(nodeID string) *StateGraph

SetFinishPoint sets the ending node of the graph.

type StreamEvent

type StreamEvent struct {
	Type      string    `json:"type"` // node_start, node_end, edge_transition, checkpoint, interrupt, error
	NodeID    string    `json:"node_id,omitempty"`
	State     State     `json:"state,omitempty"`
	Error     string    `json:"error,omitempty"`
	Timestamp time.Time `json:"timestamp"`
}

StreamEvent is emitted during graph execution for real-time observability.

type ToolCallRecord

type ToolCallRecord struct {
	Name   string         `json:"name"`
	Args   map[string]any `json:"args"`
	Result any            `json:"result"`
	Error  string         `json:"error,omitempty"`
}

ToolCallRecord records a tool invocation during the run.

type UsageStats

type UsageStats struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

UsageStats tracks cumulative token usage across a run.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL