import { type ChatMessage, type ToolCall, type ToolResult } from "../types.js"; import { type ImageBudget } from "../attachments/image-content.js"; import type { TranscriptItem } from "../app/ports/transcript-item.js"; import type { PreviousTurnSignal } from "../agent/continue-orient.js"; import { type HistorySummary } from "./history-index.js"; export type { HistorySummary } from "./history-index.js"; /** Persisted token/context footer snapshot (survives /history resume). */ export interface PersistedContextUsage { contextTokens: number; contextLimit?: number | undefined; lastCompletionTokens?: number | undefined; sessionPromptTokens?: number | undefined; sessionCompletionTokens?: number | undefined; exact: boolean; /** Additive canonical snapshot; old records retain the six legacy fields. */ contextSnapshot?: import("../llm/context-snapshot.js").ContextSnapshotV1 | undefined; routeUsage?: readonly import("../app/controllers/session-usage-ledger.js").PersistedRouteUsage[] | undefined; } export interface HistoryRecord { id: string; /** Unique writer generation, compared before the per-writer revision. */ writerGeneration?: string | undefined; /** * Monotonic whole-snapshot revision within one writer generation. Unlike * updatedAt, this records capture order rather than I/O completion order. */ revision?: number | undefined; name?: string | undefined; createdAt: string; updatedAt: string; cwd: string; messages: ChatMessage[]; /** * Optional TUI display transcript. Older records only have `messages`; * they still restore as user/assistant summaries. */ transcript?: TranscriptItem[] | undefined; /** * Last known context fill (prefer exact API usage). Without this, resume * falls back to a cheap estimate that under-counts vs live turns. */ contextUsage?: PersistedContextUsage | undefined; /** Last settled outcome, or an interrupted in-flight turn, for restart recovery. */ previousTurn?: PreviousTurnSignal | undefined; /** * Session workspace folder name under `{tmpdir}/clai/` * (e.g. `a3f9c1-18-07-2026-14-24-23`). Bound on resume so scratch + * tool outputs stay isolated per history session. */ workspaceFolder?: string | undefined; /** 6-digit hex code that prefixes {@link workspaceFolder}. */ workspaceCode?: string | undefined; } export interface ToolCallRecord { id: string; sessionId: string; createdAt: string; name: string; args: Record; ok: boolean; exitCode?: number | undefined; output: string; } export declare function materializeHistoryImages(messages: readonly ChatMessage[], budget?: ImageBudget): ChatMessage[]; /** * Compare snapshots by writer generation, then capture revision. Legacy rows * without a generation retain revision/timestamp fallback for compatibility. */ export declare function compareHistoryFreshness(left: HistoryRecord, right: HistoryRecord): number; /** Keep the newest captured version of each session id. */ export declare function dedupeHistoryById(records: readonly HistoryRecord[]): HistoryRecord[]; export declare function sortHistoryByUpdatedDesc(records: readonly HistoryRecord[]): HistoryRecord[]; /** * Apply retention by *recency* (not file order). Pruned sessions are returned * separately so callers can archive them — never silently destroy history. * limit <= 0 means unlimited (keep everything). */ export declare function partitionByRetention(records: readonly HistoryRecord[], limit: number): { kept: HistoryRecord[]; pruned: HistoryRecord[]; }; export declare function recoverOrphanedHistory(): Promise<{ recovered: number; sources: string[]; }>; export declare function saveSession(messages: ChatMessage[], name?: string | undefined, transcript?: TranscriptItem[] | undefined, contextUsage?: PersistedContextUsage | undefined, revision?: number | undefined, writerGeneration?: string | undefined, previousTurn?: PreviousTurnSignal | null | undefined): Promise; export declare function upsertSession(id: string, messages: ChatMessage[], name?: string | undefined, transcript?: TranscriptItem[] | undefined, contextUsage?: PersistedContextUsage | undefined, revision?: number | undefined, writerGeneration?: string | undefined, previousTurn?: PreviousTurnSignal | null | undefined): Promise; export declare function saveToolCall(sessionId: string, call: ToolCall, result: ToolResult): Promise; export declare function listSessionSummaries(limit?: number, options?: { recovery?: "blocking" | "background"; }): Promise; export declare function listSessions(limit?: number, options?: { recovery?: "blocking" | "background"; }): Promise; export declare function getSession(sessionId: string): Promise; export declare function getHistoryPath(): string; export declare function clearAllHistory(): Promise<{ cleared: boolean; detail: string; }>; export declare function deleteSession(sessionId: string): Promise<{ deleted: boolean; detail: string; }>; export declare function purgeSession(sessionId: string): Promise<{ deleted: boolean; detail: string; removedWorkspace: boolean; removedPlan: boolean; }>; export declare function getJsonlHistoryPath(): string; export declare function getHistoryArchivePath(): string; export declare function getHistoryBackupDir(): string;