Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions .claude/CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# CLAUDE.md — 全局开发宪法

## 一、 核心准则 (Core Principles)

- **语言偏好**:对话交互、需求分析、说明文档统一使用 **中文**;代码注释、技术文档、Git Commit 使用 **英文**。
- **能力优先**:优先调用系统 Skill/Tools 处理任务(如文件读写、搜索、测试运行,技术栈选择等)。
- **代码优先**:优先通过代码了项目实际情况,项目的文档仅供参考补充。
- **LTS 支持**:优先使用最新的 LTS 技术栈,在确保系统的稳定性的前提下合理使用新特性提高体验。
- **架构哲学**:
- **前后端分离**:天然解耦,API 驱动。
- **Monorepo 组织**:单一仓库管理多个包(如apps/packages),利用工具(如 Turborepo, Nx, pnpm workspace)优化依赖。
- **可读性 > 简洁性 > 性能**:代码是写给人看的,禁止使用晦涩的 One-liners。
- **显式胜于隐式**:严禁元编程黑魔法,确保静态分析工具(LSP)能准确跳转。

---

## 二、 编码规范 (Coding Standards)

### 1. 逻辑与结构 (KISS & SOLID)

- **函数约束**:原则上单个函数不超过 **50 行**。
- **深度限制**:严禁嵌套超过 **3 层**。
- **早退模式 (Guard Clauses)**:

```typescript
// 推荐
if (!user) return;
if (!hasPermission) throw new Error('...');
doSomething();
```

- **解耦冗余权衡**:若抽象会导致不相关的模块产生硬耦合,优先选择代码冗余。

### 2. 类型安全 (Type Safety)

- **禁止 `any**`类型**:所有未知类型必须使用 `unknown` 并配合类型守卫(Type Guards)。
- **契约优先**:所有外部输入(API、用户输入)必须有 `interface` 或 `schema`(如 Zod, Yup)。
- **副作用标注**:所有异步或产生副作用的函数必须明确定义返回类型 `Promise<T>`。

### 3. 错误处理 (Error Handling)

- **禁止静默捕获**:`catch` 块必须至少包含日志上报(Error level)。
- **结构化异常**:使用自定义异常类,包含错误码(ErrorCode)和上下文数据。

---

## 三、 工程化与质量保证 (Engineering & QA)

### 1. TDD 流程 (Red-Green-Refactor)

- **步骤**:先在 `tests/` 编写失败测试 -> 实现最小化功能使测试通过 -> 重构代码。
- **模式**:遵循 **AAA 模式** (Arrange, Act, Assert)。
- **隔离**:单元测试严禁依赖真实数据库或外部网络,必须使用 Mock。

### 2. 可观测性日志 (Logging)

- **格式**:统一使用 **JSON** 格式。
- **日志持久化**:日志文件必须存储在 `logs/` 目录下,并按天分割。
- **标准字段**:`timestamp`, `level`, `message`, `source`, `context`, `traceId`。
- **级别规范**:
| 级别 | 场景 |
| :--- | :--- |
| **DEBUG** | 开发环境下的详细运行轨迹 |
| **INFO** | 关键业务路径(如:订单已创建) |
| **WARN** | 可恢复的异常(如:网络重试) |
| **ERROR** | 需人工介入的错误(如:数据库断开) |
| **CRITICAL** | 系统级崩溃 |

### 3. 安全编码 (Security)

- **输入过滤**:对所有输入进行白名单验证。
- **敏感数据**:日志记录时必须对手机号、邮箱、密码进行脱敏处理(Masking)。
- **配置解耦**:`.env` 仅存储非敏感开发配置,生产敏感词使用 Secrets 管理器。

### 4. 测试原则 (Testing Principles)

- **不掩盖问题**:测试用例失败先考虑是否是代码问题,再考虑是否是测试用例问题。
- **覆盖率**:单元测试覆盖率目标不低于 80%。
- **独立性**:测试用例之间相互独立,不依赖外部状态。
- **可读性**:测试用例必须有描述性,便于定位问题。

---

## 四、 交互与协作流程 (Workflow)

### 1. Git 分支管理

- **任务隔离**:开始新任务前创建 `feat-xxx` 或 `fix-xxx` 分支。
- **原子提交**:一次提交只解决一个问题,解决一个问题就提交一次。

### 2. Commit Message 规范

格式:`<type>(<scope>): <description>`

- `feat`: 新功能
- `fix`: 修复 Bug
- `docs`: 文档变更
- `style`: 格式化(不影响逻辑)
- `refactor`: 重构
- `test`: 增加/修改测试
- `chore`: 构建过程或辅助工具的变动

---

## 五、 部署与运维 (Ops)

- **基础设施持久化**:基础设施(如数据库、缓存、消息队列、模型)必须持久化存储,统一存放于 `infra/` 目录下。
- **容器化**:所有服务必须提供 `Dockerfile`,且镜像包含完整运行环境。
- **CI/CD**:每次 Push 必须触发代码风格检查(Lint)、类型检查和单元测试。
- **版本发布**:遵循语义化版本 (SemVer),release 必须打 Tag。

---

## 六、 参考资料 (References)

- [GitHub Actions Workflows](https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions)
- [Dockerfile Reference](https://docs.docker.com/engine/reference/builder/)
61 changes: 61 additions & 0 deletions .claude/settings.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"permissions": {
"allow": [
"Bash(find:*)",
"Bash(wc:*)",
"Bash(npm run build:*)",
"Bash(git add:*)",
"Bash(git commit:*)",
"Bash(pnpm --version)",
"Bash(pnpm install)",
"Bash(pnpm build:*)",
"Bash(pnpm add:*)",
"Bash(for:*)",
"Bash(done:*)",
"Bash(dir:*)",
"Bash(findstr:*)",
"Bash(ls:*)",
"Bash(pnpm outdated)",
"Bash(pnpm list:*)",
"Bash(pnpm type-check)",
"Bash(pnpm lint:*)",
"Bash(del:*)",
"Bash(powershell:*)",
"Bash(npx eslint:*)",
"Bash(pnpm remove:*)",
"Bash(Select-String:*)",
"Bash(Select-Object:*)",
"Bash(npm view:*)",
"Bash(pnpm install:*)",
"Bash(timeout:*)",
"Bash(pnpm check:*)",
"mcp__plugin_context7_context7__resolve-library-id",
"mcp__plugin_context7_context7__query-docs",
"Bash(git checkout:*)",
"Bash(pnpm exec:*)",
"Bash(node:*)",
"Bash(npx @tailwindcss/upgrade:*)",
"Bash(pnpm format:check:*)",
"Bash(pnpm view:*)",
"Bash(taskkill:*)",
"Bash(tasklist:*)",
"Bash(netstat:*)",
"Bash(pnpm dev:*)",
"Bash(chmod:*)",
"Bash(pnpm test:unit:*)",
"Bash(pnpm test:*)",
"Bash(pnpm test:e2e:*)",
"Bash(git ls-tree:*)",
"Bash(pnpm test:coverage:*)",
"Bash(pnpm eslint:*)",
"Bash(pnpm vitest:*)",
"Bash(pnpm tsc:*)",
"Bash(npx tsc:*)",
"Bash(bash scripts/verify-logger.sh:*)",
"Bash(if not exist:*)",
"Bash(pnpm run build:*)",
"Bash(tail:*)",
"Bash(pnpm diagnostic:*)"
]
}
}
89 changes: 69 additions & 20 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,22 +1,71 @@
# LLM Configuration
# 支持的提供商: deepseek, qwen, ollama
# ============================================
# Formative Environment Configuration
# ============================================
# Copy this file to .env.local and fill in your values
# IMPORTANT: .env.local is ignored by git (contains sensitive data)

# ============================================
# LLM Configuration (REQUIRED)
# ============================================
# Your LLM provider API key
# Get your API key from: https://platform.deepseek.com/api_keys
LLM_API_KEY=your_deepseek_api_key_here

# LLM provider: deepseek, qwen, ollama, or mimo
LLM_PROVIDER=deepseek

# LLM model to use
# DeepSeek: deepseek-chat, deepseek-coder
# Qwen: qwen-plus, qwen-turbo
# Ollama: llama2, mistral, codellama
# Mimo: check their documentation
LLM_MODEL=deepseek-chat
LLM_API_KEY=your_api_key_here
LLM_BASE_URL=https://api.deepseek.com/v1

# Redis Configuration
# 格式: redis://username:password@host:port
REDIS_URL=redis://localhost:6379

# Optional: 如果使用Qwen
# LLM_PROVIDER=qwen
# LLM_MODEL=qwen-plus
# LLM_API_KEY=your_qwen_api_key
# LLM_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1

# Optional: 如果使用本地Ollama
# LLM_PROVIDER=ollama
# LLM_MODEL=qwen2.5:7b
# LLM_BASE_URL=http://localhost:11434/v1
# LLM_API_KEY= # Ollama不需要API Key,可留空

# Optional: Custom LLM API base URL
# Leave empty to use the default URL for your provider
# DeepSeek: https://api.deepseek.com/v1
# Qwen: https://dashscope.aliyuncs.com/compatible-mode/v1
# Mimo: https://api.xiaomimimo.com/v1
# Ollama: http://localhost:11434/v1
LLM_BASE_URL=

# ============================================
# Environment
# ============================================
NODE_ENV=development

# ============================================
# Rate Limiting (P0 Security Optimization)
# ============================================
# Enable rate limiting for API endpoints
RATE_LIMIT_ENABLED=true
# Rate limit window in milliseconds (default: 60000 = 1 minute)
RATE_LIMIT_WINDOW_MS=60000
# Maximum requests per window (default: 30)
RATE_LIMIT_MAX_REQUESTS=30

# ============================================
# Secret Management (P0 Security - Production)
# ============================================
# Secret source: env (default), vault, aws_secrets_manager, azure_key_vault, gcp_secret_manager
SECRET_SOURCE=env

# HashiCorp Vault Configuration (if using vault)
# VAULT_ADDR=https://vault.example.com
# VAULT_TOKEN=your_vault_token
# VAULT_SECRET_PATH=secret

# AWS Secrets Manager Configuration (if using aws_secrets_manager)
# AWS_REGION=us-east-1
# AWS_ACCESS_KEY_ID=your_access_key
# AWS_SECRET_ACCESS_KEY=your_secret_key

# Azure Key Vault Configuration (if using azure_key_vault)
# AZURE_KEY_VAULT_URI=https://your-vault.vault.azure.net/
# AZURE_CLIENT_ID=your_client_id
# AZURE_CLIENT_SECRET=your_client_secret
# AZURE_TENANT_ID=your_tenant_id

# GCP Secret Manager Configuration (if using gcp_secret_manager)
# GCP_PROJECT_ID=your_project_id
# GCP_CREDENTIALS=path/to/credentials.json
23 changes: 0 additions & 23 deletions .env.local.example

This file was deleted.

Loading