Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
feat(antigravity): print the raw fetchAvailableModels body as JSON
Jack Rudenko5edb656243f8
diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.tsindex c7107455..089f72b0 100644--- a/packages/cli/src/cli.ts+++ b/packages/cli/src/cli.ts@@ -2318,6 +2318,9 @@ ${h("SUBCOMMANDS")}${green("claudish providers")} ${yellow("[--json]")} Show provider credential status (no key material)${green("claudish quota")} ${yellow("[provider]")} Show remaining quota/usage (alias: usage)${green("claudish serve")} ${yellow("--port <n> --models <p>")} Run the Claude Desktop redirect gateway+ ${green("claudish subscription models antigravity")} ${yellow("--raw --json")}+ Print the raw Antigravity fetchAvailableModels+ ${dim("body as one JSON line (for models-index)")}${green("claudish update")} Check for updates and install the latest version${bold("Profiles:")}diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.tsindex abe4a9b9..dc757d4f 100644--- a/packages/cli/src/index.ts+++ b/packages/cli/src/index.ts@@ -99,6 +99,7 @@ function classifyStartupKind(): string {"stats","providers","keychain",+ "subscription","login","logout","quota",@@ -407,6 +408,8 @@ const isServeCommand = firstPositional === "serve";const isProvidersCommand = firstPositional === "providers";// Keychain subcommand: claudish keychain status|list|import|set|rm|enable|disableconst isKeychainCommand = firstPositional === "keychain";+// Subscription subcommand: claudish subscription models antigravity --raw --json+const isSubscriptionCommand = firstPositional === "subscription";// Hooks subcommand: claudish hooks list|check|init|corpus|telemetryconst isHooksCommand = firstPositional === "hooks";// Team subcommand: claudish team run|run-and-judge (multi-model orchestration)@@ -501,6 +504,19 @@ if (isMcpMode) {process.exit(1);}));+} else if (isSubscriptionCommand) {+ // Raw subscription model list for models-index: one JSON line on stdout.+ // Exit explicitly: an unkilled `security` past its deadline would keep the+ // loop alive. The catch prints a FIXED line, never the error: its message can+ // carry vendor text.+ const subscriptionArgIndex = args.indexOf("subscription");+ import("./subscription-models-command.js")+ .then((m) => m.subscriptionModelsCommand(args.slice(subscriptionArgIndex + 1)))+ .then((code) => process.exit(code))+ .catch(() => {+ process.stderr.write("[claudish subscription] internal error\n");+ process.exit(1);+ });} else if (isLoginCommand) {// Auth login subcommand: claudish login [provider]const loginProviderArg = args.find((a, i) => i > args.indexOf("login") && !a.startsWith("-"));diff --git a/packages/cli/src/pane/pane-launch.ts b/packages/cli/src/pane/pane-launch.tsindex 79f21600..16822549 100644--- a/packages/cli/src/pane/pane-launch.ts+++ b/packages/cli/src/pane/pane-launch.ts@@ -340,6 +340,7 @@ export const SUBCOMMAND_WORDS = ["serve","providers","keychain",+ "subscription","hooks","team","login",diff --git a/packages/cli/src/subscription-models-command.ts b/packages/cli/src/subscription-models-command.tsnew file mode 100644index 00000000..7114158e--- /dev/null+++ b/packages/cli/src/subscription-models-command.ts@@ -0,0 +1,553 @@+/**+ * `claudish subscription models antigravity --raw --json`+ *+ * Prints the body of Antigravity's `fetchAvailableModels` exactly as the+ * backend sent it, inside one JSON envelope on ONE stdout line. The caller is+ * the models-index wire-probe runner: a macOS LaunchAgent with no TTY and no+ * browser. It uploads stdout, and models-index seals the body as evidence of+ * what this account's subscription serves.+ *+ * Everything reuses the claudish Antigravity path. Nothing is re-implemented:+ * readAntigravityStoreClassified bounded keychain read, never kills `security`+ * getValidAntigravityAccessToken refresh through `agy models` when expired+ * setupAntigravityUser loadCodeAssist -> project+ * fetchAntigravityAvailableModelsRaw the body, before any processing+ *+ * Contract (ai-docs/architecture/providers/antigravity.md, "Raw model list for+ * models-index"):+ * - stdout carries exactly one JSON object built from the typed envelope, and+ * nothing else. An error message is never printed: messages embed vendor+ * text and agy's stderr.+ * - A field appears exactly when this run determined it.+ * - A non-2xx `fetchAvailableModels` answer is not a failure (exit 0, no body).+ * - Each failure carries a fixed `class` and `code`, and the class decides the+ * exit code.+ * - Before anything is written, the line is checked for the access token, the+ * refresh token and the project id. A hit writes nothing to stdout and exits 1.+ * - No prompt: stdin is never read, no browser path is reachable, claudish+ * never writes the keychain, and every wait is bounded.+ */++import {+ type AgyRefreshOutcome,+ type AntigravityStoreRead,+ type AntigravityTokenDeps,+ defaultAntigravityTokenDeps,+ getValidAntigravityAccessToken,+ readAntigravityStoreClassified,+ readSharedAntigravityToken,+} from "./auth/antigravity-token.js";+import {+ type AntigravityClientVersionSource,+ type FetchAvailableModelsRaw,+ fetchAntigravityAvailableModelsRaw,+ resolveAntigravityClientVersion,+ setupAntigravityUser,+} from "./auth/antigravity-user.js";+import type { CredentialFailureFields } from "./auth/credentials/credential-error.js";+import { VERSION } from "./version.js";++// ---------------------------------------------------------------------------+// Contract+// ---------------------------------------------------------------------------++export const SUBSCRIPTION_MODEL_LIST_SCHEMA = "claudish.subscription-model-list.v1" as const;++export type FailureClass =+ | "credential_missing"+ | "keychain_locked"+ | "mint_failed"+ | "network"+ | "list_incomplete";++/** Every fixed `failure.code`, grouped by the class it belongs to. */+export const FAILURE_CODES = {+ credential_missing: ["platform_unsupported", "keychain_item_absent", "token_unparseable"],+ keychain_locked: ["keychain_locked", "keychain_read_timeout", "keychain_read_failed"],+ mint_failed: [+ "agy_missing",+ "agy_refresh_failed",+ "agy_refresh_timeout",+ "agy_refresh_no_valid_token",+ "load_code_assist_4xx",+ "load_code_assist_no_project",+ "load_code_assist_bad_body",+ ],+ network: [+ "load_code_assist_network_error",+ "load_code_assist_429",+ "load_code_assist_5xx",+ "load_code_assist_timeout",+ "fetch_available_models_network_error",+ "fetch_available_models_timeout",+ ],+ list_incomplete: ["models_absent", "models_empty", "body_not_json"],+} as const satisfies Record<FailureClass, readonly string[]>;++export type FailureCode = (typeof FAILURE_CODES)[FailureClass][number];++/**+ * Exit codes. 0 includes a non-2xx `fetchAvailableModels` answer. 1 and 2 are+ * outside the contract, and stdout is empty for both.+ */+export const EXIT = {+ ok: 0,+ internal: 1,+ usage: 2,+ credential_missing: 3,+ keychain_locked: 4,+ mint_failed: 5,+ network: 6,+ list_incomplete: 7,+} as const satisfies Record<FailureClass | "ok" | "internal" | "usage", number>;++export interface SubscriptionModelListFailure {+ class: FailureClass;+ code: FailureCode;+ /** The HTTP status the failure was read from, when one exists. */+ httpStatus?: number;+}++/**+ * The one object on stdout. Key order on the wire is the order below. Optional+ * fields appear exactly when this run determined them.+ */+export interface SubscriptionModelListEnvelope {+ schema: typeof SUBSCRIPTION_MODEL_LIST_SCHEMA;+ provider: "antigravity";+ claudishVersion: string;+ /** ISO-8601 UTC, once `fetchAvailableModels` answered. */+ fetchedAt?: string;+ /** `<host>/v1internal:fetchAvailableModels`, userinfo stripped, once it was attempted. */+ sourceUrl?: string;+ /** The client version in the User-Agent, once a valid token was obtained. */+ clientVersion?: string;+ clientVersionSource?: AntigravityClientVersionSource;+ /** Whether this run asked agy to refresh (true even if that refresh failed). */+ tokenRefreshed: boolean;+ /** `fetchAvailableModels` status, once it answered. */+ httpStatus?: number;+ failure?: SubscriptionModelListFailure;+ /** The parsed 2xx body, verbatim. Success only. */+ body?: unknown;+}++/** The fixed usage line (stderr, exit 2). */+export const SUBSCRIPTION_MODELS_USAGE =+ "usage: claudish subscription models antigravity --raw --json";+/** The fixed line when the secret guard trips (stderr, exit 1). */+export const SECRET_GUARD_MESSAGE =+ "[claudish subscription] refused: output contained a credential value";+/** The fixed line for any unexpected error (stderr, exit 1). */+export const INTERNAL_ERROR_MESSAGE = "[claudish subscription] internal error";++// ---------------------------------------------------------------------------+// Deps+// ---------------------------------------------------------------------------++export interface SubscriptionModelsDeps {+ /** `process.platform`. */+ platform: () => string;+ /** The bounded, classified keychain read. */+ readStore: (deadlineMs: number) => Promise<AntigravityStoreRead>;+ /** The token module's deps. The command wraps `readStore` and `runAgyRefresh`. */+ tokenDeps: () => AntigravityTokenDeps;+ /** Clock for `fetchedAt`. */+ now: () => number;+ /** Bound on `loadCodeAssist` and on `fetchAvailableModels` (30 s). */+ httpTimeoutMs: number;+ /** Bound on the keychain read (10 s). */+ keychainDeadlineMs: number;+ /**+ * Write one complete line (its trailing newline included) to stdout. Resolves+ * only once the line is flushed: a 200 KB body is larger than a pipe buffer,+ * and `process.exit` right after an unawaited write cuts it at 65,536 bytes.+ */+ writeStdout: (line: string) => Promise<void>;+ /** Write one complete line (its trailing newline included) to stderr. */+ writeStderr: (line: string) => void;+}++export function defaultSubscriptionModelsDeps(): SubscriptionModelsDeps {+ return {+ platform: () => process.platform,+ readStore: readAntigravityStoreClassified,+ tokenDeps: defaultAntigravityTokenDeps,+ now: () => Date.now(),+ httpTimeoutMs: 30_000,+ keychainDeadlineMs: 10_000,+ writeStdout: (line) =>+ new Promise<void>((resolve, reject) => {+ process.stdout.write(line, (err) => (err ? reject(err) : resolve()));+ }),+ writeStderr: (line) => {+ process.stderr.write(line);+ },+ };+}++// ---------------------------------------------------------------------------+// Argv+// ---------------------------------------------------------------------------++export type SubscriptionModelsArgs = { ok: true; provider: "antigravity" } | { ok: false };++/**+ * Parse the argv that follows `subscription`. v1 accepts exactly+ * `models antigravity` plus `--raw` and `--json`, each once, in any order.+ * Anything else is a usage error: a missing flag, the `ag` shortcut, or an+ * extra token. That leaves the bare form free for a later human view.+ */+export function parseSubscriptionModelsArgs(argv: readonly string[]): SubscriptionModelsArgs {+ const positionals = argv.filter((a) => !a.startsWith("-"));+ const flags = argv.filter((a) => a.startsWith("-"));+ const positionalsOk =+ positionals.length === 2 && positionals[0] === "models" && positionals[1] === "antigravity";+ const flagsOk = flags.length === 2 && flags.includes("--raw") && flags.includes("--json");+ return positionalsOk && flagsOk ? { ok: true, provider: "antigravity" } : { ok: false };+}++// ---------------------------------------------------------------------------+// Run+// ---------------------------------------------------------------------------++/** What this run has determined so far: the envelope's optional fields. */+interface Facts {+ fetchedAt?: string;+ sourceUrl?: string;+ clientVersion?: string;+ clientVersionSource?: AntigravityClientVersionSource;+ tokenRefreshed: boolean;+ httpStatus?: number;+}++/** Values the secret guard checks the output line for. */+interface Secrets {+ values: Set<string>;+}++function addSecret(secrets: Secrets, value: string | null | undefined): void {+ if (typeof value === "string" && value.length > 0) secrets.values.add(value);+}++/** Build the envelope with its keys in contract order, omitting what is unknown. */+function buildEnvelope(+ facts: Facts,+ failure?: SubscriptionModelListFailure,+ body?: { value: unknown }+): SubscriptionModelListEnvelope {+ return {+ schema: SUBSCRIPTION_MODEL_LIST_SCHEMA,+ provider: "antigravity",+ claudishVersion: VERSION,+ ...(facts.fetchedAt !== undefined ? { fetchedAt: facts.fetchedAt } : {}),+ ...(facts.sourceUrl !== undefined ? { sourceUrl: facts.sourceUrl } : {}),+ ...(facts.clientVersion !== undefined ? { clientVersion: facts.clientVersion } : {}),+ ...(facts.clientVersionSource !== undefined+ ? { clientVersionSource: facts.clientVersionSource }+ : {}),+ tokenRefreshed: facts.tokenRefreshed,+ ...(facts.httpStatus !== undefined ? { httpStatus: facts.httpStatus } : {}),+ ...(failure ? { failure } : {}),+ ...(body ? { body: body.value } : {}),+ };+}++function failureOf(+ cls: FailureClass,+ code: FailureCode,+ httpStatus?: number+): SubscriptionModelListFailure {+ return { class: cls, code, ...(httpStatus !== undefined ? { httpStatus } : {}) };+}++/** `url` without userinfo, so a credential in `AICODE_ENDPOINT_URL` is never printed. */+function stripUserinfo(url: string): string {+ try {+ const parsed = new URL(url);+ parsed.username = "";+ parsed.password = "";+ return parsed.toString();+ } catch {+ return url.replace(/^([a-z][a-z0-9+.-]*:\/\/)[^@/]*@/i, "$1");+ }+}++/** The class of a keychain read that produced no value. */+function storeReadFailure(+ read: Exclude<AntigravityStoreRead, { kind: "present" }>+): SubscriptionModelListFailure {+ switch (read.kind) {+ case "absent":+ return failureOf("credential_missing", "keychain_item_absent");+ case "locked":+ return failureOf("keychain_locked", "keychain_locked");+ case "timeout":+ return failureOf("keychain_locked", "keychain_read_timeout");+ default:+ return failureOf("keychain_locked", "keychain_read_failed");+ }+}++/** The class of a token failure, from what was OBSERVED, never from message text. */+function tokenFailure(+ refreshCalled: boolean,+ outcome: AgyRefreshOutcome | null+): SubscriptionModelListFailure {+ if (!refreshCalled) return failureOf("credential_missing", "token_unparseable");+ switch (outcome?.kind) {+ case "not-installed":+ return failureOf("mint_failed", "agy_missing");+ case "timeout":+ return failureOf("mint_failed", "agy_refresh_timeout");+ case "ran":+ return failureOf("mint_failed", "agy_refresh_no_valid_token");+ default:+ // "failed", or a refresh that threw before reporting an outcome.+ return failureOf("mint_failed", "agy_refresh_failed");+ }+}++/** The class of a `setupAntigravityUser` failure, from its fields. */+function loadCodeAssistFailure(err: unknown): SubscriptionModelListFailure {+ const fields = (err ?? {}) as CredentialFailureFields;+ const status = typeof fields.upstreamStatus === "number" ? fields.upstreamStatus : undefined;+ if (status !== undefined) {+ if (status === 429) return failureOf("network", "load_code_assist_429", status);+ if (status >= 500 && status <= 599) return failureOf("network", "load_code_assist_5xx", status);+ return failureOf("mint_failed", "load_code_assist_4xx", status);+ }+ // A 2xx body that is not JSON: `Response.json()` throws SyntaxError (Phase 0, M4).+ if (err instanceof SyntaxError) return failureOf("mint_failed", "load_code_assist_bad_body");+ // No project in a 2xx answer: the only terminal error without a status.+ if (fields.terminal === true) return failureOf("mint_failed", "load_code_assist_no_project");+ return failureOf("network", "load_code_assist_network_error");+}++/** The class of a 2xx `fetchAvailableModels` JSON body, or null when it lists models. */+function listFailure(value: unknown, status: number): SubscriptionModelListFailure | null {+ const models =+ value !== null && typeof value === "object" && !Array.isArray(value)+ ? (value as { models?: unknown }).models+ : undefined;+ if (models === null || typeof models !== "object" || Array.isArray(models)) {+ return failureOf("list_incomplete", "models_absent", status);+ }+ if (Object.keys(models).length === 0) {+ return failureOf("list_incomplete", "models_empty", status);+ }+ return null;+}++/** Race `work` against `ms`. The loser keeps running; its rejection is absorbed. */+async function raceTimeout<T>(work: Promise<T>, ms: number): Promise<T | typeof TIMED_OUT> {+ let timer: ReturnType<typeof setTimeout> | undefined;+ const timedOut = new Promise<typeof TIMED_OUT>((resolve) => {+ timer = setTimeout(() => resolve(TIMED_OUT), ms);+ });+ work.catch(() => {});+ try {+ return await Promise.race([work, timedOut]);+ } finally {+ if (timer) clearTimeout(timer);+ }+}+const TIMED_OUT: unique symbol = Symbol("timed-out");++interface RunResult {+ envelope: SubscriptionModelListEnvelope;+ exitCode: number;+}++function fail(facts: Facts, failure: SubscriptionModelListFailure): RunResult {+ return { envelope: buildEnvelope(facts, failure), exitCode: EXIT[failure.class] };+}++type TokenStep = { ok: true; accessToken: string } | { ok: false; result: RunResult };++/**+ * Step 4: a valid access token, through the token module with observed deps.+ *+ * The first `readStore` call returns the bounded pre-read, so the token module+ * never spawns an unbounded `security` for it. Later reads (the re-read after a+ * refresh) go to the real store and are kept for the secret guard. The refresh+ * is recorded: whether it was called, and what it reported.+ */+async function obtainAccessToken(+ deps: SubscriptionModelsDeps,+ facts: Facts,+ secrets: Secrets,+ preRaw: string+): Promise<TokenStep> {+ const base = deps.tokenDeps();+ let firstRead = true;+ let refreshCalled = false;+ let refreshOutcome: AgyRefreshOutcome | null = null;+ const laterReads: string[] = [];+ const observed: AntigravityTokenDeps = {+ ...base,+ readStore: () => {+ if (firstRead) {+ firstRead = false;+ return preRaw;+ }+ const raw = base.readStore();+ if (raw) laterReads.push(raw);+ return raw;+ },+ runAgyRefresh: () => {+ refreshCalled = true;+ facts.tokenRefreshed = true;+ refreshOutcome = base.runAgyRefresh();+ return refreshOutcome;+ },+ };++ let accessToken: string;+ try {+ accessToken = await getValidAntigravityAccessToken(observed);+ } catch {+ // A failure envelope holds only fixed strings, and `preRaw` is already guarded.+ return { ok: false, result: fail(facts, tokenFailure(refreshCalled, refreshOutcome)) };+ }+ addSecret(secrets, accessToken);+ // Every refresh token this run saw is a secret, whichever read it came from.+ // Parsed only on success: every record read on this path has already parsed,+ // so this logs nothing a second time.+ for (const raw of [preRaw, ...laterReads]) {+ addSecret(secrets, raw);+ addSecret(+ secrets,+ readSharedAntigravityToken({ ...base, readStore: () => raw })?.refresh_token+ );+ }+ return { ok: true, accessToken };+}++/** Step 7's verdict: the envelope and exit code for one raw `fetchAvailableModels` result. */+function classifyAvailableModels(+ facts: Facts,+ raw: FetchAvailableModelsRaw,+ timedOut: boolean+): RunResult {+ // What was SENT: the same value as step 5 unless the version changed in between.+ facts.sourceUrl = stripUserinfo(raw.url);+ facts.clientVersion = raw.clientVersion;+ facts.clientVersionSource = raw.clientVersionSource;+ const networkCode = timedOut+ ? "fetch_available_models_timeout"+ : "fetch_available_models_network_error";++ if (raw.kind === "unreachable") return fail(facts, failureOf("network", networkCode));+ facts.fetchedAt = raw.fetchedAt;+ facts.httpStatus = raw.status;++ if (raw.status < 200 || raw.status >= 300) {+ // Not a failure: the backend answered, and its status is the answer.+ return { envelope: buildEnvelope(facts), exitCode: EXIT.ok };+ }+ switch (raw.body.kind) {+ case "json": {+ const incomplete = listFailure(raw.body.value, raw.status);+ if (incomplete) return fail(facts, incomplete);+ return {+ envelope: buildEnvelope(facts, undefined, { value: raw.body.value }),+ exitCode: EXIT.ok,+ };+ }+ case "not-json":+ return fail(facts, failureOf("list_incomplete", "body_not_json", raw.status));+ default:+ // read-failed (`unread` cannot occur on a 2xx).+ return fail(facts, failureOf("network", networkCode, raw.status));+ }+}++/** Steps 2-7 of the sequence: determine the envelope and its exit code. */+async function determine(deps: SubscriptionModelsDeps, secrets: Secrets): Promise<RunResult> {+ const facts: Facts = { tokenRefreshed: false };++ if (deps.platform() !== "darwin") {+ return fail(facts, failureOf("credential_missing", "platform_unsupported"));+ }++ const read = await deps.readStore(deps.keychainDeadlineMs);+ if (read.kind !== "present") return fail(facts, storeReadFailure(read));+ addSecret(secrets, read.raw);++ const token = await obtainAccessToken(deps, facts, secrets, read.raw);+ if (!token.ok) return token.result;++ const version = resolveAntigravityClientVersion();+ facts.clientVersion = version.version;+ facts.clientVersionSource = version.source;++ let projectId: string;+ try {+ const setup = await raceTimeout(setupAntigravityUser(token.accessToken), deps.httpTimeoutMs);+ if (setup === TIMED_OUT) return fail(facts, failureOf("network", "load_code_assist_timeout"));+ projectId = setup.projectId;+ } catch (err) {+ return fail(facts, loadCodeAssistFailure(err));+ }+ addSecret(secrets, projectId);++ const controller = new AbortController();+ const timer = setTimeout(() => controller.abort(), deps.httpTimeoutMs);+ try {+ const raw = await fetchAntigravityAvailableModelsRaw(token.accessToken, projectId, {+ signal: controller.signal,+ now: deps.now,+ });+ return classifyAvailableModels(facts, raw, controller.signal.aborted);+ } finally {+ clearTimeout(timer);+ }+}++/**+ * Determine the envelope, run the secret guard, write ONE flushed line, and+ * return the exit code. Never throws. Exits 1 with empty stdout and a fixed+ * stderr line on an internal error or a secret-guard hit.+ */+export async function runAntigravityRawModelList(deps: SubscriptionModelsDeps): Promise<number> {+ const secrets: Secrets = { values: new Set() };+ let line: string;+ let exitCode: number;+ try {+ const result = await determine(deps, secrets);+ line = `${JSON.stringify(result.envelope)}\n`;+ exitCode = result.exitCode;+ } catch {+ deps.writeStderr(`${INTERNAL_ERROR_MESSAGE}\n`);+ return EXIT.internal;+ }+ for (const secret of secrets.values) {+ if (line.includes(secret)) {+ deps.writeStderr(`${SECRET_GUARD_MESSAGE}\n`);+ return EXIT.internal;+ }+ }+ try {+ await deps.writeStdout(line);+ } catch {+ deps.writeStderr(`${INTERNAL_ERROR_MESSAGE}\n`);+ return EXIT.internal;+ }+ return exitCode;+}++/**+ * Entry point for the argv that follows `subscription`. Returns the exit code;+ * the caller exits with it (an unkilled `security` would keep the loop alive).+ */+export async function subscriptionModelsCommand(+ argv: readonly string[],+ deps: SubscriptionModelsDeps = defaultSubscriptionModelsDeps()+): Promise<number> {+ if (!parseSubscriptionModelsArgs(argv).ok) {+ deps.writeStderr(`${SUBSCRIPTION_MODELS_USAGE}\n`);+ return EXIT.usage;+ }+ return runAntigravityRawModelList(deps);+}