mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 12:22:14 +03:00
Add a 3-tier pricing resolution system: user overrides > synced external > hardcoded defaults. New files: - src/lib/pricingSync.ts: sync engine (fetch LiteLLM, transform, store in pricing_synced namespace) - src/app/api/pricing/sync/route.ts: POST (trigger sync), GET (status), DELETE (clear synced) - tests/unit/pricing-sync.test.mjs: 12 unit tests for transform logic - open-sse/mcp-server/__tests__/pricingSync.test.ts: 11 vitest tests for MCP schema Modified files: - src/lib/db/settings.ts: getPricing() now merges 3 layers (defaults → synced → user) - src/server-init.ts: init pricing sync on startup when PRICING_SYNC_ENABLED=true - src/lib/localDb.ts: re-export pricing sync functions - open-sse/mcp-server/schemas/tools.ts: add omniroute_sync_pricing tool definition - open-sse/mcp-server/tools/advancedTools.ts: add handleSyncPricing handler - open-sse/mcp-server/server.ts: register omniroute_sync_pricing tool Opt-in (PRICING_SYNC_ENABLED=false by default), user overrides are never touched, graceful fallback on fetch failure, zero new dependencies.
68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
// Server startup script
|
|
import initializeCloudSync from "./shared/services/initializeCloudSync";
|
|
import { enforceSecrets } from "./shared/utils/secretsValidator";
|
|
import { initAuditLog, cleanupExpiredLogs, logAuditEvent } from "./lib/compliance/index";
|
|
import { initConsoleInterceptor } from "./lib/consoleInterceptor";
|
|
|
|
async function startServer() {
|
|
// Console interceptor: capture all console output to log file (must be first)
|
|
initConsoleInterceptor();
|
|
|
|
// FASE-01: Validate required secrets before anything else (fail-fast)
|
|
enforceSecrets();
|
|
|
|
// Compliance: Initialize audit_log table
|
|
try {
|
|
initAuditLog();
|
|
console.log("[COMPLIANCE] Audit log table initialized");
|
|
} catch (err) {
|
|
console.warn("[COMPLIANCE] Could not initialize audit log:", err.message);
|
|
}
|
|
|
|
// Compliance: One-time cleanup of expired logs
|
|
try {
|
|
const cleanup = cleanupExpiredLogs();
|
|
if (cleanup.deletedUsage || cleanup.deletedCallLogs || cleanup.deletedAuditLogs) {
|
|
console.log("[COMPLIANCE] Expired log cleanup:", cleanup);
|
|
}
|
|
} catch (err) {
|
|
console.warn("[COMPLIANCE] Log cleanup failed:", err.message);
|
|
}
|
|
|
|
console.log("Starting server with cloud sync...");
|
|
|
|
try {
|
|
// Initialize cloud sync
|
|
await initializeCloudSync();
|
|
console.log("Server started with cloud sync initialized");
|
|
|
|
// Log server start event to audit log
|
|
logAuditEvent({ action: "server.start", details: { timestamp: new Date().toISOString() } });
|
|
} catch (error) {
|
|
console.error("[FATAL] Error initializing cloud sync:", error);
|
|
process.exit(1);
|
|
}
|
|
|
|
// Pricing sync: opt-in external pricing data (non-blocking, never fatal)
|
|
if (process.env.PRICING_SYNC_ENABLED === "true") {
|
|
try {
|
|
const { initPricingSync } = await import("./lib/pricingSync");
|
|
await initPricingSync();
|
|
} catch (err) {
|
|
console.warn(
|
|
"[PRICING_SYNC] Could not initialize:",
|
|
err instanceof Error ? err.message : err
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Start the server initialization
|
|
startServer().catch((err) => {
|
|
console.error("[FATAL] Server initialization failed:", err);
|
|
process.exit(1);
|
|
});
|
|
|
|
// Export for use as module if needed
|
|
export default startServer;
|