refactor: derive admin registry from PROVIDER_INFO, simplify page state

- Provider options, labels, and base-URL placeholders now come from
  PROVIDER_INFO instead of hand-copied lists (fixes SiliconFlow .com/.cn
  placeholder drift; panel names now match the model-config dialog)
- Replace free-text subgroup strings + SUBGROUP_PROVIDERS reverse map
  with a typed provider field on SettingDef
- Precompute SETTINGS_BY_GROUP and PROVIDER_SUBGROUPS at module level
- Merge justSaved into saveMessage, drop unused mainRef, hoist
  fetchSettings out of the component, dedupe savedText logic
- Serialize from SETTINGS_REGISTRY directly; json validators in a map
  instead of a hardcoded key check
- Make allowPrivateUrls a function so ALLOW_PRIVATE_URLS edits in the
  admin panel apply without restart
This commit is contained in:
dayuan.jiang
2026-06-10 21:39:27 +09:00
parent c09b454105
commit 9feab38a0b
6 changed files with 237 additions and 340 deletions

View File

@@ -6,9 +6,19 @@ import {
loadSettings,
saveSettings,
} from "@/lib/admin/settings"
import { SETTINGS_BY_KEY, type SettingDef } from "@/lib/admin/settings-registry"
import {
SETTINGS_BY_KEY,
SETTINGS_REGISTRY,
type SettingDef,
} from "@/lib/admin/settings-registry"
import { ServerModelsConfigSchema } from "@/lib/server-model-config"
// Zod schemas for json-type settings (kept here, server-side only — the
// registry is imported by the client and must stay free of server deps)
const JSON_VALIDATORS: Record<string, typeof ServerModelsConfigSchema> = {
AI_MODELS_CONFIG: ServerModelsConfigSchema,
}
export const runtime = "nodejs"
export const dynamic = "force-dynamic"
@@ -43,17 +53,13 @@ function maskSecret(value: string): { isSet: true; hint: string } {
function serializeSettings() {
const fileValues = loadSettings()
return [...SETTINGS_BY_KEY.values()].map((def) => {
return SETTINGS_REGISTRY.map((def) => {
const source = getValueSource(def.key)
const raw =
source === "file"
? fileValues[def.key]
: (getEnvFallback(def.key) ?? null)
let value: unknown = raw
if (def.type === "secret" && raw) {
value = maskSecret(raw)
}
const value = def.type === "secret" && raw ? maskSecret(raw) : raw
return { key: def.key, source, value }
})
}
@@ -94,10 +100,11 @@ function validateValue(def: SettingDef, value: string): string | null {
} catch {
return "Invalid JSON"
}
if (def.key === "AI_MODELS_CONFIG") {
const result = ServerModelsConfigSchema.safeParse(parsed)
const schema = JSON_VALIDATORS[def.key]
if (schema) {
const result = schema.safeParse(parsed)
if (!result.success) {
return `Invalid model registry: ${result.error.issues[0]?.message ?? "schema mismatch"}`
return `Invalid value: ${result.error.issues[0]?.message ?? "schema mismatch"}`
}
}
return null