import EventEmitter from 'events'; import type { Api, Model } from '@earendil-works/pi-ai/compat'; import { SessionStore } from './store.js'; import type { SessionMetadata, SessionDetail, SessionListQuery, PaginatedResult, GlobalSessionStats, ExportFormat, SessionStatus } from './types.js'; import type { SessionMetadataSeed } from '../storage/sqlite/index.js'; import type { Message } from './types.js'; import type { CompactionConfig, CompactionExecutionOptions, CompactionResult } from '../agent/memory/compaction.js'; import type { XopcSessionTranscriptV1 } from './transcript-format.js'; import type { XopcTranscriptContextEntry } from './session-context-for-llm.js'; import { type SessionPatchBody } from './patch-metadata.js'; import type { WindowConfig } from '../agent/memory/window.js'; import type { Config } from '../config/schema.js'; export interface SessionIndexConfig { config: Config; windowConfig?: Partial; compactionConfig?: Partial; } export declare class SessionIndex extends EventEmitter { private store; constructor(config: SessionIndexConfig); initialize(): Promise; /** Low-level store (e.g. cron resolving weixin delivery from session index). */ getStore(): SessionStore; listSessions(query?: SessionListQuery): Promise>; listSubagents(query?: SessionListQuery): Promise>; getSession(key: string, options?: { includeTranscriptSummary?: boolean; includeTranscriptRows?: boolean; }): Promise; getSessionMessagePage(key: string, options?: { offset?: number; limit?: number; before?: string; includeTranscriptSummary?: boolean; includeTranscriptRows?: boolean; includeContextRows?: boolean; }): Promise<{ session: SessionDetail; pagination: { total: number; limit: number; offset: number; hasMore: boolean; before?: string; nextBeforeCursor?: string; }; } | null>; /** * OpenClaw-style `sessions.patch`: partial metadata (name, tags, customData shallow merge). */ patchSession(key: string, patch: SessionPatchBody): Promise<{ ok: true; } | { ok: false; error: string; }>; getSessionMetadata(key: string): Promise; resolveSessionKeyBySessionId(sessionId: string): Promise; deleteSession(key: string): Promise; deleteSessions(keys: string[]): Promise<{ success: string[]; failed: string[]; }>; renameSession(key: string, name: string): Promise; /** Partial metadata update (caller merges nested fields like `customData` when needed). */ updateSessionMetadata(key: string, updates: Partial): Promise; tagSession(key: string, tags: string[]): Promise; untagSession(key: string, tags: string[]): Promise; setSessionTags(key: string, tags: string[]): Promise; archiveSession(key: string): Promise; unarchiveSession(key: string): Promise; pinSession(key: string): Promise; unpinSession(key: string): Promise; setSessionStatus(key: string, status: SessionStatus): Promise; searchSessions(query: string): Promise; searchInSession(key: string, keyword: string): Promise; exportSession(key: string, format: ExportFormat): Promise; importSessionExport(targetKey: string, jsonContent: string): Promise<{ sessionKey: string; rowCount: number; }>; forkSession(sourceKey: string, targetKey: string): Promise<{ sessionKey: string; rowCount: number; }>; forkSessionRows(sourceKey: string, targetKey: string, options?: { throughRow?: number; }): Promise<{ sessionKey: string; rowCount: number; }>; getStats(): Promise; archiveOldSessions(olderThanDays: number): Promise; onSessionCreated(callback: (metadata: SessionMetadata) => void): void; onSessionUpdated(callback: (data: { key: string; name?: string; tags?: string[]; }) => void): void; onSessionDeleted(callback: (data: { key: string; }) => void): void; onSessionArchived(callback: (data: { key: string; }) => void): void; onSessionRestored(callback: (data: { key: string; }) => void): void; onSessionPinned(callback: (data: { key: string; }) => void): void; onSessionUnpinned(callback: (data: { key: string; }) => void): void; onSessionStatusChanged(callback: (data: { key: string; status: SessionStatus; }) => void): void; onSessionAccessed(callback: (data: { key: string; }) => void): void; /** Load messages for a session key */ loadMessages(key: string): Promise; /** Wrapped transcript document (stable id, compaction history); null if missing or not a valid envelope. */ loadTranscriptDocument(key: string): Promise; /** * Runtime turns must use PiTranscriptManager.appendMessage; this entry point * is reserved for compaction, tests, and admin tools. */ saveMessages(key: string, messages: any[], options?: { metadata?: SessionMetadataSeed; }): Promise; deleteUserRound(key: string, userRoundIndex: number): Promise<{ deleted: number; removedMessages: import("@earendil-works/pi-agent-core").AgentMessage[]; remainingMessages: import("@earendil-works/pi-agent-core").AgentMessage[]; }>; /** * Append `kind: 'context'` transcript row (persisted, excluded from {@link loadMessages} / LLM). */ 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: { 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; /** Delete session data */ delete(key: string): Promise; /** Archive transcript and start a new session id for the same key. */ resetSession(key: string): Promise<{ sessionId: string; previousSessionId: string; } | null>; /** Token/window stats for a message list */ getWindowStats(messages: any[]): { total: number; system: number; user: number; assistant: number; tool: number; needsTrim: boolean; }; /** Compact session messages */ compact(key: string, messages: any[], model: Model, instructions?: string, force?: boolean, executionOptions?: CompactionExecutionOptions): Promise; /** Compaction stats for a session */ getCompactionStats(key: string): Promise<{ compactionCount: number; totalTokensBefore: number; totalTokensAfter: number; lastCompactionAt: string; }>; listCompactionBoundaries(key: string): Promise; restoreBeforeCompactionBoundary(key: string, compactionId: string): Promise; /** Estimate token usage for messages */ estimateTokenUsage(key: string, messages: any[]): Promise; }