mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 04:12:10 +03:00
Settings: - Add `alwaysPreserveClientCache` setting with modes: auto/always/never - UI toggle in Dashboard > Settings > Routing tab - Auto mode preserves cache_control for Claude Code clients with deterministic routing Metrics: - Track prompt cache token usage (input, cached, creation) - Display cache reuse ratio (cached/input tokens) - Breakdown by provider and routing strategy - Shows tokens saved and estimated cost savings API Endpoints: - GET /api/settings/cache-metrics - retrieve metrics - DELETE /api/settings/cache-metrics - reset metrics Files: - open-sse/utils/cacheControlPolicy.ts: CacheControlMetrics interface, trackCacheMetrics, updateCacheTokenMetrics - open-sse/handlers/chatCore.ts: Track cache tokens from provider responses - src/lib/db/settings.ts: Database functions for metrics persistence - src/lib/cacheControlSettings.ts: Cached settings accessor - src/app/(dashboard)/dashboard/settings/components/CacheStatsCard.tsx: Metrics dashboard UI - tests/unit/*.test.mjs: Unit tests (41 tests pass) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
135 lines
5.1 KiB
TypeScript
135 lines
5.1 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { getSettings, updateSettings } from "@/lib/localDb";
|
|
import { clearHealthCheckLogCache } from "@/lib/tokenHealthCheck";
|
|
import bcrypt from "bcryptjs";
|
|
import { timingSafeEqual } from "crypto";
|
|
import { getRuntimePorts } from "@/lib/runtime/ports";
|
|
import { updateSettingsSchema } from "@/shared/validation/settingsSchemas";
|
|
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
|
|
import { setCliCompatProviders } from "../../../../open-sse/config/cliFingerprints";
|
|
import { getConsistentMachineId } from "@/shared/utils/machineId";
|
|
|
|
export async function GET() {
|
|
try {
|
|
const settings = await getSettings();
|
|
const { password, ...safeSettings } = settings;
|
|
|
|
// Sync CLI fingerprint providers to runtime cache on load
|
|
if (settings.cliCompatProviders) {
|
|
setCliCompatProviders(settings.cliCompatProviders as string[]);
|
|
}
|
|
|
|
const enableRequestLogs = process.env.ENABLE_REQUEST_LOGS === "true";
|
|
const runtimePorts = getRuntimePorts();
|
|
const cloudUrl = process.env.CLOUD_URL || process.env.NEXT_PUBLIC_CLOUD_URL || null;
|
|
const machineId = await getConsistentMachineId();
|
|
|
|
return NextResponse.json({
|
|
...safeSettings,
|
|
enableRequestLogs,
|
|
hasPassword: !!password || !!process.env.INITIAL_PASSWORD,
|
|
runtimePorts,
|
|
apiPort: runtimePorts.apiPort,
|
|
dashboardPort: runtimePorts.dashboardPort,
|
|
cloudConfigured: Boolean(cloudUrl),
|
|
cloudUrl,
|
|
machineId,
|
|
});
|
|
} catch (error) {
|
|
console.log("Error getting settings:", error);
|
|
return NextResponse.json({ error: "Failed to load settings" }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function PATCH(request) {
|
|
try {
|
|
const rawBody = await request.json();
|
|
|
|
// Zod validation
|
|
const validation = validateBody(updateSettingsSchema, rawBody);
|
|
if (isValidationFailure(validation)) {
|
|
return NextResponse.json({ error: validation.error }, { status: 400 });
|
|
}
|
|
const body: typeof validation.data & { password?: string } = { ...validation.data };
|
|
|
|
// If updating password, hash it
|
|
if (body.newPassword) {
|
|
const settings = await getSettings();
|
|
const currentHash = typeof settings.password === "string" ? settings.password : "";
|
|
|
|
// Verify current password if it exists
|
|
if (currentHash) {
|
|
if (!body.currentPassword) {
|
|
return NextResponse.json({ error: "Current password required" }, { status: 400 });
|
|
}
|
|
const isValid = await bcrypt.compare(body.currentPassword, currentHash);
|
|
if (!isValid) {
|
|
return NextResponse.json({ error: "Invalid current password" }, { status: 401 });
|
|
}
|
|
} else {
|
|
// First-time password set (no DB hash yet).
|
|
const LEGACY_DEFAULT_PASSWORD = "123456";
|
|
const initialPassword = process.env.INITIAL_PASSWORD;
|
|
const currentPassword = body.currentPassword || "";
|
|
|
|
if (initialPassword) {
|
|
// If deploy is configured with INITIAL_PASSWORD, require explicit match.
|
|
if (!currentPassword) {
|
|
return NextResponse.json({ error: "Current password required" }, { status: 400 });
|
|
}
|
|
|
|
const providedBuffer = Buffer.from(currentPassword, "utf8");
|
|
const expectedBuffer = Buffer.from(initialPassword, "utf8");
|
|
const isValidInitialPassword =
|
|
providedBuffer.length === expectedBuffer.length &&
|
|
timingSafeEqual(providedBuffer, expectedBuffer);
|
|
|
|
if (!isValidInitialPassword) {
|
|
return NextResponse.json({ error: "Invalid current password" }, { status: 401 });
|
|
}
|
|
} else {
|
|
// Legacy compatibility: instances without INITIAL_PASSWORD may still use old default.
|
|
const allowedWithoutHash = ["", LEGACY_DEFAULT_PASSWORD];
|
|
if (!allowedWithoutHash.includes(currentPassword)) {
|
|
return NextResponse.json({ error: "Invalid current password" }, { status: 401 });
|
|
}
|
|
}
|
|
}
|
|
|
|
const salt = await bcrypt.genSalt(10);
|
|
body.password = await bcrypt.hash(body.newPassword, salt);
|
|
delete body.newPassword;
|
|
delete body.currentPassword;
|
|
}
|
|
|
|
const settings = await updateSettings(body);
|
|
|
|
// Clear health check log cache if that setting was updated
|
|
if ("hideHealthCheckLogs" in body) {
|
|
clearHealthCheckLogCache();
|
|
}
|
|
|
|
// Sync CLI fingerprint providers to runtime cache
|
|
if ("cliCompatProviders" in body) {
|
|
setCliCompatProviders(body.cliCompatProviders || []);
|
|
}
|
|
|
|
if ("maxCallLogs" in body) {
|
|
const { invalidateCallLogsMaxCache } = await import("@/lib/usage/callLogs");
|
|
invalidateCallLogsMaxCache();
|
|
}
|
|
|
|
// Sync cache control settings to runtime cache
|
|
if ("alwaysPreserveClientCache" in body) {
|
|
const { invalidateCacheControlSettingsCache } = await import("@/lib/cacheControlSettings");
|
|
invalidateCacheControlSettingsCache();
|
|
}
|
|
|
|
const { password, ...safeSettings } = settings;
|
|
return NextResponse.json(safeSettings);
|
|
} catch (error) {
|
|
console.log("Error updating settings:", error);
|
|
return NextResponse.json({ error: "Failed to update settings" }, { status: 500 });
|
|
}
|
|
}
|