Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/common/folderRepositoryManager.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 { IWorkspaceInfo } from './workspaceInfo';
9
10
/**
11
* The isolation mode for a chat session.
12
* - `worktree`: Creates an isolated git worktree for the session.
13
* - `workspace`: Works directly in the workspace directory without isolation.
14
* Do not change these values, they are stored in global storage.
15
*/
16
export enum IsolationMode {
17
Workspace = 'workspace',
18
Worktree = 'worktree',
19
}
20
21
22
/**
23
* Options for initializing a folder/repository for a session.
24
*/
25
export interface InitializeFolderRepositoryOptions {
26
readonly branch?: string;
27
readonly folder: vscode.Uri | undefined;
28
readonly isolation?: IsolationMode;
29
readonly stream: vscode.ChatResponseStream;
30
readonly toolInvocationToken: vscode.ChatParticipantToolToken;
31
readonly newBranch?: Promise<string | undefined>;
32
}
33
34
/**
35
* Result of folder/repository resolution for a chat session.
36
*/
37
export interface FolderRepositoryInfo extends IWorkspaceInfo {
38
/**
39
* Trust status of the folder/repository.
40
* - `true`: The folder/repository is trusted
41
* - `false`: Trust was requested but denied by user
42
* - `undefined`: Trust was not requested (options.promptForTrust was not set)
43
*/
44
readonly trusted: boolean | undefined;
45
46
/**
47
* Whether the user cancelled the operation (e.g., cancelled uncommitted changes prompt).
48
*/
49
readonly cancelled?: boolean;
50
}
51
52
/**
53
* Options for getting folder/repository information.
54
*/
55
export interface GetFolderRepositoryOptions {
56
/**
57
* If true, prompts the user for trust if the folder is not already trusted.
58
*/
59
readonly promptForTrust: true;
60
61
readonly stream: vscode.ChatResponseStream;
62
}
63
64
/**
65
* MRU (Most Recently Used) folder/repository entry.
66
*/
67
export interface FolderRepositoryMRUEntry {
68
/**
69
* The folder URI.
70
*/
71
readonly folder: vscode.Uri;
72
73
/**
74
* The repository URI if this is a git repository, undefined for plain folders.
75
*/
76
readonly repository: vscode.Uri | undefined;
77
78
/**
79
* Timestamp of last access (milliseconds since epoch).
80
*/
81
readonly lastAccessed: number;
82
}
83
84
export const IFolderRepositoryManager = createServiceIdentifier<IFolderRepositoryManager>('IFolderRepositoryManager');
85
86
export interface IFolderRepositoryManager {
87
readonly _serviceBrand: undefined;
88
89
/**
90
* @deprecated
91
*/
92
setNewSessionFolder(sessionId: string, folderUri: vscode.Uri): void;
93
94
/**
95
* Delete the tracked folder for an untitled session.
96
*/
97
deleteNewSessionFolder(sessionId: string): void;
98
99
/**
100
* Get folder/repository/worktree/trust information for a session.
101
*
102
* This method resolves folder information using the following priority:
103
* 1. Worktree properties (if session has a worktree)
104
* 2. Session workspace folder (if tracked)
105
* 3. CLI session working directory (from session metadata)
106
*
107
* Trust checking is performed on the repository path (if git repo) or folder path
108
* (if plain folder). Worktree paths are NOT used for trust checking as they inherit
109
* trust from their parent repository.
110
*/
111
getFolderRepository(
112
sessionId: string,
113
options: GetFolderRepositoryOptions | undefined,
114
token: vscode.CancellationToken
115
): Promise<FolderRepositoryInfo>;
116
117
/**
118
* Initialize folder/repository for a session, creating a worktree if applicable.
119
*
120
* This method should be called when starting a request for an untitled session.
121
* It will:
122
* 1. Get the selected folder from memory or workspace folder service
123
* 2. Check if the folder contains a git repository
124
* 3. Verify trust on the repository/folder
125
* 4. Check for uncommitted changes and prompt the user via the provided callback
126
* 5. Create a worktree if a git repo is found
127
* 6. Migrate uncommitted changes to worktree if requested
128
*/
129
initializeFolderRepository(
130
sessionId: string | undefined,
131
options: InitializeFolderRepositoryOptions,
132
token: vscode.CancellationToken
133
): Promise<FolderRepositoryInfo>;
134
135
/**
136
* Initialize all folders for a multi-root session as a batch.
137
*
138
* Unlike calling `initializeFolderRepository` per folder, this method:
139
* 1. Resolves all folder/repo info in one pass
140
* 2. Verifies trust for all folders together
141
* 3. Collects uncommitted changes across ALL git repos
142
* 4. Shows ONE combined prompt listing all repos with uncommitted changes
143
* 5. Applies the same action (move/copy/skip/cancel) to all repos
144
* 6. Creates worktrees for all git repos in parallel
145
* 7. Migrates changes to all worktrees with the same action
146
*/
147
initializeMultiRootFolderRepositories(
148
sessionId: string,
149
primaryFolder: vscode.Uri,
150
additionalFolders: vscode.Uri[],
151
options: InitializeFolderRepositoryOptions,
152
token: vscode.CancellationToken
153
): Promise<{ primary: FolderRepositoryInfo; additional: FolderRepositoryInfo[] }>;
154
155
/**
156
* Get repository information for a folder.
157
*
158
* Resolves whether the folder contains a git repository and returns
159
* the repository URI and HEAD branch name.
160
*
161
* @param folder The folder URI to check
162
* @param token Cancellation token
163
* @returns Repository URI and HEAD branch name
164
*/
165
getRepositoryInfo(
166
folder: vscode.Uri,
167
token: vscode.CancellationToken
168
): Promise<{ repository: vscode.Uri | undefined; headBranchName: string | undefined }>;
169
170
/**
171
* @deprecated
172
* Get list of most recently used folders and repositories.
173
*
174
* This is used for empty workspaces to show a list of previously used
175
* folders/repos in the folder selection dropdown.
176
*
177
* @returns Array of MRU entries sorted by last accessed time (newest first),
178
* limited to 10 items, with non-existent paths filtered out
179
*/
180
getFolderMRU(): Promise<FolderRepositoryMRUEntry[]>;
181
}
182
183
export interface IChatFolderMruService {
184
readonly _serviceBrand: undefined;
185
getRecentlyUsedFolders(token: vscode.CancellationToken): Promise<FolderRepositoryMRUEntry[]>;
186
deleteRecentlyUsedFolder(folder: vscode.Uri): Promise<void>;
187
}
188
export const IChatFolderMruService = createServiceIdentifier<IChatFolderMruService>('IChatFolderMruService');
189
190