@@ -19,7 +19,7 @@ import { PythonExtension, ResolvedEnvironment } from '../api/types';
1919import { IServiceContainer } from '../ioc/types' ;
2020import { ICodeExecutionService } from '../terminals/types' ;
2121import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/terminalCodeExecution' ;
22- import { getEnvironmentDetails , raceCancellationError } from './utils' ;
22+ import { getEnvironmentDetails , NoEnvironmentError , raceCancellationError } from './utils' ;
2323import { resolveFilePath } from './utils' ;
2424import { IRecommendedEnvironmentService } from '../interpreter/configuration/types' ;
2525import { ITerminalHelper } from '../common/terminal/types' ;
@@ -67,33 +67,51 @@ export class ConfigurePythonEnvTool implements LanguageModelTool<IResourceRefere
6767 options : LanguageModelToolInvocationOptions < IResourceReference > ,
6868 token : CancellationToken ,
6969 ) : Promise < LanguageModelToolResult > {
70- const resource = resolveFilePath ( options . input . resourcePath ) ;
71- const recommededEnv = await this . recommendedEnvService . getRecommededEnvironment ( resource ) ;
72- // Already selected workspace env, hence nothing to do.
73- if ( recommededEnv ?. reason === 'workspaceUserSelected' && workspace . workspaceFolders ?. length ) {
74- return await getEnvDetailsForResponse (
75- recommededEnv . environment ,
76- this . api ,
77- this . terminalExecutionService ,
78- this . terminalHelper ,
79- resource ,
80- token ,
81- ) ;
82- }
83- // No workspace folders, and the user selected a global environment.
84- if ( recommededEnv ?. reason === 'globalUserSelected' && ! workspace . workspaceFolders ?. length ) {
85- return await getEnvDetailsForResponse (
86- recommededEnv . environment ,
87- this . api ,
88- this . terminalExecutionService ,
89- this . terminalHelper ,
90- resource ,
91- token ,
92- ) ;
93- }
70+ try {
71+ const resource = resolveFilePath ( options . input . resourcePath ) ;
72+ const recommededEnv = await this . recommendedEnvService . getRecommededEnvironment ( resource ) ;
73+ // Already selected workspace env, hence nothing to do.
74+ if ( recommededEnv ?. reason === 'workspaceUserSelected' && workspace . workspaceFolders ?. length ) {
75+ return await getEnvDetailsForResponse (
76+ recommededEnv . environment ,
77+ this . api ,
78+ this . terminalExecutionService ,
79+ this . terminalHelper ,
80+ resource ,
81+ token ,
82+ ) ;
83+ }
84+ // No workspace folders, and the user selected a global environment.
85+ if ( recommededEnv ?. reason === 'globalUserSelected' && ! workspace . workspaceFolders ?. length ) {
86+ return await getEnvDetailsForResponse (
87+ recommededEnv . environment ,
88+ this . api ,
89+ this . terminalExecutionService ,
90+ this . terminalHelper ,
91+ resource ,
92+ token ,
93+ ) ;
94+ }
9495
95- if ( ! workspace . workspaceFolders ?. length ) {
96- const selected = await Promise . resolve ( commands . executeCommand ( Commands . Set_Interpreter ) ) ;
96+ if ( ! workspace . workspaceFolders ?. length ) {
97+ const selected = await Promise . resolve ( commands . executeCommand ( Commands . Set_Interpreter ) ) ;
98+ const env = await this . api . resolveEnvironment ( this . api . getActiveEnvironmentPath ( resource ) ) ;
99+ if ( selected && env ) {
100+ return await getEnvDetailsForResponse (
101+ env ,
102+ this . api ,
103+ this . terminalExecutionService ,
104+ this . terminalHelper ,
105+ resource ,
106+ token ,
107+ ) ;
108+ }
109+ return new LanguageModelToolResult ( [
110+ new LanguageModelTextPart ( 'User did not select a Python environment.' ) ,
111+ ] ) ;
112+ }
113+
114+ const selected = await showCreateAndSelectEnvironmentQuickPick ( resource , this . serviceContainer ) ;
97115 const env = await this . api . resolveEnvironment ( this . api . getActiveEnvironmentPath ( resource ) ) ;
98116 if ( selected && env ) {
99117 return await getEnvDetailsForResponse (
@@ -106,25 +124,18 @@ export class ConfigurePythonEnvTool implements LanguageModelTool<IResourceRefere
106124 ) ;
107125 }
108126 return new LanguageModelToolResult ( [
109- new LanguageModelTextPart ( 'User did not select a Python environment.' ) ,
127+ new LanguageModelTextPart ( 'User did not create nor select a Python environment.' ) ,
110128 ] ) ;
129+ } catch ( ex ) {
130+ if ( ex instanceof NoEnvironmentError ) {
131+ return new LanguageModelToolResult ( [
132+ new LanguageModelTextPart (
133+ 'Failed to configure a Python Environment, as the environment could not be found.' ,
134+ ) ,
135+ ] ) ;
136+ }
137+ throw ex ;
111138 }
112-
113- const selected = await showCreateAndSelectEnvironmentQuickPick ( resource , this . serviceContainer ) ;
114- const env = await this . api . resolveEnvironment ( this . api . getActiveEnvironmentPath ( resource ) ) ;
115- if ( selected && env ) {
116- return await getEnvDetailsForResponse (
117- env ,
118- this . api ,
119- this . terminalExecutionService ,
120- this . terminalHelper ,
121- resource ,
122- token ,
123- ) ;
124- }
125- return new LanguageModelToolResult ( [
126- new LanguageModelTextPart ( 'User did not create nor select a Python environment.' ) ,
127- ] ) ;
128139 }
129140
130141 async prepareInvocation ?(
0 commit comments