feat: 优化部分代码及提示词
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
"""在大屏 JSON 副本上新增组件,用于后端校验动作能否执行"""
|
||||
|
||||
from app.actions.base import BaseActionHandler
|
||||
from app.schemas.actions import AddAction
|
||||
from app.schemas.screen import Screen, ScreenNode
|
||||
|
||||
|
||||
class AddHandler(BaseActionHandler[AddAction]):
|
||||
def apply(self, screen: Screen, action: AddAction) -> Screen:
|
||||
# 重新校验节点,避免模型创建后被修改成无效内容
|
||||
node = ScreenNode.model_validate(action.node.model_dump())
|
||||
if any(existing.id == node.id for existing in screen.nodes):
|
||||
raise ValueError(f"新增组件 ID 已存在: {node.id}")
|
||||
result = screen.model_copy(deep=True)
|
||||
# 追加到末尾,不共享动作中的嵌套配置
|
||||
result.nodes.append(node.model_copy(deep=True))
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
screen = Screen.model_validate(
|
||||
{"canvas": {"width": 1920, "height": 1080}, "nodes": []}
|
||||
)
|
||||
action = AddAction.model_validate(
|
||||
{
|
||||
"type": "add",
|
||||
"node": {
|
||||
"id": "screen-title",
|
||||
"type": "text",
|
||||
"layout": {"x": 100, "y": 50, "width": 400, "height": 60},
|
||||
"props": {},
|
||||
},
|
||||
}
|
||||
)
|
||||
handler = AddHandler()
|
||||
final_screen = handler.apply(screen, action)
|
||||
print(final_screen.model_dump_json(indent=2))
|
||||
print("原始组件数:", len(screen.nodes))
|
||||
try:
|
||||
handler.apply(final_screen, action)
|
||||
except ValueError as error:
|
||||
print("重复新增:", error)
|
||||
Reference in New Issue
Block a user