- 新增后端完整业务支持:数据库模型、API接口、核心服务与RPA对接逻辑 - 新增前端工作台页面、路由适配与多语言国际化 - 新增Dify自定义工具OpenAPI规范、Agent配置脚本与部署文档 - 新增发货Excel模板生成脚本与默认模板 - 修复测试配置文件中的环境变量命名错误 - 更新本地开发环境变量配置
164 lines
6.0 KiB
Python
164 lines
6.0 KiB
Python
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
|
||
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)",
|
||
"content": {
|
||
"application/json": {
|
||
"schema": {"type": "object"},
|
||
}
|
||
},
|
||
}
|
||
},
|
||
"security": [{"ShippingToolKey": []}],
|
||
}
|
||
}
|
||
|
||
|
||
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"
|
||
|
||
case_id = _str_prop("发货任务编号,如 SN-20260529-ABCD1234")
|
||
|
||
return {
|
||
"openapi": "3.0.0",
|
||
"info": {
|
||
"title": "Hxops Shipping Notice Tools",
|
||
"version": "1.0.0",
|
||
"description": "货代发货通知 Agent Tool API。仅包含 Chatflow 编排工具,不含 RPA 回调。",
|
||
},
|
||
"servers": [{"url": server}],
|
||
"components": {
|
||
"securitySchemes": {
|
||
"ShippingToolKey": {
|
||
"type": "apiKey",
|
||
"in": "header",
|
||
"name": "X-Shipping-Tool-Key",
|
||
"description": "与 hxops SHIPPING_NOTICE_TOOL_API_KEY 相同",
|
||
}
|
||
}
|
||
},
|
||
"security": [{"ShippingToolKey": []}],
|
||
"paths": {
|
||
"/tools/create_case": _post_op(
|
||
"create_case",
|
||
"创建发货任务 case",
|
||
{
|
||
"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"]),
|
||
"raw_content": _str_prop("原始发货通知文本(可选,后续 parse 也可传入)"),
|
||
},
|
||
),
|
||
"/tools/parse_notice": _post_op(
|
||
"parse_notice",
|
||
"解析发货通知文本",
|
||
{
|
||
"case_id": case_id,
|
||
"raw_text": _str_prop("待解析的发货通知全文"),
|
||
},
|
||
required=["case_id"],
|
||
),
|
||
"/tools/ensure_inventory": _post_op(
|
||
"ensure_inventory",
|
||
"确保库存快照可用(RPA 或手工 JSON)",
|
||
{
|
||
"case_id": case_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=["case_id"],
|
||
),
|
||
"/tools/match_inventory": _post_op(
|
||
"match_inventory",
|
||
"匹配发货需求与库存",
|
||
{
|
||
"case_id": case_id,
|
||
"inventory_lines_json": _str_prop("可选,覆盖库存 JSON 数组后再匹配"),
|
||
},
|
||
required=["case_id"],
|
||
),
|
||
"/tools/submit_decision": _post_op(
|
||
"submit_decision",
|
||
"人工复核决策",
|
||
{
|
||
"case_id": case_id,
|
||
"decision": _str_prop(
|
||
"confirm=确认继续;reject=驳回;rematch=重新匹配;reparse=重新解析",
|
||
enum=["confirm", "reject", "rematch", "reparse"],
|
||
),
|
||
"comment": _str_prop("备注(可选)"),
|
||
},
|
||
required=["case_id", "decision"],
|
||
),
|
||
"/tools/generate_shipment_excel": _post_op(
|
||
"generate_shipment_excel",
|
||
"生成固定模板发货 Excel",
|
||
{"case_id": case_id},
|
||
required=["case_id"],
|
||
),
|
||
"/tools/upload_to_freight": _post_op(
|
||
"upload_to_freight",
|
||
"上传 Excel 到货代系统(RPA 或手工确认)",
|
||
{
|
||
"case_id": case_id,
|
||
"mode": _str_prop(
|
||
"auto=触发 RPA;retry_upload=重试;manual_confirm=手工上传完成",
|
||
enum=["auto", "retry_upload", "manual_confirm"],
|
||
),
|
||
"freight_ref_no": _str_prop("货代系统单号(manual_confirm 时填写)"),
|
||
},
|
||
required=["case_id"],
|
||
),
|
||
"/tools/get_case_status": _post_op(
|
||
"get_case_status",
|
||
"查询 case 当前状态与 human_options",
|
||
{"case_id": case_id},
|
||
required=["case_id"],
|
||
),
|
||
},
|
||
}
|