|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | | -import { |
5 | | - CancellationError, |
6 | | - CancellationToken, |
7 | | - l10n, |
8 | | - LanguageModelTextPart, |
9 | | - LanguageModelTool, |
10 | | - LanguageModelToolInvocationOptions, |
11 | | - LanguageModelToolInvocationPrepareOptions, |
12 | | - LanguageModelToolResult, |
13 | | - PreparedToolInvocation, |
14 | | - Uri, |
15 | | -} from 'vscode'; |
16 | | -import { PythonExtension, ResolvedEnvironment } from '../api/types'; |
17 | | -import { IServiceContainer } from '../ioc/types'; |
| 4 | +import { CancellationToken, Uri } from 'vscode'; |
| 5 | +import { ResolvedEnvironment } from '../api/types'; |
18 | 6 | import { IProcessService, IProcessServiceFactory, IPythonExecutionFactory } from '../common/process/types'; |
19 | | -import { getEnvDisplayName, isCondaEnv, raceCancellationError } from './utils'; |
20 | | -import { resolveFilePath } from './utils'; |
| 7 | +import { isCondaEnv, raceCancellationError } from './utils'; |
21 | 8 | import { parsePipList } from './pipListUtils'; |
22 | 9 | import { Conda } from '../pythonEnvironments/common/environmentManagers/conda'; |
23 | 10 | import { traceError } from '../logging'; |
24 | | -import { IDiscoveryAPI } from '../pythonEnvironments/base/locator'; |
25 | 11 | import { trackEnvUsedByTool } from './lastUsedEnvs'; |
26 | 12 |
|
27 | | -export interface IResourceReference { |
28 | | - resourcePath?: string; |
29 | | -} |
30 | | - |
31 | | -export class ListPythonPackagesTool implements LanguageModelTool<IResourceReference> { |
32 | | - private readonly pythonExecFactory: IPythonExecutionFactory; |
33 | | - private readonly processServiceFactory: IProcessServiceFactory; |
34 | | - public static readonly toolName = 'list_python_packages'; |
35 | | - constructor( |
36 | | - private readonly api: PythonExtension['environments'], |
37 | | - private readonly serviceContainer: IServiceContainer, |
38 | | - private readonly discovery: IDiscoveryAPI, |
39 | | - ) { |
40 | | - this.pythonExecFactory = this.serviceContainer.get<IPythonExecutionFactory>(IPythonExecutionFactory); |
41 | | - this.processServiceFactory = this.serviceContainer.get<IProcessServiceFactory>(IProcessServiceFactory); |
42 | | - } |
43 | | - |
44 | | - async invoke( |
45 | | - options: LanguageModelToolInvocationOptions<IResourceReference>, |
46 | | - token: CancellationToken, |
47 | | - ): Promise<LanguageModelToolResult> { |
48 | | - const resourcePath = resolveFilePath(options.input.resourcePath); |
49 | | - |
50 | | - try { |
51 | | - // environment |
52 | | - const envPath = this.api.getActiveEnvironmentPath(resourcePath); |
53 | | - const environment = await raceCancellationError(this.api.resolveEnvironment(envPath), token); |
54 | | - if (!environment) { |
55 | | - throw new Error('No environment found for the provided resource path: ' + resourcePath?.fsPath); |
56 | | - } |
57 | | - |
58 | | - const message = await getPythonPackagesResponse( |
59 | | - environment, |
60 | | - this.pythonExecFactory, |
61 | | - this.processServiceFactory, |
62 | | - resourcePath, |
63 | | - token, |
64 | | - ); |
65 | | - return new LanguageModelToolResult([new LanguageModelTextPart(message)]); |
66 | | - } catch (error) { |
67 | | - if (error instanceof CancellationError) { |
68 | | - throw error; |
69 | | - } |
70 | | - return new LanguageModelToolResult([ |
71 | | - new LanguageModelTextPart(`An error occurred while fetching environment information: ${error}`), |
72 | | - ]); |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - async prepareInvocation?( |
77 | | - options: LanguageModelToolInvocationPrepareOptions<IResourceReference>, |
78 | | - token: CancellationToken, |
79 | | - ): Promise<PreparedToolInvocation> { |
80 | | - const resourcePath = resolveFilePath(options.input.resourcePath); |
81 | | - const envName = await raceCancellationError(getEnvDisplayName(this.discovery, resourcePath, this.api), token); |
82 | | - return { |
83 | | - invocationMessage: envName |
84 | | - ? l10n.t('Listing packages in {0}', envName) |
85 | | - : l10n.t('Fetching Python environment information'), |
86 | | - }; |
87 | | - } |
88 | | -} |
89 | | - |
90 | 13 | export async function getPythonPackagesResponse( |
91 | 14 | environment: ResolvedEnvironment, |
92 | 15 | pythonExecFactory: IPythonExecutionFactory, |
|
0 commit comments