Path: blob/main/extensions/copilot/src/extension/agents/vscode-node/editModeAgentProvider.ts
13399 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 vscode from 'vscode';6import { AGENT_FILE_EXTENSION } from '../../../platform/customInstructions/common/promptTypes';7import { IVSCodeExtensionContext } from '../../../platform/extContext/common/extensionContext';8import { IFileSystemService } from '../../../platform/filesystem/common/fileSystemService';9import { ILogService } from '../../../platform/log/common/logService';10import { Disposable } from '../../../util/vs/base/common/lifecycle';11import { AgentConfig, buildAgentMarkdown } from './agentTypes';1213const BASE_EDIT_MODE_AGENT_CONFIG: AgentConfig = {14name: 'Edit',15description: 'Edit-only mode restricted to the currently active file and any files explicitly attached in the request context.',16argumentHint: 'Describe the edit to apply in the active or attached files',17target: 'vscode',18disableModelInvocation: true,19userInvocable: true,20tools: ['read', 'edit'],21agents: [],22handoffs: [23{24label: 'Continue with Agent Mode',25agent: 'agent',26prompt: 'You are now switching to Agent Mode, where you can read and edit any file in the codebase. Continue with the task without the previous restrictions of Edit Mode.',27send: true,28},29],30body: `You are a focused allowlist editing agent.3132## Rules33- Allowed files are strictly: (1) the currently active file and (2) files explicitly attached in the request context.34- Only read and edit files in that allowlist.35- Create a new file only when the user explicitly asks to create that file.36- Never create, delete, rename, or modify any file outside that allowlist.37- Never propose or use terminal commands.38- If a request requires touching files outside the allowlist, stop and explain that Edit Mode is restricted to the active file plus attached files.3940## Workflow411. Build the allowed-file set from context: active file + attached files.422. Confirm every requested edit target is in that allowed-file set before editing, unless it is an explicitly user-requested new file creation.433. Make the minimum required edits only within allowed files.444. Summarize exactly what changed and list touched files.455. If further changes are needed outside the allowlist, suggest switching to Agent Mode to complete the task without restrictions.`46};4748export class EditModeAgentProvider extends Disposable implements vscode.ChatCustomAgentProvider {49readonly label = vscode.l10n.t('Edit Mode Agent');5051private static readonly CACHE_DIR = 'edit-mode-agent';52private static readonly AGENT_FILENAME = `EditMode${AGENT_FILE_EXTENSION}`;5354constructor(55@IVSCodeExtensionContext private readonly _extensionContext: IVSCodeExtensionContext,56@IFileSystemService private readonly _fileSystemService: IFileSystemService,57@ILogService private readonly _logService: ILogService,58) {59super();60}6162async provideCustomAgents(63_context: unknown,64_token: vscode.CancellationToken65): Promise<vscode.ChatResource[]> {66const content = buildAgentMarkdown(BASE_EDIT_MODE_AGENT_CONFIG);67const fileUri = await this._writeCacheFile(content);68return [{ uri: fileUri }];69}7071private async _writeCacheFile(content: string): Promise<vscode.Uri> {72const cacheDir = vscode.Uri.joinPath(73this._extensionContext.globalStorageUri,74EditModeAgentProvider.CACHE_DIR75);7677try {78await this._fileSystemService.stat(cacheDir);79} catch {80await this._fileSystemService.createDirectory(cacheDir);81}8283const fileUri = vscode.Uri.joinPath(cacheDir, EditModeAgentProvider.AGENT_FILENAME);84await this._fileSystemService.writeFile(fileUri, new TextEncoder().encode(content));85this._logService.trace(`[EditModeAgentProvider] Wrote agent file: ${fileUri.toString()}`);86return fileUri;87}88}899091