系统架构
Mirobody Health 采用模块化、分层架构,将关注点分离,并支持轻松扩展。核心组件
API Layer
基于 FastAPI 的 REST API,用于对外集成
Provider System
面向健康设备的可插拔 OAuth 集成
Data Pipeline
健康数据转换的 ETL pipeline
AI Services
面向对话的多 LLM provider 集成
MCP Protocol
面向 AI agents 的 Model Context Protocol
Storage Layer
PostgreSQL 与 Redis 用于数据持久化
架构分层
1. API Layer
API Layer 为系统交互提供 RESTful endpoints:- 使用 FastAPI 进行异步请求处理
- 自动生成 API 文档(OpenAPI/Swagger)
- 为 Web clients 提供 CORS 支持
- 使用 Pydantic models 进行请求校验
- 基于 JWT 的认证
2. Provider System
面向健康设备集成的可插拔架构: Components:BaseThetaProvider: Abstract base class for all providersThetaDataFormatter: Standard data transformation utilitiesThetaTimeUtils: Timezone and timestamp handling- Provider-specific implementations (Garmin, Whoop, etc.)
3. Data Pipeline (TBD)
健康数据的 ETL(Extract, Transform, Load)pipeline:- Pull:从 vendor API 拉取数据
- Save:将 raw 数据保存到数据库
- Format:转换为标准格式
- Upload:推送到 Mirobody platform
4. AI Services
面向智能健康洞察的多 LLM provider 集成:- Provider-agnostic 接口(coming later)
- 对话上下文管理
- Tool calling(访问健康数据)
- 会话历史持久化
5. MCP Protocol
支持通过 Model Context Protocol 集成 AI agents:- JSON-RPC 2.0 接口
- Tool 发现与执行
- Resource 管理
- Agent 编排
6. Storage Layer
面向不同数据类型的双存储系统:- PostgreSQL
- Redis
Used for:
- 用户凭据(加密)
- OAuth tokens 与 refresh tokens
- 健康 raw 数据(JSONB)
- Provider 配置
- 会话历史
Data Flow
OAuth Authentication Flow
Health Data Synchronization
Design Patterns
Plugin Architecture
Providers are discovered and loaded dynamically:Factory Pattern
Providers use factory methods for conditional instantiation:Strategy Pattern
Different OAuth strategies for OAuth 1.0 vs 2.0:Repository Pattern
Database operations abstracted through services:安全架构
Authentication & Authorization
Authentication & Authorization
Data Protection
Data Protection
- 数据库级别加密
- 加密 OAuth tokens
- 外部通信使用 HTTPS
- 不记录敏感数据
Access Control
Access Control
- 用户范围的数据访问
- Provider 级权限控制
- API rate limiting
- CORS policies
Secure Communication
Secure Communication
- 外部 API 使用 TLS/SSL
- OAuth state 参数防 CSRF
- Token refresh 机制
- 安全的 session 管理
可扩展性考虑
Horizontal Scaling
系统支持水平扩展:Stateless API
API servers 无状态,可在负载均衡后水平扩展
Distributed Cache
Redis 支持集群,用于分布式缓存
Database Pooling
连接池支持多实例 API 并发访问数据库
Async Processing
async/await 提升单实例并发能力
Deployment Architecture
Docker Compose Architecture
Monitoring & Observability
Logging
Structured logging throughout the system:Metrics
Track key performance indicators:- Request latency
- Provider success rates
- Data processing throughput
- Error rates by provider
- Token refresh rates
Health Checks
Technology Stack
- Backend
- Storage
- Integration
- DevOps
- Python 3.12+: Core language
- FastAPI: Web framework
- asyncio: Async I/O
- Pydantic: Data validation
- SQLAlchemy: Database ORM
扩展点
该架构提供多个扩展点:New Providers
New Providers
新增健康设备集成请参见开发:
Tools & MCPs
Tools & MCPs
- See Adding Custom Tools to add new tools for AI agents
- See Adding Custom MCPs to add new MCPs for AI agents
Custom Indicators
Custom Indicators
添加新的健康指标:
- Define in
StandardIndicatorenum - Specify standard unit
- Update provider mappings
AI Providers
AI Providers
添加新的 LLM providers:
- Implement provider interface
- Add configuration
- Register in agent system
- Deploy
最佳实践
Separation of Concerns
每一层只负责单一职责
Dependency Injection
通过依赖注入使用 services,而不是硬编码
Interface-Based Design
面向接口编程,而非面向实现
Configuration-Driven
行为通过配置驱动
Async First
所有 I/O 使用 async/await
Error Handling
优雅降级与恢复