Path: blob/main/extensions/copilot/src/extension/chatSessions/common/chatSessionMetadataStore.ts
13399 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 type * as vscode from 'vscode';6import { createServiceIdentifier } from '../../../util/common/services';7import { ChatSessionWorktreeProperties } from './chatSessionWorktreeService';8import type { IWorkspaceInfo } from './workspaceInfo';910export interface WorkspaceFolderEntry {11readonly folderPath: string;12readonly timestamp: number;13}1415export interface RepositoryProperties {16readonly repositoryPath: string;17readonly branchName?: string;18readonly baseBranchName?: string;19readonly baseCommit?: string;20readonly upstreamBranchName?: string;21readonly mergeBaseCommit?: string;22readonly hasGitHubRemote?: boolean;23readonly incomingChanges?: number;24readonly outgoingChanges?: number;25readonly uncommittedChanges?: number;26}2728/**29* Serializable subset of ChatRequestModeInstructions (excludes toolReferences).30*/31export interface StoredModeInstructions {32readonly uri?: string;33readonly name: string;34readonly content: string;35readonly metadata?: Record<string, boolean | string | number>;36readonly isBuiltin?: boolean;37}3839export interface RequestDetails {40/** VS Code request ID — always available, serves as primary key. */41readonly vscodeRequestId: string;42/** Copilot SDK request ID — may not be available until the request completes. */43copilotRequestId?: string;44/**45* Map of tool call id to VS Code edit id, used to correlate edits to the tool call that created them.46*/47toolIdEditMap: { [copilotToolId: string]: string };4849/**50* @deprecated This field is deprecated in favor of modeInstructions.51* Agent used for this request.52* */53agentId?: string;5455/** Mode instructions for this request (excluding toolReferences). */56modeInstructions?: StoredModeInstructions;5758/** Checkpoint reference for this request (primary workspace). */59checkpointRef?: string;6061/** Checkpoint references for additional workspaces, keyed by folder fsPath. */62additionalCheckpointRefs?: { [folderPath: string]: string };63}6465export interface ChatSessionMetadataFile {66repositoryProperties?: RepositoryProperties;67worktreeProperties?: ChatSessionWorktreeProperties;68workspaceFolder?: WorkspaceFolderEntry;69additionalWorkspaces?: {70worktreeProperties?: ChatSessionWorktreeProperties;71workspaceFolder?: WorkspaceFolderEntry;72}[];73/**74* Whether the session metadata has been written to the Copilot CLI session state directory.75*/76writtenToDisc?: boolean;77/** The first user message sent in the session, used as the session label. */78firstUserMessage?: string;79/** Custom title set by the user or generated for the session. */80customTitle?: string;81/** The creator of this session. */82origin?: 'vscode' | 'other';83/**84* The kind of session, which can be used to determine how the session was created and possibly how it should be displayed in the UI.85*/86kind?: 'forked' | 'sub-session';87/**88* The ID of the parent session, if this session was forked from another89* session or if the session is a child session created from the Agents app.90*/91parentSessionId?: string;92/** Milliseconds since epoch when this metadata was first written. */93created?: number;94/** Milliseconds since epoch of the last write. Used for top-N trim sort and cross-process merge. */95modified?: number;96}9798/**99* One line in `~/.copilot/vscode.session.worktree.jsonl`. Maps a session id100* to the path of its worktree so folder → session lookups work even when the101* session has been evicted from the bulk metadata cache.102*/103export interface WorktreeSessionEntry {104readonly id: string;105readonly path: string;106readonly created: number;107}108109export const IChatSessionMetadataStore = createServiceIdentifier<IChatSessionMetadataStore>('IChatSessionMetadataStore');110111export interface IChatSessionMetadataStore {112readonly _serviceBrand: undefined;113getMetadataFileUri(sessionId: string): vscode.Uri;114deleteSessionMetadata(sessionId: string): Promise<void>;115storeWorktreeInfo(sessionId: string, properties: ChatSessionWorktreeProperties): Promise<void>;116storeWorkspaceFolderInfo(sessionId: string, entry: WorkspaceFolderEntry): Promise<void>;117storeRepositoryProperties(sessionId: string, properties: RepositoryProperties): Promise<void>;118getRepositoryProperties(sessionId: string): Promise<RepositoryProperties | undefined>;119getWorktreeProperties(sessionId: string): Promise<ChatSessionWorktreeProperties | undefined>;120getSessionWorkspaceFolder(sessionId: string): Promise<vscode.Uri | undefined>;121getSessionWorkspaceFolderEntry(sessionId: string): Promise<WorkspaceFolderEntry | undefined>;122getAdditionalWorkspaces(sessionId: string): Promise<IWorkspaceInfo[]>;123setAdditionalWorkspaces(sessionId: string, workspaces: IWorkspaceInfo[]): Promise<void>;124getSessionFirstUserMessage(sessionId: string): Promise<string | undefined>;125setSessionFirstUserMessage(sessionId: string, message: string): Promise<void>;126getCustomTitle(sessionId: string): Promise<string | undefined>;127setCustomTitle(sessionId: string, title: string): Promise<void>;128getRequestDetails(sessionId: string): Promise<RequestDetails[]>;129updateRequestDetails(sessionId: string, details: (Partial<RequestDetails> & { vscodeRequestId: string })[]): Promise<void>;130getSessionAgent(sessionId: string): Promise<string | undefined>;131/**132* Copy all VS Code-specific metadata (workspace info, request details, etc.) from133* an existing session to a newly forked session, overriding the custom title.134*/135storeForkedSessionMetadata(sourceSessionId: string, targetSessionId: string, customTitle: string): Promise<void>;136setSessionOrigin(sessionId: string): Promise<void>;137getSessionOrigin(sessionId: string): Promise<'vscode' | 'other'>;138setSessionParentId(sessionId: string, parentSessionId: string): Promise<void>;139getSessionParentId(sessionId: string): Promise<string | undefined>;140/**141* Re-read the shared bulk metadata file from disk and merge into the in-memory cache.142* Wired to the chat-sessions UI refresh action so cross-process writes become visible143* on demand. Concurrent calls collapse: at most one in-flight + one pending.144*/145refresh(): Promise<void>;146/**147* Returns session IDs whose working directory (worktree path or workspace folder)148* matches the given folder URI.149*/150getSessionIdsForFolder(folder: vscode.Uri): string[];151/**152* Returns session IDs that have a worktree whose path matches the given folder URI.153*/154getWorktreeSessions(folder: vscode.Uri): string[];155}156157158