coder
1 #!/home/tao/kamaji/venv/bin/python 2 3 from langchain_community.llms import Ollama 4 import sys 5 import os 6 7 # Import Kamaji's tools 8 sys.path.append('/home/tao/kamaji') 9 from kamaji.tools import ( 10 execute_shell_command, 11 read_file, 12 write_file, 13 list_directory, 14 get_current_directory, 15 append_to_file 16 ) 17 18 # Color codes 19 class Colors: 20 BLUE = '\033[94m' 21 GREEN = '\033[92m' 22 YELLOW = '\033[93m' 23 RED = '\033[91m' 24 CYAN = '\033[96m' 25 MAGENTA = '\033[95m' 26 RESET = '\033[0m' 27 BOLD = '\033[1m' 28 29 def colored_print(text, color): 30 print(f"{color}{text}{Colors.RESET}") 31 32 class Coder: 33 def __init__(self): 34 self.llm = Ollama(model="gpt-oss:120b", base_url="http://192.222.50.154:11434") 35 self.history = [] 36 37 def get_context(self): 38 if not self.history: 39 return "" 40 return "\n".join(self.history[-10:]) + "\n\n" 41 42 def chat(self, message): 43 context = self.get_context() 44 45 # Handle directory questions 46 if "directory" in message.lower() or "cwd" in message.lower() or "pwd" in message.lower(): 47 response = get_current_directory() 48 colored_print(response, Colors.GREEN) 49 self.history.append(f"User: {message}") 50 self.history.append(f"Assistant: {response}") 51 return 52 53 # Handle "read directory" or "list files" requests 54 if any(phrase in message.lower() for phrase in ["read the directory", "list files", "show files", "directory files"]): 55 response = list_directory(".") 56 colored_print(response, Colors.GREEN) 57 self.history.append(f"User: {message}") 58 self.history.append(f"Assistant: {response}") 59 return 60 61 # Handle shell commands starting with ! 62 if message.startswith("!"): 63 command = message[1:] 64 response = execute_shell_command(command) 65 colored_print(response, Colors.YELLOW) 66 self.history.append(f"User: {message}") 67 self.history.append(f"Command output: {response}") 68 return 69 70 # Regular chat with system context 71 system_info = f"{get_current_directory()}\n{list_directory('.')}" 72 response = self.llm.invoke(f"{context}{system_info}\nUser: {message}\nAssistant:") 73 74 colored_print(response, Colors.GREEN) 75 self.history.append(f"User: {message}") 76 self.history.append(f"Assistant: {response}") 77 78 def edit_file(self, filepath, prompt): 79 # Read existing file 80 content = read_file(filepath) 81 if "Error" in content: 82 colored_print(content, Colors.RED) 83 return 84 85 context = self.get_context() 86 full_prompt = f"{context}Edit {filepath}. Return ONLY the complete modified code:\n\n{content}\n\nRequest: {prompt}" 87 88 response = self.llm.invoke(full_prompt) 89 90 # Write new content using Kamaji's tool 91 result = write_file(filepath, response) 92 colored_print(result, Colors.GREEN) 93 94 self.history.append(f"User: Edit {filepath}: {prompt}") 95 self.history.append(f"Assistant: {result}") 96 97 def create_file(self, filepath, prompt): 98 context = self.get_context() 99 response = self.llm.invoke(f"{context}Create {filepath}. Return ONLY the code:\n\n{prompt}") 100 101 result = write_file(filepath, response) 102 colored_print(result, Colors.GREEN) 103 104 self.history.append(f"User: Create {filepath}: {prompt}") 105 self.history.append(f"Assistant: {result}") 106 107 def interactive_mode(self): 108 colored_print("🚀 Coder - AI Assistant using Kamaji Tools", Colors.BOLD + Colors.CYAN) 109 colored_print("Commands:", Colors.BOLD) 110 print(" edit <file> <prompt> - AI edit file") 111 print(" create <file> <prompt> - AI create file") 112 print(" read <file> - Read file") 113 print(" append <file> <content> - Append to file") 114 print(" ls [dir] - List files") 115 print(" !<command> - Run shell command") 116 print(" <anything else> - Chat\n") 117 118 while True: 119 try: 120 cmd = input(f"{Colors.BLUE}coder> {Colors.RESET}").strip() 121 if not cmd: 122 continue 123 124 if cmd == "quit": 125 break 126 127 if cmd.startswith("!"): 128 colored_print(execute_shell_command(cmd[1:]), Colors.YELLOW) 129 continue 130 131 parts = cmd.split(" ", 2) 132 action = parts[0] 133 134 if action == "edit" and len(parts) >= 3: 135 self.edit_file(parts[1], parts[2]) 136 elif action == "create" and len(parts) >= 3: 137 self.create_file(parts[1], parts[2]) 138 elif action == "read" and len(parts) == 2: 139 colored_print(read_file(parts[1]), Colors.CYAN) 140 elif action == "append" and len(parts) >= 3: 141 colored_print(append_to_file(parts[1], parts[2]), Colors.GREEN) 142 elif action == "ls": 143 directory = parts[1] if len(parts) >= 2 else "." 144 colored_print(list_directory(directory), Colors.CYAN) 145 else: 146 self.chat(cmd) 147 148 except KeyboardInterrupt: 149 colored_print("\nGoodbye!", Colors.MAGENTA) 150 break 151 152 def main(): 153 coder = Coder() 154 155 if len(sys.argv) == 1: 156 coder.interactive_mode() 157 else: 158 action = sys.argv[1] 159 if action == "edit" and len(sys.argv) >= 4: 160 coder.edit_file(sys.argv[2], " ".join(sys.argv[3:])) 161 elif action == "create" and len(sys.argv) >= 4: 162 coder.create_file(sys.argv[2], " ".join(sys.argv[3:])) 163 elif action == "read" and len(sys.argv) >= 3: 164 print(read_file(sys.argv[2])) 165 else: 166 print("Usage: coder [edit|create|read] <file> [prompt]") 167 168 if __name__ == "__main__": 169 main()