|
| 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 | +} |
0 commit comments