workflow

package
v1.5.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	DraftFormatYAML = "yaml"
	DraftFormatJSON = "json"
)

Variables

This section is empty.

Functions

func BuildNodePrompt

func BuildNodePrompt(node Node, inputs map[string]NodeResult) string

BuildNodePrompt builds the prompt of a node for its agent: the role of its kind (verifier, critic, router), the outputs of the nodes it needs, the prompt of the node and its output format.

func Validate

func Validate(def Definition) error

Validate checks a definition: it has at least one node, every node has a unique id and a prompt and a supported kind, a router declares routes, the nodes it needs and routes to exist (a router cannot route to itself), and the dependencies contain no cycle.

Types

type Definition

type Definition struct {
	Name        string `json:"name" yaml:"name"`
	Description string `json:"description,omitempty" yaml:"description,omitempty"`
	Nodes       []Node `json:"nodes" yaml:"nodes"`
}

Definition is a workflow: a named set of nodes, each one a prompt for an agent, linked by the nodes they need. It is read from YAML or JSON (see LoadFile).

func LoadFile

func LoadFile(path string) (Definition, error)

LoadFile reads a workflow definition from a file: JSON when the extension is .json, YAML otherwise.

func NormalizeDefinition

func NormalizeDefinition(def Definition) Definition

NormalizeDefinition trims the names, ids, kinds, agents, prompts and output formats of a definition, lower-cases the kinds, replaces a negative MaxTurns by 0, and cleans the lists of needs and routes.

type DraftOptions

type DraftOptions struct {
	Definition Definition
	Format     string
}

DraftOptions asks for a workflow definition to be normalised, validated and rendered. Format is "yaml" (the default, also "yml") or "json".

type DraftResult

type DraftResult struct {
	Definition  Definition `json:"definition"`
	Format      string     `json:"format"`
	Content     string     `json:"content"`
	Diagnostics []string   `json:"diagnostics,omitempty"`
}

DraftResult is a normalised definition and its rendering in Format. Diagnostics holds the reason when the definition is invalid or the format is not supported.

func Draft

func Draft(options DraftOptions) (DraftResult, error)

Draft normalises and validates a definition, then renders it as YAML or JSON. When it fails, it returns the error together with a result whose Diagnostics holds its message.

type Executor

type Executor interface {
	ExecuteNode(ctx context.Context, node Node, inputs map[string]NodeResult) (string, error)
}

Executor runs one node of a workflow. inputs holds the results of the nodes it needs; it returns the output of the node, or an error that makes the node fail.

type ExecutorFunc

type ExecutorFunc func(ctx context.Context, node Node, inputs map[string]NodeResult) (string, error)

ExecutorFunc is a function used as an Executor.

func (ExecutorFunc) ExecuteNode

func (f ExecutorFunc) ExecuteNode(ctx context.Context, node Node, inputs map[string]NodeResult) (string, error)

ExecuteNode calls f.

type Node

type Node struct {
	ID           string   `json:"id" yaml:"id"`
	Kind         string   `json:"kind,omitempty" yaml:"kind,omitempty"`
	Agent        string   `json:"agent,omitempty" yaml:"agent,omitempty"`
	Prompt       string   `json:"prompt" yaml:"prompt"`
	Needs        []string `json:"needs,omitempty" yaml:"needs,omitempty"`
	Routes       []string `json:"routes,omitempty" yaml:"routes,omitempty"`
	MaxTurns     int      `json:"max_turns,omitempty" yaml:"max_turns,omitempty"`
	OutputFormat string   `json:"output_format,omitempty" yaml:"output_format,omitempty"`
}

Node is one step of a workflow: a prompt for an agent, run once the nodes listed in Needs have succeeded, and whose output is given to the nodes that need it.

Kind labels the node's role in its prompt only - "agent" (or empty) for a plain step, "verifier", "critic", or "router" with Routes (at least one is required) describing the choices offered to it. Run does not act on Kind or on a router's chosen route or a verifier's verdict: every node whose Needs succeeded (no error from the executor) runs, in topological-level order, regardless of what a verifier concluded in its own output or which route a router named. A custom Executor can parse a node's own output and decide differently for the nodes it controls, but the engine's own flow is kind-agnostic.

Agent, when the default executor is used (see pkg/sdk.Client.RunWorkflow), is the built-in agent type that runs this node instead of a plain model call - empty means a plain call with no particular persona. MaxTurns is the number of autonomous turns that agent gets; 0 means a single turn, same as a plain call. OutputFormat describes the form the node's output should take, for the prompt only.

type NodeResult

type NodeResult struct {
	ID        string        `json:"id"`
	Agent     string        `json:"agent,omitempty"`
	Success   bool          `json:"success"`
	Output    string        `json:"output,omitempty"`
	Error     string        `json:"error,omitempty"`
	StartedAt time.Time     `json:"started_at"`
	EndedAt   time.Time     `json:"ended_at"`
	Duration  time.Duration `json:"duration"`
}

NodeResult is the outcome of one node: whether it succeeded, its output or its error, and when it ran.

type Options

type Options struct {
	MaxParallel int
}

Options tunes a workflow run. MaxParallel bounds how many nodes of the same level run at once; a value of 0 or less means 4.

type Result

type Result struct {
	Name      string                `json:"name"`
	Success   bool                  `json:"success"`
	StartedAt time.Time             `json:"started_at"`
	EndedAt   time.Time             `json:"ended_at"`
	Duration  time.Duration         `json:"duration"`
	Results   map[string]NodeResult `json:"results"`
	Order     []string              `json:"order"`
}

Result is the outcome of a workflow run: whether every node succeeded, the result of each node by id, and the order in which the nodes that ran finished. A node that did not run because a dependency failed has a result with Success false and no entry in Order.

func Run

func Run(ctx context.Context, def Definition, executor Executor, options Options) (Result, error)

Run executes the workflow with executor: the nodes are grouped in levels by their dependencies, the levels run one after the other and the nodes of a level in parallel, at most Options.MaxParallel at a time. A node whose dependency failed is not run. The returned error reports an invalid definition or a missing executor; a failing node shows in the Result, not in the error.

Jump to

Keyboard shortcuts

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