@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
@@ -0,0 +1,243 @@
1
+ export type CustodyMode = 'local' | 'claustrum'
2
+
3
+ export type ManifestCustodyState = 'absent' | 'present' | 'unreadable'
4
+
5
+ export type LocalCustodyState =
6
+ | 'real'
7
+ | 'tombstone'
8
+ | 'empty'
9
+ | 'gone'
10
+ | 'slot-absent'
11
+
12
+ export type VaultCustodyState =
13
+ | 'serves'
14
+ | 'cold'
15
+ | 'needs_reauth'
16
+ | 'identity_mismatch'
17
+ | 'no_handle'
18
+
19
+ export const CUSTODY_INERT_REASONS = [
20
+ 'needs-login',
21
+ 'mode-mismatch',
22
+ 'corrupt-under-binding',
23
+ 'manifest-unreadable',
24
+ 'unbound-under-claustrum',
25
+ 'orphan-tombstone',
26
+ 'vault-cold',
27
+ 'vault-reauth',
28
+ 'identity-mismatch',
29
+ 'no-handle',
30
+ 'takeover-incomplete',
31
+ 'new-local-family-under-claustrum',
32
+ 'takeover-incomplete/vault-unavailable',
33
+ 'takeover-incomplete/slot-absent',
34
+ ] as const
35
+
36
+ export type CustodyInertReason = (typeof CUSTODY_INERT_REASONS)[number]
37
+
38
+ export type CustodyVerdict =
39
+ | { kind: 'LOCAL' }
40
+ | {
41
+ kind: 'VAULT'
42
+ canonicalize?: boolean
43
+ installTombstone?: boolean
44
+ }
45
+ | {
46
+ kind: 'INERT'
47
+ reason: CustodyInertReason
48
+ installTombstone?: boolean
49
+ canonicalize?: boolean
50
+ }
51
+ | { kind: 'NEEDS_LOGIN'; reason?: 'corrupt' }
52
+
53
+ export interface EvaluateCustodyStartupInput {
54
+ mode: CustodyMode
55
+ manifest: ManifestCustodyState
56
+ local: LocalCustodyState
57
+ vault: () => VaultCustodyState
58
+ manifestFailureReason?: string
59
+ fingerprintMatch?: boolean
60
+ verifiedInProcessLogin: boolean
61
+ isMain?: boolean
62
+ }
63
+
64
+ function unreachable(value: never): never {
65
+ return value
66
+ }
67
+
68
+ function withCanonicalization<
69
+ T extends Extract<CustodyVerdict, { kind: 'VAULT' | 'INERT' }>,
70
+ >(local: LocalCustodyState, verdict: T): T | (T & { canonicalize: true }) {
71
+ return local === 'empty' ? { ...verdict, canonicalize: true } : verdict
72
+ }
73
+
74
+ function evaluateLocal(local: LocalCustodyState): CustodyVerdict {
75
+ switch (local) {
76
+ case 'real':
77
+ return { kind: 'LOCAL' }
78
+ case 'tombstone':
79
+ case 'empty':
80
+ case 'slot-absent':
81
+ return { kind: 'NEEDS_LOGIN' }
82
+ case 'gone':
83
+ return { kind: 'NEEDS_LOGIN', reason: 'corrupt' }
84
+ }
85
+ return unreachable(local)
86
+ }
87
+
88
+ function evaluateLocalWithBinding(
89
+ local: LocalCustodyState,
90
+ verifiedInProcessLogin: boolean,
91
+ ): CustodyVerdict {
92
+ switch (local) {
93
+ case 'real':
94
+ return verifiedInProcessLogin
95
+ ? { kind: 'LOCAL' }
96
+ : { kind: 'INERT', reason: 'needs-login' }
97
+ case 'tombstone':
98
+ case 'empty':
99
+ case 'slot-absent':
100
+ return { kind: 'INERT', reason: 'mode-mismatch' }
101
+ case 'gone':
102
+ return { kind: 'INERT', reason: 'corrupt-under-binding' }
103
+ }
104
+ return unreachable(local)
105
+ }
106
+
107
+ function evaluateUnboundClaustrum(local: LocalCustodyState): CustodyVerdict {
108
+ switch (local) {
109
+ case 'tombstone':
110
+ case 'empty':
111
+ return { kind: 'INERT', reason: 'orphan-tombstone' }
112
+ case 'real':
113
+ case 'gone':
114
+ case 'slot-absent':
115
+ return { kind: 'INERT', reason: 'unbound-under-claustrum' }
116
+ }
117
+ return unreachable(local)
118
+ }
119
+
120
+ function evaluateTombstone(
121
+ local: Extract<LocalCustodyState, 'tombstone' | 'empty'>,
122
+ vault: VaultCustodyState,
123
+ ): CustodyVerdict {
124
+ switch (vault) {
125
+ case 'serves':
126
+ return withCanonicalization(local, { kind: 'VAULT' })
127
+ case 'cold':
128
+ return withCanonicalization(local, {
129
+ kind: 'INERT',
130
+ reason: 'vault-cold',
131
+ })
132
+ case 'needs_reauth':
133
+ return withCanonicalization(local, {
134
+ kind: 'INERT',
135
+ reason: 'vault-reauth',
136
+ })
137
+ case 'identity_mismatch':
138
+ return withCanonicalization(local, {
139
+ kind: 'INERT',
140
+ reason: 'identity-mismatch',
141
+ })
142
+ case 'no_handle':
143
+ return withCanonicalization(local, { kind: 'INERT', reason: 'no-handle' })
144
+ }
145
+ return unreachable(vault)
146
+ }
147
+
148
+ function evaluateReal(
149
+ vault: VaultCustodyState,
150
+ fingerprintMatch: boolean | undefined,
151
+ ): CustodyVerdict {
152
+ switch (vault) {
153
+ case 'serves':
154
+ return fingerprintMatch === false
155
+ ? { kind: 'INERT', reason: 'new-local-family-under-claustrum' }
156
+ : { kind: 'INERT', reason: 'takeover-incomplete' }
157
+ case 'cold':
158
+ case 'needs_reauth':
159
+ return { kind: 'INERT', reason: 'takeover-incomplete/vault-unavailable' }
160
+ case 'identity_mismatch':
161
+ return { kind: 'INERT', reason: 'identity-mismatch' }
162
+ case 'no_handle':
163
+ return { kind: 'INERT', reason: 'no-handle' }
164
+ }
165
+ return unreachable(vault)
166
+ }
167
+
168
+ function evaluateGone(vault: VaultCustodyState): CustodyVerdict {
169
+ switch (vault) {
170
+ case 'serves':
171
+ return { kind: 'VAULT', installTombstone: true }
172
+ case 'cold':
173
+ return { kind: 'INERT', reason: 'vault-cold', installTombstone: true }
174
+ case 'needs_reauth':
175
+ return { kind: 'INERT', reason: 'vault-reauth', installTombstone: true }
176
+ case 'identity_mismatch':
177
+ return { kind: 'INERT', reason: 'identity-mismatch' }
178
+ case 'no_handle':
179
+ return { kind: 'INERT', reason: 'no-handle' }
180
+ }
181
+ return unreachable(vault)
182
+ }
183
+
184
+ function evaluateSlotAbsent(vault: VaultCustodyState): CustodyVerdict {
185
+ switch (vault) {
186
+ case 'serves':
187
+ case 'cold':
188
+ case 'needs_reauth':
189
+ return { kind: 'INERT', reason: 'takeover-incomplete/slot-absent' }
190
+ case 'identity_mismatch':
191
+ return { kind: 'INERT', reason: 'identity-mismatch' }
192
+ case 'no_handle':
193
+ return { kind: 'INERT', reason: 'no-handle' }
194
+ }
195
+ return unreachable(vault)
196
+ }
197
+
198
+ function evaluatePresentClaustrum(
199
+ local: LocalCustodyState,
200
+ vault: VaultCustodyState,
201
+ fingerprintMatch: boolean | undefined,
202
+ ): CustodyVerdict {
203
+ switch (local) {
204
+ case 'tombstone':
205
+ case 'empty':
206
+ return evaluateTombstone(local, vault)
207
+ case 'real':
208
+ return evaluateReal(vault, fingerprintMatch)
209
+ case 'gone':
210
+ return evaluateGone(vault)
211
+ case 'slot-absent':
212
+ return evaluateSlotAbsent(vault)
213
+ }
214
+ return unreachable(local)
215
+ }
216
+
217
+ export function evaluateCustodyStartup(
218
+ input: EvaluateCustodyStartupInput,
219
+ ): CustodyVerdict {
220
+ if (input.manifest === 'unreadable') {
221
+ return { kind: 'INERT', reason: 'manifest-unreadable' }
222
+ }
223
+
224
+ switch (input.mode) {
225
+ case 'local':
226
+ return input.manifest === 'present'
227
+ ? evaluateLocalWithBinding(input.local, input.verifiedInProcessLogin)
228
+ : evaluateLocal(input.local)
229
+ case 'claustrum':
230
+ if (input.manifest === 'absent') {
231
+ return evaluateUnboundClaustrum(input.local)
232
+ }
233
+ if (input.manifest === 'present') {
234
+ return evaluatePresentClaustrum(
235
+ input.local,
236
+ input.vault(),
237
+ input.fingerprintMatch,
238
+ )
239
+ }
240
+ return unreachable(input.manifest)
241
+ }
242
+ return unreachable(input.mode)
243
+ }
@@ -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
  }
@@ -619,7 +619,7 @@ export function openCommandDialog(
619
619
  }
620
620
 
621
621
  if (payload.command === 'openai-account') {
622
- openAccountDialog(api, apply, sessionId)
622
+ openAccountDialog(api, apply, sessionId, payload)
623
623
  return
624
624
  }
625
625
 
@@ -676,12 +676,71 @@ export function buildAccountDialogRows(
676
676
  ]
677
677
  }
678
678
 
679
+ export function accountDialogModeOption(mode: unknown) {
680
+ const claustrum = mode === 'claustrum'
681
+ return {
682
+ title: claustrum ? 'Leave Claustrum' : 'Enter Claustrum',
683
+ value: claustrum ? '__local__' : '__claustrum__',
684
+ description: claustrum
685
+ ? 'Require fresh local logins before local refresh resumes'
686
+ : 'Verify every enabled account before custody takes over',
687
+ }
688
+ }
689
+
690
+ export function buildFallbackAccountOptions(input: {
691
+ id: string
692
+ label?: string
693
+ enabled: boolean
694
+ index: number
695
+ count: number
696
+ }) {
697
+ const name = input.label ?? input.id
698
+ const options: Array<{
699
+ title: string
700
+ value: string
701
+ description: string
702
+ }> = [
703
+ {
704
+ title: input.enabled ? 'Disable' : 'Enable',
705
+ value: input.enabled ? 'disable' : 'enable',
706
+ description: `${input.enabled ? 'Disable' : 'Enable'} ${name}`,
707
+ },
708
+ {
709
+ title: 'Remove',
710
+ value: 'remove',
711
+ description: `Remove ${name}`,
712
+ },
713
+ ]
714
+ if (input.index > 0) {
715
+ options.push({
716
+ title: 'Move up',
717
+ value: 'move_up',
718
+ description: 'Swap with the preceding fallback',
719
+ })
720
+ }
721
+ if (input.index < input.count - 1) {
722
+ options.push({
723
+ title: 'Move down',
724
+ value: 'move_down',
725
+ description: 'Swap with the following fallback',
726
+ })
727
+ }
728
+ options.push({
729
+ title: 'Back',
730
+ value: 'back',
731
+ description: 'Return to account list',
732
+ })
733
+ return options
734
+ }
735
+
679
736
  function openAccountDialog(
680
737
  api: TuiPluginApi,
681
738
  apply: ApplyFn,
682
739
  sessionId?: string,
740
+ payload?: OpenDialogPayload,
683
741
  ) {
684
742
  const DialogConfirm = api.ui.DialogConfirm
743
+ let claustrumMode = payload?.knobs.claustrumMode
685
744
 
686
745
  function showL1() {
687
746
  void getSidebarState().then((state) => {
@@ -691,6 +750,7 @@ function openAccountDialog(
691
750
  <DialogSelectInner
692
751
  title='OpenAI Accounts'
693
752
  options={[
753
+ accountDialogModeOption(claustrumMode),
694
754
  ...buildAccountDialogRows(state, sessionId),
695
755
  {
696
756
  title: 'Add account\u2026',
@@ -699,6 +759,19 @@ function openAccountDialog(
699
759
  },
700
760
  ]}
701
761
  onSelect={(option) => {
762
+ if (
763
+ option.value === '__claustrum__' ||
764
+ option.value === '__local__'
765
+ ) {
766
+ const args =
767
+ option.value === '__claustrum__' ? 'claustrum' : 'local'
768
+ void apply('openai-account', args).then((r) => {
769
+ api.ui.toast({ message: r.text })
770
+ claustrumMode = r.knobs.claustrumMode
771
+ showL1()
772
+ })
773
+ return
774
+ }
702
775
  if (option.value === '__add__') {
703
776
  showAddFlow()
704
777
  return
@@ -722,44 +795,12 @@ function openAccountDialog(
722
795
  showL1()
723
796
  return
724
797
  }
725
- const options: Array<{
726
- title: string
727
- value: string
728
- description: string
729
- }> = []
730
-
731
- options.push({
732
- title: 'Remove',
733
- value: 'remove',
734
- description: `Remove ${fb.label ?? fb.id}`,
735
- })
736
-
737
- if (fbIndex > 0) {
738
- const neighbor = state.fallbacks[fbIndex - 1]
739
- if (neighbor) {
740
- options.push({
741
- title: 'Move up',
742
- value: 'move_up',
743
- description: `Swap with ${neighbor.label ?? neighbor.id}`,
744
- })
745
- }
746
- }
747
-
748
- if (fbIndex < state.fallbacks.length - 1) {
749
- const neighbor = state.fallbacks[fbIndex + 1]
750
- if (neighbor) {
751
- options.push({
752
- title: 'Move down',
753
- value: 'move_down',
754
- description: `Swap with ${neighbor.label ?? neighbor.id}`,
755
- })
756
- }
757
- }
758
-
759
- options.push({
760
- title: 'Back',
761
- value: 'back',
762
- description: 'Return to account list',
798
+ const options = buildFallbackAccountOptions({
799
+ id: fb.id,
800
+ label: fb.label,
801
+ enabled: fb.enabled,
802
+ index: fbIndex,
803
+ count: state.fallbacks.length,
763
804
  })
764
805
 
765
806
  api.ui.dialog.setSize('xlarge')
@@ -785,6 +826,15 @@ function openAccountDialog(
785
826
  ))
786
827
  return
787
828
  }
829
+ if (option.value === 'enable' || option.value === 'disable') {
830
+ void apply('openai-account', `${option.value} ${id}`).then(
831
+ (r) => {
832
+ api.ui.toast({ message: r.text })
833
+ showL1()
834
+ },
835
+ )
836
+ return
837
+ }
788
838
  if (option.value === 'move_up') {
789
839
  const neighbor = state.fallbacks[fbIndex - 1]
790
840
  if (neighbor) {