本文档描述了外部AI平台工作流与AX文档编辑器前端的集成。通过此集成,用户可以在聊天界面输入自然语言指令(如"生成一个地质报告"),系统会自动触发工作流,生成文档并返回可下载的URL链接。
用户输入(聊天)
↓
Chat Store (检查是否触发工作流)
↓
Workflow Service (调用外部AI平台 v2 API)
↓
外部工作流平台 (http://114.242.25.27:3000/api/v2/chat/completions)
↓
本地后端 API (http://192.168.0.200:8000/api/v1/export/records)
↓
返回导出记录 (Export Record with downloadUrl)
↓
在聊天中显示文档卡片
↓
用户点击查看/下载文档
src/services/workflowService.ts)作用: 处理与外部工作流API的通信。
关键函数:
shouldTriggerWorkflow(input: string): boolean判断用户输入是否应该触发文档生成工作流。
触发模式:
示例:
shouldTriggerWorkflow("生成一个地质报告") // true
shouldTriggerWorkflow("创建技术文档") // true
shouldTriggerWorkflow("你好") // false
triggerDocumentWorkflow(userInput: string, chatId: string)调用工作流API生成文档并返回导出记录。
API端点:
POST http://114.242.25.27:3000/api/v2/chat/completions
请求头:
Content-Type: application/json
Authorization: Bearer XAgent-mWHBqQw06psUYRqx6PrHWiKdfY05ebt7I9drDBHzaG9QQesIkVEICj
请求体:
{
"chatId": "session-uuid",
"stream": false,
"detail": false,
"messages": [
{
"role": "user",
"content": "生成一个地质报告"
}
]
}
响应格式:
{
"content": "已为您生成地质报告",
"exportRecord": {
"recordId": "rec-e22bbf07350d",
"userId": "default-user",
"fileName": "地质报告_1782369497876.doc",
"fileSize": 39076,
"downloadUrl": "http://192.168.0.200:8000/api/v1/export/records/rec-e22bbf07350d/download?userId=default-user",
"documentId": "doc-d2ef5b701b5c",
"styleId": "default",
"createdAt": 1782369497764
}
}
或者数组格式:
{
"content": "已为您生成地质报告",
"records": [
{
"recordId": "rec-e22bbf07350d",
...
}
]
}
src/stores/chatStore.ts)修改内容:
import { shouldTriggerWorkflow, triggerDocumentWorkflow } from '../services/workflowService';
sendMessage 函数// 检查是否需要触发工作流
const shouldUseWorkflow = shouldTriggerWorkflow(content);
if (shouldUseWorkflow) {
// 使用工作流生成文档
const workflowResult = await triggerDocumentWorkflow(content, sessionId);
aiResponse = workflowResult.content;
exportRecord = workflowResult.exportRecord;
} else {
// 使用常规AI服务
const aiResult = await getAIResponse(content, sessionId, get().messages);
aiResponse = aiResult.response;
}
// 创建AI消息并附加导出记录
const aiMessage: ChatMessage = {
id: uuidv4(),
role: 'assistant',
content: aiResponse,
timestamp: Date.now(),
exportRecord: exportRecord, // 附加导出记录
};
src/components/ChatPanel/MessageItem.tsx)功能: 已有支持渲染导出记录为可点击文档卡片。
当消息中包含 exportRecord 时,会显示一个文档卡片:
用户点击卡片会触发 onDocumentClick 回调。
src/App.tsx)功能: 处理文档点击事件,在编辑器中打开文档。
const handleDocumentClick = useCallback((exportRecord: ExportRecordInfo) => {
setPreviewDocument({
downloadUrl: exportRecord.downloadUrl,
fileName: exportRecord.fileName,
recordId: exportRecord.recordId,
userId: exportRecord.userId,
documentId: exportRecord.documentId,
});
setPreviewMode('editor');
}, []);
在 .env 文件中添加:
# 工作流API配置
# 工作流API URL(v2 - 带工作流)
VITE_WORKFLOW_API_URL=http://114.242.25.27:3000/api/v2/chat/completions
# 工作流API密钥
VITE_WORKFLOW_API_KEY=XAgent-mWHBqQw06psUYRqx6PrHWiKdfY05ebt7I9drDBHzaG9QQesIkVEICj
在 .env.example 文件中添加(示例):
# 工作流API配置
VITE_WORKFLOW_API_URL=http://114.242.25.27:3000/api/v2/chat/completions
VITE_WORKFLOW_API_KEY=your_workflow_api_key_here
输入文档生成请求,例如:
系统自动处理:
查看结果:
点击文档卡片:
✅ 会触发工作流:
❌ 不会触发工作流:
工作流集成设计为优雅降级:
API密钥未配置:
工作流API调用失败:
工作流未返回导出记录:
在浏览器控制台可以看到工作流执行日志:
🔄 Using workflow for document generation - 开始使用工作流🔄 Triggering document generation workflow... - 触发工作流📝 User input: 生成一个地质报告 - 用户输入✅ Workflow response received: - 收到工作流响应📄 Export record extracted: - 提取导出记录⚠️ No export record found in workflow response - 未找到导出记录❌ Workflow API error: - 工作流API错误启动开发服务器:
cd d:\work space\ax-shell\ax-frontend-app
npm run dev
打开应用: 浏览器访问 http://localhost:5173
创建新会话: 点击聊天面板
输入文档生成请求:
生成一个地质报告
验证工作流:
测试文档下载:
工作流内部调用本地后端导出记录API:
GET http://192.168.0.200:8000/api/v1/export/records?userId=default-user&page=1&pageSize=20
此API返回生成的导出记录列表,工作流将其转发给前端。
文档通过以下端点下载:
GET http://192.168.0.200:8000/api/v1/export/records/{recordId}/download?userId={userId}
在外部AI平台 (http://114.242.25.27:3000) 中,工作流应该配置为:
http://192.168.0.200:8000/api/v1/export/records工作流API应该返回以下格式之一:
格式1:单个导出记录
{
"content": "AI响应文本",
"exportRecord": {
"recordId": "rec-xxx",
"fileName": "地质报告_xxx.doc",
"downloadUrl": "http://192.168.0.200:8000/api/v1/export/records/rec-xxx/download?userId=xxx",
"documentId": "doc-xxx",
"userId": "default-user",
"fileSize": 39076,
"styleId": "default",
"createdAt": 1782369497764
}
}
格式2:导出记录数组
{
"content": "AI响应文本",
"records": [
{
"recordId": "rec-xxx",
"fileName": "地质报告_xxx.doc",
"downloadUrl": "http://...",
...
}
]
}
API密钥存储:
.env.example 中使用占位符身份验证:
Bearer token 进行工作流认证CORS:
检查:
.env 中是否设置了 VITE_WORKFLOW_API_KEY?检查:
exportRecord 是否正确附加到消息?检查:
多文档生成:
文档模板:
进度指示:
重试逻辑:
文档预览: