@cortexkit/common-auth 0.30.0 → 0.32.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.
@@ -0,0 +1,119 @@
1
+ // Optional THALAMUS routing for vault mode only. Account ids here are vault
2
+ // account ids; local pool rows are not known to the routing engine.
3
+ function isObject(value) {
4
+ return typeof value === 'object' && value !== null;
5
+ }
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
+ function selectionFor(answer, candidates) {
15
+ if (!isObject(answer))
16
+ return undefined;
17
+ const accountId = answer.account_id;
18
+ const credentialId = answer.credential_id;
19
+ if (typeof accountId !== 'string')
20
+ return undefined;
21
+ if (credentialId !== undefined && typeof credentialId !== 'string') {
22
+ return undefined;
23
+ }
24
+ const candidate = candidates.find((route) => route.accountId === accountId &&
25
+ (credentialId === undefined || route.credentialId === credentialId));
26
+ if (!candidate)
27
+ return undefined;
28
+ const hold = answer.hold;
29
+ const retryAfterMs = isObject(hold) ? hold.retry_after_ms : undefined;
30
+ if (typeof retryAfterMs === 'number' &&
31
+ Number.isFinite(retryAfterMs) &&
32
+ retryAfterMs >= 0) {
33
+ return { candidate, hold: { retry_after_ms: retryAfterMs } };
34
+ }
35
+ return { candidate };
36
+ }
37
+ /**
38
+ * Ask routing for an eligible vault route. Any failure or unfamiliar answer
39
+ * yields undefined so the plugin can keep its own choice. This does not admit
40
+ * routes: callers must exclude vault refusals and killswitch-blocked accounts.
41
+ */
42
+ export async function askRouting(input) {
43
+ let timer;
44
+ try {
45
+ const handle = routingHandle(input.sessionId);
46
+ if (!handle)
47
+ return undefined;
48
+ const accountFor = handle.accountFor;
49
+ if (typeof accountFor !== 'function')
50
+ return undefined;
51
+ const requested = input.deadlineMs ?? 500;
52
+ const deadlineMs = Number.isNaN(requested)
53
+ ? 500
54
+ : Math.min(2000, Math.max(1, requested));
55
+ const expired = new Promise((resolve) => {
56
+ timer = setTimeout(() => resolve(undefined), deadlineMs);
57
+ timer.unref();
58
+ });
59
+ const pending = accountFor.call(handle, { provider: input.provider, modelId: input.modelId }, { deadlineMs });
60
+ if (!isObject(pending) || typeof pending.then !== 'function')
61
+ return undefined;
62
+ const answer = await Promise.race([pending, expired]);
63
+ return selectionFor(answer, input.candidates);
64
+ }
65
+ catch {
66
+ // The process-global bridge is optional and must not break authentication.
67
+ return undefined;
68
+ }
69
+ finally {
70
+ clearTimeout(timer);
71
+ }
72
+ }
73
+ /**
74
+ * Report a vault request without waiting for routing. Only contract fields are
75
+ * sent, and all accessor failures are ignored. Headers must already be limited
76
+ * to rate-limit headers by the caller; values are not filtered or redacted here.
77
+ */
78
+ export function reportRouting(input) {
79
+ try {
80
+ const handle = routingHandle(input.sessionId);
81
+ if (!handle)
82
+ return;
83
+ const report = handle.report;
84
+ if (typeof report !== 'function')
85
+ return;
86
+ const identity = {
87
+ account_id: input.accountId,
88
+ ...(input.credentialId !== undefined
89
+ ? { credential_id: input.credentialId }
90
+ : {}),
91
+ };
92
+ let payload;
93
+ switch (input.outcome) {
94
+ case 'served':
95
+ payload = { ...identity, outcome: 'served', headers: input.headers };
96
+ break;
97
+ case 'exhausted':
98
+ payload = {
99
+ ...identity,
100
+ outcome: 'exhausted',
101
+ window: input.window,
102
+ ...(input.resetsAt !== undefined
103
+ ? { resets_at: input.resetsAt }
104
+ : {}),
105
+ };
106
+ break;
107
+ default:
108
+ return;
109
+ }
110
+ // A broken bridge may return a rejected promise despite its void contract.
111
+ const pending = report.call(handle, payload);
112
+ if (isObject(pending) && typeof pending.then === 'function') {
113
+ void Promise.resolve(pending).catch(() => { });
114
+ }
115
+ }
116
+ catch {
117
+ // Reporting must never change the result of an auth request.
118
+ }
119
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/common-auth",
3
- "version": "0.30.0",
3
+ "version": "0.32.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": {
@@ -56,6 +56,10 @@
56
56
  "types": "./dist/tui/index.d.ts",
57
57
  "import": "./dist/tui/index.js"
58
58
  },
59
+ "./quota-sidebar": {
60
+ "types": "./dist/quota-sidebar/index.d.ts",
61
+ "import": "./src/quota-sidebar/index.tsx"
62
+ },
59
63
  "./store": {
60
64
  "types": "./dist/store/index.d.ts",
61
65
  "import": "./dist/store/index.js"
@@ -68,6 +72,10 @@
68
72
  "types": "./dist/routing/index.d.ts",
69
73
  "import": "./dist/routing/index.js"
70
74
  },
75
+ "./routing-accessor": {
76
+ "types": "./dist/routing-accessor/index.d.ts",
77
+ "import": "./dist/routing-accessor/index.js"
78
+ },
71
79
  "./commands": {
72
80
  "types": "./dist/commands/index.d.ts",
73
81
  "import": "./dist/commands/index.js"
@@ -94,7 +102,11 @@
94
102
  }
95
103
  },
96
104
  "files": [
97
- "dist/"
105
+ "dist/",
106
+ "src/quota-sidebar/",
107
+ "src/quota/format.ts",
108
+ "src/quota/map.ts",
109
+ "src/quota/projection.ts"
98
110
  ],
99
111
  "scripts": {
100
112
  "build": "bun scripts/check-local-deps.mjs && bun scripts/check-installed-ranges.mjs && tsc -p tsconfig.build.json && bun scripts/check-private-files-external.ts",
@@ -108,7 +120,8 @@
108
120
  "check:ranges": "bun scripts/check-installed-ranges.mjs",
109
121
  "check:claustrum-floor": "bun scripts/check-claustrum-floor.mjs",
110
122
  "check:insula-peer": "bun scripts/check-insula-peer.mjs",
111
- "check:opentui-0511": "bun scripts/check-opentui-0511.mjs"
123
+ "check:opentui-0511": "bun scripts/check-opentui-0511.mjs",
124
+ "check:quota-sidebar-floor": "bun scripts/check-quota-sidebar-floor.mjs"
112
125
  },
113
126
  "dependencies": {
114
127
  "@cortexkit/private-files": "^0.2.2",
@@ -118,8 +131,8 @@
118
131
  "@cortexkit/claustrum-client": ">=0.6.2",
119
132
  "@cortexkit/subc-client": "^0.16.0 || ^0.22.0",
120
133
  "@opencode/plugin": ">=2.0.21",
121
- "@opentui/core": ">=0.5.11",
122
- "@opentui/solid": ">=0.5.11",
134
+ "@opentui/core": ">=0.4.5",
135
+ "@opentui/solid": ">=0.4.5",
123
136
  "solid-js": "1.9.12"
124
137
  },
125
138
  "peerDependenciesMeta": {