Skip to content
Open
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
101 changes: 101 additions & 0 deletions astrbot/core/config/config_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""Configuration type constants for AstrBot.

This module defines shared constants for configuration item types,
ensuring consistency between validation, default values, and UI rendering.
"""

from __future__ import annotations

from enum import Enum


class ConfigType(str, Enum):
"""Enumeration of all supported configuration item types."""

INT = "int"
FLOAT = "float"
BOOL = "bool"
STRING = "string"
TEXT = "text"
LIST = "list"
OBJECT = "object"
TEMPLATE_LIST = "template_list"
PALETTE = "palette"
PALETTE_RGB = "palette_rgb"
PALETTE_HSV = "palette_hsv"


# Types that expect string values
STRING_LIKE_TYPES: frozenset[str] = frozenset(
{
ConfigType.STRING,
ConfigType.TEXT,
ConfigType.PALETTE,
ConfigType.PALETTE_RGB,
ConfigType.PALETTE_HSV,
}
)

# Palette types specifically
PALETTE_TYPES: frozenset[str] = frozenset(
{
ConfigType.PALETTE,
ConfigType.PALETTE_RGB,
ConfigType.PALETTE_HSV,
}
)

# Default values for each config type
# For palette types, empty string is valid (means no color selected)
DEFAULT_VALUE_MAP: dict[str, int | float | bool | str | list | dict] = {
ConfigType.INT: 0,
ConfigType.FLOAT: 0.0,
ConfigType.BOOL: False,
ConfigType.STRING: "",
ConfigType.TEXT: "",
ConfigType.LIST: [],
ConfigType.OBJECT: {},
ConfigType.TEMPLATE_LIST: [],
ConfigType.PALETTE: "",
ConfigType.PALETTE_RGB: "",
ConfigType.PALETTE_HSV: "",
}


def get_default_value(config_type: str) -> int | float | bool | str | list | dict:
"""Get the default value for a configuration type.

Args:
config_type: The configuration type string

Returns:
The default value for the given type, or empty string if unknown

"""
return DEFAULT_VALUE_MAP.get(config_type, "")


def is_string_like_type(config_type: str) -> bool:
"""Check if a configuration type expects string values.

Args:
config_type: The configuration type string

Returns:
True if the type expects string values

"""
return config_type in STRING_LIKE_TYPES


def is_palette_type(config_type: str) -> bool:
"""Check if a configuration type is a palette type.

Args:
config_type: The configuration type string

Returns:
True if the type is a palette type

"""
return config_type in PALETTE_TYPES
12 changes: 2 additions & 10 deletions astrbot/core/config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -3239,13 +3239,5 @@ class ChatProviderTemplate(TypedDict):
}


DEFAULT_VALUE_MAP = {
"int": 0,
"float": 0.0,
"bool": False,
"string": "",
"text": "",
"list": [],
"object": {},
"template_list": [],
}
# Re-export DEFAULT_VALUE_MAP from config_types for backward compatibility
from astrbot.core.config.config_types import DEFAULT_VALUE_MAP
3 changes: 2 additions & 1 deletion astrbot/dashboard/routes/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from astrbot.core import astrbot_config, file_token_service, logger
from astrbot.core.config.astrbot_config import AstrBotConfig
from astrbot.core.config.config_types import STRING_LIKE_TYPES
from astrbot.core.config.default import (
CONFIG_METADATA_2,
CONFIG_METADATA_3,
Expand Down Expand Up @@ -141,7 +142,7 @@ def validate(data: dict, metadata: dict = schema, path=""):
errors.append(
f"错误的类型 {path}{key}: 期望是 bool, 得到了 {type(value).__name__}",
)
elif meta["type"] in ["string", "text"] and not isinstance(value, str):
elif meta["type"] in STRING_LIKE_TYPES and not isinstance(value, str):
errors.append(
f"错误的类型 {path}{key}: 期望是 string, 得到了 {type(value).__name__}",
)
Expand Down
10 changes: 10 additions & 0 deletions dashboard/src/components/shared/ConfigItemRenderer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@
class="config-field"
/>

<PaletteEditor
v-else-if="isPaletteType(itemMeta?.type)"
:model-value="modelValue"
@update:model-value="emitUpdate"
:format="getPaletteFormatFromType(itemMeta?.type)"
class="config-field"
/>

<ObjectEditor
v-else-if="itemMeta?.type === 'dict'"
:model-value="modelValue"
Expand Down Expand Up @@ -214,6 +222,8 @@ import PersonaSelector from './PersonaSelector.vue'
import KnowledgeBaseSelector from './KnowledgeBaseSelector.vue'
import PluginSetSelector from './PluginSetSelector.vue'
import T2ITemplateEditor from './T2ITemplateEditor.vue'
import PaletteEditor from './PaletteEditor.vue'
import { isPaletteType, getPaletteFormatFromType } from '@/utils/color'
import { useI18n, useModuleI18n } from '@/i18n/composables'

const props = defineProps({
Expand Down
Loading