events.go

Overview

This file provides core utilities for converting between two event models in the system:

The primary purpose of this file is to implement bidirectional translation between these formats, enabling session events generated locally to be sent as A2A messages to remote agents, and A2A events received from remote agents to be converted back into session events for local consumption.

Additionally, this file manages custom metadata keys related to task and context IDs, propagates action flags (like escalation or transfer requests), and identifies long-running tool invocations embedded in event parts.

This functionality is essential for enabling distributed multi-agent workflows, remote agent invocation, and streaming incremental event updates.


Key Entities and Functions

Constants and Metadata Keys

These keys use helper functions ToADKMetaKey and ToA2AMetaKey to apply consistent prefixes, ensuring differentiation of A2A-related metadata within session events.


NewRemoteAgentEvent(ctx agent.InvocationContext) *session.Event

Creates a new session.Event authored by the agent running within the provided invocation context.

Usage Example:

event := NewRemoteAgentEvent(ctx)

This function is used as a factory method for creating events consistently attributed to the current agent invocation.


EventToMessage(event *session.Event) (*a2a.Message, error)

Converts an ADK session event to an A2A protocol message for sending to remote agents.

Usage:
Used when sending an ADK event over the network to a remote agent using the A2A protocol.


ToSessionEvent(ctx agent.InvocationContext, event a2a.Event) (*session.Event, error)

Converts an incoming A2A event into an ADK session event authored by the agent in the given invocation context.

This function enables the client side of A2A communication to interpret remote agent events as local session events.


ToADKMetaKey(key string) string

Adds a prefix "a2a:" to a metadata key to differentiate A2A-related keys in session event custom metadata.


ToCustomMetadata(taskID a2a.TaskID, ctxID string) map[string]any

Creates a custom metadata map for a session event including the A2A task ID and context ID.

This metadata is used to track task and context IDs throughout event processing and conversion.


GetA2ATaskInfo(event *session.Event) (a2a.TaskID, string)

Extracts the A2A task ID and context ID from a session event’s custom metadata, if present.


Conversion Helper Functions (all internal)


Important Implementation Details


Interaction with Other System Components


Diagram: Function and Conversion Flow

flowchart TD
subgraph ADK Session Events
SE[session.Event]
end
subgraph A2A Protocol Events
AM[a2a.Message]
AE[a2a.Event]
end
SE -->|EventToMessage| AM
AE -->|ToSessionEvent| SE
AM -.->|Metadata: Actions| AM
SE -.->|CustomMetadata: Task & Context IDs| SE
AM -->|Parts Conversion| AM
AE -->|Parts Conversion| SE

This diagram illustrates the bidirectional conversion between ADK session events and A2A protocol messages/events, highlighting the propagation of metadata and content parts.


Usage Patterns and Examples

event := NewRemoteAgentEvent(ctx)
msg, err := EventToMessage(event)
if err != nil {
    // handle error
}
// send msg over network
sessionEvent, err := ToSessionEvent(ctx, a2aEvent)
if err != nil {
    // handle error
}
// process sessionEvent
taskID, ctxID := GetA2ATaskInfo(sessionEvent)

References to Related Topics


This file is a critical component in the event translation layer, ensuring seamless interoperability between internal session state and the external A2A communication protocol.