Skip to main content

Overview

The Event class represents an event in a conversation between agents and users. It extends LlmResponse and stores conversation content, function calls, and actions taken by agents during interactions. Events are the fundamental building blocks of agent conversations in ADK-TS, capturing both user inputs and agent responses with associated metadata and actions.

Class Definition

import { Event } from '@iqai/adk';

Constructor

opts
EventOpts
required
Configuration options for creating an Event

Properties

invocationId
string
The invocation ID of the event
author
string
The author who created this event - 'user' or agent name
actions
EventActions
default:"new EventActions()"
Actions associated with this event. See EventActions for details.
longRunningToolIds
Set<string>
IDs of long-running function calls. Only valid for function call events.
branch
string
Hierarchical branch path for multi-agent scenarios (e.g., "agent_1.agent_2")
id
string
Unique 8-character identifier for the event
timestamp
number
Unix timestamp in seconds when the event was created

Methods

isFinalResponse()

Determines if this event represents the final response from an agent.
isFinalResponse(): boolean
Returns: true if the event is a final response (no function calls, not partial, no pending code execution) Example:
const event = new Event({ author: 'assistant' });
if (event.isFinalResponse()) {
  console.log('Agent has completed its response');
}

getFunctionCalls()

Extracts all function calls from the event content.
getFunctionCalls(): any[]
Returns: Array of function call objects from the event’s content parts Example:
const functionCalls = event.getFunctionCalls();
for (const call of functionCalls) {
  console.log(`Function: ${call.name}`, call.args);
}

getFunctionResponses()

Extracts all function responses from the event content.
getFunctionResponses(): any[]
Returns: Array of function response objects from the event’s content parts

hasTrailingCodeExecutionResult()

Checks if the event ends with a code execution result.
hasTrailingCodeExecutionResult(): boolean
Returns: true if the last content part is a code execution result

Event.newId() (static)

Generates a new random 8-character ID for an event.
static newId(): string
Returns: A unique 8-character identifier Example:
const eventId = Event.newId(); // e.g., "a3f5c89d"

Usage Examples

Creating a User Event

import { Event } from '@iqai/adk';

const userEvent = new Event({
  author: 'user',
  content: {
    role: 'user',
    parts: [{ text: 'What is the weather today?' }]
  }
});

Creating an Agent Event with Actions

import { Event, EventActions } from '@iqai/adk';

const agentEvent = new Event({
  author: 'WeatherAgent',
  invocationId: 'inv-123',
  actions: new EventActions({
    stateDelta: { location: 'San Francisco' },
    transferToAgent: 'WeatherDetailAgent'
  }),
  content: {
    role: 'model',
    parts: [{ text: 'Checking weather for San Francisco...' }]
  }
});

Processing Events in Agent Flow

for await (const event of agent.runAsync(invocationContext)) {
  console.log(`Event from: ${event.author}`);
  console.log(`Event ID: ${event.id}`);
  
  // Check for function calls
  const calls = event.getFunctionCalls();
  if (calls.length > 0) {
    console.log('Function calls:', calls.map(c => c.name));
  }
  
  // Check if this is the final response
  if (event.isFinalResponse()) {
    console.log('Agent completed');
    break;
  }
}

Branching in Multi-Agent Systems

const subAgentEvent = new Event({
  author: 'SubAgent',
  branch: 'MainAgent.SubAgent',
  content: {
    role: 'model',
    parts: [{ text: 'Processing sub-task...' }]
  }
});

Type Reference

EventOpts Interface

interface EventOpts {
  invocationId?: string;
  author: string;
  actions?: EventActions;
  longRunningToolIds?: Set<string>;
  branch?: string;
  id?: string;
  timestamp?: number;
  content?: any;
  partial?: boolean;
}

EventActions

Learn about event actions and state management

BaseLlmFlow

Understand how events flow through agent execution

Source Code

View the source: event.ts

Build docs developers (and LLMs) love