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
18 changes: 17 additions & 1 deletion apps/web/src/app/api/openrouter/[...path]/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { fetchEfficientAutoDecision } from '@/lib/ai-gateway/auto-routing-decisi
import { logMicrodollarUsage } from '@/lib/ai-gateway/processUsage';
import { applyResolvedAutoModel } from '@/lib/ai-gateway/auto-model/resolution';
import { getDirectByokModel } from '@/lib/ai-gateway/providers/direct-byok';
import { handleRequestLogging } from '@/lib/ai-gateway/handleRequestLogging';

jest.mock('next/server', () => {
return {
Expand Down Expand Up @@ -95,6 +96,7 @@ const mockedFetchEfficientAutoDecision = jest.mocked(fetchEfficientAutoDecision)
const mockedLogMicrodollarUsage = jest.mocked(logMicrodollarUsage);
const mockedApplyResolvedAutoModel = jest.mocked(applyResolvedAutoModel);
const mockedGetDirectByokModel = jest.mocked(getDirectByokModel);
const mockedHandleRequestLogging = jest.mocked(handleRequestLogging);

const provider = {
id: 'openrouter',
Expand All @@ -104,12 +106,13 @@ const provider = {
transformRequest: jest.fn(),
} satisfies Provider;

function makeRequest(body: unknown) {
function makeRequest(body: unknown, headers?: HeadersInit) {
return new Request('http://localhost:3000/api/openrouter/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-forwarded-for': '127.0.0.1',
...headers,
},
body: JSON.stringify(body),
});
Expand Down Expand Up @@ -243,6 +246,19 @@ describe('POST /api/openrouter/v1/chat/completions rules-engine actions', () =>
);
});

it('passes the Vercel request ID to request logging', async () => {
const { POST } = await import('./route');

const response = await POST(
makeRequest(makeBody(), { 'x-vercel-id': 'iad1::iad1::request-id' }) as never
);

expect(response.status).toBe(200);
expect(mockedHandleRequestLogging).toHaveBeenCalledWith(
expect.objectContaining({ vercel_request_id: 'iad1::iad1::request-id' })
);
});

it('rate limits rules-engine rate-limit actions before upstream', async () => {
mockedRedisGet.mockResolvedValue(cachedRulesEngineAction('rate-limit'));
mockedClassifyAbuse.mockResolvedValue(classifyResult('rate-limit'));
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/app/api/openrouter/[...path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,7 @@ export async function POST(request: NextRequest): Promise<NextResponseType<unkno
provider: effectiveProviderContext.provider.id,
model: effectiveModelIdLowerCased,
session_id: usageContext.session_id,
vercel_request_id: extractHeaderAndLimitLength(request, 'x-vercel-id'),
request: requestBodyParsed,
});

Expand Down
13 changes: 12 additions & 1 deletion apps/web/src/lib/ai-gateway/handleRequestLogging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,21 @@ export async function handleRequestLogging(params: {
user: User | null;
organization_id: string | null;
session_id: string | null;
vercel_request_id: string | null;
provider: string;
model: string;
request: GatewayRequest;
}) {
const { clonedResponse, user, organization_id, session_id, provider, model, request } = params;
const {
clonedResponse,
user,
organization_id,
session_id,
vercel_request_id,
provider,
model,
request,
} = params;
if (!(await isLoggingEnabledForUser(user, organization_id))) {
return;
}
Expand All @@ -52,6 +62,7 @@ export async function handleRequestLogging(params: {
kilo_user_id: user?.id,
organization_id: organization_id,
session_id,
vercel_request_id,
status_code: clonedResponse.status,
model,
provider,
Expand Down
1 change: 1 addition & 0 deletions packages/db/src/migrations/0182_powerful_blacklash.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "api_request_log" ADD COLUMN "vercel_request_id" text;
Loading