ccdeck 3.53.3 → 3.81.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 (78) hide show
  1. package/README.md +63 -7
  2. package/THIRD_PARTY_NOTICES.md +31 -1
  3. package/bin/cli/tui.js +433 -11
  4. package/dist/web/assets/{BrowserWatchModal-CJ0CW4Go.js → BrowserWatchModal-DKvBSlGT.js} +1 -1
  5. package/dist/web/assets/{IntegrationModal-DlJzBN8b.js → IntegrationModal-D3NLczRe.js} +1 -1
  6. package/dist/web/assets/{RadioPanel-CHl3s3OH.js → RadioPanel-dCIMGTd9.js} +1 -1
  7. package/dist/web/assets/{TrafficRadar-Cw3i0Hux.js → TrafficRadar-CMbwnKe7.js} +1 -1
  8. package/dist/web/assets/{UsageHistoryModal-CiB5-CTH.js → UsageHistoryModal-DkGBQaM4.js} +1 -1
  9. package/dist/web/assets/{app-xTrs4aEH.css → app-BIrh5Q0W.css} +1 -1
  10. package/dist/web/assets/{app-2vj7uk2U.js → app-l0spNrc0.js} +93 -93
  11. package/dist/web/assets/{index-D1YZbdq3.js → index-inV7HzhI.js} +1 -1
  12. package/dist/web/index.html +3 -3
  13. package/node_modules/ws/LICENSE +20 -0
  14. package/node_modules/ws/README.md +540 -0
  15. package/node_modules/ws/browser.js +8 -0
  16. package/node_modules/ws/index.js +22 -0
  17. package/node_modules/ws/lib/buffer-util.js +142 -0
  18. package/node_modules/ws/lib/constants.js +19 -0
  19. package/node_modules/ws/lib/event-target.js +292 -0
  20. package/node_modules/ws/lib/extension.js +203 -0
  21. package/node_modules/ws/lib/limiter.js +55 -0
  22. package/node_modules/ws/lib/permessage-deflate.js +530 -0
  23. package/node_modules/ws/lib/receiver.js +743 -0
  24. package/node_modules/ws/lib/sender.js +610 -0
  25. package/node_modules/ws/lib/stream.js +161 -0
  26. package/node_modules/ws/lib/subprotocol.js +62 -0
  27. package/node_modules/ws/lib/validation.js +152 -0
  28. package/node_modules/ws/lib/websocket-server.js +562 -0
  29. package/node_modules/ws/lib/websocket.js +1426 -0
  30. package/node_modules/ws/package.json +74 -0
  31. package/node_modules/ws/wrapper.mjs +21 -0
  32. package/package.json +5 -3
  33. package/release-notes.json +71 -0
  34. package/src/server/account-engine.mjs +87 -0
  35. package/src/server/account-notify.mjs +81 -16
  36. package/src/server/account-routes.mjs +49 -3
  37. package/src/server/account-watch.mjs +21 -0
  38. package/src/server/codex-auto.mjs +163 -54
  39. package/src/server/codex-engine-adapter.mjs +272 -0
  40. package/src/server/codex-engine-bridge.mjs +47 -0
  41. package/src/server/codex-engine-config.mjs +42 -0
  42. package/src/server/codex-engine-rpc.mjs +88 -0
  43. package/src/server/codex-handoff.mjs +20 -0
  44. package/src/server/codex-interactive-engine.mjs +335 -0
  45. package/src/server/codex-lan-identity.mjs +61 -0
  46. package/src/server/codex-live-checkpoint.mjs +55 -0
  47. package/src/server/codex-live-credentials.mjs +214 -0
  48. package/src/server/codex-live-engine.mjs +577 -0
  49. package/src/server/codex-live-handoff.mjs +300 -0
  50. package/src/server/codex-live-lease.mjs +64 -0
  51. package/src/server/codex-live-rpc.mjs +158 -0
  52. package/src/server/codex-profiles.mjs +34 -4
  53. package/src/server/codex-projects.mjs +308 -0
  54. package/src/server/codex-quota.mjs +36 -2
  55. package/src/server/codex-selection.mjs +78 -9
  56. package/src/server/codex-share.mjs +238 -0
  57. package/src/server/codex-terminal-runner.mjs +38 -1
  58. package/src/server/codex-terminal.mjs +15 -3
  59. package/src/server/codex-usage.mjs +69 -0
  60. package/src/server/codex-watch.mjs +3 -1
  61. package/src/server/codex-windows-private.mjs +96 -0
  62. package/src/server/codex-windows-private.ps1 +140 -0
  63. package/src/server/codex-windows-proxy.mjs +53 -0
  64. package/src/server/index.mjs +12 -4
  65. package/src/server/lan-codex-store.mjs +170 -0
  66. package/src/server/lan-codex.mjs +103 -0
  67. package/src/server/lan-deck.mjs +5 -0
  68. package/src/server/lan-engine.mjs +34 -6
  69. package/src/server/lan-manifest.mjs +16 -2
  70. package/src/server/lan-serve.mjs +16 -6
  71. package/src/server/login-service.mjs +2 -0
  72. package/src/server/pinned-build.mjs +5 -0
  73. package/src/server/request-gates.mjs +3 -0
  74. package/src/server/tui-codex.mjs +229 -0
  75. package/src/server/tui-lan.mjs +684 -0
  76. package/src/server/tui-machine.mjs +492 -0
  77. package/src/server/tui-network.mjs +730 -0
  78. package/src/server/tui-view.mjs +735 -60
@@ -24,6 +24,15 @@ import {
24
24
  } from "./cli-lines.mjs";
25
25
  import { accountKey } from "./lan-copies.mjs";
26
26
  import { fg, link, sinceLabel, spinnerFrames, stripAnsi } from "./term.mjs";
27
+ import {
28
+ CODEX_LOGIN_FAILED, codexMenuItems, codexNameTrouble, codexRows, hasCodex,
29
+ } from "./tui-codex.mjs";
30
+ import { machinePanel } from "./tui-machine.mjs";
31
+ import {
32
+ hasLan, lanAsking, lanFooterKeys, lanFormSpec, lanInviteKeys, lanInviteLines, lanInvitePage, lanKey, lanLines, onLan,
33
+ } from "./tui-network.mjs";
34
+
35
+ export { hasLan, onLan };
27
36
 
28
37
  /** Text from elsewhere — the deck's own sentences, an alias — on a console
29
38
  * that draws ASCII alone: its punctuation in ASCII's, anything else a "?". */
@@ -49,6 +58,9 @@ export function initialState() {
49
58
  trouble: null, // why the roster has no rows, as lines, when it has none
50
59
  auto: null, // GET /api/cswap-auto, or null
51
60
  sessions: null, // GET /api/sessions' rows, or null
61
+ machine: null, // the deck's machine readings, kept (tui-machine.mjs withReading)
62
+ machineView: "live", // "live" | "hours": what the Machine panel's charts reach back over
63
+ history: null, // the deck's minute history, while the hours are shown (historyMachine)
52
64
  skew: 0, // the deck's clock minus this one, for the sessions' ages
53
65
  at: 0, // the selected account, by index
54
66
  top: 0, // the first account row on screen
@@ -57,6 +69,19 @@ export function initialState() {
57
69
  busy: null, // { text, since } while a switch or a start is under way
58
70
  starting: false,
59
71
  signin: null, // the sign-in page while it is up (openSignin)
72
+ menu: null, // an account's menu while it is up: { num, id, view, at, draft, error }
73
+ side: "claude", // "claude" | "codex" | "lan": whose accounts the list shows, or the Local network
74
+ lan: null, // GET /api/lan, or null: not read, or no Local network to show
75
+ lanMissing: false, // the deck answered 404: it has no Local network
76
+ lanPrefs: null, // GET /api/prefs' `lan`, for the addresses it calls
77
+ lanSel: null, // the selected Network row, by its id
78
+ lanForm: null, // a Network form while it is up (tui-network.mjs lanFormKey)
79
+ lanFrom: "claude", // the side l came from, which l goes back to
80
+ codex: null, // GET /api/codex-profiles, or null: no Codex side to show
81
+ codexQuota: {}, // each Codex account's GET /api/codex-profile-quota answer, by id
82
+ codexAuto: null, // GET /api/codex-auto, or null
83
+ codexAt: null, // the selected Codex account, by index; null until the side is first shown
84
+ codexSignin: null, // the Codex sign-in page while it is up: { id, name, adding, started, login }
60
85
  };
61
86
  }
62
87
 
@@ -65,31 +90,74 @@ export function note(tone, text, now) {
65
90
  return { tone, text, until: tone === "err" || tone === "warn" ? null : now + NOTICE_MS };
66
91
  }
67
92
 
68
- const accountsOf = (state) => (state.roster?.ok && Array.isArray(state.roster.accounts) ? state.roster.accounts : []);
93
+ const claudeRowsOf = (state) => (state.roster?.ok && Array.isArray(state.roster.accounts) ? state.roster.accounts : []);
94
+
95
+ /** Whether the list shows the Codex accounts. */
96
+ export const onCodex = (state) => state.side === "codex";
97
+
98
+ /** The rows of the side on screen. `now` only ages the Codex readings. */
99
+ const accountsOf = (state, now = null) => (onCodex(state) ? codexRows(state, now) : claudeRowsOf(state));
69
100
 
70
- /** Auto-switch's home account, by slot, or null. */
101
+ /** The selected row's index on the side on screen. */
102
+ const atOf = (state) => (onCodex(state) ? state.codexAt ?? 0 : state.at);
103
+
104
+ /** The selection moved to index `i` on the side on screen. */
105
+ const withAt = (state, i) => (onCodex(state) ? { ...state, codexAt: i } : { ...state, at: i });
106
+
107
+ /** The row an open menu is about: a Codex account by its id, which keeps
108
+ * when the list's order does not; a Claude one by its slot. */
109
+ const menuRow = (state, m) => accountsOf(state).find(a => (m.id != null ? a.id === m.id : a.num === m.num)) ?? null;
110
+
111
+ /** Auto-switch's home account, by slot, or null. A Codex home is a profile id. */
71
112
  export function homeSlot(state) {
113
+ if (onCodex(state)) {
114
+ const id = state.codexAuto?.homeId;
115
+ return typeof id === "string" ? accountsOf(state).find(a => a.id === id)?.num ?? null : null;
116
+ }
72
117
  const home = state.auto?.choice?.home;
73
118
  if (!home || typeof home.email !== "string") return null;
74
119
  const key = accountKey(home.email, home.orgUuid);
75
- return accountsOf(state).find(a => accountKey(a.email, a.orgUuid) === key)?.num ?? null;
120
+ return claudeRowsOf(state).find(a => accountKey(a.email, a.orgUuid) === key)?.num ?? null;
76
121
  }
77
122
 
78
- /** The selection moved onto the account in slot `num`, if there is one. */
123
+ /** The selection moved onto the Claude account in slot `num`, if there is one. */
79
124
  export function selectSlot(state, num) {
80
- const i = accountsOf(state).findIndex(a => a.num === num);
125
+ const i = claudeRowsOf(state).findIndex(a => a.num === num);
81
126
  return i === -1 ? state : { ...state, at: i };
82
127
  }
83
128
 
129
+ /** The selection moved onto the Codex account `id`, if there is one. */
130
+ export function selectCodex(state, id) {
131
+ const i = codexRows(state).findIndex(a => a.id === id);
132
+ return i === -1 ? state : { ...state, codexAt: i };
133
+ }
134
+
135
+ /**
136
+ * The deck's Codex accounts in place of the old answer, the selection kept on
137
+ * the account it was on, or — until the side has been shown — put on the
138
+ * default. An answer with no accounts leaves the side; a side left showing
139
+ * goes back to Claude's.
140
+ */
141
+ export function withCodex(state, codex) {
142
+ const before = state.codexAt == null ? null : codexRows(state)[state.codexAt]?.id ?? null;
143
+ const next = { ...state, codex };
144
+ const rows = codexRows(next);
145
+ if (!rows.length) return { ...next, codexAt: null, side: onCodex(state) && !codex ? "claude" : state.side };
146
+ const kept = before == null ? -1 : rows.findIndex(a => a.id === before);
147
+ if (kept !== -1) return { ...next, codexAt: kept };
148
+ if (state.codexAt == null) return { ...next, codexAt: null };
149
+ return { ...next, codexAt: Math.max(0, Math.min(state.codexAt, rows.length - 1)) };
150
+ }
151
+
84
152
  /**
85
153
  * A fresh roster in place of the old one, the selection kept on the account it
86
154
  * was on — the list's order is the rotation's and can change under it — or,
87
155
  * on the first read, put on the account in use.
88
156
  */
89
157
  export function withRoster(state, roster, trouble = null) {
90
- const before = accountsOf(state)[state.at]?.num;
158
+ const before = claudeRowsOf(state)[state.at]?.num;
91
159
  const next = { ...state, roster, trouble };
92
- const rows = accountsOf(next);
160
+ const rows = claudeRowsOf(next);
93
161
  const kept = before == null ? -1 : rows.findIndex(a => a.num === before);
94
162
  const at = kept !== -1 ? kept : Math.max(0, rows.findIndex(a => a.active));
95
163
  return { ...next, at: Math.min(at, Math.max(0, rows.length - 1)) };
@@ -111,6 +179,12 @@ export function withRoster(state, roster, trouble = null) {
111
179
  * { type: "signin-code", code } hand it the code the page showed
112
180
  * { type: "signin-cancel", quiet } let go of the deck's sign-in
113
181
  * { type: "signin-done", account } the page closed on a sign-in that took
182
+ * { type: "history" } read the deck's machine history (t)
183
+ * { type: "home", account, set } make it Auto-switch's home, or stop
184
+ * { type: "hold", account, out } hold it out of rotation, or put it back
185
+ * { type: "alias", account, alias } rename it ("" takes the alias away)
186
+ * { type: "move", account, slot }
187
+ * { type: "remove", account }
114
188
  *
115
189
  * The refusals a switch can be told before it is asked for — the account in
116
190
  * use, a deck that is running and not answering — are answered here, in the
@@ -122,34 +196,63 @@ export function tuiKey(state, str, key = {}, { now = 0, g } = {}) {
122
196
  if (key.ctrl && name === "c") return { state, effect: { type: "quit" } };
123
197
  if (key.ctrl && name === "l") return { state, effect: { type: "redraw" } };
124
198
  if (state.signin) return signinKey(state, str, key, { now, g });
199
+ if (state.codexSignin) return codexSigninKey(state, str, key, { now, g });
125
200
  // Help is a page over the view: any key puts the view back, and the key is
126
201
  // spent doing it — q there closes the page, it does not quit.
127
202
  if (state.help) return { state: { ...state, help: false }, effect: null };
203
+ if (state.menu) return menuKey(state, str, key, { now, g });
204
+ if (state.lanForm) return lanKey(state, str, key, { now, g, note });
128
205
  if (str === "q" || name === "escape") return { state, effect: { type: "quit" } };
129
206
  if (str === "?") return { state: { ...state, help: true }, effect: null };
130
207
 
208
+ // The sides: c turns Claude's list over to Codex's, l opens the Local
209
+ // network and closes it again, the arrows walk along the tabs.
210
+ if (str === "c") return sideKey({ ...state, notice: null }, null, { now, g });
211
+ if (str === "l") return lanSideKey({ ...state, notice: null }, { now, g });
212
+ if (name === "left" || name === "right") return stepSide({ ...state, notice: null }, name === "right" ? 1 : -1);
213
+ if (onLan(state)) {
214
+ const done = lanKey({ ...state, notice: null }, str, key, { now, g, note });
215
+ if (done) return done;
216
+ // The machine's keys and the deck's are everybody's; an account's are not.
217
+ if (str !== "t" && str !== "d") return { state, effect: null };
218
+ }
219
+
131
220
  const rows = accountsOf(state);
221
+ const at = atOf(state);
132
222
  const s = { ...state, notice: null };
133
- const go = (at) => ({ state: { ...s, at }, effect: null });
223
+ const go = (i) => ({ state: withAt(s, i), effect: null });
134
224
  const last = Math.max(0, rows.length - 1);
135
- if (name === "up" || str === "k") return go(rows.length ? (state.at - 1 + rows.length) % rows.length : 0);
136
- if (name === "down" || str === "j" || (name === "tab" && !key.shift)) return go(rows.length ? (state.at + 1) % rows.length : 0);
225
+ if (name === "up" || str === "k") return go(rows.length ? (at - 1 + rows.length) % rows.length : 0);
226
+ if (name === "down" || str === "j" || (name === "tab" && !key.shift)) return go(rows.length ? (at + 1) % rows.length : 0);
137
227
  if (name === "home" || str === "g") return go(0);
138
228
  if (name === "end" || str === "G") return go(last);
139
- if (name === "pageup") return go(Math.max(0, state.at - 5));
140
- if (name === "pagedown") return go(Math.min(last, state.at + 5));
229
+ if (name === "pageup") return go(Math.max(0, at - 5));
230
+ if (name === "pagedown") return go(Math.min(last, at + 5));
141
231
  if (/^[1-9]$/.test(str ?? "")) {
142
232
  const i = rows.findIndex(a => a.num === Number(str));
143
233
  return i === -1 ? { state: s, effect: null } : go(i);
144
234
  }
145
- if (name === "return" || name === "enter" || str === "s") return switchKey(s, rows[state.at], { now, g });
235
+ if (name === "return" || name === "enter" || str === "s") {
236
+ return onCodex(state) ? codexSwitchKey(s, rows[at], { now, g }) : switchKey(s, rows[at], { now, g });
237
+ }
146
238
  if (str === "b") {
147
239
  if (!rows.length) return { state: s, effect: null };
148
- return { state: s, effect: { type: "best" } };
240
+ return onCodex(state) ? codexBestKey(s, rows, { now, g }) : { state: s, effect: { type: "best" } };
149
241
  }
150
242
  if (str === "a") return autoKey(s, { now, g });
151
- if (str === "+") return addKey(s, { now, g });
243
+ if (str === "+") return onCodex(state) ? codexAddKey(s, { now, g }) : addKey(s, { now, g });
244
+ // The account's own actions: the menu, or one of them straight from the list.
245
+ const row = rows[at];
246
+ if (row && (str === "m" || str === ".")) return menuOpen(s, row, "menu", { now, g });
247
+ if (row && ACTION_KEYS[str] && !key.ctrl && !key.meta) return menuAct(s, row, ACTION_KEYS[str], { now, g });
248
+ if (row && name === "delete") return menuAct(s, row, "remove", { now, g });
249
+ if (row?.codex && str === "i") return menuAct(s, row, "signin", { now, g });
152
250
  if (str === "r") return { state: s, effect: { type: "refresh" } };
251
+ if (str === "t") {
252
+ if (!state.deck) return { state: s, effect: null };
253
+ const view = state.machineView === "hours" ? "live" : "hours";
254
+ return { state: { ...s, machineView: view }, effect: view === "hours" ? { type: "history" } : null };
255
+ }
153
256
  if (str === "d") {
154
257
  if (state.deck) return { state: { ...s, notice: note("info", `the deck is already running on ${state.deck.port}`, now) }, effect: null };
155
258
  if (state.starting) return { state: s, effect: null };
@@ -181,10 +284,124 @@ function switchKey(state, row, { now, g }) {
181
284
 
182
285
  function autoKey(state, { now, g }) {
183
286
  const told = (tone, text) => ({ state: { ...state, notice: note(tone, text, now) }, effect: null });
184
- if (!state.deck) return told("info", `Auto-switch runs inside the deck ${g.dash} d starts one`);
287
+ const codex = onCodex(state);
288
+ const what = codex ? "Codex Auto-switch" : "Auto-switch";
289
+ const auto = codex ? state.codexAuto : state.auto;
290
+ if (!state.deck) return told("info", `${what} runs inside the deck ${g.dash} d starts one`);
291
+ if (state.busy) return told("info", `${state.busy.text} ${g.dash} one thing at a time`);
292
+ if (!auto) return told("info", `the deck has not said whether ${what} is on yet`);
293
+ return { state, effect: { type: "auto", enabled: auto.enabled !== true, codex } };
294
+ }
295
+
296
+ // ── the Codex side ──────────────────────────────────────────────────────────
297
+
298
+ /** The list turned to `side`, or over when `side` is null. The arrows reach
299
+ * the Codex side once the deck has a Codex account; c, asked for by name,
300
+ * once the deck answers for Codex at all — to add the first one. */
301
+ function sideKey(state, side, { now, g }) {
302
+ // From the Local network, c goes back to the accounts, Claude's first.
303
+ const to = side ?? (onCodex(state) || onLan(state) ? "claude" : "codex");
304
+ if (to === state.side) return { state, effect: null };
305
+ if (to === "codex" && !(side ? hasCodex(state) : state.deck && state.codex)) {
306
+ if (side) return { state, effect: null };
307
+ const why = state.deck ? `this deck does not serve Codex accounts ${g.dash} an update of ccdeck brings them` : `the Codex accounts are the deck's ${g.dash} d starts one`;
308
+ return { state: { ...state, notice: note("info", why, now) }, effect: null };
309
+ }
310
+ return { state: toSide(state, to), effect: null };
311
+ }
312
+
313
+ /** The screen turned to `to`, its list from the top. */
314
+ function toSide(state, to) {
315
+ const next = { ...state, side: to, top: 0 };
316
+ // The first look at the Codex side lands on the account Codex runs on.
317
+ if (to === "codex" && state.codexAt == null) next.codexAt = Math.max(0, codexRows(state).findIndex(a => a.active));
318
+ if (to === "lan" && !onLan(state)) next.lanFrom = state.side;
319
+ return next;
320
+ }
321
+
322
+ /** The tabs there are: Claude's always, Codex's and the Local network once
323
+ * the deck serves them. */
324
+ const sidesOf = (state) => [
325
+ "claude",
326
+ ...(hasCodex(state) || onCodex(state) ? ["codex"] : []),
327
+ ...(hasLan(state) || onLan(state) ? ["lan"] : []),
328
+ ];
329
+
330
+ /** The arrows: one tab along, stopping at either end. */
331
+ function stepSide(state, by) {
332
+ const list = sidesOf(state);
333
+ const to = list[Math.max(0, Math.min(list.length - 1, list.indexOf(state.side) + by))];
334
+ return to === state.side ? { state, effect: null } : { state: toSide(state, to), effect: null };
335
+ }
336
+
337
+ /** l: the Local network, and back to the accounts it was opened from. */
338
+ function lanSideKey(state, { now, g }) {
339
+ if (onLan(state)) return { state: toSide(state, state.lanFrom === "codex" && hasCodex(state) ? "codex" : "claude"), effect: null };
340
+ if (!hasLan(state)) {
341
+ const why = !state.deck ? `Local network is the deck's ${g.dash} d starts one`
342
+ : state.lanMissing ? `this deck has no Local network ${g.dash} an update of ccdeck brings it`
343
+ : `reading the Local network${g.ellipsis}`;
344
+ return { state: { ...state, notice: note("info", why, now) }, effect: null };
345
+ }
346
+ return { state: toSide(state, "lan"), effect: null };
347
+ }
348
+
349
+ /** Enter on a Codex account: make it the default, or sign in an account that
350
+ * has no login to switch to. */
351
+ function codexSwitchKey(state, row, { now, g }) {
352
+ if (!row) return { state, effect: null };
353
+ const told = (tone, text) => ({ state: { ...state, notice: note(tone, text, now) }, effect: null });
185
354
  if (state.busy) return told("info", `${state.busy.text} ${g.dash} one thing at a time`);
186
- if (!state.auto) return told("info", `the deck has not said whether Auto-switch is on yet`);
187
- return { state, effect: { type: "auto", enabled: state.auto.enabled !== true } };
355
+ if (!state.deck) return told("info", `Codex accounts switch through the deck ${g.dash} d starts one`);
356
+ if (row.issue?.blocks) {
357
+ if (row.issue.fix) return codexSigninOpen(state, row);
358
+ return told("err", `${accountName(row)}: ${row.issue.word} ${g.dash} ${row.issue.hint}`);
359
+ }
360
+ if (row.active) return told("ok", `Codex already runs on ${accountName(row)}`);
361
+ return { state, effect: { type: "switch", account: row } };
362
+ }
363
+
364
+ /** b on the Codex side: the account with the most room left, the deck's own
365
+ * "next" for Codex Auto-switch — selected, for enter to switch. */
366
+ function codexBestKey(state, rows, { now, g }) {
367
+ const can = rows.filter(r => !r.issue?.blocks && !r.disabled && r.headroom != null);
368
+ if (!can.length) return { state: { ...state, notice: note("err", `no Codex account has a reading of its room ${g.dash} every one is held out, signed out or unread`, now) }, effect: null };
369
+ const best = can.reduce((a, b) => (b.headroom > a.headroom || (b.headroom === a.headroom && b.active) ? b : a));
370
+ const at = rows.indexOf(best);
371
+ if (best.active) return { state: { ...state, codexAt: at, notice: note("ok", `Codex already runs on the most room: ${accountName(best)}`, now) }, effect: null };
372
+ return { state: { ...state, codexAt: at, notice: note("info", `enter switches Codex to ${accountName(best)} ${g.dash} the most room`, now) }, effect: null };
373
+ }
374
+
375
+ function codexAddKey(state, { now, g }) {
376
+ const told = (tone, text) => ({ state: { ...state, notice: note(tone, text, now) }, effect: null });
377
+ if (!state.deck) return told("info", `Codex accounts are added through the deck ${g.dash} d starts one`);
378
+ if (state.busy) return told("info", `${state.busy.text} ${g.dash} one thing at a time`);
379
+ return codexSigninOpen(state, null);
380
+ }
381
+
382
+ /** The Codex sign-in page, for `row` in place or for a new account. */
383
+ function codexSigninOpen(state, row) {
384
+ return { state: { ...state, menu: null, codexSignin: { id: row?.id ?? null, name: row ? accountName(row) : null, adding: !row, started: false, login: null } }, effect: null };
385
+ }
386
+
387
+ /** The keys while the Codex sign-in page is up. Nothing is typed here: the
388
+ * code goes into the browser's page. */
389
+ function codexSigninKey(state, str, key, { now, g }) {
390
+ const si = state.codexSignin;
391
+ const name = key.name ?? "";
392
+ const enter = name === "return" || name === "enter";
393
+ const back = name === "escape" || str === "q";
394
+ const stay = { state, effect: null };
395
+ const close = (effect = null) => ({ state: { ...state, codexSignin: null }, effect });
396
+ const login = si.login?.state;
397
+ if (!si.started || login === "failed" || login === "expired" || login === "cancelled") {
398
+ if (enter) return { state: { ...state, codexSignin: { ...si, started: true, login: null } }, effect: { type: "codex-signin-start" } };
399
+ return back ? close() : stay;
400
+ }
401
+ if (login === "done") return enter || back ? close({ type: "codex-signin-done" }) : stay;
402
+ // Leaving while it runs lets the deck's codex login go: no process stays behind.
403
+ if (back) return close(si.id ? { type: "codex-signin-cancel", id: si.id } : null);
404
+ return stay;
188
405
  }
189
406
 
190
407
  function addKey(state, { now, g }) {
@@ -199,6 +416,143 @@ function addKey(state, { now, g }) {
199
416
  return { state: { ...state, signin: openSignin() }, effect: null };
200
417
  }
201
418
 
419
+ // ── an account's actions ────────────────────────────────────────────────────
420
+ //
421
+ // The deck's ⋯ menu on an account row (AccountMenuPopover.tsx), item for item
422
+ // and by the same rules: hold out is offered on every account but the one in
423
+ // use, putting one back whenever it is out; home whenever the account has an
424
+ // address or already is home; remove asks first. Share and Projects stay in
425
+ // the deck: one hands over a login as text, the other is a chart. Each item
426
+ // has a key that works from the list too, so the menu is where they are
427
+ // learnt, not the only way to them.
428
+
429
+ /** The keys that reach an item straight from the list. */
430
+ const ACTION_KEYS = Object.freeze({ n: "rename", v: "move", o: "hold", h: "home", x: "remove" });
431
+
432
+ /** What claude-swap takes as an alias (cswap-admin.mjs ALIAS_OK). */
433
+ const ALIAS_OK = /^(?!-)[A-Za-z0-9 ._-]{1,64}$/;
434
+
435
+ /** The menu's items for `row`, in the deck's order — the Codex panel's, for
436
+ * a Codex account. */
437
+ export function menuItems(state, row) {
438
+ const home = homeSlot(state) === row.num;
439
+ if (row.codex) return codexMenuItems(row, { home, homeable: state.codexAuto?.ok === true });
440
+ const dead = !switchable(row) && !row.disabled;
441
+ return [
442
+ !row.active || dead ? { id: "switch", key: "enter", label: dead ? "Sign in again" : "Switch to this account" } : null,
443
+ { id: "rename", key: "n", label: "Rename…" },
444
+ { id: "move", key: "v", label: "Move to slot…" },
445
+ !row.active || row.disabled ? { id: "hold", key: "o", label: row.disabled ? "Put back in rotation" : "Hold out of rotation" } : null,
446
+ home || row.email ? { id: "home", key: "h", label: home ? "Remove home" : "Set as home" } : null,
447
+ { id: "remove", key: "x", label: "Remove…", danger: true },
448
+ ].filter(Boolean);
449
+ }
450
+
451
+ function menuOpen(state, row, view, { now, g }) {
452
+ if (!state.deck) {
453
+ return { state: { ...state, notice: note("info", `the account's actions go through the deck ${g.dash} d starts one`, now) }, effect: null };
454
+ }
455
+ if (state.busy) return { state: { ...state, notice: note("info", `${state.busy.text} ${g.dash} one thing at a time`, now) }, effect: null };
456
+ const draft = view === "rename" ? oneLine(row.alias) : "";
457
+ return { state: { ...state, menu: { num: row.num, id: row.codex ? row.id : null, view, at: 0, draft, error: null } }, effect: null };
458
+ }
459
+
460
+ /** An item chosen, from the menu or from the list. */
461
+ function menuAct(state, row, id, { now, g }) {
462
+ if (row.codex) {
463
+ if (id === "switch") return codexSwitchKey({ ...state, menu: null }, row, { now, g });
464
+ // Codex has no slots: the key says so rather than nothing.
465
+ if (id === "move") return { state: { ...state, notice: note("info", `Codex accounts have no slots ${g.dash} the list is the deck's order`, now) }, effect: null };
466
+ if (id === "home" && state.codexAuto?.ok !== true && homeSlot(state) !== row.num) {
467
+ return { state: { ...state, notice: note("info", `Codex Auto-switch is not read yet ${g.dash} home waits for it`, now) }, effect: null };
468
+ }
469
+ if (id === "remove" && !row.managed) {
470
+ return { state: { ...state, notice: note("info", `${accountName(row)} is Codex's own login ${g.dash} only accounts added in the deck can be removed`, now) }, effect: null };
471
+ }
472
+ } else if (id === "switch") return switchKey({ ...state, menu: null }, row, { now, g });
473
+ if (!state.deck || state.busy) return menuOpen(state, row, "menu", { now, g });
474
+ if (id === "signin" && row.codex) return codexSigninOpen(state, row);
475
+ if (id === "rename" || id === "move" || id === "remove") {
476
+ const open = menuOpen(state, row, id, { now, g });
477
+ // Back from a form lands on the item it came from.
478
+ const at = menuItems(state, row).findIndex(i => i.id === id);
479
+ return { ...open, state: { ...open.state, menu: { ...open.state.menu, at: Math.max(0, at) } } };
480
+ }
481
+ if (id === "hold") {
482
+ if (row.active && !row.disabled) {
483
+ return { state: { ...state, notice: note("info", `${accountName(row)} is in use ${g.dash} hold another account out, or switch first`, now) }, effect: null };
484
+ }
485
+ return { state, effect: { type: "hold", account: row, out: !row.disabled } };
486
+ }
487
+ if (id === "home") {
488
+ const home = homeSlot(state) === row.num;
489
+ if (!home && !row.email && !row.codex) return { state, effect: null };
490
+ return { state, effect: { type: "home", account: row, set: !home } };
491
+ }
492
+ return { state, effect: null };
493
+ }
494
+
495
+ function menuKey(state, str, key, { now, g }) {
496
+ const name = key.name ?? "";
497
+ const m = state.menu;
498
+ const row = menuRow(state, m);
499
+ // The account went — removed, or moved by somebody else — and the menu with it.
500
+ if (!row) return { state: { ...state, menu: null }, effect: null };
501
+ // While the deck works on what was asked, the menu waits for its answer.
502
+ if (state.busy) return { state, effect: null };
503
+ const set = (patch) => ({ state: { ...state, menu: { ...m, ...patch } }, effect: null });
504
+ const enter = name === "return" || name === "enter";
505
+ const typed = typeof str === "string" && str.length > 0 && !key.ctrl && !key.meta && !/[\x00-\x1f\x7f]/.test(str);
506
+
507
+ if (m.view === "menu") {
508
+ const items = menuItems(state, row);
509
+ if (name === "escape" || str === "q" || str === "m" || str === ".") return { state: { ...state, menu: null }, effect: null };
510
+ if (name === "up" || str === "k") return set({ at: (m.at - 1 + items.length) % items.length, error: null });
511
+ if (name === "down" || str === "j" || name === "tab") return set({ at: (m.at + 1) % items.length, error: null });
512
+ if (enter) return menuAct({ ...state, menu: { ...m, error: null } }, row, items[m.at]?.id ?? "", { now, g });
513
+ const hot = name === "delete" ? items.find(i => i.id === "remove") : items.find(i => i.key === str);
514
+ if (hot) {
515
+ const at = items.indexOf(hot);
516
+ return menuAct({ ...state, menu: { ...m, at, error: null } }, row, hot.id, { now, g });
517
+ }
518
+ return { state, effect: null };
519
+ }
520
+
521
+ const back = () => set({ view: "menu", draft: "", error: null });
522
+ if (m.view === "remove") {
523
+ if (str === "y" || str === "Y") return { state, effect: { type: "remove", account: row } };
524
+ return back();
525
+ }
526
+
527
+ // The two fields: an alias, a slot.
528
+ if (name === "escape") return back();
529
+ if (name === "backspace") return set({ draft: [...m.draft].slice(0, -1).join(""), error: null });
530
+ if (key.ctrl && name === "u") return set({ draft: "", error: null });
531
+ if (m.view === "rename") {
532
+ if (enter) {
533
+ const alias = m.draft.trim();
534
+ const trouble = row.codex ? codexNameTrouble(alias)
535
+ : alias && !ALIAS_OK.test(alias) ? "letters, digits, spaces and . _ - only, up to 64, not starting with -" : null;
536
+ if (trouble) return set({ error: trouble });
537
+ if (alias === oneLine(row.alias)) return { state: { ...state, menu: null }, effect: null };
538
+ return { state, effect: { type: "alias", account: row, alias } };
539
+ }
540
+ if (typed) return set({ draft: (m.draft + str).slice(0, row.codex ? 80 : 64), error: null });
541
+ return { state, effect: null };
542
+ }
543
+ if (m.view === "move") {
544
+ if (enter) {
545
+ const slot = Number(m.draft);
546
+ if (!Number.isInteger(slot) || slot < 1 || slot > 999) return set({ error: "a slot is a number from 1 to 999" });
547
+ if (slot === row.num) return { state: { ...state, menu: null, notice: note("ok", `${accountName(row)} is already in slot ${slot}`, now) }, effect: null };
548
+ return { state, effect: { type: "move", account: row, slot } };
549
+ }
550
+ if (typed && /^\d+$/.test(str)) return set({ draft: (m.draft + str).slice(0, 3), error: null });
551
+ return { state, effect: null };
552
+ }
553
+ return { state, effect: null };
554
+ }
555
+
202
556
 
203
557
  // ── the frame ───────────────────────────────────────────────────────────────
204
558
 
@@ -255,16 +609,22 @@ export function tuiFrame(state, { columns = 80, rows = 24, tone, g, unicode = tr
255
609
  const times = unicode ? "×" : "x";
256
610
  return { lines: fill([`${tone.muted}too small ${g.dash} ${MIN_COLUMNS}${times}${MIN_ROWS} at least${tone.reset}`]), top: state.top, run: null };
257
611
  }
258
- const look = { tone, g, unicode, now, links, skin: skinOf(profile ?? (tone.reset ? "ansi16" : "none")) };
259
- if (state.signin) {
260
- const page = signinLines(state, rows - 3, width, columns, look);
612
+ // `plain` is for text from elsewhere that is measured inside a panel: in
613
+ // ASCII it has to be ASCII before it is cut, or "…" grows past the border.
614
+ const look = { tone, g, unicode, now, links, plain, skin: skinOf(profile ?? (tone.reset ? "ansi16" : "none")) };
615
+ if (state.signin || state.codexSignin || lanInvitePage(state)) {
616
+ const page = state.signin ? signinLines(state, rows - 3, width, columns, look)
617
+ : state.codexSignin ? codexSigninLines(state, rows - 3, width, columns, look)
618
+ : lanInviteLines(state, rows - 3, width, columns, look);
261
619
  const run = page.run ? { ...page.run, row: page.run.row + 1 } : null;
262
620
  const lines = [titleBar(state, width, look), ...page.lines, noticeLine(state, look), footerLine(state, width, look)];
263
621
  return { lines: fill(lines, run), top: state.top, run };
264
622
  }
265
623
  const body = bodyLines(state, rows - 3, width, look);
266
624
  const lines = [titleBar(state, width, look), ...body.lines, noticeLine(state, look), footerLine(state, width, look)];
267
- return { lines: fill(state.help ? withHelp(lines, width, look) : lines), top: body.top, run: null };
625
+ const over = state.help ? withHelp(lines, width, look) : state.menu ? withMenu(lines, state, width, look)
626
+ : state.lanForm ? withLanForm(lines, state, width, look) : lines;
627
+ return { lines: fill(over), top: body.top, run: null };
268
628
  }
269
629
 
270
630
  // ── the look ────────────────────────────────────────────────────────────────
@@ -374,7 +734,21 @@ function gauge(pct, width, { tone, unicode, skin }) {
374
734
  const LIVE_MS = 10_000;
375
735
 
376
736
  function titleBar(state, width, { tone, g, unicode, now, skin }) {
377
- const left = ` ${tone.accent}${tone.bold}ccdeck${tone.reset} ${tone.bold}Claude accounts${tone.reset}`;
737
+ // Claude | Codex, the side shown lit, once the deck has a Codex side: a
738
+ // tab where the colours draw one, brackets where they do not.
739
+ const tab = (label, lit) => (lit
740
+ ? skin.select ? onBg(`${tone.bold} ${label} `, skin.select) : `${tone.bold}[${label}]${tone.reset}`
741
+ : `${tone.muted} ${label} ${tone.reset}`);
742
+ const sides = sidesOf(state);
743
+ // The Local network's tab counts the decks waiting for an answer here.
744
+ const asking = lanAsking(state);
745
+ const lanTab = (lit) => (!asking ? tab("Network", lit)
746
+ : lit ? tab(`Network ${asking}`, true)
747
+ : `${tone.muted} Network ${tone.reset}${tone.warn}${asking}${tone.reset} `);
748
+ const what = sides.length > 1
749
+ ? sides.map(side => (side === "claude" ? tab("Claude", state.side === "claude") : side === "codex" ? tab("Codex", onCodex(state)) : lanTab(onLan(state)))).join("")
750
+ : `${tone.bold}Claude accounts${tone.reset}`;
751
+ const left = ` ${tone.accent}${tone.bold}ccdeck${tone.reset} ${what}`;
378
752
  let right = "";
379
753
  if (state.deck) {
380
754
  const ago = state.readAt == null ? null : now - state.readAt;
@@ -406,10 +780,20 @@ function noticeLine(state, { tone, g, unicode, now }) {
406
780
 
407
781
  /** The keys, as many as fit, the ones that leave and explain kept to the last.
408
782
  * Each key is a cap where the terminal can draw one. */
409
- function footerLine(state, width, { tone, unicode, skin }) {
410
- const keys = state.signin ? signinKeys(state.signin) : [
783
+ function footerLine(state, width, look) {
784
+ const { tone, unicode, skin } = look;
785
+ const keys = state.signin ? signinKeys(state.signin) : state.codexSignin ? codexSigninKeys(state.codexSignin)
786
+ : lanInvitePage(state) ? lanInviteKeys() : onLan(state) ? [
787
+ ...lanFooterKeys(state, { now: look.now, unicode }),
788
+ ["l", "accounts"],
789
+ ["?", "keys"],
790
+ ["q", "quit"],
791
+ ] : [
411
792
  [unicode ? "↑↓" : "up/down", "move"],
412
793
  ["enter", "switch"],
794
+ ["m", "actions"],
795
+ ...(hasCodex(state) || onCodex(state) ? [["c", onCodex(state) ? "Claude" : "Codex"]] : []),
796
+ ...(hasLan(state) ? [["l", "network"]] : []),
413
797
  ["b", "best"],
414
798
  state.deck ? ["a", "auto-switch"] : ["d", "start the deck"],
415
799
  ["+", "add"],
@@ -422,7 +806,7 @@ function footerLine(state, width, { tone, unicode, skin }) {
422
806
  const list = keys.slice();
423
807
  // r goes first, then the moves, then b: the screen says what they do, and ?
424
808
  // and q are how to find out and how to leave.
425
- for (const drop of ["r", unicode ? "↑↓" : "up/down", "b", "+", "a", "d", "enter"]) {
809
+ for (const drop of ["r", unicode ? "↑↓" : "up/down", "b", "+", "a", "d", "m", "c", "l", "n", "x", "space", "enter"]) {
426
810
  if (textWidth(draw(list)) <= width) break;
427
811
  const i = list.findIndex(([k]) => k === drop);
428
812
  if (i !== -1) list.splice(i, 1);
@@ -433,14 +817,21 @@ function footerLine(state, width, { tone, unicode, skin }) {
433
817
  // ── the panels ──────────────────────────────────────────────────────────────
434
818
 
435
819
  function bodyLines(state, room, width, look) {
820
+ if (onLan(state)) return lanBody(state, room, width, look);
436
821
  const { tone, g, unicode } = look;
437
- const rows = accountsOf(state);
822
+ const rows = accountsOf(state, look.now);
438
823
  const home = homeSlot(state);
439
- const selected = rows[state.at] ?? null;
824
+ const at = atOf(state);
825
+ const selected = rows[at] ?? null;
440
826
  const inner = width - 4;
441
827
 
442
828
  let accounts;
443
- if (!state.roster) accounts = [`${tone.muted}reading the accounts${g.ellipsis}${tone.reset}`];
829
+ if (onCodex(state)) {
830
+ accounts = !state.deck ? [`${tone.muted}the Codex accounts are the deck's ${g.dash} d starts one${tone.reset}`]
831
+ : !state.codex ? [`${tone.muted}reading the Codex accounts${g.ellipsis}${tone.reset}`]
832
+ : !rows.length ? [`${tone.muted}no Codex accounts yet ${g.dash} + signs one in${tone.reset}`]
833
+ : accountRows(rows, { home, at, width: inner }, look);
834
+ } else if (!state.roster) accounts = [`${tone.muted}reading the accounts${g.ellipsis}${tone.reset}`];
444
835
  else if (!state.roster.ok && state.roster.reason === "no_accounts" && state.deck) {
445
836
  accounts = [`${tone.muted}no Claude accounts yet ${g.dash} + signs one in${tone.reset}`];
446
837
  } else if (!state.roster.ok) {
@@ -448,8 +839,8 @@ function bodyLines(state, room, width, look) {
448
839
  accounts = [].concat(state.trouble ?? "could not read the accounts")
449
840
  .flatMap((l, i) => wrap(String(l), inner).map(part => `${i ? tone.muted : tone.warn}${part}${tone.reset}`));
450
841
  } else if (!rows.length) accounts = [`${tone.muted}no Claude accounts yet${tone.reset}`];
451
- else accounts = accountRows(rows, { home, at: state.at, width: inner }, look);
452
- const listed = rows.length > 0 && Boolean(state.roster?.ok);
842
+ else accounts = accountRows(rows, { home, at, width: inner }, look);
843
+ const listed = rows.length > 0 && (onCodex(state) || Boolean(state.roster?.ok));
453
844
  const auto = autoSummary(state, look);
454
845
  const waiting = waitingOf(state);
455
846
 
@@ -460,30 +851,53 @@ function bodyLines(state, room, width, look) {
460
851
  let shown = accounts;
461
852
  let where = "";
462
853
  if (shownRows < accounts.length) {
463
- top = listed ? Math.min(Math.max(0, state.top, state.at - shownRows + 1), state.at, accounts.length - shownRows) : 0;
854
+ top = listed ? Math.min(Math.max(0, state.top, at - shownRows + 1), at, accounts.length - shownRows) : 0;
464
855
  shown = accounts.slice(top, top + shownRows);
465
856
  if (listed) where = `${tone.muted}${top + 1}${unicode ? "–" : "-"}${top + shown.length} of ${accounts.length}${tone.reset}`;
466
857
  }
467
858
  const lines = panel(width, height, {
468
- title: `${tone.bold}Accounts${tone.reset}`, footLeft: where, footRight: auto, lines: shown, selected: listed ? state.at - top : -1,
859
+ title: `${tone.bold}Accounts${tone.reset}`, footLeft: where, footRight: auto, lines: shown, selected: listed ? at - top : -1,
469
860
  }, look);
470
861
  return { lines, top };
471
862
  };
472
863
 
864
+ // The waiting sessions have a panel only while somebody waits: the rest of
865
+ // the time the room is the machine's, and the count of the sessions at work
866
+ // is on its foot.
867
+ const someoneWaits = waiting.list.length > 0;
868
+ const working = waiting.read && !someoneWaits ? waiting.working : 0;
869
+ const machineFor = (panelW, height) => panel(panelW, height, machinePanel(state.machine, {
870
+ width: panelW - 4, height: height - 2, look, working, deck: Boolean(state.deck), starting: state.starting,
871
+ view: state.machineView, history: state.history,
872
+ }), look);
873
+ // What the charts ask for before the panels above them give way; before the
874
+ // first reading the panel has one line to say so, and asks for no more.
875
+ const chartsWant = state.machine?.count > 0 ? 6 : 1;
876
+
473
877
  const stacked = () => {
474
878
  const detail = selected ? detailPanel(selected, home, inner, look) : null;
475
879
  let accountRoom = accounts.length;
476
880
  let showDetail = Boolean(detail);
477
881
  let max = waiting.list.length;
478
882
  let asks = true;
479
- let showWaiting = true;
883
+ let showWaiting = someoneWaits || !state.deck;
884
+ let machineMin = chartsWant;
885
+ let showMachine = Boolean(state.deck);
480
886
  const sessions = () => sessionsPanel(state, waiting, { max, asks, width: inner }, look);
481
- const need = () => 2 + accountRoom + (showDetail ? 2 + detail.lines.length : 0) + (showWaiting ? 2 + sessions().lines.length : 0);
887
+ const need = () => 2 + accountRoom + (showDetail ? 2 + detail.lines.length : 0)
888
+ + (showWaiting ? 2 + sessions().lines.length : 0) + (showMachine ? 2 + machineMin : 0);
482
889
  const giveWay = [
483
890
  () => max > 1 && (max--, true),
484
891
  () => showDetail && !(showDetail = false),
485
- () => asks && waiting.list.length > 0 && !(asks = false),
892
+ () => asks && someoneWaits && !(asks = false),
893
+ () => showMachine && machineMin > 3 && (machineMin--, true),
486
894
  () => accountRoom > 3 && (accountRoom--, true),
895
+ () => showMachine && machineMin > 1 && (machineMin--, true),
896
+ // Somebody waiting on you is the one thing here that is somebody's
897
+ // work stopped: the charts go first.
898
+ () => showMachine && someoneWaits && !(showMachine = false),
899
+ () => showWaiting && showMachine && !(showWaiting = false),
900
+ () => showMachine && !(showMachine = false),
487
901
  () => showWaiting && !(showWaiting = false),
488
902
  () => accountRoom > 1 && (accountRoom--, true),
489
903
  ];
@@ -492,16 +906,19 @@ function bodyLines(state, room, width, look) {
492
906
  while (showWaiting && max < waiting.list.length && (max++, need() > room ? (max--, false) : true));
493
907
  // The last panel takes the rows left over.
494
908
  const spare = Math.max(0, room - need());
495
- const list = accountsPanel(accountRoom, 2 + accountRoom + (showDetail || showWaiting ? 0 : spare));
909
+ const list = accountsPanel(accountRoom, 2 + accountRoom + (showDetail || showWaiting || showMachine ? 0 : spare));
496
910
  const out = [...list.lines];
497
- if (showDetail) out.push(...panel(width, 2 + detail.lines.length + (showWaiting ? 0 : spare), detail, look));
911
+ if (showDetail) out.push(...panel(width, 2 + detail.lines.length + (showWaiting || showMachine ? 0 : spare), detail, look));
498
912
  if (showWaiting) {
499
913
  const s = sessions();
500
- out.push(...panel(width, 2 + s.lines.length + spare, s, look));
914
+ out.push(...panel(width, 2 + s.lines.length + (showMachine ? 0 : spare), s, look));
501
915
  }
916
+ if (showMachine) out.push(...machineFor(width, 2 + machineMin + spare));
502
917
  return { lines: out, top: list.top };
503
918
  };
504
919
 
920
+ // From WIDE_COLUMNS, while somebody waits: the detail and the waiting side by
921
+ // side under the list, and the machine under both.
505
922
  const sideBySide = () => {
506
923
  const leftW = Math.floor((width - 1) / 2);
507
924
  const rightW = width - 1 - leftW;
@@ -509,28 +926,93 @@ function bodyLines(state, room, width, look) {
509
926
  let accountRoom = accounts.length;
510
927
  let max = waiting.list.length;
511
928
  let asks = true;
929
+ let machineMin = chartsWant;
930
+ let showMachine = Boolean(state.deck);
512
931
  const sessions = () => sessionsPanel(state, waiting, { max, asks, width: rightW - 4 }, look);
513
- const need = () => 2 + accountRoom + 2 + Math.max(detail.lines.length, sessions().lines.length);
932
+ const pair = () => 2 + Math.max(detail.lines.length, sessions().lines.length);
933
+ const need = () => 2 + accountRoom + pair() + (showMachine ? 2 + machineMin : 0);
514
934
  const giveWay = [
515
935
  () => max > 1 && (max--, true),
936
+ () => showMachine && machineMin > 3 && (machineMin--, true),
516
937
  () => accountRoom > 3 && (accountRoom--, true),
517
- () => asks && waiting.list.length > 0 && !(asks = false),
938
+ () => asks && !(asks = false),
939
+ () => showMachine && machineMin > 1 && (machineMin--, true),
518
940
  ];
519
941
  while (need() > room && giveWay.some(step => step()));
520
942
  if (need() > room) return null;
521
943
  const list = accountsPanel(accountRoom, 2 + accountRoom);
522
- const height = room - list.lines.length;
944
+ const height = showMachine ? pair() : room - list.lines.length;
523
945
  const left = panel(leftW, height, detail, look);
524
946
  const right = panel(rightW, height, sessions(), look);
525
- return { lines: [...list.lines, ...left.map((l, i) => `${l} ${right[i]}`)], top: list.top };
947
+ const out = [...list.lines, ...left.map((l, i) => `${l} ${right[i]}`)];
948
+ if (showMachine) out.push(...machineFor(width, room - out.length));
949
+ return { lines: out, top: list.top };
526
950
  };
527
951
 
528
- const laid = (width >= WIDE_COLUMNS - 1 && selected && sideBySide()) || stacked();
952
+ const laid = (width >= WIDE_COLUMNS - 1 && selected && someoneWaits && sideBySide()) || stacked();
529
953
  return { lines: [...laid.lines, ...pad(room - laid.lines.length)].slice(0, room), top: laid.top };
530
954
  }
531
955
 
956
+ /**
957
+ * The Local network in one panel: the list, scrolled so the selection is on
958
+ * it, its bottom border saying which rows show and when the deck last checked
959
+ * its paired decks. The machine keeps whatever room the list leaves it.
960
+ */
961
+ function lanBody(state, room, width, look) {
962
+ const { tone, g, unicode } = look;
963
+ const content = state.lan ? lanLines(state, width - 4, look)
964
+ : { lines: [`${tone.muted}${state.deck ? `reading the Local network${g.ellipsis}` : `Local network is the deck's ${g.dash} d starts one`}${tone.reset}`], selected: -1, foot: "" };
965
+ const total = content.lines.length;
966
+ const chartsWant = state.machine?.count > 0 ? 6 : 1;
967
+ const showMachine = Boolean(state.deck) && room - (2 + total) >= 2 + chartsWant;
968
+ const listRows = showMachine ? total : Math.max(1, room - 2);
969
+ let top = 0;
970
+ let shown = content.lines;
971
+ let where = "";
972
+ if (listRows < total) {
973
+ const at = Math.max(0, content.selected);
974
+ top = Math.max(0, Math.min(Math.max(state.top, at - listRows + 1), at, total - listRows));
975
+ shown = content.lines.slice(top, top + listRows);
976
+ where = `${tone.muted}${top + 1}${unicode ? "–" : "-"}${top + shown.length} of ${total}${tone.reset}`;
977
+ }
978
+ const out = panel(width, 2 + listRows, {
979
+ title: `${tone.bold}Local network${tone.reset}`,
980
+ footLeft: where,
981
+ footRight: content.foot ? `${tone.muted}${content.foot}${tone.reset}` : "",
982
+ lines: shown,
983
+ selected: content.selected >= 0 ? content.selected - top : -1,
984
+ }, look);
985
+ if (showMachine) {
986
+ const waiting = waitingOf(state);
987
+ const height = room - out.length;
988
+ out.push(...panel(width, height, machinePanel(state.machine, {
989
+ width: width - 4, height: height - 2, look, working: waiting.read && !waiting.list.length ? waiting.working : 0,
990
+ deck: true, starting: state.starting, view: state.machineView, history: state.history,
991
+ }), look));
992
+ }
993
+ return { lines: [...out, ...pad(room - out.length)].slice(0, room), top };
994
+ }
995
+
996
+ /** Local network's form, in a box over the screen as an account's menu is. */
997
+ function withLanForm(base, state, width, look) {
998
+ const boxW = Math.min(width - 2, 64);
999
+ const spec = lanFormSpec(state, boxW - 4, look);
1000
+ const height = Math.min(base.length - 2, spec.lines.length + 2);
1001
+ return overlay(base, panel(boxW, height, spec, look), boxW, width, look);
1002
+ }
1003
+
1004
+ /** The Local network as read, or none: the screen leaves its tab when it goes. */
1005
+ export function withLan(state, lan, { missing = false } = {}) {
1006
+ const next = { ...state, lan, lanMissing: missing };
1007
+ if (!lan && onLan(state)) return { ...next, side: state.lanFrom === "codex" && hasCodex(state) ? "codex" : "claude", top: 0, lanForm: null };
1008
+ return next;
1009
+ }
1010
+
532
1011
  const LANE_IDS = ["five_hour", "seven_day"];
533
1012
 
1013
+ /** The held-out mark: ASCII's, and Unicode's pause-like double bar. */
1014
+ const HELD_MARK = ["=", "‖"];
1015
+
534
1016
  /** A lane's columns: its label, the meter and the percentage, and the reset —
535
1017
  * all of it (A), without the reset (B), or the percentage alone (C). */
536
1018
  const laneWidth = (kind, bar, labelW) => (kind === "A" ? labelW + bar + 17 : kind === "B" ? labelW + bar + 6 : labelW + 5);
@@ -551,10 +1033,11 @@ function accountRows(rows, { home, at, width }, look) {
551
1033
  const emailOf = (a) => oneLine(a.email) || "(no email)";
552
1034
  const numW = Math.max(...rows.map(a => String(a.num).length));
553
1035
  const showHome = home != null && rows.some(a => a.num === home);
1036
+ const showHeld = rows.some(a => a.disabled);
554
1037
  const labelW = Math.max(2, ...rows.flatMap(a => (a.lanes ?? []).filter(l => LANE_IDS.includes(l.id)).map(l => textWidth(oneLine(l.label)))));
555
1038
  const longest = Math.max(...rows.map(a => textWidth(emailOf(a))));
556
1039
  const aliasW = Math.min(14, Math.max(0, ...rows.map(a => textWidth(aliasOf(a)))));
557
- const lead = (alias) => 2 + 2 + numW + 1 + (showHome ? 2 : 0) + (alias ? aliasW + 2 : 0);
1040
+ const lead = (alias) => 2 + 2 + numW + 1 + (showHome ? 2 : 0) + (showHeld ? 2 : 0) + (alias ? aliasW + 2 : 0);
558
1041
  const lanesW = ({ kind, bar }) => 2 * laneWidth(kind, bar, labelW) + 3;
559
1042
  const want = Math.min(longest, 20);
560
1043
  const tries = [["A", 10], ["A", 8], ["A", 6], ["B", 10], ["B", 8], ["B", 6], ["B", 4], ["C", 0]];
@@ -575,11 +1058,14 @@ function accountRows(rows, { home, at, width }, look) {
575
1058
  const mark = a.active ? `${tone.ok}${g.pulse}${tone.reset}` : " ";
576
1059
  const num = `${a.active ? tone.bold : ""}${padStart(String(a.num), numW)}${tone.reset}`;
577
1060
  const house = !showHome ? "" : a.num === home ? `${tone.accent}${unicode ? "⌂" : "H"}${tone.reset} ` : " ";
1061
+ // Held out of rotation: a mark of its own, the usage still beside it —
1062
+ // it is still an account to switch to, and the room it has still counts.
1063
+ const held = !showHeld ? "" : a.disabled ? `${tone.warn}${HELD_MARK[unicode ? 1 : 0]}${tone.reset} ` : " ";
578
1064
  const alias = pick.alias ? `${tone.accent}${padEnd(clip(aliasOf(a), aliasW, g.ellipsis), aliasW)}${tone.reset} ` : "";
579
1065
  const email = `${a.disabled ? tone.muted : ""}${padEnd(clip(emailOf(a), emailW, g.ellipsis), emailW)}${tone.reset}`;
580
- const trouble = accountTrouble(a);
1066
+ const trouble = troubleOf(a);
581
1067
  const status = trouble ? `${tone[trouble.tone]}${trouble.word}${tone.reset}` : laneCells(a, pick, labelW, look);
582
- return `${pointer} ${mark} ${num} ${house}${alias}${email} ${status}`;
1068
+ return `${pointer} ${mark} ${num} ${house}${held}${alias}${email} ${status}`;
583
1069
  });
584
1070
  }
585
1071
 
@@ -601,10 +1087,22 @@ function laneCells(a, { kind, bar }, labelW, look) {
601
1087
  /** What the trouble on an account means for a switch to it. */
602
1088
  const TROUBLE_SAYS = {
603
1089
  "sign-in needed": "its login has expired; enter signs it in again, keeping its slot, alias and history",
604
- disabled: "kept out of Auto-switch's rotation; enter still switches to it",
605
1090
  "not collecting usage": "nothing has been read for it in half a day, so these are its last numbers",
606
1091
  };
607
1092
 
1093
+ /** What is wrong with a row, for the list and the detail: a Codex account's
1094
+ * issue, or what a Claude account's trouble is called. */
1095
+ const troubleOf = (a) => (a.codex ? (a.issue ? { word: a.issue.word, tone: a.issue.tone } : null) : accountTrouble({ ...a, disabled: false }));
1096
+
1097
+ /** The deck's hint, run on after a dash: its first letter small. */
1098
+ const sentenceLower = (s) => (s ? s[0].toLowerCase() + s.slice(1) : "");
1099
+
1100
+ /** A Codex plan as its name reads: Plus, Pro, Team. */
1101
+ const planWord = (plan) => oneLine(plan).replace(/^./, c => c.toUpperCase());
1102
+
1103
+ /** What holding an account out means, the deck's own word for it. */
1104
+ const HELD_SAYS = "held out of rotation — Auto-switch passes over it; enter still switches to it, o puts it back";
1105
+
608
1106
  /** The selected account at length: what it is, what is wrong, every lane. */
609
1107
  function detailPanel(a, home, width, look) {
610
1108
  const { tone, g, now } = look;
@@ -612,14 +1110,17 @@ function detailPanel(a, home, width, look) {
612
1110
  const tags = [
613
1111
  a.active ? `${tone.ok}in use${tone.reset}` : "",
614
1112
  a.num === home ? `${tone.accent}home${tone.reset}` : "",
1113
+ a.disabled ? `${tone.warn}held out${tone.reset}` : "",
1114
+ a.plan ? `${tone.muted}${planWord(a.plan)}${tone.reset}` : "",
615
1115
  ].filter(Boolean);
616
1116
  const read = a.fetchedAt != null ? `${a.stale ? tone.warn : tone.muted}read ${sinceLabel(now - a.fetchedAt)} ago${tone.reset}` : "";
617
1117
  const note = [[...tags, read].filter(Boolean).join(sep), tags.join(sep)];
618
1118
  const title = `${tone.bold}${accountName(a)}${tone.reset}`;
619
- const trouble = accountTrouble(a);
620
- const said = trouble
621
- ? wrap(`${trouble.word} ${g.dash} ${TROUBLE_SAYS[trouble.word] ?? ""}`, width).map(l => `${tone[trouble.tone]}${l}${tone.reset}`)
622
- : [];
1119
+ const trouble = troubleOf(a);
1120
+ const said = [
1121
+ ...(trouble ? wrap(`${trouble.word} ${g.dash} ${a.codex ? sentenceLower(a.issue.hint) : TROUBLE_SAYS[trouble.word] ?? ""}`, width).map(l => `${tone[trouble.tone]}${l}${tone.reset}`) : []),
1122
+ ...(a.disabled ? wrap(HELD_SAYS.replace("—", g.dash), width).map(l => `${tone.muted}${l}${tone.reset}`) : []),
1123
+ ];
623
1124
  const lanes = a.lanes ?? [];
624
1125
  if (!lanes.length) {
625
1126
  return { title, note, lines: [...said, `${tone.muted}${a.fetchedAt == null ? "not read yet" : "no usage reading"}${tone.reset}`] };
@@ -650,7 +1151,7 @@ const NEXT_WORDS = { room: "most room", weekly: "soonest weekly reset" };
650
1151
 
651
1152
  /** Auto-switch in the list's bottom border, longest first. */
652
1153
  function autoSummary(state, { tone, g }) {
653
- const auto = state.deck ? state.auto : null;
1154
+ const auto = !state.deck ? null : onCodex(state) ? state.codexAuto : state.auto;
654
1155
  if (!auto || auto.ok === false) return "";
655
1156
  const sep = ` ${g.bullet} `;
656
1157
  const external = auto.external ? `${sep}${tone.warn}your own cswap auto is running${tone.reset}` : "";
@@ -1015,6 +1516,94 @@ function signinLines(state, room, width, columns, look) {
1015
1516
  return out(lines, run && run.row + run.rows <= room ? run : null);
1016
1517
  }
1017
1518
 
1519
+ // ── the Codex sign-in ───────────────────────────────────────────────────────
1520
+
1521
+ /** The footer's keys while the Codex sign-in page is up. */
1522
+ function codexSigninKeys(si) {
1523
+ const login = si.login?.state;
1524
+ if (!si.started) return [["enter", "get the code"], ["esc", "back"]];
1525
+ if (login === "done") return [["enter", "done"]];
1526
+ if (login === "failed" || login === "expired" || login === "cancelled") return [["enter", "try again"], ["esc", "back"]];
1527
+ return [["esc", "cancel"]];
1528
+ }
1529
+
1530
+ /**
1531
+ * The Codex sign-in, a page of its own: what it does before it starts, then
1532
+ * the link — edge to edge, so the terminal makes it one link — and the code
1533
+ * to type on the page it opens, until codex says the sign-in is done.
1534
+ */
1535
+ function codexSigninLines(state, room, width, columns, look) {
1536
+ const { tone, g, unicode, now, links } = look;
1537
+ const si = state.codexSignin;
1538
+ const inner = width - 4;
1539
+ const title = `${tone.bold}${si.adding ? "Add a Codex account" : `Sign in: ${oneLine(si.name)}`}${tone.reset}`;
1540
+ const quiet = (l) => `${tone.muted}${l}${tone.reset}`;
1541
+ const say = (text, t = tone.muted) => wrap(text, width - 2).map(l => ` ${t}${l}${tone.reset}`);
1542
+ const out = (lines, run = null) => ({ lines: [...lines, ...pad(room - lines.length)].slice(0, room), run });
1543
+ const box = (lines, note = "") => panel(width, Math.min(room, lines.length + 2), { title, note, lines }, look);
1544
+ const login = si.login ?? {};
1545
+
1546
+ if (!si.started) {
1547
+ return out(box([
1548
+ ...wrap(`codex on this machine makes a sign-in link and a one-time code. Open the link in any browser ${g.dash} a phone works ${g.dash} and type the code there. It finishes by itself.`, inner),
1549
+ "",
1550
+ ...wrap(si.adding
1551
+ ? "The deck adds a new account for it. Codex keeps running on the account it runs on now."
1552
+ : "Signing in replaces this account's login in place: it keeps its name, settings and history.", inner),
1553
+ ].map(quiet)));
1554
+ }
1555
+ const frames = spinnerFrames(unicode);
1556
+ const spin = frames[Math.floor(now / 100) % frames.length];
1557
+ if (login.state === "done") {
1558
+ const who = oneLine(si.account);
1559
+ return out(box([
1560
+ `${tone.ok}${g.ok}${tone.reset} ${tone.bold}Signed in${tone.reset}${who ? quiet(` as ${who}`) : ""}`,
1561
+ ...wrap(si.adding ? "The account is in the list. Codex keeps running on the account it ran on." : "Its login is replaced; the account keeps its name, settings and history.", inner - 3).map(l => quiet(` ${l}`)),
1562
+ ]));
1563
+ }
1564
+ if (login.state === "failed" || login.state === "expired" || login.state === "cancelled") {
1565
+ const why = CODEX_LOGIN_FAILED[login.reason] ?? (login.state === "cancelled" ? "the sign-in was cancelled" : oneLine(login.reason) || "the deck gave no reason");
1566
+ return out(box(wrap(sentence(why), inner - 3).map((l, i) => `${tone.err}${i ? " " : `${g.fail} `}${l}${tone.reset}`)));
1567
+ }
1568
+ const url = safeLink(login.url);
1569
+ const code = oneLine(login.code);
1570
+ if (login.state !== "waiting" || !code) {
1571
+ return out(box([`${tone.accent}${spin}${tone.reset} ${quiet(`asking codex on this machine for a sign-in code${g.ellipsis}`)}`]));
1572
+ }
1573
+
1574
+ const left = typeof login.expiresAt === "number" ? Math.ceil((login.expiresAt - (now + (state.skew ?? 0))) / 60_000) : null;
1575
+ const clock = left >= 1 ? quiet(`${left} min left`) : "";
1576
+ const pieces = [];
1577
+ if (url) for (let i = 0; i < url.length; i += columns) pieces.push(url.slice(i, i + columns));
1578
+ const blocks = {
1579
+ box: [...box([`${tone.accent}${spin}${tone.reset} waiting for the code to be entered`], clock), ""],
1580
+ open: say("Open this link in any browser:", ""),
1581
+ link: url ? pieces : say(`the deck's sign-in link could not be shown ${g.dash} sign in from its Accounts panel`, tone.warn),
1582
+ gap: [""],
1583
+ enter: say("and type this code on its page:", ""),
1584
+ code: [` ${tone.accent}${tone.bold}${code}${tone.reset}`],
1585
+ after: ["", ...say("It finishes by itself once the page takes the code. Nothing is typed here.")],
1586
+ };
1587
+ const order = ["box", "open", "link", "gap", "enter", "code", "after"];
1588
+ const shownBlocks = new Set(order);
1589
+ const height = () => order.filter(b => shownBlocks.has(b)).reduce((n, b) => n + blocks[b].length, 0);
1590
+ for (const drop of ["after", "gap", "box"]) {
1591
+ if (height() <= room) break;
1592
+ shownBlocks.delete(drop);
1593
+ }
1594
+ if (height() > room) blocks.link = say(`make the window taller to see the sign-in link ${g.dash} it needs ${pieces.length} rows`, tone.warn);
1595
+ const lines = [];
1596
+ let run = null;
1597
+ for (const b of order) {
1598
+ if (!shownBlocks.has(b)) continue;
1599
+ if (b === "link" && url && blocks.link === pieces) {
1600
+ run = { row: lines.length, rows: pieces.length, full: url.length % columns === 0, text: `${tone.accent}${link(url, url, links)}${tone.reset}` };
1601
+ }
1602
+ lines.push(...blocks[b]);
1603
+ }
1604
+ return out(lines, run && run.row + run.rows <= room ? run : null);
1605
+ }
1606
+
1018
1607
  // ── help ────────────────────────────────────────────────────────────────────
1019
1608
 
1020
1609
  /** The keys, in a box over the screen, the screen under it faded. */
@@ -1023,44 +1612,130 @@ function withHelp(base, width, look) {
1023
1612
  const keys = [
1024
1613
  [unicode ? "↑ ↓ j k" : "up down j k", "move"],
1025
1614
  ["1-9", "the account in that slot"],
1026
- ["enter s", "switch to the selected account, or sign it in again"],
1615
+ ["enter s", "switch to it, or sign it in again"],
1616
+ ["m", "its actions, as in the deck's account menu"],
1617
+ ["h o", `set or clear home ${g.bullet} hold out, or put back`],
1618
+ ["n v x", `rename ${g.bullet} move to another slot ${g.bullet} remove`],
1027
1619
  ["b", "select the account Auto-switch would pick"],
1028
1620
  ["a", "turn Auto-switch on or off"],
1029
- ["+", "add a Claude account, signed in from any browser"],
1621
+ ["+", "add an account, signed in from any browser"],
1030
1622
  ["r", "read the usage again"],
1623
+ [unicode ? "c l ← →" : "c l", `Claude's or Codex's accounts ${g.bullet} Local network`],
1624
+ ["t", "the machine: live, or the last hours"],
1031
1625
  ["d", "start the deck, when none is running"],
1032
1626
  ["ctrl+l", "draw the screen again"],
1033
1627
  ["q esc", "quit"],
1034
1628
  ];
1035
1629
  const marks = [
1036
- [g.pulse, "the account in use"],
1037
- [unicode ? "⌂" : "H", "Auto-switch's home account"],
1630
+ [g.pulse, "in use"],
1631
+ [unicode ? "⌂" : "H", "Auto-switch's home"],
1632
+ [HELD_MARK[unicode ? 1 : 0], "held out of rotation"],
1038
1633
  ];
1039
1634
  const boxW = Math.min(width - 2, 66);
1040
1635
  const inner = boxW - 4;
1041
1636
  const keyW = Math.max(...keys.map(([k]) => textWidth(k))) + 3;
1042
1637
  const entry = ([k, what]) => wrap(what, Math.max(8, inner - keyW))
1043
1638
  .map((part, i) => `${tone.bold}${padEnd(i ? "" : k, keyW)}${tone.reset}${tone.muted}${part}${tone.reset}`);
1639
+ // The marks read as one legend line while it fits, one per line when it does not.
1640
+ const legend = marks.map(([k, what]) => `${k} ${what}`).join(" ");
1044
1641
  const lines = [
1045
1642
  ...keys.flatMap(entry),
1046
1643
  "",
1047
- ...marks.flatMap(entry),
1644
+ ...(textWidth(legend) <= inner
1645
+ ? [marks.map(([k, what]) => `${tone.bold}${k}${tone.reset} ${tone.muted}${what}${tone.reset}`).join(" ")]
1646
+ : marks.flatMap(entry)),
1048
1647
  "",
1049
1648
  ...wrap("A switch moves the live Claude login. Every running Claude session uses the new account from its next message.", inner)
1050
1649
  .map(l => `${tone.muted}${l}${tone.reset}`),
1051
1650
  ];
1052
1651
  const height = Math.min(base.length - 2, lines.length + 2);
1053
1652
  const box = panel(boxW, height, { title: `${tone.bold}Keys${tone.reset}`, footRight: `${tone.muted}any key closes${tone.reset}`, lines }, look);
1653
+ return overlay(base, box, boxW, width, look);
1654
+ }
1655
+
1656
+ /** `box`, `boxW` wide, in the middle of the screen, the screen under it faded. */
1657
+ function overlay(base, box, boxW, width, { tone, skin }) {
1054
1658
  const x = Math.floor((width - boxW) / 2);
1055
- const y = Math.max(1, Math.floor((base.length - height) / 2));
1659
+ const y = Math.max(1, Math.floor((base.length - box.length) / 2));
1056
1660
  const faded = (s) => (s && skin.faded ? `${skin.faded}${s}${tone.reset}` : s);
1057
1661
  return base.map((line, i) => {
1058
1662
  const plain = stripAnsi(line);
1059
- if (i < y || i >= y + height) return faded(plain);
1663
+ if (i < y || i >= y + box.length) return faded(plain);
1060
1664
  return `${faded(sliceCells(plain, 0, x))}${onBg(box[i - y], skin.raised)}${faded(sliceCells(plain, x + boxW, width))}`;
1061
1665
  });
1062
1666
  }
1063
1667
 
1668
+ // ── an account's menu, drawn ────────────────────────────────────────────────
1669
+
1670
+ /** The menu, or the one form an item turned it into, in a box over the screen. */
1671
+ function withMenu(base, state, width, look) {
1672
+ const { tone, g, unicode } = look;
1673
+ const m = state.menu;
1674
+ const row = menuRow(state, m);
1675
+ if (!row) return base;
1676
+ const boxW = Math.min(width - 2, 60);
1677
+ const inner = boxW - 4;
1678
+ const say = (text, code = tone.muted) => wrap(text, inner).map(l => `${code}${l}${tone.reset}`);
1679
+ const refused = m.error ? wrap(m.error, inner - 3).map((l, i) => `${tone.err}${i ? " " : `${g.fail} `}${l}${tone.reset}`) : [];
1680
+ const caret = `${tone.accent}${unicode ? "▏" : "_"}${tone.reset}`;
1681
+ const field = (text) => `${tone.accent}${unicode ? "›" : ">"}${tone.reset} ${text}${caret}`;
1682
+ const name = accountName(row);
1683
+ let spec;
1684
+ if (m.view === "menu") {
1685
+ const items = menuItems(state, row);
1686
+ const keyW = Math.max(...items.map(i => textWidth(i.key)));
1687
+ const lines = [];
1688
+ let selected = -1;
1689
+ items.forEach((item, i) => {
1690
+ if (item.danger) lines.push(`${tone.muted}${(unicode ? "─" : "-").repeat(inner)}${tone.reset}`);
1691
+ if (i === m.at) selected = lines.length;
1692
+ const pointer = i === m.at ? `${tone.accent}${tone.bold}${unicode ? "›" : ">"}${tone.reset}` : " ";
1693
+ const room = inner - 2 - keyW - 2;
1694
+ const shown = padEnd(clip(item.label, room, g.ellipsis), room);
1695
+ lines.push(`${pointer} ${item.danger ? tone.err : ""}${shown}${tone.reset} ${tone.muted}${padStart(item.key, keyW)}${tone.reset}`);
1696
+ });
1697
+ spec = {
1698
+ title: `${tone.bold}${name}${tone.reset}`,
1699
+ footRight: [`${tone.muted}enter chooses ${g.bullet} esc closes${tone.reset}`, `${tone.muted}esc closes${tone.reset}`],
1700
+ lines: [...lines, ...refused], selected,
1701
+ };
1702
+ } else if (m.view === "rename") {
1703
+ spec = {
1704
+ title: `${tone.bold}Rename ${name}${tone.reset}`,
1705
+ footRight: [`${tone.muted}enter saves ${g.bullet} esc back${tone.reset}`, `${tone.muted}esc back${tone.reset}`],
1706
+ lines: [...say(row.codex ? "A short name to show instead of the email. Empty takes it away." : "A short name the list shows beside the address. Empty takes it away."), "", field(m.draft), ...refused],
1707
+ };
1708
+ } else if (m.view === "move") {
1709
+ const slot = Number(m.draft);
1710
+ const holder = m.draft ? accountsOf(state).find(a => a.num === slot) : null;
1711
+ const hint = !m.draft ? `the slot numbers are the list's ${g.dash} 1 to 9 select them there`
1712
+ : holder && holder.num === row.num ? `it is already in slot ${slot}`
1713
+ : holder ? `slot ${slot} holds ${accountName(holder)} ${g.dash} the two trade places`
1714
+ : `slot ${slot} is free`;
1715
+ spec = {
1716
+ title: `${tone.bold}Move ${name}${tone.reset}`,
1717
+ footRight: [`${tone.muted}enter moves ${g.bullet} esc back${tone.reset}`, `${tone.muted}esc back${tone.reset}`],
1718
+ lines: [...say("To slot:", ""), field(m.draft), ...say(hint), ...refused],
1719
+ };
1720
+ } else {
1721
+ spec = {
1722
+ title: `${tone.err}${tone.bold}Remove ${name}?${tone.reset}`,
1723
+ footRight: `${tone.muted}any other key keeps it${tone.reset}`,
1724
+ lines: [
1725
+ ...(row.codex
1726
+ ? say("The deck takes this account out. Its sign-in and history are kept aside in the deck's data folder, not deleted.", "")
1727
+ : say("claude-swap deletes this account's stored login. This cannot be undone.", "")),
1728
+ ...(row.active && !row.codex ? say("It is the account in use: Claude Code stays signed in to it until the next switch.") : []),
1729
+ "",
1730
+ `${tone.bold}y${tone.reset} ${tone.muted}removes it${tone.reset}`,
1731
+ ...refused,
1732
+ ],
1733
+ };
1734
+ }
1735
+ const height = Math.min(base.length - 2, spec.lines.length + 2);
1736
+ return overlay(base, panel(boxW, height, spec, look), boxW, width, look);
1737
+ }
1738
+
1064
1739
  function wrap(text, width) {
1065
1740
  const out = [];
1066
1741
  let line = "";