tuxevil-rotator 4.4.8 → 4.5.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/CHANGELOG.md +40 -0
- package/README.md +3 -1
- package/package.json +1 -1
- package/plugins/opencode-discovery/index.mjs +50 -21
- package/src/access-probe.ts +1 -1
- package/src/account-store.ts +14 -2
- package/src/audio-transcription.ts +1 -0
- package/src/auto-model-access.ts +22 -0
- package/src/compat/model-specs.ts +7 -0
- package/src/compat/schema-sanitizer.ts +420 -287
- package/src/compat.ts +110 -66
- package/src/config-defaults.ts +7 -0
- package/src/fetch-with-retry.ts +294 -16
- package/src/idempotency.ts +3 -1
- package/src/model-access-api.ts +9 -1
- package/src/model-access.ts +2 -2
- package/src/notification-poller.ts +9 -1
- package/src/provider-catalog-evidence.ts +18 -1
- package/src/providers/adapter.ts +10 -0
- package/src/providers/credential-helpers.ts +34 -0
- package/src/providers/google-antigravity/aicode-eligibility.ts +256 -0
- package/src/providers/google-antigravity/aicode-forward.ts +381 -0
- package/src/providers/google-antigravity/aicode.ts +478 -0
- package/src/providers/google-antigravity/catalog.ts +6 -0
- package/src/providers/google-antigravity/code-assist.ts +53 -41
- package/src/providers/google-antigravity/dynamic-catalog.ts +72 -7
- package/src/providers/google-antigravity/forward.ts +309 -86
- package/src/providers/google-antigravity/index.ts +122 -4
- package/src/providers/google-antigravity/oauth.ts +22 -12
- package/src/providers/google-antigravity/quota.ts +781 -34
- package/src/providers/google-antigravity/translators.ts +55 -46
- package/src/providers/google-antigravity/twin-payload.ts +231 -0
- package/src/providers/ollama/quota.ts +6 -4
- package/src/providers/openai-codex/compat.ts +11 -2
- package/src/providers/openai-codex/quota.ts +2 -1
- package/src/providers/opencode-zen/catalog.ts +159 -13
- package/src/providers/opencode-zen/discovery.ts +50 -0
- package/src/providers/opencode-zen/index.ts +7 -2
- package/src/providers/opencode-zen/quota.ts +14 -1
- package/src/proxy.ts +570 -87
- package/src/quota-pools.ts +76 -1
- package/src/rate-limit-parser.ts +239 -19
- package/src/rotator.ts +848 -297
- package/src/stream-guards.ts +87 -1
- package/src/telemetry.ts +17 -3
- package/src/types.ts +184 -9
- package/src/validators.ts +3 -0
- package/src/web/lib/accounts.ts +2 -0
- package/src/web/lib/attention.ts +19 -0
- package/src/web/lib/models.ts +6 -1
- package/src/web/lib/status.ts +22 -4
- package/src/web/pages/AccountDetail.tsx +49 -2
- package/src/web/pages/Accounts.tsx +38 -3
- package/src/web/pages/ModelAccess.tsx +4 -0
- package/src/web/pages/Models.tsx +2 -1
- package/src/web/pages/Overview.tsx +15 -1
- package/src/web/styles/app.css +73 -3
- package/tools/telemetry-receiver/receiver.js +523 -266
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
// Google AICode (aicode.googleapis.com / v1alpha) client helpers:
|
|
2
|
+
// management discovery (fetchConfig, fetchLicenses), entitlement resolution,
|
|
3
|
+
// and 1h scope-cooldown tracking when ACCESS_TOKEN_SCOPE_INSUFFICIENT occurs.
|
|
4
|
+
|
|
5
|
+
import { getAccountIdentity, getCredentialGeneration } from "../../account-identity.js";
|
|
6
|
+
import { fetchWithRetry, type RequestInitWithDispatcher } from "../../fetch-with-retry.js";
|
|
7
|
+
import { logger } from "../../logger.js";
|
|
8
|
+
import { AICODE_ENDPOINTS, REQUEST_USER_AGENT } from "../../types.js";
|
|
9
|
+
import type { AccountRuntime } from "../../types.js";
|
|
10
|
+
import { DEFAULT_PROVIDER } from "../credential-helpers.js";
|
|
11
|
+
import { getAccountProxyDispatcher } from "../proxy-dispatcher.js";
|
|
12
|
+
import {
|
|
13
|
+
INFERENCE_TRAFFIC_MIGRATION_FLAG,
|
|
14
|
+
parseExperimentBoolFlag,
|
|
15
|
+
parseUseAicodeMendel,
|
|
16
|
+
} from "./aicode-eligibility.js";
|
|
17
|
+
|
|
18
|
+
const aicodeLogger = logger.child("google-aicode");
|
|
19
|
+
|
|
20
|
+
export const AICODE_SCOPE_COOLDOWN_MS = 60 * 60 * 1000;
|
|
21
|
+
export const AICODE_DISCOVERY_CACHE_TTL_MS = 5 * 60 * 1000;
|
|
22
|
+
|
|
23
|
+
export type AicodeConfigSnapshot = NonNullable<
|
|
24
|
+
NonNullable<AccountRuntime["aicodeState"]>["config"]
|
|
25
|
+
>;
|
|
26
|
+
export type AicodeLicenseSnapshot = NonNullable<
|
|
27
|
+
NonNullable<AccountRuntime["aicodeState"]>["licenses"]
|
|
28
|
+
>;
|
|
29
|
+
|
|
30
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
31
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function resolveAicodeEndpoint(): string {
|
|
35
|
+
const override = process.env.AICODE_ENDPOINT_URL?.trim();
|
|
36
|
+
const base = override || AICODE_ENDPOINTS[0] || "https://aicode.googleapis.com";
|
|
37
|
+
return base.replace(/\/+$/, "");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function buildAicodeWireHeaders(
|
|
41
|
+
account: AccountRuntime,
|
|
42
|
+
originalHeaders: Record<string, string> = {},
|
|
43
|
+
): Record<string, string> {
|
|
44
|
+
const headers: Record<string, string> = {
|
|
45
|
+
"Content-Type": "application/json",
|
|
46
|
+
Authorization: `Bearer ${account.accessToken ?? ""}`,
|
|
47
|
+
"User-Agent": REQUEST_USER_AGENT,
|
|
48
|
+
};
|
|
49
|
+
let requestReason = "";
|
|
50
|
+
let userProjectHeader = "";
|
|
51
|
+
for (const [key, value] of Object.entries(originalHeaders)) {
|
|
52
|
+
const lower = key.toLowerCase();
|
|
53
|
+
if (lower === "x-goog-request-reason" && typeof value === "string" && value.trim()) {
|
|
54
|
+
requestReason = value.trim();
|
|
55
|
+
} else if (lower === "x-goog-user-project" && typeof value === "string" && value.trim()) {
|
|
56
|
+
userProjectHeader = value.trim();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
requestReason ||= process.env.GOOG_REQUEST_REASON?.trim() || "";
|
|
60
|
+
userProjectHeader ||=
|
|
61
|
+
process.env.AICODE_DEV_PROJECT?.trim() ||
|
|
62
|
+
process.env.GOOG_USER_PROJECT?.trim() ||
|
|
63
|
+
"";
|
|
64
|
+
if (requestReason) {
|
|
65
|
+
headers["X-Goog-Request-Reason"] = requestReason;
|
|
66
|
+
}
|
|
67
|
+
if (userProjectHeader) {
|
|
68
|
+
headers["X-Goog-User-Project"] = userProjectHeader;
|
|
69
|
+
}
|
|
70
|
+
return headers;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function isAicodeInsufficientScopeError(
|
|
74
|
+
status: number,
|
|
75
|
+
errorText: string,
|
|
76
|
+
): boolean {
|
|
77
|
+
return status === 403 && errorText.includes("ACCESS_TOKEN_SCOPE_INSUFFICIENT");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function ensureAicodeState(account: AccountRuntime): NonNullable<AccountRuntime["aicodeState"]> {
|
|
81
|
+
const gen = getCredentialGeneration(account, DEFAULT_PROVIDER);
|
|
82
|
+
if (!account.aicodeState || account.aicodeState.credentialGeneration !== gen) {
|
|
83
|
+
account.aicodeState = { credentialGeneration: gen };
|
|
84
|
+
}
|
|
85
|
+
return account.aicodeState;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function clearAicodeState(account: AccountRuntime): void {
|
|
89
|
+
delete account.aicodeState;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function isInAicodeScopeCooldown(
|
|
93
|
+
account: AccountRuntime,
|
|
94
|
+
now = Date.now(),
|
|
95
|
+
): boolean {
|
|
96
|
+
const gen = getCredentialGeneration(account, DEFAULT_PROVIDER);
|
|
97
|
+
const state = account.aicodeState;
|
|
98
|
+
if (!state || state.credentialGeneration !== gen) return false;
|
|
99
|
+
return typeof state.scopeCooldownUntil === "number" && state.scopeCooldownUntil > now;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function markAicodeScopeMissing(
|
|
103
|
+
account: AccountRuntime,
|
|
104
|
+
now = Date.now(),
|
|
105
|
+
): number {
|
|
106
|
+
const state = ensureAicodeState(account);
|
|
107
|
+
const until = now + AICODE_SCOPE_COOLDOWN_MS;
|
|
108
|
+
const wasCooling = typeof state.scopeCooldownUntil === "number" && state.scopeCooldownUntil > now;
|
|
109
|
+
state.scopeCooldownUntil = until;
|
|
110
|
+
if (!wasCooling) {
|
|
111
|
+
const label = account.config.label || account.config.email;
|
|
112
|
+
aicodeLogger.log(
|
|
113
|
+
"warn",
|
|
114
|
+
`[${label}] OAuth token lacks aicode scope (ACCESS_TOKEN_SCOPE_INSUFFICIENT); routing AICode traffic to CCPA for 60m`,
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
return until;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function parseAicodeFetchConfig(
|
|
121
|
+
raw: unknown,
|
|
122
|
+
credentialGeneration: string,
|
|
123
|
+
fetchedAt = Date.now(),
|
|
124
|
+
): AicodeConfigSnapshot | null {
|
|
125
|
+
if (!isRecord(raw)) return null;
|
|
126
|
+
const experiencesByLowerName: Record<string, string> = Object.create(null);
|
|
127
|
+
const experienceMetaByLowerName: Record<
|
|
128
|
+
string,
|
|
129
|
+
{ modelId?: string; displayName?: string }
|
|
130
|
+
> = Object.create(null);
|
|
131
|
+
const experienceNames: string[] = [];
|
|
132
|
+
const rawExperiences = Array.isArray(raw.availableExperiences)
|
|
133
|
+
? raw.availableExperiences
|
|
134
|
+
: [];
|
|
135
|
+
for (const entry of rawExperiences) {
|
|
136
|
+
if (!isRecord(entry)) continue;
|
|
137
|
+
const name = typeof entry.name === "string" ? entry.name.trim() : "";
|
|
138
|
+
if (!name || !isRecord(entry.model)) continue;
|
|
139
|
+
const lower = name.toLowerCase();
|
|
140
|
+
if (!Object.hasOwn(experiencesByLowerName, lower)) {
|
|
141
|
+
experiencesByLowerName[lower] = name;
|
|
142
|
+
experienceNames.push(name);
|
|
143
|
+
const modelId =
|
|
144
|
+
typeof entry.model.modelId === "string" && entry.model.modelId.trim()
|
|
145
|
+
? entry.model.modelId.trim()
|
|
146
|
+
: undefined;
|
|
147
|
+
const displayName =
|
|
148
|
+
typeof entry.model.displayName === "string" && entry.model.displayName.trim()
|
|
149
|
+
? entry.model.displayName.trim()
|
|
150
|
+
: undefined;
|
|
151
|
+
experienceMetaByLowerName[lower] = {
|
|
152
|
+
...(modelId ? { modelId } : {}),
|
|
153
|
+
...(displayName ? { displayName } : {}),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const useAicodeMendel = isRecord(raw.experimentConfig)
|
|
158
|
+
? parseUseAicodeMendel(raw.experimentConfig)
|
|
159
|
+
: null;
|
|
160
|
+
const inferenceTrafficMigration = isRecord(raw.experimentConfig)
|
|
161
|
+
? parseExperimentBoolFlag(raw.experimentConfig, INFERENCE_TRAFFIC_MIGRATION_FLAG)
|
|
162
|
+
: null;
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
credentialGeneration,
|
|
166
|
+
fetchedAt,
|
|
167
|
+
experiencesByLowerName,
|
|
168
|
+
experienceNames,
|
|
169
|
+
experienceMetaByLowerName,
|
|
170
|
+
useAicodeMendel,
|
|
171
|
+
inferenceTrafficMigration,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function parseAicodeFetchLicenses(
|
|
176
|
+
raw: unknown,
|
|
177
|
+
credentialGeneration: string,
|
|
178
|
+
fetchedAt = Date.now(),
|
|
179
|
+
): AicodeLicenseSnapshot | null {
|
|
180
|
+
if (!isRecord(raw)) return null;
|
|
181
|
+
const licenses = Array.isArray(raw.licenses) ? raw.licenses : [];
|
|
182
|
+
let userTier: string | null = null;
|
|
183
|
+
let tierDisplayName: string | null = null;
|
|
184
|
+
for (const entry of licenses) {
|
|
185
|
+
if (!isRecord(entry)) continue;
|
|
186
|
+
const tier = typeof entry.userTier === "string" ? entry.userTier.trim() : "";
|
|
187
|
+
if (tier) {
|
|
188
|
+
userTier = tier;
|
|
189
|
+
tierDisplayName =
|
|
190
|
+
typeof entry.tierDisplayName === "string" && entry.tierDisplayName.trim()
|
|
191
|
+
? entry.tierDisplayName.trim()
|
|
192
|
+
: null;
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
credentialGeneration,
|
|
198
|
+
fetchedAt,
|
|
199
|
+
userTier,
|
|
200
|
+
tierDisplayName,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function resolveAicodeExperience(
|
|
205
|
+
config: AicodeConfigSnapshot | undefined,
|
|
206
|
+
model: string,
|
|
207
|
+
): string | null {
|
|
208
|
+
if (!config) return null;
|
|
209
|
+
const normalized = model.trim().toLowerCase();
|
|
210
|
+
if (!normalized) return null;
|
|
211
|
+
return Object.hasOwn(config.experiencesByLowerName, normalized)
|
|
212
|
+
? config.experiencesByLowerName[normalized]
|
|
213
|
+
: null;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const inFlightConfigFetches = new Map<string, Promise<AicodeConfigSnapshot | null>>();
|
|
217
|
+
const inFlightLicenseFetches = new Map<string, Promise<AicodeLicenseSnapshot | null>>();
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Wait for a coalesced discovery flight while honoring only this caller's
|
|
221
|
+
* abort signal. The shared fetch itself never uses a caller signal, so one
|
|
222
|
+
* disconnecting client cannot abort the lookup for concurrent callers.
|
|
223
|
+
*/
|
|
224
|
+
function awaitSharedFlight<T>(flight: Promise<T>, signal?: AbortSignal): Promise<T> {
|
|
225
|
+
if (!signal) return flight;
|
|
226
|
+
if (signal.aborted) {
|
|
227
|
+
return Promise.reject(signal.reason ?? new DOMException("Aborted", "AbortError"));
|
|
228
|
+
}
|
|
229
|
+
return new Promise<T>((resolve, reject) => {
|
|
230
|
+
const onAbort = () => reject(signal.reason ?? new DOMException("Aborted", "AbortError"));
|
|
231
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
232
|
+
flight.then(
|
|
233
|
+
(value) => {
|
|
234
|
+
signal.removeEventListener("abort", onAbort);
|
|
235
|
+
resolve(value);
|
|
236
|
+
},
|
|
237
|
+
(err: unknown) => {
|
|
238
|
+
signal.removeEventListener("abort", onAbort);
|
|
239
|
+
reject(err);
|
|
240
|
+
},
|
|
241
|
+
);
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export async function fetchAicodeConfig(
|
|
246
|
+
account: AccountRuntime,
|
|
247
|
+
originalHeaders: Record<string, string> = {},
|
|
248
|
+
signal?: AbortSignal,
|
|
249
|
+
options?: { force?: boolean; now?: number },
|
|
250
|
+
): Promise<AicodeConfigSnapshot | null> {
|
|
251
|
+
if (!account.accessToken) return null;
|
|
252
|
+
const now = options?.now ?? Date.now();
|
|
253
|
+
const gen = getCredentialGeneration(account, DEFAULT_PROVIDER);
|
|
254
|
+
const state = ensureAicodeState(account);
|
|
255
|
+
if (
|
|
256
|
+
!options?.force &&
|
|
257
|
+
state.config &&
|
|
258
|
+
state.config.credentialGeneration === gen &&
|
|
259
|
+
now - state.config.fetchedAt < AICODE_DISCOVERY_CACHE_TTL_MS
|
|
260
|
+
) {
|
|
261
|
+
return state.config;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const flightKey = `${getAccountIdentity(account)}:${gen}`;
|
|
265
|
+
const existing = inFlightConfigFetches.get(flightKey);
|
|
266
|
+
if (existing) {
|
|
267
|
+
return awaitSharedFlight(existing, signal);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const promise = (async (): Promise<AicodeConfigSnapshot | null> => {
|
|
271
|
+
try {
|
|
272
|
+
const endpoint = resolveAicodeEndpoint();
|
|
273
|
+
const response = await fetchWithRetry(`${endpoint}/v1alpha:fetchConfig`, {
|
|
274
|
+
method: "GET",
|
|
275
|
+
headers: buildAicodeWireHeaders(account, originalHeaders),
|
|
276
|
+
timeoutMs: 8000,
|
|
277
|
+
retries: 0,
|
|
278
|
+
dispatcher: getAccountProxyDispatcher(account, DEFAULT_PROVIDER),
|
|
279
|
+
} as RequestInitWithDispatcher);
|
|
280
|
+
|
|
281
|
+
if (getCredentialGeneration(account, DEFAULT_PROVIDER) !== gen) {
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (!response.ok) {
|
|
286
|
+
const errorText = await response.text().catch(() => "");
|
|
287
|
+
if (isAicodeInsufficientScopeError(response.status, errorText)) {
|
|
288
|
+
markAicodeScopeMissing(account);
|
|
289
|
+
}
|
|
290
|
+
return state.config?.credentialGeneration === gen ? state.config : null;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const raw = await response.json().catch(() => null);
|
|
294
|
+
if (getCredentialGeneration(account, DEFAULT_PROVIDER) !== gen) {
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
const parsed = parseAicodeFetchConfig(raw, gen, Date.now());
|
|
298
|
+
if (parsed) {
|
|
299
|
+
ensureAicodeState(account).config = parsed;
|
|
300
|
+
}
|
|
301
|
+
return parsed;
|
|
302
|
+
} catch {
|
|
303
|
+
return state.config?.credentialGeneration === gen ? state.config : null;
|
|
304
|
+
}
|
|
305
|
+
})().finally(() => {
|
|
306
|
+
inFlightConfigFetches.delete(flightKey);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
inFlightConfigFetches.set(flightKey, promise);
|
|
310
|
+
return awaitSharedFlight(promise, signal);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export async function fetchAicodeLicenses(
|
|
314
|
+
account: AccountRuntime,
|
|
315
|
+
originalHeaders: Record<string, string> = {},
|
|
316
|
+
signal?: AbortSignal,
|
|
317
|
+
options?: { force?: boolean; now?: number },
|
|
318
|
+
): Promise<AicodeLicenseSnapshot | null> {
|
|
319
|
+
if (!account.accessToken) return null;
|
|
320
|
+
const now = options?.now ?? Date.now();
|
|
321
|
+
const gen = getCredentialGeneration(account, DEFAULT_PROVIDER);
|
|
322
|
+
const state = ensureAicodeState(account);
|
|
323
|
+
if (
|
|
324
|
+
!options?.force &&
|
|
325
|
+
state.licenses &&
|
|
326
|
+
state.licenses.credentialGeneration === gen &&
|
|
327
|
+
now - state.licenses.fetchedAt < AICODE_DISCOVERY_CACHE_TTL_MS
|
|
328
|
+
) {
|
|
329
|
+
return state.licenses;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const flightKey = `${getAccountIdentity(account)}:${gen}`;
|
|
333
|
+
const existing = inFlightLicenseFetches.get(flightKey);
|
|
334
|
+
if (existing) {
|
|
335
|
+
return awaitSharedFlight(existing, signal);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const promise = (async (): Promise<AicodeLicenseSnapshot | null> => {
|
|
339
|
+
try {
|
|
340
|
+
const endpoint = resolveAicodeEndpoint();
|
|
341
|
+
const response = await fetchWithRetry(`${endpoint}/v1alpha:fetchLicenses`, {
|
|
342
|
+
method: "GET",
|
|
343
|
+
headers: buildAicodeWireHeaders(account, originalHeaders),
|
|
344
|
+
timeoutMs: 8000,
|
|
345
|
+
retries: 0,
|
|
346
|
+
dispatcher: getAccountProxyDispatcher(account, DEFAULT_PROVIDER),
|
|
347
|
+
} as RequestInitWithDispatcher);
|
|
348
|
+
|
|
349
|
+
if (getCredentialGeneration(account, DEFAULT_PROVIDER) !== gen) {
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (!response.ok) {
|
|
354
|
+
const errorText = await response.text().catch(() => "");
|
|
355
|
+
if (isAicodeInsufficientScopeError(response.status, errorText)) {
|
|
356
|
+
markAicodeScopeMissing(account);
|
|
357
|
+
}
|
|
358
|
+
return state.licenses?.credentialGeneration === gen ? state.licenses : null;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const raw = await response.json().catch(() => null);
|
|
362
|
+
if (getCredentialGeneration(account, DEFAULT_PROVIDER) !== gen) {
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
const parsed = parseAicodeFetchLicenses(raw, gen, Date.now());
|
|
366
|
+
if (parsed) {
|
|
367
|
+
ensureAicodeState(account).licenses = parsed;
|
|
368
|
+
}
|
|
369
|
+
return parsed;
|
|
370
|
+
} catch {
|
|
371
|
+
return state.licenses?.credentialGeneration === gen ? state.licenses : null;
|
|
372
|
+
}
|
|
373
|
+
})().finally(() => {
|
|
374
|
+
inFlightLicenseFetches.delete(flightKey);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
inFlightLicenseFetches.set(flightKey, promise);
|
|
378
|
+
return awaitSharedFlight(promise, signal);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
export interface AicodeQuotaBucket {
|
|
382
|
+
modelId: string;
|
|
383
|
+
displayName?: string;
|
|
384
|
+
description?: string;
|
|
385
|
+
remainingFraction?: number;
|
|
386
|
+
resetTime?: string;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export interface AicodeQuotaStatusResult {
|
|
390
|
+
buckets: AicodeQuotaBucket[];
|
|
391
|
+
quotaSummary: unknown;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
export function parseAicodeQuotaStatus(raw: unknown): AicodeQuotaStatusResult | null {
|
|
395
|
+
if (!isRecord(raw)) return null;
|
|
396
|
+
const rawBuckets = Array.isArray(raw.buckets) ? raw.buckets : [];
|
|
397
|
+
const buckets: AicodeQuotaBucket[] = [];
|
|
398
|
+
for (const entry of rawBuckets) {
|
|
399
|
+
if (!isRecord(entry) || !isRecord(entry.model)) continue;
|
|
400
|
+
const modelId =
|
|
401
|
+
typeof entry.model.modelId === "string" ? entry.model.modelId.trim() : "";
|
|
402
|
+
if (!modelId) continue;
|
|
403
|
+
const fraction =
|
|
404
|
+
typeof entry.remainingFraction === "number" &&
|
|
405
|
+
Number.isFinite(entry.remainingFraction) &&
|
|
406
|
+
entry.remainingFraction >= 0 &&
|
|
407
|
+
entry.remainingFraction <= 1
|
|
408
|
+
? entry.remainingFraction
|
|
409
|
+
: undefined;
|
|
410
|
+
const resetTime =
|
|
411
|
+
typeof entry.resetTime === "string" && entry.resetTime.trim()
|
|
412
|
+
? entry.resetTime.trim()
|
|
413
|
+
: undefined;
|
|
414
|
+
const displayName =
|
|
415
|
+
typeof entry.model.displayName === "string" && entry.model.displayName.trim()
|
|
416
|
+
? entry.model.displayName.trim()
|
|
417
|
+
: undefined;
|
|
418
|
+
const description =
|
|
419
|
+
typeof entry.model.description === "string" && entry.model.description.trim()
|
|
420
|
+
? entry.model.description.trim()
|
|
421
|
+
: undefined;
|
|
422
|
+
buckets.push({
|
|
423
|
+
modelId,
|
|
424
|
+
...(displayName ? { displayName } : {}),
|
|
425
|
+
...(description ? { description } : {}),
|
|
426
|
+
...(fraction !== undefined ? { remainingFraction: fraction } : {}),
|
|
427
|
+
...(resetTime ? { resetTime } : {}),
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
if (buckets.length === 0 && !isRecord(raw.quotaSummary)) {
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
433
|
+
return {
|
|
434
|
+
buckets,
|
|
435
|
+
quotaSummary: isRecord(raw.quotaSummary) ? raw.quotaSummary : null,
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
export async function fetchAicodeQuotaStatus(
|
|
440
|
+
account: AccountRuntime,
|
|
441
|
+
originalHeaders: Record<string, string> = {},
|
|
442
|
+
signal?: AbortSignal,
|
|
443
|
+
): Promise<AicodeQuotaStatusResult | null> {
|
|
444
|
+
if (!account.accessToken) return null;
|
|
445
|
+
const gen = getCredentialGeneration(account, DEFAULT_PROVIDER);
|
|
446
|
+
try {
|
|
447
|
+
const endpoint = resolveAicodeEndpoint();
|
|
448
|
+
const response = await fetchWithRetry(`${endpoint}/v1alpha:fetchQuotaStatus`, {
|
|
449
|
+
method: "GET",
|
|
450
|
+
headers: buildAicodeWireHeaders(account, originalHeaders),
|
|
451
|
+
timeoutMs: 8000,
|
|
452
|
+
retries: 0,
|
|
453
|
+
signal,
|
|
454
|
+
dispatcher: getAccountProxyDispatcher(account, DEFAULT_PROVIDER),
|
|
455
|
+
} as RequestInitWithDispatcher);
|
|
456
|
+
|
|
457
|
+
if (getCredentialGeneration(account, DEFAULT_PROVIDER) !== gen) {
|
|
458
|
+
return null;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (!response.ok) {
|
|
462
|
+
const errorText = await response.text().catch(() => "");
|
|
463
|
+
if (isAicodeInsufficientScopeError(response.status, errorText)) {
|
|
464
|
+
markAicodeScopeMissing(account);
|
|
465
|
+
}
|
|
466
|
+
return null;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const raw = await response.json().catch(() => null);
|
|
470
|
+
if (getCredentialGeneration(account, DEFAULT_PROVIDER) !== gen) {
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
473
|
+
return parseAicodeQuotaStatus(raw);
|
|
474
|
+
} catch (err) {
|
|
475
|
+
if (signal?.aborted) throw err;
|
|
476
|
+
return null;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
@@ -28,10 +28,12 @@ export interface AntigravityModelSpec {
|
|
|
28
28
|
export const ANTIGRAVITY_CONTEXT_WINDOWS: Record<string, number> = {
|
|
29
29
|
// Anthropic Claude 5.5 family — 1M context window per Anthropic docs.
|
|
30
30
|
"claude-opus-5-5": 1_000_000,
|
|
31
|
+
"claude-opus-5-5-max": 1_000_000,
|
|
31
32
|
"claude-opus-5-5-high": 1_000_000,
|
|
32
33
|
"claude-opus-5-5-medium": 1_000_000,
|
|
33
34
|
"claude-opus-5-5-low": 1_000_000,
|
|
34
35
|
"claude-sonnet-5-5": 1_000_000,
|
|
36
|
+
"claude-sonnet-5-5-max": 1_000_000,
|
|
35
37
|
"claude-sonnet-5-5-high": 1_000_000,
|
|
36
38
|
"claude-sonnet-5-5-medium": 1_000_000,
|
|
37
39
|
"claude-sonnet-5-5-low": 1_000_000,
|
|
@@ -82,6 +84,9 @@ export const ANTIGRAVITY_CONTEXT_WINDOWS: Record<string, number> = {
|
|
|
82
84
|
"gemini-3.8-flash-medium": 1_000_000,
|
|
83
85
|
"gemini-3.8-flash-low": 1_000_000,
|
|
84
86
|
"gemini-3.7-flash": 1_000_000,
|
|
87
|
+
"gemini-3.7-flash-high": 1_000_000,
|
|
88
|
+
"gemini-3.7-flash-medium": 1_000_000,
|
|
89
|
+
"gemini-3.7-flash-low": 1_000_000,
|
|
85
90
|
"gemini-3.7-flash-tiered": 1_000_000,
|
|
86
91
|
"gemini-3.6-flash": 1_000_000,
|
|
87
92
|
"gemini-3.6-flash-high": 1_000_000,
|
|
@@ -97,6 +102,7 @@ export const ANTIGRAVITY_CONTEXT_WINDOWS: Record<string, number> = {
|
|
|
97
102
|
"gemini-3.1-pro-high": 1_000_000,
|
|
98
103
|
"gemini-3.1-pro-low": 1_000_000,
|
|
99
104
|
"gemini-3.1-pro-preview": 1_000_000,
|
|
105
|
+
"gemini-3.1-pro-preview-customtools": 1_000_000,
|
|
100
106
|
// Google Gemini 2.5 — 1M context window per Google docs.
|
|
101
107
|
"gemini-2.5-pro": 1_000_000,
|
|
102
108
|
"gemini-2.5-flash": 1_000_000,
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
|
+
ANTIGRAVITY_CLIENT_METADATA,
|
|
2
3
|
ANTIGRAVITY_ENDPOINTS,
|
|
3
|
-
REQUEST_CLIENT_METADATA,
|
|
4
|
-
REQUEST_GOOG_API_CLIENT,
|
|
5
|
-
REQUEST_USER_AGENT,
|
|
6
4
|
} from "../../types.js";
|
|
7
5
|
import type { AccountRuntime } from "../../types.js";
|
|
8
6
|
import type { ForwardedResponse } from "../../proxy.js";
|
|
9
7
|
import { fetchWithRetry } from "../../fetch-with-retry.js";
|
|
10
8
|
import { DEFAULT_PROVIDER, getProviderProjectId } from "../credential-helpers.js";
|
|
11
9
|
import { getAccountProxyDispatcher } from "../proxy-dispatcher.js";
|
|
10
|
+
import { buildAntigravityWireHeaders } from "./forward.js";
|
|
12
11
|
|
|
13
12
|
export const CODE_ASSIST_ACTIONS = [
|
|
14
13
|
"loadCodeAssist",
|
|
@@ -43,8 +42,14 @@ export function validateCodeAssistPayload(
|
|
|
43
42
|
): void {
|
|
44
43
|
const object = requireObjectBody(action, body);
|
|
45
44
|
if (action === "onboardUser") {
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
const tierId =
|
|
46
|
+
typeof object.tierId === "string" && object.tierId.trim()
|
|
47
|
+
? object.tierId.trim()
|
|
48
|
+
: typeof object.tier_id === "string" && object.tier_id.trim()
|
|
49
|
+
? object.tier_id.trim()
|
|
50
|
+
: "";
|
|
51
|
+
if (!tierId) {
|
|
52
|
+
throw new Error("Code Assist onboardUser requires tierId");
|
|
48
53
|
}
|
|
49
54
|
}
|
|
50
55
|
if (action === "countTokens") {
|
|
@@ -57,37 +62,28 @@ export function validateCodeAssistPayload(
|
|
|
57
62
|
}
|
|
58
63
|
}
|
|
59
64
|
|
|
60
|
-
function
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
"user-agent",
|
|
78
|
-
"x-goog-api-client",
|
|
79
|
-
"client-metadata",
|
|
80
|
-
]);
|
|
81
|
-
for (const key of Object.keys(headers)) {
|
|
82
|
-
if (hopByHop.has(key.toLowerCase())) delete headers[key];
|
|
65
|
+
function normalizeClientMetadata(raw: unknown): Record<string, unknown> {
|
|
66
|
+
if (!isRecord(raw)) return { ...ANTIGRAVITY_CLIENT_METADATA };
|
|
67
|
+
const normalized: Record<string, unknown> = { ...raw };
|
|
68
|
+
const renamePairs: Array<[string, string]> = [
|
|
69
|
+
["ide_type", "ideType"],
|
|
70
|
+
["ide_version", "ideVersion"],
|
|
71
|
+
["plugin_version", "pluginVersion"],
|
|
72
|
+
["update_channel", "updateChannel"],
|
|
73
|
+
["duet_project", "duetProject"],
|
|
74
|
+
["plugin_type", "pluginType"],
|
|
75
|
+
["ide_name", "ideName"],
|
|
76
|
+
];
|
|
77
|
+
for (const [snake, camel] of renamePairs) {
|
|
78
|
+
if (snake in normalized) {
|
|
79
|
+
if (!(camel in normalized)) normalized[camel] = normalized[snake];
|
|
80
|
+
delete normalized[snake];
|
|
81
|
+
}
|
|
83
82
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
headers["X-Goog-Api-Client"] = REQUEST_GOOG_API_CLIENT;
|
|
89
|
-
headers["Client-Metadata"] = REQUEST_CLIENT_METADATA;
|
|
90
|
-
return headers;
|
|
83
|
+
return {
|
|
84
|
+
...ANTIGRAVITY_CLIENT_METADATA,
|
|
85
|
+
...normalized,
|
|
86
|
+
};
|
|
91
87
|
}
|
|
92
88
|
|
|
93
89
|
function buildUpstreamBody(
|
|
@@ -95,18 +91,36 @@ function buildUpstreamBody(
|
|
|
95
91
|
action: CodeAssistAction,
|
|
96
92
|
body: Record<string, unknown>,
|
|
97
93
|
): Record<string, unknown> {
|
|
94
|
+
const normalizedBody: Record<string, unknown> = { ...body };
|
|
95
|
+
if (action === "onboardUser") {
|
|
96
|
+
if (!("tierId" in normalizedBody) && typeof normalizedBody.tier_id === "string") {
|
|
97
|
+
normalizedBody.tierId = normalizedBody.tier_id;
|
|
98
|
+
}
|
|
99
|
+
delete normalizedBody.tier_id;
|
|
100
|
+
if (
|
|
101
|
+
!("cloudaicompanionProject" in normalizedBody) &&
|
|
102
|
+
typeof normalizedBody.cloudaicompanion_project === "string"
|
|
103
|
+
) {
|
|
104
|
+
normalizedBody.cloudaicompanionProject =
|
|
105
|
+
normalizedBody.cloudaicompanion_project;
|
|
106
|
+
}
|
|
107
|
+
delete normalizedBody.cloudaicompanion_project;
|
|
108
|
+
normalizedBody.metadata = normalizeClientMetadata(normalizedBody.metadata);
|
|
109
|
+
} else if (action === "loadCodeAssist") {
|
|
110
|
+
normalizedBody.metadata = normalizeClientMetadata(normalizedBody.metadata);
|
|
111
|
+
}
|
|
98
112
|
const projectScoped = new Set([
|
|
99
113
|
"fetchAvailableModels",
|
|
100
114
|
"countTokens",
|
|
101
115
|
"retrieveUserQuota",
|
|
102
116
|
"retrieveUserQuotaSummary",
|
|
103
117
|
]);
|
|
104
|
-
if (!projectScoped.has(action)) return
|
|
118
|
+
if (!projectScoped.has(action)) return normalizedBody;
|
|
105
119
|
const projectId = getProviderProjectId(account.config, DEFAULT_PROVIDER);
|
|
106
120
|
if (!projectId) {
|
|
107
121
|
throw new Error(`Code Assist ${action} requires the active account projectId`);
|
|
108
122
|
}
|
|
109
|
-
return { ...
|
|
123
|
+
return { ...normalizedBody, project: projectId };
|
|
110
124
|
}
|
|
111
125
|
|
|
112
126
|
export async function forwardCodeAssistRequest(
|
|
@@ -122,7 +136,7 @@ export async function forwardCodeAssistRequest(
|
|
|
122
136
|
action,
|
|
123
137
|
body as Record<string, unknown>,
|
|
124
138
|
);
|
|
125
|
-
const headers =
|
|
139
|
+
const headers = buildAntigravityWireHeaders(account, originalHeaders);
|
|
126
140
|
let lastError: unknown;
|
|
127
141
|
|
|
128
142
|
for (let index = 0; index < ANTIGRAVITY_ENDPOINTS.length; index++) {
|
|
@@ -141,9 +155,7 @@ export async function forwardCodeAssistRequest(
|
|
|
141
155
|
},
|
|
142
156
|
);
|
|
143
157
|
if (
|
|
144
|
-
|
|
145
|
-
response.status === 403 ||
|
|
146
|
-
response.status === 404) &&
|
|
158
|
+
response.status === 404 &&
|
|
147
159
|
index < ANTIGRAVITY_ENDPOINTS.length - 1
|
|
148
160
|
) {
|
|
149
161
|
await response.body?.cancel().catch(() => undefined);
|