Skip to content

Commit b7d9cc9

Browse files
committed
fix: stabilize tool-calling flow and speed up task handling
1 parent ea4f5ca commit b7d9cc9

10 files changed

Lines changed: 912 additions & 580 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ dist/
44
dist-ssr/
55
*.local
66

7+
8+
79
# Python
810
__pycache__/
911
*.py[cod]
@@ -31,6 +33,8 @@ fix_*.py
3133
# IDE
3234
.vscode/
3335
.idea/
36+
.claude/
37+
.gitignore
3438
*.suo
3539
*.ntvs
3640
*.njsproj

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ ENV USERS_FILE=/workspace/data/users.json
4545
ENV CAPTURES_FILE=/workspace/data/captures.json
4646
ENV PYTHONPATH=/workspace
4747

48-
CMD ["python", "backend/main.py"]
48+
CMD ["python", "-m", "backend.main"]

backend/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Backend package

backend/api/anthropic.py

Lines changed: 403 additions & 153 deletions
Large diffs are not rendered by default.

backend/api/v1_chat.py

Lines changed: 292 additions & 190 deletions
Large diffs are not rendered by default.

backend/core/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Backend core package

backend/core/browser_engine.py

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
JS_STREAM_FULL = """
2525
async (args) => {
2626
const controller = new AbortController();
27-
const timer = setTimeout(() => controller.abort(), 150000); // 150s timeout
27+
const timer = setTimeout(() => controller.abort(), 1800000); // 1800s timeout
2828
try {
2929
const res = await fetch(args.url, {
3030
method: 'POST',
@@ -42,16 +42,14 @@
4242
}
4343
const reader = res.body.getReader();
4444
const decoder = new TextDecoder();
45+
let body = '';
4546
while (true) {
4647
const { done, value } = await reader.read();
4748
if (done) break;
48-
const chunk = decoder.decode(value, { stream: true });
49-
if (window.send_chunk) {
50-
await window.send_chunk(args.chat_id, chunk);
51-
}
49+
body += decoder.decode(value, { stream: true });
5250
}
5351
clearTimeout(timer);
54-
return { status: res.status, body: "streamed" };
52+
return { status: res.status, body: body };
5553
} catch(e) {
5654
clearTimeout(timer);
5755
return { status: 0, body: 'JS error: ' + e.message };
@@ -216,41 +214,24 @@ async def fetch_chat(self, token: str, chat_id: str, payload: dict):
216214
yield {"status": 429, "body": "Too Many Requests (Queue full)"}
217215
return
218216

219-
queue = asyncio.Queue()
220-
self.stream_queues[chat_id] = queue
221-
222217
needs_refresh = False
223218
url = f'/api/v2/chat/completions?chat_id={chat_id}'
224-
225-
async def _run_eval():
226-
try:
227-
res = await asyncio.wait_for(
228-
page.evaluate(JS_STREAM_FULL, {"url": url, "token": token, "payload": payload, "chat_id": chat_id}),
229-
timeout=180,
230-
)
231-
queue.put_nowait({"type": "end", "result": res})
232-
except asyncio.TimeoutError:
233-
queue.put_nowait({"type": "end", "result": {"status": 0, "body": "Timeout"}})
234-
except Exception as e:
235-
queue.put_nowait({"type": "end", "result": {"status": 0, "body": str(e)}})
236-
237-
task = asyncio.create_task(_run_eval())
238-
239219
try:
240-
while True:
241-
item = await queue.get()
242-
if isinstance(item, str):
243-
yield {"status": 200, "chunk": item}
244-
elif isinstance(item, dict) and item.get("type") == "end":
245-
res = item["result"]
246-
if res.get("status") != 200 and res.get("status") != "streamed":
247-
log.warning(f"[Browser] JS Error/Non-200: {res.get('body','')[:100]}")
248-
needs_refresh = True
249-
yield res
250-
break
220+
res = await asyncio.wait_for(
221+
page.evaluate(JS_STREAM_FULL, {"url": url, "token": token, "payload": payload}),
222+
timeout=1800,
223+
)
224+
if res.get("status") != 200:
225+
log.warning(f"[Browser] JS Error/Non-200: {res.get('body','')[:100]}")
226+
needs_refresh = True
227+
yield res
228+
except asyncio.TimeoutError:
229+
needs_refresh = True
230+
yield {"status": 0, "body": "Timeout"}
231+
except Exception as e:
232+
needs_refresh = True
233+
yield {"status": 0, "body": str(e)}
251234
finally:
252-
if chat_id in self.stream_queues:
253-
del self.stream_queues[chat_id]
254235
if needs_refresh:
255236
asyncio.create_task(self._refresh_page_and_return(page))
256237
else:

0 commit comments

Comments
 (0)