Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/copilotcli/vscode-node/commands/sendContext.ts
13406 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 * as l10n from '@vscode/l10n';
7
import * as vscode from 'vscode';
8
import { ILogger } from '../../../../../platform/log/common/logService';
9
import { Schemas } from '../../../../../util/vs/base/common/network';
10
import { ICopilotCLISessionTracker } from '../copilotCLISessionTracker';
11
import { InProcHttpServer } from '../inProcHttpServer';
12
import { getSelectionInfo } from '../tools';
13
import { pickSession } from './pickSession';
14
15
export interface FileReferenceInfo {
16
filePath: string;
17
fileUrl: string;
18
selection: {
19
start: { line: number; character: number };
20
end: { line: number; character: number };
21
} | null;
22
selectedText: string | null;
23
}
24
25
export const ADD_FILE_REFERENCE_NOTIFICATION = 'add_file_reference';
26
27
/**
28
* URI schemes that represent real file-system files and can be sent to CLI sessions.
29
*/
30
const ALLOWED_SCHEMES = new Set([Schemas.file]);
31
32
/**
33
* Validates URI scheme and shows warning if not allowed.
34
* Returns true if allowed, false otherwise.
35
*/
36
function validateScheme(logger: ILogger, uri: vscode.Uri): boolean {
37
if (ALLOWED_SCHEMES.has(uri.scheme)) {
38
return true;
39
}
40
logger.debug(`Unsupported URI scheme: ${uri.scheme}`);
41
vscode.window.showWarningMessage(l10n.t('Cannot send virtual files to Copilot CLI.'));
42
return false;
43
}
44
45
/**
46
* Picks a session (if needed) and sends a file reference notification.
47
*/
48
export async function sendToSession(
49
logger: ILogger,
50
httpServer: InProcHttpServer,
51
sessionTracker: ICopilotCLISessionTracker,
52
fileReferenceInfo: FileReferenceInfo,
53
): Promise<void> {
54
const sessionId = await pickSession(logger, httpServer, sessionTracker);
55
if (!sessionId) {
56
return;
57
}
58
59
logger.info(`Sending context to session ${sessionId}: ${fileReferenceInfo.filePath}`);
60
httpServer.sendNotification(sessionId, ADD_FILE_REFERENCE_NOTIFICATION, fileReferenceInfo as unknown as Record<string, unknown>);
61
}
62
63
/**
64
* Sends a file reference (from explorer URI) to a CLI session.
65
*/
66
export async function sendUriToSession(
67
logger: ILogger,
68
httpServer: InProcHttpServer,
69
sessionTracker: ICopilotCLISessionTracker,
70
uri: vscode.Uri,
71
): Promise<void> {
72
if (!validateScheme(logger, uri)) {
73
return;
74
}
75
76
await sendToSession(logger, httpServer, sessionTracker, {
77
filePath: uri.fsPath,
78
fileUrl: uri.toString(),
79
selection: null,
80
selectedText: null,
81
});
82
}
83
84
/**
85
* Sends editor context (file + optional selection) to a CLI session.
86
*/
87
export async function sendEditorContextToSession(
88
logger: ILogger,
89
httpServer: InProcHttpServer,
90
sessionTracker: ICopilotCLISessionTracker,
91
): Promise<void> {
92
const editor = vscode.window.activeTextEditor;
93
if (!editor) {
94
logger.debug('No active editor');
95
vscode.window.showWarningMessage(l10n.t('No active editor. Open a file to add a reference.'));
96
return;
97
}
98
99
if (!validateScheme(logger, editor.document.uri)) {
100
return;
101
}
102
103
const selectionInfo = getSelectionInfo(editor);
104
105
await sendToSession(logger, httpServer, sessionTracker, {
106
filePath: selectionInfo.filePath,
107
fileUrl: selectionInfo.fileUrl,
108
selection: selectionInfo.selection.isEmpty
109
? null
110
: {
111
start: selectionInfo.selection.start,
112
end: selectionInfo.selection.end,
113
},
114
selectedText: selectionInfo.selection.isEmpty ? null : selectionInfo.text,
115
});
116
}
117
118