forked from Doriandarko/claude-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
610 lines (526 loc) · 24.9 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
import os
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
console = Console()
# Add these constants at the top of the file
CONTINUATION_EXIT_PHRASE = "AUTOMODE_COMPLETE"
MAX_CONTINUATION_ITERATIONS = 25
# Models to use
MAINMODEL = "claude-3-5-sonnet-20240620"
TOOLCHECKERMODEL = "claude-3-5-sonnet-20240620"
# Initialize the Anthropic client
client = Anthropic(api_key="YOUR KEY")
# Initialize the Tavily client
tavily = TavilyClient(api_key="YOUR KEY")
# Set up the conversation memory
conversation_history = []
# automode flag
automode = False
# base prompt
base_system_prompt = """
You are Claude, an AI assistant powered by Anthropic's Claude-3.5-Sonnet model, specializing in software development. 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
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.
3. edit_and_apply: Examine and modify existing files.
4. read_file: View the contents of existing files without making changes.
5. list_files: Understand the current project structure or locate specific files.
6. tavily_search: Obtain current information on technologies, libraries, or best practices.
7. Analyzing images provided by the user
Tool Usage Guidelines:
- Always use the most appropriate tool for the task at hand.
- For file modifications, use edit_and_apply. Read the file first, then apply changes if needed.
- When editing files, apply changes in chunks for large modifications.
- After making changes, always review the diff output to ensure accuracy.
- Proactively use tavily_search when you need up-to-date information or context.
Error Handling and Recovery:
- If a tool operation fails, analyze the error message and attempt to resolve the issue.
- For file-related errors, check file paths and permissions before retrying.
- If a search fails, try rephrasing the query or breaking it into smaller, more specific searches.
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.
Code Editing Best Practices:
1. Always read the file content before making changes.
2. Analyze the code and determine necessary modifications.
3. Make changes incrementally, especially for large files.
4. Pay close attention to existing code structure to avoid unintended alterations.
5. Review changes thoroughly after each modification.
Always strive for accuracy, clarity, and efficiency in your responses and actions. If uncertain, use the tavily_search tool or admit your limitations.
"""
# Auto mode-specific system prompt
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=None, max_iterations=None):
global base_system_prompt, automode_system_prompt
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.
"""
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 + "\n\n" + automode_system_prompt.format(iteration_info=iteration_info) + "\n\n" + chain_of_thought_prompt
else:
return base_system_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=""):
try:
with open(path, 'w') as f:
f.write(content)
return f"File created: {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)
def generate_and_apply_diff(original_content, new_content, path):
diff = list(difflib.unified_diff(
original_content.splitlines(keepends=True),
new_content.splitlines(keepends=True),
fromfile=f"a/{path}",
tofile=f"b/{path}",
n=3
))
if not diff:
return "No changes detected."
try:
with open(path, 'w') as f:
f.writelines(new_content)
diff_text = ''.join(diff)
highlighted_diff = highlight_diff(diff_text)
diff_panel = Panel(
highlighted_diff,
title=f"Changes in {path}",
expand=False,
border_style="cyan"
)
console.print(diff_panel)
added_lines = sum(1 for line in diff if line.startswith('+') and not line.startswith('+++'))
removed_lines = sum(1 for line in diff if line.startswith('-') and not line.startswith('---'))
summary = f"Changes applied to {path}:\n"
summary += f" Lines added: {added_lines}\n"
summary += f" Lines removed: {removed_lines}\n"
return summary
except Exception as e:
error_panel = Panel(
f"Error: {str(e)}",
title="Error Applying Changes",
style="bold red"
)
console.print(error_panel)
return f"Error applying changes: {str(e)}"
# Update the edit_file function
def edit_and_apply(path, new_content):
try:
with open(path, 'r') as file:
original_content = file.read()
if new_content != original_content:
diff_result = generate_and_apply_diff(original_content, new_content, path)
return f"Changes applied to {path}:\n{diff_result}"
else:
return f"No changes needed for {path}"
except Exception as e:
return f"Error editing/applying to file: {str(e)}"
def read_file(path):
try:
with open(path, 'r') as f:
content = f.read()
return content
except Exception as e:
return f"Error reading file: {str(e)}"
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)}"
tools = [
{
"name": "create_folder",
"description": "Create a new folder at the specified path. Use this when you need to create a new directory in the project structure.",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The path where the folder should be created"
}
},
"required": ["path"]
}
},
{
"name": "create_file",
"description": "Create a new file at the specified path with content. Use this when you need to create a new file in the project structure.",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The path where the file should be created"
},
"content": {
"type": "string",
"description": "The content of the file"
}
},
"required": ["path", "content"]
}
},
{
"name": "search_file",
"description": "Search for a specific pattern in a file and return the line numbers where the pattern is found. Use this to locate specific code or text within a file.",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The path of the file to search"
},
"search_pattern": {
"type": "string",
"description": "The pattern to search for in the file"
}
},
"required": ["path", "search_pattern"]
}
},
{
"name": "edit_and_apply",
"description": "Apply changes to a file. Use this when you need to edit a file.",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The path of the file to edit"
},
"new_content": {
"type": "string",
"description": "The new content to apply to the file"
}
},
"required": ["path", "new_content"]
}
},
{
"name": "read_file",
"description": "Read the contents of a file at the specified path. Use this when you need to examine the contents of an existing file.",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The path of the file to read"
}
},
"required": ["path"]
}
},
{
"name": "list_files",
"description": "List all files and directories in the specified folder. Use this when you need to see the contents of a directory.",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "The path of the folder to list (default: current directory)"
}
}
}
},
{
"name": "tavily_search",
"description": "Perform a web search using Tavily API to get up-to-date information or additional context. Use this when you need current information or feel a search could provide a better answer.",
"input_schema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query"
}
},
"required": ["query"]
}
}
]
# Update the execute_tool function
def execute_tool(tool_name, tool_input):
try:
if tool_name == "create_folder":
return create_folder(tool_input["path"])
elif tool_name == "create_file":
return create_file(tool_input["path"], tool_input.get("content", ""))
elif tool_name == "edit_and_apply":
return edit_and_apply(tool_input["path"], tool_input.get("new_content"))
elif tool_name == "read_file":
return read_file(tool_input["path"])
elif tool_name == "list_files":
return list_files(tool_input.get("path", "."))
elif tool_name == "tavily_search":
return tavily_search(tool_input["query"])
else:
return f"Unknown tool: {tool_name}"
except KeyError as e:
return f"Error: Missing required parameter {str(e)} for tool {tool_name}"
except Exception as e:
return f"Error executing tool {tool_name}: {str(e)}"
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)}"
def parse_goals(response):
goals = re.findall(r'Goal \d+: (.+)', response)
return goals
def execute_goals(goals):
global automode
for i, goal in enumerate(goals, 1):
console.print(Panel(f"Executing Goal {i}: {goal}", title="Goal Execution", style="bold yellow"))
response, _ = chat_with_claude(f"Continue working on goal: {goal}")
if CONTINUATION_EXIT_PHRASE in response:
automode = False
console.print(Panel("Exiting automode.", title="Automode", style="bold green"))
break
def chat_with_claude(user_input, image_path=None, current_iteration=None, max_iterations=None):
global conversation_history, automode
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})
messages = conversation_history + current_conversation
try:
response = client.messages.create(
model=MAINMODEL,
max_tokens=4000,
system=update_system_prompt(current_iteration, max_iterations),
messages=messages,
tools=tools,
tool_choice={"type": "auto"}
)
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 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", 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"))
try:
result = execute_tool(tool_name, tool_input)
console.print(Panel(result, title_align="left", title="Tool Result", style="green"))
except Exception as e:
result = f"Error executing tool: {str(e)}"
console.print(Panel(result, title="Tool Execution Error", style="bold red"))
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": result
}
]
})
messages = conversation_history + current_conversation
try:
tool_response = client.messages.create(
model=TOOLCHECKERMODEL,
max_tokens=4000,
system=update_system_prompt(current_iteration, max_iterations),
messages=messages,
tools=tools,
tool_choice={"type": "auto"}
)
tool_checker_response = ""
for tool_content_block in tool_response.content:
if tool_content_block.type == "text":
tool_checker_response += tool_content_block.text
console.print(Panel(Markdown(tool_checker_response), title="Claude's Response to Tool Result", title_align="left"))
assistant_response += "\n\n" + tool_checker_response
except APIError as e:
error_message = f"Error in tool response: {str(e)}"
console.print(Panel(error_message, title="Error", style="bold red"))
assistant_response += f"\n\n{error_message}"
if assistant_response:
current_conversation.append({"role": "assistant", "content": assistant_response})
conversation_history = messages + [{"role": "assistant", "content": assistant_response}]
return assistant_response, exit_continuation
def main():
global automode, conversation_history
console.print(Panel("Welcome to the Claude-3-Sonnet Engineer Chat with Image Support!", title="Welcome", style="bold green"))
console.print("Type 'exit' to end the conversation.")
console.print("Type 'image' to include an image in your message.")
console.print("Type 'automode [number]' to enter Autonomous mode with a specific number of iterations.")
console.print("While in automode, press Ctrl+C at any time to exit the automode to return to regular chat.")
while True:
user_input = console.input("[bold cyan]You:[/bold cyan] ")
if user_input.lower() == 'exit':
console.print(Panel("Thank you for chatting. Goodbye!", title_align="left", title="Goodbye", style="bold green"))
break
if user_input.lower() == 'image':
image_path = console.input("[bold cyan]Drag and drop your image here, then press enter:[/bold cyan] ").strip().replace("'", "")
if os.path.isfile(image_path):
user_input = console.input("[bold cyan]You (prompt for image):[/bold cyan] ")
response, _ = chat_with_claude(user_input, image_path)
else:
console.print(Panel("Invalid image path. Please try again.", title="Error", style="bold red"))
continue
elif user_input.lower().startswith('automode'):
try:
parts = user_input.split()
if len(parts) > 1 and parts[1].isdigit():
max_iterations = int(parts[1])
else:
max_iterations = MAX_CONTINUATION_ITERATIONS
automode = True
console.print(Panel(f"Entering automode with {max_iterations} iterations. Please provide the goal of the automode.", title_align="left", title="Automode", style="bold yellow"))
console.print(Panel("Press Ctrl+C at any time to exit the automode loop.", style="bold yellow"))
user_input = console.input("[bold cyan]You:[/bold cyan] ")
iteration_count = 0
try:
while automode and iteration_count < max_iterations:
response, exit_continuation = chat_with_claude(user_input, current_iteration=iteration_count+1, max_iterations=max_iterations)
if exit_continuation or CONTINUATION_EXIT_PHRASE in response:
console.print(Panel("Automode completed.", title_align="left", title="Automode", style="green"))
automode = False
else:
console.print(Panel(f"Continuation iteration {iteration_count + 1} completed. Press Ctrl+C to exit automode. ", title_align="left", title="Automode", style="yellow"))
user_input = "Continue with the next step. Or STOP by saying 'AUTOMODE_COMPLETE' if you think you've achieved the results established in the original request."
iteration_count += 1
if iteration_count >= max_iterations:
console.print(Panel("Max iterations reached. Exiting automode.", title_align="left", title="Automode", style="bold red"))
automode = False
except KeyboardInterrupt:
console.print(Panel("\nAutomode interrupted by user. Exiting automode.", title_align="left", title="Automode", style="bold red"))
automode = False
if conversation_history and conversation_history[-1]["role"] == "user":
conversation_history.append({"role": "assistant", "content": "Automode interrupted. How can I assist you further?"})
except KeyboardInterrupt:
console.print(Panel("\nAutomode interrupted by user. Exiting automode.", title_align="left", title="Automode", style="bold red"))
automode = False
if conversation_history and conversation_history[-1]["role"] == "user":
conversation_history.append({"role": "assistant", "content": "Automode interrupted. How can I assist you further?"})
console.print(Panel("Exited automode. Returning to regular chat.", style="green"))
else:
response, _ = chat_with_claude(user_input)
if __name__ == "__main__":
main()