feat: 添加判断修改范围、规划和资源需求节点

This commit is contained in:
2026-09-23 16:51:10 +08:00
parent 5ef873bfe9
commit 5f5d733a9b
11 changed files with 566 additions and 1 deletions
+160
View File
@@ -0,0 +1,160 @@
"""将简单修改需求转换为动作列表"""
import json
from langchain_core.messages import HumanMessage, SystemMessage
from app.clients.llm import get_llm
from app.processor.base import BaseNode
from app.processor.state import QueryState
from app.prompt.action import ACTION_SYSTEM_PROMPT
from app.schemas.actions import ActionBatch
def can_generate_actions(state: QueryState) -> bool:
requirement = state["requirement"]
scope = state["change_scope"]
# 当前只接入三种已有组件操作,后续能力逐步扩展
return (
requirement.intent == "modify_screen"
and not requirement.needs_clarification
and not scope.needs_planning
and not scope.resource_needs
and bool(requirement.changes)
and all(
change.target_scope == "nodes"
and change.operation
in (
"move",
"resize",
"update",
)
for change in requirement.changes
)
)
class ActionNode(BaseNode):
name = "action_node"
async def process(self, state: QueryState) -> QueryState:
if not can_generate_actions(state):
raise ValueError(
"ActionNode 当前只支持无需规划和资源的 move、resize、update 需求"
)
batch = await get_llm(ActionBatch).ainvoke(
[
SystemMessage(content=ACTION_SYSTEM_PROMPT),
HumanMessage(
content=json.dumps(
{
"input_context": state["input_context"].model_dump(
mode="json"
),
"requirement": state["requirement"].model_dump(mode="json"),
"change_scope": state["change_scope"].model_dump(
mode="json"
),
},
ensure_ascii=False,
)
),
]
)
batch = ActionBatch.model_validate(batch.model_dump())
allowed = {}
for change in state["requirement"].changes:
for target_id in change.target_ids:
allowed.setdefault(target_id, set()).add(change.operation)
existing = {node.id for node in state["input_context"].screen.nodes}
for action in batch.actions:
if action.target_id not in existing:
raise ValueError(f"动作引用了不存在的组件: {action.target_id}")
if action.type not in allowed.get(action.target_id, set()):
raise ValueError(
f"动作超出需求范围: {action.target_id} / {action.type}"
)
expected = {
(target, operation) for target, ops in allowed.items() for operation in ops
}
actual = {(action.target_id, action.type) for action in batch.actions}
if expected - actual:
raise ValueError(
f"动作遗漏了需求中的目标或操作: {sorted(expected - actual)}"
)
return {
**state,
"actions": batch.actions,
"validation_passed": None,
"validation_errors": [],
"simulated_screen": None,
}
if __name__ == "__main__":
import asyncio
from pydantic import BaseModel
from app.processor.nodes.input_node import InputNode
from app.schemas.change_scope import ChangeScope
from app.schemas.requirement import Requirement
async def main():
state = InputNode().process(
{
"request": {
"prompt": "将销售额统计向右移动 50px",
"screen": {
"canvas": {"width": 1920, "height": 1080},
"nodes": [
{
"id": "sales-chart",
"type": "chart",
"layout": {
"x": 100,
"y": 200,
"width": 400,
"height": 300,
},
}
],
},
}
}
)
state["requirement"] = Requirement(
intent="modify_screen",
goal="向右移动 50px",
needs_clarification=False,
changes=[
{
"target_scope": "nodes",
"target_ids": ["sales-chart"],
"operation": "move",
"parameters": {"delta_x": 50},
}
],
)
state["change_scope"] = ChangeScope(
scope="local",
target_ids=["sales-chart"],
needs_planning=False,
reason="明确的组件位移",
)
node = ActionNode()
final_state = await node.process(state)
print(
json.dumps(
final_state,
ensure_ascii=False,
indent=2,
default=lambda value: (
value.model_dump(mode="json")
if isinstance(value, BaseModel)
else str(value)
),
)
)
asyncio.run(main())
+134
View File
@@ -0,0 +1,134 @@
"""按顺序在副本上模拟执行动作"""
from pydantic import BaseModel, ValidationError
from app.actions.registry import apply_action
from app.processor.base import BaseNode
from app.processor.state import QueryState
from app.schemas.action_validation import ActionValidationError
from app.schemas.actions import ActionBatch
class ValidatorNode(BaseNode):
name = "validator_node"
def process(self, state: QueryState) -> QueryState:
actions = state["actions"]
try:
# 重新校验模型内容,避免赋值后绕过字段校验
batch = ActionBatch.model_validate(
{
"actions": [
action.model_dump() if isinstance(action, BaseModel) else action
for action in actions
]
if isinstance(actions, list)
else actions
}
)
except ValidationError as error:
errors = []
for item in error.errors(include_url=False, include_input=False):
location = item["loc"]
index = (
location[1]
if len(location) > 1 and isinstance(location[1], int)
else None
)
errors.append(
ActionValidationError(
action_index=index,
code="schema_error",
message=item["msg"],
)
)
return {
**state,
"validation_passed": False,
"validation_errors": errors,
"simulated_screen": None,
}
screen = state["input_context"].screen.model_copy(deep=True)
for index, action in enumerate(batch.actions):
try:
screen = apply_action(screen, action)
except ValueError as error:
# 首个失败即停止,后续动作可能依赖前序结果,不返回部分成功快照
return {
**state,
"validation_passed": False,
"validation_errors": [
ActionValidationError(
action_index=index,
code="execution_error",
message=str(error),
)
],
"simulated_screen": None,
}
return {
**state,
"actions": batch.actions,
"validation_passed": True,
"validation_errors": [],
"simulated_screen": screen,
}
if __name__ == "__main__":
import json
from app.processor.nodes.input_node import InputNode
from app.schemas.actions import MoveAction, ResizeAction, UpdateAction
# 1. 创建 state,不需要调用模型
state = InputNode().process(
{
"request": {
"prompt": "移动、调整尺寸并修改标题",
"screen": {
"canvas": {"width": 1920, "height": 1080},
"nodes": [
{
"id": "sales-chart",
"type": "chart",
"layout": {"x": 100, "y": 200, "width": 400, "height": 300},
"props": {"title": "销售额"},
}
],
},
}
}
)
state["actions"] = [
MoveAction(target_id="sales-chart", delta_x=50),
ResizeAction(target_id="sales-chart", width=500),
UpdateAction(
target_id="sales-chart", section="props", path=["title"], value="月度销售额"
),
]
# 2. 执行节点并打印完整 state
node = ValidatorNode()
final_state = node.process(state)
print(
json.dumps(
final_state,
ensure_ascii=False,
indent=2,
default=lambda value: value.model_dump(mode="json"),
)
)
# 3. 在第二个动作制造错误,验证整批失败且不返回部分结果
failed_state = {
**final_state,
"actions": [
state["actions"][0],
MoveAction(target_id="missing", delta_x=50),
],
}
failed_result = node.process(failed_state)
print("失败校验:", failed_result["validation_passed"])
print("错误:", [error.model_dump() for error in failed_result["validation_errors"]])
print("失败快照:", failed_result["simulated_screen"])