opencode-antigravity-auth 1.0.2 → 1.0.4

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,197 +0,0 @@
1
- const ANTIGRAVITY_PREVIEW_LINK = "https://goo.gle/enable-preview-features"; // TODO: Update to Antigravity link if available
2
-
3
- export interface AntigravityApiError {
4
- code?: number;
5
- message?: string;
6
- status?: string;
7
- [key: string]: unknown;
8
- }
9
-
10
- /**
11
- * Minimal representation of Antigravity API responses we touch.
12
- */
13
- export interface AntigravityApiBody {
14
- response?: unknown;
15
- error?: AntigravityApiError;
16
- [key: string]: unknown;
17
- }
18
-
19
- /**
20
- * Usage metadata exposed by Antigravity responses. Fields are optional to reflect partial payloads.
21
- */
22
- export interface AntigravityUsageMetadata {
23
- totalTokenCount?: number;
24
- promptTokenCount?: number;
25
- candidatesTokenCount?: number;
26
- cachedContentTokenCount?: number;
27
- }
28
-
29
- /**
30
- * Normalized thinking configuration accepted by Antigravity.
31
- */
32
- export interface ThinkingConfig {
33
- thinkingBudget?: number;
34
- includeThoughts?: boolean;
35
- }
36
-
37
- /**
38
- * Ensures thinkingConfig is valid: includeThoughts only allowed when budget > 0.
39
- */
40
- export function normalizeThinkingConfig(config: unknown): ThinkingConfig | undefined {
41
- if (!config || typeof config !== "object") {
42
- return undefined;
43
- }
44
-
45
- const record = config as Record<string, unknown>;
46
- const budgetRaw = record.thinkingBudget ?? record.thinking_budget;
47
- const includeRaw = record.includeThoughts ?? record.include_thoughts;
48
-
49
- const thinkingBudget = typeof budgetRaw === "number" && Number.isFinite(budgetRaw) ? budgetRaw : undefined;
50
- const includeThoughts = typeof includeRaw === "boolean" ? includeRaw : undefined;
51
-
52
- const enableThinking = thinkingBudget !== undefined && thinkingBudget > 0;
53
- const finalInclude = enableThinking ? includeThoughts ?? false : false;
54
-
55
- if (!enableThinking && finalInclude === false && thinkingBudget === undefined && includeThoughts === undefined) {
56
- return undefined;
57
- }
58
-
59
- const normalized: ThinkingConfig = {};
60
- if (thinkingBudget !== undefined) {
61
- normalized.thinkingBudget = thinkingBudget;
62
- }
63
- if (finalInclude !== undefined) {
64
- normalized.includeThoughts = finalInclude;
65
- }
66
- return normalized;
67
- }
68
-
69
- /**
70
- * Parses an Antigravity API body; handles array-wrapped responses the API sometimes returns.
71
- */
72
- export function parseAntigravityApiBody(rawText: string): AntigravityApiBody | null {
73
- try {
74
- const parsed = JSON.parse(rawText);
75
- if (Array.isArray(parsed)) {
76
- const firstObject = parsed.find((item: unknown) => typeof item === "object" && item !== null);
77
- if (firstObject && typeof firstObject === "object") {
78
- return firstObject as AntigravityApiBody;
79
- }
80
- return null;
81
- }
82
-
83
- if (parsed && typeof parsed === "object") {
84
- return parsed as AntigravityApiBody;
85
- }
86
-
87
- return null;
88
- } catch {
89
- return null;
90
- }
91
- }
92
-
93
- /**
94
- * Extracts usageMetadata from a response object, guarding types.
95
- */
96
- export function extractUsageMetadata(body: AntigravityApiBody): AntigravityUsageMetadata | null {
97
- const usage = (body.response && typeof body.response === "object"
98
- ? (body.response as { usageMetadata?: unknown }).usageMetadata
99
- : undefined) as AntigravityUsageMetadata | undefined;
100
-
101
- if (!usage || typeof usage !== "object") {
102
- return null;
103
- }
104
-
105
- const asRecord = usage as Record<string, unknown>;
106
- const toNumber = (value: unknown): number | undefined =>
107
- typeof value === "number" && Number.isFinite(value) ? value : undefined;
108
-
109
- return {
110
- totalTokenCount: toNumber(asRecord.totalTokenCount),
111
- promptTokenCount: toNumber(asRecord.promptTokenCount),
112
- candidatesTokenCount: toNumber(asRecord.candidatesTokenCount),
113
- cachedContentTokenCount: toNumber(asRecord.cachedContentTokenCount),
114
- };
115
- }
116
-
117
- /**
118
- * Walks SSE lines to find a usage-bearing response chunk.
119
- */
120
- export function extractUsageFromSsePayload(payload: string): AntigravityUsageMetadata | null {
121
- const lines = payload.split("\n");
122
- for (const line of lines) {
123
- if (!line.startsWith("data:")) {
124
- continue;
125
- }
126
- const jsonText = line.slice(5).trim();
127
- if (!jsonText) {
128
- continue;
129
- }
130
- try {
131
- const parsed = JSON.parse(jsonText);
132
- if (parsed && typeof parsed === "object") {
133
- const usage = extractUsageMetadata({ response: (parsed as Record<string, unknown>).response });
134
- if (usage) {
135
- return usage;
136
- }
137
- }
138
- } catch {
139
- continue;
140
- }
141
- }
142
- return null;
143
- }
144
-
145
- /**
146
- * Enhances 404 errors for Antigravity models with a direct preview-access message.
147
- */
148
- export function rewriteAntigravityPreviewAccessError(
149
- body: AntigravityApiBody,
150
- status: number,
151
- requestedModel?: string,
152
- ): AntigravityApiBody | null {
153
- if (!needsPreviewAccessOverride(status, body, requestedModel)) {
154
- return null;
155
- }
156
-
157
- const error: AntigravityApiError = body.error ?? {};
158
- const trimmedMessage = typeof error.message === "string" ? error.message.trim() : "";
159
- const messagePrefix = trimmedMessage.length > 0
160
- ? trimmedMessage
161
- : "Antigravity preview features are not enabled for this account.";
162
- const enhancedMessage = `${messagePrefix} Request preview access at ${ANTIGRAVITY_PREVIEW_LINK} before using this model.`;
163
-
164
- return {
165
- ...body,
166
- error: {
167
- ...error,
168
- message: enhancedMessage,
169
- },
170
- };
171
- }
172
-
173
- function needsPreviewAccessOverride(
174
- status: number,
175
- body: AntigravityApiBody,
176
- requestedModel?: string,
177
- ): boolean {
178
- if (status !== 404) {
179
- return false;
180
- }
181
-
182
- if (isAntigravityModel(requestedModel)) {
183
- return true;
184
- }
185
-
186
- const errorMessage = typeof body.error?.message === "string" ? body.error.message : "";
187
- return isAntigravityModel(errorMessage);
188
- }
189
-
190
- function isAntigravityModel(target?: string): boolean {
191
- if (!target) {
192
- return false;
193
- }
194
-
195
- // Check for Antigravity models instead of Gemini 3
196
- return /antigravity/i.test(target) || /opus/i.test(target) || /claude/i.test(target);
197
- }