graph

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package graph provides the core domain types for representing execution graphs.

A graph consists of nodes (execution units) connected by edges (transitions). The graph structure defines the flow of execution through the system.

Node Types:

  • ExecutorNode: Executes tasks like LLM calls, tool invocations, or code execution
  • RouterNode: Makes routing decisions based on state conditions
  • Start/End: Special nodes for graph entry and exit points

This package defines only the domain models and interfaces. Actual implementations of node execution logic should be in the main dago repository.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseNode

type BaseNode struct {
	ID          string                 `json:"id"`
	Type        NodeType               `json:"type"`
	Name        string                 `json:"name,omitempty"`
	Description string                 `json:"description,omitempty"`
	Metadata    map[string]interface{} `json:"metadata,omitempty"`
}

BaseNode provides common fields for all node types. This is a helper struct that can be embedded in concrete node implementations.

func (*BaseNode) GetID

func (n *BaseNode) GetID() string

GetID returns the node's ID.

func (*BaseNode) GetType

func (n *BaseNode) GetType() NodeType

GetType returns the node's type.

type Edge

type Edge struct {
	// ID is a unique identifier for this edge.
	ID string `json:"id,omitempty"`

	// From is the ID of the source node.
	From string `json:"from"`

	// To is the ID of the target node.
	To string `json:"to"`

	// Condition is an optional expression that must evaluate to true for this edge to be traversed.
	// If empty, the edge is always traversable.
	// The expression syntax is implementation-specific.
	Condition string `json:"condition,omitempty"`

	// Label provides a human-readable description of this edge.
	Label string `json:"label,omitempty"`

	// Metadata stores additional edge-specific data.
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

Edge represents a directed connection between two nodes in the graph.

func NewEdge

func NewEdge(from, to string) *Edge

NewEdge creates a new edge with the given source and target.

func (*Edge) Validate

func (e *Edge) Validate() error

Validate checks if the edge configuration is valid.

func (*Edge) WithCondition

func (e *Edge) WithCondition(condition string) *Edge

WithCondition sets a condition on the edge.

func (*Edge) WithLabel

func (e *Edge) WithLabel(label string) *Edge

WithLabel sets a label on the edge.

type ExecutorNode

type ExecutorNode struct {
	BaseNode
	// ExecutorType specifies what kind of executor this is (e.g., "llm", "tool", "python").
	ExecutorType string `json:"executor_type"`

	// Config contains executor-specific configuration.
	// The structure depends on the ExecutorType.
	Config map[string]interface{} `json:"config"`

	// InputMapping defines how to map state values to executor inputs.
	InputMapping map[string]string `json:"input_mapping,omitempty"`

	// OutputMapping defines how to map executor outputs back to state.
	OutputMapping map[string]string `json:"output_mapping,omitempty"`
}

ExecutorNode represents a node that executes tasks like LLM calls or tool invocations.

func (*ExecutorNode) Execute

func (n *ExecutorNode) Execute(ctx context.Context, s state.State) (state.State, error)

Execute is a placeholder that should be implemented in the main repository.

func (*ExecutorNode) Validate

func (n *ExecutorNode) Validate() error

Validate checks if the executor node configuration is valid.

type Graph

type Graph struct {
	// ID is a unique identifier for this graph.
	ID string `json:"id"`

	// Name is a human-readable name for the graph.
	Name string `json:"name,omitempty"`

	// Description provides context about what this graph does.
	Description string `json:"description,omitempty"`

	// Nodes is a map of node ID to node instance.
	// Using a map allows O(1) lookups by ID.
	Nodes map[string]Node `json:"nodes"`

	// Edges defines the connections between nodes.
	Edges []*Edge `json:"edges"`

	// EntryNode is the ID of the node where execution begins.
	EntryNode string `json:"entry_node"`

	// Metadata stores additional graph-level data.
	Metadata map[string]interface{} `json:"metadata,omitempty"`

	// Version is the schema version of this graph definition.
	Version string `json:"version,omitempty"`
}

Graph represents a directed graph of nodes and edges that defines the execution flow.

func FromJSON

func FromJSON(jsonStr string) (*Graph, error)

FromJSON deserializes a graph from JSON. Note: This is a basic implementation. Full deserialization with proper node type handling should be implemented in the main dago repository.

func NewGraph

func NewGraph(name string) *Graph

NewGraph creates a new graph with a generated UUID.

func (*Graph) AddEdge

func (g *Graph) AddEdge(edge *Edge) error

AddEdge adds an edge to the graph. Validates that both source and target nodes exist.

func (*Graph) AddNode

func (g *Graph) AddNode(node Node) error

AddNode adds a node to the graph. Returns an error if a node with the same ID already exists.

func (*Graph) Clone

func (g *Graph) Clone() (*Graph, error)

Clone creates a deep copy of the graph. Note: This uses JSON serialization for simplicity. TODO: Consider more efficient cloning for performance-critical paths.

func (*Graph) EdgeCount

func (g *Graph) EdgeCount() int

EdgeCount returns the number of edges in the graph.

func (*Graph) GetIncomingEdges

func (g *Graph) GetIncomingEdges(nodeID string) []*Edge

GetIncomingEdges returns all edges targeting the given node.

func (*Graph) GetNode

func (g *Graph) GetNode(nodeID string) Node

GetNode retrieves a node by its ID. Returns nil if the node doesn't exist.

func (*Graph) GetOutgoingEdges

func (g *Graph) GetOutgoingEdges(nodeID string) []*Edge

GetOutgoingEdges returns all edges originating from the given node.

func (*Graph) NodeCount

func (g *Graph) NodeCount() int

NodeCount returns the number of nodes in the graph.

func (*Graph) RemoveNode

func (g *Graph) RemoveNode(nodeID string)

RemoveNode removes a node from the graph. Also removes any edges connected to this node.

func (*Graph) ToJSON

func (g *Graph) ToJSON() (string, error)

ToJSON serializes the graph to JSON.

func (*Graph) Validate

func (g *Graph) Validate() error

Validate performs comprehensive validation of the graph structure.

type Node

type Node interface {
	// GetID returns the unique identifier for this node.
	GetID() string

	// GetType returns the type of this node.
	GetType() NodeType

	// Execute runs the node's logic with the given context and state.
	// It returns the updated state and any error that occurred.
	// Implementations should be provided in the main dago repository, not here.
	Execute(ctx context.Context, state state.State) (state.State, error)

	// Validate checks if the node configuration is valid.
	Validate() error
}

Node defines the interface that all graph nodes must implement.

type NodeType

type NodeType string

NodeType represents the type of a node in the graph.

const (
	// NodeTypeExecutor represents a node that executes a task (calls LLM, runs tools, etc).
	NodeTypeExecutor NodeType = "executor"

	// NodeTypeRouter represents a node that makes routing decisions based on state.
	NodeTypeRouter NodeType = "router"

	// NodeTypeStart represents the entry point of the graph.
	NodeTypeStart NodeType = "start"

	// NodeTypeEnd represents an exit point of the graph.
	NodeTypeEnd NodeType = "end"
)

type Route

type Route struct {
	// Condition is an expression evaluated against the state.
	// The expression syntax is implementation-specific (e.g., JSONPath, simple comparisons).
	Condition string `json:"condition"`

	// Target is the ID of the node to route to if the condition is true.
	Target string `json:"target"`

	// Description provides human-readable context for this route.
	Description string `json:"description,omitempty"`
}

Route represents a conditional routing rule.

type RouterNode

type RouterNode struct {
	BaseNode
	// Routes defines the routing logic.
	// Each route has a condition and target node ID.
	Routes []Route `json:"routes"`

	// DefaultRoute is the fallback route if no conditions match.
	DefaultRoute string `json:"default_route,omitempty"`
}

RouterNode represents a node that makes routing decisions based on state.

func (*RouterNode) Execute

func (n *RouterNode) Execute(ctx context.Context, s state.State) (state.State, error)

Execute is a placeholder that should be implemented in the main repository.

func (*RouterNode) Validate

func (n *RouterNode) Validate() error

Validate checks if the router node configuration is valid.

type ValidationError

type ValidationError struct {
	Field   string
	Message string
}

ValidationError represents a node validation error.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

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