UNPKG

@waldzellai/adk-typescript

Version:

TypeScript SDK for Google Agent Development Kit (ADK) - A comprehensive framework for building AI agents

204 lines (203 loc) 6.2 kB
"use strict"; // Event module for the Google Agent Development Kit (ADK) in TypeScript // Mirrors the event functionality from the Python SDK Object.defineProperty(exports, "__esModule", { value: true }); exports.Event = void 0; const event_actions_1 = require("./event_actions"); /** * Represents an event in a conversation between agents and users. * * It is used to store the content of the conversation, as well as the actions * taken by the agents like function calls, etc. */ class Event { /** * Creates a new Event instance * * @param options Event configuration options */ constructor(options) { this.id = options.id || Event.newId(); this.invocationId = options.invocationId || ''; this.author = options.author; this.branch = options.branch || null; this.timestamp = options.timestamp || Date.now(); this.partial = options.partial || false; this.content = options.content || null; this.actions = options.actions || new event_actions_1.EventActions(); this.longRunningToolIds = options.longRunningToolIds || null; } /** * Gets the event ID * * @returns The event ID */ getId() { return this.id; } /** * Gets the invocation ID * * @returns The invocation ID */ getInvocationId() { return this.invocationId; } /** * Gets the event author * * @returns The author of the event */ getAuthor() { return this.author; } /** * Gets the event branch * * @returns The branch of the event */ getBranch() { return this.branch; } /** * Gets the event timestamp * * @returns The timestamp of the event */ getTimestamp() { return this.timestamp; } /** * Checks if the event is partial * * @returns Whether the event is partial */ isPartial() { return this.partial; } /** * Gets the event content * * @returns The content of the event */ getContent() { return this.content; } /** * Gets the event actions * * @returns The actions of the event */ getActions() { return this.actions; } /** * Gets the long running tool IDs * * @returns The long running tool IDs */ getLongRunningToolIds() { return this.longRunningToolIds; } /** * Checks if the event is a final response * * @returns Whether the event is a final response */ isFinalResponse() { if (this.actions.skipSummarization || (this.longRunningToolIds && this.longRunningToolIds.size > 0)) { return true; } return (this.getFunctionCalls().length === 0 && this.getFunctionResponses().length === 0 && !this.partial && !this.hasTrailingCodeExecutionResult()); } /** * Gets function calls from the event content * * @returns The function calls in the event */ getFunctionCalls() { const funcCalls = []; if (this.content && this.content.parts) { for (const part of this.content.parts) { if (part.functionCall) { funcCalls.push({ functionCall: { name: part.functionCall.name ?? '', args: part.functionCall.args ?? {} } }); } } } return funcCalls; } /** * Gets function responses from the event content * * @returns The function responses in the event */ getFunctionResponses() { const funcResponses = []; if (this.content && this.content.parts) { for (const part of this.content.parts) { if (part.functionResponse) { funcResponses.push({ functionResponse: { name: part.functionResponse.name ?? '', response: part.functionResponse.response ?? {} } }); } } } return funcResponses; } /** * Checks if the event has a trailing code execution result * * @returns Whether the event has a trailing code execution result */ hasTrailingCodeExecutionResult() { if (this.content && this.content.parts && this.content.parts.length > 0) { return !!this.content.parts[this.content.parts.length - 1].codeExecutionResult; } return false; } /** * Creates a copy of this event with modifications * * @param modifications The modifications to apply to the event * @returns A new event with the modifications applied */ withModifications(modifications) { return new Event({ id: modifications.id || this.id, invocationId: modifications.invocationId || this.invocationId, author: modifications.author || this.author, branch: modifications.branch !== undefined ? modifications.branch : this.branch, timestamp: modifications.timestamp || this.timestamp, partial: modifications.partial !== undefined ? modifications.partial : this.partial, content: modifications.content !== undefined ? modifications.content : this.content, actions: modifications.actions || this.actions, longRunningToolIds: modifications.longRunningToolIds !== undefined ? modifications.longRunningToolIds : this.longRunningToolIds }); } /** * Generates a new random ID for an event * * @returns A new random ID */ static newId() { const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; let result = ''; for (let i = 0; i < 8; i++) { result += characters.charAt(Math.floor(Math.random() * characters.length)); } return result; } } exports.Event = Event;