forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetExecutableTool.ts
More file actions
90 lines (84 loc) · 3.37 KB
/
getExecutableTool.ts
File metadata and controls
90 lines (84 loc) · 3.37 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {
CancellationToken,
l10n,
LanguageModelTextPart,
LanguageModelTool,
LanguageModelToolInvocationOptions,
LanguageModelToolInvocationPrepareOptions,
LanguageModelToolResult,
PreparedToolInvocation,
Uri,
} from 'vscode';
import { PythonExtension } from '../api/types';
import { IServiceContainer } from '../ioc/types';
import { ICodeExecutionService } from '../terminals/types';
import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/terminalCodeExecution';
import {
getEnvDisplayName,
getEnvironmentDetails,
getEnvTypeForTelemetry,
getToolResponseIfNotebook,
IResourceReference,
raceCancellationError,
} from './utils';
import { ITerminalHelper } from '../common/terminal/types';
import { IDiscoveryAPI } from '../pythonEnvironments/base/locator';
import { BaseTool } from './baseTool';
export class GetExecutableTool extends BaseTool<IResourceReference> implements LanguageModelTool<IResourceReference> {
private readonly terminalExecutionService: TerminalCodeExecutionProvider;
private readonly terminalHelper: ITerminalHelper;
public static readonly toolName = 'get_python_executable_details';
constructor(
private readonly api: PythonExtension['environments'],
private readonly serviceContainer: IServiceContainer,
private readonly discovery: IDiscoveryAPI,
) {
super(GetExecutableTool.toolName);
this.terminalExecutionService = this.serviceContainer.get<TerminalCodeExecutionProvider>(
ICodeExecutionService,
'standard',
);
this.terminalHelper = this.serviceContainer.get<ITerminalHelper>(ITerminalHelper);
}
async invokeImpl(
_options: LanguageModelToolInvocationOptions<IResourceReference>,
resourcePath: Uri | undefined,
token: CancellationToken,
): Promise<LanguageModelToolResult> {
const notebookResponse = getToolResponseIfNotebook(resourcePath);
if (notebookResponse) {
return notebookResponse;
}
const envPath = this.api.getActiveEnvironmentPath(resourcePath);
const environment = await raceCancellationError(this.api.resolveEnvironment(envPath), token);
if (environment) {
this.extraTelemetryProperties.envType = getEnvTypeForTelemetry(environment);
}
const message = await getEnvironmentDetails(
resourcePath,
this.api,
this.terminalExecutionService,
this.terminalHelper,
undefined,
token,
);
return new LanguageModelToolResult([new LanguageModelTextPart(message)]);
}
async prepareInvocationImpl(
_options: LanguageModelToolInvocationPrepareOptions<IResourceReference>,
resourcePath: Uri | undefined,
token: CancellationToken,
): Promise<PreparedToolInvocation> {
if (getToolResponseIfNotebook(resourcePath)) {
return {};
}
const envName = await raceCancellationError(getEnvDisplayName(this.discovery, resourcePath, this.api), token);
return {
invocationMessage: envName
? l10n.t('Fetching Python executable information for {0}', envName)
: l10n.t('Fetching Python executable information'),
};
}
}