-
Notifications
You must be signed in to change notification settings - Fork 39.7k
Expand file tree
/
Copy pathcopilotCLIChatSessionInitializer.ts
More file actions
265 lines (237 loc) · 12.4 KB
/
copilotCLIChatSessionInitializer.ts
File metadata and controls
265 lines (237 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { SweCustomAgent } from '@github/copilot/sdk';
import * as l10n from '@vscode/l10n';
import * as vscode from 'vscode';
import { ConfigKey, IConfigurationService } from '../../../platform/configuration/common/configurationService';
import { ILogService } from '../../../platform/log/common/logService';
import { IPromptsService, ParsedPromptFile } from '../../../platform/promptFiles/common/promptsService';
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService';
import { createServiceIdentifier } from '../../../util/common/services';
import { DisposableStore, IReference } from '../../../util/vs/base/common/lifecycle';
import { URI } from '../../../util/vs/base/common/uri';
import { ChatVariablesCollection, extractDebugTargetSessionIds, isPromptFile } from '../../prompt/common/chatVariablesCollection';
import { FolderRepositoryInfo, IFolderRepositoryManager, IsolationMode } from '../common/folderRepositoryManager';
import { emptyWorkspaceInfo, getWorkingDirectory, isIsolationEnabled, IWorkspaceInfo } from '../common/workspaceInfo';
import { SessionIdForCLI } from '../copilotcli/common/utils';
import { COPILOT_CLI_REASONING_EFFORT_PROPERTY, ICopilotCLIAgents, ICopilotCLIModels } from '../copilotcli/node/copilotCli';
import { ICopilotCLISession } from '../copilotcli/node/copilotcliSession';
import { ICopilotCLISessionService } from '../copilotcli/node/copilotcliSessionService';
import { buildMcpServerMappings, McpServerMappings } from '../copilotcli/node/mcpHandler';
function isReasoningEffortFeatureEnabled(configurationService: IConfigurationService): boolean {
return configurationService.getConfig(ConfigKey.Advanced.CLIThinkingEffortEnabled);
}
export interface SessionInitOptions {
isolation?: IsolationMode;
branch?: string;
folder?: vscode.Uri;
newBranch?: Promise<string | undefined>;
stream: vscode.ChatResponseStream;
}
export interface ICopilotCLIChatSessionInitializer {
readonly _serviceBrand: undefined;
/**
* Get or create a session for a chat request with a chat session context.
* Handles working directory initialization, model/agent resolution,
* session creation, worktree properties, workspace folder tracking,
* stream attachment, permission level, and request metadata recording.
*/
getOrCreateSession(
request: vscode.ChatRequest,
chatResource: vscode.Uri,
options: SessionInitOptions,
disposables: DisposableStore,
token: vscode.CancellationToken
): Promise<{ session: IReference<ICopilotCLISession> | undefined; isNewSession: boolean; model: { model: string; reasoningEffort?: string } | undefined; agent: SweCustomAgent | undefined; trusted: boolean }>;
/**
* Initialize a working directory, optionally based on a chat session context.
* Used for both normal requests and delegation flows.
*/
initializeWorkingDirectory(
chatResource: vscode.Uri | undefined,
options: SessionInitOptions,
toolInvocationToken: vscode.ChatParticipantToolToken,
token: vscode.CancellationToken
): Promise<{ workspaceInfo: IWorkspaceInfo; cancelled: boolean; trusted: boolean }>;
/**
* Create a new session for delegation and handle post-creation bookkeeping
* including request metadata recording.
*/
createDelegatedSession(
request: vscode.ChatRequest,
workspace: IWorkspaceInfo,
options: { mcpServerMappings: McpServerMappings },
token: vscode.CancellationToken
): Promise<{ session: IReference<ICopilotCLISession>; model: { model: string; reasoningEffort?: string } | undefined; agent: SweCustomAgent | undefined }>;
}
export const ICopilotCLIChatSessionInitializer = createServiceIdentifier<ICopilotCLIChatSessionInitializer>('ICopilotCLIChatSessionInitializer');
export class CopilotCLIChatSessionInitializer implements ICopilotCLIChatSessionInitializer {
declare readonly _serviceBrand: undefined;
constructor(
@ICopilotCLISessionService private readonly sessionService: ICopilotCLISessionService,
@IFolderRepositoryManager private readonly folderRepositoryManager: IFolderRepositoryManager,
@IWorkspaceService private readonly workspaceService: IWorkspaceService,
@ICopilotCLIModels private readonly copilotCLIModels: ICopilotCLIModels,
@ICopilotCLIAgents private readonly copilotCLIAgents: ICopilotCLIAgents,
@IPromptsService private readonly promptsService: IPromptsService,
@ILogService private readonly logService: ILogService,
@IConfigurationService private readonly configurationService: IConfigurationService,
) { }
async getOrCreateSession(
request: vscode.ChatRequest,
chatResource: vscode.Uri,
options: SessionInitOptions,
disposables: DisposableStore,
token: vscode.CancellationToken
): Promise<{ session: IReference<ICopilotCLISession> | undefined; isNewSession: boolean; model: { model: string; reasoningEffort?: string } | undefined; agent: SweCustomAgent | undefined; trusted: boolean }> {
const sessionId = SessionIdForCLI.parse(chatResource);
const isNewSession = this.sessionService.isNewSessionId(sessionId);
const { stream } = options;
const [{ workspaceInfo, cancelled, trusted }, model, agent] = await Promise.all([
this.initializeWorkingDirectory(chatResource, options, request.toolInvocationToken, token),
this.resolveModel(request, token),
this.resolveAgent(request, token),
]);
const workingDirectory = getWorkingDirectory(workspaceInfo);
const worktreeProperties = workspaceInfo.worktreeProperties;
if (cancelled || token.isCancellationRequested) {
return { session: undefined, isNewSession, model, agent, trusted };
}
const debugTargetSessionIds = extractDebugTargetSessionIds(request.references);
const mcpServerMappings = buildMcpServerMappings(request.tools);
const session = isNewSession ?
await this.sessionService.createSession({ sessionId, model: model?.model, reasoningEffort: model?.reasoningEffort, workspace: workspaceInfo, agent, debugTargetSessionIds, mcpServerMappings }, token) :
await this.sessionService.getSession({ sessionId, model: model?.model, reasoningEffort: model?.reasoningEffort, workspace: workspaceInfo, agent, debugTargetSessionIds, mcpServerMappings }, token);
if (!session) {
stream.warning(l10n.t('Chat session not found.'));
return { session: undefined, isNewSession, model, agent, trusted };
}
this.logService.info(`Using Copilot CLI session: ${session.object.sessionId} (isNewSession: ${isNewSession}, isolationEnabled: ${isIsolationEnabled(workspaceInfo)}, workingDirectory: ${workingDirectory}, worktreePath: ${worktreeProperties?.worktreePath})`);
disposables.add(session);
disposables.add(session.object.attachStream(stream));
session.object.setPermissionLevel(request.permissionLevel);
return { session, isNewSession, model, agent, trusted };
}
async initializeWorkingDirectory(
chatResource: vscode.Uri | undefined,
options: SessionInitOptions,
toolInvocationToken: vscode.ChatParticipantToolToken,
token: vscode.CancellationToken
): Promise<{ workspaceInfo: IWorkspaceInfo; cancelled: boolean; trusted: boolean }> {
let folderInfo: FolderRepositoryInfo;
const { stream } = options;
let folder: undefined | vscode.Uri = options?.folder;
const workspaceFolders = this.workspaceService.getWorkspaceFolders();
if (workspaceFolders.length === 1 && !folder) {
folder = workspaceFolders[0];
}
if (chatResource) {
const sessionId = SessionIdForCLI.parse(chatResource);
const isNewSession = this.sessionService.isNewSessionId(sessionId);
if (isNewSession) {
const isolation = options?.isolation ?? IsolationMode.Workspace;
const branch = options?.branch;
// Use FolderRepositoryManager to initialize folder/repository with worktree creation
folderInfo = await this.folderRepositoryManager.initializeFolderRepository(sessionId, { stream, toolInvocationToken, branch, isolation, folder, newBranch: options?.newBranch }, token);
} else {
// Existing session - use getFolderRepository for resolution with trust check
folderInfo = await this.folderRepositoryManager.getFolderRepository(sessionId, { promptForTrust: true, stream }, token);
}
} else {
// No chat session context (e.g., delegation) - initialize with active repository
folderInfo = await this.folderRepositoryManager.initializeFolderRepository(undefined, { stream, toolInvocationToken, isolation: options?.isolation, folder }, token);
}
if (folderInfo.trusted === false || folderInfo.cancelled) {
return { workspaceInfo: emptyWorkspaceInfo(), cancelled: true, trusted: folderInfo.trusted !== false };
}
const workspaceInfo = Object.assign({}, folderInfo);
return { workspaceInfo, cancelled: false, trusted: true };
}
async createDelegatedSession(
request: vscode.ChatRequest,
workspace: IWorkspaceInfo,
options: { mcpServerMappings: McpServerMappings },
token: vscode.CancellationToken
): Promise<{ session: IReference<ICopilotCLISession>; model: { model: string; reasoningEffort?: string } | undefined; agent: SweCustomAgent | undefined }> {
const [model, agent] = await Promise.all([
this.resolveModel(request, token),
this.resolveAgent(request, token),
]);
const session = await this.sessionService.createSession({ workspace, agent, model: model?.model, reasoningEffort: model?.reasoningEffort, mcpServerMappings: options.mcpServerMappings }, token);
return { session, model, agent };
}
/**
* Resolve the model ID to use for a request.
*/
async resolveModel(request: vscode.ChatRequest | undefined, token: vscode.CancellationToken): Promise<{ model: string; reasoningEffort?: string } | undefined> {
const promptFile = request ? await this.getPromptInfoFromRequest(request, token) : undefined;
const model = promptFile?.header?.model ? await this.getModelFromPromptFile(promptFile.header.model) : undefined;
if (token.isCancellationRequested) {
return undefined;
}
if (model) {
return { model };
}
// Get model from request.
const preferredModelInRequest = request?.model?.id ? await this.copilotCLIModels.resolveModel(request.model.id) : undefined;
if (preferredModelInRequest) {
const reasoningEffort = isReasoningEffortFeatureEnabled(this.configurationService) ? request?.modelConfiguration?.[COPILOT_CLI_REASONING_EFFORT_PROPERTY] : undefined;
return {
model: preferredModelInRequest,
reasoningEffort: typeof reasoningEffort === 'string' && reasoningEffort ? reasoningEffort : undefined
};
}
const defaultModel = await this.copilotCLIModels.getDefaultModel();
if (!defaultModel) {
return undefined;
}
return { model: defaultModel };
}
/**
* Resolve the agent to use for a request.
*/
async resolveAgent(request: vscode.ChatRequest | undefined, token: vscode.CancellationToken): Promise<SweCustomAgent | undefined> {
if (request?.modeInstructions2) {
const customAgent = request.modeInstructions2.uri ? await this.copilotCLIAgents.resolveAgent(request.modeInstructions2.uri.toString()) : await this.copilotCLIAgents.resolveAgent(request.modeInstructions2.name);
if (customAgent) {
const tools = (request.modeInstructions2.toolReferences || []).map(t => t.name);
if (tools.length > 0) {
customAgent.tools = tools;
}
return customAgent;
}
}
return undefined;
}
private async getPromptInfoFromRequest(request: vscode.ChatRequest, token: vscode.CancellationToken): Promise<ParsedPromptFile | undefined> {
const promptFile = new ChatVariablesCollection(request.references).find(isPromptFile);
if (!promptFile || !URI.isUri(promptFile.reference.value)) {
return undefined;
}
try {
return await this.promptsService.parseFile(promptFile.reference.value, token);
} catch (ex) {
this.logService.error(`Failed to parse the prompt file: ${promptFile.reference.value.toString()}`, ex);
return undefined;
}
}
private async getModelFromPromptFile(models: readonly string[]): Promise<string | undefined> {
for (const model of models) {
let modelId = await this.copilotCLIModels.resolveModel(model);
if (modelId) {
return modelId;
}
// Sometimes the models can contain ` (Copilot)` suffix, try stripping that and resolving again.
if (!model.includes('(')) {
continue;
}
modelId = await this.copilotCLIModels.resolveModel(model.substring(0, model.indexOf('(')).trim());
if (modelId) {
return modelId;
}
}
return undefined;
}
}