agent.go

Overview

The agent.go file implements the ParallelAgent, a specialized workflow agent designed to run multiple sub-agents concurrently in isolated invocation contexts. This agent type is part of the broader Agent Workflow Management system (80558), providing an execution strategy where sub-agents operate in parallel, enabling diverse perspectives or simultaneous attempts on a given task.

The ParallelAgent is particularly useful for scenarios such as running different algorithms simultaneously or generating multiple independent responses for downstream evaluation. It manages concurrency, event aggregation, cancellation, and error propagation, exposing a unified event stream to callers.


Key Types and Functions

Config Struct

type Config struct {
	AgentConfig agent.Config
}

New(cfg Config) (agent.Agent, error)

cfg := parallelagent.Config{
	AgentConfig: agent.Config{
		Name:      "MyParallelAgent",
		SubAgents: []agent.Agent{subAgent1, subAgent2},
	},
}
parallelAgent, err := parallelagent.New(cfg)
if err != nil {
	// handle error
}

run(ctx agent.InvocationContext) iter.Seq2[*session.Event, error]


runSubAgent(ctx agent.InvocationContext, agent agent.Agent, results chan<- result, done <-chan bool) error


result Struct

type result struct {
	event *session.Event
	err   error
}

Important Implementation Details


Interactions with Other System Components


Visual Diagram: ParallelAgent Structure

classDiagram
class ParallelAgent {
+Config cfg
+New(cfg) agent.Agent
+run(ctx) iter.Seq2
-runSubAgent(ctx, agent, results, done) error
}
class Config {
+AgentConfig agent.Config
}
class result {
-event *session.Event
-err error
}
ParallelAgent --> Config : uses
ParallelAgent --> agent.Agent : implements
ParallelAgent --> result : produces

Visual Diagram: ParallelAgent Execution Workflow

flowchart TD
Start(("Start ParallelAgent Run"))
Sub1["Sub-Agent 1 Run"]
Sub2["Sub-Agent 2 Run"]
SubN["Sub-Agent N Run"]
ErrGroup["Error Group Wait"]
Results["Collect Events"]
Yield["Yield Events to Caller"]
Done["Done / Cancel Signal"]
Start -->|Create isolated contexts| Sub1
Start --> Sub2
Start --> SubN
Sub1 --> Results
Sub2 --> Results
SubN --> Results
ErrGroup -->|Wait for all sub-agents| Results
Results --> Yield
Yield --> Done

This flowchart illustrates the ParallelAgent's concurrent execution model:


References to Related Topics


This documentation provides a detailed technical overview of the agent.go file responsible for implementing the ParallelAgent, describing its design, usage, and integration within the larger agent orchestration framework.