antigravity-auth 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -0
- package/dist/antigravity/oauth.d.ts +30 -0
- package/dist/antigravity/oauth.js +170 -0
- package/dist/claude/login.d.ts +7 -0
- package/dist/claude/login.js +480 -0
- package/dist/claude/menu-helpers.d.ts +22 -0
- package/dist/claude/menu-helpers.js +281 -0
- package/dist/claude/proxy-manager.d.ts +11 -0
- package/dist/claude/proxy-manager.js +129 -0
- package/dist/claude/proxy.d.ts +1 -0
- package/dist/claude/proxy.js +733 -0
- package/dist/constants.d.ts +138 -0
- package/dist/constants.js +216 -0
- package/dist/hooks/auto-update-checker/cache.d.ts +2 -0
- package/dist/hooks/auto-update-checker/cache.js +70 -0
- package/dist/hooks/auto-update-checker/checker.d.ts +15 -0
- package/dist/hooks/auto-update-checker/checker.js +233 -0
- package/dist/hooks/auto-update-checker/constants.d.ts +8 -0
- package/dist/hooks/auto-update-checker/constants.js +22 -0
- package/dist/hooks/auto-update-checker/index.d.ts +33 -0
- package/dist/hooks/auto-update-checker/index.js +121 -0
- package/dist/hooks/auto-update-checker/logging.d.ts +2 -0
- package/dist/hooks/auto-update-checker/logging.js +8 -0
- package/dist/hooks/auto-update-checker/types.d.ts +24 -0
- package/dist/hooks/auto-update-checker/types.js +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/opencode/hooks/auto-update-checker/cache.d.ts +2 -0
- package/dist/opencode/hooks/auto-update-checker/cache.js +70 -0
- package/dist/opencode/hooks/auto-update-checker/checker.d.ts +15 -0
- package/dist/opencode/hooks/auto-update-checker/checker.js +233 -0
- package/dist/opencode/hooks/auto-update-checker/constants.d.ts +8 -0
- package/dist/opencode/hooks/auto-update-checker/constants.js +22 -0
- package/dist/opencode/hooks/auto-update-checker/index.d.ts +33 -0
- package/dist/opencode/hooks/auto-update-checker/index.js +121 -0
- package/dist/opencode/hooks/auto-update-checker/logging.d.ts +2 -0
- package/dist/opencode/hooks/auto-update-checker/logging.js +8 -0
- package/dist/opencode/hooks/auto-update-checker/types.d.ts +24 -0
- package/dist/opencode/hooks/auto-update-checker/types.js +1 -0
- package/dist/opencode/plugin.d.ts +29 -0
- package/dist/opencode/plugin.js +2954 -0
- package/dist/plugin/accounts.d.ts +173 -0
- package/dist/plugin/accounts.js +966 -0
- package/dist/plugin/auth.d.ts +20 -0
- package/dist/plugin/auth.js +44 -0
- package/dist/plugin/cache/index.d.ts +4 -0
- package/dist/plugin/cache/index.js +4 -0
- package/dist/plugin/cache/signature-cache.d.ts +110 -0
- package/dist/plugin/cache/signature-cache.js +347 -0
- package/dist/plugin/cache.d.ts +43 -0
- package/dist/plugin/cache.js +180 -0
- package/dist/plugin/cli.d.ts +26 -0
- package/dist/plugin/cli.js +126 -0
- package/dist/plugin/config/index.d.ts +15 -0
- package/dist/plugin/config/index.js +15 -0
- package/dist/plugin/config/loader.d.ts +38 -0
- package/dist/plugin/config/loader.js +150 -0
- package/dist/plugin/config/models.d.ts +26 -0
- package/dist/plugin/config/models.js +95 -0
- package/dist/plugin/config/schema.d.ts +144 -0
- package/dist/plugin/config/schema.js +458 -0
- package/dist/plugin/config/updater.d.ts +76 -0
- package/dist/plugin/config/updater.js +205 -0
- package/dist/plugin/core/streaming/index.d.ts +2 -0
- package/dist/plugin/core/streaming/index.js +2 -0
- package/dist/plugin/core/streaming/transformer.d.ts +9 -0
- package/dist/plugin/core/streaming/transformer.js +301 -0
- package/dist/plugin/core/streaming/types.d.ts +28 -0
- package/dist/plugin/core/streaming/types.js +1 -0
- package/dist/plugin/debug.d.ts +93 -0
- package/dist/plugin/debug.js +375 -0
- package/dist/plugin/errors.d.ts +27 -0
- package/dist/plugin/errors.js +41 -0
- package/dist/plugin/fingerprint.d.ts +69 -0
- package/dist/plugin/fingerprint.js +137 -0
- package/dist/plugin/image-saver.d.ts +24 -0
- package/dist/plugin/image-saver.js +78 -0
- package/dist/plugin/logger.d.ts +35 -0
- package/dist/plugin/logger.js +67 -0
- package/dist/plugin/logging-utils.d.ts +22 -0
- package/dist/plugin/logging-utils.js +91 -0
- package/dist/plugin/project.d.ts +32 -0
- package/dist/plugin/project.js +229 -0
- package/dist/plugin/quota.d.ts +34 -0
- package/dist/plugin/quota.js +261 -0
- package/dist/plugin/recovery/constants.d.ts +21 -0
- package/dist/plugin/recovery/constants.js +42 -0
- package/dist/plugin/recovery/index.d.ts +11 -0
- package/dist/plugin/recovery/index.js +11 -0
- package/dist/plugin/recovery/storage.d.ts +23 -0
- package/dist/plugin/recovery/storage.js +340 -0
- package/dist/plugin/recovery/types.d.ts +115 -0
- package/dist/plugin/recovery/types.js +6 -0
- package/dist/plugin/recovery.d.ts +60 -0
- package/dist/plugin/recovery.js +360 -0
- package/dist/plugin/refresh-queue.d.ts +99 -0
- package/dist/plugin/refresh-queue.js +235 -0
- package/dist/plugin/request-helpers.d.ts +281 -0
- package/dist/plugin/request-helpers.js +2200 -0
- package/dist/plugin/request.d.ts +110 -0
- package/dist/plugin/request.js +1489 -0
- package/dist/plugin/rotation.d.ts +182 -0
- package/dist/plugin/rotation.js +364 -0
- package/dist/plugin/search.d.ts +31 -0
- package/dist/plugin/search.js +185 -0
- package/dist/plugin/server.d.ts +22 -0
- package/dist/plugin/server.js +306 -0
- package/dist/plugin/storage.d.ts +136 -0
- package/dist/plugin/storage.js +599 -0
- package/dist/plugin/stores/signature-store.d.ts +4 -0
- package/dist/plugin/stores/signature-store.js +24 -0
- package/dist/plugin/thinking-recovery.d.ts +89 -0
- package/dist/plugin/thinking-recovery.js +289 -0
- package/dist/plugin/token.d.ts +18 -0
- package/dist/plugin/token.js +127 -0
- package/dist/plugin/transform/claude.d.ts +79 -0
- package/dist/plugin/transform/claude.js +256 -0
- package/dist/plugin/transform/cross-model-sanitizer.d.ts +34 -0
- package/dist/plugin/transform/cross-model-sanitizer.js +224 -0
- package/dist/plugin/transform/gemini.d.ts +132 -0
- package/dist/plugin/transform/gemini.js +659 -0
- package/dist/plugin/transform/index.d.ts +14 -0
- package/dist/plugin/transform/index.js +9 -0
- package/dist/plugin/transform/model-resolver.d.ts +98 -0
- package/dist/plugin/transform/model-resolver.js +320 -0
- package/dist/plugin/transform/types.d.ts +110 -0
- package/dist/plugin/transform/types.js +1 -0
- package/dist/plugin/types.d.ts +95 -0
- package/dist/plugin/types.js +1 -0
- package/dist/plugin/ui/ansi.d.ts +31 -0
- package/dist/plugin/ui/ansi.js +45 -0
- package/dist/plugin/ui/auth-menu.d.ts +47 -0
- package/dist/plugin/ui/auth-menu.js +199 -0
- package/dist/plugin/ui/confirm.d.ts +1 -0
- package/dist/plugin/ui/confirm.js +14 -0
- package/dist/plugin/ui/select.d.ts +22 -0
- package/dist/plugin/ui/select.js +243 -0
- package/dist/plugin/version.d.ts +18 -0
- package/dist/plugin/version.js +79 -0
- package/dist/src/antigravity/oauth.d.ts +30 -0
- package/dist/src/antigravity/oauth.js +170 -0
- package/dist/src/constants.d.ts +138 -0
- package/dist/src/constants.js +216 -0
- package/dist/src/hooks/auto-update-checker/cache.d.ts +2 -0
- package/dist/src/hooks/auto-update-checker/cache.js +70 -0
- package/dist/src/hooks/auto-update-checker/checker.d.ts +15 -0
- package/dist/src/hooks/auto-update-checker/checker.js +233 -0
- package/dist/src/hooks/auto-update-checker/constants.d.ts +8 -0
- package/dist/src/hooks/auto-update-checker/constants.js +22 -0
- package/dist/src/hooks/auto-update-checker/index.d.ts +33 -0
- package/dist/src/hooks/auto-update-checker/index.js +121 -0
- package/dist/src/hooks/auto-update-checker/logging.d.ts +2 -0
- package/dist/src/hooks/auto-update-checker/logging.js +8 -0
- package/dist/src/hooks/auto-update-checker/types.d.ts +24 -0
- package/dist/src/hooks/auto-update-checker/types.js +1 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.js +5 -0
- package/dist/src/plugin/accounts.d.ts +173 -0
- package/dist/src/plugin/accounts.js +966 -0
- package/dist/src/plugin/auth.d.ts +20 -0
- package/dist/src/plugin/auth.js +44 -0
- package/dist/src/plugin/cache/index.d.ts +4 -0
- package/dist/src/plugin/cache/index.js +4 -0
- package/dist/src/plugin/cache/signature-cache.d.ts +110 -0
- package/dist/src/plugin/cache/signature-cache.js +347 -0
- package/dist/src/plugin/cache.d.ts +43 -0
- package/dist/src/plugin/cache.js +180 -0
- package/dist/src/plugin/cli.d.ts +26 -0
- package/dist/src/plugin/cli.js +126 -0
- package/dist/src/plugin/config/index.d.ts +15 -0
- package/dist/src/plugin/config/index.js +15 -0
- package/dist/src/plugin/config/loader.d.ts +38 -0
- package/dist/src/plugin/config/loader.js +150 -0
- package/dist/src/plugin/config/models.d.ts +26 -0
- package/dist/src/plugin/config/models.js +95 -0
- package/dist/src/plugin/config/schema.d.ts +144 -0
- package/dist/src/plugin/config/schema.js +458 -0
- package/dist/src/plugin/config/updater.d.ts +76 -0
- package/dist/src/plugin/config/updater.js +205 -0
- package/dist/src/plugin/core/streaming/index.d.ts +2 -0
- package/dist/src/plugin/core/streaming/index.js +2 -0
- package/dist/src/plugin/core/streaming/transformer.d.ts +9 -0
- package/dist/src/plugin/core/streaming/transformer.js +301 -0
- package/dist/src/plugin/core/streaming/types.d.ts +28 -0
- package/dist/src/plugin/core/streaming/types.js +1 -0
- package/dist/src/plugin/debug.d.ts +93 -0
- package/dist/src/plugin/debug.js +375 -0
- package/dist/src/plugin/errors.d.ts +27 -0
- package/dist/src/plugin/errors.js +41 -0
- package/dist/src/plugin/fingerprint.d.ts +69 -0
- package/dist/src/plugin/fingerprint.js +137 -0
- package/dist/src/plugin/image-saver.d.ts +24 -0
- package/dist/src/plugin/image-saver.js +78 -0
- package/dist/src/plugin/logger.d.ts +35 -0
- package/dist/src/plugin/logger.js +67 -0
- package/dist/src/plugin/logging-utils.d.ts +22 -0
- package/dist/src/plugin/logging-utils.js +91 -0
- package/dist/src/plugin/project.d.ts +32 -0
- package/dist/src/plugin/project.js +229 -0
- package/dist/src/plugin/quota.d.ts +34 -0
- package/dist/src/plugin/quota.js +261 -0
- package/dist/src/plugin/recovery/constants.d.ts +21 -0
- package/dist/src/plugin/recovery/constants.js +42 -0
- package/dist/src/plugin/recovery/index.d.ts +11 -0
- package/dist/src/plugin/recovery/index.js +11 -0
- package/dist/src/plugin/recovery/storage.d.ts +23 -0
- package/dist/src/plugin/recovery/storage.js +340 -0
- package/dist/src/plugin/recovery/types.d.ts +115 -0
- package/dist/src/plugin/recovery/types.js +6 -0
- package/dist/src/plugin/recovery.d.ts +60 -0
- package/dist/src/plugin/recovery.js +360 -0
- package/dist/src/plugin/refresh-queue.d.ts +99 -0
- package/dist/src/plugin/refresh-queue.js +235 -0
- package/dist/src/plugin/request-helpers.d.ts +281 -0
- package/dist/src/plugin/request-helpers.js +2200 -0
- package/dist/src/plugin/request.d.ts +110 -0
- package/dist/src/plugin/request.js +1489 -0
- package/dist/src/plugin/rotation.d.ts +182 -0
- package/dist/src/plugin/rotation.js +364 -0
- package/dist/src/plugin/search.d.ts +31 -0
- package/dist/src/plugin/search.js +185 -0
- package/dist/src/plugin/server.d.ts +22 -0
- package/dist/src/plugin/server.js +306 -0
- package/dist/src/plugin/storage.d.ts +136 -0
- package/dist/src/plugin/storage.js +599 -0
- package/dist/src/plugin/stores/signature-store.d.ts +4 -0
- package/dist/src/plugin/stores/signature-store.js +24 -0
- package/dist/src/plugin/thinking-recovery.d.ts +89 -0
- package/dist/src/plugin/thinking-recovery.js +289 -0
- package/dist/src/plugin/token.d.ts +18 -0
- package/dist/src/plugin/token.js +127 -0
- package/dist/src/plugin/transform/claude.d.ts +79 -0
- package/dist/src/plugin/transform/claude.js +256 -0
- package/dist/src/plugin/transform/cross-model-sanitizer.d.ts +34 -0
- package/dist/src/plugin/transform/cross-model-sanitizer.js +224 -0
- package/dist/src/plugin/transform/gemini.d.ts +132 -0
- package/dist/src/plugin/transform/gemini.js +659 -0
- package/dist/src/plugin/transform/index.d.ts +14 -0
- package/dist/src/plugin/transform/index.js +9 -0
- package/dist/src/plugin/transform/model-resolver.d.ts +98 -0
- package/dist/src/plugin/transform/model-resolver.js +320 -0
- package/dist/src/plugin/transform/types.d.ts +110 -0
- package/dist/src/plugin/transform/types.js +1 -0
- package/dist/src/plugin/types.d.ts +95 -0
- package/dist/src/plugin/types.js +1 -0
- package/dist/src/plugin/ui/ansi.d.ts +31 -0
- package/dist/src/plugin/ui/ansi.js +45 -0
- package/dist/src/plugin/ui/auth-menu.d.ts +47 -0
- package/dist/src/plugin/ui/auth-menu.js +199 -0
- package/dist/src/plugin/ui/confirm.d.ts +1 -0
- package/dist/src/plugin/ui/confirm.js +14 -0
- package/dist/src/plugin/ui/select.d.ts +22 -0
- package/dist/src/plugin/ui/select.js +243 -0
- package/dist/src/plugin/version.d.ts +18 -0
- package/dist/src/plugin/version.js +79 -0
- package/package.json +54 -0
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# plugin-antigravity-auth
|
|
2
|
+
|
|
3
|
+
Google Antigravity OAuth plugin for OpenCode and Claude Code. Intercepts `fetch()` calls to `generativelanguage.googleapis.com`, transforms them to Antigravity format, and handles auth, quota, recovery, and multi-account rotation.
|
|
4
|
+
|
|
5
|
+
## Under-the-Hood Architecture
|
|
6
|
+
|
|
7
|
+
```mermaid
|
|
8
|
+
flowchart TD
|
|
9
|
+
%% Environments
|
|
10
|
+
subgraph Claude_Code [Claude Code Environment]
|
|
11
|
+
CC_CLI[Claude CLI]
|
|
12
|
+
CC_PROXY[Standalone Local Proxy Server]
|
|
13
|
+
CC_CLI -->|API Calls (localhost:port)| CC_PROXY
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
subgraph OpenCode_Env [OpenCode Environment]
|
|
17
|
+
OC_CLI[OpenCode CLI]
|
|
18
|
+
OC_INTERCEPTOR[Fetch Interceptor Hook]
|
|
19
|
+
OC_CLI -->|Native fetch()| OC_INTERCEPTOR
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
%% Shared Core
|
|
23
|
+
subgraph Shared_Core [Plugin Shared Core (src/)]
|
|
24
|
+
TRANSFORMER[Request/Response Transformer]
|
|
25
|
+
AUTH_ROUTER[Auth Header Injector]
|
|
26
|
+
ACCOUNT_MGR[Multi-Account Manager]
|
|
27
|
+
QUOTA_TRACKER[Quota & 429 Tracker]
|
|
28
|
+
OAUTH_FLOW[Localhost OAuth Desktop Flow]
|
|
29
|
+
|
|
30
|
+
CC_PROXY -->|Raw Request| TRANSFORMER
|
|
31
|
+
OC_INTERCEPTOR -->|Raw Request| TRANSFORMER
|
|
32
|
+
|
|
33
|
+
TRANSFORMER --> AUTH_ROUTER
|
|
34
|
+
AUTH_ROUTER --> ACCOUNT_MGR
|
|
35
|
+
|
|
36
|
+
ACCOUNT_MGR <-->|Read/Write Accounts| FILE_STORAGE[(Account Storage .json)]
|
|
37
|
+
ACCOUNT_MGR -->|Select Account| QUOTA_TRACKER
|
|
38
|
+
|
|
39
|
+
QUOTA_TRACKER -->|Needs Refresh| OAUTH_FLOW
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
%% External
|
|
43
|
+
subgraph Google_Services [Google External APIs]
|
|
44
|
+
G_OAUTH[Google OAuth 2.0 API]
|
|
45
|
+
GEMINI_API[Gemini / Antigravity API]
|
|
46
|
+
|
|
47
|
+
OAUTH_FLOW <-->|Authorize / Tokens| G_OAUTH
|
|
48
|
+
QUOTA_TRACKER -->|Decorated Fetch| GEMINI_API
|
|
49
|
+
end
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Structure
|
|
53
|
+
|
|
54
|
+
- `src/` - Shared core logic (API transforms, OAuth flows, account management)
|
|
55
|
+
- `claude/` - Claude Code specific wrappers (standalone proxy server)
|
|
56
|
+
- `opencode/` - OpenCode specific wrappers (fetch interception plugin)
|
|
57
|
+
- `dist/` - Single compiled output supporting both environments
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
MIT
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result returned to the caller after constructing an OAuth authorization URL.
|
|
3
|
+
*/
|
|
4
|
+
export interface AntigravityAuthorization {
|
|
5
|
+
url: string;
|
|
6
|
+
verifier: string;
|
|
7
|
+
projectId: string;
|
|
8
|
+
}
|
|
9
|
+
interface AntigravityTokenExchangeSuccess {
|
|
10
|
+
type: "success";
|
|
11
|
+
refresh: string;
|
|
12
|
+
access: string;
|
|
13
|
+
expires: number;
|
|
14
|
+
email?: string;
|
|
15
|
+
projectId: string;
|
|
16
|
+
}
|
|
17
|
+
interface AntigravityTokenExchangeFailure {
|
|
18
|
+
type: "failed";
|
|
19
|
+
error: string;
|
|
20
|
+
}
|
|
21
|
+
export type AntigravityTokenExchangeResult = AntigravityTokenExchangeSuccess | AntigravityTokenExchangeFailure;
|
|
22
|
+
/**
|
|
23
|
+
* Build the Antigravity OAuth authorization URL including PKCE and optional project metadata.
|
|
24
|
+
*/
|
|
25
|
+
export declare function authorizeAntigravity(projectId?: string): Promise<AntigravityAuthorization>;
|
|
26
|
+
/**
|
|
27
|
+
* Exchange an authorization code for Antigravity CLI access and refresh tokens.
|
|
28
|
+
*/
|
|
29
|
+
export declare function exchangeAntigravity(code: string, state: string): Promise<AntigravityTokenExchangeResult>;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { generatePKCE } from "@openauthjs/openauth/pkce";
|
|
2
|
+
import { ANTIGRAVITY_CLIENT_ID, ANTIGRAVITY_CLIENT_SECRET, ANTIGRAVITY_REDIRECT_URI, ANTIGRAVITY_SCOPES, ANTIGRAVITY_ENDPOINT_FALLBACKS, ANTIGRAVITY_LOAD_ENDPOINTS, getAntigravityHeaders, GEMINI_CLI_HEADERS, } from "../constants";
|
|
3
|
+
import { createLogger } from "../plugin/logger";
|
|
4
|
+
import { calculateTokenExpiry } from "../plugin/auth";
|
|
5
|
+
const log = createLogger("oauth");
|
|
6
|
+
/**
|
|
7
|
+
* Encode an object into a URL-safe base64 string.
|
|
8
|
+
*/
|
|
9
|
+
function encodeState(payload) {
|
|
10
|
+
return Buffer.from(JSON.stringify(payload), "utf8").toString("base64url");
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Decode an OAuth state parameter back into its structured representation.
|
|
14
|
+
*/
|
|
15
|
+
function decodeState(state) {
|
|
16
|
+
const normalized = state.replace(/-/g, "+").replace(/_/g, "/");
|
|
17
|
+
const padded = normalized.padEnd(normalized.length + ((4 - normalized.length % 4) % 4), "=");
|
|
18
|
+
const json = Buffer.from(padded, "base64").toString("utf8");
|
|
19
|
+
const parsed = JSON.parse(json);
|
|
20
|
+
if (typeof parsed.verifier !== "string") {
|
|
21
|
+
throw new Error("Missing PKCE verifier in state");
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
verifier: parsed.verifier,
|
|
25
|
+
projectId: typeof parsed.projectId === "string" ? parsed.projectId : "",
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Build the Antigravity OAuth authorization URL including PKCE and optional project metadata.
|
|
30
|
+
*/
|
|
31
|
+
export async function authorizeAntigravity(projectId = "") {
|
|
32
|
+
const pkce = (await generatePKCE());
|
|
33
|
+
const url = new URL("https://accounts.google.com/o/oauth2/v2/auth");
|
|
34
|
+
url.searchParams.set("client_id", ANTIGRAVITY_CLIENT_ID);
|
|
35
|
+
url.searchParams.set("response_type", "code");
|
|
36
|
+
url.searchParams.set("redirect_uri", ANTIGRAVITY_REDIRECT_URI);
|
|
37
|
+
url.searchParams.set("scope", ANTIGRAVITY_SCOPES.join(" "));
|
|
38
|
+
url.searchParams.set("code_challenge", pkce.challenge);
|
|
39
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
40
|
+
url.searchParams.set("state", encodeState({ verifier: pkce.verifier, projectId: projectId || "" }));
|
|
41
|
+
url.searchParams.set("access_type", "offline");
|
|
42
|
+
url.searchParams.set("prompt", "consent");
|
|
43
|
+
return {
|
|
44
|
+
url: url.toString(),
|
|
45
|
+
verifier: pkce.verifier,
|
|
46
|
+
projectId: projectId || "",
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const FETCH_TIMEOUT_MS = 10000;
|
|
50
|
+
async function fetchWithTimeout(url, options, timeoutMs = FETCH_TIMEOUT_MS) {
|
|
51
|
+
const controller = new AbortController();
|
|
52
|
+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
53
|
+
try {
|
|
54
|
+
return await fetch(url, { ...options, signal: controller.signal });
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
clearTimeout(timeout);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async function fetchProjectID(accessToken) {
|
|
61
|
+
const errors = [];
|
|
62
|
+
const loadHeaders = {
|
|
63
|
+
Authorization: `Bearer ${accessToken}`,
|
|
64
|
+
"Content-Type": "application/json",
|
|
65
|
+
"User-Agent": GEMINI_CLI_HEADERS["User-Agent"],
|
|
66
|
+
"Client-Metadata": getAntigravityHeaders()["Client-Metadata"],
|
|
67
|
+
};
|
|
68
|
+
const loadEndpoints = Array.from(new Set([...ANTIGRAVITY_LOAD_ENDPOINTS, ...ANTIGRAVITY_ENDPOINT_FALLBACKS]));
|
|
69
|
+
for (const baseEndpoint of loadEndpoints) {
|
|
70
|
+
try {
|
|
71
|
+
const url = `${baseEndpoint}/v1internal:loadCodeAssist`;
|
|
72
|
+
const response = await fetchWithTimeout(url, {
|
|
73
|
+
method: "POST",
|
|
74
|
+
headers: loadHeaders,
|
|
75
|
+
body: JSON.stringify({
|
|
76
|
+
metadata: {
|
|
77
|
+
ideType: "ANTIGRAVITY",
|
|
78
|
+
platform: process.platform === "win32" ? "WINDOWS" : "MACOS",
|
|
79
|
+
pluginType: "GEMINI",
|
|
80
|
+
},
|
|
81
|
+
}),
|
|
82
|
+
});
|
|
83
|
+
if (!response.ok) {
|
|
84
|
+
const message = await response.text().catch(() => "");
|
|
85
|
+
errors.push(`loadCodeAssist ${response.status} at ${baseEndpoint}${message ? `: ${message}` : ""}`);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const data = await response.json();
|
|
89
|
+
if (typeof data.cloudaicompanionProject === "string" && data.cloudaicompanionProject) {
|
|
90
|
+
return data.cloudaicompanionProject;
|
|
91
|
+
}
|
|
92
|
+
if (data.cloudaicompanionProject &&
|
|
93
|
+
typeof data.cloudaicompanionProject.id === "string" &&
|
|
94
|
+
data.cloudaicompanionProject.id) {
|
|
95
|
+
return data.cloudaicompanionProject.id;
|
|
96
|
+
}
|
|
97
|
+
errors.push(`loadCodeAssist missing project id at ${baseEndpoint}`);
|
|
98
|
+
}
|
|
99
|
+
catch (e) {
|
|
100
|
+
errors.push(`loadCodeAssist error at ${baseEndpoint}: ${e instanceof Error ? e.message : String(e)}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (errors.length) {
|
|
104
|
+
log.warn("Failed to resolve Antigravity project via loadCodeAssist", { errors: errors.join("; ") });
|
|
105
|
+
}
|
|
106
|
+
return "";
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Exchange an authorization code for Antigravity CLI access and refresh tokens.
|
|
110
|
+
*/
|
|
111
|
+
export async function exchangeAntigravity(code, state) {
|
|
112
|
+
try {
|
|
113
|
+
const { verifier, projectId } = decodeState(state);
|
|
114
|
+
const startTime = Date.now();
|
|
115
|
+
const tokenResponse = await fetch("https://oauth2.googleapis.com/token", {
|
|
116
|
+
method: "POST",
|
|
117
|
+
headers: {
|
|
118
|
+
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
|
119
|
+
"Accept": "*/*",
|
|
120
|
+
"Accept-Encoding": "gzip, deflate, br",
|
|
121
|
+
"User-Agent": GEMINI_CLI_HEADERS["User-Agent"],
|
|
122
|
+
},
|
|
123
|
+
body: new URLSearchParams({
|
|
124
|
+
client_id: ANTIGRAVITY_CLIENT_ID,
|
|
125
|
+
client_secret: ANTIGRAVITY_CLIENT_SECRET,
|
|
126
|
+
code,
|
|
127
|
+
grant_type: "authorization_code",
|
|
128
|
+
redirect_uri: ANTIGRAVITY_REDIRECT_URI,
|
|
129
|
+
code_verifier: verifier,
|
|
130
|
+
}),
|
|
131
|
+
});
|
|
132
|
+
if (!tokenResponse.ok) {
|
|
133
|
+
const errorText = await tokenResponse.text();
|
|
134
|
+
return { type: "failed", error: errorText };
|
|
135
|
+
}
|
|
136
|
+
const tokenPayload = (await tokenResponse.json());
|
|
137
|
+
const userInfoResponse = await fetch("https://www.googleapis.com/oauth2/v1/userinfo?alt=json", {
|
|
138
|
+
headers: {
|
|
139
|
+
Authorization: `Bearer ${tokenPayload.access_token}`,
|
|
140
|
+
"User-Agent": GEMINI_CLI_HEADERS["User-Agent"],
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
const userInfo = userInfoResponse.ok
|
|
144
|
+
? (await userInfoResponse.json())
|
|
145
|
+
: {};
|
|
146
|
+
const refreshToken = tokenPayload.refresh_token;
|
|
147
|
+
if (!refreshToken) {
|
|
148
|
+
return { type: "failed", error: "Missing refresh token in response" };
|
|
149
|
+
}
|
|
150
|
+
let effectiveProjectId = projectId;
|
|
151
|
+
if (!effectiveProjectId) {
|
|
152
|
+
effectiveProjectId = await fetchProjectID(tokenPayload.access_token);
|
|
153
|
+
}
|
|
154
|
+
const storedRefresh = `${refreshToken}|${effectiveProjectId || ""}`;
|
|
155
|
+
return {
|
|
156
|
+
type: "success",
|
|
157
|
+
refresh: storedRefresh,
|
|
158
|
+
access: tokenPayload.access_token,
|
|
159
|
+
expires: calculateTokenExpiry(startTime, tokenPayload.expires_in),
|
|
160
|
+
email: userInfo.email,
|
|
161
|
+
projectId: effectiveProjectId || "",
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
return {
|
|
166
|
+
type: "failed",
|
|
167
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
}
|