opencode-antigravity-auth 1.0.2 → 1.0.3

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.
@@ -1,198 +0,0 @@
1
- import { createWriteStream } from "node:fs";
2
- import { join } from "node:path";
3
- import { cwd, env } from "node:process";
4
-
5
- const DEBUG_FLAG = env.OPENCODE_ANTIGRAVITY_DEBUG ?? "";
6
- const MAX_BODY_PREVIEW_CHARS = 12000;
7
- const debugEnabled = DEBUG_FLAG.trim() === "1";
8
- const logFilePath = debugEnabled ? defaultLogFilePath() : undefined;
9
- const logWriter = createLogWriter(logFilePath);
10
-
11
- export interface AntigravityDebugContext {
12
- id: string;
13
- streaming: boolean;
14
- startedAt: number;
15
- }
16
-
17
- interface AntigravityDebugRequestMeta {
18
- originalUrl: string;
19
- resolvedUrl: string;
20
- method?: string;
21
- headers?: HeadersInit;
22
- body?: BodyInit | null;
23
- streaming: boolean;
24
- projectId?: string;
25
- }
26
-
27
- interface AntigravityDebugResponseMeta {
28
- body?: string;
29
- note?: string;
30
- error?: unknown;
31
- headersOverride?: HeadersInit;
32
- }
33
-
34
- let requestCounter = 0;
35
-
36
- /**
37
- * Begins a debug trace for an Antigravity request, logging request metadata when debugging is enabled.
38
- */
39
- export function startAntigravityDebugRequest(meta: AntigravityDebugRequestMeta): AntigravityDebugContext | null {
40
- if (!debugEnabled) {
41
- return null;
42
- }
43
-
44
- const id = `ANTIGRAVITY-${++requestCounter}`;
45
- const method = meta.method ?? "GET";
46
- logDebug(`[Antigravity Debug ${id}] ${method} ${meta.resolvedUrl}`);
47
- if (meta.originalUrl && meta.originalUrl !== meta.resolvedUrl) {
48
- logDebug(`[Antigravity Debug ${id}] Original URL: ${meta.originalUrl}`);
49
- }
50
- if (meta.projectId) {
51
- logDebug(`[Antigravity Debug ${id}] Project: ${meta.projectId}`);
52
- }
53
- logDebug(`[Antigravity Debug ${id}] Streaming: ${meta.streaming ? "yes" : "no"}`);
54
- logDebug(`[Antigravity Debug ${id}] Headers: ${JSON.stringify(maskHeaders(meta.headers))}`);
55
- const bodyPreview = formatBodyPreview(meta.body);
56
- if (bodyPreview) {
57
- logDebug(`[Antigravity Debug ${id}] Body Preview: ${bodyPreview}`);
58
- }
59
-
60
- return { id, streaming: meta.streaming, startedAt: Date.now() };
61
- }
62
-
63
- /**
64
- * Logs response details for a previously started debug trace when debugging is enabled.
65
- */
66
- export function logAntigravityDebugResponse(
67
- context: AntigravityDebugContext | null | undefined,
68
- response: Response,
69
- meta: AntigravityDebugResponseMeta = {},
70
- ): void {
71
- if (!debugEnabled || !context) {
72
- return;
73
- }
74
-
75
- const durationMs = Date.now() - context.startedAt;
76
- logDebug(
77
- `[Antigravity Debug ${context.id}] Response ${response.status} ${response.statusText} (${durationMs}ms)`,
78
- );
79
- logDebug(
80
- `[Antigravity Debug ${context.id}] Response Headers: ${JSON.stringify(
81
- maskHeaders(meta.headersOverride ?? response.headers),
82
- )}`,
83
- );
84
-
85
- if (meta.note) {
86
- logDebug(`[Antigravity Debug ${context.id}] Note: ${meta.note}`);
87
- }
88
-
89
- if (meta.error) {
90
- logDebug(`[Antigravity Debug ${context.id}] Error: ${formatError(meta.error)}`);
91
- }
92
-
93
- if (meta.body) {
94
- logDebug(
95
- `[Antigravity Debug ${context.id}] Response Body Preview: ${truncateForLog(meta.body)}`,
96
- );
97
- }
98
- }
99
-
100
- /**
101
- * Obscures sensitive headers and returns a plain object for logging.
102
- */
103
- function maskHeaders(headers?: HeadersInit | Headers): Record<string, string> {
104
- if (!headers) {
105
- return {};
106
- }
107
-
108
- const result: Record<string, string> = {};
109
- const parsed = headers instanceof Headers ? headers : new Headers(headers);
110
- parsed.forEach((value, key) => {
111
- if (key.toLowerCase() === "authorization") {
112
- result[key] = "[redacted]";
113
- } else {
114
- result[key] = value;
115
- }
116
- });
117
- return result;
118
- }
119
-
120
- /**
121
- * Produces a short, type-aware preview of a request/response body for logs.
122
- */
123
- function formatBodyPreview(body?: BodyInit | null): string | undefined {
124
- if (body == null) {
125
- return undefined;
126
- }
127
-
128
- if (typeof body === "string") {
129
- return truncateForLog(body);
130
- }
131
-
132
- if (body instanceof URLSearchParams) {
133
- return truncateForLog(body.toString());
134
- }
135
-
136
- if (typeof Blob !== "undefined" && body instanceof Blob) {
137
- return `[Blob size=${body.size}]`;
138
- }
139
-
140
- if (typeof FormData !== "undefined" && body instanceof FormData) {
141
- return "[FormData payload omitted]";
142
- }
143
-
144
- return `[${body.constructor?.name ?? typeof body} payload omitted]`;
145
- }
146
-
147
- /**
148
- * Truncates long strings to a fixed preview length for logging.
149
- */
150
- function truncateForLog(text: string): string {
151
- if (text.length <= MAX_BODY_PREVIEW_CHARS) {
152
- return text;
153
- }
154
- return `${text.slice(0, MAX_BODY_PREVIEW_CHARS)}... (truncated ${text.length - MAX_BODY_PREVIEW_CHARS} chars)`;
155
- }
156
-
157
- /**
158
- * Writes a single debug line using the configured writer.
159
- */
160
- function logDebug(line: string): void {
161
- logWriter(line);
162
- }
163
-
164
- /**
165
- * Converts unknown error-like values into printable strings.
166
- */
167
- function formatError(error: unknown): string {
168
- if (error instanceof Error) {
169
- return error.stack ?? error.message;
170
- }
171
- try {
172
- return JSON.stringify(error);
173
- } catch {
174
- return String(error);
175
- }
176
- }
177
-
178
- /**
179
- * Builds a timestamped log file path in the current working directory.
180
- */
181
- function defaultLogFilePath(): string {
182
- const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
183
- return join(cwd(), `antigravity-debug-${timestamp}.log`);
184
- }
185
-
186
- /**
187
- * Creates a line writer that appends to a file when provided.
188
- */
189
- function createLogWriter(filePath?: string): (line: string) => void {
190
- if (!filePath) {
191
- return () => {};
192
- }
193
-
194
- const stream = createWriteStream(filePath, { flags: "a" });
195
- return (line: string) => {
196
- stream.write(`${line}\n`);
197
- };
198
- }
@@ -1,334 +0,0 @@
1
- import {
2
- ANTIGRAVITY_HEADERS,
3
- ANTIGRAVITY_ENDPOINT_FALLBACKS,
4
- ANTIGRAVITY_LOAD_ENDPOINTS,
5
- ANTIGRAVITY_DEFAULT_PROJECT_ID,
6
- } from "../constants";
7
- import { formatRefreshParts, parseRefreshParts } from "./auth";
8
- import type {
9
- OAuthAuthDetails,
10
- PluginClient,
11
- ProjectContextResult,
12
- } from "./types";
13
-
14
- const projectContextResultCache = new Map<string, ProjectContextResult>();
15
- const projectContextPendingCache = new Map<string, Promise<ProjectContextResult>>();
16
-
17
- const CODE_ASSIST_METADATA = {
18
- ideType: "IDE_UNSPECIFIED",
19
- platform: "PLATFORM_UNSPECIFIED",
20
- pluginType: "GEMINI",
21
- } as const;
22
-
23
- interface AntigravityUserTier {
24
- id?: string;
25
- isDefault?: boolean;
26
- userDefinedCloudaicompanionProject?: boolean;
27
- }
28
-
29
- interface LoadCodeAssistPayload {
30
- cloudaicompanionProject?: string;
31
- currentTier?: {
32
- id?: string;
33
- };
34
- allowedTiers?: AntigravityUserTier[];
35
- }
36
-
37
- interface OnboardUserPayload {
38
- done?: boolean;
39
- response?: {
40
- cloudaicompanionProject?: {
41
- id?: string;
42
- };
43
- };
44
- }
45
-
46
- class ProjectIdRequiredError extends Error {
47
- /**
48
- * Error raised when a required Google Cloud project is missing during Antigravity onboarding.
49
- */
50
- constructor() {
51
- super(
52
- "Google Antigravity requires a Google Cloud project. Enable the Antigravity API on a project you control, rerun `opencode auth login`, and supply that project ID when prompted.",
53
- );
54
- }
55
- }
56
-
57
- /**
58
- * Builds metadata headers required by the Code Assist API.
59
- */
60
- function buildMetadata(projectId?: string): Record<string, string> {
61
- const metadata: Record<string, string> = {
62
- ideType: CODE_ASSIST_METADATA.ideType,
63
- platform: CODE_ASSIST_METADATA.platform,
64
- pluginType: CODE_ASSIST_METADATA.pluginType,
65
- };
66
- if (projectId) {
67
- metadata.duetProject = projectId;
68
- }
69
- return metadata;
70
- }
71
-
72
- /**
73
- * Selects the default tier ID from the allowed tiers list.
74
- */
75
- function getDefaultTierId(allowedTiers?: AntigravityUserTier[]): string | undefined {
76
- if (!allowedTiers || allowedTiers.length === 0) {
77
- return undefined;
78
- }
79
- for (const tier of allowedTiers) {
80
- if (tier?.isDefault) {
81
- return tier.id;
82
- }
83
- }
84
- return allowedTiers[0]?.id;
85
- }
86
-
87
- /**
88
- * Promise-based delay utility.
89
- */
90
- function wait(ms: number): Promise<void> {
91
- return new Promise(function (resolve) {
92
- setTimeout(resolve, ms);
93
- });
94
- }
95
-
96
- /**
97
- * Extracts the cloudaicompanion project id from loadCodeAssist responses.
98
- */
99
- function extractManagedProjectId(payload: LoadCodeAssistPayload | null): string | undefined {
100
- if (!payload) {
101
- return undefined;
102
- }
103
- if (typeof payload.cloudaicompanionProject === "string") {
104
- return payload.cloudaicompanionProject;
105
- }
106
- if (payload.cloudaicompanionProject && typeof payload.cloudaicompanionProject.id === "string") {
107
- return payload.cloudaicompanionProject.id;
108
- }
109
- return undefined;
110
- }
111
-
112
- /**
113
- * Generates a cache key for project context based on refresh token.
114
- */
115
- function getCacheKey(auth: OAuthAuthDetails): string | undefined {
116
- const refresh = auth.refresh?.trim();
117
- return refresh ? refresh : undefined;
118
- }
119
-
120
- /**
121
- * Clears cached project context results and pending promises, globally or for a refresh key.
122
- */
123
- export function invalidateProjectContextCache(refresh?: string): void {
124
- if (!refresh) {
125
- projectContextPendingCache.clear();
126
- projectContextResultCache.clear();
127
- return;
128
- }
129
- projectContextPendingCache.delete(refresh);
130
- projectContextResultCache.delete(refresh);
131
- }
132
-
133
- /**
134
- * Loads managed project information for the given access token and optional project.
135
- */
136
- export async function loadManagedProject(
137
- accessToken: string,
138
- projectId?: string,
139
- ): Promise<LoadCodeAssistPayload | null> {
140
- const metadata = buildMetadata(projectId);
141
- const requestBody: Record<string, unknown> = { metadata };
142
-
143
- const loadHeaders: Record<string, string> = {
144
- "Content-Type": "application/json",
145
- Authorization: `Bearer ${accessToken}`,
146
- "User-Agent": "google-api-nodejs-client/9.15.1",
147
- "X-Goog-Api-Client": "google-cloud-sdk vscode_cloudshelleditor/0.1",
148
- "Client-Metadata": ANTIGRAVITY_HEADERS["Client-Metadata"],
149
- };
150
-
151
- const loadEndpoints = Array.from(
152
- new Set<string>([...ANTIGRAVITY_LOAD_ENDPOINTS, ...ANTIGRAVITY_ENDPOINT_FALLBACKS]),
153
- );
154
-
155
- for (const baseEndpoint of loadEndpoints) {
156
- try {
157
- const response = await fetch(
158
- `${baseEndpoint}/v1internal:loadCodeAssist`,
159
- {
160
- method: "POST",
161
- headers: loadHeaders,
162
- body: JSON.stringify(requestBody),
163
- },
164
- );
165
-
166
- if (!response.ok) {
167
- continue;
168
- }
169
-
170
- return (await response.json()) as LoadCodeAssistPayload;
171
- } catch (error) {
172
- console.error(`Failed to load Antigravity managed project via ${baseEndpoint}:`, error);
173
- continue;
174
- }
175
- }
176
-
177
- return null;
178
- }
179
-
180
-
181
- /**
182
- * Onboards a managed project for the user, optionally retrying until completion.
183
- */
184
- export async function onboardManagedProject(
185
- accessToken: string,
186
- tierId: string,
187
- projectId?: string,
188
- attempts = 10,
189
- delayMs = 5000,
190
- ): Promise<string | undefined> {
191
- const metadata = buildMetadata(projectId);
192
- const requestBody: Record<string, unknown> = {
193
- tierId,
194
- metadata,
195
- };
196
-
197
- if (tierId !== "FREE") {
198
- if (!projectId) {
199
- throw new ProjectIdRequiredError();
200
- }
201
- requestBody.cloudaicompanionProject = projectId;
202
- }
203
-
204
- for (const baseEndpoint of ANTIGRAVITY_ENDPOINT_FALLBACKS) {
205
- for (let attempt = 0; attempt < attempts; attempt += 1) {
206
- try {
207
- const response = await fetch(
208
- `${baseEndpoint}/v1internal:onboardUser`,
209
- {
210
- method: "POST",
211
- headers: {
212
- "Content-Type": "application/json",
213
- Authorization: `Bearer ${accessToken}`,
214
- ...ANTIGRAVITY_HEADERS,
215
- },
216
- body: JSON.stringify(requestBody),
217
- },
218
- );
219
-
220
- if (!response.ok) {
221
- break;
222
- }
223
-
224
- const payload = (await response.json()) as OnboardUserPayload;
225
- const managedProjectId = payload.response?.cloudaicompanionProject?.id;
226
- if (payload.done && managedProjectId) {
227
- return managedProjectId;
228
- }
229
- if (payload.done && projectId) {
230
- return projectId;
231
- }
232
- } catch (error) {
233
- console.error(
234
- `Failed to onboard Antigravity managed project via ${baseEndpoint}:`,
235
- error,
236
- );
237
- break;
238
- }
239
-
240
- await wait(delayMs);
241
- }
242
- }
243
-
244
- return undefined;
245
- }
246
-
247
- /**
248
- * Resolves an effective project ID for the current auth state, caching results per refresh token.
249
- */
250
- export async function ensureProjectContext(
251
- auth: OAuthAuthDetails,
252
- client: PluginClient,
253
- providerId: string,
254
- ): Promise<ProjectContextResult> {
255
- const accessToken = auth.access;
256
- if (!accessToken) {
257
- return { auth, effectiveProjectId: "" };
258
- }
259
-
260
- const cacheKey = getCacheKey(auth);
261
- if (cacheKey) {
262
- const cached = projectContextResultCache.get(cacheKey);
263
- if (cached) {
264
- return cached;
265
- }
266
- const pending = projectContextPendingCache.get(cacheKey);
267
- if (pending) {
268
- return pending;
269
- }
270
- }
271
-
272
- const resolveContext = async (): Promise<ProjectContextResult> => {
273
- const parts = parseRefreshParts(auth.refresh);
274
- if (parts.managedProjectId) {
275
- return { auth, effectiveProjectId: parts.managedProjectId };
276
- }
277
-
278
- const fallbackProjectId = ANTIGRAVITY_DEFAULT_PROJECT_ID;
279
- const persistManagedProject = async (managedProjectId: string): Promise<ProjectContextResult> => {
280
- const updatedAuth: OAuthAuthDetails = {
281
- ...auth,
282
- refresh: formatRefreshParts({
283
- refreshToken: parts.refreshToken,
284
- projectId: parts.projectId,
285
- managedProjectId,
286
- }),
287
- };
288
-
289
- await client.auth.set({
290
- path: { id: providerId },
291
- body: updatedAuth,
292
- });
293
-
294
- return { auth: updatedAuth, effectiveProjectId: managedProjectId };
295
- };
296
-
297
- // Try to resolve a managed project from Antigravity if possible.
298
- const loadPayload = await loadManagedProject(accessToken, parts.projectId ?? fallbackProjectId);
299
- const resolvedManagedProjectId = extractManagedProjectId(loadPayload);
300
-
301
- if (resolvedManagedProjectId) {
302
- return persistManagedProject(resolvedManagedProjectId);
303
- }
304
-
305
- if (parts.projectId) {
306
- return { auth, effectiveProjectId: parts.projectId };
307
- }
308
-
309
- // No project id present in auth; fall back to the hardcoded id for requests.
310
- return { auth, effectiveProjectId: fallbackProjectId };
311
- };
312
-
313
- if (!cacheKey) {
314
- return resolveContext();
315
- }
316
-
317
- const promise = resolveContext()
318
- .then((result) => {
319
- const nextKey = getCacheKey(result.auth) ?? cacheKey;
320
- projectContextPendingCache.delete(cacheKey);
321
- projectContextResultCache.set(nextKey, result);
322
- if (nextKey !== cacheKey) {
323
- projectContextResultCache.delete(cacheKey);
324
- }
325
- return result;
326
- })
327
- .catch((error) => {
328
- projectContextPendingCache.delete(cacheKey);
329
- throw error;
330
- });
331
-
332
- projectContextPendingCache.set(cacheKey, promise);
333
- return promise;
334
- }