UNPKG

@waldzellai/adk-typescript

Version:

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

209 lines (208 loc) 5.32 kB
"use strict"; // State module for the Google Agent Development Kit (ADK) in TypeScript // Mirrors the state functionality from the Python SDK Object.defineProperty(exports, "__esModule", { value: true }); exports.State = void 0; /** * A state object that maintains the current value and the pending-commit delta. * * This class provides app-level, user-level, and session-level state management * with prefixes to distinguish between them. */ class State { /** * Creates a new State instance. * * @param value The current value of the state * @param delta The delta change to the current value that hasn't been committed */ constructor(value, delta) { this._value = value || {}; this._delta = delta || {}; } /** * Creates a new state from a plain object. * * @param obj The object to create the state from * @returns A new state instance */ static fromObject(obj) { return new State(obj || {}, {}); } /** * Sets a value in the state. * * @param key The key to set * @param value The value to set */ set(key, value) { this._value[key] = value; this._delta[key] = value; } /** * Gets a value from the state. * * @param key The key to get * @returns The value for the key or undefined if not found */ get(key) { if (key in this._delta) { return this._delta[key]; } if (key in this._value) { return this._value[key]; } return undefined; } /** * Checks if the state has a value for the given key. * * @param key The key to check * @returns Whether the state has a value for the key */ hasValue(key) { return key in this._delta || key in this._value; } /** * Checks if the state has any values. * * @returns Whether the state has any values */ hasAny() { return Object.keys(this._value).length > 0 || Object.keys(this._delta).length > 0; } /** * Checks if the state has any pending changes. * * @returns Whether the state has any pending changes */ hasDelta() { return Object.keys(this._delta).length > 0; } /** * Gets the state delta. * * @returns The state delta */ getDelta() { return { ...this._delta }; } /** * Commits the delta to the value and clears the delta. */ commitDelta() { for (const [key, value] of Object.entries(this._delta)) { this._value[key] = value; } this._delta = {}; } /** * Clears the delta without committing. */ clearDelta() { this._delta = {}; } /** * Updates the state with the given values. * * @param values The values to update the state with */ update(values) { for (const [key, value] of Object.entries(values)) { this.set(key, value); } } /** * Sets an app-level state value. * * @param key The key to set * @param value The value to set */ setAppValue(key, value) { this.set(State.APP_PREFIX + key, value); } /** * Gets an app-level state value. * * @param key The key to get * @returns The app-level state value */ getAppValue(key) { return this.get(State.APP_PREFIX + key); } /** * Sets a user-level state value. * * @param key The key to set * @param value The value to set */ setUserValue(key, value) { this.set(State.USER_PREFIX + key, value); } /** * Gets a user-level state value. * * @param key The key to get * @returns The user-level state value */ getUserValue(key) { return this.get(State.USER_PREFIX + key); } /** * Sets a temporary state value. * * @param key The key to set * @param value The value to set */ setTempValue(key, value) { this.set(State.TEMP_PREFIX + key, value); } /** * Gets a temporary state value. * * @param key The key to get * @returns The temporary state value */ getTempValue(key) { return this.get(State.TEMP_PREFIX + key); } /** * Returns the state as a plain object. * * @returns The state as a plain object */ toObject() { const result = {}; // Add all values for (const [key, value] of Object.entries(this._value)) { result[key] = value; } // Override with deltas for (const [key, value] of Object.entries(this._delta)) { result[key] = value; } return result; } /** * Returns the metadata associated with the state. * TODO(b/314118552): Implement proper metadata handling if needed for Python ADK compatibility. * * @returns The state metadata as a plain object. */ getMetadata() { return {}; } } exports.State = State; /** * Prefix for app-level state keys. */ State.APP_PREFIX = 'app:'; /** * Prefix for user-level state keys. */ State.USER_PREFIX = 'user:'; /** * Prefix for temporary state keys. */ State.TEMP_PREFIX = 'temp:';