forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathgetPythonEnvTool.ts
More file actions
108 lines (101 loc) · 4.52 KB
/
getPythonEnvTool.ts
File metadata and controls
108 lines (101 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {
CancellationError,
CancellationToken,
l10n,
LanguageModelTextPart,
LanguageModelTool,
LanguageModelToolInvocationOptions,
LanguageModelToolInvocationPrepareOptions,
LanguageModelToolResult,
PreparedToolInvocation,
} from 'vscode';
import { PythonExtension } from '../api/types';
import { IServiceContainer } from '../ioc/types';
import { ICodeExecutionService } from '../terminals/types';
import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/terminalCodeExecution';
import { IProcessServiceFactory, IPythonExecutionFactory } from '../common/process/types';
import { getEnvironmentDetails, NoEnvironmentError, raceCancellationError } from './utils';
import { resolveFilePath } from './utils';
import { getPythonPackagesResponse } from './listPackagesTool';
import { ITerminalHelper } from '../common/terminal/types';
export interface IResourceReference {
resourcePath?: string;
}
export class GetEnvironmentInfoTool implements LanguageModelTool<IResourceReference> {
private readonly terminalExecutionService: TerminalCodeExecutionProvider;
private readonly pythonExecFactory: IPythonExecutionFactory;
private readonly processServiceFactory: IProcessServiceFactory;
private readonly terminalHelper: ITerminalHelper;
public static readonly toolName = 'get_python_environment_details';
constructor(
private readonly api: PythonExtension['environments'],
private readonly serviceContainer: IServiceContainer,
) {
this.terminalExecutionService = this.serviceContainer.get<TerminalCodeExecutionProvider>(
ICodeExecutionService,
'standard',
);
this.pythonExecFactory = this.serviceContainer.get<IPythonExecutionFactory>(IPythonExecutionFactory);
this.processServiceFactory = this.serviceContainer.get<IProcessServiceFactory>(IProcessServiceFactory);
this.terminalHelper = this.serviceContainer.get<ITerminalHelper>(ITerminalHelper);
}
/**
* Invokes the tool to get the information about the Python environment.
* @param options - The invocation options containing the file path.
* @param token - The cancellation token.
* @returns The result containing the information about the Python environment or an error message.
*/
async invoke(
options: LanguageModelToolInvocationOptions<IResourceReference>,
token: CancellationToken,
): Promise<LanguageModelToolResult> {
const resourcePath = resolveFilePath(options.input.resourcePath);
try {
// environment
const envPath = this.api.getActiveEnvironmentPath(resourcePath);
const environment = await raceCancellationError(this.api.resolveEnvironment(envPath), token);
if (!environment || !environment.version) {
throw new Error('No environment found for the provided resource path: ' + resourcePath?.fsPath);
}
const packages = await getPythonPackagesResponse(
environment,
this.pythonExecFactory,
this.processServiceFactory,
resourcePath,
token,
);
const message = await getEnvironmentDetails(
resourcePath,
this.api,
this.terminalExecutionService,
this.terminalHelper,
packages,
token,
);
return new LanguageModelToolResult([new LanguageModelTextPart(message)]);
} catch (error) {
if (error instanceof NoEnvironmentError) {
return new LanguageModelToolResult([
new LanguageModelTextPart(
'Failed to configure a Python Environment, as the environment could not be found.',
),
]);
}
if (error instanceof CancellationError) {
throw error;
}
const errorMessage: string = `An error occurred while fetching environment information: ${error}`;
return new LanguageModelToolResult([new LanguageModelTextPart(errorMessage)]);
}
}
async prepareInvocation?(
_options: LanguageModelToolInvocationPrepareOptions<IResourceReference>,
_token: CancellationToken,
): Promise<PreparedToolInvocation> {
return {
invocationMessage: l10n.t('Fetching Python environment information'),
};
}
}