feat(aia): Integrate PromptService for 10 AI agents

Features:
- Migrate 10 agent prompts from hardcoded to database
- Add grayscale preview support (DRAFT/ACTIVE distribution)
- Implement 3-tier fallback (DB -> Cache -> Hardcoded)
- Add version management and rollback capability

Files changed:
- backend/scripts/migrate-aia-prompts.ts (new migration script)
- backend/src/common/prompt/prompt.fallbacks.ts (add AIA fallbacks)
- backend/src/modules/aia/services/agentService.ts (integrate PromptService)
- backend/src/modules/aia/services/conversationService.ts (pass userId)
- backend/src/modules/aia/types/index.ts (fix AgentStage type)

Documentation:
- docs/03-业务模块/AIA-AI智能问答/06-开发记录/2026-01-18-Prompt管理系统集成.md
- docs/02-通用能力层/00-通用能力层清单.md (add FileCard, Prompt management)
- docs/00-系统总体设计/00-系统当前状态与开发指南.md (update to v3.6)

Prompt codes:
- AIA_SCIENTIFIC_QUESTION, AIA_PICO_ANALYSIS, AIA_TOPIC_EVALUATION
- AIA_OUTCOME_DESIGN, AIA_CRF_DESIGN, AIA_SAMPLE_SIZE
- AIA_PROTOCOL_WRITING, AIA_METHODOLOGY_REVIEW
- AIA_PAPER_POLISH, AIA_PAPER_TRANSLATE

Tested: Migration script executed, all 10 prompts inserted successfully
This commit is contained in:
2026-01-18 15:48:53 +08:00
parent 66255368b7
commit 57fdc6ef00
290 changed files with 2950 additions and 106 deletions

View File

@@ -297,3 +297,6 @@ Level 3: 兜底Prompt缓存也失效

View File

@@ -483,3 +483,6 @@ const pageSize = Number(query.pageSize) || 20;
**开发完成时间:** 2026-01-16 13:30

View File

@@ -217,3 +217,6 @@ ADMIN-运营管理端/

View File

@@ -316,3 +316,6 @@ INST-机构管理端/

View File

@@ -1,14 +1,15 @@
# AIA AI智能问答模块 - 当前状态与开发指南
> **文档版本:** v2.0
> **文档版本:** v2.1
> **创建日期:** 2026-01-14
> **维护者:** AIA模块开发团队
> **最后更新:** 2026-01-14 🎉 **V2版本发布 - 通用能力层架构重构完成**
> **最后更新:** 2026-01-18 🎉 **V2.1版本发布 - Prompt管理系统集成完成**
> **重大里程碑:**
> - 🏆 通用流式响应服务OpenAI Compatible
> - 🎨 现代感UI100%还原原型图V11
> - 🚀 Ant Design X 深度集成
> - ✨ 12个智能体配置完成
> - 🆕 Prompt管理系统集成灰度预览、版本管理
---
@@ -40,9 +41,33 @@ AIAAI Intelligent Assistant模块提供覆盖临床研究全生命周期
### 当前状态
- **开发阶段:** ✅ **V2.0 MVP 完成**
- **架构版本:** V2(基于通用能力层重构
- **完成度:** 85%(核心功能完成,部分高级特性待开发
- **开发阶段:** ✅ **V2.1 Prompt 管理集成完成**
- **架构版本:** V2.1(集成 PromptService
- **完成度:** 90%Prompt 管理已对接,支持灰度预览
### ✅ V2.1 新增功能2026-01-18
**Prompt 管理系统集成**
- [x] 10 个智能体 Prompt 迁移到数据库
- [x] 支持在管理端在线编辑和调试
- [x] 灰度预览(调试者看 DRAFT普通用户看 ACTIVE
- [x] 三级容灾(数据库 → 缓存 → 兜底)
- [x] 版本管理和回滚
**Prompt Code 映射表**
| 智能体 | Prompt Code |
|--------|-------------|
| 科学问题梳理 | `AIA_SCIENTIFIC_QUESTION` |
| PICO 梳理 | `AIA_PICO_ANALYSIS` |
| 选题评价 | `AIA_TOPIC_EVALUATION` |
| 观察指标设计 | `AIA_OUTCOME_DESIGN` |
| 病例报告表设计 | `AIA_CRF_DESIGN` |
| 样本量计算 | `AIA_SAMPLE_SIZE` |
| 临床研究方案撰写 | `AIA_PROTOCOL_WRITING` |
| 方法学评审智能体 | `AIA_METHODOLOGY_REVIEW` |
| 论文润色 | `AIA_PAPER_POLISH` |
| 论文翻译 | `AIA_PAPER_TRANSLATE` |
---
@@ -657,3 +682,6 @@ const DEFAULT_MODEL = 'qwen-max'; // 或 'gpt-5', 'claude-4.5'

View File

@@ -883,3 +883,6 @@ export interface SlashCommand {

View File

@@ -0,0 +1,193 @@
# AIA 模块开发记录 - Prompt 管理系统集成
> **日期:** 2026-01-18
> **开发者:** AI Assistant (Claude)
> **版本:** V2.1
> **状态:** ✅ 已完成
---
## 📋 开发目标
将 AIA 智能问答模块的 10 个智能体 Prompt 从硬编码迁移到 PromptService实现
- 在管理端可视化配置和调试提示词
- 灰度预览(调试者看 DRAFT普通用户看 ACTIVE
- 版本管理和回滚
- 三级容灾(数据库 → 缓存 → 兜底)
---
## 🎯 完成内容
### 1. 数据库迁移脚本
**文件:** `backend/scripts/migrate-aia-prompts.ts`
将 10 个智能体的 Prompt 插入 `capability_schema.prompt_templates``prompt_versions` 表。
**Prompt Code 命名规则(语义化):**
| 智能体 ID | Prompt Code | 名称 |
|-----------|-------------|------|
| TOPIC_01 | `AIA_SCIENTIFIC_QUESTION` | 科学问题梳理 |
| TOPIC_02 | `AIA_PICO_ANALYSIS` | PICO 梳理 |
| TOPIC_03 | `AIA_TOPIC_EVALUATION` | 选题评价 |
| DESIGN_04 | `AIA_OUTCOME_DESIGN` | 观察指标设计 |
| DESIGN_05 | `AIA_CRF_DESIGN` | 病例报告表设计 |
| DESIGN_06 | `AIA_SAMPLE_SIZE` | 样本量计算 |
| DESIGN_07 | `AIA_PROTOCOL_WRITING` | 临床研究方案撰写 |
| REVIEW_08 | `AIA_METHODOLOGY_REVIEW` | 方法学评审智能体 |
| WRITING_11 | `AIA_PAPER_POLISH` | 论文润色 |
| WRITING_12 | `AIA_PAPER_TRANSLATE` | 论文翻译 |
**执行命令:**
```bash
cd backend && npx tsx scripts/migrate-aia-prompts.ts
```
### 2. 兜底 Prompt
**文件:** `backend/src/common/prompt/prompt.fallbacks.ts`
添加 `AIA_FALLBACKS` 对象,包含 10 个智能体的兜底提示词,当数据库和缓存都不可用时使用。
### 3. agentService 改造
**文件:** `backend/src/modules/aia/services/agentService.ts`
**改动:**
- 添加 `AGENT_TO_PROMPT_CODE` 映射表
- 修改 `getAgentSystemPrompt()` 函数:
- 新增 `userId` 参数(用于灰度预览)
- 调用 `PromptService.get()` 获取提示词
- 返回 `{ content, isDraft }` 结构
- 失败时降级到硬编码兜底
**关键代码:**
```typescript
export async function getAgentSystemPrompt(
agentId: string,
userId?: string
): Promise<{ content: string; isDraft: boolean }> {
const promptCode = AGENT_TO_PROMPT_CODE[agentId];
if (promptCode) {
try {
const promptService = getPromptService(prisma);
const result = await promptService.get(promptCode, {}, { userId });
if (result.isDraft) {
logger.info('[AIA] 使用 DRAFT 版本 Prompt调试模式', { agentId, userId });
}
return { content: result.content, isDraft: result.isDraft };
} catch (error) {
logger.warn('[AIA] PromptService 获取失败,使用兜底', { agentId });
}
}
// 兜底:使用硬编码
const agent = await getAgentById(agentId);
return { content: agent?.systemPrompt || '', isDraft: false };
}
```
### 4. conversationService 改造
**文件:** `backend/src/modules/aia/services/conversationService.ts`
**改动:**
- 调用 `getAgentSystemPrompt()` 时传递 `userId`
- 处理返回的 `{ content, isDraft }` 结构
- 添加调试模式日志
**关键代码:**
```typescript
const { content: systemPrompt, isDraft } = await agentService.getAgentSystemPrompt(
conversation.agentId,
userId // 传递 userId 以支持灰度预览
);
if (isDraft) {
logger.info('[AIA:Conversation] 使用 DRAFT 版本 Prompt调试模式', {
userId,
agentId: conversation.agentId
});
}
```
### 5. 类型定义修复
**文件:** `backend/src/modules/aia/types/index.ts`
修复 `AgentStage` 类型定义,添加缺失的阶段值:
```typescript
export type AgentStage = 'topic' | 'design' | 'review' | 'data' | 'writing';
```
---
## 📁 修改文件清单
| 文件路径 | 操作 | 说明 |
|----------|------|------|
| `backend/scripts/migrate-aia-prompts.ts` | 新增 | 数据库迁移脚本 |
| `backend/src/common/prompt/prompt.fallbacks.ts` | 修改 | 添加 AIA 兜底 Prompt |
| `backend/src/modules/aia/services/agentService.ts` | 修改 | 集成 PromptService |
| `backend/src/modules/aia/services/conversationService.ts` | 修改 | 传递 userId |
| `backend/src/modules/aia/types/index.ts` | 修改 | 修复类型定义 |
---
## 🧪 测试验证
### 1. 迁移脚本执行结果
```
✅ 共迁移 10 个 AIA Prompt:
📋 AIA_CRF_DESIGN - 病例报告表设计 - v1 (ACTIVE)
📋 AIA_METHODOLOGY_REVIEW - 方法学评审智能体 - v1 (ACTIVE)
📋 AIA_OUTCOME_DESIGN - 观察指标设计 - v1 (ACTIVE)
...
```
### 2. 功能测试
- [x] 管理端可以看到 AIA 的 10 个 Prompt
- [x] 可以编辑和保存草稿
- [x] 开启调试模式后可以预览 DRAFT 版本
- [x] 发布后普通用户使用新版本
- [x] 数据库不可用时自动降级到兜底
---
## 🔮 后续可优化
1. **变量支持**
- 数据库层面已支持(`variables` 字段)
- 代码层面暂未传入变量
- 后续可添加:`{{目标期刊}}``{{研究类型}}`
2. **A/B 测试**
- 按用户比例分流不同 Prompt 版本
- 收集效果数据进行对比
3. **Prompt 模板**
- 提供常用 Prompt 模板库
- 支持一键套用和自定义修改
---
## 📚 相关文档
- [ADMIN 模块状态](../../ADMIN-运营管理端/00-模块当前状态与开发指南.md)
- [Prompt 管理系统快速参考](../../../02-技术设计/03-Prompt管理系统快速参考.md)
- [通用能力层清单](../../../02-通用能力层/00-通用能力层清单.md)
---
## 📝 备注
本次开发遵循了现有 PromptService 的设计模式,与 RVW 模块集成方式保持一致。

View File

@@ -1293,6 +1293,9 @@ interface FulltextScreeningResult {

View File

@@ -407,6 +407,9 @@ GET /api/v1/asl/fulltext-screening/tasks/:taskId/export

View File

@@ -509,6 +509,9 @@ Failed to open file '\\tmp\\extraction_service\\temp_10000_test.pdf'

View File

@@ -575,6 +575,9 @@ df['creatinine'] = pd.to_numeric(df['creatinine'], errors='coerce')

View File

@@ -990,6 +990,9 @@ export const aiController = new AIController();

View File

@@ -1324,6 +1324,9 @@ npm install react-markdown

View File

@@ -232,6 +232,9 @@ FMA___基线 | FMA___1个月 | FMA___2个月

View File

@@ -390,6 +390,9 @@ formula = "FMA总分0-100 / 100"

View File

@@ -224,6 +224,9 @@ async handleFillnaMice(request, reply) {

View File

@@ -196,6 +196,9 @@ method: 'mean' | 'median' | 'mode' | 'constant' | 'ffill' | 'bfill'

View File

@@ -647,6 +647,9 @@ import { logger } from '../../../../common/logging/index.js';

View File

@@ -450,6 +450,9 @@ import { ChatContainer } from '@/shared/components/Chat';

View File

@@ -360,6 +360,9 @@ const initialMessages = defaultMessages.length > 0 ? defaultMessages : [{

View File

@@ -648,6 +648,9 @@ http://localhost:5173/data-cleaning/tool-c

View File

@@ -436,6 +436,9 @@ Docs: docs/03-业务模块/DC-数据清洗整理/06-开发记录/DC模块重建

View File

@@ -309,6 +309,9 @@ ConflictDetectionService // 冲突检测(字段级对比)

View File

@@ -473,6 +473,9 @@ Tool B后端代码**100%复用**了平台通用能力层,无任何重复开发

View File

@@ -250,6 +250,9 @@ $ node scripts/check-dc-tables.mjs

View File

@@ -483,6 +483,9 @@ ${fields.map((f, i) => `${i + 1}. ${f.name}${f.desc}`).join('\n')}

View File

@@ -692,3 +692,6 @@ private async processMessageAsync(xmlData: any) {

View File

@@ -1083,6 +1083,9 @@ async function testIntegration() {

View File

@@ -223,6 +223,9 @@ Content-Type: application/json

View File

@@ -647,3 +647,6 @@ REDCap API: exportRecords success { recordCount: 1 }

View File

@@ -650,6 +650,9 @@ backend/src/modules/iit-manager/

View File

@@ -801,5 +801,8 @@ CREATE TABLE iit_schema.wechat_tokens (

View File

@@ -557,6 +557,9 @@ Day 3 的开发工作虽然遇到了多个技术问题,但最终成功完成

View File

@@ -326,4 +326,7 @@ AI: "出生日期2017-01-04

View File

@@ -267,6 +267,9 @@ Day 4: REDCap EMWebhook推送← 作为增强,而非核心

View File

@@ -685,3 +685,6 @@ const answer = `根据研究方案[1]和CRF表格[2],纳入标准包括:

View File

@@ -442,3 +442,6 @@ export const calculateAvailableQuota = async (tenantId: string) => {

View File

@@ -315,3 +315,6 @@ https://platform.example.com/t/pharma-abc/login

View File

@@ -365,3 +365,6 @@ const newResults = resultsData.map((docResult: any) => ({

View File

@@ -772,6 +772,9 @@ docker exec redcap-apache php /tmp/create-redcap-password.php

View File

@@ -154,6 +154,9 @@ AIclinicalresearch/redcap-docker-dev/