Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/agents/vscode-node/exploreAgentProvider.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 * as vscode from 'vscode';
7
import { ConfigKey, IConfigurationService } from '../../../platform/configuration/common/configurationService';
8
import { AGENT_FILE_EXTENSION } from '../../../platform/customInstructions/common/promptTypes';
9
import { IVSCodeExtensionContext } from '../../../platform/extContext/common/extensionContext';
10
import { IFileSystemService } from '../../../platform/filesystem/common/fileSystemService';
11
import { ILogService } from '../../../platform/log/common/logService';
12
import { Disposable } from '../../../util/vs/base/common/lifecycle';
13
import { AgentConfig, buildAgentMarkdown, DEFAULT_READ_TOOLS } from './agentTypes';
14
15
/**
16
* Fallback model priority list for the Explore agent.
17
* Passed as a YAML array; the runtime picks the first available model.
18
*/
19
const EXPLORE_AGENT_FALLBACK_MODELS: readonly string[] = [
20
'Claude Haiku 4.5 (copilot)',
21
'Gemini 3 Flash (Preview) (copilot)',
22
'Auto (copilot)',
23
];
24
25
/**
26
* Base Explore agent configuration.
27
* The Explore agent is a read-only code research subagent that autonomously
28
* digs through codebases using multiple search strategies.
29
*/
30
const BASE_EXPLORE_AGENT_CONFIG: AgentConfig = {
31
name: 'Explore',
32
description: 'Fast read-only codebase exploration and Q&A subagent. Prefer over manually chaining multiple search and file-reading operations to avoid cluttering the main conversation. Safe to call in parallel. Specify thoroughness: quick, medium, or thorough.',
33
argumentHint: 'Describe WHAT you\'re looking for and desired thoroughness (quick/medium/thorough)',
34
target: 'vscode',
35
userInvocable: false,
36
agents: [],
37
tools: [
38
...DEFAULT_READ_TOOLS,
39
],
40
body: '' // Generated dynamically in buildCustomizedConfig
41
};
42
43
/**
44
* Provides the Explore agent dynamically with settings-based customization.
45
*
46
* The Explore agent is a read-only code research subagent designed to be
47
* invoked by other agents (e.g., Plan) for autonomous codebase exploration.
48
* It uses small/fast models by default and focuses on search-heavy workflows.
49
*/
50
export class ExploreAgentProvider extends Disposable implements vscode.ChatCustomAgentProvider {
51
readonly label = vscode.l10n.t('Explore Agent');
52
53
private static readonly CACHE_DIR = 'explore-agent';
54
private static readonly AGENT_FILENAME = `Explore${AGENT_FILE_EXTENSION}`;
55
56
private readonly _onDidChangeCustomAgents = this._register(new vscode.EventEmitter<void>());
57
readonly onDidChangeCustomAgents = this._onDidChangeCustomAgents.event;
58
59
constructor(
60
@IConfigurationService private readonly _configurationService: IConfigurationService,
61
@IVSCodeExtensionContext private readonly _extensionContext: IVSCodeExtensionContext,
62
@IFileSystemService private readonly _fileSystemService: IFileSystemService,
63
@ILogService private readonly _logService: ILogService,
64
) {
65
super();
66
67
this._register(this._configurationService.onDidChangeConfiguration(e => {
68
if (e.affectsConfiguration('chat.exploreAgent.defaultModel') ||
69
e.affectsConfiguration(ConfigKey.ExploreAgentModel.fullyQualifiedId)) {
70
this._onDidChangeCustomAgents.fire();
71
}
72
}));
73
}
74
75
async provideCustomAgents(
76
_context: unknown,
77
_token: vscode.CancellationToken
78
): Promise<vscode.ChatResource[]> {
79
const config = this._buildCustomizedConfig();
80
const content = buildAgentMarkdown(config);
81
const fileUri = await this._writeCacheFile(content);
82
return [{ uri: fileUri }];
83
}
84
85
private async _writeCacheFile(content: string): Promise<vscode.Uri> {
86
const cacheDir = vscode.Uri.joinPath(
87
this._extensionContext.globalStorageUri,
88
ExploreAgentProvider.CACHE_DIR
89
);
90
91
try {
92
await this._fileSystemService.stat(cacheDir);
93
} catch {
94
await this._fileSystemService.createDirectory(cacheDir);
95
}
96
97
const fileUri = vscode.Uri.joinPath(cacheDir, ExploreAgentProvider.AGENT_FILENAME);
98
await this._fileSystemService.writeFile(fileUri, new TextEncoder().encode(content));
99
this._logService.trace(`[ExploreAgentProvider] Wrote agent file: ${fileUri.toString()}`);
100
return fileUri;
101
}
102
103
static buildAgentBody(): string {
104
return `You are an exploration agent specialized in rapid codebase analysis and answering questions efficiently.
105
106
## Search Strategy
107
108
- Go **broad to narrow**:
109
1. Start with glob patterns or semantic codesearch to discover relevant areas
110
2. Narrow with text search (regex) or usages (LSP) for specific symbols or patterns
111
3. Read files only when you know the path or need full context
112
- Pay attention to provided agent instructions/rules/skills as they apply to areas of the codebase to better understand architecture and best practices.
113
- Use the github repo tool to search references in external dependencies.
114
115
## Speed Principles
116
117
Adapt search strategy based on the requested thoroughness level.
118
119
**Bias for speed** — return findings as quickly as possible:
120
- Parallelize independent tool calls (multiple greps, multiple reads)
121
- Stop searching once you have sufficient context
122
- Make targeted searches, not exhaustive sweeps
123
124
## Output
125
126
Report findings directly as a message. Include:
127
- Files with absolute links
128
- Specific functions, types, or patterns that can be reused
129
- Analogous existing features that serve as implementation templates
130
- Clear answers to what was asked, not comprehensive overviews
131
132
Remember: Your goal is searching efficiently through MAXIMUM PARALLELISM to report concise and clear answers.`;
133
}
134
135
private _buildCustomizedConfig(): AgentConfig {
136
// Model selection priority: core config > extension config > fallback list
137
// Empty string means "not set", so we explicitly check for truthy values
138
const coreDefaultModel = this._configurationService.getNonExtensionConfig<string>('chat.exploreAgent.defaultModel');
139
const extModel = this._configurationService.getConfig(ConfigKey.ExploreAgentModel);
140
const model: string | readonly string[] = coreDefaultModel || extModel || EXPLORE_AGENT_FALLBACK_MODELS;
141
142
return {
143
...BASE_EXPLORE_AGENT_CONFIG,
144
body: ExploreAgentProvider.buildAgentBody(),
145
model,
146
};
147
}
148
}
149
150