feat: 完成后端处理链路、优化部分提示词

This commit is contained in:
2026-09-26 12:53:07 +08:00
parent 0e5738788e
commit d3e2c6b544
11 changed files with 133 additions and 54 deletions
+41 -7
View File
@@ -8,6 +8,7 @@ from app.schemas.action_validation import ActionValidationError
from app.schemas.actions import (
ActionBatch,
AddAction,
DeleteAction,
MoveAction,
ResizeAction,
UpdateAction,
@@ -25,16 +26,15 @@ def plan_block_reason(state: QueryState) -> str | None:
return "规划包含画布修改,当前尚未接入对应动作"
if plan.resource_needs:
return "规划仍有待获取资源,不能直接生成动作"
if any(
item.disposition not in {"add", "modify", "keep"} for item in plan.components
):
return "当前规划执行支持新增、调整和保留组件,移除后续接入"
additions = [item for item in plan.components if item.disposition == "add"]
removals = {
item.target_id for item in plan.components if item.disposition == "remove"
}
modifications = {
item.target_id: item for item in plan.components if item.disposition == "modify"
}
if not additions and not modifications:
return "规划未包含可执行的新增或调整组件"
if not additions and not modifications and not removals:
return "规划未包含可执行的新增、调整或删除组件"
if additions and (resources is None or resources.status != "resolved"):
return "组件目录尚未就绪"
supported = {item.type for item in resources.components} if resources else set()
@@ -54,10 +54,21 @@ def plan_block_reason(state: QueryState) -> str | None:
# 已有目标的修改诉求不能通过 keep 或遗漏目标绕过
supported_operations = {"move", "resize", "update", "restyle", "reflow", "layout"}
changes = state["requirement"].changes
# 删除必须逐个目标获得明确授权,整体布局调整不隐含删除权限
allowed_removals = {
target
for change in changes
if change.target_scope == "nodes" and change.operation == "delete"
for target in change.target_ids
}
if removals != allowed_removals:
return "规划删除目标与需求不一致,不能遗漏或扩大删除范围"
for change in changes:
if change.target_scope == "screen" and change.operation == "add":
if not additions:
return "规划遗漏新增需求"
elif change.target_scope == "nodes" and change.operation == "delete":
continue
elif (
change.target_scope in {"nodes", "screen"}
and change.operation in supported_operations
@@ -149,10 +160,23 @@ def validate_plan_actions(
materials = {item.key: item for item in resources.components} if resources else {}
seen = set()
modified = set()
removals = {
item.target_id
for item in state["screen_plan"].components
if item.disposition == "remove"
}
removed = set()
errors = []
for index, action in enumerate(batch.actions):
message = None
if not isinstance(action, AddAction):
if isinstance(action, DeleteAction):
if action.target_id not in removals:
message = f"不能删除非 remove 规划目标: {action.target_id}"
elif action.target_id in removed:
message = f"不能重复删除组件: {action.target_id}"
else:
removed.add(action.target_id)
elif not isinstance(action, AddAction):
if not isinstance(action, (MoveAction, ResizeAction, UpdateAction)):
message = "规划调整仅支持 move、resize 和 update"
elif action.target_id not in modifications:
@@ -168,6 +192,10 @@ def validate_plan_actions(
message = f"动作未关联有效的新增规划: {action.plan_id}"
elif action.plan_id in seen:
message = f"同一规划组件不能重复新增: {action.plan_id}"
elif action.node.id in {
node.id for node in state["input_context"].screen.nodes
}:
message = f"新增组件不能复用原有 ID: {action.node.id}"
else:
seen.add(action.plan_id)
item = expected[action.plan_id]
@@ -202,6 +230,12 @@ def validate_plan_actions(
code="scope_error", message=f"遗漏调整规划组件: {sorted(missing)}"
)
)
if missing := removals - removed:
errors.append(
ActionValidationError(
code="scope_error", message=f"遗漏删除规划组件: {sorted(missing)}"
)
)
return errors
+2
View File
@@ -4,6 +4,7 @@ from typing import Any
from app.actions.add import AddHandler
from app.actions.base import BaseActionHandler
from app.actions.delete import DeleteHandler
from app.actions.move import MoveHandler
from app.actions.resize import ResizeHandler
from app.actions.update import UpdateHandler
@@ -12,6 +13,7 @@ from app.schemas.screen import Screen
# 新增操作时在此注册,未实现的操作明确报错
ACTION_HANDLERS: dict[str, BaseActionHandler[Any]] = {
"delete": DeleteHandler(),
"add": AddHandler(),
"move": MoveHandler(),
"resize": ResizeHandler(),