1
1
import sys
2
2
import argparse
3
3
import subprocess
4
- import pyperclip
5
-
6
- from open_codex .agent_builder import AgentBuilder
7
- from open_codex .interfaces .llm_agent import LLMAgent
8
-
9
4
10
5
GREEN = "\033 [92m"
11
6
RED = "\033 [91m"
14
9
15
10
# Capture single keypress (terminal) from the user
16
11
# and returns it as a string. It works on both Windows and Unix systems.
12
+
17
13
# Windows
18
14
if sys .platform == "win32" :
19
15
import msvcrt
20
-
21
16
def get_keypress ():
22
17
return msvcrt .getch ().decode ("utf-8" )
23
-
24
- # Unix (Linux/macOS)
18
+ # Unix
25
19
else :
26
- import termios
27
- import tty
28
-
20
+ import termios , tty
29
21
def get_keypress ():
30
22
fd = sys .stdin .fileno ()
31
23
old_settings = termios .tcgetattr (fd )
@@ -37,61 +29,60 @@ def get_keypress():
37
29
return key
38
30
39
31
def 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 } " )
43
34
print (f"{ BLUE } ====================={ RESET } " )
44
-
45
35
print (f"{ BLUE } What do you want to do with this command?{ RESET } " )
46
36
print (f"{ BLUE } [c] Copy [e] Execute [a] Abort{ RESET } " )
47
37
print (f"{ BLUE } Press key: " , end = "" , flush = True )
38
+
48
39
choice = get_keypress ().lower ()
49
- print (f"{ RESET } " ) # Clear the line after the prompt
40
+ print (f"{ RESET } " )
50
41
51
42
if choice == "e" :
52
43
print (f"{ BLUE } Executing command: { command } { RESET } " )
53
44
result = subprocess .run (command , shell = True , capture_output = True , text = True )
54
45
print (f"{ GREEN } Command output: { result .stdout } { RESET } " )
55
46
if result .stderr :
56
47
print (f"{ RED } Error: { result .stderr } { RESET } " )
57
-
58
48
elif choice == "c" :
49
+ import pyperclip # ⏱ lazy import
59
50
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 } " )
62
52
elif choice == "a" :
63
53
print (f"{ BLUE } Aborted.{ RESET } " )
64
54
else :
65
55
print (f"{ RED } Unknown choice. Nothing happened.{ RESET } " )
66
56
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 } " )
68
60
try :
61
+ agent = AgentBuilder .get_agent ()
69
62
response = agent .one_shot_mode (prompt )
70
63
print_response (response )
71
64
except Exception as e :
72
65
print (f"{ RED } Error: { e } { RESET } " )
73
66
74
67
def print_help_message ():
75
- """Print help message with usage examples."""
76
- print (f"{ BLUE } Open Codex - Natural Language to CLI commands{ RESET } " )
77
68
print (f"{ BLUE } Usage examples:{ RESET } " )
78
69
print (f"{ GREEN } open-codex \" list all files in current directory\" " )
79
70
print (f"{ GREEN } open-codex \" find all python files modified in the last week\" " )
80
71
print (f"{ GREEN } open-codex \" create a tarball of the src directory\" " )
81
72
print ()
82
73
83
74
def 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 " )
86
77
args = parser .parse_args ()
87
78
prompt = " " .join (args .prompt ).strip ()
79
+
88
80
if not prompt or prompt == "--help" :
89
81
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 )
95
86
96
87
if __name__ == "__main__" :
97
88
main ()
0 commit comments