|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (C) 2025 Posit Software, PBC. All rights reserved. |
| 3 | + * Licensed under the Elastic License 2.0. See LICENSE.txt for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | + |
| 8 | +export interface IPositronInstallPackageArgs { |
| 9 | + packages: string[]; |
| 10 | +} |
| 11 | + |
| 12 | +export class PositronInstallPackagesTool implements vscode.LanguageModelTool<IPositronInstallPackageArgs> { |
| 13 | + public static readonly toolName = 'installPythonPackage'; |
| 14 | + |
| 15 | + async prepareInvocation( |
| 16 | + options: vscode.LanguageModelToolInvocationPrepareOptions<IPositronInstallPackageArgs>, |
| 17 | + _token: vscode.CancellationToken, |
| 18 | + ): Promise<vscode.PreparedToolInvocation> { |
| 19 | + const packageNames = options.input.packages.join(', '); |
| 20 | + const result: vscode.PreparedToolInvocation = { |
| 21 | + // Display a generic command description rather than a specific pip command |
| 22 | + // The actual implementation uses environment-aware package management (pip, conda, poetry, etc.) |
| 23 | + // via the Python extension's installPackages command, not direct pip execution |
| 24 | + invocationMessage: `Install Python packages: ${packageNames}`, |
| 25 | + confirmationMessages: { |
| 26 | + title: vscode.l10n.t('Install Python Packages'), |
| 27 | + message: |
| 28 | + options.input.packages.length === 1 |
| 29 | + ? vscode.l10n.t( |
| 30 | + 'Positron Assistant wants to install the package {0}. Is this okay?', |
| 31 | + packageNames, |
| 32 | + ) |
| 33 | + : vscode.l10n.t( |
| 34 | + 'Positron Assistant wants to install the following packages: {0}. Is this okay?', |
| 35 | + packageNames, |
| 36 | + ), |
| 37 | + }, |
| 38 | + }; |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + async invoke( |
| 43 | + options: vscode.LanguageModelToolInvocationOptions<IPositronInstallPackageArgs>, |
| 44 | + _token: vscode.CancellationToken, |
| 45 | + ): Promise<vscode.LanguageModelToolResult> { |
| 46 | + try { |
| 47 | + // Use command-based communication - no API leakage |
| 48 | + const results = await vscode.commands.executeCommand( |
| 49 | + 'python.installPackages', |
| 50 | + options.input.packages, |
| 51 | + { requireConfirmation: false }, // Chat handles confirmations |
| 52 | + ); |
| 53 | + |
| 54 | + return new vscode.LanguageModelToolResult([ |
| 55 | + new vscode.LanguageModelTextPart(Array.isArray(results) ? results.join('\n') : String(results)), |
| 56 | + ]); |
| 57 | + } catch (error) { |
| 58 | + const errorMessage = error instanceof Error ? error.message : String(error); |
| 59 | + |
| 60 | + // Parse error code prefixes from Python extension's installPackages command |
| 61 | + // Expected prefixes: [NO_INSTALLER], [VALIDATION_ERROR] |
| 62 | + // See: installPackages.ts JSDoc for complete error code documentation |
| 63 | + let assistantGuidance = ''; |
| 64 | + |
| 65 | + if (errorMessage.startsWith('[NO_INSTALLER]')) { |
| 66 | + assistantGuidance = |
| 67 | + '\n\nSuggestion: The Python environment may not be properly configured. Ask the user to check their Python interpreter selection or create a new environment.'; |
| 68 | + } else if (errorMessage.startsWith('[VALIDATION_ERROR]')) { |
| 69 | + assistantGuidance = '\n\nSuggestion: Check that the package names are correct and properly formatted.'; |
| 70 | + } else { |
| 71 | + // Fallback for unexpected errors (network issues, permissions, etc.) |
| 72 | + assistantGuidance = |
| 73 | + '\n\nSuggestion: This may be a network, permissions, or environment issue. You can suggest the user retry the installation or try manual installation via terminal.'; |
| 74 | + } |
| 75 | + |
| 76 | + return new vscode.LanguageModelToolResult([ |
| 77 | + new vscode.LanguageModelTextPart( |
| 78 | + `Package installation encountered an issue: ${errorMessage}${assistantGuidance}`, |
| 79 | + ), |
| 80 | + ]); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments