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: 7 additions & 4 deletions src/app/api/openrouter/[...path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,19 @@ export async function POST(request: NextRequest): Promise<NextResponseType<unkno

console.debug(`Routing request to ${provider.id}`);

const isLegacyOpenRouterPath = url.pathname.includes('/openrouter');
const feature = validateFeatureHeader(
request.headers.get(FEATURE_HEADER) || (isLegacyOpenRouterPath ? '' : 'direct-gateway')
);

// Start abuse classification early (non-blocking) - we'll await it before creating usage context
const classifyPromise = classifyAbuse(request, requestBodyParsed, {
kiloUserId: user.id,
organizationId,
projectId,
provider: provider.id,
isByok: !!userByok,
feature,
});

// large responses may run longer than the 800s serverless function timeout, usually this value is set to 8192 tokens
Expand All @@ -264,7 +270,6 @@ export async function POST(request: NextRequest): Promise<NextResponseType<unkno
// Extract properties for usage context
const tokenEstimates = estimateChatTokens_ignoringToolDefinitions(requestBodyParsed);
const promptInfo = extractPromptInfo(requestBodyParsed);
const isLegacyOpenRouterPath = url.pathname.includes('/openrouter');

const usageContext: MicrodollarUsageContext = {
kiloUserId: user.id,
Expand All @@ -288,9 +293,7 @@ export async function POST(request: NextRequest): Promise<NextResponseType<unkno
has_tools: (requestBodyParsed.tools?.length ?? 0) > 0,
botId,
tokenSource,
feature: validateFeatureHeader(
request.headers.get(FEATURE_HEADER) || (isLegacyOpenRouterPath ? '' : 'direct-gateway')
),
feature,
session_id: taskId ?? null,
mode: modeHeader,
auto_model: autoModel,
Expand Down
4 changes: 4 additions & 0 deletions src/lib/abuse-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { getFraudDetectionHeaders } from '@/lib/utils';
import type { OpenRouterChatCompletionRequest } from '@/lib/providers/openrouter/types';
import type { AuthProviderId } from '@/lib/auth/provider-metadata';
import type { FeatureValue } from '@/lib/feature-detection';
import 'server-only';

/**
Expand Down Expand Up @@ -183,6 +184,7 @@ export type UsagePayload = {
is_byok?: boolean | null;
is_user_byok?: boolean | null;
editor_name?: string | null;
feature?: string | null;

// Existing classification (if any)
abuse_classification?: number | null;
Expand Down Expand Up @@ -332,6 +334,7 @@ export type AbuseClassificationContext = {
projectId?: string | null;
provider?: string | null;
isByok?: boolean | null;
feature?: FeatureValue | null;
};

/**
Expand Down Expand Up @@ -372,6 +375,7 @@ export async function classifyAbuse(
streamed: body.stream === true,
is_user_byok: context?.isByok ?? null,
editor_name: request.headers.get('x-kilocode-editorname') ?? null,
feature: context?.feature ?? null,
};

return classifyRequest(payload);
Expand Down