@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
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credit pricing for deployment-funded live (speech-to-speech) voice.
|
|
3
|
+
*
|
|
4
|
+
* Live voice is metered by server-observed connection time, billed per started
|
|
5
|
+
* minute. Prices are deployment configuration, never code defaults: when credit
|
|
6
|
+
* billing is active, a deployment-funded voice model without a configured price
|
|
7
|
+
* is not offered and cannot be started.
|
|
8
|
+
*/
|
|
9
|
+
export type RealtimeVoicePricing = {
|
|
10
|
+
/** Upstream cost of one minute of live voice, in USD micros. */
|
|
11
|
+
microsPerMinute: number;
|
|
12
|
+
/** Opengeni margin in basis points added on top of the upstream cost. */
|
|
13
|
+
marginBps?: number | undefined;
|
|
14
|
+
};
|
|
15
|
+
/** Model id of the deployment-hosted Azure GPT Live voice. */
|
|
16
|
+
export declare const AZURE_LIVE_REALTIME_MODEL_ID: "opengeni-azure/gpt-live-1";
|
|
17
|
+
/** One live-voice price, e.g. `{"microsPerMinute":50000,"marginBps":500}`. */
|
|
18
|
+
export declare function parseRealtimeVoicePricingJson(raw: string | undefined, env?: string): RealtimeVoicePricing | null;
|
|
19
|
+
/**
|
|
20
|
+
* Prices keyed by upstream AI Gateway realtime model id, e.g.
|
|
21
|
+
* `{"openai/gpt-realtime-2.1":{"microsPerMinute":120000,"marginBps":500}}`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function parseRealtimeVoicePricingTableJson(raw: string | undefined, env?: string): Record<string, RealtimeVoicePricing>;
|
|
24
|
+
/** Credit price of one started minute, after margin, rounded up to whole micros. */
|
|
25
|
+
export declare function realtimeVoiceMinuteCreditMicros(pricing: RealtimeVoicePricing): {
|
|
26
|
+
providerCostMicros: number;
|
|
27
|
+
creditCostMicros: number;
|
|
28
|
+
};
|
|
29
|
+
/** Billed minutes for a server-observed interval: every started minute, at least one. */
|
|
30
|
+
export declare function realtimeVoiceStartedMinutes(startedAt: Date, observedUntil: Date): number;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Stable account affinity shared by subscription providers. Candidate order is durable creation order. */
|
|
2
|
+
export declare function subscriptionAccountShardIndex(sessionId: string, candidateCount: number): number;
|
|
3
|
+
/**
|
|
4
|
+
* Manual pins are binding even when unavailable. Policy homes remain sticky only
|
|
5
|
+
* while rotation is on; an exhausted home re-shards over the eligible survivors.
|
|
6
|
+
* Selection never changes the pool's active pointer.
|
|
7
|
+
*/
|
|
8
|
+
export declare function selectSubscriptionAccount<T extends {
|
|
9
|
+
id: string;
|
|
10
|
+
}>(input: {
|
|
11
|
+
sessionId: string;
|
|
12
|
+
eligible: readonly T[];
|
|
13
|
+
rotationEnabled: boolean;
|
|
14
|
+
activeCredentialId: string | null;
|
|
15
|
+
pinnedCredentialId: string | null;
|
|
16
|
+
pinSource: "manual" | "policy" | null;
|
|
17
|
+
}): T | null;
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* Provider-agnostic web search: the deployment-funded `web_search` and
|
|
4
|
+
* `web_fetch` agent tools.
|
|
5
|
+
*
|
|
6
|
+
* Hosted search (the provider-executed `web_search` tool of the model's own
|
|
7
|
+
* API) follows the model catalog. Provider search is a separate,
|
|
8
|
+
* deployment-configured HTTP provider that Opengeni calls from the worker.
|
|
9
|
+
* Search and page reading are separate slots; each slot holds an ordered
|
|
10
|
+
* list of providers, and a call moves to the next provider when one fails.
|
|
11
|
+
* By default the provider tools are offered only where the turn has no
|
|
12
|
+
* hosted search (`OPENGENI_WEB_SEARCH_PREFER=native`).
|
|
13
|
+
*
|
|
14
|
+
* Adding a provider means one catalog entry here plus one adapter in
|
|
15
|
+
* `@opengeni/runtime/web-search`.
|
|
16
|
+
*/
|
|
17
|
+
export declare const WEB_SEARCH_PROVIDER_IDS: readonly ["tinyfish", "parallel", "perplexity", "exa", "tavily", "firecrawl", "brave", "jina", "searxng"];
|
|
18
|
+
export type WebSearchProviderId = (typeof WEB_SEARCH_PROVIDER_IDS)[number];
|
|
19
|
+
/** Used when `OPENGENI_WEB_SEARCH_PROVIDER` is unset: free with an account key. */
|
|
20
|
+
export declare const DEFAULT_WEB_SEARCH_PROVIDER: WebSearchProviderId;
|
|
21
|
+
/** Model-visible names of the provider tools. */
|
|
22
|
+
export declare const WEB_SEARCH_TOOL_NAME = "web_search";
|
|
23
|
+
export declare const WEB_FETCH_TOOL_NAME = "web_fetch";
|
|
24
|
+
export type WebSearchProviderToolName = typeof WEB_SEARCH_TOOL_NAME | typeof WEB_FETCH_TOOL_NAME;
|
|
25
|
+
/**
|
|
26
|
+
* `native` (default): offer provider tools only to turns without hosted
|
|
27
|
+
* search. `provider`: offer them to every turn and withhold the SDK-hosted
|
|
28
|
+
* `web_search` tool. SuperGrok keeps its transport-level native search either
|
|
29
|
+
* way because Opengeni does not attach it as an agent tool.
|
|
30
|
+
*/
|
|
31
|
+
export declare const WebSearchPreference: z.ZodEnum<{
|
|
32
|
+
native: "native";
|
|
33
|
+
provider: "provider";
|
|
34
|
+
}>;
|
|
35
|
+
export type WebSearchPreference = z.infer<typeof WebSearchPreference>;
|
|
36
|
+
/** One operation (search or page fetch) a provider offers. */
|
|
37
|
+
export type WebSearchProviderOperation = {
|
|
38
|
+
/** Pay-as-you-go list price of one successful call, in USD micros. */
|
|
39
|
+
listPriceMicros: number;
|
|
40
|
+
/** The operation needs an API key; keyless calls are billed as free. */
|
|
41
|
+
keyRequired: boolean;
|
|
42
|
+
/** Default API base URL; null when the operator must supply one. */
|
|
43
|
+
defaultBaseUrl: string | null;
|
|
44
|
+
};
|
|
45
|
+
export type WebSearchProviderCatalogEntry = {
|
|
46
|
+
id: WebSearchProviderId;
|
|
47
|
+
label: string;
|
|
48
|
+
search: WebSearchProviderOperation | null;
|
|
49
|
+
fetch: WebSearchProviderOperation | null;
|
|
50
|
+
/** Where the list price comes from (checked 2026-10). */
|
|
51
|
+
pricingUrl: string;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Every supported provider with its capabilities, default endpoints and
|
|
55
|
+
* list prices. A provider response that reports its own dollar cost (Exa
|
|
56
|
+
* `costDollars`, Tavily `usage.credits`) bills that cost instead, and
|
|
57
|
+
* `OPENGENI_WEB_SEARCH_PRICING_JSON` overrides both.
|
|
58
|
+
*/
|
|
59
|
+
export declare const WEB_SEARCH_PROVIDER_CATALOG: Readonly<Record<WebSearchProviderId, WebSearchProviderCatalogEntry>>;
|
|
60
|
+
/** Per-provider API key variable, e.g. `OPENGENI_WEB_TINYFISH_API_KEY`. */
|
|
61
|
+
export declare function webSearchProviderApiKeyEnv(id: WebSearchProviderId): string;
|
|
62
|
+
/** Per-provider base URL variable, e.g. `OPENGENI_WEB_SEARXNG_BASE_URL`. */
|
|
63
|
+
export declare function webSearchProviderBaseUrlEnv(id: WebSearchProviderId): string;
|
|
64
|
+
/** Every environment variable that configures provider web search. */
|
|
65
|
+
export declare function webSearchEnvironmentVariables(): string[];
|
|
66
|
+
export type WebSearchProviderCredentials = Partial<Record<string, {
|
|
67
|
+
apiKey?: string | undefined;
|
|
68
|
+
baseUrl?: string | undefined;
|
|
69
|
+
}>>;
|
|
70
|
+
export declare const WebSearchProviderCredentialsSchema: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
71
|
+
apiKey: z.ZodOptional<z.ZodString>;
|
|
72
|
+
baseUrl: z.ZodOptional<z.ZodString>;
|
|
73
|
+
}, z.core.$strict>>>;
|
|
74
|
+
/** Per-provider keys and base URLs from the environment. */
|
|
75
|
+
export declare function webSearchProviderCredentialsFromEnv(source: Record<string, string | undefined>): WebSearchProviderCredentials;
|
|
76
|
+
/**
|
|
77
|
+
* Upstream price of one call in integer USD micros, plus the Opengeni
|
|
78
|
+
* margin in basis points (500 = +5%, as for model pricing). Credit billing
|
|
79
|
+
* charges `ceil(providerCost * (10000 + marginBps) / 10000)` per call.
|
|
80
|
+
*/
|
|
81
|
+
export type WebSearchPricing = {
|
|
82
|
+
searchMicros: number;
|
|
83
|
+
fetchMicros: number;
|
|
84
|
+
marginBps?: number | undefined;
|
|
85
|
+
};
|
|
86
|
+
/** Tavily's pay-as-you-go price of one credit, used for reported usage. */
|
|
87
|
+
export declare const TAVILY_CREDIT_MICROS = 8000;
|
|
88
|
+
export type WebSearchProviderEndpoint = {
|
|
89
|
+
provider: WebSearchProviderId;
|
|
90
|
+
apiKey: string | null;
|
|
91
|
+
baseUrl: string | null;
|
|
92
|
+
};
|
|
93
|
+
export type WebSearchProviderConfig = {
|
|
94
|
+
preference: WebSearchPreference;
|
|
95
|
+
/** Search providers in failover order; never empty. */
|
|
96
|
+
search: WebSearchProviderEndpoint[];
|
|
97
|
+
/** Page readers in failover order; empty when pages cannot be fetched. */
|
|
98
|
+
fetch: WebSearchProviderEndpoint[];
|
|
99
|
+
/** Explicit operator prices by provider, overriding list prices. */
|
|
100
|
+
pricingOverrides: Partial<Record<WebSearchProviderId, WebSearchPricing>>;
|
|
101
|
+
timeoutMs: number;
|
|
102
|
+
};
|
|
103
|
+
export type WebSearchSettings = {
|
|
104
|
+
webSearchEnabled: boolean;
|
|
105
|
+
webSearchProvider?: string | undefined;
|
|
106
|
+
webFetchProvider?: string | undefined;
|
|
107
|
+
webSearchPrefer?: string | undefined;
|
|
108
|
+
/** Per-provider keys and base URLs (`OPENGENI_WEB_<PROVIDER>_API_KEY` / `_BASE_URL`). */
|
|
109
|
+
webSearchProviderCredentials?: WebSearchProviderCredentials | undefined;
|
|
110
|
+
/** Earlier single-provider settings: apply to the first provider of their slot. */
|
|
111
|
+
webSearchApiKey?: string | undefined;
|
|
112
|
+
webSearchBaseUrl?: string | undefined;
|
|
113
|
+
webFetchApiKey?: string | undefined;
|
|
114
|
+
webFetchBaseUrl?: string | undefined;
|
|
115
|
+
webSearchProviderMode?: string | undefined;
|
|
116
|
+
webSearchPricingJson?: string | undefined;
|
|
117
|
+
webSearchRequestTimeoutMs?: number | undefined;
|
|
118
|
+
/** Credit billing is active for this deployment (see webSearchCreditBillingActive). */
|
|
119
|
+
billingMode?: string | undefined;
|
|
120
|
+
usageLimitsMode?: string | undefined;
|
|
121
|
+
};
|
|
122
|
+
export type WebSearchProviderResolution = {
|
|
123
|
+
status: "off";
|
|
124
|
+
} | {
|
|
125
|
+
status: "invalid";
|
|
126
|
+
reason: string;
|
|
127
|
+
} | {
|
|
128
|
+
status: "configured";
|
|
129
|
+
config: WebSearchProviderConfig;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Resolve the deployment's provider search. `off` applies when
|
|
133
|
+
* `OPENGENI_WEB_SEARCH_ENABLED=false` (the server-wide web search switch),
|
|
134
|
+
* when the provider is `none`, and when the provider is left at its default
|
|
135
|
+
* (TinyFish) without that provider's key. `invalid` means an operator named
|
|
136
|
+
* providers but the configuration cannot work; the tools stay unoffered and
|
|
137
|
+
* the worker logs the reason.
|
|
138
|
+
*/
|
|
139
|
+
export declare function resolveWebSearchProvider(settings: WebSearchSettings): WebSearchProviderResolution;
|
|
140
|
+
/** The configured provider, or null when off or invalid. */
|
|
141
|
+
export declare function webSearchProviderConfig(settings: WebSearchSettings): WebSearchProviderConfig | null;
|
|
142
|
+
/**
|
|
143
|
+
* The single web-search plan for one turn, shared by the worker (which tools
|
|
144
|
+
* it attaches) and the API's effective-tools projection (what it reports).
|
|
145
|
+
*
|
|
146
|
+
* `hostedWebSearch` is whether the resolved model attaches the SDK-hosted
|
|
147
|
+
* `web_search` tool. `transportHostedSearch` is true for providers whose native
|
|
148
|
+
* search is added by the transport rather than as an agent tool (SuperGrok).
|
|
149
|
+
*/
|
|
150
|
+
export declare function webSearchToolPlan(settings: WebSearchSettings, turn: {
|
|
151
|
+
hostedWebSearch: boolean;
|
|
152
|
+
transportHostedSearch?: boolean;
|
|
153
|
+
/**
|
|
154
|
+
* The workspace turned Opengeni credits off: a provider tool is offered
|
|
155
|
+
* only when one of its providers is free (admission still refuses paid
|
|
156
|
+
* calls if the switch flips mid-turn).
|
|
157
|
+
*/
|
|
158
|
+
creditsDisabled?: boolean;
|
|
159
|
+
}): {
|
|
160
|
+
hostedWebSearch: boolean;
|
|
161
|
+
providerTools: WebSearchProviderToolName[];
|
|
162
|
+
};
|
|
163
|
+
/** Whether this deployment bills web search and fetch in Opengeni credits. */
|
|
164
|
+
export declare function webSearchCreditBillingActive(settings: Pick<WebSearchSettings, "billingMode" | "usageLimitsMode">): boolean;
|
|
165
|
+
/** Whether every provider of this tool would spend Opengeni credits per call. */
|
|
166
|
+
export declare function webSearchToolSpendsCredits(settings: WebSearchSettings, config: WebSearchProviderConfig, tool: WebSearchProviderToolName): boolean;
|
|
167
|
+
/** Upstream price for one call to one provider, before any provider-reported cost. */
|
|
168
|
+
export declare function webSearchCallPricing(config: Pick<WebSearchProviderConfig, "pricingOverrides">, endpoint: WebSearchProviderEndpoint, operation: "search" | "fetch"): {
|
|
169
|
+
providerMicros: number;
|
|
170
|
+
marginBps: number;
|
|
171
|
+
explicit: boolean;
|
|
172
|
+
};
|
|
173
|
+
/** Credit cost of a provider cost after margin, rounded up to whole micros. */
|
|
174
|
+
export declare function webSearchCreditMicros(providerMicros: number, marginBps: number): number;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opengeni/config",
|
|
3
|
-
"version": "3.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "3.1.2",
|
|
4
|
+
"description": "Opengeni runtime configuration: settings resolution, deployment knobs, and config validation shared across the server packages.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"prepublishOnly": "bash ../../scripts/prepublish-guard"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@opengeni/codex": "
|
|
37
|
-
"@opengeni/contracts": "
|
|
38
|
-
"@opengeni/xai-subscription": "
|
|
36
|
+
"@opengeni/codex": "3.1.2",
|
|
37
|
+
"@opengeni/contracts": "3.1.2",
|
|
38
|
+
"@opengeni/xai-subscription": "3.1.2",
|
|
39
39
|
"zod": "^4.2.1"
|
|
40
40
|
},
|
|
41
41
|
"engines": {
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { ClaudeSubscriptionUsage, ClaudeUsageWindow } from "@opengeni/contracts";
|
|
2
|
+
|
|
3
|
+
const UNKNOWN_RESET_RECHECK_MS = 60_000;
|
|
4
|
+
const allowed = (status: ClaudeUsageWindow["status"]) =>
|
|
5
|
+
status === "allowed" || status === "allowed_warning";
|
|
6
|
+
|
|
7
|
+
/** Evaluate provider-reported plan capacity and its explicitly allowed paid fallback. */
|
|
8
|
+
export function claudeSubscriptionCapacity(
|
|
9
|
+
usage: ClaudeSubscriptionUsage,
|
|
10
|
+
upstreamModelId: string,
|
|
11
|
+
now: Date,
|
|
12
|
+
options: { allowOverage?: boolean } = {},
|
|
13
|
+
): { available: boolean; nextCheckAt: Date | null; blockingWindows: ClaudeUsageWindow["id"][] } {
|
|
14
|
+
const available = { available: true, nextCheckAt: null, blockingWindows: [] };
|
|
15
|
+
if (!usage.connected || usage.refreshStatus === "reconnect")
|
|
16
|
+
return { ...available, available: false };
|
|
17
|
+
const relevant = (id: ClaudeUsageWindow["id"]): boolean => {
|
|
18
|
+
if (id === "seven_day_opus") return /^claude-opus-/.test(upstreamModelId);
|
|
19
|
+
if (id === "seven_day_sonnet") return /^claude-sonnet-/.test(upstreamModelId);
|
|
20
|
+
return id === "five_hour" || id === "seven_day";
|
|
21
|
+
};
|
|
22
|
+
const applicable = usage.windows.filter((window) => relevant(window.id));
|
|
23
|
+
const relevantClaim = (id: ClaudeUsageWindow["id"] | null) =>
|
|
24
|
+
id === "seven_day_opus" || id === "seven_day_sonnet" ? relevant(id) : true;
|
|
25
|
+
const restrictions = (usage.requestRestrictions ?? []).filter(
|
|
26
|
+
(item) =>
|
|
27
|
+
item.status === "rejected" &&
|
|
28
|
+
(item.upstreamModelId === upstreamModelId ||
|
|
29
|
+
(item.upstreamModelId === null && relevantClaim(item.representativeClaim))) &&
|
|
30
|
+
!applicable.some(
|
|
31
|
+
(window) =>
|
|
32
|
+
window.source === "provider" &&
|
|
33
|
+
window.usedPercent !== null &&
|
|
34
|
+
window.usedPercent < 100 &&
|
|
35
|
+
window.id === item.representativeClaim &&
|
|
36
|
+
window.observedAt > item.observedAt,
|
|
37
|
+
) &&
|
|
38
|
+
(item.resetsAt
|
|
39
|
+
? Date.parse(item.resetsAt)
|
|
40
|
+
: Date.parse(item.observedAt) + UNKNOWN_RESET_RECHECK_MS) > now.getTime(),
|
|
41
|
+
);
|
|
42
|
+
const blocking = applicable.filter((window) => {
|
|
43
|
+
if (
|
|
44
|
+
(window.resetsAt
|
|
45
|
+
? Date.parse(window.resetsAt)
|
|
46
|
+
: Date.parse(window.observedAt) + UNKNOWN_RESET_RECHECK_MS) <= now.getTime()
|
|
47
|
+
)
|
|
48
|
+
return false;
|
|
49
|
+
return (
|
|
50
|
+
window.status === "rejected" || (window.status === null && (window.usedPercent ?? 0) >= 100)
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
const request = usage.requestStatus;
|
|
54
|
+
const exactModel = request?.upstreamModelId === upstreamModelId;
|
|
55
|
+
const relevantRequest =
|
|
56
|
+
request &&
|
|
57
|
+
(exactModel ||
|
|
58
|
+
(request.upstreamModelId === null && relevantClaim(request.representativeClaim)));
|
|
59
|
+
const freshRequest =
|
|
60
|
+
relevantRequest &&
|
|
61
|
+
applicable.every((window) => window.observedAt <= request.observedAt) &&
|
|
62
|
+
restrictions.every((item) => item.observedAt <= request.observedAt);
|
|
63
|
+
const planResetElapsed =
|
|
64
|
+
request &&
|
|
65
|
+
(request.resetsAt
|
|
66
|
+
? Date.parse(request.resetsAt)
|
|
67
|
+
: Date.parse(request.observedAt) + UNKNOWN_RESET_RECHECK_MS) <= now.getTime();
|
|
68
|
+
const exactAllowance = (usage.requestRestrictions ?? []).find(
|
|
69
|
+
(item) =>
|
|
70
|
+
item.upstreamModelId === upstreamModelId &&
|
|
71
|
+
allowed(item.status) &&
|
|
72
|
+
blocking.every((window) => window.observedAt < item.observedAt) &&
|
|
73
|
+
restrictions.every((restriction) => restriction.observedAt < item.observedAt),
|
|
74
|
+
);
|
|
75
|
+
if (exactAllowance) return available;
|
|
76
|
+
// An allowed response for Sonnet never overrides an independently observed
|
|
77
|
+
// Opus cap. Only the exact model's fresh response proves overall dispatch.
|
|
78
|
+
if (freshRequest && exactModel && allowed(request.status) && !planResetElapsed) return available;
|
|
79
|
+
const mayUseOverage = options.allowOverage !== false;
|
|
80
|
+
const overageResetElapsed =
|
|
81
|
+
request?.overageResetsAt && Date.parse(request.overageResetsAt) <= now.getTime();
|
|
82
|
+
if (
|
|
83
|
+
freshRequest &&
|
|
84
|
+
request.status === "rejected" &&
|
|
85
|
+
mayUseOverage &&
|
|
86
|
+
allowed(request.overageStatus) &&
|
|
87
|
+
!overageResetElapsed
|
|
88
|
+
)
|
|
89
|
+
return available;
|
|
90
|
+
const rejectedRequest = freshRequest && request.status === "rejected" && !planResetElapsed;
|
|
91
|
+
if (blocking.length === 0 && !rejectedRequest && restrictions.length === 0) return available;
|
|
92
|
+
// An extra-usage balance is a separate fallback. Its rejection does not block
|
|
93
|
+
// an available base plan or delay the next base-plan reset to a monthly date.
|
|
94
|
+
const resets = blocking.map((window) =>
|
|
95
|
+
window.resetsAt
|
|
96
|
+
? Date.parse(window.resetsAt)
|
|
97
|
+
: Date.parse(window.observedAt) + UNKNOWN_RESET_RECHECK_MS,
|
|
98
|
+
);
|
|
99
|
+
if (rejectedRequest)
|
|
100
|
+
resets.push(
|
|
101
|
+
request.resetsAt
|
|
102
|
+
? Date.parse(request.resetsAt)
|
|
103
|
+
: Date.parse(request.observedAt) + UNKNOWN_RESET_RECHECK_MS,
|
|
104
|
+
);
|
|
105
|
+
resets.push(
|
|
106
|
+
...restrictions.map((item) =>
|
|
107
|
+
item.resetsAt
|
|
108
|
+
? Date.parse(item.resetsAt)
|
|
109
|
+
: Date.parse(item.observedAt) + UNKNOWN_RESET_RECHECK_MS,
|
|
110
|
+
),
|
|
111
|
+
);
|
|
112
|
+
let nextCheckAt = Math.max(...resets);
|
|
113
|
+
if (
|
|
114
|
+
rejectedRequest &&
|
|
115
|
+
mayUseOverage &&
|
|
116
|
+
request.overageStatus === "rejected" &&
|
|
117
|
+
request.overageResetsAt
|
|
118
|
+
) {
|
|
119
|
+
const overageReset = Date.parse(request.overageResetsAt);
|
|
120
|
+
// A known reset permits a new authoritative probe, never invents a reading.
|
|
121
|
+
if (overageReset <= now.getTime()) return available;
|
|
122
|
+
nextCheckAt = Math.min(nextCheckAt, overageReset);
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
available: false,
|
|
126
|
+
nextCheckAt: new Date(nextCheckAt),
|
|
127
|
+
blockingWindows: blocking.map((window) => window.id),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/** Claude Code 2.1.285's installed OAuth flow; model calls and profile reads only. */
|
|
4
|
+
export const CLAUDE_OAUTH_CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e";
|
|
5
|
+
export const CLAUDE_OAUTH_AUTHORIZE_URL = "https://claude.com/cai/oauth/authorize";
|
|
6
|
+
export const CLAUDE_OAUTH_TOKEN_URL = "https://platform.claude.com/v1/oauth/token";
|
|
7
|
+
export const CLAUDE_OAUTH_REDIRECT_URL = "https://platform.claude.com/oauth/code/callback";
|
|
8
|
+
export const CLAUDE_OAUTH_SCOPES = ["user:inference", "user:profile"] as const;
|
|
9
|
+
|
|
10
|
+
export const ClaudeOAuthTokenResponse = z
|
|
11
|
+
.object({
|
|
12
|
+
access_token: z
|
|
13
|
+
.string()
|
|
14
|
+
.max(16384)
|
|
15
|
+
.regex(/^sk-ant-oat[0-9]+-\S+$/),
|
|
16
|
+
refresh_token: z.string().min(1).max(16384).optional(),
|
|
17
|
+
expires_in: z.number().int().positive().max(31536000),
|
|
18
|
+
scope: z.string().min(1).max(8192),
|
|
19
|
+
})
|
|
20
|
+
.passthrough();
|
|
21
|
+
|
|
22
|
+
export function claudeAuthorizationUrl(input: { challenge: string; state: string }) {
|
|
23
|
+
const url = new URL(CLAUDE_OAUTH_AUTHORIZE_URL);
|
|
24
|
+
url.searchParams.set("code", "true");
|
|
25
|
+
url.searchParams.set("client_id", CLAUDE_OAUTH_CLIENT_ID);
|
|
26
|
+
url.searchParams.set("response_type", "code");
|
|
27
|
+
url.searchParams.set("redirect_uri", CLAUDE_OAUTH_REDIRECT_URL);
|
|
28
|
+
url.searchParams.set("scope", CLAUDE_OAUTH_SCOPES.join(" "));
|
|
29
|
+
url.searchParams.set("code_challenge", input.challenge);
|
|
30
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
31
|
+
url.searchParams.set("state", input.state);
|
|
32
|
+
return url.toString();
|
|
33
|
+
}
|