|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { |
| 5 | + CancellationError, |
| 6 | + CancellationToken, |
| 7 | + l10n, |
| 8 | + LanguageModelTextPart, |
| 9 | + LanguageModelTool, |
| 10 | + LanguageModelToolInvocationOptions, |
| 11 | + LanguageModelToolInvocationPrepareOptions, |
| 12 | + LanguageModelToolResult, |
| 13 | + PreparedToolInvocation, |
| 14 | + Uri, |
| 15 | +} from 'vscode'; |
| 16 | +import { PythonExtension, ResolvedEnvironment } from '../api/types'; |
| 17 | +import { IServiceContainer } from '../ioc/types'; |
| 18 | +import { ICodeExecutionService } from '../terminals/types'; |
| 19 | +import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/terminalCodeExecution'; |
| 20 | +import { getEnvDisplayName, isCondaEnv, raceCancellationError } from './utils'; |
| 21 | +import { resolveFilePath } from './utils'; |
| 22 | +import { traceError } from '../logging'; |
| 23 | +import { ITerminalHelper, TerminalShellType } from '../common/terminal/types'; |
| 24 | +import { IDiscoveryAPI } from '../pythonEnvironments/base/locator'; |
| 25 | +import { Conda } from '../pythonEnvironments/common/environmentManagers/conda'; |
| 26 | + |
| 27 | +export interface IResourceReference { |
| 28 | + resourcePath: string; |
| 29 | +} |
| 30 | + |
| 31 | +export class GetExecutableTool implements LanguageModelTool<IResourceReference> { |
| 32 | + private readonly terminalExecutionService: TerminalCodeExecutionProvider; |
| 33 | + private readonly terminalHelper: ITerminalHelper; |
| 34 | + public static readonly toolName = 'get_python_executable'; |
| 35 | + constructor( |
| 36 | + private readonly api: PythonExtension['environments'], |
| 37 | + private readonly serviceContainer: IServiceContainer, |
| 38 | + private readonly discovery: IDiscoveryAPI, |
| 39 | + ) { |
| 40 | + this.terminalExecutionService = this.serviceContainer.get<TerminalCodeExecutionProvider>( |
| 41 | + ICodeExecutionService, |
| 42 | + 'standard', |
| 43 | + ); |
| 44 | + this.terminalHelper = this.serviceContainer.get<ITerminalHelper>(ITerminalHelper); |
| 45 | + } |
| 46 | + async invoke( |
| 47 | + options: LanguageModelToolInvocationOptions<IResourceReference>, |
| 48 | + token: CancellationToken, |
| 49 | + ): Promise<LanguageModelToolResult> { |
| 50 | + const resourcePath = resolveFilePath(options.input.resourcePath); |
| 51 | + |
| 52 | + try { |
| 53 | + // environment |
| 54 | + const envPath = this.api.getActiveEnvironmentPath(resourcePath); |
| 55 | + const environment = await raceCancellationError(this.api.resolveEnvironment(envPath), token); |
| 56 | + if (!environment || !environment.version) { |
| 57 | + throw new Error('No environment found for the provided resource path: ' + resourcePath.fsPath); |
| 58 | + } |
| 59 | + const runCommand = await raceCancellationError(this.getTerminalCommand(environment, resourcePath), token); |
| 60 | + |
| 61 | + const message = [ |
| 62 | + `Following is the information about the Python environment:`, |
| 63 | + `1. Environment Type: ${environment.environment?.type || 'unknown'}`, |
| 64 | + `2. Version: ${environment.version.sysVersion || 'unknown'}`, |
| 65 | + '', |
| 66 | + `3. Command Prefix to run Python in a terminal is: \`${runCommand}\``, |
| 67 | + `Instead of running \`Python sample.py\` in the terminal, you will now run: \`${runCommand} sample.py\``, |
| 68 | + `Similarly instead of running \`Python -c "import sys;...."\` in the terminal, you will now run: \`${runCommand} -c "import sys;...."\``, |
| 69 | + ]; |
| 70 | + return new LanguageModelToolResult([new LanguageModelTextPart(message.join('\n'))]); |
| 71 | + } catch (error) { |
| 72 | + if (error instanceof CancellationError) { |
| 73 | + throw error; |
| 74 | + } |
| 75 | + traceError('Error while getting environment information', error); |
| 76 | + const errorMessage: string = `An error occurred while fetching environment information: ${error}`; |
| 77 | + return new LanguageModelToolResult([new LanguageModelTextPart(errorMessage)]); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private async getTerminalCommand(environment: ResolvedEnvironment, resource: Uri) { |
| 82 | + let cmd: { command: string; args: string[] }; |
| 83 | + if (isCondaEnv(environment)) { |
| 84 | + cmd = |
| 85 | + (await this.getCondaRunCommand(environment)) || |
| 86 | + (await this.terminalExecutionService.getExecutableInfo(resource)); |
| 87 | + } else { |
| 88 | + cmd = await this.terminalExecutionService.getExecutableInfo(resource); |
| 89 | + } |
| 90 | + return this.terminalHelper.buildCommandForTerminal(TerminalShellType.other, cmd.command, cmd.args); |
| 91 | + } |
| 92 | + |
| 93 | + private async getCondaRunCommand(environment: ResolvedEnvironment) { |
| 94 | + if (!environment.executable.uri) { |
| 95 | + return; |
| 96 | + } |
| 97 | + const conda = await Conda.getConda(); |
| 98 | + if (!conda) { |
| 99 | + return; |
| 100 | + } |
| 101 | + const condaEnv = await conda.getCondaEnvironment(environment.executable.uri?.fsPath); |
| 102 | + if (!condaEnv) { |
| 103 | + return; |
| 104 | + } |
| 105 | + const cmd = await conda.getRunPythonArgs(condaEnv, true, false); |
| 106 | + if (!cmd) { |
| 107 | + return; |
| 108 | + } |
| 109 | + return { command: cmd[0], args: cmd.slice(1) }; |
| 110 | + } |
| 111 | + |
| 112 | + async prepareInvocation?( |
| 113 | + options: LanguageModelToolInvocationPrepareOptions<IResourceReference>, |
| 114 | + token: CancellationToken, |
| 115 | + ): Promise<PreparedToolInvocation> { |
| 116 | + const resourcePath = resolveFilePath(options.input.resourcePath); |
| 117 | + const envName = await raceCancellationError(getEnvDisplayName(this.discovery, resourcePath, this.api), token); |
| 118 | + return { |
| 119 | + invocationMessage: envName |
| 120 | + ? l10n.t('Fetching Python executable information for {0}', envName) |
| 121 | + : l10n.t('Fetching Python executable information'), |
| 122 | + }; |
| 123 | + } |
| 124 | +} |
0 commit comments