@@ -11,18 +11,17 @@ import {
1111 LanguageModelToolInvocationPrepareOptions ,
1212 LanguageModelToolResult ,
1313 PreparedToolInvocation ,
14- Uri ,
1514} from 'vscode' ;
16- import { PythonExtension , ResolvedEnvironment } from '../api/types' ;
15+ import { PythonExtension } from '../api/types' ;
1716import { IServiceContainer } from '../ioc/types' ;
1817import { ICodeExecutionService } from '../terminals/types' ;
1918import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/terminalCodeExecution' ;
20- import { getEnvDisplayName , isCondaEnv , raceCancellationError } from './utils' ;
19+ import { getEnvDisplayName , getEnvironmentDetails , raceCancellationError } from './utils' ;
2120import { resolveFilePath } from './utils' ;
2221import { traceError } from '../logging' ;
23- import { ITerminalHelper , TerminalShellType } from '../common/terminal/types' ;
22+ import { ITerminalHelper } from '../common/terminal/types' ;
2423import { IDiscoveryAPI } from '../pythonEnvironments/base/locator' ;
25- import { Conda } from '../pythonEnvironments/common/environmentManagers/conda ' ;
24+ import { ConfigurePythonEnvTool } from './configurePythonEnvTool ' ;
2625
2726export interface IResourceReference {
2827 resourcePath ?: string ;
@@ -47,30 +46,30 @@ export class GetExecutableTool implements LanguageModelTool<IResourceReference>
4746 options : LanguageModelToolInvocationOptions < IResourceReference > ,
4847 token : CancellationToken ,
4948 ) : Promise < LanguageModelToolResult > {
49+ if ( ! ConfigurePythonEnvTool . EnvironmentConfigured ) {
50+ return new LanguageModelToolResult ( [
51+ new LanguageModelTextPart (
52+ [
53+ `A Python environment is not configured. Please configure a Python environment first using the ${ ConfigurePythonEnvTool . toolName } .` ,
54+ `The ${ ConfigurePythonEnvTool . toolName } tool will guide the user through the process of configuring a Python environment.` ,
55+ 'Once the environment is configured, you can use this tool to get the Python executable information.' ,
56+ ] . join ( '\n' ) ,
57+ ) ,
58+ ] ) ;
59+ }
60+
5061 const resourcePath = resolveFilePath ( options . input . resourcePath ) ;
5162
5263 try {
53- // environment
54- const envPath = this . api . getActiveEnvironmentPath ( resourcePath ) ;
55- const environment = await raceCancellationError ( this . api . resolveEnvironment ( envPath ) , token ) ;
56- if ( ! environment || ! environment . version ) {
57- throw new Error ( 'No environment found for the provided resource path: ' + resourcePath ?. fsPath ) ;
58- }
59- const runCommand = await raceCancellationError (
60- getTerminalCommand ( environment , resourcePath , this . terminalExecutionService , this . terminalHelper ) ,
64+ const message = await getEnvironmentDetails (
65+ resourcePath ,
66+ this . api ,
67+ this . terminalExecutionService ,
68+ this . terminalHelper ,
69+ undefined ,
6170 token ,
6271 ) ;
63-
64- const message = [
65- `Following is the information about the Python environment:` ,
66- `1. Environment Type: ${ environment . environment ?. type || 'unknown' } ` ,
67- `2. Version: ${ environment . version . sysVersion || 'unknown' } ` ,
68- '' ,
69- `3. Command Prefix to run Python in a terminal is: \`${ runCommand } \`` ,
70- `Instead of running \`Python sample.py\` in the terminal, you will now run: \`${ runCommand } sample.py\`` ,
71- `Similarly instead of running \`Python -c "import sys;...."\` in the terminal, you will now run: \`${ runCommand } -c "import sys;...."\`` ,
72- ] ;
73- return new LanguageModelToolResult ( [ new LanguageModelTextPart ( message . join ( '\n' ) ) ] ) ;
72+ return new LanguageModelToolResult ( [ new LanguageModelTextPart ( message ) ] ) ;
7473 } catch ( error ) {
7574 if ( error instanceof CancellationError ) {
7675 throw error ;
@@ -94,36 +93,3 @@ export class GetExecutableTool implements LanguageModelTool<IResourceReference>
9493 } ;
9594 }
9695}
97-
98- export async function getTerminalCommand (
99- environment : ResolvedEnvironment ,
100- resource : Uri | undefined ,
101- terminalExecutionService : TerminalCodeExecutionProvider ,
102- terminalHelper : ITerminalHelper ,
103- ) : Promise < string > {
104- let cmd : { command : string ; args : string [ ] } ;
105- if ( isCondaEnv ( environment ) ) {
106- cmd = ( await getCondaRunCommand ( environment ) ) || ( await terminalExecutionService . getExecutableInfo ( resource ) ) ;
107- } else {
108- cmd = await terminalExecutionService . getExecutableInfo ( resource ) ;
109- }
110- return terminalHelper . buildCommandForTerminal ( TerminalShellType . other , cmd . command , cmd . args ) ;
111- }
112- async function getCondaRunCommand ( environment : ResolvedEnvironment ) {
113- if ( ! environment . executable . uri ) {
114- return ;
115- }
116- const conda = await Conda . getConda ( ) ;
117- if ( ! conda ) {
118- return ;
119- }
120- const condaEnv = await conda . getCondaEnvironment ( environment . executable . uri ?. fsPath ) ;
121- if ( ! condaEnv ) {
122- return ;
123- }
124- const cmd = await conda . getRunPythonArgs ( condaEnv , true , false ) ;
125- if ( ! cmd ) {
126- return ;
127- }
128- return { command : cmd [ 0 ] , args : cmd . slice ( 1 ) } ;
129- }
0 commit comments