This document provides detailed instructions for setting up and running the JoySafeter in development mode.
- Python 3.12+ with uv package manager
- Node.js 20+ with npm, pnpm, or bun
- PostgreSQL 15+
- Redis (optional, for caching)
- Docker (optional, for containerized development)
在提交代码前,必须在仓库根目录执行以下脚本,将 pre-commit 与后端 UV 环境绑定并安装 Git hooks:
# 在仓库根目录执行(需已安装 uv)
./scripts/setup-pre-commit.sh执行后,每次 git commit 将自动运行代码校验。手动全量检查:./scripts/run-pre-commit.sh 或 backend/.venv/bin/python -m pre_commit run --all-files。
Using Docker (recommended):
cd backend/docker
./start.shOr manually start PostgreSQL and Redis on your system.
cd backend
# Create and activate virtual environment
uv venv
source .venv/bin/activate # Linux/macOS
# .venv\Scripts\activate # Windows
# Install dependencies
uv sync --dev
# Configure environment
cp env.example .env
# Edit .env with your settings
# Note: UV uses Tsinghua mirror by default (configured in uv.toml)
# You can customize via UV_INDEX_URL environment variable
# Run database migrations
alembic upgrade head
# Start development server
uv run uvicorn app.main:app --reload --host 0.0.0.0 --port 8000Backend will be available at http://localhost:8000
项目默认使用清华大学镜像源 (https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple) 以加速依赖安装。配置方式:
-
环境变量 (优先级最高):
export UV_INDEX_URL=https://pypi.org/simple # 使用官方源
-
.env文件: 在.env中设置UV_INDEX_URL变量 -
配置文件:
- 编辑
backend/pyproject.toml中的[tool.uv]部分 (推荐) - 编辑
backend/uv.toml中的[index]部分
- 编辑
The project uses Tsinghua mirror by default. You can customize it via:
- Environment variable:
UV_INDEX_URL(highest priority) .envfile: SetUV_INDEX_URLvariable- Configuration file:
pyproject.tomloruv.toml
cd frontend
# Install dependencies
bun install # or: npm install / pnpm install
# Configure environment
cp env.example .env.local
# Edit .env.local with your settings
# Start development server
bun run dev # or: npm run devFrontend will be available at http://localhost:3000
# Backend tests
cd backend
pytest tests/ -v
# With coverage
pytest tests/ --cov=app --cov-report=html
# Frontend tests
cd frontend
npm run test# Backend
cd backend
ruff check . # Lint
ruff format . # Format
mypy app # Type check
# Frontend
cd frontend
npm run lint # ESLint
npm run type-check # TypeScript项目使用 pre-commit hooks 来确保代码质量。在提交代码之前,会自动运行代码检查。pre-commit 与后端 UV 环境绑定,请通过 Quick Start 中的 安装 Pre-commit Hooks(必须) 步骤完成安装。
在仓库根目录执行(需已安装 uv):
./scripts/setup-pre-commit.sh该脚本会执行:cd backend && uv sync --dev、uv run pre-commit install --install-hooks 等,无需单独安装全局 pre-commit。
后端检查:
- Ruff Lint - 自动修复可修复的代码问题
- Ruff Format - 检查代码格式
- Ruff Check (严格模式) - 强制检查,不允许任何 lint 错误 (
uv run ruff check .) - MyPy - Python 类型检查
前端检查:
- ESLint - JavaScript/TypeScript 代码检查 (
pnpm run lint)
通用检查:
- 行尾空白检查
- 文件末尾换行检查
- YAML/JSON 格式检查
- 大文件检查
- 合并冲突检查
- 私钥检测
正常提交流程:
当你执行 git commit 时,pre-commit hooks 会自动运行:
git add .
git commit -m "your message"如果检查失败,提交会被阻止。你需要:
- 修复报告的错误
- 重新添加文件 (
git add .) - 再次提交
手动运行检查:
# 检查所有文件(使用后端 UV 环境)
backend/.venv/bin/python -m pre_commit run --all-files
# 检查暂存的文件(在仓库根目录,需已通过上述脚本安装 hook)
pre-commit run
# 检查特定 hook
pre-commit run ruff --all-files
pre-commit run frontend-lint --all-files跳过 Hooks(不推荐):
如果确实需要跳过 hooks(例如紧急修复),可以使用:
git commit --no-verify -m "emergency fix"注意: 跳过 hooks 会绕过代码质量检查,可能导致 CI 失败。
问题:uv run ruff check 找不到命令
解决方案:
- 确保已安装 uv:
curl -LsSf https://astral.sh/uv/install.sh | sh - 确保 backend 目录下有虚拟环境:
cd backend && uv venv - 确保已安装依赖:
cd backend && uv sync --dev
问题:pnpm run lint 找不到命令
解决方案:
- 确保已安装 pnpm:
npm install -g pnpm - 确保 frontend 目录下已安装依赖:
cd frontend && pnpm install
问题:Hooks 运行太慢
解决方案:
- Hooks 默认只检查更改的文件
- 如果需要跳过某些检查,可以临时使用
--no-verify - 考虑优化检查配置,排除不需要检查的文件
# 更新 hooks 到最新版本
backend/.venv/bin/python -m pre_commit autoupdate
# 然后重新安装
backend/.venv/bin/python -m pre_commit install --install-hooks更多详细信息请参考 Pre-commit Setup Guide。
cd backend
# Create a new migration
alembic revision --autogenerate -m "description"
# Apply migrations
alembic upgrade head
# Rollback one migration
alembic downgrade -1agent-platform/
├── backend/ # FastAPI backend
│ ├── app/
│ │ ├── api/ # REST API routes
│ │ ├── core/ # Core business logic (agents, graphs)
│ │ ├── models/ # SQLAlchemy database models
│ │ ├── schemas/ # Pydantic schemas
│ │ ├── services/ # Business services
│ │ └── utils/ # Utilities
│ ├── alembic/ # Database migrations
│ └── tests/ # Test suite
│
├── frontend/ # Next.js frontend
│ ├── app/ # App Router pages
│ ├── components/ # React components
│ ├── lib/ # Utilities, API clients
│ ├── stores/ # Zustand state stores
│ └── services/ # API service layer
│
└── deploy/ # Deployment configurations
See backend/env.example and frontend/env.example for all available configuration options.
| Variable | Description |
|---|---|
POSTGRES_HOST |
PostgreSQL host address |
POSTGRES_PORT |
PostgreSQL port |
POSTGRES_USER |
PostgreSQL username |
POSTGRES_PASSWORD |
PostgreSQL password |
POSTGRES_DB |
PostgreSQL database name |
SECRET_KEY |
JWT signing key |
DEBUG |
Enable debug mode |
CORS_ORIGINS |
Allowed CORS origins |
| Variable | Description |
|---|---|
NEXT_PUBLIC_API_URL |
Backend API URL |
BETTER_AUTH_SECRET |
Auth secret key |
- Ensure PostgreSQL is running
- Check
POSTGRES_HOST,POSTGRES_PORT,POSTGRES_USER,POSTGRES_PASSWORD,POSTGRES_DBin.env - Verify database exists:
createdb joysafeter
- Clear Next.js cache:
rm -rf .next - Reinstall dependencies:
rm -rf node_modules && npm install - Check Node.js version:
node --version(should be 20+)
- Ensure virtual environment is activated
- Run
uv syncto install all dependencies - Check Python version:
python --version(should be 3.12+)
Recommended extensions:
- Python (Microsoft)
- Pylance
- ESLint
- Prettier
- Tailwind CSS IntelliSense
- Set Python interpreter to
.venv/bin/python - Enable Django/FastAPI support
- Configure Ruff as external tool
- Check GitHub Issues
- Read the Contributing Guide
- Review API Documentation