forked from pythaiml/automindx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminai.py
76 lines (65 loc) · 2.6 KB
/
terminai.py
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
import curses
import requests
import os
import subprocess
def get_api_key():
env_file = './.env'
if os.path.exists(env_file):
with open(env_file, 'r') as file:
return file.read().strip()
else:
api_key = input("Enter your OpenAI API key: ").strip()
with open(env_file, 'w') as file:
file.write(api_key)
return api_key
class TerminAI:
def __init__(self, stdscr, api_key):
self.stdscr = stdscr
self.api_key = api_key
self.setup_folders()
def setup_folders(self):
# Set up both terminai and saindbx folders
os.makedirs('./terminai', exist_ok=True)
os.makedirs('./saindbx', exist_ok=True)
os.chmod('./terminai', 0o700)
os.chmod('./saindbx', 0o700)
def talk_to_ai(self, message):
# Simulate talking to an AI (like using SimpleCoder)
return f"Simulated response for: {message}"
def execute_and_save_command(self, command):
# Execute command and handle SimpleCoder interaction
try:
# Assuming SimpleCoder could be a method to generate or analyze code
# This is a placeholder for the actual function call
# output = SimpleCoder.generate_code(command)
output = f"Simulated code generation for: {command}"
filepath = os.path.join('./saindbx', 'output.txt')
with open(filepath, 'w') as file:
file.write(output)
return f"Code saved to saindbx/output.txt\n{output}"
except Exception as e:
return f"An error occurred: {str(e)}"
def main(self):
self.stdscr.clear()
self.stdscr.addstr("Welcome to TerminAI. Type 'exit' to quit, or type a command to execute it.\n")
curses.echo()
self.stdscr.keypad(True)
while True:
self.stdscr.addstr("> ")
input_str = self.stdscr.getstr().decode('utf-8').strip()
if input_str == 'exit':
break
elif input_str.startswith("cmd:"):
command = input_str[4:]
output = self.execute_and_save_command(command)
self.stdscr.addstr(f"Command executed and result saved.\nOutput:\n{output}\n")
else:
response = self.talk_to_ai(input_str)
self.stdscr.addstr(f"AI: {response}\n")
self.stdscr.refresh()
self.stdscr.addstr("Goodbye!")
self.stdscr.refresh()
self.stdscr.getch()
if __name__ == '__main__':
api_key = get_api_key() # Get the API key before initializing curses
curses.wrapper(TerminAI, api_key)