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
11 changes: 0 additions & 11 deletions apps/web/src/app/api/openrouter/[...path]/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
getOpenRouterModelsFromDatabase,
isValidOpenRouterModelId,
} from '@/lib/ai-gateway/providers/gateway-models-cache';
import { emitApiMetricsForResponse } from '@/lib/ai-gateway/o11y/api-metrics.server';
import { accountForMicrodollarUsage, INVALID_TOKEN_CODE } from '@/lib/ai-gateway/llm-proxy-helpers';
import { ReasoningDetailsTransform, type Provider } from '@/lib/ai-gateway/providers/types';
import { fetchEfficientAutoDecision } from '@/lib/ai-gateway/auto-routing-decision';
Expand Down Expand Up @@ -82,11 +81,6 @@ jest.mock('@/lib/ai-gateway/providers/direct-byok', () => ({
}));
jest.mock('@/lib/ai-gateway/providers/upstream-request');
jest.mock('@/lib/ai-gateway/providers/gateway-models-cache');
jest.mock('@/lib/ai-gateway/o11y/api-metrics.server', () => ({
emitApiMetricsForResponse: jest.fn(),
getToolsAvailable: jest.fn(() => false),
getToolsUsed: jest.fn(() => false),
}));
jest.mock('@/lib/ai-gateway/rewriteModelResponse', () => {
const actual = jest.requireActual('@/lib/ai-gateway/rewriteModelResponse');
const { wrapInSafeNextResponse } = jest.requireActual('@/lib/ai-gateway/llm-proxy-helpers');
Expand Down Expand Up @@ -133,7 +127,6 @@ const mockedGetProvider = jest.mocked(getProvider);
const mockedUpstreamRequest = jest.mocked(upstreamRequest);
const mockedGetOpenRouterModels = jest.mocked(getOpenRouterModelsFromDatabase);
const mockedIsValidOpenRouterModelId = jest.mocked(isValidOpenRouterModelId);
const mockedEmitApiMetricsForResponse = jest.mocked(emitApiMetricsForResponse);
const mockedAccountForMicrodollarUsage = jest.mocked(accountForMicrodollarUsage);
const mockedFetchEfficientAutoDecision = jest.mocked(fetchEfficientAutoDecision);
const mockedCollectDeniedAutoRoutingModelIds = jest.mocked(collectDeniedAutoRoutingModelIds);
Expand Down Expand Up @@ -267,7 +260,6 @@ describe('POST /api/openrouter/v1/chat/completions bearer audiences', () => {
type: 'success',
response: upstreamJsonResponse({ id: 'chatcmpl-1', model: 'openai/gpt-4o', choices: [] }),
});
mockedEmitApiMetricsForResponse.mockReturnValue(undefined);
mockedAccountForMicrodollarUsage.mockReturnValue(undefined);
});

Expand Down Expand Up @@ -568,7 +560,6 @@ describe('POST /api/openrouter/v1/chat/completions request handling', () => {
type: 'success',
response: upstreamJsonResponse({ id: 'chatcmpl-1', model: 'openai/gpt-4o', choices: [] }),
});
mockedEmitApiMetricsForResponse.mockReturnValue(undefined);
mockedAccountForMicrodollarUsage.mockReturnValue(undefined);
});

Expand Down Expand Up @@ -916,7 +907,6 @@ describe('kilo-auto/efficient classifier billing', () => {
choices: [],
}),
});
mockedEmitApiMetricsForResponse.mockReturnValue(undefined);
mockedAccountForMicrodollarUsage.mockReturnValue(undefined);
mockedLogMicrodollarUsage.mockResolvedValue(null);
mockedGetEffectiveModelDecision.mockResolvedValue({ allowed: true });
Expand Down Expand Up @@ -1385,7 +1375,6 @@ describe('auto-routing shadow classifier', () => {
type: 'success',
response: upstreamJsonResponse({ id: 'chatcmpl-1', model: 'openai/gpt-4o', choices: [] }),
});
mockedEmitApiMetricsForResponse.mockReturnValue(undefined);
mockedAccountForMicrodollarUsage.mockReturnValue(undefined);
mockedApplyResolvedAutoModel.mockImplementation(async (opts, request) => {
if (opts.efficientDecision) await opts.efficientDecision();
Expand Down
22 changes: 1 addition & 21 deletions apps/web/src/app/api/openrouter/[...path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ import {
checkPromotionLimit,
} from '@/lib/free-model-rate-limiter';
import { PROMOTION_MAX_REQUESTS, PROMOTION_WINDOW_HOURS } from '@/lib/constants';
import { emitApiMetricsForResponse } from '@/lib/ai-gateway/o11y/api-metrics.server';
import {
gatewayRateLimitKey,
isGatewayAccountRateLimited,
Expand Down Expand Up @@ -932,7 +931,7 @@ async function openRouterPost(request: NextRequest): Promise<NextResponseType<un
}
if (attempt.type === 'error') return attempt.response;

const { response, toolsAvailable, toolsUsed, experimentPromptCapture } = attempt;
const { response, experimentPromptCapture } = attempt;
if (experimentPromptCapture) usageContext.experimentPromptCapture = experimentPromptCapture;
const finalUpstreamModel = requestBodyParsed.body.model ?? effectiveModelIdLowerCased;
logExceptInTest(
Expand All @@ -945,25 +944,6 @@ async function openRouterPost(request: NextRequest): Promise<NextResponseType<un
const ttfbMs = Math.max(0, Math.round(performance.now() - requestStartedAt));
usageContext.ttfb_ms = ttfbMs;

emitApiMetricsForResponse(
{
kiloUserId: user.id,
organizationId,
isAnonymous: isAnonymousContext(user),
isStreaming: requestBodyParsed.body.stream === true,
userByok: !!effectiveProviderContext.userByok,
mode: modeHeader || undefined,
provider: effectiveProviderContext.provider.id,
requestedModel: requestedModelLowerCased,
resolvedModel: normalizeModelId(effectiveModelIdLowerCased),
toolsAvailable,
toolsUsed,
ttfbMs,
statusCode: response.status,
},
response.clone(),
requestStartedAt
);
usageContext.status_code = response.status;

// Handle OpenRouter 402 errors - don't pass them through to the client. We need to pay, not them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { getBalanceAndOrgSettings } from '@/lib/organizations/organization-usage
import { isFreeModel } from '@/lib/ai-gateway/is-free-model';
import { isAutoTopUpInFlight } from '@/lib/autoTopUpInFlight';
import type { User } from '@kilocode/db/schema';
import { emitApiMetricsForResponse } from '@/lib/ai-gateway/o11y/api-metrics.server';
import type { OrganizationSettings } from '@/lib/organizations/organization-types';

jest.mock('next/server', () => {
Expand All @@ -17,9 +16,6 @@ jest.mock('next/server', () => {
jest.mock('@/lib/user/server');
jest.mock('@/lib/organizations/organization-usage');
jest.mock('@/lib/autoTopUpInFlight');
jest.mock('@/lib/ai-gateway/o11y/api-metrics.server', () => ({
emitApiMetricsForResponse: jest.fn(),
}));
jest.mock('@/lib/ai-gateway/is-free-model', () => ({
isFreeModel: jest.fn(),
}));
Expand All @@ -35,7 +31,6 @@ const mockedGetUserFromAuth = jest.mocked(getUserFromAuth);
const mockedGetBalanceAndOrgSettings = jest.mocked(getBalanceAndOrgSettings);
const mockedIsFreeModel = jest.mocked(isFreeModel);
const mockedIsAutoTopUpInFlight = jest.mocked(isAutoTopUpInFlight);
const mockedEmitApiMetricsForResponse = jest.mocked(emitApiMetricsForResponse);
const mockedFetch = jest.fn() as jest.MockedFunction<typeof globalThis.fetch>;
const originalFetch = globalThis.fetch;

Expand Down Expand Up @@ -152,9 +147,6 @@ describe('POST /api/gateway/v1/audio/transcriptions', () => {
expect(upstream.input_audio).toEqual({ data: 'UklGRiQA', format: 'wav' });
expect(upstream.safety_identifier).toBeTruthy();
expect(upstream.user).toBe(upstream.safety_identifier);
expect(mockedEmitApiMetricsForResponse.mock.calls[0]?.[0]).not.toMatchObject({
feature: 'vscode-extension',
});
});

it('forwards organization provider policy through the OpenRouter provider field', async () => {
Expand Down
21 changes: 0 additions & 21 deletions apps/web/src/app/api/openrouter/audio/transcriptions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import type { OpenRouterProviderConfig } from '@/lib/ai-gateway/providers/openro
import { ProxyErrorType } from '@/lib/proxy-error-types';
import { getBalanceAndOrgSettings } from '@/lib/organizations/organization-usage';
import { isFreeModel } from '@/lib/ai-gateway/is-free-model';
import { emitApiMetricsForResponse } from '@/lib/ai-gateway/o11y/api-metrics.server';
import { normalizeModelId } from '@/lib/ai-gateway/model-utils';
import {
buildUpstreamBody,
extractTranscriptionPromptInfo,
Expand Down Expand Up @@ -343,25 +341,6 @@ export async function POST(request: NextRequest): Promise<NextResponseType<unkno
usageContext.ttfb_ms = ttfbMs;
usageContext.status_code = response.status;

emitApiMetricsForResponse(
{
kiloUserId: user.id,
organizationId,
isAnonymous: false,
isStreaming: false,
userByok: !!userByok,
provider: provider.id,
requestedModel: requestedModelLowerCased,
resolvedModel: normalizeModelId(requestedModelLowerCased),
toolsAvailable: [],
toolsUsed: [],
ttfbMs,
statusCode: response.status,
},
response.clone(),
requestStartedAt
);

if (response.status === 402 && !userByok) {
await captureProxyError({
user,
Expand Down
21 changes: 0 additions & 21 deletions apps/web/src/app/api/openrouter/embeddings/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import {
isAnonymousContext,
type AnonymousUserContext,
} from '@/lib/anonymous';
import { emitApiMetricsForResponse } from '@/lib/ai-gateway/o11y/api-metrics.server';
import { normalizeModelId } from '@/lib/ai-gateway/model-utils';
import {
buildUpstreamBody,
type EmbeddingProxyRequest,
Expand Down Expand Up @@ -313,25 +311,6 @@ export async function POST(request: NextRequest): Promise<NextResponseType<unkno
const ttfbMs = Math.max(0, Math.round(performance.now() - requestStartedAt));
usageContext.ttfb_ms = ttfbMs;

emitApiMetricsForResponse(
{
kiloUserId: user.id,
organizationId,
isAnonymous: isAnonymousContext(user),
isStreaming: false,
userByok: !!userByok,
provider: provider.id,
requestedModel: requestedModelLowerCased,
resolvedModel: normalizeModelId(requestedModelLowerCased),
toolsAvailable: [],
toolsUsed: [],
ttfbMs,
statusCode: response.status,
},
response.clone(),
requestStartedAt
);

usageContext.status_code = response.status;

// Handle upstream 402 — don't pass through to client (same as chat completions)
Expand Down
185 changes: 0 additions & 185 deletions apps/web/src/lib/ai-gateway/o11y/api-metrics.server.test.ts

This file was deleted.

Loading