fix:处理模型错误添加大量文本的bug
This commit is contained in:
+32
-8
@@ -8,7 +8,12 @@ from copy import deepcopy
|
||||
from app.actions.layout import EXPANDERS
|
||||
from app.actions.registry import apply_action
|
||||
from app.actions.update import fit_existing_path, same_value
|
||||
from app.resources.material_fields import check_update_field, find_material
|
||||
from app.resources.material_fields import (
|
||||
check_add_fields,
|
||||
check_update_field,
|
||||
find_material,
|
||||
retained_examples,
|
||||
)
|
||||
from app.schemas.actions import Action, AddAction, AgentAction, UpdateAction
|
||||
from app.schemas.capabilities import DATA_BINDING_ERROR, DATA_BINDING_SUPPORTED
|
||||
from app.schemas.component_resource import ComponentCatalog
|
||||
@@ -45,19 +50,38 @@ def merge_config(defaults: dict, overrides: dict) -> dict:
|
||||
def complete_add(action: AddAction, catalog: ComponentCatalog) -> AddAction:
|
||||
"""按 node.type 找物料补全新增组件,模型显式写出的字段优先
|
||||
|
||||
sample_data 模型写了就以模型为准(只有它知道是否换成了自己的数据),没写时按物料设置
|
||||
显式写的字段先按字段定义检查,和 update 是同一套规则;sample_data 保持模型的声明,
|
||||
交付时再由 mark_sample_data 按组件上是否还有示例数据补上
|
||||
"""
|
||||
material = find_material(catalog, action.node.type)
|
||||
if not DATA_BINDING_SUPPORTED and action.node.model_extra.get("dataId"):
|
||||
raise ValueError(DATA_BINDING_ERROR)
|
||||
overrides = action.node.model_dump(mode="json", exclude_unset=True)
|
||||
check_add_fields(catalog, material.type, overrides)
|
||||
node = ScreenNode.model_validate(merge_config(material.template(), overrides))
|
||||
sample_data = (
|
||||
action.sample_data
|
||||
if "sample_data" in action.model_fields_set
|
||||
else material.sample_data
|
||||
)
|
||||
return action.model_copy(update={"node": node, "sample_data": sample_data})
|
||||
return action.model_copy(update={"node": node})
|
||||
|
||||
|
||||
def mark_sample_data(
|
||||
actions: list[Action], screen: Screen, catalog: ComponentCatalog | None
|
||||
) -> list[Action]:
|
||||
"""交付前给新增组件定 sample_data:模型声明了自己编的数据,或者组件上还留着物料的示例数据
|
||||
|
||||
按交付后的大屏判断,不管数据是新增时写的、之后用 update 换的,还是 $query 查来的;
|
||||
后来被删掉的组件保持模型的声明。只改动作上的标记,交付记录依次执行仍得到同一个大屏
|
||||
"""
|
||||
nodes = {node.id: node for node in screen.nodes}
|
||||
result = []
|
||||
for action in actions:
|
||||
if isinstance(action, AddAction) and not action.sample_data:
|
||||
node = nodes.get(action.node.id)
|
||||
if node is not None and any(
|
||||
field.example == "data"
|
||||
for field, _ in retained_examples(catalog, node.model_dump(mode="json"))
|
||||
):
|
||||
action = action.model_copy(update={"sample_data": True})
|
||||
result.append(action)
|
||||
return result
|
||||
|
||||
|
||||
def misplaced_props(actions: list[AgentAction], catalog: ComponentCatalog) -> list[str]:
|
||||
|
||||
Reference in New Issue
Block a user