Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultMergeFunc ¶
func DefaultMergeFunc(results []AgentOutput) model.Message
DefaultMergeFunc is the default result merger. Each agent's final assistant text content is formatted with an attribution header, in definition order:
[agent-name] content of the last assistant text message [agent-name-2] content ...
Agents that produce no assistant text are omitted.
Types ¶
type AgentOutput ¶
type AgentOutput struct {
// Name is the agent's Name() value.
Name string
// Messages are all messages yielded by the agent during its Run, in order.
Messages []model.Message
}
AgentOutput holds the name of an agent and the messages it produced.
type Config ¶
type Config struct {
// Name is the agent's name, returned by Name().
Name string
// Description is a human-readable description of the agent's purpose.
Description string
// Agents is the list of sub-agents to run concurrently.
// At least one agent is required.
Agents []agent.Agent
// MergeFunc combines all agent outputs into a single assistant message.
// Defaults to DefaultMergeFunc when nil.
MergeFunc MergeFunc
}
Config holds the configuration for a ParallelAgent.
type MergeFunc ¶
type MergeFunc func(results []AgentOutput) model.Message
MergeFunc combines the collected outputs of all agents into a single assistant message that is yielded by the ParallelAgent.
results contains one entry per agent in definition order. Each entry holds the agent's name and all messages it produced during its Run.
type ParallelAgent ¶
type ParallelAgent struct {
// contains filtered or unexported fields
}
ParallelAgent fans out to all sub-agents concurrently with the same input messages. Each agent is independent — they do not share state and cannot see each other's output.
Once all agents have finished, their results are passed to the configured MergeFunc (defaulting to DefaultMergeFunc), and the single merged assistant message is yielded. This ensures the output is always one message regardless of how many agents ran, keeping downstream conversation history well-formed.
If any agent returns an error, the shared context is cancelled so that sibling agents can exit promptly. The error is then yielded and iteration ends.
Typical use-cases:
- Fan-out: run multiple specialist agents on the same task, collect all answers.
- Multi-model: compare responses from different LLMs side-by-side.
- Independent tasks: run a translator and a summariser on the same text at once.
func (*ParallelAgent) Description ¶
func (p *ParallelAgent) Description() string
func (*ParallelAgent) Name ¶
func (p *ParallelAgent) Name() string
func (*ParallelAgent) Run ¶
func (p *ParallelAgent) Run(ctx context.Context, messages []model.Message) iter.Seq2[*model.Event, error]
Run fans out to all agents concurrently, waits for all to complete, merges their outputs via MergeFunc, and yields the single resulting complete event.
Execution model:
- A child context is derived from ctx; all agents share it.
- Each agent is launched in its own goroutine.
- Run blocks until every goroutine finishes (via sync.WaitGroup).
- If any agent errors, the child context is cancelled immediately so that sibling agents can detect cancellation and exit early.
- All AgentOutputs are passed to MergeFunc; the returned message is yielded.
Note: partial (streaming) events from sub-agents are consumed silently; only complete messages are collected and merged.