feat(shipping-notice): 新增通用解析能力,优化性能并清理遗留废弃代码

This commit is contained in:
hugo committed 2026-08-23 00:27:18 +08:00
1 parent 935bf26fa6
commit 7cfd5d167e
230 files changed
+9337 -10989

No files matched your search

@@ -1,7 +1,7 @@
"""
backend/app/services/shipping_notice/reference_context.py
用途:从发货通知批次/任务/事件构建 shipping_notice 专用 reference_context。
用途:从发货通知任务/事件构建 shipping_notice 专用 reference_context。
依赖:models.ShippingNotice/ShippingTaskEvent、agent_runtime_context.reference。
逻辑:runtime_context 在调用 Dify 前经本模块提炼当前 focus、最近动作、最近失败和候选对象,避免把全量事件流水直接暴露给 Runtime。
"""
@@ -59,7 +59,7 @@ def _task_recency_key(task: ShippingNotice) -> tuple[datetime, int]:
return updated_at, int(getattr(task, "id", 0) or 0)
def _collect_batch_tasks(batch: Any | None, focus_task: ShippingNotice | None) -> list[ShippingNotice]:
def _collect_focus_task(focus_task: ShippingNotice | None) -> list[ShippingNotice]:
"""
汇总当前 reference_context 可见的任务集合。
@@ -72,9 +72,9 @@ def _collect_batch_tasks(batch: Any | None, focus_task: ShippingNotice | None) -
def _recent_events(tasks: list[ShippingNotice]) -> list[ShippingTaskEvent]:
"""
查询最近关键事件。
查询当前任务最近关键事件。
核心逻辑:只取当前批次/任务的最近少量事件,用于推断最后动作,不把全量事件流水塞进上下文。
核心逻辑:只取当前任务的最近少量事件,用于推断最后动作,不把全量事件流水塞进上下文。
"""
task_ids = [int(task.id) for task in tasks if getattr(task, "id", None) is not None]
if not task_ids:
@@ -210,7 +210,6 @@ def _candidate_refs(tasks: list[ShippingNotice], focus_task_no: str) -> list[dic
def _build_recent_activity_summary(
*,
batch: Any | None,
focus_task_no: str,
last_action_summary: str,
last_failed_summary: str,
@@ -226,8 +225,6 @@ def _build_recent_activity_summary(
pieces.append(last_action_summary)
if focus_task_no:
pieces.append(f"当前 focus 为 {focus_task_no}")
elif _clean_text(getattr(batch, "batch_no", None)):
pieces.append(f"当前批次为 {_clean_text(batch.batch_no).upper()}")
if last_failed_summary:
pieces.append(last_failed_summary)
if len(candidates) > 1:
@@ -243,7 +240,6 @@ def _build_recent_activity_summary(
def build_shipping_reference_context(
*,
batch: Any | None,
focus_task: ShippingNotice | None,
) -> dict[str, Any]:
"""
@@ -252,20 +248,17 @@ def build_shipping_reference_context(
【AI 触发时机】:当用户在 HXOPS 已做确认、重匹配、切票等动作后,又在对话里说“继续”“刚才那个”“上一个失败的”时,
Dify 需要优先读取这份上下文理解当前现场,但不能直接据此推进业务状态。
核心逻辑:读取 batch.current_focus_task_no、任务状态和最近少量事件,提炼 focus_object / last_action_target /
核心逻辑:读取当前任务状态和最近少量事件,提炼 focus_object / last_action_target /
last_failed_target / candidates / recent_activity_summary 五类短期线索;不输出全量任务详情或完整事件日志。
Args:
batch: 当前批次;可为空。
focus_task: 当前焦点任务;可为空。
Returns:
shipping_notice 领域的 reference_context;没有有效线索时返回空 dict。
"""
tasks = _collect_batch_tasks(batch, focus_task)
tasks = _collect_focus_task(focus_task)
focus_task_no = _task_no(focus_task)
if not focus_task_no and batch and _clean_text(getattr(batch, "current_focus_task_no", None)):
focus_task_no = _clean_text(batch.current_focus_task_no).upper()
if not tasks and not focus_task_no:
return {}
@@ -278,7 +271,6 @@ def build_shipping_reference_context(
focus_object = build_object_ref("task", focus_task_no) if focus_task_no else {}
recent_activity_summary = _build_recent_activity_summary(
batch=batch,
focus_task_no=focus_task_no,
last_action_summary=last_action_summary,
last_failed_summary=last_failed_summary,