astrbot_plugin_imagestudio/backend/services/settings_service.py

676 行
29 KiB
Python

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

"""Settings schema and current configuration service."""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any, Mapping
from uuid import uuid4
from backend.dto.common import ApiError, ErrorCode
from core.config_service import ConfigService
from core.config_writer import config_file_lock, dump_yaml, write_text_atomic
from core.audit_service import AuditService
from core.identity import RequestContext
from core.runtime_paths import is_default_config_path, resolve_plugin_path
from database.db_helper import (
Database,
DatabaseSettings,
get_runtime_config_overrides,
load_config_file_payload,
load_config_payload,
)
SENSITIVE_KEY_PARTS = ("password", "token", "secret", "api_key", "apikey", "key", "dsn")
DATABASE_BACKENDS = {"auto", "sqlite", "postgresql", "postgres", "pg"}
class SettingsService:
def __init__(
self,
config_service: ConfigService | None = None,
schema_path: str | Path = "_conf_schema.json",
audit_service: AuditService | None = None,
):
self.config_service = config_service or ConfigService()
self.schema_path = _resolve_path(schema_path)
self.audit_service = audit_service
def get_schema(self) -> dict[str, Any]:
return {
"schema_version": "3.0",
"raw_schema": self._load_raw_schema(),
"sections": [
{
"key": "plugin",
"title": "插件",
"fields": [
_field("name", "string", label="插件名", readonly=True),
_field("display_name", "string", label="显示名称"),
_field("version", "string", label="版本", readonly=True),
],
},
{
"key": "features",
"title": "功能",
"fields": [
_field("quick_profile", "bool", label="快速建档"),
_field("workspace", "bool", label="工作台"),
_field("material_library", "bool", label="素材库"),
_field("wardrobe", "bool", label="衣橱"),
_field("api_discovery", "bool", label="能力探测"),
_field("private_companion_integration", "bool", label="私伴插件集成"),
],
},
{
"key": "generation",
"title": "生图提供方",
"fields": [
_field("default_model", "string", label="默认路由名"),
_field("provider.provider_id", "string", label="提供方 ID"),
_field("provider.base_url", "string", label="API 根地址"),
_field("provider.image_endpoint", "string", label="生图 endpoint"),
_field("provider.model_id", "string", label="模型 ID"),
_field("provider.route_name", "string", label="路由名"),
_field("provider.api_key_env_var", "string", label="API Key 环境变量"),
_field("provider.timeout_seconds", "number", label="超时秒数"),
],
},
{
"key": "vision",
"title": "素材视觉识别",
"fields": [
_field("default_route", "string", label="默认 Vision 路由名"),
_field("provider.provider_id", "string", label="Vision 提供方 ID(留空复用生图)"),
_field("provider.base_url", "string", label="Vision API 根地址(留空复用生图)"),
_field("provider.vision_endpoint", "string", label="Vision endpoint"),
_field("provider.model_id", "string", label="Vision 模型 ID"),
_field("provider.route_name", "string", label="Vision 路由名"),
_field(
"provider.api_key_env_var",
"string",
label="Vision API Key 环境变量(留空复用生图)",
),
_field("provider.timeout_seconds", "number", label="Vision 超时秒数"),
_field("provider.enabled", "bool", label="启用 Vision 路由"),
],
},
{
"key": "model_assignments",
"title": "模型分工",
"fields": [
_field("material_recognition", "string", label="素材解析路由"),
_field("profile_parse", "string", label="角色导入解析路由"),
_field("persona_split", "string", label="人设拆解路由"),
_field("prompt_planning", "string", label="提示词规划路由"),
_field("cross_review_primary", "string", label="交叉审核主模型路由"),
_field("cross_review_secondary", "string", label="交叉审核复核模型路由"),
_field("cross_review_routes", "list", label="交叉审核模型路由列表"),
_field("quick_generation", "string", label="快速生图路由"),
_field("portrait_generation", "string", label="头像生成路由"),
_field("halfbody_generation", "string", label="半身生成路由"),
_field("fullbody_generation", "string", label="全身生成路由"),
_field("sheet_generation", "string", label="设定图生成路由"),
_field("hires_fix", "string", label="高清修复路由"),
],
},
{
"key": "recognition_prompts",
"title": "识别提示词",
"fields": [
_field("default_instruction", "text", label="默认素材识别指令"),
_field("category_requirements", "list", label="分类附加要求"),
],
},
{
"key": "chat_access",
"title": "聊天权限",
"fields": [
_field("group_allowlist", "list", label="群聊白名单"),
_field("user_allowlist", "list", label="用户白名单"),
_field("user_concurrency_limit", "number", label="用户并发任务上限"),
_field("group_concurrency_limit", "number", label="群聊并发任务上限"),
_field("daily_user_generation_limit", "number", label="用户每日生成上限"),
_field("daily_group_generation_limit", "number", label="群聊每日生成上限"),
_field("allow_2k", "bool", label="允许聊天命令使用 2K"),
_field("allow_4k", "bool", label="允许聊天命令使用 4K"),
],
},
{
"key": "storage",
"title": "存储",
"fields": [
_field("images_path", "path", label="图片目录"),
_field("thumbnails_path", "path", label="缩略图目录"),
_field("materials_path", "path", label="素材目录"),
_field("styles_path", "path", label="风格目录"),
_field("archives_path", "path", label="归档目录"),
_field("temp_path", "path", label="临时目录"),
],
},
{
"key": "database",
"title": "数据库",
"fields": [
_field("backend", "select", label="后端", options=["auto", "sqlite", "postgresql"]),
_field("sqlite.path", "path", label="SQLite 路径"),
_field("postgresql.enabled", "bool", label="启用 PostgreSQL"),
_field("postgresql.dsn", "string", label="PG DSN", sensitive=True),
_field("postgresql.host", "string", label="PG 主机"),
_field("postgresql.port", "number", label="PG 端口"),
_field("postgresql.database", "string", label="PG 数据库"),
_field("postgresql.user", "string", label="PG 用户", sensitive=True),
_field("postgresql.password", "string", label="PG 密码", sensitive=True),
],
},
{
"key": "security",
"title": "安全",
"fields": [
_field("integration_token_set", "bool", label="集成令牌已设置", readonly=True),
_field("allowed_integration_plugins", "string_list", label="允许集成的插件"),
],
},
],
}
def get_current(self) -> dict[str, Any]:
raw = self._load_current_mapping()
return {
"config_path": "***PATH***",
"config": _mask_sensitive(raw),
}
def validate(self, candidate: Mapping[str, Any]) -> dict[str, Any]:
config = candidate.get("config", candidate)
if not isinstance(config, Mapping):
raise ApiError(ErrorCode.VALIDATION_FAILED, "config must be an object", {"field": "config"})
errors: list[dict[str, Any]] = []
warnings: list[dict[str, Any]] = []
generation = config.get("generation", {})
generation_provider: Mapping[str, Any] = {}
if isinstance(generation, Mapping):
provider = generation.get("provider", {})
provider = provider if isinstance(provider, Mapping) else {}
generation_provider = provider
route_name = str(provider.get("route_name") or generation.get("default_model") or "").strip()
model_id = str(provider.get("model_id") or generation.get("default_model") or "").strip()
base_url = str(provider.get("base_url") or "").strip()
if any((route_name, model_id, base_url)):
for field, value in (
("generation.default_model", route_name),
("generation.provider.model_id", model_id),
("generation.provider.base_url", base_url),
):
if not value:
errors.append({"field": field, "message": "configured generation provider field is required"})
vision = config.get("vision", {})
if isinstance(vision, Mapping):
provider = vision.get("provider", {})
provider = provider if isinstance(provider, Mapping) else {}
route_name = str(provider.get("route_name") or vision.get("default_route") or "").strip()
model_id = str(provider.get("model_id") or "").strip()
base_url = str(provider.get("base_url") or generation_provider.get("base_url") or "").strip()
explicitly_configured = any(
str(provider.get(key) or "").strip()
for key in (
"provider_id",
"base_url",
"model_id",
"api_key_env_var",
"api_key_secret_ref",
)
)
if explicitly_configured:
for field, value in (
("vision.default_route", route_name),
("vision.provider.model_id", model_id),
("vision.provider.base_url", base_url),
):
if not value:
errors.append({"field": field, "message": "configured vision provider field is required"})
database = config.get("database", {})
if isinstance(database, Mapping):
backend = str(database.get("backend", "auto")).lower()
if backend not in DATABASE_BACKENDS:
errors.append(
{
"field": "database.backend",
"message": "unsupported database backend",
"allowed": sorted(DATABASE_BACKENDS),
}
)
postgresql = database.get("postgresql", {})
if backend in {"postgresql", "postgres", "pg"} and isinstance(postgresql, Mapping):
if not postgresql.get("dsn") and not (
postgresql.get("host") and postgresql.get("database") and postgresql.get("user")
):
warnings.append(
{
"field": "database.postgresql",
"message": "postgresql backend is selected but connection fields are incomplete",
}
)
return {"valid": not errors, "errors": errors, "warnings": warnings}
def diff(self, candidate: Mapping[str, Any]) -> dict[str, Any]:
current = self._load_current_mapping()
incoming = candidate.get("config", candidate)
if not isinstance(incoming, Mapping):
raise ApiError(ErrorCode.VALIDATION_FAILED, "config must be an object", {"field": "config"})
before = _flatten(current)
after = _flatten(_merge_masked(current, incoming))
keys = sorted(set(before) | set(after))
changes = []
for key in keys:
before_value = before.get(key)
after_value = after.get(key)
if before_value == after_value:
continue
if _is_sensitive_key(key):
changes.append(
{
"path": key,
"before_set": bool(before_value),
"after_set": bool(after_value),
"sensitive": True,
}
)
elif _is_path_key(key):
changes.append(
{
"path": key,
"before": _mask_path_value(before_value),
"after": _mask_path_value(after_value),
"sensitive": False,
}
)
else:
changes.append(
{
"path": key,
"before": before_value,
"after": after_value,
"sensitive": False,
}
)
return {"has_changes": bool(changes), "changes": changes}
def save(self, candidate: Mapping[str, Any], ctx: RequestContext) -> dict[str, Any]:
validation = self.validate(candidate)
if not validation["valid"]:
raise ApiError(ErrorCode.VALIDATION_FAILED, "config validation failed", validation)
incoming = candidate.get("config", candidate)
if not isinstance(incoming, Mapping):
raise ApiError(ErrorCode.VALIDATION_FAILED, "config must be an object", {"field": "config"})
with config_file_lock(self.config_service.config_path):
before = self._load_current_mapping()
before_file = load_config_file_payload(self.config_service.config_path)
merged_effective = _merge_masked(before, incoming)
conflicts = _runtime_override_conflicts(merged_effective, self.config_service.config_path)
if conflicts:
raise ApiError(
ErrorCode.VALIDATION_FAILED,
"these settings are managed by AstrBot; update them in the AstrBot plugin settings",
{"runtime_managed_fields": conflicts},
)
merged = _restore_runtime_managed_fields(
merged_effective,
before_file,
get_runtime_config_overrides() if is_default_config_path(self.config_service.config_path) else {},
)
_initialize_database_if_changed(before_file, merged)
self._record_snapshot(before_file, ctx, "before_save")
write_text_atomic(self.config_service.config_path, dump_yaml(merged))
after = self._load_current_mapping()
if self.audit_service:
self.audit_service.record(
ctx=ctx,
action="save_settings",
target_type="config",
target_id="***PATH***",
before_snapshot=_mask_sensitive(before),
after_snapshot=_mask_sensitive(after),
)
return {"saved": True, "config": _mask_sensitive(after)}
def reload(self) -> dict[str, Any]:
return {"reloaded": True, "config": self.get_current()["config"]}
def export(self) -> dict[str, Any]:
return {
"redacted": True,
"config": self.get_current()["config"],
}
def import_config(self, payload: Mapping[str, Any], ctx: RequestContext) -> dict[str, Any]:
config = payload.get("config", payload)
if not isinstance(config, Mapping):
raise ApiError(ErrorCode.VALIDATION_FAILED, "config must be an object", {"field": "config"})
return self.save({"config": config}, ctx)
def list_snapshots(self, limit: int = 20) -> list[dict[str, Any]]:
if not self.audit_service:
return []
placeholder = self.audit_service.database._placeholder
normalized_limit = _limit(limit, default=20, maximum=100)
conn = self.audit_service.database.connect()
try:
rows = conn.execute(
f"""
SELECT snapshot_id, reason, actor_id, config_json, created_at
FROM config_snapshots
ORDER BY created_at DESC
LIMIT {placeholder}
""",
(normalized_limit,),
).fetchall()
finally:
conn.close()
snapshots = []
for row in rows:
config = json.loads(row["config_json"] or "{}")
snapshots.append(
{
"snapshot_id": row["snapshot_id"],
"reason": row["reason"],
"actor_id": row["actor_id"],
"config": _mask_sensitive(config),
"created_at": row["created_at"],
}
)
return snapshots
def get_snapshot(self, snapshot_id: str) -> dict[str, Any]:
if not self.audit_service:
raise ApiError(ErrorCode.VALIDATION_FAILED, "config snapshots are unavailable", {})
normalized_snapshot_id = str(snapshot_id or "").strip()
if not normalized_snapshot_id:
raise ApiError(ErrorCode.VALIDATION_FAILED, "snapshot_id is required", {"field": "snapshot_id"})
row = self._load_snapshot_row(normalized_snapshot_id)
if row is None:
raise ApiError(
ErrorCode.RESOURCE_NOT_FOUND,
"config snapshot not found",
{"snapshot_id": normalized_snapshot_id},
)
config = json.loads(row["config_json"] or "{}")
return {
"snapshot_id": row["snapshot_id"],
"reason": row["reason"],
"actor_id": row["actor_id"],
"config": _mask_sensitive(config),
"created_at": row["created_at"],
}
def rollback(self, payload: Mapping[str, Any], ctx: RequestContext) -> dict[str, Any]:
if not self.audit_service:
raise ApiError(ErrorCode.VALIDATION_FAILED, "config snapshots are unavailable", {})
snapshot_id = str(payload.get("snapshot_id") or "").strip()
if not snapshot_id:
raise ApiError(ErrorCode.VALIDATION_FAILED, "snapshot_id is required", {"field": "snapshot_id"})
row = self._load_snapshot_row(snapshot_id)
if row is None:
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "config snapshot not found", {"snapshot_id": snapshot_id})
config = json.loads(row["config_json"] or "{}")
validation = self.validate({"config": config})
if not validation["valid"]:
raise ApiError(ErrorCode.VALIDATION_FAILED, "snapshot config validation failed", validation)
with config_file_lock(self.config_service.config_path):
before = self._load_current_mapping()
before_file = load_config_file_payload(self.config_service.config_path)
conflicts = _runtime_override_conflicts(config, self.config_service.config_path)
if conflicts:
raise ApiError(
ErrorCode.VALIDATION_FAILED,
"snapshot changes settings managed by AstrBot",
{"runtime_managed_fields": conflicts},
)
rollback_config = _restore_runtime_managed_fields(
config,
before_file,
get_runtime_config_overrides() if is_default_config_path(self.config_service.config_path) else {},
)
_initialize_database_if_changed(before_file, rollback_config)
self._record_snapshot(before_file, ctx, "before_rollback")
write_text_atomic(self.config_service.config_path, dump_yaml(rollback_config))
after = self._load_current_mapping()
self.audit_service.record(
ctx=ctx,
action="rollback_settings",
target_type="config",
target_id="***PATH***",
before_snapshot=_mask_sensitive(before),
after_snapshot=_mask_sensitive(after),
metadata={"snapshot_id": snapshot_id},
)
return {"rolled_back": True, "snapshot_id": snapshot_id, "config": _mask_sensitive(after)}
def _load_snapshot_row(self, snapshot_id: str):
if not self.audit_service:
return None
placeholder = self.audit_service.database._placeholder
conn = self.audit_service.database.connect()
try:
return conn.execute(
f"""
SELECT snapshot_id, reason, actor_id, config_json, created_at
FROM config_snapshots
WHERE snapshot_id = {placeholder}
""",
(snapshot_id,),
).fetchone()
finally:
conn.close()
def _load_raw_schema(self) -> dict[str, Any]:
if not self.schema_path.exists():
return {}
return json.loads(self.schema_path.read_text(encoding="utf-8"))
def _load_current_mapping(self) -> dict[str, Any]:
return load_config_payload(self.config_service.config_path)
def _record_snapshot(self, config: Mapping[str, Any], ctx: RequestContext, reason: str) -> str | None:
if not self.audit_service:
return None
snapshot_id = f"cfgsnap_{uuid4().hex}"
placeholder = self.audit_service.database._placeholder
conn = self.audit_service.database.connect()
try:
conn.execute(
f"""
INSERT INTO config_snapshots (
snapshot_id, reason, actor_id, config_json
) VALUES (
{placeholder}, {placeholder}, {placeholder}, {placeholder}
)
""",
(
snapshot_id,
reason,
ctx.actor_id,
json.dumps(dict(config), ensure_ascii=False, sort_keys=True),
),
)
conn.commit()
finally:
conn.close()
return snapshot_id
def _field(
key: str,
field_type: str,
*,
label: str = "",
options: list[str] | None = None,
sensitive: bool = False,
readonly: bool = False,
) -> dict[str, Any]:
return {
"key": key,
"label": label or key,
"type": field_type,
"options": options or [],
"sensitive": sensitive,
"readonly": readonly,
}
def _mask_sensitive(value: Any, path: str = "") -> Any:
if isinstance(value, Mapping):
masked: dict[str, Any] = {}
for key, child in value.items():
child_path = f"{path}.{key}" if path else str(key)
if _is_sensitive_key(child_path) and child:
masked[str(key)] = "***MASKED***"
elif _is_path_key(child_path) and _is_absolute_path_value(child):
masked[str(key)] = "***PATH***"
else:
masked[str(key)] = _mask_sensitive(child, child_path)
return masked
if isinstance(value, list):
return [_mask_sensitive(item, path) for item in value]
return value
def _limit(value: Any, *, default: int, maximum: int) -> int:
try:
limit = int(value)
except (TypeError, ValueError):
limit = default
return min(max(limit, 1), maximum)
def _flatten(value: Mapping[str, Any], prefix: str = "") -> dict[str, Any]:
flattened: dict[str, Any] = {}
for key, child in value.items():
path = f"{prefix}.{key}" if prefix else str(key)
if isinstance(child, Mapping):
flattened.update(_flatten(child, path))
else:
flattened[path] = child
return flattened
def _is_sensitive_key(path: str) -> bool:
lowered = path.lower()
if lowered.endswith("_set"):
return False
return any(part in lowered for part in SENSITIVE_KEY_PARTS)
def _is_path_key(path: str) -> bool:
lowered = path.lower()
return lowered.endswith("_path") or lowered.endswith(".path")
def _is_absolute_path_value(value: Any) -> bool:
return isinstance(value, str) and Path(value).is_absolute()
def _mask_path_value(value: Any) -> Any:
return "***PATH***" if _is_absolute_path_value(value) else value
def _resolve_path(path: str | Path) -> Path:
return resolve_plugin_path(path)
def _merge_masked(current: Mapping[str, Any], incoming: Mapping[str, Any]) -> dict[str, Any]:
result: dict[str, Any] = dict(current)
for key, value in incoming.items():
if isinstance(value, str) and value in {"***MASKED***", "***PATH***"}:
continue
if isinstance(value, Mapping) and isinstance(result.get(key), Mapping):
result[str(key)] = _merge_masked(result[key], value)
else:
result[str(key)] = value
return result
def _runtime_override_conflicts(incoming: Mapping[str, Any], config_path: Path) -> list[str]:
if not is_default_config_path(config_path):
return []
overrides = _flatten(get_runtime_config_overrides())
if not overrides:
return []
candidate = _flatten(incoming)
return sorted(path for path, value in overrides.items() if path in candidate and candidate[path] != value)
def _restore_runtime_managed_fields(
candidate: Mapping[str, Any],
file_config: Mapping[str, Any],
runtime_overrides: Mapping[str, Any],
) -> dict[str, Any]:
result = _copy_mapping(candidate)
file_values = _flatten(file_config)
for path in _flatten(runtime_overrides):
if path in file_values:
_set_nested_value(result, path.split("."), file_values[path])
else:
_delete_nested_value(result, path.split("."))
return result
def _copy_mapping(value: Mapping[str, Any]) -> dict[str, Any]:
copied: dict[str, Any] = {}
for key, child in value.items():
if isinstance(child, Mapping):
copied[str(key)] = _copy_mapping(child)
elif isinstance(child, list):
copied[str(key)] = [
_copy_mapping(item) if isinstance(item, Mapping) else item
for item in child
]
else:
copied[str(key)] = child
return copied
def _set_nested_value(target: dict[str, Any], parts: list[str], value: Any) -> None:
cursor = target
for part in parts[:-1]:
child = cursor.get(part)
if not isinstance(child, dict):
child = {}
cursor[part] = child
cursor = child
if parts:
cursor[parts[-1]] = value
def _delete_nested_value(target: dict[str, Any], parts: list[str]) -> None:
if not parts:
return
cursor: dict[str, Any] = target
parents: list[tuple[dict[str, Any], str]] = []
for part in parts[:-1]:
child = cursor.get(part)
if not isinstance(child, dict):
return
parents.append((cursor, part))
cursor = child
cursor.pop(parts[-1], None)
for parent, key in reversed(parents):
child = parent.get(key)
if isinstance(child, dict) and not child:
parent.pop(key, None)
else:
break
def _initialize_database_if_changed(before: Mapping[str, Any], after: Mapping[str, Any]) -> None:
before_database = before.get("database") if isinstance(before.get("database"), Mapping) else {}
after_database = after.get("database") if isinstance(after.get("database"), Mapping) else {}
if dict(before_database) == dict(after_database):
return
Database(DatabaseSettings.from_mapping(after)).initialize()
# Compatibility alias for existing tests and services that imported the
# previous private serializer. New code should import core.config_writer.
_dump_yaml = dump_yaml