@fish-under-sea/dsh-ssh-fish 0.0.0-stage → 1.0.0

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 (82) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +23 -0
  3. package/README.en.md +29 -0
  4. package/README.i18n.yaml +6 -0
  5. package/README.md +28 -2
  6. package/README.zh.md +29 -0
  7. package/cordis.patch.yml +12 -0
  8. package/icon.svg +3 -0
  9. package/lib/client.js +16487 -0
  10. package/lib/client.js.map +1 -0
  11. package/lib/index.js +3511 -0
  12. package/lib/types/client/api.d.ts +141 -0
  13. package/lib/types/client/index.d.ts +37 -0
  14. package/lib/types/client/locales.d.ts +164 -0
  15. package/lib/types/client/native-panel.d.ts +70 -0
  16. package/lib/types/client/panel/ClusterTab.d.ts +7 -0
  17. package/lib/types/client/panel/HostFormDialog.d.ts +12 -0
  18. package/lib/types/client/panel/HostsTab.d.ts +25 -0
  19. package/lib/types/client/panel/SshPanel.d.ts +14 -0
  20. package/lib/types/client/panel/TerminalTab.d.ts +22 -0
  21. package/lib/types/client/panel/TransferTab.d.ts +7 -0
  22. package/lib/types/client/panel/TunnelsTab.d.ts +19 -0
  23. package/lib/types/client/panel/controller.d.ts +89 -0
  24. package/lib/types/client/panel/helpers.d.ts +45 -0
  25. package/lib/types/client/panel/xterm.css.d.ts +2 -0
  26. package/lib/types/client/settings-binding.d.ts +61 -0
  27. package/lib/types/dsh-home.d.ts +16 -0
  28. package/lib/types/engine/cluster.d.ts +19 -0
  29. package/lib/types/engine/connection-pool.d.ts +112 -0
  30. package/lib/types/engine/proxy-command.d.ts +36 -0
  31. package/lib/types/engine/pty.d.ts +32 -0
  32. package/lib/types/engine/sftp.d.ts +20 -0
  33. package/lib/types/engine/terminal-sessions.d.ts +67 -0
  34. package/lib/types/engine/tunnel.d.ts +36 -0
  35. package/lib/types/engine.d.ts +89 -0
  36. package/lib/types/http.d.ts +59 -0
  37. package/lib/types/index.d.ts +83 -0
  38. package/lib/types/loopback.d.ts +24 -0
  39. package/lib/types/mount-once.d.ts +55 -0
  40. package/lib/types/protocol.d.ts +239 -0
  41. package/lib/types/routes.d.ts +33 -0
  42. package/lib/types/ssh-config.d.ts +34 -0
  43. package/lib/types/store.d.ts +71 -0
  44. package/lib/types/tool-surface.d.ts +71 -0
  45. package/lib/types/tools.d.ts +42 -0
  46. package/package.json +109 -4
  47. package/src/client/api.ts +562 -0
  48. package/src/client/css-modules.d.ts +5 -0
  49. package/src/client/index.ts +137 -0
  50. package/src/client/locales.ts +338 -0
  51. package/src/client/native-panel.tsx +141 -0
  52. package/src/client/panel/ClusterTab.tsx +123 -0
  53. package/src/client/panel/HostFormDialog.tsx +258 -0
  54. package/src/client/panel/HostsTab.tsx +325 -0
  55. package/src/client/panel/SshPanel.tsx +90 -0
  56. package/src/client/panel/TerminalTab.tsx +330 -0
  57. package/src/client/panel/TransferTab.tsx +232 -0
  58. package/src/client/panel/TunnelsTab.tsx +244 -0
  59. package/src/client/panel/controller.ts +167 -0
  60. package/src/client/panel/helpers.ts +84 -0
  61. package/src/client/panel/panel.module.css +876 -0
  62. package/src/client/panel/xterm.css.ts +2 -0
  63. package/src/client/settings-binding.ts +165 -0
  64. package/src/dsh-home.ts +42 -0
  65. package/src/engine/cluster.ts +60 -0
  66. package/src/engine/connection-pool.ts +543 -0
  67. package/src/engine/proxy-command.ts +248 -0
  68. package/src/engine/pty.ts +78 -0
  69. package/src/engine/sftp.ts +226 -0
  70. package/src/engine/terminal-sessions.ts +280 -0
  71. package/src/engine/tunnel.ts +194 -0
  72. package/src/engine.ts +176 -0
  73. package/src/http.ts +127 -0
  74. package/src/index.ts +234 -0
  75. package/src/loopback.ts +63 -0
  76. package/src/mount-once.ts +142 -0
  77. package/src/protocol.ts +230 -0
  78. package/src/routes.ts +494 -0
  79. package/src/ssh-config.ts +153 -0
  80. package/src/store.ts +419 -0
  81. package/src/tool-surface.ts +95 -0
  82. package/src/tools.ts +404 -0
package/src/tools.ts ADDED
@@ -0,0 +1,404 @@
1
+ /**
2
+ * Agent tools: the DSH-native counterpart of ssh-skill's CLI. Every tool
3
+ * talks to the same engine the web UI uses, so a host configured in the GUI
4
+ * is immediately operable by any agent, and vice versa.
5
+ */
6
+
7
+ import { defineTool } from '@deepseek-ai/dsh-tools'
8
+ import type { ContentBlock } from '@deepseek-ai/dsh-llm'
9
+ import type { SshEngine } from './engine.ts'
10
+ import type { ClusterResult, ExecResult, SshAuthKind, SshHostSummary, TunnelInfo } from './protocol.ts'
11
+
12
+ /** One text content block (the only render shape these tools emit). */
13
+ function text(value: string): ContentBlock[] {
14
+ return [{ type: 'text', text: value }]
15
+ }
16
+
17
+ /**
18
+ * One host row for the agent surface. The ProxyCommand STRING is deliberately
19
+ * projected to a boolean: the command may embed credentials for a bastion
20
+ * client, and the model only needs to know that the host goes through one.
21
+ */
22
+ export interface AgentHostRow {
23
+ alias: string
24
+ host: string
25
+ port: number
26
+ user: string
27
+ auth: SshAuthKind
28
+ keyReady: boolean
29
+ proxyJump: string[]
30
+ proxyCommandConfigured: boolean
31
+ description?: string
32
+ environment?: string
33
+ tags: string[]
34
+ location?: string
35
+ createdAt: number
36
+ updatedAt: number
37
+ }
38
+
39
+ /** Project one summary onto the agent-facing row shape. */
40
+ export function toAgentHostRow(host: SshHostSummary): AgentHostRow {
41
+ const { proxyCommand, ...rest } = host
42
+ return { ...rest, proxyCommandConfigured: proxyCommand !== undefined }
43
+ }
44
+
45
+ /** Host table render shared by list surfaces. */
46
+ function renderHosts(hosts: AgentHostRow[]): string {
47
+ if (hosts.length === 0) return 'no hosts configured'
48
+ const rows = hosts.map(host => [
49
+ host.alias,
50
+ host.host,
51
+ String(host.port),
52
+ host.user,
53
+ host.auth,
54
+ host.proxyJump.length > 0 ? 'jump:' + host.proxyJump.join(',') : host.proxyCommandConfigured ? 'proxy-command' : '-',
55
+ host.environment ?? '-',
56
+ (host.tags.length > 0 ? host.tags.join(',') : '-'),
57
+ host.description ?? '',
58
+ ].join(' | '))
59
+ return ['alias | host | port | user | auth | proxy | environment | tags | description', '--- | --- | --- | --- | --- | --- | --- | --- | ---', ...rows].join('\n')
60
+ }
61
+
62
+ /** Render one exec result (mirrors the bash-tool exit-code convention). */
63
+ function renderExec(result: ExecResult): string {
64
+ const marker = result.timedOut
65
+ ? '[timed out]'
66
+ : `[exit code: ${result.exitCode ?? 'null'}]`
67
+ const parts = [marker]
68
+ if (result.stdout !== '') parts.push('stdout:\n' + result.stdout)
69
+ if (result.stderr !== '') parts.push('stderr:\n' + result.stderr)
70
+ if (result.error !== undefined) parts.push('error: ' + result.error)
71
+ parts.push(`duration: ${result.durationMs} ms`)
72
+ return parts.join('\n')
73
+ }
74
+
75
+ /** Render cluster outcomes compactly. */
76
+ function renderCluster(results: ClusterResult[]): string {
77
+ if (results.length === 0) return 'no hosts matched'
78
+ return results.map(result => {
79
+ const status = result.ok ? 'ok' : result.timedOut === true ? 'timed out' : 'failed'
80
+ const tail = result.error !== undefined ? ' (' + result.error + ')' : ''
81
+ return `${result.alias}: ${status} [exit code: ${result.exitCode ?? 'null'}]${tail}`
82
+ }).join('\n')
83
+ }
84
+
85
+ /** One tunnel line. */
86
+ function renderTunnel(tunnel: TunnelInfo): string {
87
+ return `${tunnel.id} ${tunnel.alias} 127.0.0.1:${tunnel.localPort} -> ${tunnel.remoteHost}:${tunnel.remotePort} [${tunnel.state}]`
88
+ }
89
+
90
+ /** The host-list tool. */
91
+ export function sshListTool(engine: SshEngine) {
92
+ return defineTool({
93
+ name: 'ssh_list',
94
+ description: 'List configured SSH hosts (alias, host, user, auth, environment, tags, description). Use ssh_exec etc. with the alias.',
95
+ parameters: {
96
+ query: { type: 'string', description: 'Optional fuzzy match against alias, description, host, and tags.' },
97
+ },
98
+ output: {
99
+ schema: {
100
+ type: 'object',
101
+ additionalProperties: false,
102
+ properties: {
103
+ hosts: {
104
+ type: 'array',
105
+ required: true,
106
+ items: {
107
+ type: 'object',
108
+ additionalProperties: false,
109
+ properties: {
110
+ alias: { type: 'string', required: true },
111
+ host: { type: 'string', required: true },
112
+ port: { type: 'integer', required: true },
113
+ user: { type: 'string', required: true },
114
+ auth: { type: 'string', enum: ['key', 'password', 'agent'], required: true },
115
+ keyReady: { type: 'boolean', required: true },
116
+ proxyJump: { type: 'array', items: { type: 'string' }, required: true },
117
+ proxyCommandConfigured: { type: 'boolean', required: true },
118
+ description: { type: 'string' },
119
+ environment: { type: 'string' },
120
+ tags: { type: 'array', items: { type: 'string' }, required: true },
121
+ location: { type: 'string' },
122
+ createdAt: { type: 'integer', required: true },
123
+ updatedAt: { type: 'integer', required: true },
124
+ },
125
+ },
126
+ },
127
+ },
128
+ },
129
+ render: (_args, value: { hosts?: AgentHostRow[] }) => text(renderHosts(value.hosts ?? [])),
130
+ },
131
+ // Reads the local host registry only: safe to overlap with sibling calls.
132
+ isConcurrencySafe: () => true,
133
+ async execute(args) {
134
+ return { hosts: engine.list(args.query).map(toAgentHostRow) }
135
+ },
136
+ })
137
+ }
138
+
139
+ /** The command-execution tool. */
140
+ export function sshExecTool(engine: SshEngine) {
141
+ return defineTool({
142
+ name: 'ssh_exec',
143
+ description: 'Execute a shell command on a REMOTE SSH host by alias; the command runs on the remote host, never on this machine. For commands on this machine, use the local bash tool. Prefer combining independent read-only queries into one command.',
144
+ parameters: {
145
+ alias: { type: 'string', required: true, description: 'Host alias from ssh_list.' },
146
+ command: { type: 'string', required: true, description: 'The shell command to run remotely.' },
147
+ timeoutMs: { type: 'integer', description: 'Timeout in milliseconds (default 60000).' },
148
+ },
149
+ output: {
150
+ schema: {
151
+ type: 'object',
152
+ additionalProperties: false,
153
+ properties: {
154
+ success: { type: 'boolean', required: true },
155
+ exitCode: { oneOf: [{ type: 'integer' }, { type: 'null' }], required: true },
156
+ timedOut: { type: 'boolean', required: true },
157
+ stdout: { type: 'string', required: true },
158
+ stderr: { type: 'string', required: true },
159
+ durationMs: { type: 'integer', required: true },
160
+ error: { type: 'string' },
161
+ },
162
+ },
163
+ render: (_args, value: ExecResult) => text(renderExec(value)),
164
+ },
165
+ async execute(args, exec) {
166
+ try {
167
+ // Forward the caller's cancellation: aborting closes the remote channel
168
+ // instead of leaving the command running on the host.
169
+ return await engine.exec(args.alias, args.command, args.timeoutMs, exec.signal)
170
+ } catch (error) {
171
+ return {
172
+ success: false,
173
+ exitCode: null,
174
+ timedOut: false,
175
+ stdout: '',
176
+ stderr: '',
177
+ durationMs: 0,
178
+ error: error instanceof Error ? error.message : String(error),
179
+ }
180
+ }
181
+ },
182
+ })
183
+ }
184
+
185
+ /** The upload tool. */
186
+ export function sshUploadTool(engine: SshEngine) {
187
+ return defineTool({
188
+ name: 'ssh_upload',
189
+ description: 'Transfer a file FROM this machine (the dsh host) TO a remote SSH host. Use this only when the file must be copied to the remote host. Files that stay on this machine are handled with the local file tools (read / write / edit), not ssh_upload.',
190
+ parameters: {
191
+ alias: { type: 'string', required: true, description: 'Host alias from ssh_list.' },
192
+ localPath: { type: 'string', required: true, description: 'Absolute path of the source file on THIS machine (the dsh host) — not a path on the remote host.' },
193
+ remotePath: { type: 'string', required: true, description: 'Absolute destination path on the remote SSH host (parent dirs are created).' },
194
+ },
195
+ output: {
196
+ schema: {
197
+ type: 'object',
198
+ additionalProperties: false,
199
+ properties: {
200
+ ok: { type: 'boolean', required: true },
201
+ transferredBytes: { type: 'integer' },
202
+ files: { type: 'integer' },
203
+ error: { type: 'string' },
204
+ },
205
+ },
206
+ render: (_args, value: { ok: boolean; transferredBytes?: number; files?: number; error?: string }) => text(value.ok
207
+ ? `uploaded ${value.files ?? 1} file(s), ${value.transferredBytes ?? 0} bytes`
208
+ : `upload failed: ${value.error ?? 'unknown error'}`),
209
+ },
210
+ async execute(args) {
211
+ try {
212
+ const outcome = await engine.upload(args.alias, args.localPath, args.remotePath, false)
213
+ return { ok: true, transferredBytes: outcome.bytes, files: outcome.files }
214
+ } catch (error) {
215
+ return { ok: false, error: error instanceof Error ? error.message : String(error) }
216
+ }
217
+ },
218
+ })
219
+ }
220
+
221
+ /** The download tool. */
222
+ export function sshDownloadTool(engine: SshEngine) {
223
+ return defineTool({
224
+ name: 'ssh_download',
225
+ description: 'Copy a remote FILE from a configured SSH host to this machine (the dsh host). Use this only when the source is on the remote host; files already on this machine are read with the local file tools (read / write / edit), not ssh_download. Directory download is not supported — download files individually.',
226
+ parameters: {
227
+ alias: { type: 'string', required: true, description: 'Host alias from ssh_list.' },
228
+ remotePath: { type: 'string', required: true, description: 'Absolute path of the source file on the remote SSH host.' },
229
+ localPath: { type: 'string', required: true, description: 'Absolute destination path on THIS machine (the dsh host).' },
230
+ },
231
+ output: {
232
+ schema: {
233
+ type: 'object',
234
+ additionalProperties: false,
235
+ properties: {
236
+ ok: { type: 'boolean', required: true },
237
+ bytes: { type: 'integer' },
238
+ error: { type: 'string' },
239
+ },
240
+ },
241
+ render: (_args, value: { ok: boolean; bytes?: number; error?: string }) => text(value.ok
242
+ ? `downloaded ${value.bytes ?? 0} bytes`
243
+ : `download failed: ${value.error ?? 'unknown error'}`),
244
+ },
245
+ async execute(args) {
246
+ try {
247
+ const outcome = await engine.download(args.alias, args.remotePath, args.localPath)
248
+ return { ok: true, bytes: outcome.bytes }
249
+ } catch (error) {
250
+ return { ok: false, error: error instanceof Error ? error.message : String(error) }
251
+ }
252
+ },
253
+ })
254
+ }
255
+
256
+ /** The tunnel tool. */
257
+ export function sshTunnelTool(engine: SshEngine) {
258
+ return defineTool({
259
+ name: 'ssh_tunnel',
260
+ description: 'Manage local port-forward tunnels to a configured SSH host. Start a tunnel to reach a remote internal service (database, web UI, API) through 127.0.0.1 on this machine.',
261
+ parameters: {
262
+ action: { type: 'string', required: true, enum: ['start', 'list', 'stop', 'stop-all'], description: 'start / list / stop / stop-all.' },
263
+ alias: { type: 'string', description: 'Host alias (required for start, optional for stop-all).' },
264
+ remotePort: { type: 'integer', description: 'Port on the remote side (required for start).' },
265
+ remoteHost: { type: 'string', description: 'Remote host to forward to (default 127.0.0.1 — the server itself).' },
266
+ localPort: { type: 'integer', description: 'Local listening port (default: auto-assigned).' },
267
+ tunnelId: { type: 'string', description: 'Tunnel id (required for stop).' },
268
+ },
269
+ output: {
270
+ schema: {
271
+ type: 'object',
272
+ additionalProperties: false,
273
+ properties: {
274
+ ok: { type: 'boolean', required: true },
275
+ tunnel: {
276
+ type: 'object',
277
+ additionalProperties: false,
278
+ properties: {
279
+ id: { type: 'string', required: true },
280
+ alias: { type: 'string', required: true },
281
+ localPort: { type: 'integer', required: true },
282
+ remoteHost: { type: 'string', required: true },
283
+ remotePort: { type: 'integer', required: true },
284
+ state: { type: 'string', enum: ['forwarding', 'connecting', 'failed'], required: true },
285
+ error: { type: 'string' },
286
+ startedAt: { type: 'integer', required: true },
287
+ },
288
+ },
289
+ tunnels: {
290
+ type: 'array',
291
+ items: {
292
+ type: 'object',
293
+ additionalProperties: false,
294
+ properties: {
295
+ id: { type: 'string', required: true },
296
+ alias: { type: 'string', required: true },
297
+ localPort: { type: 'integer', required: true },
298
+ remoteHost: { type: 'string', required: true },
299
+ remotePort: { type: 'integer', required: true },
300
+ state: { type: 'string', enum: ['forwarding', 'connecting', 'failed'], required: true },
301
+ error: { type: 'string' },
302
+ startedAt: { type: 'integer', required: true },
303
+ },
304
+ },
305
+ },
306
+ stopped: { type: 'integer' },
307
+ error: { type: 'string' },
308
+ },
309
+ },
310
+ render: (_args, value: { ok: boolean; tunnel?: TunnelInfo; tunnels?: TunnelInfo[]; stopped?: number; error?: string }) => {
311
+ if (value.error !== undefined) return text(`tunnel error: ${value.error}`)
312
+ if (value.tunnel !== undefined) {
313
+ if (value.tunnel.state === 'failed') return text(`tunnel failed: ${value.tunnel.error ?? 'unknown error'}`)
314
+ return text(`tunnel started: ${renderTunnel(value.tunnel)}`)
315
+ }
316
+ if (value.tunnels !== undefined) return text(value.tunnels.length === 0 ? 'no active tunnels' : value.tunnels.map(renderTunnel).join('\n'))
317
+ return text(`stopped ${value.stopped ?? 0} tunnel(s)`)
318
+ },
319
+ },
320
+ async execute(args) {
321
+ if (args.action === 'list') {
322
+ return { ok: true, tunnels: engine.listTunnels() }
323
+ }
324
+ if (args.action === 'start') {
325
+ if (args.alias === undefined || args.remotePort === undefined) {
326
+ throw new Error('alias and remotePort are required for start')
327
+ }
328
+ try {
329
+ const tunnel = await engine.startTunnel(args.alias, {
330
+ remotePort: args.remotePort,
331
+ remoteHost: args.remoteHost,
332
+ localPort: args.localPort,
333
+ })
334
+ return { ok: true, tunnel }
335
+ } catch (error) {
336
+ return { ok: false, tunnel: { id: '', alias: args.alias, localPort: 0, remoteHost: args.remoteHost ?? '127.0.0.1', remotePort: args.remotePort, state: 'failed' as const, error: error instanceof Error ? error.message : String(error), startedAt: Date.now() } }
337
+ }
338
+ }
339
+ if (args.action === 'stop') {
340
+ if (args.tunnelId === undefined) throw new Error('tunnelId is required for stop')
341
+ const stopped = engine.stopTunnel(args.tunnelId)
342
+ return stopped
343
+ ? { ok: true, stopped: 1 }
344
+ : { ok: false, stopped: 0, error: `tunnel '${args.tunnelId}' not found` }
345
+ }
346
+ if (args.action === 'stop-all') {
347
+ const stopped = engine.stopAllTunnels(args.alias)
348
+ return { ok: true, stopped }
349
+ }
350
+ throw new Error(`unknown action '${String(args.action)}'`)
351
+ },
352
+ })
353
+ }
354
+
355
+ /** The cluster tool. */
356
+ export function sshClusterTool(engine: SshEngine) {
357
+ return defineTool({
358
+ name: 'ssh_cluster',
359
+ description: 'Run one command concurrently across selected SSH hosts; at least one aliases, environment, or tags filter is required.',
360
+ parameters: {
361
+ command: { type: 'string', required: true, description: 'The shell command to run on every matched host.' },
362
+ aliases: { type: 'array', items: { type: 'string' }, description: 'Optional alias filter; at least one of aliases, environment, or tags is required.' },
363
+ environment: { type: 'string', description: 'Optional environment filter; at least one selector is required.' },
364
+ tags: { type: 'array', items: { type: 'string' }, description: 'Optional ALL-tags filter; at least one selector is required.' },
365
+ timeoutMs: { type: 'integer', description: 'Per-host timeout in milliseconds.' },
366
+ maxWorkers: { type: 'integer', description: 'Concurrency cap (default 8).' },
367
+ },
368
+ output: {
369
+ schema: {
370
+ type: 'object',
371
+ additionalProperties: false,
372
+ properties: {
373
+ results: {
374
+ type: 'array',
375
+ required: true,
376
+ items: {
377
+ type: 'object',
378
+ additionalProperties: false,
379
+ properties: {
380
+ alias: { type: 'string', required: true },
381
+ ok: { type: 'boolean', required: true },
382
+ exitCode: { oneOf: [{ type: 'integer' }, { type: 'null' }] },
383
+ timedOut: { type: 'boolean' },
384
+ stdout: { type: 'string' },
385
+ stderr: { type: 'string' },
386
+ durationMs: { type: 'integer' },
387
+ error: { type: 'string' },
388
+ },
389
+ },
390
+ },
391
+ },
392
+ },
393
+ render: (_args, value: { results?: ClusterResult[] }) => text(renderCluster(value.results ?? [])),
394
+ },
395
+ async execute(args, exec) {
396
+ const hasSelector = (Array.isArray(args.aliases) && args.aliases.some((alias) => typeof alias === 'string' && alias.trim() !== '')) ||
397
+ (typeof args.environment === 'string' && args.environment.trim() !== '') ||
398
+ (Array.isArray(args.tags) && args.tags.some((tag) => typeof tag === 'string' && tag.trim() !== ''))
399
+ if (!hasSelector) throw new Error('ssh_cluster requires aliases, environment, or tags to limit the target set')
400
+ // Cancellation reaches every host's in-flight command.
401
+ return { results: await engine.cluster({ ...args, signal: exec.signal }) }
402
+ },
403
+ })
404
+ }