agent-limit 0.7.1 → 0.7.2
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/package.json +1 -1
- package/src/providers/claude.ts +2 -32
- package/src/utils/keychain.ts +0 -68
package/package.json
CHANGED
package/src/providers/claude.ts
CHANGED
|
@@ -1,30 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getClaudeCredentials,
|
|
3
|
-
refreshClaudeToken,
|
|
4
|
-
saveClaudeCredentials,
|
|
5
3
|
type ClaudeCredentials,
|
|
6
4
|
} from "../utils/keychain";
|
|
7
5
|
import { timeUntil } from "../utils/time";
|
|
8
6
|
import type { ProviderStatus } from "./types";
|
|
9
7
|
|
|
10
|
-
const TOKEN_REFRESH_BUFFER_MS = 5 * 60 * 1000;
|
|
11
|
-
|
|
12
8
|
interface ClaudeUsageResponse {
|
|
13
9
|
five_hour: { utilization: number; resets_at: string | null } | null;
|
|
14
10
|
seven_day: { utilization: number; resets_at: string | null } | null;
|
|
15
11
|
seven_day_opus: { utilization: number; resets_at: string | null } | null;
|
|
16
12
|
}
|
|
17
13
|
|
|
18
|
-
async function tryRefreshCredentials(
|
|
19
|
-
credentials: ClaudeCredentials
|
|
20
|
-
): Promise<ClaudeCredentials | null> {
|
|
21
|
-
const refreshed = await refreshClaudeToken(credentials.refreshToken);
|
|
22
|
-
if (refreshed) {
|
|
23
|
-
await saveClaudeCredentials(refreshed);
|
|
24
|
-
}
|
|
25
|
-
return refreshed;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
14
|
async function fetchUsageWithCredentials(
|
|
29
15
|
credentials: ClaudeCredentials
|
|
30
16
|
): Promise<Response> {
|
|
@@ -41,7 +27,7 @@ async function fetchUsageWithCredentials(
|
|
|
41
27
|
}
|
|
42
28
|
|
|
43
29
|
export async function fetchClaudeUsage(): Promise<ProviderStatus> {
|
|
44
|
-
|
|
30
|
+
const credentials = await getClaudeCredentials();
|
|
45
31
|
|
|
46
32
|
if (!credentials) {
|
|
47
33
|
return {
|
|
@@ -52,24 +38,8 @@ export async function fetchClaudeUsage(): Promise<ProviderStatus> {
|
|
|
52
38
|
};
|
|
53
39
|
}
|
|
54
40
|
|
|
55
|
-
const tokenExpiresSoon = credentials.expiresAt < Date.now() + TOKEN_REFRESH_BUFFER_MS;
|
|
56
|
-
if (tokenExpiresSoon) {
|
|
57
|
-
const refreshed = await tryRefreshCredentials(credentials);
|
|
58
|
-
if (refreshed) {
|
|
59
|
-
credentials = refreshed;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
41
|
try {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (response.status === 401) {
|
|
67
|
-
const refreshed = await tryRefreshCredentials(credentials);
|
|
68
|
-
if (refreshed) {
|
|
69
|
-
credentials = refreshed;
|
|
70
|
-
response = await fetchUsageWithCredentials(credentials);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
42
|
+
const response = await fetchUsageWithCredentials(credentials);
|
|
73
43
|
|
|
74
44
|
if (!response.ok) {
|
|
75
45
|
if (response.status === 401) {
|
package/src/utils/keychain.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { $ } from "bun";
|
|
2
2
|
|
|
3
|
-
const CLAUDE_CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e";
|
|
4
|
-
const CLAUDE_TOKEN_ENDPOINT = "https://console.anthropic.com/v1/oauth/token";
|
|
5
3
|
const KEYCHAIN_SERVICE = "Claude Code-credentials";
|
|
6
4
|
|
|
7
5
|
export async function getKeychainCredentials(service: string): Promise<string | null> {
|
|
@@ -15,16 +13,6 @@ export async function getKeychainCredentials(service: string): Promise<string |
|
|
|
15
13
|
}
|
|
16
14
|
}
|
|
17
15
|
|
|
18
|
-
async function setKeychainCredentials(service: string, data: string): Promise<boolean> {
|
|
19
|
-
try {
|
|
20
|
-
await $`security delete-generic-password -s ${service}`.quiet().nothrow();
|
|
21
|
-
await $`security add-generic-password -s ${service} -w ${data}`.quiet();
|
|
22
|
-
return true;
|
|
23
|
-
} catch {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
16
|
export interface ClaudeCredentials {
|
|
29
17
|
accessToken: string;
|
|
30
18
|
refreshToken: string;
|
|
@@ -45,62 +33,6 @@ export async function getClaudeCredentials(): Promise<ClaudeCredentials | null>
|
|
|
45
33
|
}
|
|
46
34
|
}
|
|
47
35
|
|
|
48
|
-
interface TokenRefreshResponse {
|
|
49
|
-
token_type: string;
|
|
50
|
-
access_token: string;
|
|
51
|
-
expires_in: number;
|
|
52
|
-
refresh_token: string;
|
|
53
|
-
scope: string;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export async function refreshClaudeToken(
|
|
57
|
-
refreshToken: string
|
|
58
|
-
): Promise<ClaudeCredentials | null> {
|
|
59
|
-
try {
|
|
60
|
-
const response = await fetch(CLAUDE_TOKEN_ENDPOINT, {
|
|
61
|
-
method: "POST",
|
|
62
|
-
headers: { "Content-Type": "application/json" },
|
|
63
|
-
body: JSON.stringify({
|
|
64
|
-
grant_type: "refresh_token",
|
|
65
|
-
refresh_token: refreshToken,
|
|
66
|
-
client_id: CLAUDE_CLIENT_ID,
|
|
67
|
-
}),
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
if (!response.ok) {
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const data: TokenRefreshResponse = await response.json();
|
|
75
|
-
|
|
76
|
-
return {
|
|
77
|
-
accessToken: data.access_token,
|
|
78
|
-
refreshToken: data.refresh_token,
|
|
79
|
-
expiresAt: Date.now() + data.expires_in * 1000,
|
|
80
|
-
scopes: data.scope.split(" "),
|
|
81
|
-
subscriptionType: "Pro",
|
|
82
|
-
};
|
|
83
|
-
} catch {
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export async function saveClaudeCredentials(
|
|
89
|
-
credentials: ClaudeCredentials
|
|
90
|
-
): Promise<boolean> {
|
|
91
|
-
try {
|
|
92
|
-
const raw = await getKeychainCredentials(KEYCHAIN_SERVICE);
|
|
93
|
-
if (!raw) return false;
|
|
94
|
-
|
|
95
|
-
const parsed = JSON.parse(raw);
|
|
96
|
-
parsed.claudeAiOauth = credentials;
|
|
97
|
-
|
|
98
|
-
return await setKeychainCredentials(KEYCHAIN_SERVICE, JSON.stringify(parsed));
|
|
99
|
-
} catch {
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
36
|
export interface CodexCredentials {
|
|
105
37
|
accessToken: string;
|
|
106
38
|
accountId: string;
|