@linxin666/dsh-client-ui-task-board-ssh 0.0.0-stage → 0.4.20

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 (126) hide show
  1. package/LICENSE +202 -0
  2. package/README.i18n.yaml +6 -0
  3. package/README.md +85 -2
  4. package/README.zh.md +86 -0
  5. package/cordis.patch.yml +15 -0
  6. package/icon.svg +5 -0
  7. package/lib/client.js +4429 -0
  8. package/lib/index.js +6100 -0
  9. package/lib/types/client/PluginSettingsCard.d.ts +255 -0
  10. package/lib/types/client/RangeBubble.d.ts +46 -0
  11. package/lib/types/client/SegmentGroup.d.ts +28 -0
  12. package/lib/types/client/index.d.ts +100 -0
  13. package/lib/types/client/locales.d.ts +125 -0
  14. package/lib/types/client/seat-props.d.ts +53 -0
  15. package/lib/types/client/settings-entry-form.d.ts +49 -0
  16. package/lib/types/client/settings-form.d.ts +237 -0
  17. package/lib/types/client/ssh/card-decoration.d.ts +23 -0
  18. package/lib/types/client/ssh/compose-section.d.ts +7 -0
  19. package/lib/types/client/ssh/detail-section.d.ts +7 -0
  20. package/lib/types/client/ssh/extension.d.ts +54 -0
  21. package/lib/types/client/ssh/log-buffer.d.ts +128 -0
  22. package/lib/types/client/ssh/log-view.d.ts +37 -0
  23. package/lib/types/client/ssh/model.d.ts +130 -0
  24. package/lib/types/client/ssh/settings-section.d.ts +134 -0
  25. package/lib/types/client/ssh/summary.d.ts +160 -0
  26. package/lib/types/client/ssh/target-form.d.ts +60 -0
  27. package/lib/types/core/action.d.ts +89 -0
  28. package/lib/types/core/adapters/claude.d.ts +15 -0
  29. package/lib/types/core/adapters/codex.d.ts +15 -0
  30. package/lib/types/core/adapters/custom.d.ts +29 -0
  31. package/lib/types/core/adapters/dsh.d.ts +21 -0
  32. package/lib/types/core/adapters/index.d.ts +25 -0
  33. package/lib/types/core/adapters/types.d.ts +76 -0
  34. package/lib/types/core/contract.d.ts +445 -0
  35. package/lib/types/core/launcher-script.d.ts +13 -0
  36. package/lib/types/core/launcher.d.ts +153 -0
  37. package/lib/types/core/ledger.d.ts +123 -0
  38. package/lib/types/core/shell.d.ts +61 -0
  39. package/lib/types/core/target.d.ts +143 -0
  40. package/lib/types/core/task-record.d.ts +63 -0
  41. package/lib/types/core/workspace.d.ts +74 -0
  42. package/lib/types/host/board.d.ts +71 -0
  43. package/lib/types/host/engine.d.ts +418 -0
  44. package/lib/types/host/extension.d.ts +59 -0
  45. package/lib/types/host/logs.d.ts +135 -0
  46. package/lib/types/host/payload.d.ts +75 -0
  47. package/lib/types/host/prompt.d.ts +44 -0
  48. package/lib/types/host/queue.d.ts +53 -0
  49. package/lib/types/host/remote.d.ts +120 -0
  50. package/lib/types/host/settings.d.ts +149 -0
  51. package/lib/types/host/timers.d.ts +20 -0
  52. package/lib/types/host/tools.d.ts +15 -0
  53. package/lib/types/host/workspace/errors.d.ts +29 -0
  54. package/lib/types/host/workspace/git.d.ts +108 -0
  55. package/lib/types/host/workspace/in-place.d.ts +36 -0
  56. package/lib/types/host/workspace/index.d.ts +28 -0
  57. package/lib/types/host/workspace/layout.d.ts +62 -0
  58. package/lib/types/host/workspace/local-git.d.ts +72 -0
  59. package/lib/types/host/workspace/remote-git.d.ts +111 -0
  60. package/lib/types/host/workspace/remote.d.ts +85 -0
  61. package/lib/types/host/workspace/types.d.ts +28 -0
  62. package/lib/types/host/workspace-seam.d.ts +100 -0
  63. package/lib/types/index.d.ts +97 -0
  64. package/lib/types/mount-once.d.ts +55 -0
  65. package/locale/en.json +6 -0
  66. package/locale/zh.json +6 -0
  67. package/package.json +89 -3
  68. package/src/client/PluginSettingsCard.tsx +838 -0
  69. package/src/client/RangeBubble.tsx +164 -0
  70. package/src/client/SegmentGroup.tsx +93 -0
  71. package/src/client/css-modules.d.ts +5 -0
  72. package/src/client/index.ts +195 -0
  73. package/src/client/locales.ts +257 -0
  74. package/src/client/seat-props.ts +58 -0
  75. package/src/client/settings-card.module.css +821 -0
  76. package/src/client/settings-entry-form.ts +141 -0
  77. package/src/client/settings-form.ts +529 -0
  78. package/src/client/ssh/card-decoration.tsx +55 -0
  79. package/src/client/ssh/compose-section.tsx +147 -0
  80. package/src/client/ssh/detail-section.tsx +345 -0
  81. package/src/client/ssh/extension.ts +271 -0
  82. package/src/client/ssh/log-buffer.ts +224 -0
  83. package/src/client/ssh/log-view.tsx +149 -0
  84. package/src/client/ssh/model.ts +299 -0
  85. package/src/client/ssh/settings-section.tsx +401 -0
  86. package/src/client/ssh/summary.ts +388 -0
  87. package/src/client/ssh/target-form.tsx +380 -0
  88. package/src/client/ssh.module.css +617 -0
  89. package/src/core/action.ts +270 -0
  90. package/src/core/adapters/claude.ts +64 -0
  91. package/src/core/adapters/codex.ts +83 -0
  92. package/src/core/adapters/custom.ts +88 -0
  93. package/src/core/adapters/dsh.ts +87 -0
  94. package/src/core/adapters/index.ts +74 -0
  95. package/src/core/adapters/types.ts +95 -0
  96. package/src/core/contract.ts +510 -0
  97. package/src/core/launcher-script.ts +313 -0
  98. package/src/core/launcher.ts +283 -0
  99. package/src/core/ledger.ts +372 -0
  100. package/src/core/shell.ts +77 -0
  101. package/src/core/target.ts +413 -0
  102. package/src/core/task-record.ts +65 -0
  103. package/src/core/workspace.ts +78 -0
  104. package/src/host/board.ts +103 -0
  105. package/src/host/engine.ts +1602 -0
  106. package/src/host/extension.ts +304 -0
  107. package/src/host/logs.ts +297 -0
  108. package/src/host/payload.ts +134 -0
  109. package/src/host/prompt.ts +89 -0
  110. package/src/host/queue.ts +73 -0
  111. package/src/host/remote.ts +328 -0
  112. package/src/host/settings.ts +294 -0
  113. package/src/host/timers.ts +33 -0
  114. package/src/host/tools.ts +259 -0
  115. package/src/host/workspace/errors.ts +43 -0
  116. package/src/host/workspace/git.ts +305 -0
  117. package/src/host/workspace/in-place.ts +58 -0
  118. package/src/host/workspace/index.ts +37 -0
  119. package/src/host/workspace/layout.ts +122 -0
  120. package/src/host/workspace/local-git.ts +164 -0
  121. package/src/host/workspace/remote-git.ts +244 -0
  122. package/src/host/workspace/remote.ts +181 -0
  123. package/src/host/workspace/types.ts +34 -0
  124. package/src/host/workspace-seam.ts +132 -0
  125. package/src/index.ts +243 -0
  126. package/src/mount-once.ts +142 -0
@@ -0,0 +1,271 @@
1
+ /**
2
+ * Browser-half installer for the SSH provider.
3
+ *
4
+ * A provider's browser half registers its own contributions into the child seats
5
+ * the board declares; it resolves the board's client capability face from the
6
+ * shared service name and adds no HTTP surface of its own. Three switches
7
+ * compose here:
8
+ *
9
+ * - the BOARD's master switch collapses the seats (the board mirror reports it,
10
+ * so the seats are rebuilt when the board comes back);
11
+ * - this EXTENSION's own volatile switch hides the seats entirely, without a
12
+ * restart, because the installer follows the settings form the section edits;
13
+ * - the settings section itself follows neither: it is where that switch lives.
14
+ *
15
+ * The board's browser half publishes its `taskBoard` service inside its own
16
+ * apply, and the aggregate mounts its client children without ordering those
17
+ * applies, so the service is frequently NOT there yet when this installer runs.
18
+ * The seats therefore live behind a cordis dependency scope (`ctx.inject`)
19
+ * instead of a one-shot lookup: the scope mounts once the board serves the
20
+ * service and unloads — releasing every seat — when the board withdraws it.
21
+ *
22
+ * @module dsh-task-board-ssh/client/ssh/extension
23
+ */
24
+ import type { Context as ClientContext } from '@deepseek-ai/cordis'
25
+ import {
26
+ resolveTaskBoardClientFace,
27
+ TASK_BOARD_CARD_DECORATION,
28
+ TASK_BOARD_COMPOSE_SECTION,
29
+ TASK_BOARD_DETAIL_SECTION,
30
+ TASK_BOARD_SETTINGS_SECTION,
31
+ } from '../../core/contract.ts'
32
+ import { SSH_EXTENSION_ID } from '../../core/action.ts'
33
+ import { SshCardDecoration } from './card-decoration.tsx'
34
+ import { SshComposeSection } from './compose-section.tsx'
35
+ import { SshDetailSection } from './detail-section.tsx'
36
+ import { acceptBoardContext, acceptPublishedSummary, clearSummary } from './summary.ts'
37
+
38
+ /** Locale namespace the seats bind to. */
39
+ const LOCALE_NS = 'task-board-ssh'
40
+
41
+ /** The live switch the installer follows; the settings section's form supplies it. */
42
+ export interface ExtensionEnabledSource {
43
+ /** Whether the extension is currently enabled (absent config means enabled). */
44
+ read(): boolean
45
+ /** Observe switch changes; the returned disposer removes the listener. */
46
+ subscribe(listener: () => void): () => void
47
+ }
48
+
49
+ /**
50
+ * The configuration section this extension contributes to the board's own
51
+ * settings card.
52
+ *
53
+ * The section is NOT gated by the extension's own switch: turning the extension
54
+ * off must not remove the control that turns it back on. It is gated by the
55
+ * board's service alone, because the board's settings card is the only surface
56
+ * that declares the seat it renders into.
57
+ */
58
+ export interface SshSettingsSectionSeat {
59
+ /** Build the face the section renders with (its staged form and actions). */
60
+ inject(): unknown
61
+ /** The section component itself. */
62
+ component: unknown
63
+ }
64
+
65
+ /**
66
+ * Install the SSH provider's browser half.
67
+ * @param ctx - client context (slot registry).
68
+ * @param source - the extension's live enabled switch.
69
+ * @param settings - the section to contribute into the board's settings card.
70
+ * @returns disposer releasing every contribution.
71
+ */
72
+ export function installSshClientHalf(
73
+ ctx: ClientContext,
74
+ source: ExtensionEnabledSource,
75
+ settings?: SshSettingsSectionSeat,
76
+ ): () => void {
77
+ /** The dependency-scoped fiber that owns the seats, while both gates are on. */
78
+ let injection: ReturnType<ClientContext['inject']> | undefined
79
+ /** The fiber that owns the board's provider-settings section, independent of the switch. */
80
+ let settingsInjection: ReturnType<ClientContext['inject']> | undefined
81
+
82
+ const install = (): void => {
83
+ if (injection !== undefined) return
84
+ // Cordis runs this callback once the board serves `taskBoard` — immediately
85
+ // if it is already there, or the moment it appears otherwise — and disposes
86
+ // the scope when the service is withdrawn or replaced, which is what
87
+ // releases the seats again.
88
+ injection = ctx.inject(['taskBoard'], (scope: ClientContext) => {
89
+ scope.effect(() => installSeats(scope), 'task-board-ssh: provider seats')
90
+ })
91
+ }
92
+
93
+ const release = (): void => {
94
+ const current = injection
95
+ injection = undefined
96
+ if (current === undefined) return
97
+ void current.dispose()
98
+ // A hidden provider publishes no summary; keep the last one from looking
99
+ // like live state on a settings page that is still open.
100
+ clearSummary()
101
+ }
102
+
103
+ const apply = (): void => {
104
+ if (source.read()) install()
105
+ else release()
106
+ }
107
+
108
+ // The provider-settings section: installed once, behind the board service,
109
+ // and left alone by the extension's own switch (see SshSettingsSectionSeat).
110
+ if (settings !== undefined) {
111
+ settingsInjection = ctx.inject(['taskBoard'], (scope: ClientContext) => {
112
+ scope.effect(() => registerSettingsSection(scope, settings), 'task-board-ssh: provider settings section')
113
+ })
114
+ }
115
+
116
+ const unsubscribe = source.subscribe(apply)
117
+ apply()
118
+
119
+ return () => {
120
+ unsubscribe()
121
+ release()
122
+ const currentSettings = settingsInjection
123
+ settingsInjection = undefined
124
+ if (currentSettings !== undefined) void currentSettings.dispose()
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Register the configuration section into the seat the board's settings card
130
+ * renders.
131
+ *
132
+ * The registration follows the seat's OWN declaration lifecycle
133
+ * (`slots.inject`), not a one-shot `register`: the board's card declares the
134
+ * seat, and re-declares it whenever it moves between plugin-card seats (the
135
+ * family group loading after boot is the normal case). Re-declaring RELEASES the
136
+ * seat's declarations together with every entry registered in it, so a one-shot
137
+ * registration is silently dropped — the section then never renders while
138
+ * nothing reports an error.
139
+ * @param ctx - the dependency-scoped context (slot registry).
140
+ * @param seat - the contribution to register.
141
+ * @returns disposer releasing the registration.
142
+ */
143
+ function registerSettingsSection(ctx: ClientContext, seat: SshSettingsSectionSeat): () => void {
144
+ const slots = ctx.slots as {
145
+ register(options: Record<string, unknown>, component: unknown): () => void
146
+ inject?(key: string, callback: () => () => void): () => void
147
+ }
148
+ const register = (): (() => void) => {
149
+ try {
150
+ const dispose = slots.register({
151
+ name: TASK_BOARD_SETTINGS_SECTION,
152
+ id: SSH_EXTENSION_ID,
153
+ locale: LOCALE_NS,
154
+ inject: seat.inject,
155
+ }, seat.component as never)
156
+ return () => {
157
+ try { dispose() } catch { /* best-effort */ }
158
+ }
159
+ } catch (error) {
160
+ console.error('[dsh-task-board-ssh] provider settings section registration failed', error)
161
+ return () => {}
162
+ }
163
+ }
164
+ if (typeof slots.inject !== 'function') {
165
+ // A shell without the declaration-tracking face: register once. The section
166
+ // renders as long as the board's card keeps its first declaration.
167
+ return register()
168
+ }
169
+ try {
170
+ return slots.inject(TASK_BOARD_SETTINGS_SECTION, register)
171
+ } catch (error) {
172
+ console.error('[dsh-task-board-ssh] provider settings section could not follow its seat', error)
173
+ return () => {}
174
+ }
175
+ }
176
+
177
+ /**
178
+ * Register the three rendering seats for as long as the board's own master switch
179
+ * is on, and mirror the published summary into the browser-side store.
180
+ * @param ctx - client context.
181
+ * @returns disposer releasing every contribution.
182
+ */
183
+ function installSeats(ctx: ClientContext): () => void {
184
+ const face = resolveTaskBoardClientFace(ctx)
185
+ if (face === undefined) {
186
+ // Unreachable while the dependency scope holds: `ctx.inject(['taskBoard'])`
187
+ // only runs this once the service is served. It stays as a guard against a
188
+ // service that answers the name without carrying the client contract, and it
189
+ // is not a dead end — cordis re-runs this effect when the implementation
190
+ // behind the name changes.
191
+ console.error('[dsh-task-board-ssh] the taskBoard service does not answer the client contract')
192
+ return () => {}
193
+ }
194
+ const slots = ctx.slots as {
195
+ register(options: Record<string, unknown>, component: unknown): () => void
196
+ inject?(key: string, callback: () => () => void): () => void
197
+ }
198
+ const seats: Array<() => void> = []
199
+ let registered = false
200
+
201
+ /** Follow a slot declaration via `slots.inject` when available, falling back to one-shot `register`. */
202
+ const followSeat = (options: Record<string, unknown>, component: unknown): (() => void) => {
203
+ const seatName = String(options.name)
204
+ const doRegister = (): (() => void) => {
205
+ try {
206
+ const dispose = slots.register(options, component)
207
+ return () => {
208
+ try { dispose() } catch { /* best-effort */ }
209
+ }
210
+ } catch (error) {
211
+ console.error(`[dsh-task-board-ssh] seat registration failed for ${seatName}`, error)
212
+ return () => {}
213
+ }
214
+ }
215
+ if (typeof slots.inject !== 'function') return doRegister()
216
+ try {
217
+ return slots.inject(seatName, doRegister)
218
+ } catch (error) {
219
+ console.error(`[dsh-task-board-ssh] seat ${seatName} could not follow its declaration`, error)
220
+ return doRegister()
221
+ }
222
+ }
223
+
224
+ const ensure = (): void => {
225
+ if (registered) return
226
+ const next: Array<() => void> = []
227
+ try {
228
+ next.push(followSeat({ name: TASK_BOARD_DETAIL_SECTION, id: SSH_EXTENSION_ID, locale: LOCALE_NS }, SshDetailSection as never))
229
+ next.push(followSeat({ name: TASK_BOARD_CARD_DECORATION, id: SSH_EXTENSION_ID }, SshCardDecoration as never))
230
+ next.push(followSeat({ name: TASK_BOARD_COMPOSE_SECTION, id: SSH_EXTENSION_ID, locale: LOCALE_NS }, SshComposeSection as never))
231
+ seats.push(...next)
232
+ registered = true
233
+ } catch (error) {
234
+ for (const dispose of next) {
235
+ try { dispose() } catch { /* best-effort */ }
236
+ }
237
+ console.error('[dsh-task-board-ssh] seat registration failed', error)
238
+ }
239
+ }
240
+ const release = (): void => {
241
+ registered = false
242
+ for (const dispose of seats.splice(0)) {
243
+ try { dispose() } catch { /* best-effort */ }
244
+ }
245
+ }
246
+
247
+ const applyMirror = (mirror: ReturnType<typeof face.snapshot>): void => {
248
+ if (mirror.enabled) ensure()
249
+ else release()
250
+ acceptBoardContext({
251
+ ...(mirror.workspaces === undefined ? {} : { workspaces: mirror.workspaces }),
252
+ lastError: mirror.lastError,
253
+ })
254
+ // Before the first authoritative Host snapshot arrives (`hydrated === false`),
255
+ // `mirror.extensions` is an empty initial placeholder; wait for hydration
256
+ // instead of flashing a false "host half unmounted" state on cold start.
257
+ if (mirror.hydrated !== false) {
258
+ acceptPublishedSummary(mirror.extensions)
259
+ }
260
+ }
261
+
262
+ const unsubscribe = face.subscribe(applyMirror)
263
+ // Establish immediately when the board is already running; the mirror
264
+ // subscription covers the enable transition that rebuilds the seats.
265
+ applyMirror(face.snapshot())
266
+
267
+ return () => {
268
+ unsubscribe()
269
+ release()
270
+ }
271
+ }
@@ -0,0 +1,224 @@
1
+ /**
2
+ * The run log: byte-offset increments in, one bounded window of rows out.
3
+ *
4
+ * The provider publishes log INCREMENTS, not documents: each window carries
5
+ * `[baseOffset, endOffset)` and the bytes it read since the previous window. This
6
+ * module turns that stream into lines without ever re-walking text it has
7
+ * already seen — an append splits only the incoming chunk and the partial line
8
+ * it continues — and hands the renderer a fixed-height window of that line
9
+ * store, so a hundred thousand lines cost a hundred thousand short strings and
10
+ * a few dozen DOM rows instead of a hundred thousand nodes.
11
+ *
12
+ * Two rules keep the stream honest:
13
+ *
14
+ * - a window whose `endOffset` is not past what was consumed is a duplicate and
15
+ * changes nothing;
16
+ * - a window that does not continue at the consumed offset left bytes unseen
17
+ * (the provider restarted its read above zero), so the buffer keeps what it
18
+ * has, flags the gap, and appends after it. Nothing already shown is dropped;
19
+ * the notice under the log says earlier output is missing.
20
+ *
21
+ * Virtualization by fixed row height is the mechanism chosen here: log rows are
22
+ * monospace and never wrap, so a row's height is a constant and the window is
23
+ * arithmetic rather than measurement — no layout read on scroll, no observer,
24
+ * and a deterministic window the tests can state exactly.
25
+ *
26
+ * @module dsh-task-board-ssh/client/ssh/log-buffer
27
+ */
28
+
29
+ /** One increment of a run's remote stdout, as the provider publishes it. */
30
+ export interface SshLogFrame {
31
+ /** Run the bytes belong to; a different run resets the buffer. */
32
+ runId?: string
33
+ /** Byte offset of `text`'s first byte in the remote file. */
34
+ baseOffset: number
35
+ /** Byte offset just past `text`'s last byte. */
36
+ endOffset: number
37
+ /** The bytes themselves, verbatim. */
38
+ text: string
39
+ /** Legacy flag: whether bytes before `baseOffset` were skipped (unless `hasMore` is set). */
40
+ truncated?: boolean
41
+ /** Explicit flag from Host: more trailing bytes exist after `endOffset` (NOT missing earlier output). */
42
+ hasMore?: boolean
43
+ }
44
+
45
+ /** Drop the first `from` UTF-8 bytes of `text` without splitting multi-byte code points. */
46
+ function sliceTextFromByte(text: string, from: number): string {
47
+ if (from <= 0) return text
48
+ const encoder = new TextEncoder()
49
+ let bytes = 0
50
+ let index = 0
51
+ while (index < text.length) {
52
+ const code = text.codePointAt(index) ?? 0
53
+ const character = String.fromCodePoint(code)
54
+ const size = encoder.encode(character).length
55
+ if (bytes + size > from) break
56
+ bytes += size
57
+ index += character.length
58
+ }
59
+ return text.slice(index)
60
+ }
61
+
62
+ /** What a renderer reads out of the buffer. */
63
+ export interface SshLogSnapshot {
64
+ /** Run the lines belong to, absent while the buffer is empty. */
65
+ runId?: string
66
+ /** How many rows the buffer holds. */
67
+ count: number
68
+ /** Whether bytes before the first line are known to be missing. */
69
+ truncated: boolean
70
+ /** Byte offset of the first byte held. */
71
+ start: number
72
+ /** Byte offset just past the last byte consumed. */
73
+ end: number
74
+ }
75
+
76
+ /** Geometry of the log viewport. */
77
+ export interface LogViewport {
78
+ /** Current vertical scroll offset of the scroller, in pixels. */
79
+ scrollTop: number
80
+ /** Visible height of the scroller, in pixels. */
81
+ height: number
82
+ /** Height of every row, in pixels. */
83
+ rowHeight: number
84
+ /** Rows rendered above and below the visible band. */
85
+ overscan: number
86
+ }
87
+
88
+ /** The rows a renderer actually mounts. */
89
+ export interface LogWindow {
90
+ /** First mounted row index. */
91
+ start: number
92
+ /** One past the last mounted row index. */
93
+ end: number
94
+ /** Vertical offset of the mounted block inside the scroll surface. */
95
+ offsetY: number
96
+ /** Full height of the scroll surface, so the scrollbar reflects every line. */
97
+ totalHeight: number
98
+ /** How many rows exist in total. */
99
+ total: number
100
+ }
101
+
102
+ /**
103
+ * Accumulates log increments into lines.
104
+ *
105
+ * One instance belongs to one open detail panel; closing the panel drops it,
106
+ * which is what makes reopening replay from the first byte — the buffer asks
107
+ * the provider for that replay and renders whatever windows arrive.
108
+ */
109
+ export class SshLogBuffer {
110
+ private run: string | undefined
111
+ private startOffset = 0
112
+ private endOffset = 0
113
+ private rows: string[] = []
114
+ private tail: string | undefined
115
+ private gap = false
116
+
117
+ /** How many rows the buffer holds, partial trailing line included. */
118
+ get count(): number {
119
+ return this.tail === undefined ? this.rows.length : this.rows.length + 1
120
+ }
121
+
122
+ /**
123
+ * Apply one published increment.
124
+ * @param frame - the increment; duplicates and replays are tolerated.
125
+ * @returns true when the visible content changed.
126
+ */
127
+ append(frame: SshLogFrame): boolean {
128
+ if (frame.runId !== undefined && this.run !== undefined && frame.runId !== this.run) {
129
+ this.reset(frame.runId)
130
+ } else if (frame.runId !== undefined && this.run === undefined) {
131
+ this.run = frame.runId
132
+ }
133
+ if (frame.text.length === 0) return false
134
+ if (frame.endOffset <= this.endOffset) return false
135
+ let incoming = frame.text
136
+ if (this.count === 0 && frame.baseOffset > 0) this.gap = true
137
+ else if (this.endOffset > 0 && frame.baseOffset > this.endOffset) this.gap = true
138
+ else if (this.endOffset > 0 && frame.baseOffset < this.endOffset) {
139
+ incoming = sliceTextFromByte(incoming, this.endOffset - frame.baseOffset)
140
+ if (incoming.length === 0) return false
141
+ }
142
+ // `hasMore: true` means more tail bytes remain to be paged/polled — NOT that
143
+ // earlier output before `baseOffset` was skipped. Only flag `gap` when a
144
+ // legacy frame sets `truncated` without `hasMore`.
145
+ if (frame.truncated === true && frame.hasMore !== true) this.gap = true
146
+ this.startOffset = this.count === 0 ? frame.baseOffset : Math.min(this.startOffset, frame.baseOffset)
147
+ this.endOffset = frame.endOffset
148
+ const combined = (this.tail ?? '') + incoming
149
+ let from = 0
150
+ for (;;) {
151
+ const newline = combined.indexOf('\n', from)
152
+ if (newline === -1) break
153
+ this.rows.push(combined.slice(from, newline))
154
+ from = newline + 1
155
+ }
156
+ this.tail = from < combined.length ? combined.slice(from) : undefined
157
+ return true
158
+ }
159
+
160
+ /**
161
+ * Read one row.
162
+ * @param index - zero-based row index below {@link count}.
163
+ * @returns the row text; the empty string outside the buffer.
164
+ */
165
+ lineAt(index: number): string {
166
+ if (index < this.rows.length) return this.rows[index] as string
167
+ if (index === this.rows.length && this.tail !== undefined) return this.tail
168
+ return ''
169
+ }
170
+
171
+ /**
172
+ * Copy every row out; for tests and small surfaces, never for a live render.
173
+ * @returns the rows, oldest first.
174
+ */
175
+ toLines(): string[] {
176
+ return this.tail === undefined ? [...this.rows] : [...this.rows, this.tail]
177
+ }
178
+
179
+ /** Drop every line; a later replay starts from the first byte again. */
180
+ reset(runId?: string): void {
181
+ this.run = runId
182
+ this.startOffset = 0
183
+ this.endOffset = 0
184
+ this.rows = []
185
+ this.tail = undefined
186
+ this.gap = false
187
+ }
188
+
189
+ /** Read the buffer's account of itself. */
190
+ snapshot(): SshLogSnapshot {
191
+ const snapshot: SshLogSnapshot = {
192
+ count: this.count,
193
+ truncated: this.gap,
194
+ start: this.startOffset,
195
+ end: this.endOffset,
196
+ }
197
+ if (this.run !== undefined) snapshot.runId = this.run
198
+ return snapshot
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Compute the rows to mount for one scroll position.
204
+ *
205
+ * Pure arithmetic over a fixed row height: the tests state the window for a
206
+ * given geometry exactly, and the component never reads layout to decide what
207
+ * to render.
208
+ * @param total - how many rows exist.
209
+ * @param viewport - scroller geometry and overscan.
210
+ * @returns the mounted block and the scroll surface it sits in.
211
+ */
212
+ export function visibleLogWindow(total: number, viewport: LogViewport): LogWindow {
213
+ const rowHeight = viewport.rowHeight > 0 ? viewport.rowHeight : 1
214
+ const overscan = viewport.overscan > 0 ? viewport.overscan : 0
215
+ const totalHeight = total * rowHeight
216
+ if (total <= 0) return { start: 0, end: 0, offsetY: 0, totalHeight, total }
217
+ const maxScroll = Math.max(0, totalHeight - Math.max(0, viewport.height))
218
+ const scrollTop = Math.min(Math.max(0, viewport.scrollTop), maxScroll)
219
+ const first = Math.floor(scrollTop / rowHeight)
220
+ const visible = Math.max(1, Math.ceil(Math.max(0, viewport.height) / rowHeight))
221
+ const start = Math.max(0, Math.min(total - 1, first - overscan))
222
+ const end = Math.min(total, first + visible + overscan)
223
+ return { start, end, offsetY: start * rowHeight, totalHeight, total }
224
+ }
@@ -0,0 +1,149 @@
1
+ /**
2
+ * The run log surface: a fixed-height, keyboard-reachable scroller that mounts
3
+ * only the rows in view.
4
+ *
5
+ * The window comes from {@link visibleLogWindow}'s arithmetic, so a scroll costs
6
+ * one state update and a few dozen rows, never a re-layout of the whole log.
7
+ * While the run is live the surface follows the tail; scrolling up stops
8
+ * following — a reader is never yanked away from what they are reading — and a
9
+ * control returns them to the newest line.
10
+ *
11
+ * The scroller owns its height in pixels rather than measuring one, so the
12
+ * window is a function of the scroll offset alone: no layout read on the scroll
13
+ * path, no resize observer, and a geometry the tests state exactly.
14
+ *
15
+ * @module dsh-task-board-ssh/client/ssh/log-view
16
+ */
17
+ import { type UIEvent, useCallback, useEffect, useRef, useState } from 'react'
18
+ import { visibleLogWindow, type SshLogBuffer } from './log-buffer.ts'
19
+ import css from '../ssh.module.css'
20
+
21
+ /** Height of one log row in pixels; every row is one line of monospace text. */
22
+ export const LOG_ROW_HEIGHT = 18
23
+
24
+ /** How many rows the scroller shows at once. */
25
+ export const LOG_VISIBLE_ROWS = 12
26
+
27
+ /** Height of the scroller in pixels, derived from the two constants above. */
28
+ export const LOG_VIEWPORT_HEIGHT = LOG_ROW_HEIGHT * LOG_VISIBLE_ROWS
29
+
30
+ /** Rows mounted above and below the visible band. */
31
+ const LOG_OVERSCAN = 8
32
+
33
+ /** Copy the log surface needs, localized by the caller. */
34
+ export interface SshLogLabels {
35
+ /** Shown while nothing has been received. */
36
+ empty: string
37
+ /** Shown when bytes before the first line are known to be missing. */
38
+ truncated: string
39
+ /** Control that returns the scroller to the newest line. */
40
+ latest: string
41
+ /** Accessible name of the scroller itself. */
42
+ name: string
43
+ }
44
+
45
+ /** Props of the log surface. */
46
+ export interface SshLogViewProps {
47
+ /** The buffer the parent owns for this panel. */
48
+ buffer: SshLogBuffer
49
+ /** Bumped whenever the buffer changed; it is what makes the view re-render. */
50
+ version: number
51
+ /** Whether the run is still producing output, so the tail follows. */
52
+ live: boolean
53
+ /** Identity key (e.g. task + run) that resets scroll position and follow state when changed. */
54
+ resetKey?: string
55
+ /** Localized copy. */
56
+ labels: SshLogLabels
57
+ }
58
+
59
+ /**
60
+ * Render the run log.
61
+ * @param props - the buffer, its revision, and the copy it reads.
62
+ * @returns the scroller, its notices and the return-to-tail control.
63
+ */
64
+ export function SshLogView({ buffer, live, labels, resetKey, version }: SshLogViewProps) {
65
+ const scroller = useRef<HTMLDivElement | null>(null)
66
+ const [scrollTop, setScrollTop] = useState(0)
67
+ const [following, setFollowing] = useState(true)
68
+
69
+ useEffect(() => {
70
+ setScrollTop(0)
71
+ setFollowing(true)
72
+ if (scroller.current !== null) scroller.current.scrollTop = 0
73
+ }, [resetKey])
74
+ const snapshot = buffer.snapshot()
75
+ const count = snapshot.count
76
+ const maxScroll = Math.max(0, count * LOG_ROW_HEIGHT - LOG_VIEWPORT_HEIGHT)
77
+ const window = visibleLogWindow(count, {
78
+ scrollTop: following ? maxScroll : scrollTop,
79
+ height: LOG_VIEWPORT_HEIGHT,
80
+ rowHeight: LOG_ROW_HEIGHT,
81
+ overscan: LOG_OVERSCAN,
82
+ })
83
+
84
+ // A live run keeps its newest line in view without moving a reader who
85
+ // scrolled away from it.
86
+ useEffect(() => {
87
+ if (!following) return
88
+ const element = scroller.current
89
+ if (element === null) return
90
+ element.scrollTop = maxScroll
91
+ setScrollTop(maxScroll)
92
+ }, [version, following, maxScroll])
93
+
94
+ const onScroll = useCallback((event: UIEvent<HTMLDivElement>) => {
95
+ const top = event.currentTarget.scrollTop
96
+ setScrollTop(top)
97
+ setFollowing(maxScroll - top <= LOG_ROW_HEIGHT)
98
+ }, [maxScroll])
99
+
100
+ const jumpToLatest = useCallback(() => { setFollowing(true) }, [])
101
+
102
+ // An empty log is one line, not a bordered box: a run that has printed
103
+ // nothing yet should not own a third of the panel.
104
+ if (count === 0) {
105
+ return (
106
+ <div className={css.log} data-dsh-part="log">
107
+ <p className={css.logEmpty}>{labels.empty}</p>
108
+ </div>
109
+ )
110
+ }
111
+
112
+ return (
113
+ <div className={css.log} data-dsh-part="log">
114
+ <div
115
+ ref={scroller}
116
+ className={css.logScroller}
117
+ style={{ height: `${LOG_VIEWPORT_HEIGHT}px` }}
118
+ tabIndex={0}
119
+ role="group"
120
+ aria-label={labels.name}
121
+ aria-live="off"
122
+ onScroll={onScroll}
123
+ >
124
+ <div className={css.logSurface} style={{ height: `${window.totalHeight}px` }}>
125
+ <div className={css.logBlock} style={{ transform: `translateY(${window.offsetY}px)` }}>
126
+ {Array.from({ length: window.end - window.start }, (_unused, offset) => {
127
+ const index = window.start + offset
128
+ return (
129
+ <div key={index} className={css.logRow} style={{ height: `${LOG_ROW_HEIGHT}px` }}>
130
+ {buffer.lineAt(index)}
131
+ </div>
132
+ )
133
+ })}
134
+ </div>
135
+ </div>
136
+ </div>
137
+ {(snapshot.truncated || (!following && live)) && (
138
+ <div className={css.logNotices}>
139
+ {snapshot.truncated && <p className={css.logNotice}>{labels.truncated}</p>}
140
+ {!following && live && (
141
+ <button type="button" className={css.linkButton} onClick={jumpToLatest}>
142
+ {labels.latest}
143
+ </button>
144
+ )}
145
+ </div>
146
+ )}
147
+ </div>
148
+ )
149
+ }