feat(aia): Implement Protocol Agent MVP with reusable Agent framework

Sprint 1-3 Completed (Backend + Frontend):

Backend (Sprint 1-2):
- Implement 5-layer Agent framework (Query->Planner->Executor->Tools->Reflection)
- Create agent_schema with 6 tables (agent_definitions, stages, prompts, sessions, traces, reflexion_rules)
- Create protocol_schema with 2 tables (protocol_contexts, protocol_generations)
- Implement Protocol Agent core services (Orchestrator, ContextService, PromptBuilder)
- Integrate LLM service adapter (DeepSeek/Qwen/GPT-5/Claude)
- 6 API endpoints with full authentication
- 10/10 API tests passed

Frontend (Sprint 3):
- Add Protocol Agent entry in AgentHub (indigo theme card)
- Implement ProtocolAgentPage with 3-column layout
- Collapsible sidebar (Gemini style, 48px <-> 280px)
- StatePanel with 5 stage cards (scientific_question, pico, study_design, sample_size, endpoints)
- ChatArea with sync button and action cards integration
- 100% prototype design restoration (608 lines CSS)
- Detailed endpoints structure: baseline, exposure, outcomes, confounders

Features:
- 5-stage dialogue flow for research protocol design
- Conversation-driven interaction with sync-to-protocol button
- Real-time context state management
- One-click protocol generation button (UI ready, backend pending)

Database:
- agent_schema: 6 tables for reusable Agent framework
- protocol_schema: 2 tables for Protocol Agent
- Seed data: 1 agent + 5 stages + 9 prompts + 4 reflexion rules

Code Stats:
- Backend: 13 files, 4338 lines
- Frontend: 14 files, 2071 lines
- Total: 27 files, 6409 lines

Status: MVP core functionality completed, pending frontend-backend integration testing

Next: Sprint 4 - One-click protocol generation + Word export
This commit is contained in:
2026-01-24 17:29:24 +08:00
parent 61cdc97eeb
commit 96290d2f76
345 changed files with 13945 additions and 47 deletions

View File

@@ -0,0 +1,158 @@
# **产品需求文档 (PRD): 研究方案制定智能体 (Protocol Agent) & AIA 基础平台**
文档版本: v1.0
状态: Final Draft
日期: 2026-01-20
项目代号: AIA-Protocol-V3
核心理念: Plan-Execute-Reflexion (规划-执行-反思) | Backend-Driven SOP (后端驱动) | Deep Link Action (深度链接)
## **1\. 项目背景与目标 (Background & Goals)**
### **1.1 背景**
当前 AIA 模块包含 7 个独立运行的 AI 问答工具科学问题、PICO、选题评价等。这种“散点式”的交互导致用户体验割裂上下文Context无法流转且缺乏对复杂逻辑如样本量计算的深度支持。
### **1.2 核心目标**
1. **统一入口 (Unified Entry)**:将 7 个模块整合为一个 **Protocol Agent**,通过自然语言对话引导用户完成全流程。
2. **长程记忆 (Long-term Memory)**:利用结构化数据 (JSONB) 维护一份“活的”研究方案,突破 LLM Context 限制。
3. **专业深度 (Professional Depth)**:引入 **自建知识库 (EKB)****后端配置化思维链 (CoT)**,确保输出符合医学逻辑。
4. **平台化基石 (Platform Foundation)**:沉淀一套通用的 Agent 开发框架 (AIA-OS),为未来的统计、清洗 Agent 打下地基。
## **2\. 产品核心理念:仿生架构 (The Bionic Architecture)**
产品设计严格遵循 **"Brain-Memory-Knowledge-Hands"** 四位一体架构:
| 组件 | 对应系统模块 | 核心职责 |
| :---- | :---- | :---- |
| **🧠 大脑 (Brain)** | **Orchestrator Engine** | 负责意图识别 (Router)、流程规划 (Plan)、思维链执行 (SOP) 和结果反思 (Reflexion)。 |
| **💾 记忆 (Memory)** | **ProtocolContext (DB)** | 存储结构化的方案数据PICO、N值、指标实现跨会话、跨工具的状态同步。 |
| **📚 知识 (Knowledge)** | **EKB (Self-built RAG)** | 基于 Postgres 的混合检索系统,提供 RCT 案例、指南、评分标准等外部知识。 |
| **🛠️ 手脚 (Hands)** | **Deep Link Action** | 通过 **Action Card** 协议唤起现有的 Web 工具(样本量计算器、清洗工具),并回写结果。 |
## **3\. 用户流程 (User Journey)**
### **3.1 典型交互路径**
1. **意图识别**:用户输入“我想做一个阿司匹林的研究”。
* *Brain*: 识别为 SCIENTIFIC\_QUESTION 阶段,加载对应 SOP。
2. **多轮对话 (Plan)**AI 引导用户完善 PICO 信息。
* *Memory*: 实时提取 P, I, C, O 并存入数据库。
* *UI*: 右侧 **状态面板 (State Panel)** 实时点亮已确认的字段。
3. **工具执行 (Execute)**:用户确认 PICO 后AI 判断需要计算样本量。
* *Brain*: 生成 Action Card。
* *UI*: 聊天窗显示“🚀 前往计算样本量”卡片。
* *Hands*: 用户点击跳转 \-\> 在工具页完成计算 \-\> 点击“同步”。
4. **反思校验 (Reflexion)**:数据回写后。
* *Brain*: 触发后端校验规则(如 N \< 20
* *UI*: AI 发送消息:“⚠️ 样本量过小,建议重新调整 Alpha 值。”
5. **阶段流转**:校验通过后,自动进入下一阶段(观察指标设计)。
## **4\. 功能需求详情 (Functional Requirements)**
### **4.1 核心交互界面 (Unified Interface)**
#### **F1. 流式对话窗口 (Chat Interface)**
* **支持格式**Markdown, Latex, **XML Thinking Block** (深度思考折叠区)。
* **Action Card 渲染**:识别后端返回的 type: action\_card JSON渲染为操作卡片。
* **Reflexion 反馈**识别系统级通知System Message以特殊样式如紫色边框展示 AI 的反思结果。
#### **F2. 实时状态面板 (Protocol State Panel)**
* **位置**:屏幕右侧(桌面端)/ 抽屉式(移动端)。
* **数据源**:实时订阅 ProtocolContext.data。
* **展示内容**
* 科学问题 (Text)
* PICO (Table)
* 样本量 (Highlight Number)
* 核心指标 (Tags)
* **交互**:支持用户**手动编辑**面板内容,编辑结果优先级高于 AI 提取。
### **4.2 编排引擎 (Orchestrator Backend)**
#### **F3. 动态 SOP 编排 (Backend CoT)**
* **机制**:从数据库 prompt\_templates.chain\_config 加载 SOP 步骤。
* **执行**:将 SOP 编译为 System Prompt强制 AI 输出 XML 思考标签。
* **配置项**:支持在 Admin 后台配置 Step 顺序、指令内容、输出 Key。
#### **F4. 反思卫士 (Reflexion Guard)**
* **触发时机**:工具回写数据后、阶段切换前。
* **校验逻辑**
* **硬规则**N \< 10 (Fail), Power \< 0.8 (Warning)。
* **软规则**Prompt-based check ("检查对照组设置是否符合伦理?")。
### **4.3 知识库集成 (EKB Integration)**
#### **F5. 混合检索 (Hybrid Search)**
* **场景**:选题评价、指标设计。
* **逻辑**
* SQL 过滤WHERE study\_design-\>\>'sample\_size' \> 500。
* Vector 检索ORDER BY embedding \<=\> query。
* Rerank调用重排序模型取 Top 3。
### **4.4 工具集成 (Deep Link)**
#### **F6. 通用回写协议 (Universal Sync API)**
* **API**: POST /api/v1/aia/context/:id/sync
* **Payload**: { "agent\_type": "PROTOCOL", "key": "sample\_size", "value": {...} }
* **工具端改造**:现有样本量计算器需支持解析 URL 参数 (?ctx\_id=...) 并在计算完成后调用回写 API。
## **5\. 后端配置与运营 (LLM Ops)**
为了支持“后端配置化”,需要构建一个简易的运营后台。
### **5.1 配置管理**
* **全局配置**Base Persona, Memory Schema。
* **阶段配置**7 个阶段的 Prompt, SOP (JSON Array), Reflexion Rules。
### **5.2 可观测性 (Trace)**
* **会话回放**:查看每一次 Router 决策、CoT 思考过程、Tool 调用结果。
* **Bad Case 修正**:基于 Trace ID 快速定位问题,调整 Prompt 后一键重测。
## **6\. 平台化扩展策略 (Platform Strategy)**
本项目的交付物不仅仅是一个 Agent而是一套 **AIA-OS 规范**
### **6.1 通用能力抽象**
* **Context Interface**:定义 IContext 接口,未来支持 StatsContext (统计)、CleaningContext (清洗)。
* **Action Card Protocol**action\_card JSON 结构在所有 Agent 中通用。
* **Orchestrator Core**:状态机引擎与业务逻辑解耦,未来只需编写不同的 Config 即可生成新 Agent。
## **7\. 技术架构 (Technical Architecture)**
* **前端**React 19 \+ Ant Design X \+ Tailwind CSS
* **后端**Node.js (Fastify)
* **语言**TypeScript (全栈)
* **数据库**PostgreSQL 15+ (pgvector 插件)
* **ORM**Prisma (Schema-first)
* **异步队列**pg-boss (处理耗时任务如 PDF 解析、结构化提取)
* **模型层**OpenAI Compatible API (DeepSeek/Qwen)
## **8\. 实施路线图 (Roadmap)**
### **Phase 1: 核心骨架 (Week 1\)**
* \[后端\] 建立 protocol\_contexts 和 prompt\_templates (含 CoT 字段) 表。
* \[后端\] 实现 ProtocolOrchestrator 基础状态机。
* \[前端\] 搭建 Chat \+ State Panel 布局。
### **Phase 2: 智能增强 (Week 2\)**
* \[后端\] 实现动态 CoT 组装与 XML 解析。
* \[后端\] 集成 EKB V2 混合检索服务。
* \[后端\] 实现异步 PICO 提取 Worker。
### **Phase 3: 闭环打通 (Week 3\)**
* \[前端\] 开发 Action Card 组件。
* \[工具\] 改造样本量计算器,支持 Deep Link 回写。
* \[后端\] 实现 Reflexion Guard 校验逻辑。
* \[Ops\] 上线简易配置后台 (Admin UI)。