OpenHands 源码解析 · 第 1 讲
OpenHands 源码解析 第 1 讲:整体架构与定位 基于 master 分支源码 · 2026-07-28 一、OpenHands 是什么 OpenHands 的官方定位是 "The self-hosted developer control center for coding agents and automations" —— 自托管的开发者控制中心。 🔹 Python 3.12+ / FastAPI 后端 + React 前端 🔹 多 Agent 支持(ACP 协议):OpenHands、Claude Code、Codex 等 🔹 多 Runtime 支持:Docker、K8s、远程 VM、本地 🔹 完整事件溯源 + 断点续跑 📦 源码仓库 https://github.com/All-Hands-AI/OpenHands 本地路径: ~/Applications/OpenHands/ · 约 4 万行 Python 二、核心数据 指标 数据 提交数 18,706+ Python 文件 234 个 代码行数 40,226 行 Web 框架 FastAPI 前端 React + Vite 协议 MIT 三、整体架构 React Frontend (SPA) /api/v1/* ←→ FastAPI Backend 13 个路由模块(conversation/sandbox/event/settings/secrets/user/git/webhook/mcp...) Service 层(Conversation/Sandbox/Event/Config/JWT/DB) Storage(PostgreSQL + FileStore: S3/GCS/Local) Sandboxes(Docker / K8s / Remote / Local) 四、入口:app.py(86 行) openhands/app_server/app.py 是整个后端的入口,结构清晰: # app.py 第 54-72 行 app = FastAPI ( title = 'OpenHands' , description = 'OpenHands: Code Less, Make More' , version = get_version (), lifespan = combine_lifespans (*lifespans), routes =[ Mount (path= '/mcp' , app=mcp_app)], ) app. include_router (v1_router.router) app. include_router (health_router) if os. getenv ( 'SERVE_FRONTEND' ) == 'true' : app. mount ( '/' , SPAStaticFiles (...)) 关键设计: 🔹 combine_lifespans 合并 MCP 服务器和应用生命周期 🔹 三个中间件:CORS → CacheControl → RateLimit(10 req/s) 🔹 SPA 静态文件服务,非 API 路径回退到 index.html 五、13 个路由模块 所有 V1 API 挂载在 /api/v1 ,由 v1_router.py (37 行)组装: router.include_router(event_router) # 事件 router.include_router(app_conversation_router) # 会话 router.include_router(sandbox_router) # 沙箱 router.include_router(settings_router) # 设置 router.include_router(secrets_router) # 密钥 router.include_router(user_router) # 用户 router.include_router(skills_router) # 技能 router.include_router(webhook_router) # Webhook router.include_router…
阅读全文:OpenHands 源码解析 第 1 讲