-
Notifications
You must be signed in to change notification settings - Fork 24
feat(fim): add Mercury Edit FIM support with provider-based routing #1693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
49a23ae
229e93e
46f88d1
c243996
196e2b5
50b236c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { MISTRAL_API_KEY } from '@/lib/config.server'; | ||
| import { MISTRAL_API_KEY, INCEPTION_API_KEY } from '@/lib/config.server'; | ||
| import type { NextRequest } from 'next/server'; | ||
| import { NextResponse } from 'next/server'; | ||
| import z from 'zod'; | ||
|
|
@@ -25,12 +25,45 @@ | |
| import { sentryLogger } from '@/lib/utils.server'; | ||
| import { getBYOKforOrganization, getBYOKforUser } from '@/lib/byok'; | ||
|
|
||
| const MISTRAL_URL = 'https://api.mistral.ai/v1/fim/completions'; | ||
| const MISTRAL_FIM_URL = 'https://api.mistral.ai/v1/fim/completions'; | ||
| const INCEPTION_FIM_URL = 'https://api.inceptionlabs.ai/v1/fim/completions'; | ||
| const FIM_MAX_TOKENS_LIMIT = 1000; | ||
|
|
||
| type FimProvider = 'mistral' | 'inception'; | ||
|
|
||
| function resolveFimProvider(model: string): { | ||
| provider: FimProvider; | ||
| upstreamModel: string; | ||
| upstreamUrl: string; | ||
| } | null { | ||
| if (model.startsWith('mistralai/')) { | ||
| return { | ||
| provider: 'mistral', | ||
| upstreamModel: model.slice('mistralai/'.length), | ||
| upstreamUrl: MISTRAL_FIM_URL, | ||
| }; | ||
| } | ||
| if (model === 'inception/mercury-edit') { | ||
| return { | ||
| provider: 'inception', | ||
| upstreamModel: 'mercury-edit', | ||
| upstreamUrl: INCEPTION_FIM_URL, | ||
| }; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function getSystemApiKey(provider: FimProvider): string | null { | ||
| switch (provider) { | ||
| case 'mistral': | ||
| return MISTRAL_API_KEY || null; | ||
| case 'inception': | ||
| return INCEPTION_API_KEY || null; | ||
| } | ||
| } | ||
|
|
||
| const FIMRequestBody = z.object({ | ||
| //ref: https://docs.mistral.ai/api/endpoint/fim#operation-fim_completion_v1_fim_completions_post | ||
| provider: z.enum(['mistral', 'inceptionlabs']).optional(), | ||
| model: z.string(), | ||
| prompt: z.string(), | ||
| suffix: z.string().optional(), | ||
|
|
@@ -82,17 +115,15 @@ | |
| return invalidRequestResponse(); | ||
| } | ||
|
|
||
| if ((requestBody.provider ?? 'mistral') !== 'mistral') { | ||
| // Resolve provider from model name | ||
| const resolved = resolveFimProvider(requestBody.model); | ||
| if (!resolved) { | ||
| return NextResponse.json( | ||
| { error: requestBody.provider + ' provider not yet supported' }, | ||
| { error: requestBody.model + ' is not a supported FIM model' }, | ||
| { status: 400 } | ||
| ); | ||
| //NOTE: mistral does not do data collection on paid org accounts like ours. | ||
| //If we ever support OTHER providers, we need to either ensure they don't | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this note relevant?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we just remove it and collect now?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inception doesn't do collection either
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there are real a gaps, we don't enforce the provider blocklist for example, but that's not a regression |
||
| //either, or at least enforce the rules the org settings configure | ||
| //see getBalanceAndOrgSettings below and its usage in the openrouter proxy. | ||
| //ref: https://help.mistral.ai/en/articles/347617-do-you-use-my-user-data-to-train-your-artificial-intelligence-models | ||
| } | ||
| const { provider: fimProvider, upstreamModel, upstreamUrl } = resolved; | ||
|
|
||
| // Validate max_tokens | ||
| if (!requestBody.max_tokens || requestBody.max_tokens > FIM_MAX_TOKENS_LIMIT) { | ||
|
|
@@ -102,35 +133,24 @@ | |
| return temporarilyUnavailableResponse(); | ||
| } | ||
|
|
||
| // Map FIM model to OpenRouter format for org settings compatibility | ||
| const fimModel_withOpenRouterStyleProviderPrefix = requestBody.model; | ||
|
|
||
| const requiredModelPrefix = 'mistralai/'; | ||
| if (!fimModel_withOpenRouterStyleProviderPrefix.startsWith(requiredModelPrefix)) { | ||
| return NextResponse.json( | ||
| { error: fimModel_withOpenRouterStyleProviderPrefix + ' is not a mistralai model' }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const mistralModel = fimModel_withOpenRouterStyleProviderPrefix.slice(requiredModelPrefix.length); | ||
|
|
||
| // Use new shared helper for fraud & project headers | ||
| const { fraudHeaders, projectId } = extractFraudAndProjectHeaders(request); | ||
| const taskId = extractHeaderAndLimitLength(request, 'x-kilocode-taskid') ?? undefined; | ||
|
|
||
| // Extract properties for usage context | ||
| const promptInfo = extractFimPromptInfo(requestBody); | ||
|
|
||
| const byokProviderKey = fimProvider === 'mistral' ? 'codestral' : 'inception'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not just make the fim provider be codestral in the first place?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's called mistral everywhere, but this hack is needed for the "not all API keys support all models" feature that mistral has. |
||
|
|
||
| const userByok = organizationId | ||
| ? await getBYOKforOrganization(readDb, organizationId, ['codestral']) | ||
| : await getBYOKforUser(readDb, user.id, ['codestral']); | ||
| ? await getBYOKforOrganization(readDb, organizationId, [byokProviderKey]) | ||
| : await getBYOKforUser(readDb, user.id, [byokProviderKey]); | ||
|
|
||
| const usageContext: MicrodollarUsageContext = { | ||
| api_kind: 'fim_completions', | ||
| kiloUserId: user.id, | ||
| provider: 'mistral', | ||
| requested_model: fimModel_withOpenRouterStyleProviderPrefix, | ||
| provider: fimProvider, | ||
chrarnoldus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| requested_model: requestBody.model, | ||
| promptInfo, | ||
| max_tokens: requestBody.max_tokens ?? null, | ||
| has_middle_out_transform: null, // N/A for FIM | ||
|
|
@@ -151,44 +171,57 @@ | |
| auto_model: null, | ||
| }; | ||
|
|
||
| setTag('ui.ai_model', fimModel_withOpenRouterStyleProviderPrefix); | ||
| setTag('ui.ai_model', requestBody.model); | ||
| // Use read replica for balance check - this is a read-only operation that can tolerate | ||
| // slight replication lag, and provides lower latency for US users | ||
| const { balance, settings, plan } = await getBalanceAndOrgSettings(organizationId, user, readDb); | ||
|
|
||
| if (balance <= 0 && !isFreeModel(fimModel_withOpenRouterStyleProviderPrefix) && !userByok) { | ||
| if (balance <= 0 && !isFreeModel(requestBody.model) && !userByok) { | ||
| return NextResponse.json({ error: { message: 'Insufficient credits' } }, { status: 402 }); | ||
| } | ||
|
|
||
| // Use shared helper for organization model restrictions | ||
| // Model allow list only applies to Enterprise plans | ||
| // Provider allow list applies to Enterprise plans; data collection applies to all plans (but FIM doesn't use provider config) | ||
| const { error: modelRestrictionError } = checkOrganizationModelRestrictions({ | ||
| modelId: fimModel_withOpenRouterStyleProviderPrefix, | ||
| modelId: requestBody.model, | ||
| settings, | ||
| organizationPlan: plan, | ||
| }); | ||
| if (modelRestrictionError) return modelRestrictionError; | ||
|
|
||
| const systemKey = getSystemApiKey(fimProvider); | ||
| const apiKey = userByok?.at(0)?.decryptedAPIKey ?? systemKey; | ||
|
|
||
| if (!apiKey) { | ||
| return NextResponse.json( | ||
| { | ||
| error: 'This model requires a BYOK API key. Please configure your API key in settings.', | ||
| }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| sentryRootSpan()?.setAttribute( | ||
| 'mistral-fim.time_to_request_start_ms', | ||
| 'fim.time_to_request_start_ms', | ||
| performance.now() - requestStartedAt | ||
| ); | ||
|
|
||
| const mistralRequestSpan = startInactiveSpan({ | ||
| name: 'mistral-fim-request-start', | ||
| const fimRequestSpan = startInactiveSpan({ | ||
| name: 'fim-request-start', | ||
| op: 'http.client', | ||
| }); | ||
|
|
||
| const bodyWithCorrectedModel = { ...requestBody, model: mistralModel }; | ||
| // Make upstream request to Mistral | ||
| const proxyRes = await fetch(MISTRAL_URL, { | ||
| const bodyForUpstream = { ...requestBody, model: upstreamModel }; | ||
|
|
||
| // Make upstream request to the resolved provider | ||
| const proxyRes = await fetch(upstreamUrl, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Authorization: `Bearer ${userByok?.at(0)?.decryptedAPIKey ?? MISTRAL_API_KEY}`, | ||
| Authorization: `Bearer ${apiKey}`, | ||
| }, | ||
| body: JSON.stringify(bodyWithCorrectedModel), | ||
| body: JSON.stringify(bodyForUpstream), | ||
| }); | ||
| usageContext.status_code = proxyRes.status; | ||
|
|
||
|
|
@@ -200,19 +233,19 @@ | |
| if (proxyRes.status >= 400) { | ||
| await captureProxyError({ | ||
| user, | ||
| request: bodyWithCorrectedModel, | ||
| request: bodyForUpstream, | ||
| response: proxyRes, | ||
| organizationId, | ||
| model: fimModel_withOpenRouterStyleProviderPrefix, | ||
| errorMessage: `Mistral FIM returned error ${proxyRes.status}`, | ||
| model: requestBody.model, | ||
| errorMessage: `FIM provider returned error ${proxyRes.status}`, | ||
| trackInSentry: proxyRes.status >= 500, | ||
| }); | ||
| } | ||
|
|
||
| const clonedResponse = proxyRes.clone(); // reading from body is side-effectful | ||
|
|
||
| // Account for usage using FIM-specific parser | ||
| countAndStoreFimUsage(clonedResponse, usageContext, mistralRequestSpan); | ||
| countAndStoreFimUsage(clonedResponse, usageContext, fimRequestSpan); | ||
|
|
||
| return wrapInSafeNextResponse(proxyRes); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.