Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'server-only';

import { captureException } from '@sentry/nextjs';
import { ai_gateway_sync_providers_state } from '@kilocode/db/schema';
import { APP_URL } from '@/lib/constants';
import { db } from '@/lib/drizzle';
import { redisClient } from '@/lib/redis';
import {
SYNC_PROVIDERS_LAST_COMPLETED_AT_REDIS_KEY,
Expand Down Expand Up @@ -173,9 +175,17 @@ async function defaultGetLastAlertAt(): Promise<string | null> {
}

async function defaultSetLastAlertAt(iso: string): Promise<unknown> {
return redisClient.set(SYNC_PROVIDERS_STALE_ALERT_LAST_POSTED_AT_REDIS_KEY, iso, {
const result = await redisClient.set(SYNC_PROVIDERS_STALE_ALERT_LAST_POSTED_AT_REDIS_KEY, iso, {
ex: SYNC_PROVIDERS_STALE_ALERT_TTL_SECONDS,
});
await db
.insert(ai_gateway_sync_providers_state)
.values({ stale_alert_last_posted_at: iso })
.onConflictDoUpdate({
target: ai_gateway_sync_providers_state.id,
set: { stale_alert_last_posted_at: iso },
});
return result;
}

export async function postStaleSyncAlert(input: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
} from '@/lib/ai-gateway/providers/openrouter/openrouter-types';
import { OpenRouterProvidersResponse } from '@/lib/ai-gateway/providers/openrouter/openrouter-types';
import { fetchModelsForProvider } from '@/lib/ai-gateway/providers/openrouter/fetch-provider-models';
import { modelsByProvider } from '@kilocode/db/schema';
import { ai_gateway_sync_providers_state, modelsByProvider } from '@kilocode/db/schema';
import { db } from '@/lib/drizzle';
import { desc, lt, sql } from 'drizzle-orm';
import { captureException } from '@sentry/nextjs';
Expand Down Expand Up @@ -396,6 +396,13 @@ export async function syncAndStoreProviders() {

const completed_at = new Date().toISOString();
await redisClient.set(SYNC_PROVIDERS_LAST_COMPLETED_AT_REDIS_KEY, completed_at);
await db
.insert(ai_gateway_sync_providers_state)
.values({ last_completed_at: completed_at })
.onConflictDoUpdate({
target: ai_gateway_sync_providers_state.id,
set: { last_completed_at: completed_at },
});

return {
id: result.id,
Expand Down
22 changes: 20 additions & 2 deletions apps/web/src/lib/ai-gateway/request-logging-opt-ins.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as z from 'zod';
import { ai_gateway_request_logging_opt_ins } from '@kilocode/db/schema';
import { createCachedFetch } from '@/lib/cached-fetch';
import { db } from '@/lib/drizzle';
import { redisClient } from '@/lib/redis';
import { REQUEST_LOGGING_OPT_INS_REDIS_KEY } from '@/lib/redis-keys';

Expand Down Expand Up @@ -80,6 +82,17 @@ const getCachedRequestLoggingOptIns = createCachedFetch<RequestLoggingOptIn[]>(
[]
);

async function mirrorRequestLoggingOptInsToDatabase(): Promise<void> {
const optIns = await getRequestLoggingOptIns();
await db
.insert(ai_gateway_request_logging_opt_ins)
.values({ opt_ins: optIns })
.onConflictDoUpdate({
target: ai_gateway_request_logging_opt_ins.id,
set: { opt_ins: optIns },
});
}

export async function createRequestLoggingOptIn(
entry: RequestLoggingOptIn
): Promise<'created' | 'duplicate' | 'full'> {
Expand All @@ -89,7 +102,10 @@ export async function createRequestLoggingOptIn(
[REQUEST_LOGGING_OPT_INS_REDIS_KEY],
[JSON.stringify(validated)]
);
if (result === 1) return 'created';
if (result === 1) {
await mirrorRequestLoggingOptInsToDatabase();
return 'created';
}
if (result === 0) return 'duplicate';
return 'full';
}
Expand All @@ -100,7 +116,9 @@ export async function deleteRequestLoggingOptIn(id: string): Promise<boolean> {
[REQUEST_LOGGING_OPT_INS_REDIS_KEY],
[id]
);
return result === 1;
if (result !== 1) return false;
await mirrorRequestLoggingOptInsToDatabase();
return true;
}

export async function isDynamicallyOptedIntoRequestLogging(params: {
Expand Down
6 changes: 6 additions & 0 deletions apps/web/src/routers/admin/gateway-config-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
import { VERCEL_ROUTING_REDIS_KEY } from '@/lib/redis-keys';
import type { GatewayConfig } from '@/lib/ai-gateway/gateway-config';
import { TRPCError } from '@trpc/server';
import { ai_gateway_config } from '@kilocode/db/schema';
import { db } from '@/lib/drizzle';

async function readConfig(): Promise<GatewayConfig> {
try {
Expand Down Expand Up @@ -47,6 +49,10 @@ export const adminGatewayConfigRouter = createTRPCRouter({
message: 'Redis is not configured — cannot save routing override',
});
}
await db.insert(ai_gateway_config).values({ config }).onConflictDoUpdate({
target: ai_gateway_config.id,
set: { config },
});
return config;
}),
});
18 changes: 18 additions & 0 deletions packages/db/src/migrations/0238_dizzy_namora.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TABLE "ai_gateway_config" (
"id" integer PRIMARY KEY DEFAULT 1 NOT NULL,
"config" jsonb DEFAULT '{}'::jsonb NOT NULL,
CONSTRAINT "ai_gateway_config_singleton" CHECK ("ai_gateway_config"."id" = 1)
);
--> statement-breakpoint
CREATE TABLE "ai_gateway_request_logging_opt_ins" (
"id" integer PRIMARY KEY DEFAULT 1 NOT NULL,
"opt_ins" jsonb DEFAULT '[]'::jsonb NOT NULL,
CONSTRAINT "ai_gateway_request_logging_opt_ins_singleton" CHECK ("ai_gateway_request_logging_opt_ins"."id" = 1)
);
--> statement-breakpoint
CREATE TABLE "ai_gateway_sync_providers_state" (
"id" integer PRIMARY KEY DEFAULT 1 NOT NULL,
"last_completed_at" timestamp with time zone,
"stale_alert_last_posted_at" timestamp with time zone,
CONSTRAINT "ai_gateway_sync_providers_state_singleton" CHECK ("ai_gateway_sync_providers_state"."id" = 1)
);
Loading