Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/agents/vscode-node/askAgentProvider.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
* Base Ask agent configuration.
17
* The Ask agent is read-only: it answers questions, explains code, and
18
* provides information without modifying the workspace.
19
*/
20
const BASE_ASK_AGENT_CONFIG: AgentConfig = {
21
name: 'Ask',
22
description: 'Answers questions without making changes',
23
argumentHint: 'Ask a question about your code or project',
24
target: 'vscode',
25
disableModelInvocation: true,
26
agents: [],
27
tools: [
28
...DEFAULT_READ_TOOLS,
29
'vscode.mermaid-chat-features/renderMermaidDiagram',
30
],
31
body: '' // Generated dynamically in buildCustomizedConfig
32
};
33
34
/**
35
* Provides the Ask agent dynamically with settings-based customization.
36
*
37
* The Ask agent is a read-only conversational mode for answering questions,
38
* explaining code, and researching topics without making any edits to the
39
* workspace. It uses an embedded configuration and generates .agent.md content
40
* with settings-based customization (additional tools and model override).
41
*/
42
export class AskAgentProvider extends Disposable implements vscode.ChatCustomAgentProvider {
43
readonly label = vscode.l10n.t('Ask Agent');
44
45
private static readonly CACHE_DIR = 'ask-agent';
46
private static readonly AGENT_FILENAME = `Ask${AGENT_FILE_EXTENSION}`;
47
48
private readonly _onDidChangeCustomAgents = this._register(new vscode.EventEmitter<void>());
49
readonly onDidChangeCustomAgents = this._onDidChangeCustomAgents.event;
50
51
constructor(
52
@IConfigurationService private readonly _configurationService: IConfigurationService,
53
@IVSCodeExtensionContext private readonly _extensionContext: IVSCodeExtensionContext,
54
@IFileSystemService private readonly _fileSystemService: IFileSystemService,
55
@ILogService private readonly _logService: ILogService,
56
) {
57
super();
58
59
this._register(this._configurationService.onDidChangeConfiguration(e => {
60
if (e.affectsConfiguration(ConfigKey.AskAgentAdditionalTools.fullyQualifiedId) ||
61
e.affectsConfiguration(ConfigKey.AskAgentModel.fullyQualifiedId)) {
62
this._onDidChangeCustomAgents.fire();
63
}
64
}));
65
}
66
67
async provideCustomAgents(
68
_context: unknown,
69
_token: vscode.CancellationToken
70
): Promise<vscode.ChatResource[]> {
71
const config = this._buildCustomizedConfig();
72
const content = buildAgentMarkdown(config);
73
const fileUri = await this._writeCacheFile(content);
74
return [{ uri: fileUri }];
75
}
76
77
private async _writeCacheFile(content: string): Promise<vscode.Uri> {
78
const cacheDir = vscode.Uri.joinPath(
79
this._extensionContext.globalStorageUri,
80
AskAgentProvider.CACHE_DIR
81
);
82
83
try {
84
await this._fileSystemService.stat(cacheDir);
85
} catch {
86
await this._fileSystemService.createDirectory(cacheDir);
87
}
88
89
const fileUri = vscode.Uri.joinPath(cacheDir, AskAgentProvider.AGENT_FILENAME);
90
await this._fileSystemService.writeFile(fileUri, new TextEncoder().encode(content));
91
this._logService.trace(`[AskAgentProvider] Wrote agent file: ${fileUri.toString()}`);
92
return fileUri;
93
}
94
95
static buildAgentBody(): string {
96
return `You are an ASK AGENT — a knowledgeable assistant that answers questions, explains code, and provides information.
97
98
Your job: understand the user's question → research the codebase as needed → provide a clear, thorough answer. You are strictly read-only: NEVER modify files or run commands that change state.
99
100
<rules>
101
- NEVER use file editing tools, terminal commands that modify state, or any write operations
102
- Focus on answering questions, explaining concepts, and providing information
103
- Use search and read tools to gather context from the codebase when needed
104
- Provide code examples in your responses when helpful, but do NOT apply them
105
- Use #tool:vscode/askQuestions to clarify ambiguous questions before researching
106
- When the user's question is about code, reference specific files and symbols
107
- If a question would require making changes, explain what changes would be needed but do NOT make them
108
</rules>
109
110
<capabilities>
111
You can help with:
112
- **Code explanation**: How does this code work? What does this function do?
113
- **Architecture questions**: How is the project structured? How do components interact?
114
- **Debugging guidance**: Why might this error occur? What could cause this behavior?
115
- **Best practices**: What's the recommended approach for X? How should I structure Y?
116
- **API and library questions**: How do I use this API? What does this method expect?
117
- **Codebase navigation**: Where is X defined? Where is Y used?
118
- **General programming**: Language features, algorithms, design patterns, etc.
119
</capabilities>
120
121
<workflow>
122
1. **Understand** the question — identify what the user needs to know
123
2. **Research** the codebase if needed — use search and read tools to find relevant code
124
3. **Clarify** if the question is ambiguous — use #tool:vscode/askQuestions
125
4. **Answer** clearly — provide a well-structured response with references to relevant code
126
</workflow>`;
127
}
128
129
private _buildCustomizedConfig(): AgentConfig {
130
const additionalTools = this._configurationService.getConfig(ConfigKey.AskAgentAdditionalTools);
131
const modelOverride = this._configurationService.getConfig(ConfigKey.AskAgentModel);
132
133
// Collect tools to add
134
const toolsToAdd: string[] = [...additionalTools];
135
136
// Always include askQuestions tool (now provided by core)
137
toolsToAdd.push('vscode/askQuestions');
138
139
// Merge additional tools (deduplicated)
140
const tools = toolsToAdd.length > 0
141
? [...new Set([...BASE_ASK_AGENT_CONFIG.tools, ...toolsToAdd])]
142
: [...BASE_ASK_AGENT_CONFIG.tools];
143
144
return {
145
...BASE_ASK_AGENT_CONFIG,
146
tools,
147
body: AskAgentProvider.buildAgentBody(),
148
...(modelOverride ? { model: modelOverride } : {}),
149
};
150
}
151
}
152
153