|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { |
| 5 | + lm, |
| 6 | + CancellationError, |
| 7 | + CancellationToken, |
| 8 | + l10n, |
| 9 | + LanguageModelTextPart, |
| 10 | + LanguageModelTool, |
| 11 | + LanguageModelToolInvocationOptions, |
| 12 | + LanguageModelToolInvocationPrepareOptions, |
| 13 | + LanguageModelToolResult, |
| 14 | + PreparedToolInvocation, |
| 15 | +} from 'vscode'; |
| 16 | +import { PythonExtension } from '../api/types'; |
| 17 | +import { IServiceContainer } from '../ioc/types'; |
| 18 | +import { ICodeExecutionService } from '../terminals/types'; |
| 19 | +import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/terminalCodeExecution'; |
| 20 | +import { getEnvironmentDetails, raceCancellationError } from './utils'; |
| 21 | +import { resolveFilePath } from './utils'; |
| 22 | +import { IInterpreterQuickPick } from '../interpreter/configuration/types'; |
| 23 | +import { ITerminalHelper } from '../common/terminal/types'; |
| 24 | +import { StopWatch } from '../common/utils/stopWatch'; |
| 25 | +import { sleep } from '../common/utils/async'; |
| 26 | +import { CreateVenvTool } from './createVenvTool'; |
| 27 | + |
| 28 | +export interface IResourceReference { |
| 29 | + resourcePath?: string; |
| 30 | +} |
| 31 | + |
| 32 | +export class ConfigurePythonEnvTool implements LanguageModelTool<IResourceReference> { |
| 33 | + public static get EnvironmentConfigured() { |
| 34 | + return ConfigurePythonEnvTool._environmentConfigured; |
| 35 | + } |
| 36 | + private static _environmentConfigured = false; |
| 37 | + private readonly terminalExecutionService: TerminalCodeExecutionProvider; |
| 38 | + private readonly interpreterPicker: IInterpreterQuickPick; |
| 39 | + private readonly terminalHelper: ITerminalHelper; |
| 40 | + public static readonly toolName = 'configure_python_environment'; |
| 41 | + constructor( |
| 42 | + private readonly api: PythonExtension['environments'], |
| 43 | + private readonly serviceContainer: IServiceContainer, |
| 44 | + ) { |
| 45 | + this.terminalExecutionService = this.serviceContainer.get<TerminalCodeExecutionProvider>( |
| 46 | + ICodeExecutionService, |
| 47 | + 'standard', |
| 48 | + ); |
| 49 | + this.interpreterPicker = this.serviceContainer.get<IInterpreterQuickPick>(IInterpreterQuickPick); |
| 50 | + this.terminalHelper = this.serviceContainer.get<ITerminalHelper>(ITerminalHelper); |
| 51 | + } |
| 52 | + /** |
| 53 | + * Invokes the tool to get the information about the Python environment. |
| 54 | + * @param options - The invocation options containing the file path. |
| 55 | + * @param token - The cancellation token. |
| 56 | + * @returns The result containing the information about the Python environment or an error message. |
| 57 | + */ |
| 58 | + async invoke( |
| 59 | + options: LanguageModelToolInvocationOptions<IResourceReference>, |
| 60 | + token: CancellationToken, |
| 61 | + ): Promise<LanguageModelToolResult> { |
| 62 | + const resourcePath = resolveFilePath(options.input.resourcePath); |
| 63 | + |
| 64 | + try { |
| 65 | + if (!ConfigurePythonEnvTool.EnvironmentConfigured) { |
| 66 | + // Try to create one. |
| 67 | + const result = await lm.invokeTool(CreateVenvTool.toolName, { resourcePath: options.input.resourcePath } as any, token); |
| 68 | + console.log('CreateVenvTool result:', result); |
| 69 | + const interpreterPath = await this.interpreterPicker.getInterpreterViaQuickPick( |
| 70 | + resourcePath, |
| 71 | + undefined, |
| 72 | + { |
| 73 | + showCreateEnvironment: true, |
| 74 | + }, |
| 75 | + ); |
| 76 | + if (!interpreterPath) { |
| 77 | + return new LanguageModelToolResult([ |
| 78 | + new LanguageModelTextPart('No Python Environment configured.'), |
| 79 | + ]); |
| 80 | + } |
| 81 | + ConfigurePythonEnvTool._environmentConfigured = true; |
| 82 | + |
| 83 | + const stopWatch = new StopWatch(); |
| 84 | + while (stopWatch.elapsedTime < 5_000) { |
| 85 | + try { |
| 86 | + await this.api.getActiveEnvironmentPath(resourcePath); |
| 87 | + } catch { |
| 88 | + await sleep(500); |
| 89 | + continue; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + // environment |
| 94 | + const envPath = this.api.getActiveEnvironmentPath(resourcePath); |
| 95 | + const environment = await raceCancellationError(this.api.resolveEnvironment(envPath), token); |
| 96 | + if (!environment || !environment.version) { |
| 97 | + throw new Error('No environment found for the provided resource path: ' + resourcePath?.fsPath); |
| 98 | + } |
| 99 | + const message = await getEnvironmentDetails( |
| 100 | + resourcePath, |
| 101 | + this.api, |
| 102 | + this.terminalExecutionService, |
| 103 | + this.terminalHelper, |
| 104 | + undefined, |
| 105 | + token, |
| 106 | + ); |
| 107 | + return new LanguageModelToolResult([new LanguageModelTextPart(message)]); |
| 108 | + } catch (error) { |
| 109 | + if (error instanceof CancellationError) { |
| 110 | + throw error; |
| 111 | + } |
| 112 | + const errorMessage: string = `An error occurred while fetching environment information: ${error}`; |
| 113 | + return new LanguageModelToolResult([new LanguageModelTextPart(errorMessage)]); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + async prepareInvocation?( |
| 118 | + _options: LanguageModelToolInvocationPrepareOptions<IResourceReference>, |
| 119 | + _token: CancellationToken, |
| 120 | + ): Promise<PreparedToolInvocation> { |
| 121 | + if (ConfigurePythonEnvTool._environmentConfigured) { |
| 122 | + return {}; |
| 123 | + } |
| 124 | + return { |
| 125 | + confirmationMessages: { |
| 126 | + title: l10n.t('Configure your Python Environment?'), |
| 127 | + message: l10n.t( |
| 128 | + 'You can either select a Python environment or create a new Environment. \nThe latter being the recommended option.', |
| 129 | + ), |
| 130 | + }, |
| 131 | + invocationMessage: l10n.t('Configuring Python environment'), |
| 132 | + }; |
| 133 | + } |
| 134 | +} |
0 commit comments