feat(shipping-notice): 新增临时库存管理功能,支持创建会话、获取分析结果及字段映射建议
This commit is contained in:
1 parent
46eb63f5bb
commit
e9bdd81f1a
320 files changed
+28361
-10487
No files matched your search
@@ -10,9 +10,6 @@ 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:
|
||||
@@ -77,6 +74,30 @@ def build_dify_openapi_spec(base_url: str) -> dict[str, Any]:
|
||||
runtime_context = _str_prop(
|
||||
"HXOPS 注入的完整 agent_runtime_context JSON 字符串;必须原样透传,禁止由 LLM 构造或修改"
|
||||
)
|
||||
observation_schema = {
|
||||
"type": "object",
|
||||
"required": ["value", "document_id", "quote"],
|
||||
"properties": {
|
||||
"value": {"description": "从证据原文提取的字段值,类型必须符合动态字段契约"},
|
||||
"document_id": _int_prop("证据来源 Document ID,必须来自抽取上下文"),
|
||||
"quote": _str_prop("证据原文中的逐字引用,HXOPS 将验证其确实存在"),
|
||||
"confidence": {"type": "number", "minimum": 0, "maximum": 1},
|
||||
},
|
||||
"additionalProperties": False,
|
||||
}
|
||||
extracted_field_schema = {
|
||||
"type": "object",
|
||||
"required": ["field_id", "observations"],
|
||||
"properties": {
|
||||
"field_id": _str_prop("动态字段契约中的稳定 field_id,禁止传 field_code 或显示名称"),
|
||||
"observations": {
|
||||
"type": "array",
|
||||
"minItems": 1,
|
||||
"items": observation_schema,
|
||||
},
|
||||
},
|
||||
"additionalProperties": False,
|
||||
}
|
||||
|
||||
return {
|
||||
"openapi": "3.0.0",
|
||||
@@ -107,7 +128,7 @@ def build_dify_openapi_spec(base_url: str) -> dict[str, Any]:
|
||||
required=["agent_runtime_context"],
|
||||
description=(
|
||||
"仅在 refs.capability=order_extraction 分支调用。Tool 从 platform.session_id 反查当前用户,"
|
||||
"并返回 input_snapshot_hash、documents 与固定输出契约;禁止让 LLM 传 user_id。"
|
||||
"并返回 input_snapshot_hash、documents 与当前 published 动态 Schema 契约;禁止让 LLM 传 user_id。"
|
||||
),
|
||||
),
|
||||
"/tools/submit_order_extraction": _post_op(
|
||||
@@ -123,61 +144,43 @@ def build_dify_openapi_spec(base_url: str) -> dict[str, Any]:
|
||||
"items": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"schema_version_id",
|
||||
"schema_checksum",
|
||||
"source_intake_no",
|
||||
"primary_document_id",
|
||||
"classification",
|
||||
"classification_confidence",
|
||||
"field_confidence",
|
||||
"header_fields",
|
||||
"lines",
|
||||
"evidence",
|
||||
],
|
||||
"properties": {
|
||||
"source_intake_no": _str_prop("来源 Intake 编号,必须来自上下文"),
|
||||
"schema_version_id": _int_prop("context.contract.schema_version_id,必须原样回传"),
|
||||
"schema_checksum": _str_prop("context.contract.checksum,必须原样回传"),
|
||||
"primary_document_id": _int_prop("主来源 Document ID,必须来自上下文"),
|
||||
"candidate_key_hint": _str_prop("可选稳定订单标识,如 PO/订单号;不得使用随机值"),
|
||||
"classification": _str_prop("仅 shipping_request 可自动建单", enum=["shipping_request", "non_shipping_request", "uncertain"]),
|
||||
"classification_confidence": {"type": "number", "minimum": 0, "maximum": 1},
|
||||
"field_confidence": {
|
||||
"type": "object",
|
||||
"required": ["part_number", "qty"],
|
||||
"properties": {
|
||||
"part_number": {"type": "number", "minimum": 0, "maximum": 1},
|
||||
"qty": {"type": "number", "minimum": 0, "maximum": 1},
|
||||
},
|
||||
"header_fields": {
|
||||
"type": "array",
|
||||
"description": "Header scope 动态字段;仅可使用 context.contract.fields 中的稳定 field_id",
|
||||
"items": extracted_field_schema,
|
||||
},
|
||||
"po_number": _str_prop("可为空的 PO/订单号"),
|
||||
"lines": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"required": ["part_number", "qty"],
|
||||
"required": ["fields"],
|
||||
"properties": {
|
||||
"part_number": _str_prop("内部料号/主匹配料号"),
|
||||
"qty": {"type": "number", "exclusiveMinimum": 0},
|
||||
"reservation_id": _str_prop("可为空,缺失时 Task 停在补字段门禁"),
|
||||
"inventory_source_hint": _str_prop(
|
||||
"可选库存来源提示;仅在原文有可靠证据时填写,"
|
||||
"不代表任务通知来源或已确认的库存连接器类型"
|
||||
),
|
||||
"customer_part_number": _str_prop("可选客户料号"),
|
||||
"aws_part_number": _str_prop("可选 AWS 料号"),
|
||||
"fields": {
|
||||
"type": "array",
|
||||
"description": "Line scope 动态字段;每项含 field_id 与 observations[]",
|
||||
"items": extracted_field_schema,
|
||||
},
|
||||
},
|
||||
"additionalProperties": False,
|
||||
},
|
||||
},
|
||||
"evidence": {
|
||||
"type": "array",
|
||||
"description": "至少分别提供内部料号与 Qty 原文证据,元素含 field 与非空 quote",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"required": ["field", "quote"],
|
||||
"properties": {
|
||||
"field": _str_prop("证据字段,如 lines[0].part_number 或 lines[0].qty"),
|
||||
"quote": _str_prop("原文片段,禁止改写"),
|
||||
"document_id": _int_prop("证据来源 Document ID"),
|
||||
},
|
||||
},
|
||||
},
|
||||
"conflicts": {
|
||||
"type": "array",
|
||||
"items": {"type": "object", "additionalProperties": True},
|
||||
@@ -189,13 +192,73 @@ def build_dify_openapi_spec(base_url: str) -> dict[str, Any]:
|
||||
},
|
||||
required=["agent_runtime_context", "input_snapshot_hash", "orders"],
|
||||
description=(
|
||||
"只接受 get_order_extraction_context 的同一快照。HXOPS 仅在分类、内部料号/Qty 置信度及原文证据均达标,"
|
||||
"且无阻断冲突/修订决策时自动建单;Reservation ID 允许缺失,"
|
||||
"订单行 inventory_source_hint 是可选路由线索并独立持久化。"
|
||||
"只接受同一快照和同一 Schema checksum。Dify 必须逐字段返回稳定 field_id 与 observations[];"
|
||||
"每个 Observation 包含 value、document_id、逐字 quote 和可选 confidence。"
|
||||
"HXOPS 验证原文、执行类型/validator/证据策略与跨 Document 裁决后,才按 field_code 投影给下游。"
|
||||
"响应以持久化结果分别返回 created_count、opened_existing_count、pending_count、"
|
||||
"non_shipping_count;auto_created_count 仅为兼容字段且等于 created_count。"
|
||||
),
|
||||
),
|
||||
"/tools/create_temporary_inventory_session": _post_op(
|
||||
"create_temporary_inventory_session",
|
||||
"创建对话上传库存文件的临时分析会话",
|
||||
{
|
||||
"agent_runtime_context": runtime_context,
|
||||
"asset_no": _str_prop("当前用户上传的 FileAsset 编号;必须是 xls、xlsx 或 csv"),
|
||||
},
|
||||
required=["agent_runtime_context", "asset_no"],
|
||||
description=(
|
||||
"仅在用户明确说明附件为库存文件的 temporary_inventory 分支调用。"
|
||||
"后端按会话用户校验资产归属并执行结构初检;不会创建生产库存快照、"
|
||||
"不会绑定库存站点、不会执行库存匹配。"
|
||||
),
|
||||
),
|
||||
"/tools/get_temporary_inventory_session": _post_op(
|
||||
"get_temporary_inventory_session",
|
||||
"读取临时库存文件的脱敏结构分析和方案候选",
|
||||
{
|
||||
"agent_runtime_context": runtime_context,
|
||||
"temporary_session_no": _str_prop("create_temporary_inventory_session 返回的临时会话编号"),
|
||||
},
|
||||
required=["agent_runtime_context", "temporary_session_no"],
|
||||
description=(
|
||||
"返回后端已分析的工作表、表头、字段目录和已发布方案候选,不返回样例库存值。"
|
||||
"Dify 不得读取文件路径、全量库存行,也不得据此直接宣称库存已可用于订单匹配。"
|
||||
),
|
||||
),
|
||||
"/tools/submit_temporary_inventory_mapping_suggestion": _post_op(
|
||||
"submit_temporary_inventory_mapping_suggestion",
|
||||
"保存对话临时库存的候选字段映射",
|
||||
{
|
||||
"agent_runtime_context": runtime_context,
|
||||
"temporary_session_no": _str_prop("临时库存会话编号"),
|
||||
"field_mapping": {
|
||||
"type": "object",
|
||||
"description": "库存字段目录 ID 到真实来源表头的映射;只能使用 get_temporary_inventory_session 返回的字段 ID 和表头",
|
||||
"additionalProperties": {"type": "string"},
|
||||
},
|
||||
},
|
||||
required=["agent_runtime_context", "temporary_session_no", "field_mapping"],
|
||||
description=(
|
||||
"仅在没有 exact 已发布方案可复用时调用。HXOPS 校验字段目录、真实表头、重复列和核心字段完整性,"
|
||||
"只保存待用户确认的候选;不会发布方案、绑定库存站点、创建库存快照或执行订单匹配。"
|
||||
),
|
||||
),
|
||||
"/tools/confirm_temporary_inventory_use": _post_op(
|
||||
"confirm_temporary_inventory_use",
|
||||
"确认当前对话临时库存文件使用",
|
||||
{
|
||||
"agent_runtime_context": runtime_context,
|
||||
"temporary_session_no": _str_prop("临时库存会话编号"),
|
||||
"selected_profile_id": _int_prop("仅 exact 已发布方案候选时由用户选择的方案 ID"),
|
||||
},
|
||||
required=["agent_runtime_context", "temporary_session_no"],
|
||||
description=(
|
||||
"仅在用户明确确认本次使用后调用。HXOPS 重新确定性解析文件并校验料号、预约号、库存数量;"
|
||||
"只锁定当前对话的临时会话审计,不接受任务编号,绝不创建生产库存快照、绑定库存站点、"
|
||||
"写入任务库存行或执行库存匹配。"
|
||||
),
|
||||
),
|
||||
"/tools/parse_notice": _post_op(
|
||||
"parse_notice",
|
||||
"解析发货通知文本",
|
||||
@@ -267,38 +330,18 @@ def build_dify_openapi_spec(base_url: str) -> dict[str, Any]:
|
||||
},
|
||||
required=["task_id", "line_index"],
|
||||
),
|
||||
"/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(
|
||||
"Dify Tool 参数使用 JSON 字符串,内容含 part_number/reservation_id/available_qty,"
|
||||
"可选 aws_part_number;"
|
||||
"可选 match_rule(LLM 自然语言匹配说明)与 match_key(匹配键列表)。"
|
||||
),
|
||||
},
|
||||
required=["task_id", MAP_JSON],
|
||||
),
|
||||
"/tools/confirm_inventory_source": _post_op(
|
||||
"confirm_inventory_source",
|
||||
"在库存源人工检查点确认正式库存查询来源(仅提交 source_code)",
|
||||
{
|
||||
"task_id": task_id,
|
||||
"source_code": _str_prop(
|
||||
"启用配置中的库存源编码,如 DSV360_AWSDU、CRANE;"
|
||||
"站点 site_id 与库位 warehouse_id 由后端根据配置与订单路由解析,禁止前端直传"
|
||||
"启用配置中的库存源编码;Warehouse 由 fixed Source 配置或库存行匹配解析,禁止前端直传"
|
||||
),
|
||||
},
|
||||
required=["task_id", "source_code"],
|
||||
description=(
|
||||
"库存连接器门禁:稳定身份须同时具备 source_code、connector_type、site_id、warehouse_id,"
|
||||
"库存连接器门禁:稳定身份须具备 source_code、connector_type;fixed Source 同时具备 warehouse_id,"
|
||||
"且 routing_fingerprint 与当前配置版本一致。"
|
||||
"Tool API 使用集成密钥鉴权,确认人审计由宿主会话内部链路写入,不接受外部 user_id。"
|
||||
"若返回 needs_human,说明身份未完整解析或需先完成解析确认。"
|
||||
@@ -324,8 +367,7 @@ def build_dify_openapi_spec(base_url: str) -> dict[str, Any]:
|
||||
"task_id": task_id,
|
||||
"conversation_id": _str_prop("Dify 会话 ID;用户仅回复「确认」时用于定位任务"),
|
||||
"decision": _str_prop(
|
||||
"confirm=确认继续;reject=驳回;retry_rpa=重新获取库存(清理旧库存解析状态并重新触发 RPA);rematch=重新匹配;reparse=重新解析;"
|
||||
"confirm_inventory_mapping=确认库存字段对齐;correct_inventory_mapping=更正列映射;reparse_inventory=重新上传库存",
|
||||
"confirm=确认继续;reject=驳回;retry_rpa=按已发布解析方案重新获取库存;rematch=重新匹配;reparse=重新解析",
|
||||
enum=[
|
||||
"confirm",
|
||||
"confirm_parse",
|
||||
@@ -333,9 +375,6 @@ def build_dify_openapi_spec(base_url: str) -> dict[str, Any]:
|
||||
"retry_rpa",
|
||||
"rematch",
|
||||
"reparse",
|
||||
"confirm_inventory_mapping",
|
||||
"correct_inventory_mapping",
|
||||
"reparse_inventory",
|
||||
],
|
||||
),
|
||||
"comment": _str_prop("备注(可选)"),
|
||||
@@ -358,7 +397,7 @@ def build_dify_openapi_spec(base_url: str) -> dict[str, Any]:
|
||||
"conversation_id": _str_prop("Dify 会话 ID"),
|
||||
},
|
||||
description=(
|
||||
"返回 inventory_source_code、inventory_connector_type、inventory_site_id、"
|
||||
"返回 inventory_source_code、inventory_connector_type、"
|
||||
"inventory_warehouse_id、inventory_source_status(unresolved|ambiguous|resolved|"
|
||||
"user_confirmed|invalid|conflicted)、inventory_source_method、"
|
||||
"inventory_source_config_version、inventory_routing_fingerprint、"
|
||||
|
||||
Reference in new issue
Block a user