mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-09-02 01:20:23 +08:00
- Security: test-model no longer resolves a stored secret when the request's baseUrl/provider differs from the stored entry, closing a path where a tampered baseUrl could exfiltrate a saved key - Save failures are now visible: the save bar shows the error in red (was masked by the persistent 'Unsaved changes' text), and per-field validation errors from the settings API are surfaced under each field - The Observability/Quota enable switch is now real: toggling off stages deletion of the group's saved values, and the toggle no longer snaps back to Enabled after saving - Env provider's default star is hidden when a panel provider is the active default (no more double star) - Clearing a credential field reverts to the stored value instead of silently deleting it; an explicit X button removes a stored secret - Form inputs are disabled during an in-flight save
90 lines
2.7 KiB
TypeScript
90 lines
2.7 KiB
TypeScript
import { checkAdminAuth } from "@/lib/admin/auth"
|
|
import {
|
|
AdminProvidersSchema,
|
|
deriveEnvUpdates,
|
|
loadAdminProviders,
|
|
maskAdminProviders,
|
|
mergeSecrets,
|
|
validateAdminProviders,
|
|
} from "@/lib/admin/providers"
|
|
import { isSettingsWritable, saveSettings } from "@/lib/admin/settings"
|
|
import { loadEnvServerModelsConfig } from "@/lib/server-model-config"
|
|
|
|
export const runtime = "nodejs"
|
|
export const dynamic = "force-dynamic"
|
|
|
|
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()
|
|
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,
|
|
)
|
|
return {
|
|
writable: isSettingsWritable(),
|
|
providers: maskAdminProviders(adminProviders),
|
|
envProviders:
|
|
envConfig?.providers.map((p) => ({
|
|
name: p.name,
|
|
provider: p.provider,
|
|
models: p.models,
|
|
isDefault: !!p.default && !adminHasDefault,
|
|
})) ?? [],
|
|
}
|
|
}
|
|
|
|
export async function GET(req: Request) {
|
|
const authError = checkAdminAuth(req)
|
|
if (authError) return authError
|
|
return Response.json(await payload())
|
|
}
|
|
|
|
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)
|
|
|
|
const envConfig = await loadEnvServerModelsConfig()
|
|
const validationError = validateAdminProviders(merged, envConfig)
|
|
if (validationError) {
|
|
return Response.json({ error: validationError }, { status: 400 })
|
|
}
|
|
|
|
saveSettings(deriveEnvUpdates(merged, stored))
|
|
|
|
return Response.json(await payload())
|
|
}
|