UNPKG

@waldzellai/adk-typescript

Version:

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

154 lines (153 loc) 4.5 kB
"use strict"; // Session module for the Google Agent Development Kit (ADK) in TypeScript // Mirrors the session functionality from the Python SDK Object.defineProperty(exports, "__esModule", { value: true }); exports.Session = void 0; const event_1 = require("../events/event"); /** * Represents a series of interactions between a user and agents. */ class Session { /** * Creates a new Session. * * @param data The session data */ constructor(data) { this.id = data.id; this.appName = data.appName; this.userId = data.userId; this.state = data.state || {}; this.events = data.events || []; this.lastUpdateTime = data.lastUpdateTime || Date.now(); } /** * Adds an event to the session. * * @param event The event to add * @returns The added event */ addEvent(event) { this.events.push(event); this.lastUpdateTime = Date.now(); return event; } /** * Gets all the content from events in the session. * * @returns The content from events in the session */ getContents() { const contents = []; for (const event of this.events) { const content = event.getContent(); if (content) { contents.push(content); } } return contents; } /** * Gets the state value for the given key. * * @param key The state key * @param defaultValue The default value to return if the key is not found * @returns The state value or the default value if not found */ getStateValue(key, defaultValue = undefined) { return key in this.state ? this.state[key] : defaultValue; } /** * Sets the state value for the given key. * * @param key The state key * @param value The state value */ setStateValue(key, value) { this.state[key] = value; this.lastUpdateTime = Date.now(); } /** * Deletes the state value for the given key. * * @param key The state key * @returns True if the key was found and deleted, false otherwise */ deleteStateValue(key) { if (key in this.state) { delete this.state[key]; this.lastUpdateTime = Date.now(); return true; } return false; } /** * Checks if the state has a value for the given key. * * @param key The state key * @returns True if the key exists in the state, false otherwise */ hasStateValue(key) { return key in this.state; } /** * Updates the session state with the given values. * * @param values The values to update the state with */ updateState(values) { for (const [key, value] of Object.entries(values)) { this.state[key] = value; } this.lastUpdateTime = Date.now(); } /** * Gets the session as a JSON object. * * @returns The session as a JSON object */ toJSON() { return { id: this.id, appName: this.appName, userId: this.userId, state: this.state, events: this.events, lastUpdateTime: this.lastUpdateTime }; } /** * Creates a session from a JSON object. * * @param json The JSON object * @returns A new session instance */ static fromJSON(json) { // Convert raw event objects to Event instances const eventsArray = json.events || []; const events = eventsArray.map((eventData) => { return new event_1.Event({ id: eventData.id, invocationId: eventData.invocationId, author: eventData.author, branch: eventData.branch, content: eventData.content, actions: eventData.actions, timestamp: eventData.timestamp, partial: eventData.partial, longRunningToolIds: eventData.longRunningToolIds ? new Set(eventData.longRunningToolIds) : undefined }); }); return new Session({ id: json.id, appName: json.appName, userId: json.userId, state: json.state || {}, events: events, lastUpdateTime: json.lastUpdateTime }); } } exports.Session = Session;