copilot-api-plus 1.0.35 → 1.0.37
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/dist/main.js +64 -12
- package/dist/main.js.map +1 -1
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -1831,6 +1831,49 @@ app$1.post("/", async (c) => {
|
|
|
1831
1831
|
});
|
|
1832
1832
|
const antigravityChatCompletionsRoute = app$1;
|
|
1833
1833
|
|
|
1834
|
+
//#endregion
|
|
1835
|
+
//#region src/lib/request-queue.ts
|
|
1836
|
+
var RequestQueue = class {
|
|
1837
|
+
queue = [];
|
|
1838
|
+
activeCount = 0;
|
|
1839
|
+
maxConcurrent;
|
|
1840
|
+
minDelayMs;
|
|
1841
|
+
lastRequestTime = 0;
|
|
1842
|
+
constructor(maxConcurrent = 2, minDelayMs = 300) {
|
|
1843
|
+
this.maxConcurrent = maxConcurrent;
|
|
1844
|
+
this.minDelayMs = minDelayMs;
|
|
1845
|
+
}
|
|
1846
|
+
async enqueue(execute) {
|
|
1847
|
+
return new Promise((resolve, reject) => {
|
|
1848
|
+
this.queue.push({
|
|
1849
|
+
execute,
|
|
1850
|
+
resolve,
|
|
1851
|
+
reject
|
|
1852
|
+
});
|
|
1853
|
+
this.processQueue();
|
|
1854
|
+
});
|
|
1855
|
+
}
|
|
1856
|
+
async processQueue() {
|
|
1857
|
+
if (this.activeCount >= this.maxConcurrent || this.queue.length === 0) return;
|
|
1858
|
+
const request = this.queue.shift();
|
|
1859
|
+
if (!request) return;
|
|
1860
|
+
this.activeCount++;
|
|
1861
|
+
const elapsed = Date.now() - this.lastRequestTime;
|
|
1862
|
+
if (elapsed < this.minDelayMs) await new Promise((r) => setTimeout(r, this.minDelayMs - elapsed));
|
|
1863
|
+
this.lastRequestTime = Date.now();
|
|
1864
|
+
try {
|
|
1865
|
+
const result = await request.execute();
|
|
1866
|
+
request.resolve(result);
|
|
1867
|
+
} catch (error) {
|
|
1868
|
+
request.reject(error);
|
|
1869
|
+
} finally {
|
|
1870
|
+
this.activeCount--;
|
|
1871
|
+
this.processQueue();
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
};
|
|
1875
|
+
const antigravityQueue = new RequestQueue(2, 500);
|
|
1876
|
+
|
|
1834
1877
|
//#endregion
|
|
1835
1878
|
//#region src/services/antigravity/anthropic-events.ts
|
|
1836
1879
|
/**
|
|
@@ -2163,13 +2206,12 @@ function createErrorResponse(type, message, status) {
|
|
|
2163
2206
|
* Note: Both Gemini and Claude models use the same endpoint and Gemini-style format
|
|
2164
2207
|
*/
|
|
2165
2208
|
const MAX_RETRIES$3 = 5;
|
|
2166
|
-
async function
|
|
2209
|
+
async function executeAntigravityRequest(request) {
|
|
2167
2210
|
const endpoint = request.stream ? ANTIGRAVITY_STREAM_URL : ANTIGRAVITY_NO_STREAM_URL;
|
|
2168
2211
|
const body = buildGeminiRequest(request);
|
|
2169
2212
|
for (let attempt = 0; attempt <= MAX_RETRIES$3; attempt++) {
|
|
2170
2213
|
const accessToken = await getValidAccessToken();
|
|
2171
|
-
if (!accessToken) return createErrorResponse("authentication_error", "No valid Antigravity access token available.
|
|
2172
|
-
consola.debug(`Antigravity request to ${endpoint} (attempt ${attempt + 1}/${MAX_RETRIES$3 + 1})`);
|
|
2214
|
+
if (!accessToken) return createErrorResponse("authentication_error", "No valid Antigravity access token available.", 401);
|
|
2173
2215
|
try {
|
|
2174
2216
|
const response = await fetch(endpoint, {
|
|
2175
2217
|
method: "POST",
|
|
@@ -2185,13 +2227,13 @@ async function createAntigravityMessages(request) {
|
|
|
2185
2227
|
if (response.ok) return request.stream ? transformStreamResponse(response, request.model) : await transformNonStreamResponse(response, request.model);
|
|
2186
2228
|
const errorResult = await handleApiError(response);
|
|
2187
2229
|
if (errorResult.shouldRetry && attempt < MAX_RETRIES$3) {
|
|
2188
|
-
consola.info(`Rate limited, retrying in ${errorResult.retryDelayMs}ms
|
|
2230
|
+
consola.info(`Rate limited, retrying in ${errorResult.retryDelayMs}ms`);
|
|
2189
2231
|
await sleep(errorResult.retryDelayMs);
|
|
2190
2232
|
continue;
|
|
2191
2233
|
}
|
|
2192
2234
|
return errorResult.response;
|
|
2193
2235
|
} catch (error) {
|
|
2194
|
-
consola.error("Antigravity
|
|
2236
|
+
consola.error("Antigravity request error:", error);
|
|
2195
2237
|
if (attempt < MAX_RETRIES$3) {
|
|
2196
2238
|
await sleep(500);
|
|
2197
2239
|
continue;
|
|
@@ -2201,6 +2243,9 @@ async function createAntigravityMessages(request) {
|
|
|
2201
2243
|
}
|
|
2202
2244
|
return createErrorResponse("api_error", "Max retries exceeded", 429);
|
|
2203
2245
|
}
|
|
2246
|
+
async function createAntigravityMessages(request) {
|
|
2247
|
+
return antigravityQueue.enqueue(() => executeAntigravityRequest(request));
|
|
2248
|
+
}
|
|
2204
2249
|
/**
|
|
2205
2250
|
* Parse retry delay from error response
|
|
2206
2251
|
*/
|
|
@@ -2315,6 +2360,18 @@ function transformStreamResponse(response, model) {
|
|
|
2315
2360
|
} });
|
|
2316
2361
|
}
|
|
2317
2362
|
/**
|
|
2363
|
+
* Process candidate parts and handle finish
|
|
2364
|
+
*/
|
|
2365
|
+
function processCandidate(candidate, state$1, emit) {
|
|
2366
|
+
const parts = candidate?.content?.parts ?? [];
|
|
2367
|
+
for (const part of parts) processPart(part, state$1, emit);
|
|
2368
|
+
if (candidate?.finishReason === "STOP") {
|
|
2369
|
+
handleFinish(state$1, emit);
|
|
2370
|
+
return true;
|
|
2371
|
+
}
|
|
2372
|
+
return false;
|
|
2373
|
+
}
|
|
2374
|
+
/**
|
|
2318
2375
|
* Process the stream and emit events
|
|
2319
2376
|
*/
|
|
2320
2377
|
async function processStream(reader, decoder, state$1, controller) {
|
|
@@ -2323,8 +2380,7 @@ async function processStream(reader, decoder, state$1, controller) {
|
|
|
2323
2380
|
while (!finished) {
|
|
2324
2381
|
const { done, value } = await reader.read();
|
|
2325
2382
|
if (done) break;
|
|
2326
|
-
const
|
|
2327
|
-
const lines = processChunk(chunk, state$1);
|
|
2383
|
+
const lines = processChunk(decoder.decode(value, { stream: true }), state$1);
|
|
2328
2384
|
for (const line of lines) {
|
|
2329
2385
|
if (finished) break;
|
|
2330
2386
|
const data = parseSSELine(line);
|
|
@@ -2334,11 +2390,7 @@ async function processStream(reader, decoder, state$1, controller) {
|
|
|
2334
2390
|
state$1.inputTokens = usage.promptTokenCount ?? state$1.inputTokens;
|
|
2335
2391
|
state$1.outputTokens = usage.candidatesTokenCount ?? state$1.outputTokens;
|
|
2336
2392
|
}
|
|
2337
|
-
|
|
2338
|
-
const parts = candidate?.content?.parts ?? [];
|
|
2339
|
-
for (const part of parts) processPart(part, state$1, emit);
|
|
2340
|
-
if (candidate?.finishReason === "STOP") {
|
|
2341
|
-
handleFinish(state$1, emit);
|
|
2393
|
+
if (candidates[0] && processCandidate(candidates[0], state$1, emit)) {
|
|
2342
2394
|
finished = true;
|
|
2343
2395
|
break;
|
|
2344
2396
|
}
|