feat: 添加判断修改范围、规划和资源需求节点
This commit is contained in:
+22
-1
@@ -13,6 +13,8 @@ from app.processor.nodes.requirement_node import RequirementNode
|
||||
from app.processor.nodes.wait_for_user_node import WaitForUserNode
|
||||
from app.processor.nodes.qa_node import QANode
|
||||
from app.processor.nodes.scope_node import ScopeNode
|
||||
from app.processor.nodes.action_node import ActionNode, can_generate_actions
|
||||
from app.processor.nodes.validator_node import ValidatorNode
|
||||
from app.processor.state import QueryState
|
||||
|
||||
|
||||
@@ -41,6 +43,8 @@ def build_graph(checkpointer=None):
|
||||
"wait_for_user_node": WaitForUserNode(),
|
||||
"qa_node": QANode(),
|
||||
"scope_node": ScopeNode(),
|
||||
"action_node": ActionNode(),
|
||||
"validator_node": ValidatorNode(),
|
||||
}
|
||||
|
||||
# 注册节点,异步节点使用 BaseNode 的异步入口
|
||||
@@ -71,7 +75,14 @@ def build_graph(checkpointer=None):
|
||||
)
|
||||
workflow.add_edge("clarify_node", "wait_for_user_node")
|
||||
workflow.add_edge("qa_node", END)
|
||||
workflow.add_edge("scope_node", END)
|
||||
# 其余能力仍停在范围判断,后续接入 Planner 和 Resource
|
||||
workflow.add_conditional_edges(
|
||||
"scope_node",
|
||||
can_generate_actions,
|
||||
{True: "action_node", False: END},
|
||||
)
|
||||
workflow.add_edge("action_node", "validator_node")
|
||||
workflow.add_edge("validator_node", END)
|
||||
workflow.add_conditional_edges(
|
||||
"wait_for_user_node",
|
||||
route_after_wait,
|
||||
@@ -85,6 +96,11 @@ def build_graph(checkpointer=None):
|
||||
("app.schemas.decision", "DecisionResponse"),
|
||||
("app.schemas.requirement", "Requirement"),
|
||||
("app.schemas.change_scope", "ChangeScope"),
|
||||
("app.schemas.actions.move", "MoveAction"),
|
||||
("app.schemas.actions.resize", "ResizeAction"),
|
||||
("app.schemas.actions.update", "UpdateAction"),
|
||||
("app.schemas.action_validation", "ActionValidationError"),
|
||||
("app.schemas.screen", "Screen"),
|
||||
("app.schemas.clarification", "Clarification"),
|
||||
("app.schemas.clarification_reply", "ClarificationReply"),
|
||||
]
|
||||
@@ -167,6 +183,11 @@ if __name__ == "__main__":
|
||||
},
|
||||
ensure_ascii=False,
|
||||
indent=2,
|
||||
default=lambda value: (
|
||||
value.model_dump(mode="json")
|
||||
if isinstance(value, BaseModel)
|
||||
else str(value)
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -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())
|
||||
@@ -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"])
|
||||
@@ -1,5 +1,7 @@
|
||||
from typing import Literal, NotRequired, TypedDict
|
||||
|
||||
from app.schemas.action_validation import ActionValidationError
|
||||
from app.schemas.actions import Action
|
||||
from app.schemas.base import JsonObject
|
||||
from app.schemas.change_scope import ChangeScope
|
||||
from app.schemas.clarification import Clarification
|
||||
@@ -7,6 +9,7 @@ from app.schemas.clarification_reply import ClarificationReply
|
||||
from app.schemas.decision import DecisionResponse
|
||||
from app.schemas.input_context import InputContext
|
||||
from app.schemas.requirement import Intent, Requirement
|
||||
from app.schemas.screen import Screen
|
||||
|
||||
|
||||
class QueryState(TypedDict):
|
||||
@@ -16,6 +19,18 @@ class QueryState(TypedDict):
|
||||
intent: NotRequired[Intent | None] # 经过置信度门控的最终意图
|
||||
requirement: NotRequired[Requirement | None] # 结构化需求及信息完整性判断
|
||||
change_scope: NotRequired[ChangeScope | None] # 修改范围、规划和资源需求
|
||||
actions: NotRequired[
|
||||
list[Action] | None
|
||||
] # 生成的动作列表,是否可模拟执行由校验结果标记
|
||||
validation_passed: NotRequired[
|
||||
bool | None
|
||||
] # 整批模拟执行是否通过,None 表示尚未校验
|
||||
validation_errors: NotRequired[
|
||||
list[ActionValidationError]
|
||||
] # 动作下标、错误类型和原因
|
||||
simulated_screen: NotRequired[
|
||||
Screen | None
|
||||
] # 整批通过后的模拟快照,不覆盖原始上下文
|
||||
clarification: NotRequired[Clarification | None] # 当前澄清问题及选项
|
||||
clarification_reply: NotRequired[
|
||||
ClarificationReply | None
|
||||
|
||||
Reference in New Issue
Block a user