forked from Doriandarko/claude-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1216 lines (1023 loc) · 56.4 KB
/
main.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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
from dotenv import load_dotenv
import json
from tavily import TavilyClient
import base64
from PIL import Image
import io
import re
from anthropic import Anthropic, APIStatusError, APIError
import difflib
import time
from rich.console import Console
from rich.panel import Panel
from rich.syntax import Syntax
from rich.markdown import Markdown
import asyncio
import aiohttp
from prompt_toolkit import PromptSession
from prompt_toolkit.styles import Style
async def get_user_input(prompt="You: "):
style = Style.from_dict({
'prompt': 'cyan bold',
})
session = PromptSession(style=style)
return await session.prompt_async(prompt, multiline=False)
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn
import datetime
import venv
import subprocess
import sys
import signal
import logging
from typing import Tuple, Optional
def setup_virtual_environment() -> Tuple[str, str]:
venv_name = "code_execution_env"
venv_path = os.path.join(os.getcwd(), venv_name)
try:
if not os.path.exists(venv_path):
venv.create(venv_path, with_pip=True)
# Activate the virtual environment
if sys.platform == "win32":
activate_script = os.path.join(venv_path, "Scripts", "activate.bat")
else:
activate_script = os.path.join(venv_path, "bin", "activate")
return venv_path, activate_script
except Exception as e:
logging.error(f"Error setting up virtual environment: {str(e)}")
raise
# Load environment variables from .env file
load_dotenv()
# Initialize the Anthropic client
anthropic_api_key = os.getenv("ANTHROPIC_API_KEY")
if not anthropic_api_key:
raise ValueError("ANTHROPIC_API_KEY not found in environment variables")
client = Anthropic(api_key=anthropic_api_key)
# Initialize the Tavily client
tavily_api_key = os.getenv("TAVILY_API_KEY")
if not tavily_api_key:
raise ValueError("TAVILY_API_KEY not found in environment variables")
tavily = TavilyClient(api_key=tavily_api_key)
console = Console()
# Token tracking variables
main_model_tokens = {'input': 0, 'output': 0}
tool_checker_tokens = {'input': 0, 'output': 0}
code_editor_tokens = {'input': 0, 'output': 0}
code_execution_tokens = {'input': 0, 'output': 0}
# Set up the conversation memory (maintains context for MAINMODEL)
conversation_history = []
# Store file contents (part of the context for MAINMODEL)
file_contents = {}
# Code editor memory (maintains some context for CODEEDITORMODEL between calls)
code_editor_memory = []
# Files already present in code editor's context
code_editor_files = set()
# automode flag
automode = False
# Store file contents
file_contents = {}
# Global dictionary to store running processes
running_processes = {}
# Constants
CONTINUATION_EXIT_PHRASE = "AUTOMODE_COMPLETE"
MAX_CONTINUATION_ITERATIONS = 25
MAX_CONTEXT_TOKENS = 200000 # Reduced to 200k tokens for context window
# Models
# Models that maintain context memory across interactions
MAINMODEL = "claude-3-5-sonnet-20240620" # Maintains conversation history and file contents
# Models that don't maintain context (memory is reset after each call)
TOOLCHECKERMODEL = "claude-3-5-sonnet-20240620"
CODEEDITORMODEL = "claude-3-5-sonnet-20240620"
CODEEXECUTIONMODEL = "claude-3-5-sonnet-20240620"
# System prompts
BASE_SYSTEM_PROMPT = """
You are Claude, an AI assistant powered by Anthropic's Claude-3.5-Sonnet model, specialized in software development with access to a variety of tools and the ability to instruct and direct a coding agent and a code execution one. Your capabilities include:
1. Creating and managing project structures
2. Writing, debugging, and improving code across multiple languages
3. Providing architectural insights and applying design patterns
4. Staying current with the latest technologies and best practices
5. Analyzing and manipulating files within the project directory
6. Performing web searches for up-to-date information
7. Executing code and analyzing its output within an isolated 'code_execution_env' virtual environment
8. Managing and stopping running processes started within the 'code_execution_env'
Available tools and their optimal use cases:
1. create_folder: Create new directories in the project structure.
2. create_file: Generate new files with specified content. Strive to make the file as complete and useful as possible.
3. edit_and_apply: Examine and modify existing files by instructing a separate AI coding agent. You are responsible for providing clear, detailed instructions to this agent. When using this tool:
- Provide comprehensive context about the project, including recent changes, new variables or functions, and how files are interconnected.
- Clearly state the specific changes or improvements needed, explaining the reasoning behind each modification.
- Include ALL the snippets of code to change, along with the desired modifications.
- Specify coding standards, naming conventions, or architectural patterns to be followed.
- Anticipate potential issues or conflicts that might arise from the changes and provide guidance on how to handle them.
4. execute_code: Run Python code exclusively in the 'code_execution_env' virtual environment and analyze its output. Use this when you need to test code functionality or diagnose issues. Remember that all code execution happens in this isolated environment. This tool now returns a process ID for long-running processes.
5. stop_process: Stop a running process by its ID. Use this when you need to terminate a long-running process started by the execute_code tool.
6. read_file: Read the contents of an existing file.
7. read_multiple_files: Read the contents of multiple existing files at once. Use this when you need to examine or work with multiple files simultaneously.
8. list_files: List all files and directories in a specified folder.
9. tavily_search: Perform a web search using the Tavily API for up-to-date information.
Tool Usage Guidelines:
- Always use the most appropriate tool for the task at hand.
- Provide detailed and clear instructions when using tools, especially for edit_and_apply.
- After making changes, always review the output to ensure accuracy and alignment with intentions.
- Use execute_code to run and test code within the 'code_execution_env' virtual environment, then analyze the results.
- For long-running processes, use the process ID returned by execute_code to stop them later if needed.
- Proactively use tavily_search when you need up-to-date information or additional context.
- When working with multiple files, consider using read_multiple_files for efficiency.
Error Handling and Recovery:
- If a tool operation fails, carefully analyze the error message and attempt to resolve the issue.
- For file-related errors, double-check file paths and permissions before retrying.
- If a search fails, try rephrasing the query or breaking it into smaller, more specific searches.
- If code execution fails, analyze the error output and suggest potential fixes, considering the isolated nature of the environment.
- If a process fails to stop, consider potential reasons and suggest alternative approaches.
Project Creation and Management:
1. Start by creating a root folder for new projects.
2. Create necessary subdirectories and files within the root folder.
3. Organize the project structure logically, following best practices for the specific project type.
Always strive for accuracy, clarity, and efficiency in your responses and actions. Your instructions must be precise and comprehensive. If uncertain, use the tavily_search tool or admit your limitations. When executing code, always remember that it runs in the isolated 'code_execution_env' virtual environment. Be aware of any long-running processes you start and manage them appropriately, including stopping them when they are no longer needed.
When using tools:
1. Carefully consider if a tool is necessary before using it.
2. Ensure all required parameters are provided and valid.
3. Handle both successful results and errors gracefully.
4. Provide clear explanations of tool usage and results to the user.
Remember, you are an AI assistant, and your primary goal is to help the user accomplish their tasks effectively and efficiently while maintaining the integrity and security of their development environment.
"""
AUTOMODE_SYSTEM_PROMPT = """
You are currently in automode. Follow these guidelines:
1. Goal Setting:
- Set clear, achievable goals based on the user's request.
- Break down complex tasks into smaller, manageable goals.
2. Goal Execution:
- Work through goals systematically, using appropriate tools for each task.
- Utilize file operations, code writing, and web searches as needed.
- Always read a file before editing and review changes after editing.
3. Progress Tracking:
- Provide regular updates on goal completion and overall progress.
- Use the iteration information to pace your work effectively.
4. Tool Usage:
- Leverage all available tools to accomplish your goals efficiently.
- Prefer edit_and_apply for file modifications, applying changes in chunks for large edits.
- Use tavily_search proactively for up-to-date information.
5. Error Handling:
- If a tool operation fails, analyze the error and attempt to resolve the issue.
- For persistent errors, consider alternative approaches to achieve the goal.
6. Automode Completion:
- When all goals are completed, respond with "AUTOMODE_COMPLETE" to exit automode.
- Do not ask for additional tasks or modifications once goals are achieved.
7. Iteration Awareness:
- You have access to this {iteration_info}.
- Use this information to prioritize tasks and manage time effectively.
Remember: Focus on completing the established goals efficiently and effectively. Avoid unnecessary conversations or requests for additional tasks.
"""
def update_system_prompt(current_iteration: Optional[int] = None, max_iterations: Optional[int] = None) -> str:
global file_contents
chain_of_thought_prompt = """
Answer the user's request using relevant tools (if they are available). Before calling a tool, do some analysis within <thinking></thinking> tags. First, think about which of the provided tools is the relevant tool to answer the user's request. Second, go through each of the required parameters of the relevant tool and determine if the user has directly provided or given enough information to infer a value. When deciding if the parameter can be inferred, carefully consider all the context to see if it supports a specific value. If all of the required parameters are present or can be reasonably inferred, close the thinking tag and proceed with the tool call. BUT, if one of the values for a required parameter is missing, DO NOT invoke the function (not even with fillers for the missing params) and instead, ask the user to provide the missing parameters. DO NOT ask for more information on optional parameters if it is not provided.
Do not reflect on the quality of the returned search results in your response.
"""
file_contents_prompt = "\n\nFile Contents:\n"
for path, content in file_contents.items():
file_contents_prompt += f"\n--- {path} ---\n{content}\n"
if automode:
iteration_info = ""
if current_iteration is not None and max_iterations is not None:
iteration_info = f"You are currently on iteration {current_iteration} out of {max_iterations} in automode."
return BASE_SYSTEM_PROMPT + file_contents_prompt + "\n\n" + AUTOMODE_SYSTEM_PROMPT.format(iteration_info=iteration_info) + "\n\n" + chain_of_thought_prompt
else:
return BASE_SYSTEM_PROMPT + file_contents_prompt + "\n\n" + chain_of_thought_prompt
def create_folder(path):
try:
os.makedirs(path, exist_ok=True)
return f"Folder created: {path}"
except Exception as e:
return f"Error creating folder: {str(e)}"
def create_file(path, content=""):
global file_contents
try:
with open(path, 'w') as f:
f.write(content)
file_contents[path] = content
return f"File created and added to system prompt: {path}"
except Exception as e:
return f"Error creating file: {str(e)}"
def highlight_diff(diff_text):
return Syntax(diff_text, "diff", theme="monokai", line_numbers=True)
async def generate_edit_instructions(file_path, file_content, instructions, project_context, full_file_contents):
global code_editor_tokens, code_editor_memory, code_editor_files
try:
# Prepare memory context (this is the only part that maintains some context between calls)
memory_context = "\n".join([f"Memory {i+1}:\n{mem}" for i, mem in enumerate(code_editor_memory)])
# Prepare full file contents context, excluding the file being edited if it's already in code_editor_files
full_file_contents_context = "\n\n".join([
f"--- {path} ---\n{content}" for path, content in full_file_contents.items()
if path != file_path or path not in code_editor_files
])
system_prompt = f"""
You are an AI coding agent that generates edit instructions for code files. Your task is to analyze the provided code and generate SEARCH/REPLACE blocks for necessary changes. Follow these steps:
1. Review the entire file content to understand the context:
{file_content}
2. Carefully analyze the specific instructions:
{instructions}
3. Take into account the overall project context:
{project_context}
4. Consider the memory of previous edits:
{memory_context}
5. Consider the full context of all files in the project:
{full_file_contents_context}
6. Generate SEARCH/REPLACE blocks for each necessary change. Each block should:
- Include enough context to uniquely identify the code to be changed
- Provide the exact replacement code, maintaining correct indentation and formatting
- Focus on specific, targeted changes rather than large, sweeping modifications
7. Ensure that your SEARCH/REPLACE blocks:
- Address all relevant aspects of the instructions
- Maintain or enhance code readability and efficiency
- Consider the overall structure and purpose of the code
- Follow best practices and coding standards for the language
- Maintain consistency with the project context and previous edits
- Take into account the full context of all files in the project
IMPORTANT: RETURN ONLY THE SEARCH/REPLACE BLOCKS. NO EXPLANATIONS OR COMMENTS.
USE THE FOLLOWING FORMAT FOR EACH BLOCK:
<SEARCH>
Code to be replaced
</SEARCH>
<REPLACE>
New code to insert
</REPLACE>
If no changes are needed, return an empty list.
"""
# Make the API call to CODEEDITORMODEL (context is not maintained except for code_editor_memory)
response = client.messages.create(
model=CODEEDITORMODEL,
max_tokens=8000,
system=system_prompt,
extra_headers={"anthropic-beta": "max-tokens-3-5-sonnet-2024-07-15"},
messages=[
{"role": "user", "content": "Generate SEARCH/REPLACE blocks for the necessary changes."}
]
)
# Update token usage for code editor
code_editor_tokens['input'] += response.usage.input_tokens
code_editor_tokens['output'] += response.usage.output_tokens
# Parse the response to extract SEARCH/REPLACE blocks
edit_instructions = parse_search_replace_blocks(response.content[0].text)
# Update code editor memory (this is the only part that maintains some context between calls)
code_editor_memory.append(f"Edit Instructions for {file_path}:\n{response.content[0].text}")
# Add the file to code_editor_files set
code_editor_files.add(file_path)
return edit_instructions
except Exception as e:
console.print(f"Error in generating edit instructions: {str(e)}", style="bold red")
return [] # Return empty list if any exception occurs
def parse_search_replace_blocks(response_text):
blocks = []
pattern = r'<SEARCH>\n(.*?)\n</SEARCH>\n<REPLACE>\n(.*?)\n</REPLACE>'
matches = re.findall(pattern, response_text, re.DOTALL)
for search, replace in matches:
blocks.append({
'search': search.strip(),
'replace': replace.strip()
})
return json.dumps(blocks) # Keep returning JSON string
async def edit_and_apply(path, instructions, project_context, is_automode=False, max_retries=3):
global file_contents
try:
original_content = file_contents.get(path, "")
if not original_content:
with open(path, 'r') as file:
original_content = file.read()
file_contents[path] = original_content
for attempt in range(max_retries):
edit_instructions_json = await generate_edit_instructions(path, original_content, instructions, project_context, file_contents)
if edit_instructions_json:
edit_instructions = json.loads(edit_instructions_json) # Parse JSON here
console.print(Panel(f"Attempt {attempt + 1}/{max_retries}: The following SEARCH/REPLACE blocks have been generated:", title="Edit Instructions", style="cyan"))
for i, block in enumerate(edit_instructions, 1):
console.print(f"Block {i}:")
console.print(Panel(f"SEARCH:\n{block['search']}\n\nREPLACE:\n{block['replace']}", expand=False))
edited_content, changes_made, failed_edits = await apply_edits(path, edit_instructions, original_content)
if changes_made:
file_contents[path] = edited_content # Update the file_contents with the new content
console.print(Panel(f"File contents updated in system prompt: {path}", style="green"))
if failed_edits:
console.print(Panel(f"Some edits could not be applied. Retrying...", style="yellow"))
instructions += f"\n\nPlease retry the following edits that could not be applied:\n{failed_edits}"
original_content = edited_content
continue
return f"Changes applied to {path}"
elif attempt == max_retries - 1:
return f"No changes could be applied to {path} after {max_retries} attempts. Please review the edit instructions and try again."
else:
console.print(Panel(f"No changes could be applied in attempt {attempt + 1}. Retrying...", style="yellow"))
else:
return f"No changes suggested for {path}"
return f"Failed to apply changes to {path} after {max_retries} attempts."
except Exception as e:
return f"Error editing/applying to file: {str(e)}"
async def apply_edits(file_path, edit_instructions, original_content):
changes_made = False
edited_content = original_content
total_edits = len(edit_instructions)
failed_edits = []
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
console=console
) as progress:
edit_task = progress.add_task("[cyan]Applying edits...", total=total_edits)
for i, edit in enumerate(edit_instructions, 1):
search_content = edit['search'].strip()
replace_content = edit['replace'].strip()
# Use regex to find the content, ignoring leading/trailing whitespace
pattern = re.compile(re.escape(search_content), re.DOTALL)
match = pattern.search(edited_content)
if match:
# Replace the content, preserving the original whitespace
start, end = match.span()
# Strip <SEARCH> and <REPLACE> tags from replace_content
replace_content_cleaned = re.sub(r'</?SEARCH>|</?REPLACE>', '', replace_content)
edited_content = edited_content[:start] + replace_content_cleaned + edited_content[end:]
changes_made = True
# Display the diff for this edit
diff_result = generate_diff(search_content, replace_content, file_path)
console.print(Panel(diff_result, title=f"Changes in {file_path} ({i}/{total_edits})", style="cyan"))
else:
console.print(Panel(f"Edit {i}/{total_edits} not applied: content not found", style="yellow"))
failed_edits.append(f"Edit {i}: {search_content}")
progress.update(edit_task, advance=1)
if not changes_made:
console.print(Panel("No changes were applied. The file content already matches the desired state.", style="green"))
else:
# Write the changes to the file
with open(file_path, 'w') as file:
file.write(edited_content)
console.print(Panel(f"Changes have been written to {file_path}", style="green"))
return edited_content, changes_made, "\n".join(failed_edits)
def generate_diff(original, new, path):
diff = list(difflib.unified_diff(
original.splitlines(keepends=True),
new.splitlines(keepends=True),
fromfile=f"a/{path}",
tofile=f"b/{path}",
n=3
))
diff_text = ''.join(diff)
highlighted_diff = highlight_diff(diff_text)
return highlighted_diff
async def execute_code(code, timeout=10):
global running_processes
venv_path, activate_script = setup_virtual_environment()
# Generate a unique identifier for this process
process_id = f"process_{len(running_processes)}"
# Write the code to a temporary file
with open(f"{process_id}.py", "w") as f:
f.write(code)
# Prepare the command to run the code
if sys.platform == "win32":
command = f'"{activate_script}" && python3 {process_id}.py'
else:
command = f'source "{activate_script}" && python3 {process_id}.py'
# Create a process to run the command
process = await asyncio.create_subprocess_shell(
command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
shell=True,
preexec_fn=None if sys.platform == "win32" else os.setsid
)
# Store the process in our global dictionary
running_processes[process_id] = process
try:
# Wait for initial output or timeout
stdout, stderr = await asyncio.wait_for(process.communicate(), timeout=timeout)
stdout = stdout.decode()
stderr = stderr.decode()
return_code = process.returncode
except asyncio.TimeoutError:
# If we timeout, it means the process is still running
stdout = "Process started and running in the background."
stderr = ""
return_code = "Running"
execution_result = f"Process ID: {process_id}\n\nStdout:\n{stdout}\n\nStderr:\n{stderr}\n\nReturn Code: {return_code}"
return process_id, execution_result
def read_file(path):
global file_contents
try:
with open(path, 'r') as f:
content = f.read()
file_contents[path] = content
return f"File '{path}' has been read and stored in the system prompt."
except Exception as e:
return f"Error reading file: {str(e)}"
def read_multiple_files(paths):
global file_contents
results = []
for path in paths:
try:
with open(path, 'r') as f:
content = f.read()
file_contents[path] = content
results.append(f"File '{path}' has been read and stored in the system prompt.")
except Exception as e:
results.append(f"Error reading file '{path}': {str(e)}")
return "\n".join(results)
def list_files(path="."):
try:
files = os.listdir(path)
return "\n".join(files)
except Exception as e:
return f"Error listing files: {str(e)}"
def tavily_search(query):
try:
response = tavily.qna_search(query=query, search_depth="advanced")
return response
except Exception as e:
return f"Error performing search: {str(e)}"
def stop_process(process_id):
global running_processes
if process_id in running_processes:
process = running_processes[process_id]
if sys.platform == "win32":
process.terminate()
else:
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
del running_processes[process_id]
return f"Process {process_id} has been stopped."
else:
return f"No running process found with ID {process_id}."
tools = [
{
"name": "create_folder",
"description": "Create a new folder at the specified path. This tool should be used when you need to create a new directory in the project structure. It will create all necessary parent directories if they don't exist. The tool will return a success message if the folder is created or already exists, and an error message if there's a problem creating the folder.",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The absolute or relative path where the folder should be created. Use forward slashes (/) for path separation, even on Windows systems."
}
},
"required": ["path"]
}
},
{
"name": "create_file",
"description": "Create a new file at the specified path with the given content. This tool should be used when you need to create a new file in the project structure. It will create all necessary parent directories if they don't exist. The tool will return a success message if the file is created, and an error message if there's a problem creating the file or if the file already exists. The content should be as complete and useful as possible, including necessary imports, function definitions, and comments.",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The absolute or relative path where the file should be created. Use forward slashes (/) for path separation, even on Windows systems."
},
"content": {
"type": "string",
"description": "The content of the file. This should include all necessary code, comments, and formatting."
}
},
"required": ["path", "content"]
}
},
{
"name": "edit_and_apply",
"description": "Apply AI-powered improvements to a file based on specific instructions and detailed project context. This function reads the file, processes it in batches using AI with conversation history and comprehensive code-related project context. It generates a diff and allows the user to confirm changes before applying them. The goal is to maintain consistency and prevent breaking connections between files. This tool should be used for complex code modifications that require understanding of the broader project context.",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The absolute or relative path of the file to edit. Use forward slashes (/) for path separation, even on Windows systems."
},
"instructions": {
"type": "string",
"description": "After completing the code review, construct a plan for the change between <PLANNING> tags. Ask for additional source files or documentation that may be relevant. The plan should avoid duplication (DRY principle), and balance maintenance and flexibility. Present trade-offs and implementation choices at this step. Consider available Frameworks and Libraries and suggest their use when relevant. STOP at this step if we have not agreed a plan.\n\nOnce agreed, produce code between <OUTPUT> tags. Pay attention to Variable Names, Identifiers and String Literals, and check that they are reproduced accurately from the original source files unless otherwise directed. When naming by convention surround in double colons and in ::UPPERCASE::. Maintain existing code style, use language appropriate idioms. Produce Code Blocks with the language specified after the first backticks"
},
"project_context": {
"type": "string",
"description": "Comprehensive context about the project, including recent changes, new variables or functions, interconnections between files, coding standards, and any other relevant information that might affect the edit."
}
},
"required": ["path", "instructions", "project_context"]
}
},
{
"name": "execute_code",
"description": "Execute Python code in the 'code_execution_env' virtual environment and return the output. This tool should be used when you need to run code and see its output or check for errors. All code execution happens exclusively in this isolated environment. The tool will return the standard output, standard error, and return code of the executed code. Long-running processes will return a process ID for later management.",
"input_schema": {
"type": "object",
"properties": {
"code": {
"type": "string",
"description": "The Python code to execute in the 'code_execution_env' virtual environment. Include all necessary imports and ensure the code is complete and self-contained."
}
},
"required": ["code"]
}
},
{
"name": "stop_process",
"description": "Stop a running process by its ID. This tool should be used to terminate long-running processes that were started by the execute_code tool. It will attempt to stop the process gracefully, but may force termination if necessary. The tool will return a success message if the process is stopped, and an error message if the process doesn't exist or can't be stopped.",
"input_schema": {
"type": "object",
"properties": {
"process_id": {
"type": "string",
"description": "The ID of the process to stop, as returned by the execute_code tool for long-running processes."
}
},
"required": ["process_id"]
}
},
{
"name": "read_file",
"description": "Read the contents of a file at the specified path. This tool should be used when you need to examine the contents of an existing file. It will return the entire contents of the file as a string. If the file doesn't exist or can't be read, an appropriate error message will be returned.",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The absolute or relative path of the file to read. Use forward slashes (/) for path separation, even on Windows systems."
}
},
"required": ["path"]
}
},
{
"name": "read_multiple_files",
"description": "Read the contents of multiple files at the specified paths. This tool should be used when you need to examine the contents of multiple existing files at once. It will return the status of reading each file, and store the contents of successfully read files in the system prompt. If a file doesn't exist or can't be read, an appropriate error message will be returned for that file.",
"input_schema": {
"type": "object",
"properties": {
"paths": {
"type": "array",
"items": {
"type": "string"
},
"description": "An array of absolute or relative paths of the files to read. Use forward slashes (/) for path separation, even on Windows systems."
}
},
"required": ["paths"]
}
},
{
"name": "list_files",
"description": "List all files and directories in the specified folder. This tool should be used when you need to see the contents of a directory. It will return a list of all files and subdirectories in the specified path. If the directory doesn't exist or can't be read, an appropriate error message will be returned.",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The absolute or relative path of the folder to list. Use forward slashes (/) for path separation, even on Windows systems. If not provided, the current working directory will be used."
}
}
}
},
{
"name": "tavily_search",
"description": "Perform a web search using the Tavily API to get up-to-date information or additional context. This tool should be used when you need current information or feel a search could provide a better answer to the user's query. It will return a summary of the search results, including relevant snippets and source URLs.",
"input_schema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query. Be as specific and detailed as possible to get the most relevant results."
}
},
"required": ["query"]
}
}
]
from typing import Dict, Any
async def execute_tool(tool_name: str, tool_input: Dict[str, Any]) -> Dict[str, Any]:
try:
result = None
is_error = False
if tool_name == "create_folder":
result = create_folder(tool_input["path"])
elif tool_name == "create_file":
result = create_file(tool_input["path"], tool_input.get("content", ""))
elif tool_name == "edit_and_apply":
result = await edit_and_apply(
tool_input["path"],
tool_input["instructions"],
tool_input["project_context"],
is_automode=automode
)
elif tool_name == "read_file":
result = read_file(tool_input["path"])
elif tool_name == "read_multiple_files":
result = read_multiple_files(tool_input["paths"])
elif tool_name == "list_files":
result = list_files(tool_input.get("path", "."))
elif tool_name == "tavily_search":
result = tavily_search(tool_input["query"])
elif tool_name == "stop_process":
result = stop_process(tool_input["process_id"])
elif tool_name == "execute_code":
process_id, execution_result = await execute_code(tool_input["code"])
analysis_task = asyncio.create_task(send_to_ai_for_executing(tool_input["code"], execution_result))
analysis = await analysis_task
result = f"{execution_result}\n\nAnalysis:\n{analysis}"
if process_id in running_processes:
result += "\n\nNote: The process is still running in the background."
else:
is_error = True
result = f"Unknown tool: {tool_name}"
return {
"content": result,
"is_error": is_error
}
except KeyError as e:
logging.error(f"Missing required parameter {str(e)} for tool {tool_name}")
return {
"content": f"Error: Missing required parameter {str(e)} for tool {tool_name}",
"is_error": True
}
except Exception as e:
logging.error(f"Error executing tool {tool_name}: {str(e)}")
return {
"content": f"Error executing tool {tool_name}: {str(e)}",
"is_error": True
}
def encode_image_to_base64(image_path):
try:
with Image.open(image_path) as img:
max_size = (1024, 1024)
img.thumbnail(max_size, Image.DEFAULT_STRATEGY)
if img.mode != 'RGB':
img = img.convert('RGB')
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='JPEG')
return base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')
except Exception as e:
return f"Error encoding image: {str(e)}"
async def send_to_ai_for_executing(code, execution_result):
global code_execution_tokens
try:
system_prompt = f"""
You are an AI code execution agent. Your task is to analyze the provided code and its execution result from the 'code_execution_env' virtual environment, then provide a concise summary of what worked, what didn't work, and any important observations. Follow these steps:
1. Review the code that was executed in the 'code_execution_env' virtual environment:
{code}
2. Analyze the execution result from the 'code_execution_env' virtual environment:
{execution_result}
3. Provide a brief summary of:
- What parts of the code executed successfully in the virtual environment
- Any errors or unexpected behavior encountered in the virtual environment
- Potential improvements or fixes for issues, considering the isolated nature of the environment
- Any important observations about the code's performance or output within the virtual environment
- If the execution timed out, explain what this might mean (e.g., long-running process, infinite loop)
Be concise and focus on the most important aspects of the code execution within the 'code_execution_env' virtual environment.
IMPORTANT: PROVIDE ONLY YOUR ANALYSIS AND OBSERVATIONS. DO NOT INCLUDE ANY PREFACING STATEMENTS OR EXPLANATIONS OF YOUR ROLE.
"""
response = client.messages.create(
model=CODEEXECUTIONMODEL,
max_tokens=2000,
system=system_prompt,
messages=[
{"role": "user", "content": f"Analyze this code execution from the 'code_execution_env' virtual environment:\n\nCode:\n{code}\n\nExecution Result:\n{execution_result}"}
]
)
# Update token usage for code execution
code_execution_tokens['input'] += response.usage.input_tokens
code_execution_tokens['output'] += response.usage.output_tokens
analysis = response.content[0].text
return analysis
except Exception as e:
console.print(f"Error in AI code execution analysis: {str(e)}", style="bold red")
return f"Error analyzing code execution from 'code_execution_env': {str(e)}"
def save_chat():
# Generate filename
now = datetime.datetime.now()
filename = f"Chat_{now.strftime('%H%M')}.md"
# Format conversation history
formatted_chat = "# Claude-3-Sonnet Engineer Chat Log\n\n"
for message in conversation_history:
if message['role'] == 'user':
formatted_chat += f"## User\n\n{message['content']}\n\n"
elif message['role'] == 'assistant':
if isinstance(message['content'], str):
formatted_chat += f"## Claude\n\n{message['content']}\n\n"
elif isinstance(message['content'], list):
for content in message['content']:
if content['type'] == 'tool_use':
formatted_chat += f"### Tool Use: {content['name']}\n\n```json\n{json.dumps(content['input'], indent=2)}\n```\n\n"
elif content['type'] == 'text':
formatted_chat += f"## Claude\n\n{content['text']}\n\n"
elif message['role'] == 'user' and isinstance(message['content'], list):
for content in message['content']:
if content['type'] == 'tool_result':
formatted_chat += f"### Tool Result\n\n```\n{content['content']}\n```\n\n"
# Save to file
with open(filename, 'w', encoding='utf-8') as f:
f.write(formatted_chat)
return filename
async def chat_with_claude(user_input, image_path=None, current_iteration=None, max_iterations=None):
global conversation_history, automode, main_model_tokens
# This function uses MAINMODEL, which maintains context across calls
current_conversation = []
if image_path:
console.print(Panel(f"Processing image at path: {image_path}", title_align="left", title="Image Processing", expand=False, style="yellow"))
image_base64 = encode_image_to_base64(image_path)
if image_base64.startswith("Error"):
console.print(Panel(f"Error encoding image: {image_base64}", title="Error", style="bold red"))
return "I'm sorry, there was an error processing the image. Please try again.", False
image_message = {
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": image_base64
}
},
{
"type": "text",
"text": f"User input for image: {user_input}"
}
]
}
current_conversation.append(image_message)
console.print(Panel("Image message added to conversation history", title_align="left", title="Image Added", style="green"))
else:
current_conversation.append({"role": "user", "content": user_input})
# Filter conversation history to maintain context
filtered_conversation_history = []
for message in conversation_history:
if isinstance(message['content'], list):
filtered_content = [
content for content in message['content']
if content.get('type') != 'tool_result' or (
content.get('type') == 'tool_result' and
not any(keyword in content.get('output', '') for keyword in [
"File contents updated in system prompt",
"File created and added to system prompt",
"has been read and stored in the system prompt"
])
)
]
if filtered_content:
filtered_conversation_history.append({**message, 'content': filtered_content})
else:
filtered_conversation_history.append(message)
# Combine filtered history with current conversation to maintain context
messages = filtered_conversation_history + current_conversation
try:
# MAINMODEL call, which maintains context
response = client.messages.create(
model=MAINMODEL,
max_tokens=8000,
system=update_system_prompt(current_iteration, max_iterations),
extra_headers={"anthropic-beta": "max-tokens-3-5-sonnet-2024-07-15"},
messages=messages,
tools=tools,
tool_choice={"type": "auto"}
)
# Update token usage for MAINMODEL
main_model_tokens['input'] += response.usage.input_tokens
main_model_tokens['output'] += response.usage.output_tokens
except APIStatusError as e:
if e.status_code == 429:
console.print(Panel("Rate limit exceeded. Retrying after a short delay...", title="API Error", style="bold yellow"))
time.sleep(5)
return await chat_with_claude(user_input, image_path, current_iteration, max_iterations)
else:
console.print(Panel(f"API Error: {str(e)}", title="API Error", style="bold red"))
return "I'm sorry, there was an error communicating with the AI. Please try again.", False
except APIError as e:
console.print(Panel(f"API Error: {str(e)}", title="API Error", style="bold red"))
return "I'm sorry, there was an error communicating with the AI. Please try again.", False
assistant_response = ""
exit_continuation = False
tool_uses = []
for content_block in response.content:
if content_block.type == "text":
assistant_response += content_block.text
if CONTINUATION_EXIT_PHRASE in content_block.text:
exit_continuation = True
elif content_block.type == "tool_use":
tool_uses.append(content_block)
console.print(Panel(Markdown(assistant_response), title="Claude's Response", title_align="left", border_style="blue", expand=False))
# Display files in context
if file_contents:
files_in_context = "\n".join(file_contents.keys())
else:
files_in_context = "No files in context. Read, create, or edit files to add."
console.print(Panel(files_in_context, title="Files in Context", title_align="left", border_style="white", expand=False))
for tool_use in tool_uses:
tool_name = tool_use.name
tool_input = tool_use.input
tool_use_id = tool_use.id
console.print(Panel(f"Tool Used: {tool_name}", style="green"))
console.print(Panel(f"Tool Input: {json.dumps(tool_input, indent=2)}", style="green"))
tool_result = await execute_tool(tool_name, tool_input)
if tool_result["is_error"]:
console.print(Panel(tool_result["content"], title="Tool Execution Error", style="bold red"))
else:
console.print(Panel(tool_result["content"], title_align="left", title="Tool Result", style="green"))
current_conversation.append({
"role": "assistant",
"content": [
{
"type": "tool_use",
"id": tool_use_id,
"name": tool_name,
"input": tool_input
}
]
})
current_conversation.append({
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": tool_use_id,
"content": tool_result["content"],
"is_error": tool_result["is_error"]
}
]
})
# Update the file_contents dictionary if applicable
if tool_name in ['create_file', 'edit_and_apply', 'read_file'] and not tool_result["is_error"]:
if 'path' in tool_input:
file_path = tool_input['path']