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