2026-04-29 06:28:38 -04:00
|
|
|
const { err, createResponseDumper } = require("../logger");
|
2026-05-17 05:37:26 -04:00
|
|
|
const { IS_DEV } = require("../config");
|
2026-03-17 05:12:25 -04:00
|
|
|
const { fetchRouter, pipeSSE } = require("./base");
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-29 06:28:38 -04:00
|
|
|
* Intercept Antigravity request — forward Gemini body as-is to /v1/chat/completions.
|
|
|
|
|
* Router auto-detects format via body.userAgent==="antigravity" + body.request.contents,
|
|
|
|
|
* runs antigravity→openai→provider→openai→antigravity translators internally.
|
2026-03-17 05:12:25 -04:00
|
|
|
*/
|
|
|
|
|
async function intercept(req, res, bodyBuffer, mappedModel) {
|
2026-05-17 05:37:26 -04:00
|
|
|
const dumper = IS_DEV ? createResponseDumper(req, "intercept-antigravity") : null;
|
2026-04-29 06:28:38 -04:00
|
|
|
const isStream = req.url.includes(":streamGenerateContent");
|
2026-03-17 05:12:25 -04:00
|
|
|
try {
|
|
|
|
|
const body = JSON.parse(bodyBuffer.toString());
|
2026-04-29 06:28:38 -04:00
|
|
|
if (body.model) body.model = mappedModel;
|
|
|
|
|
|
2026-04-13 01:04:57 -04:00
|
|
|
const routerRes = await fetchRouter(body, "/v1/chat/completions", req.headers);
|
2026-04-29 06:28:38 -04:00
|
|
|
await pipeSSE(routerRes, res, dumper);
|
2026-03-17 05:12:25 -04:00
|
|
|
} catch (error) {
|
|
|
|
|
err(`[antigravity] ${error.message}`);
|
2026-04-29 06:28:38 -04:00
|
|
|
if (dumper) { dumper.writeChunk(`\n[ERROR] ${error.message}\n`); dumper.end(); }
|
|
|
|
|
// For stream endpoint, send SSE error chunk so SDK doesn't hang waiting
|
|
|
|
|
if (isStream) {
|
|
|
|
|
if (!res.headersSent) res.writeHead(200, { "Content-Type": "text/event-stream" });
|
|
|
|
|
res.end(`data: ${JSON.stringify({ error: { message: error.message } })}\r\n\r\n`);
|
|
|
|
|
} else {
|
|
|
|
|
if (!res.headersSent) res.writeHead(500, { "Content-Type": "application/json" });
|
|
|
|
|
res.end(JSON.stringify({ error: { message: error.message, type: "mitm_error" } }));
|
|
|
|
|
}
|
2026-03-17 05:12:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = { intercept };
|