adk-typescript
Version:
TypeScript port of Google's Agent Development Kit (ADK)
105 lines (104 loc) • 3.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Event = void 0;
const LlmResponse_1 = require("../models/LlmResponse");
const EventActions_1 = require("./EventActions");
/**
* 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 extends LlmResponse_1.LlmResponse {
/**
* Constructor for Event class
*/
constructor(params) {
super();
/**
* The invocation ID of the event.
*/
this.invocationId = '';
/**
* The unique identifier of the event.
*/
this.id = '';
this.invocationId = params.invocationId || '';
this.author = params.author;
this.actions = params.actions || new EventActions_1.EventActions();
this.longRunningToolIds = params.longRunningToolIds;
this.branch = params.branch;
this.id = params.id || Event.newId();
this.timestamp = params.timestamp || Date.now() / 1000;
this.content = params.content;
this.partial = params.partial;
this.turnComplete = params.turnComplete;
this.errorCode = params.errorCode;
this.errorMessage = params.errorMessage;
this.interrupted = params.interrupted;
this.customMetadata = params.customMetadata;
}
/**
* Returns whether the event is the final response of the agent.
*/
isFinalResponse() {
if (this.actions.skipSummarization || this.longRunningToolIds) {
return true;
}
return (this.getFunctionCalls().length === 0 &&
this.getFunctionResponses().length === 0 &&
!this.partial &&
!this.hasTrailingCodeExecutionResult());
}
/**
* 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(part.functionCall);
}
}
}
return funcCalls;
}
/**
* 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(part.functionResponse);
}
}
}
return funcResponses;
}
/**
* Returns whether the event has a trailing code execution result.
*/
hasTrailingCodeExecutionResult() {
if (this.content && this.content.parts && this.content.parts.length > 0) {
const lastPart = this.content.parts[this.content.parts.length - 1];
return lastPart.codeExecutionResult !== undefined &&
lastPart.codeExecutionResult !== null;
}
return false;
}
/**
* Generate a new random ID for an event.
*/
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;