129 lines
3.7 KiB
Python
129 lines
3.7 KiB
Python
"""
|
|
services/shipping_notice/tool_common.py
|
|
|
|
用途:发货通知 Tool 链共享配置、payload 合并与消息拼接(无阶段业务编排)。
|
|
依赖:constants、models.ShippingTaskEvent。
|
|
逻辑:task_workflow / inventory_workflow / fulfillment_workflow 共用,避免 tools 循环依赖。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Any
|
|
import uuid
|
|
|
|
from flask import current_app
|
|
|
|
from app import db
|
|
from app.models import ShippingTask, ShippingTaskEvent
|
|
from app.services.shipping_notice.constants import AUTO_CHAIN_FALLBACK
|
|
|
|
|
|
def cfg_bool(key: str, default: bool = False) -> bool:
|
|
val = current_app.config.get(key)
|
|
if val is None:
|
|
return default
|
|
if isinstance(val, bool):
|
|
return val
|
|
return str(val).strip().lower() in {"1", "true", "yes", "on"}
|
|
|
|
|
|
def cfg_int(key: str, default: int = 0, *, min_value: int | None = None) -> int:
|
|
val = current_app.config.get(key)
|
|
if val is None:
|
|
parsed = default
|
|
else:
|
|
try:
|
|
parsed = int(val)
|
|
except (TypeError, ValueError):
|
|
parsed = default
|
|
if min_value is not None:
|
|
parsed = max(parsed, min_value)
|
|
return parsed
|
|
|
|
|
|
def auto_progress_enabled() -> bool:
|
|
return cfg_bool("SHIPPING_NOTICE_AUTO_PROGRESS_ENABLED", False)
|
|
|
|
|
|
def auto_rpa_inventory_enabled() -> bool:
|
|
return cfg_bool("SHIPPING_NOTICE_AUTO_RPA_INVENTORY_ENABLED", True)
|
|
|
|
|
|
def rpa_inventory_timeout_seconds() -> int:
|
|
return cfg_int("SHIPPING_NOTICE_RPA_INVENTORY_TIMEOUT_SECONDS", 120, min_value=5)
|
|
|
|
|
|
def auto_mapping_allow_llm() -> bool:
|
|
return cfg_bool("SHIPPING_NOTICE_AUTO_MAPPING_ALLOW_LLM", False)
|
|
|
|
|
|
def auto_match_enabled() -> bool:
|
|
return cfg_bool("SHIPPING_NOTICE_AUTO_MATCH_ENABLED", True)
|
|
|
|
|
|
def auto_chain_fallback_enabled() -> bool:
|
|
return cfg_bool("SHIPPING_NOTICE_AUTO_CHAIN_FALLBACK", True)
|
|
|
|
|
|
def payload_wants_auto_chain_fallback(payload: dict[str, Any] | None) -> bool:
|
|
"""True when caller explicitly opts into backend fallback chaining (Hxops workspace/chat)."""
|
|
if not payload or not auto_progress_enabled() or not auto_chain_fallback_enabled():
|
|
return False
|
|
val = payload.get(AUTO_CHAIN_FALLBACK)
|
|
if val is None:
|
|
return False
|
|
if isinstance(val, bool):
|
|
return val
|
|
return str(val).strip().lower() in {"1", "true", "yes", "on"}
|
|
|
|
|
|
def merge_tool_payload(
|
|
task: ShippingTask,
|
|
payload: dict[str, Any] | None = None,
|
|
*,
|
|
extra: dict[str, Any] | None = None,
|
|
) -> dict[str, Any]:
|
|
result = tool_payload_for_task(task, payload)
|
|
if extra:
|
|
result.update(extra)
|
|
if payload_wants_auto_chain_fallback(payload):
|
|
result[AUTO_CHAIN_FALLBACK] = True
|
|
return result
|
|
|
|
|
|
def tool_payload_for_task(task: ShippingTask, payload: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
result: dict[str, Any] = {"task_id": task.task_no}
|
|
if not payload:
|
|
return result
|
|
for key in ("lang", "locale", "language"):
|
|
lang = str(payload.get(key) or "").strip()
|
|
if lang:
|
|
result["lang"] = lang
|
|
break
|
|
return result
|
|
def prepend_tool_message(result: dict[str, Any], prefix: str) -> dict[str, Any]:
|
|
note = str(prefix or "").strip()
|
|
if not note:
|
|
return result
|
|
body = str(result.get("message") or "").strip()
|
|
return {**result, "message": f"{note}\n\n{body}" if body else note}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_task_no() -> str:
|
|
return f"SN-{datetime.utcnow().strftime('%Y%m%d')}-{uuid.uuid4().hex[:8].upper()}"
|
|
|
|
def log_task_event(task: ShippingTask, event_type: str, detail: dict[str, Any] | None = None) -> None:
|
|
if task.id is None:
|
|
db.session.flush()
|
|
db.session.add(ShippingTaskEvent(
|
|
task_id=task.id,
|
|
event_type=event_type,
|
|
detail_json=detail or {},
|
|
))
|