|
1 | | -# PROJECT KNOWLEDGE BASE |
| 1 | +# AGENTS.md |
2 | 2 |
|
3 | | -**Generated:** 2026-03-15 |
4 | | -**Commit:** b797332 |
5 | | -**Branch:** main |
| 3 | +## Repository overview |
6 | 4 |
|
7 | | -## OVERVIEW |
8 | | -Python 3.8+ library for Capsolver service API. Dual sync (`requests`) / async (`aiohttp`) support. `msgspec` for serialization, `tenacity` for retries. |
| 5 | +Python 3.8+ client library for the Capsolver captcha-solving API. Single-package `src/` layout, dual sync (`requests`) / async (`aiohttp`) execution, `msgspec` for serialization, `tenacity` for async retries. |
9 | 6 |
|
10 | | -## STRUCTURE |
11 | | -``` |
| 7 | +## Where to work |
| 8 | + |
| 9 | +```text |
12 | 10 | ./ |
13 | | -├── src/python3_capsolver/ # Main library (service implementations) |
14 | | -│ ├── core/ # Base classes, instruments, serializers |
15 | | -│ └── *.py # Service-specific (ReCaptcha, Cloudflare, etc.) |
16 | | -├── tests/ # Pytest suite (matches source structure) |
17 | | -├── docs/ # Sphinx documentation |
18 | | -├── ARCHITECTURE.md # System architecture (matklad-style) |
19 | | -└── pyproject.toml # Build, uv, pytest, black/isort config |
| 11 | +├── src/python3_capsolver/ # Main library |
| 12 | +│ ├── core/ # Base classes, instruments, serializers, enums |
| 13 | +│ ├── control.py # Raw API: get_balance, create_task, get_task_result |
| 14 | +│ ├── recaptcha.py # ReCaptcha V2/V3/Enterprise |
| 15 | +│ ├── cloudflare.py # Cloudflare Turnstile/Challenge |
| 16 | +│ └── *.py # Other captcha services (see package AGENTS.md) |
| 17 | +├── tests/ # Pytest suite mirroring source structure |
| 18 | +│ ├── conftest.py # BaseTest class, fixtures, rate-limiting delays |
| 19 | +│ └── test_*.py # One file per service + test_core.py + test_instrument.py |
| 20 | +├── docs/ # Sphinx documentation (make html) |
| 21 | +├── ARCHITECTURE.md # Layered architecture, data flow, invariants |
| 22 | +├── pyproject.toml # Build, deps, black/isort/pytest config |
| 23 | +└── Makefile # make tests, make refactor, make build, make doc |
20 | 24 | ``` |
21 | 25 |
|
22 | | -## WHERE TO LOOK |
23 | | -| Task | Location | Notes | |
24 | | -|------|----------|-------| |
25 | | -| **Architecture** | `ARCHITECTURE.md` | Layered design, invariants, life of a request | |
26 | | -| **Base Logic** | `src/python3_capsolver/core/` | `base.py`, `serializer.py`, `enum.py`, instruments | |
27 | | -| **Service Implementations** | `src/python3_capsolver/*.py` | `recaptcha.py`, `cloudflare.py`, `control.py` | |
28 | | -| **Tests** | `tests/` | `conftest.py` (BaseTest, fixtures), per-service tests | |
29 | | -| **Configuration** | `pyproject.toml` | uv, black (120), isort, pytest (asyncio auto) | |
30 | | -| **Commands** | `Makefile` | `make tests`, `make build`, `make upload` | |
31 | | - |
32 | | -## CONVENTIONS |
33 | | -- **Toolchain**: `uv` for package management (`uv sync`, `uv run`, `uv build`, `uv publish`) |
34 | | -- **Formatter**: `black` (line-length 120), `isort` (profile "black") |
35 | | -- **Cleanup**: `autoflake` (remove unused imports/variables) |
36 | | -- **Serialization**: `msgspec` (not `json`) for performance |
37 | | -- **Concurrency**: Dual sync/async required for all instruments |
38 | | -- **Retries**: `tenacity` (async), `requests.Retry` (sync) — 5 attempts, exponential backoff |
39 | | -- **Testing**: pytest 7.0+, `pytest-asyncio` (auto mode), rate-limiting fixtures (1s func, 2s class) |
40 | | - |
41 | | -## ANTI-PATTERNS (THIS PROJECT) |
42 | | -- **Empty `__init__.py` files**: `src/python3_capsolver/__init__.py` only exports `__version__`; `core/__init__.py` is completely empty. Users must import via full paths (`from python3_capsolver.recaptcha import ReCaptcha`) |
43 | | -- **AGENTS.md in package dirs**: Will ship with distribution unless excluded in `pyproject.toml` |
44 | | -- **No CLI entry points**: Library-only, no console_scripts defined |
45 | | - |
46 | | -## UNIQUE STYLES |
47 | | -- **Service Pattern**: Each captcha service inherits from `CaptchaParams` with `captcha_handler()` (sync) + `aio_captcha_handler()` (async) |
48 | | -- **Task Payload**: Dict merged with internal params, passed to `create_task()` API |
49 | | -- **Context Managers**: All services support `with` / `async with` for session cleanup |
50 | | -- **Test Duplication**: Every sync test (`def test_*`) has async counterpart (`async def test_aio_*`) |
51 | | - |
52 | | -## COMMANDS |
| 26 | +## Architecture and boundaries |
| 27 | + |
| 28 | +Four layers with strict dependency direction (top → bottom only): |
| 29 | + |
| 30 | +1. **Service layer** (`src/python3_capsolver/*.py`) — thin wrappers inheriting `CaptchaParams`, zero HTTP logic |
| 31 | +2. **Base layer** (`core/base.py`) — `CaptchaParams` merges payloads, delegates to instruments |
| 32 | +3. **Instrument layer** (`core/*_instrument.py`) — `SIOCaptchaInstrument` (sync) and `AIOCaptchaInstrument` (async) handle all HTTP, retries, and polling |
| 33 | +4. **Support layer** (`core/serializer.py`, `core/enum.py`, `core/const.py`) — `msgspec.Struct` classes, enums, constants |
| 34 | + |
| 35 | +**Forbidden**: service files importing `requests`/`aiohttp` directly; support layer depending on upper layers. |
| 36 | + |
| 37 | +## Change rules |
| 38 | + |
| 39 | +- Every new captcha service must inherit `CaptchaParams`, provide `captcha_handler()` + `aio_captcha_handler()`, and register its type in `CaptchaTypeEnm` |
| 40 | +- All serialization must use `msgspec.Struct` — never raw `json` module |
| 41 | +- Dual sync/async is mandatory for any new instrument or service |
| 42 | +- Service classes are thin: only `__init__` with captcha-type-specific params; all HTTP goes through instruments |
| 43 | +- Context manager support (`with` / `async with`) is required on all service classes |
| 44 | + |
| 45 | +## Validation |
| 46 | + |
53 | 47 | ```bash |
54 | | -# Development |
55 | | -uv sync --all-groups # Install all dependencies |
56 | | -uv run pytest tests/ # Run tests |
57 | | -uv run black src/ tests/ # Format |
58 | | -uv run isort src/ tests/ # Sort imports |
59 | | - |
60 | | -# Build & Publish |
61 | | -uv build # Build wheel/sdist |
62 | | -uv publish # Upload to PyPI |
63 | | - |
64 | | -# Documentation |
65 | | -cd docs/ && uv run --group docs make html -e |
| 48 | +make tests # pytest + coverage (HTML + XML) |
| 49 | +make refactor # autoflake + black + isort on src/ and tests/ |
| 50 | +make lint # autoflake --check, black --check, isort --check-only |
| 51 | +uv run pytest tests/ -k <name> # run specific tests |
66 | 52 | ``` |
67 | 53 |
|
68 | | -## NOTES |
69 | | -- **API Key**: Tests require `API_KEY` environment variable |
70 | | -- **Coverage**: HTML reports in `coverage/html/`, XML in `coverage/coverage.xml` |
71 | | -- **Python Support**: 3.8–3.12 (tested via `target-version = ['py310']`) |
72 | | -- **Dependencies**: `requests>=2.21.0`, `aiohttp>=3.9.2`, `msgspec>=0.18,<=0.21`, `tenacity>=8,<10` |
| 54 | +Tests require the `API_KEY` environment variable. Rate-limiting fixtures (`delay_func` 1s, `delay_class` 2s) prevent API throttling. |
| 55 | + |
| 56 | +## Key docs |
| 57 | + |
| 58 | +- `ARCHITECTURE.md` — full system map, data flow, invariants |
| 59 | +- `src/python3_capsolver/AGENTS.md` — service-level conventions |
| 60 | +- `src/python3_capsolver/core/AGENTS.md` — core module internals |
| 61 | +- `tests/AGENTS.md` — test patterns and fixtures |
| 62 | + |
| 63 | +## Repository-specific gotchas |
| 64 | + |
| 65 | +- **Empty `__init__.py` files**: users import via full path (`from python3_capsolver.recaptcha import ReCaptcha`), never from top-level package |
| 66 | +- **`AGENTS.md` in package dirs**: these ship with the wheel unless excluded in `pyproject.toml` — do not add more inside `src/` |
| 67 | +- **`control.py` is the largest file** (~431 lines) and provides direct API access without the captcha-handling abstraction |
| 68 | +- **Toolchain is `uv`**: use `uv run`, `uv sync`, `uv build` — not bare `pip` or `pytest` |
| 69 | +- **`captcha_instrument.py` is ~9.3k lines**: contains both `CaptchaInstrumentBase` and `FileInstrument`; edits here affect all services |
0 commit comments