1111from codegen .cli .rich .codeblocks import format_command , format_path
1212from codegen .cli .rich .pretty_print import pretty_print_error
1313from codegen .cli .rich .spinners import create_spinner
14- from codegen .cli .utils .constants import ProgrammingLanguage
1514from codegen .cli .utils .default_code import DEFAULT_CODEMOD
1615from codegen .cli .workspace .decorators import requires_init
1716
@@ -29,7 +28,7 @@ def get_prompts_dir() -> Path:
2928 return PROMPTS_DIR
3029
3130
32- def get_target_path (name : str , path : Path ) -> Path :
31+ def get_target_paths (name : str , path : Path ) -> tuple [ Path , Path ] :
3332 """Get the target path for the new function file.
3433
3534 Creates a directory structure like:
@@ -47,7 +46,9 @@ def get_target_path(name: str, path: Path) -> Path:
4746 # Create path within .codegen/codemods
4847 codemods_dir = base_dir / ".codegen" / "codemods"
4948 function_dir = codemods_dir / name_snake
50- return function_dir / f"{ name_snake } .py"
49+ codemod_path = function_dir / f"{ name_snake } .py"
50+ prompt_path = function_dir / f"{ name_snake } -system-prompt.txt"
51+ return codemod_path , prompt_path
5152
5253
5354def make_relative (path : Path ) -> str :
@@ -76,11 +77,11 @@ def create_command(session: CodegenSession, name: str, path: Path, description:
7677 PATH is where to create the function (default: current directory)
7778 """
7879 # Get the target path for the function
79- target_path = get_target_path (name , path )
80+ codemod_path , prompt_path = get_target_paths (name , path )
8081
8182 # Check if file exists
82- if target_path .exists () and not overwrite :
83- rel_path = make_relative (target_path )
83+ if codemod_path .exists () and not overwrite :
84+ rel_path = make_relative (codemod_path )
8485 pretty_print_error (f"File already exists at { format_path (rel_path )} \n \n To overwrite the file:\n { format_command (f'codegen create { name } { rel_path } --overwrite' )} " )
8586 return
8687
@@ -89,34 +90,30 @@ def create_command(session: CodegenSession, name: str, path: Path, description:
8990 try :
9091 if description :
9192 # Use API to generate implementation
92- with create_spinner ("Generating function (using LLM, this will take ~30s )" ) as status :
93+ with create_spinner ("Generating function (using LLM, this will take ~10s )" ) as status :
9394 response = RestAPI (session .token ).create (name = name , query = description )
94- code = convert_to_cli (response .code , session .config .programming_language or ProgrammingLanguage .PYTHON , name )
95-
96- # Write the system prompt if provided
97- if response .context :
98- prompt_path = get_prompts_dir () / f"{ name .lower ().replace (' ' , '-' )} -system-prompt.md"
99- prompt_path .write_text (response .context )
95+ code = convert_to_cli (response .code , session .language , name )
10096 else :
10197 # Use default implementation
10298 code = DEFAULT_CODEMOD .format (name = name )
10399
104100 # Create the target directory if needed
105- target_path .parent .mkdir (parents = True , exist_ok = True )
101+ codemod_path .parent .mkdir (parents = True , exist_ok = True )
106102
107103 # Write the function code
108- target_path .write_text (code )
104+ codemod_path .write_text (code )
105+ prompt_path .write_text (response .context )
109106
110107 except (ServerError , ValueError ) as e :
111108 raise click .ClickException (str (e ))
112109
113110 # Success message
114- rich .print (f"\n ✅ { 'Overwrote' if overwrite and target_path .exists () else 'Created' } function '{ name } '" )
111+ rich .print (f"\n ✅ { 'Overwrote' if overwrite and codemod_path .exists () else 'Created' } function '{ name } '" )
115112 rich .print ("" )
116113 rich .print ("📁 Files Created:" )
117- rich .print (f" [dim]Function:[/dim] { make_relative (target_path )} " )
114+ rich .print (f" [dim]Function:[/dim] { make_relative (codemod_path )} " )
118115 if description and response .context :
119- rich .print (f" [dim]Prompt:[/dim] { make_relative (get_prompts_dir () / f' { name . lower (). replace ( " " , "-" ) } -system-prompt.md' )} " )
116+ rich .print (f" [dim]Prompt:[/dim] { make_relative (prompt_path )} " )
120117
121118 # Next steps
122119 rich .print ("\n [bold]What's next?[/bold]\n " )
0 commit comments