adk-typescript
Version:
TypeScript port of Google's Agent Development Kit (ADK)
87 lines (86 loc) • 2.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Session = void 0;
const State_1 = require("./State");
/**
* Represents a session for managing agents and their state.
*/
class Session {
/**
* Creates a new session.
*
* @param options Options for the session
*/
constructor(options = {}) {
/** The agents in the session - directly accessible as public property */
this.agents = new Map();
/** The events of the session */
this.events = [];
/** The conversation history */
this.conversationHistory = [];
/** The last update time of the session */
this.lastUpdateTime = 0;
this.id = options.id || generateUuid();
this.appName = options.appName || 'app';
this.userId = options.userId || 'user';
this.state = new State_1.State(options.state);
if (options.events) {
this.events = [...options.events];
}
}
/**
* Adds an agent to the session.
*
* @param agent The agent to add
*/
addAgent(agent) {
this.agents.set(agent.name, agent);
}
/**
* Gets an agent from the session.
*
* @param name The name of the agent
* @returns The agent, or undefined if not found
*/
getAgent(name) {
return this.agents.get(name);
}
/**
* Adds content to the conversation history.
*
* @param content The content to add
*/
addConversationHistory(content) {
this.conversationHistory.push(content);
}
/**
* Gets the conversation history.
*
* @returns The conversation history
*/
getConversationHistory() {
return [...this.conversationHistory];
}
/**
* Adds an event to the session.
*
* @param event The event to add
*/
addEvent(event) {
this.events.push(event);
this.lastUpdateTime = Date.now();
}
}
exports.Session = Session;
/**
* Generates a UUID.
*
* @returns A UUID string
*/
function generateUuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}