Skip to content

Commit 97e6369

Browse files
committed
refactor(ai-gateway): tighten LLM proxy phase types and guard early auth rejections
1 parent dc04362 commit 97e6369

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

‎packages/web-shared/src/lib/ai-gateway/handlers/llm-proxy-model-resolution.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ import type {
6868
ValidatedGatewayRequest,
6969
} from '@kilocode/web-shared/lib/ai-gateway/handlers/llm-proxy-request';
7070

71-
export type AutoRoutingFailure =
71+
type AutoRoutingFailure =
7272
| { kind: 'invalid_token' }
7373
| { kind: 'auth_required' }
7474
| { kind: 'decision_secret_missing' }
@@ -78,7 +78,7 @@ export type AutoRoutingFailure =
7878
| { kind: 'custom_router'; code: CustomRouterErrorCode; message: string }
7979
| { kind: 'unavailable' };
8080

81-
export type ModelResolution =
81+
type ModelResolution =
8282
| { kind: 'direct' }
8383
| {
8484
kind: 'routed';
@@ -408,7 +408,7 @@ export function billLegacyClassifierCost(params: {
408408
headers: LlmProxyRequestHeaders;
409409
}): void {
410410
const { resolution, user, usage, headers } = params;
411-
if (resolution.kind === 'direct' || resolution.classifierCostUsd <= 0) return;
411+
if (resolution.kind === 'direct' || !(resolution.classifierCostUsd > 0)) return;
412412
const costMicrodollars = toMicrodollars(resolution.classifierCostUsd);
413413
const priorMicrodollarUsage = user.microdollars_used;
414414
after(

‎packages/web-shared/src/lib/ai-gateway/handlers/llm-proxy-respond.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Span } from '@sentry/nextjs';
33
import type { User } from '@kilocode/db';
44
import { applyUpstreamErrorPolicy } from '@kilocode/web-shared/lib/ai-gateway/upstream-error-policy';
55
import { recordBriefingUsage } from '@kilocode/web-shared/lib/agent-briefing-usage';
6+
import type { sendUpstreamAttempt } from '@kilocode/web-shared/lib/ai-gateway/providers/upstream-attempt';
67
import type { GetProviderProviderResult } from '@kilocode/web-shared/lib/ai-gateway/providers/get-provider';
78
import {
89
accountForMicrodollarUsage,
@@ -21,7 +22,7 @@ import { errorExceptInTest, logExceptInTest } from '@kilocode/web-shared/lib/uti
2122
import type { GatewayRequest } from '@kilocode/web-shared/lib/ai-gateway/providers/openrouter/types';
2223

2324
type RespondInput = {
24-
upstream: { response: Response; toolsAvailable: string[]; toolsUsed: string[] };
25+
upstream: Extract<Awaited<ReturnType<typeof sendUpstreamAttempt>>, { type: 'success' }>;
2526
/** The request as sent upstream; provider transforms may have removed its model. */
2627
request: GatewayRequest;
2728
/** The resolved model id, lowercased. */

‎packages/web-shared/src/lib/ai-gateway/handlers/llm-proxy.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ export async function handleLlmProxyRequest(
184184
adminOnly: false,
185185
expectedAudience: KILO_GATEWAY_AUDIENCE,
186186
});
187+
// Invalid requests return before anything awaits auth. Keep those paths from
188+
// surfacing auth failures as unhandled rejections.
189+
void authPromise.catch(() => {});
187190
debugSaveProxyRequest(requestBodyText);
188191
const parsed = parseLlmProxyRequest(path, requestBodyText, rawIpAddress);
189192
if (!parsed.ok) return parsed.response;

0 commit comments

Comments
 (0)