/** * Session - Unified session management for PraisonAI * * Python parity with praisonaiagents/session/api.py * Provides session management with persistent state, memory, and knowledge. */ import type { DbAdapter } from '../db/types'; /** * Message in a session. */ export interface SessionMessage { role: 'user' | 'assistant' | 'system' | 'tool'; content: string; name?: string; timestamp?: number; metadata?: Record; } /** * Configuration for Session. */ export interface SessionConfig { /** Unique session identifier (auto-generated if not provided) */ id?: string; /** User identifier for user-specific operations */ userId?: string; /** Parent session for hierarchy */ parent?: Session; /** Database adapter for persistence */ db?: DbAdapter; /** Time-to-live in seconds */ ttl?: number; /** Memory configuration */ memoryConfig?: Record; /** Knowledge configuration */ knowledgeConfig?: Record; /** Remote agent URL for direct connectivity */ agentUrl?: string; /** HTTP timeout for remote calls */ timeout?: number; } /** * Session state that can be persisted. */ export interface SessionState { /** Session ID */ id: string; /** User ID */ userId: string; /** Custom state data */ data: Record; /** Messages in the session */ messages: SessionMessage[]; /** Creation timestamp */ createdAt: number; /** Last update timestamp */ updatedAt: number; /** Parent session ID if any */ parentId?: string; } /** * A unified session management class for PraisonAI. * * Provides: * - Session management with persistent state * - Memory operations (short-term, long-term, user-specific) * - Knowledge base operations * - Agent state management * - Session hierarchy (parent/child relationships) * * @example * ```typescript * import { Session } from 'praisonai'; * * // Create a session * const session = new Session({ userId: 'user_123' }); * * // Store state * session.set('topic', 'AI research'); * * // Add messages * session.addMessage({ role: 'user', content: 'Hello!' }); * * // Create child session * const childSession = session.createChild(); * * // Save session * await session.save(); * ``` */ export declare class Session { readonly id: string; readonly userId: string; readonly parent?: Session; private readonly db?; private readonly ttl?; private readonly agentUrl?; private readonly timeout; private readonly isRemote; private state; private messages; private createdAt; private updatedAt; private children; constructor(config?: SessionConfig); /** * Create a child session. */ createChild(config?: Partial): Session; /** * Get the root session in the hierarchy. */ getRoot(): Session; /** * Get all ancestors (parent chain). */ getAncestors(): Session[]; /** * Get all children. */ getChildren(): Session[]; /** * Get depth in hierarchy (0 = root). */ getDepth(): number; /** * Get a value from session state. */ get(key: string): T | undefined; /** * Set a value in session state. */ set(key: string, value: T): void; /** * Delete a value from session state. */ delete(key: string): boolean; /** * Check if key exists in state. */ has(key: string): boolean; /** * Get all state keys. */ keys(): string[]; /** * Get entire state object. */ getState(): Record; /** * Set entire state object. */ setState(state: Record): void; /** * Clear all state. */ clearState(): void; /** * Add a message to the session. */ addMessage(message: SessionMessage): void; /** * Get all messages. */ getMessages(): SessionMessage[]; /** * Get last N messages. */ getLastMessages(n: number): SessionMessage[]; /** * Clear all messages. */ clearMessages(): void; /** * Get message count. */ getMessageCount(): number; /** * Save session to database. */ save(): Promise; /** * Load session from database. */ load(): Promise; /** * Export session to JSON. */ toJSON(): SessionState; /** * Create session from JSON. */ static fromJSON(json: SessionState, config?: Partial): Session; /** * Check if session is expired. */ isExpired(): boolean; /** * Get session age in seconds. */ getAge(): number; /** * Get time since last update in seconds. */ getIdleTime(): number; } /** * Create a Session instance. */ export declare function createSession(config?: SessionConfig): Session;