Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions astrbot/core/config/astrbot_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def _parse_schema(schema: dict, conf: dict):
if v["type"] == "object":
conf[k] = {}
_parse_schema(v["items"], conf[k])
elif v["type"] == "template_list":
conf[k] = default
else:
conf[k] = default

Expand Down
1 change: 1 addition & 0 deletions astrbot/core/config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -3049,4 +3049,5 @@ class ChatProviderTemplate(TypedDict):
"text": "",
"list": [],
"object": {},
"template_list": [],
}
45 changes: 45 additions & 0 deletions astrbot/dashboard/routes/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,46 @@ def try_cast(value: Any, type_: str):
return None


def _expect_type(value, expected_type, path_key, errors, expected_name=None):
if not isinstance(value, expected_type):
errors.append(
f"错误的类型 {path_key}: 期望是 {expected_name or expected_type.__name__}, "
f"得到了 {type(value).__name__}"
)
return False
return True


def _validate_template_list(value, meta, path_key, errors, validate_fn):
if not _expect_type(value, list, path_key, errors, "list"):
return

templates = meta.get("templates")
if not isinstance(templates, dict):
templates = {}

for idx, item in enumerate(value):
item_path = f"{path_key}[{idx}]"
if not _expect_type(item, dict, item_path, errors, "dict"):
continue

template_key = item.get("__template_key") or item.get("template")
if not template_key:
errors.append(f"缺少模板选择 {item_path}: 需要 __template_key")
continue

template_meta = templates.get(template_key)
if not template_meta:
errors.append(f"未知模板 {item_path}: {template_key}")
continue

validate_fn(
item,
template_meta.get("items", {}),
path=f"{item_path}.",
)


def validate_config(data, schema: dict, is_core: bool) -> tuple[list[str], dict]:
errors = []

Expand All @@ -61,6 +101,11 @@ def validate(data: dict, metadata: dict = schema, path=""):
if value is None:
data[key] = DEFAULT_VALUE_MAP[meta["type"]]
continue

if meta["type"] == "template_list":
_validate_template_list(value, meta, f"{path}{key}", errors, validate)
continue

if meta["type"] == "list" and not isinstance(value, list):
errors.append(
f"错误的类型 {path}{key}: 期望是 list, 得到了 {type(value).__name__}",
Expand Down
Loading