mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-09-03 01:50:23 +08:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user