mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 09:11:26 +00:00
refactor!: remove google-antigravity provider support
This commit is contained in:
@@ -216,17 +216,17 @@ describe("resolveProviderAuths key normalization", () => {
|
||||
it("keeps raw google token when token payload is not JSON", async () => {
|
||||
await withSuiteHome(async (home) => {
|
||||
await writeAuthProfiles(home, {
|
||||
"google-antigravity:default": {
|
||||
"google-gemini-cli:default": {
|
||||
type: "token",
|
||||
provider: "google-antigravity",
|
||||
provider: "google-gemini-cli",
|
||||
token: "plain-google-token",
|
||||
},
|
||||
});
|
||||
|
||||
const auths = await resolveProviderAuths({
|
||||
providers: ["google-antigravity"],
|
||||
providers: ["google-gemini-cli"],
|
||||
});
|
||||
expect(auths).toEqual([{ provider: "google-antigravity", token: "plain-google-token" }]);
|
||||
expect(auths).toEqual([{ provider: "google-gemini-cli", token: "plain-google-token" }]);
|
||||
}, {});
|
||||
});
|
||||
|
||||
|
||||
@@ -158,7 +158,7 @@ async function resolveOAuthToken(params: {
|
||||
});
|
||||
if (resolved) {
|
||||
let token = resolved.apiKey;
|
||||
if (params.provider === "google-gemini-cli" || params.provider === "google-antigravity") {
|
||||
if (params.provider === "google-gemini-cli") {
|
||||
const parsed = parseGoogleToken(resolved.apiKey);
|
||||
token = parsed?.token ?? resolved.apiKey;
|
||||
}
|
||||
@@ -188,7 +188,6 @@ function resolveOAuthProviders(agentDir?: string): UsageProviderId[] {
|
||||
"anthropic",
|
||||
"github-copilot",
|
||||
"google-gemini-cli",
|
||||
"google-antigravity",
|
||||
"openai-codex",
|
||||
] satisfies UsageProviderId[];
|
||||
const isOAuthLikeCredential = (id: string) => {
|
||||
|
||||
@@ -1,469 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createProviderUsageFetch, makeResponse } from "../test-utils/provider-usage-fetch.js";
|
||||
import { fetchAntigravityUsage } from "./provider-usage.fetch.antigravity.js";
|
||||
|
||||
const getRequestBody = (init?: Parameters<typeof fetch>[1]) =>
|
||||
typeof init?.body === "string" ? init.body : undefined;
|
||||
|
||||
type EndpointHandler = (init?: Parameters<typeof fetch>[1]) => Promise<Response> | Response;
|
||||
|
||||
function createEndpointFetch(spec: {
|
||||
loadCodeAssist?: EndpointHandler;
|
||||
fetchAvailableModels?: EndpointHandler;
|
||||
}) {
|
||||
return createProviderUsageFetch(async (url, init) => {
|
||||
if (url.includes("loadCodeAssist")) {
|
||||
return (await spec.loadCodeAssist?.(init)) ?? makeResponse(404, "not found");
|
||||
}
|
||||
if (url.includes("fetchAvailableModels")) {
|
||||
return (await spec.fetchAvailableModels?.(init)) ?? makeResponse(404, "not found");
|
||||
}
|
||||
return makeResponse(404, "not found");
|
||||
});
|
||||
}
|
||||
|
||||
async function runUsage(mockFetch: ReturnType<typeof createProviderUsageFetch>) {
|
||||
return fetchAntigravityUsage("token-123", 5000, mockFetch as unknown as typeof fetch);
|
||||
}
|
||||
|
||||
function findWindow(snapshot: Awaited<ReturnType<typeof fetchAntigravityUsage>>, label: string) {
|
||||
return snapshot.windows.find((window) => window.label === label);
|
||||
}
|
||||
|
||||
function expectTokenExpired(snapshot: Awaited<ReturnType<typeof fetchAntigravityUsage>>) {
|
||||
expect(snapshot.error).toBe("Token expired");
|
||||
expect(snapshot.windows).toHaveLength(0);
|
||||
}
|
||||
|
||||
function expectSingleWindow(
|
||||
snapshot: Awaited<ReturnType<typeof fetchAntigravityUsage>>,
|
||||
label: string,
|
||||
) {
|
||||
expect(snapshot.windows).toHaveLength(1);
|
||||
expect(snapshot.windows[0]?.label).toBe(label);
|
||||
return snapshot.windows[0];
|
||||
}
|
||||
|
||||
describe("fetchAntigravityUsage", () => {
|
||||
it("returns 3 windows when both endpoints succeed", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () =>
|
||||
makeResponse(200, {
|
||||
availablePromptCredits: 750,
|
||||
planInfo: { monthlyPromptCredits: 1000 },
|
||||
planType: "Standard",
|
||||
currentTier: { id: "tier1", name: "Standard Tier" },
|
||||
}),
|
||||
fetchAvailableModels: () =>
|
||||
makeResponse(200, {
|
||||
models: {
|
||||
"gemini-pro-1.5": {
|
||||
quotaInfo: {
|
||||
remainingFraction: 0.6,
|
||||
resetTime: "2026-01-08T00:00:00Z",
|
||||
isExhausted: false,
|
||||
},
|
||||
},
|
||||
"gemini-flash-2.0": {
|
||||
quotaInfo: {
|
||||
remainingFraction: 0.8,
|
||||
resetTime: "2026-01-08T00:00:00Z",
|
||||
isExhausted: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
|
||||
expect(snapshot.provider).toBe("google-antigravity");
|
||||
expect(snapshot.displayName).toBe("Antigravity");
|
||||
expect(snapshot.windows).toHaveLength(3);
|
||||
expect(snapshot.plan).toBe("Standard Tier");
|
||||
expect(snapshot.error).toBeUndefined();
|
||||
|
||||
const creditsWindow = findWindow(snapshot, "Credits");
|
||||
expect(creditsWindow?.usedPercent).toBe(25); // (1000 - 750) / 1000 * 100
|
||||
|
||||
const proWindow = findWindow(snapshot, "gemini-pro-1.5");
|
||||
expect(proWindow?.usedPercent).toBe(40); // (1 - 0.6) * 100
|
||||
expect(proWindow?.resetAt).toBe(new Date("2026-01-08T00:00:00Z").getTime());
|
||||
|
||||
const flashWindow = findWindow(snapshot, "gemini-flash-2.0");
|
||||
expect(flashWindow?.usedPercent).toBeCloseTo(20, 1); // (1 - 0.8) * 100
|
||||
expect(flashWindow?.resetAt).toBe(new Date("2026-01-08T00:00:00Z").getTime());
|
||||
|
||||
expect(mockFetch).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("returns Credits only when loadCodeAssist succeeds but fetchAvailableModels fails", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () =>
|
||||
makeResponse(200, {
|
||||
availablePromptCredits: 250,
|
||||
planInfo: { monthlyPromptCredits: 1000 },
|
||||
currentTier: { name: "Free" },
|
||||
}),
|
||||
fetchAvailableModels: () => makeResponse(403, { error: { message: "Permission denied" } }),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
|
||||
expect(snapshot.provider).toBe("google-antigravity");
|
||||
expect(snapshot.windows).toHaveLength(1);
|
||||
expect(snapshot.plan).toBe("Free");
|
||||
expect(snapshot.error).toBeUndefined();
|
||||
|
||||
const creditsWindow = snapshot.windows[0];
|
||||
expect(creditsWindow?.label).toBe("Credits");
|
||||
expect(creditsWindow?.usedPercent).toBe(75); // (1000 - 250) / 1000 * 100
|
||||
|
||||
expect(mockFetch).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("returns model IDs when fetchAvailableModels succeeds but loadCodeAssist fails", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () => makeResponse(500, "Internal server error"),
|
||||
fetchAvailableModels: () =>
|
||||
makeResponse(200, {
|
||||
models: {
|
||||
"gemini-pro-1.5": {
|
||||
quotaInfo: { remainingFraction: 0.5, resetTime: "2026-01-08T00:00:00Z" },
|
||||
},
|
||||
"gemini-flash-2.0": {
|
||||
quotaInfo: { remainingFraction: 0.7, resetTime: "2026-01-08T00:00:00Z" },
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
|
||||
expect(snapshot.provider).toBe("google-antigravity");
|
||||
expect(snapshot.windows).toHaveLength(2);
|
||||
expect(snapshot.error).toBeUndefined();
|
||||
|
||||
const proWindow = findWindow(snapshot, "gemini-pro-1.5");
|
||||
expect(proWindow?.usedPercent).toBe(50); // (1 - 0.5) * 100
|
||||
|
||||
const flashWindow = findWindow(snapshot, "gemini-flash-2.0");
|
||||
expect(flashWindow?.usedPercent).toBeCloseTo(30, 1); // (1 - 0.7) * 100
|
||||
|
||||
expect(mockFetch).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
name: "uses cloudaicompanionProject string as project id",
|
||||
project: "projects/alpha",
|
||||
expectedBody: JSON.stringify({ project: "projects/alpha" }),
|
||||
},
|
||||
{
|
||||
name: "uses cloudaicompanionProject object id when present",
|
||||
project: { id: "projects/beta" },
|
||||
expectedBody: JSON.stringify({ project: "projects/beta" }),
|
||||
},
|
||||
])("project payload: $name", async ({ project, expectedBody }) => {
|
||||
let capturedBody: string | undefined;
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () =>
|
||||
makeResponse(200, {
|
||||
availablePromptCredits: 900,
|
||||
planInfo: { monthlyPromptCredits: 1000 },
|
||||
cloudaicompanionProject: project,
|
||||
}),
|
||||
fetchAvailableModels: (init) => {
|
||||
capturedBody = getRequestBody(init);
|
||||
return makeResponse(200, { models: {} });
|
||||
},
|
||||
});
|
||||
|
||||
await runUsage(mockFetch);
|
||||
expect(capturedBody).toBe(expectedBody);
|
||||
});
|
||||
|
||||
it("returns error snapshot when both endpoints fail", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () => makeResponse(403, { error: { message: "Access denied" } }),
|
||||
fetchAvailableModels: () => makeResponse(403, "Forbidden"),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
|
||||
expect(snapshot.provider).toBe("google-antigravity");
|
||||
expect(snapshot.windows).toHaveLength(0);
|
||||
expect(snapshot.error).toBe("Access denied");
|
||||
|
||||
expect(mockFetch).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("returns Token expired when fetchAvailableModels returns 401 and no windows", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () => makeResponse(500, "Boom"),
|
||||
fetchAvailableModels: () => makeResponse(401, { error: { message: "Unauthorized" } }),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
expectTokenExpired(snapshot);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
name: "extracts plan info from currentTier.name",
|
||||
loadCodeAssist: {
|
||||
availablePromptCredits: 500,
|
||||
planInfo: { monthlyPromptCredits: 1000 },
|
||||
planType: "Basic",
|
||||
currentTier: { id: "tier2", name: "Premium Tier" },
|
||||
},
|
||||
expectedPlan: "Premium Tier",
|
||||
},
|
||||
{
|
||||
name: "falls back to planType when currentTier.name is missing",
|
||||
loadCodeAssist: {
|
||||
availablePromptCredits: 500,
|
||||
planInfo: { monthlyPromptCredits: 1000 },
|
||||
planType: "Basic Plan",
|
||||
},
|
||||
expectedPlan: "Basic Plan",
|
||||
},
|
||||
])("plan label: $name", async ({ loadCodeAssist, expectedPlan }) => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () => makeResponse(200, loadCodeAssist),
|
||||
fetchAvailableModels: () => makeResponse(500, "Error"),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
expect(snapshot.plan).toBe(expectedPlan);
|
||||
});
|
||||
|
||||
it("includes reset times in model windows", async () => {
|
||||
const resetTime = "2026-01-10T12:00:00Z";
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () => makeResponse(500, "Error"),
|
||||
fetchAvailableModels: () =>
|
||||
makeResponse(200, {
|
||||
models: {
|
||||
"gemini-pro-experimental": {
|
||||
quotaInfo: { remainingFraction: 0.3, resetTime },
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
const proWindow = snapshot.windows.find((w) => w.label === "gemini-pro-experimental");
|
||||
expect(proWindow?.resetAt).toBe(new Date(resetTime).getTime());
|
||||
});
|
||||
|
||||
it("parses string numbers correctly", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () =>
|
||||
makeResponse(200, {
|
||||
availablePromptCredits: "600",
|
||||
planInfo: { monthlyPromptCredits: "1000" },
|
||||
}),
|
||||
fetchAvailableModels: () =>
|
||||
makeResponse(200, {
|
||||
models: {
|
||||
"gemini-flash-lite": {
|
||||
quotaInfo: { remainingFraction: "0.9" },
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
expect(snapshot.windows).toHaveLength(2);
|
||||
|
||||
const creditsWindow = snapshot.windows.find((w) => w.label === "Credits");
|
||||
expect(creditsWindow?.usedPercent).toBe(40); // (1000 - 600) / 1000 * 100
|
||||
|
||||
const flashWindow = snapshot.windows.find((w) => w.label === "gemini-flash-lite");
|
||||
expect(flashWindow?.usedPercent).toBeCloseTo(10, 1); // (1 - 0.9) * 100
|
||||
});
|
||||
|
||||
it("skips internal models", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () =>
|
||||
makeResponse(200, {
|
||||
availablePromptCredits: 500,
|
||||
planInfo: { monthlyPromptCredits: 1000 },
|
||||
cloudaicompanionProject: "projects/internal",
|
||||
}),
|
||||
fetchAvailableModels: () =>
|
||||
makeResponse(200, {
|
||||
models: {
|
||||
chat_hidden: { quotaInfo: { remainingFraction: 0.1 } },
|
||||
tab_hidden: { quotaInfo: { remainingFraction: 0.2 } },
|
||||
"gemini-pro-1.5": { quotaInfo: { remainingFraction: 0.7 } },
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
expect(snapshot.windows.map((w) => w.label)).toEqual(["Credits", "gemini-pro-1.5"]);
|
||||
});
|
||||
|
||||
it("sorts models by usage and shows individual model IDs", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () => makeResponse(500, "Error"),
|
||||
fetchAvailableModels: () =>
|
||||
makeResponse(200, {
|
||||
models: {
|
||||
"gemini-pro-1.0": { quotaInfo: { remainingFraction: 0.8 } },
|
||||
"gemini-pro-1.5": { quotaInfo: { remainingFraction: 0.3 } },
|
||||
"gemini-flash-1.5": { quotaInfo: { remainingFraction: 0.6 } },
|
||||
"gemini-flash-2.0": { quotaInfo: { remainingFraction: 0.9 } },
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
expect(snapshot.windows).toHaveLength(4);
|
||||
expect(snapshot.windows[0]?.label).toBe("gemini-pro-1.5");
|
||||
expect(snapshot.windows[0]?.usedPercent).toBe(70); // (1 - 0.3) * 100
|
||||
expect(snapshot.windows[1]?.label).toBe("gemini-flash-1.5");
|
||||
expect(snapshot.windows[1]?.usedPercent).toBe(40); // (1 - 0.6) * 100
|
||||
expect(snapshot.windows[2]?.label).toBe("gemini-pro-1.0");
|
||||
expect(snapshot.windows[2]?.usedPercent).toBeCloseTo(20, 1); // (1 - 0.8) * 100
|
||||
expect(snapshot.windows[3]?.label).toBe("gemini-flash-2.0");
|
||||
expect(snapshot.windows[3]?.usedPercent).toBeCloseTo(10, 1); // (1 - 0.9) * 100
|
||||
});
|
||||
|
||||
it("returns Token expired error on 401 from loadCodeAssist", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () => makeResponse(401, { error: { message: "Unauthorized" } }),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
expectTokenExpired(snapshot);
|
||||
expect(mockFetch).toHaveBeenCalledTimes(1); // Should stop early on 401
|
||||
});
|
||||
|
||||
it("handles empty models object gracefully", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () =>
|
||||
makeResponse(200, {
|
||||
availablePromptCredits: 800,
|
||||
planInfo: { monthlyPromptCredits: 1000 },
|
||||
}),
|
||||
fetchAvailableModels: () => makeResponse(200, { models: {} }),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
expect(snapshot.windows).toHaveLength(1);
|
||||
const creditsWindow = snapshot.windows[0];
|
||||
expect(creditsWindow?.label).toBe("Credits");
|
||||
expect(creditsWindow?.usedPercent).toBe(20);
|
||||
});
|
||||
|
||||
it("handles missing or invalid model quota payloads", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () => makeResponse(500, "Error"),
|
||||
fetchAvailableModels: () =>
|
||||
makeResponse(200, {
|
||||
models: {
|
||||
no_quota: {},
|
||||
missing_fraction: { quotaInfo: {} },
|
||||
invalid_fraction: { quotaInfo: { remainingFraction: "oops" } },
|
||||
valid_model: { quotaInfo: { remainingFraction: 0.25 } },
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
expect(snapshot.windows).toEqual([{ label: "valid_model", usedPercent: 75 }]);
|
||||
});
|
||||
|
||||
it("handles non-object models payload gracefully", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () =>
|
||||
makeResponse(200, {
|
||||
availablePromptCredits: 900,
|
||||
planInfo: { monthlyPromptCredits: 1000 },
|
||||
}),
|
||||
fetchAvailableModels: () => makeResponse(200, { models: null }),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
expect(snapshot.windows).toEqual([{ label: "Credits", usedPercent: 10 }]);
|
||||
});
|
||||
|
||||
it("handles missing credits fields gracefully", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () => makeResponse(200, { planType: "Free" }),
|
||||
fetchAvailableModels: () =>
|
||||
makeResponse(200, {
|
||||
models: {
|
||||
"gemini-flash-experimental": {
|
||||
quotaInfo: { remainingFraction: 0.5 },
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
const flashWindow = expectSingleWindow(snapshot, "gemini-flash-experimental");
|
||||
expect(flashWindow?.usedPercent).toBe(50);
|
||||
expect(snapshot.plan).toBe("Free");
|
||||
});
|
||||
|
||||
it("handles invalid reset time gracefully", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () => makeResponse(500, "Error"),
|
||||
fetchAvailableModels: () =>
|
||||
makeResponse(200, {
|
||||
models: {
|
||||
"gemini-pro-test": {
|
||||
quotaInfo: { remainingFraction: 0.4, resetTime: "invalid-date" },
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
const proWindow = snapshot.windows.find((w) => w.label === "gemini-pro-test");
|
||||
expect(proWindow?.usedPercent).toBe(60);
|
||||
expect(proWindow?.resetAt).toBeUndefined();
|
||||
});
|
||||
|
||||
it("handles loadCodeAssist network errors with graceful degradation", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () => {
|
||||
throw new Error("Network failure");
|
||||
},
|
||||
fetchAvailableModels: () =>
|
||||
makeResponse(200, {
|
||||
models: {
|
||||
"gemini-flash-stable": {
|
||||
quotaInfo: { remainingFraction: 0.85 },
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
const flashWindow = expectSingleWindow(snapshot, "gemini-flash-stable");
|
||||
expect(flashWindow?.usedPercent).toBeCloseTo(15, 1);
|
||||
expect(snapshot.error).toBeUndefined();
|
||||
});
|
||||
|
||||
it("handles fetchAvailableModels network errors with graceful degradation", async () => {
|
||||
const mockFetch = createEndpointFetch({
|
||||
loadCodeAssist: () =>
|
||||
makeResponse(200, {
|
||||
availablePromptCredits: 300,
|
||||
planInfo: { monthlyPromptCredits: 1000 },
|
||||
}),
|
||||
fetchAvailableModels: () => {
|
||||
throw new Error("Network failure");
|
||||
},
|
||||
});
|
||||
|
||||
const snapshot = await runUsage(mockFetch);
|
||||
expect(snapshot.windows).toEqual([{ label: "Credits", usedPercent: 70 }]);
|
||||
expect(snapshot.error).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -1,305 +0,0 @@
|
||||
import { logDebug } from "../logger.js";
|
||||
import { fetchJson, parseFiniteNumber } from "./provider-usage.fetch.shared.js";
|
||||
import { clampPercent, PROVIDER_LABELS } from "./provider-usage.shared.js";
|
||||
import type { ProviderUsageSnapshot, UsageWindow } from "./provider-usage.types.js";
|
||||
|
||||
type LoadCodeAssistResponse = {
|
||||
availablePromptCredits?: number | string;
|
||||
planInfo?: { monthlyPromptCredits?: number | string };
|
||||
planType?: string;
|
||||
currentTier?: { id?: string; name?: string };
|
||||
cloudaicompanionProject?: string | { id?: string };
|
||||
};
|
||||
|
||||
type FetchAvailableModelsResponse = {
|
||||
models?: Record<
|
||||
string,
|
||||
{
|
||||
displayName?: string;
|
||||
quotaInfo?: {
|
||||
remainingFraction?: number | string;
|
||||
resetTime?: string;
|
||||
isExhausted?: boolean;
|
||||
};
|
||||
}
|
||||
>;
|
||||
};
|
||||
|
||||
type ModelQuota = {
|
||||
remainingFraction: number;
|
||||
resetTime?: number;
|
||||
};
|
||||
|
||||
type CreditsInfo = {
|
||||
available: number;
|
||||
monthly: number;
|
||||
};
|
||||
|
||||
const BASE_URL = "https://cloudcode-pa.googleapis.com";
|
||||
const LOAD_CODE_ASSIST_PATH = "/v1internal:loadCodeAssist";
|
||||
const FETCH_AVAILABLE_MODELS_PATH = "/v1internal:fetchAvailableModels";
|
||||
|
||||
const METADATA = {
|
||||
ideType: "ANTIGRAVITY",
|
||||
platform: "PLATFORM_UNSPECIFIED",
|
||||
pluginType: "GEMINI",
|
||||
};
|
||||
|
||||
function parseNumber(value: number | string | undefined): number | undefined {
|
||||
return parseFiniteNumber(value);
|
||||
}
|
||||
|
||||
function parseEpochMs(isoString: string | undefined): number | undefined {
|
||||
if (!isoString?.trim()) {
|
||||
return undefined;
|
||||
}
|
||||
try {
|
||||
const ms = Date.parse(isoString);
|
||||
if (Number.isFinite(ms)) {
|
||||
return ms;
|
||||
}
|
||||
} catch {
|
||||
// ignore parse errors
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function parseErrorMessage(res: Response): Promise<string> {
|
||||
try {
|
||||
const data = (await res.json()) as { error?: { message?: string } };
|
||||
const message = data?.error?.message?.trim();
|
||||
if (message) {
|
||||
return message;
|
||||
}
|
||||
} catch {
|
||||
// ignore parse errors
|
||||
}
|
||||
return `HTTP ${res.status}`;
|
||||
}
|
||||
|
||||
function extractCredits(data: LoadCodeAssistResponse): CreditsInfo | undefined {
|
||||
const available = parseNumber(data.availablePromptCredits);
|
||||
const monthly = parseNumber(data.planInfo?.monthlyPromptCredits);
|
||||
if (available === undefined || monthly === undefined || monthly <= 0) {
|
||||
return undefined;
|
||||
}
|
||||
return { available, monthly };
|
||||
}
|
||||
|
||||
function extractPlanInfo(data: LoadCodeAssistResponse): string | undefined {
|
||||
const tierName = data.currentTier?.name?.trim();
|
||||
if (tierName) {
|
||||
return tierName;
|
||||
}
|
||||
const planType = data.planType?.trim();
|
||||
if (planType) {
|
||||
return planType;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function extractProjectId(data: LoadCodeAssistResponse): string | undefined {
|
||||
const project = data.cloudaicompanionProject;
|
||||
if (!project) {
|
||||
return undefined;
|
||||
}
|
||||
if (typeof project === "string") {
|
||||
return project.trim() ? project : undefined;
|
||||
}
|
||||
const projectId = typeof project.id === "string" ? project.id.trim() : undefined;
|
||||
return projectId || undefined;
|
||||
}
|
||||
|
||||
function extractModelQuotas(data: FetchAvailableModelsResponse): Map<string, ModelQuota> {
|
||||
const result = new Map<string, ModelQuota>();
|
||||
if (!data.models || typeof data.models !== "object") {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (const [modelId, modelInfo] of Object.entries(data.models)) {
|
||||
const quotaInfo = modelInfo.quotaInfo;
|
||||
if (!quotaInfo) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const remainingFraction = parseNumber(quotaInfo.remainingFraction);
|
||||
if (remainingFraction === undefined) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const resetTime = parseEpochMs(quotaInfo.resetTime);
|
||||
result.set(modelId, { remainingFraction, resetTime });
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function buildUsageWindows(opts: {
|
||||
credits?: CreditsInfo;
|
||||
modelQuotas?: Map<string, ModelQuota>;
|
||||
}): UsageWindow[] {
|
||||
const windows: UsageWindow[] = [];
|
||||
|
||||
// Credits window (overall)
|
||||
if (opts.credits) {
|
||||
const { available, monthly } = opts.credits;
|
||||
const used = monthly - available;
|
||||
const usedPercent = clampPercent((used / monthly) * 100);
|
||||
windows.push({ label: "Credits", usedPercent });
|
||||
}
|
||||
|
||||
// Individual model windows
|
||||
if (opts.modelQuotas && opts.modelQuotas.size > 0) {
|
||||
const modelWindows: UsageWindow[] = [];
|
||||
|
||||
for (const [modelId, quota] of opts.modelQuotas) {
|
||||
const lowerModelId = modelId.toLowerCase();
|
||||
|
||||
// Skip internal models
|
||||
if (lowerModelId.includes("chat_") || lowerModelId.includes("tab_")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const usedPercent = clampPercent((1 - quota.remainingFraction) * 100);
|
||||
const window: UsageWindow = { label: modelId, usedPercent };
|
||||
if (quota.resetTime) {
|
||||
window.resetAt = quota.resetTime;
|
||||
}
|
||||
modelWindows.push(window);
|
||||
}
|
||||
|
||||
// Sort by usage (highest first) and take top 10
|
||||
modelWindows.sort((a, b) => b.usedPercent - a.usedPercent);
|
||||
const topModels = modelWindows.slice(0, 10);
|
||||
logDebug(
|
||||
`[antigravity] Built ${topModels.length} model windows from ${opts.modelQuotas.size} total models`,
|
||||
);
|
||||
for (const w of topModels) {
|
||||
logDebug(
|
||||
`[antigravity] ${w.label}: ${w.usedPercent.toFixed(1)}% used${w.resetAt ? ` (resets at ${new Date(w.resetAt).toISOString()})` : ""}`,
|
||||
);
|
||||
}
|
||||
windows.push(...topModels);
|
||||
}
|
||||
|
||||
return windows;
|
||||
}
|
||||
|
||||
export async function fetchAntigravityUsage(
|
||||
token: string,
|
||||
timeoutMs: number,
|
||||
fetchFn: typeof fetch,
|
||||
): Promise<ProviderUsageSnapshot> {
|
||||
const headers: Record<string, string> = {
|
||||
Authorization: `Bearer ${token}`,
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "antigravity",
|
||||
"X-Goog-Api-Client": "google-cloud-sdk vscode_cloudshelleditor/0.1",
|
||||
};
|
||||
|
||||
let credits: CreditsInfo | undefined;
|
||||
let modelQuotas: Map<string, ModelQuota> | undefined;
|
||||
let planInfo: string | undefined;
|
||||
let lastError: string | undefined;
|
||||
let projectId: string | undefined;
|
||||
|
||||
// Fetch loadCodeAssist (credits + plan info)
|
||||
try {
|
||||
const res = await fetchJson(
|
||||
`${BASE_URL}${LOAD_CODE_ASSIST_PATH}`,
|
||||
{ method: "POST", headers, body: JSON.stringify({ metadata: METADATA }) },
|
||||
timeoutMs,
|
||||
fetchFn,
|
||||
);
|
||||
|
||||
if (res.ok) {
|
||||
const data = (await res.json()) as LoadCodeAssistResponse;
|
||||
|
||||
// Extract project ID for subsequent calls
|
||||
projectId = extractProjectId(data);
|
||||
|
||||
credits = extractCredits(data);
|
||||
planInfo = extractPlanInfo(data);
|
||||
logDebug(
|
||||
`[antigravity] Credits: ${credits ? `${credits.available}/${credits.monthly}` : "none"}${planInfo ? ` (plan: ${planInfo})` : ""}`,
|
||||
);
|
||||
} else {
|
||||
lastError = await parseErrorMessage(res);
|
||||
// Fatal auth errors - stop early
|
||||
if (res.status === 401) {
|
||||
return {
|
||||
provider: "google-antigravity",
|
||||
displayName: PROVIDER_LABELS["google-antigravity"],
|
||||
windows: [],
|
||||
error: "Token expired",
|
||||
};
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
lastError = "Network error";
|
||||
}
|
||||
|
||||
// Fetch fetchAvailableModels (model quotas)
|
||||
if (!projectId) {
|
||||
logDebug("[antigravity] Missing project id; requesting available models without project");
|
||||
}
|
||||
try {
|
||||
const body = JSON.stringify(projectId ? { project: projectId } : {});
|
||||
const res = await fetchJson(
|
||||
`${BASE_URL}${FETCH_AVAILABLE_MODELS_PATH}`,
|
||||
{ method: "POST", headers, body },
|
||||
timeoutMs,
|
||||
fetchFn,
|
||||
);
|
||||
|
||||
if (res.ok) {
|
||||
const data = (await res.json()) as FetchAvailableModelsResponse;
|
||||
modelQuotas = extractModelQuotas(data);
|
||||
logDebug(`[antigravity] Extracted ${modelQuotas.size} model quotas from API`);
|
||||
for (const [modelId, quota] of modelQuotas) {
|
||||
logDebug(
|
||||
`[antigravity] ${modelId}: ${(quota.remainingFraction * 100).toFixed(1)}% remaining${quota.resetTime ? ` (resets ${new Date(quota.resetTime).toISOString()})` : ""}`,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
const err = await parseErrorMessage(res);
|
||||
if (res.status === 401) {
|
||||
lastError = "Token expired";
|
||||
} else if (!lastError) {
|
||||
lastError = err;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
if (!lastError) {
|
||||
lastError = "Network error";
|
||||
}
|
||||
}
|
||||
|
||||
// Build windows from available data
|
||||
const windows = buildUsageWindows({ credits, modelQuotas });
|
||||
|
||||
// Return error only if we got nothing
|
||||
if (windows.length === 0 && lastError) {
|
||||
logDebug(`[antigravity] Returning error snapshot: ${lastError}`);
|
||||
return {
|
||||
provider: "google-antigravity",
|
||||
displayName: PROVIDER_LABELS["google-antigravity"],
|
||||
windows: [],
|
||||
error: lastError,
|
||||
};
|
||||
}
|
||||
|
||||
const snapshot: ProviderUsageSnapshot = {
|
||||
provider: "google-antigravity",
|
||||
displayName: PROVIDER_LABELS["google-antigravity"],
|
||||
windows,
|
||||
plan: planInfo,
|
||||
};
|
||||
|
||||
logDebug(
|
||||
`[antigravity] Returning snapshot with ${windows.length} windows${planInfo ? ` (plan: ${planInfo})` : ""}`,
|
||||
);
|
||||
logDebug(`[antigravity] Snapshot: ${JSON.stringify(snapshot, null, 2)}`);
|
||||
|
||||
return snapshot;
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
export { fetchAntigravityUsage } from "./provider-usage.fetch.antigravity.js";
|
||||
export { fetchClaudeUsage } from "./provider-usage.fetch.claude.js";
|
||||
export { fetchCodexUsage } from "./provider-usage.fetch.codex.js";
|
||||
export { fetchCopilotUsage } from "./provider-usage.fetch.copilot.js";
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { resolveFetch } from "./fetch.js";
|
||||
import { type ProviderAuth, resolveProviderAuths } from "./provider-usage.auth.js";
|
||||
import {
|
||||
fetchAntigravityUsage,
|
||||
fetchClaudeUsage,
|
||||
fetchCodexUsage,
|
||||
fetchCopilotUsage,
|
||||
@@ -58,8 +57,6 @@ export async function loadProviderUsageSummary(
|
||||
return await fetchClaudeUsage(auth.token, timeoutMs, fetchFn);
|
||||
case "github-copilot":
|
||||
return await fetchCopilotUsage(auth.token, timeoutMs, fetchFn);
|
||||
case "google-antigravity":
|
||||
return await fetchAntigravityUsage(auth.token, timeoutMs, fetchFn);
|
||||
case "google-gemini-cli":
|
||||
return await fetchGeminiUsage(auth.token, timeoutMs, fetchFn, auth.provider);
|
||||
case "openai-codex":
|
||||
|
||||
@@ -4,7 +4,7 @@ import { clampPercent, resolveUsageProviderId, withTimeout } from "./provider-us
|
||||
describe("provider-usage.shared", () => {
|
||||
it("normalizes supported usage provider ids", () => {
|
||||
expect(resolveUsageProviderId("z-ai")).toBe("zai");
|
||||
expect(resolveUsageProviderId(" GOOGLE-ANTIGRAVITY ")).toBe("google-antigravity");
|
||||
expect(resolveUsageProviderId(" GOOGLE-GEMINI-CLI ")).toBe("google-gemini-cli");
|
||||
expect(resolveUsageProviderId("unknown-provider")).toBeUndefined();
|
||||
expect(resolveUsageProviderId()).toBeUndefined();
|
||||
});
|
||||
|
||||
@@ -7,7 +7,6 @@ export const PROVIDER_LABELS: Record<UsageProviderId, string> = {
|
||||
anthropic: "Claude",
|
||||
"github-copilot": "Copilot",
|
||||
"google-gemini-cli": "Gemini",
|
||||
"google-antigravity": "Antigravity",
|
||||
minimax: "MiniMax",
|
||||
"openai-codex": "Codex",
|
||||
xiaomi: "Xiaomi",
|
||||
@@ -18,7 +17,6 @@ export const usageProviders: UsageProviderId[] = [
|
||||
"anthropic",
|
||||
"github-copilot",
|
||||
"google-gemini-cli",
|
||||
"google-antigravity",
|
||||
"minimax",
|
||||
"openai-codex",
|
||||
"xiaomi",
|
||||
|
||||
@@ -338,7 +338,7 @@ describe("provider usage loading", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("loads snapshots for copilot antigravity gemini codex and xiaomi", async () => {
|
||||
it("loads snapshots for copilot gemini codex and xiaomi", async () => {
|
||||
const mockFetch = createProviderUsageFetch(async (url) => {
|
||||
if (url.includes("api.github.com/copilot_internal/user")) {
|
||||
return makeResponse(200, {
|
||||
@@ -346,14 +346,6 @@ describe("provider usage loading", () => {
|
||||
copilot_plan: "Copilot Pro",
|
||||
});
|
||||
}
|
||||
if (url.includes("cloudcode-pa.googleapis.com/v1internal:loadCodeAssist")) {
|
||||
return makeResponse(200, {
|
||||
availablePromptCredits: 80,
|
||||
planInfo: { monthlyPromptCredits: 100 },
|
||||
currentTier: { name: "Antigravity Pro" },
|
||||
cloudaicompanionProject: "projects/demo",
|
||||
});
|
||||
}
|
||||
if (url.includes("cloudcode-pa.googleapis.com/v1internal:fetchAvailableModels")) {
|
||||
return makeResponse(200, {
|
||||
models: {
|
||||
@@ -380,7 +372,6 @@ describe("provider usage loading", () => {
|
||||
const summary = await loadUsageWithAuth(
|
||||
[
|
||||
{ provider: "github-copilot", token: "copilot-token" },
|
||||
{ provider: "google-antigravity", token: "antigravity-token" },
|
||||
{ provider: "google-gemini-cli", token: "gemini-token" },
|
||||
{ provider: "openai-codex", token: "codex-token", accountId: "acc-1" },
|
||||
{ provider: "xiaomi", token: "xiaomi-token" },
|
||||
@@ -390,7 +381,6 @@ describe("provider usage loading", () => {
|
||||
|
||||
expect(summary.providers.map((provider) => provider.provider)).toEqual([
|
||||
"github-copilot",
|
||||
"google-antigravity",
|
||||
"google-gemini-cli",
|
||||
"openai-codex",
|
||||
"xiaomi",
|
||||
@@ -398,10 +388,6 @@ describe("provider usage loading", () => {
|
||||
expect(
|
||||
summary.providers.find((provider) => provider.provider === "github-copilot")?.windows,
|
||||
).toEqual([{ label: "Chat", usedPercent: 20 }]);
|
||||
expect(
|
||||
summary.providers.find((provider) => provider.provider === "google-antigravity")?.windows
|
||||
.length,
|
||||
).toBeGreaterThan(0);
|
||||
expect(
|
||||
summary.providers.find((provider) => provider.provider === "google-gemini-cli")?.windows[0]
|
||||
?.label,
|
||||
|
||||
@@ -21,7 +21,6 @@ export type UsageProviderId =
|
||||
| "anthropic"
|
||||
| "github-copilot"
|
||||
| "google-gemini-cli"
|
||||
| "google-antigravity"
|
||||
| "minimax"
|
||||
| "openai-codex"
|
||||
| "xiaomi"
|
||||
|
||||
Reference in New Issue
Block a user