-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
1492 lines (1309 loc) · 63.3 KB
/
Copy pathserver.py
File metadata and controls
1492 lines (1309 loc) · 63.3 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
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
OpenAI-compatible API server backed by browser automation.
Providers are selected per-request via the OpenAI ``model`` field:
- "gemini-browser" → gemini.google.com (text + image generation)
- "chatgpt-browser" → chatgpt.com (text + image generation)
An unknown/absent model falls back to DEFAULT_PROVIDER (env, default gemini-browser).
Endpoints:
GET / (mini web UI — chat, image gen, gallery, status)
GET /widget.js (embeddable floating chat widget for any LAN page)
GET /v1/models
POST /v1/chat/completions (streaming + non-streaming; images in AND out)
POST /v1/images/generations (OpenAI-style image generation; `image` = img2img)
POST /v1/images/edits (image + prompt -> image; JSON or multipart)
GET /images/<provider>/<file> (saved images, per-provider subfolder of GEMINI_IMAGE_DIR)
GET /api/status (per-provider busy/browser/recycle + live telemetry)
GET /api/gallery (list saved images, newest first; ?provider=&limit=)
Usage:
python server.py # listens on 0.0.0.0:8081
OpenClaw openclaw.json:
{
"models": {
"providers": {
"gemini-browser": {
"baseUrl": "http://localhost:8081/v1",
"apiKey": "local",
"api": "openai-completions",
"models": [
{"id": "gemini-browser", "name": "Gemini (Browser)"},
{"id": "chatgpt-browser", "name": "ChatGPT (Browser)"}
]
}
}
}
}
"""
import asyncio
import base64
import json
import os
import re
import shutil
import tempfile
import time
import uuid
import logging
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Any, AsyncGenerator, Optional, Union
import nodriver as uc
import websockets.exceptions
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse, StreamingResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
try:
import httpx # only needed when REMOTE_PROVIDERS is configured
except ImportError: # older venv without httpx: local providers still work
httpx = None
from providers import (
PROVIDERS, DEFAULT_PROVIDER, get_provider, patch_cdp, CHROME_ARGS,
CompletionTracker,
)
import authz
from _version import __version__
# ---------------------------------------------------------------------------
# Logging
# ---------------------------------------------------------------------------
logger = logging.getLogger("gemini_server")
logger.setLevel(logging.INFO)
_fmt = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")
_fh = logging.FileHandler("server.log", mode="w")
_fh.setFormatter(_fmt)
_sh = logging.StreamHandler()
_sh.setFormatter(_fmt)
logger.addHandler(_fh)
logger.addHandler(_sh)
# ---------------------------------------------------------------------------
# Generated-image storage. Each provider's images go in its own subfolder with
# its own filename prefix (see _persist), so e.g. ChatGPT images are no longer
# mislabeled/saved as "gemini". IMAGE_DIR is the shared base.
# GEMINI_IMAGE_DIR — base folder for saved images (created if needed).
# (env name kept for back-compat; IMAGE_DIR also accepted)
# GEMINI_PUBLIC_URL — base URL images are served under (for the returned links)
# ---------------------------------------------------------------------------
IMAGE_DIR = (os.environ.get("GEMINI_IMAGE_DIR") or os.environ.get("IMAGE_DIR")
or os.path.expanduser("~/Pictures/browser-llm"))
PUBLIC_URL = os.environ.get("GEMINI_PUBLIC_URL", "http://localhost:8081").rstrip("/")
_image_dir = Path(IMAGE_DIR)
try:
_image_dir.mkdir(parents=True, exist_ok=True)
_SAVE_ENABLED = True
logger.info(f"Saving generated images to {_image_dir}")
except Exception as e:
_SAVE_ENABLED = False
logger.warning(f"Image dir {_image_dir} unavailable ({e}); images will not be saved to disk")
_EXT = {"image/jpeg": "jpg", "image/jpg": "jpg", "image/png": "png",
"image/webp": "webp", "image/gif": "gif"}
# ---------------------------------------------------------------------------
# Image INPUT (attachments). A request can hand us an image as a data: URL,
# bare base64, an http(s) URL, a local file path, or multipart upload bytes;
# the browser's file picker needs a real file on disk, so specs are materialized
# into a temp dir for the duration of the drive and deleted afterwards.
# MAX_ATTACHMENTS — most files one request may attach
# MAX_ATTACHMENT_MB — per-file size ceiling
# ALLOW_REMOTE_FILE_PATHS — let NON-loopback clients attach server-side file
# paths (off by default: a LAN client shouldn't be
# able to upload arbitrary files off this box)
# ---------------------------------------------------------------------------
_MAX_ATTACHMENTS = max(1, int(os.environ.get("MAX_ATTACHMENTS", "6")))
_MAX_ATTACHMENT_BYTES = int(float(os.environ.get("MAX_ATTACHMENT_MB", "20")) * 1024 * 1024)
_ALLOW_REMOTE_FILE_PATHS = (os.environ.get("ALLOW_REMOTE_FILE_PATHS", "").strip().lower()
in ("1", "true", "yes", "on"))
_DATA_URL_RE = re.compile(r"^data:([\w.+-]+/[\w.+-]+)?((?:;[\w.+-]+=?[\w.+-]*)*),(.*)$",
re.I | re.S)
_B64_RE = re.compile(r"^[A-Za-z0-9+/\s]+={0,2}$")
# Image signatures, so a spec that carries no mime/extension still gets a name
# the site's file input will accept.
_MAGIC = (
(b"\x89PNG\r\n\x1a\n", "png"),
(b"\xff\xd8\xff", "jpg"),
(b"GIF87a", "gif"),
(b"GIF89a", "gif"),
(b"BM", "bmp"),
(b"\x00\x00\x01\x00", "ico"),
)
def _sniff_ext(data: bytes) -> str:
for magic, ext in _MAGIC:
if data.startswith(magic):
return ext
if data[:4] == b"RIFF" and data[8:12] == b"WEBP":
return "webp"
if data.lstrip()[:5] == b"<?xml" or data.lstrip()[:4] == b"<svg":
return "svg"
return ""
def _spec_kind(spec: str) -> str:
"""How to turn an attachment spec into bytes: 'data' | 'http' | 'path'."""
s = (spec or "").strip()
low = s[:8].lower()
if low.startswith("data:"):
return "data"
if low.startswith(("http://", "https:/")):
return "http"
if low.startswith("file://"):
return "path"
# Long, base64-looking and not a plausible path → treat as raw base64.
if len(s) > 256 and "/" not in s[:1] and "~" not in s[:1] and _B64_RE.match(s):
return "data"
return "path"
def _decode_data_spec(spec: str) -> tuple[bytes, str]:
"""(bytes, extension) for a data: URL or a bare base64 blob."""
s = spec.strip()
declared = ""
if s[:5].lower() == "data:":
m = _DATA_URL_RE.match(s)
if not m:
raise HTTPException(status_code=400, detail="malformed data: URL attachment")
declared, params, payload = (m.group(1) or ""), (m.group(2) or ""), m.group(3)
if "base64" not in params.lower():
raise HTTPException(status_code=400,
detail="only base64 data: URLs are supported for attachments")
else:
payload = s
try:
data = base64.b64decode(re.sub(r"\s+", "", payload), validate=False)
except Exception as e:
raise HTTPException(status_code=400, detail=f"attachment is not valid base64: {e}")
if not data:
raise HTTPException(status_code=400, detail="attachment decoded to zero bytes")
_check_attachment_size(len(data))
return data, (_sniff_ext(data) or _EXT.get(declared.lower(), "")
or (declared.split("/")[-1] if "/" in declared else "") or "png")
def _check_attachment_size(n: int) -> None:
if n > _MAX_ATTACHMENT_BYTES:
raise HTTPException(
status_code=413,
detail=f"attachment is {n / 1e6:.1f} MB; the limit is "
f"{_MAX_ATTACHMENT_BYTES / 1e6:.0f} MB (raise MAX_ATTACHMENT_MB)")
async def _download_attachment(url: str) -> bytes:
"""Fetch an http(s) attachment, capped at MAX_ATTACHMENT_MB."""
if httpx is not None:
try:
async with httpx.AsyncClient(timeout=httpx.Timeout(60.0, connect=10)) as client:
async with client.stream("GET", url, follow_redirects=True) as r:
if r.status_code >= 400:
raise HTTPException(status_code=400,
detail=f"attachment URL returned {r.status_code}: {url}")
buf = bytearray()
async for chunk in r.aiter_bytes():
buf += chunk
_check_attachment_size(len(buf))
return bytes(buf)
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=400, detail=f"could not fetch attachment {url}: {e}")
def _get() -> bytes:
import urllib.request
with urllib.request.urlopen(url, timeout=60) as resp:
return resp.read(_MAX_ATTACHMENT_BYTES + 1)
try:
data = await asyncio.to_thread(_get)
except Exception as e:
raise HTTPException(status_code=400, detail=f"could not fetch attachment {url}: {e}")
_check_attachment_size(len(data))
return data
def _resolve_local_attachment(spec: str, *, allow_local_paths: bool) -> Path:
if not allow_local_paths:
raise HTTPException(
status_code=400,
detail="file-path attachments are only accepted from localhost; send the "
"image as a data: URL (or set ALLOW_REMOTE_FILE_PATHS=1 on the server)")
raw = spec.strip()
if raw[:7].lower() == "file://":
from urllib.parse import unquote, urlparse
raw = unquote(urlparse(raw).path)
p = Path(raw).expanduser()
if not p.is_file():
raise HTTPException(status_code=400, detail=f"attachment file not found: {spec}")
_check_attachment_size(p.stat().st_size)
return p.resolve()
@asynccontextmanager
async def _attachment_files(specs: Optional[list], raw: Optional[list] = None, *,
allow_local_paths: bool = True):
"""Materialize attachment specs (and raw ``(filename, bytes)`` uploads) into
real files for the browser's file input. Temp files are deleted on exit;
caller-supplied local paths are used in place and never touched."""
specs = [s for s in (specs or []) if isinstance(s, str) and s.strip()]
raw = list(raw or [])
if len(specs) + len(raw) > _MAX_ATTACHMENTS:
raise HTTPException(
status_code=400,
detail=f"too many attachments ({len(specs) + len(raw)}); the limit is "
f"{_MAX_ATTACHMENTS} (raise MAX_ATTACHMENTS)")
tmpdir: Optional[str] = None
paths: list[str] = []
def _write(data: bytes, ext: str, name: str = "") -> str:
nonlocal tmpdir
if tmpdir is None:
tmpdir = tempfile.mkdtemp(prefix="browser-llm-input-")
stem = re.sub(r"[^A-Za-z0-9._-]+", "_", Path(name or "").stem)[:40] or "attachment"
fname = f"{stem}_{len(paths) + 1}.{(ext or 'png').lstrip('.')}"
fpath = Path(tmpdir) / fname
fpath.write_bytes(data)
return str(fpath)
try:
for name, data in raw:
_check_attachment_size(len(data))
if not data:
raise HTTPException(status_code=400, detail=f"uploaded file {name!r} is empty")
ext = _sniff_ext(data) or Path(name or "").suffix.lstrip(".") or "png"
paths.append(_write(data, ext, name))
for spec in specs:
kind = _spec_kind(spec)
if kind == "data":
data, ext = _decode_data_spec(spec)
paths.append(_write(data, ext))
elif kind == "http":
data = await _download_attachment(spec.strip())
ext = _sniff_ext(data) or Path(spec.split("?")[0]).suffix.lstrip(".") or "png"
paths.append(_write(data, ext, Path(spec.split("?")[0]).name))
else:
paths.append(str(_resolve_local_attachment(
spec, allow_local_paths=allow_local_paths)))
if paths:
logger.info(f"attachments ready: {[Path(p).name for p in paths]}")
yield paths
finally:
if tmpdir:
shutil.rmtree(tmpdir, ignore_errors=True)
# ---------------------------------------------------------------------------
# Access control + remote upstreams (see authz.py for the full contract).
# BROWSER_LLM_API_KEY — when set, non-loopback clients must send this key
# (Bearer/X-Api-Key) on /v1/* and /api/*. Localhost stays open.
# REMOTE_PROVIDERS — "model=url[,model=url…]": proxy those models to
# another browser-llm-api instance instead of a local browser (e.g.
# forward chatgpt-browser to the one machine with a ChatGPT login).
# REMOTE_API_KEY — Bearer key sent on proxied requests (the upstream's
# BROWSER_LLM_API_KEY).
# ---------------------------------------------------------------------------
API_KEY = os.environ.get("BROWSER_LLM_API_KEY", "").strip()
REMOTES = authz.parse_remote_providers(os.environ.get("REMOTE_PROVIDERS"))
REMOTE_API_KEY = os.environ.get("REMOTE_API_KEY", "").strip()
# Upstream drive can legitimately take up to _MAX_DEADLINE (900s); give the
# proxy a little headroom on top so we never cut off a still-working upstream.
_REMOTE_TIMEOUT = 940.0
if REMOTES and httpx is None:
raise RuntimeError(
"REMOTE_PROVIDERS is set but httpx is not installed — "
"run: ./venv/bin/pip install -r requirements.txt"
)
# Suppress KeyError from unknown CDP events (e.g. DOM.adoptedStyleSheetsModified).
patch_cdp()
# ---------------------------------------------------------------------------
# Pydantic models (OpenAI wire format)
# ---------------------------------------------------------------------------
class ImageURL(BaseModel):
url: str
detail: Optional[str] = None
class ContentPart(BaseModel):
"""One part of a multimodal message. Deliberately permissive so the common
dialects all work: OpenAI (``{"type":"image_url","image_url":{"url":…}}``),
the Responses-style plain-string ``image_url``, and Anthropic's
``{"type":"image","source":{"type":"base64","media_type":…,"data":…}}``."""
model_config = {"extra": "allow"}
type: Optional[str] = None
text: Optional[str] = None
image_url: Optional[Union[ImageURL, str, dict]] = None
source: Optional[dict] = None
image: Optional[str] = None
class Message(BaseModel):
role: str
# str for plain text, or a list of content parts for text+images (vision).
content: Union[str, list[ContentPart], None] = None
class ChatCompletionRequest(BaseModel):
model: str = DEFAULT_PROVIDER
messages: list[Message]
stream: Optional[bool] = False
temperature: Optional[float] = None
max_tokens: Optional[int] = None
# Convenience shorthand (not OpenAI): attach these images to the prompt
# without building content parts. Each item is a data: URL, an http(s) URL,
# bare base64, or a local file path (localhost callers only).
images: Optional[list[str]] = None
class ImageGenRequest(BaseModel):
prompt: str
model: str = DEFAULT_PROVIDER
n: Optional[int] = 1
size: Optional[str] = None
response_format: Optional[str] = "b64_json" # "b64_json" | "url" (data: URL)
# Reference image(s) -> image-to-image / edit. Same spec forms as
# ChatCompletionRequest.images. `image` matches OpenAI's edits field name.
image: Optional[Union[str, list[str]]] = None
images: Optional[list[str]] = None
# ---------------------------------------------------------------------------
# Browser state — one persistent instance per provider, one request at a time
# per provider (so Gemini and ChatGPT can run concurrently).
# ---------------------------------------------------------------------------
_browsers: dict[str, uc.Browser] = {}
_locks: dict[str, asyncio.Lock] = {name: asyncio.Lock() for name in PROVIDERS}
# The persistent browser's renderer bloats after a handful of image generations
# (heavy canvas/blobs + growing SPA DOM) and starts timing out; a fresh browser
# resets it. Recycle a provider's browser once it has done this many image gens.
_RECYCLE_AFTER_IMAGES = int(os.environ.get("BROWSER_RECYCLE_AFTER_IMAGES", "3"))
_img_gen_count: dict[str, int] = {name: 0 for name in PROVIDERS}
def _note_image_gen(provider) -> None:
_img_gen_count[provider.name] = _img_gen_count.get(provider.name, 0) + 1
# Lightweight per-provider telemetry for the dashboard's live status panel.
# In-memory only (reset on restart). Latency is wall-clock for the actual browser
# drive (queue-wait excluded), so it reflects model/site speed, not our overhead.
_metrics: dict[str, dict] = {
name: {
"requests": 0, # completed requests (chat + image)
"errors": 0, # of which failed
"total_latency": 0.0, # sum of durations, for the running average
"last_latency": None, # most recent duration (s)
"last_error": None, # most recent error text (truncated)
"last_error_at": None, # epoch seconds
"last_request_at": None, # epoch seconds
}
for name in {*PROVIDERS, *REMOTES} # remote-only models get telemetry too
}
def _record_request(name: str, started: float, error: Optional[BaseException] = None) -> None:
"""Fold one finished request into the provider's telemetry (in-memory).
``started`` is a ``time.monotonic()`` stamp taken *after* the per-provider
lock is acquired, so the recorded latency excludes time spent queued behind
another in-flight request.
"""
m = _metrics.get(name)
if m is None:
return
dur = time.monotonic() - started
m["requests"] += 1
m["total_latency"] += dur
m["last_latency"] = round(dur, 2)
m["last_request_at"] = int(time.time())
if error is not None:
m["errors"] += 1
m["last_error"] = str(error)[:200]
m["last_error_at"] = int(time.time())
async def _browser_alive(b: uc.Browser) -> bool:
"""Cheap liveness probe for a cached browser: the Chrome process must still
be running AND its CDP websocket must answer. Catches both an exited Chrome
and the 2026-07-09 failure mode where the process lived on but the CDP
connection had silently died — previously the first (or, before the
eviction fix, every) request after that just failed."""
try:
if b.stopped: # Chrome process has exited
return False
# Same call nodriver's own Browser._get_targets makes; Browser.send
# re-attaches a dropped socket first, so this probes "usable", and
# can even transparently heal a dropped-but-recoverable connection.
await asyncio.wait_for(b.send(uc.cdp.target.get_targets()), timeout=5)
return True
except Exception:
return False
async def get_browser(provider) -> uc.Browser:
b = _browsers.get(provider.name)
# Proactively recycle a browser that has generated enough images to bloat.
# Called at the start of each (per-provider serialized) request, so the
# browser is never recycled mid-response.
if b is not None and _img_gen_count.get(provider.name, 0) >= _RECYCLE_AFTER_IMAGES:
logger.info(f"[{provider.name}] recycling browser after "
f"{_img_gen_count[provider.name]} image gens")
try:
b.stop()
except Exception as e:
logger.warning(f"[{provider.name}] browser stop during recycle failed: {e}")
_browsers.pop(provider.name, None)
_img_gen_count[provider.name] = 0
b = None
await asyncio.sleep(2) # let Chrome release the profile before restart
# Never hand out a browser whose process or CDP connection is dead —
# replace it now so THIS request succeeds, instead of failing it once
# and only recovering on the next one.
if b is not None and not await _browser_alive(b):
logger.warning(f"[{provider.name}] cached browser is dead (process or CDP "
f"connection); starting a fresh one")
try:
b.stop()
except Exception:
pass
_browsers.pop(provider.name, None)
_img_gen_count[provider.name] = 0
b = None
await asyncio.sleep(2) # let Chrome release the profile before restart
if b is None:
# Clear a stale singleton lock so the fresh start isn't blocked.
try:
for f in Path(provider.profile_dir).glob("Singleton*"):
f.unlink()
except Exception:
pass
logger.info(f"[{provider.name}] Starting browser (profile {provider.profile_dir})...")
b = await uc.start(user_data_dir=provider.profile_dir, browser_args=list(CHROME_ARGS))
_browsers[provider.name] = b
return b
def _is_dead_transport(exc: BaseException) -> bool:
"""True if `exc` indicates the CDP connection/browser process itself died
(as opposed to an ordinary in-page failure like 'no image produced'). A
provider that hits this must not be reused for the next request — it will
just fail identically until the process is restarted (see 2026-07-09
incident: one dead ChatGPT connection silently broke every request for the
rest of the day)."""
if isinstance(exc, (websockets.exceptions.ConnectionClosed, ConnectionError, OSError)):
return True
# nodriver's Connection.send raises RuntimeError("WebSocket is not connected")
# once the socket is gone — same corpse, different wrapper.
if isinstance(exc, RuntimeError) and "websocket" in str(exc).lower():
return True
return False
async def _evict_dead_browser(provider, exc: BaseException) -> None:
"""Drop a provider's cached browser after a transport-level failure so the
*next* request starts a fresh one instead of retrying against a corpse."""
if not _is_dead_transport(exc):
return
b = _browsers.pop(provider.name, None)
_img_gen_count[provider.name] = 0 # count tracked THAT browser's bloat, not the next one's
if b is not None:
logger.warning(f"[{provider.name}] browser connection died ({exc!r}); "
f"evicting so the next request starts fresh")
try:
b.stop()
except Exception:
pass
# ---------------------------------------------------------------------------
# Generated-image helpers (generic)
# ---------------------------------------------------------------------------
def _provider_slug(provider) -> str:
"""Short filesystem-safe label for a provider ('chatgpt' / 'gemini'), used to
name and folder saved images so one provider's images aren't mislabeled under
another's (the bug: everything was foldered + prefixed 'gemini')."""
name = (getattr(provider, "name", "") or "provider").replace("-browser", "")
slug = "".join(c if (c.isalnum() or c in "-_") else "_" for c in name).strip("_-")
return slug or "provider"
def _persist(im: dict, provider) -> dict:
"""Write an extracted image (if it has inline base64) into a per-provider
subfolder of IMAGE_DIR, adding 'path' and 'url'. Images that are remote-only
(e.g. CORS-blocked) keep their 'src' and are left untouched."""
if not im.get("b64") or not _SAVE_ENABLED:
return im
try:
slug = _provider_slug(provider)
ext = _EXT.get(im.get("mime", "image/jpeg"), "jpg")
subdir = _image_dir / slug
subdir.mkdir(parents=True, exist_ok=True)
fname = f"{slug}_{int(time.time())}_{uuid.uuid4().hex[:8]}.{ext}"
fpath = subdir / fname
fpath.write_bytes(base64.b64decode(im["b64"]))
im["path"] = str(fpath)
im["url"] = f"{PUBLIC_URL}/images/{slug}/{fname}"
logger.info(f"saved image -> {fpath}")
except Exception as e:
logger.warning(f"failed to save image: {e}")
return im
def _img_markdown(im: dict) -> str:
alt = im.get("alt") or "generated image"
# Prefer the served URL (small), then a remote src, then inline data URL.
src = im.get("url") or im.get("src")
if not src and im.get("b64"):
src = f"data:{im['mime']};base64,{im['b64']}"
return f"\n\n"
def _compose(text: str, imgs: list[dict], provider) -> str:
"""Build chat message content. For providers whose image-prompt prose is
just internal 'thinking' (Gemini), return image markdown only. For providers
where it's a real caption (ChatGPT), keep the text and append the images."""
if not imgs:
return text
md = "".join(_img_markdown(im) for im in imgs).strip()
if provider.image_text_is_caption and text.strip():
return (text.strip() + "\n\n" + md).strip()
return md
# ---------------------------------------------------------------------------
# Core: send prompt → stream response text, then generated images
# ---------------------------------------------------------------------------
def _message_pieces(m: Message) -> tuple[str, list[str]]:
"""Split one message into (text, image specs). Plain-string content has no
images; a content-part list is walked for text and every image dialect we
accept (see ContentPart)."""
if m.content is None:
return "", []
if isinstance(m.content, str):
return m.content, []
texts: list[str] = []
specs: list[str] = []
for part in m.content:
if isinstance(part, str):
if part.strip():
texts.append(part)
continue
if part.text:
texts.append(part.text)
spec = None
iu = part.image_url
if isinstance(iu, ImageURL):
spec = iu.url
elif isinstance(iu, str):
spec = iu
elif isinstance(iu, dict):
spec = iu.get("url")
if not spec and isinstance(part.image, str):
spec = part.image
if not spec and isinstance(part.source, dict):
src = part.source
data = src.get("data")
if isinstance(data, str) and data:
mt = src.get("media_type") or "image/png"
spec = data if data[:5].lower() == "data:" else f"data:{mt};base64,{data}"
elif isinstance(src.get("url"), str):
spec = src["url"]
if isinstance(spec, str) and spec.strip():
specs.append(spec.strip())
elif (part.type or "").lower() in ("image_url", "image", "input_image"):
logger.warning(f"content part typed {part.type!r} carried no usable image")
return "\n".join(t for t in texts if t).strip(), specs
def _build_prompt(messages: list[Message]) -> tuple[str, list[str]]:
"""
Flatten the OpenAI messages list into a single prompt plus the list of image
attachments found in it. System messages become a preamble; multi-turn
history is included so agents get context.
Every attached image goes into the one composer message we send, so a turn
that carried images is annotated — otherwise a multi-turn transcript gives
the model no way to tell which image belonged to which turn.
"""
system: list[str] = []
turns: list[tuple[Message, str, list[str]]] = []
specs: list[str] = []
for m in messages:
text, imgs = _message_pieces(m)
specs.extend(imgs)
if m.role == "system":
system.append(text)
else:
turns.append((m, text, imgs))
def _annotate(text: str, imgs: list[str], multi: bool) -> str:
if not imgs or not multi:
return text
note = f"[{len(imgs)} attached image{'s' if len(imgs) > 1 else ''}]"
return f"{text} {note}".strip()
parts = []
if any(s for s in system):
parts.append("[Context/Instructions: " + " ".join(s for s in system if s) + "]")
multi = len(turns) > 1
if len(turns) == 1:
parts.append(turns[0][1])
else:
for m, text, imgs in turns:
label = "User" if m.role == "user" else "Assistant"
parts.append(f"{label}: {_annotate(text, imgs, multi)}")
if len(specs) > _MAX_ATTACHMENTS:
# Keep the most recent images — those belong to the live turn.
logger.warning(f"{len(specs)} images in this conversation; sending the last "
f"{_MAX_ATTACHMENTS} (MAX_ATTACHMENTS)")
specs = specs[-_MAX_ATTACHMENTS:]
return "\n\n".join(p for p in parts if p), specs
_BASE_DEADLINE = 420.0 # base ceiling; long image gen on the free tier is slow
_MAX_DEADLINE = 900.0 # hard cap even for an answer that keeps actively streaming
async def _stream_completion(provider, page, monitor) -> AsyncGenerator[str, None]:
"""
Poll the response, yielding text deltas as they grow; return when complete.
The completion decision lives in ``CompletionTracker`` (unit-tested). This
loop only polls the page, feeds samples in, and yields chunks. Long answers
(e.g. a whole HTML page) that are *still actively streaming* extend the
deadline up to ``_MAX_DEADLINE`` so they aren't truncated mid-generation,
while a stalled request still gives up near ``_BASE_DEADLINE``.
"""
tracker = CompletionTracker()
start = time.monotonic()
deadline = start + _BASE_DEADLINE
# Some providers' extracted text reshapes near the end (see
# Provider.buffered_stream); for those we suppress incremental deltas and
# emit the final authoritative text once, so append-only SSE stays correct.
buffered = getattr(provider, "buffered_stream", False)
while True:
now = time.monotonic()
if now >= deadline:
# Extend only while the answer is plainly still in flight — text
# still growing, or WebSocket frames still arriving (ChatGPT).
ws_idle = monitor.seconds_since_ws_frame(now)
active = (tracker.silent_for(now) < 5.0
or (ws_idle is not None and ws_idle < CompletionTracker.WS_ACTIVE_WINDOW))
if active and deadline < start + _MAX_DEADLINE:
deadline = min(start + _MAX_DEADLINE, deadline + 120.0)
else:
logger.warning(
f"[{provider.name}] completion deadline reached "
f"({now - start:.0f}s, {tracker.text_len} chars)"
)
if buffered and tracker.text:
yield tracker.text
return
await asyncio.sleep(0.8)
now = time.monotonic()
raw = await provider.get_response_text(page)
img = await provider.image_status(page)
is_gen = await provider.is_generating(page)
cdp_done = monitor.stream_done.is_set()
chunk, done = tracker.feed(now, raw, is_gen, img, cdp_done=cdp_done)
if chunk and not buffered:
yield chunk
logger.debug(
f"[{provider.name}] poll: text={tracker.text_len} "
f"silent={tracker.silent_for(now):.1f}s cdp={'y' if tracker.cdp_fired else 'n'} "
f"gen={is_gen} img={img}"
)
if done:
logger.info(
f"[{provider.name}] Done ({done}). {tracker.text_len} chars"
f"{' (CDP)' if tracker.cdp_fired else ''}"
)
if buffered and tracker.text:
yield tracker.text
return
async def run_chat(provider, prompt: str, attachments: Optional[list] = None, *,
raw_uploads: Optional[list] = None,
allow_local_paths: bool = True) -> AsyncGenerator[str, None]:
"""Open the provider's chat, attach any input images, send the prompt, stream
text deltas, then append any generated images as markdown links."""
browser = await get_browser(provider)
try:
async with _attachment_files(attachments, raw_uploads,
allow_local_paths=allow_local_paths) as files:
page, monitor = await provider.open_and_send(browser, prompt, attachments=files)
async for delta in _stream_completion(provider, page, monitor):
yield delta
n = 0
for im in await provider.get_images(page):
_persist(im, provider)
n += 1
logger.info(f"[{provider.name}] attaching image ({im.get('mime')})")
yield _img_markdown(im)
if n:
_note_image_gen(provider) # count toward browser recycle
except Exception as e:
await _evict_dead_browser(provider, e)
raise
# Leave the tab open — closing or navigating away disrupts the browser.
async def drive_once(provider, prompt: str, attachments: Optional[list] = None, *,
raw_uploads: Optional[list] = None,
allow_local_paths: bool = True) -> tuple[str, list[dict]]:
"""Non-streaming drive used by non-streaming chat and the images endpoints:
returns (text, images)."""
browser = await get_browser(provider)
try:
async with _attachment_files(attachments, raw_uploads,
allow_local_paths=allow_local_paths) as files:
page, monitor = await provider.open_and_send(browser, prompt, attachments=files)
text = ""
async for delta in _stream_completion(provider, page, monitor):
text += delta
imgs = [_persist(im, provider) for im in await provider.get_images(page)]
if imgs:
_note_image_gen(provider) # count toward browser recycle
return text, imgs
except Exception as e:
await _evict_dead_browser(provider, e)
raise
def _prevalidate_specs(specs: list, *, allow_local_paths: bool) -> None:
"""Catch the cheap attachment mistakes *before* a streaming response starts,
so the client gets a real 4xx instead of an in-band error chunk. Only checks
that cost nothing (count, and local paths: policy + existence + size);
decoding/downloading still happens during the drive."""
if len(specs) > _MAX_ATTACHMENTS:
raise HTTPException(
status_code=400,
detail=f"too many attachments ({len(specs)}); the limit is "
f"{_MAX_ATTACHMENTS} (raise MAX_ATTACHMENTS)")
for spec in specs:
if _spec_kind(spec) == "path":
_resolve_local_attachment(spec, allow_local_paths=allow_local_paths)
def _check_upload_support(provider, specs: list, raw: Optional[list] = None) -> None:
"""Fail fast (and clearly) when a request carries images but the target
provider can't take them."""
if not (specs or raw):
return
if not getattr(provider, "supports_upload", False):
raise HTTPException(
status_code=400,
detail=f"{provider.name} does not support image input (attachments)")
# ---------------------------------------------------------------------------
# Remote upstream proxying (REMOTE_PROVIDERS): requests for a mapped model are
# forwarded verbatim to another browser-llm-api instance — no local browser.
# A remote mapping OVERRIDES the local provider of the same name (that's the
# point: this install has the code for chatgpt-browser but no login).
# ---------------------------------------------------------------------------
def _resolve_model(model: Optional[str]) -> str:
"""The model name a request actually routes to: a known local provider or
remote mapping wins; anything else falls back to DEFAULT_PROVIDER (same
fallback rule as get_provider, but remote-aware)."""
if model and (model in REMOTES or model in PROVIDERS):
return model
return DEFAULT_PROVIDER
def _remote_headers() -> dict:
h = {"Content-Type": "application/json"}
if REMOTE_API_KEY:
h["Authorization"] = f"Bearer {REMOTE_API_KEY}"
return h
def _remote_timeout():
return httpx.Timeout(_REMOTE_TIMEOUT, connect=15)
_MIME_BY_EXT = {v: k for k, v in _EXT.items()}
async def _spec_to_data_url(spec: str, *, allow_local_paths: bool) -> str:
"""Turn a local-path attachment spec into a data: URL. A file path is
meaningless on the upstream machine, so proxied requests must carry bytes.
data:/http(s) specs are already portable and pass through untouched."""
if _spec_kind(spec) != "path":
return spec
p = _resolve_local_attachment(spec, allow_local_paths=allow_local_paths)
data = await asyncio.to_thread(p.read_bytes)
_check_attachment_size(len(data))
ext = (_sniff_ext(data) or p.suffix.lstrip(".")).lower()
mime = _MIME_BY_EXT.get(ext, "image/png" if ext != "svg" else "image/svg+xml")
return f"data:{mime};base64,{base64.b64encode(data).decode()}"
async def _inline_paths_for_remote(payload: dict, *, allow_local_paths: bool) -> dict:
"""Rewrite every local-path attachment in an outgoing proxied payload into a
data: URL, in the shapes this API accepts (top-level images/image, plus
content parts inside messages)."""
async def fix(v):
return await _spec_to_data_url(v, allow_local_paths=allow_local_paths) \
if isinstance(v, str) and v.strip() else v
for key in ("images", "image"):
v = payload.get(key)
if isinstance(v, str):
payload[key] = await fix(v)
elif isinstance(v, list):
payload[key] = [await fix(s) for s in v]
for m in payload.get("messages") or []:
content = m.get("content") if isinstance(m, dict) else None
if not isinstance(content, list):
continue
for part in content:
if not isinstance(part, dict):
continue
iu = part.get("image_url")
if isinstance(iu, str):
part["image_url"] = await fix(iu)
elif isinstance(iu, dict) and isinstance(iu.get("url"), str):
iu["url"] = await fix(iu["url"])
if isinstance(part.get("image"), str):
part["image"] = await fix(part["image"])
src = part.get("source")
if isinstance(src, dict) and isinstance(src.get("url"), str):
src["url"] = await fix(src["url"])
return payload
async def _proxy_chat(name: str, url: str, req: ChatCompletionRequest, *,
allow_local_paths: bool = True):
"""Forward a chat completion to the upstream instance. Streaming responses
are relayed byte-for-byte (the upstream already speaks correct SSE);
failures are surfaced in-band as a chunk, matching local behavior."""
payload = req.model_dump(exclude_none=True)
payload["model"] = name
payload = await _inline_paths_for_remote(payload, allow_local_paths=allow_local_paths)
if req.stream:
async def relay():
started = time.monotonic()
err: Optional[BaseException] = None
try:
async with httpx.AsyncClient(timeout=_remote_timeout()) as client:
async with client.stream("POST", f"{url}/v1/chat/completions",
json=payload, headers=_remote_headers()) as r:
if r.status_code != 200:
body = (await r.aread()).decode("utf-8", "ignore")[:300]
raise RuntimeError(f"upstream returned {r.status_code}: {body}")
async for chunk in r.aiter_bytes():
yield chunk
except Exception as e:
err = e
logger.error(f"[{name}] remote proxy ({url}) failed: {e}")
data = {
"id": f"chatcmpl-{uuid.uuid4().hex[:12]}",
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": name,
"choices": [{"index": 0,
"delta": {"role": "assistant",
"content": f"\n\n[browser-llm error: remote: {e}]"},
"finish_reason": None}],
}
yield f"data: {json.dumps(data)}\n\n".encode()
data["choices"][0]["delta"] = {}
data["choices"][0]["finish_reason"] = "stop"
yield f"data: {json.dumps(data)}\n\n".encode()
yield b"data: [DONE]\n\n"
finally:
_record_request(name, started, err)
return StreamingResponse(relay(), media_type="text/event-stream")
started = time.monotonic()
try:
async with httpx.AsyncClient(timeout=_remote_timeout()) as client:
r = await client.post(f"{url}/v1/chat/completions",
json=payload, headers=_remote_headers())
except Exception as e:
_record_request(name, started, e)
logger.error(f"[{name}] remote proxy ({url}) failed: {e}")
raise HTTPException(status_code=502, detail=f"remote provider {url} unreachable: {e}")
if r.status_code != 200:
err = RuntimeError(f"upstream returned {r.status_code}")
_record_request(name, started, err)
raise HTTPException(status_code=502,
detail=f"remote provider returned {r.status_code}: {r.text[:300]}")
_record_request(name, started)
return JSONResponse(r.json())
async def _proxy_images(name: str, url: str, req: ImageGenRequest, *,
path: str = "/v1/images/generations",
allow_local_paths: bool = True):
"""Forward an image generation/edit to the upstream instance. Returned image
URLs point at the upstream (its GEMINI_PUBLIC_URL); /images/* is public
there even with an API key set, so the links work in a plain browser."""
payload = req.model_dump(exclude_none=True)
payload["model"] = name
payload = await _inline_paths_for_remote(payload, allow_local_paths=allow_local_paths)
started = time.monotonic()
try:
async with httpx.AsyncClient(timeout=_remote_timeout()) as client:
r = await client.post(f"{url}{path}",
json=payload, headers=_remote_headers())
except Exception as e:
_record_request(name, started, e)
logger.error(f"[{name}] remote image proxy ({url}) failed: {e}")
raise HTTPException(status_code=502, detail=f"remote provider {url} unreachable: {e}")
if r.status_code != 200:
err = RuntimeError(f"upstream returned {r.status_code}")
_record_request(name, started, err)
raise HTTPException(status_code=r.status_code if r.status_code in (501, 502) else 502,
detail=f"remote provider returned {r.status_code}: {r.text[:300]}")
_record_request(name, started)
return JSONResponse(r.json())
# ---------------------------------------------------------------------------
# FastAPI app
# ---------------------------------------------------------------------------
@asynccontextmanager
async def lifespan(app: FastAPI):