feat(chat): 更新发货任务处理逻辑和界面文本

- 移除不必要的状态引用
- 更新上传处理提示文本,使用国际化支持
- 扩展发货任务摘要接口,增加错误信息和短缺信息
- 更新多语言支持的文本,改善用户体验
- 增加批次工作区的汇总信息和操作提示
This commit is contained in:
hugo committed 2026-06-29 14:43:27 +08:00
1 parent 0b083cfdbd
commit 1fc9976ca3
60 files changed
+3876 -639

No files matched your search

@@ -14,7 +14,7 @@ from enum import Enum
from pathlib import Path
from typing import Any
from sqlalchemy import desc
from sqlalchemy import asc, desc
from app import db
from app.models import ShippingBatch, ShippingTask
@@ -146,6 +146,55 @@ def compute_batch_process_summary(tasks: list[ShippingTask]) -> dict[str, int]:
}
def _is_task_completed(task: ShippingTask) -> bool:
"""判断任务是否已进入完成态,用于会话级聚合统计。"""
return str(task.process_status or "").strip().lower() in {"uploaded", "completed", "closed"}
def _is_task_failed(task: ShippingTask) -> bool:
"""判断任务是否失败或上传失败,用于会话级异常优先展示。"""
return (
str(task.process_status or "").strip().lower() in {"rejected"}
or str(task.upload_status or "").strip().lower() == "failed"
)
def _is_task_needs_human(task: ShippingTask) -> bool:
"""判断任务是否停在人工门;失败态不重复计入人工门。"""
if _is_task_failed(task) or _is_task_completed(task):
return False
return bool(getattr(task, "review_status", None) or getattr(task, "pending_human_reason", None))
def _is_task_active(task: ShippingTask) -> bool:
"""判断任务是否仍处于自动执行或等待外部系统返回的状态。"""
if _is_task_completed(task) or _is_task_failed(task) or _is_task_needs_human(task):
return False
status = str(task.process_status or "").strip().lower()
return status not in {"created", "pending_review"} and bool(status)
def compute_conversation_workspace_summary(batches: list[ShippingBatch]) -> dict[str, int]:
"""按会话聚合多个批次的状态计数,供 Agent 会话级工作台展示。"""
tasks: list[ShippingTask] = []
for batch in batches:
tasks.extend(list(batch.tasks or []))
completed = sum(1 for task in tasks if _is_task_completed(task))
failed = sum(1 for task in tasks if _is_task_failed(task))
needs_human = sum(1 for task in tasks if _is_task_needs_human(task))
running = sum(1 for task in tasks if _is_task_active(task))
pending = max(0, len(tasks) - completed - failed - needs_human - running)
return {
"batch_count": len(batches),
"total": len(tasks),
"completed": completed,
"running": running,
"failed": failed,
"needs_human": needs_human,
"pending": pending,
}
def extract_task_no_from_text(text: str) -> str | None:
"""从自然语言文本中提取 SN- 任务编号。"""
match = _TASK_NO_RE.search(str(text or ""))
@@ -243,9 +292,13 @@ def serialize_task(task: ShippingTask, projection: TaskProjection) -> dict[str,
"upload_mode": task.upload_mode,
"upload_attempt": task.upload_attempt,
"pending_human_reason": task.pending_human_reason,
"error_message": task.error_message or task.upload_error_message,
"conversation_id": task.conversation_id,
"export_file_name": task.export_file_name,
"freight_ref_no": task.freight_ref_no,
"step_payload": task.step_payload_json or {},
"shortage": task.shortage_json,
"line_results": task.line_results_json or [],
"created_at": task.created_at.isoformat() if task.created_at else None,
"updated_at": task.updated_at.isoformat() if task.updated_at else None,
}
@@ -321,6 +374,38 @@ def serialize_batch_summary(batch: ShippingBatch) -> dict[str, Any]:
}
def serialize_conversation_batch_workspace(batch: ShippingBatch, user_id: int) -> dict[str, Any]:
"""序列化会话工作台中的单个批次,包含批次摘要、任务列表与焦点任务。"""
focus_task = get_focus_task_for_batch(batch.batch_no, user_id)
return {
**serialize_batch_summary(batch),
"tasks": list_batch_task_summaries(batch),
"focus_task": serialize_task_summary(focus_task) if focus_task else None,
}
def get_conversation_batch_workspace(conversation_id: str, user_id: int) -> dict[str, Any]:
"""返回一个 Agent 会话下用户可见的所有发货批次与聚合摘要。"""
value = str(conversation_id or "").strip()
batches: list[ShippingBatch] = []
if value:
batches = (
ShippingBatch.query.filter(ShippingBatch.conversation_id == value)
.filter((ShippingBatch.user_id == user_id) | (ShippingBatch.user_id.is_(None)))
.order_by(asc(ShippingBatch.created_at), asc(ShippingBatch.id))
.all()
)
ordered_batches = batches
return {
"conversation_id": value,
"summary": compute_conversation_workspace_summary(ordered_batches),
"batches": [
serialize_conversation_batch_workspace(batch, user_id)
for batch in ordered_batches
],
}
def serialize_task_summary(task: ShippingTask) -> dict[str, Any]:
"""工作台任务列表用的 SUMMARY 投影。"""
return serialize_task(task, TaskProjection.SUMMARY)