Skip to content
Open
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
6 changes: 3 additions & 3 deletions ENVIRONMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,11 @@ Manage shared web env var additions and rotations with `pnpm web:env set <VARIAB

### AI Providers

- `OPENROUTER_API_KEY` - Primary OpenRouter API key for model inference through the AI gateway; provider definition in `apps/web/src/lib/ai-gateway/providers/provider-definitions.ts` pointing to `https://openrouter.ai/api/v1`. `[SECRET]`
- `OPENROUTER_API_KEY` - Primary OpenRouter API key for model inference through the AI gateway; provider definition in `apps/web/src/lib/ai-gateway/providers/definitions/openrouter.ts` pointing to `https://openrouter.ai/api/v1`. `[SECRET]`
- `OPENAI_API_KEY` - OpenAI API key supplied as a managed BYOK credential when managed inference requests route through the Vercel AI Gateway and permit the OpenAI provider. `[SECRET]`
- `MISTRAL_API_KEY` - Mistral API key; used in `apps/web/src/lib/ai-gateway/embeddings/embedding-providers.ts` for `codestral-embed-2505` and `mistral-embed` embeddings, in the FIM completions proxy at `apps/web/src/app/api/fim/completions/route.ts` (routes Mistral Codestral vs. La Plateforme keys), and as a provider config in `apps/web/src/lib/config.server.ts`. `[SECRET]`
- `LONGCAT_API_KEY` - LongCat API key for model inference through the AI gateway; provider definition in `apps/web/src/lib/ai-gateway/providers/provider-definitions.ts`. `[SECRET]`
- `STREAMLAKE_API_KEY` - StreamLake API key for model inference through the AI gateway; provider definition in `apps/web/src/lib/ai-gateway/providers/provider-definitions.ts`. `[SECRET]`
- `LONGCAT_API_KEY` - LongCat API key for model inference through the AI gateway; provider definition in `apps/web/src/lib/ai-gateway/providers/definitions/longcat.ts`. `[SECRET]`
- `STREAMLAKE_API_KEY` - StreamLake API key for model inference through the AI gateway; provider definition in `apps/web/src/lib/ai-gateway/providers/definitions/streamlake.ts`. `[SECRET]`
- `PERPLEXITY_API_KEY` - Perplexity API key for percentage-routed Kimi K3 inference through the AI gateway; provider definition in `apps/web/src/lib/ai-gateway/providers/partner/providers.ts`. `[SECRET]`
- `INCEPTION_API_KEY` - Inception Labs API key; used in `apps/web/src/app/api/fim/completions/route.ts` and `apps/web/src/app/api/edit/completions/route.ts` as a fill-in-the-middle (FIM) provider, with endpoint `https://api.inceptionlabs.ai/v1/fim/completions`. Defined in `apps/web/src/lib/config.server.ts`. `[SECRET]`
- `AI_ATTRIBUTION_ADMIN_SECRET` - Admin secret for the AI Attribution service (`apps/web/src/lib/ai-attribution-service.ts`); sent as `X-Admin-Secret` header. `[SECRET]`
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/lib/ai-gateway/auto-model/resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
selectAutoFreeCandidate,
} from '@/lib/ai-gateway/models';
import { getOpenRouterModelsFromDatabase } from '@/lib/ai-gateway/providers/gateway-models-cache';
import { tryGetProviderById } from '@/lib/ai-gateway/providers/provider-definitions';
import { tryGetProviderById } from '@/lib/ai-gateway/providers/definitions/try-get-provider-by-id';
import type { ProviderId } from '@/lib/ai-gateway/providers/types';
import {
getOrganizationAutoRoute,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/lib/ai-gateway/local-fake-llm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { buildDirectProvider } from '@/lib/ai-gateway/experiments/build-direct-provider';
import { OPENROUTER } from '@/lib/ai-gateway/providers/openrouter-definition';
import { OPENROUTER } from '@/lib/ai-gateway/providers/definitions/openrouter';
import type { Provider } from '@/lib/ai-gateway/providers/types';
import type { OpenRouterModel } from '@/lib/organizations/organization-types';

Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/lib/ai-gateway/model-api-kinds.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { findKiloExclusiveModel } from '@/lib/ai-gateway/models';
import { OPENROUTER, tryGetProviderById } from '@/lib/ai-gateway/providers/provider-definitions';
import { OPENROUTER } from '@/lib/ai-gateway/providers/definitions/openrouter';
import { tryGetProviderById } from '@/lib/ai-gateway/providers/definitions/try-get-provider-by-id';
import type { GatewayChatApiKind } from '@/lib/ai-gateway/providers/types';

const GATEWAY_CHAT_API_KINDS: readonly GatewayChatApiKind[] = [
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/lib/ai-gateway/processUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import type {
OpenRouterGeneration,
} from './providers/openrouter/types';
import { fetchGeneration } from './providers/upstream-request';
import { OPENROUTER, VERCEL_AI_GATEWAY } from './providers/provider-definitions';
import { OPENROUTER } from './providers/definitions/openrouter';
import { VERCEL_AI_GATEWAY } from './providers/definitions/vercel';
import { toMicrodollars } from '../utils';
import { captureException, captureMessage, startSpan, startInactiveSpan } from '@sentry/nextjs';
import type { Span } from '@sentry/nextjs';
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/lib/ai-gateway/providerHash.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { OPENROUTER, VERCEL_AI_GATEWAY } from '@/lib/ai-gateway/providers/provider-definitions';
import { OPENROUTER } from '@/lib/ai-gateway/providers/definitions/openrouter';
import { VERCEL_AI_GATEWAY } from '@/lib/ai-gateway/providers/definitions/vercel';
import type { GatewayRequest } from '@/lib/ai-gateway/providers/openrouter/types';
import {
applyTrackingIds,
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/lib/ai-gateway/providerHash.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import crypto from 'crypto';
import { type Provider } from '@/lib/ai-gateway/providers/types';
import { OPENROUTER, VERCEL_AI_GATEWAY } from '@/lib/ai-gateway/providers/provider-definitions';
import { OPENROUTER } from '@/lib/ai-gateway/providers/definitions/openrouter';
import { VERCEL_AI_GATEWAY } from '@/lib/ai-gateway/providers/definitions/vercel';
import { getEnvVariable } from '@/lib/dotenvx';
import type { GatewayRequest } from '@/lib/ai-gateway/providers/openrouter/types';

Expand Down
19 changes: 19 additions & 0 deletions apps/web/src/lib/ai-gateway/providers/definitions/alibaba.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getEnvVariable } from '@/lib/dotenvx';
import { isReasoningExplicitlyDisabled } from '@/lib/ai-gateway/providers/openrouter/request-helpers';
import type { Provider } from '@/lib/ai-gateway/providers/types';

export const ALIBABA = {
id: 'alibaba',
apiUrl: 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1',
apiUrlOverrides: {},
apiKey: getEnvVariable('ALIBABA_API_KEY'),
apiKeyHeader: null,
supportedChatApis: [
'chat_completions',
// 'responses', // supported, not tested
],
responseTransforms: null,
async transformRequest(context) {
context.request.body.enable_thinking = !isReasoningExplicitlyDisabled(context.request);
},
} as const satisfies Provider;
22 changes: 22 additions & 0 deletions apps/web/src/lib/ai-gateway/providers/definitions/longcat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getEnvVariable } from '@/lib/dotenvx';
import { isReasoningExplicitlyDisabled } from '@/lib/ai-gateway/providers/openrouter/request-helpers';
import { ReasoningDetailsTransform, type Provider } from '@/lib/ai-gateway/providers/types';

export const LONGCAT = {
id: 'longcat',
apiUrl: 'https://api.longcat.ai/openai/v1',
apiUrlOverrides: {},
apiKey: getEnvVariable('LONGCAT_API_KEY'),
apiKeyHeader: null,
supportedChatApis: ['chat_completions'],
responseTransforms: ReasoningDetailsTransform.ReasoningContent,
async transformRequest(context) {
context.request.body.thinking = {
type: isReasoningExplicitlyDisabled(context.request) ? 'disabled' : 'enabled',
};
delete context.request.body.provider;
if (context.request.body.user) {
context.extraHeaders['Mt-User-Id'] = context.request.body.user;
}
},
} as const satisfies Provider;
15 changes: 15 additions & 0 deletions apps/web/src/lib/ai-gateway/providers/definitions/martian.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getEnvVariable } from '@/lib/dotenvx';
import type { Provider } from '@/lib/ai-gateway/providers/types';

export const MARTIAN = {
id: 'martian',
apiUrl: 'https://api.withmartian.com/v1',
apiUrlOverrides: {},
apiKey: getEnvVariable('MARTIAN_API_KEY'),
apiKeyHeader: null,
supportedChatApis: ['chat_completions', 'responses', 'messages'],
responseTransforms: null,
async transformRequest(context) {
delete context.request.body.provider;
},
} as const satisfies Provider;
13 changes: 13 additions & 0 deletions apps/web/src/lib/ai-gateway/providers/definitions/mistral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getEnvVariable } from '@/lib/dotenvx';
import type { Provider } from '@/lib/ai-gateway/providers/types';

export const MISTRAL = {
id: 'mistral',
apiUrl: 'https://api.mistral.ai/v1',
apiUrlOverrides: {},
apiKey: getEnvVariable('MISTRAL_API_KEY'),
apiKeyHeader: null,
supportedChatApis: [],
responseTransforms: null,
async transformRequest() {},
} as const satisfies Provider;
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { getEnvVariable } from '@/lib/dotenvx';
import type { Provider } from '@/lib/ai-gateway/providers/types';

/**
* Self-contained OpenRouter provider definition. Kept in a leaf module so
* importers that only need `OPENROUTER` (for example the local fake LLM) do
* not pull the rest of the provider graph into a circular dependency.
*/
export const OPENROUTER = {
id: 'openrouter',
apiUrl: 'https://openrouter.ai/api/v1',
Expand Down
32 changes: 32 additions & 0 deletions apps/web/src/lib/ai-gateway/providers/definitions/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getEnvVariable } from '@/lib/dotenvx';
import { isReasoningExplicitlyDisabled } from '@/lib/ai-gateway/providers/openrouter/request-helpers';
import type { Provider } from '@/lib/ai-gateway/providers/types';

export const SEED = {
id: 'seed',
apiUrl: 'https://ark.ap-southeast.bytepluses.com/api/v3',
apiUrlOverrides: {},
apiKey: getEnvVariable('BYTEDANCE_API_KEY'),
apiKeyHeader: null,
supportedChatApis: [
'chat_completions',
// 'responses', // supported, not tested
],
responseTransforms: null,
async transformRequest(context) {
if (!isReasoningExplicitlyDisabled(context.request)) {
context.request.body.thinking = { type: 'enabled' };
if (context.request.kind === 'chat_completions') {
context.request.body.reasoning_effort ??= context.request.body.reasoning?.effort;
}
} else {
context.request.body.thinking = { type: 'disabled' };
}
if (context.request.kind === 'responses') {
delete context.request.body.prompt_cache_key;
delete context.request.body.safety_identifier;
delete context.request.body.user;
delete context.request.body.provider;
}
},
} as const satisfies Provider;
15 changes: 15 additions & 0 deletions apps/web/src/lib/ai-gateway/providers/definitions/streamlake.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getEnvVariable } from '@/lib/dotenvx';
import type { Provider } from '@/lib/ai-gateway/providers/types';

export const STREAMLAKE = {
id: 'streamlake',
apiUrl: 'https://vanchin.streamlake.ai/api/gateway/v1/endpoints',
apiUrlOverrides: {},
apiKey: getEnvVariable('STREAMLAKE_API_KEY'),
apiKeyHeader: null,
supportedChatApis: ['chat_completions'],
responseTransforms: null,
async transformRequest(context) {
delete context.request.body.provider;
},
} as const satisfies Provider;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ALIBABA } from '@/lib/ai-gateway/providers/definitions/alibaba';
import { LONGCAT } from '@/lib/ai-gateway/providers/definitions/longcat';
import { MARTIAN } from '@/lib/ai-gateway/providers/definitions/martian';
import { MISTRAL } from '@/lib/ai-gateway/providers/definitions/mistral';
import { OPENROUTER } from '@/lib/ai-gateway/providers/definitions/openrouter';
import { SEED } from '@/lib/ai-gateway/providers/definitions/seed';
import { STREAMLAKE } from '@/lib/ai-gateway/providers/definitions/streamlake';
import { VERCEL_AI_GATEWAY } from '@/lib/ai-gateway/providers/definitions/vercel';
import type { Provider, ProviderId } from '@/lib/ai-gateway/providers/types';

export function tryGetProviderById(providerId: ProviderId): Provider | undefined {
switch (providerId) {
case 'openrouter':
return OPENROUTER;
case 'alibaba':
return ALIBABA;
case 'seed':
return SEED;
case 'longcat':
return LONGCAT;
case 'martian':
return MARTIAN;
case 'mistral':
return MISTRAL;
case 'streamlake':
return STREAMLAKE;
case 'vercel':
return VERCEL_AI_GATEWAY;
default:
return undefined;
}
}
16 changes: 16 additions & 0 deletions apps/web/src/lib/ai-gateway/providers/definitions/vercel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getEnvVariable } from '@/lib/dotenvx';
import type { Provider } from '@/lib/ai-gateway/providers/types';
import { applyVercelSettings } from '@/lib/ai-gateway/providers/vercel';

export const VERCEL_AI_GATEWAY = {
id: 'vercel',
apiUrl: 'https://ai-gateway.vercel.sh/v1',
apiUrlOverrides: {},
apiKey: getEnvVariable('VERCEL_AI_GATEWAY_API_KEY'),
apiKeyHeader: null,
supportedChatApis: ['chat_completions', 'messages', 'responses'],
responseTransforms: null,
async transformRequest(context) {
await applyVercelSettings(context.model, context.request, context.userByok);
},
} as const satisfies Provider;
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { afterEach, beforeEach, describe, expect, test } from '@jest/globals';
import { getProvider, getTranscriptionProvider } from '@/lib/ai-gateway/providers/get-provider';
import { OPENROUTER, VERCEL_AI_GATEWAY } from '@/lib/ai-gateway/providers/provider-definitions';
import { OPENROUTER } from '@/lib/ai-gateway/providers/definitions/openrouter';
import { VERCEL_AI_GATEWAY } from '@/lib/ai-gateway/providers/definitions/vercel';
import { shouldRouteToVercel } from '@/lib/ai-gateway/providers/vercel';
import type { GatewayRequest } from '@/lib/ai-gateway/providers/openrouter/types';
import type { User } from '@kilocode/db/schema';
Expand Down
8 changes: 3 additions & 5 deletions apps/web/src/lib/ai-gateway/providers/get-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ import { eq } from 'drizzle-orm';
import type { AnonymousUserContext } from '@/lib/anonymous';
import { isAnonymousContext } from '@/lib/anonymous';
import type { BYOKResult, Provider } from '@/lib/ai-gateway/providers/types';
import {
OPENROUTER,
tryGetProviderById,
VERCEL_AI_GATEWAY,
} from '@/lib/ai-gateway/providers/provider-definitions';
import { OPENROUTER } from '@/lib/ai-gateway/providers/definitions/openrouter';
import { tryGetProviderById } from '@/lib/ai-gateway/providers/definitions/try-get-provider-by-id';
import { VERCEL_AI_GATEWAY } from '@/lib/ai-gateway/providers/definitions/vercel';
import { getDirectByokModel } from '@/lib/ai-gateway/providers/direct-byok';
import { CustomLlmCredentialsSchema, CustomLlmDefinitionSchema } from '@kilocode/db/schema-types';
import { buildDirectProvider } from '@/lib/ai-gateway/experiments/build-direct-provider';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
getEmbeddingProvider,
getTranscriptionProvider,
} from '@/lib/ai-gateway/providers/get-provider';
import { OPENROUTER, VERCEL_AI_GATEWAY } from '@/lib/ai-gateway/providers/provider-definitions';
import { OPENROUTER } from '@/lib/ai-gateway/providers/definitions/openrouter';
import { VERCEL_AI_GATEWAY } from '@/lib/ai-gateway/providers/definitions/vercel';
import { createAnonymousContext } from '@/lib/anonymous';
import {
getModelUserByokProviders,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/lib/ai-gateway/providers/openrouter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getLocalFakeTranscriptionModelsUrl,
LOCAL_FAKE_LLM_API_KEY,
} from '@/lib/ai-gateway/local-fake-llm';
import { OPENROUTER } from '@/lib/ai-gateway/providers/provider-definitions';
import { OPENROUTER } from '@/lib/ai-gateway/providers/definitions/openrouter';
import type { OpenRouterModel } from '@/lib/organizations/organization-types';
import {
OpenRouterModelsResponseSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { ai_gateway_sync_providers_state, modelsByProvider } from '@kilocode/db/
import { db } from '@/lib/drizzle';
import { desc, eq, lt, sql } from 'drizzle-orm';
import { captureException } from '@sentry/nextjs';
import { OPENROUTER, VERCEL_AI_GATEWAY } from '@/lib/ai-gateway/providers/provider-definitions';
import { OPENROUTER } from '@/lib/ai-gateway/providers/definitions/openrouter';
import { VERCEL_AI_GATEWAY } from '@/lib/ai-gateway/providers/definitions/vercel';
import { logAutoModelChangesForAllOrgs } from '@/lib/organizations/auto-model-change-log';
import type { Provider } from '@/lib/ai-gateway/providers/types';
import type { StoredModel } from '@kilocode/db/schema-types';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { describe, expect, test } from '@jest/globals';

import {
LONGCAT,
OPENROUTER,
tryGetProviderById,
} from '@/lib/ai-gateway/providers/provider-definitions';
import { LONGCAT } from '@/lib/ai-gateway/providers/definitions/longcat';
import { OPENROUTER } from '@/lib/ai-gateway/providers/definitions/openrouter';
import { tryGetProviderById } from '@/lib/ai-gateway/providers/definitions/try-get-provider-by-id';
import type { GatewayRequest } from '@/lib/ai-gateway/providers/openrouter/types';
import {
ReasoningDetailsTransform,
Expand Down
Loading
Loading