Repository navigation
Add Mercury Next Edit (NES) as an opt-in autocomplete mode #10536
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 1 commit
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 |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { getAutocompleteModel, type DirectAutocompleteProviderID } from "./autocomplete.js" | ||
|
|
||
| /** | ||
| * Env var(s) consulted as a fallback for BYOK keys when the provider hasn't | ||
| * been authenticated via the gateway's Auth store. Mirrors `DIRECT_FIM_ENV`. | ||
| */ | ||
| export const DIRECT_EDIT_ENV: Record<DirectAutocompleteProviderID, string[]> = { | ||
| mistral: ["MISTRAL_API_KEY"], | ||
| inception: ["INCEPTION_API_KEY"], | ||
| } | ||
|
|
||
| export type EditTarget = | ||
| | { provider: "inception"; model: string; url: string } | ||
| | { provider: "kilo"; model: string; url: string } | ||
|
|
||
| const INCEPTION_EDIT_URL = "https://api.inceptionlabs.ai/v1/edit/completions" | ||
|
|
||
| /** | ||
| * Pick the upstream edit endpoint for a (provider, model) pair. Only Inception | ||
| * is wired up today — Mercury is the only model family with a documented | ||
| * /v1/edit/completions endpoint. Mistral does not expose a comparable surface. | ||
| */ | ||
| export function resolveEditTarget(provider?: string, model?: string): EditTarget { | ||
| const info = getAutocompleteModel(provider, model) | ||
| if (info.directProvider === "inception") { | ||
| return { provider: "inception", model: info.requestModel, url: INCEPTION_EDIT_URL } | ||
| } | ||
| // Kilo Gateway does not currently proxy an edit endpoint; callers should | ||
| // fall back to FIM. We still return a kilo target so the handler can surface | ||
| // a 400 rather than silently routing somewhere unexpected. | ||
| return { provider: "kilo", model: info.requestModel, url: "" } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { DIRECT_EDIT_ENV, resolveEditTarget, type EditTarget } from "../edit.js" | ||
| import type { AuthStore } from "./handlers.js" | ||
|
|
||
| type Auth = Pick<AuthStore, "get"> | ||
|
|
||
| const EDIT_TIMEOUT_MS = 30_000 | ||
| const MAX_TOKENS_DEFAULT = 512 | ||
|
|
||
| async function getProviderKey(Auth: Auth, provider: "inception" | "mistral"): Promise<string | undefined> { | ||
| const auth = await Auth.get(provider) | ||
| if (auth?.type === "api") return auth.key | ||
| return DIRECT_EDIT_ENV[provider].map((key) => process.env[key]).find(Boolean) | ||
| } | ||
|
|
||
| /** | ||
| * Extract the rewritten code from Mercury's reply. Mercury always wraps the | ||
| * editable region in a triple-backtick fence, sometimes with a language tag | ||
| * and sometimes with `<|code_to_edit|>` markers inside. Mirrors the parser the | ||
| * VSCode side used to run; doing it gateway-side keeps the Mercury contract | ||
| * in one place. | ||
| */ | ||
| function extractFencedBody(message: string): string { | ||
| if (!message) return "" | ||
| const fenceOpen = message.indexOf("```") | ||
| if (fenceOpen === -1) return message | ||
| const afterFenceOpen = message.indexOf("\n", fenceOpen + 3) | ||
| if (afterFenceOpen === -1) return "" | ||
| const fenceClose = message.lastIndexOf("```") | ||
| if (fenceClose <= afterFenceOpen) return "" | ||
| let body = message.slice(afterFenceOpen + 1, fenceClose) | ||
| if (body.endsWith("\n")) body = body.slice(0, -1) | ||
| body = body.replace(/^<\|code_to_edit\|>\n?/, "") | ||
| body = body.replace(/\n?<\|\/code_to_edit\|>$/, "") | ||
| return body | ||
| } | ||
|
|
||
| interface UpstreamResponse { | ||
| choices?: Array<{ message?: { content?: string } }> | ||
| usage?: { prompt_tokens?: number; completion_tokens?: number } | ||
| } | ||
|
|
||
| export function createEditHandler(Auth: Auth) { | ||
| return async (c: any) => { | ||
| const { content, provider, model, maxTokens } = c.req.valid("json") | ||
| const target = resolveEditTarget(provider, model) | ||
|
|
||
| if (target.provider !== "inception") { | ||
| return c.json({ error: "Next Edit currently requires the Inception provider (mercury-edit-2)." }, 400 as any) | ||
| } | ||
|
|
||
| const token = await getProviderKey(Auth, target.provider) | ||
| if (!token) { | ||
| return c.json({ error: `Missing ${target.provider} provider API key` }, 401 as any) | ||
| } | ||
|
|
||
| const signal = AbortSignal.any([c.req.raw.signal, AbortSignal.timeout(EDIT_TIMEOUT_MS)]) | ||
| console.info(`[EDIT] request provider=${target.provider} model=${target.model} url=${target.url} chars=${content.length}`) | ||
|
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. WARNING: |
||
|
|
||
| let response: Response | ||
| try { | ||
| response = await fetch(target.url, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: `Bearer ${token}`, | ||
| }, | ||
| signal, | ||
| body: JSON.stringify({ | ||
| model: target.model, | ||
| max_tokens: maxTokens ?? MAX_TOKENS_DEFAULT, | ||
| // Mercury rejects role:"system" on this endpoint — must be a single | ||
| // user message. See the integration's constants.ts for context. | ||
| messages: [{ role: "user", content }], | ||
| }), | ||
| }) | ||
| } catch (err) { | ||
| if (err instanceof DOMException && err.name === "TimeoutError") { | ||
| return c.json({ error: "Edit request timed out" }, 504 as any) | ||
| } | ||
| if (signal.aborted) return c.json({ error: "Edit request canceled" }, 499 as any) | ||
| throw err | ||
| } | ||
|
|
||
| if (!response.ok) { | ||
| const text = await safeText(response) | ||
| return c.json({ error: `Edit request failed: ${response.status} ${text}` }, response.status as any) | ||
| } | ||
|
|
||
| const json = (await response.json()) as UpstreamResponse | ||
| const replyContent = json.choices?.[0]?.message?.content ?? "" | ||
| const body = extractFencedBody(replyContent) | ||
| return c.json({ | ||
| content: body, | ||
| usage: json.usage | ||
| ? { | ||
| prompt_tokens: json.usage.prompt_tokens, | ||
| completion_tokens: json.usage.completion_tokens, | ||
| } | ||
| : undefined, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| async function safeText(res: Response): Promise<string> { | ||
| try { | ||
| return await res.text() | ||
| } catch { | ||
| return "<unreadable>" | ||
| } | ||
| } | ||
|
|
||
| // Re-export the target type for tests + the opencode handler | ||
| export type { EditTarget } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WARNING:
extractFencedBodyreturns the rawmessagewhen no code fence is found, but the oldparseMercuryEditReply(still present ineditCompletionParser.ts) returnednullfor the same case.If Mercury responds without a triple-backtick fence — e.g. a rate-limit prose message, a refusal, or an unexpected format — the full response text will propagate to
parseSuccessasreplacement, pass thereplacement.length === 0guard, and be inserted directly into the user's document as code.Consider returning
""instead so the gateway's response schema sendscontent: "", whichparseSuccessalready treats as a no-op (replacement.length === 0→ returnnull):