137 lines
4.6 KiB
Python
137 lines
4.6 KiB
Python
"""
|
|
货代发货通知 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 _payload() -> dict:
|
|
return request.get_json(silent=True) or {}
|
|
|
|
|
|
@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/parse_inventory", methods=["POST"])
|
|
def tool_parse_inventory():
|
|
return jsonify(shipping_tools.parse_inventory(_payload()))
|
|
|
|
|
|
@shipping_notice_bp.route("/tools/upload_inventory_file", methods=["POST"])
|
|
def tool_upload_inventory_file():
|
|
"""Multipart upload — parse server-side without sending Excel through LLM."""
|
|
case_id = str(request.form.get("case_id") or request.args.get("case_id") or "").strip()
|
|
upload = request.files.get("file")
|
|
return jsonify(shipping_tools.upload_inventory_file(case_id, upload))
|
|
|
|
|
|
@shipping_notice_bp.route("/tools/get_inventory_schema_preview", methods=["POST"])
|
|
def tool_get_inventory_schema_preview():
|
|
return jsonify(shipping_tools.get_inventory_schema_preview(_payload()))
|
|
|
|
|
|
@shipping_notice_bp.route("/tools/apply_inventory_mapping", methods=["POST"])
|
|
def tool_apply_inventory_mapping():
|
|
return jsonify(shipping_tools.apply_inventory_mapping(_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/<case_no>/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",
|
|
)
|