Skip to content

Commit cd52101

Browse files
authored
Register terminal variables from vscode (#239653)
* Register terminal variables from vscode * Simplify terminalLastCommand * Bump chatParticipantPrivate version just to ensure that moving variables from the extension to core doesn't lead to dupes or missing vars
1 parent a26f7bb commit cd52101

File tree

5 files changed

+94
-4
lines changed

5 files changed

+94
-4
lines changed

Diff for: src/vs/platform/extensions/common/extensionsApiProposals.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const _allApiProposals = {
3333
},
3434
chatParticipantPrivate: {
3535
proposal: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.chatParticipantPrivate.d.ts',
36-
version: 2
36+
version: 3
3737
},
3838
chatProvider: {
3939
proposal: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.chatProvider.d.ts',

Diff for: src/vs/workbench/contrib/chat/browser/chat.contribution.ts

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import { IContextKeyService } from '../../../../platform/contextkey/common/conte
9191
import { ChatContextKeys } from '../common/chatContextKeys.js';
9292
import { IPromptSyntaxService } from '../common/promptSyntax/service/types.js';
9393
import { PromptSyntaxService } from '../common/promptSyntax/service/promptSyntaxService.js';
94+
import { BuiltinVariablesContribution } from './variables/variables.js';
9495

9596
// Register configuration
9697
const configurationRegistry = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration);
@@ -392,6 +393,7 @@ registerWorkbenchContribution2(ChatGettingStartedContribution.ID, ChatGettingSta
392393
registerWorkbenchContribution2(ChatSetupContribution.ID, ChatSetupContribution, WorkbenchPhase.BlockRestore);
393394
registerWorkbenchContribution2(ChatQuotasStatusBarEntry.ID, ChatQuotasStatusBarEntry, WorkbenchPhase.Eventually);
394395
registerWorkbenchContribution2(BuiltinToolsContribution.ID, BuiltinToolsContribution, WorkbenchPhase.Eventually);
396+
registerWorkbenchContribution2(BuiltinVariablesContribution.ID, BuiltinVariablesContribution, WorkbenchPhase.Eventually);
395397
registerWorkbenchContribution2(ChatAgentSettingContribution.ID, ChatAgentSettingContribution, WorkbenchPhase.BlockRestore);
396398

397399
registerChatActions();

Diff for: src/vs/workbench/contrib/chat/browser/contrib/chatInputCompletions.ts

-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { IOutlineModelService } from '../../../../../editor/contrib/documentSymb
2222
import { localize } from '../../../../../nls.js';
2323
import { Action2, registerAction2 } from '../../../../../platform/actions/common/actions.js';
2424
import { CommandsRegistry } from '../../../../../platform/commands/common/commands.js';
25-
import { IConfigurationService } from '../../../../../platform/configuration/common/configuration.js';
2625
import { IInstantiationService, ServicesAccessor } from '../../../../../platform/instantiation/common/instantiation.js';
2726
import { ILabelService } from '../../../../../platform/label/common/label.js';
2827
import { Registry } from '../../../../../platform/registry/common/platform.js';
@@ -835,7 +834,6 @@ class VariableCompletions extends Disposable {
835834
@ILanguageFeaturesService private readonly languageFeaturesService: ILanguageFeaturesService,
836835
@IChatWidgetService private readonly chatWidgetService: IChatWidgetService,
837836
@IChatVariablesService private readonly chatVariablesService: IChatVariablesService,
838-
@IConfigurationService configService: IConfigurationService,
839837
@ILanguageModelToolsService toolsService: ILanguageModelToolsService
840838
) {
841839
super();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 { CancellationToken } from '../../../../../base/common/cancellation.js';
7+
import { Codicon } from '../../../../../base/common/codicons.js';
8+
import { Disposable } from '../../../../../base/common/lifecycle.js';
9+
import { ProviderResult } from '../../../../../editor/common/languages.js';
10+
import { localize } from '../../../../../nls.js';
11+
import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';
12+
import { TerminalCapability } from '../../../../../platform/terminal/common/capabilities/capabilities.js';
13+
import { IWorkbenchContribution } from '../../../../common/contributions.js';
14+
import { ITerminalService } from '../../../terminal/browser/terminal.js';
15+
import { IChatModel } from '../../common/chatModel.js';
16+
import { IChatRequestVariableValue, IChatVariableResolverProgress, IChatVariablesService } from '../../common/chatVariables.js';
17+
18+
export class BuiltinVariablesContribution extends Disposable implements IWorkbenchContribution {
19+
20+
static readonly ID = 'chat.builtinVariables';
21+
22+
constructor(
23+
@IChatVariablesService varsService: IChatVariablesService,
24+
@IInstantiationService instantiationService: IInstantiationService,
25+
) {
26+
super();
27+
28+
const terminalSelectionVar = instantiationService.createInstance(TerminalSelectionVariable);
29+
varsService.registerVariable({
30+
id: 'copilot.terminalSelection',
31+
name: 'terminalSelection',
32+
fullName: localize('termSelection', "Terminal Selection"),
33+
description: localize('termSelectionDescription', "The active terminal's selection"),
34+
icon: Codicon.terminal,
35+
}, async (messageText: string, arg: string | undefined, model: IChatModel, progress: (part: IChatVariableResolverProgress) => void, token: CancellationToken): Promise<IChatRequestVariableValue | undefined> => {
36+
return await terminalSelectionVar.resolve(token);
37+
});
38+
39+
const terminalLastCommandVar = instantiationService.createInstance(TerminalLastCommandVariable);
40+
varsService.registerVariable({
41+
id: 'copilot.terminalLastCommand',
42+
name: 'terminalLastCommand',
43+
fullName: localize('terminalLastCommand', "Terminal Last Command"),
44+
description: localize('termLastCommandDesc', "The active terminal's last run command"),
45+
icon: Codicon.terminal,
46+
}, async (messageText: string, arg: string | undefined, model: IChatModel, progress: (part: IChatVariableResolverProgress) => void, token: CancellationToken): Promise<IChatRequestVariableValue | undefined> => {
47+
return await terminalLastCommandVar.resolve(token);
48+
});
49+
}
50+
}
51+
52+
class TerminalSelectionVariable {
53+
constructor(
54+
@ITerminalService private readonly terminalService: ITerminalService
55+
) { }
56+
57+
resolve(token: CancellationToken): ProviderResult<IChatRequestVariableValue> {
58+
const terminalSelection = this.terminalService.activeInstance?.selection;
59+
return terminalSelection ? `The active terminal's selection:\n${terminalSelection}` : undefined;
60+
}
61+
}
62+
63+
class TerminalLastCommandVariable {
64+
constructor(
65+
@ITerminalService private readonly terminalService: ITerminalService
66+
) { }
67+
68+
resolve(token: CancellationToken): ProviderResult<IChatRequestVariableValue> {
69+
const lastCommand = this.terminalService.activeInstance?.capabilities.get(TerminalCapability.CommandDetection)?.commands.at(-1);
70+
if (!lastCommand) {
71+
return;
72+
}
73+
74+
const userPrompt: string[] = [];
75+
userPrompt.push(`The following is the last command run in the terminal:`);
76+
userPrompt.push(lastCommand.command);
77+
if (lastCommand.cwd) {
78+
userPrompt.push(`It was run in the directory:`);
79+
userPrompt.push(lastCommand.cwd);
80+
}
81+
const output = lastCommand.getOutput();
82+
if (output) {
83+
userPrompt.push(`It has the following output:`);
84+
userPrompt.push(output);
85+
}
86+
87+
const prompt = userPrompt.join('\n');
88+
return `The active terminal's last run command:\n${prompt}`;
89+
}
90+
}

Diff for: src/vscode-dts/vscode.proposed.chatParticipantPrivate.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
// version: 2
6+
// version: 3
77

88
declare module 'vscode' {
99

0 commit comments

Comments
 (0)