|
24 | 24 | JS_STREAM_FULL = """ |
25 | 25 | async (args) => { |
26 | 26 | const controller = new AbortController(); |
27 | | - const timer = setTimeout(() => controller.abort(), 150000); // 150s timeout |
| 27 | + const timer = setTimeout(() => controller.abort(), 1800000); // 1800s timeout |
28 | 28 | try { |
29 | 29 | const res = await fetch(args.url, { |
30 | 30 | method: 'POST', |
|
42 | 42 | } |
43 | 43 | const reader = res.body.getReader(); |
44 | 44 | const decoder = new TextDecoder(); |
| 45 | + let body = ''; |
45 | 46 | while (true) { |
46 | 47 | const { done, value } = await reader.read(); |
47 | 48 | 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 }); |
52 | 50 | } |
53 | 51 | clearTimeout(timer); |
54 | | - return { status: res.status, body: "streamed" }; |
| 52 | + return { status: res.status, body: body }; |
55 | 53 | } catch(e) { |
56 | 54 | clearTimeout(timer); |
57 | 55 | return { status: 0, body: 'JS error: ' + e.message }; |
@@ -216,41 +214,24 @@ async def fetch_chat(self, token: str, chat_id: str, payload: dict): |
216 | 214 | yield {"status": 429, "body": "Too Many Requests (Queue full)"} |
217 | 215 | return |
218 | 216 |
|
219 | | - queue = asyncio.Queue() |
220 | | - self.stream_queues[chat_id] = queue |
221 | | - |
222 | 217 | needs_refresh = False |
223 | 218 | 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 | | - |
239 | 219 | 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)} |
251 | 234 | finally: |
252 | | - if chat_id in self.stream_queues: |
253 | | - del self.stream_queues[chat_id] |
254 | 235 | if needs_refresh: |
255 | 236 | asyncio.create_task(self._refresh_page_and_return(page)) |
256 | 237 | else: |
|
0 commit comments