executor.go

Overview

The executor.go file implements the Executor component responsible for executing AI agents in response to Agent-To-Agent (A2A) protocol requests. It acts as a server-side bridge that:

The Executor thus enables remote execution of AI agents, managing the entire task lifecycle and event streaming as part of the Remote Agent Communication (A2A) infrastructure.


Types and Interfaces

ExecutorConfig

Holds the mandatory dependencies needed by the Executor to run:

Executor

The main struct implementing the a2asrv.AgentExecutor interface. It orchestrates the execution of ADK agents and streaming of events according to A2A protocol semantics.


Functions and Methods

NewExecutor(config ExecutorConfig) *Executor

Creates and returns a new initialized Executor instance.

Parameters:

Returns:

Usage Example:

executor := NewExecutor(ExecutorConfig{
    RunnerConfig: runnerCfg,
    RunConfig:    runCfg,
})

(e *Executor) Execute(ctx context.Context, reqCtx *a2asrv.RequestContext, queue eventqueue.Queue) error

Primary method invoked to execute an agent task in response to an A2A request.

Parameters:

Returns:

Detailed Behavior:

  1. Validates that the incoming A2A message (reqCtx.Message) is present; otherwise, returns an error.

  2. Converts the A2A message to internal genai.Content using toGenAIContent.

  3. Creates a new agent runner.Runner instance using the configured RunnerConfig.

  4. If the request does not reference an existing stored task, writes a TaskStateSubmitted status update event to the queue.

  5. Prepares the session by ensuring it exists or creating it (prepareSession).

  6. Writes a TaskStateWorking status update event to indicate the agent execution is starting.

  7. Instantiates an eventProcessor to convert session events to A2A events.

  8. Invokes the internal process method to run the agent and stream events.


(e *Executor) Cancel(ctx context.Context, reqCtx *a2asrv.RequestContext, queue eventqueue.Queue) error

Handles cancellation requests for running tasks.


(e *Executor) process(ctx context.Context, r *runner.Runner, processor *eventProcessor, content *genai.Content, q eventqueue.Queue) error

Internal helper method that runs the agent and processes its output events.

Parameters:

Behavior:


(e *Executor) prepareSession(ctx context.Context, meta invocationMeta) error

Ensures the session exists before agent execution.

Parameters:

Behavior:


Important Implementation Details and Algorithms


Interaction with Other System Components

The Executor thus acts as the execution engine bridging incoming A2A requests to local agent execution and streaming back A2A-compatible events.


Mermaid Diagram: Executor Structure and Workflow

sequenceDiagram
participant Client as A2A Client
participant Executor as Executor
participant Runner as runner.Runner
participant Processor as eventProcessor
participant Queue as eventqueue.Queue
Client->>Executor: Execute(request)
Executor->>Executor: prepareSession()
Executor->>Queue: Write(TaskStateSubmitted) [if new task]
Executor->>Queue: Write(TaskStateWorking)
Executor->>Runner: Run(agent input)
loop For each session.Event
Runner-->>Executor: session.Event
Executor->>Processor: process(session.Event)
Processor-->>Executor: a2a.Event
Executor->>Queue: Write(a2a.Event)
end
Executor->>Queue: Write(Terminal events)
Client->>Executor: Cancel(request)
Executor->>Queue: Write(TaskStateCanceled)

Summary of Main Entities

Entity

Description

Executor

Implements A2A task execution, event streaming, session management.

ExecutorConfig

Holds configurations for runner creation and execution.

Execute method

Orchestrates agent execution lifecycle and event streaming.

Cancel method

Handles task cancellation by sending cancel status event.

process method

Runs the agent and translates session events to A2A events.

prepareSession

Ensures session exists before running the agent.

runner.Runner

Underlying component that actually runs the agent logic.

eventqueue.Queue

Interface to send A2A events asynchronously to the client.

eventProcessor

Converts internal session events to A2A events during execution.


Usage Context

The executor.go file is part of the Remote Agent Communication (A2A) system. It implements the server-side executor that runs local ADK agents in response to remote A2A requests. It is tightly coupled with the agent runner infrastructure (Agent Execution Runner) and event processing (Event Conversion and Processing) to enable streaming, stateful, and cancellable remote agent execution. This file is central to supporting distributed multi-agent workflows where agents can be invoked remotely and report progress asynchronously via the Agent-To-Agent protocol.

For detailed event translation and agent invocation context, refer to the related topics: