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

@@ -34,20 +34,6 @@ def _clean_text(value: Any) -> str:
return str(value or "").strip()
def _batch_no(batch: Any | None, task: ShippingNotice | None) -> str:
"""
获取当前上下文的批次号。
核心逻辑:shipping_batches 已删除;仅保留显式传入 batch 对象的兼容读取。
Returns:
批次号;无法识别时返回空字符串。
"""
if batch and _clean_text(getattr(batch, "batch_no", None)):
return _clean_text(batch.batch_no).upper()
return ""
def _task_no(task: ShippingNotice | None) -> str:
"""
获取当前上下文的任务号。
@@ -66,7 +52,6 @@ def _task_no(task: ShippingNotice | None) -> str:
def _build_refs(
*,
task: ShippingNotice | None,
batch: Any | None,
run_purpose: str | None,
intake_nos: list[str] | None = None,
capability: str | None = None,
@@ -76,11 +61,10 @@ def _build_refs(
"""
构建新版 agent_runtime_context.refs。
核心逻辑:使用稳定业务字段名 task_no/batch_no/run_purpose/file_name,区别于旧 hxops_context 的 task/batch。
核心逻辑:使用稳定业务字段名 task_no/run_purpose/file_name。
Args:
task: 当前任务。
batch: 当前批次。
run_purpose: 本次 Dify run 的目的。
intake_nos: 点击发送时锁定的 Intake 编号集合。
capability: 当前固定画布能力,如 order_extraction。
@@ -90,13 +74,10 @@ def _build_refs(
"""
refs: dict[str, str] = {}
task_no = _task_no(task)
batch_no = _batch_no(batch, task)
file_name = _clean_text(getattr(task, "source_file_name", None))
normalized_run_purpose = _clean_text(run_purpose)
if task_no:
refs["task_no"] = task_no
if batch_no:
refs["batch_no"] = batch_no
if normalized_run_purpose:
refs["run_purpose"] = normalized_run_purpose
if file_name:
@@ -115,7 +96,7 @@ def _build_refs(
return refs
def _build_state_hint(task: ShippingNotice | None, batch: Any | None) -> dict[str, Any]:
def _build_state_hint(task: ShippingNotice | None) -> dict[str, Any]:
"""
构建发货通知运行态提示。
@@ -123,7 +104,6 @@ def _build_state_hint(task: ShippingNotice | None, batch: Any | None) -> dict[st
Args:
task: 当前任务。
batch: 当前批次。
Returns:
状态提示字典。
@@ -132,8 +112,6 @@ def _build_state_hint(task: ShippingNotice | None, batch: Any | None) -> dict[st
task_no = _task_no(task)
if task_no:
state_hint["focus_task_no"] = task_no
elif batch and _clean_text(getattr(batch, "current_focus_task_no", None)):
state_hint["focus_task_no"] = _clean_text(batch.current_focus_task_no).upper()
if task is not None:
for attr, key in (
@@ -147,26 +125,21 @@ def _build_state_hint(task: ShippingNotice | None, batch: Any | None) -> dict[st
if value:
state_hint[key] = value
batch_no = _batch_no(batch, task)
if batch_no:
state_hint["batch_no"] = batch_no
return state_hint
def _build_summary(
*,
task: ShippingNotice | None,
batch: Any | None,
run_purpose: str | None,
) -> str:
"""
构建给 Dify 阅读的短摘要。
核心逻辑:用自然语言说明当前 run 的任务、批次、文件和状态,避免 Dify 依赖 conversation memory 猜现场。
核心逻辑:用自然语言说明当前 run 的任务、文件和状态,避免 Dify 依赖 conversation memory 猜现场。
Args:
task: 当前任务。
batch: 当前批次。
run_purpose: 本次 Dify run 的目的。
Returns:
@@ -174,14 +147,11 @@ def _build_summary(
"""
pieces: list[str] = []
task_no = _task_no(task)
batch_no = _batch_no(batch, task)
file_name = _clean_text(getattr(task, "source_file_name", None))
current_step = _clean_text(getattr(task, "current_step", None))
normalized_run_purpose = _clean_text(run_purpose)
if task_no:
pieces.append(f"当前任务 {task_no}")
if batch_no:
pieces.append(f"属于批次 {batch_no}")
if file_name:
pieces.append(f"来源文件 {file_name}")
if normalized_run_purpose:
@@ -189,7 +159,7 @@ def _build_summary(
if current_step:
pieces.append(f"当前步骤 {current_step}")
if not pieces:
return "当前为货代发货通知 Agent 会话,尚未解析出明确任务或批次锚点。"
return "当前为货代发货通知 Agent 会话,尚未解析出明确任务锚点。"
return ",".join(pieces) + "。"
@@ -197,7 +167,6 @@ def build_shipping_runtime_context(
*,
session_id: str,
task: ShippingNotice | None = None,
batch: Any | None = None,
trigger_source: str | None = None,
request_id: str | None = None,
locale: str | None = None,
@@ -211,13 +180,12 @@ def build_shipping_runtime_context(
"""
构建货代发货通知 Agent 的 agent_runtime_context。
核心逻辑:以 HXOPS DB 中的 task/batch 为事实源,生成 refs/domain.state_hint/reference_context snapshot,
核心逻辑:以 HXOPS DB 中的 task 为事实源,生成 refs/domain.state_hint/reference_context snapshot,
供 Dify 每次 run 显式识别现场。
Args:
session_id: HXOPS 本地 ChatSession.session_id。
task: 当前显式任务;禁止在本函数内按会话猜测。
batch: 当前批次。
trigger_source: 触发来源,如 frontend_chat/backend_orchestrator。
request_id: 本次请求 ID。
locale: 用户语言。
@@ -233,14 +201,11 @@ def build_shipping_runtime_context(
"""
domain = {
"type": SHIPPING_NOTICE_AGENT_CODE,
"summary": _build_summary(task=task, batch=batch, run_purpose=run_purpose),
"state_hint": _build_state_hint(task, batch),
"summary": _build_summary(task=task, run_purpose=run_purpose),
"state_hint": _build_state_hint(task),
"available_actions_hint": [],
}
reference_context = build_shipping_reference_context(
batch=batch,
focus_task=task,
)
reference_context = build_shipping_reference_context(focus_task=task)
return build_agent_runtime_context(
agent_code=SHIPPING_NOTICE_AGENT_CODE,
scope=scope,
@@ -250,7 +215,6 @@ def build_shipping_runtime_context(
locale=locale,
refs=_build_refs(
task=task,
batch=batch,
run_purpose=run_purpose,
intake_nos=intake_nos,
capability=capability,