# WorkflowRun API 数据问题分析和解决方案 ## 🔍 **问题分析** 您遇到的问题是:`/console/api/apps/{app_id}/advanced-chat/workflow-runs` API 只返回一条数据,但实际执行了多次工作流。 ### 根本原因 1. **数据存储分离**: - `WorkflowExecution` (域模型) → 存储在 `dify-workflow-executions-*` 索引 - `WorkflowRun` (数据库模型) → 存储在 `dify-workflow-runs-*` 索引 - API 查询的是 `WorkflowRun` 数据 2. **查询类型过滤**: - API 只查询 `triggered_from == debugging` 的记录 - 但前端执行的工作流可能是 `app-run` 类型 3. **数据同步缺失**: - 系统创建了 `WorkflowExecution` 记录(65条) - 但没有创建对应的 `WorkflowRun` 记录 ## ✅ **解决方案** ### 1. 修改 WorkflowExecutionRepository 我已经修改了 `ElasticsearchWorkflowExecutionRepository.save()` 方法,现在它会: - 保存 `WorkflowExecution` 数据到 `workflow-executions` 索引 - 同时保存对应的 `WorkflowRun` 数据到 `workflow-runs` 索引 ### 2. 修改查询逻辑 修改了 `WorkflowRunService.get_paginate_advanced_chat_workflow_runs()` 方法: - 从查询 `debugging` 类型改为查询 `app-run` 类型 - 这样可以返回用户在前端执行的工作流记录 ## 🚀 **测试步骤** ### 1. 重启应用 使用新的配置重启 Dify API 服务 ### 2. 执行新的工作流 在前端执行一个新的工作流对话 ### 3. 检查数据 ```bash # 检查 Elasticsearch 中的数据 curl -k -u elastic:2gYvv6+O36PGwaVD6yzE -X GET "https://localhost:9200/dify-workflow-runs-*/_search?pretty&size=1" # 检查 triggered_from 统计 curl -k -u elastic:2gYvv6+O36PGwaVD6yzE -X GET "https://localhost:9200/dify-workflow-runs-*/_search?pretty" -H 'Content-Type: application/json' -d '{ "size": 0, "aggs": { "triggered_from_stats": { "terms": { "field": "triggered_from" } } } }' ``` ### 4. 测试 API 访问 `http://localhost:5001/console/api/apps/2b517b83-ecd1-4097-83e4-48bc626fd0af/advanced-chat/workflow-runs` ## 📊 **数据流程图** ``` 前端执行工作流 ↓ WorkflowCycleManager.handle_workflow_run_start() ↓ WorkflowExecutionRepository.save(WorkflowExecution) ↓ ElasticsearchWorkflowExecutionRepository.save() ↓ 保存到两个索引: ├── dify-workflow-executions-* (WorkflowExecution 数据) └── dify-workflow-runs-* (WorkflowRun 数据) ↓ API 查询 workflow-runs 索引 ↓ 返回完整的工作流运行列表 ``` ## 🔧 **配置要求** 确保您的 `.env` 文件包含: ```bash # Elasticsearch 配置 ELASTICSEARCH_ENABLED=true ELASTICSEARCH_HOSTS=["https://localhost:9200"] ELASTICSEARCH_USERNAME=elastic ELASTICSEARCH_PASSWORD=2gYvv6+O36PGwaVD6yzE ELASTICSEARCH_USE_SSL=true ELASTICSEARCH_VERIFY_CERTS=false # Repository 配置 CORE_WORKFLOW_EXECUTION_REPOSITORY=core.repositories.elasticsearch_workflow_execution_repository.ElasticsearchWorkflowExecutionRepository CORE_WORKFLOW_NODE_EXECUTION_REPOSITORY=core.repositories.elasticsearch_workflow_node_execution_repository.ElasticsearchWorkflowNodeExecutionRepository API_WORKFLOW_RUN_REPOSITORY=repositories.elasticsearch_api_workflow_run_repository.ElasticsearchAPIWorkflowRunRepository ``` ## 🎯 **预期结果** 修复后,您应该能够: 1. 在前端执行多次工作流 2. API 返回所有执行的工作流记录 3. 数据同时存储在两个索引中,保持一致性 现在重启应用并测试新的工作流执行,应该可以看到完整的运行历史了!