-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.py
More file actions
142 lines (113 loc) · 4.08 KB
/
command.py
File metadata and controls
142 lines (113 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
"""
Terminal Command Generator using Groq API
Generates the terminal command to run the transpiled code
"""
import os
import re
from groq import Groq
from dotenv import load_dotenv
def generate_run_command(file_path, changes_summary):
"""
Generate terminal command to run the transpiled code using Groq inference.
Args:
file_path: Original .n<lang> file path (e.g., "hello.npy")
changes_summary: Git diff summary showing what files were created/modified
Returns:
dict: {
"success": bool,
"command": str,
"error": str
}
"""
# Load environment and get API key
load_dotenv()
api_key = os.getenv("GROQ_API")
if not api_key:
return {
"success": False,
"command": None,
"error": "GROQ_API key not found in .env",
}
# Build prompt for Groq
prompt = f"""Generate the terminal command to run the transpiled code.
Original file: {file_path}
(e.g., if original is "hello.npy", the transpiled file will be "hello.py")
Changes made by the transpiler:
{changes_summary[:600]}
Your task: Generate the exact terminal command to run the main transpiled file.
Examples:
- If original: hello.npy → command: python hello.py
- If original: server.njs → command: node server.js
- If original: app.ngo → command: go run app.go
- If original: main.nts → command: tsx main.ts
Return ONLY the terminal command in this format:
```bash
<command>
```
"""
try:
# Call Groq API
client = Groq(api_key=api_key)
system_prompt = """You are a terminal command expert for a natural code transpiler system.
The system works like this:
- Users write code in .n<language> files (e.g., .npy for Python, .njs for JavaScript)
- The transpiler converts these to actual runnable code files
- Your job: Generate the EXACT terminal command to run the transpiled file
Rules:
1. The transpiled file name = original filename with 'n' removed from extension
- hello.npy → hello.py → command: python hello.py
- server.njs → server.js → command: node server.js
- app.njava → app.java → command: java app (for Java, no extension)
2. Use the appropriate runtime/interpreter:
- .py → python or python3
- .js → node
- .java → javac <file_name>.java && java <file_name>
- .go → go run
- .ts/.tsx → tsx or ts-node
- .rb → ruby
- .php → php
NOTE: the command should be self contained and should NOT any further steps to run the code.
3. Return ONLY the command in this format:
```bash
<command>
```
Do NOT add explanations, do NOT add extra text. ONLY the command in a bash code block."""
response = client.chat.completions.create(
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt},
],
model="llama-3.3-70b-versatile",
temperature=0.1,
max_tokens=100,
)
# Parse command from response
raw_output = response.choices[0].message.content.strip()
command = parse_bash_block(raw_output)
if not command:
return {
"success": False,
"command": None,
"error": "Empty command generated",
}
return {"success": True, "command": command, "error": None}
except Exception as e:
return {"success": False, "command": None, "error": f"Groq API error: {str(e)}"}
def parse_bash_block(raw_output):
"""
Parse command from Groq output in format:
```bash
<command>
```
"""
# Extract content between ```bash and ```
match = re.search(r"```bash\s*\n(.+?)\n```", raw_output, re.DOTALL)
if match:
return match.group(1).strip().split("\n")[0].strip()
# Fallback: try generic code block
match = re.search(r"```\s*\n(.+?)\n```", raw_output, re.DOTALL)
if match:
return match.group(1).strip().split("\n")[0].strip()
# Fallback: use raw output first line
return raw_output.strip().split("\n")[0].strip()