/** * @packageDocumentation * GS2-121 — opening the files the history store is made of. Every opener in the history modules * comes through here, so the rules in `historyLayout.ts` (a single-file store is split on first * open, every file is migrated when opened, a missing index is rebuilt from the thread files) apply * to all of them at once. */ import { DatabaseSync } from 'node:sqlite'; /** * What an open found: * - `ready` — the store is a directory in the current layout (possibly just split into it); * - `absent` — nothing is there and the caller did not ask to create it; * - `unavailable` — something is there that cannot be used now: a split another process is * running, an old file that would not open, or a path that is neither a file nor a directory. */ export type HistoryStoreState = 'ready' | 'absent' | 'unavailable'; /** * Make the store at `storePath` ready to open: split a single-file store (or finish a split that * was interrupted), and with `create`, create the store directory and `threads/`. The directory * is created WITHOUT its parents, so a path under a directory that does not exist is refused. * Never throws. */ export declare function prepareHistoryStore(storePath: string, options?: { create?: boolean; }): HistoryStoreState; /** One file in `threads/`. */ export interface ThreadFileEntry { /** The file's name inside `threads/`. */ name: string; /** Its full path. */ path: string; } /** Every thread file of the store, sorted by name. `[]` when there is no `threads/`. */ export declare function listThreadFiles(storePath: string): ThreadFileEntry[]; /** The thread id stored inside an open thread file, or `undefined` when it has none. */ export declare function storedThreadId(db: DatabaseSync): string | undefined; /** * The thread a file holds: read off its name when the name spells it, otherwise from inside the * file. `undefined` when neither answers. */ export declare function threadIdOfFile(entry: ThreadFileEntry): string | undefined; /** * Open the file of `threadId`, migrated and stamped with its id. `null` when the file does not exist * and `create` is false. THROWS when the file cannot be opened or migrated, or holds another * thread: the checkpoint saver treats that as a failed write, and every other caller is fail-soft * around it. */ export declare function openThreadDb(storePath: string, threadId: string, options?: { create?: boolean; }): DatabaseSync | null; /** Make sure the file of `threadId` exists and is migrated. Throws like {@link openThreadDb}. */ export declare function ensureThreadFile(storePath: string, threadId: string): string; /** * Open the index, migrated. A missing index beside thread files is rebuilt from them first; a * missing index with no thread files is created only when `create` is set. `null` when there is * nothing to open. Throws when the index cannot be opened or migrated. */ export declare function openIndexDb(storePath: string, options?: { create?: boolean; }): DatabaseSync | null; /** What {@link rebuildHistoryIndex} put back. */ export interface IndexRebuildSummary { /** Files under `threads/` read: thread files and conversation record files alike. */ threadFiles: number; /** Conversations restored into the index. */ conversations: number; /** Turns restored into the index. */ turns: number; /** Files that could not be read, and were left alone. */ unreadable: number; } /** * Rebuild `index.db` from the thread files alone, replacing whatever index was there. Each * conversation keeps its integer id and run id. Should two files claim the same integer id (an * index that was lost and started again before this ran), the later one is given a new id and its * file is updated to match, so the files stay the record. Two records of the same run id are one * conversation: the first is kept. * * An index row whose record file is gone is not carried over, which is the rule that deleting one * file loses only what it held. The rebuilt index is written beside the old one and renamed over it, * so a failure part-way leaves the old index as it was. Throws on failure; the callers report it. */ export declare function rebuildHistoryIndex(storePath: string): IndexRebuildSummary; /** * The bytes the store occupies on disk: every file under it, the index and its journal and every * thread file included. Zero when nothing is there. */ export declare function storeDiskBytes(storePath: string): number; /** * Delete one thread file and its journal. Returns whether the file is gone. On win32 a file another * connection holds open cannot be deleted; that is reported as `false`, never thrown. */ export declare function deleteThreadFile(path: string): boolean;