feat(file-check): 更新文件检核工具 API Key 配置及相关文档
This commit is contained in:
1 parent
634dee4b81
commit
55cf7ff734
74 files changed
+1525
-1182
No files matched your search
@@ -1,16 +1,14 @@
|
||||
"""
|
||||
Canonical registry for shipping-notice human nodes.
|
||||
services/shipping_notice/state_machine.py
|
||||
|
||||
Single source of truth for:
|
||||
- checkpoint ids (API `checkpoint`, Dify `flow_ckpt`)
|
||||
- action button specs (`action_buttons`, HXOPS_SHIPPING_ACTIONS)
|
||||
- task DB fields → checkpoint + human_options (chat-actions API)
|
||||
- task DB field writes (`TaskTransition` / `apply_task_transition` — tools.py must use these)
|
||||
用途:发货通知人工节点(checkpoint)与动作按钮的单一注册表,负责任务 DB 字段与 UI 状态的映射。
|
||||
依赖:models.ShippingTask、constants(CP_*/PH_*/RS_*)、rpa_attempts。
|
||||
逻辑:tools.py 通过 apply_task_transition 写库并返回 checkpoint;chat_actions 与前端 chat-actions API 读本模块生成按钮与 ui_state;新增人工节点时仅改本文件与测试。
|
||||
|
||||
When adding a human node, update ONLY this module (+ tests), then:
|
||||
- tools.py sets task fields via `apply_task_transition(...)` and calls `tool_response(..., checkpoint=CP_*)`
|
||||
- Dify `组装直接回复` embeds directive only from HTTP `act_json` (approach B); text-only rounds use Hxops `/chat-actions`
|
||||
- `GET /tools/checkpoint-catalog` is for developers/docs/CI — not wired in Dify canvas
|
||||
扩展说明(开发者):
|
||||
- checkpoint id 即 API `checkpoint` / Dify `flow_ckpt`
|
||||
- 动作按钮 id 定义于 ACTION_SPECS;DB 写入统一走 TaskTransition
|
||||
- GET /tools/checkpoint-catalog 供文档/CI,不接入 Dify 画布
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -154,6 +152,7 @@ class TaskUiState:
|
||||
|
||||
|
||||
def build_action_buttons(option_ids: list[str] | None) -> list[dict[str, str]]:
|
||||
"""将 human_options 动作 id 列表转为前端/Dify 可用的 action_buttons 结构。"""
|
||||
if not option_ids:
|
||||
return []
|
||||
buttons: list[dict[str, str]] = []
|
||||
@@ -166,6 +165,7 @@ def build_action_buttons(option_ids: list[str] | None) -> list[dict[str, str]]:
|
||||
|
||||
|
||||
def human_options_for_checkpoint(checkpoint: str | None) -> list[str] | None:
|
||||
"""按静态 checkpoint id 查 CHECKPOINT_REGISTRY 中的默认 human_options。"""
|
||||
if not checkpoint:
|
||||
return None
|
||||
key = str(checkpoint).strip()
|
||||
@@ -176,10 +176,12 @@ def human_options_for_checkpoint(checkpoint: str | None) -> list[str] | None:
|
||||
|
||||
|
||||
def action_buttons_for_checkpoint(checkpoint: str | None) -> list[dict[str, str]]:
|
||||
"""checkpoint → action_buttons 的便捷封装(tool_response 与 Dify fallback 用)。"""
|
||||
return build_action_buttons(human_options_for_checkpoint(checkpoint))
|
||||
|
||||
|
||||
def format_actions_directive(option_ids: list[str] | None) -> str:
|
||||
"""将 action_buttons 序列化为 HTML 注释指令,嵌入 Agent 回复供前端解析。"""
|
||||
buttons = build_action_buttons(option_ids)
|
||||
if not buttons:
|
||||
return ""
|
||||
@@ -188,6 +190,7 @@ def format_actions_directive(option_ids: list[str] | None) -> str:
|
||||
|
||||
|
||||
def ui_state_for_checkpoint(checkpoint: str | None) -> TaskUiState:
|
||||
"""无 DB 任务时,仅按 checkpoint id 构造 TaskUiState(Tool 早停响应用)。"""
|
||||
options = human_options_for_checkpoint(checkpoint)
|
||||
cp = str(checkpoint).strip() if checkpoint else None
|
||||
if cp and cp not in CHECKPOINT_REGISTRY:
|
||||
@@ -196,7 +199,7 @@ def ui_state_for_checkpoint(checkpoint: str | None) -> TaskUiState:
|
||||
|
||||
|
||||
def upload_req_options_for_task(task: ShippingTask) -> list[str]:
|
||||
"""upload_req checkpoint with RPA-attempt-aware button subsets."""
|
||||
"""upload_req checkpoint 的动作子集,随 RPA 重试次数动态收缩。"""
|
||||
if task.pending_human_reason == PH_RPA_EXHAUSTED:
|
||||
return list(OPTIONS_UPLOAD_FILE_ONLY)
|
||||
if inventory_rpa_attempts(task) >= max_rpa_inventory_attempts():
|
||||
@@ -211,7 +214,7 @@ def human_options_for_task_or_checkpoint(
|
||||
task: ShippingTask | None = None,
|
||||
checkpoint: str | None = None,
|
||||
) -> list[str] | None:
|
||||
"""Prefer task DB state; fall back to static checkpoint registry (for tool_response)."""
|
||||
"""优先从 task DB 状态解析 human_options;无 task 时回退静态 checkpoint 注册表。"""
|
||||
if task is not None:
|
||||
opts = resolve_task_ui_state(task).human_options
|
||||
if opts is not None:
|
||||
@@ -220,7 +223,12 @@ def human_options_for_task_or_checkpoint(
|
||||
|
||||
|
||||
def resolve_task_ui_state(task: ShippingTask) -> TaskUiState:
|
||||
"""Map ShippingTask DB fields → checkpoint + human_options for /chat-actions."""
|
||||
"""
|
||||
将 ShippingTask DB 字段映射为 checkpoint + human_options(/chat-actions 核心)。
|
||||
|
||||
核心逻辑:终态 → 正向流程节点 → pending_human_reason 分支 → 默认 None。
|
||||
"""
|
||||
# [STEP 1] 终态:已上传、已关闭、已驳回 — 无可用按钮。
|
||||
if task.process_status == "uploaded":
|
||||
return TaskUiState(checkpoint=CP_UPLOADED, human_options=[])
|
||||
if task.process_status == "closed":
|
||||
@@ -229,6 +237,7 @@ def resolve_task_ui_state(task: ShippingTask) -> TaskUiState:
|
||||
if task.process_status == "rejected" or task.review_status == "rejected":
|
||||
return TaskUiState(checkpoint=CP_REJECTED, human_options=[])
|
||||
|
||||
# [STEP 2] RPA 库存耗尽与解析后待库存 — 正向流程中的「下一步」节点。
|
||||
if task.pending_human_reason == PH_RPA_EXHAUSTED:
|
||||
return TaskUiState(
|
||||
checkpoint=CP_UPLOAD_REQ,
|
||||
@@ -259,6 +268,7 @@ def resolve_task_ui_state(task: ShippingTask) -> TaskUiState:
|
||||
human_options=list(CHECKPOINT_REGISTRY[CP_READY_MATCH]),
|
||||
)
|
||||
|
||||
# [STEP 3] 匹配/导出阶段 — matched 后区分待生成与已生成 Excel。
|
||||
if task.process_status == "matched":
|
||||
if task.export_file_path:
|
||||
return TaskUiState(
|
||||
@@ -270,6 +280,7 @@ def resolve_task_ui_state(task: ShippingTask) -> TaskUiState:
|
||||
human_options=list(CHECKPOINT_REGISTRY[CP_READY_EXPORT]),
|
||||
)
|
||||
|
||||
# [STEP 4] 人审分支 — 按 pending_human_reason / review_status 映射 checkpoint。
|
||||
reason = task.pending_human_reason
|
||||
if reason == PH_ALIGN_REVIEW or task.review_status == RS_ALIGN_REVIEW:
|
||||
return TaskUiState(
|
||||
@@ -322,14 +333,22 @@ def resolve_task_ui_state(task: ShippingTask) -> TaskUiState:
|
||||
|
||||
|
||||
def checkpoint_for_task(task: ShippingTask) -> str | None:
|
||||
"""从 ShippingTask DB 字段解析当前 checkpoint id。"""
|
||||
return resolve_task_ui_state(task).checkpoint
|
||||
|
||||
|
||||
def human_options_for_task(task: ShippingTask) -> list[str] | None:
|
||||
"""从 ShippingTask DB 字段解析当前可用 human_options(含 RPA 动态子集)。"""
|
||||
return resolve_task_ui_state(task).human_options
|
||||
|
||||
|
||||
def build_chat_actions_payload(task: ShippingTask) -> dict[str, Any]:
|
||||
"""
|
||||
组装 /chat-actions API 响应体,供前端渲染步骤条与动作按钮。
|
||||
|
||||
Returns:
|
||||
dict: 含 task_no、checkpoint、process_status、action_buttons 等字段。
|
||||
"""
|
||||
ui = resolve_task_ui_state(task)
|
||||
return {
|
||||
"task_no": task.task_no,
|
||||
@@ -342,7 +361,7 @@ def build_chat_actions_payload(task: ShippingTask) -> dict[str, Any]:
|
||||
|
||||
|
||||
def build_checkpoint_catalog() -> dict[str, Any]:
|
||||
"""Export for docs, CI, and developer reference — not embedded in Dify (approach B)."""
|
||||
"""导出 checkpoint 注册表供文档/CI 校验(不接入 Dify 画布)。"""
|
||||
checkpoints = []
|
||||
for checkpoint_id, option_ids in CHECKPOINT_REGISTRY.items():
|
||||
buttons = build_action_buttons(list(option_ids))
|
||||
@@ -363,7 +382,7 @@ def build_checkpoint_catalog() -> dict[str, Any]:
|
||||
|
||||
|
||||
def validate_registry() -> list[str]:
|
||||
"""Return list of validation errors (empty if ok). Used in tests."""
|
||||
"""校验 CHECKPOINT_REGISTRY 中 action id 是否均存在于 ACTION_SPECS;测试用。"""
|
||||
errors: list[str] = []
|
||||
for checkpoint_id, option_ids in CHECKPOINT_REGISTRY.items():
|
||||
for option_id in option_ids:
|
||||
@@ -534,7 +553,15 @@ TASK_TRANSITIONS: dict[TaskTransition, _TransitionSpec] = {
|
||||
|
||||
|
||||
def apply_task_transition(task: ShippingTask, transition: TaskTransition) -> None:
|
||||
"""Apply a named transition to task process/review/pending_human_reason fields."""
|
||||
"""
|
||||
将命名状态迁移写入 ShippingTask 的 process_status / review_status / pending_human_reason。
|
||||
|
||||
核心逻辑:查 TASK_TRANSITIONS 表驱动字段更新;所有 Tool 写库须经本函数,禁止散落赋值。
|
||||
|
||||
Args:
|
||||
task: 待更新的 ShippingTask 实例(尚未 commit)。
|
||||
transition: TaskTransition 枚举值。
|
||||
"""
|
||||
spec = TASK_TRANSITIONS[transition]
|
||||
if spec.process_status is not _UNSET:
|
||||
task.process_status = spec.process_status
|
||||
|
||||
Reference in new issue
Block a user