Files
AIclinicalresearch/docs/03-业务模块/AIA-AI智能问答/04-开发计划/基于对话流的文档生成与导出技术方案.md
HaHafeng 303dd78c54 feat(aia): Protocol Agent MVP complete with one-click generation and Word export
- Add one-click research protocol generation with streaming output

- Implement Word document export via Pandoc integration

- Add dynamic dual-panel layout with resizable split pane

- Implement collapsible content for StatePanel stages

- Add conversation history management with title auto-update

- Fix scroll behavior, markdown rendering, and UI layout issues

- Simplify conversation creation logic for reliability
2026-01-25 19:16:36 +08:00

105 lines
3.6 KiB
Markdown
Raw Permalink 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.
# **基于对话流的文档生成与导出技术方案**
**核心策略**: No-Editor (无编辑器模式)
**目标**: 在 Chat 界面完成方案生成与修改,后端直接合成 Word 下载。
## **1\. 业务流程 (User Flow)**
sequenceDiagram
participant User
participant ChatUI
participant Agent (Node.js)
participant PandocSvc (Python)
User-\>\>ChatUI: "生成完整方案"
ChatUI-\>\>Agent: POST /generate
Agent--\>\>ChatUI: Stream Markdown ("\# 1\. 研究背景...")
User-\>\>ChatUI: "把样本量部分改一下..."
ChatUI-\>\>Agent: POST /regenerate
Agent--\>\>ChatUI: Stream Updated Markdown
User-\>\>ChatUI: 点击 \[📥 导出 Word\]
ChatUI-\>\>Agent: POST /export/docx { markdown }
Agent-\>\>PandocSvc: Convert(markdown, reference.docx)
PandocSvc--\>\>Agent: Buffer (Binary)
Agent--\>\>ChatUI: Blob (Download)
## **2\. 核心技术实现**
### **2.1 Python 微服务Pandoc 转换器**
利用你们现有的 Python 微服务,集成 pypandoc。
**Dockerfile 增加依赖:**
RUN apt-get update && apt-get install \-y pandoc
**Service 代码 (python-service/app/services/doc\_service.py):**
import pypandoc
import os
def convert\_md\_to\_docx(markdown\_text: str, output\_path: str):
\# 使用参考文档 (Reference Doc) 来控制样式(字体、字号、页眉)
reference\_doc \= os.path.join(os.path.dirname(\_\_file\_\_), 'assets/style\_template.docx')
pypandoc.convert\_text(
markdown\_text,
'docx',
format='markdown',
outputfile=output\_path,
extra\_args=\[f'--reference-doc={reference\_doc}'\]
)
### **2.2 Node.js 后端:导出 API**
// backend/src/modules/aia/controllers/exportController.ts
export const exportToWord \= async (req, reply) \=\> {
const { markdown } \= req.body;
// 1\. 调用 Python 微服务
const response \= await pythonService.post('/convert/docx', { content: markdown }, { responseType: 'arraybuffer' });
// 2\. 返回文件流
reply.header('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
reply.header('Content-Disposition', 'attachment; filename="protocol.docx"');
return reply.send(response.data);
};
### **2.3 前端Chat 组件增强**
在 AIStreamChat 的消息组件中,增加导出按钮。
// frontend-v2/src/shared/components/Chat/MessageBubble.tsx
const MessageBubble \= ({ content, role }) \=\> {
const handleDownload \= async () \=\> {
const blob \= await api.post('/aia/export/docx', { markdown: content }, { responseType: 'blob' });
saveAs(blob, '研究方案.docx');
};
return (
\<div className="message-bubble"\>
\<Markdown\>{content}\</Markdown\>
{role \=== 'assistant' && (
\<div className="toolbar"\>
\<Button icon={\<DownloadIcon /\>} onClick={handleDownload}\>
导出 Word
\</Button\>
\</div\>
)}
\</div\>
);
};
## **3\. 方案优势总结**
1. **格式完美**:通过 Pandoc 的 Reference Doc可以保证导出的 Word 完全符合医院的格式要求(如宋体小四、行间距等),这是前端编辑器很难做到的。
2. **开发极快**:不需要处理 Tiptap 的状态管理、协同冲突、光标位置等复杂问题。
3. **符合直觉**:用户习惯在 Word 里做最后的精修。
**结论:** 这是一个非常务实且高效的决策。我们可以先不上编辑器,把精力花在 **AI 生成内容的质量****Word 导出的样式** 上。