Files
syops/backend/app/services/file_check.py
T

117 lines
4.2 KiB
Python

"""
backend/app/services/file_check.py
用途:文件检核统一判定入口,调用 ScheduleTask 绑定的 Dify Workflow。
依赖:DifyService.run_workflow_streaming。
逻辑:由 schedule/srm_service 检核循环逐条调用;RoHS/MSDS 等差异在 Workflow 与排程 SQL 配置。
"""
from __future__ import annotations
from typing import Any
from app.services.dify import DifyService
VALID_FILE_CHECK_RESULTS = {"合格", "不合格", "人工判定"}
class FileCheckService:
"""文件检核 Dify Workflow 调用与结果规范化。"""
PROVIDER = "dify_workflow"
@classmethod
def evaluate(
cls,
*,
agent,
run_no: str,
record: str,
attachment_url: str | None = None,
system_prompt: str | None = None,
user: str = "schedule_system",
timeout: tuple[int, int] = (5, 300),
) -> dict[str, Any]:
"""
对单条待检记录调用 Dify Workflow 并返回规范化判定。
Args:
agent: ScheduleTask.dify_agent 对应的 Agent 记录。
run_no: 当前检核批次号。
record: 待检记录文本(由排程 SQL 组装)。
attachment_url: 附件 URL,可选。
system_prompt: 排程配置的补充系统提示,可选。
user: Dify 用户标识。
timeout: HTTP 超时 (connect, read) 秒。
Returns:
含 dify_result、dify_message、workflow_run_id 等字段的 dict。
"""
inputs: dict[str, Any] = {
"run_no": run_no,
"record": record,
"attachment_url": attachment_url,
}
prompt = str(system_prompt or "").strip()
if prompt:
inputs["system_prompt"] = prompt
try:
workflow_result = DifyService.run_workflow_streaming(
agent=agent,
inputs=inputs,
user=user,
timeout=timeout,
)
except Exception as exc:
return {
"dify_result": "人工判定",
"dify_message": f"Dify执行失败:{exc}",
"provider": cls.PROVIDER,
"workflow_run_id": None,
"error": str(exc),
}
normalized = cls.normalize_workflow_result(workflow_result)
normalized["provider"] = cls.PROVIDER
return normalized
@staticmethod
def normalize_workflow_result(workflow_result: dict[str, Any] | None) -> dict[str, Any]:
"""将 Dify Workflow 输出规范为合格/不合格/人工判定。"""
data = workflow_result if isinstance(workflow_result, dict) else {}
outputs = (data.get("outputs") or {}).get("output") or data.get("outputs") or data
output_data = outputs if isinstance(outputs, dict) else {}
dify_result = str(output_data.get("dify_result") or output_data.get("result") or "").strip()
dify_message = str(output_data.get("dify_message") or output_data.get("message") or "").strip()
workflow_run_id = data.get("workflow_run_id") if isinstance(data, dict) else None
status = data.get("status") if isinstance(data, dict) else None
error = data.get("error") if isinstance(data, dict) else None
if status == "failed" or (error and not output_data):
return {
"dify_result": "人工判定",
"dify_message": f"Dify执行失败:{error or f'status={status}'}",
"workflow_run_id": workflow_run_id,
"error": str(error or ""),
"raw_outputs": output_data,
}
if dify_result not in VALID_FILE_CHECK_RESULTS:
bad_value = dify_result or "空"
extra = f" | {dify_message}" if dify_message else ""
return {
"dify_result": "人工判定",
"dify_message": f"返回值异常:{bad_value}{extra}",
"workflow_run_id": workflow_run_id,
"error": None,
"raw_outputs": output_data,
}
return {
"dify_result": dify_result,
"dify_message": dify_message,
"workflow_run_id": workflow_run_id,
"error": None,
"raw_outputs": output_data,
}