@opengeni/config 3.1.1 → 3.1.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/dist/claude-subscription-capacity.d.ts +9 -0
- package/dist/claude-subscription-oauth.d.ts +17 -0
- package/dist/claude-subscription-usage.d.ts +4 -2
- package/dist/credit-promotions.d.ts +25 -0
- package/dist/index.d.ts +1564 -56
- package/dist/index.js +3524 -614
- package/dist/index.js.map +1 -1
- package/dist/realtime-voice-pricing.d.ts +30 -0
- package/dist/subscription-account-selection.d.ts +17 -0
- package/dist/web-search.d.ts +174 -0
- package/package.json +5 -5
- package/src/claude-subscription-capacity.ts +129 -0
- package/src/claude-subscription-oauth.ts +33 -0
- package/src/claude-subscription-usage.ts +225 -5
- package/src/credit-promotions.ts +68 -0
- package/src/index.ts +3406 -178
- package/src/realtime-voice-pricing.ts +83 -0
- package/src/subscription-account-selection.ts +36 -0
- package/src/web-search.ts +600 -0
|
@@ -2,12 +2,15 @@ import {
|
|
|
2
2
|
ClaudeUsageWindowId,
|
|
3
3
|
type ClaudeSubscriptionUsage,
|
|
4
4
|
type ClaudeUsageWindow,
|
|
5
|
+
type ClaudeUsageRequestStatus,
|
|
5
6
|
} from "@opengeni/contracts";
|
|
6
7
|
|
|
7
8
|
export type ClaudeUsageObservation = {
|
|
8
9
|
windows: ClaudeUsageWindow[];
|
|
9
10
|
observedAt: string;
|
|
10
11
|
source: "response_headers" | "provider";
|
|
12
|
+
requestStatus?: ClaudeUsageRequestStatus | null;
|
|
13
|
+
requestRestrictions?: ClaudeUsageRequestStatus[];
|
|
11
14
|
};
|
|
12
15
|
const prefixes: Record<string, string[]> = {
|
|
13
16
|
five_hour: ["5h"],
|
|
@@ -31,9 +34,50 @@ function status(value: unknown): ClaudeUsageWindow["status"] {
|
|
|
31
34
|
return value === "allowed" || value === "allowed_warning" || value === "rejected" ? value : null;
|
|
32
35
|
}
|
|
33
36
|
|
|
37
|
+
function latestReset(left: string | null, right: string | null, observedAt: string): string {
|
|
38
|
+
return new Date(
|
|
39
|
+
Math.max(
|
|
40
|
+
left ? Date.parse(left) : Date.parse(observedAt) + 60_000,
|
|
41
|
+
right ? Date.parse(right) : Date.parse(observedAt) + 60_000,
|
|
42
|
+
),
|
|
43
|
+
).toISOString();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function evidenceKey(item: ClaudeUsageRequestStatus): string {
|
|
47
|
+
// A legacy Sonnet claim does not prove an account-wide restriction cleared.
|
|
48
|
+
return JSON.stringify([
|
|
49
|
+
item.upstreamModelId,
|
|
50
|
+
item.upstreamModelId ? null : item.representativeClaim,
|
|
51
|
+
item.source === "provider",
|
|
52
|
+
]);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function tiedRequest(
|
|
56
|
+
left: ClaudeUsageRequestStatus,
|
|
57
|
+
right: ClaudeUsageRequestStatus,
|
|
58
|
+
): ClaudeUsageRequestStatus {
|
|
59
|
+
const denied = left.status === "rejected" ? left : right.status === "rejected" ? right : null;
|
|
60
|
+
return {
|
|
61
|
+
...(denied ?? left),
|
|
62
|
+
resetsAt: denied
|
|
63
|
+
? latestReset(
|
|
64
|
+
left.status === "rejected" ? left.resetsAt : denied.resetsAt,
|
|
65
|
+
right.status === "rejected" ? right.resetsAt : denied.resetsAt,
|
|
66
|
+
left.observedAt,
|
|
67
|
+
)
|
|
68
|
+
: null,
|
|
69
|
+
representativeClaim:
|
|
70
|
+
left.representativeClaim === right.representativeClaim ? left.representativeClaim : null,
|
|
71
|
+
status: denied ? "rejected" : null,
|
|
72
|
+
overageStatus: null,
|
|
73
|
+
overageResetsAt: null,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
34
77
|
export function parseClaudeUsageHeaders(
|
|
35
78
|
headers: Headers,
|
|
36
79
|
now = new Date(),
|
|
80
|
+
upstreamModelId?: string,
|
|
37
81
|
): ClaudeUsageObservation | null {
|
|
38
82
|
const observedAt = now.toISOString();
|
|
39
83
|
const claim = ClaudeUsageWindowId.safeParse(
|
|
@@ -41,6 +85,8 @@ export function parseClaudeUsageHeaders(
|
|
|
41
85
|
);
|
|
42
86
|
const claimedStatus = status(headers.get("anthropic-ratelimit-unified-status"));
|
|
43
87
|
const claimedReset = epoch(headers.get("anthropic-ratelimit-unified-reset"));
|
|
88
|
+
const overageStatus = status(headers.get("anthropic-ratelimit-unified-overage-status"));
|
|
89
|
+
const overageResetsAt = epoch(headers.get("anthropic-ratelimit-unified-overage-reset"));
|
|
44
90
|
const windows = ClaudeUsageWindowId.options.flatMap((id) => {
|
|
45
91
|
const header = (suffix: string) =>
|
|
46
92
|
prefixes[id]!.map((prefix) =>
|
|
@@ -53,7 +99,22 @@ export function parseClaudeUsageHeaders(
|
|
|
53
99
|
if (usedPercent === null && resetsAt === null && windowStatus === null) return [];
|
|
54
100
|
return [{ id, usedPercent, resetsAt, status: windowStatus, observedAt }];
|
|
55
101
|
});
|
|
56
|
-
return windows.length
|
|
102
|
+
return windows.length || claimedStatus || overageStatus
|
|
103
|
+
? {
|
|
104
|
+
windows,
|
|
105
|
+
observedAt,
|
|
106
|
+
source: "response_headers",
|
|
107
|
+
requestStatus: {
|
|
108
|
+
status: claimedStatus,
|
|
109
|
+
resetsAt: claimedReset,
|
|
110
|
+
representativeClaim: claim.success ? claim.data : null,
|
|
111
|
+
overageStatus,
|
|
112
|
+
overageResetsAt,
|
|
113
|
+
upstreamModelId: upstreamModelId ?? null,
|
|
114
|
+
observedAt,
|
|
115
|
+
},
|
|
116
|
+
}
|
|
117
|
+
: null;
|
|
57
118
|
}
|
|
58
119
|
|
|
59
120
|
export function parseClaudeUsageResponse(
|
|
@@ -93,16 +154,175 @@ export function mergeClaudeUsage(
|
|
|
93
154
|
observation: ClaudeUsageObservation,
|
|
94
155
|
): ClaudeSubscriptionUsage {
|
|
95
156
|
const windows = new Map(current.windows.map((window) => [window.id, window]));
|
|
96
|
-
for (const
|
|
157
|
+
for (const incomingWindow of observation.windows) {
|
|
158
|
+
const window = { ...incomingWindow, source: incomingWindow.source ?? observation.source };
|
|
97
159
|
const previous = windows.get(window.id);
|
|
98
|
-
if (!previous || previous.observedAt
|
|
160
|
+
if (!previous || previous.observedAt < window.observedAt) windows.set(window.id, window);
|
|
161
|
+
else if (
|
|
162
|
+
previous.observedAt === window.observedAt &&
|
|
163
|
+
JSON.stringify(previous) !== JSON.stringify(window)
|
|
164
|
+
) {
|
|
165
|
+
const rejected = previous.status === "rejected" || window.status === "rejected";
|
|
166
|
+
const full = (previous.usedPercent ?? 0) >= 100 || (window.usedPercent ?? 0) >= 100;
|
|
167
|
+
windows.set(window.id, {
|
|
168
|
+
...previous,
|
|
169
|
+
usedPercent:
|
|
170
|
+
previous.usedPercent === null
|
|
171
|
+
? window.usedPercent
|
|
172
|
+
: window.usedPercent === null
|
|
173
|
+
? previous.usedPercent
|
|
174
|
+
: Math.max(previous.usedPercent, window.usedPercent),
|
|
175
|
+
status: rejected ? "rejected" : previous.status === window.status ? previous.status : null,
|
|
176
|
+
resetsAt:
|
|
177
|
+
rejected || full
|
|
178
|
+
? latestReset(previous.resetsAt, window.resetsAt, window.observedAt)
|
|
179
|
+
: previous.resetsAt === window.resetsAt
|
|
180
|
+
? previous.resetsAt
|
|
181
|
+
: null,
|
|
182
|
+
source:
|
|
183
|
+
previous.source === "response_headers" || window.source === "response_headers"
|
|
184
|
+
? "response_headers"
|
|
185
|
+
: "provider",
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const incoming = observation.requestStatus;
|
|
190
|
+
const restrictions = new Map(
|
|
191
|
+
(current.requestRestrictions ?? []).map((item) => [evidenceKey(item), item]),
|
|
192
|
+
);
|
|
193
|
+
// Legacy snapshots also contribute their denial; a different model's success
|
|
194
|
+
// must not erase a restriction with an unknown representative claim.
|
|
195
|
+
for (const item of [
|
|
196
|
+
current.requestStatus?.status ? current.requestStatus : null,
|
|
197
|
+
...(observation.requestRestrictions ?? []),
|
|
198
|
+
incoming?.status ? incoming : null,
|
|
199
|
+
]) {
|
|
200
|
+
if (!item) continue;
|
|
201
|
+
const key = evidenceKey(item);
|
|
202
|
+
const previous = restrictions.get(key);
|
|
203
|
+
const sanitized = {
|
|
204
|
+
...item,
|
|
205
|
+
overageStatus: null,
|
|
206
|
+
overageResetsAt: null,
|
|
207
|
+
};
|
|
208
|
+
if (!previous || item.observedAt > previous.observedAt) restrictions.set(key, sanitized);
|
|
209
|
+
else if (
|
|
210
|
+
item.observedAt === previous.observedAt &&
|
|
211
|
+
JSON.stringify(previous) !== JSON.stringify(sanitized)
|
|
212
|
+
)
|
|
213
|
+
restrictions.set(key, tiedRequest(previous, sanitized));
|
|
214
|
+
}
|
|
215
|
+
// Direct refreshes can precede late turn finalizers. Retain each direct
|
|
216
|
+
// window's watermark even when no model restriction is present yet.
|
|
217
|
+
const directWindows = [
|
|
218
|
+
...current.windows.filter(
|
|
219
|
+
(window) =>
|
|
220
|
+
window.source === "provider" ||
|
|
221
|
+
(!window.source &&
|
|
222
|
+
current.source === "provider" &&
|
|
223
|
+
current.observedAt === window.observedAt),
|
|
224
|
+
),
|
|
225
|
+
...observation.windows.filter((window) => (window.source ?? observation.source) === "provider"),
|
|
226
|
+
];
|
|
227
|
+
for (const window of directWindows) {
|
|
228
|
+
if (window.usedPercent === null || window.usedPercent >= 100) continue;
|
|
229
|
+
const proof: ClaudeUsageRequestStatus = {
|
|
230
|
+
status: null,
|
|
231
|
+
resetsAt: null,
|
|
232
|
+
representativeClaim: window.id,
|
|
233
|
+
upstreamModelId: null,
|
|
234
|
+
overageStatus: null,
|
|
235
|
+
overageResetsAt: null,
|
|
236
|
+
observedAt: window.observedAt,
|
|
237
|
+
source: "provider",
|
|
238
|
+
};
|
|
239
|
+
const key = evidenceKey(proof);
|
|
240
|
+
if (!restrictions.has(key) || restrictions.get(key)!.observedAt < proof.observedAt)
|
|
241
|
+
restrictions.set(key, proof);
|
|
242
|
+
}
|
|
243
|
+
for (const [key, item] of restrictions) {
|
|
244
|
+
if (item.status !== "rejected") continue;
|
|
245
|
+
const proof = [...restrictions.values()].find(
|
|
246
|
+
(candidate) =>
|
|
247
|
+
candidate.source === "provider" &&
|
|
248
|
+
candidate.representativeClaim === item.representativeClaim &&
|
|
249
|
+
candidate.observedAt > item.observedAt &&
|
|
250
|
+
candidate.representativeClaim !== "overage" &&
|
|
251
|
+
candidate.representativeClaim !== "seven_day_overage_included",
|
|
252
|
+
);
|
|
253
|
+
if (proof)
|
|
254
|
+
restrictions.set(key, {
|
|
255
|
+
...item,
|
|
256
|
+
status: null,
|
|
257
|
+
resetsAt: null,
|
|
258
|
+
observedAt: proof.observedAt,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
// Bound persisted state without dropping denials: overflow collapses to a
|
|
262
|
+
// conservative account-wide restriction until the latest known reset.
|
|
263
|
+
let requestRestrictions = [...restrictions.values()];
|
|
264
|
+
const evidenceTime = requestRestrictions.reduce(
|
|
265
|
+
(value, item) => (item.observedAt > value ? item.observedAt : value),
|
|
266
|
+
observation.observedAt,
|
|
267
|
+
);
|
|
268
|
+
const liveDenials = requestRestrictions.filter(
|
|
269
|
+
(item) =>
|
|
270
|
+
item.status === "rejected" &&
|
|
271
|
+
(item.resetsAt ? Date.parse(item.resetsAt) : Date.parse(item.observedAt) + 60_000) >
|
|
272
|
+
Date.parse(evidenceTime),
|
|
273
|
+
);
|
|
274
|
+
if (requestRestrictions.length > 64 && liveDenials.length === 0) {
|
|
275
|
+
requestRestrictions = requestRestrictions
|
|
276
|
+
.sort((left, right) => right.observedAt.localeCompare(left.observedAt))
|
|
277
|
+
.slice(0, 64);
|
|
278
|
+
} else if (requestRestrictions.length > 64) {
|
|
279
|
+
const observedAt = evidenceTime;
|
|
280
|
+
const resetsAt = new Date(
|
|
281
|
+
Math.max(
|
|
282
|
+
...liveDenials.map((item) =>
|
|
283
|
+
item.resetsAt ? Date.parse(item.resetsAt) : Date.parse(item.observedAt) + 60_000,
|
|
284
|
+
),
|
|
285
|
+
Date.parse(observedAt) + 60_000,
|
|
286
|
+
),
|
|
287
|
+
).toISOString();
|
|
288
|
+
requestRestrictions = [
|
|
289
|
+
{
|
|
290
|
+
status: "rejected",
|
|
291
|
+
resetsAt,
|
|
292
|
+
representativeClaim: null,
|
|
293
|
+
overageStatus: null,
|
|
294
|
+
overageResetsAt: null,
|
|
295
|
+
upstreamModelId: null,
|
|
296
|
+
observedAt,
|
|
297
|
+
},
|
|
298
|
+
];
|
|
299
|
+
}
|
|
300
|
+
const newest = !current.observedAt || current.observedAt < observation.observedAt;
|
|
301
|
+
const tied = current.observedAt === observation.observedAt;
|
|
302
|
+
let requestStatus = current.requestStatus ?? null;
|
|
303
|
+
if (newest) requestStatus = incoming ?? null;
|
|
304
|
+
else if (tied && JSON.stringify(requestStatus) !== JSON.stringify(incoming ?? null)) {
|
|
305
|
+
// Millisecond timestamps do not order concurrent responses. Never restore
|
|
306
|
+
// paid authority or erase a denial on an ambiguous tie.
|
|
307
|
+
requestStatus =
|
|
308
|
+
incoming && requestStatus && evidenceKey(incoming) === evidenceKey(requestStatus)
|
|
309
|
+
? tiedRequest(requestStatus, incoming)
|
|
310
|
+
: null;
|
|
99
311
|
}
|
|
100
|
-
const newest = !current.observedAt || current.observedAt <= observation.observedAt;
|
|
101
312
|
return {
|
|
102
313
|
...current,
|
|
314
|
+
requestRestrictions,
|
|
315
|
+
requestStatus,
|
|
103
316
|
windows: ClaudeUsageWindowId.options.flatMap((id) =>
|
|
104
317
|
windows.has(id) ? [windows.get(id)!] : [],
|
|
105
318
|
),
|
|
106
|
-
...(newest
|
|
319
|
+
...(newest
|
|
320
|
+
? {
|
|
321
|
+
observedAt: observation.observedAt,
|
|
322
|
+
source: observation.source,
|
|
323
|
+
// A newer direct or partial observation cannot retain an older paid
|
|
324
|
+
// fallback authorization. Window readings remain independently merged.
|
|
325
|
+
}
|
|
326
|
+
: {}),
|
|
107
327
|
};
|
|
108
328
|
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { PromotionalCreditScope } from "@opengeni/contracts";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
const ModelIds = PromotionalCreditScope.shape.eligibleModelIds.transform((ids) => [
|
|
5
|
+
...new Set(ids),
|
|
6
|
+
]);
|
|
7
|
+
|
|
8
|
+
/** Operator configuration only. Keys are Stripe coupon IDs, never customer account IDs. */
|
|
9
|
+
export const CreditPromotionPolicy = z
|
|
10
|
+
.object({
|
|
11
|
+
defaultModelIds: ModelIds.optional(),
|
|
12
|
+
signupModelIds: ModelIds.optional(),
|
|
13
|
+
offers: z
|
|
14
|
+
.record(
|
|
15
|
+
z.string().min(1),
|
|
16
|
+
z
|
|
17
|
+
.object({
|
|
18
|
+
label: PromotionalCreditScope.shape.label,
|
|
19
|
+
eligibleModelIds: ModelIds.optional(),
|
|
20
|
+
})
|
|
21
|
+
.strict(),
|
|
22
|
+
)
|
|
23
|
+
.default({}),
|
|
24
|
+
})
|
|
25
|
+
.strict()
|
|
26
|
+
.superRefine((policy, ctx) => {
|
|
27
|
+
for (const [id, offer] of Object.entries(policy.offers)) {
|
|
28
|
+
if (!offer.eligibleModelIds && !policy.defaultModelIds) {
|
|
29
|
+
ctx.addIssue({
|
|
30
|
+
code: "custom",
|
|
31
|
+
path: ["offers", id],
|
|
32
|
+
message: "Offer requires eligibleModelIds or defaultModelIds",
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export const EnvCreditPromotionPolicy = z.preprocess(
|
|
39
|
+
(value) => {
|
|
40
|
+
if (typeof value !== "string") return value;
|
|
41
|
+
try {
|
|
42
|
+
return JSON.parse(value);
|
|
43
|
+
} catch {
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
CreditPromotionPolicy.default({ offers: {} }),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
export function signupCreditModelIds(settings: {
|
|
51
|
+
creditPromotionPolicy: z.infer<typeof CreditPromotionPolicy>;
|
|
52
|
+
}) {
|
|
53
|
+
return (
|
|
54
|
+
settings.creditPromotionPolicy.signupModelIds ?? settings.creditPromotionPolicy.defaultModelIds
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function promotionalCreditScope(
|
|
59
|
+
settings: { creditPromotionPolicy: z.infer<typeof CreditPromotionPolicy> },
|
|
60
|
+
couponId: string,
|
|
61
|
+
): PromotionalCreditScope | undefined {
|
|
62
|
+
const policy = settings.creditPromotionPolicy;
|
|
63
|
+
const offer = policy.offers[couponId];
|
|
64
|
+
const eligibleModelIds = offer?.eligibleModelIds ?? policy.defaultModelIds;
|
|
65
|
+
return eligibleModelIds
|
|
66
|
+
? { label: offer?.label ?? "Promotional credits", eligibleModelIds }
|
|
67
|
+
: undefined;
|
|
68
|
+
}
|