""" services/shipping_notice/openapi_spec.py 用途:构建发货通知 Tool API 的 OpenAPI 描述,供 Dify 导入和联调用。 依赖:shipping_notice.constants 中的请求字段常量、标准 `typing` 类型。 逻辑:由 api/shipping_notice_tools.py 调用本文件生成 JSON/YAML OpenAPI 文档,再暴露给 Dify 或开发工具。 """ from __future__ import annotations from typing import Any from app.services.shipping_notice.constants import MAP_JSON def _str_prop(description: str, *, enum: list[str] | None = None) -> dict[str, Any]: prop: dict[str, Any] = {"type": "string", "description": description} if enum: prop["enum"] = enum return prop def _int_prop(description: str) -> dict[str, Any]: return {"type": "integer", "description": description} def _post_op( operation_id: str, summary: str, properties: dict[str, Any], *, required: list[str] | None = None, ) -> dict[str, Any]: schema: dict[str, Any] = {"type": "object", "properties": properties} if required: schema["required"] = required return { "post": { "operationId": operation_id, "summary": summary, "description": summary, "parameters": [], "requestBody": { "required": True, "content": { "application/json": { "schema": schema, } }, }, "responses": { "200": { "description": ( "Tool response (status: ok|running|needs_human|failed). " "May include checkpoint, human_options, action_buttons." ), "content": { "application/json": { "schema": {"type": "object"}, } }, } }, } } def build_dify_openapi_spec(base_url: str) -> dict[str, Any]: """Build OpenAPI 3.0 spec for Dify Chatflow custom tools (agent-facing only).""" server = str(base_url or "").strip().rstrip("/") if not server.endswith("/api/shipping-notice"): server = f"{server}/api/shipping-notice" if server else "/api/shipping-notice" task_id = _str_prop("发货任务编号(task id),如 SN-20260529-ABCD1234") batch_id = _str_prop("发货批次编号(batch id),如 SB-20260617-ABCD1234") return { "openapi": "3.0.0", "info": { "title": "Hxops Shipping Notice Tools", "version": "1.0.0", "description": "货代发货通知 Agent Tool API。仅包含 Chatflow 编排工具,不含 RPA 回调。鉴权:Authorization Bearer 或 X-Integration-Key。", }, "servers": [{"url": server}], "paths": { "/tools/create_batch": _post_op( "create_batch", "创建发货批次 batch(一次多文件上传的聚合容器)", { "batch_no": _str_prop("已存在的批次编号(可选)。传入时将直接复用该批次。"), "user_id": _int_prop("Hxops 用户 ID(可选)"), "conversation_id": _str_prop("Dify 会话 ID,便于工作台关联"), "file_count": _int_prop("本批通知文件数量"), }, ), "/tools/create_task": _post_op( "create_task", "创建发货任务 task(可挂到某个 batch)", { "batch_no": batch_id, "task_id": _str_prop("已存在的发货任务编号(可选)。传入时将直接复用该任务,禁止重复创建。"), "user_id": _int_prop("Hxops 用户 ID(可选)"), "conversation_id": _str_prop("Dify 会话 ID,便于工作台关联"), "source_type": _str_prop("来源:manual|email|wecom|pdf", enum=["manual", "email", "wecom", "pdf"]), "source_file_name": _str_prop("原始通知文件名(多文件场景建议传)"), "source_file_type": _str_prop("原始通知文件类型(多文件场景建议传)"), "batch_index": _int_prop("当前任务在批次中的顺序,从 1 开始"), "batch_total": _int_prop("当前批次总任务数"), "raw_content": _str_prop("原始发货通知文本(可选,后续 parse 也可传入)"), }, ), "/tools/parse_notice": _post_op( "parse_notice", "解析发货通知文本", { "task_id": task_id, "raw_text": _str_prop("待解析的发货通知全文"), }, required=["task_id"], ), "/tools/update_notice_fields": _post_op( "update_notice_fields", "更新解析后的发货通知单头字段,如客户、PO、交期、收货地", { "task_id": task_id, "fields": { "type": "object", "description": "可更新字段:customer_name/customer_code/ship_to/po_number/required_date", "additionalProperties": True, }, "comment": _str_prop("修改备注(可选)"), }, required=["task_id", "fields"], ), "/tools/update_notice_line": _post_op( "update_notice_line", "更新解析结果中的某一行,如 SKU、Reservation ID、数量、交期", { "task_id": task_id, "line_index": _int_prop("行下标,从 0 开始"), "patch": { "type": "object", "description": "可更新字段:sku/reservation_id/quantity/qty/required_date/uom", "additionalProperties": True, }, "comment": _str_prop("修改备注(可选)"), }, required=["task_id", "line_index", "patch"], ), "/tools/add_notice_line": _post_op( "add_notice_line", "向解析结果新增一行发货需求", { "task_id": task_id, "line": { "type": "object", "description": "新增行,至少包含 sku 和 qty/quantity", "additionalProperties": True, }, "comment": _str_prop("修改备注(可选)"), }, required=["task_id", "line"], ), "/tools/remove_notice_line": _post_op( "remove_notice_line", "删除解析结果中的某一行发货需求", { "task_id": task_id, "line_index": _int_prop("行下标,从 0 开始"), "comment": _str_prop("修改备注(可选)"), }, required=["task_id", "line_index"], ), "/tools/parse_inventory": _post_op( "parse_inventory", "解析库存。大 Excel 禁止 raw_inventory_text(会超 LLM token);" "先 POST /tools/upload_inventory_file 上传,再 get_inventory_schema_preview → " "apply_inventory_mapping;或 use_uploaded_file=true(规则映射兜底)。小表可粘贴文本。" "成功后 stop_agent_turn,等 confirm_inventory_mapping 后再 match。", { "task_id": task_id, "use_uploaded_file": _str_prop( "true=解析该任务已上传的 Excel(服务端直传,不经过 LLM)", enum=["true", "false"], ), MAP_JSON: _str_prop( '可选,列映射 JSON 字符串,如 {"sku":"AWS Sku","available_qty":"Qty Available"}' ), "raw_inventory_text": _str_prop( "仅小表(约 <8 万字符)。大 Excel 勿传此字段,改用 upload_inventory_file" ), "inventory_lines_json": _str_prop( '可选,已整理好的 JSON 数组字符串,如 [{"sku":"A001","available_qty":10}]' ), }, required=["task_id"], ), "/tools/get_inventory_schema_preview": _post_op( "get_inventory_schema_preview", "读取已上传库存 Excel 的表头与样例行(小 payload,供 LLM 推断列映射)", {"task_id": task_id}, required=["task_id"], ), "/tools/apply_inventory_mapping": _post_op( "apply_inventory_mapping", "应用 LLM/人工列映射并解析完整库存文件;返回 field_alignment 等待用户确认", { "task_id": task_id, MAP_JSON: _str_prop( '列映射 JSON,含 sku/reservation_id/available_qty;可选 match_rule(LLM 自然语言匹配说明,缺省则后端动态兜底)' ), }, required=["task_id", MAP_JSON], ), "/tools/ensure_inventory": _post_op( "ensure_inventory", "确保库存快照可用(RPA 自动导出;对话库存请优先用 parse_inventory)", { "task_id": task_id, "mode": _str_prop("auto=自动/RPA;retry_rpa=重试 RPA", enum=["auto", "retry_rpa"]), "inventory_lines_json": _str_prop( '手工库存 JSON 数组字符串,如 [{"sku":"A001","available_qty":10}]' ), }, required=["task_id"], ), "/tools/auto_progress": _post_op( "auto_progress", "按后端全局配置自动推进:RPA 获取库存 → 字段对齐(满足条件可自动确认)→ 匹配 → 生成 Excel →(可选)上传。遇到人工节点会停住返回 action_buttons。", { "task_id": task_id, "conversation_id": _str_prop("Dify 会话 ID(可选,用于仅知道会话时定位任务)"), }, ), "/tools/match_inventory": _post_op( "match_inventory", "匹配发货需求与库存。前置条件:submit_decision(confirm_inventory_mapping) 已成功;" "未确认时调用会被拒绝并返回 forbidden_next_tools", { "task_id": task_id, "inventory_lines_json": _str_prop("可选,覆盖库存 JSON 数组后再匹配"), }, required=["task_id"], ), "/tools/submit_decision": _post_op( "submit_decision", "人工复核决策。task_id 与 conversation_id 至少传一个;用户确认时禁止 create_task", { "task_id": task_id, "conversation_id": _str_prop("Dify 会话 ID;用户仅回复「确认」时用于定位任务"), "decision": _str_prop( "confirm=确认继续;reject=驳回;rematch=重新匹配;reparse=重新解析;" "confirm_inventory_mapping=确认库存字段对齐;correct_inventory_mapping=更正列映射;reparse_inventory=重新上传库存", enum=[ "confirm", "confirm_parse", "reject", "rematch", "reparse", "confirm_inventory_mapping", "correct_inventory_mapping", "reparse_inventory", ], ), "comment": _str_prop("备注(可选)"), }, required=["decision"], ), "/tools/generate_shipment_excel": _post_op( "generate_shipment_excel", "生成固定模板发货 Excel(须 match_inventory 成功且 process_status=matched)", {"task_id": task_id}, required=["task_id"], ), "/tools/upload_to_freight": _post_op( "upload_to_freight", "上传 Excel 到货代系统(RPA 或手工确认)", { "task_id": task_id, "mode": _str_prop( "auto=触发 RPA;retry_upload=重试;manual_confirm=手工上传完成", enum=["auto", "retry_upload", "manual_confirm"], ), "freight_ref_no": _str_prop("货代系统单号(可选;manual_confirm 通常无需填写)"), }, required=["task_id"], ), "/tools/get_task_status": _post_op( "get_task_status", "查询任务当前状态与 human_options。task_id 与 conversation_id 至少传一个", { "task_id": task_id, "conversation_id": _str_prop("Dify 会话 ID"), }, ), "/tools/get_batch_status": _post_op( "get_batch_status", "查询批次当前状态、任务列表与当前聚焦任务", { "batch_no": batch_id, }, required=["batch_no"], ), "/tools/switch_focus_task": _post_op( "switch_focus_task", "切换当前批次的聚焦任务", { "batch_no": batch_id, "task_no": task_id, "target_task_no": task_id, }, required=["batch_no"], ), }, }