-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
feat(mitm): translate Antigravity cloudcode end-to-end (Gap B) #4299
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,47 @@ | ||||||||||||||||||
| import { handleChat } from "@/sse/handlers/chat"; | ||||||||||||||||||
| import { initTranslators } from "@omniroute/open-sse/translator/index.ts"; | ||||||||||||||||||
|
|
||||||||||||||||||
| let initialized = false; | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Initialize translators once. | ||||||||||||||||||
| */ | ||||||||||||||||||
| async function ensureInitialized() { | ||||||||||||||||||
| if (!initialized) { | ||||||||||||||||||
| await initTranslators(); | ||||||||||||||||||
| initialized = true; | ||||||||||||||||||
| console.log("[SSE] Translators initialized for /v1/antigravity"); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Handle CORS preflight. | ||||||||||||||||||
| */ | ||||||||||||||||||
| export async function OPTIONS() { | ||||||||||||||||||
| return new Response(null, { | ||||||||||||||||||
| headers: { | ||||||||||||||||||
| "Access-Control-Allow-Methods": "POST, OPTIONS", | ||||||||||||||||||
| "Access-Control-Allow-Headers": "*", | ||||||||||||||||||
| }, | ||||||||||||||||||
| }); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * POST /v1/antigravity — Antigravity/cloudcode-compatible endpoint. | ||||||||||||||||||
| * | ||||||||||||||||||
| * Accepts the Antigravity IDE's cloudcode envelope | ||||||||||||||||||
| * { model, project, request: { contents, systemInstruction, tools, generationConfig } } | ||||||||||||||||||
| * and returns a cloudcode SSE reply | ||||||||||||||||||
| * { response: { candidates: [...], usageMetadata } }. | ||||||||||||||||||
| * | ||||||||||||||||||
| * `detectFormatFromEndpoint()` classifies the `/antigravity` path as | ||||||||||||||||||
| * sourceFormat "antigravity" (mirrors `/v1/messages` → claude), so `handleChat` | ||||||||||||||||||
| * translates the request antigravity→openai, routes it to the resolved | ||||||||||||||||||
| * provider/model, and translates the response openai→antigravity — reusing the | ||||||||||||||||||
| * already-registered bidirectional translators. The AgentBridge MITM proxy | ||||||||||||||||||
| * (`server.cjs`) forwards the IDE's intercepted cloudcode request here. | ||||||||||||||||||
| */ | ||||||||||||||||||
| export async function POST(request: Request): Promise<Response> { | ||||||||||||||||||
| await ensureInitialized(); | ||||||||||||||||||
| return await handleChat(request); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+44
to
+47
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. Since
Suggested change
|
||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| "use strict"; | ||
|
|
||
| // ========================================================================= | ||
| // Decide where the standalone proxy (server.cjs) forwards an intercepted, | ||
| // decrypted request — and which router endpoint speaks its wire format. | ||
| // | ||
| // The Antigravity IDE sends a cloudcode envelope: | ||
| // { model, project, request: { contents, systemInstruction, tools, ... } } | ||
| // and expects a cloudcode SSE reply ({ response: { candidates, ... } }). | ||
| // That envelope is forwarded to the antigravity-compatible endpoint | ||
| // (/v1/antigravity), where the pipeline translates request antigravity→openai | ||
| // and response openai→antigravity, so the IDE gets its own format back | ||
| // regardless of which provider actually served it. | ||
| // | ||
| // Plain OpenAI bodies ({ messages: [...] }) keep going to /v1/chat/completions. | ||
| // | ||
| // Pure + deterministic so it is unit-testable without the proxy. | ||
| // ========================================================================= | ||
|
|
||
| const CHAT_PATH = "/v1/chat/completions"; | ||
| const ANTIGRAVITY_PATH = "/v1/antigravity"; | ||
|
|
||
| /** | ||
| * True when the parsed body is an Antigravity/cloudcode envelope (the IDE wraps | ||
| * the Gemini-style payload under `request`, with a `contents` array). | ||
| */ | ||
| function isCloudcodeEnvelope(body) { | ||
| return !!( | ||
| body && | ||
| typeof body === "object" && | ||
| !Array.isArray(body) && | ||
| body.request && | ||
| typeof body.request === "object" && | ||
| Array.isArray(body.request.contents) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the forward URL + the format the IDE expects back. `baseUrl` is the | ||
| * router base (e.g. http://localhost:20128); trailing slashes are trimmed. | ||
| */ | ||
| function resolveForwardTarget(baseUrl, body) { | ||
| const base = String(baseUrl || "").replace(/\/+$/, ""); | ||
| if (isCloudcodeEnvelope(body)) { | ||
| return { url: `${base}${ANTIGRAVITY_PATH}`, format: "antigravity" }; | ||
| } | ||
| return { url: `${base}${CHAT_PATH}`, format: "openai" }; | ||
| } | ||
|
|
||
| module.exports = { | ||
| resolveForwardTarget, | ||
| isCloudcodeEnvelope, | ||
| CHAT_PATH, | ||
| ANTIGRAVITY_PATH, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { detectFormatFromEndpoint } from "@omniroute/open-sse/services/provider.ts"; | ||
|
|
||
| const ENVELOPE = { | ||
| model: "gemini-2.5-pro", | ||
| project: "projects/test", | ||
| request: { contents: [{ role: "user", parts: [{ text: "oi" }] }] }, | ||
| }; | ||
|
|
||
| test("detectFormatFromEndpoint classifies the /v1/antigravity path as antigravity", () => { | ||
| assert.equal(detectFormatFromEndpoint(ENVELOPE, "/v1/antigravity"), "antigravity"); | ||
| assert.equal(detectFormatFromEndpoint(ENVELOPE, "/api/v1/antigravity"), "antigravity"); | ||
| assert.equal( | ||
| detectFormatFromEndpoint(ENVELOPE, "/v1/antigravity:streamGenerateContent"), | ||
| "antigravity" | ||
| ); | ||
| }); | ||
|
|
||
| test("the antigravity carve-out does not disturb the other endpoint formats", () => { | ||
| assert.equal(detectFormatFromEndpoint({ messages: [] }, "/v1/messages"), "claude"); | ||
| assert.equal(detectFormatFromEndpoint({ messages: [] }, "/v1/chat/completions"), "openai"); | ||
| assert.equal(detectFormatFromEndpoint({ input: "x" }, "/v1/responses"), "openai-responses"); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { createRequire } from "node:module"; | ||
|
|
||
| const require = createRequire(import.meta.url); | ||
| const { resolveForwardTarget, isCloudcodeEnvelope, CHAT_PATH, ANTIGRAVITY_PATH } = require( | ||
| "../../src/mitm/_internal/forwardTarget.cjs" | ||
| ); | ||
|
|
||
| const BASE = "http://localhost:20128"; | ||
|
|
||
| test("cloudcode envelope routes to the antigravity endpoint", () => { | ||
| const body = { | ||
| model: "gemini-2.5-pro", | ||
| project: "projects/test", | ||
| request: { contents: [{ role: "user", parts: [{ text: "oi" }] }] }, | ||
| }; | ||
| assert.equal(isCloudcodeEnvelope(body), true); | ||
| const t = resolveForwardTarget(BASE, body); | ||
| assert.equal(t.url, `${BASE}${ANTIGRAVITY_PATH}`); | ||
| assert.equal(t.format, "antigravity"); | ||
| }); | ||
|
|
||
| test("plain OpenAI body routes to chat/completions", () => { | ||
| const body = { model: "gemini-2.5-pro", messages: [{ role: "user", content: "oi" }] }; | ||
| assert.equal(isCloudcodeEnvelope(body), false); | ||
| const t = resolveForwardTarget(BASE, body); | ||
| assert.equal(t.url, `${BASE}${CHAT_PATH}`); | ||
| assert.equal(t.format, "openai"); | ||
| }); | ||
|
|
||
| test("base url trailing slash is trimmed", () => { | ||
| const t = resolveForwardTarget("http://localhost:20128/", { messages: [] }); | ||
| assert.equal(t.url, `http://localhost:20128${CHAT_PATH}`); | ||
| }); | ||
|
|
||
| test("non-envelope shapes are not misclassified as cloudcode", () => { | ||
| // request present but contents missing / wrong type | ||
| assert.equal(isCloudcodeEnvelope({ request: {} }), false); | ||
| assert.equal(isCloudcodeEnvelope({ request: { contents: "nope" } }), false); | ||
| assert.equal(isCloudcodeEnvelope({ contents: [{ parts: [] }] }), false); // bare gemini, no envelope | ||
| assert.equal(isCloudcodeEnvelope(null), false); | ||
| assert.equal(isCloudcodeEnvelope([]), false); | ||
| assert.equal(isCloudcodeEnvelope("string"), false); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Since
initTranslatorsis a synchronous function, wrapping it in anasyncfunction and usingawaitintroduces an unnecessary asynchronous gap (microtask yield) beforeinitialized = trueis set. If multiple requests hit this endpoint concurrently at startup, they can all pass theif (!initialized)check and callinitTranslators()multiple times. Removingasync/awaitmakes the initialization completely synchronous and race-condition free.