dsh-agent-control-center 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,648 @@
1
+ /* Instruction preview page (module J) — read-only view of what the model
2
+ * received.
3
+ *
4
+ * Three views of the same instruction surface:
5
+ * files — module A's chain discovery plus its rendered text: the
6
+ * approximation the host can rebuild without a session;
7
+ * session — the durable `agent-instructions` blocks of one session: the exact
8
+ * text the host injected;
9
+ * compare — byte and text comparison of the two sides, with the reason a
10
+ * difference is expected.
11
+ *
12
+ * Nothing here writes an instruction file, resumes a session or invents text:
13
+ * every `{available:false}` payload renders its reason and hint, and a thrown
14
+ * request renders the shared error state with a retry. The settings content
15
+ * column already scrolls and pads its content, so this page owns vertical
16
+ * rhythm only and never claims height/overflow. */
17
+ (function instructionPage() {
18
+ /** Root rhythm: vertical only (the host owns padding and scrolling). */
19
+ const PAGE_STYLE = { display: 'flex', flexDirection: 'column', gap: '16px', padding: '8px 0' }
20
+ const STACK_STYLE = { display: 'flex', flexDirection: 'column', gap: '10px', minWidth: 0 }
21
+ const BUDGET_STACK_STYLE = { display: 'flex', flexDirection: 'column', gap: '12px', minWidth: 0 }
22
+ const BUDGET_ROW_STYLE = { display: 'flex', flexDirection: 'column', gap: '4px', minWidth: 0 }
23
+ const FACT_STYLE = { display: 'flex', flexDirection: 'column', gap: '2px', minWidth: 0 }
24
+ const FACT_LABEL_STYLE = { color: 'var(--dsw-alias-label-tertiary, #81858c)', font: '11px/16px var(--dsw-font-family, sans-serif)' }
25
+ const FACT_VALUE_STYLE = { font: '600 18px/24px var(--dsw-font-family, sans-serif)', fontVariantNumeric: 'tabular-nums' }
26
+ const MUTED_STYLE = { color: 'var(--dsw-alias-label-tertiary, #81858c)', font: '12px/18px var(--dsw-font-family, sans-serif)' }
27
+ const BREAK_STYLE = { minWidth: 0, wordBreak: 'break-word' }
28
+ const LIST_STYLE = { margin: 0, paddingInlineStart: '18px', color: 'var(--dsw-alias-label-secondary, #61666b)' }
29
+
30
+ /** Above this many characters a block starts collapsed to keep cards dense. */
31
+ const LONG_BLOCK_CHARS = 900
32
+ /** The session picker lists the host's first page of sessions. */
33
+ const SESSION_PAGE_SIZE = 50
34
+
35
+ const VIEWS = [
36
+ { id: 'files', labelKey: 'instruction.tabFiles' },
37
+ { id: 'session', labelKey: 'instruction.tabSession' },
38
+ { id: 'compare', labelKey: 'instruction.tabCompare' },
39
+ ]
40
+
41
+ /** @param {unknown} value @returns {string} */
42
+ function textOf(value) {
43
+ return value === null || value === undefined ? '' : String(value)
44
+ }
45
+
46
+ /** @param {unknown} value @returns {string} signed byte delta; positive means the session side is larger */
47
+ function signedBytes(value, t) {
48
+ if (typeof value !== 'number' || !Number.isFinite(value)) return t('common.unknown')
49
+ if (value === 0) return ACC.l10n.bytes(0, t)
50
+ return `${value > 0 ? '+' : '-'}${ACC.l10n.bytes(Math.abs(value), t)}`
51
+ }
52
+
53
+ /** Localized label for a chain file's discovered source. @param {Function} t @param {unknown} source */
54
+ function sourceLabel(t, source) {
55
+ if (source === 'user-global') return t('instruction.sourceUserGlobal')
56
+ if (source === 'project') return t('instruction.sourceProject')
57
+ if (source === 'agents-home') return t('instruction.sourceAgentsHome')
58
+ if (source === 'project-local') return t('instruction.sourceProjectLocal')
59
+ return t('instruction.sourceUnknown')
60
+ }
61
+
62
+ /** @param {unknown} source */
63
+ function sourceTone(source) {
64
+ return source === 'user-global' || source === 'agents-home' ? 'info' : 'neutral'
65
+ }
66
+
67
+ /** Inline copy feedback (the host has no toast service). */
68
+ function CopyButton(props) {
69
+ const [state, setState] = ACC.useState('idle')
70
+ const copy = ACC.useCallback(() => {
71
+ const text = textOf(props.text)
72
+ if (text.length === 0) {
73
+ setState('failed')
74
+ return
75
+ }
76
+ if (typeof navigator !== 'undefined' && navigator.clipboard && typeof navigator.clipboard.writeText === 'function') {
77
+ navigator.clipboard.writeText(text).then(() => setState('done'), () => setState('failed'))
78
+ return
79
+ }
80
+ setState('failed')
81
+ }, [props.text])
82
+ ACC.useEffect(() => {
83
+ if (state === 'idle') return undefined
84
+ const timer = setTimeout(() => setState('idle'), 1800)
85
+ return () => clearTimeout(timer)
86
+ }, [state])
87
+ const label = state === 'done' ? props.t('instruction.copied') : state === 'failed' ? props.t('instruction.copyFailed') : props.label
88
+ return h(C.Btn, { tone: 'ghost', onClick: copy, title: label, ariaLabel: props.ariaLabel || label }, label)
89
+ }
90
+
91
+ /**
92
+ * `{available:false}` body: reason and hint from the host, plus the original
93
+ * text in the collapsible raw block when the English UI would show Chinese.
94
+ * @param {{ data?: any, t: Function, onRetry?: Function }} props
95
+ */
96
+ function UnavailableBody(props) {
97
+ const t = props.t
98
+ const data = props.data !== null && typeof props.data === 'object' ? props.data : {}
99
+ const reason = ACC.l10n.host('instruction.reasonGeneric', data.reason)
100
+ const hint = ACC.l10n.host('instruction.hintGeneric', data.hint)
101
+ const raw = [textOf(data.reason), textOf(data.hint)].filter((entry) => entry.length > 0).join('\n')
102
+ return h(
103
+ 'div',
104
+ { style: STACK_STYLE },
105
+ reason.length > 0
106
+ ? h('div', { className: 'acc-row' }, h('span', { style: MUTED_STYLE }, `${t('common.reason')}:`), h('span', { style: BREAK_STYLE }, reason))
107
+ : null,
108
+ hint.length > 0
109
+ ? h('div', { className: 'acc-row' }, h('span', { style: MUTED_STYLE }, `${t('instruction.hint')}:`), h('span', { style: BREAK_STYLE }, hint))
110
+ : null,
111
+ raw.length > 0 ? ACC.l10n.raw(t('instruction.rawHostText'), raw) : null,
112
+ props.onRetry ? h(C.Toolbar, null, h(C.Btn, { icon: 'retry', onClick: props.onRetry }, t('common.retry'))) : null,
113
+ )
114
+ }
115
+
116
+ /** @param {{ data?: any, t: Function, title: string, onRetry?: Function }} props */
117
+ function UnavailablePanel(props) {
118
+ return h(C.Card, { title: props.title }, h(UnavailableBody, { data: props.data, t: props.t, onRetry: props.onRetry }))
119
+ }
120
+
121
+ /** Budget bar for one chain: renderedBytes / maxBytes plus the fits badge. */
122
+ function BudgetBar(props) {
123
+ const t = props.t
124
+ const chain = props.chain !== null && typeof props.chain === 'object' ? props.chain : {}
125
+ const renderedBytes = typeof chain.renderedBytes === 'number' && Number.isFinite(chain.renderedBytes) ? chain.renderedBytes : null
126
+ const rawBytes = typeof chain.bytes === 'number' && Number.isFinite(chain.bytes) ? chain.bytes : null
127
+ // Module A reports per-chain maxima in some deployments and one top-level
128
+ // maximum in others; either source is authoritative, never a guessed value.
129
+ const maxBytes = typeof chain.maxBytes === 'number' && Number.isFinite(chain.maxBytes) && chain.maxBytes > 0
130
+ ? chain.maxBytes
131
+ : typeof props.maxBytes === 'number' && Number.isFinite(props.maxBytes) && props.maxBytes > 0
132
+ ? props.maxBytes
133
+ : null
134
+ const files = typeof chain.files === 'number' && Number.isFinite(chain.files) ? chain.files : null
135
+ const fits = chain.fits === true
136
+ const ratio = renderedBytes !== null && maxBytes !== null ? Math.max(0, Math.min(1, renderedBytes / maxBytes)) : null
137
+ const usage = renderedBytes !== null && maxBytes !== null
138
+ ? t('instruction.budgetUsage', { rendered: ACC.l10n.bytes(renderedBytes, t), max: ACC.l10n.bytes(maxBytes, t) })
139
+ : t('common.unknown')
140
+ return h(
141
+ 'div',
142
+ { style: BUDGET_ROW_STYLE },
143
+ h(
144
+ 'div',
145
+ { className: 'acc-row', style: { justifyContent: 'space-between' } },
146
+ h('span', null, props.label),
147
+ h('span', { className: 'acc-muted', style: { fontVariantNumeric: 'tabular-nums' } }, usage),
148
+ ),
149
+ ratio === null
150
+ ? null
151
+ : h(
152
+ 'div',
153
+ { className: 'acc-bar', role: 'img', 'aria-label': `${props.label}: ${usage} · ${fits ? t('instruction.budgetFits') : t('instruction.budgetOver')}` },
154
+ h('div', { className: `acc-bar-fill${fits ? '' : ' is-warn'}`, style: { width: `${Math.round(ratio * 100)}%` } }),
155
+ ),
156
+ h(
157
+ 'div',
158
+ { className: 'acc-row', style: { gap: '6px' } },
159
+ h(C.Badge, { tone: fits ? 'success' : 'warn' }, fits ? t('instruction.budgetFits') : t('instruction.budgetOver')),
160
+ rawBytes !== null ? h('span', { style: MUTED_STYLE }, t('instruction.budgetRaw', { bytes: ACC.l10n.bytes(rawBytes, t) })) : null,
161
+ files !== null ? h('span', { style: MUTED_STYLE }, t('instruction.chainFilesCount', { count: ACC.fmt.num(files) })) : null,
162
+ ),
163
+ )
164
+ }
165
+
166
+ /** One badge per chain flag the host set on a file row. */
167
+ function FileFlags(props) {
168
+ const t = props.t
169
+ const row = props.row !== null && typeof props.row === 'object' ? props.row : {}
170
+ const badges = []
171
+ if (row.inEffectiveChain === true) badges.push(h(C.Badge, { key: 'effective', tone: 'success' }, t('instruction.badgeEffective')))
172
+ if (row.deduped === true) {
173
+ badges.push(
174
+ h(
175
+ C.Badge,
176
+ { key: 'deduped', tone: 'neutral', title: row.dedupedBy ? t('instruction.badgeDedupedBy', { path: textOf(row.dedupedBy) }) : undefined },
177
+ t('instruction.badgeDeduped'),
178
+ ),
179
+ )
180
+ }
181
+ if (row.budgetOmitted === true) badges.push(h(C.Badge, { key: 'omitted', tone: 'warn' }, t('instruction.badgeOmitted')))
182
+ if (row.budgetTruncated === true) badges.push(h(C.Badge, { key: 'truncated', tone: 'danger' }, t('instruction.badgeTruncated')))
183
+ if (badges.length === 0) return h('span', { className: 'acc-cell-empty' }, t('common.none'))
184
+ return h('div', { className: 'acc-row', style: { gap: '4px' } }, badges)
185
+ }
186
+
187
+ /** Chain rows: display path, source, bytes and the budget/dedup flags. */
188
+ function ChainTable(props) {
189
+ const t = props.t
190
+ return h(C.Table, {
191
+ columns: [
192
+ {
193
+ key: 'displayPath',
194
+ label: t('instruction.displayPath'),
195
+ render: (row) => h(C.Mono, { text: textOf(row.displayPath) || textOf(row.path) || '—', title: textOf(row.path) || undefined }),
196
+ },
197
+ { key: 'source', label: t('instruction.source'), render: (row) => h(C.Badge, { tone: sourceTone(row.source) }, sourceLabel(t, row.source)) },
198
+ { key: 'bytes', label: t('common.bytes'), align: 'right', render: (row) => ACC.l10n.bytes(row.bytes, t) },
199
+ { key: 'flags', label: t('instruction.flags'), render: (row) => h(FileFlags, { row, t }) },
200
+ ],
201
+ rows: Array.isArray(props.files) ? props.files : [],
202
+ rowKey: 'path',
203
+ empty: h(C.Empty, { title: t('instruction.chainEmpty'), hint: t('instruction.chainEmptyHint') }),
204
+ })
205
+ }
206
+
207
+ /** Chain warnings are host free text: listed in Chinese, disclosed raw in English. */
208
+ function ChainWarnings(props) {
209
+ const t = props.t
210
+ const warnings = props.warnings.map(textOf).filter((entry) => entry.length > 0)
211
+ if (warnings.length === 0) return null
212
+ if (ACC.l10n.lang() === 'en' && ACC.l10n.hasCjk(warnings.join(''))) {
213
+ return ACC.l10n.raw(t('instruction.rawHostText'), warnings.join('\n'))
214
+ }
215
+ return h(
216
+ 'div',
217
+ { style: STACK_STYLE },
218
+ h('div', { className: 'acc-muted' }, t('instruction.chainWarnings')),
219
+ h('ul', { style: LIST_STYLE }, warnings.map((warning, index) => h('li', { key: String(index) }, warning))),
220
+ )
221
+ }
222
+
223
+ /** Files view, first card: discovery rows plus the budget bars. */
224
+ function ChainCard(props) {
225
+ const t = props.t
226
+ const chain = props.chain
227
+ const chains = chain.chains !== null && typeof chain.chains === 'object' ? chain.chains : {}
228
+ const budget = chain.budget !== null && typeof chain.budget === 'object' ? chain.budget : {}
229
+ const files = Array.isArray(chain.files) ? chain.files : []
230
+ const warnings = Array.isArray(chain.warnings) ? chain.warnings : []
231
+ const maxBytes = typeof budget.maxBytes === 'number' && Number.isFinite(budget.maxBytes) ? budget.maxBytes : null
232
+ const budgetSource = textOf(budget.source)
233
+ const cwd = textOf(chain.cwd)
234
+ const projectRoot = textOf(chain.projectRoot)
235
+ const cwdSource = textOf(chain.cwdSource)
236
+ return h(
237
+ C.Card,
238
+ {
239
+ title: t('instruction.chainTitle'),
240
+ subtitle: t('instruction.chainFilesCount', { count: ACC.fmt.num(files.length) }),
241
+ actions: h(
242
+ 'div',
243
+ { className: 'acc-row' },
244
+ h(C.Btn, { tone: 'ghost', icon: 'retry', onClick: props.reload, title: t('common.refresh'), ariaLabel: t('common.refresh') }, t('common.refresh')),
245
+ ),
246
+ },
247
+ h(
248
+ 'div',
249
+ { style: BUDGET_STACK_STYLE },
250
+ h(BudgetBar, { label: t('instruction.chainUserGlobal'), chain: chains.userGlobal, maxBytes, t }),
251
+ h(BudgetBar, { label: t('instruction.chainProject'), chain: chains.project, maxBytes, t }),
252
+ ),
253
+ h(C.KeyValues, {
254
+ items: [
255
+ { label: t('instruction.chainCwd'), value: cwd.length > 0 ? h(C.Mono, { text: cwd }) : t('common.unknown') },
256
+ { label: t('instruction.chainProjectRoot'), value: projectRoot.length > 0 ? h(C.Mono, { text: projectRoot }) : t('common.unknown') },
257
+ { label: t('instruction.chainSource'), value: cwdSource.length > 0 ? cwdSource : t('common.unknown') },
258
+ {
259
+ label: t('instruction.budgetSource'),
260
+ value: budgetSource.length > 0
261
+ ? budgetSource
262
+ : maxBytes !== null
263
+ ? `${t('instruction.budgetUsage', { rendered: ACC.l10n.bytes(0, t), max: ACC.l10n.bytes(maxBytes, t) })}`
264
+ : t('instruction.budgetSourceUnknown'),
265
+ },
266
+ ],
267
+ }),
268
+ h(ChainWarnings, { warnings, t }),
269
+ chain.truncated === true ? h(C.Notice, { tone: 'warn' }, t('instruction.chainTruncated')) : null,
270
+ h(ChainTable, { files, t }),
271
+ )
272
+ }
273
+
274
+ /** Files view, second card: the rendered text (approximation) plus byte count. */
275
+ function PreviewCard(props) {
276
+ const t = props.t
277
+ const preview = props.preview !== null && typeof props.preview === 'object' ? props.preview : null
278
+ if (preview === null) return null
279
+ if (preview.available !== true) {
280
+ return h(C.Card, { title: t('instruction.previewTitle') }, h(UnavailableBody, { data: preview, t, onRetry: props.reload }))
281
+ }
282
+ const text = typeof preview.text === 'string' ? preview.text : ''
283
+ const exact = preview.exact === true
284
+ const bytes = typeof preview.bytes === 'number' && Number.isFinite(preview.bytes) ? preview.bytes : null
285
+ const sections = Array.isArray(preview.sections) ? preview.sections : []
286
+ const omitted = Array.isArray(preview.omitted) ? preview.omitted : []
287
+ const truncated = Array.isArray(preview.truncated) ? preview.truncated : []
288
+ const note = ACC.l10n.host('instruction.previewNote', preview.note)
289
+ const actions = h(
290
+ 'div',
291
+ { className: 'acc-row' },
292
+ h(
293
+ C.Badge,
294
+ { tone: exact ? 'success' : 'warn', title: exact ? t('instruction.previewExact') : t('instruction.previewApprox') },
295
+ exact ? t('instruction.exact') : t('instruction.approximate'),
296
+ ),
297
+ bytes !== null ? h('span', { className: 'acc-muted', style: { fontVariantNumeric: 'tabular-nums' } }, t('instruction.previewBytes', { bytes: ACC.l10n.bytes(bytes, t) })) : null,
298
+ text.length > 0 ? h(CopyButton, { text, t, label: t('instruction.copyPreview') }) : null,
299
+ )
300
+ return h(
301
+ C.Card,
302
+ { title: t('instruction.previewTitle'), actions },
303
+ h(
304
+ 'div',
305
+ { style: STACK_STYLE },
306
+ text.length > 0 ? h(C.Code, { text }) : h(C.Empty, { title: t('instruction.previewEmpty'), hint: t('instruction.previewEmptyHint') }),
307
+ sections.length > 0 ? h('div', { style: { ...MUTED_STYLE, fontSize: '11px' } }, t('instruction.previewSections', { count: ACC.fmt.num(sections.length) })) : null,
308
+ omitted.length > 0 ? h(C.Notice, { tone: 'warn' }, t('instruction.previewOmitted', { count: ACC.fmt.num(omitted.length) })) : null,
309
+ truncated.length > 0 ? h(C.Notice, { tone: 'warn' }, t('instruction.previewTruncated', { count: ACC.fmt.num(truncated.length) })) : null,
310
+ preview.responseTruncated === true ? h(C.Notice, { tone: 'warn' }, t('instruction.previewResponseTruncated')) : null,
311
+ note.length > 0 ? h('div', { style: MUTED_STYLE }, note) : null,
312
+ ACC.l10n.raw(t('instruction.rawHostText'), preview.note),
313
+ ),
314
+ )
315
+ }
316
+
317
+ /** Files view: discovery and rendering are independent requests. */
318
+ function FilesView(props) {
319
+ const t = props.t
320
+ const state = ACC.useAsync(
321
+ (context) => Promise.all([ACC.api('instruction.chain', {}, context), ACC.api('instruction.preview', {}, context)]),
322
+ [],
323
+ )
324
+ if (state.status === 'loading') return h(C.Loading, { label: t('common.loading') })
325
+ if (state.status === 'error') return h(C.ErrorBox, { error: state.error, onRetry: state.reload, label: t('instruction.failed') })
326
+ const pair = Array.isArray(state.data) ? state.data : []
327
+ const chain = pair[0]
328
+ const preview = pair[1]
329
+ return h(
330
+ 'div',
331
+ { style: STACK_STYLE },
332
+ chain !== null && typeof chain === 'object' && chain.available === true
333
+ ? h(ChainCard, { chain, t, reload: state.reload })
334
+ : h(UnavailablePanel, { data: chain, t, title: t('instruction.chainUnavailableTitle'), onRetry: state.reload }),
335
+ h(PreviewCard, { preview, t, reload: state.reload }),
336
+ )
337
+ }
338
+
339
+ /** Host session rows; `items` is the documented field, `rows` a tolerated alias. */
340
+ function sessionItems(feed) {
341
+ if (feed === null || feed === undefined || feed.status !== 'ready') return []
342
+ const data = feed.data
343
+ if (data === null || typeof data !== 'object') return []
344
+ const list = Array.isArray(data.items) ? data.items : Array.isArray(data.rows) ? data.rows : []
345
+ return list.filter((row) => row !== null && typeof row === 'object' && typeof row.id === 'string' && row.id.length > 0)
346
+ }
347
+
348
+ /** Picker label: the title when the host has one, never a fabricated id title. */
349
+ function sessionOptionLabel(row, t) {
350
+ const title = typeof row.title === 'string' && row.title.length > 0 ? row.title : t('instruction.untitled')
351
+ const at = typeof row.updatedAt === 'number' && Number.isFinite(row.updatedAt) ? ACC.l10n.dateTime(row.updatedAt, t) : ''
352
+ return at.length > 0 && at !== t('common.unknown') ? `${title} · ${at}` : title
353
+ }
354
+
355
+ /** Session picker shared by the session and compare views. */
356
+ function SessionPickerCard(props) {
357
+ const t = props.t
358
+ const feed = props.feed
359
+ const items = sessionItems(feed)
360
+ const data = feed !== null && feed !== undefined && feed.status === 'ready' ? feed.data : null
361
+ const unavailable = data !== null && typeof data === 'object' && data.available === false
362
+ let body = null
363
+ if (feed === undefined || feed === null) body = null
364
+ else if (feed.status === 'loading') body = h(C.Loading, { label: t('common.loading') })
365
+ else if (feed.status === 'error') body = h(C.ErrorBox, { error: feed.error, onRetry: feed.reload, label: t('instruction.sessionsFailed') })
366
+ else if (unavailable) body = h(UnavailableBody, { data, t, onRetry: feed.reload })
367
+ else if (items.length === 0) {
368
+ body = h(C.Empty, {
369
+ title: t('instruction.sessionsEmpty'),
370
+ hint: t('instruction.sessionsEmptyHint'),
371
+ action: h(C.Btn, { onClick: feed.reload }, t('common.refresh')),
372
+ })
373
+ } else {
374
+ body = h(
375
+ 'div',
376
+ { className: 'acc-row' },
377
+ h(
378
+ 'select',
379
+ {
380
+ className: 'acc-select',
381
+ value: props.value,
382
+ 'aria-label': t('instruction.sessionPickAria'),
383
+ onChange: (event) => props.onChange(event.target.value),
384
+ },
385
+ h('option', { value: '' }, t('instruction.sessionPick')),
386
+ items.map((row) => h('option', { key: row.id, value: row.id }, sessionOptionLabel(row, t))),
387
+ ),
388
+ h('span', { className: 'acc-muted', style: { fontVariantNumeric: 'tabular-nums' } }, t('instruction.sessionListLoaded', { count: ACC.fmt.num(items.length) })),
389
+ )
390
+ }
391
+ return h(
392
+ C.Card,
393
+ {
394
+ title: t('instruction.sessionPickTitle'),
395
+ subtitle: t('instruction.sessionPickHint'),
396
+ actions: feed === undefined || feed === null
397
+ ? null
398
+ : h(C.Btn, { tone: 'ghost', icon: 'retry', onClick: feed.reload, title: t('instruction.sessionListRefresh'), ariaLabel: t('instruction.sessionListRefresh') }, t('common.refresh')),
399
+ },
400
+ body,
401
+ )
402
+ }
403
+
404
+ /** One durable instruction block, collapsible when the text is long. */
405
+ function InstructionBlock(props) {
406
+ const t = props.t
407
+ const item = props.item !== null && typeof props.item === 'object' ? props.item : {}
408
+ const text = typeof item.text === 'string' ? item.text : ''
409
+ const truncated = item.truncated === true
410
+ const long = text.length > LONG_BLOCK_CHARS
411
+ const [open, setOpen] = ACC.useState(!long)
412
+ const seqText = typeof item.seq === 'number' ? ACC.fmt.num(item.seq) : textOf(item.seq)
413
+ const bytesText = ACC.l10n.bytes(item.bytes, t)
414
+ return h(
415
+ C.Card,
416
+ {
417
+ title: t('instruction.blockTitle', { seq: seqText }),
418
+ subtitle: `${t('instruction.blockTime')}: ${ACC.l10n.dateTime(item.at, t)} · ${bytesText}`,
419
+ actions: h(
420
+ 'div',
421
+ { className: 'acc-row' },
422
+ h(C.Badge, { tone: item.baseline === true ? 'success' : 'neutral' }, item.baseline === true ? t('instruction.baseline') : t('instruction.change')),
423
+ truncated ? h(C.Badge, { tone: 'warn' }, t('instruction.blockTruncated')) : null,
424
+ text.length > 0 ? h(CopyButton, { text, t, label: t('instruction.copyBlock'), ariaLabel: t('instruction.copyBlockAria', { seq: seqText }) }) : null,
425
+ long
426
+ ? h(
427
+ 'button',
428
+ {
429
+ type: 'button',
430
+ className: 'acc-btn acc-btn-ghost',
431
+ 'aria-expanded': open,
432
+ onClick: () => setOpen((value) => !value),
433
+ },
434
+ h('span', { 'aria-hidden': 'true' }, open ? '▾' : '▸'),
435
+ open ? t('instruction.collapse') : t('instruction.expand'),
436
+ )
437
+ : null,
438
+ ),
439
+ },
440
+ open
441
+ ? h(
442
+ 'div',
443
+ { style: STACK_STYLE },
444
+ text.length > 0 ? h(C.Code, { text }) : h('div', { className: 'acc-muted' }, t('instruction.blockEmpty')),
445
+ truncated ? h(C.Notice, { tone: 'warn' }, t('instruction.blockTruncatedHint')) : null,
446
+ )
447
+ : h('div', { className: 'acc-muted' }, t('instruction.blockCollapsed', { bytes: bytesText })),
448
+ )
449
+ }
450
+
451
+ /** Session view: picker plus the exact instruction blocks of the picked session. */
452
+ function SessionView(props) {
453
+ const t = props.t
454
+ const state = ACC.useAsync(
455
+ (context) => (props.sessionId.length === 0 ? null : ACC.api('instruction.session', { sessionId: props.sessionId }, context)),
456
+ [props.sessionId],
457
+ )
458
+ let content = null
459
+ if (props.sessionId.length === 0) {
460
+ content = h(C.Empty, { title: t('instruction.noSelection'), hint: t('instruction.noSelectionHint') })
461
+ } else if (state.status === 'loading') {
462
+ content = h(C.Loading, { label: t('common.loading') })
463
+ } else if (state.status === 'error') {
464
+ content = h(C.ErrorBox, { error: state.error, onRetry: state.reload, label: t('instruction.sessionFailed') })
465
+ } else {
466
+ const data = state.data
467
+ if (data === null || typeof data !== 'object') {
468
+ content = h(C.Empty, { title: t('instruction.noSelection'), hint: t('instruction.noSelectionHint') })
469
+ } else if (data.available !== true) {
470
+ content = h(UnavailablePanel, { data, t, title: t('instruction.sessionUnavailableTitle'), onRetry: state.reload })
471
+ } else {
472
+ const items = Array.isArray(data.items) ? data.items : []
473
+ const reason = ACC.l10n.host('instruction.sessionReason', data.reason)
474
+ content = h(
475
+ 'div',
476
+ { style: STACK_STYLE },
477
+ h(
478
+ 'div',
479
+ { className: 'acc-row', style: { justifyContent: 'space-between' } },
480
+ h('span', { className: 'acc-muted' }, t('instruction.sessionInstructionCount', { count: ACC.fmt.num(items.length) })),
481
+ h(
482
+ 'span',
483
+ { className: 'acc-row', style: { gap: '4px' } },
484
+ h('span', { style: MUTED_STYLE }, `${t('instruction.sessionId')}:`),
485
+ h(C.Mono, { text: props.sessionId, title: props.sessionId, className: 'acc-muted' }),
486
+ ),
487
+ ),
488
+ reason.length > 0 ? h('div', { style: MUTED_STYLE }, `${t('instruction.sessionReason')}: ${reason}`) : null,
489
+ items.length === 0
490
+ ? h(C.Empty, { title: t('instruction.sessionEmpty'), hint: t('instruction.sessionEmptyHint') })
491
+ : h(
492
+ 'div',
493
+ { style: STACK_STYLE },
494
+ h('h3', { className: 'acc-section-title' }, t('instruction.sessionBlocksTitle')),
495
+ items.map((item, index) => h(InstructionBlock, { key: `${textOf(item.seq)}-${index}`, item, t })),
496
+ ),
497
+ )
498
+ }
499
+ }
500
+ return h(
501
+ 'div',
502
+ { style: STACK_STYLE },
503
+ h(SessionPickerCard, { feed: props.sessions, value: props.sessionId, onChange: props.onSelect, t }),
504
+ content,
505
+ )
506
+ }
507
+
508
+ /** One headline number in the comparison grid. */
509
+ function CompareStat(props) {
510
+ return h('div', { style: FACT_STYLE }, h('span', { style: FACT_LABEL_STYLE }, props.label), h('strong', { style: FACT_VALUE_STYLE }, props.value))
511
+ }
512
+
513
+ /** Comparison result: three byte counts, text/format badges and one explanation. */
514
+ function CompareResult(props) {
515
+ const t = props.t
516
+ const data = props.data
517
+ const same = data.sameText === true
518
+ const exactFiles = data.exactFiles === true
519
+ const sessionItems = typeof data.sessionItems === 'number' && Number.isFinite(data.sessionItems) ? data.sessionItems : null
520
+ const note = ACC.l10n.host('instruction.compareNote', data.note)
521
+ return h(
522
+ C.Card,
523
+ { title: t('instruction.compareTitle'), subtitle: t('instruction.compareSubtitle') },
524
+ h(
525
+ 'div',
526
+ { style: STACK_STYLE },
527
+ h(
528
+ 'div',
529
+ { className: 'acc-grid-fit' },
530
+ h(CompareStat, { label: t('instruction.compareFilesBytes'), value: ACC.l10n.bytes(data.filesBytes, t) }),
531
+ h(CompareStat, { label: t('instruction.compareSessionBytes'), value: ACC.l10n.bytes(data.sessionBytes, t) }),
532
+ h(CompareStat, { label: t('instruction.compareDeltaBytes'), value: signedBytes(data.deltaBytes, t) }),
533
+ ),
534
+ h(
535
+ 'div',
536
+ { className: 'acc-row' },
537
+ h(C.Badge, { tone: same ? 'success' : 'warn' }, same ? t('instruction.sameText') : t('instruction.differentText')),
538
+ h(C.Badge, { tone: exactFiles ? 'success' : 'warn' }, exactFiles ? t('instruction.exactFilesYes') : t('instruction.exactFilesNo')),
539
+ sessionItems === null ? null : h(C.Badge, { tone: 'neutral' }, t('instruction.sessionItemsValue', { count: ACC.fmt.num(sessionItems) })),
540
+ ),
541
+ same ? null : h(C.Notice, { tone: 'warn' }, t('instruction.compareWhy')),
542
+ note.length > 0 ? h('div', { style: MUTED_STYLE }, note) : null,
543
+ ACC.l10n.raw(t('instruction.rawHostText'), data.note),
544
+ ),
545
+ )
546
+ }
547
+
548
+ /** Compare view: files approximation vs the session's exact baseline block. */
549
+ function CompareView(props) {
550
+ const t = props.t
551
+ const state = ACC.useAsync(
552
+ (context) => (props.sessionId.length === 0 ? null : ACC.api('instruction.compare', { sessionId: props.sessionId }, context)),
553
+ [props.sessionId],
554
+ )
555
+ let content = null
556
+ if (props.sessionId.length === 0) {
557
+ content = h(C.Empty, { title: t('instruction.noSelection'), hint: t('instruction.noSelectionHint') })
558
+ } else if (state.status === 'loading') {
559
+ content = h(C.Loading, { label: t('common.loading') })
560
+ } else if (state.status === 'error') {
561
+ content = h(C.ErrorBox, { error: state.error, onRetry: state.reload, label: t('instruction.compareFailed') })
562
+ } else {
563
+ const data = state.data
564
+ if (data === null || typeof data !== 'object') {
565
+ content = h(C.Empty, { title: t('instruction.noSelection'), hint: t('instruction.noSelectionHint') })
566
+ } else if (data.available !== true) {
567
+ content = h(UnavailablePanel, { data, t, title: t('instruction.compareUnavailableTitle'), onRetry: state.reload })
568
+ } else {
569
+ content = h(CompareResult, { data, t })
570
+ }
571
+ }
572
+ return h(
573
+ 'div',
574
+ { style: STACK_STYLE },
575
+ h(SessionPickerCard, { feed: props.sessions, value: props.sessionId, onChange: props.onSelect, t }),
576
+ content,
577
+ )
578
+ }
579
+
580
+ /** @param {{ t: Function, navigate: Function }} props */
581
+ function InstructionPage(props) {
582
+ const t = props.t
583
+ const summary = ACC.useAsync((context) => ACC.api('instruction.summary', {}, context), [])
584
+ const sessions = ACC.useAsync((context) => ACC.api('sessions.list', { page: 1, limit: SESSION_PAGE_SIZE }, context), [])
585
+ const [view, setView] = ACC.useState('files')
586
+ const [sessionId, setSessionId] = ACC.useState('')
587
+
588
+ // Picking the most recent session keeps the exact/compare views usable
589
+ // without a second interaction; the user can change it at any time.
590
+ ACC.useEffect(() => {
591
+ if (sessionId.length > 0 || sessions.status !== 'ready') return
592
+ const rows = sessionItems(sessions)
593
+ if (rows.length > 0) setSessionId(String(rows[0].id))
594
+ // eslint-disable-next-line react-hooks/exhaustive-deps
595
+ }, [sessionId, sessions.status, sessions.data])
596
+
597
+ const readOnly = h(C.Notice, { tone: 'info' }, t('instruction.readOnly'))
598
+ let body = null
599
+ if (summary.status === 'loading') {
600
+ body = h(C.Loading, { label: t('common.loading') })
601
+ } else if (summary.status === 'error') {
602
+ body = h(C.ErrorBox, { error: summary.error, onRetry: summary.reload, label: t('instruction.failed') })
603
+ } else if (summary.data === null || typeof summary.data !== 'object' || summary.data.available !== true) {
604
+ body = h(UnavailablePanel, { data: summary.data, t, title: t('instruction.unavailableTitle'), onRetry: summary.reload })
605
+ } else {
606
+ const tabs = h(
607
+ 'div',
608
+ { className: 'acc-tabs', role: 'tablist', 'aria-label': t('instruction.tabsLabel') },
609
+ VIEWS.map((entry) =>
610
+ h(
611
+ 'button',
612
+ {
613
+ key: entry.id,
614
+ type: 'button',
615
+ role: 'tab',
616
+ 'aria-selected': view === entry.id,
617
+ className: `acc-tab${view === entry.id ? ' is-active' : ''}`,
618
+ onClick: () => setView(entry.id),
619
+ },
620
+ t(entry.labelKey),
621
+ ),
622
+ ),
623
+ )
624
+ body = h(
625
+ 'div',
626
+ { style: STACK_STYLE },
627
+ summary.data.moduleA === false ? h(C.Notice, { tone: 'warn' }, t('instruction.moduleAUnavailable')) : null,
628
+ h(C.Toolbar, null, tabs),
629
+ view === 'files'
630
+ ? h(FilesView, { t })
631
+ : view === 'session'
632
+ ? h(SessionView, { t, sessions, sessionId, onSelect: setSessionId })
633
+ : h(CompareView, { t, sessions, sessionId, onSelect: setSessionId }),
634
+ )
635
+ }
636
+ return h('div', { style: PAGE_STYLE }, readOnly, body)
637
+ }
638
+
639
+ ACC.registerPage({
640
+ id: 'instruction',
641
+ titleKey: 'nav.instruction',
642
+ descriptionKey: 'nav.desc.instruction',
643
+ icon: 'file-text',
644
+ order: 40,
645
+ group: 'edit',
646
+ render: (props) => h(InstructionPage, props),
647
+ })
648
+ })()
@@ -907,6 +907,7 @@
907
907
  id: 'meta',
908
908
  titleKey: 'nav.meta',
909
909
  order: 60,
910
+ group: 'insight',
910
911
  render: (props) => h(MetaPage, props),
911
912
  })
912
913
  })()
@@ -328,6 +328,7 @@
328
328
  id: 'overview',
329
329
  titleKey: 'nav.overview',
330
330
  order: 10,
331
+ group: 'insight',
331
332
  render: (props) => h(OverviewPage, props),
332
333
  })
333
334
  })()
@@ -488,6 +488,7 @@
488
488
  id: 'recovery',
489
489
  titleKey: 'nav.recovery',
490
490
  order: 90,
491
+ group: 'maintain',
491
492
  render: (props) => h(RecoveryPage, props),
492
493
  })
493
494
  })()