/**
 * SessionManager manages persistent REPL sessions for JS and Python.
 * It keeps workers alive across multiple tool calls.
 */
export class SessionManager {
    sessions = new Map();
    createSession(type, worker, id) {
        const sessionId = id || Math.random().toString(36).substring(2, 11);
        this.sessions.set(sessionId, {
            id: sessionId,
            type,
            worker,
            createdAt: Date.now(),
            lastUsedAt: Date.now(),
        });
        return sessionId;
    }
    getSession(id) {
        const session = this.sessions.get(id);
        if (session) {
            session.lastUsedAt = Date.now();
        }
        return session;
    }
    terminateSession(id) {
        const session = this.sessions.get(id);
        if (session) {
            session.worker.terminate();
            this.sessions.delete(id);
            return true;
        }
        return false;
    }
    listSessions() {
        return Array.from(this.sessions.values()).map((s) => ({
            id: s.id,
            type: s.type,
            age: Date.now() - s.createdAt,
            lastUsed: Date.now() - s.lastUsedAt,
        }));
    }
    /** Terminate all sessions and release resources. */
    async dispose() {
        for (const session of this.sessions.values()) {
            session.worker.terminate();
        }
        this.sessions.clear();
    }
}
