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