-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
326 lines (262 loc) · 10.6 KB
/
utils.py
File metadata and controls
326 lines (262 loc) · 10.6 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
import os
import json
import sys
from datetime import datetime
def get_config_dir():
"""Returns a persistent application folder compatible with all OS."""
if os.name == "nt": # Windows
base = os.environ.get("LOCALAPPDATA") or os.path.expanduser("~")
return os.path.join(base, "operafor")
else: # macOS, Linux, others
return os.path.join(os.path.expanduser("~"), ".operafor")
# Determine paths based on whether we're running as a packaged app
if hasattr(sys, '_MEIPASS'):
DATA_DIR = get_config_dir()
os.makedirs(DATA_DIR, exist_ok=True)
CONFIG_PATH = os.path.join(DATA_DIR, "config.json")
CONV_FILE = os.path.join(DATA_DIR, "sandboxes.json")
SANDBOXES_DIR = os.path.join(DATA_DIR, "sandboxes")
else:
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_PATH = os.path.join(BASE_DIR, "config.json")
CONV_FILE = os.path.join(BASE_DIR, "sandboxes.json")
SANDBOXES_DIR = os.path.join(BASE_DIR, "sandboxes")
# Default configuration
DEFAULT_CONFIG = {
"llm": {
"endpoint": "https://openrouter.ai/api/v1",
"model": "deepseek/deepseek-chat",
"apiKey": "your_api_key"
},
"proxy": {
"enabled": False,
"http": "",
"https": ""
},
"context_management": {
"enabled": True,
"strategy": "hybrid",
"max_tokens": 4000,
"summarization_threshold": 1500, # Reduced from 3000 for earlier compression
"preserve_recent_messages": 3, # Reduced from 5 to keep less context
"preserve_system_prompt": True,
"max_context_during_run": 20000, # Reduced from 100000 to trigger more often
"max_tool_result_chars": 2000, # Truncate tool results to 2000 chars
"preserve_latest_tool_results": 1 # Number of latest tool results to preserve uncompressed
}
}
def get_proxies(config: dict, url: str) -> dict:
"""
Returns a proxy dictionary for requests if proxy is enabled and URL is not localhost.
Args:
config: The application configuration dictionary
url: The URL to check for proxy application
Returns:
A dictionary containing proxy settings or None if proxy should not be used
"""
proxy_config = config.get("proxy", {})
if not proxy_config.get("enabled", False):
return None
# Check for localhost
from urllib.parse import urlparse
parsed = urlparse(url)
hostname = parsed.hostname
if hostname in ["localhost", "127.0.0.1"]:
return None
proxies = {}
if proxy_config.get("http"):
proxies["http"] = proxy_config["http"]
if proxy_config.get("https"):
proxies["https"] = proxy_config["https"]
return proxies if proxies else None
# --- Git utilities ---
def init_or_get_repo(sandbox_path: str):
"""Initialize git repository in sandbox folder if it doesn't exist, or get existing repo."""
from dulwich import porcelain
from dulwich.repo import Repo
if not os.path.exists(sandbox_path):
os.makedirs(sandbox_path)
git_path = os.path.join(sandbox_path, '.git')
if not os.path.exists(git_path):
repo = porcelain.init(sandbox_path)
else:
repo = Repo(sandbox_path)
return repo
def write_conversation_json(sandbox_path: str, messages: list) -> str:
"""Write the conversation messages to conversation.json in the sandbox folder."""
conversation_path = os.path.join(sandbox_path, 'conversation.json')
with open(conversation_path, 'w') as f:
json.dump({"messages": messages}, f, indent=2)
return conversation_path
def commit_sandbox_changes(sandbox_id: str, step: int, commit_message: str) -> str:
"""Commit all changes in the sandbox folder and update sandboxes.json."""
from dulwich import porcelain
from datetime import datetime
sandbox_path = get_sandbox_path(sandbox_id)
repo = init_or_get_repo(sandbox_path)
# Load conversation to get current messages
sandboxes = load_all_sandboxes()
conv = sandboxes.get(sandbox_id)
if not conv:
print(f"Error: Sandbox {sandbox_id} not found when committing.")
return ""
messages = conv.get("messages", [])
# Write conversation.json into the sandbox folder for self-containment
write_conversation_json(sandbox_path, messages)
# Add all files in the sandbox folder
for root, dirs, files in os.walk(sandbox_path):
# Skip .git directory
if '.git' in dirs:
dirs.remove('.git')
for file in files:
file_path = os.path.join(root, file)
rel_path = os.path.relpath(file_path, sandbox_path)
try:
porcelain.add(sandbox_path, rel_path)
except Exception as e:
print(f"Warning: Could not add file {rel_path}: {e}")
# Commit changes
try:
commit_hash_bytes = porcelain.commit(sandbox_path, commit_message.encode('utf-8'))
commit_hash = commit_hash_bytes.decode('utf-8')
# Update commits list in sandboxes.json
commits = conv.setdefault("commits", [])
commit_info = {
"hash": commit_hash,
"step": step,
"message": commit_message,
"timestamp": datetime.now().isoformat()
}
commits.append(commit_info)
# Save updated sandboxes.json
sandboxes[sandbox_id] = conv
save_all_sandboxes(sandboxes)
return commit_hash
except Exception as e:
# porcelain.commit can fail if there's nothing to commit
if "nothing to commit" in str(e).lower():
# If nothing to commit, we might still want to record the step if it's important,
# but usually we only care about changes.
# However, for the user's requirement "commit after each tool call that make a modification",
# if we call it and nothing changed, it's fine to just return empty.
return ""
print(f"Error committing changes: {e}")
return ""
def revert_sandbox_to_commit(sandbox_path: str, commit_hash: str) -> bool:
"""Revert the sandbox to a specific commit."""
from dulwich import porcelain
try:
porcelain.reset(sandbox_path, "hard", commit_hash)
# Update sandboxes.json
with open(CONV_FILE, 'r') as f:
sandboxes = json.load(f)
sandbox_id = os.path.basename(sandbox_path)
conv = sandboxes.get(sandbox_id)
if conv is not None:
commits = conv.get("commits", [])
idx = next((i for i, c in enumerate(commits) if c["hash"] == commit_hash), None)
if idx is not None:
conv["commits"] = commits[:idx+1]
sandboxes[sandbox_id] = conv
with open(CONV_FILE, 'w') as f:
json.dump(sandboxes, f, indent=2)
return True
except Exception as e:
print(f"Error reverting to commit {commit_hash}: {e}")
return False
# --- Sandbox management ---
def load_all_sandboxes():
if not os.path.exists(CONV_FILE):
return {}
with open(CONV_FILE, 'r') as f:
return json.load(f)
def save_all_sandboxes(convs):
with open(CONV_FILE, 'w') as f:
json.dump(convs, f, indent=2)
def get_sandbox_path(sandbox_id: str) -> str:
convs = load_all_sandboxes()
conv = convs.get(sandbox_id)
if conv and conv.get("custom_path"):
return conv["custom_path"]
return os.path.join(SANDBOXES_DIR, sandbox_id)
# --- Model utilities ---
def is_vlm(model_name: str) -> bool:
"""Check if the model has vision capabilities."""
if not model_name:
return False
model_lower = model_name.lower()
vision_keywords = ["vision", "4o", "claude-3", "gemini", "sonnet", "opus", "pixtral", "llavanext"]
return any(keyword in model_lower for keyword in vision_keywords)
# --- Context cache management ---
def clear_sandbox_cache(sandbox_id: str) -> bool:
"""
Clear context cache for a specific sandbox.
Useful for debugging or when cache becomes corrupted.
Returns:
True if cache was cleared successfully, False otherwise
"""
try:
from context_cache import invalidate_cache
invalidate_cache(sandbox_id)
return True
except Exception as e:
print(f"Error clearing cache for sandbox {sandbox_id}: {e}")
return False
def get_cache_stats(sandbox_id: str) -> dict:
"""
Get cache statistics for a sandbox.
Returns:
Dictionary with cache statistics or empty dict if unavailable
"""
try:
from context_cache import load_stats
return load_stats(sandbox_id)
except Exception as e:
print(f"Error loading cache stats for sandbox {sandbox_id}: {e}")
return {}
# --- LLM utilities ---
def call_llm(messages, tools, config):
"""Call LLM with retry logic."""
import time
import requests
endpoint = config.get("llm", {}).get("endpoint", "https://openrouter.ai/api/v1")
if not endpoint.endswith("/chat/completions"):
endpoint = endpoint.rstrip("/") + "/chat/completions"
api_key = config.get("llm", {}).get("apiKey")
model = config.get("llm", {}).get("model")
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
# Some providers 400 if tools is empty list
payload_tools = tools if tools else None
data = {
"model": model,
"messages": messages,
"tools": payload_tools,
"stream": False
}
if not payload_tools:
del data["tools"]
max_retries = 3
for attempt in range(max_retries):
try:
print(f"LLM Call Attempt {attempt+1}/{max_retries}...")
proxies = get_proxies(config, endpoint)
response = requests.post(endpoint, json=data, headers=headers, timeout=60, proxies=proxies)
if response.status_code == 400:
print(f"400 Bad Request Details: {response.text}")
return {"error": f"400 Bad Request: {response.text}"}
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"LLM Call Error (Attempt {attempt+1}): {e}")
if getattr(e, 'response', None):
print(f"Error Response Body: {e.response.text}")
if attempt < max_retries - 1:
sleep_time = 2 ** attempt
print(f"Retrying in {sleep_time} seconds...")
time.sleep(sleep_time)
else:
return {"error": str(e)}
return {"error": "Max retries exceeded"}