@cortexkit/opencode-openai-auth 0.8.0 → 0.9.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.
Files changed (39) hide show
  1. package/dist/index-301y4qa1.js +2 -0
  2. package/dist/index-338z7171.js +211 -0
  3. package/dist/index-5y4vzdf8.js +2 -0
  4. package/dist/{index-507jh8bv.js → index-9ehhb051.js} +1 -1
  5. package/dist/{index-944gsj1s.js → index-jwn4r3sw.js} +1 -1
  6. package/dist/{index-02h7g5f9.js → index-sdt2d2ra.js} +1 -1
  7. package/dist/{index-3w4vmckx.js → index-w42s0g6m.js} +2 -2
  8. package/dist/index.d.ts +172 -341
  9. package/dist/index.js +61 -61
  10. package/dist/internal-Y7QNVDJT.d.ts +908 -0
  11. package/dist/quota-normalize-3qffywcm.js +1 -0
  12. package/dist/rpc/port-file.js +1 -1
  13. package/dist/rpc/protocol.d.ts +1 -1
  14. package/dist/rpc/rpc-client.d.ts +2 -1
  15. package/dist/rpc/rpc-client.js +1 -1
  16. package/dist/rpc/rpc-dir.js +1 -1
  17. package/dist/sidebar-state-B98sERYt.d.ts +219 -0
  18. package/dist/sidebar-state.d.ts +2 -172
  19. package/dist/sidebar-state.js +1 -1
  20. package/dist/{token-jdvpwk60.js → token-9823y0pc.js} +1 -1
  21. package/dist/token-util-ypgz7rge.js +1 -0
  22. package/dist/tui-preferences.js +1 -1
  23. package/dist/tui.d.ts +27 -5
  24. package/package.json +2 -1
  25. package/src/tui-compiled/raw/core-custody-state.ts +243 -0
  26. package/src/tui-compiled/raw/sidebar-state.ts +140 -11
  27. package/src/tui-compiled/raw/tui-command-dialogs.tsx +89 -39
  28. package/src/tui-compiled/raw/tui.tsx +138 -18
  29. package/src/tui-compiled/runtime/core-custody-state.ts +243 -0
  30. package/src/tui-compiled/runtime/sidebar-state.ts +140 -11
  31. package/src/tui-compiled/runtime/tui-command-dialogs.tsx +70 -33
  32. package/src/tui-compiled/runtime/tui.tsx +241 -143
  33. package/src/tui-compiled/shared/logger.ts +9 -2
  34. package/dist/index-2xppkvkx.js +0 -2
  35. package/dist/index-nshbgkz1.js +0 -198
  36. package/dist/index-xyg86jgt.js +0 -2
  37. package/dist/protocol-Cv9ZmU0p.d.ts +0 -24
  38. package/dist/quota-normalize-wap0rne8.js +0 -1
  39. package/dist/token-util-zzm7paz9.js +0 -1
@@ -6,10 +6,23 @@ export interface QuotaWindow {
6
6
  windowMinutes?: number
7
7
  }
8
8
 
9
+ export interface SpendControlReading {
10
+ limit: number
11
+ used: number
12
+ remaining: number
13
+ usedPercent: number
14
+ remainingPercent: number
15
+ resetsAt?: string
16
+ unit?: string
17
+ source?: string
18
+ reached: boolean
19
+ }
20
+
9
21
  export interface AccountQuota {
10
22
  checkedAt?: number
11
23
  primary?: QuotaWindow
12
24
  secondary?: QuotaWindow
25
+ spendControl?: SpendControlReading
13
26
  resetCreditsAvailable?: number
14
27
  }
15
28
 
@@ -88,6 +101,17 @@ export interface SidebarAccountState {
88
101
  killed: boolean
89
102
  enabled: boolean
90
103
  resetCredits?: number
104
+ custody?: SidebarAccountCustody
105
+ }
106
+
107
+ export type SidebarCustodyState = 'vault' | 'needsLogin' | 'local' | 'inert'
108
+
109
+ export type SidebarCustodyReason = CustodyInertReason | 'corrupt'
110
+
111
+ export interface SidebarAccountCustody {
112
+ state: SidebarCustodyState
113
+ reason?: SidebarCustodyReason
114
+ recordVersion?: number
91
115
  }
92
116
 
93
117
  export interface ActiveRoutingEntry {
@@ -132,6 +156,7 @@ export interface SidebarState {
132
156
  quota: AccountQuota | null
133
157
  /** ChatGPT identity of the main account this quota belongs to. */
134
158
  mainAccountId?: string
159
+ custody?: SidebarAccountCustody
135
160
  killed: boolean
136
161
  quotaBackedOff?: boolean
137
162
  quotaBackoffUntil?: number
@@ -156,6 +181,11 @@ import { chmod, mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises'
156
181
  import { tmpdir } from 'node:os'
157
182
  import { dirname, join } from 'node:path'
158
183
  import { acquireRefreshFileLock } from '../shared/internal.ts'
184
+ import {
185
+ CUSTODY_INERT_REASONS,
186
+ type CustodyInertReason,
187
+ type CustodyVerdict,
188
+ } from './core-custody-state.ts'
159
189
  import { createLogger } from './logger.ts'
160
190
 
161
191
  const logSb = createLogger('sidebar')
@@ -183,6 +213,49 @@ function resetCreditsField(value: unknown): { resetCredits?: number } {
183
213
  return credits !== undefined ? { resetCredits: credits } : {}
184
214
  }
185
215
 
216
+ const CUSTODY_STATES = new Set<SidebarCustodyState>([
217
+ 'vault',
218
+ 'needsLogin',
219
+ 'local',
220
+ 'inert',
221
+ ])
222
+
223
+ const CUSTODY_REASONS = new Set<SidebarCustodyReason>([
224
+ ...CUSTODY_INERT_REASONS,
225
+ 'corrupt',
226
+ ])
227
+
228
+ /**
229
+ * Tolerant reader for the per-fallback `custody` projection. Unknown state
230
+ * or reason values are dropped (NOT replaced with a default — a stale state
231
+ * file with an experimental `vaultHealing` value must not silently render
232
+ * as `local`, it must render as no-projection-at-all). Valid values round-
233
+ * trip byte-identical. The output contains only `state` and `reason`.
234
+ */
235
+ function normalizeSidebarCustody(
236
+ value: unknown,
237
+ ): SidebarAccountCustody | undefined {
238
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
239
+ return undefined
240
+ }
241
+ const c = value as Record<string, unknown>
242
+ if (
243
+ typeof c.state !== 'string' ||
244
+ !CUSTODY_STATES.has(c.state as SidebarCustodyState)
245
+ ) {
246
+ return undefined
247
+ }
248
+ const state = c.state as SidebarCustodyState
249
+ const out: SidebarAccountCustody = { state }
250
+ if (
251
+ typeof c.reason === 'string' &&
252
+ CUSTODY_REASONS.has(c.reason as SidebarCustodyReason)
253
+ ) {
254
+ out.reason = c.reason as SidebarCustodyReason
255
+ }
256
+ return out
257
+ }
258
+
186
259
  function normalizeActiveRouting(value: unknown): ActiveRoutingMap | undefined {
187
260
  if (value === null || typeof value !== 'object' || Array.isArray(value)) {
188
261
  return undefined
@@ -276,6 +349,23 @@ export function getSidebarStateFile(): string {
276
349
  return process.env[STATE_FILE_ENV] || DEFAULT_STATE_FILE
277
350
  }
278
351
 
352
+ export function projectCustodyForSidebar(
353
+ verdict: CustodyVerdict,
354
+ ): SidebarAccountCustody {
355
+ switch (verdict.kind) {
356
+ case 'LOCAL':
357
+ return { state: 'local' }
358
+ case 'VAULT':
359
+ return { state: 'vault' }
360
+ case 'INERT':
361
+ return { state: 'inert', reason: verdict.reason }
362
+ case 'NEEDS_LOGIN':
363
+ return verdict.reason === 'corrupt'
364
+ ? { state: 'needsLogin', reason: 'corrupt' }
365
+ : { state: 'needsLogin' }
366
+ }
367
+ }
368
+
279
369
  export const DEFAULT_SIDEBAR_STATE: SidebarState = {
280
370
  main: { quota: null, killed: false },
281
371
  fallbacks: [],
@@ -315,6 +405,10 @@ export function normalizeSidebarState(raw: unknown): SidebarState {
315
405
  ...(typeof m.mainAccountId === 'string'
316
406
  ? { mainAccountId: m.mainAccountId }
317
407
  : {}),
408
+ ...(() => {
409
+ const custody = normalizeSidebarCustody(m.custody)
410
+ return custody ? { custody } : {}
411
+ })(),
318
412
  // Preserve optional backoff fields if present
319
413
  ...(typeof m.quotaBackedOff === 'boolean'
320
414
  ? { quotaBackedOff: m.quotaBackedOff }
@@ -347,17 +441,21 @@ export function normalizeSidebarState(raw: unknown): SidebarState {
347
441
  !Array.isArray(entry) &&
348
442
  typeof (entry as Record<string, unknown>).id === 'string',
349
443
  )
350
- .map((e) => ({
351
- id: e.id as string,
352
- label: typeof e.label === 'string' ? e.label : undefined,
353
- ...(typeof e.accountId === 'string'
354
- ? { accountId: e.accountId }
355
- : {}),
356
- quota: ('quota' in e ? e.quota : null) as AccountQuota | null,
357
- killed: typeof e.killed === 'boolean' ? e.killed : false,
358
- enabled: typeof e.enabled === 'boolean' ? e.enabled : true,
359
- ...resetCreditsField(e.resetCredits),
360
- }))
444
+ .map((e) => {
445
+ const custody = normalizeSidebarCustody(e.custody)
446
+ return {
447
+ id: e.id as string,
448
+ label: typeof e.label === 'string' ? e.label : undefined,
449
+ ...(typeof e.accountId === 'string'
450
+ ? { accountId: e.accountId }
451
+ : {}),
452
+ quota: ('quota' in e ? e.quota : null) as AccountQuota | null,
453
+ killed: typeof e.killed === 'boolean' ? e.killed : false,
454
+ enabled: typeof e.enabled === 'boolean' ? e.enabled : true,
455
+ ...resetCreditsField(e.resetCredits),
456
+ ...(custody ? { custody } : {}),
457
+ }
458
+ })
361
459
  : []
362
460
 
363
461
  // activeId — string or undefined
@@ -428,6 +526,27 @@ export function isUsableRoutingEntry(
428
526
  )
429
527
  }
430
528
 
529
+ // The credit budget's own exhaustion signal, shared by admission and sticky
530
+ // migration so both agree on what "spent" means. `reached` is the provider's
531
+ // authoritative boolean — the percentage is only a display approximation — and
532
+ // the check fails open on a missing or lapsed reset exactly like a rate-limit
533
+ // window, so a stale or corrupt reading never blocks.
534
+ export function spendControlExhaustedResetAt(
535
+ quota: AccountQuota | null | undefined,
536
+ now = Date.now(),
537
+ ): { resetsAt: string; resetAtMs: number } | undefined {
538
+ const spendControl = quota?.spendControl
539
+ if (
540
+ spendControl?.reached !== true ||
541
+ typeof spendControl.resetsAt !== 'string'
542
+ ) {
543
+ return undefined
544
+ }
545
+ const resetAtMs = Date.parse(spendControl.resetsAt)
546
+ if (!Number.isFinite(resetAtMs) || resetAtMs <= now) return undefined
547
+ return { resetsAt: spendControl.resetsAt, resetAtMs }
548
+ }
549
+
431
550
  // Earliest future reset among the quota's exhausted windows, or undefined when
432
551
  // no present window is exhausted. Every present window is evaluated — matching
433
552
  // the admission policy, which rejects an account when ANY live window is below
@@ -454,6 +573,12 @@ export function exhaustedQuotaResetAt(
454
573
  earliest = { resetsAt: window.resetsAt, resetAtMs }
455
574
  }
456
575
  }
576
+ // The credit budget is a third axis on its own reset clock (a month, not
577
+ // 5h/7d), judged by the same shared signal sticky migration uses.
578
+ const spendReset = spendControlExhaustedResetAt(quota, now)
579
+ if (spendReset && (!earliest || spendReset.resetAtMs < earliest.resetAtMs)) {
580
+ earliest = spendReset
581
+ }
457
582
  return earliest
458
583
  }
459
584
 
@@ -1004,6 +1129,9 @@ function mergeQuotaByWindow(
1004
1129
  incoming.checkedAt,
1005
1130
  existing.checkedAt,
1006
1131
  )
1132
+ const spendControl = existingSnapshotIsFresher
1133
+ ? (existing.spendControl ?? incoming.spendControl)
1134
+ : (incoming.spendControl ?? existing.spendControl)
1007
1135
  let checkedAt: number | undefined
1008
1136
  for (const stamp of [
1009
1137
  finiteWindowCheckedAt(primary),
@@ -1017,6 +1145,7 @@ function mergeQuotaByWindow(
1017
1145
  ...incoming,
1018
1146
  primary,
1019
1147
  secondary,
1148
+ ...(spendControl !== undefined ? { spendControl } : {}),
1020
1149
  checkedAt: checkedAt ?? incoming.checkedAt,
1021
1150
  }
1022
1151
  }
@@ -466,7 +466,7 @@ export function openCommandDialog(api, payload, apply, sessionId) {
466
466
  return;
467
467
  }
468
468
  if (payload.command === 'openai-account') {
469
- openAccountDialog(api, apply, sessionId);
469
+ openAccountDialog(api, apply, sessionId, payload);
470
470
  return;
471
471
  }
472
472
 
@@ -507,8 +507,49 @@ export function buildAccountDialogRows(state, sessionId) {
507
507
  description: formatQuotaWindows(fb.quota)
508
508
  }))];
509
509
  }
510
- function openAccountDialog(api, apply, sessionId) {
510
+ export function accountDialogModeOption(mode) {
511
+ const claustrum = mode === 'claustrum';
512
+ return {
513
+ title: claustrum ? 'Leave Claustrum' : 'Enter Claustrum',
514
+ value: claustrum ? '__local__' : '__claustrum__',
515
+ description: claustrum ? 'Require fresh local logins before local refresh resumes' : 'Verify every enabled account before custody takes over'
516
+ };
517
+ }
518
+ export function buildFallbackAccountOptions(input) {
519
+ const name = input.label ?? input.id;
520
+ const options = [{
521
+ title: input.enabled ? 'Disable' : 'Enable',
522
+ value: input.enabled ? 'disable' : 'enable',
523
+ description: `${input.enabled ? 'Disable' : 'Enable'} ${name}`
524
+ }, {
525
+ title: 'Remove',
526
+ value: 'remove',
527
+ description: `Remove ${name}`
528
+ }];
529
+ if (input.index > 0) {
530
+ options.push({
531
+ title: 'Move up',
532
+ value: 'move_up',
533
+ description: 'Swap with the preceding fallback'
534
+ });
535
+ }
536
+ if (input.index < input.count - 1) {
537
+ options.push({
538
+ title: 'Move down',
539
+ value: 'move_down',
540
+ description: 'Swap with the following fallback'
541
+ });
542
+ }
543
+ options.push({
544
+ title: 'Back',
545
+ value: 'back',
546
+ description: 'Return to account list'
547
+ });
548
+ return options;
549
+ }
550
+ function openAccountDialog(api, apply, sessionId, payload) {
511
551
  const DialogConfirm = api.ui.DialogConfirm;
552
+ let claustrumMode = payload?.knobs.claustrumMode;
512
553
  function showL1() {
513
554
  void getSidebarState().then(state => {
514
555
  const DialogSelectInner = api.ui.DialogSelect;
@@ -516,13 +557,24 @@ function openAccountDialog(api, apply, sessionId) {
516
557
  api.ui.dialog.replace(() => _$createComponent(DialogSelectInner, {
517
558
  title: "OpenAI Accounts",
518
559
  get options() {
519
- return [...buildAccountDialogRows(state, sessionId), {
560
+ return [accountDialogModeOption(claustrumMode), ...buildAccountDialogRows(state, sessionId), {
520
561
  title: 'Add account\u2026',
521
562
  value: '__add__',
522
563
  description: 'Sign in to a new OpenAI account'
523
564
  }];
524
565
  },
525
566
  onSelect: option => {
567
+ if (option.value === '__claustrum__' || option.value === '__local__') {
568
+ const args = option.value === '__claustrum__' ? 'claustrum' : 'local';
569
+ void apply('openai-account', args).then(r => {
570
+ api.ui.toast({
571
+ message: r.text
572
+ });
573
+ claustrumMode = r.knobs.claustrumMode;
574
+ showL1();
575
+ });
576
+ return;
577
+ }
526
578
  if (option.value === '__add__') {
527
579
  showAddFlow();
528
580
  return;
@@ -544,36 +596,12 @@ function openAccountDialog(api, apply, sessionId) {
544
596
  showL1();
545
597
  return;
546
598
  }
547
- const options = [];
548
- options.push({
549
- title: 'Remove',
550
- value: 'remove',
551
- description: `Remove ${fb.label ?? fb.id}`
552
- });
553
- if (fbIndex > 0) {
554
- const neighbor = state.fallbacks[fbIndex - 1];
555
- if (neighbor) {
556
- options.push({
557
- title: 'Move up',
558
- value: 'move_up',
559
- description: `Swap with ${neighbor.label ?? neighbor.id}`
560
- });
561
- }
562
- }
563
- if (fbIndex < state.fallbacks.length - 1) {
564
- const neighbor = state.fallbacks[fbIndex + 1];
565
- if (neighbor) {
566
- options.push({
567
- title: 'Move down',
568
- value: 'move_down',
569
- description: `Swap with ${neighbor.label ?? neighbor.id}`
570
- });
571
- }
572
- }
573
- options.push({
574
- title: 'Back',
575
- value: 'back',
576
- description: 'Return to account list'
599
+ const options = buildFallbackAccountOptions({
600
+ id: fb.id,
601
+ label: fb.label,
602
+ enabled: fb.enabled,
603
+ index: fbIndex,
604
+ count: state.fallbacks.length
577
605
  });
578
606
  api.ui.dialog.setSize('xlarge');
579
607
  api.ui.dialog.replace(() => _$createComponent(DialogSelectInner, {
@@ -601,6 +629,15 @@ function openAccountDialog(api, apply, sessionId) {
601
629
  }));
602
630
  return;
603
631
  }
632
+ if (option.value === 'enable' || option.value === 'disable') {
633
+ void apply('openai-account', `${option.value} ${id}`).then(r => {
634
+ api.ui.toast({
635
+ message: r.text
636
+ });
637
+ showL1();
638
+ });
639
+ return;
640
+ }
604
641
  if (option.value === 'move_up') {
605
642
  const neighbor = state.fallbacks[fbIndex - 1];
606
643
  if (neighbor) {