feat: graphical model management in admin panel
Replace the provider credential fields and raw AI_MODELS_CONFIG JSON
textarea with a Models section mirroring the in-app model settings UI:
provider instance list with logos, credential fields per provider type,
model add/remove with suggestions, per-model connectivity test, and a
default-provider star.
On save the server derives everything the runtime needs into
settings.json: credential env vars (with _2 suffixes for multiple
instances of one provider), AI_MODELS_CONFIG, and AI_PROVIDER/AI_MODEL
for the default. Secrets round-trip as masked markers and are never
sent back to the browser. The general settings registry now only
covers non-provider settings (generation, access, features,
observability, quota).
2026-06-10 23:15:18 +09:00
|
|
|
import { checkAdminAuth } from "@/lib/admin/auth"
|
|
|
|
|
import {
|
|
|
|
|
AdminProvidersSchema,
|
|
|
|
|
deriveEnvUpdates,
|
|
|
|
|
loadAdminProviders,
|
|
|
|
|
maskAdminProviders,
|
|
|
|
|
mergeSecrets,
|
|
|
|
|
validateAdminProviders,
|
|
|
|
|
} from "@/lib/admin/providers"
|
|
|
|
|
import { isSettingsWritable, saveSettings } from "@/lib/admin/settings"
|
2026-06-11 13:44:38 +09:00
|
|
|
import { loadEnvServerModelsConfig } from "@/lib/server-model-config"
|
feat: graphical model management in admin panel
Replace the provider credential fields and raw AI_MODELS_CONFIG JSON
textarea with a Models section mirroring the in-app model settings UI:
provider instance list with logos, credential fields per provider type,
model add/remove with suggestions, per-model connectivity test, and a
default-provider star.
On save the server derives everything the runtime needs into
settings.json: credential env vars (with _2 suffixes for multiple
instances of one provider), AI_MODELS_CONFIG, and AI_PROVIDER/AI_MODEL
for the default. Secrets round-trip as masked markers and are never
sent back to the browser. The general settings registry now only
covers non-provider settings (generation, access, features,
observability, quota).
2026-06-10 23:15:18 +09:00
|
|
|
|
|
|
|
|
export const runtime = "nodejs"
|
|
|
|
|
export const dynamic = "force-dynamic"
|
|
|
|
|
|
2026-06-11 13:44:38 +09:00
|
|
|
async function payload() {
|
|
|
|
|
// Env-based providers (AI_MODELS_CONFIG / ai-models.json) are shown
|
|
|
|
|
// read-only in the panel; their credentials live in the environment
|
|
|
|
|
const envConfig = await loadEnvServerModelsConfig()
|
2026-06-12 08:13:26 +09:00
|
|
|
const adminProviders = loadAdminProviders()
|
|
|
|
|
// A panel default overrides any env default (matches the merge in
|
|
|
|
|
// loadRawServerModelsConfig), so env stars must reflect that
|
|
|
|
|
const adminHasDefault = adminProviders.some(
|
|
|
|
|
(p) => p.isDefault && p.models.length > 0,
|
|
|
|
|
)
|
feat: graphical model management in admin panel
Replace the provider credential fields and raw AI_MODELS_CONFIG JSON
textarea with a Models section mirroring the in-app model settings UI:
provider instance list with logos, credential fields per provider type,
model add/remove with suggestions, per-model connectivity test, and a
default-provider star.
On save the server derives everything the runtime needs into
settings.json: credential env vars (with _2 suffixes for multiple
instances of one provider), AI_MODELS_CONFIG, and AI_PROVIDER/AI_MODEL
for the default. Secrets round-trip as masked markers and are never
sent back to the browser. The general settings registry now only
covers non-provider settings (generation, access, features,
observability, quota).
2026-06-10 23:15:18 +09:00
|
|
|
return {
|
|
|
|
|
writable: isSettingsWritable(),
|
2026-06-12 08:13:26 +09:00
|
|
|
providers: maskAdminProviders(adminProviders),
|
2026-06-11 13:44:38 +09:00
|
|
|
envProviders:
|
|
|
|
|
envConfig?.providers.map((p) => ({
|
|
|
|
|
name: p.name,
|
|
|
|
|
provider: p.provider,
|
|
|
|
|
models: p.models,
|
2026-06-12 08:13:26 +09:00
|
|
|
isDefault: !!p.default && !adminHasDefault,
|
2026-06-11 13:44:38 +09:00
|
|
|
})) ?? [],
|
feat: graphical model management in admin panel
Replace the provider credential fields and raw AI_MODELS_CONFIG JSON
textarea with a Models section mirroring the in-app model settings UI:
provider instance list with logos, credential fields per provider type,
model add/remove with suggestions, per-model connectivity test, and a
default-provider star.
On save the server derives everything the runtime needs into
settings.json: credential env vars (with _2 suffixes for multiple
instances of one provider), AI_MODELS_CONFIG, and AI_PROVIDER/AI_MODEL
for the default. Secrets round-trip as masked markers and are never
sent back to the browser. The general settings registry now only
covers non-provider settings (generation, access, features,
observability, quota).
2026-06-10 23:15:18 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function GET(req: Request) {
|
|
|
|
|
const authError = checkAdminAuth(req)
|
|
|
|
|
if (authError) return authError
|
2026-06-11 13:44:38 +09:00
|
|
|
return Response.json(await payload())
|
feat: graphical model management in admin panel
Replace the provider credential fields and raw AI_MODELS_CONFIG JSON
textarea with a Models section mirroring the in-app model settings UI:
provider instance list with logos, credential fields per provider type,
model add/remove with suggestions, per-model connectivity test, and a
default-provider star.
On save the server derives everything the runtime needs into
settings.json: credential env vars (with _2 suffixes for multiple
instances of one provider), AI_MODELS_CONFIG, and AI_PROVIDER/AI_MODEL
for the default. Secrets round-trip as masked markers and are never
sent back to the browser. The general settings registry now only
covers non-provider settings (generation, access, features,
observability, quota).
2026-06-10 23:15:18 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function PUT(req: Request) {
|
|
|
|
|
const authError = checkAdminAuth(req)
|
|
|
|
|
if (authError) return authError
|
|
|
|
|
|
|
|
|
|
if (!isSettingsWritable()) {
|
|
|
|
|
return Response.json(
|
|
|
|
|
{
|
|
|
|
|
error: "Settings file is not writable on this deployment. Configure via environment variables instead.",
|
|
|
|
|
},
|
|
|
|
|
{ status: 503 },
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let body: unknown
|
|
|
|
|
try {
|
|
|
|
|
body = await req.json()
|
|
|
|
|
} catch {
|
|
|
|
|
return Response.json({ error: "Invalid JSON body" }, { status: 400 })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parsed = AdminProvidersSchema.safeParse(
|
|
|
|
|
(body as { providers?: unknown })?.providers,
|
|
|
|
|
)
|
|
|
|
|
if (!parsed.success) {
|
|
|
|
|
return Response.json(
|
|
|
|
|
{
|
|
|
|
|
error: `Invalid providers: ${parsed.error.issues[0]?.message ?? "schema mismatch"}`,
|
|
|
|
|
},
|
|
|
|
|
{ status: 400 },
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stored = loadAdminProviders()
|
|
|
|
|
const merged = mergeSecrets(parsed.data, stored)
|
|
|
|
|
|
2026-06-11 13:44:38 +09:00
|
|
|
const envConfig = await loadEnvServerModelsConfig()
|
2026-06-11 18:08:02 +09:00
|
|
|
const validationError = validateAdminProviders(merged, envConfig)
|
feat: graphical model management in admin panel
Replace the provider credential fields and raw AI_MODELS_CONFIG JSON
textarea with a Models section mirroring the in-app model settings UI:
provider instance list with logos, credential fields per provider type,
model add/remove with suggestions, per-model connectivity test, and a
default-provider star.
On save the server derives everything the runtime needs into
settings.json: credential env vars (with _2 suffixes for multiple
instances of one provider), AI_MODELS_CONFIG, and AI_PROVIDER/AI_MODEL
for the default. Secrets round-trip as masked markers and are never
sent back to the browser. The general settings registry now only
covers non-provider settings (generation, access, features,
observability, quota).
2026-06-10 23:15:18 +09:00
|
|
|
if (validationError) {
|
|
|
|
|
return Response.json({ error: validationError }, { status: 400 })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveSettings(deriveEnvUpdates(merged, stored))
|
|
|
|
|
|
2026-06-11 13:44:38 +09:00
|
|
|
return Response.json(await payload())
|
feat: graphical model management in admin panel
Replace the provider credential fields and raw AI_MODELS_CONFIG JSON
textarea with a Models section mirroring the in-app model settings UI:
provider instance list with logos, credential fields per provider type,
model add/remove with suggestions, per-model connectivity test, and a
default-provider star.
On save the server derives everything the runtime needs into
settings.json: credential env vars (with _2 suffixes for multiple
instances of one provider), AI_MODELS_CONFIG, and AI_PROVIDER/AI_MODEL
for the default. Secrets round-trip as masked markers and are never
sent back to the browser. The general settings registry now only
covers non-provider settings (generation, access, features,
observability, quota).
2026-06-10 23:15:18 +09:00
|
|
|
}
|