@cortexkit/common-auth 0.46.0 → 0.47.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.
@@ -12,14 +12,16 @@ export interface RoutingCandidate {
12
12
  accountId: string;
13
13
  credentialId: string;
14
14
  }
15
- export interface AskRoutingInput<Candidate extends RoutingCandidate = RoutingCandidate> {
15
+ export interface AskRoutingInput<Candidate extends RoutingCandidate = RoutingCandidate, Refusals extends boolean = false> {
16
16
  sessionId: string;
17
17
  provider: string;
18
18
  modelId: string;
19
19
  /** Only routes already eligible under the plugin's vault and killswitch rules. */
20
20
  candidates: readonly Candidate[];
21
- /** Defaults to 500 ms, clamped to 1–2000 ms. */
21
+ /** Defaults to 1500 ms, above routing's normal tail; clamped to 1–2000 ms. */
22
22
  deadlineMs?: number;
23
+ /** Opt in to refusals, which stop the request instead of using a local choice. */
24
+ refusals?: Refusals;
23
25
  /**
24
26
  * Longest same-account hold, in seconds, that routing may advise. The
25
27
  * holdWaitMaxS helper from `./limits` caps it by the setting and the current
@@ -38,6 +40,14 @@ export interface RoutingSelection<Candidate extends RoutingCandidate = RoutingCa
38
40
  candidate: Candidate;
39
41
  hold?: RoutingHold;
40
42
  }
43
+ export type RoutingRefusal = {
44
+ refused: {
45
+ code: 'account_limit_exhausted' | 'account_selection_refused' | 'account_selection_final';
46
+ routingCode?: string;
47
+ retryAfterSecs?: number;
48
+ resetsAtMs?: number;
49
+ };
50
+ };
41
51
  interface RoutingReportIdentity {
42
52
  sessionId: string;
43
53
  accountId: string;
@@ -145,10 +155,11 @@ export declare function turnNotes(sessionId: string): readonly {
145
155
  }[];
146
156
  /**
147
157
  * Ask routing for an eligible vault route. Any failure or unfamiliar answer
148
- * yields undefined so the plugin can keep its own choice. This does not admit
149
- * routes: callers must exclude vault refusals and killswitch-blocked accounts.
158
+ * yields undefined so the plugin can keep its own choice. Opted-in routing
159
+ * refusals instead stop the request. This does not admit routes: callers must
160
+ * exclude vault refusals and killswitch-blocked accounts.
150
161
  */
151
- export declare function askRouting<Candidate extends RoutingCandidate>(input: AskRoutingInput<Candidate>): Promise<RoutingSelection<Candidate> | undefined>;
162
+ export declare function askRouting<Candidate extends RoutingCandidate, Refusals extends boolean = false>(input: AskRoutingInput<Candidate, Refusals>): Promise<RoutingSelection<Candidate> | (Refusals extends true ? RoutingRefusal : never) | undefined>;
152
163
  /**
153
164
  * Report a vault request, billing result or turn-note event without waiting for
154
165
  * routing. Only contract fields are sent, and all accessor failures are ignored.
@@ -136,11 +136,35 @@ function selectionFor(answer, candidates) {
136
136
  }
137
137
  return { candidate };
138
138
  }
139
- /**
140
- * Ask routing for an eligible vault route. Any failure or unfamiliar answer
141
- * yields undefined so the plugin can keep its own choice. This does not admit
142
- * routes: callers must exclude vault refusals and killswitch-blocked accounts.
143
- */
139
+ function refusalFor(value) {
140
+ if (!isObject(value))
141
+ return undefined;
142
+ const code = value.code;
143
+ if (code !== 'account_limit_exhausted' &&
144
+ code !== 'account_selection_refused' &&
145
+ code !== 'account_selection_final') {
146
+ return undefined;
147
+ }
148
+ const refused = { code };
149
+ const routingCode = value.routing_code;
150
+ if (typeof routingCode === 'string' &&
151
+ /^[a-z0-9._-]{1,64}$/.test(routingCode)) {
152
+ refused.routingCode = routingCode;
153
+ }
154
+ const retryAfterSecs = value.retry_after_secs;
155
+ if (typeof retryAfterSecs === 'number' &&
156
+ Number.isFinite(retryAfterSecs) &&
157
+ retryAfterSecs >= 0) {
158
+ refused.retryAfterSecs = retryAfterSecs;
159
+ }
160
+ const resetsAtMs = value.resets_at_ms;
161
+ if (typeof resetsAtMs === 'number' &&
162
+ Number.isSafeInteger(resetsAtMs) &&
163
+ resetsAtMs > 0) {
164
+ refused.resetsAtMs = resetsAtMs;
165
+ }
166
+ return { refused };
167
+ }
144
168
  export async function askRouting(input) {
145
169
  let timer;
146
170
  try {
@@ -150,9 +174,9 @@ export async function askRouting(input) {
150
174
  const accountFor = handle.accountFor;
151
175
  if (typeof accountFor !== 'function')
152
176
  return undefined;
153
- const requested = input.deadlineMs ?? 500;
177
+ const requested = input.deadlineMs ?? 1500;
154
178
  const deadlineMs = Number.isNaN(requested)
155
- ? 500
179
+ ? 1500
156
180
  : Math.min(2000, Math.max(1, requested));
157
181
  const expired = new Promise((resolve) => {
158
182
  timer = setTimeout(() => resolve(undefined), deadlineMs);
@@ -166,10 +190,15 @@ export async function askRouting(input) {
166
190
  if (typeof cap === 'number' && Number.isFinite(cap)) {
167
191
  request.hold_wait_max_s = Math.max(0, Math.floor(cap));
168
192
  }
193
+ if (input.refusals === true)
194
+ request.refusals = true;
169
195
  const pending = accountFor.call(handle, request, { deadlineMs });
170
196
  if (!isObject(pending) || typeof pending.then !== 'function')
171
197
  return undefined;
172
198
  const answer = await Promise.race([pending, expired]);
199
+ if (input.refusals === true && isObject(answer) && 'refused' in answer) {
200
+ return refusalFor(answer.refused);
201
+ }
173
202
  return selectionFor(answer, input.candidates);
174
203
  }
175
204
  catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/common-auth",
3
- "version": "0.46.0",
3
+ "version": "0.47.0",
4
4
  "description": "Shared code for the CortexKit auth plugins: account pool, quota and routing, commands and auth menu, OpenCode 2 hooks, Claustrum custody, and plumbing (loopback RPC, file locks, logger, sidebar state, TUI preferences and build).",
5
5
  "license": "MIT",
6
6
  "repository": {