from __future__ import annotations from typing import Any from app.models import ShippingTask 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"}, "generate_excel": {"label": "生成发货 Excel", "message": "生成发货 Excel", "variant": "primary"}, "mark_manual_uploaded": { "label": "已手工上传", "message": "已在货代系统手工上传完成", "variant": "primary", }, "paste_inventory_json": { "label": "粘贴库存", "message": "我粘贴库存小表", "variant": "secondary", }, "edit_inventory_mapping": { "label": "修改映射", "message": "修改库存列映射", "variant": "primary", }, "match_inventory": { "label": "匹配库存", "message": "匹配库存", "variant": "primary", }, } def human_options_for_task(task: ShippingTask) -> list[str] | None: if task.process_status == "rejected" or task.review_status == "rejected": return [] if ( task.process_status == "parsed" and not task.review_status and not task.pending_human_reason and not task.inventory_lines_json ): return ["upload_inventory_file", "retry_rpa", "paste_inventory_json"] if ( task.process_status == "inventory_ready" and not task.review_status and not task.pending_human_reason and not task.match_status and isinstance(task.lines_json, list) and len(task.lines_json) > 0 ): return ["match_inventory"] if task.process_status == "matched": if task.export_file_path: return ["download_excel", "mark_manual_uploaded"] return ["generate_excel"] reason = task.pending_human_reason if reason == "inventory_field_mapping_review" or task.review_status == "inventory_mapping_review": return ["confirm_inventory_mapping", "correct_inventory_mapping", "reparse_inventory", "reject"] if reason == "inventory_mapping_inference": return ["edit_inventory_mapping", "upload_inventory_file", "paste_inventory_json", "retry_rpa"] if reason == "reservation_id_required" or task.review_status == "reservation_id_required": return ["reparse", "edit_inventory_mapping", "upload_inventory_file", "reject"] if reason == "inventory_unavailable": return ["retry_rpa", "upload_inventory_file"] if reason == "inventory_shortage" or task.review_status == "inventory_shortage_review": return ["confirm", "reject", "rematch"] if reason == "low_parse_confidence" or task.review_status == "parse_review": return ["confirm", "reject", "reparse"] if reason == "upload_rpa_failed" or task.process_status == "upload_failed": return ["retry_upload", "download_excel", "mark_manual_uploaded"] if reason == "inventory_rpa_pending" or ( task.process_status == "waiting_inventory" and reason == "inventory_rpa_pending" ): return ["retry_rpa", "upload_inventory_file"] if reason == "upload_rpa_pending" or task.process_status == "waiting_upload": return ["retry_upload", "download_excel", "mark_manual_uploaded"] return None def checkpoint_for_task(task: ShippingTask) -> str | None: if task.process_status == "rejected" or task.review_status == "rejected": return "rejected" reason = task.pending_human_reason if reason == "inventory_field_mapping_review" or task.review_status == "inventory_mapping_review": return "inventory_field_mapping_review" if reason == "low_parse_confidence" or task.review_status == "parse_review": return "parse_review" if reason == "inventory_shortage" or task.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 == "reservation_id_required" or task.review_status == "reservation_id_required": return "reservation_id_required" if reason == "upload_rpa_failed" or task.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(task: ShippingTask) -> dict[str, Any]: options = human_options_for_task(task) buttons = build_action_buttons(options) return { "task_no": task.task_no, "checkpoint": checkpoint_for_task(task), "process_status": task.process_status, "review_status": task.review_status, "pending_human_reason": task.pending_human_reason, "action_buttons": buttons, }