@cortexkit/anthropic-auth-core 1.0.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/LICENSE +21 -0
- package/README.md +8 -0
- package/dist/accounts.d.ts +121 -0
- package/dist/accounts.js +606 -0
- package/dist/auth.d.ts +16 -0
- package/dist/auth.js +93 -0
- package/dist/cache1h.d.ts +33 -0
- package/dist/cache1h.js +97 -0
- package/dist/cch.d.ts +36 -0
- package/dist/cch.js +77 -0
- package/dist/constants.d.ts +39 -0
- package/dist/constants.js +56 -0
- package/dist/dump.d.ts +33 -0
- package/dist/dump.js +218 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/logger.d.ts +3 -0
- package/dist/logger.js +66 -0
- package/dist/pkce.d.ts +5 -0
- package/dist/pkce.js +17 -0
- package/dist/quotas.d.ts +23 -0
- package/dist/quotas.js +100 -0
- package/dist/relay.d.ts +27 -0
- package/dist/relay.js +867 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ex Machina
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# @cortexkit/anthropic-auth-core
|
|
2
|
+
|
|
3
|
+
Shared Anthropic OAuth, quota, cache, relay, dump, and request-signing helpers used by CortexKit's OpenCode and Pi integrations.
|
|
4
|
+
|
|
5
|
+
User-facing packages:
|
|
6
|
+
|
|
7
|
+
- `@cortexkit/opencode-anthropic-auth` for OpenCode
|
|
8
|
+
- `@cortexkit/pi-anthropic-auth` for Pi
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { type Cache1hMode } from './constants.ts';
|
|
2
|
+
export declare const ACCOUNT_FILE_NAME = "anthropic-auth.json";
|
|
3
|
+
export declare const QUOTA_URL = "https://api.anthropic.com/api/oauth/usage";
|
|
4
|
+
export type QuotaWindowName = 'five_hour' | 'seven_day';
|
|
5
|
+
export type OAuthAccount = {
|
|
6
|
+
id: string;
|
|
7
|
+
label?: string;
|
|
8
|
+
type: 'oauth';
|
|
9
|
+
access?: string;
|
|
10
|
+
refresh: string;
|
|
11
|
+
expires?: number;
|
|
12
|
+
enabled?: boolean;
|
|
13
|
+
addedAt?: number;
|
|
14
|
+
lastUsed?: number;
|
|
15
|
+
lastRefreshedAt?: number;
|
|
16
|
+
lastRefreshError?: AccountOperationError;
|
|
17
|
+
lastQuotaRefreshError?: AccountOperationError;
|
|
18
|
+
quota?: Partial<Record<QuotaWindowName, AccountQuotaWindow>>;
|
|
19
|
+
};
|
|
20
|
+
export type AccountOperationError = {
|
|
21
|
+
message: string;
|
|
22
|
+
checkedAt: number;
|
|
23
|
+
};
|
|
24
|
+
export type AccountQuotaWindow = {
|
|
25
|
+
usedPercent: number;
|
|
26
|
+
remainingPercent: number;
|
|
27
|
+
resetsAt?: string;
|
|
28
|
+
checkedAt: number;
|
|
29
|
+
};
|
|
30
|
+
export type AccountStorage = {
|
|
31
|
+
version: 1;
|
|
32
|
+
main?: {
|
|
33
|
+
type: 'opencode';
|
|
34
|
+
provider: 'anthropic';
|
|
35
|
+
};
|
|
36
|
+
fallbackOn?: number[];
|
|
37
|
+
refresh?: {
|
|
38
|
+
enabled?: boolean;
|
|
39
|
+
intervalMinutes?: number;
|
|
40
|
+
refreshBeforeExpiryMinutes?: number;
|
|
41
|
+
};
|
|
42
|
+
quota?: {
|
|
43
|
+
enabled?: boolean;
|
|
44
|
+
checkIntervalMinutes?: number;
|
|
45
|
+
minimumRemaining?: Partial<Record<QuotaWindowName | '5h' | '1w', number>>;
|
|
46
|
+
failClosedOnUnknownQuota?: boolean;
|
|
47
|
+
};
|
|
48
|
+
claudeCache?: {
|
|
49
|
+
enabled?: boolean;
|
|
50
|
+
mode?: Cache1hMode;
|
|
51
|
+
};
|
|
52
|
+
dump?: {
|
|
53
|
+
enabled?: boolean;
|
|
54
|
+
};
|
|
55
|
+
relay?: {
|
|
56
|
+
enabled?: boolean;
|
|
57
|
+
url?: string;
|
|
58
|
+
token?: string;
|
|
59
|
+
fallbackToDirect?: boolean;
|
|
60
|
+
transport?: 'http' | 'websocket';
|
|
61
|
+
};
|
|
62
|
+
accounts: OAuthAccount[];
|
|
63
|
+
};
|
|
64
|
+
export type OAuthQuotaSnapshot = Partial<Record<QuotaWindowName, AccountQuotaWindow>>;
|
|
65
|
+
export type AccountManagerOptions = {
|
|
66
|
+
now?: () => number;
|
|
67
|
+
fetchImpl?: typeof fetch;
|
|
68
|
+
configPath?: string;
|
|
69
|
+
};
|
|
70
|
+
export type AccountRefreshError = {
|
|
71
|
+
accountId: string;
|
|
72
|
+
message: string;
|
|
73
|
+
};
|
|
74
|
+
export declare function getAccountStoragePath(): string;
|
|
75
|
+
export declare function loadAccounts(path?: string): Promise<AccountStorage | null>;
|
|
76
|
+
export declare function saveAccounts(storage: AccountStorage, path?: string): Promise<void>;
|
|
77
|
+
export declare function isCache1hPersistentlyEnabled(storage: AccountStorage | null): boolean;
|
|
78
|
+
export declare function getCache1hPersistentMode(storage: AccountStorage | null): Cache1hMode;
|
|
79
|
+
export declare function setCache1hPersistentEnabled(enabled: boolean, mode?: Cache1hMode, path?: string): Promise<AccountStorage>;
|
|
80
|
+
export declare function setCache1hPersistentMode(mode: Cache1hMode, path?: string): Promise<AccountStorage>;
|
|
81
|
+
export declare function isDumpPersistentlyEnabled(storage: AccountStorage | null): boolean;
|
|
82
|
+
export declare function setDumpPersistentEnabled(enabled: boolean, path?: string): Promise<AccountStorage>;
|
|
83
|
+
export declare function shouldFallbackStatus(status: number, storage: AccountStorage | null): boolean;
|
|
84
|
+
export declare function getQuotaCheckIntervalMs(storage: AccountStorage | null): number;
|
|
85
|
+
export declare function quotaSnapshotPassesPolicy(quota: OAuthQuotaSnapshot | undefined, storage: AccountStorage | null): boolean;
|
|
86
|
+
export declare function getQuotaNextRefreshAt(quota: OAuthQuotaSnapshot | undefined, storage: AccountStorage | null, now: number): number;
|
|
87
|
+
export declare function fetchOAuthQuotaSnapshot(input: {
|
|
88
|
+
accessToken: string;
|
|
89
|
+
fetchImpl?: typeof fetch;
|
|
90
|
+
now?: () => number;
|
|
91
|
+
}): Promise<{
|
|
92
|
+
five_hour: AccountQuotaWindow | undefined;
|
|
93
|
+
seven_day: AccountQuotaWindow | undefined;
|
|
94
|
+
}>;
|
|
95
|
+
export declare class FallbackAccountManager {
|
|
96
|
+
private readonly now;
|
|
97
|
+
private readonly fetchImpl;
|
|
98
|
+
private readonly configPath;
|
|
99
|
+
private readonly refreshPromises;
|
|
100
|
+
private refreshTimer;
|
|
101
|
+
private quotaTimer;
|
|
102
|
+
constructor(options?: AccountManagerOptions);
|
|
103
|
+
load(): Promise<AccountStorage | null>;
|
|
104
|
+
save(storage: AccountStorage): Promise<void>;
|
|
105
|
+
startBackgroundRefresh(): void;
|
|
106
|
+
stopBackgroundRefresh(): void;
|
|
107
|
+
getUsableFallbackAccounts(): Promise<OAuthAccount[]>;
|
|
108
|
+
markUsed(account: OAuthAccount): Promise<void>;
|
|
109
|
+
accountPassesQuotaPolicy(account: OAuthAccount, storage: AccountStorage | null): boolean;
|
|
110
|
+
refreshDueAccounts(): Promise<void>;
|
|
111
|
+
refreshQuotaForDueAccounts(): Promise<void>;
|
|
112
|
+
refreshQuotaForAllAccounts(): Promise<{
|
|
113
|
+
storage: AccountStorage | null;
|
|
114
|
+
errors: AccountRefreshError[];
|
|
115
|
+
}>;
|
|
116
|
+
refreshAccount(account: OAuthAccount, storage: AccountStorage, options?: {
|
|
117
|
+
force?: boolean;
|
|
118
|
+
}): Promise<OAuthAccount>;
|
|
119
|
+
private refreshAccountNow;
|
|
120
|
+
refreshAccountQuota(account: OAuthAccount, storage: AccountStorage): Promise<OAuthAccount>;
|
|
121
|
+
}
|