Skip to content
Merged
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
10 changes: 9 additions & 1 deletion bin/antigravity-ls-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,16 @@ const resolveModelEnum = (requestedModel, requestedEffort, liveModels = {}) => {
for (const cand of candIds) {
const family = MODEL_MAP[cand];
if (family) {
// A family entry also carries metadata keys — `default` (an effort
// name) and `name` (a display name) — whose values are not model
// enums. Accept an effort only when it maps to an actual enum,
// otherwise fall back to the family's default effort (#686).
const isModelEnum = (v) => typeof v === 'string' && v.startsWith('MODEL_');
let effort = (requestedEffort || family.default || 'medium').toLowerCase().trim();
if (!family[effort]) effort = family.default || Object.keys(family)[0];
if (!isModelEnum(family[effort])) effort = family.default;
if (!isModelEnum(family[effort])) {
effort = Object.keys(family).find((k) => isModelEnum(family[k]));
}
const enumVal = family[effort];
return {
modelEnum: enumVal,
Expand Down
134 changes: 134 additions & 0 deletions bin/antigravity-ls-proxy.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Tests for antigravity-ls-proxy's model resolution (#686).
// Run: node --test bin/antigravity-ls-proxy.test.mjs
//
// The proxy talks to an Antigravity language server over HTTP RPC. These
// tests run a stub language server, point the proxy at it via
// ANTIGRAVITY_LS_ADDRESS / ANTIGRAVITY_CSRF_TOKEN (the env attach path in
// detectRunningInstances), and capture the `model` value the proxy sends
// in GetModelResponse. A MODEL_MAP family entry carries metadata keys
// (`default`, `name`) besides effort -> enum, and an effort string that
// collides with one of those keys used to "resolve" to the metadata
// value — an effort name or a display name — instead of a model enum.
import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import fs from 'node:fs';
import http from 'node:http';
import net from 'node:net';
import os from 'node:os';
import path from 'node:path';
import test, { after, before } from 'node:test';
import { fileURLToPath } from 'node:url';

const binDir = path.dirname(fileURLToPath(import.meta.url));

const freePort = () => new Promise((resolve, reject) => {
const srv = net.createServer();
srv.once('error', reject);
srv.listen(0, '127.0.0.1', () => {
const { port } = srv.address();
srv.close(() => resolve(port));
});
});

let lsServer;
let proxyChild;
let proxyPort;
let capturedModel;

const startStubLs = async () => {
lsServer = http.createServer((req, res) => {
let body = '';
req.on('data', (c) => { body += c; });
req.on('end', () => {
res.writeHead(200, { 'Content-Type': 'application/json' });
if (req.url.endsWith('/GetAvailableModels')) {
res.end(JSON.stringify({ response: { models: {} } }));
} else if (req.url.endsWith('/GetModelResponse')) {
try { capturedModel = JSON.parse(body).model; } catch { capturedModel = null; }
res.end(JSON.stringify({ response: 'stub answer' }));
} else {
// Heartbeat and anything else: a bare 200 is all the proxy checks.
res.end('{}');
}
});
});
const port = await freePort();
await new Promise((resolve) => lsServer.listen(port, '127.0.0.1', resolve));
return port;
};

before(async () => {
const lsPort = await startStubLs();
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ls-proxy-test-'));
proxyPort = await freePort();
proxyChild = spawn(process.execPath, [path.join(binDir, 'antigravity-ls-proxy.mjs')], {
env: {
...process.env,
ANTIGRAVITY_LS_PROXY_PORT: String(proxyPort),
ANTIGRAVITY_LS_ADDRESS: `127.0.0.1:${lsPort}`,
ANTIGRAVITY_CSRF_TOKEN: 'test-token',
XDG_STATE_HOME: path.join(tmp, 'state')
},
stdio: ['ignore', 'pipe', 'pipe']
});
let log = '';
proxyChild.stdout.on('data', (d) => { log += d; });
proxyChild.stderr.on('data', (d) => { log += d; });
const deadline = Date.now() + 10000;
for (;;) {
try {
const res = await fetch(`http://127.0.0.1:${proxyPort}/health`);
if (res.ok) break;
} catch {}
if (Date.now() > deadline) throw new Error(`proxy did not start: ${log}`);
await new Promise((r) => setTimeout(r, 100));
}
});

after(() => {
if (proxyChild) proxyChild.kill('SIGKILL');
if (lsServer) lsServer.close();
});

const chat = async (payload) => {
capturedModel = undefined;
const res = await fetch(`http://127.0.0.1:${proxyPort}/v1/chat/completions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
messages: [{ role: 'user', content: 'hello' }],
...payload
})
});
return res;
};

test("effort 'default' falls back to the family default enum, not the string 'medium'", async () => {
const res = await chat({ model: 'gemini-3.8-flash', reasoning_effort: 'default' });
assert.equal(res.status, 200);
assert.equal(capturedModel, 'MODEL_PLACEHOLDER_M319');
});

test("effort 'name' falls back to the family default enum, not the display name", async () => {
const res = await chat({ model: 'gemini-3.8-flash', reasoning_effort: 'name' });
assert.equal(res.status, 200);
assert.equal(capturedModel, 'MODEL_PLACEHOLDER_M319');
});

test('a valid effort still resolves to its own enum', async () => {
const res = await chat({ model: 'gemini-3.8-flash', reasoning_effort: 'high' });
assert.equal(res.status, 200);
assert.equal(capturedModel, 'MODEL_PLACEHOLDER_M318');
});

test('an unoffered effort still falls back to the family default', async () => {
const res = await chat({ model: 'gemini-3.1-pro', reasoning_effort: 'medium' });
assert.equal(res.status, 200);
assert.equal(capturedModel, 'MODEL_PLACEHOLDER_M37');
});

test('an unknown model is still refused with 400', async () => {
const res = await chat({ model: 'no-such-model-xyz' });
assert.equal(res.status, 400);
assert.equal(capturedModel, undefined);
});
Loading