-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathgetEnvInfoTool.ts
More file actions
61 lines (55 loc) · 2.26 KB
/
getEnvInfoTool.ts
File metadata and controls
61 lines (55 loc) · 2.26 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {
CancellationToken,
l10n,
LanguageModelTextPart,
LanguageModelTool,
LanguageModelToolInvocationOptions,
LanguageModelToolInvocationPrepareOptions,
LanguageModelToolResult,
PreparedToolInvocation,
} from 'vscode';
import { PythonEnvironmentApi } from '../../api';
import { EnvironmentManagers } from '../../internal.api';
import { getPythonPackagesResponse } from './listPackagesTool';
import { getEnvironmentDetails, getToolResponseIfNotebook, raceCancellationError, resolveFilePath } from './utils';
export interface IResourceReference {
resourcePath?: string;
}
export class GetEnvironmentInfoTool implements LanguageModelTool<IResourceReference> {
public static readonly toolName = 'get_python_environment_info';
constructor(private readonly api: PythonEnvironmentApi, private readonly envManagers: EnvironmentManagers) {}
async invoke(
options: LanguageModelToolInvocationOptions<IResourceReference>,
token: CancellationToken,
): Promise<LanguageModelToolResult> {
const resourcePath = resolveFilePath(options.input.resourcePath);
const notebookResponse = getToolResponseIfNotebook(resourcePath);
if (notebookResponse) {
return notebookResponse;
}
const environment = await raceCancellationError(this.api.getEnvironment(resourcePath), token);
if (!environment) {
throw new Error(`No environment found for the provided resource path ${resourcePath?.fsPath}`);
}
const packages = await getPythonPackagesResponse(environment, this.api, token);
const message = await getEnvironmentDetails(
resourcePath,
undefined,
this.api,
this.envManagers,
packages,
token,
);
return new LanguageModelToolResult([new LanguageModelTextPart(message)]);
}
async prepareInvocation?(
_options: LanguageModelToolInvocationPrepareOptions<IResourceReference>,
_token: CancellationToken,
): Promise<PreparedToolInvocation> {
return {
invocationMessage: l10n.t('Fetching Python environment information'),
};
}
}