Created
July 31, 2025 17:57
-
-
Save Thomas-TyTech/f14566352f70aee6f5e630943b6f00b5 to your computer and use it in GitHub Desktop.
Simple Coding Agent - 125 lines of code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| from anthropic import Anthropic | |
| def handle_text_editor(tool_call): | |
| """Handle text editor commands for Claude""" | |
| params = tool_call.input | |
| command = params.get('command') | |
| path = params.get('path') | |
| try: | |
| if command == 'view': | |
| if os.path.isdir(path): | |
| return '\n'.join(os.listdir(path)) | |
| with open(path, 'r') as f: | |
| lines = f.readlines() | |
| # Handle view_range if specified | |
| view_range = params.get('view_range') | |
| if view_range: | |
| start, end = view_range | |
| if end == -1: | |
| end = len(lines) | |
| lines = lines[start-1:end] | |
| start_line = start | |
| else: | |
| start_line = 1 | |
| return ''.join(f"{i+start_line}: {line}" for i, line in enumerate(lines)) | |
| elif command == 'str_replace': | |
| with open(path, 'r') as f: | |
| content = f.read() | |
| old_str = params.get('old_str') | |
| new_str = params.get('new_str') | |
| if content.count(old_str) != 1: | |
| return f"Error: Found {content.count(old_str)} matches. Need exactly 1." | |
| with open(path, 'w') as f: | |
| f.write(content.replace(old_str, new_str)) | |
| return "Successfully replaced text at exactly one location." | |
| elif command == 'create': | |
| with open(path, 'w') as f: | |
| f.write(params.get('file_text')) | |
| return "File created" | |
| elif command == 'insert': | |
| with open(path, 'r') as f: | |
| lines = f.readlines() | |
| insert_line = params.get('insert_line') | |
| lines.insert(insert_line, params.get('new_str') + '\n') | |
| with open(path, 'w') as f: | |
| f.writelines(lines) | |
| return f"Text inserted at line {insert_line}." | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| def run_agent(prompt): | |
| """Run the agent""" | |
| client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"]) | |
| messages = [{"role": "user", "content": prompt}] | |
| print(f"🤖 Working on: {prompt}\n") | |
| while True: | |
| response = client.messages.create( | |
| model="claude-3-5-sonnet-20241022", | |
| max_tokens=1024, | |
| tools=[{"type": "text_editor_20250124", "name": "str_replace_editor"}], | |
| messages=messages | |
| ) | |
| messages.append({ | |
| "role": "assistant", | |
| "content": response.content | |
| }) | |
| tool_calls = [c for c in response.content if c.type == "tool_use"] | |
| if tool_calls: | |
| tool_results = [] | |
| for call in tool_calls: | |
| print(f"🔧 Using tool: {call.name}") | |
| result = handle_text_editor(call) | |
| tool_results.append({ | |
| "type": "tool_result", | |
| "tool_use_id": call.id, | |
| "content": result | |
| }) | |
| messages.append({ | |
| "role": "user", | |
| "content": tool_results | |
| }) | |
| else: | |
| text_blocks = [c for c in response.content if c.type == "text"] | |
| if text_blocks: | |
| print(f"\n✅ Done: {text_blocks[0].text}") | |
| break | |
| if __name__ == "__main__": | |
| while True: | |
| try: | |
| prompt = input("\n💬 What would you like me to do? (or 'quit' to exit)\n> ") | |
| if prompt.lower() == 'quit': | |
| break | |
| run_agent(prompt) | |
| except KeyboardInterrupt: | |
| print("\n👋 Goodbye!") | |
| break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment