feat(compression): add settings API route for compression configuration

Add GET/PUT /api/settings/compression with authentication, Zod body validation (enabled, defaultMode, autoTriggerTokens, cacheMinutes, preserveSystemPrompt, comboOverrides). Follows existing settings route pattern.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
oyi77
2026-04-27 03:52:50 +07:00
parent f0a750c8a3
commit 0fb535db8e

View File

@@ -0,0 +1,58 @@
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { isAuthenticated } from "@/shared/utils/apiAuth";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
import { getCompressionSettings, updateCompressionSettings } from "@/lib/db/compression";
const compressionModeSchema = z.enum(["off", "lite", "standard", "aggressive", "ultra"]);
const compressionSettingsUpdateSchema = z
.object({
enabled: z.boolean().optional(),
defaultMode: compressionModeSchema.optional(),
autoTriggerTokens: z.number().int().min(0).optional(),
cacheMinutes: z.number().int().min(1).max(60).optional(),
preserveSystemPrompt: z.boolean().optional(),
comboOverrides: z.record(z.string(), compressionModeSchema).optional(),
})
.strict();
export async function GET(request: NextRequest) {
if (!(await isAuthenticated(request))) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
try {
const settings = await getCompressionSettings();
return NextResponse.json(settings);
} catch (error) {
return NextResponse.json({ error: String(error) }, { status: 500 });
}
}
export async function PUT(request: NextRequest) {
if (!(await isAuthenticated(request))) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
try {
let rawBody: unknown;
try {
rawBody = await request.json();
} catch {
return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
}
const validation = validateBody(compressionSettingsUpdateSchema, rawBody);
if (isValidationFailure(validation)) {
return NextResponse.json({ error: validation.error }, { status: 400 });
}
const settings = await updateCompressionSettings(
validation.data as Parameters<typeof updateCompressionSettings>[0]
);
return NextResponse.json(settings);
} catch (error) {
return NextResponse.json({ error: String(error) }, { status: 500 });
}
}