Path: blob/main/extensions/copilot/src/platform/chronicle/common/sessionStore.ts
13401 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { createServiceIdentifier } from '../../../util/common/services';67// ── Service identifier ──────────────────────────────────────────────────────────89export const ISessionStore = createServiceIdentifier<ISessionStore>('ISessionStore');1011// ── Row types (same as copilot-agent-runtime SessionStore) ──────────────────────1213/**14* Metadata for a session row.15*/16export interface SessionRow {17id: string;18cwd?: string;19repository?: string;20host_type?: string;21branch?: string;22summary?: string;23agent_name?: string;24agent_description?: string;25created_at?: string;26updated_at?: string;27}2829/**30* A conversation turn (user→assistant pair).31*/32export interface TurnRow {33session_id: string;34turn_index: number;35user_message?: string;36assistant_response?: string;37timestamp?: string;38}3940/**41* A compaction checkpoint broken into sections.42*/43export interface CheckpointRow {44session_id: string;45checkpoint_number: number;46title?: string;47overview?: string;48history?: string;49work_done?: string;50technical_details?: string;51important_files?: string;52next_steps?: string;53created_at?: string;54}5556/**57* A file touched during a session.58*/59export interface FileRow {60session_id: string;61file_path: string;62tool_name?: string;63turn_index?: number;64first_seen_at?: string;65}6667/**68* A reference linking a session to an external entity.69*/70export interface RefRow {71session_id: string;72ref_type: 'commit' | 'pr' | 'issue';73ref_value: string;74turn_index?: number;75created_at?: string;76}7778/**79* A single FTS5 search result.80*/81export interface SearchResult {82session_id: string;83source_type: string;84content: string;85rank: number;86}8788// ── Service interface ───────────────────────────────────────────────────────────8990export interface ISessionStore {91readonly _serviceBrand: undefined;9293/** Get the path to the database file. */94getPath(): string;9596// ── CRUD ────────────────────────────────────────────────────────────9798/** Insert or update a session's metadata. */99upsertSession(session: SessionRow): void;100101/** Insert a conversation turn and index it for full-text search. */102insertTurn(turn: TurnRow): void;103104/** Insert a compaction checkpoint and index its sections for full-text search. */105insertCheckpoint(checkpoint: CheckpointRow): void;106107/** Record a file touched during a session. First occurrence wins. */108insertFile(file: FileRow): void;109110/** Record a reference (commit, PR, issue) for a session. */111insertRef(ref: RefRow): void;112113/** Index a workspace artifact for full-text search. Upserts by file path. */114indexWorkspaceArtifact(sessionId: string, filePath: string, content: string): void;115116// ── Queries ─────────────────────────────────────────────────────────117118/** Full-text search across all indexed content. */119search(query: string, limit?: number): SearchResult[];120121/** Get a session by ID. */122getSession(sessionId: string): SessionRow | undefined;123124/** Get all turns for a session, ordered by turn index. */125getTurns(sessionId: string): TurnRow[];126127/** Get all files touched in a session. */128getFiles(sessionId: string): FileRow[];129130/** Get all refs for a session. */131getRefs(sessionId: string): RefRow[];132133/** Get the highest turn_index for a session, or -1 if none. */134getMaxTurnIndex(sessionId: string): number;135136/** Get basic stats about the store. */137getStats(): { sessions: number; turns: number; checkpoints: number; files: number; refs: number };138139/** Execute a raw read-only SQL query (enforced via SQLite authorizer). */140executeReadOnly(sql: string): Record<string, unknown>[];141142/**143* Execute a read-only SQL query without authorizer enforcement.144* Used as a fallback when the authorizer API is unavailable (Node.js < 24.2).145* Callers MUST validate SQL safety before calling this method.146*/147executeReadOnlyFallback(sql: string): Record<string, unknown>[];148149/** Run a function inside a SQLite transaction. */150runInTransaction(fn: () => void): void;151152/** Close the database connection. */153close(): void;154}155156157