import type { AgentMessage } from '@earendil-works/pi-agent-core';
import type { Config } from '../config/schema.js';
import { type CompactionConfig, type CompactionResult } from '../agent/memory/compaction.js';
import { type WindowConfig } from '../agent/memory/window.js';
import type { XopcSessionTranscriptV1 } from './transcript-format.js';
import { type TranscriptStoredRow, type XopcTranscriptContextEntry } from './session-context-for-llm.js';
import type { SessionMetadata, SessionDetail, SessionListQuery, PaginatedResult, GlobalSessionStats, ExportFormat, CompactionCheckpointSummary, CompactionCheckpointDetail } from './types.js';
import { SessionStatus } from './types.js';
import type { Message } from './types.js';
import type { SessionTranscriptUpdate } from './transcript-events.js';
export interface SessionStoreOptions {
    config: Config;
    agentId?: string;
    sessionsDir?: string;
}
export declare class SessionStore {
    private options;
    private sessionsDir;
    private archiveDir;
    private storePath;
    private window;
    private compactor;
    private storeMutationDepth;
    private storeMutationChain;
    /** Cache of per-agent sessions dirs to avoid re-resolution on every call. */
    private agentSessionsDirCache;
    constructor(options: SessionStoreOptions, windowConfig?: Partial<WindowConfig>, compactionConfig?: Partial<CompactionConfig>);
    getSessionsRoot(): string;
    /**
     * OpenClaw-aligned: resolve the sessions directory for a given session key.
     * Extracts agentId from the session key and routes to `agents/<agentId>/sessions/`.
     * Falls back to the default sessions directory when agentId cannot be parsed
     * or when `sessionsDir` was explicitly provided in options.
     */
    private resolveSessionsDirForKey;
    private resolveStorePathForKey;
    private runStoreMutation;
    initialize(): Promise<void>;
    private transcriptPathForEntry;
    private readMapForKey;
    private readMap;
    /**
     * OpenClaw-aligned: read sessions.json from ALL known agents and merge into a single map.
     * Used by aggregation queries (list, getByAgent, getByAccount, etc.) so the gateway UI
     * can display sessions across all agents.
     */
    private readAllMaps;
    private getDiskEntry;
    private buildDefaultMetadata;
    private parseSessionKey;
    private extractRoutingFromKey;
    /** Resolve on-disk transcript path; creates session row + empty JSONL when missing. */
    resolveTranscriptPath(sessionKey: string): Promise<{
        sessionId: string;
        absPath: string;
        sessionsDir: string;
    }>;
    /** Ensure sessions.json has an entry and transcript file exist for `sessionKey`. */
    private ensureSession;
    private metadataFromEntry;
    getByAgent(agentId: string): Promise<SessionMetadata[]>;
    getByAccount(accountId: string): Promise<SessionMetadata[]>;
    getByPeer(peerKind: string, peerId: string): Promise<SessionMetadata[]>;
    getMainSession(channel: string, accountId: string): Promise<SessionMetadata | null>;
    refreshIndex(): Promise<void>;
    list(query?: SessionListQuery): Promise<PaginatedResult<SessionMetadata>>;
    get(key: string, options?: {
        includeTranscriptSummary?: boolean;
        includeTranscriptRows?: boolean;
    }): Promise<SessionDetail | null>;
    getMessagePage(key: string, options?: {
        offset?: number;
        limit?: number;
        before?: string;
        includeTranscriptSummary?: boolean;
        includeTranscriptRows?: boolean;
    }): Promise<{
        session: SessionDetail;
        pagination: {
            total: number;
            limit: number;
            offset: number;
            hasMore: boolean;
            before?: string;
            nextBeforeCursor?: string;
        };
    } | null>;
    private buildSessionDetail;
    loadTranscriptRows(key: string): Promise<TranscriptStoredRow[]>;
    getMetadata(key: string): Promise<SessionMetadata | null>;
    updateMetadata(key: string, updates: Partial<SessionMetadata>): Promise<void>;
    /**
     * Reset transcript for an existing session key: archive the current JSONL as
     * `*.reset.*`, assign a new `sessionId`, and preserve per-session overrides
     * on the disk entry (thinking/verbose) and in `sessions/config/*.json`.
     */
    reset(key: string): Promise<{
        sessionId: string;
        previousSessionId: string;
    } | null>;
    delete(key: string): Promise<boolean>;
    deleteMany(keys: string[]): Promise<{
        success: string[];
        failed: string[];
    }>;
    setStatus(key: string, status: SessionStatus): Promise<void>;
    archive(key: string): Promise<void>;
    unarchive(key: string): Promise<void>;
    pin(key: string): Promise<void>;
    unpin(key: string): Promise<void>;
    private moveToArchive;
    private findMostRecentDeletedTranscript;
    private moveFromArchive;
    loadMessages(key: string, options?: {
        fromArchive?: boolean;
    }): Promise<AgentMessage[]>;
    private loadDisplayMessages;
    loadTranscriptDocument(key: string): Promise<XopcSessionTranscriptV1 | null>;
    private writeTranscriptAndSyncIndex;
    /** Incremental sessions.json stats after guard append (OpenClaw transcript-events). */
    syncSessionsJsonFromTranscriptUpdate(update: SessionTranscriptUpdate): Promise<void>;
    appendTranscriptContextEntry(key: string, entry: Omit<XopcTranscriptContextEntry, 'kind'> & Partial<Pick<XopcTranscriptContextEntry, 'kind'>>): Promise<void>;
    /**
     * Bulk write entry point used by compaction, tests, and admin tools.
     * Runtime agent turns must persist via {@link guardSessionManager} + appendMessage.
     */
    saveMessages(key: string, messages: AgentMessage[]): Promise<void>;
    getWindowStats(messages: AgentMessage[]): {
        total: number;
        system: number;
        user: number;
        assistant: number;
        tool: number;
        needsTrim: boolean;
    };
    needsCompaction(key: string, messages: AgentMessage[], contextWindow: number): {
        needed: boolean;
        reason: string;
        usagePercent?: number;
    };
    prepareCompaction(key: string, messages: AgentMessage[], contextWindow: number): {
        needsCompaction: boolean;
        messages: AgentMessage[];
        stats?: ReturnType<typeof this.compactor.needsCompaction>;
    };
    private checkpointBasename;
    private pruneCompactionCheckpoints;
    private captureCompactionCheckpoint;
    applyCompaction(key: string, messages: AgentMessage[], result: CompactionResult): Promise<AgentMessage[]>;
    compact(key: string, messages: AgentMessage[], contextWindow: number, instructions?: string, force?: boolean): Promise<CompactionResult>;
    getCompactionStats(key: string): Promise<{
        compactionCount: number;
        totalTokensBefore: number;
        totalTokensAfter: number;
        lastCompactionAt: any;
    }>;
    listCompactionCheckpoints(key: string): Promise<CompactionCheckpointSummary[]>;
    getCompactionCheckpointDetail(key: string, checkpointId: string): Promise<CompactionCheckpointDetail | null>;
    restoreCompactionCheckpoint(key: string, checkpointId: string): Promise<void>;
    deleteSession(key: string): Promise<boolean>;
    load(key: string, options?: {
        fromArchive?: boolean;
    }): Promise<AgentMessage[]>;
    estimateTokenUsage(_key: string, messages: AgentMessage[]): Promise<number>;
    searchInSession(key: string, keyword: string): Promise<Message[]>;
    exportSession(key: string, format: ExportFormat): Promise<string>;
    getStats(): Promise<GlobalSessionStats>;
    archiveOld(olderThanDays: number): Promise<number>;
    estimateTokens(messages: AgentMessage[]): number;
    private messageContent;
    private isCompactionSummaryMessage;
    private displayMessageIdentity;
    private extractTextContent;
    private injectPostCompactionContext;
    private convertMessages;
}
