22
33use anyhow:: { Context , Result , bail} ;
44use std:: io:: { self , IsTerminal , Read , Write } ;
5+ use std:: path:: Path ;
56use std:: time:: Duration ;
67
78use crate :: styled_output:: { print_success, print_warning} ;
@@ -17,6 +18,15 @@ use super::output::{copy_to_clipboard, send_notification};
1718use super :: session:: { SessionMode , resolve_session_id} ;
1819use super :: system:: check_file_descriptor_limits;
1920
21+ /// Return the working directory value exposed to custom command templates.
22+ pub fn command_template_cwd ( cwd : Option < & Path > ) -> String {
23+ cwd. map ( Path :: to_path_buf)
24+ . or_else ( || std:: env:: current_dir ( ) . ok ( ) )
25+ . unwrap_or_default ( )
26+ . to_string_lossy ( )
27+ . to_string ( )
28+ }
29+
2030impl RunCli {
2131 /// Run the command.
2232 pub async fn run ( self ) -> Result < ( ) > {
@@ -428,12 +438,8 @@ impl RunCli {
428438 // Try to get the custom command registry
429439 if let Some ( registry) = cortex_engine:: try_custom_command_registry ( ) {
430440 // Try to execute the custom command
431- let ctx = cortex_engine:: TemplateContext :: new ( message. to_string ( ) ) . with_cwd (
432- std:: env:: current_dir ( )
433- . unwrap_or_default ( )
434- . to_string_lossy ( )
435- . to_string ( ) ,
436- ) ;
441+ let ctx = cortex_engine:: TemplateContext :: new ( message. to_string ( ) )
442+ . with_cwd ( command_template_cwd ( self . cwd . as_deref ( ) ) ) ;
437443
438444 // Use blocking runtime to get the command
439445 let prompt = tokio:: task:: block_in_place ( || {
0 commit comments