mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-03 20:09:33 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The Paperclip Runner executes Google Antigravity agents on local hosts and remote sandboxes > - Previously, Paperclip shipped two separate Google runtimes: an 853 MiB IDE-extension ACP bundle (`agy_acp_server.par` 1.5.2 + `localharness_external` + Python admission monkey-patch) for ordinary turns and a separate `agy` 1.5.15 CLI binary for local-only `/swarm` turns > - Because the legacy server lacked `/swarm` and the CLI path refused remote targets and required macOS Keychain or Linux user namespaces (`auto` mode), remote sandboxes could not run `/swarm` and carried 853 MiB of legacy binaries > - This pull request unifies all Antigravity surfaces (ordinary turns, `/swarm`, interactive sign-in, and model discovery) onto one pinned `agy` 1.5.19 CLI binary (`1.5.19-4821414830800896`) with file-backed OAuth isolation (`AGY_CLI_FORCE_FILE_TOKEN_STORAGE=1`) and `--dangerously-skip-permissions` support for remote sandboxes > - The benefit is one 268 MiB Linux binary instead of 853 MiB (saving ~585 MiB in remote images), no Python runtime monkey-patching, full `/swarm` support on both local and remote hosts, and a unified 25-model catalog including Gemini 4 Neon, Carbon, Hydrogen, and Argon ## Linked Issues or Issue Description **What existing behavior does this improve?** The Google Antigravity Runner driver, provider pack materialization, Google sign-in flow, model discovery, and `/swarm` execution in `packages/paperclip-runner`, `packages/shared`, `server`, and `ui`. **Subsystem affected** Cross-cutting (multiple of the above) **Current behavior** Ordinary Antigravity turns use the legacy 1.5.2 IDE-extension ACP server (`agy_acp_server.par` + `localharness_external` + Python monkey-patch) at ~853 MiB on `linux-x64`. Native `/swarm` turns require a separate `agy` 1.5.15 CLI binary, refuse remote execution targets (`input.remote`), and fail on Linux sandboxes without user namespaces (`auto` mode). **Proposed behavior** All Antigravity flows use one pinned `agy` 1.5.19 binary (`1.5.19-4821414830800896`, ~268 MiB on `linux-x64`) across all four platforms (`darwin-arm64`, `darwin-x64`, `linux-x64`, `linux-arm64`). Ordinary turns speak ACP directly through `openAntigravityAcpRuntime`. Both ordinary and `/swarm` turns stage file-backed OAuth tokens under `<agentHomeDirectory>/antigravity-cli/antigravity-oauth-token` with `AGY_CLI_FORCE_FILE_TOKEN_STORAGE=1`, isolate from host keychains, and allow `/swarm` on remote targets using `--dangerously-skip-permissions`. Interactive Google sign-in runs `agy acp --login` with `https://antigravity.google/oauth-callback`, pre-creates `<home>/antigravity-cli` with mode `0700`, and prompts for the authorization code in the UI. **Reason and benefit** Unifying onto `agy` 1.5.19 cuts the `linux-x64` provider-pack footprint by ~585 MiB, removes Python bytecode patching from materialization, enables `/swarm` in headless Linux sandboxes without user namespaces or D-Bus, and aligns ordinary turns, model discovery, and `/swarm` on the same 25-model catalog. **Breaking changes** - `acpx-profiles.json` bumps `antigravity` to `agentProfileVersion: 3` (`native:antigravity-cli`, version `1.5.19`). Existing saved legacy 1.5.2 sessions fail closed as incompatible saved state and start fresh cleanly. - Existing Google OAuth connections carrying `client_secret` remain fully backward-compatible; new `agy acp --login` connections store canonical credentials with `auth_method: "consumer"` and no `client_secret`. ## What Changed - Pin `agy` `1.5.19-4821414830800896` across all four platforms (`darwin-arm64`, `darwin-x64`, `linux-x64`, `linux-arm64`) in `antigravity-distributions.json`, `antigravity-cli-distributions.json`, `antigravity-cli-pins.ts`, and `acpx-profiles.json` (`agentProfileVersion: 3`), and regenerate TypeScript and Rust profile artifacts. - Replace ZIP + Python patch materialization with strict single-member `tar.gz` verification (`materialize-antigravity-distribution.mjs`, `provision-antigravity.mjs`, `probe-antigravity-metadata.mjs`, `candidate-provider-pack.mjs`, `release-provider-pack.mjs`). - Regenerate `packages/shared/src/antigravity-models.generated.ts` from `agy` 1.5.19 (25 models across 8 families, including Gemini 4 Neon, Carbon, Hydrogen, and Argon) and update `packages/shared/src/antigravity-models.ts` and `tests/runner-e2e/antigravity-qualification-cases.ts` for `xhigh` thinking levels. - Add direct ACP runtime `packages/paperclip-runner/src/drivers/acpx/antigravity-acp-runtime.ts` (`openAntigravityAcpRuntime`) for ordinary Antigravity turns with OAuth and `GEMINI_API_KEY` support, `session/new` + `session/resume`, `readOnly`/plan mode enforcement, HTTP and stdio MCP forwarding, live `setModel`, slash-command neutralization, refusal classification, cancellation, and lineage retirement; route ordinary Antigravity turns unconditionally in `acpx-runtime-adapter.ts` without an ACPX fallback seam. - Update `antigravity-auth.ts`, `antigravity-credentials.ts`, `antigravity-models.ts`, `antigravity-process.ts`, `runtime-host.ts`, and `runtime-sandbox.ts` to run `agy acp --login`, pre-create `<home>/antigravity-cli` with mode `0700` (preventing Go `os.MkdirAll` `0755` readback failures), force file-backed token storage (`AGY_CLI_FORCE_FILE_TOKEN_STORAGE=1`), align `--gemini-dir` with `agentHomeDirectory`, bound model-discovery token sync cleanup, and scrub live CLI token files on cleanup. - Enable remote `/swarm` by removing the `input.remote` refusal in `server/src/services/antigravity-swarm.ts` and allowing `--dangerously-skip-permissions` + `default` mode in `antigravity-swarm-route.ts`, `antigravity-swarm-guard.ts`, and `antigravity-swarm-runtime.ts` when `permissionMode === "approve-all"`. - Update `ui/src/components/AdapterLoginChrome.tsx` copy to prompt for an authorization code (`Submit code`) for `https://antigravity.google/oauth-callback` while preserving callback URL handling for loopback redirects. - Add unit tests for `openAntigravityAcpRuntime`, direct Antigravity routing, `0700` token directory pre-creation, and hosted authorization-code login UX, and update Runner, script, shared, server, Rust, UI, and runner-e2e test suites for `agy` 1.5.19. ## Verification - Runner driver tests: `vitest run` across Antigravity driver test files (`antigravity-auth.test.ts`, `antigravity-models.test.ts`, `antigravity-acp-runtime.test.ts`, `antigravity-swarm-route.test.ts`, `codex-credentials.test.ts`, `acpx-runtime-adapter.test.ts`, `candidate-admission.test.ts`, `native-distribution-integrity.test.ts`) — all passing. - Runner script tests: `node --test` across all 7 Antigravity & provider-pack script suites — 98/98 passed. - Shared, UI, Server, and runner-e2e tests: `antigravity-models.test.ts`, `AdapterLoginChrome.test.tsx` (23/23), `antigravity-thread.test.tsx`, 23 server Antigravity/provider-pack suites, and 7 `runner-e2e` Antigravity suites — all passing. - Rust tests: `cargo test --manifest-path packages/paperclip-runner/runner/Cargo.toml --locked -p paperclip-runner-core -- acpx antigravity` — 145/145 passed. - Typechecks & generators: `@paperclipai/shared`, `@paperclipai/paperclip-runner` (TS + Rust), `@paperclipai/server` (all layers), `@paperclipai/ui`, `generate-acpx-profiles.mjs --check`, `generate-antigravity-models.mjs --check` — all passing. - Live E2E against real pinned `agy` 1.5.19 binaries (macOS `darwin-arm64`, headless Linux `linux-arm64` Docker with `--security-opt no-new-privileges`, and a live remote test deployment): - Verified `materialize-antigravity-distribution.mjs` on all 4 platform archives and `probe-antigravity-metadata.mjs` on macOS and headless Linux Docker. - Verified live Google OAuth sign-in end-to-end via `agy acp --login` + `https://antigravity.google/oauth-callback` authorization code exchange on a live remote deployment, creating a working Antigravity connection. - Verified `stageManagedAntigravityCredential` + `AGY_CLI_FORCE_FILE_TOKEN_STORAGE=1` file-backed isolation and cleanup scrub on macOS and headless Linux Docker. - Verified live `openAntigravityAcpRuntime` ordinary turn + HTTP MCP tool call + `_meta.antigravity.systemPrompt.append` + `setModel(gemini-4-argon-low)` + `session/resume` on real `agy` 1.5.19. - Verified live `openAntigravitySwarmRuntime` `/swarm` turn with `--dangerously-skip-permissions` spawning a real helper, writing workspace file, calling `paperclip_finish`, and retiring cleanly. ## Risks - **Coordinated Cloud deployment**: Remote sandboxes use the companion transition-safe Cloud update to expect `provider-assets/antigravity/linux-x64/agy`, accept `auth_method: "consumer"` credentials without `client_secret`, and scrub `antigravity-cli/antigravity-oauth-token`. - **Saved session continuity**: Existing saved 1.5.2 sessions fail closed as `ANTIGRAVITY_SAVED_STATE_INCOMPATIBLE` and start fresh cleanly on 1.5.19. ## Model Used - Google Gemini (Antigravity agentic coding assistant, reasoning/tool-use/code-execution enabled) ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
291 lines
17 KiB
JavaScript
291 lines
17 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Derives the Google Antigravity model catalog from the pinned 1.5.2 runtime.
|
|
*
|
|
* Generation (on a host with the pinned closure installed) does two reads and
|
|
* requires them to agree:
|
|
* - live: the real patched runtime is started without a credential or prompt,
|
|
* authenticated with `gemini-api-key` (the server does not validate the key
|
|
* and builds that catalog locally), and one session lists its models;
|
|
* - static: the server's built-in catalog table, read from the verified
|
|
* vendor archive, supplies every family's levels, including the Gemini 4
|
|
* families the server lists only for Agent Platform.
|
|
* The output is bound to the runtime version and the vendor archive identity.
|
|
* `--check` needs no runtime: it rejects a hand-edited file and a file whose
|
|
* binding no longer matches the checked-in distribution declaration.
|
|
*/
|
|
import { spawn } from "node:child_process";
|
|
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
import { inflateRawSync } from "node:zlib";
|
|
|
|
const PACKAGE_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
const DISTRIBUTIONS_PATH = join(PACKAGE_ROOT, "antigravity-distributions.json");
|
|
export const ANTIGRAVITY_MODELS_OUTPUT = resolve(PACKAGE_ROOT, "../shared/src/antigravity-models.generated.ts");
|
|
const SOURCE_PLATFORM = "darwin-arm64";
|
|
const SCHEMA = "paperclip.antigravity.model_catalog.v1";
|
|
const MODEL_SELECTION_SOURCE = "google3/cloud/developer_experience/antigravity_extensions/acp_server/model_selection.py";
|
|
const LEVELS = { MINIMAL: "minimal", LOW: "low", MEDIUM: "medium", HIGH: "high", EXTRA_HIGH: "extra_high" };
|
|
/** Not a credential: the server stores it without a network call, and no prompt is sent. */
|
|
const PLACEHOLDER_API_KEY = "paperclip-catalog-probe-sends-no-prompt";
|
|
const BEGIN = "/* BEGIN GENERATED CATALOG */";
|
|
const END = "/* END GENERATED CATALOG */";
|
|
const fail = message => { throw new Error(`Antigravity model catalog: ${message}`); };
|
|
|
|
/** Reads the families from `DEFAULT_MODEL_METADATA_MAP` in the server's own source.
|
|
* Every entry must be a `_thinking_family(...)` call this parser fully understands. */
|
|
export function parseAntigravityCatalogSource(source) {
|
|
const tuples = new Map();
|
|
for (const match of source.matchAll(/^(_[A-Z_]+) = \(\s*([^)]*?)\s*\)$/gm)) {
|
|
tuples.set(match[1], parseLevels(match[2]));
|
|
}
|
|
const start = source.indexOf("\nDEFAULT_MODEL_METADATA_MAP = {\n");
|
|
const end = source.indexOf("\n}\n", start);
|
|
if (start < 0 || end < 0) fail("the built-in catalog table was not found");
|
|
const body = source.slice(start + "\nDEFAULT_MODEL_METADATA_MAP = {\n".length, end);
|
|
const code = body.split("\n").filter(line => !line.trim().startsWith("#")).join("\n").trim();
|
|
const calls = code.split(/\*\*_thinking_family\(/).map(part => part.trim()).filter(Boolean);
|
|
if (!code.startsWith("**_thinking_family(")) fail("the built-in catalog has an entry this generator does not understand");
|
|
const families = calls.map(call => {
|
|
const strings = [...call.matchAll(/^\s*"([^"]+)",\s*$/gm)].map(match => match[1]);
|
|
if (strings.length !== 2) fail("a catalog family has an unexpected shape");
|
|
const [id, label] = strings;
|
|
let levels;
|
|
const named = call.match(/^\s*(_[A-Z_]+),\s*$/m);
|
|
const inline = call.match(/^\s*\(\s*([\s\S]*?)\s*\),\s*$/m);
|
|
if (named) levels = tuples.get(named[1]);
|
|
else if (inline) levels = parseLevels(inline[1]);
|
|
if (!levels?.length) fail(`levels for ${id} were not found`);
|
|
const base = call.match(/base_model_id="([^"]+)"/);
|
|
const agentPlatformOnly = /agent_platform_only=True/.test(call);
|
|
const known = call.replace(/^\s*"[^"]+",\s*$/gm, "").replace(/^\s*_[A-Z_]+,\s*$/m, "").replace(/^\s*\(\s*[\s\S]*?\s*\),\s*$/m, "")
|
|
.replace(/max_tokens=[\d_]+,?/, "").replace(/base_model_id="[^"]+",?/, "").replace(/agent_platform_only=True,?/, "").replace(/\),?$/, "");
|
|
if (known.trim()) fail(`${id} has a catalog field this generator does not understand`);
|
|
if (!/^[a-z0-9][a-z0-9.-]*$/.test(id) || !label.trim()) fail("invalid family identity");
|
|
return { id, label, baseModelId: base ? base[1] : id, levels, agentPlatformOnly };
|
|
});
|
|
if (new Set(families.map(family => family.id)).size !== families.length) fail("duplicate family");
|
|
return families;
|
|
}
|
|
|
|
function parseLevels(text) {
|
|
const names = text.split(",").map(part => part.trim()).filter(Boolean);
|
|
return names.map(name => {
|
|
const match = name.match(/^sdk_types_module\.ThinkingLevel\.([A-Z_]+)$/);
|
|
if (!match || !Object.hasOwn(LEVELS, match[1])) fail(`unknown thinking level ${name}`);
|
|
return LEVELS[match[1]];
|
|
});
|
|
}
|
|
|
|
/** Reads one stored or deflated member of the verified ZIP embedded in the server. */
|
|
export function readZipMember(zip, name) {
|
|
let end = zip.length - 22;
|
|
while (end >= 0 && zip.readUInt32LE(end) !== 0x06054b50) end--;
|
|
if (end < 0) fail("embedded archive has no directory");
|
|
let cursor = zip.readUInt32LE(end + 16);
|
|
const count = zip.readUInt16LE(end + 10);
|
|
for (let index = 0; index < count; index++) {
|
|
if (zip.readUInt32LE(cursor) !== 0x02014b50) fail("embedded archive directory is invalid");
|
|
const method = zip.readUInt16LE(cursor + 10), compressed = zip.readUInt32LE(cursor + 20);
|
|
const nameLength = zip.readUInt16LE(cursor + 28), extra = zip.readUInt16LE(cursor + 30), comment = zip.readUInt16LE(cursor + 32);
|
|
const local = zip.readUInt32LE(cursor + 42);
|
|
if (zip.subarray(cursor + 46, cursor + 46 + nameLength).toString("utf8") === name) {
|
|
const start = local + 30 + zip.readUInt16LE(local + 26) + zip.readUInt16LE(local + 28);
|
|
const data = zip.subarray(start, start + compressed);
|
|
return method === 0 ? data : method === 8 ? inflateRawSync(data) : fail("unsupported compression");
|
|
}
|
|
cursor += 46 + nameLength + extra + comment;
|
|
}
|
|
return fail(`${name} is not in the embedded archive`);
|
|
}
|
|
|
|
/** Minimal line-delimited JSON-RPC client. Provider text never enters errors. */
|
|
function rpcClient(child) {
|
|
const waiting = new Map();
|
|
let buffer = "", bytes = 0, failure;
|
|
const stop = error => { failure ??= error; for (const { reject } of waiting.values()) reject(failure); waiting.clear(); };
|
|
child.stdout.on("data", chunk => {
|
|
bytes += chunk.length;
|
|
if (bytes > 4 * 1024 * 1024) { stop(new Error("antigravity_stdout_limit")); return; }
|
|
buffer += chunk.toString("utf8");
|
|
while (buffer.includes("\n")) {
|
|
const line = buffer.slice(0, buffer.indexOf("\n")); buffer = buffer.slice(buffer.indexOf("\n") + 1);
|
|
if (!line.trim()) continue;
|
|
let message;
|
|
try { message = JSON.parse(line); } catch { stop(new Error("antigravity_malformed_json")); return; }
|
|
if (message.method !== undefined) {
|
|
// Requests from the agent are refused; notifications are ignored.
|
|
if (message.id !== undefined) child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id: message.id, error: { code: -32601, message: "unsupported" } })}\n`);
|
|
continue;
|
|
}
|
|
const entry = waiting.get(message.id);
|
|
if (!entry) continue;
|
|
waiting.delete(message.id);
|
|
if (message.error) entry.reject(Object.assign(new Error(`antigravity_request_rejected:${entry.method}`), { rpcError: { code: message.error.code, message: String(message.error.message ?? "").slice(0, 300) } }));
|
|
else entry.resolve(message.result);
|
|
}
|
|
});
|
|
child.once("error", () => stop(new Error("antigravity_spawn_failure")));
|
|
child.once("close", () => stop(new Error("antigravity_exited")));
|
|
let next = 1;
|
|
return (method, params, timeoutMs = 30_000) => new Promise((resolve, reject) => {
|
|
if (failure) { reject(failure); return; }
|
|
const id = next++;
|
|
const timer = setTimeout(() => { waiting.delete(id); reject(new Error(`antigravity_timeout:${method}`)); }, timeoutMs);
|
|
waiting.set(id, { method, resolve: value => { clearTimeout(timer); resolve(value); }, reject: error => { clearTimeout(timer); reject(error); } });
|
|
child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Starts the pinned patched runtime with only an API-key auth choice, runs
|
|
* `steps(request, workspace)` and always stops the process tree. Never sends a
|
|
* prompt.
|
|
*/
|
|
export async function withPromptFreeAntigravitySession(steps) {
|
|
const { antigravityDistribution, verifyAntigravityDistribution } = await import("./materialize-antigravity-distribution.mjs");
|
|
const { antigravityRuntimeCachePath } = await import("./antigravity-provisioner-layout.mjs");
|
|
const { antigravityMetadataEnvironment, stopAntigravityMetadataChild } = await import("./probe-antigravity-metadata.mjs");
|
|
const distribution = await antigravityDistribution();
|
|
if (`${distribution.platform}-${distribution.architecture}` !== SOURCE_PLATFORM) fail(`generate on ${SOURCE_PLATFORM}`);
|
|
const runtimeDirectory = antigravityRuntimeCachePath(distribution.closureSha256, distribution.platform, distribution.architecture);
|
|
await verifyAntigravityDistribution(runtimeDirectory, distribution);
|
|
const root = await mkdtemp(join(tmpdir(), "paperclip-antigravity-catalog-"));
|
|
let child;
|
|
try {
|
|
const env = { ...antigravityMetadataEnvironment(root, runtimeDirectory), GEMINI_API_KEY: PLACEHOLDER_API_KEY };
|
|
for (const key of ["HOME", "GEMINI_HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "TMPDIR"]) await mkdir(env[key], { mode: 0o700 });
|
|
const workspace = join(root, "workspace");
|
|
await mkdir(workspace, { mode: 0o700 });
|
|
child = spawn(join(runtimeDirectory, distribution.executable), [...distribution.args, "--gemini-dir", env.GEMINI_HOME, "--disable-telemetry"], { cwd: root, env, stdio: "pipe", detached: true });
|
|
child.stderr.resume();
|
|
const request = rpcClient(child);
|
|
const initialized = await request("initialize", { protocolVersion: 1, clientInfo: { name: "paperclip-runner", version: "1" }, clientCapabilities: {} });
|
|
if (initialized?.agentInfo?.name !== "Antigravity ACP Server" || initialized.agentInfo.version !== distribution.version) fail("runtime identity differs from the pin");
|
|
await request("authenticate", { methodId: "gemini-api-key" });
|
|
return { distribution, result: await steps(request, workspace) };
|
|
} finally {
|
|
await stopAntigravityMetadataChild(child, { eofTimeoutMs: 1_000, terminateTimeoutMs: 1_000, killTimeoutMs: 1_000 });
|
|
await rm(root, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
/** Derive families from the live `session/new` model list and model config options. */
|
|
export function deriveAntigravityFamilies(session) {
|
|
const models = session?.models?.availableModels;
|
|
if (!Array.isArray(models) || models.length === 0) fail("session did not list models");
|
|
const configOptions = Array.isArray(session?.configOptions) ? session.configOptions : [];
|
|
const familyLabels = new Map();
|
|
const modelOption = configOptions.find(option => option?.id === "model");
|
|
for (const choice of Array.isArray(modelOption?.options) ? modelOption.options : []) {
|
|
if (typeof choice?.value === "string" && typeof choice?.name === "string") familyLabels.set(choice.value, choice.name.trim());
|
|
}
|
|
const byFamily = new Map();
|
|
for (const entry of models) {
|
|
const id = typeof entry?.modelId === "string" ? entry.modelId : "";
|
|
const label = typeof entry?.name === "string" ? entry.name.trim() : "";
|
|
const cut = id.lastIndexOf("-");
|
|
if (cut < 1) fail(`unexpected model id ${id}`);
|
|
const familyId = id.slice(0, cut);
|
|
const level = id.slice(cut + 1);
|
|
const familyLabel = familyLabels.get(familyId) ?? label.replace(/\s*\([^)]+\)$/, "").trim();
|
|
if (!/^[a-z0-9][a-z0-9.-]*$/.test(familyId) || !familyLabel) fail(`invalid family identity for ${id}`);
|
|
let family = byFamily.get(familyId);
|
|
if (!family) {
|
|
family = { id: familyId, label: familyLabel, baseModelId: familyId, levels: [], agentPlatformOnly: false };
|
|
byFamily.set(familyId, family);
|
|
}
|
|
if (!family.levels.includes(level)) family.levels.push(level);
|
|
}
|
|
return [...byFamily.values()];
|
|
}
|
|
|
|
/** The API-key catalog exactly as one fresh native session lists it. */
|
|
export async function readLiveApiKeyCatalog() {
|
|
return withPromptFreeAntigravitySession(async (request, workspace) => {
|
|
const session = await request("session/new", { cwd: workspace, mcpServers: [] });
|
|
const models = session?.models;
|
|
if (!Array.isArray(models?.availableModels) || typeof models.currentModelId !== "string") fail("session did not list models");
|
|
const families = deriveAntigravityFamilies(session);
|
|
await request("session/close", { sessionId: session.sessionId }).catch(() => undefined);
|
|
return {
|
|
defaultModelId: models.currentModelId,
|
|
models: models.availableModels.map(model => ({ id: model.modelId, label: model.name })),
|
|
families,
|
|
};
|
|
});
|
|
}
|
|
|
|
/** Joins the two reads; any disagreement stops generation. */
|
|
export function buildAntigravityModelCatalog({ families, live, binding }) {
|
|
const expanded = families.filter(family => !family.agentPlatformOnly).flatMap(family => family.levels.map(level => `${family.id}-${level}`));
|
|
const liveIds = live.models.map(model => model.id);
|
|
if (JSON.stringify(expanded) !== JSON.stringify(liveIds)) fail("the live API-key catalog differs from the built-in table");
|
|
if (!liveIds.includes(live.defaultModelId)) fail("the live default model is not listed");
|
|
const labels = new Map(live.models.map(model => [model.id, model.label]));
|
|
for (const family of families) {
|
|
for (const level of family.levels) {
|
|
const id = `${family.id}-${level}`;
|
|
if (labels.has(id) && !labels.get(id).startsWith(`${family.label} (`)) fail(`label for ${id} differs from its family`);
|
|
}
|
|
}
|
|
return { schema: SCHEMA, source: binding, apiKey: { defaultModelId: live.defaultModelId, models: live.models }, families };
|
|
}
|
|
|
|
export function bindingFromDeclaration(declaration) {
|
|
const item = declaration.platforms?.[SOURCE_PLATFORM];
|
|
if (!item) fail(`${SOURCE_PLATFORM} is not declared`);
|
|
return { runtimeVersion: declaration.version, platform: SOURCE_PLATFORM, archiveSha256: item.archiveSha256, closureSha256: item.closureSha256 };
|
|
}
|
|
|
|
export function renderAntigravityModelCatalog(catalog) {
|
|
return `// Generated by packages/paperclip-runner/scripts/generate-antigravity-models.mjs from the
|
|
// pinned Google Antigravity runtime. Do not edit; regenerate with
|
|
// \`node packages/paperclip-runner/scripts/generate-antigravity-models.mjs\` on darwin-arm64.
|
|
${BEGIN}
|
|
export const ANTIGRAVITY_GENERATED_MODEL_CATALOG = ${JSON.stringify(catalog, null, 2)} as const;
|
|
${END}
|
|
`;
|
|
}
|
|
|
|
export function parseRenderedCatalog(text) {
|
|
const start = text.indexOf(BEGIN), end = text.indexOf(END);
|
|
if (start < 0 || end < 0) fail("generated markers are missing");
|
|
const body = text.slice(start + BEGIN.length, end).trim();
|
|
const match = body.match(/^export const ANTIGRAVITY_GENERATED_MODEL_CATALOG = ([\s\S]*) as const;$/);
|
|
if (!match) fail("generated body has an unexpected shape");
|
|
return JSON.parse(match[1]);
|
|
}
|
|
|
|
/** Static staleness check used by the package build. */
|
|
export async function checkAntigravityModelCatalog({ output = ANTIGRAVITY_MODELS_OUTPUT, declarationPath = DISTRIBUTIONS_PATH } = {}) {
|
|
const text = await readFile(output, "utf8");
|
|
const catalog = parseRenderedCatalog(text);
|
|
if (renderAntigravityModelCatalog(catalog) !== text) fail("generated file was edited by hand; regenerate it");
|
|
if (catalog.schema !== SCHEMA) fail("unexpected schema");
|
|
const expected = bindingFromDeclaration(JSON.parse(await readFile(declarationPath, "utf8")));
|
|
if (JSON.stringify(catalog.source) !== JSON.stringify(expected)) fail("catalog is stale: the pinned Antigravity runtime changed; regenerate it on darwin-arm64");
|
|
buildAntigravityModelCatalog({ families: catalog.families, live: catalog.apiKey, binding: catalog.source });
|
|
return catalog;
|
|
}
|
|
|
|
async function main() {
|
|
if (process.argv.includes("--check")) {
|
|
await checkAntigravityModelCatalog();
|
|
process.stdout.write("Antigravity model catalog is current.\n");
|
|
return;
|
|
}
|
|
const { distribution, result: live } = await readLiveApiKeyCatalog();
|
|
const binding = bindingFromDeclaration(JSON.parse(await readFile(DISTRIBUTIONS_PATH, "utf8")));
|
|
if (binding.runtimeVersion !== distribution.version) fail("runtime version differs from the declaration");
|
|
const catalog = buildAntigravityModelCatalog({ families: live.families, live: { defaultModelId: live.defaultModelId, models: live.models }, binding });
|
|
await writeFile(ANTIGRAVITY_MODELS_OUTPUT, renderAntigravityModelCatalog(catalog));
|
|
process.stdout.write(`Wrote ${catalog.apiKey.models.length} API-key models and ${catalog.families.length} families.\n`);
|
|
}
|
|
|
|
if (process.argv[1] && pathToFileURL(resolve(process.argv[1])).href === import.meta.url) {
|
|
main().catch(error => { process.stderr.write(`${error.message}\n`); process.exitCode = 1; });
|
|
}
|