Documentation
¶
Overview ¶
Package recipe provides a declarative YAML-based system for defining and building blades.Agent workflows. An agent spec is a YAML specification that describes an agent (or a pipeline of agents) including model selection, instructions, parameters, context management, middlewares, and sub-agents for multi-step workflows.
Usage:
// Register models
registry := recipe.NewModelRegistry()
registry.Register("gpt-4o", myModelProvider)
// Load and build
spec, err := recipe.LoadFromFile("agent.yaml")
agent, err := recipe.Build(spec,
recipe.WithModelRegistry(registry),
recipe.WithParams(map[string]any{"language": "go"}),
)
// Run normally
runner := blades.NewRunner(agent)
output, err := runner.Run(ctx, blades.UserMessage("Review this code"))
Index ¶
- func Build(spec *AgentSpec, opts ...BuildOption) (blades.Agent, error)
- func BuildSessionOption(spec *AgentSpec, opts ...BuildOption) (blades.SessionOption, error)
- func Validate(spec *AgentSpec) error
- func ValidateParams(spec *AgentSpec, params map[string]any) error
- type AgentSpec
- type BuildOption
- type ContextSpec
- type ContextStrategy
- type ExecutionMode
- type MiddlewareFactory
- type MiddlewareRegistry
- type MiddlewareResolver
- type MiddlewareSpec
- type ModelRegistry
- type ModelResolver
- type ParameterRequirement
- type ParameterSpec
- type ParameterType
- type SubAgentSpec
- type ToolRegistry
- type ToolResolver
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Build ¶
func Build(spec *AgentSpec, opts ...BuildOption) (blades.Agent, error)
Build constructs a blades.Agent from a AgentSpec.
func BuildSessionOption ¶
func BuildSessionOption(spec *AgentSpec, opts ...BuildOption) (blades.SessionOption, error)
BuildSessionOption returns a blades.SessionOption that installs the ContextCompressor described by spec.Context onto a Session at creation time. Callers should use this to create their session before running the agent:
sessOpt, err := recipe.BuildSessionOption(spec, opts...) session := blades.NewSession(sessOpt) runner.Run(ctx, msg, blades.WithSession(session))
Returns nil when spec has no Context field; nil options are safe to pass to blades.NewSession.
Types ¶
type AgentSpec ¶
type AgentSpec struct {
Version string `yaml:"version"`
Name string `yaml:"name"`
Description string `yaml:"description"`
Model string `yaml:"model,omitempty"`
Instruction string `yaml:"instruction"`
Prompt string `yaml:"prompt,omitempty"`
Parameters []ParameterSpec `yaml:"parameters,omitempty"`
SubAgents []SubAgentSpec `yaml:"sub_agents,omitempty"`
Execution ExecutionMode `yaml:"execution,omitempty"`
Tools []string `yaml:"tools,omitempty"`
OutputKey string `yaml:"output_key,omitempty"`
MaxIterations int `yaml:"max_iterations,omitempty"`
Context *ContextSpec `yaml:"context,omitempty"`
Middlewares []MiddlewareSpec `yaml:"middlewares,omitempty"`
}
AgentSpec is the top-level declarative specification for a recipe. A recipe YAML file is parsed into this structure and then built into a blades.Agent.
func LoadFromFS ¶
LoadFromFS loads and parses a AgentSpec from an fs.FS (e.g., embed.FS).
func LoadFromFile ¶
LoadFromFile loads and parses a AgentSpec from a YAML file path.
type BuildOption ¶
type BuildOption func(*buildOptions)
BuildOption configures the Build process.
func WithContext ¶
func WithContext(enabled bool) BuildOption
WithContext explicitly controls whether built agents load session history. When omitted, the underlying agent default behavior is preserved.
func WithMiddlewareRegistry ¶
func WithMiddlewareRegistry(r MiddlewareResolver) BuildOption
WithMiddlewareRegistry sets the middleware resolver for resolving middleware names.
func WithModelRegistry ¶
func WithModelRegistry(r ModelResolver) BuildOption
WithModelRegistry sets the model resolver for resolving model names.
func WithParams ¶
func WithParams(params map[string]any) BuildOption
WithParams sets parameter values for template rendering.
func WithToolRegistry ¶
func WithToolRegistry(r ToolResolver) BuildOption
WithToolRegistry sets the tool resolver for resolving tool names.
type ContextSpec ¶
type ContextSpec struct {
// Strategy selects the implementation: "summarize" or "window".
Strategy ContextStrategy `yaml:"strategy"`
// MaxTokens is the token budget. When exceeded, old messages are compressed or dropped.
MaxTokens int64 `yaml:"max_tokens,omitempty"`
// MaxMessages is the maximum number of messages to retain (window only).
MaxMessages int `yaml:"max_messages,omitempty"`
// KeepRecent is the number of recent messages always kept verbatim (summarize only, default 10).
KeepRecent int `yaml:"keep_recent,omitempty"`
// BatchSize is the number of messages summarized per compression pass (summarize only, default 20).
BatchSize int `yaml:"batch_size,omitempty"`
// Model is the model name used for summarization (summarize strategy only).
// If omitted, falls back to the agent's own model.
Model string `yaml:"model,omitempty"`
}
ContextSpec configures the context window compression for an agent. It maps to either a summarizing or a sliding-window ContextCompressor.
Example (summarize):
context: strategy: summarize max_tokens: 80000 keep_recent: 10 batch_size: 20 model: gpt-4o-mini
Example (window):
context: strategy: window max_tokens: 80000 max_messages: 100
type ContextStrategy ¶
type ContextStrategy string
ContextStrategy defines the context management strategy for an agent.
const ( // ContextStrategySummarize compresses old messages using an LLM-based rolling summary. ContextStrategySummarize ContextStrategy = "summarize" // ContextStrategyWindow truncates oldest messages to stay within a token or message budget. ContextStrategyWindow ContextStrategy = "window" )
type ExecutionMode ¶
type ExecutionMode string
ExecutionMode defines how sub-agents are executed.
const ( // ExecutionSequential runs sub-agents one after another. ExecutionSequential ExecutionMode = "sequential" // ExecutionParallel runs sub-agents concurrently. ExecutionParallel ExecutionMode = "parallel" // ExecutionLoop runs sub-agents in a repeated loop until max_iterations is // reached or a sub-agent signals exit via the loop_exit tool. ExecutionLoop ExecutionMode = "loop" // ExecutionTool wraps each sub-agent as a tool for the parent agent. ExecutionTool ExecutionMode = "tool" )
type MiddlewareFactory ¶
type MiddlewareFactory func(options map[string]any) (blades.Middleware, error)
MiddlewareFactory constructs a blades.Middleware from YAML options. The options map contains the key-value pairs from the middleware's `options:` block. A nil or empty map is passed when no options are declared.
type MiddlewareRegistry ¶
type MiddlewareRegistry struct {
// contains filtered or unexported fields
}
MiddlewareRegistry is a simple in-memory MiddlewareResolver backed by factories.
func NewMiddlewareRegistry ¶
func NewMiddlewareRegistry() *MiddlewareRegistry
NewMiddlewareRegistry creates a new empty MiddlewareRegistry.
func (*MiddlewareRegistry) Register ¶
func (r *MiddlewareRegistry) Register(name string, factory MiddlewareFactory)
Register adds a middleware factory under the given name.
func (*MiddlewareRegistry) Resolve ¶
func (r *MiddlewareRegistry) Resolve(name string, options map[string]any) (blades.Middleware, error)
Resolve calls the registered factory for name, passing options, and returns the Middleware.
type MiddlewareResolver ¶
type MiddlewareResolver interface {
Resolve(name string, options map[string]any) (blades.Middleware, error)
}
MiddlewareResolver resolves middleware names from YAML to Middleware instances, passing the per-declaration options to the registered factory.
type MiddlewareSpec ¶
type MiddlewareSpec struct {
Name string `yaml:"name"`
Options map[string]any `yaml:"options,omitempty"`
}
MiddlewareSpec declares a single middleware to apply to an agent. The middleware is resolved by name from the MiddlewareRegistry at build time, with Options passed as-is to the factory function.
Example:
middlewares:
- name: tracing
- name: logging
options:
level: info
type ModelRegistry ¶
type ModelRegistry struct {
// contains filtered or unexported fields
}
ModelRegistry is a simple in-memory ModelResolver.
func NewModelRegistry ¶
func NewModelRegistry() *ModelRegistry
NewModelRegistry creates a new empty ModelRegistry.
func (*ModelRegistry) Register ¶
func (r *ModelRegistry) Register(name string, provider blades.ModelProvider)
Register adds a model provider under the given name.
func (*ModelRegistry) Resolve ¶
func (r *ModelRegistry) Resolve(name string) (blades.ModelProvider, error)
Resolve returns the ModelProvider registered under the given name.
type ModelResolver ¶
type ModelResolver interface {
Resolve(name string) (blades.ModelProvider, error)
}
ModelResolver resolves model names from YAML to actual ModelProvider instances.
type ParameterRequirement ¶
type ParameterRequirement string
ParameterRequirement defines whether a parameter is required or optional.
const ( ParameterRequired ParameterRequirement = "required" ParameterOptional ParameterRequirement = "optional" )
type ParameterSpec ¶
type ParameterSpec struct {
Name string `yaml:"name"`
Type ParameterType `yaml:"type"`
Description string `yaml:"description"`
Default any `yaml:"default,omitempty"`
Required ParameterRequirement `yaml:"required,omitempty"`
Options []string `yaml:"options,omitempty"`
}
ParameterSpec defines a configurable parameter for a recipe.
type ParameterType ¶
type ParameterType string
ParameterType defines the type of a recipe parameter.
const ( ParameterString ParameterType = "string" ParameterNumber ParameterType = "number" ParameterBoolean ParameterType = "boolean" ParameterSelect ParameterType = "select" )
type SubAgentSpec ¶
type SubAgentSpec struct {
Name string `yaml:"name"`
Description string `yaml:"description,omitempty"`
Model string `yaml:"model,omitempty"`
Instruction string `yaml:"instruction"`
Prompt string `yaml:"prompt,omitempty"`
Parameters []ParameterSpec `yaml:"parameters,omitempty"`
Tools []string `yaml:"tools,omitempty"`
OutputKey string `yaml:"output_key,omitempty"`
MaxIterations int `yaml:"max_iterations,omitempty"`
Context *ContextSpec `yaml:"context,omitempty"`
Middlewares []MiddlewareSpec `yaml:"middlewares,omitempty"`
}
SubAgentSpec defines a child agent within a recipe.
type ToolRegistry ¶
type ToolRegistry struct {
// contains filtered or unexported fields
}
ToolRegistry is a simple in-memory ToolResolver.
func NewToolRegistry ¶
func NewToolRegistry() *ToolRegistry
NewToolRegistry creates a new empty ToolRegistry.