-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm.py
More file actions
418 lines (342 loc) · 15.1 KB
/
Copy pathllm.py
File metadata and controls
418 lines (342 loc) · 15.1 KB
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
# claude_handler.py
from anthropic import Anthropic
from openai import OpenAI
from typing import List, Dict, Any, Union
import json
import mcp_connections
# Global conversation history and API type
conversation_history = []
api_type = None # 'anthropic' or 'openrouter'
# System prompt - ENHANCED
SYSTEM_PROMPT = """You are an AI Development Assistant that helps developers with their workflow.
You have access to:
- GitHub tools: Create repos, issues, PRs, manage files, search code, clone repositories
- Docker Code Execution: Execute Python/Bash code in a sandboxed environment, create/read/write files, install packages
- Render tools: Deploy and manage services on Render
- Vercel tools: Deploy projects to Vercel, list projects/deployments, view logs, manage env vars
Your capabilities:
1. Code Management: Clone repos, create branches, commit changes, push code
2. Issue Tracking: Create/update issues, link to PRs
3. Code Execution: Run Python scripts, execute bash commands, install packages
4. File Operations: Create, read, write, list files in the sandbox
5. Data Processing: Install and use libraries like pandas, numpy, matplotlib
6. Testing: Run tests, linters, formatters
7. Vercel Deployment: Deploy projects (preview/production), list projects, view deployment logs, inspect deployments, manage environment variables
8. Render Deployment: Deploy and manage services on Render
CRITICAL RULES FOR TASK COMPLETION:
1. ALWAYS complete the ENTIRE task before responding with just text
2. If asked to "create and run" - do BOTH: create file, THEN execute it
3. If asked to "install and use" - do BOTH: install package, THEN use it
4. If asked to "analyze data" - download, process, AND show results
5. Use multiple tools in sequence to complete multi-step tasks
6. After each tool use, check: "Is the task fully complete?" If NO, use more tools
7. Only respond with text when the ENTIRE task is done
Examples of CORRECT behavior:
- User: "Create hello.py and run it"
→ write_file (create hello.py)
→ execute_python (run hello.py)
→ Respond: "Created and ran hello.py. Output: Hello World"
- User: "Install requests and fetch data from an API"
→ execute_bash (pip install requests)
→ execute_python (script that uses requests)
→ Respond: "Installed requests and fetched data: [results]"
Examples of WRONG behavior:
- User: "Create hello.py and run it"
→ write_file (create hello.py)
→ Respond: "I've created hello.py" WRONG - didn't run it!
Guidelines:
- Break down tasks into steps
- Execute ALL steps before giving final response
- Always show outputs from code execution
- Handle errors gracefully and suggest fixes
- Confirm destructive operations before executing"""
def init_claude(api_key: str) -> Anthropic:
"""Initialize Claude client with Anthropic API"""
global api_type
api_type = 'anthropic'
return Anthropic(api_key=api_key)
def init_openrouter(api_key: str, site_url: str = "http://localhost:3000", site_name: str = "CodeBuddy MCP") -> OpenAI:
"""Initialize OpenRouter client using OpenAI library"""
global api_type
api_type = 'openrouter'
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=api_key,
default_headers={
"HTTP-Referer": site_url,
"X-Title": site_name,
}
)
return client
async def send_message(
client: Union[Anthropic, OpenAI],
user_message: str,
max_tool_rounds: int = 20,
model: str = None
) -> str:
"""Send message to LLM and handle tool calls until task is complete"""
global conversation_history, api_type
print(f"\n👤 You: {user_message}\n")
# Add user message to history
conversation_history.append({
"role": "user",
"content": user_message
})
# Get available tools from MCP and E2B
tools = await mcp_connections.get_all_tools_for_claude()
# Track tool rounds to prevent infinite loops
tool_round = 0
# Set default model based on API type
if model is None:
if api_type == 'anthropic':
model = "claude-sonnet-4-20250514"
else: # openrouter
model = "anthropic/claude-sonnet-4-20250514"
# Call LLM based on API type
if api_type == 'anthropic':
response = client.messages.create(
model=model,
max_tokens=4096,
system=SYSTEM_PROMPT,
tools=tools,
messages=conversation_history
)
else: # openrouter
# Convert conversation history to OpenAI format
openai_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
openai_messages.extend(conversation_history)
response = client.chat.completions.create(
model=model,
max_tokens=4096,
tools=convert_tools_to_openai_format(tools) if tools else None,
messages=openai_messages
)
# Debug: show what the model returned
if api_type == 'openrouter':
print(f"🔍 Debug - finish_reason: {response.choices[0].finish_reason}")
print(f"🔍 Debug - tool_calls: {response.choices[0].message.tool_calls}")
print(f"🔍 Debug - total tools sent: {len(tools)}")
# Handle tool use loop - KEEP GOING UNTIL LLM STOPS
should_continue = (api_type == 'anthropic' and response.stop_reason == "tool_use") or \
(api_type == 'openrouter' and response.choices[0].finish_reason == "tool_calls")
while should_continue:
tool_round += 1
# Safety check: prevent infinite loops
if tool_round > max_tool_rounds:
print(f" Warning: Reached maximum tool rounds ({max_tool_rounds})")
print(" Forcing completion to prevent infinite loop")
break
print(f" AI is using tools... (Round {tool_round})\n")
assistant_content = []
tool_results = []
if api_type == 'anthropic':
# Process Anthropic response
for content_block in response.content:
if content_block.type == "text":
if content_block.text.strip():
print(f"💭 AI: {content_block.text}\n")
assistant_content.append(content_block)
elif content_block.type == "tool_use":
assistant_content.append(content_block)
tool_name = content_block.name
tool_input = content_block.input
print(f" Tool #{len(tool_results)+1}: {tool_name}")
print(f" Input: {json.dumps(tool_input, indent=2)}")
try:
result = await mcp_connections.execute_tool(tool_name, tool_input)
result_text = extract_result_text(result)
display_text = result_text[:500] + "..." if len(result_text) > 500 else result_text
print(f" Result: {display_text}\n")
tool_results.append({
"type": "tool_result",
"tool_use_id": content_block.id,
"content": result_text
})
except Exception as e:
error_msg = str(e)
print(f" Error: {error_msg}\n")
tool_results.append({
"type": "tool_result",
"tool_use_id": content_block.id,
"content": f"Error executing tool: {error_msg}",
"is_error": True
})
# Add to conversation history
conversation_history.append({
"role": "assistant",
"content": assistant_content
})
conversation_history.append({
"role": "user",
"content": tool_results
})
# Continue conversation
response = client.messages.create(
model=model,
max_tokens=4096,
system=SYSTEM_PROMPT,
tools=tools,
messages=conversation_history
)
should_continue = response.stop_reason == "tool_use"
else: # openrouter
# Process OpenRouter/OpenAI response
message = response.choices[0].message
if message.content:
print(f"💭 AI: {message.content}\n")
# Process tool calls (check if tool_calls exists and is not None)
if not message.tool_calls:
print(" Warning: No tool calls found in response")
break
for tool_call in message.tool_calls:
tool_name = tool_call.function.name
# Handle arguments - could be string or dict
if isinstance(tool_call.function.arguments, str):
tool_input = json.loads(tool_call.function.arguments) if tool_call.function.arguments else {}
else:
tool_input = tool_call.function.arguments or {}
print(f" Tool #{len(tool_results)+1}: {tool_name}")
print(f" Input: {json.dumps(tool_input, indent=2)}")
try:
result = await mcp_connections.execute_tool(tool_name, tool_input)
result_text = extract_result_text(result)
display_text = result_text[:500] + "..." if len(result_text) > 500 else result_text
print(f" Result: {display_text}\n")
tool_results.append({
"role": "tool",
"tool_call_id": tool_call.id,
"name": tool_name,
"content": result_text
})
except Exception as e:
error_msg = str(e)
print(f" Error: {error_msg}\n")
tool_results.append({
"role": "tool",
"tool_call_id": tool_call.id,
"name": tool_name,
"content": f"Error executing tool: {error_msg}"
})
# Add to conversation history (OpenAI format)
assistant_msg = {
"role": "assistant",
"content": message.content
}
if message.tool_calls:
assistant_msg["tool_calls"] = [
{
"id": tc.id,
"type": "function",
"function": {
"name": tc.function.name,
"arguments": tc.function.arguments
}
} for tc in message.tool_calls
]
conversation_history.append(assistant_msg)
# Add tool results
conversation_history.extend(tool_results)
# Continue conversation
openai_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
openai_messages.extend(conversation_history)
response = client.chat.completions.create(
model=model,
max_tokens=4096,
tools=convert_tools_to_openai_format(tools) if tools else None,
messages=openai_messages
)
should_continue = response.choices[0].finish_reason == "tool_calls"
# Show progress
print(f"{'='*60}")
print(f"Completed tool round {tool_round}")
print(f"AI's decision: {response.stop_reason if api_type == 'anthropic' else response.choices[0].finish_reason}")
print(f"{'='*60}\n")
# Extract final response
final_response = ""
if api_type == 'anthropic':
for content_block in response.content:
if hasattr(content_block, "text"):
final_response += content_block.text
# Add to history
conversation_history.append({
"role": "assistant",
"content": response.content
})
else: # openrouter
final_response = response.choices[0].message.content or ""
# Add to history
conversation_history.append({
"role": "assistant",
"content": final_response
})
# Show completion summary
if tool_round > 0:
print(f" Task completed after {tool_round} tool rounds\n")
return final_response
def convert_tools_to_openai_format(anthropic_tools: List[Dict]) -> List[Dict]:
"""Convert Anthropic tool format to OpenAI function calling format"""
openai_tools = []
for tool in anthropic_tools:
openai_tool = {
"type": "function",
"function": {
"name": tool["name"],
"description": tool["description"],
"parameters": tool["input_schema"]
}
}
openai_tools.append(openai_tool)
return openai_tools
def extract_result_text(result: Any) -> str:
"""Extract text from MCP or E2B result"""
# Handle MCP result format
if hasattr(result, 'content'):
if isinstance(result.content, list):
text_parts = []
for item in result.content:
if hasattr(item, 'text'):
text_parts.append(str(item.text))
else:
text_parts.append(str(item))
return "\n".join(text_parts)
else:
return str(result.content)
# Handle direct string
if isinstance(result, str):
return result
# Handle dict format (from E2B wrapper)
if isinstance(result, dict) and 'content' in result:
return str(result['content'])
# Fallback
return str(result)
def clear_history():
"""Clear conversation history"""
global conversation_history
conversation_history = []
print(" Conversation history cleared")
def get_conversation_length() -> int:
"""Get number of messages in conversation"""
return len(conversation_history)
def print_conversation_stats():
"""Print statistics about current conversation"""
total_messages = len(conversation_history)
user_messages = sum(1 for msg in conversation_history if msg['role'] == 'user')
assistant_messages = sum(1 for msg in conversation_history if msg['role'] == 'assistant')
# Count tool uses
tool_uses = 0
for msg in conversation_history:
if msg['role'] == 'assistant' and isinstance(msg.get('content'), list):
tool_uses += sum(1 for item in msg['content']
if hasattr(item, 'type') and item.type == 'tool_use')
print(f"\n Conversation Stats:")
print(f" Total messages: {total_messages}")
print(f" User messages: {user_messages}")
print(f" Assistant messages: {assistant_messages}")
print(f" Tool uses: {tool_uses}")
def truncate_history(keep_last_n: int):
"""Keep only the last n messages in history to manage token usage"""
global conversation_history
if len(conversation_history) > keep_last_n:
removed = len(conversation_history) - keep_last_n
conversation_history = conversation_history[-keep_last_n:]
print(f" Truncated history: removed {removed} old messages, kept last {keep_last_n}")