mirror of
https://github.com/openswarm-ai/openswarm.git
synced 2026-09-25 11:04:54 +02:00
3.0 KiB
3.0 KiB
backend/CLAUDE.md
FastAPI orchestrator. Entry: backend/main.py (uvicorn :8324, REST /api/*, WS /ws/*, Swagger /docs). See root CLAUDE.md for repo-wide constraints.
Coding precedences
Full precedences live in root CLAUDE.md. Always: understand the end goal before coding (what does the user actually need?); reuse before you write (grep existing routes / SubApps / helpers — most needs already have one); ~300 LOC/file ceiling; downward-tree imports; no comments except WHY-non-obvious; test after meaningful changes; weigh speed, efficiency, robustness, UX, security on every change.
Run / test
- Dev:
bash backend/run.sh(creates.venv/, installsrequirements.txt, runs uvicorn with--reload). - Tests:
pip install -r requirements-dev.txt && pytest tests/. requirements-dev.txtis deliberately kept out ofrequirements.txtsopytestetc. don't ship in the production DMG. Sync both files when adding deps that need to exist in either place.
Layout
apps/agents/— agent orchestration, WS manager, MCP plumbing.providers/registry.py— resolves primary + aux models across Anthropic / OpenAI / Google / OpenRouter / custom OpenAI-compatible providers. Always go through here; never hardcode a model ID.mcp_preflight.py— vague-prompt classifier that surfaces the one-click MCP-connect modal.mcp_meta_server.py,mcp_registry.py— MCP discovery + registry.9router_gpt5_patch.js— patch loaded into 9router to translate OpenAImax_tokenssemantics.
apps/nine_router.py— supervises the 9router subprocess on:20128.apps/subscription/router.py— OAuth + Stripe callbacks for openswarm-pro signup.apps/outputs/— view renderer (HTML/JS/CSS iframes, sandboxed Python execution).auth.py— per-install bearer token. Generated BEFORE the HTTP bind so the Electron shell can read it from disk; don't reorder.
MCP gate
- Dispatch flows through
_build_mcp_servers. This is the only place MCP tools become reachable. session.active_mcpsdefaults to empty; the user opts in viaMCPSearch+MCPActivate(HITL).- New MCP-related code path? It must respect this gate. Don't add side channels.
- Suggestions surfaced to users must come from the vetted/default set — not the full upstream registry.
Providers / models
- Primary model: per-session user choice, resolved by
providers.registry. - Aux model (preflight, classifier, summarizers): pick the cheap tier of the user's configured provider — Haiku for Anthropic, GPT-5-mini for OpenAI, Gemini Flash for Google, etc. Never hardcode Haiku.
Common pitfalls
- New endpoint? Use a pydantic request/response model and
@typechecked. - Pinning matters —
requirements.txtis fully pinned for reproducibility. - Token middleware already scrubs bearer tokens from logs; don't re-add raw logging.
- MCP bundles in
mcp-bundles/are esbuild output; regenerate via the bundle script rather than editing.