@linxin666/dsh-client-ui-task-board-ssh 0.0.0-stage → 0.4.21
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.
- package/LICENSE +202 -0
- package/README.i18n.yaml +6 -0
- package/README.md +96 -2
- package/README.zh.md +97 -0
- package/cordis.patch.yml +15 -0
- package/icon.svg +5 -0
- package/lib/client.js +4435 -0
- package/lib/index.js +7082 -0
- package/lib/types/client/PluginSettingsCard.d.ts +255 -0
- package/lib/types/client/RangeBubble.d.ts +46 -0
- package/lib/types/client/SegmentGroup.d.ts +28 -0
- package/lib/types/client/index.d.ts +100 -0
- package/lib/types/client/locales.d.ts +125 -0
- package/lib/types/client/seat-props.d.ts +53 -0
- package/lib/types/client/settings-entry-form.d.ts +49 -0
- package/lib/types/client/settings-form.d.ts +237 -0
- package/lib/types/client/ssh/card-decoration.d.ts +23 -0
- package/lib/types/client/ssh/compose-section.d.ts +7 -0
- package/lib/types/client/ssh/detail-section.d.ts +7 -0
- package/lib/types/client/ssh/extension.d.ts +54 -0
- package/lib/types/client/ssh/log-buffer.d.ts +128 -0
- package/lib/types/client/ssh/log-view.d.ts +37 -0
- package/lib/types/client/ssh/model.d.ts +130 -0
- package/lib/types/client/ssh/settings-section.d.ts +134 -0
- package/lib/types/client/ssh/summary.d.ts +160 -0
- package/lib/types/client/ssh/target-form.d.ts +60 -0
- package/lib/types/core/action.d.ts +89 -0
- package/lib/types/core/adapters/claude.d.ts +15 -0
- package/lib/types/core/adapters/codex.d.ts +15 -0
- package/lib/types/core/adapters/custom.d.ts +29 -0
- package/lib/types/core/adapters/dsh.d.ts +21 -0
- package/lib/types/core/adapters/index.d.ts +25 -0
- package/lib/types/core/adapters/types.d.ts +76 -0
- package/lib/types/core/contract.d.ts +453 -0
- package/lib/types/core/launcher-script.d.ts +13 -0
- package/lib/types/core/launcher.d.ts +153 -0
- package/lib/types/core/ledger.d.ts +123 -0
- package/lib/types/core/shell.d.ts +61 -0
- package/lib/types/core/target.d.ts +149 -0
- package/lib/types/core/task-record.d.ts +74 -0
- package/lib/types/core/workspace.d.ts +74 -0
- package/lib/types/host/board.d.ts +75 -0
- package/lib/types/host/engine.d.ts +563 -0
- package/lib/types/host/extension.d.ts +61 -0
- package/lib/types/host/inspect.d.ts +99 -0
- package/lib/types/host/logs.d.ts +135 -0
- package/lib/types/host/payload.d.ts +75 -0
- package/lib/types/host/prompt.d.ts +44 -0
- package/lib/types/host/queue.d.ts +53 -0
- package/lib/types/host/remote.d.ts +137 -0
- package/lib/types/host/settings.d.ts +149 -0
- package/lib/types/host/timers.d.ts +20 -0
- package/lib/types/host/tools.d.ts +17 -0
- package/lib/types/host/workspace/errors.d.ts +29 -0
- package/lib/types/host/workspace/git.d.ts +108 -0
- package/lib/types/host/workspace/in-place.d.ts +36 -0
- package/lib/types/host/workspace/index.d.ts +28 -0
- package/lib/types/host/workspace/layout.d.ts +62 -0
- package/lib/types/host/workspace/local-git.d.ts +72 -0
- package/lib/types/host/workspace/remote-git.d.ts +111 -0
- package/lib/types/host/workspace/remote.d.ts +85 -0
- package/lib/types/host/workspace/types.d.ts +28 -0
- package/lib/types/host/workspace-seam.d.ts +100 -0
- package/lib/types/index.d.ts +97 -0
- package/lib/types/mount-once.d.ts +55 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +89 -3
- package/src/client/PluginSettingsCard.tsx +838 -0
- package/src/client/RangeBubble.tsx +164 -0
- package/src/client/SegmentGroup.tsx +93 -0
- package/src/client/css-modules.d.ts +5 -0
- package/src/client/index.ts +195 -0
- package/src/client/locales.ts +257 -0
- package/src/client/seat-props.ts +58 -0
- package/src/client/settings-card.module.css +821 -0
- package/src/client/settings-entry-form.ts +141 -0
- package/src/client/settings-form.ts +529 -0
- package/src/client/ssh/card-decoration.tsx +55 -0
- package/src/client/ssh/compose-section.tsx +147 -0
- package/src/client/ssh/detail-section.tsx +345 -0
- package/src/client/ssh/extension.ts +271 -0
- package/src/client/ssh/log-buffer.ts +224 -0
- package/src/client/ssh/log-view.tsx +149 -0
- package/src/client/ssh/model.ts +299 -0
- package/src/client/ssh/settings-section.tsx +401 -0
- package/src/client/ssh/summary.ts +388 -0
- package/src/client/ssh/target-form.tsx +380 -0
- package/src/client/ssh.module.css +617 -0
- package/src/core/action.ts +270 -0
- package/src/core/adapters/claude.ts +64 -0
- package/src/core/adapters/codex.ts +83 -0
- package/src/core/adapters/custom.ts +88 -0
- package/src/core/adapters/dsh.ts +87 -0
- package/src/core/adapters/index.ts +74 -0
- package/src/core/adapters/types.ts +95 -0
- package/src/core/contract.ts +515 -0
- package/src/core/launcher-script.ts +313 -0
- package/src/core/launcher.ts +283 -0
- package/src/core/ledger.ts +377 -0
- package/src/core/shell.ts +77 -0
- package/src/core/target.ts +420 -0
- package/src/core/task-record.ts +73 -0
- package/src/core/workspace.ts +78 -0
- package/src/host/board.ts +107 -0
- package/src/host/engine.ts +2026 -0
- package/src/host/extension.ts +310 -0
- package/src/host/inspect.ts +264 -0
- package/src/host/logs.ts +297 -0
- package/src/host/payload.ts +134 -0
- package/src/host/prompt.ts +89 -0
- package/src/host/queue.ts +73 -0
- package/src/host/remote.ts +356 -0
- package/src/host/settings.ts +294 -0
- package/src/host/timers.ts +33 -0
- package/src/host/tools.ts +449 -0
- package/src/host/workspace/errors.ts +43 -0
- package/src/host/workspace/git.ts +305 -0
- package/src/host/workspace/in-place.ts +58 -0
- package/src/host/workspace/index.ts +37 -0
- package/src/host/workspace/layout.ts +122 -0
- package/src/host/workspace/local-git.ts +164 -0
- package/src/host/workspace/remote-git.ts +244 -0
- package/src/host/workspace/remote.ts +181 -0
- package/src/host/workspace/types.ts +34 -0
- package/src/host/workspace-seam.ts +132 -0
- package/src/index.ts +243 -0
- package/src/mount-once.ts +142 -0
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The SSH provider assembled as one task-board extension.
|
|
3
|
+
*
|
|
4
|
+
* Everything SSH-specific lives behind this factory: the board admits it through
|
|
5
|
+
* the cordis `taskBoard` service, hands it the capability face, and never learns
|
|
6
|
+
* what the provider does with it. The factory imports no board internals — only
|
|
7
|
+
* the same-shape contract in `../core/contract.ts` — and it probes the two seams
|
|
8
|
+
* a board revision may or may not serve (`launch.intercept` and the in-process
|
|
9
|
+
* action seat) instead of assuming them, so a board that serves neither still
|
|
10
|
+
* mounts this extension: its tools answer, its polls stop, and the reason is
|
|
11
|
+
* logged rather than thrown at the board.
|
|
12
|
+
*
|
|
13
|
+
* @module dsh-task-board-ssh/host/extension
|
|
14
|
+
*/
|
|
15
|
+
import { parseSshActionRequest, type SshActionReply } from '../core/action.ts'
|
|
16
|
+
import {
|
|
17
|
+
TASK_BOARD_API_VERSION,
|
|
18
|
+
type DshSshService,
|
|
19
|
+
type TaskBoardExtension,
|
|
20
|
+
type TaskBoardExtensionActionRequest,
|
|
21
|
+
type TaskBoardExtensionHost,
|
|
22
|
+
} from '../core/contract.ts'
|
|
23
|
+
import { SSH_AGENT_KINDS, type SshAgentKind, type SshExecutionMode } from '../core/target.ts'
|
|
24
|
+
import type { TaskRecord } from '../core/task-record.ts'
|
|
25
|
+
import type { BoardActionSeat } from './board.ts'
|
|
26
|
+
import { SshRunEngine, type SshEngineReply, type SshLogPage } from './engine.ts'
|
|
27
|
+
import {
|
|
28
|
+
SshSettingsError,
|
|
29
|
+
type SshProviderSettings,
|
|
30
|
+
type SshSettingsPatch,
|
|
31
|
+
type SshSettingsWriter,
|
|
32
|
+
} from './settings.ts'
|
|
33
|
+
import type { HostTimerFace } from './timers.ts'
|
|
34
|
+
import { buildSshTools } from './tools.ts'
|
|
35
|
+
import type { SshWorkspaceStrategyResolver } from './workspace-seam.ts'
|
|
36
|
+
|
|
37
|
+
/** Extension id this provider registers under, and the key of its ledger payload. */
|
|
38
|
+
export const SSH_EXTENSION_ID = 'ssh'
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Actions this package handles on top of the frozen card protocol.
|
|
42
|
+
*
|
|
43
|
+
* The six card actions are the board's own vocabulary and are parsed by
|
|
44
|
+
* `src/core/action.ts`. These three are host-half additions a settings card or a
|
|
45
|
+
* log viewer drives: the log pager (a remote file read by offset, never a new
|
|
46
|
+
* HTTP route) and the configuration read/write pair, whose write goes through the
|
|
47
|
+
* extension's single write path and therefore through `ctx.settings.mutate`.
|
|
48
|
+
*/
|
|
49
|
+
export const SSH_HOST_ACTIONS = {
|
|
50
|
+
/** Read one page of a run's log by byte offset. */
|
|
51
|
+
readLog: 'read-log',
|
|
52
|
+
/** Read the extension's effective settings. */
|
|
53
|
+
setupStatus: 'setup-status',
|
|
54
|
+
/** Write the extension's settings through the one write path. */
|
|
55
|
+
setupSave: 'setup-save',
|
|
56
|
+
} as const
|
|
57
|
+
|
|
58
|
+
/** Everything the extension needs from its mount. */
|
|
59
|
+
export interface SshExtensionOptions {
|
|
60
|
+
/** The `dshSsh` forwarder every remote operation goes through. */
|
|
61
|
+
ssh: DshSshService
|
|
62
|
+
/** Live configuration, read at use time. */
|
|
63
|
+
settings: () => SshProviderSettings
|
|
64
|
+
/** The one configuration write path. */
|
|
65
|
+
settingsWriter: SshSettingsWriter
|
|
66
|
+
/** Timer seat the poll cadence runs on. */
|
|
67
|
+
timers: HostTimerFace
|
|
68
|
+
/** The board's in-process action seat, resolved per call so a late one is honoured. */
|
|
69
|
+
seat?: () => BoardActionSeat | undefined
|
|
70
|
+
/** Placement strategies, when this deployment serves any. */
|
|
71
|
+
workspaces?: SshWorkspaceStrategyResolver
|
|
72
|
+
/** Resolve a local workspace id to the repository root a git run bundles. */
|
|
73
|
+
localRepository?: (workspaceId: string) => string | undefined
|
|
74
|
+
/** Clock (ms epoch). */
|
|
75
|
+
now?: () => number
|
|
76
|
+
/** Identifier source for request ids and run ids. */
|
|
77
|
+
uuid?: () => string
|
|
78
|
+
/** Wait source of a blocking status call; a real timer when absent. */
|
|
79
|
+
sleep?: (ms: number) => Promise<void>
|
|
80
|
+
/** Poll cadence, in milliseconds. */
|
|
81
|
+
pollIntervalMs?: number
|
|
82
|
+
/** Ceiling for one remote command, in milliseconds. */
|
|
83
|
+
commandTimeoutMs?: number
|
|
84
|
+
/** Where a refusal or a failed remote call is reported. */
|
|
85
|
+
log?: (message: string, detail?: unknown) => void
|
|
86
|
+
/** Register a hook invoked whenever volatile settings change while mounted. */
|
|
87
|
+
onSettingsHook?: (notify: (() => void) | undefined) => void
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The message of anything thrown below. */
|
|
91
|
+
function messageOf(error: unknown): string {
|
|
92
|
+
return error instanceof Error ? error.message : String(error)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Read one plain object off an untyped payload. */
|
|
96
|
+
function recordOf(value: unknown): Record<string, unknown> {
|
|
97
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) ? value as Record<string, unknown> : {}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Read one bounded non-negative integer option. */
|
|
101
|
+
function optionalNumber(value: unknown): number | undefined {
|
|
102
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? Math.trunc(value) : undefined
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Read the log pager's options out of an action payload. */
|
|
106
|
+
function readLogOptions(payload: Record<string, unknown>): { runId?: string; offset?: number; limit?: number } {
|
|
107
|
+
const options: { runId?: string; offset?: number; limit?: number } = {}
|
|
108
|
+
if (typeof payload.runId === 'string' && payload.runId !== '') options.runId = payload.runId
|
|
109
|
+
const offset = optionalNumber(payload.offset)
|
|
110
|
+
if (offset !== undefined) options.offset = offset
|
|
111
|
+
const limit = optionalNumber(payload.limit)
|
|
112
|
+
if (limit !== undefined) options.limit = limit
|
|
113
|
+
return options
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** One validated settings patch, or the reason it was refused. */
|
|
117
|
+
type SettingsPatchResult = { ok: true; patch: SshSettingsPatch } | { ok: false; error: string }
|
|
118
|
+
|
|
119
|
+
/** Read one settings patch out of an action payload, refusing a malformed field. */
|
|
120
|
+
function readSettingsPatch(payload: Record<string, unknown>): SettingsPatchResult {
|
|
121
|
+
const patch: SshSettingsPatch = {}
|
|
122
|
+
if (payload.enabled !== undefined) {
|
|
123
|
+
if (typeof payload.enabled !== 'boolean') return { ok: false, error: 'enabled must be a boolean' }
|
|
124
|
+
patch.enabled = payload.enabled
|
|
125
|
+
}
|
|
126
|
+
if (payload.defaultHost !== undefined) {
|
|
127
|
+
if (typeof payload.defaultHost !== 'string') return { ok: false, error: 'defaultHost must be a string' }
|
|
128
|
+
patch.defaultHost = payload.defaultHost
|
|
129
|
+
}
|
|
130
|
+
if (payload.defaultAgent !== undefined) {
|
|
131
|
+
if (!(SSH_AGENT_KINDS as readonly string[]).includes(String(payload.defaultAgent))) {
|
|
132
|
+
return { ok: false, error: `defaultAgent must be one of: ${SSH_AGENT_KINDS.join(', ')}` }
|
|
133
|
+
}
|
|
134
|
+
patch.defaultAgent = String(payload.defaultAgent) as SshAgentKind
|
|
135
|
+
}
|
|
136
|
+
if (payload.defaultMode !== undefined) {
|
|
137
|
+
if (payload.defaultMode !== 'direct' && payload.defaultMode !== 'proxy') {
|
|
138
|
+
return { ok: false, error: 'defaultMode must be "direct" or "proxy"' }
|
|
139
|
+
}
|
|
140
|
+
patch.defaultMode = payload.defaultMode as SshExecutionMode
|
|
141
|
+
}
|
|
142
|
+
if (payload.perHostCap !== undefined) {
|
|
143
|
+
if (typeof payload.perHostCap !== 'number' || !Number.isFinite(payload.perHostCap)) {
|
|
144
|
+
return { ok: false, error: 'perHostCap must be a number' }
|
|
145
|
+
}
|
|
146
|
+
patch.perHostCap = payload.perHostCap
|
|
147
|
+
}
|
|
148
|
+
if (payload.agentCommands !== undefined) {
|
|
149
|
+
if (!Array.isArray(payload.agentCommands)) return { ok: false, error: 'agentCommands must be an array' }
|
|
150
|
+
const rows: Array<{ kind: SshAgentKind; command: string; resumeCommand?: string }> = []
|
|
151
|
+
for (const entry of payload.agentCommands) {
|
|
152
|
+
const row = recordOf(entry)
|
|
153
|
+
const kind = (SSH_AGENT_KINDS as readonly string[]).includes(String(row.kind)) ? String(row.kind) as SshAgentKind : undefined
|
|
154
|
+
if (kind === undefined) return { ok: false, error: `an agentCommands row names an unknown agent: ${String(row.kind)}` }
|
|
155
|
+
if (typeof row.command !== 'string') return { ok: false, error: 'an agentCommands row needs a command template' }
|
|
156
|
+
rows.push({
|
|
157
|
+
kind,
|
|
158
|
+
command: row.command,
|
|
159
|
+
...(typeof row.resumeCommand === 'string' ? { resumeCommand: row.resumeCommand } : {}),
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
patch.agentCommands = rows
|
|
163
|
+
}
|
|
164
|
+
return { ok: true, patch }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Route one action to the engine and the settings writer. */
|
|
168
|
+
async function routeAction(
|
|
169
|
+
request: TaskBoardExtensionActionRequest,
|
|
170
|
+
engine: SshRunEngine | undefined,
|
|
171
|
+
writer: SshSettingsWriter,
|
|
172
|
+
): Promise<unknown> {
|
|
173
|
+
const payload = recordOf(request.payload)
|
|
174
|
+
switch (request.action) {
|
|
175
|
+
case SSH_HOST_ACTIONS.readLog: {
|
|
176
|
+
if (engine === undefined) return { ok: false, error: 'the SSH extension is not running' }
|
|
177
|
+
if (typeof request.taskId !== 'string' || request.taskId === '') return { ok: false, error: 'read-log needs a taskId' }
|
|
178
|
+
const page: SshLogPage = await engine.readLog(request.taskId, readLogOptions(payload))
|
|
179
|
+
return page
|
|
180
|
+
}
|
|
181
|
+
case SSH_HOST_ACTIONS.setupStatus: {
|
|
182
|
+
return {
|
|
183
|
+
ok: true,
|
|
184
|
+
writable: writer.available(),
|
|
185
|
+
settings: engine === undefined ? null : engine.summary().defaults,
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
case SSH_HOST_ACTIONS.setupSave: {
|
|
189
|
+
const parsed = readSettingsPatch(payload)
|
|
190
|
+
if (!parsed.ok) return { ok: false, error: parsed.error }
|
|
191
|
+
try {
|
|
192
|
+
const result = await writer.save(parsed.patch)
|
|
193
|
+
return { ok: true, entry: result.entry, fields: result.fields }
|
|
194
|
+
} catch (error) {
|
|
195
|
+
return {
|
|
196
|
+
ok: false,
|
|
197
|
+
code: error instanceof SshSettingsError ? error.code : 'settings-write-failed',
|
|
198
|
+
error: messageOf(error),
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
default:
|
|
203
|
+
break
|
|
204
|
+
}
|
|
205
|
+
if (engine === undefined) return { ok: false, error: 'the SSH extension is not running' }
|
|
206
|
+
const parsed = parseSshActionRequest(request)
|
|
207
|
+
if (!parsed.ok) {
|
|
208
|
+
const reply: SshActionReply = { ok: false, error: 'the request was refused', issues: parsed.errors }
|
|
209
|
+
return reply
|
|
210
|
+
}
|
|
211
|
+
const action = parsed.value
|
|
212
|
+
let reply: SshEngineReply
|
|
213
|
+
switch (action.action) {
|
|
214
|
+
case 'run':
|
|
215
|
+
reply = engine.runNow(action.taskId)
|
|
216
|
+
break
|
|
217
|
+
case 'cancel':
|
|
218
|
+
reply = engine.cancel(action.taskId)
|
|
219
|
+
break
|
|
220
|
+
case 'follow-up':
|
|
221
|
+
reply = await engine.instruct(action.taskId, action.text, 'follow-up')
|
|
222
|
+
break
|
|
223
|
+
case 'rework':
|
|
224
|
+
reply = await engine.instruct(action.taskId, action.instructions, 'rework')
|
|
225
|
+
break
|
|
226
|
+
case 'set-target':
|
|
227
|
+
reply = engine.setTarget(action.taskId, action.target)
|
|
228
|
+
break
|
|
229
|
+
default:
|
|
230
|
+
// The detail view names the run (and optional byte offset for paging past
|
|
231
|
+
// one window) whose log it wants, so a settled run can be read in full.
|
|
232
|
+
reply = await engine.refresh(action.taskId, action.runId, action.offset)
|
|
233
|
+
break
|
|
234
|
+
}
|
|
235
|
+
const answer: SshActionReply = reply.ok
|
|
236
|
+
? {
|
|
237
|
+
ok: true,
|
|
238
|
+
taskId: reply.taskId,
|
|
239
|
+
...(reply.runId === undefined ? {} : { runId: reply.runId }),
|
|
240
|
+
...(reply.message === undefined ? {} : { message: reply.message }),
|
|
241
|
+
}
|
|
242
|
+
: { ok: false, error: reply.error }
|
|
243
|
+
return answer
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Build the SSH provider extension.
|
|
248
|
+
* @param options - the mount's faces, settings and clock.
|
|
249
|
+
* @returns the extension the board registers.
|
|
250
|
+
*/
|
|
251
|
+
export function createSshExtension(options: SshExtensionOptions): TaskBoardExtension {
|
|
252
|
+
let engine: SshRunEngine | undefined
|
|
253
|
+
const toolDisposers: Array<() => void> = []
|
|
254
|
+
const log = options.log ?? ((message: string, detail?: unknown) => {
|
|
255
|
+
if (detail === undefined) console.error('[dsh-task-board-ssh] ' + message)
|
|
256
|
+
else console.error('[dsh-task-board-ssh] ' + message, detail)
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
return {
|
|
260
|
+
id: SSH_EXTENSION_ID,
|
|
261
|
+
apiVersion: TASK_BOARD_API_VERSION,
|
|
262
|
+
enabled: () => options.settings().enabled,
|
|
263
|
+
start(host: TaskBoardExtensionHost): void {
|
|
264
|
+
const live = new SshRunEngine({
|
|
265
|
+
host,
|
|
266
|
+
ssh: options.ssh,
|
|
267
|
+
timers: options.timers,
|
|
268
|
+
settings: options.settings,
|
|
269
|
+
...(options.seat === undefined ? {} : { seat: options.seat }),
|
|
270
|
+
...(options.workspaces === undefined ? {} : { workspaces: options.workspaces }),
|
|
271
|
+
...(options.localRepository === undefined ? {} : { localRepository: options.localRepository }),
|
|
272
|
+
...(options.now === undefined ? {} : { now: options.now }),
|
|
273
|
+
...(options.uuid === undefined ? {} : { uuid: options.uuid }),
|
|
274
|
+
...(options.sleep === undefined ? {} : { sleep: options.sleep }),
|
|
275
|
+
...(options.pollIntervalMs === undefined ? {} : { pollIntervalMs: options.pollIntervalMs }),
|
|
276
|
+
...(options.commandTimeoutMs === undefined ? {} : { commandTimeoutMs: options.commandTimeoutMs }),
|
|
277
|
+
log,
|
|
278
|
+
})
|
|
279
|
+
engine = live
|
|
280
|
+
options.onSettingsHook?.(() => { live.onSettingsUpdated() })
|
|
281
|
+
live.start()
|
|
282
|
+
for (const tool of buildSshTools(live)) {
|
|
283
|
+
try {
|
|
284
|
+
toolDisposers.push(host.registerTool(tool))
|
|
285
|
+
} catch (error) {
|
|
286
|
+
// A missing tool registry costs the three tools, not the provider.
|
|
287
|
+
log(`the tool ${tool.name} could not register`, error)
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
try {
|
|
291
|
+
host.publish(live.summary())
|
|
292
|
+
} catch (error) {
|
|
293
|
+
log('the initial summary was refused', error)
|
|
294
|
+
}
|
|
295
|
+
},
|
|
296
|
+
stop(): void {
|
|
297
|
+
options.onSettingsHook?.(undefined)
|
|
298
|
+
for (const dispose of toolDisposers.splice(0)) {
|
|
299
|
+
try { dispose() } catch { /* best-effort */ }
|
|
300
|
+
}
|
|
301
|
+
engine?.stop()
|
|
302
|
+
engine = undefined
|
|
303
|
+
},
|
|
304
|
+
describeTask: (task: TaskRecord) => engine?.describeTask(task),
|
|
305
|
+
describeRun: (taskId: string) => engine?.describeRun(taskId),
|
|
306
|
+
async handleAction(request: TaskBoardExtensionActionRequest): Promise<unknown> {
|
|
307
|
+
return await routeAction(request, engine, options.settingsWriter)
|
|
308
|
+
},
|
|
309
|
+
}
|
|
310
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an agent asks about a remote run before and while it runs: whether the
|
|
3
|
+
* target can work at all (preflight), what the log says without its noise
|
|
4
|
+
* (filtered log query), and what the agent did last (activity).
|
|
5
|
+
*
|
|
6
|
+
* Every command here is read-only and composed through `shellQuote`; none of it
|
|
7
|
+
* touches the launcher script, so the remote side of a run is unchanged.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-task-board-ssh/host/inspect
|
|
10
|
+
*/
|
|
11
|
+
import { shellQuote } from '../core/shell.ts'
|
|
12
|
+
import type { SshAgentTarget, SshTarget } from '../core/target.ts'
|
|
13
|
+
|
|
14
|
+
/** Stable refusal codes a target check or a run can answer with. */
|
|
15
|
+
export const SSH_PREFLIGHT_CODES = ['host-unreachable', 'agent-missing', 'path-missing', 'permission-required'] as const
|
|
16
|
+
|
|
17
|
+
/** One stable preflight code. */
|
|
18
|
+
export type SshPreflightCode = (typeof SSH_PREFLIGHT_CODES)[number]
|
|
19
|
+
|
|
20
|
+
/** A target check that failed, in words the caller can act on. */
|
|
21
|
+
export interface SshPreflightFailure {
|
|
22
|
+
code: SshPreflightCode
|
|
23
|
+
message: string
|
|
24
|
+
/** The next step, one sentence. */
|
|
25
|
+
hint: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Program a built-in agent kind starts on the host, or undefined when it is configured. */
|
|
29
|
+
export function agentProgram(agent: SshAgentTarget, override?: string): string | undefined {
|
|
30
|
+
if (override !== undefined) return override
|
|
31
|
+
switch (agent.kind) {
|
|
32
|
+
case 'dsh': return 'dsh'
|
|
33
|
+
case 'claude': return 'claude'
|
|
34
|
+
case 'codex': return 'codex'
|
|
35
|
+
default: {
|
|
36
|
+
// A custom template starts with the program; a placeholder-led one has none to probe.
|
|
37
|
+
const first = agent.command?.trim().split(/\s+/)[0]
|
|
38
|
+
return first === undefined || first === '' || first.includes('{') || first.includes('$') ? undefined : first
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The single remote command one preflight runs: it always exits zero and prints
|
|
45
|
+
* one `key=value` per fact, so a failed channel is told apart from a failed check.
|
|
46
|
+
*/
|
|
47
|
+
export function preflightCommand(target: SshTarget, program: string | undefined): string {
|
|
48
|
+
const parts = ['echo reach=ok']
|
|
49
|
+
if (program !== undefined) {
|
|
50
|
+
parts.push(`if command -v ${shellQuote(program)} >/dev/null 2>&1; then echo agent=ok; else echo agent=missing; fi`)
|
|
51
|
+
}
|
|
52
|
+
if (target.workspace.kind === 'in-place') {
|
|
53
|
+
const path = shellQuote(target.workspace.path)
|
|
54
|
+
parts.push(`if [ -d ${path} ]; then echo path=ok; if [ -w ${path} ] && [ -x ${path} ]; then echo write=ok; else echo write=denied; fi; else echo path=missing; fi`)
|
|
55
|
+
}
|
|
56
|
+
return parts.join('; ')
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Read one preflight answer.
|
|
61
|
+
* @param target - the checked target.
|
|
62
|
+
* @param program - the probed program, if any.
|
|
63
|
+
* @param result - what `exec` returned.
|
|
64
|
+
* @returns the first failed check, or undefined when the target can work.
|
|
65
|
+
*/
|
|
66
|
+
export function readPreflight(
|
|
67
|
+
target: SshTarget,
|
|
68
|
+
program: string | undefined,
|
|
69
|
+
result: { success: boolean; stdout: string; stderr: string; error?: string; timedOut?: boolean },
|
|
70
|
+
): SshPreflightFailure | undefined {
|
|
71
|
+
const facts = new Map<string, string>()
|
|
72
|
+
for (const line of result.stdout.split('\n')) {
|
|
73
|
+
const at = line.indexOf('=')
|
|
74
|
+
if (at > 0) facts.set(line.slice(0, at).trim(), line.slice(at + 1).trim())
|
|
75
|
+
}
|
|
76
|
+
if (facts.get('reach') !== 'ok') {
|
|
77
|
+
const why = result.error ?? (result.stderr.trim() || (result.timedOut === true ? 'the connection timed out' : 'no answer'))
|
|
78
|
+
return {
|
|
79
|
+
code: 'host-unreachable',
|
|
80
|
+
message: `host "${target.host}" did not answer an ssh command: ${why.slice(0, 200)}`,
|
|
81
|
+
hint: 'Check the alias and credentials in the dsh-ssh settings (or use task_board_ssh action "hosts" to list the aliases), then retry.',
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (program !== undefined && facts.get('agent') !== 'ok') {
|
|
85
|
+
return {
|
|
86
|
+
code: 'agent-missing',
|
|
87
|
+
message: `"${program}" is not on the PATH of host "${target.host}"`,
|
|
88
|
+
hint: `Install ${program} there, or pick another agent (dsh, claude, codex, custom).`,
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (target.workspace.kind === 'in-place') {
|
|
92
|
+
if (facts.get('path') !== 'ok') {
|
|
93
|
+
return {
|
|
94
|
+
code: 'path-missing',
|
|
95
|
+
message: `directory ${target.workspace.path} does not exist on host "${target.host}"; an in-place target never creates it`,
|
|
96
|
+
hint: 'Create the directory on the host first, or use workspaceKind "git" to get a per-run worktree.',
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (facts.get('write') !== 'ok') {
|
|
100
|
+
return {
|
|
101
|
+
code: 'permission-required',
|
|
102
|
+
message: `directory ${target.workspace.path} on host "${target.host}" is not writable by the ssh user`,
|
|
103
|
+
hint: 'Grant that user write access (or pick a directory it owns), then retry.',
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return undefined
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// --- filtered log ---------------------------------------------------------------
|
|
111
|
+
|
|
112
|
+
/** Longest line a filtered log returns, in bytes; longer ones are cut and marked. */
|
|
113
|
+
export const LOG_LINE_MAX_BYTES = 2_000
|
|
114
|
+
|
|
115
|
+
/** Default and ceiling of one log reply, in bytes. */
|
|
116
|
+
export const LOG_REPLY_DEFAULT_BYTES = 64 * 1024
|
|
117
|
+
|
|
118
|
+
/** Lines `tail` returns when the caller names neither `tail` nor `since`. */
|
|
119
|
+
export const LOG_TAIL_DEFAULT_LINES = 40
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* awk program that drops what an agent never needs from a stream-json log:
|
|
123
|
+
* every `system` event except `init` (thinking_tokens, hooks, status), rate
|
|
124
|
+
* limit notices and partial stream deltas. It also cuts overlong lines.
|
|
125
|
+
*/
|
|
126
|
+
const NOISE_FILTER = [
|
|
127
|
+
`/^\\{"type":"(stream_event|rate_limit_event)"/ {next}`,
|
|
128
|
+
`/^\\{"type":"system","subtype":"init"/ {print; next}`,
|
|
129
|
+
`/^\\{"type":"system"/ {next}`,
|
|
130
|
+
`{ if (length($0) > ${String(LOG_LINE_MAX_BYTES)}) $0 = substr($0, 1, ${String(LOG_LINE_MAX_BYTES)}) " ...[line cut]"; print }`,
|
|
131
|
+
].join(' ')
|
|
132
|
+
|
|
133
|
+
/** What one log query asks for. */
|
|
134
|
+
export interface SshLogQuery {
|
|
135
|
+
/** Absolute path of the run's stdout log. */
|
|
136
|
+
file: string
|
|
137
|
+
/** Last N filtered lines (default 40). Ignored when `since` is given. */
|
|
138
|
+
tail?: number
|
|
139
|
+
/** First filtered line to return, 1-based: the `nextSince` of an earlier reply. */
|
|
140
|
+
since?: number
|
|
141
|
+
/** Skip the filter and read raw lines. */
|
|
142
|
+
raw?: boolean
|
|
143
|
+
/** Cap on the reply text, in bytes. */
|
|
144
|
+
maxBytes?: number
|
|
145
|
+
/** Count only: no text. */
|
|
146
|
+
statsOnly?: boolean
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Counts of one log, before any text is cost. */
|
|
150
|
+
export interface SshLogStats {
|
|
151
|
+
/** Size of the raw log on the host. */
|
|
152
|
+
rawBytes: number
|
|
153
|
+
/** Lines the filter keeps. */
|
|
154
|
+
lines: number
|
|
155
|
+
/** Bytes the kept lines take. */
|
|
156
|
+
bytes: number
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The command that answers one log query, text after a `stats` line. */
|
|
160
|
+
export function logCommand(query: SshLogQuery): string {
|
|
161
|
+
const file = shellQuote(query.file)
|
|
162
|
+
const filter = query.raw === true ? 'cat' : `LC_ALL=C awk ${shellQuote(NOISE_FILTER)}`
|
|
163
|
+
const count = `LC_ALL=C awk '{ l += 1; b += length($0) + 1 } END { printf "%d %d", l, b }'`
|
|
164
|
+
const cap = Math.max(1, Math.min(Math.trunc(query.maxBytes ?? LOG_REPLY_DEFAULT_BYTES), 128 * 1024))
|
|
165
|
+
const select = query.since !== undefined
|
|
166
|
+
? `tail -n +${String(Math.max(1, Math.trunc(query.since)))}`
|
|
167
|
+
: `tail -n ${String(Math.max(1, Math.min(Math.trunc(query.tail ?? LOG_TAIL_DEFAULT_LINES), 2_000)))}`
|
|
168
|
+
const body = query.statsOnly === true ? '' : `; ${filter} ${file} | ${select} | head -c ${String(cap + 1)}`
|
|
169
|
+
return `if [ ! -f ${file} ]; then echo "stats none"; exit 0; fi; printf 'stats %s ' "$(wc -c < ${file} | tr -d ' ')"; ${filter} ${file} | ${count}; echo${body}`
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** One answered log query. */
|
|
173
|
+
export interface SshLogAnswer {
|
|
174
|
+
stats: SshLogStats | undefined
|
|
175
|
+
/** Filtered text, complete lines only, within the cap. */
|
|
176
|
+
text: string
|
|
177
|
+
/** Filtered lines in `text`. */
|
|
178
|
+
lines: number
|
|
179
|
+
/** Whether lines remain after `text`. */
|
|
180
|
+
more: boolean
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Parse the answer of {@link logCommand}.
|
|
185
|
+
* @param stdout - what the command printed.
|
|
186
|
+
* @param query - the query it answered.
|
|
187
|
+
* @returns stats and the complete lines that fit the cap.
|
|
188
|
+
*/
|
|
189
|
+
export function readLogAnswer(stdout: string, query: SshLogQuery): SshLogAnswer {
|
|
190
|
+
const newline = stdout.indexOf('\n')
|
|
191
|
+
const head = newline === -1 ? stdout : stdout.slice(0, newline)
|
|
192
|
+
const rest = newline === -1 ? '' : stdout.slice(newline + 1)
|
|
193
|
+
const match = /^stats (\d+) (\d+) (\d+)$/.exec(head.trim())
|
|
194
|
+
const stats = match === null
|
|
195
|
+
? undefined
|
|
196
|
+
: { rawBytes: Number(match[1]), lines: Number(match[2]), bytes: Number(match[3]) }
|
|
197
|
+
const cap = Math.max(1, Math.min(Math.trunc(query.maxBytes ?? LOG_REPLY_DEFAULT_BYTES), 128 * 1024))
|
|
198
|
+
let text = rest
|
|
199
|
+
let cut = false
|
|
200
|
+
if (new TextEncoder().encode(text).length > cap) {
|
|
201
|
+
cut = true
|
|
202
|
+
text = text.slice(0, text.lastIndexOf('\n') + 1)
|
|
203
|
+
while (new TextEncoder().encode(text).length > cap) text = text.slice(0, text.slice(0, -1).lastIndexOf('\n') + 1)
|
|
204
|
+
}
|
|
205
|
+
const lines = text === '' ? 0 : text.split('\n').filter((line, index, all) => line !== '' || index < all.length - 1).length
|
|
206
|
+
const firstLine = query.since ?? Math.max(1, (stats?.lines ?? 0) - Math.trunc(query.tail ?? LOG_TAIL_DEFAULT_LINES) + 1)
|
|
207
|
+
const more = cut || (stats !== undefined && firstLine + lines - 1 < stats.lines)
|
|
208
|
+
return { stats, text, lines, more }
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** First filtered line number a tail of `tail` lines starts at, given the totals. */
|
|
212
|
+
export function tailStart(stats: SshLogStats, tail: number): number {
|
|
213
|
+
return Math.max(1, stats.lines - Math.max(1, tail) + 1)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// --- last activity --------------------------------------------------------------
|
|
217
|
+
|
|
218
|
+
/** The agent's last visible action: a tool call or a message. */
|
|
219
|
+
export interface SshActivity {
|
|
220
|
+
/** `tool_use`, `assistant` or `result`. */
|
|
221
|
+
kind: string
|
|
222
|
+
/** Tool name for a tool call. */
|
|
223
|
+
name?: string
|
|
224
|
+
/** One line of what it was given or said. */
|
|
225
|
+
detail: string
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** One short line out of a tool input. */
|
|
229
|
+
function inputLine(input: unknown): string {
|
|
230
|
+
if (typeof input !== 'object' || input === null) return ''
|
|
231
|
+
const row = input as Record<string, unknown>
|
|
232
|
+
for (const key of ['command', 'file_path', 'path', 'pattern', 'url', 'description', 'prompt']) {
|
|
233
|
+
const value = row[key]
|
|
234
|
+
if (typeof value === 'string' && value !== '') return value.replace(/\s+/g, ' ').slice(0, 160)
|
|
235
|
+
}
|
|
236
|
+
return JSON.stringify(row).slice(0, 160)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The last tool call (or message) in a stretch of stream-json lines.
|
|
241
|
+
* @param text - filtered log lines, oldest first.
|
|
242
|
+
* @returns the last action, or undefined when none parses.
|
|
243
|
+
*/
|
|
244
|
+
export function lastActivity(text: string): SshActivity | undefined {
|
|
245
|
+
const lines = text.split('\n').filter(line => line.trim() !== '')
|
|
246
|
+
for (let index = lines.length - 1; index >= 0; index -= 1) {
|
|
247
|
+
let event: unknown
|
|
248
|
+
try { event = JSON.parse(lines[index] as string) } catch { continue }
|
|
249
|
+
if (typeof event !== 'object' || event === null) continue
|
|
250
|
+
const row = event as Record<string, unknown>
|
|
251
|
+
if (row.type === 'result') return { kind: 'result', detail: String(row.subtype ?? 'result') }
|
|
252
|
+
if (row.type !== 'assistant') continue
|
|
253
|
+
const message = row.message as { content?: unknown } | undefined
|
|
254
|
+
if (!Array.isArray(message?.content)) continue
|
|
255
|
+
for (let at = message.content.length - 1; at >= 0; at -= 1) {
|
|
256
|
+
const block = message.content[at] as Record<string, unknown>
|
|
257
|
+
if (block.type === 'tool_use') return { kind: 'tool_use', name: String(block.name ?? ''), detail: inputLine(block.input) }
|
|
258
|
+
if (block.type === 'text' && typeof block.text === 'string' && block.text.trim() !== '') {
|
|
259
|
+
return { kind: 'assistant', detail: block.text.trim().replace(/\s+/g, ' ').slice(0, 160) }
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return undefined
|
|
264
|
+
}
|