@cortexkit/common-auth 0.32.1 → 0.34.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/claustrum/consumer.d.ts +12 -0
- package/dist/claustrum/consumer.js +36 -0
- package/dist/claustrum/custody.d.ts +16 -4
- package/dist/claustrum/custody.js +74 -9
- package/dist/claustrum/hub.d.ts +3 -2
- package/dist/claustrum/hub.js +8 -0
- package/dist/claustrum/index.d.ts +1 -0
- package/dist/claustrum/roster.d.ts +2 -0
- package/dist/claustrum/roster.js +2 -0
- package/dist/claustrum/usage.d.ts +41 -0
- package/dist/claustrum/usage.js +145 -0
- package/dist/commands/builtins.d.ts +5 -0
- package/dist/commands/builtins.js +38 -0
- package/dist/fs/private-files.d.ts +5 -0
- package/dist/fs/private-files.js +7 -0
- package/dist/fs/refresh-file-lock.js +144 -49
- package/dist/fs/with-lock.js +34 -10
- package/dist/limits/activity.d.ts +26 -0
- package/dist/limits/activity.js +52 -0
- package/dist/limits/axes.d.ts +63 -0
- package/dist/limits/axes.js +7 -0
- package/dist/limits/bridge.d.ts +4 -0
- package/dist/limits/bridge.js +10 -0
- package/dist/limits/decide.d.ts +141 -0
- package/dist/limits/decide.js +383 -0
- package/dist/limits/duration.d.ts +8 -0
- package/dist/limits/duration.js +20 -0
- package/dist/limits/envelope-loss.d.ts +29 -0
- package/dist/limits/envelope-loss.js +150 -0
- package/dist/limits/families.d.ts +57 -0
- package/dist/limits/families.js +143 -0
- package/dist/limits/header-classifier.d.ts +9 -0
- package/dist/limits/header-classifier.js +121 -0
- package/dist/limits/hold.d.ts +13 -0
- package/dist/limits/hold.js +25 -0
- package/dist/limits/identity.d.ts +14 -0
- package/dist/limits/identity.js +21 -0
- package/dist/limits/index.d.ts +44 -0
- package/dist/limits/index.js +25 -0
- package/dist/limits/jitter.d.ts +2 -0
- package/dist/limits/jitter.js +9 -0
- package/dist/limits/labels.d.ts +3 -0
- package/dist/limits/labels.js +11 -0
- package/dist/limits/marker.d.ts +64 -0
- package/dist/limits/marker.js +96 -0
- package/dist/limits/merge-laws.d.ts +14 -0
- package/dist/limits/merge-laws.js +31 -0
- package/dist/limits/namespace.d.ts +89 -0
- package/dist/limits/namespace.js +183 -0
- package/dist/limits/persistence.d.ts +33 -0
- package/dist/limits/persistence.js +9 -0
- package/dist/limits/pi-wait.d.ts +16 -0
- package/dist/limits/pi-wait.js +51 -0
- package/dist/limits/pool-adapter.d.ts +16 -0
- package/dist/limits/pool-adapter.js +162 -0
- package/dist/limits/presets.d.ts +11 -0
- package/dist/limits/presets.js +223 -0
- package/dist/limits/registry.d.ts +49 -0
- package/dist/limits/registry.js +471 -0
- package/dist/limits/request-history.d.ts +77 -0
- package/dist/limits/request-history.js +117 -0
- package/dist/limits/reset.d.ts +21 -0
- package/dist/limits/reset.js +44 -0
- package/dist/limits/routing-hold.d.ts +16 -0
- package/dist/limits/routing-hold.js +23 -0
- package/dist/limits/scope.d.ts +12 -0
- package/dist/limits/scope.js +10 -0
- package/dist/limits/signal.d.ts +41 -0
- package/dist/limits/signal.js +1 -0
- package/dist/limits/terminal.d.ts +52 -0
- package/dist/limits/terminal.js +133 -0
- package/dist/limits/vault-adapter.d.ts +16 -0
- package/dist/limits/vault-adapter.js +123 -0
- package/dist/limits/vault.d.ts +1 -0
- package/dist/limits/vault.js +1 -0
- package/dist/limits/wake.d.ts +21 -0
- package/dist/limits/wake.js +47 -0
- package/dist/limits/wording.d.ts +25 -0
- package/dist/limits/wording.js +114 -0
- package/dist/opencode2/index.d.ts +1 -1
- package/dist/opencode2/install.d.ts +2 -2
- package/dist/opencode2/install.js +161 -18
- package/dist/opencode2/types.d.ts +95 -9
- package/dist/quota/format.d.ts +9 -4
- package/dist/quota/format.js +45 -26
- package/dist/quota-sidebar/index.jsx +27 -19
- package/dist/routing/admission.d.ts +7 -2
- package/dist/routing/admission.js +17 -7
- package/dist/routing-accessor/handle.d.ts +1 -0
- package/dist/routing-accessor/handle.js +10 -0
- package/dist/routing-accessor/index.d.ts +18 -2
- package/dist/routing-accessor/index.js +21 -11
- package/dist/rpc/port-file.d.ts +12 -3
- package/dist/rpc/port-file.js +46 -4
- package/dist/store/attribution.js +3 -2
- package/dist/store/errors.d.ts +1 -1
- package/dist/store/hooks.d.ts +5 -1
- package/dist/store/hooks.js +20 -5
- package/dist/store/index.d.ts +1 -0
- package/dist/store/limits-namespace.d.ts +7 -0
- package/dist/store/limits-namespace.js +34 -0
- package/dist/store/pool.d.ts +10 -1
- package/dist/store/pool.js +2 -0
- package/dist/store/provider-state.js +5 -5
- package/dist/store/refresh.js +1 -1
- package/dist/store/schema.js +9 -4
- package/dist/store/settings.d.ts +2 -0
- package/dist/store/settings.js +11 -4
- package/package.json +10 -2
- package/src/quota/format.ts +56 -26
- package/src/quota-sidebar/index.tsx +56 -32
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Plugin } from '@opencode/plugin';
|
|
2
2
|
import type { SessionRequestKind, SessionRetryDecision } from '@opencode/plugin/promise/session';
|
|
3
|
+
import type { WaitCheckpoint } from '../limits/decide.js';
|
|
3
4
|
/** The parts of the OpenCode 2 plugin context the installer uses. */
|
|
4
5
|
export type OpenCode2HookContext = {
|
|
5
6
|
readonly session: Pick<Plugin.Context['session'], 'hook'>;
|
|
@@ -35,6 +36,8 @@ export interface EventVerdict<Q> {
|
|
|
35
36
|
readonly outputStarted?: boolean;
|
|
36
37
|
readonly quota?: Q;
|
|
37
38
|
readonly limit?: LimitSignal;
|
|
39
|
+
/** Applies only to this attempt, after ownership and attribution checks. */
|
|
40
|
+
readonly hostRetry?: RetryDisposition['hostRetry'];
|
|
38
41
|
/**
|
|
39
42
|
* This is the last event of the response (completed or failed); the
|
|
40
43
|
* attempt ends here. On WebSocket this is the only way an attempt ends
|
|
@@ -55,6 +58,16 @@ export type HeaderEdits = Readonly<Record<string, string | null>>;
|
|
|
55
58
|
export interface ChooseAccountInput extends RequestScope {
|
|
56
59
|
/** The retry hook's advance choice (which may hold), or a request's own choice. */
|
|
57
60
|
readonly phase: 'retry' | 'request';
|
|
61
|
+
/** Retry hook draft's attempt count; also carried by a consumed hold's wake. */
|
|
62
|
+
readonly hostAttempt?: number;
|
|
63
|
+
/** Revalidate the held account before dispatch, even if the target is not due. */
|
|
64
|
+
readonly wake?: RetryWake;
|
|
65
|
+
/**
|
|
66
|
+
* Accounts refused by earlier reroutes of this logical request. Absent means
|
|
67
|
+
* empty. Holds and wakes do not exclude the held account. A snapshot, not a
|
|
68
|
+
* live set; supplied whether or not enforcement is enabled.
|
|
69
|
+
*/
|
|
70
|
+
readonly excluded?: readonly string[];
|
|
58
71
|
/**
|
|
59
72
|
* The account of the attempt this one retries, when the retry hook just
|
|
60
73
|
* asked the host to retry an attempt of this session and kind; otherwise
|
|
@@ -75,19 +88,43 @@ export interface ChooseAccountInput extends RequestScope {
|
|
|
75
88
|
export interface AccountRequest extends RequestScope {
|
|
76
89
|
readonly accountId: string;
|
|
77
90
|
}
|
|
91
|
+
/** The held account is ChooseAccountInput.previousAccountId. */
|
|
92
|
+
export interface RetryWake {
|
|
93
|
+
readonly target: number;
|
|
94
|
+
readonly checkpoint?: WaitCheckpoint;
|
|
95
|
+
/** Terminal quota waits do not require the session's provider prompt-cache TTL. */
|
|
96
|
+
readonly kind?: 'hold' | 'terminal';
|
|
97
|
+
}
|
|
78
98
|
/** An explicit retry on the refused account, without moving its routing pin. */
|
|
79
99
|
export interface RetryHold {
|
|
80
100
|
readonly hold: {
|
|
81
101
|
/**
|
|
82
102
|
* Adapter-computed wait in milliseconds, including per-session jitter.
|
|
83
|
-
* Finite, non-negative values
|
|
84
|
-
*
|
|
103
|
+
* Finite, non-negative values up to MAX_RETRY_HOLD_MS (3_621_000) are
|
|
104
|
+
* accepted. Larger or invalid values keep the host's decision and release
|
|
105
|
+
* the choice rather than dispatching before the requested wait has elapsed.
|
|
85
106
|
*/
|
|
86
107
|
readonly delayMs: number;
|
|
108
|
+
/** An absolute wake target; its presence forces a request-phase re-check. */
|
|
109
|
+
readonly target?: number;
|
|
110
|
+
readonly checkpoint?: WaitCheckpoint;
|
|
111
|
+
/** Forwarded to wake.kind so terminal quota waits omit the cache bound. */
|
|
112
|
+
readonly kind?: RetryWake['kind'];
|
|
87
113
|
};
|
|
88
114
|
}
|
|
89
|
-
/**
|
|
90
|
-
export
|
|
115
|
+
/** Leave the host's retry decision alone, or forbid replay of this attempt. */
|
|
116
|
+
export interface RetryDisposition {
|
|
117
|
+
readonly hostRetry: 'leave' | 'veto';
|
|
118
|
+
}
|
|
119
|
+
/** Wait through one host retry, then choose afresh without reserving an account. */
|
|
120
|
+
export interface RetryWaitThenReselect {
|
|
121
|
+
readonly waitThenReselect: {
|
|
122
|
+
/** Same finite, non-negative delay bound as RetryHold.hold.delayMs. */
|
|
123
|
+
readonly delayMs: number;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/** Non-string choices are valid only in phase `retry`, after an account limit. */
|
|
127
|
+
export type ChooseAccountResult = string | RetryHold | RetryDisposition | RetryWaitThenReselect | undefined;
|
|
91
128
|
/**
|
|
92
129
|
* One physical send: created when an account is chosen for a request, and
|
|
93
130
|
* handed back on every hook call and event that belongs to that send, so a
|
|
@@ -194,7 +231,7 @@ export interface SameAccountRetryResult {
|
|
|
194
231
|
* quota reading type; the installer only carries it to the `quota` event.
|
|
195
232
|
* `A` is the plugin's per-attempt value (see `Attempt`).
|
|
196
233
|
* `Choice` defaults to the legacy ID-only result so existing adapters' direct
|
|
197
|
-
* callers still see IDs. Opt into
|
|
234
|
+
* callers still see IDs. Opt into retry directives with `ChooseAccountResult`.
|
|
198
235
|
*/
|
|
199
236
|
export interface OpenCode2AuthAdapter<Q = unknown, A = unknown, Choice extends ChooseAccountResult = string | undefined> {
|
|
200
237
|
/** Every hook is scoped to this provider; other providers are untouched. */
|
|
@@ -209,11 +246,24 @@ export interface OpenCode2AuthAdapter<Q = unknown, A = unknown, Choice extends C
|
|
|
209
246
|
* After a limit before any output, the retry hook makes this call itself,
|
|
210
247
|
* with `phase: 'retry'` and `rerouteFrom` set. Every returned account,
|
|
211
248
|
* including the refused one, is kept for the next attempt of that session
|
|
212
|
-
* and kind: it does not choose again
|
|
249
|
+
* and kind: it does not choose again, except for a hold with a target.
|
|
250
|
+
* Another account retries at once;
|
|
213
251
|
* the refused account keeps the host's decision and wait. `{ hold: { delayMs } }`
|
|
214
252
|
* commits the refused account without changing its pin and replaces the host's
|
|
215
253
|
* wait, even its no-retry decision, only for a recognised account limit before
|
|
216
|
-
* output in
|
|
254
|
+
* output in a session with no foreign transport. A hold with a target releases its queued
|
|
255
|
+
* choice and calls again in phase `request` with `wake` and `hostAttempt`
|
|
256
|
+
* before fetching account headers. The adapter must check that the account
|
|
257
|
+
* is still eligible, still represents the same login, is still available
|
|
258
|
+
* from the credential store and has any required provider prompt-cache TTL
|
|
259
|
+
* remaining, then choose normally if invalid.
|
|
260
|
+
* A hold without a target takes the queued account as before.
|
|
261
|
+
* `{ hostRetry: 'veto' }` forbids replay; `'leave'` preserves the host's decision
|
|
262
|
+
* without reserving an account. `{ waitThenReselect: { delayMs } }` asks for one
|
|
263
|
+
* delayed host retry, then chooses afresh. It commits no account or routing pin.
|
|
264
|
+
* Like holds, its delay must fit MAX_RETRY_HOLD_MS and the host retry budget.
|
|
265
|
+
* These directives obey the same ownership, attribution and output fences.
|
|
266
|
+
* Non-string choices returned in phase `request` cannot
|
|
217
267
|
* select an account and are treated as undefined. `undefined` or a
|
|
218
268
|
* throw commits nothing: the host decides, and the next attempt chooses
|
|
219
269
|
* with `phase: 'request'`, `previousAccountId` and no `rerouteFrom`.
|
|
@@ -222,7 +272,8 @@ export interface OpenCode2AuthAdapter<Q = unknown, A = unknown, Choice extends C
|
|
|
222
272
|
/**
|
|
223
273
|
* Releases a kept retry choice that will not be used: no retry, a changed
|
|
224
274
|
* model or agent, cancellation of a queued retry (session idle), session forgetting/deletion,
|
|
225
|
-
* disposal
|
|
275
|
+
* disposal, record eviction or a targeted hold's wake re-check. A hold
|
|
276
|
+
* releases the refused account's choice.
|
|
226
277
|
* Called once with the original choose input plus its returned accountId,
|
|
227
278
|
* never for a used choice. Awaited before any later chooseAccount for the
|
|
228
279
|
* same session; errors are logged and swallowed. A model/agent change
|
|
@@ -264,6 +315,21 @@ export interface OpenCode2AuthAdapter<Q = unknown, A = unknown, Choice extends C
|
|
|
264
315
|
readonly response: Response;
|
|
265
316
|
readonly attempt: Attempt<A>;
|
|
266
317
|
}): Promise<Response | undefined> | Response | undefined;
|
|
318
|
+
/**
|
|
319
|
+
* Optional retry policy for the final HTTP response. Runs immediately after
|
|
320
|
+
* `rewriteResponse` (or on the unchanged response when no rewrite is returned).
|
|
321
|
+
* Return `veto` to forbid host replay of a refusal the adapter formatted,
|
|
322
|
+
* even without a recognised limit. A veto is sticky for this attempt.
|
|
323
|
+
* Ownership and attribution checks still run first: a veto cannot affect a
|
|
324
|
+
* foreign request, a session with mixed ownership, or a retry whose attempt is
|
|
325
|
+
* ambiguous. `leave` or `undefined` preserves the existing retry policy,
|
|
326
|
+
* including an earlier veto.
|
|
327
|
+
*/
|
|
328
|
+
responseRetry?(input: AccountRequest & {
|
|
329
|
+
readonly request: Request;
|
|
330
|
+
readonly response: Response;
|
|
331
|
+
readonly attempt: Attempt<A>;
|
|
332
|
+
}): Promise<'leave' | 'veto' | undefined> | 'leave' | 'veto' | undefined;
|
|
267
333
|
/**
|
|
268
334
|
* Optional. For an adapter whose own sender can move a request to another
|
|
269
335
|
* account after `rewriteRequest` (a bridge that answers a 401 or 403 by
|
|
@@ -435,6 +501,24 @@ export interface InstallOpenCode2AuthOptions {
|
|
|
435
501
|
readonly hostCredentials?: readonly string[];
|
|
436
502
|
/** Most attempts kept before the oldest is abandoned and dropped. */
|
|
437
503
|
readonly maxRecords?: number;
|
|
504
|
+
/**
|
|
505
|
+
* Host retry budget, default 10. When hostAttempt leaves no retry available,
|
|
506
|
+
* limit holds and reroutes keep the host's decision and release their choice.
|
|
507
|
+
*/
|
|
508
|
+
readonly hostRetryLimit?: number;
|
|
509
|
+
/**
|
|
510
|
+
* Reject chooser results naming a request-excluded account (default false).
|
|
511
|
+
* A rejected choice warns and acts like undefined: the host decides in retry,
|
|
512
|
+
* and request selection stops with no-account. Without this option, chooser
|
|
513
|
+
* results retain their existing meaning.
|
|
514
|
+
*
|
|
515
|
+
* The host supplies no logical request id. The first later attempt of the
|
|
516
|
+
* same session and kind consumes a queued retry's exclusions. If another
|
|
517
|
+
* attempt is in flight, that binding warns but keeps the exclusions. A new
|
|
518
|
+
* concurrent request can therefore inherit them; if it consumes the retry
|
|
519
|
+
* first, the actual retry instead starts a fresh token without them.
|
|
520
|
+
*/
|
|
521
|
+
readonly enforceRequestExclusion?: boolean;
|
|
438
522
|
readonly logger?: OpenCode2AuthLogger;
|
|
439
523
|
}
|
|
440
524
|
/**
|
|
@@ -449,13 +533,15 @@ export interface InstallOpenCode2AuthOptions {
|
|
|
449
533
|
* (its wait, or its refusal to retry) stands and the next attempt may go
|
|
450
534
|
* to the refused account again;
|
|
451
535
|
* - `hold`: an explicit adapter wait on the refused account, before output;
|
|
536
|
+
* - `wait-then-reselect`: wait without reserving the next account;
|
|
537
|
+
* - `adapter-veto`: the attributed adapter disposition forbids host replay;
|
|
452
538
|
* - `same-account`: the adapter requested immediate recovery on the same route,
|
|
453
539
|
* without account selection, before output;
|
|
454
540
|
* - `host-decides`: no limit was recognised, or the failing call cannot be
|
|
455
541
|
* attributed safely (ambiguous attempts or mixed ownership in a gated
|
|
456
542
|
* session), so the host's decision stands.
|
|
457
543
|
*/
|
|
458
|
-
export type RetryReason = 'reroute' | 'hold' | 'same-account' | 'output-started' | 'no-account' | 'host-decides-last-account' | 'host-decides';
|
|
544
|
+
export type RetryReason = 'reroute' | 'hold' | 'wait-then-reselect' | 'adapter-veto' | 'same-account' | 'output-started' | 'no-account' | 'host-decides-last-account' | 'host-decides';
|
|
459
545
|
/**
|
|
460
546
|
* The hook that picked an account. Normally `model.request`; a transport hook
|
|
461
547
|
* picks only when the host skipped `model.request` for that request, which a
|
package/dist/quota/format.d.ts
CHANGED
|
@@ -30,6 +30,8 @@ export interface QuotaTextOptions {
|
|
|
30
30
|
}
|
|
31
31
|
export interface QuotaWindowPart {
|
|
32
32
|
kind: 'window' | 'budget';
|
|
33
|
+
/** Unknown windows have no dated percentage; budgets retain their own wording. */
|
|
34
|
+
reading: 'known' | 'unknown';
|
|
33
35
|
/** The display name of a quota window, or `credits` for the budget. */
|
|
34
36
|
name: string;
|
|
35
37
|
/**
|
|
@@ -45,7 +47,8 @@ export interface QuotaWindowPart {
|
|
|
45
47
|
remainingPercent?: number;
|
|
46
48
|
/** A bare relative reset span such as `4d`, only when the reset is future. */
|
|
47
49
|
resetIn?: string;
|
|
48
|
-
|
|
50
|
+
/** The reading time, omitted for a window with no reading. */
|
|
51
|
+
checkedAt?: number;
|
|
49
52
|
/** Whether the reset followed this reading and has arrived by `now`. */
|
|
50
53
|
resetSinceReading: boolean;
|
|
51
54
|
}
|
|
@@ -64,9 +67,11 @@ export interface QuotaParts {
|
|
|
64
67
|
*/
|
|
65
68
|
export declare function quotaWindowName(limit: Pick<ProjectedLimit, 'scope' | 'label' | 'windowMinutes'>): string;
|
|
66
69
|
/**
|
|
67
|
-
* Structured quota wording for layouts that need one row per window.
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
+
* Structured quota wording for layouts that need one row per window.
|
|
71
|
+
* Windows without a dated percentage remain as explicit unknown parts. Limits
|
|
72
|
+
* marked absent (not reported) or retired (no longer tracked) remain omitted.
|
|
73
|
+
* Reported windows, future resets and the optional age keep the shared
|
|
74
|
+
* formatter's wording and order.
|
|
70
75
|
*/
|
|
71
76
|
export declare function quotaWindowParts(quota: ProjectedQuota | undefined, options: QuotaTextOptions): QuotaParts;
|
|
72
77
|
/**
|
package/dist/quota/format.js
CHANGED
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
//
|
|
6
6
|
// The rules: a window is named by its length (`5h`, `7d`), or, for a window
|
|
7
7
|
// that binds only one model family, by the label the plugin stored it under.
|
|
8
|
-
// Every figure is "% left". A window the provider did not
|
|
9
|
-
// record or a retirement tombstone) is left out rather than
|
|
10
|
-
// reported". A reset is a short time from now (`resets 2h`).
|
|
11
|
-
// than fifteen minutes says how old it is (`checked 3h ago`).
|
|
8
|
+
// Every reported window figure is "% left". A window the provider did not
|
|
9
|
+
// report (an absence record or a retirement tombstone) is left out rather than
|
|
10
|
+
// written as "not reported". A reset is a short time from now (`resets 2h`).
|
|
11
|
+
// A reading older than fifteen minutes says how old it is (`checked 3h ago`).
|
|
12
12
|
//
|
|
13
13
|
// Like the rest of this module it reads no clock: `now` is a parameter.
|
|
14
14
|
import { ALL_SCOPE } from './map.js';
|
|
@@ -97,12 +97,14 @@ function displayOrder(left, right) {
|
|
|
97
97
|
return left.label < right.label ? -1 : left.label > right.label ? 1 : 0;
|
|
98
98
|
}
|
|
99
99
|
/**
|
|
100
|
-
* Structured quota wording for layouts that need one row per window.
|
|
101
|
-
*
|
|
102
|
-
*
|
|
100
|
+
* Structured quota wording for layouts that need one row per window.
|
|
101
|
+
* Windows without a dated percentage remain as explicit unknown parts. Limits
|
|
102
|
+
* marked absent (not reported) or retired (no longer tracked) remain omitted.
|
|
103
|
+
* Reported windows, future resets and the optional age keep the shared
|
|
104
|
+
* formatter's wording and order.
|
|
103
105
|
*/
|
|
104
106
|
export function quotaWindowParts(quota, options) {
|
|
105
|
-
const { windows, age } = scopedWindowParts(quota, options);
|
|
107
|
+
const { windows, age } = scopedWindowParts(quota, options, true);
|
|
106
108
|
const parts = windows.map(({ part }) => part);
|
|
107
109
|
return age === undefined ? { windows: parts } : { windows: parts, age };
|
|
108
110
|
}
|
|
@@ -125,34 +127,47 @@ function withShortNames(windows) {
|
|
|
125
127
|
}
|
|
126
128
|
return windows;
|
|
127
129
|
}
|
|
130
|
+
function hasWindowReading(limit) {
|
|
131
|
+
return (Number.isFinite(limit.remainingPercent) && Number.isFinite(limit.checkedAt));
|
|
132
|
+
}
|
|
128
133
|
/**
|
|
129
134
|
* `quotaWindowParts` with each window's scope kept beside it. The narrow form
|
|
130
135
|
* needs the scope to name and pick model-scoped windows; the public parts do
|
|
131
|
-
* not carry
|
|
136
|
+
* not carry the scope.
|
|
132
137
|
*/
|
|
133
|
-
function scopedWindowParts(quota, options) {
|
|
138
|
+
function scopedWindowParts(quota, options, includeUnknown = false) {
|
|
134
139
|
if (!quota)
|
|
135
140
|
return { windows: [] };
|
|
136
141
|
const { now } = options;
|
|
137
142
|
const readings = quota.limits
|
|
138
|
-
.filter((limit) => limit.kind === 'reading' &&
|
|
143
|
+
.filter((limit) => limit.kind === 'reading' && (includeUnknown || hasWindowReading(limit)))
|
|
139
144
|
.sort(displayOrder);
|
|
140
145
|
const windows = readings.map((limit) => {
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
scope: limit.scope,
|
|
144
|
-
...(limit.windowMinutes === undefined
|
|
145
|
-
? {}
|
|
146
|
-
: { windowMinutes: limit.windowMinutes }),
|
|
147
|
-
part: {
|
|
146
|
+
const part = hasWindowReading(limit)
|
|
147
|
+
? {
|
|
148
148
|
kind: 'window',
|
|
149
|
+
reading: 'known',
|
|
149
150
|
name: quotaWindowName(limit),
|
|
150
151
|
short: '',
|
|
151
|
-
figure: percentLeft(limit.remainingPercent
|
|
152
|
-
remainingPercent,
|
|
152
|
+
figure: percentLeft(limit.remainingPercent),
|
|
153
|
+
remainingPercent: clampPercent(limit.remainingPercent),
|
|
153
154
|
checkedAt: limit.checkedAt,
|
|
154
155
|
...resetDetails(limit.resetsAt, limit.checkedAt, now),
|
|
155
|
-
}
|
|
156
|
+
}
|
|
157
|
+
: {
|
|
158
|
+
kind: 'window',
|
|
159
|
+
reading: 'unknown',
|
|
160
|
+
name: quotaWindowName(limit),
|
|
161
|
+
short: '',
|
|
162
|
+
figure: 'no reading',
|
|
163
|
+
resetSinceReading: false,
|
|
164
|
+
};
|
|
165
|
+
return {
|
|
166
|
+
scope: limit.scope,
|
|
167
|
+
...(limit.windowMinutes === undefined
|
|
168
|
+
? {}
|
|
169
|
+
: { windowMinutes: limit.windowMinutes }),
|
|
170
|
+
part,
|
|
156
171
|
};
|
|
157
172
|
});
|
|
158
173
|
const budget = quota.budget;
|
|
@@ -168,6 +183,7 @@ function scopedWindowParts(quota, options) {
|
|
|
168
183
|
spent: budget.reached,
|
|
169
184
|
part: {
|
|
170
185
|
kind: 'budget',
|
|
186
|
+
reading: 'known',
|
|
171
187
|
name: 'credits',
|
|
172
188
|
short: '',
|
|
173
189
|
figure,
|
|
@@ -180,8 +196,11 @@ function scopedWindowParts(quota, options) {
|
|
|
180
196
|
});
|
|
181
197
|
}
|
|
182
198
|
withShortNames(windows);
|
|
183
|
-
|
|
184
|
-
|
|
199
|
+
const readingTimes = windows.flatMap(({ part }) => part.reading === 'known' && part.checkedAt !== undefined
|
|
200
|
+
? [part.checkedAt]
|
|
201
|
+
: []);
|
|
202
|
+
if (readingTimes.length > 0) {
|
|
203
|
+
const age = now - Math.min(...readingTimes);
|
|
185
204
|
if (age > (options.staleAfterMs ?? QUOTA_STALE_AFTER_MS)) {
|
|
186
205
|
return { windows, age: `checked ${shortSpan(age)} ago` };
|
|
187
206
|
}
|
|
@@ -357,13 +376,13 @@ function narrowTextParts(quota, options) {
|
|
|
357
376
|
export function quotaTextParts(quota, options) {
|
|
358
377
|
if (options.form === 'narrow')
|
|
359
378
|
return narrowTextParts(quota, options);
|
|
360
|
-
const
|
|
379
|
+
const { windows, age } = scopedWindowParts(quota, options);
|
|
361
380
|
const full = (options.form ?? 'full') === 'full';
|
|
362
|
-
const text =
|
|
381
|
+
const text = windows.map(({ part }) => {
|
|
363
382
|
const reset = full && part.resetIn ? `, resets ${part.resetIn}` : '';
|
|
364
383
|
return `${part.kind === 'budget' ? '' : `${part.name} `}${part.figure}${reset}`;
|
|
365
384
|
});
|
|
366
|
-
return
|
|
385
|
+
return age === undefined ? text : [...text, age];
|
|
367
386
|
}
|
|
368
387
|
/**
|
|
369
388
|
* A row's quota as one line: `5h 58% left · 7d 90% left` in the compact
|
|
@@ -106,11 +106,14 @@ function QuotaRow(props) {
|
|
|
106
106
|
const label = () => props.part.name +
|
|
107
107
|
' '.repeat(Math.max(0, props.labelWidth - displayWidth(props.part.name)));
|
|
108
108
|
const figure = () => ` ${props.part.kind === 'budget' && props.part.figure.startsWith(`${props.part.name} `) ? props.part.figure.slice(props.part.name.length + 1) : props.part.figure}`;
|
|
109
|
+
const hasReading = () => props.part.reading === 'known';
|
|
109
110
|
const used = () => 100 - (props.part.remainingPercent ?? 100);
|
|
110
111
|
// Size notifications can recompute an unchanged fit after text has been flushed
|
|
111
112
|
// for the frame. Keep equal segments, and update fixed slots in place when the
|
|
112
113
|
// bar changes, rather than replacing text nodes during layout.
|
|
113
|
-
const segments = createMemo(() =>
|
|
114
|
+
const segments = createMemo(() => hasReading()
|
|
115
|
+
? barSegments(used(), { ...context.appearance, barWidth: props.barCells }, props.pace)
|
|
116
|
+
: [], undefined, {
|
|
114
117
|
equals: (previous, next) => previous.length === next.length &&
|
|
115
118
|
previous.every((segment, index) => segment.text === next[index]?.text &&
|
|
116
119
|
segment.tone === next[index]?.tone),
|
|
@@ -131,23 +134,27 @@ function QuotaRow(props) {
|
|
|
131
134
|
{label()}
|
|
132
135
|
</text>
|
|
133
136
|
</Show>
|
|
134
|
-
<Show when={
|
|
135
|
-
|
|
137
|
+
<Show when={hasReading()} fallback={<text fg={context.theme.textMuted} wrapMode='none'>
|
|
138
|
+
no reading
|
|
136
139
|
</text>}>
|
|
137
|
-
<Show when={props.
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
140
|
+
<Show when={!props.part.resetSinceReading} fallback={<text fg={context.theme.textMuted} wrapMode='none'>
|
|
141
|
+
reset since last check
|
|
142
|
+
</text>}>
|
|
143
|
+
<Show when={props.barCells > 0}>
|
|
144
|
+
{/* Text nodes clear omitted theme colours; empty slots must not reserve a cell. */}
|
|
145
|
+
<Index each={segments()}>
|
|
146
|
+
{(segment) => (<text fg={context.theme[segment().tone]} wrapMode='none' width={segment().text ? undefined : 0}>
|
|
147
|
+
{segment().text}
|
|
148
|
+
</text>)}
|
|
149
|
+
</Index>
|
|
150
|
+
</Show>
|
|
151
|
+
<text fg={context.theme[usageTone(used(), context.appearance)]} wrapMode='none'>
|
|
152
|
+
{figure()}
|
|
153
|
+
</text>
|
|
144
154
|
</Show>
|
|
145
|
-
<text fg={context.theme[usageTone(used(), context.appearance)]} wrapMode='none'>
|
|
146
|
-
{figure()}
|
|
147
|
-
</text>
|
|
148
155
|
</Show>
|
|
149
156
|
</box>
|
|
150
|
-
<Show when={props.showReset && !props.part.resetSinceReading}>
|
|
157
|
+
<Show when={hasReading() && props.showReset && !props.part.resetSinceReading}>
|
|
151
158
|
<box width={props.resetWidth} flexShrink={0} flexDirection='row' justifyContent='flex-end'>
|
|
152
159
|
<Show when={props.part.resetIn}>
|
|
153
160
|
<text fg={context.theme.textMuted} wrapMode='none'>
|
|
@@ -158,7 +165,7 @@ function QuotaRow(props) {
|
|
|
158
165
|
</Show>
|
|
159
166
|
</box>
|
|
160
167
|
{/* A changed fitted line needs a fresh text node to draw at its new width. */}
|
|
161
|
-
<Show when={!props.part.resetSinceReading && paceLine()} keyed>
|
|
168
|
+
<Show when={hasReading() && !props.part.resetSinceReading && paceLine()} keyed>
|
|
162
169
|
{(line) => (<box width='100%' flexDirection='row'>
|
|
163
170
|
<Show when={props.labelWidth > 0}>
|
|
164
171
|
<text fg={context.theme.textMuted}>
|
|
@@ -176,8 +183,7 @@ function CollapsedAccountRow(props) {
|
|
|
176
183
|
const context = frameContext();
|
|
177
184
|
const [width, setWidth] = createSignal(0);
|
|
178
185
|
let row;
|
|
179
|
-
const hasQuota = () => quotaWindowParts(props.account.quota, { now: context.now }).windows.
|
|
180
|
-
0;
|
|
186
|
+
const hasQuota = () => quotaWindowParts(props.account.quota, { now: context.now }).windows.some((part) => part.reading === 'known');
|
|
181
187
|
const text = () => hasQuota()
|
|
182
188
|
? formatQuota(props.account.quota, { now: context.now, form: 'narrow' })
|
|
183
189
|
: (props.account.emptyQuotaText?.collapsed ?? '\u2014');
|
|
@@ -219,9 +225,11 @@ function AccountBlock(props) {
|
|
|
219
225
|
let block;
|
|
220
226
|
const rows = () => props.account.groups.flatMap((group) => group.rows);
|
|
221
227
|
const labelWidth = () => Math.max(0, ...rows().map(({ part }) => part.name ? Math.max(3, displayWidth(part.name) + 1) : 0));
|
|
222
|
-
const resetWidth = () => Math.max(0, ...rows().map(({ part }) => part.
|
|
228
|
+
const resetWidth = () => Math.max(0, ...rows().map(({ part }) => part.reading === 'unknown' || part.resetSinceReading
|
|
229
|
+
? 0
|
|
230
|
+
: displayWidth(part.resetIn ?? '')));
|
|
223
231
|
const figureWidth = createMemo(() => Math.max(10, ...rows()
|
|
224
|
-
.filter(({ part }) => !part.resetSinceReading)
|
|
232
|
+
.filter(({ part }) => part.reading === 'known' && !part.resetSinceReading)
|
|
225
233
|
.map(({ part }) => displayWidth(` ${part.kind === 'budget' && part.figure.startsWith(`${part.name} `) ? part.figure.slice(part.name.length + 1) : part.figure}`))));
|
|
226
234
|
const fit = (rowLabelWidth) => {
|
|
227
235
|
const desired = context.appearance.barWidth;
|
|
@@ -23,11 +23,16 @@ export interface AdmissionInput extends ExclusionInputs {
|
|
|
23
23
|
scope?: string;
|
|
24
24
|
/** Labels the scope requires an entry for; defaults to `['primary']`. */
|
|
25
25
|
requiredLabels?: readonly string[];
|
|
26
|
+
/**
|
|
27
|
+
* One-dispatch exemptions keyed by row; waive only gate 4. The caller first
|
|
28
|
+
* validates the current account incarnation and checkpointed reset times.
|
|
29
|
+
*/
|
|
30
|
+
liftedWindows?: ReadonlyMap<string, readonly WindowRef[]>;
|
|
26
31
|
now: number;
|
|
27
32
|
/**
|
|
28
|
-
* Called synchronously
|
|
33
|
+
* Called synchronously once per row needing a pull, including lifted windows.
|
|
29
34
|
* Its return value is ignored and never awaited, so a pull the caller
|
|
30
|
-
* starts from here cannot delay
|
|
35
|
+
* starts from here cannot delay admission or a refusal.
|
|
31
36
|
*/
|
|
32
37
|
requestPull?: (rowId: string) => void;
|
|
33
38
|
}
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
// 3. a required label with no entry is unknown: refused, pull requested;
|
|
15
15
|
// 4. a reading at or beyond 100% whose reset is missing, unparsable or not
|
|
16
16
|
// after `now` cannot confirm the exhaustion, so the window is unknown:
|
|
17
|
-
// refused, pull requested;
|
|
17
|
+
// refused unless lifted for this wake dispatch; either way, pull requested;
|
|
18
18
|
// 5. a reading at or beyond 100% with a future reset is exhausted: refused.
|
|
19
19
|
// Gates 4 and 5 look at every projected limit, not only the required labels.
|
|
20
20
|
// Stage 2 judges the credit budget across the stage-1 survivors: a row whose
|
|
@@ -36,7 +36,7 @@ export function exclusionFor(id, inputs, now) {
|
|
|
36
36
|
}
|
|
37
37
|
return undefined;
|
|
38
38
|
}
|
|
39
|
-
function judgeStageOne(id, projection, requiredLabels, now) {
|
|
39
|
+
function judgeStageOne(id, projection, requiredLabels, now, liftedWindows) {
|
|
40
40
|
if (projection.limits.length === 0) {
|
|
41
41
|
return {
|
|
42
42
|
id,
|
|
@@ -60,7 +60,8 @@ function judgeStageOne(id, projection, requiredLabels, now) {
|
|
|
60
60
|
}
|
|
61
61
|
const exhausted = projection.limits.filter(readsExhausted);
|
|
62
62
|
for (const limit of exhausted) {
|
|
63
|
-
if (futureResetAt(limit, now) === undefined
|
|
63
|
+
if (futureResetAt(limit, now) === undefined &&
|
|
64
|
+
!liftedWindows.some((window) => window.scope === limit.scope && window.label === limit.label)) {
|
|
64
65
|
return {
|
|
65
66
|
id,
|
|
66
67
|
stage: 1,
|
|
@@ -94,6 +95,7 @@ export function admit(input) {
|
|
|
94
95
|
const excluded = [];
|
|
95
96
|
const refused = [];
|
|
96
97
|
const survivors = [];
|
|
98
|
+
const needsPull = new Set();
|
|
97
99
|
for (const row of input.rows) {
|
|
98
100
|
const exclusion = exclusionFor(row.id, input, input.now);
|
|
99
101
|
if (exclusion) {
|
|
@@ -105,7 +107,12 @@ export function admit(input) {
|
|
|
105
107
|
continue;
|
|
106
108
|
}
|
|
107
109
|
const projection = projectQuota(row.quota, scope);
|
|
108
|
-
const
|
|
110
|
+
const lifted = input.liftedWindows?.get(row.id) ?? [];
|
|
111
|
+
if (projection.limits.some((limit) => readsExhausted(limit) &&
|
|
112
|
+
futureResetAt(limit, input.now) === undefined &&
|
|
113
|
+
lifted.some((window) => window.scope === limit.scope && window.label === limit.label)))
|
|
114
|
+
needsPull.add(row.id);
|
|
115
|
+
const refusal = judgeStageOne(row.id, projection, requiredLabels, input.now, lifted);
|
|
109
116
|
if (refusal) {
|
|
110
117
|
refused.push(refusal);
|
|
111
118
|
continue;
|
|
@@ -133,9 +140,12 @@ export function admit(input) {
|
|
|
133
140
|
const order = new Map(input.rows.map((row, index) => [row.id, index]));
|
|
134
141
|
const byInput = (left, right) => (order.get(left.id) ?? 0) - (order.get(right.id) ?? 0);
|
|
135
142
|
refused.sort(byInput);
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
143
|
+
for (const refusal of refused)
|
|
144
|
+
if ('pullRequested' in refusal)
|
|
145
|
+
needsPull.add(refusal.id);
|
|
146
|
+
const pulls = input.rows
|
|
147
|
+
.filter((row) => needsPull.has(row.id))
|
|
148
|
+
.map((row) => row.id);
|
|
139
149
|
for (const id of pulls)
|
|
140
150
|
input.requestPull?.(id);
|
|
141
151
|
return { admitted, refused, excluded, pulls };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function routingHandle(sessionId: string): Record<string, unknown> | undefined;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function routingHandle(sessionId) {
|
|
2
|
+
// The bridge can load after the auth plugin, so never cache the accessor.
|
|
3
|
+
const routing = Reflect.get(globalThis, Symbol.for('cortexkit.routing'));
|
|
4
|
+
if (typeof routing !== 'function')
|
|
5
|
+
return undefined;
|
|
6
|
+
const handle = routing(sessionId);
|
|
7
|
+
return typeof handle === 'object' && handle !== null
|
|
8
|
+
? handle
|
|
9
|
+
: undefined;
|
|
10
|
+
}
|
|
@@ -12,6 +12,14 @@ export interface AskRoutingInput<Candidate extends RoutingCandidate = RoutingCan
|
|
|
12
12
|
candidates: readonly Candidate[];
|
|
13
13
|
/** Defaults to 500 ms, clamped to 1–2000 ms. */
|
|
14
14
|
deadlineMs?: number;
|
|
15
|
+
/**
|
|
16
|
+
* Longest same-account hold, in seconds, that routing may advise. The
|
|
17
|
+
* holdWaitMaxS helper from `./limits` caps it by the setting and the current
|
|
18
|
+
* account's cache expiry, returning 0 when there is no accepted prompt cache
|
|
19
|
+
* worth waiting for. Fractions are floored and negatives become 0. Omit it to leave
|
|
20
|
+
* routing's default cap in force.
|
|
21
|
+
*/
|
|
22
|
+
holdWaitMaxS?: number;
|
|
15
23
|
}
|
|
16
24
|
export interface RoutingHold {
|
|
17
25
|
/** Uncapped advice from routing. The plugin must apply its own hold cap. */
|
|
@@ -36,8 +44,16 @@ export type ReportRoutingInput = RoutingReportIdentity & ({
|
|
|
36
44
|
headers: Readonly<Record<string, string>>;
|
|
37
45
|
} | {
|
|
38
46
|
outcome: 'exhausted';
|
|
39
|
-
/**
|
|
40
|
-
window:
|
|
47
|
+
/** The exhausted quota window; keep scoped quota groups in `pool`. */
|
|
48
|
+
window: '5h' | '7d';
|
|
49
|
+
/**
|
|
50
|
+
* Scoped quota group, such as Antigravity's `3p` or `gemini`.
|
|
51
|
+
* Must match `^[a-z0-9._-]{1,64}$`. Routing compares names exactly, so
|
|
52
|
+
* callers must lowercase names from elsewhere (e.g. `Fable` to `fable`).
|
|
53
|
+
* Names are never rewritten here. Omit or pass `undefined` to block the
|
|
54
|
+
* whole account; include to block only this pool.
|
|
55
|
+
*/
|
|
56
|
+
pool?: string;
|
|
41
57
|
/** Optional reset time in epoch milliseconds. */
|
|
42
58
|
resetsAt?: number;
|
|
43
59
|
});
|
|
@@ -1,16 +1,9 @@
|
|
|
1
1
|
// Optional THALAMUS routing for vault mode only. Account ids here are vault
|
|
2
2
|
// account ids; local pool rows are not known to the routing engine.
|
|
3
|
+
import { routingHandle } from './handle.js';
|
|
3
4
|
function isObject(value) {
|
|
4
5
|
return typeof value === 'object' && value !== null;
|
|
5
6
|
}
|
|
6
|
-
function routingHandle(sessionId) {
|
|
7
|
-
// The bridge can load after the auth plugin, so never cache the accessor.
|
|
8
|
-
const routing = Reflect.get(globalThis, Symbol.for('cortexkit.routing'));
|
|
9
|
-
if (typeof routing !== 'function')
|
|
10
|
-
return undefined;
|
|
11
|
-
const handle = routing(sessionId);
|
|
12
|
-
return isObject(handle) ? handle : undefined;
|
|
13
|
-
}
|
|
14
7
|
function selectionFor(answer, candidates) {
|
|
15
8
|
if (!isObject(answer))
|
|
16
9
|
return undefined;
|
|
@@ -56,7 +49,15 @@ export async function askRouting(input) {
|
|
|
56
49
|
timer = setTimeout(() => resolve(undefined), deadlineMs);
|
|
57
50
|
timer.unref();
|
|
58
51
|
});
|
|
59
|
-
const
|
|
52
|
+
const request = {
|
|
53
|
+
provider: input.provider,
|
|
54
|
+
modelId: input.modelId,
|
|
55
|
+
};
|
|
56
|
+
const cap = input.holdWaitMaxS;
|
|
57
|
+
if (typeof cap === 'number' && Number.isFinite(cap)) {
|
|
58
|
+
request.hold_wait_max_s = Math.max(0, Math.floor(cap));
|
|
59
|
+
}
|
|
60
|
+
const pending = accountFor.call(handle, request, { deadlineMs });
|
|
60
61
|
if (!isObject(pending) || typeof pending.then !== 'function')
|
|
61
62
|
return undefined;
|
|
62
63
|
const answer = await Promise.race([pending, expired]);
|
|
@@ -94,16 +95,25 @@ export function reportRouting(input) {
|
|
|
94
95
|
case 'served':
|
|
95
96
|
payload = { ...identity, outcome: 'served', headers: input.headers };
|
|
96
97
|
break;
|
|
97
|
-
case 'exhausted':
|
|
98
|
+
case 'exhausted': {
|
|
99
|
+
const { window, pool } = input;
|
|
100
|
+
if (window !== '5h' && window !== '7d')
|
|
101
|
+
return;
|
|
102
|
+
// An undefined pool means the whole account, as an omitted one does.
|
|
103
|
+
if (pool !== undefined &&
|
|
104
|
+
(typeof pool !== 'string' || !/^[a-z0-9._-]{1,64}$/.test(pool)))
|
|
105
|
+
return;
|
|
98
106
|
payload = {
|
|
99
107
|
...identity,
|
|
100
108
|
outcome: 'exhausted',
|
|
101
|
-
window
|
|
109
|
+
window,
|
|
110
|
+
...(pool ? { pool } : {}),
|
|
102
111
|
...(input.resetsAt !== undefined
|
|
103
112
|
? { resets_at: input.resetsAt }
|
|
104
113
|
: {}),
|
|
105
114
|
};
|
|
106
115
|
break;
|
|
116
|
+
}
|
|
107
117
|
default:
|
|
108
118
|
return;
|
|
109
119
|
}
|