feat(shipping-notice): 支持发货通知解析延后至 Dify 画布,优化任务创建流程

This commit is contained in:
hugo committed 2026-06-22 16:59:38 +08:00
1 parent d46d61bfa1
commit 70e8f700ff
38 files changed
+3215 -714

No files matched your search

+12 -4
View File
@@ -1065,6 +1065,8 @@ def chat_completions():
query = data.get('query')
# 前端传入的 conversation_id 在 B2 里代表本地 session_id
conversation_id = data.get('conversation_id')
shipping_task_id = str(data.get('task_id') or '').strip()
shipping_batch_no = str(data.get('batch_no') or '').strip()
kb_type = data.get('kb_type', '企业知识库')
files = data.get('files')
request_id = uuid.uuid4().hex[:12]
@@ -1194,14 +1196,13 @@ def chat_completions():
)
from app.services.file_check_tools.dify_inputs import build_file_check_dify_inputs
from app.services.shipping_notice.dify_inputs import (
build_dify_chat_inputs as build_shipping_dify_chat_inputs,
build_dify_chat_inputs as build_shipping_notice_dify_inputs,
is_shipping_notice_chat_agent,
)
declares_file_check = agent_declares_file_check_progress(agent_proxy)
is_file_check_agent = is_file_check_chat_agent(agent_proxy)
if is_shipping_notice_chat_agent(agent_proxy):
dify_inputs = build_shipping_dify_chat_inputs()
elif declares_file_check or is_file_check_agent:
is_shipping_agent = is_shipping_notice_chat_agent(agent_proxy)
if declares_file_check or is_file_check_agent:
# [STEP 1] 先校验 task_id,避免 Dify Chatflow 缺参时长连接挂死
dify_inputs = build_file_check_dify_inputs(agent_proxy, required=True)
if is_file_check_agent:
@@ -1215,6 +1216,13 @@ def chat_completions():
agent_id=agent_proxy.id,
query=query,
)
elif is_shipping_agent:
# [STEP 2] 发货通知 Chatflow 依赖会话级 task/batch 上下文稳定定位当前任务
dify_inputs = build_shipping_notice_dify_inputs(
external_conversation_id or session.session_id,
task_id=shipping_task_id,
batch_no=shipping_batch_no,
)
generator = DifyService.stream_chat(
query,
user_proxy.id,
+8 -7
View File
@@ -36,22 +36,23 @@ def _require_tool_auth() -> None:
校验货代 Tool API 鉴权 Header。
【AI 触发时机】:本函数不是给 Agent 直接调用的 Tool;当 Dify、RPA 或外部系统请求本蓝图时由 before_request 自动触发。
核心逻辑:读取配置中的 `SHIPPING_NOTICE_TOOL_API_KEY`,并校验 `Authorization: Bearer <token>` 或
`X-Shipping-Tool-Key` 是否命中;未配置时报服务不可用,缺失或错误时报未授权。
核心逻辑:对齐文件检核 Tool,只接受 `Authorization: Bearer <token>` 或
`X-Integration-Key`,并统一使用 `INTEGRATION_API_KEY` 作为服务端校验值;
未配置时报服务不可用,缺失或错误时报未授权。
"""
configured = str(current_app.config.get("SHIPPING_NOTICE_TOOL_API_KEY") or "").strip()
configured = str(current_app.config.get("INTEGRATION_API_KEY") or "").strip()
if not configured:
raise ServiceUnavailableError(
"SHIPPING_NOTICE_TOOL_API_KEY is not configured",
"INTEGRATION_API_KEY is not configured",
code="missing_auth",
)
bearer = str(request.headers.get("Authorization") or "").strip()
token = bearer[7:].strip() if bearer.lower().startswith("bearer ") else ""
shipping_key = str(request.headers.get("X-Shipping-Tool-Key") or "").strip()
if configured in {token, shipping_key}:
integration_key = str(request.headers.get("X-Integration-Key") or "").strip()
if configured in {token, integration_key}:
return
if not token and not shipping_key:
if not token and not integration_key:
raise UnauthorizedError("missing_auth")
raise UnauthorizedError("invalid_auth")
+34 -9
View File
@@ -121,8 +121,22 @@ def workspace_create_shipping_batch_from_notices():
"conversation_id": conversation_id or None,
"file_count": len(notice_items),
"notice_items": notice_items,
# 用户态上传通知后仅完成抽文、建批/建任务与 raw_text 落库;
# 首轮订单需求解析由 Dify 画布读取 raw_text 后调用 parse_notice(structured=...)。
"defer_parse": True,
}
return jsonify(shipping_tools.create_batch_tasks(payload))
created = shipping_tools.create_batch_tasks(payload)
data = created.get("data") if isinstance(created, dict) else None
batch_data = data.get("batch") if isinstance(data, dict) else None
tasks_data = data.get("tasks") if isinstance(data, dict) else None
batch_no = str(created.get("batch_no") or (batch_data or {}).get("batch_no") or "").strip()
if isinstance(data, dict) and batch_no:
focus_task = get_focus_task_for_batch(batch_no, user.id)
if focus_task:
data["focus_task"] = serialize_task_summary(focus_task)
if isinstance(tasks_data, list):
data["tasks"] = tasks_data
return jsonify(created)
@workspace_shipping_notice_bp.route("/shipping-batches/<batch_no>", methods=["GET"])
@@ -222,7 +236,7 @@ def workspace_create_shipping_task():
@workspace_shipping_notice_bp.route("/shipping-tasks/create-from-notice", methods=["POST"])
def workspace_create_shipping_task_from_notice():
"""接收单个发货通知附件,建单后抽取文本并立即触发解析。"""
"""接收单个发货通知附件,建单后抽取文本并保存 raw_text,解析延后到 Dify。"""
user, err = _require_user()
if err:
return err
@@ -277,13 +291,24 @@ def workspace_create_shipping_task_from_notice():
task.raw_content = text
db.session.commit()
payload = {"task_id": task_id}
parsed = shipping_tools.parse_notice(payload)
data = parsed.get("data") if isinstance(parsed, dict) else None
if isinstance(data, dict):
data["notice_upload"] = {"filename": upload.filename or "", **meta}
parsed["message"] = f"已上传发货通知文件并创建任务 {task_id}。{parsed.get('message') or ''}".strip()
return jsonify(parsed)
task = ShippingTask.query.filter_by(task_no=task_id).first()
if task:
task.source_file_name = str(upload.filename or "").strip() or task.source_file_name
task.source_file_type = str(getattr(upload, "mimetype", "") or "").strip() or task.source_file_type
db.session.commit()
return jsonify(
{
"status": "ok",
"task_id": task_id,
"message": f"已上传发货通知文件并创建任务 {task_id}。通知原文已保存,等待 Dify 继续解析。",
"data": {
"task": serialize_task_summary(task) if task else {"task_no": task_id},
"notice_upload": {"filename": upload.filename or "", **meta},
"parse_status": "deferred",
},
}
)
@workspace_shipping_notice_bp.route("/shipping-tasks/<task_no>", methods=["GET"])