import type { AgentMessage } from '@earendil-works/pi-agent-core'; import type { Api, Model } from '@earendil-works/pi-ai/compat'; import { IdleCompactionScheduler } from '../agent/memory/idle-compaction-scheduler.js'; import type { ResolvedCompactionPolicy } from '../agent/memory/compaction-policy.js'; import type { Config } from '../config/schema.js'; import type { SessionAgentConfig } from './config-types.js'; import { type CompactionConfig, type CompactionExecutionOptions, type CompactionResult } from '../agent/memory/compaction.js'; import { type WindowConfig } from '../agent/memory/window.js'; import { loadCompactionSourceSnapshot, type SessionMetadataSeed } from '../storage/sqlite/index.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, CompactionBoundarySummary } from './types.js'; import { SessionStatus } from './types.js'; import type { Message } from './types.js'; import type { SessionTranscriptUpdate } from './transcript-events.js'; import { type SessionFindResult } from './session-find.js'; export interface SessionStoreOptions { config: Config; } export interface SessionCompactionHooks { before?: (event: { conversationId: string; messageCount: number; tokenCount: number; }) => Promise | void; after?: (event: { conversationId: string; messageCount: number; tokenCount: number; compactedCount: number; }) => Promise | void; } export type ModelFallbackPreparation = 'prompt' | 'resume' | 'unsafe'; export interface ForkSessionAtTurnOptions { targetKey: string; lastTurnId: string; targetMetadata: SessionMetadataSeed; } export interface ForkSessionResult { conversationId: string; rowCount: number; lastTurnId?: string; } export interface CreateSessionFromRowsOptions { targetKey: string; cwd: string; metadata: SessionMetadataSeed & { agentId: string; }; rows: TranscriptStoredRow[]; config?: SessionAgentConfig; } export interface PreparedCompaction { conversationId: string; snapshot: NonNullable>; result: CompactionResult; } export declare class SessionStore { private options; readonly idleCompaction: IdleCompactionScheduler; private window; private compactor; private compactionHooks; private readonly compactionTails; private compactionCheckpoint; private clearCompactionCheckpoint; constructor(options: SessionStoreOptions, windowConfig?: Partial, compactionConfig?: Partial); /** Refresh policy for subsequent compactions without changing an in-flight request. */ updateConfig(config: Config): void; setCompactionHooks(hooks: SessionCompactionHooks): void; private runCompactionHook; private resolveWorkspaceCwd; private runStoreMutation; private runCompactionExclusive; initialize(): Promise; getSessionsRoot(): string; resolveTranscriptPath(conversationId: string, options?: { metadata?: SessionMetadataSeed; }): Promise<{ transcriptId: string; conversationId: string; }>; appendTranscriptMessage(conversationId: string, message: AgentMessage, options?: { metadata?: SessionMetadataSeed; }): Promise; getByAgent(agentId: string): Promise; getByAccount(accountId: string): Promise; getByPeer(peerKind: string, peerId: string): Promise; getMainSession(channel: string, accountId: string): Promise; refreshIndex(): Promise; list(query?: SessionListQuery): Promise>; get(key: string, options?: { includeTranscriptSummary?: boolean; includeTranscriptRows?: boolean; }): Promise; getMessagePage(key: string, options?: { offset?: number; limit?: number; before?: string; includeTranscriptSummary?: boolean; includeTranscriptRows?: boolean; includeContextRows?: boolean; compact?: boolean; }): Promise<{ session: SessionDetail; pagination: { total: number; limit: number; offset: number; hasMore: boolean; before?: string; nextBeforeCursor?: string; revision?: number; }; } | null>; private buildSessionDetail; loadTranscriptRows(key: string): Promise; loadTranscriptHistoryRows(key: string): Promise; prepareModelFallback(key: string, rowsBeforeAttempt: readonly TranscriptStoredRow[]): Promise; getMetadata(key: string): Promise; resolveKeyByTranscriptId(transcriptId: string): Promise; updateMetadata(key: string, updates: Partial): Promise; reset(key: string): Promise<{ transcriptId: string; previousTranscriptId: string; } | null>; delete(key: string): Promise; deleteMany(keys: string[]): Promise<{ success: string[]; failed: string[]; }>; setStatus(key: string, status: SessionStatus): Promise; archive(key: string): Promise; unarchive(key: string): Promise; pin(key: string): Promise; unpin(key: string): Promise; loadMessages(_key: string, _options?: { fromArchive?: boolean; }): Promise; loadTranscriptDocument(key: string): Promise; syncEmbeddedTranscriptUpdate(update: SessionTranscriptUpdate): Promise; appendTranscriptContextEntry(key: string, entry: Omit & Partial>): Promise; appendTranscriptLabelEntry(key: string, entry: { targetId: string; label?: string; }): Promise; appendTranscriptCustomEntry(key: string, entry: { customType: string; data?: unknown; }): Promise; appendTranscriptCustomMessageEntry(key: string, entry: { expectedTranscriptId?: string; customType: string; content?: string | unknown[]; display?: boolean; details?: unknown; }): Promise; appendTranscriptBashExecutionEntry(key: string, entry: { command: string; output?: string; exitCode?: number | null; signal?: string | null; excludeFromContext?: boolean; truncated?: boolean; fullOutputPath?: string; }): Promise; saveMessages(key: string, messages: AgentMessage[], options?: { metadata?: SessionMetadataSeed; }): Promise; deleteUserRound(key: string, userRoundIndex: number): Promise<{ deleted: number; removedMessages: AgentMessage[]; remainingMessages: AgentMessage[]; } | null>; getWindowStats(messages: AgentMessage[]): { total: number; system: number; user: number; assistant: number; tool: number; needsTrim: boolean; }; applyCompaction(key: string, result: CompactionResult, expectedSnapshot?: import("../storage/sqlite/transcript-repository.js").CompactionSourceSnapshot): Promise; /** Generate against a fixed snapshot without holding the foreground compaction queue. */ prepareCompaction(key: string, model: Model, policy: ResolvedCompactionPolicy, signal: AbortSignal): Promise; commitCompaction(candidate: PreparedCompaction, isCurrent: () => boolean): Promise; compact(key: string, messages: AgentMessage[], model: Model, instructions?: string, force?: boolean, executionOptions?: CompactionExecutionOptions): Promise; getCompactionStats(key: string): Promise<{ compactionCount: number; totalTokensBefore: number; totalTokensAfter: number; auditPassedCount: number; auditDegradedCount: number; auditMissingItemsFound: number; lastCompactionAt: string; }>; listCompactionBoundaries(key: string): Promise; restoreBeforeCompactionBoundary(key: string, compactionId: string): Promise; deleteSession(key: string): Promise; load(key: string, options?: { fromArchive?: boolean; }): Promise; estimateTokenUsage(_key: string, messages: AgentMessage[]): Promise; searchInSession(key: string, keyword: string): Promise; findInSession(key: string, query: string, limit?: number): Promise; recallSession(key: string, query: string, options?: { limit?: number; beforeSeq?: number; }): import("../storage/sqlite/transcript-repository.js").SessionTranscriptRecallMatch[]; exportSession(key: string, format: ExportFormat): Promise; importSessionExport(targetKey: string, jsonContent: string): Promise<{ conversationId: string; rowCount: number; }>; forkSession(sourceKey: string, targetKey: string): Promise<{ conversationId: string; rowCount: number; }>; createSessionFromRows(options: CreateSessionFromRowsOptions): Promise<{ conversationId: string; rowCount: number; }>; forkSessionRows(sourceKey: string, targetKey: string, options?: { throughRow?: number; }): Promise<{ conversationId: string; rowCount: number; }>; /** * Atomically copy a completed turn prefix into a new session, including the * source session's per-session agent configuration. */ forkSessionAtTurn(sourceKey: string, options: ForkSessionAtTurnOptions): Promise; getStats(): Promise; archiveOld(olderThanDays: number): Promise; estimateTokens(messages: AgentMessage[]): number; private loadDisplayMessages; private paginateDisplayMessages; private messageContent; private extractTextContent; private injectPostCompactionContext; private convertMessages; }