event.go

Overview

The event.go file defines the core data models and conversion functions related to events within a session. An event represents a discrete interaction or state change in a session, encapsulating metadata, content generated by large language models (LLMs), and associated actions affecting session state or artifacts. This file primarily facilitates mapping between the internal Event representation and the external session.Event type used throughout the system, enabling seamless integration between session management and LLM response handling.

Data Structures

EventActions

EventActions models the delta changes occurring in a session as a result of an event. It captures:

This struct corresponds directly to session.EventActions in the session package and is embedded inside the main Event struct.

Event

Event represents a single event occurrence within a session, aggregating metadata, LLM-generated content, and action deltas. It contains the following fields:

Field Name

Type

Description

ID

string

Unique identifier of the event.

Time

int64

Event timestamp represented as Unix epoch seconds.

InvocationID

string

Identifier of the invocation that generated this event.

Branch

string

Branch name or label associated with the event.

Author

string

Author or originator of the event.

Partial

bool

Indicates if the event content is partial or incomplete (streaming or incremental results).

LongRunningToolIDs

[]string

List of tool identifiers that are considered long-running within this event.

Content

*genai.Content

Pointer to LLM-generated content for this event (refer to [LLM Integration and Agents](80562)).

GroundingMetadata

*genai.GroundingMetadata

Metadata that grounds or contextualizes the LLM content.

TurnComplete

bool

Flag indicating whether this event marks completion of a conversational turn.

Interrupted

bool

Indicates if the event was interrupted prematurely.

ErrorCode

string

Error code string if an error occurred during event generation.

ErrorMessage

string

Human-readable error message associated with the event.

Actions

EventActions

The state and artifact changes caused by this event.

Functions

ToSessionEvent

func ToSessionEvent(event Event) *session.Event

Purpose: Converts an Event instance to a pointer to a session.Event instance.

Parameters:

Returns:

Description:

This function maps all fields from the local Event struct to the session.Event struct, transforming the Unix timestamp integer into a time.Time instance. It also converts embedded content and grounding metadata, and maps the internal EventActions to session.EventActions. This enables interoperability between the data model used internally and the session handling subsystem described in [Session Management](80559).

Usage Example:

e := Event{/* initialized fields */}
sessionEvent := ToSessionEvent(e)
// sessionEvent can now be used where session.Event is required

FromSessionEvent

func FromSessionEvent(event session.Event) Event

Purpose: Converts a session.Event instance back into an Event struct.

Parameters:

Returns:

Description:

This function performs the inverse of ToSessionEvent, mapping from the session package's event type back to the local Event representation. It converts the time.Time timestamp to Unix seconds, and extracts embedded LLM response data and action deltas. This supports scenarios where events are received or stored as session.Event and require conversion to the local data model for further processing or persistence.

Usage Example:

sessionEvent := session.Event{/* initialized fields */}
e := FromSessionEvent(sessionEvent)
// e can now be manipulated using Event methods and fields

Important Implementation Details

Interaction with Other System Components

Diagram: Class Structure and Conversion Functions

classDiagram
class Event {
+string ID
+int64 Time
+string InvocationID
+string Branch
+string Author
+bool Partial
+[]string LongRunningToolIDs
+*Content Content
+*GroundingMetadata GroundingMetadata
+bool TurnComplete
+bool Interrupted
+string ErrorCode
+string ErrorMessage
+EventActions Actions
}
class EventActions {
+map[string]any StateDelta
+map[string]int64 ArtifactDelta
}
class session.Event {
+string ID
+time.Time Timestamp
+string InvocationID
+string Branch
+string Author
+[]string LongRunningToolIDs
+LLMResponse LLMResponse
+EventActions Actions
}
class LLMResponse {
+*Content Content
+*GroundingMetadata GroundingMetadata
+bool Partial
+bool TurnComplete
+bool Interrupted
+string ErrorCode
+string ErrorMessage
}
Event --> EventActions : contains
session.Event --> LLMResponse : contains
session.Event --> EventActions : contains
Event ..> session.Event : ToSessionEvent()
session.Event ..> Event : FromSessionEvent()

This diagram illustrates the primary data models defined in this file and their relationships, including the conversion pathways between the local Event struct and the external session.Event type used elsewhere in the system. The LLMResponse struct represents the embedded LLM content and metadata within a session.Event.


This file is a foundational component for event representation and conversion within the session lifecycle, enabling integration with LLM content and managing state and artifact changes central to the system's operation.