Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register terminal variables from vscode #239653

Merged
merged 3 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const _allApiProposals = {
},
chatParticipantPrivate: {
proposal: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.chatParticipantPrivate.d.ts',
version: 2
version: 3
},
chatProvider: {
proposal: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.chatProvider.d.ts',
Expand Down
2 changes: 2 additions & 0 deletions src/vs/workbench/contrib/chat/browser/chat.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import { IContextKeyService } from '../../../../platform/contextkey/common/conte
import { ChatContextKeys } from '../common/chatContextKeys.js';
import { IPromptSyntaxService } from '../common/promptSyntax/service/types.js';
import { PromptSyntaxService } from '../common/promptSyntax/service/promptSyntaxService.js';
import { BuiltinVariablesContribution } from './variables/variables.js';

// Register configuration
const configurationRegistry = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration);
Expand Down Expand Up @@ -391,6 +392,7 @@ registerWorkbenchContribution2(ChatGettingStartedContribution.ID, ChatGettingSta
registerWorkbenchContribution2(ChatSetupContribution.ID, ChatSetupContribution, WorkbenchPhase.BlockRestore);
registerWorkbenchContribution2(ChatQuotasStatusBarEntry.ID, ChatQuotasStatusBarEntry, WorkbenchPhase.Eventually);
registerWorkbenchContribution2(BuiltinToolsContribution.ID, BuiltinToolsContribution, WorkbenchPhase.Eventually);
registerWorkbenchContribution2(BuiltinVariablesContribution.ID, BuiltinVariablesContribution, WorkbenchPhase.Eventually);
registerWorkbenchContribution2(ChatAgentSettingContribution.ID, ChatAgentSettingContribution, WorkbenchPhase.BlockRestore);

registerChatActions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { IOutlineModelService } from '../../../../../editor/contrib/documentSymb
import { localize } from '../../../../../nls.js';
import { Action2, registerAction2 } from '../../../../../platform/actions/common/actions.js';
import { CommandsRegistry } from '../../../../../platform/commands/common/commands.js';
import { IConfigurationService } from '../../../../../platform/configuration/common/configuration.js';
import { IInstantiationService, ServicesAccessor } from '../../../../../platform/instantiation/common/instantiation.js';
import { ILabelService } from '../../../../../platform/label/common/label.js';
import { Registry } from '../../../../../platform/registry/common/platform.js';
Expand Down Expand Up @@ -835,7 +834,6 @@ class VariableCompletions extends Disposable {
@ILanguageFeaturesService private readonly languageFeaturesService: ILanguageFeaturesService,
@IChatWidgetService private readonly chatWidgetService: IChatWidgetService,
@IChatVariablesService private readonly chatVariablesService: IChatVariablesService,
@IConfigurationService configService: IConfigurationService,
@ILanguageModelToolsService toolsService: ILanguageModelToolsService
) {
super();
Expand Down
90 changes: 90 additions & 0 deletions src/vs/workbench/contrib/chat/browser/variables/variables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { CancellationToken } from '../../../../../base/common/cancellation.js';
import { Codicon } from '../../../../../base/common/codicons.js';
import { Disposable } from '../../../../../base/common/lifecycle.js';
import { ProviderResult } from '../../../../../editor/common/languages.js';
import { localize } from '../../../../../nls.js';
import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';
import { TerminalCapability } from '../../../../../platform/terminal/common/capabilities/capabilities.js';
import { IWorkbenchContribution } from '../../../../common/contributions.js';
import { ITerminalService } from '../../../terminal/browser/terminal.js';
import { IChatModel } from '../../common/chatModel.js';
import { IChatRequestVariableValue, IChatVariableResolverProgress, IChatVariablesService } from '../../common/chatVariables.js';

export class BuiltinVariablesContribution extends Disposable implements IWorkbenchContribution {

static readonly ID = 'chat.builtinVariables';

constructor(
@IChatVariablesService varsService: IChatVariablesService,
@IInstantiationService instantiationService: IInstantiationService,
) {
super();

const terminalSelectionVar = instantiationService.createInstance(TerminalSelectionVariable);
varsService.registerVariable({
id: 'copilot.terminalSelection',
name: 'terminalSelection',
fullName: localize('termSelection', "Terminal Selection"),
description: localize('termSelectionDescription', "The active terminal's selection"),
icon: Codicon.terminal,
}, async (messageText: string, arg: string | undefined, model: IChatModel, progress: (part: IChatVariableResolverProgress) => void, token: CancellationToken): Promise<IChatRequestVariableValue | undefined> => {
return await terminalSelectionVar.resolve(token);
});

const terminalLastCommandVar = instantiationService.createInstance(TerminalLastCommandVariable);
varsService.registerVariable({
id: 'copilot.terminalLastCommand',
name: 'terminalLastCommand',
fullName: localize('terminalLastCommand', "Terminal Last Command"),
description: localize('termLastCommandDesc', "The active terminal's last run command"),
icon: Codicon.terminal,
}, async (messageText: string, arg: string | undefined, model: IChatModel, progress: (part: IChatVariableResolverProgress) => void, token: CancellationToken): Promise<IChatRequestVariableValue | undefined> => {
return await terminalLastCommandVar.resolve(token);
});
}
}

class TerminalSelectionVariable {
constructor(
@ITerminalService private readonly terminalService: ITerminalService
) { }

resolve(token: CancellationToken): ProviderResult<IChatRequestVariableValue> {
const terminalSelection = this.terminalService.activeInstance?.selection;
return terminalSelection ? `The active terminal's selection:\n${terminalSelection}` : undefined;
}
}

class TerminalLastCommandVariable {
constructor(
@ITerminalService private readonly terminalService: ITerminalService
) { }

resolve(token: CancellationToken): ProviderResult<IChatRequestVariableValue> {
const lastCommand = this.terminalService.activeInstance?.capabilities.get(TerminalCapability.CommandDetection)?.commands.at(-1);
if (!lastCommand) {
return;
}

const userPrompt: string[] = [];
userPrompt.push(`The following is the last command run in the terminal:`);
userPrompt.push(lastCommand.command);
if (lastCommand.cwd) {
userPrompt.push(`It was run in the directory:`);
userPrompt.push(lastCommand.cwd);
}
const output = lastCommand.getOutput();
if (output) {
userPrompt.push(`It has the following output:`);
userPrompt.push(output);
}

const prompt = userPrompt.join('\n');
return `The active terminal's last run command:\n${prompt}`;
}
}
2 changes: 1 addition & 1 deletion src/vscode-dts/vscode.proposed.chatParticipantPrivate.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

// version: 2
// version: 3

declare module 'vscode' {

Expand Down
Loading