Files
AIclinicalresearch/docs/03-业务模块/SSA-智能统计分析/00-模块当前状态与开发指南.md
HaHafeng 371e1c069c feat(ssa): Complete QPER architecture - Query, Planner, Execute, Reflection layers
Implement the full QPER intelligent analysis pipeline:

- Phase E+: Block-based standardization for all 7 R tools, DynamicReport renderer, Word export enhancement

- Phase Q: LLM intent parsing with dynamic Zod validation against real column names, ClarificationCard component, DataProfile is_id_like tagging

- Phase P: ConfigLoader with Zod schema validation and hot-reload API, DecisionTableService (4-dimension matching), FlowTemplateService with EPV protection, PlannedTrace audit output

- Phase R: ReflectionService with statistical slot injection, sensitivity analysis conflict rules, ConclusionReport with section reveal animation, conclusion caching API, graceful R error classification

End-to-end test: 40/40 passed across two complete analysis scenarios.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-21 18:15:53 +08:00

211 lines
9.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# SSA智能统计分析模块 - 当前状态与开发指南
> **文档版本:** v2.0
> **创建日期:** 2026-02-18
> **最后更新:** 2026-02-21
> **维护者:** 开发团队
> **当前状态:** 🎉 **QPER 智能化主线闭环完成Q→P→E→R 端到端 40/40 通过**
> **文档目的:** 快速了解SSA模块状态为新AI助手提供上下文
>
> **🎉 重大里程碑2026-02-21**
> - ✅ **QPER 四层架构主线闭环** — Phase E+ / Q / P / R 全部完成93.5h 计划工时
> - ✅ **端到端测试 40/40 通过** — 两条完整链路(差异比较 + 相关分析)全部跑通
> - ✅ **LLM 智能意图理解** — 自然语言→四维信息提取Confidence=0.95
> - ✅ **配置化决策表驱动** — JSON 驱动方法选择,热更新 API方法学团队可配置
> - ✅ **LLM 论文级结论生成** — 6 要素结论 + 槽位注入反幻觉 + Zod 强校验 + 敏感性冲突准则
> - ✅ **四层降级体系** — 每层 LLM 失败时自动 fallback系统不中断
---
## 📊 模块概览
### 基本信息
| 项目 | 信息 |
|------|------|
| **模块名称** | SSA - 智能统计分析 (Smart Statistical Analysis) |
| **模块定位** | AI驱动的"白盒"统计分析系统 |
| **架构模式** | **QPER — Query → Planner → Execute → Reflection** |
| **商业价值** | ⭐⭐⭐⭐⭐ 极高 |
| **目标用户** | 临床研究人员、生物统计师 |
| **开发状态** | 🎉 **QPER 主线闭环完成Phase Deploy 待启动** |
### 核心目标
> 让**不懂统计的医生**完成**专业级的统计分析**。
>
> **三大特征**
> 1. **白盒**:用户完全理解 AI 做了什么,为什么这样做
> 2. **严谨**:统计护栏自动检测前提条件,违规时自动降级
> 3. **可交付**:生成论文级结论 + 可在本地运行的 R 代码,支持审计复现
---
## 🏗️ QPER 四层架构
```
用户:"比较两组血压有没有差别"
┌─ Q · Query ─────────────────────────────────────┐
│ LLM 意图解析 + Zod 动态防幻觉 + 追问卡片 │
│ 输出ParsedQuery { goal, y, x, design } │
└──────────────────────┬──────────────────────────┘
┌─ P · Planner ────────────────────────────────────┐
│ 决策表四维匹配 + 流程模板填充 + EPV 防护 │
│ 输出WorkflowPlan + PlannedTrace │
└──────────────────────┬──────────────────────────┘
┌─ E · Execute ────────────────────────────────────┐
│ R 引擎执行 + SSE 实时进度 + Block-based 输出 │
│ 输出StepResult[] + ReportBlock[] │
└──────────────────────┬──────────────────────────┘
┌─ R · Reflection ─────────────────────────────────┐
│ LLM 论文级结论 + 槽位注入 + Zod 校验 │
│ 输出ConclusionReport6 要素) │
└──────────────────────────────────────────────────┘
```
### 降级体系
| 层 | 正常路径 | 降级路径 | 触发条件 |
|----|---------|---------|---------|
| Q | QueryServiceLLM | 正则匹配 fallback | LLM 超时/不可用 |
| P | DecisionTable + FlowTemplate | 硬编码 if/else | 决策表无匹配 |
| E | R 引擎 | 错误分类→友好提示 | R 运行时崩溃 |
| R | ReflectionServiceLLM | ConclusionGeneratorService规则拼接 | LLM 失败/Zod 校验失败 |
---
## 📋 开发进度
| Phase | 任务 | 工时 | 状态 | 完成日期 |
|-------|------|------|------|---------|
| Phase 0 | 需求分析与架构设计 | - | ✅ 已完成 | 2026-02-18 |
| Phase 1 | 骨架搭建T 检验端到端) | - | ✅ 已完成 | 2026-02-19 |
| Phase 1.5 | V11 UI 前后端联调 | - | ✅ 已完成 | 2026-02-20 |
| Phase 2A | 多步骤工作流 + 前端集成 | - | ✅ 已完成 | 2026-02-20 |
| **Phase E+** | **Block-based 标准化** | **15.5h** | ✅ **已完成** | 2026-02-20 |
| **Phase Q** | **LLM 意图理解** | **33h** | ✅ **已完成** | 2026-02-21 |
| **Phase P** | **决策表 + 流程模板** | **23h** | ✅ **已完成** | 2026-02-21 |
| **Phase R** | **LLM 论文级结论** | **22h** | ✅ **已完成** | 2026-02-21 |
| Phase Deploy | 工具补齐 + 部署上线 | 37h | 📋 待开始 | - |
| Phase Q+ | 人机协同增强 | 20h | 📋 待开始 | - |
### 已完成核心功能
| 组件 | 完成项 | 状态 |
|------|--------|------|
| **R 服务** | 7 个 R 工具T 检验、描述统计、卡方、Logistic、相关分析等+ Block-based 输出 | ✅ |
| **Q 层** | QueryService + LLM Intent + Zod 动态防幻觉 + 追问卡片 + DataProfile 增强 | ✅ |
| **P 层** | ConfigLoader + DecisionTable + FlowTemplate + PlannedTrace + 热更新 API | ✅ |
| **E 层** | WorkflowExecutor + RClient + SSE 实时进度 + 错误分类映射 | ✅ |
| **R 层** | ReflectionService + 槽位注入 + Zod 校验 + 敏感性冲突准则 + 结论缓存 + Word 增强 | ✅ |
| **前端** | V11 UI + DynamicReport + ClarificationCard + ConclusionReport渐入动画+ Word/R 代码导出 | ✅ |
| **Python** | DataProfileServiceis_id_like 标记)+ CSV 解析 | ✅ |
| **测试** | QPER 端到端测试 40/40 通过 | ✅ |
---
## 📂 代码目录结构
```
backend/src/modules/ssa/
├── services/
│ ├── QueryService.ts # Q 层LLM 意图解析
│ ├── DecisionTableService.ts # P 层:四维匹配
│ ├── FlowTemplateService.ts # P 层:流程模板
│ ├── WorkflowPlannerService.ts # P 层:核心规划入口
│ ├── WorkflowExecutorService.ts # E 层:步骤编排 + SSE
│ ├── RClientService.ts # E 层R 引擎调用
│ ├── ReflectionService.ts # R 层LLM 结论生成
│ ├── ConclusionGeneratorService.ts # R 层 fallback
│ ├── DataProfileService.ts # 共享Python 数据质量
│ └── DataParserService.ts # 共享:文件解析
├── config/
│ ├── ConfigLoader.ts # 通用 JSON 加载 + Zod 校验
│ ├── tools_registry.json # R 工具注册表
│ ├── decision_tables.json # 四维匹配规则
│ └── flow_templates.json # 流程模板
├── types/
│ ├── query.types.ts # Q 层接口
│ └── reflection.types.ts # R 层接口
├── routes/
│ ├── workflow.routes.ts # 工作流 API含结论缓存
│ └── config.routes.ts # 热更新 API
└── ...
backend/scripts/
├── seed-ssa-intent-prompt.ts # Q 层 Prompt 种子
├── seed-ssa-reflection-prompt.ts # R 层 Prompt 种子
├── test-ssa-qper-e2e.ts # QPER 端到端测试
└── ...
```
---
## 🔧 开发环境
### 启动服务
```bash
# 1. 数据库Docker
docker start ai-clinical-postgres
# 2. Python 服务
cd extraction_service && python main.py
# 3. R 服务
cd r-statistics-service && Rscript plumber_api.R
# 4. Node.js 后端
cd backend && npm run dev
# 5. 前端
cd frontend-v2 && npm run dev
```
### 运行测试
```bash
cd backend
npx tsx scripts/test-ssa-qper-e2e.ts
```
### Prompt 种子(需数据库运行)
```bash
cd backend
npx tsx scripts/seed-ssa-intent-prompt.ts
npx tsx scripts/seed-ssa-reflection-prompt.ts
```
---
## 📚 相关文档
| 文档 | 路径 |
|------|------|
| **QPER 开发计划(主线)** | `04-开发计划/10-QPER架构开发计划-智能化主线.md` |
| **QPER 开发总结** | `06-开发记录/SSA-QPER架构开发总结-2026-02-21.md` |
| **智能化愿景设计** | `00-系统设计/SSA-Pro 理想状态与智能化愿景设计.md` |
| **PRD** | `00-系统设计/PRD SSA-Pro 严谨型智能统计分析模块.md` |
| **架构设计 V4** | `00-系统设计/SSA-Pro 严谨型智能统计分析架构设计方案V4.md` |
---
## 🎯 下一步
1. **Phase Deploy37h** — 补齐 ANOVA / Fisher / Wilcoxon / 线性回归 + 复合工具 ST_BASELINE_TABLE + 部署上线
2. **Phase Q+20h** — 变量数据字典AI 先猜用户微调)+ 变量选择确认面板AI 推荐医生确认)
3. **前端集成测试** — 用户手动测试 QPER 全链路的真实交互体验
---
**文档版本:** v2.0
**最后更新:** 2026-02-21
**当前状态:** 🎉 QPER 主线闭环完成,端到端 40/40 通过
**下一步:** Phase Deploy 工具补齐 + 部署上线