diff --git a/astrbot/core/config/astrbot_config.py b/astrbot/core/config/astrbot_config.py index 9477eabaa..2208ee766 100644 --- a/astrbot/core/config/astrbot_config.py +++ b/astrbot/core/config/astrbot_config.py @@ -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 diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index b45917026..4f128b998 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -3049,4 +3049,5 @@ class ChatProviderTemplate(TypedDict): "text": "", "list": [], "object": {}, + "template_list": [], } diff --git a/astrbot/dashboard/routes/config.py b/astrbot/dashboard/routes/config.py index f39cccfe6..077edf099 100644 --- a/astrbot/dashboard/routes/config.py +++ b/astrbot/dashboard/routes/config.py @@ -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 = [] @@ -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__}", diff --git a/dashboard/src/components/shared/AstrBotConfig.vue b/dashboard/src/components/shared/AstrBotConfig.vue index 045506b63..7cc93b0bb 100644 --- a/dashboard/src/components/shared/AstrBotConfig.vue +++ b/dashboard/src/components/shared/AstrBotConfig.vue @@ -1,11 +1,8 @@ + + diff --git a/dashboard/src/components/shared/TemplateListEditor.vue b/dashboard/src/components/shared/TemplateListEditor.vue new file mode 100644 index 000000000..796dae2dd --- /dev/null +++ b/dashboard/src/components/shared/TemplateListEditor.vue @@ -0,0 +1,450 @@ + + + + + diff --git a/dashboard/src/i18n/locales/en-US/core/common.json b/dashboard/src/i18n/locales/en-US/core/common.json index b25999e28..a08ece91e 100644 --- a/dashboard/src/i18n/locales/en-US/core/common.json +++ b/dashboard/src/i18n/locales/en-US/core/common.json @@ -65,6 +65,12 @@ "fullscreen": "Fullscreen Edit", "editingTitle": "Editing Content" }, + "templateList": { + "addEntry": "Add Entry", + "empty": "No entries yet, pick a template to add", + "missingTemplate": "Template not found, please remove and add again.", + "unknownTemplate": "Template not specified" + }, "list": { "addItemPlaceholder": "Add new item, press Enter to confirm", "addButton": "Add", @@ -84,7 +90,6 @@ "enabled": "Enabled", "disabled": "Disabled", "delete": "Delete", - "copy": "Copy", "edit": "Edit", "copy": "Copy", "noData": "No data available" diff --git a/dashboard/src/i18n/locales/zh-CN/core/common.json b/dashboard/src/i18n/locales/zh-CN/core/common.json index 1adc47f0f..3dd1778ea 100644 --- a/dashboard/src/i18n/locales/zh-CN/core/common.json +++ b/dashboard/src/i18n/locales/zh-CN/core/common.json @@ -65,6 +65,12 @@ "fullscreen": "全屏编辑", "editingTitle": "编辑内容" }, + "templateList": { + "addEntry": "添加条目", + "empty": "暂无条目,请选择模板添加", + "missingTemplate": "找不到对应模板,请删除后重新添加。", + "unknownTemplate": "未指定模板" + }, "list": { "addItemPlaceholder": "添加新项,按回车确认添加", "addButton": "添加",