feat:简化部分代码
This commit is contained in:
@@ -21,26 +21,10 @@ class PlannerNode(BaseNode):
|
||||
requirement = state["requirement"]
|
||||
scope = state["change_scope"]
|
||||
|
||||
# 如果需求仍需澄清、不是新建或修改大屏,或无需规划,则拒绝进入本节点
|
||||
if (
|
||||
requirement.needs_clarification
|
||||
or requirement.intent
|
||||
not in (
|
||||
"create_screen",
|
||||
"modify_screen",
|
||||
)
|
||||
or not scope.needs_planning
|
||||
):
|
||||
raise ValueError("PlannerNode 只处理信息完整且需要规划的新建或修改需求")
|
||||
|
||||
# 获取当前大屏和用户输入,供模型结合已有内容安排布局与组件
|
||||
context = state["input_context"]
|
||||
# 获取可选的组件查询结果
|
||||
resources = state.get("component_resources")
|
||||
|
||||
# 如果已有查询结果但目录未就绪,则停止规划,避免忽略资源查询失败
|
||||
if resources is not None and resources.status != "resolved":
|
||||
raise ValueError("组件目录尚未就绪,不能继续规划")
|
||||
# 图保证组件目录已就绪后才进入规划
|
||||
resources = state["component_resources"]
|
||||
|
||||
llm_client = get_llm(ScreenPlan)
|
||||
result = await llm_client.ainvoke(
|
||||
@@ -54,10 +38,8 @@ class PlannerNode(BaseNode):
|
||||
"input_context": context.model_dump(mode="json"),
|
||||
"requirement": requirement.model_dump(mode="json"),
|
||||
"change_scope": scope.model_dump(mode="json"),
|
||||
# 仅提供规划需要的组件摘要,省略完整默认配置,没有目录时传入 None
|
||||
"component_resources": resources.to_planner_context()
|
||||
if resources is not None
|
||||
else None,
|
||||
# 仅提供规划需要的组件摘要,省略完整默认配置
|
||||
"component_resources": resources.to_planner_context(),
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
@@ -65,26 +47,23 @@ class PlannerNode(BaseNode):
|
||||
]
|
||||
)
|
||||
|
||||
# 将模型结果转成字典后重新校验,确保规划字段及组件关系符合输出协议
|
||||
plan = ScreenPlan.model_validate(result.model_dump())
|
||||
plan = result
|
||||
|
||||
# 如果提供了组件目录,则额外校验规划中的物料类型是否真实受支持
|
||||
if resources is not None:
|
||||
# 提取目录支持的前端组件类型,使用集合进行成员检查
|
||||
supported_types = {component.type for component in resources.components}
|
||||
for component in plan.components:
|
||||
# 如果新增组件没有指定类型,无法选择对应物料,立即报错
|
||||
if component.disposition == "add" and component.component_type is None:
|
||||
raise ValueError(f"新增规划组件未指定物料类型: {component.plan_id}")
|
||||
# 新增或修改组件时,只要明确指定了类型,就必须在目录中存在
|
||||
if (
|
||||
component.disposition in ("add", "modify")
|
||||
and component.component_type is not None
|
||||
):
|
||||
if component.component_type not in supported_types:
|
||||
raise ValueError(
|
||||
f"规划使用了目录中不存在的组件类型: {component.component_type}"
|
||||
)
|
||||
# 校验规划中的物料类型是否真实受支持
|
||||
supported_types = {component.type for component in resources.components}
|
||||
for component in plan.components:
|
||||
# 如果新增组件没有指定类型,无法选择对应物料,立即报错
|
||||
if component.disposition == "add" and component.component_type is None:
|
||||
raise ValueError(f"新增规划组件未指定物料类型: {component.plan_id}")
|
||||
# 新增或修改组件时,只要明确指定了类型,就必须在目录中存在
|
||||
if (
|
||||
component.disposition in ("add", "modify")
|
||||
and component.component_type is not None
|
||||
and component.component_type not in supported_types
|
||||
):
|
||||
raise ValueError(
|
||||
f"规划使用了目录中不存在的组件类型: {component.component_type}"
|
||||
)
|
||||
|
||||
# 收集当前大屏中真实存在的节点 ID,用于检查规划引用的已有目标
|
||||
existing_ids = {node.id for node in context.screen.nodes}
|
||||
|
||||
Reference in New Issue
Block a user