项目文件夹

文件
T
Asim Aslam e416ea4a75 Enhance agent workflows with guardrails and documentation updates (#2952)
* docs: map go-micro onto Anthropic's workflows-vs-agents taxonomy

- new guide 'Agents and Workflows': adopts Anthropic's Building Effective
  Agents vocabulary — workflow (predefined path) = flow, agent (dynamic
  self-direction) = agent — maps the augmented-LLM building block and the
  five workflow patterns onto go-micro, and shows routing (chat router)
  and orchestrator-workers (conductor + plan/delegate) are already native.
- flow package doc reframed as a workflow (predefined path) per the same
  taxonomy, with guidance on flow vs agent.
- nav + README link the new guide.

* feat: agent guardrails — step limit and tool approval hook

Anthropic's Building Effective Agents stresses stopping conditions and
human-in-the-loop checkpoints for autonomous agents. Add both as plain
options enforced at the tool-handler choke point — no provider changes,
no new abstraction:

- MaxSteps(n): bound tool executions per Ask; beyond the limit, actions
  are refused and the model is told to stop and summarize.
- ApproveTool(fn): gate each action before it runs; returning false
  blocks it and surfaces the reason to the model. The internal plan tool
  is never gated.

Exposed at the micro package (AgentMaxSteps, AgentApproveTool, ApproveFunc).
Tests cover the limit, blocking, and that plan is not gated. Guardrails
section of the agents-and-workflows guide updated from 'active work' to
documented options.

* feat: flow can dispatch to an agent (flow triggers, agent reasons)

Unify the engine without collapsing the workflow/agent distinction. A
Flow with Agent set hands each event's rendered prompt to a named
registered agent over RPC (Agent.Chat) instead of running its own LLM
step — so the workflow stays the deterministic trigger and the agent is
the reasoning engine, with its plan, delegate, memory, and guardrails.
A plain flow is unchanged (single augmented-LLM step).

- flow.Agent(name) / micro.FlowAgent(name); flow stores the client and
  skips model setup when dispatching.
- test: dispatch routes to comms.Agent.Chat with the rendered prompt and
  records the reply.
- guide: 'Flow triggers, Agent reasons' section.

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-06-08 08:32:32 +01:00

83 行
2.5 KiB
Go

package flow
// Options configures a Flow.
type Options struct {
// TriggerTopic is the broker topic that triggers this flow.
TriggerTopic string
// Prompt is a Go template string. {{.Data}} is the event payload.
Prompt string
// SystemPrompt is the system instruction for the LLM.
SystemPrompt string
// Provider is the AI provider name (e.g. "anthropic", "openai").
Provider string
// APIKey for the AI provider.
APIKey string
// Model overrides the provider's default model.
Model string
// BaseURL overrides the provider's default base URL.
BaseURL string
// HistoryLimit is the max messages per flow execution.
HistoryLimit int
// OnResult is called after each execution with the result.
OnResult func(Result)
// Agent, if set, names a registered agent the flow hands each event
// to (over RPC). The flow triggers; the agent reasons. When empty,
// the flow runs a single augmented-LLM step itself.
Agent string
}
// Option applies a configuration to Options.
type Option func(*Options)
// Trigger sets the broker topic that triggers this flow.
func Trigger(topic string) Option {
return func(o *Options) { o.TriggerTopic = topic }
}
// Prompt sets the prompt template. Use {{.Data}} for the event payload.
func Prompt(p string) Option {
return func(o *Options) { o.Prompt = p }
}
// SystemPrompt sets the system instruction for the LLM.
func SystemPrompt(p string) Option {
return func(o *Options) { o.SystemPrompt = p }
}
// Provider sets the AI provider name.
func Provider(name string) Option {
return func(o *Options) { o.Provider = name }
}
// APIKey sets the API key for the AI provider.
func APIKey(key string) Option {
return func(o *Options) { o.APIKey = key }
}
// Model sets the model name.
func Model(name string) Option {
return func(o *Options) { o.Model = name }
}
// BaseURL sets the provider base URL.
func BaseURL(url string) Option {
return func(o *Options) { o.BaseURL = url }
}
// HistoryLimit sets the max messages per execution.
func HistoryLimit(n int) Option {
return func(o *Options) { o.HistoryLimit = n }
}
// OnResult sets a callback for each execution result.
func OnResult(fn func(Result)) Option {
return func(o *Options) { o.OnResult = fn }
}
// Agent makes the flow hand each event to a named registered agent over
// RPC instead of running its own LLM step. The flow triggers; the agent
// reasons (with its plan, delegate, memory, and guardrails).
func Agent(name string) Option {
return func(o *Options) { o.Agent = name }
}