@cortexkit/pi-anthropic-auth 1.12.1 → 1.13.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/dist/stream.js +40 -6
- package/package.json +2 -2
package/dist/stream.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { applyClaudeCodeHeaders, CACHE_KEEP_EXTENDED_TTL_BETA, CacheKeepManager, dumpDirectRequest, FAST_MODE_BETA, FallbackAccountManager, getCache1hPersistentMode, getRelayConfig, getRoutingMode, isApiKeyAccount, isCache1hPersistentlyEnabled, isCacheKeepHybridActive, isDumpPersistentlyEnabled, isFastModePersistentlyEnabled, isOAuthAccount, isValidApiBaseURL, loadAccounts, mergeAnthropicBetas, QuotaManager, resolveClaudeCodeIdentity, sendViaRelay, setDumpEnabled, shouldFallbackStatus, } from '@cortexkit/anthropic-auth-core';
|
|
1
|
+
import { applyClaudeCodeHeaders, CACHE_KEEP_EXTENDED_TTL_BETA, CacheKeepManager, dumpDirectRequest, FAST_MODE_BETA, FallbackAccountManager, getCache1hPersistentMode, getRelayConfig, getRoutingMode, isApiKeyAccount, isCache1hPersistentlyEnabled, isCacheKeepHybridActive, isDumpPersistentlyEnabled, isFastModePersistentlyEnabled, isOAuthAccount, isValidApiBaseURL, loadAccounts, mergeAnthropicBetas, QuotaManager, quotaSnapshotModelScopeIsExhausted, resolveClaudeCodeIdentity, sendViaRelay, setDumpEnabled, shouldFallbackStatus, } from '@cortexkit/anthropic-auth-core';
|
|
2
2
|
import { calculateCost, createAssistantMessageEventStream, } from '@earendil-works/pi-ai';
|
|
3
3
|
import { buildAnthropicRequest, fromClaudeCodeToolName } from "./convert.js";
|
|
4
4
|
import { getPiAccountStoragePath } from "./paths.js";
|
|
@@ -219,7 +219,7 @@ async function sendAnthropicRequest(options) {
|
|
|
219
219
|
});
|
|
220
220
|
}
|
|
221
221
|
function quotaSnapshotIsExhausted(quota) {
|
|
222
|
-
return
|
|
222
|
+
return ['five_hour', 'seven_day'].some((key) => (quota?.[key]?.remainingPercent ?? 1) <= 0);
|
|
223
223
|
}
|
|
224
224
|
export function primaryResponseAllowsApiFallback(preflight) {
|
|
225
225
|
return (preflight === 'rate_limit_error' ||
|
|
@@ -262,8 +262,33 @@ async function executeWithFallback(options) {
|
|
|
262
262
|
return false;
|
|
263
263
|
}
|
|
264
264
|
}
|
|
265
|
+
async function primaryQuotaRefreshConfirmsModelScopeExhausted() {
|
|
266
|
+
try {
|
|
267
|
+
const quota = await quotaManager.refreshMain(options.primaryAccessToken);
|
|
268
|
+
const entry = quotaManager.getMain(options.primaryAccessToken);
|
|
269
|
+
return Boolean(entry &&
|
|
270
|
+
entry.refreshAfter > Date.now() &&
|
|
271
|
+
quotaSnapshotModelScopeIsExhausted(quota, options.model.id));
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
function primaryCachedModelScopeExhausted() {
|
|
278
|
+
const entry = quotaManager.getMain(options.primaryAccessToken);
|
|
279
|
+
return Boolean(entry &&
|
|
280
|
+
quotaSnapshotModelScopeIsExhausted(entry.quota, options.model.id));
|
|
281
|
+
}
|
|
282
|
+
function primaryFreshModelScopeExhausted() {
|
|
283
|
+
const entry = quotaManager.getMain(options.primaryAccessToken);
|
|
284
|
+
return Boolean(entry &&
|
|
285
|
+
entry.refreshAfter > Date.now() &&
|
|
286
|
+
quotaSnapshotModelScopeIsExhausted(entry.quota, options.model.id));
|
|
287
|
+
}
|
|
265
288
|
async function tryFallbackAccounts(routeOptions = {}) {
|
|
266
|
-
const usableOAuth = await manager.getUsableFallbackAccounts(storage
|
|
289
|
+
const usableOAuth = await manager.getUsableFallbackAccounts(storage, {
|
|
290
|
+
modelId: options.model.id,
|
|
291
|
+
});
|
|
267
292
|
const usableOAuthById = new Map(usableOAuth.map((account) => [account.id, account]));
|
|
268
293
|
for (const configured of storage?.accounts ?? []) {
|
|
269
294
|
let response = null;
|
|
@@ -311,6 +336,13 @@ async function executeWithFallback(options) {
|
|
|
311
336
|
if (fallback)
|
|
312
337
|
return fallback;
|
|
313
338
|
}
|
|
339
|
+
else if (primaryFreshModelScopeExhausted() ||
|
|
340
|
+
(primaryCachedModelScopeExhausted() &&
|
|
341
|
+
(await primaryQuotaRefreshConfirmsModelScopeExhausted()))) {
|
|
342
|
+
const fallback = await tryFallbackAccounts();
|
|
343
|
+
if (fallback)
|
|
344
|
+
return fallback;
|
|
345
|
+
}
|
|
314
346
|
const primary = await sendAnthropicRequest({
|
|
315
347
|
...options,
|
|
316
348
|
accessToken: options.primaryAccessToken,
|
|
@@ -320,9 +352,11 @@ async function executeWithFallback(options) {
|
|
|
320
352
|
if (!shouldFallbackStatus(primaryPreflight.status, storage))
|
|
321
353
|
return primaryPreflight;
|
|
322
354
|
}
|
|
323
|
-
const
|
|
324
|
-
|
|
325
|
-
|
|
355
|
+
const primaryAllowsQuotaFallback = primaryResponseAllowsApiFallback(primaryPreflight);
|
|
356
|
+
const allowApiFallback = primaryAllowsQuotaFallback && (await primaryQuotaRefreshConfirmsExhausted());
|
|
357
|
+
const allowModelScopedOAuthFallback = primaryAllowsQuotaFallback &&
|
|
358
|
+
(await primaryQuotaRefreshConfirmsModelScopeExhausted());
|
|
359
|
+
if (!fallbackFirst || allowApiFallback || allowModelScopedOAuthFallback) {
|
|
326
360
|
const fallback = await tryFallbackAccounts({
|
|
327
361
|
includeApiRoutes: allowApiFallback,
|
|
328
362
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cortexkit/pi-anthropic-auth",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"prepublishOnly": "bun run build"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@cortexkit/anthropic-auth-core": "1.
|
|
39
|
+
"@cortexkit/anthropic-auth-core": "1.13.0"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"@earendil-works/pi-ai": "*",
|