# **基于对话流的文档生成与导出技术方案** **核心策略**: 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 ( \