-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.py
More file actions
438 lines (371 loc) · 16.1 KB
/
Copy pathcommand.py
File metadata and controls
438 lines (371 loc) · 16.1 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# Command - Super Terminal com IA
# Autor: Mari05liM
# Versão: 6.0
#
# Instalacao:
# 1. Ollama: https://ollama.com/download → ollama pull qwen2.5:3b
# 2. pip install -r requirements.txt
# 3. python command.py
import os
import platform
import re
import subprocess
import requests
from typing import List, Dict
_IS_WINDOWS = platform.system() == "Windows"
from rich.console import Console
from rich.markup import escape
from rich.panel import Panel
from rich.rule import Rule
from rich.text import Text
from rich.theme import Theme
# ── Tema ──────────────────────────────────────────────────────────────────────
_theme = Theme({
"cmd": "bold yellow",
"output": "dim white",
"ai": "white",
"success": "bold green",
"error": "bold red",
"warn": "yellow",
"info": "bold cyan",
"muted": "bright_black",
"banner": "bold cyan",
})
console = Console(theme=_theme, highlight=False)
# ── Arquivo .env ───────────────────────────────────────────────────────────────
ENV_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env")
def _load_env_file():
"""Carrega .env sem sobrescrever variáveis já definidas no ambiente."""
if not os.path.exists(ENV_FILE):
return
with open(ENV_FILE, encoding="utf-8-sig") as f:
for line in f:
line = line.strip()
if line and not line.startswith("#") and "=" in line:
k, _, v = line.partition("=")
k, v = k.strip(), v.strip()
os.environ[k] = v
def salvar_modelo_env(modelo: str):
"""Atualiza MODELO_ATUAL no arquivo .env."""
if not os.path.exists(ENV_FILE):
return
lines = []
encontrado = False
with open(ENV_FILE, encoding="utf-8-sig") as f:
for line in f:
if line.startswith("MODELO_ATUAL="):
lines.append(f"MODELO_ATUAL={modelo}\n")
encontrado = True
else:
lines.append(line)
if not encontrado:
lines.append(f"MODELO_ATUAL={modelo}\n")
with open(ENV_FILE, "w", encoding="utf-8") as f:
f.writelines(lines)
_load_env_file()
# ── Configuracoes ─────────────────────────────────────────────────────────────
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
USUARIO = os.getenv("USUARIO", os.getenv("USERNAME") or os.getenv("USER", "user"))
OLLAMA_URL = os.getenv("OLLAMA_URL", "http://localhost:11434")
PROVIDER_ENV = os.getenv("PROVIDER", "")
MODELO_ENV = os.getenv("MODELO_ATUAL", "")
MODELOS_OPENAI = ["gpt-4o-mini", "gpt-4.1-mini", "gpt-4.1-nano", "gpt-5-mini", "gpt-5-nano"]
MODELOS_PREFERIDOS = [
"qwen2.5:3b", "qwen2.5",
"gemma3:1b", "gemma3:4b",
"llama3.2", "llama3.2:3b",
"mistral", "llama3.1",
]
# Comandos que destroem dados sem volta
_BLOQUEADOS_WINDOWS = [
"format c:", "format d:", "format e:",
"del /f /s /q c:\\", "rd /s /q c:\\",
"rmdir /s /q c:\\",
]
_BLOQUEADOS_UNIX = [
"rm -rf /", "rm -rf /*",
"sudo rm -rf /", "sudo rm -rf /*",
"mkfs", "dd if=/dev/zero of=/dev/sd",
"> /dev/sda", "chmod -r /", "chmod 000 /",
]
BLOQUEADOS = _BLOQUEADOS_WINDOWS if _IS_WINDOWS else _BLOQUEADOS_UNIX
BANNER = """\
[bold cyan] ██████╗ ██████╗ ███╗ ███╗███╗ ███╗ █████╗ ███╗ ██╗██████╗ [/]
[bold cyan]██╔════╝██╔═══██╗████╗ ████║████╗ ████║██╔══██╗████╗ ██║██╔══██╗[/]
[bold cyan]██║ ██║ ██║██╔████╔██║██╔████╔██║███████║██╔██╗ ██║██║ ██║[/]
[cyan]██║ ██║ ██║██║╚██╔╝██║██║╚██╔╝██║██╔══██║██║╚██╗██║██║ ██║[/]
[cyan]╚██████╗╚██████╔╝██║ ╚═╝ ██║██║ ╚═╝ ██║██║ ██║██║ ╚████║██████╔╝[/]
[cyan] ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═════╝ [/]\
"""
_SYSTEM_PROMPT_WINDOWS = """You are Command, an AI-powered terminal for Windows CMD.
Execute actions by wrapping commands in curly braces: {command}
Use multiple {command} blocks for multi-step tasks. Keep responses short.
Windows CMD syntax reference (always use these — never use Linux/bash syntax):
List files → {dir}
Change directory → {cd foldername}
Create folder → {mkdir foldername}
Delete folder → {rmdir /s /q foldername}
Create empty file → {copy nul filename.txt}
Write to file → {echo content > filename.txt}
Read file → {type filename.txt}
Delete file → {del filename.txt}
Copy file → {copy source.txt dest.txt}
Move file → {move source.txt dest\\}
Rename → {ren oldname.txt newname.txt}
Open app → {start appname} or {appname}
Open Notepad → {notepad}
Open Paint → {start mspaint}
Open VS Code → {code .}
Run Python → {python script.py}
Install package → {pip install package}
Git status → {git status}
For paths with spaces use quotes: {cd "C:\\My Folder"}
Multi-step example — "create folder dog with empty file boing.txt inside":
{mkdir dog}
{copy nul dog\\boing.txt}
"""
_SYSTEM_PROMPT_UNIX = """You are Command, an AI-powered terminal for bash/zsh.
Execute actions by wrapping commands in curly braces: {command}
Use multiple {command} blocks for multi-step tasks. Keep responses short.
Bash syntax reference (always use these — never use Windows CMD syntax):
List files → {ls}
Change directory → {cd foldername}
Create folder → {mkdir foldername}
Delete folder → {rm -rf foldername}
Create empty file → {touch filename.txt}
Write to file → {echo "content" > filename.txt}
Read file → {cat filename.txt}
Delete file → {rm filename.txt}
Copy file → {cp source.txt dest.txt}
Move/Rename file → {mv source.txt dest.txt}
Open VS Code → {code .}
Run Python → {python3 script.py}
Install package → {pip3 install package}
Git status → {git status}
For paths with spaces use quotes: {cd "My Folder"}
Multi-step example — "create folder dog with empty file boing.txt inside":
{mkdir dog}
{touch dog/boing.txt}
"""
SYSTEM_PROMPT = _SYSTEM_PROMPT_WINDOWS if _IS_WINDOWS else _SYSTEM_PROMPT_UNIX
# ── Helpers ───────────────────────────────────────────────────────────────────
def limpar_tela():
os.system("cls" if _IS_WINDOWS else "clear")
def prompt_dir(cwd: str) -> str:
"""Retorna o markup de prompt colorido."""
try:
home = os.path.expanduser("~")
if cwd.startswith(home):
cwd = "~" + cwd[len(home):]
except Exception:
pass
return f"[bold green]{USUARIO}[/][bright_black]@[/][bold cyan]{cwd}[/] [bold white]❯[/] "
def executar(cmd: str, cwd: str) -> tuple[str, int, str]:
"""Executa um comando e retorna (output, returncode, novo_cwd)."""
cmd = cmd.strip()
# cd precisa alterar o cwd do processo pai
if re.match(r"^cd\b", cmd, re.IGNORECASE):
path = cmd[2:].strip().strip('"').strip("'")
if not path or path == ".":
return "", 0, cwd
novo = path if os.path.isabs(path) else os.path.normpath(os.path.join(cwd, path))
if os.path.isdir(novo):
return "", 0, novo
return f"Diretorio nao encontrado: {path}", 1, cwd
if any(b in cmd.lower() for b in BLOQUEADOS):
return "Comando bloqueado por seguranca.", 1, cwd
try:
result = subprocess.run(
cmd, shell=True, cwd=cwd,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
timeout=30, encoding="utf-8", errors="replace",
)
return result.stdout.strip(), result.returncode, cwd
except subprocess.TimeoutExpired:
return "Timeout (30s)", 1, cwd
except Exception as e:
return str(e), 1, cwd
def extrair_comandos(texto: str) -> tuple[str, List[str]]:
"""Separa o texto limpo dos {comandos} contidos na resposta."""
cmds = re.findall(r"\{([^}]+)\}", texto)
limpo = re.sub(r"\{[^}]+\}", "", texto).strip()
return limpo, cmds
def detectar_melhor_modelo_ollama() -> str | None:
try:
resp = requests.get(f"{OLLAMA_URL}/api/tags", timeout=3)
if resp.status_code != 200:
return None
modelos = [m["name"] for m in resp.json().get("models", [])]
for preferido in MODELOS_PREFERIDOS:
for m in modelos:
if preferido in m:
return m
return modelos[0] if modelos else None
except requests.RequestException:
return None
def gerar_resposta_ollama(historico: List[Dict], modelo: str, cwd: str) -> str:
mensagens = [
{"role": "system", "content": SYSTEM_PROMPT + f"\n\nCurrent directory: {cwd}"}
] + historico
try:
resp = requests.post(
f"{OLLAMA_URL}/api/chat",
json={"model": modelo, "messages": mensagens, "stream": False},
timeout=60,
)
resp.raise_for_status()
return resp.json()["message"]["content"].strip()
except Exception as e:
return f"Erro Ollama: {e}"
def _modelos_ollama_instalados() -> List[str]:
"""Retorna os modelos instalados localmente no Ollama."""
try:
resp = requests.get(f"{OLLAMA_URL}/api/tags", timeout=3)
return [m["name"] for m in resp.json().get("models", [])]
except Exception:
return []
def gerar_resposta_openai(historico: List[Dict], modelo: str, cwd: str) -> str:
try:
from openai import OpenAI
client = OpenAI(api_key=OPENAI_API_KEY)
mensagens = [
{"role": "system", "content": SYSTEM_PROMPT + f"\n\nCurrent directory: {cwd}"}
] + historico
resp = client.chat.completions.create(
model=modelo, messages=mensagens,
)
return resp.choices[0].message.content
except Exception as e:
return f"Erro OpenAI: {e}"
# ── Main ──────────────────────────────────────────────────────────────────────
def main():
limpar_tela()
os.environ["PYTHONIOENCODING"] = "utf-8"
cwd = os.getcwd()
# Banner
console.print(BANNER)
console.print(
"[muted] Super Terminal com IA[/] "
"[bright_black]─[/] [muted]v6.0[/]"
)
console.print()
# Detecta provider e modelo a partir do .env; auto-detecta como fallback
provider = PROVIDER_ENV
modelo_atual = MODELO_ENV
if not provider or not modelo_atual:
with console.status("[bold cyan]Detectando modelos...[/]", spinner="dots"):
modelo_ollama = detectar_melhor_modelo_ollama()
if modelo_ollama:
if not provider:
provider = "ollama"
if not modelo_atual:
modelo_atual = modelo_ollama
elif OPENAI_API_KEY:
if not provider:
provider = "openai"
if not modelo_atual:
modelo_atual = "gpt-5-nano"
else:
console.print(Panel(
"[error]Nenhum provider encontrado.[/]\n\n"
"[muted]Crie um[/] [cmd].env[/] [muted]na pasta do command.py com:[/]\n"
" [bright_black]PROVIDER=ollama[/]\n"
" [bright_black]MODELO_ATUAL=qwen2.5:3b[/]\n\n"
"[muted]Ou para OpenAI:[/]\n"
" [bright_black]PROVIDER=openai[/]\n"
" [bright_black]MODELO_ATUAL=gpt-4o-mini[/]\n"
" [bright_black]OPENAI_API_KEY=sk-...[/]",
title="[error]✗ Erro[/]",
border_style="red",
padding=(0, 2),
))
return
provider_label = "Ollama" if provider == "ollama" else "OpenAI"
console.print(Panel(
f"[success]●[/] [bold]{provider_label}[/] "
f"[muted]modelo:[/] [info]{modelo_atual}[/]",
border_style="cyan",
padding=(0, 2),
))
# Atalhos
console.print()
console.print(
" [muted]![/][bright_black]cmd[/] executa direto "
"[muted]:[/][bright_black]modelo[/] troca modelo "
"[muted]exit[/] sair"
)
console.print(Rule(style="bright_black"))
console.print()
historico: List[Dict] = []
while True:
try:
entrada = console.input(prompt_dir(cwd)).strip()
except (KeyboardInterrupt, EOFError):
console.print("\n[muted]Até logo![/]")
break
if not entrada:
continue
# Sair
if entrada.lower() in ("sair", "exit", "quit"):
console.print("[muted]Até logo![/]")
break
# Trocar modelo → :llama3.2 ou : (interativo)
if entrada.startswith(":"):
novo = entrada[1:].strip()
if not novo:
# Sugestões: modelos instalados no Ollama ou lista OpenAI
if provider == "ollama":
instalados = _modelos_ollama_instalados()
sugestoes = instalados if instalados else MODELOS_PREFERIDOS
else:
sugestoes = MODELOS_OPENAI
console.print(f" [muted]Sugestões:[/] [bright_black]{', '.join(sugestoes)}[/]")
try:
novo = console.input(" [muted]Novo modelo →[/] ").strip()
except (KeyboardInterrupt, EOFError):
continue
if not novo:
continue
modelo_atual = novo
salvar_modelo_env(novo)
console.print(f" [muted]modelo →[/] [info]{modelo_atual}[/]")
continue
# Modo direto → !dir !git status !python script.py
if entrada.startswith("!"):
cmd = entrada[1:].strip()
console.print(f" [cmd]▶ {escape(cmd)}[/]")
output, code, cwd = executar(cmd, cwd)
if output:
style = "error" if code != 0 else "output"
console.print(Text(output, style=style))
continue
# IA processa a entrada
historico.append({"role": "user", "content": entrada})
with console.status(
f"[bold cyan]Pensando[/] [muted]({modelo_atual})[/]...",
spinner="dots",
):
resposta = (
gerar_resposta_ollama(historico, modelo_atual, cwd)
if provider == "ollama"
else gerar_resposta_openai(historico, modelo_atual, cwd)
)
historico.append({"role": "assistant", "content": resposta})
if len(historico) > 30:
historico = historico[-30:]
texto, cmds = extrair_comandos(resposta)
if texto:
console.print(Panel(
Text(texto, style="white"),
border_style="cyan",
padding=(0, 2),
))
for cmd in cmds:
console.print(f"\n [cmd]▶ {escape(cmd)}[/]")
output, code, cwd = executar(cmd, cwd)
if output:
style = "error" if code != 0 else "output"
console.print(Text(output, style=style))
console.print()
if __name__ == "__main__":
main()