from __future__ import annotations from typing import Any from app.models import ShippingCase ACTION_SPECS: dict[str, dict[str, str]] = { "confirm": {"label": "确认", "message": "确认", "variant": "primary"}, "reject": {"label": "驳回", "message": "驳回", "variant": "danger"}, "reparse": {"label": "重新解析", "message": "重新解析", "variant": "secondary"}, "confirm_inventory_mapping": {"label": "确认", "message": "确认", "variant": "primary"}, "correct_inventory_mapping": {"label": "更正映射", "message": "更正映射", "variant": "secondary"}, "reparse_inventory": {"label": "重新上传", "message": "重新上传库存", "variant": "secondary"}, "rematch": {"label": "重新匹配", "message": "重新匹配", "variant": "secondary"}, "retry_rpa": {"label": "重试 RPA", "message": "重试 RPA 导出库存", "variant": "secondary"}, "upload_inventory_file": { "label": "选择库存文件", "message": "我要上传库存 Excel", "variant": "secondary", }, "retry_upload": {"label": "重试上传", "message": "重试上传货代", "variant": "secondary"}, "download_excel": {"label": "下载 Excel", "message": "下载发货 Excel", "variant": "secondary"}, "mark_manual_uploaded": { "label": "已手工上传", "message": "已在货代系统手工上传完成", "variant": "primary", }, "paste_inventory_json": { "label": "粘贴库存", "message": "我粘贴库存小表", "variant": "secondary", }, } def human_options_for_case(case: ShippingCase) -> list[str] | None: if case.process_status == "rejected" or case.review_status == "rejected": return [] reason = case.pending_human_reason if reason == "inventory_field_mapping_review" or case.review_status == "inventory_mapping_review": return ["confirm_inventory_mapping", "correct_inventory_mapping", "reparse_inventory", "reject"] if reason == "inventory_mapping_inference": return ["upload_inventory_file", "paste_inventory_json"] if reason == "inventory_unavailable": return ["retry_rpa", "upload_inventory_file"] if reason == "inventory_shortage" or case.review_status == "inventory_shortage_review": return ["confirm", "reject", "rematch"] if reason == "low_parse_confidence" or case.review_status == "parse_review": return ["confirm", "reject", "reparse"] if reason == "upload_rpa_failed" or case.process_status == "upload_failed": return ["retry_upload", "download_excel", "mark_manual_uploaded"] if reason == "inventory_rpa_pending" or ( case.process_status == "waiting_inventory" and reason == "inventory_rpa_pending" ): return ["retry_rpa", "upload_inventory_file"] if reason == "upload_rpa_pending" or case.process_status == "waiting_upload": return ["retry_upload", "download_excel", "mark_manual_uploaded"] return None def checkpoint_for_case(case: ShippingCase) -> str | None: if case.process_status == "rejected" or case.review_status == "rejected": return "rejected" reason = case.pending_human_reason if reason == "inventory_field_mapping_review" or case.review_status == "inventory_mapping_review": return "inventory_field_mapping_review" if reason == "low_parse_confidence" or case.review_status == "parse_review": return "parse_review" if reason == "inventory_shortage" or case.review_status == "inventory_shortage_review": return "inventory_shortage_review" if reason == "inventory_unavailable": return "inventory_upload_required" if reason == "inventory_mapping_inference": return "inventory_mapping_inference" if reason == "upload_rpa_failed" or case.process_status == "upload_failed": return "upload_failed" return None def build_action_buttons(option_ids: list[str] | None) -> list[dict[str, str]]: if not option_ids: return [] buttons: list[dict[str, str]] = [] for option_id in option_ids: spec = ACTION_SPECS.get(option_id) if not spec: continue buttons.append({"id": option_id, **spec}) return buttons def build_chat_actions_payload(case: ShippingCase) -> dict[str, Any]: options = human_options_for_case(case) buttons = build_action_buttons(options) return { "case_no": case.case_no, "checkpoint": checkpoint_for_case(case), "process_status": case.process_status, "review_status": case.review_status, "pending_human_reason": case.pending_human_reason, "action_buttons": buttons, }