refactor(backend): 迁移基础设施,core,拆分models
This commit is contained in:
1 parent
b89d93d738
commit
053ea392f2
61 files changed
+3329
-3268
No files matched your search
@@ -1,93 +1,93 @@
|
||||
"""␍
|
||||
services/file_check_tools/dify_inputs.py␍
|
||||
␍
|
||||
用途:构建文件检核 Chatflow 的 Dify chat inputs,并在 task_id 缺失时快速失败。␍
|
||||
依赖:task_resolver、agent_resolver、errors.ValidationError。␍
|
||||
逻辑:chat.py 在调 Dify 前调用本模块;缺失 schedule_task_id 时抛 ValidationError,避免 Dify 长连接挂死。␍
|
||||
"""␍
|
||||
from __future__ import annotations␍
|
||||
␍
|
||||
from app.errors import ValidationError␍
|
||||
from app.services.file_check_tools.task_resolver import resolve_schedule_task_id_from_agent␍
|
||||
␍
|
||||
# Dify Chatflow「用户输入」变量名 — 须与画布 Start 节点配置一致␍
|
||||
FILE_CHECK_TASK_ID_INPUT = "task_id"␍
|
||||
FILE_CHECK_TASK_ID_MISSING_CODE = "file_check_task_id_required"␍
|
||||
␍
|
||||
␍
|
||||
def _missing_task_id_error(agent) -> ValidationError:␍
|
||||
"""␍
|
||||
构造 task_id 未配置时的业务异常。␍
|
||||
␍
|
||||
核心逻辑:返回可操作的配置提示,供管理后台与前端直接展示。␍
|
||||
"""␍
|
||||
return ValidationError(␍
|
||||
"文件检核 Agent 未配置有效的 schedule_task_id,无法调用 Dify Chatflow。"␍
|
||||
"请在管理后台编辑该 Agent,于 extra_config 中设置 "␍
|
||||
"tool_run_progress.schedule_task_id(绑定排程任务 ID)。",␍
|
||||
details={␍
|
||||
"error_code": FILE_CHECK_TASK_ID_MISSING_CODE,␍
|
||||
"agent_id": getattr(agent, "id", None),␍
|
||||
"agent_name": getattr(agent, "name", None) or "",␍
|
||||
"config_hint": {␍
|
||||
"capabilities": ["tool_run_progress"],␍
|
||||
"tool_run_progress": {␍
|
||||
"type": "file_check",␍
|
||||
"schedule_task_id": "<ScheduleTask.id>",␍
|
||||
},␍
|
||||
},␍
|
||||
},␍
|
||||
)␍
|
||||
␍
|
||||
␍
|
||||
def ensure_file_check_task_id_configured(agent) -> int:␍
|
||||
"""␍
|
||||
解析并校验文件检核 Agent 绑定的 schedule_task_id。␍
|
||||
␍
|
||||
Args:␍
|
||||
agent: 当前对话 Agent。␍
|
||||
␍
|
||||
Returns:␍
|
||||
有效的 ScheduleTask.id。␍
|
||||
␍
|
||||
Raises:␍
|
||||
ValidationError: extra_config 未配置或 schedule_task_id 非法。␍
|
||||
"""␍
|
||||
task_id = resolve_schedule_task_id_from_agent(agent)␍
|
||||
if not task_id:␍
|
||||
raise _missing_task_id_error(agent)␍
|
||||
return task_id␍
|
||||
␍
|
||||
␍
|
||||
def build_file_check_dify_inputs(agent, *, required: bool = False) -> dict[str, str]:␍
|
||||
"""␍
|
||||
构建文件检核 Chatflow 的 Dify chat inputs。␍
|
||||
␍
|
||||
仅注入 task_id。hxops_session_id / hxops_turn_id 由 chat.py 登记 turn 注册表,␍
|
||||
Tool API 启动时通过 user_message 推断,无需传入 Dify 画布。␍
|
||||
␍
|
||||
Args:␍
|
||||
agent: 当前对话 Agent;task_id 从其 extra_config 解析。␍
|
||||
required: 为 True 时若 task_id 缺失立即抛 ValidationError,避免 Dify 流式挂死。␍
|
||||
␍
|
||||
Returns:␍
|
||||
含 task_id 的 inputs 字典;required=False 且未配置时返回空 dict。␍
|
||||
␍
|
||||
Raises:␍
|
||||
ValidationError: required=True 且 schedule_task_id 未配置。␍
|
||||
"""␍
|
||||
task_id = resolve_schedule_task_id_from_agent(agent)␍
|
||||
if not task_id:␍
|
||||
if required:␍
|
||||
raise _missing_task_id_error(agent)␍
|
||||
return {}␍
|
||||
return {FILE_CHECK_TASK_ID_INPUT: str(task_id)}␍
|
||||
␍
|
||||
␍
|
||||
__all__ = [␍
|
||||
"FILE_CHECK_TASK_ID_INPUT",␍
|
||||
"FILE_CHECK_TASK_ID_MISSING_CODE",␍
|
||||
"build_file_check_dify_inputs",␍
|
||||
"ensure_file_check_task_id_configured",␍
|
||||
]␍
|
||||
"""
|
||||
services/file_check_tools/dify_inputs.py
|
||||
|
||||
用途:构建文件检核 Chatflow 的 Dify chat inputs,并在 task_id 缺失时快速失败。
|
||||
依赖:task_resolver、agent_resolver、errors.ValidationError。
|
||||
逻辑:chat.py 在调 Dify 前调用本模块;缺失 schedule_task_id 时抛 ValidationError,避免 Dify 长连接挂死。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from app.core.errors import ValidationError
|
||||
from app.services.file_check_tools.task_resolver import resolve_schedule_task_id_from_agent
|
||||
|
||||
# Dify Chatflow「用户输入」变量名 — 须与画布 Start 节点配置一致
|
||||
FILE_CHECK_TASK_ID_INPUT = "task_id"
|
||||
FILE_CHECK_TASK_ID_MISSING_CODE = "file_check_task_id_required"
|
||||
|
||||
|
||||
def _missing_task_id_error(agent) -> ValidationError:
|
||||
"""
|
||||
构造 task_id 未配置时的业务异常。
|
||||
|
||||
核心逻辑:返回可操作的配置提示,供管理后台与前端直接展示。
|
||||
"""
|
||||
return ValidationError(
|
||||
"文件检核 Agent 未配置有效的 schedule_task_id,无法调用 Dify Chatflow。"
|
||||
"请在管理后台编辑该 Agent,于 extra_config 中设置 "
|
||||
"tool_run_progress.schedule_task_id(绑定排程任务 ID)。",
|
||||
details={
|
||||
"error_code": FILE_CHECK_TASK_ID_MISSING_CODE,
|
||||
"agent_id": getattr(agent, "id", None),
|
||||
"agent_name": getattr(agent, "name", None) or "",
|
||||
"config_hint": {
|
||||
"capabilities": ["tool_run_progress"],
|
||||
"tool_run_progress": {
|
||||
"type": "file_check",
|
||||
"schedule_task_id": "<ScheduleTask.id>",
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def ensure_file_check_task_id_configured(agent) -> int:
|
||||
"""
|
||||
解析并校验文件检核 Agent 绑定的 schedule_task_id。
|
||||
|
||||
Args:
|
||||
agent: 当前对话 Agent。
|
||||
|
||||
Returns:
|
||||
有效的 ScheduleTask.id。
|
||||
|
||||
Raises:
|
||||
ValidationError: extra_config 未配置或 schedule_task_id 非法。
|
||||
"""
|
||||
task_id = resolve_schedule_task_id_from_agent(agent)
|
||||
if not task_id:
|
||||
raise _missing_task_id_error(agent)
|
||||
return task_id
|
||||
|
||||
|
||||
def build_file_check_dify_inputs(agent, *, required: bool = False) -> dict[str, str]:
|
||||
"""
|
||||
构建文件检核 Chatflow 的 Dify chat inputs。
|
||||
|
||||
仅注入 task_id。hxops_session_id / hxops_turn_id 由 chat.py 登记 turn 注册表,
|
||||
Tool API 启动时通过 user_message 推断,无需传入 Dify 画布。
|
||||
|
||||
Args:
|
||||
agent: 当前对话 Agent;task_id 从其 extra_config 解析。
|
||||
required: 为 True 时若 task_id 缺失立即抛 ValidationError,避免 Dify 流式挂死。
|
||||
|
||||
Returns:
|
||||
含 task_id 的 inputs 字典;required=False 且未配置时返回空 dict。
|
||||
|
||||
Raises:
|
||||
ValidationError: required=True 且 schedule_task_id 未配置。
|
||||
"""
|
||||
task_id = resolve_schedule_task_id_from_agent(agent)
|
||||
if not task_id:
|
||||
if required:
|
||||
raise _missing_task_id_error(agent)
|
||||
return {}
|
||||
return {FILE_CHECK_TASK_ID_INPUT: str(task_id)}
|
||||
|
||||
|
||||
__all__ = [
|
||||
"FILE_CHECK_TASK_ID_INPUT",
|
||||
"FILE_CHECK_TASK_ID_MISSING_CODE",
|
||||
"build_file_check_dify_inputs",
|
||||
"ensure_file_check_task_id_configured",
|
||||
]
|
||||
␍
|
||||
@@ -11,7 +11,7 @@ import re
|
||||
from typing import Any
|
||||
|
||||
from app import db
|
||||
from app.errors import NotFoundError, ServiceUnavailableError, ValidationError
|
||||
from app.core.errors import NotFoundError, ServiceUnavailableError, ValidationError
|
||||
from app.models import KbGroup, ScheduleTask
|
||||
from app.services.knowledge_cleaning.dify_sync_service import KbDifySyncError, KbDifySyncService
|
||||
|
||||
|
||||
Reference in new issue
Block a user