@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,270 @@
1
+ /**
2
+ * The action protocol between this provider's browser half and its host half.
3
+ *
4
+ * The browser never calls the host directly: it hands an envelope to the
5
+ * board's same-origin `dispatch`, the board forwards it as
6
+ * `{ extensionId, action, taskId?, payload? }`, and this module turns that
7
+ * untrusted envelope into a typed request. Everything arrives from outside the
8
+ * process, so parsing reports every problem with its path instead of throwing;
9
+ * a malformed action must never become an exception in the board's channel.
10
+ *
11
+ * @module dsh-task-board-ssh/core/action
12
+ */
13
+ import { TASK_BOARD_MESSAGE_MAX_LENGTH } from './contract.ts'
14
+ import { parseSshTarget, type SshTarget, type SshValidationIssue, type SshValidationResult } from './target.ts'
15
+
16
+ /** Extension id the board routes this provider's actions under. */
17
+ export const SSH_EXTENSION_ID = 'ssh'
18
+
19
+ /** Every action this provider accepts, in the order the card surface offers them. */
20
+ export const SSH_ACTION_NAMES = ['run', 'cancel', 'follow-up', 'rework', 'set-target', 'refresh'] as const
21
+
22
+ /** One action name. */
23
+ export type SshActionName = (typeof SSH_ACTION_NAMES)[number]
24
+
25
+ /** Start the pinned card's remote run now. */
26
+ export interface SshRunActionRequest {
27
+ /** Action discriminator. */
28
+ action: 'run'
29
+ /** Card to run. */
30
+ taskId: string
31
+ }
32
+
33
+ /** Stop the card's active remote run through the launcher's cancel verb. */
34
+ export interface SshCancelActionRequest {
35
+ /** Action discriminator. */
36
+ action: 'cancel'
37
+ /** Card whose run to stop. */
38
+ taskId: string
39
+ }
40
+
41
+ /** Deliver one follow-up user message into the card's remote agent session. */
42
+ export interface SshFollowUpActionRequest {
43
+ /** Action discriminator. */
44
+ action: 'follow-up'
45
+ /** Card whose session receives the message. */
46
+ taskId: string
47
+ /** Message text, bounded like every board message. */
48
+ text: string
49
+ }
50
+
51
+ /** Send the card back with rework instructions, in the same remote session. */
52
+ export interface SshReworkActionRequest {
53
+ /** Action discriminator. */
54
+ action: 'rework'
55
+ /** Card to send back. */
56
+ taskId: string
57
+ /** Rework instructions, bounded like every board message. */
58
+ instructions: string
59
+ }
60
+
61
+ /** Pin or replace the card's execution target. */
62
+ export interface SshSetTargetActionRequest {
63
+ /** Action discriminator. */
64
+ action: 'set-target'
65
+ /** Card to retarget. */
66
+ taskId: string
67
+ /** The target to pin. */
68
+ target: SshTarget
69
+ }
70
+
71
+ /** Re-read a run's remote state (the card's poll bypass, and the settings probe). */
72
+ export interface SshRefreshActionRequest {
73
+ /** Action discriminator. */
74
+ action: 'refresh'
75
+ /** Card to refresh, or omitted to refresh every targeted card. */
76
+ taskId?: string
77
+ /** One run of that card to re-read, or omitted to re-read whatever is active. */
78
+ runId?: string
79
+ /** Byte offset to replay the run's log from (defaults to 0). */
80
+ offset?: number
81
+ }
82
+
83
+ /** One request this provider's host half accepts. */
84
+ export type SshActionRequest =
85
+ | SshRunActionRequest
86
+ | SshCancelActionRequest
87
+ | SshFollowUpActionRequest
88
+ | SshReworkActionRequest
89
+ | SshSetTargetActionRequest
90
+ | SshRefreshActionRequest
91
+
92
+ /** A successful reply, as the browser half reads it. */
93
+ export interface SshActionReplyOk {
94
+ /** Discriminator. */
95
+ ok: true
96
+ /** Card the reply is about, when it is about one. */
97
+ taskId?: string
98
+ /** Run the reply is about, when it is about one. */
99
+ runId?: string
100
+ /** One sentence the card shows next to the run state. */
101
+ message?: string
102
+ }
103
+
104
+ /** A refused reply; `issues` carries the field diagnostics when there are any. */
105
+ export interface SshActionReplyFailure {
106
+ /** Discriminator. */
107
+ ok: false
108
+ /** One sentence a person can act on. */
109
+ error: string
110
+ /** Field-level diagnostics, when the refusal came from a malformed request. */
111
+ issues?: readonly SshValidationIssue[]
112
+ }
113
+
114
+ /** What an action returns; never a throw, so the browser always has a value. */
115
+ export type SshActionReply = SshActionReplyOk | SshActionReplyFailure
116
+
117
+ /** Longest accepted id (task or run), in characters. */
118
+ const ID_MAX_LENGTH = 128
119
+
120
+ /** Collects diagnostics while one envelope is parsed. */
121
+ class ActionIssues {
122
+ readonly errors: SshValidationIssue[] = []
123
+ add(path: string, message: string): void {
124
+ this.errors.push({ path, message })
125
+ }
126
+ }
127
+
128
+ /** Whether a value is a plain object (not an array, not null, no exotic prototype). */
129
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
130
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) return false
131
+ const prototype = Object.getPrototypeOf(value)
132
+ return prototype === Object.prototype || prototype === null
133
+ }
134
+
135
+ /** Join one path segment onto a parent path. */
136
+ function pathOf(parent: string, key: string | number): string {
137
+ return parent === '' ? String(key) : `${parent}.${String(key)}`
138
+ }
139
+
140
+ /** Read one bounded identifier. */
141
+ function readId(value: unknown, path: string, issues: ActionIssues, required: boolean): string | undefined {
142
+ if (value === undefined) {
143
+ if (required) issues.add(path, 'is required')
144
+ return undefined
145
+ }
146
+ if (typeof value !== 'string') {
147
+ issues.add(path, 'must be a string')
148
+ return undefined
149
+ }
150
+ const text = value.trim()
151
+ if (text.length === 0) {
152
+ issues.add(path, 'must not be empty')
153
+ return undefined
154
+ }
155
+ if (text.length > ID_MAX_LENGTH) {
156
+ issues.add(path, `must be at most ${ID_MAX_LENGTH} characters`)
157
+ return undefined
158
+ }
159
+ if (/\s/.test(text)) {
160
+ issues.add(path, 'must not contain whitespace')
161
+ return undefined
162
+ }
163
+ return text
164
+ }
165
+
166
+ /** Read one bounded free-text message. */
167
+ function readMessage(value: unknown, path: string, issues: ActionIssues): string | undefined {
168
+ if (typeof value !== 'string') {
169
+ issues.add(path, 'must be a string')
170
+ return undefined
171
+ }
172
+ if (value.trim().length === 0) {
173
+ issues.add(path, 'must not be empty')
174
+ return undefined
175
+ }
176
+ if (value.length > TASK_BOARD_MESSAGE_MAX_LENGTH) {
177
+ issues.add(path, `must be at most ${TASK_BOARD_MESSAGE_MAX_LENGTH} characters`)
178
+ return undefined
179
+ }
180
+ return value
181
+ }
182
+
183
+ /** Refuse keys the action's payload does not define. */
184
+ function rejectUnknownPayloadKeys(payload: Record<string, unknown>, allowed: readonly string[], issues: ActionIssues): void {
185
+ for (const key of Object.keys(payload)) {
186
+ if (!allowed.includes(key)) issues.add(pathOf('payload', key), `unknown field (accepted: ${allowed.join(', ')})`)
187
+ }
188
+ }
189
+
190
+ /**
191
+ * Parse one dispatched envelope.
192
+ * @param value - candidate request as the board forwards it.
193
+ * @returns the typed request, or every reason it was refused.
194
+ */
195
+ export function parseSshActionRequest(value: unknown): SshValidationResult<SshActionRequest> {
196
+ const issues = new ActionIssues()
197
+ if (!isPlainObject(value)) return { ok: false, errors: [{ path: '', message: 'must be an object' }] }
198
+ for (const key of Object.keys(value)) {
199
+ if (!['extensionId', 'action', 'taskId', 'payload'].includes(key)) {
200
+ issues.add(key, `unknown field (accepted: extensionId, action, taskId, payload)`)
201
+ }
202
+ }
203
+ if (value.extensionId !== SSH_EXTENSION_ID) {
204
+ issues.add('extensionId', `must be "${SSH_EXTENSION_ID}"`)
205
+ }
206
+ const action = typeof value.action === 'string' && (SSH_ACTION_NAMES as readonly string[]).includes(value.action)
207
+ ? (value.action as SshActionName)
208
+ : undefined
209
+ if (action === undefined) {
210
+ issues.add('action', `must be one of: ${SSH_ACTION_NAMES.join(', ')}`)
211
+ return { ok: false, errors: issues.errors }
212
+ }
213
+ const payload = value.payload === undefined ? {} : value.payload
214
+ if (!isPlainObject(payload)) {
215
+ issues.add('payload', 'must be an object')
216
+ return { ok: false, errors: issues.errors }
217
+ }
218
+ const taskId = readId(value.taskId, 'taskId', issues, action !== 'refresh')
219
+
220
+ switch (action) {
221
+ case 'run':
222
+ case 'cancel': {
223
+ rejectUnknownPayloadKeys(payload, [], issues)
224
+ if (issues.errors.length > 0) return { ok: false, errors: issues.errors }
225
+ return { ok: true, value: { action, taskId: taskId as string } }
226
+ }
227
+ case 'follow-up': {
228
+ rejectUnknownPayloadKeys(payload, ['text'], issues)
229
+ const text = readMessage(payload.text, 'payload.text', issues)
230
+ if (issues.errors.length > 0) return { ok: false, errors: issues.errors }
231
+ return { ok: true, value: { action, taskId: taskId as string, text: text as string } }
232
+ }
233
+ case 'rework': {
234
+ rejectUnknownPayloadKeys(payload, ['instructions'], issues)
235
+ const instructions = readMessage(payload.instructions, 'payload.instructions', issues)
236
+ if (issues.errors.length > 0) return { ok: false, errors: issues.errors }
237
+ return { ok: true, value: { action, taskId: taskId as string, instructions: instructions as string } }
238
+ }
239
+ case 'set-target': {
240
+ rejectUnknownPayloadKeys(payload, ['target'], issues)
241
+ const parsed = parseSshTarget(payload.target)
242
+ if (!parsed.ok) {
243
+ for (const issue of parsed.errors) {
244
+ issues.add(issue.path === '' ? 'payload.target' : pathOf('payload.target', issue.path), issue.message)
245
+ }
246
+ }
247
+ if (issues.errors.length > 0) return { ok: false, errors: issues.errors }
248
+ if (!parsed.ok) return { ok: false, errors: issues.errors }
249
+ return { ok: true, value: { action, taskId: taskId as string, target: parsed.value } }
250
+ }
251
+ default: {
252
+ rejectUnknownPayloadKeys(payload, ['runId', 'offset'], issues)
253
+ const runId = readId(payload.runId, 'payload.runId', issues, false)
254
+ let offset: number | undefined
255
+ if (payload.offset !== undefined) {
256
+ if (typeof payload.offset !== 'number' || !Number.isInteger(payload.offset) || payload.offset < 0) {
257
+ issues.add('payload.offset', 'must be a non-negative integer')
258
+ } else {
259
+ offset = payload.offset
260
+ }
261
+ }
262
+ if (issues.errors.length > 0) return { ok: false, errors: issues.errors }
263
+ const refresh: SshRefreshActionRequest = { action: 'refresh' }
264
+ if (taskId !== undefined) refresh.taskId = taskId
265
+ if (runId !== undefined) refresh.runId = runId
266
+ if (offset !== undefined) refresh.offset = offset
267
+ return { ok: true, value: refresh }
268
+ }
269
+ }
270
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * The Claude Code adapter.
3
+ *
4
+ * Flags come from this machine's `claude --help` (2.1.319): `-p`/`--print`
5
+ * runs one task without the TUI, `--output-format stream-json` with
6
+ * `--verbose` writes one JSON object per line, `--dangerously-skip-permissions`
7
+ * is the bypass flag, and `--resume <id>` continues a session. The session id
8
+ * arrives twice — on the `system/init` event and on the closing `result` — and
9
+ * the summary is the `result` event's `result` field.
10
+ *
11
+ * @module dsh-task-board-ssh/core/adapters/claude
12
+ */
13
+ import { shellJoin } from '../shell.ts'
14
+ import { readJsonLines, readString, type SshAgentCommand, type SshAgentOutput, type SshAgentRunInput } from './types.ts'
15
+
16
+ /** Program named on the remote host. */
17
+ export const CLAUDE_PROGRAM = 'claude'
18
+
19
+ /**
20
+ * Build the Claude Code command line for one run.
21
+ * @param input - run facts.
22
+ * @returns the quoted command line, reading the prompt from stdin.
23
+ */
24
+ export function claudeAgentCommand(input: SshAgentRunInput): SshAgentCommand {
25
+ const argv = [
26
+ CLAUDE_PROGRAM,
27
+ '-p',
28
+ '--output-format',
29
+ 'stream-json',
30
+ '--verbose',
31
+ '--dangerously-skip-permissions',
32
+ ]
33
+ if (input.resumeSessionId !== undefined) argv.push('--resume', input.resumeSessionId)
34
+ return { command: shellJoin(argv), stdinFile: input.promptFile }
35
+ }
36
+
37
+ /**
38
+ * Read the session id and the final summary out of a Claude Code stream.
39
+ * @param output - captured stdout of one run.
40
+ * @returns whatever the stream reported.
41
+ */
42
+ export function readClaudeOutput(output: string): SshAgentOutput {
43
+ const result: SshAgentOutput = {}
44
+ for (const event of readJsonLines(output)) {
45
+ const type = readString(event, 'type')
46
+ if (type === 'system' && readString(event, 'subtype') === 'init' && result.sessionId === undefined) {
47
+ result.sessionId = readString(event, 'session_id')
48
+ continue
49
+ }
50
+ if (type === 'result') {
51
+ const sessionId = readString(event, 'session_id')
52
+ if (sessionId !== undefined) result.sessionId = sessionId
53
+ const text = readString(event, 'result') ?? ''
54
+ const isError = event.is_error === true
55
+ result.outcome = isError
56
+ ? { ok: false, error: text.length > 0 ? text : `the run ended with subtype "${readString(event, 'subtype') ?? 'error'}"` }
57
+ : { ok: true, text }
58
+ }
59
+ }
60
+ if (result.outcome === undefined) {
61
+ result.outcome = { ok: false, error: 'the Claude Code stream ended without a result event' }
62
+ }
63
+ return result
64
+ }
@@ -0,0 +1,83 @@
1
+ /**
2
+ * The Codex adapter.
3
+ *
4
+ * NOT VERIFIED ON A MACHINE THAT HAS CODEX: this repository's host does not
5
+ * install it, so these flags are written from the official documentation
6
+ * (`codex exec` non-interactive mode) and the event names come from its
7
+ * published sample stream. Every assertion about Codex in this package is
8
+ * therefore fixture-tested against that documented shape only, and the first
9
+ * run on a host that has Codex must confirm or correct all of it.
10
+ *
11
+ * What the documentation pins: `codex exec --json` writes newline-delimited
12
+ * events (`thread.started`, `turn.started`, `item.*`, `turn.completed`,
13
+ * `turn.failed`, `error`), the thread id is `thread_id` on `thread.started`,
14
+ * the final message is an `item.completed` with `item.type == "agent_message"`,
15
+ * `--sandbox danger-full-access` is the unrestricted sandbox, `--full-auto` is
16
+ * retired, `--skip-git-repo-check` lifts the "must run inside a git repository"
17
+ * gate an in-place workspace may not satisfy, and `codex exec resume <id>`
18
+ * continues a session.
19
+ *
20
+ * @module dsh-task-board-ssh/core/adapters/codex
21
+ */
22
+ import { shellJoin } from '../shell.ts'
23
+ import { readJsonLines, readString, type SshAgentCommand, type SshAgentOutput, type SshAgentRunInput } from './types.ts'
24
+
25
+ /** Program named on the remote host. */
26
+ export const CODEX_PROGRAM = 'codex'
27
+
28
+ /**
29
+ * Build the Codex command line for one run.
30
+ * @param input - run facts.
31
+ * @returns the quoted command line, reading the prompt from stdin.
32
+ */
33
+ export function codexAgentCommand(input: SshAgentRunInput): SshAgentCommand {
34
+ const argv = [
35
+ CODEX_PROGRAM,
36
+ 'exec',
37
+ '--json',
38
+ '--sandbox',
39
+ 'danger-full-access',
40
+ '--skip-git-repo-check',
41
+ ]
42
+ if (input.resumeSessionId !== undefined) argv.push('resume', input.resumeSessionId)
43
+ return { command: shellJoin(argv), stdinFile: input.promptFile }
44
+ }
45
+
46
+ /**
47
+ * Read the thread id and the final message out of a Codex event stream.
48
+ * @param output - captured stdout of one run.
49
+ * @returns whatever the stream reported; unverified against a real binary.
50
+ */
51
+ export function readCodexOutput(output: string): SshAgentOutput {
52
+ const result: SshAgentOutput = {}
53
+ let lastMessage: string | undefined
54
+ for (const event of readJsonLines(output)) {
55
+ const type = readString(event, 'type')
56
+ if (type === 'thread.started') {
57
+ result.sessionId = readString(event, 'thread_id')
58
+ continue
59
+ }
60
+ if (type === 'item.completed') {
61
+ const item = event.item
62
+ if (readString(item, 'type') === 'agent_message') {
63
+ const text = readString(item, 'text')
64
+ if (text !== undefined) lastMessage = text
65
+ }
66
+ continue
67
+ }
68
+ if (type === 'turn.completed' && lastMessage !== undefined) {
69
+ result.outcome = { ok: true, text: lastMessage }
70
+ continue
71
+ }
72
+ if (type === 'turn.failed' || type === 'error') {
73
+ const message = readString(event, 'message') ?? readString(event.error, 'message')
74
+ result.outcome = { ok: false, error: message ?? 'the run reported a failure' }
75
+ }
76
+ }
77
+ if (result.outcome === undefined) {
78
+ result.outcome = lastMessage === undefined
79
+ ? { ok: false, error: 'the Codex event stream ended without an agent message' }
80
+ : { ok: true, text: lastMessage }
81
+ }
82
+ return result
83
+ }
@@ -0,0 +1,88 @@
1
+ /**
2
+ * The custom adapter: a user-written command template.
3
+ *
4
+ * The three placeholders are the whole contract. `{prompt_file}` (required) is
5
+ * the remote prompt file, `{cwd}` is the run's working directory and
6
+ * `{session}` is the session to continue; each is replaced by one quoted word,
7
+ * so a template can never splice a path or a session id into the remote shell.
8
+ * The template's own words stay exactly as the deployment wrote them: they are
9
+ * the operator's command, not a value this package injects.
10
+ * No output is parsed: a command this package does not know cannot have its
11
+ * summary or its session id read, so a custom run records its exit code and its
12
+ * log and nothing else.
13
+ *
14
+ * @module dsh-task-board-ssh/core/adapters/custom
15
+ */
16
+ import { shellQuote } from '../shell.ts'
17
+ import type { SshAgentCommand, SshAgentRunInput, SshCustomTemplates } from './types.ts'
18
+
19
+ /** Placeholders a template may carry. */
20
+ export const CUSTOM_PLACEHOLDERS = ['prompt_file', 'cwd', 'session'] as const
21
+
22
+ /** One placeholder name. */
23
+ export type CustomPlaceholder = (typeof CUSTOM_PLACEHOLDERS)[number]
24
+
25
+ /** Placeholder pattern: a braced lower-case name. */
26
+ const PLACEHOLDER_RE = /\{([a-z_]+)\}/g
27
+
28
+ /**
29
+ * Check one template before a run depends on it.
30
+ * @param template - the template text.
31
+ * @returns the reason it cannot be used, or undefined when it can.
32
+ */
33
+ export function validateCustomTemplate(template: string): string | undefined {
34
+ if (template.trim().length === 0) return 'the template must not be empty'
35
+ if (template.length > 2_048) return 'the template must be at most 2048 characters'
36
+ if (template.includes('\u0000')) return 'the template must not contain a NUL byte'
37
+ for (const match of template.matchAll(PLACEHOLDER_RE)) {
38
+ const name = match[1]
39
+ if (!(CUSTOM_PLACEHOLDERS as readonly string[]).includes(name)) {
40
+ return `unknown placeholder {${name}} (accepted: ${CUSTOM_PLACEHOLDERS.map(p => `{${p}}`).join(', ')})`
41
+ }
42
+ }
43
+ if (!template.includes('{prompt_file}')) return 'the start template must reference {prompt_file}'
44
+ return undefined
45
+ }
46
+
47
+ /** Replace every placeholder with its quoted value. */
48
+ function substitute(template: string, values: Record<CustomPlaceholder, string>): string {
49
+ return template.replace(PLACEHOLDER_RE, (_match, name: string) => shellQuote(values[name as CustomPlaceholder] ?? ''))
50
+ }
51
+
52
+ /**
53
+ * Build a custom agent's command line for one run.
54
+ *
55
+ * The start template is used when `resumeSessionId` is absent. A resume without
56
+ * a resume template is a fresh run carrying the earlier summary, which is the
57
+ * documented degradation, so the start template answers it.
58
+ * @param input - run facts.
59
+ * @param templates - the user's templates.
60
+ * @returns the quoted command line.
61
+ * @throws RangeError when a template fails {@link validateCustomTemplate}.
62
+ */
63
+ export function customAgentCommand(input: SshAgentRunInput, templates: SshCustomTemplates): SshAgentCommand {
64
+ const startProblem = validateCustomTemplate(templates.command)
65
+ if (startProblem !== undefined) throw new RangeError(`custom agent start template: ${startProblem}`)
66
+ const resumeTemplate = input.resumeSessionId !== undefined ? templates.resumeCommand : undefined
67
+ if (resumeTemplate !== undefined) {
68
+ const resumeProblem = validateCustomTemplate(resumeTemplate)
69
+ if (resumeProblem !== undefined) throw new RangeError(`custom agent resume template: ${resumeProblem}`)
70
+ }
71
+ const template = input.resumeSessionId !== undefined && resumeTemplate !== undefined ? resumeTemplate : templates.command
72
+ return {
73
+ command: substitute(template, {
74
+ prompt_file: input.promptFile,
75
+ cwd: input.cwd,
76
+ session: input.resumeSessionId ?? '',
77
+ }),
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Whether a custom agent can continue a session.
83
+ * @param templates - the user's templates.
84
+ * @returns true when a resume template is present.
85
+ */
86
+ export function customResumeSupported(templates: SshCustomTemplates): boolean {
87
+ return templates.resumeCommand !== undefined && templates.resumeCommand.trim().length > 0
88
+ }
@@ -0,0 +1,87 @@
1
+ /**
2
+ * The dsh agent adapter.
3
+ *
4
+ * Flags and event shapes are read from this machine's `dsh headless --help` and
5
+ * from recorded runs, not from memory: `--json` writes newline-delimited run
6
+ * events, `--session-id` adopts a persisted session, and `--permission-mode`
7
+ * selects a configured preset. The preset named here is the fully unattended
8
+ * one, verified against the CLI's own error text, which lists the three presets
9
+ * a deployment always has:
10
+ *
11
+ * ```text
12
+ * dsh: permission: unknown preset "not-a-preset" (known: read-only, workspace-write, danger-full-access)
13
+ * ```
14
+ *
15
+ * @module dsh-task-board-ssh/core/adapters/dsh
16
+ */
17
+ import { shellJoin } from '../shell.ts'
18
+ import { readJsonLines, readString, type SshAgentCommand, type SshAgentOutput, type SshAgentRunInput } from './types.ts'
19
+
20
+ /**
21
+ * Permission preset every remote run uses: unrestricted sandbox with approval
22
+ * policy `never`, which is what "fully unattended" means on a host nobody is
23
+ * watching.
24
+ */
25
+ export const DSH_FULL_ACCESS_PRESET = 'danger-full-access'
26
+
27
+ /** Program named on the remote host. */
28
+ export const DSH_PROGRAM = 'dsh'
29
+
30
+ /**
31
+ * Build the dsh command line for one run.
32
+ * @param input - run facts.
33
+ * @returns the quoted command line, reading the prompt from stdin.
34
+ */
35
+ export function dshAgentCommand(input: SshAgentRunInput): SshAgentCommand {
36
+ const argv = [
37
+ DSH_PROGRAM,
38
+ 'headless',
39
+ '--json',
40
+ '--permission-mode',
41
+ DSH_FULL_ACCESS_PRESET,
42
+ ]
43
+ if (input.resumeSessionId !== undefined) argv.push('--session-id', input.resumeSessionId)
44
+ // A trailing `-` is the CLI's documented way to read the task from stdin, so
45
+ // a multi-megabyte prompt never has to fit in one argument.
46
+ argv.push('-')
47
+ return { command: shellJoin(argv), stdinFile: input.promptFile }
48
+ }
49
+
50
+ /**
51
+ * Read the session id and the final summary out of a dsh event stream.
52
+ * @param output - captured stdout of one run.
53
+ * @returns whatever the stream reported.
54
+ */
55
+ export function readDshOutput(output: string): SshAgentOutput {
56
+ const result: SshAgentOutput = {}
57
+ for (const event of readJsonLines(output)) {
58
+ const type = readString(event, 'type')
59
+ if (type === 'session' && result.sessionId === undefined) {
60
+ result.sessionId = readString(event, 'sessionId')
61
+ continue
62
+ }
63
+ if (type === 'final') {
64
+ const sessionId = readString(event, 'sessionId')
65
+ if (sessionId !== undefined) result.sessionId = sessionId
66
+ const reason = event.reason
67
+ const kind = readString(reason, 'kind')
68
+ if (kind === 'completed') {
69
+ result.outcome = { ok: true, text: readString(event, 'text') ?? '' }
70
+ } else {
71
+ const message = readString(reason, 'error') ?? readString(reason, 'message')
72
+ const error = typeof reason === 'object' && reason !== null
73
+ ? readString((reason as Record<string, unknown>).error, 'message')
74
+ : undefined
75
+ result.outcome = { ok: false, error: error ?? message ?? 'the run ended without a completed turn' }
76
+ }
77
+ continue
78
+ }
79
+ if (type === 'error' && result.outcome === undefined) {
80
+ result.outcome = { ok: false, error: readString(event, 'message') ?? 'the run reported a failure' }
81
+ }
82
+ }
83
+ if (result.outcome === undefined) {
84
+ result.outcome = { ok: false, error: 'the dsh event stream ended without a final event' }
85
+ }
86
+ return result
87
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * The adapter table: one entry per agent CLI this provider can start.
3
+ *
4
+ * The three built-in CLIs are verified against their help output and recorded
5
+ * runs on this machine, except Codex, which is not installed here and is
6
+ * written from its official documentation until a host that has it confirms the
7
+ * shape. A custom agent is whatever command a deployment configures.
8
+ *
9
+ * @module dsh-task-board-ssh/core/adapters
10
+ */
11
+ import { claudeAgentCommand, readClaudeOutput } from './claude.ts'
12
+ import { codexAgentCommand, readCodexOutput } from './codex.ts'
13
+ import { customAgentCommand } from './custom.ts'
14
+ import { dshAgentCommand, readDshOutput } from './dsh.ts'
15
+ import type { SshAgentCommand, SshAgentOutput, SshAgentRunInput, SshCustomTemplates } from './types.ts'
16
+
17
+ export * from './types.ts'
18
+ export { DSH_FULL_ACCESS_PRESET, DSH_PROGRAM, dshAgentCommand, readDshOutput } from './dsh.ts'
19
+ export { CLAUDE_PROGRAM, claudeAgentCommand, readClaudeOutput } from './claude.ts'
20
+ export { CODEX_PROGRAM, codexAgentCommand, readCodexOutput } from './codex.ts'
21
+ export {
22
+ CUSTOM_PLACEHOLDERS,
23
+ customAgentCommand,
24
+ customResumeSupported,
25
+ validateCustomTemplate,
26
+ } from './custom.ts'
27
+
28
+ /**
29
+ * Build the command line one run starts with.
30
+ *
31
+ * The prompt reaches the agent through `stdinFile` for the three built-in CLIs,
32
+ * so a long prompt never has to fit in one argument; a custom template decides
33
+ * for itself and usually reads `{prompt_file}`.
34
+ * @param input - run facts; `resumeSessionId` selects the resume form.
35
+ * @param templates - the deployment's custom-agent templates, required for kind `custom`.
36
+ * @returns the quoted command line and the stdin file, when the agent reads one.
37
+ * @throws RangeError when a custom agent is selected without usable templates.
38
+ */
39
+ export function buildAgentCommandLine(input: SshAgentRunInput, templates?: SshCustomTemplates): SshAgentCommand {
40
+ switch (input.agent) {
41
+ case 'dsh':
42
+ return dshAgentCommand(input)
43
+ case 'claude':
44
+ return claudeAgentCommand(input)
45
+ case 'codex':
46
+ return codexAgentCommand(input)
47
+ case 'custom':
48
+ if (templates === undefined) throw new RangeError('a custom agent needs a command template')
49
+ return customAgentCommand(input, templates)
50
+ default: {
51
+ const unexpected: never = input.agent
52
+ throw new RangeError(`unknown agent kind: ${String(unexpected)}`)
53
+ }
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Read the session id and the final summary out of one run's output stream.
59
+ * @param input - which agent produced the stream.
60
+ * @param output - captured stdout.
61
+ * @returns whatever the agent's reader found; a custom agent reports nothing.
62
+ */
63
+ export function readAgentOutput(input: SshAgentRunInput, output: string): SshAgentOutput {
64
+ switch (input.agent) {
65
+ case 'dsh':
66
+ return readDshOutput(output)
67
+ case 'claude':
68
+ return readClaudeOutput(output)
69
+ case 'codex':
70
+ return readCodexOutput(output)
71
+ default:
72
+ return {}
73
+ }
74
+ }