import { CheckpointManager, DeepAgentSessionStore } from './deepagent-sessions.js';
import { MemoryStore } from '../memory/memory-store.js';
import { extractSessionMemory } from '../memory/extract-session-memory.js';
export { deriveSessionTitle } from './deepagent-sessions.js';
export class SessionService {
    store;
    memoryStore;
    sessionsDir;
    checkpointer;
    checkpointManager;
    pendingCheckpointInitializer;
    checkpointAccessEnsured = false;
    constructor(options) {
        this.sessionsDir = options.sessionsDir;
        this.store = new DeepAgentSessionStore(options.sessionsDir);
        this.memoryStore = new MemoryStore(options.memoriesDir);
    }
    setCheckpointer(checkpointer) {
        this.checkpointer = checkpointer;
        this.checkpointManager = new CheckpointManager(checkpointer, this.sessionsDir);
        this.checkpointAccessEnsured = true;
    }
    registerCheckpointInitializer(initializer) {
        this.pendingCheckpointInitializer = initializer;
    }
    async ensureCheckpointAccess() {
        if (this.checkpointManager) {
            this.checkpointAccessEnsured = true;
            return;
        }
        if (this.pendingCheckpointInitializer) {
            const checkpointer = await this.pendingCheckpointInitializer();
            this.setCheckpointer(checkpointer);
            return;
        }
        throw new Error('Checkpoint access not available. Call setCheckpointer() or register a checkpoint initializer first.');
    }
    isCheckpointAccessReady() {
        return this.checkpointManager !== undefined;
    }
    list() {
        const records = this.store.list();
        return records.map((r) => ({
            id: r.id,
            title: r.title,
            createdAt: r.createdAt,
            updatedAt: r.updatedAt,
            messageCount: 0,
        }));
    }
    get(id) {
        return this.store.get(id);
    }
    create(options = {}) {
        return this.store.create(options);
    }
    ensure(sessionId, options = {}) {
        return this.store.ensure(sessionId, options);
    }
    resume(id) {
        return this.store.get(id);
    }
    async delete(id) {
        if (this.checkpointManager) {
            await this.checkpointManager.deleteAllCheckpoints(id);
        }
        this.store.delete(id);
        this.memoryStore.delete(id);
        if (this.checkpointer) {
            await this.store.deleteThread(this.checkpointer, id);
        }
    }
    touch(id, options) {
        return this.store.touch(id, options);
    }
    getOrCreateForScope(scope, options) {
        return this.store.getOrCreateActiveForScope(scope, { title: options?.title });
    }
    rotateForScope(scope, options) {
        return this.store.rotateActiveForScope(scope, { title: options?.title });
    }
    clearScope(scope) {
        this.store.clearActiveScope(scope);
    }
    listForScope(scope) {
        const all = this.store.list();
        return all.filter((r) => r.scope?.kind === scope.kind && r.scope?.key === scope.key);
    }
    getActiveForScope(scope) {
        return this.store.getActiveForScope(scope);
    }
    listCheckpointsSync(sessionId) {
        if (!this.checkpointManager) {
            return [];
        }
        return this.checkpointManager.listCheckpointsSync(sessionId);
    }
    persistMemory(sessionId, title, createdAt) {
        try {
            const memory = extractSessionMemory(sessionId, title, createdAt, []);
            this.memoryStore.save(memory);
        }
        catch {
            // Best-effort — never block.
        }
    }
    buildSessionConfig(sessionId) {
        return {
            configurable: { thread_id: sessionId },
            version: 'v2',
        };
    }
    async listCheckpoints(sessionId) {
        if (!this.checkpointManager) {
            await this.ensureCheckpointAccess();
        }
        return this.checkpointManager.listCheckpoints(sessionId);
    }
    async saveCheckpoint(sessionId, options = {}) {
        if (!this.checkpointManager) {
            await this.ensureCheckpointAccess();
        }
        return this.checkpointManager.saveCheckpoint(sessionId, options.compactionState);
    }
    async restoreCheckpoint(sessionId, checkpointId) {
        if (!this.checkpointManager) {
            await this.ensureCheckpointAccess();
        }
        const compactionState = await this.checkpointManager.restoreCheckpoint(sessionId, checkpointId);
        return {
            checkpointId,
            sessionId,
            compactionState,
            restoredAt: new Date().toISOString(),
        };
    }
    async deleteCheckpoint(sessionId, checkpointId) {
        if (!this.checkpointManager) {
            await this.ensureCheckpointAccess();
        }
        return this.checkpointManager.deleteCheckpoint(sessionId, checkpointId);
    }
    async deleteAllCheckpoints(sessionId) {
        if (!this.checkpointManager) {
            await this.ensureCheckpointAccess();
        }
        return this.checkpointManager.deleteAllCheckpoints(sessionId);
    }
}
//# sourceMappingURL=session-service.js.map