pantheon-opencode 2.0.0-beta.3 → 2.0.0-beta.5

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.
@@ -24,7 +24,7 @@ import {
24
24
  writeFileSync,
25
25
  } from 'node:fs'
26
26
  import { dirname, join } from 'node:path'
27
- import type { SettingsRow, SettingsSection } from './pantheon-settings-state.ts'
27
+ import type { SettingsRow, SettingsTab } from './pantheon-settings-state.ts'
28
28
 
29
29
  /** Mirrors routing.yml `presets:` — drift-pinned against loadPresetDefs(). */
30
30
  export const PRESET_NAMES: readonly string[] = Object.freeze([
@@ -111,33 +111,32 @@ export interface PresetSectionInput {
111
111
  projectName: string | null
112
112
  }
113
113
 
114
- export function buildPresetSection(input: PresetSectionInput): SettingsSection {
115
- const rows: SettingsRow[] = PRESET_NAMES.map((name) => ({
116
- id: `preset.${name}`,
117
- title: name,
118
- description: input.globalName === name ? 'active (global)' : 'enter: activate globally',
119
- disabled: input.globalName === name,
120
- }))
121
- rows.push({
122
- id: 'preset.none',
123
- title: 'none',
124
- description: 'clear the global marker — opencode defaults',
125
- disabled: input.globalName === null,
126
- marker: input.globalName === null ? '[x]' : '[ ]',
127
- })
114
+ /** The staged value 'none' means: delete the global marker (the CLI contract). */
115
+ export const PRESET_NONE = 'none'
116
+
117
+ export const PRESET_CYCLE: readonly string[] = Object.freeze([PRESET_NONE, ...PRESET_NAMES])
118
+
119
+ export function buildPresetTab(input: PresetSectionInput): SettingsTab {
120
+ const rows: SettingsRow[] = [
121
+ {
122
+ id: 'preset',
123
+ name: 'Preset',
124
+ value: input.globalName ?? PRESET_NONE,
125
+ cycle: PRESET_CYCLE,
126
+ note: 'global marker · atomic write, .bak backup · none = opencode defaults',
127
+ },
128
+ ]
128
129
  if (input.projectName !== null) {
129
130
  rows.push({
130
131
  id: 'preset.project',
131
- title: `${input.projectName} (project)`,
132
- description: `${projectActivePresetPath()} wins over the global marker`,
133
- marker: '[!]',
134
- selectable: false,
132
+ name: 'Project',
133
+ value: input.projectName,
134
+ note: `${projectActivePresetPath()} wins over the global marker — read-only here`,
135
135
  })
136
136
  }
137
137
  return {
138
138
  id: 'preset',
139
- title: 'Model Preset',
140
- rows,
141
- hint: 'enter: activate globally · writes <config>/opencode/.pantheon/active-preset.json',
139
+ title: 'Preset',
140
+ groups: [{ title: 'Model preset', rows }],
142
141
  }
143
142
  }
@@ -0,0 +1,378 @@
1
+ // biome-ignore-all lint/suspicious/noExplicitAny: stored prefs are host JSON of a known shape.
2
+ /**
3
+ * tui-prefs.ts — the TUI personalization core (the settings window's TUI tab).
4
+ *
5
+ * TUI-local copy doctrine (same as cost-data.ts): the bundle must stay
6
+ * self-contained. Persistence is the HOST's durable plugin storage
7
+ * (`context.storage.store('tui-prefs', { initial })` — reactive solid store,
8
+ * live-synced across TUI instances); this module owns the SHAPE and the pure
9
+ * operations, never the store handle.
10
+ *
11
+ * v1 surfaces: sidebar fixed headers, sidebar blocks, the Context block's
12
+ * sub-lines and gauge style, and the two Pantheon footer lines. Everything
13
+ * defaults to on — the window exists to turn things OFF.
14
+ */
15
+ import type { Context } from '@opencode/plugin/tui/context'
16
+ import type { SettingsRow, SettingsTab } from './pantheon-settings-state.ts'
17
+
18
+ // ── The durable store handle (host storage, one per plugin) ────────────
19
+ // Created once at setup and stashed under a globalThis symbol so
20
+ // hot-reload generations keep sharing the same live store (the same
21
+ // singleton doctrine as the plugin's SINGLETON guard).
22
+
23
+ const PREFS_SYMBOL = Symbol.for('pantheon.tui.prefs.v1')
24
+ type PrefsStore = readonly [TuiPrefs, (mutation: (draft: TuiPrefs) => void) => Promise<void>]
25
+
26
+ function stash(): PrefsStore | undefined {
27
+ return (globalThis as Record<PropertyKey, unknown>)[PREFS_SYMBOL] as PrefsStore | undefined
28
+ }
29
+
30
+ /** Create (or reuse) the durable prefs store. Fail-open: without host
31
+ * storage the consumers fall back to DEFAULT_TUI_PREFS (read-only). */
32
+ export function initTuiPrefs(context: Context): void {
33
+ if (stash() !== undefined) return
34
+ try {
35
+ const handle = context.storage?.store?.('tui-prefs', { initial: DEFAULT_TUI_PREFS }) as
36
+ | PrefsStore
37
+ | undefined
38
+ if (handle !== undefined) {
39
+ ;(globalThis as unknown as Record<PropertyKey, unknown>)[PREFS_SYMBOL] = handle
40
+ }
41
+ } catch {
42
+ /* no host storage — defaults stay read-only */
43
+ }
44
+ }
45
+
46
+ /** The live reactive prefs (read properties inside memos/JSX — tracked). */
47
+ export function tuiPrefs(): TuiPrefs {
48
+ return stash()?.[0] ?? DEFAULT_TUI_PREFS
49
+ }
50
+
51
+ /** Stage a mutation against the durable store; resolves when persisted. */
52
+ export function mutateTuiPrefs(mutation: (draft: TuiPrefs) => void): Promise<void> {
53
+ const handle = stash()
54
+ if (handle === undefined) return Promise.resolve()
55
+ return handle[1](mutation)
56
+ }
57
+
58
+ export const GAUGE_STYLES: readonly string[] = Object.freeze(['both', 'bar', 'percent', 'off'])
59
+ export const ENABLED_CYCLE: readonly string[] = Object.freeze(['enabled', 'disabled'])
60
+
61
+ export type GaugeStyle = 'both' | 'bar' | 'percent' | 'off'
62
+
63
+ export interface TuiPrefs {
64
+ version: 1
65
+ sidebar: {
66
+ header: { version: boolean; preset: boolean; session: boolean }
67
+ blocks: { context: boolean; git: boolean; todo: boolean; dispatch: boolean }
68
+ context: {
69
+ gauge: GaugeStyle
70
+ tokens: boolean
71
+ spent: boolean
72
+ modelUsage: boolean
73
+ performance: boolean
74
+ localSpend: boolean
75
+ }
76
+ }
77
+ footer: { prompt: boolean; home: boolean }
78
+ }
79
+
80
+ export const DEFAULT_TUI_PREFS: TuiPrefs = {
81
+ version: 1,
82
+ sidebar: {
83
+ header: { version: true, preset: true, session: true },
84
+ blocks: { context: true, git: true, todo: true, dispatch: true },
85
+ context: {
86
+ gauge: 'both',
87
+ tokens: true,
88
+ spent: true,
89
+ modelUsage: true,
90
+ performance: true,
91
+ localSpend: true,
92
+ },
93
+ },
94
+ footer: { prompt: true, home: true },
95
+ }
96
+
97
+ function asBool(value: unknown, fallback: boolean): boolean {
98
+ return typeof value === 'boolean' ? value : fallback
99
+ }
100
+
101
+ /** Deep-merge stored JSON over the defaults; unknown keys and junk are ignored. */
102
+ export function normalizeTuiPrefs(raw: unknown): TuiPrefs {
103
+ const stored = (typeof raw === 'object' && raw !== null ? raw : {}) as Record<string, any>
104
+ const sidebar = (
105
+ typeof stored.sidebar === 'object' && stored.sidebar !== null ? stored.sidebar : {}
106
+ ) as Record<string, any>
107
+ const header = (
108
+ typeof sidebar.header === 'object' && sidebar.header !== null ? sidebar.header : {}
109
+ ) as Record<string, any>
110
+ const blocks = (
111
+ typeof sidebar.blocks === 'object' && sidebar.blocks !== null ? sidebar.blocks : {}
112
+ ) as Record<string, any>
113
+ const context = (
114
+ typeof sidebar.context === 'object' && sidebar.context !== null ? sidebar.context : {}
115
+ ) as Record<string, any>
116
+ const footer = (
117
+ typeof stored.footer === 'object' && stored.footer !== null ? stored.footer : {}
118
+ ) as Record<string, any>
119
+ const gauge = GAUGE_STYLES.includes(context.gauge as string)
120
+ ? (context.gauge as GaugeStyle)
121
+ : DEFAULT_TUI_PREFS.sidebar.context.gauge
122
+ return {
123
+ version: 1,
124
+ sidebar: {
125
+ header: {
126
+ version: asBool(header.version, true),
127
+ preset: asBool(header.preset, true),
128
+ session: asBool(header.session, true),
129
+ },
130
+ blocks: {
131
+ context: asBool(blocks.context, true),
132
+ git: asBool(blocks.git, true),
133
+ todo: asBool(blocks.todo, true),
134
+ dispatch: asBool(blocks.dispatch, true),
135
+ },
136
+ context: {
137
+ gauge,
138
+ tokens: asBool(context.tokens, true),
139
+ spent: asBool(context.spent, true),
140
+ modelUsage: asBool(context.modelUsage, true),
141
+ performance: asBool(context.performance, true),
142
+ localSpend: asBool(context.localSpend, true),
143
+ },
144
+ },
145
+ footer: {
146
+ prompt: asBool(footer.prompt, true),
147
+ home: asBool(footer.home, true),
148
+ },
149
+ }
150
+ }
151
+
152
+ // ── The TUI tab (cycler rows over the dotted pref paths) ───────────────
153
+
154
+ const onOff = ENABLED_CYCLE
155
+ const row = (id: string, name: string, value: boolean, note: string): SettingsRow => ({
156
+ id,
157
+ name,
158
+ value: value ? 'enabled' : 'disabled',
159
+ cycle: onOff,
160
+ note,
161
+ })
162
+
163
+ export function buildTuiTab(prefs: TuiPrefs): SettingsTab {
164
+ return {
165
+ id: 'tui',
166
+ title: 'TUI',
167
+ groups: [
168
+ {
169
+ title: 'Sidebar header',
170
+ rows: [
171
+ row(
172
+ 'tui.sidebar.header.version',
173
+ 'Version line',
174
+ prefs.sidebar.header.version,
175
+ 'the `Pantheon v…` line',
176
+ ),
177
+ row(
178
+ 'tui.sidebar.header.preset',
179
+ 'Preset line',
180
+ prefs.sidebar.header.preset,
181
+ 'the `▸ preset · provider` line',
182
+ ),
183
+ row(
184
+ 'tui.sidebar.header.session',
185
+ 'Session line',
186
+ prefs.sidebar.header.session,
187
+ 'the active session title',
188
+ ),
189
+ ],
190
+ },
191
+ {
192
+ title: 'Sidebar blocks',
193
+ rows: [
194
+ row(
195
+ 'tui.sidebar.block.context',
196
+ 'Context block',
197
+ prefs.sidebar.blocks.context,
198
+ 'tokens, gauge, spend, model usage',
199
+ ),
200
+ row(
201
+ 'tui.sidebar.block.git',
202
+ 'Git block',
203
+ prefs.sidebar.blocks.git,
204
+ 'branch + working-tree counts',
205
+ ),
206
+ row(
207
+ 'tui.sidebar.block.todo',
208
+ 'Todo block',
209
+ prefs.sidebar.blocks.todo,
210
+ 'the newest todowrite',
211
+ ),
212
+ row(
213
+ 'tui.sidebar.block.dispatch',
214
+ 'Dispatch block',
215
+ prefs.sidebar.blocks.dispatch,
216
+ 'live delegations + rollup',
217
+ ),
218
+ ],
219
+ },
220
+ {
221
+ title: 'Context block detail',
222
+ rows: [
223
+ {
224
+ id: 'tui.sidebar.context.gauge',
225
+ name: 'Gauge style',
226
+ value: prefs.sidebar.context.gauge,
227
+ cycle: GAUGE_STYLES,
228
+ note: 'both | bar | percent | off — `▰▱ 42% of 200k`',
229
+ },
230
+ row(
231
+ 'tui.sidebar.context.tokens',
232
+ 'Tokens row',
233
+ prefs.sidebar.context.tokens,
234
+ 'the raw token count line',
235
+ ),
236
+ row(
237
+ 'tui.sidebar.context.spent',
238
+ 'Spent row',
239
+ prefs.sidebar.context.spent,
240
+ 'the `$… gasto na sessão` line',
241
+ ),
242
+ row(
243
+ 'tui.sidebar.context.modelUsage',
244
+ 'Model usage table',
245
+ prefs.sidebar.context.modelUsage,
246
+ 'per-model tokens/USD',
247
+ ),
248
+ row(
249
+ 'tui.sidebar.context.performance',
250
+ 'Performance line',
251
+ prefs.sidebar.context.performance,
252
+ 'cache % + tok/s',
253
+ ),
254
+ row(
255
+ 'tui.sidebar.context.localSpend',
256
+ 'Local spend',
257
+ prefs.sidebar.context.localSpend,
258
+ 'hoje / 30d totals',
259
+ ),
260
+ ],
261
+ },
262
+ {
263
+ title: 'Footers',
264
+ rows: [
265
+ row(
266
+ 'tui.footer.prompt',
267
+ 'Prompt footer',
268
+ prefs.footer.prompt,
269
+ 'the `▸preset · ✦n ativos` line while delegations run',
270
+ ),
271
+ row(
272
+ 'tui.footer.home',
273
+ 'Home status',
274
+ prefs.footer.home,
275
+ 'the `Pantheon v…` line under the home prompt',
276
+ ),
277
+ ],
278
+ },
279
+ ],
280
+ }
281
+ }
282
+
283
+ /**
284
+ * Apply one staged row INTO a prefs draft (in place — the storage mutate
285
+ * callback hands an immer-style draft). Unknown ids or junk values are no-ops.
286
+ */
287
+ export function applyTuiRow(draft: TuiPrefs, id: string, value: string): void {
288
+ const enabled = value === 'enabled'
289
+ switch (id) {
290
+ case 'tui.sidebar.header.version':
291
+ draft.sidebar.header.version = enabled
292
+ return
293
+ case 'tui.sidebar.header.preset':
294
+ draft.sidebar.header.preset = enabled
295
+ return
296
+ case 'tui.sidebar.header.session':
297
+ draft.sidebar.header.session = enabled
298
+ return
299
+ case 'tui.sidebar.block.context':
300
+ draft.sidebar.blocks.context = enabled
301
+ return
302
+ case 'tui.sidebar.block.git':
303
+ draft.sidebar.blocks.git = enabled
304
+ return
305
+ case 'tui.sidebar.block.todo':
306
+ draft.sidebar.blocks.todo = enabled
307
+ return
308
+ case 'tui.sidebar.block.dispatch':
309
+ draft.sidebar.blocks.dispatch = enabled
310
+ return
311
+ case 'tui.sidebar.context.gauge':
312
+ if (GAUGE_STYLES.includes(value)) draft.sidebar.context.gauge = value as GaugeStyle
313
+ return
314
+ case 'tui.sidebar.context.tokens':
315
+ draft.sidebar.context.tokens = enabled
316
+ return
317
+ case 'tui.sidebar.context.spent':
318
+ draft.sidebar.context.spent = enabled
319
+ return
320
+ case 'tui.sidebar.context.modelUsage':
321
+ draft.sidebar.context.modelUsage = enabled
322
+ return
323
+ case 'tui.sidebar.context.performance':
324
+ draft.sidebar.context.performance = enabled
325
+ return
326
+ case 'tui.sidebar.context.localSpend':
327
+ draft.sidebar.context.localSpend = enabled
328
+ return
329
+ case 'tui.footer.prompt':
330
+ draft.footer.prompt = enabled
331
+ return
332
+ case 'tui.footer.home':
333
+ draft.footer.home = enabled
334
+ return
335
+ default:
336
+ return
337
+ }
338
+ }
339
+
340
+ /** Read the current pref value for a row id ('enabled'/'disabled'/gauge). */
341
+ export function tuiRowValue(prefs: TuiPrefs, id: string): string | undefined {
342
+ if (id === 'tui.sidebar.context.gauge') return prefs.sidebar.context.gauge
343
+ const enabled = (() => {
344
+ switch (id) {
345
+ case 'tui.sidebar.header.version':
346
+ return prefs.sidebar.header.version
347
+ case 'tui.sidebar.header.preset':
348
+ return prefs.sidebar.header.preset
349
+ case 'tui.sidebar.header.session':
350
+ return prefs.sidebar.header.session
351
+ case 'tui.sidebar.block.context':
352
+ return prefs.sidebar.blocks.context
353
+ case 'tui.sidebar.block.git':
354
+ return prefs.sidebar.blocks.git
355
+ case 'tui.sidebar.block.todo':
356
+ return prefs.sidebar.blocks.todo
357
+ case 'tui.sidebar.block.dispatch':
358
+ return prefs.sidebar.blocks.dispatch
359
+ case 'tui.sidebar.context.tokens':
360
+ return prefs.sidebar.context.tokens
361
+ case 'tui.sidebar.context.spent':
362
+ return prefs.sidebar.context.spent
363
+ case 'tui.sidebar.context.modelUsage':
364
+ return prefs.sidebar.context.modelUsage
365
+ case 'tui.sidebar.context.performance':
366
+ return prefs.sidebar.context.performance
367
+ case 'tui.sidebar.context.localSpend':
368
+ return prefs.sidebar.context.localSpend
369
+ case 'tui.footer.prompt':
370
+ return prefs.footer.prompt
371
+ case 'tui.footer.home':
372
+ return prefs.footer.home
373
+ default:
374
+ return undefined
375
+ }
376
+ })()
377
+ return enabled === undefined ? undefined : enabled ? 'enabled' : 'disabled'
378
+ }
@@ -7,12 +7,8 @@
7
7
  * whole thing is testable without network.
8
8
  */
9
9
 
10
- import type { SettingsRow, SettingsSection } from './pantheon-settings-state.ts'
11
- import {
12
- comparePantheonVersions,
13
- UPDATE_CHECK_TTL_MS,
14
- type UpdateCheckState,
15
- } from './update-notice.ts'
10
+ import type { SettingsRow, SettingsTab } from './pantheon-settings-state.ts'
11
+ import { comparePantheonVersions, type UpdateCheckState } from './update-notice.ts'
16
12
 
17
13
  export type UpdateChannel = 'stable' | 'beta'
18
14
 
@@ -47,7 +43,7 @@ export interface UpdatesSectionInput {
47
43
  nowMs?: number
48
44
  }
49
45
 
50
- export function buildUpdatesSection(input: UpdatesSectionInput): SettingsSection {
46
+ export function buildUpdatesTab(input: UpdatesSectionInput): SettingsTab {
51
47
  const nowMs = input.nowMs ?? Date.now()
52
48
  const channel = updateChannelFor(input.currentVersion)
53
49
  const lastCheckMs = input.state.lastCheckMs
@@ -56,20 +52,20 @@ export function buildUpdatesSection(input: UpdatesSectionInput): SettingsSection
56
52
  const rows: SettingsRow[] = [
57
53
  {
58
54
  id: 'update.channel',
59
- title: `Channel: ${channel}`,
60
- description: 'stable tracks npm `latest`; beta tracks npm `beta`',
61
- selectable: false,
55
+ name: 'Channel',
56
+ value: channel,
57
+ note: 'stable tracks npm `latest`; beta tracks npm `beta`',
62
58
  },
63
59
  {
64
60
  id: 'update.check',
65
- title: 'Check now',
66
- description: input.checking
61
+ name: 'Check now',
62
+ value: input.checking
67
63
  ? 'checking…'
68
64
  : lastCheckMs
69
65
  ? `last check ${formatAgo(nowMs - lastCheckMs)}`
70
66
  : 'never checked',
71
- marker: ' → ',
72
- selectable: true,
67
+ action: 'update-check',
68
+ note: 'enter probes the registry for the running channel',
73
69
  },
74
70
  ]
75
71
 
@@ -82,17 +78,15 @@ export function buildUpdatesSection(input: UpdatesSectionInput): SettingsSection
82
78
  !(stable(input.currentVersion) && !stable(latest))
83
79
  rows.push({
84
80
  id: 'update.latest',
85
- title: newer ? `Update available: v${latest}` : `Up to date (latest seen v${latest})`,
86
- description: newer ? 'adopt with `pantheon-opencode update`' : 'checked against the registry',
87
- marker: newer ? '[!]' : '[✓]',
88
- selectable: false,
81
+ name: 'Registry',
82
+ value: newer ? `v${latest} available` : 'up to date',
83
+ note: newer ? 'adopt with `pantheon-opencode update`' : 'no newer compatible version',
89
84
  })
90
85
  }
91
86
 
92
87
  return {
93
88
  id: 'updates',
94
89
  title: 'Updates',
95
- hint: `refresh = \`pantheon-opencode update\` · notice TTL ${UPDATE_CHECK_TTL_MS / 3_600_000}h`,
96
- rows,
90
+ groups: [{ title: 'Updates', rows }],
97
91
  }
98
92
  }