workflow

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRunAlreadyExists = errors.New("workflow run already exists")

ErrRunAlreadyExists is returned when StartRun is called with an existing run ID.

View Source
var ErrRunNotFound = errors.New("workflow run not found")

ErrRunNotFound is returned when a workflow run does not exist in a RunStore.

Functions

This section is empty.

Types

type Context

type Context struct {
	context.Context

	SessionID    string
	InvocationID string
	State        map[string]any
}

Context is passed to workflow nodes during graph execution.

type Edge

type Edge struct {
	From  Node
	To    Node
	Route Route
}

Edge connects two workflow nodes. A nil Route is an unconditional edge.

func Chain

func Chain(nodes ...Node) []Edge

Chain wires nodes into a sequential route.

func Concat

func Concat(groups ...[]Edge) []Edge

Concat joins multiple edge slices.

type EdgeBuilder

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

EdgeBuilder provides a fluent way to assemble graph edges.

func NewEdgeBuilder

func NewEdgeBuilder() *EdgeBuilder

func (*EdgeBuilder) Add

func (b *EdgeBuilder) Add(from, to Node) *EdgeBuilder

func (*EdgeBuilder) AddFanIn

func (b *EdgeBuilder) AddFanIn(sources []Node, target Node) *EdgeBuilder

func (*EdgeBuilder) AddFanOut

func (b *EdgeBuilder) AddFanOut(from Node, targets ...Node) *EdgeBuilder

func (*EdgeBuilder) AddRoute

func (b *EdgeBuilder) AddRoute(from, to Node, route Route) *EdgeBuilder

func (*EdgeBuilder) Build

func (b *EdgeBuilder) Build() []Edge

type EmitFunc

type EmitFunc func(*Event) error

EmitFunc lets a node publish one or more explicit workflow events.

type Event

type Event struct {
	Output   any
	Message  string
	Routes   []any
	Metadata map[string]string
}

Event carries node output and optional route selections to successor edges.

type FileRunStore added in v1.13.5

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

FileRunStore stores each workflow run as an atomically replaced JSON file. It is useful for local deployments and can be replaced with a database-backed RunStore in production.

func NewFileRunStore added in v1.13.5

func NewFileRunStore(dir string) (*FileRunStore, error)

NewFileRunStore creates a JSON-backed RunStore rooted at dir.

func (*FileRunStore) Create added in v1.13.5

func (s *FileRunStore) Create(ctx context.Context, state *RunState) error

func (*FileRunStore) Delete added in v1.13.5

func (s *FileRunStore) Delete(ctx context.Context, runID string) error

func (*FileRunStore) Load added in v1.13.5

func (s *FileRunStore) Load(ctx context.Context, runID string) (*RunState, error)

func (*FileRunStore) Save added in v1.13.5

func (s *FileRunStore) Save(ctx context.Context, state *RunState) error

type Graph

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

Graph executes workflow nodes according to explicit edges and routes.

func NewGraph

func NewGraph(edges []Edge, config GraphConfig) (*Graph, error)

NewGraph validates and constructs a workflow graph.

func (*Graph) Edges

func (g *Graph) Edges() []Edge

Edges returns a copy of graph edges.

func (*Graph) ResumeRun added in v1.13.5

func (g *Graph) ResumeRun(ctx context.Context, store RunStore, runID string) (any, error)

ResumeRun loads a previously started durable run and continues it. Resuming a completed run returns its persisted result without invoking nodes again.

func (*Graph) Run

func (g *Graph) Run(ctx context.Context, sessionID string, input any) (any, error)

Run executes the graph from START with input as the first node input.

func (*Graph) StartRun added in v1.13.5

func (g *Graph) StartRun(ctx context.Context, store RunStore, runID, sessionID string, input any) (any, error)

StartRun creates and executes a durable workflow run. Every completed node transition is saved to store before the next node begins. If execution is interrupted or a node returns an error, call ResumeRun with the same ID.

type GraphConfig

type GraphConfig struct {
	MaxSteps    int
	JoinTimeout time.Duration
}

GraphConfig configures workflow graph execution.

type InMemoryRunStore added in v1.13.5

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

InMemoryRunStore is a JSON-round-tripping RunStore suited to tests and single-process development. It enforces the same serialization requirements as FileRunStore so that a run which works in tests can be persisted later.

func NewInMemoryRunStore added in v1.13.5

func NewInMemoryRunStore() *InMemoryRunStore

NewInMemoryRunStore creates an empty in-memory durable workflow store.

func (*InMemoryRunStore) Create added in v1.13.5

func (s *InMemoryRunStore) Create(ctx context.Context, state *RunState) error

func (*InMemoryRunStore) Delete added in v1.13.5

func (s *InMemoryRunStore) Delete(ctx context.Context, runID string) error

func (*InMemoryRunStore) Load added in v1.13.5

func (s *InMemoryRunStore) Load(ctx context.Context, runID string) (*RunState, error)

func (*InMemoryRunStore) Save added in v1.13.5

func (s *InMemoryRunStore) Save(ctx context.Context, state *RunState) error

type JoinFunc added in v1.13.2

type JoinFunc func(Context, map[string]any) (any, error)

JoinFunc combines the outputs of every direct predecessor of a JoinNode. The map is keyed by the predecessor node name.

type JoinState added in v1.13.5

type JoinState struct {
	Values       map[string]any `json:"values"`
	FirstArrival time.Time      `json:"first_arrival"`
}

JoinState holds the accumulated predecessor outputs for a join node.

type Node

type Node interface {
	Name() string
	Description() string
	// contains filtered or unexported methods
}

Node is an executable step in a workflow graph.

var Start Node = startNode{}

Start is the synthetic graph entry point used in edges and chains.

func NewAgentNode

func NewAgentNode(name string, runner SessionGenerator, config NodeConfig) Node

NewAgentNode wraps a session-aware agent as a graph node.

func NewEmittingFunctionNode

func NewEmittingFunctionNode[I, O any](name string, fn func(Context, I, EmitFunc) (O, error), config NodeConfig) Node

NewEmittingFunctionNode wraps a function that can emit explicit Events. Returning nil and emitting no events suppresses automatic output.

func NewFunctionNode

func NewFunctionNode[I, O any](name string, fn func(Context, I) (O, error), config NodeConfig) Node

NewFunctionNode wraps a plain Go function as a workflow node. The returned value is forwarded to successor nodes through Event.Output.

func NewJoinNode added in v1.13.2

func NewJoinNode(name string, fn JoinFunc, config NodeConfig) Node

NewJoinNode creates a barrier node. It runs once after every direct predecessor has produced one output for the current graph invocation. The reducer receives a map keyed by predecessor node name. A join needs at least two distinct direct predecessors; NewGraph validates that requirement.

func NewToolNode

func NewToolNode(name string, tool goagent.Tool, config NodeConfig) Node

NewToolNode wraps an agent.Tool as a graph node. Map inputs are passed as arguments; other inputs are passed as the "input" argument.

type NodeConfig

type NodeConfig struct {
	Description string
}

NodeConfig carries metadata for workflow nodes.

type QueuedNode added in v1.13.5

type QueuedNode struct {
	From  string `json:"from"`
	Node  string `json:"node"`
	Input any    `json:"input"`
}

QueuedNode is a node invocation waiting to be executed as part of a durable workflow run. Node names are persisted instead of Node implementations so a run can be resumed after a process restart.

type Route

type Route interface {
	Match(Event) bool
	// contains filtered or unexported methods
}

Route selects successor edges based on emitted Event.Routes.

var Default Route = routeFunc{
	// contains filtered or unexported fields
}

Default matches only when no non-default route from the same node matches.

func BoolRoute

func BoolRoute(value bool) Route

BoolRoute matches an emitted bool route value.

func IntRoute

func IntRoute(value int) Route

IntRoute matches an emitted int route value.

func MultiRoute

func MultiRoute[T comparable](values ...T) Route

MultiRoute matches any emitted route value in values.

func StringRoute

func StringRoute(value string) Route

StringRoute matches an emitted string route value.

type RunState added in v1.13.5

type RunState struct {
	ID           string               `json:"id"`
	SessionID    string               `json:"session_id"`
	InvocationID string               `json:"invocation_id"`
	Status       RunStatus            `json:"status"`
	Queue        []QueuedNode         `json:"queue"`
	Finals       []any                `json:"finals,omitempty"`
	JoinStates   map[string]JoinState `json:"join_states,omitempty"`
	ContextState map[string]any       `json:"context_state,omitempty"`
	Steps        int                  `json:"steps"`
	Result       any                  `json:"result,omitempty"`
	LastError    string               `json:"last_error,omitempty"`
	CreatedAt    time.Time            `json:"created_at"`
	UpdatedAt    time.Time            `json:"updated_at"`
}

RunState is the serializable execution state for a durable workflow run.

Inputs, terminal outputs, join values, and ContextState must be JSON marshalable when using the supplied stores. Runs have at-least-once node execution semantics: if a process stops after a node runs but before its transition is saved, resuming the run invokes that node again. Side-effecting nodes should therefore be idempotent.

type RunStatus added in v1.13.5

type RunStatus string

RunStatus describes the lifecycle state of a durable workflow run.

const (
	// RunStatusRunning identifies a run with pending work. A run remains running
	// after a node error so it can be resumed.
	RunStatusRunning RunStatus = "running"
	// RunStatusCompleted identifies a run that reached a terminal graph output.
	RunStatusCompleted RunStatus = "completed"
	// RunStatusFailed identifies a run that cannot make progress, such as an
	// incomplete join or an invalid persisted node reference.
	RunStatusFailed RunStatus = "failed"
)

type RunStore added in v1.13.5

type RunStore interface {
	Create(ctx context.Context, state *RunState) error
	Load(ctx context.Context, runID string) (*RunState, error)
	Save(ctx context.Context, state *RunState) error
	Delete(ctx context.Context, runID string) error
}

RunStore persists workflow execution state. Implementations must return an isolated snapshot from Load and retain an isolated snapshot passed to Create or Save, because workflow execution mutates RunState between checkpoints.

type SessionGenerator

type SessionGenerator interface {
	Generate(ctx context.Context, sessionID, input string) (any, error)
}

SessionGenerator is the minimal surface needed to wrap an agent node.

Jump to

Keyboard shortcuts

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