forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathgetExecutableTool.ts
More file actions
82 lines (77 loc) · 3.29 KB
/
getExecutableTool.ts
File metadata and controls
82 lines (77 loc) · 3.29 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
// 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 { getEnvDisplayName, getEnvironmentDetails, raceCancellationError } from './utils';
import { resolveFilePath } from './utils';
import { traceError } from '../logging';
import { ITerminalHelper } from '../common/terminal/types';
import { IDiscoveryAPI } from '../pythonEnvironments/base/locator';
export interface IResourceReference {
resourcePath?: string;
}
export class GetExecutableTool implements LanguageModelTool<IResourceReference> {
private readonly terminalExecutionService: TerminalCodeExecutionProvider;
private readonly terminalHelper: ITerminalHelper;
public static readonly toolName = 'get_python_executable';
constructor(
private readonly api: PythonExtension['environments'],
private readonly serviceContainer: IServiceContainer,
private readonly discovery: IDiscoveryAPI,
) {
this.terminalExecutionService = this.serviceContainer.get<TerminalCodeExecutionProvider>(
ICodeExecutionService,
'standard',
);
this.terminalHelper = this.serviceContainer.get<ITerminalHelper>(ITerminalHelper);
}
async invoke(
options: LanguageModelToolInvocationOptions<IResourceReference>,
token: CancellationToken,
): Promise<LanguageModelToolResult> {
const resourcePath = resolveFilePath(options.input.resourcePath);
try {
const message = await getEnvironmentDetails(
resourcePath,
this.api,
this.terminalExecutionService,
this.terminalHelper,
undefined,
token,
);
return new LanguageModelToolResult([new LanguageModelTextPart(message)]);
} catch (error) {
if (error instanceof CancellationError) {
throw error;
}
traceError('Error while getting environment information', 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> {
const resourcePath = resolveFilePath(options.input.resourcePath);
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'),
};
}
}