2026-09-22 21:44:43 +03:00
|
|
|
import {
|
|
|
|
|
clampChatLevel,
|
|
|
|
|
effectiveSettings,
|
|
|
|
|
getChat,
|
|
|
|
|
parseSettingsBody,
|
|
|
|
|
stores,
|
|
|
|
|
validateChatSettings,
|
|
|
|
|
} from "@/src/server";
|
2026-09-18 09:25:40 +03:00
|
|
|
import type { ChatSettings } from "@/src/core";
|
2026-09-12 06:02:40 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The per-chat model control (model-control wave): replace-whole settings —
|
2026-09-22 21:44:43 +03:00
|
|
|
* the picker sends the complete desired object ({} clears all overrides).
|
|
|
|
|
* Validation is the catalog gate (src/server/model-catalog.ts): unknown
|
|
|
|
|
* provider/model is a 400. Thinking levels never reject (owner,
|
|
|
|
|
* interlude 2026-09-22): a pinned level clamps to the named model's
|
|
|
|
|
* nearest supported and the clamped value is stored. Effective next turn
|
2026-09-12 06:02:40 +03:00
|
|
|
* by construction (agents are stateless per turn).
|
2026-09-18 09:25:40 +03:00
|
|
|
*
|
|
|
|
|
* The body parsing itself is shared with the draft route
|
2026-09-18 09:37:20 +03:00
|
|
|
* (src/server/settings-body.ts, default-settings wave 2026-09-18).
|
2026-09-12 06:02:40 +03:00
|
|
|
*/
|
|
|
|
|
export async function PUT(req: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
|
|
|
const { id } = await params;
|
|
|
|
|
if (!getChat(id)) return Response.json({ error: "Not found" }, { status: 404 });
|
|
|
|
|
|
2026-09-18 09:25:40 +03:00
|
|
|
const body = await req.json().catch(() => null);
|
|
|
|
|
const parsed = parseSettingsBody(body);
|
|
|
|
|
if (!parsed.ok) return Response.json({ error: parsed.error }, { status: 400 });
|
2026-09-12 06:02:40 +03:00
|
|
|
|
2026-09-18 09:25:40 +03:00
|
|
|
const verdict = validateChatSettings(parsed.settings);
|
2026-09-12 06:02:40 +03:00
|
|
|
if (!verdict.ok) {
|
|
|
|
|
return Response.json({ error: verdict.error, supported: verdict.supported }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 21:44:43 +03:00
|
|
|
// Replace-whole persists the validated, level-clamped object — no
|
|
|
|
|
// merges, so a stale client can never resurrect a cleared override.
|
|
|
|
|
// Clamp-on-write (owner, interlude 2026-09-22): a pinned thinking
|
|
|
|
|
// level never rejects — it clamps to the named model's nearest
|
|
|
|
|
// supported (body model, else the record's strong slot).
|
|
|
|
|
const settings = clampChatLevel(parsed.settings, effectiveSettings().models.strong);
|
|
|
|
|
stores.chatStore.saveSettings(id, settings);
|
|
|
|
|
return Response.json({ settings });
|
2026-09-12 06:02:40 +03:00
|
|
|
}
|