11import sys
22import argparse
33import subprocess
4- import pyperclip
5-
6- from open_codex .agent_builder import AgentBuilder
7- from open_codex .interfaces .llm_agent import LLMAgent
8-
94
105GREEN = "\033 [92m"
116RED = "\033 [91m"
149
1510# Capture single keypress (terminal) from the user
1611# and returns it as a string. It works on both Windows and Unix systems.
12+
1713# Windows
1814if sys .platform == "win32" :
1915 import msvcrt
20-
2116 def get_keypress ():
2217 return msvcrt .getch ().decode ("utf-8" )
23-
24- # Unix (Linux/macOS)
18+ # Unix
2519else :
26- import termios
27- import tty
28-
20+ import termios , tty
2921 def get_keypress ():
3022 fd = sys .stdin .fileno ()
3123 old_settings = termios .tcgetattr (fd )
@@ -37,61 +29,60 @@ def get_keypress():
3729 return key
3830
3931def print_response (command : str ):
40- print (f"{ BLUE } Command found:" )
41- print (f"{ BLUE } =====================" )
42- print (f"{ GREEN } { command } { RESET } " ) # Print the command in green
32+ print (f"{ BLUE } Command found:\n =====================" )
33+ print (f"{ GREEN } { command } { RESET } " )
4334 print (f"{ BLUE } ====================={ RESET } " )
44-
4535 print (f"{ BLUE } What do you want to do with this command?{ RESET } " )
4636 print (f"{ BLUE } [c] Copy [e] Execute [a] Abort{ RESET } " )
4737 print (f"{ BLUE } Press key: " , end = "" , flush = True )
38+
4839 choice = get_keypress ().lower ()
49- print (f"{ RESET } " ) # Clear the line after the prompt
40+ print (f"{ RESET } " )
5041
5142 if choice == "e" :
5243 print (f"{ BLUE } Executing command: { command } { RESET } " )
5344 result = subprocess .run (command , shell = True , capture_output = True , text = True )
5445 print (f"{ GREEN } Command output: { result .stdout } { RESET } " )
5546 if result .stderr :
5647 print (f"{ RED } Error: { result .stderr } { RESET } " )
57-
5848 elif choice == "c" :
49+ import pyperclip # ⏱ lazy import
5950 pyperclip .copy (command )
60- print (f"{ GREEN } Command copied to clipboard! Paste it manually in your terminal.{ RESET } " )
61-
51+ print (f"{ GREEN } Command copied to clipboard!{ RESET } " )
6252 elif choice == "a" :
6353 print (f"{ BLUE } Aborted.{ RESET } " )
6454 else :
6555 print (f"{ RED } Unknown choice. Nothing happened.{ RESET } " )
6656
67- def one_shot_mode (agent : LLMAgent , prompt : str ):
57+ def one_shot_mode (prompt : str ):
58+ from open_codex .agent_builder import AgentBuilder
59+ print (f"{ BLUE } Using model: phi-4-mini-instruct{ RESET } " )
6860 try :
61+ agent = AgentBuilder .get_agent ()
6962 response = agent .one_shot_mode (prompt )
7063 print_response (response )
7164 except Exception as e :
7265 print (f"{ RED } Error: { e } { RESET } " )
7366
7467def print_help_message ():
75- """Print help message with usage examples."""
76- print (f"{ BLUE } Open Codex - Natural Language to CLI commands{ RESET } " )
7768 print (f"{ BLUE } Usage examples:{ RESET } " )
7869 print (f"{ GREEN } open-codex \" list all files in current directory\" " )
7970 print (f"{ GREEN } open-codex \" find all python files modified in the last week\" " )
8071 print (f"{ GREEN } open-codex \" create a tarball of the src directory\" " )
8172 print ()
8273
8374def main ():
84- parser = argparse .ArgumentParser (description = "Open Codex - Natural Language to CLI commands" )
85- parser .add_argument ("prompt" , nargs = "*" , help = "Optional prompt for one-shot mode " )
75+ parser = argparse .ArgumentParser ()
76+ parser .add_argument ("prompt" , nargs = "*" , help = "Natural language prompt " )
8677 args = parser .parse_args ()
8778 prompt = " " .join (args .prompt ).strip ()
79+
8880 if not prompt or prompt == "--help" :
8981 print_help_message ()
90- sys .exit (1 )
91-
92- agent = AgentBuilder .get_agent ()
93- print (f"{ BLUE } Using model: phi-4-mini-instruct{ RESET } " )
94- one_shot_mode (agent , prompt )
82+ sys .exit (0 )
83+
84+ print (f"{ BLUE } Prompt: { prompt } { RESET } " , flush = True )
85+ one_shot_mode (prompt )
9586
9687if __name__ == "__main__" :
9788 main ()
0 commit comments