adk-typescript
Version:
TypeScript port of Google's Agent Development Kit (ADK)
99 lines (98 loc) • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.State = exports.StatePrefix = void 0;
/**
* Constants for state key prefixes
*/
class StatePrefix {
}
exports.StatePrefix = StatePrefix;
StatePrefix.APP_PREFIX = 'app:';
StatePrefix.USER_PREFIX = 'user:';
StatePrefix.TEMP_PREFIX = 'temp:';
/**
* A class for managing session state.
* This is a dictionary-like object with methods similar to the Python version.
*/
class State {
/**
* Creates a new state.
*
* @param initialState Initial state values
*/
constructor(initialState = {}) {
// Copy all properties from initialState to this object
Object.assign(this, initialState);
}
/**
* Gets a value from the state.
* Similar to Python's dictionary get method.
*
* @param key The key of the value
* @returns The value, or undefined if not found
*/
get(key) {
return this[key];
}
/**
* Sets a value in the state.
*
* @param key The key of the value
* @param value The value to set
*/
set(key, value) {
this[key] = value;
}
/**
* Checks if the state has a value for the key.
*
* @param key The key to check
* @returns True if the state has a value for the key, false otherwise
*/
has(key) {
return key in this;
}
/**
* Deletes a value from the state.
*
* @param key The key of the value to delete
* @returns True if the value was deleted, false otherwise
*/
delete(key) {
if (key in this) {
delete this[key];
return true;
}
return false;
}
/**
* Gets all the state as a record.
*
* @returns The state as a record
*/
getAll() {
const result = {};
for (const key in this) {
if (typeof this[key] !== 'function' && Object.prototype.hasOwnProperty.call(this, key)) {
result[key] = this[key];
}
}
return result;
}
/**
* Update state with new key-value pairs.
* Similar to Python's dictionary update method.
*
* @param data The data to update
*/
update(data) {
Object.assign(this, data);
}
/**
* Custom implementation for JSON serialization
*/
toJSON() {
return this.getAll();
}
}
exports.State = State;