|
| 1 | +--- |
| 2 | +alwaysApply: true |
| 3 | +--- |
| 4 | +### Project structure and entrypoints |
| 5 | + |
| 6 | +- **Entrypoint**: [main.py](mdc:main.py) defines the Kernel `App("browser-agent")` and the `perform` action that runs the browser agent. |
| 7 | + |
| 8 | +### Key modules |
| 9 | + |
| 10 | +- **LLM gateway and providers**: [lib/ai.py](mdc:lib/ai.py) |
| 11 | + - Maps `provider` → chat client (`anthropic`, `gemini`, `openai`) via Cloudflare AI Gateway (`AI_GATEWAY_URL`, `AI_GATEWAY_TOKEN`). |
| 12 | + |
| 13 | +- **Request/response models**: [lib/models.py](mdc:lib/models.py) |
| 14 | + - `BrowserAgentRequest` (task, provider/model/api_key, browser flags, reasoning) and `BrowserAgentResponse` (session, success, duration, result, downloads). |
| 15 | + |
| 16 | +- **Browser lifecycle**: [lib/browser.py](mdc:lib/browser.py) |
| 17 | + - `create_browser` creates a remote Kernel browser from `KERNEL_API_KEY`, returns `(session, browser)` with fixed viewport and auto PDF downloads. |
| 18 | + - `downloaded_files(agent)` collects files from the profile downloads path. |
| 19 | + |
| 20 | +- **Browser patches**: [lib/patch.py](mdc:lib/patch.py) |
| 21 | + - `PatchedBrowser` and `PatchedDownloadsWatchdog` ensure download events fire reliably (injects a synthetic `NavigationCompleteEvent`). |
| 22 | + |
| 23 | +- **Storage (Cloudflare R2)**: [lib/storage.py](mdc:lib/storage.py) |
| 24 | + - Uploads downloaded files and trajectory JSON to R2; returns presigned URLs. |
| 25 | + |
| 26 | +- **Async helpers**: [lib/asyncio.py](mdc:lib/asyncio.py) |
| 27 | + - `asyncify`/`syncify` wrappers used by storage uploads. |
| 28 | + |
| 29 | +### Runtime flow |
| 30 | + |
| 31 | +1. `POST /apps/browser-agent/actions/perform` is invoked (Kernel). |
| 32 | +2. [main.py](mdc:main.py) validates payload into `BrowserAgentRequest`. |
| 33 | +3. LLM client resolved from [lib/ai.py](mdc:lib/ai.py) using AI Gateway. |
| 34 | +4. Browser created via Kernel in [lib/browser.py](mdc:lib/browser.py), returning `session` and `browser`. |
| 35 | +5. `Agent(...).run(max_steps)` executes the task; downloads auto-captured. |
| 36 | +6. Files and trajectory uploaded via [lib/storage.py](mdc:lib/storage.py); response built from `AgentHistoryList`. |
| 37 | + |
| 38 | +### Useful references |
| 39 | + |
| 40 | +- [pyproject.toml](mdc:pyproject.toml): deps and dev tools |
| 41 | +- [README.md](mdc:README.md): API contract, examples, env |
| 42 | +- [justfile](mdc:justfile): `dev`, `deploy`, `logs`, `fmt`, `lint` |
| 43 | + |
1 | 44 | --- |
2 | 45 | alwaysApply: true |
3 | 46 | --- |
|
0 commit comments