""" 货代发货通知 Tool API — 供 Dify Chatflow Agent 调用 """ from __future__ import annotations from flask import Blueprint, current_app, jsonify, request, send_file from pathlib import Path from app.services.shipping_notice import tools as shipping_tools from app.services.shipping_notice.openapi_spec import build_dify_openapi_spec shipping_notice_bp = Blueprint("shipping_notice", __name__) def _tool_api_base_url() -> str: """Base URL for OpenAPI servers — hxops backend, NOT Dify (8882).""" configured = str(current_app.config.get("SHIPPING_NOTICE_TOOL_API_BASE") or "").strip().rstrip("/") if configured: return configured configured = str(current_app.config.get("PUBLIC_BASE_URL") or "").strip().rstrip("/") if configured: return configured host = request.host_url.rstrip("/") if request else "" return host def _dify_openapi_spec() -> dict: return build_dify_openapi_spec(_tool_api_base_url()) def _require_tool_key(): configured = ( str(current_app.config.get("SHIPPING_NOTICE_TOOL_API_KEY") or "").strip() or str(current_app.config.get("INTEGRATION_API_KEY") or "").strip() ) if not configured: return jsonify({"error": "SHIPPING_NOTICE_TOOL_API_KEY is not configured"}), 503 incoming = ( str(request.headers.get("X-Shipping-Tool-Key") or "").strip() or str(request.headers.get("X-Integration-Key") or "").strip() ) if incoming and incoming == configured: return None return jsonify({"error": "Unauthorized"}), 401 def _payload() -> dict: return request.get_json(silent=True) or {} @shipping_notice_bp.before_request def _auth_tools(): if request.endpoint in { "shipping_notice.openapi_spec", "shipping_notice.openapi_dify_yaml", "shipping_notice.download_case_export", }: return None err = _require_tool_key() if err: return err return None @shipping_notice_bp.route("/tools/openapi.json", methods=["GET"]) def openapi_spec(): return jsonify(_dify_openapi_spec()) @shipping_notice_bp.route("/tools/openapi.dify.yaml", methods=["GET"]) def openapi_dify_yaml(): import yaml body = yaml.safe_dump(_dify_openapi_spec(), allow_unicode=True, sort_keys=False) return current_app.response_class(body, mimetype="text/yaml; charset=utf-8") @shipping_notice_bp.route("/tools/create_case", methods=["POST"]) def tool_create_case(): return jsonify(shipping_tools.create_case(_payload())) @shipping_notice_bp.route("/tools/parse_notice", methods=["POST"]) def tool_parse_notice(): return jsonify(shipping_tools.parse_notice(_payload())) @shipping_notice_bp.route("/tools/ensure_inventory", methods=["POST"]) def tool_ensure_inventory(): return jsonify(shipping_tools.ensure_inventory(_payload())) @shipping_notice_bp.route("/tools/match_inventory", methods=["POST"]) def tool_match_inventory(): return jsonify(shipping_tools.match_inventory(_payload())) @shipping_notice_bp.route("/tools/submit_decision", methods=["POST"]) def tool_submit_decision(): return jsonify(shipping_tools.submit_decision(_payload())) @shipping_notice_bp.route("/tools/generate_shipment_excel", methods=["POST"]) def tool_generate_shipment_excel(): return jsonify(shipping_tools.generate_excel(_payload())) @shipping_notice_bp.route("/tools/upload_to_freight", methods=["POST"]) def tool_upload_to_freight(): return jsonify(shipping_tools.upload_to_freight(_payload())) @shipping_notice_bp.route("/tools/get_case_status", methods=["POST"]) def tool_get_case_status(): return jsonify(shipping_tools.get_case_status(_payload())) @shipping_notice_bp.route("/rpa/inventory-ready", methods=["POST"]) def rpa_inventory_ready(): return jsonify(shipping_tools.rpa_report_inventory(_payload())) @shipping_notice_bp.route("/rpa/upload-complete", methods=["POST"]) def rpa_upload_complete(): return jsonify(shipping_tools.rpa_report_upload(_payload())) @shipping_notice_bp.route("/cases//export", methods=["GET"]) def download_case_export(case_no: str): from app.services.shipping_notice.tools import _get_case case = _get_case(case_no) if not case or not case.export_file_path: return jsonify({"error": "export file not found"}), 404 path = Path(case.export_file_path) if not path.is_file(): return jsonify({"error": "export file missing on disk"}), 404 return send_file( path, as_attachment=True, download_name=case.export_file_name or path.name, mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", )