@ai-devkit/agent-manager 0.42.2 → 0.43.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.
Files changed (33) hide show
  1. package/dist/AgentManager.d.ts +14 -5
  2. package/dist/AgentManager.d.ts.map +1 -1
  3. package/dist/AgentManager.js +32 -13
  4. package/dist/AgentManager.js.map +1 -1
  5. package/dist/__tests__/AgentManager.test.js +131 -0
  6. package/dist/__tests__/AgentManager.test.js.map +1 -1
  7. package/dist/__tests__/capacity/codex.test.js +41 -48
  8. package/dist/__tests__/capacity/codex.test.js.map +1 -1
  9. package/dist/__tests__/capacity/openai.test.js +9 -1
  10. package/dist/__tests__/capacity/openai.test.js.map +1 -1
  11. package/dist/__tests__/capacity/wham.test.js +21 -6
  12. package/dist/__tests__/capacity/wham.test.js.map +1 -1
  13. package/dist/capacity/providers/openai-subscription.d.ts +1 -5
  14. package/dist/capacity/providers/openai-subscription.d.ts.map +1 -1
  15. package/dist/capacity/providers/openai-subscription.js +37 -13
  16. package/dist/capacity/providers/openai-subscription.js.map +1 -1
  17. package/dist/capacity/sources/codex.d.ts +1 -8
  18. package/dist/capacity/sources/codex.d.ts.map +1 -1
  19. package/dist/capacity/sources/codex.js +10 -19
  20. package/dist/capacity/sources/codex.js.map +1 -1
  21. package/dist/index.d.ts +1 -0
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js.map +1 -1
  24. package/package.json +2 -2
  25. package/src/AgentManager.ts +49 -20
  26. package/src/__tests__/AgentManager.test.ts +95 -0
  27. package/src/__tests__/capacity/codex.test.ts +42 -47
  28. package/src/__tests__/capacity/fixtures/wham-usage.json +14 -1
  29. package/src/__tests__/capacity/openai.test.ts +12 -1
  30. package/src/__tests__/capacity/wham.test.ts +20 -2
  31. package/src/capacity/providers/openai-subscription.ts +42 -20
  32. package/src/capacity/sources/codex.ts +7 -18
  33. package/src/index.ts +1 -0
@@ -1,4 +1,4 @@
1
- import type { CapacityWindow, ProviderCapacitySnapshot } from "../types.js";
1
+ import type { Availability, CapacityWindow, ProviderCapacitySnapshot } from "../types.js";
2
2
 
3
3
  const WHAM_USAGE_URL = "https://chatgpt.com/backend-api/wham/usage";
4
4
  const OPENAI_WHOAMI_URL = "https://auth.openai.com/api/accounts/v1/user-auth-credential/whoami";
@@ -13,11 +13,6 @@ export type OpenAiSubscriptionCapacityOptions = {
13
13
  timeoutMs?: number;
14
14
  };
15
15
 
16
- export type WhamUsageSnapshot = {
17
- windows: CapacityWindow[];
18
- creditsRemaining: number | null;
19
- };
20
-
21
16
  type UnknownRecord = Record<string, unknown>;
22
17
 
23
18
  function record(value: unknown): UnknownRecord | null {
@@ -30,6 +25,14 @@ function finiteNumber(value: unknown): number | null {
30
25
  return typeof value === "number" && Number.isFinite(value) ? value : null;
31
26
  }
32
27
 
28
+ function numericValue(value: unknown): number | null {
29
+ const direct = finiteNumber(value);
30
+ if (direct !== null) return direct;
31
+ if (typeof value !== "string" || value.trim().length === 0) return null;
32
+ const parsed = Number(value);
33
+ return Number.isFinite(parsed) ? parsed : null;
34
+ }
35
+
33
36
  function nonEmptyText(value: unknown): string | null {
34
37
  return typeof value === "string" && value.length > 0 ? value : null;
35
38
  }
@@ -78,16 +81,42 @@ function extraWindows(value: unknown): CapacityWindow[] {
78
81
  });
79
82
  }
80
83
 
81
- export function parseWhamUsage(raw: unknown): WhamUsageSnapshot {
84
+ function spendControlWindow(value: unknown): CapacityWindow | null {
85
+ const limit = record(record(value)?.individual_limit);
86
+ if (!limit) return null;
87
+ return {
88
+ id: "spend-control",
89
+ label: "Spend control",
90
+ durationMinutes: null,
91
+ usedPercent: finiteNumber(limit.used_percent),
92
+ resetsAt: resetTime(limit.reset_at),
93
+ limitType: "CREDIT_LIMIT",
94
+ total: numericValue(limit.limit),
95
+ current: numericValue(limit.used),
96
+ remaining: numericValue(limit.remaining),
97
+ };
98
+ }
99
+
100
+ function availability(windows: CapacityWindow[], spendControl: UnknownRecord | null): Availability {
101
+ if (spendControl?.reached === true) return "no";
102
+ return windows.some((window) => window.usedPercent !== null) ? "yes" : "unknown";
103
+ }
104
+
105
+ export function parseWhamUsage(raw: unknown): ProviderCapacitySnapshot {
82
106
  const response = record(raw) ?? {};
83
107
  const limits = record(response.rate_limit) ?? {};
84
108
  const credits = record(response.credits) ?? {};
109
+ const spendControl = record(response.spend_control);
110
+ const windows = [
111
+ toRateWindow(limits.primary_window, "session", "Session"),
112
+ toRateWindow(limits.secondary_window, "weekly", "Weekly"),
113
+ ...extraWindows(response.additional_rate_limits),
114
+ spendControlWindow(spendControl),
115
+ ].filter((window): window is CapacityWindow => window !== null);
85
116
  return {
86
- windows: [
87
- toRateWindow(limits.primary_window, "session", "Session"),
88
- toRateWindow(limits.secondary_window, "weekly", "Weekly"),
89
- ...extraWindows(response.additional_rate_limits),
90
- ].filter((window): window is CapacityWindow => window !== null),
117
+ authenticated: true,
118
+ available: availability(windows, spendControl),
119
+ windows,
91
120
  creditsRemaining: finiteNumber(credits.balance),
92
121
  };
93
122
  }
@@ -126,14 +155,7 @@ export async function fetchOpenAiSubscriptionCapacity(
126
155
  } catch {
127
156
  throw new Error("OpenAI usage response is not valid JSON");
128
157
  }
129
- const snapshot = parseWhamUsage(raw);
130
- const hasUsage = snapshot.windows.some((window) => window.usedPercent !== null);
131
- return {
132
- authenticated: true,
133
- available: hasUsage ? "yes" : "unknown",
134
- windows: snapshot.windows,
135
- creditsRemaining: snapshot.creditsRemaining,
136
- };
158
+ return parseWhamUsage(raw);
137
159
  }
138
160
 
139
161
  export async function fetchOpenAiPatAccountId(
@@ -2,7 +2,6 @@ import { spawn } from "node:child_process";
2
2
  import {
3
3
  fetchOpenAiPatAccountId,
4
4
  fetchOpenAiSubscriptionCapacity,
5
- parseWhamUsage,
6
5
  resetTime,
7
6
  safeIdentifier,
8
7
  } from "../providers/openai-subscription.js";
@@ -12,11 +11,10 @@ import {
12
11
  type CodexCredentialOptions,
13
12
  } from "../../harnesses/codex/credentials.js";
14
13
 
15
- type CodexUsageSource = "pat" | "oauth" | "cli";
16
14
  type UsageSnapshot = {
17
15
  windows: CapacityWindow[];
18
16
  creditsRemaining: number | null;
19
- source: CodexUsageSource;
17
+ available: CapacityReport["available"] | null;
20
18
  };
21
19
 
22
20
  type UnknownRecord = Record<string, unknown>;
@@ -48,11 +46,6 @@ function nonEmptyText(value: unknown): string | null {
48
46
  return typeof value === "string" && value.length > 0 ? value : null;
49
47
  }
50
48
 
51
- export function parseUsage(raw: unknown, source: "pat" | "oauth"): UsageSnapshot {
52
- const { windows, creditsRemaining } = parseWhamUsage(raw);
53
- return { windows, creditsRemaining, source };
54
- }
55
-
56
49
  function cliWindow(value: unknown, id: string, label: string): CapacityWindow | null {
57
50
  const input = record(value);
58
51
  if (!input) return null;
@@ -86,7 +79,7 @@ function parseCliUsage(raw: unknown): UsageSnapshot {
86
79
  }
87
80
  }
88
81
  const unique = [...new Map(windows.map((window) => [window.id, window])).values()];
89
- return { windows: unique, creditsRemaining: null, source: "cli" };
82
+ return { windows: unique, creditsRemaining: null, available: null };
90
83
  }
91
84
 
92
85
  function capacityFromSnapshot(
@@ -99,12 +92,15 @@ function capacityFromSnapshot(
99
92
  const reached = nonEmptyText(rateLimits?.rateLimitReachedType);
100
93
  const resetCredits =
101
94
  record(record(raw)?.rateLimitResetCredits) ?? record(record(raw)?.usageLimitResetCredits);
95
+ let available = snapshot.available;
96
+ if (reached) available = "no";
97
+ else if (available === null) available = hasUsage ? "yes" : "unknown";
102
98
  return {
103
99
  harness: "codex",
104
100
  provider: "openai",
105
101
  generatedAt: options.checkedAt,
106
102
  authenticated: true,
107
- available: reached ? "no" : hasUsage ? "yes" : "unknown",
103
+ available,
108
104
  windows: snapshot.windows,
109
105
  creditsRemaining: snapshot.creditsRemaining ?? finiteNumber(resetCredits?.availableCount),
110
106
  };
@@ -214,16 +210,11 @@ async function cliFallback(options: CodexProbeOptions): Promise<CapacityReport>
214
210
  async function subscriptionSnapshot(
215
211
  access: string,
216
212
  accountId: string,
217
- source: "pat" | "oauth",
218
213
  options: CodexProbeOptions,
219
214
  ): Promise<UsageSnapshot> {
220
215
  const snapshot = await fetchOpenAiSubscriptionCapacity({ access, accountId }, options);
221
216
  if (snapshot.authenticated === false) throw new Error("unauthorized");
222
- return {
223
- windows: snapshot.windows,
224
- creditsRemaining: snapshot.creditsRemaining,
225
- source,
226
- };
217
+ return snapshot;
227
218
  }
228
219
 
229
220
  export async function probeCodexCapacity(options: CodexProbeOptions): Promise<CapacityReport> {
@@ -236,7 +227,6 @@ export async function probeCodexCapacity(options: CodexProbeOptions): Promise<Ca
236
227
  const snapshot = await subscriptionSnapshot(
237
228
  credentials.personalAccessToken,
238
229
  accountId,
239
- "pat",
240
230
  options,
241
231
  );
242
232
  return capacityFromSnapshot(snapshot, options);
@@ -250,7 +240,6 @@ export async function probeCodexCapacity(options: CodexProbeOptions): Promise<Ca
250
240
  const snapshot = await subscriptionSnapshot(
251
241
  credentials.oauth.access,
252
242
  credentials.oauth.accountId,
253
- "oauth",
254
243
  options,
255
244
  );
256
245
  return capacityFromSnapshot(snapshot, options);
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { AgentManager } from "./AgentManager.js";
2
+ export type { EnrichedAgentWire, ListAgentsOptions } from "./AgentManager.js";
2
3
  export {
3
4
  getAnthropicCapacityReport,
4
5
  getClaudeCapacityReport,