utils.go
Overview
The utils.go file provides utility functions that facilitate the handling, inspection, and transformation of session events within the remote agent communication subsystem. It primarily supports the integration of local session events with the Agent-To-Agent (A2A) protocol by identifying specific event types, extracting relevant metadata, and preparing event content for remote invocation contexts.
Key responsibilities include:
Identifying user function calls within session event streams.
Detecting and parsing function call and response events.
Extracting missing content parts from sessions that have not yet been synchronized with remote agents.
Reformatting remote agent authored events as user-context messages for clarity in multi-agent conversations.
This file is an essential helper for bridging session events with the A2A protocol operations, enabling robust event handling and context propagation in distributed agent workflows.
Types and Structs
userFunctionCall
type userFunctionCall struct {
event *session.Event
taskID a2a.TaskID
contextID string
}
Purpose: Represents a user-originated function call event paired with its corresponding task ID and context ID.
Fields:
event: The session event representing the function response from the user.taskID: The A2A task identifier associated with the function call.contextID: The A2A context string linking related events.
Functions
getUserFunctionCallAt
func getUserFunctionCallAt(events session.Events, index int) *userFunctionCall
Description: Searches the session event list at the specified index for a user-authored function response event. If found, it scans backwards to locate the matching function call event by call ID. Returns a
userFunctionCallstruct containing the matched events and associated A2A task and context IDs.Parameters:
events: The collection of session events.index: The index within theeventsslice to inspect.
Returns: A pointer to
userFunctionCallif a matching function call and response pair is found; otherwisenil.Usage Example:
ufCall := getUserFunctionCallAt(sessionEvents, someIndex) if ufCall != nil { // Process the user function call event and metadata }Implementation Details:
Ensures the event at
indexis authored by "user".Extracts the function response call ID using
getFunctionResponseCallID.Iterates backwards to find the matching function call event using
isFunctionCallEvent.Retrieves A2A task and context IDs from the matching function call event metadata.
isFunctionCallEvent
func isFunctionCallEvent(event *session.Event, callID string) bool
Description: Checks whether a given session event contains a function call part with the specified call ID.
Parameters:
event: The session event to inspect.callID: The function call ID string to match.
Returns:
trueif the event contains a function call with the matching ID; otherwisefalse.Implementation Details:
Safely handles
nilevents or content.Uses a slice utility to check if any content part includes a function call with the given ID.
getFunctionResponseCallID
func getFunctionResponseCallID(event *session.Event) (string, bool)
Description: Retrieves the function call ID from the first content part of the event that contains a non-
nilfunction response.Parameters:
event: The session event to analyze.
Returns:
The function call ID string.
A boolean indicating whether a function response was found.
Implementation Details:
Uses slice indexing to find the first content part with a function response.
Returns false if no suitable part is found or content is absent.
toMissingRemoteSessionParts
func toMissingRemoteSessionParts(ctx agent.InvocationContext, events session.Events) ([]a2a.Part, string)
Description: Constructs a list of A2A content parts representing session events that are missing from the remote session. It scans backward through events until it encounters one authored by the remote agent, collecting all subsequent events as missing. Additionally, it extracts the associated A2A context ID if available.
Parameters:
ctx: The current agent invocation context.events: The session events to evaluate.
Returns:
A slice of
a2a.Partobjects representing the missing event content parts.A string representing the A2A context ID, or empty if none found.
Usage Example:
missingParts, contextID := toMissingRemoteSessionParts(ctx, sessionEvents)Implementation Details:
Iterates backward through events to find the last event authored by the remote agent.
Aggregates the content parts of all events after this index.
Converts event content parts to A2A parts, skipping events with empty or nil content.
Re-presents non-user and non-agent authored events as user messages for clarity using
presentAsUserMessage.Returns the combined parts and the remote context ID if found.
presentAsUserMessage
func presentAsUserMessage(ctx agent.InvocationContext, agentEvent *session.Event) *session.Event
Description: Converts an event authored by a remote agent into a user-authored session event that provides context about the remote agent's actions. This rephrasing helps the local session present remote agent events as contextual user messages.
Parameters:
ctx: The current agent invocation context.agentEvent: The original event authored by the remote agent.
Returns: A new session event authored by "user" that includes text parts describing the remote agent's contributions.
Implementation Details:
Creates a new event with the current invocation ID.
Prepends a "For context:" text part.
Iterates event parts, excluding thought parts.
Wraps textual content, function calls, and function responses with descriptive text indicating the original author and action.
If no meaningful parts exist beyond "For context:", returns an event without content.
Example Usage:
When an event from a remote agent needs to be incorporated into the local session as user context, this function ensures clear attribution and readability.
Important Implementation Details
The file leverages utilities from the
slicespackage for efficient event part searching and filtering.The core logic revolves around parsing events' content parts, specifically focusing on function calls and function responses, which are critical for managing long-running tools and remote task contexts.
Conversion to A2A content parts (
a2a.Part) is performed with error handling to ensure robustness despite possible event inconsistencies.The
presentAsUserMessagefunction is a notable design for improving user experience by rephrasing remote agent events as user messages, maintaining conversational clarity in multi-agent interactions.
Interaction with Other System Components
This file is part of the Remote Agent Communication (A2A) module and is tightly integrated with event conversion utilities described in
Event Conversion and Processing.It supports the remote agent proxy implementation in
A2A Agent Implementationby providing event inspection and extraction helpers needed to correctly prepare function call contexts and session synchronization data.The functions here interact with session event structures defined in the
Session Managementtopic and rely on agent invocation contexts fromAgent Invocation Context.The conversion of content parts to
a2a.Parttypes uses the utilities in theadka2apackage, connecting to the core A2A protocol implementation.The re-presentation of remote agent events facilitates the user interface and session history coherence, contributing indirectly to workflows handled by
Agent Workflow Management.
Visual Diagram
flowchart TD
A[getUserFunctionCallAt] -->|uses| B[getFunctionResponseCallID]
A -->|uses| C[isFunctionCallEvent]
D[toMissingRemoteSessionParts] -->|calls| E[presentAsUserMessage]
D -->|uses| F[adk2a.ToA2AParts]
E -->|creates new| G[session.Event as User Message]
B -->|searches| H[event.Content.Parts]
subgraph Function Call Handling
A
B
C
H
end
subgraph Missing Parts Handling
D
E
F
G
end
The diagram illustrates the main functions and their relationships:
getUserFunctionCallAtdepends ongetFunctionResponseCallIDandisFunctionCallEventto identify user function call events.toMissingRemoteSessionPartsrelies onpresentAsUserMessageto convert non-user events before transforming event parts to A2A protocol parts.The flow clarifies separation between function call detection logic and session content synchronization logic.
This documentation presents a detailed view of the utils.go file’s utilities, their roles in event processing within the remote agent communication system, and their collaboration with other modules for effective distributed agent operation. For broader context, see [Remote Agent Communication (A2A)](80565) and related files such as a2a_agent.go and events.go.