wangdl 49151117c8
Some checks failed
Deploy API Server / build-and-unit (push) Successful in 32s
Deploy API Server / current-integration (push) Failing after 1m34s
Deploy API Server / backward-compat (push) Successful in 16s
Deploy API Server / m-ai-03-synthetic-e2e (push) Failing after 12s
Deploy API Server / deploy (push) Has been skipped
fix: M-AI-03-GATE-FIX — Admin Retry 统一链路 + 队列测试修正 + E2E 激活 + CI 门禁
### 一、生命周期契约核对
- ADR-003 §1.1: 5 状态(queued/running/succeeded/failed/cancelled)
- cancel_requested = cancelRequestedAt 信号(非状态)
- created/retrying 不在状态机中
- 代码完全一致 

### 二、Admin Retry 修复
- retryJob 不再绕过 Registry/CreationService/Outbox
- 改为调用 AiJobCreationService.createJob()(统一事务链路)
- parentJobId + triggerType=admin_manual 正确传入
- 幂等键: admin-retry:<jobId>:<timestamp>(允许不同重试产生不同 Job)
- 原 Job 不修改,新 Job 使用新 ID
- 新增 AiJobCreationService.retrySnapshotContent 支持复用 Snapshot

### 三、队列定义测试修正
- 旧队列断言保持 6 个 legacy queues 的默认值检查
- 新增 M-AI-03 per-queue 断言(ai-interactive: concurrency=2/attempts=2/backoff=2000; ai-background: concurrency=1/attempts=3/backoff=5000/lockDuration=60s)
- lockDuration 测试改为 >= 30s(允许 per-queue 差异)

### 四、Synthetic E2E 激活
- test/m-ai-03-synthetic.e2e-spec.ts: 12 真实场景(创建/幂等/原子/cancel/JWT/隔离/脱敏/未知type/状态机)
- test/jest-m-ai-03.json: 真实 infra config(无 mock Prisma/BullMQ/Redis)
- CI Workflow 新增 m-ai-03-synthetic-e2e job
- deploy needs 新增 m-ai-03-synthetic-e2e 依赖

### 五、测试结果
- 单元测试: 471/471 passed, 0 failed
- E2E: 12 场景待 CI 真实 infra 执行
- queue-definitions: 19/19 passed

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-21 10:47:09 +08:00
2026-05-04 16:09:01 +08:00
2026-05-04 16:09:01 +08:00
2026-05-04 16:09:01 +08:00

AI Study Product API

AI 学习产品的后端 API 服务v0.1),基于 NestJS + TypeScript 构建。

功能模块

模块 说明 状态
health 健康检查
auth 用户认证dev-login / Apple / 刷新 / 登出)
users 用户管理
knowledge-base 知识库 CRUD → Prisma
knowledge-items 知识点 CRUD → Prisma
ai AI 三层架构Provider → Gateway → Workflow15 文件
ai-analysis AI 分析结果查询 → Prisma
active-recall 主动回忆(提交答案触发分析)→ Prisma
learning-session 学习会话 → Prisma
review 间隔重复复习 → Prisma
focus-items 待巩固项 → Prisma
learning-activity 学习活跃统计 → Prisma
document-import 文档导入 → Prisma
notifications 通知 → Prisma
feedback 用户反馈 → Prisma
waitlist 等待名单 → Prisma

快速开始

# 安装依赖
npm install

# 开发模式
npm run start:dev

# 生产模式
npm run build && npm run start:prod

接口文档

本地开发环境

启动服务后,访问:

本地开发环境默认启用 Swagger无需认证。

服务器内测环境

生产环境启用 Swagger 后Swagger UI 和 OpenAPI JSON 均受 Basic Auth 保护:

Apifox 导入时在 URL 中带上认证: https://admin:your_password@api.longde.cloud/api-docs-json

配置环境变量:

NODE_ENV=production
ENABLE_SWAGGER=true
SWAGGER_USER=your_admin_username
SWAGGER_PASSWORD=your_secure_password

如需临时开启内网访问,配置同上并设置强密码。

环境变量

复制 .env.example.env 并配置:

PORT=3000
DATABASE_URL="mysql://user:password@localhost:3306/ai_study"
REDIS_HOST=localhost
REDIS_PORT=6379
AI_PROVIDER=mock
AI_API_KEY=
AI_BASE_URL=
JWT_SECRET=change_me_in_production

NODE_ENV=development
ENABLE_SWAGGER=true
SWAGGER_USER=admin
SWAGGER_PASSWORD=change_me
变量 说明 默认值
PORT 服务端口 3000
NODE_ENV 运行环境 development
ENABLE_SWAGGER 是否启用 Swagger true (开发) / false (生产)
SWAGGER_USER Swagger 用户名 admin
SWAGGER_PASSWORD Swagger 密码 change_me

API 端点概览

健康检查

  • GET / - 服务健康检查

认证

  • POST /auth/login - 用户登录
  • POST /auth/register - 用户注册
  • POST /auth/refresh - 刷新 Token
  • POST /auth/logout - 退出登录

用户

  • POST /users - 创建用户
  • GET /users/:id - 获取用户信息
  • GET /users/:id/profile - 获取用户详情(含统计数据)
  • PATCH /users/:id - 更新用户信息
  • GET /users - 用户列表

知识库

  • GET /knowledge/categories - 获取分类
  • GET /knowledge/articles - 获取文章列表
  • GET /knowledge/articles/:id - 获取文章详情

学习

  • GET /learning/paths - 获取所有学习路径
  • GET /learning/paths/:id - 获取学习路径详情
  • GET /learning/paths/:id/courses - 获取课程列表
  • GET /learning/courses/:id - 获取课程详情
  • GET /learning/lessons/:id - 获取课时详情
  • GET /learning/records?userId= - 获取用户学习记录
  • PATCH /learning/progress - 更新学习进度

AI

  • POST /ai/analyze-recall - AI 主动回忆分析
  • GET /ai/models - 查看模型分流配置
  • POST /ai-analysis - 提交 AI 分析
  • GET /ai-analysis/:id - 获取分析结果

主动回忆

  • GET /active-recalls - 获取问题列表
  • POST /active-recalls/:id/submit - 提交回答(自动触发 AI 分析)

复习

  • GET /learning/review?userId= - 获取复习任务
  • POST /learning/review - 创建复习任务

反馈

  • POST /feedback - 提交反馈
  • GET /feedback - 获取反馈列表
  • GET /feedback/stats - 反馈统计
  • PATCH /feedback/:id/status - 更新反馈状态

等待名单

  • POST /waitlist - 加入等待名单
  • GET /waitlist - 获取所有报名
  • GET /waitlist/stats - 报名统计

技术栈

  • NestJS 11.x
  • TypeScript
  • class-validator / class-transformer
  • @nestjs/swagger

目录结构

src/
├── main.ts                  # 入口
├── app.module.ts            # 根模块
├── config/                  # 配置app / jwt / ai / database / redis
├── common/                  # 公共guard / decorator / filter / pipe
├── infrastructure/          # 基础设施Prisma / Redis / Storage / Logger / Queue
│   └── ai/                  # ❌ 已删除,迁至 modules/ai/
└── modules/
    ├── auth/                # 认证模块dev-login / Apple / refresh / logout
    ├── users/               # 用户模块
    ├── knowledge-base/      # 知识库模块
    ├── knowledge-items/     # 知识点模块
    ├── active-recall/       # 主动回忆模块(提交答案 → 触发 AI 分析)
    ├── ai/                  # ✅ 三层 AI 架构Provider → Gateway → Workflow
    │   ├── gateway/         # AiGatewayService选模型、记日志、JSON容错、超时重试
    │   ├── providers/       # DeepSeek / MiniMax / Mock
    │   ├── prompts/         # System Prompt + Zod Schema
    │   ├── usage/           # UsageLog + CostCalculator
    │   └── workflows/       # ActiveRecallAnalysisWorkflow
    ├── ai-analysis/         # AI 分析结果查询
    ├── review/              # 复习模块
    ├── learning-session/    # 学习会话
    ├── learning-activity/   # 学习活跃
    ├── focus-items/         # 待巩固项
    ├── document-import/     # 文档导入
    ├── notifications/       # 通知
    ├── feedback/            # 用户反馈
    ├── waitlist/            # 等待名单
    └── system/              # 系统状态

后续规划

  • 接入真实 AI APIDeepSeek + MiniMax 三层架构)
  • JWT 认证中间件
  • 数据库持久化Prisma + MySQL
  • 12 个业务 Repository 从内存 Map 迁到 Prisma
  • 全局 JwtAuthGuard
  • Swagger 端点 Basic Auth 保护
  • 更多 AI Workflow知识导入、费曼分析、复习卡片生成
  • AI 联调 + Prompt 调优
  • 添加 Redis 缓存
  • iOS 集成
Description
No description provided
Readme 6.4 MiB
Languages
TypeScript 99.3%
Shell 0.6%