@opengeni/config 1.2.2 → 1.4.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/claude-subscription-capacity.d.ts +9 -0
- package/dist/claude-subscription-oauth.d.ts +17 -0
- package/dist/claude-subscription-usage.d.ts +13 -0
- package/dist/credit-promotions.d.ts +25 -0
- package/dist/index.d.ts +928 -15
- package/dist/index.js +3010 -628
- package/dist/index.js.map +1 -1
- package/dist/managed-user-admission.d.ts +2 -0
- package/dist/realtime-voice-pricing.d.ts +30 -0
- package/dist/subscription-account-selection.d.ts +17 -0
- package/dist/web-search.d.ts +127 -0
- package/package.json +4 -4
- package/src/claude-subscription-capacity.ts +129 -0
- package/src/claude-subscription-oauth.ts +33 -0
- package/src/claude-subscription-usage.ts +328 -0
- package/src/credit-promotions.ts +68 -0
- package/src/index.ts +2757 -252
- package/src/managed-user-admission.ts +11 -0
- package/src/realtime-voice-pricing.ts +83 -0
- package/src/subscription-account-selection.ts +36 -0
- package/src/web-search.ts +353 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ClaudeUsageWindowId,
|
|
3
|
+
type ClaudeSubscriptionUsage,
|
|
4
|
+
type ClaudeUsageWindow,
|
|
5
|
+
type ClaudeUsageRequestStatus,
|
|
6
|
+
} from "@opengeni/contracts";
|
|
7
|
+
|
|
8
|
+
export type ClaudeUsageObservation = {
|
|
9
|
+
windows: ClaudeUsageWindow[];
|
|
10
|
+
observedAt: string;
|
|
11
|
+
source: "response_headers" | "provider";
|
|
12
|
+
requestStatus?: ClaudeUsageRequestStatus | null;
|
|
13
|
+
requestRestrictions?: ClaudeUsageRequestStatus[];
|
|
14
|
+
};
|
|
15
|
+
const prefixes: Record<string, string[]> = {
|
|
16
|
+
five_hour: ["5h"],
|
|
17
|
+
seven_day: ["7d"],
|
|
18
|
+
seven_day_opus: ["7d-opus"],
|
|
19
|
+
seven_day_sonnet: ["7d-sonnet"],
|
|
20
|
+
seven_day_overage_included: ["7d_oi", "7d-overage-included"],
|
|
21
|
+
overage: ["overage"],
|
|
22
|
+
};
|
|
23
|
+
function percent(value: unknown, scale: number): number | null {
|
|
24
|
+
if (typeof value !== "number" && (typeof value !== "string" || !value.trim())) return null;
|
|
25
|
+
const number = Number(value) * scale;
|
|
26
|
+
return Number.isFinite(number) && number >= 0 ? number : null;
|
|
27
|
+
}
|
|
28
|
+
function epoch(value: string | null): string | null {
|
|
29
|
+
if (!value || !/^\d+(?:\.\d+)?$/.test(value)) return null;
|
|
30
|
+
const time = Number(value) * 1000;
|
|
31
|
+
return Number.isFinite(time) && time > 0 && time < 8.64e15 ? new Date(time).toISOString() : null;
|
|
32
|
+
}
|
|
33
|
+
function status(value: unknown): ClaudeUsageWindow["status"] {
|
|
34
|
+
return value === "allowed" || value === "allowed_warning" || value === "rejected" ? value : null;
|
|
35
|
+
}
|
|
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
|
+
|
|
77
|
+
export function parseClaudeUsageHeaders(
|
|
78
|
+
headers: Headers,
|
|
79
|
+
now = new Date(),
|
|
80
|
+
upstreamModelId?: string,
|
|
81
|
+
): ClaudeUsageObservation | null {
|
|
82
|
+
const observedAt = now.toISOString();
|
|
83
|
+
const claim = ClaudeUsageWindowId.safeParse(
|
|
84
|
+
headers.get("anthropic-ratelimit-unified-representative-claim"),
|
|
85
|
+
);
|
|
86
|
+
const claimedStatus = status(headers.get("anthropic-ratelimit-unified-status"));
|
|
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"));
|
|
90
|
+
const windows = ClaudeUsageWindowId.options.flatMap((id) => {
|
|
91
|
+
const header = (suffix: string) =>
|
|
92
|
+
prefixes[id]!.map((prefix) =>
|
|
93
|
+
headers.get(`anthropic-ratelimit-unified-${prefix}-${suffix}`),
|
|
94
|
+
).find((value) => value !== null) ?? null;
|
|
95
|
+
const representative = claim.success && claim.data === id;
|
|
96
|
+
const usedPercent = percent(header("utilization"), 100);
|
|
97
|
+
const resetsAt = epoch(header("reset")) ?? (representative ? claimedReset : null);
|
|
98
|
+
const windowStatus = status(header("status")) ?? (representative ? claimedStatus : null);
|
|
99
|
+
if (usedPercent === null && resetsAt === null && windowStatus === null) return [];
|
|
100
|
+
return [{ id, usedPercent, resetsAt, status: windowStatus, observedAt }];
|
|
101
|
+
});
|
|
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;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function parseClaudeUsageResponse(
|
|
121
|
+
value: unknown,
|
|
122
|
+
now = new Date(),
|
|
123
|
+
): ClaudeUsageObservation | null {
|
|
124
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
125
|
+
const observedAt = now.toISOString();
|
|
126
|
+
const windows = ClaudeUsageWindowId.options.flatMap((id) => {
|
|
127
|
+
const window = (value as Record<string, unknown>)[id];
|
|
128
|
+
if (!window || typeof window !== "object" || Array.isArray(window)) return [];
|
|
129
|
+
const fields = window as Record<string, unknown>;
|
|
130
|
+
const usedPercent = percent(fields.utilization, 1);
|
|
131
|
+
const time = typeof fields.resets_at === "string" ? Date.parse(fields.resets_at) : NaN;
|
|
132
|
+
const resetsAt = Number.isFinite(time) ? new Date(time).toISOString() : null;
|
|
133
|
+
if (usedPercent === null && resetsAt === null) return [];
|
|
134
|
+
return [{ id, usedPercent, resetsAt, status: null, observedAt }];
|
|
135
|
+
});
|
|
136
|
+
return windows.length ? { windows, observedAt, source: "provider" } : null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function emptyClaudeUsage(credentialVersion: number | null): ClaudeSubscriptionUsage {
|
|
140
|
+
return {
|
|
141
|
+
connected: credentialVersion !== null,
|
|
142
|
+
credentialVersion,
|
|
143
|
+
windows: [],
|
|
144
|
+
observedAt: null,
|
|
145
|
+
source: null,
|
|
146
|
+
refreshStatus: "not_checked",
|
|
147
|
+
refreshCheckedAt: null,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Preserve omitted windows and reject observations older than their individual window. */
|
|
152
|
+
export function mergeClaudeUsage(
|
|
153
|
+
current: ClaudeSubscriptionUsage,
|
|
154
|
+
observation: ClaudeUsageObservation,
|
|
155
|
+
): ClaudeSubscriptionUsage {
|
|
156
|
+
const windows = new Map(current.windows.map((window) => [window.id, window]));
|
|
157
|
+
for (const incomingWindow of observation.windows) {
|
|
158
|
+
const window = { ...incomingWindow, source: incomingWindow.source ?? observation.source };
|
|
159
|
+
const previous = windows.get(window.id);
|
|
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;
|
|
311
|
+
}
|
|
312
|
+
return {
|
|
313
|
+
...current,
|
|
314
|
+
requestRestrictions,
|
|
315
|
+
requestStatus,
|
|
316
|
+
windows: ClaudeUsageWindowId.options.flatMap((id) =>
|
|
317
|
+
windows.has(id) ? [windows.get(id)!] : [],
|
|
318
|
+
),
|
|
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
|
+
: {}),
|
|
327
|
+
};
|
|
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
|
+
}
|