dsh-ssh-hub 0.0.0-stage → 0.2.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.
- package/LICENSE +202 -0
- package/README.md +271 -2
- package/cordis.patch.yml +16 -0
- package/icon.svg +3 -0
- package/lib/client.js +13975 -0
- package/lib/index.js +6908 -0
- package/lib/types/activity/history.d.ts +50 -0
- package/lib/types/alerting/notify.d.ts +89 -0
- package/lib/types/alerting/prompt-summary.d.ts +28 -0
- package/lib/types/alerting/rules.d.ts +135 -0
- package/lib/types/alerting/tools.d.ts +30 -0
- package/lib/types/audit/audit-log.d.ts +84 -0
- package/lib/types/audit/recorder.d.ts +50 -0
- package/lib/types/client/api.d.ts +222 -0
- package/lib/types/client/index.d.ts +45 -0
- package/lib/types/client/locales.d.ts +319 -0
- package/lib/types/client/native-panel.d.ts +70 -0
- package/lib/types/client/panel/ClusterTab.d.ts +7 -0
- package/lib/types/client/panel/DeployTab.d.ts +10 -0
- package/lib/types/client/panel/FleetTab.d.ts +10 -0
- package/lib/types/client/panel/HostFormDialog.d.ts +12 -0
- package/lib/types/client/panel/HostsTab.d.ts +25 -0
- package/lib/types/client/panel/LogsTab.d.ts +10 -0
- package/lib/types/client/panel/SshPanel.d.ts +14 -0
- package/lib/types/client/panel/TerminalTab.d.ts +27 -0
- package/lib/types/client/panel/TransferTab.d.ts +7 -0
- package/lib/types/client/panel/TunnelEditDialog.d.ts +15 -0
- package/lib/types/client/panel/TunnelsTab.d.ts +27 -0
- package/lib/types/client/panel/controller.d.ts +253 -0
- package/lib/types/client/panel/helpers.d.ts +45 -0
- package/lib/types/client/panel/tabs.d.ts +62 -0
- package/lib/types/client/panel/xterm.css.d.ts +2 -0
- package/lib/types/client/settings-binding.d.ts +71 -0
- package/lib/types/deploy/orchestrator.d.ts +158 -0
- package/lib/types/deploy/tools.d.ts +14 -0
- package/lib/types/dsh-home.d.ts +16 -0
- package/lib/types/engine/cluster.d.ts +19 -0
- package/lib/types/engine/connection-pool.d.ts +183 -0
- package/lib/types/engine/proxy-command.d.ts +36 -0
- package/lib/types/engine/pty.d.ts +32 -0
- package/lib/types/engine/sftp.d.ts +20 -0
- package/lib/types/engine/terminal-sessions.d.ts +67 -0
- package/lib/types/engine/tunnel-discovery.d.ts +89 -0
- package/lib/types/engine/tunnel-remarks.d.ts +49 -0
- package/lib/types/engine/tunnel.d.ts +76 -0
- package/lib/types/engine.d.ts +103 -0
- package/lib/types/http.d.ts +59 -0
- package/lib/types/index.d.ts +128 -0
- package/lib/types/inspect/checks.d.ts +118 -0
- package/lib/types/inspect/inspection.d.ts +111 -0
- package/lib/types/inspect/tools.d.ts +50 -0
- package/lib/types/logs/search.d.ts +131 -0
- package/lib/types/logs/tools.d.ts +6 -0
- package/lib/types/loopback.d.ts +64 -0
- package/lib/types/monitor/loop.d.ts +133 -0
- package/lib/types/monitor/metrics.d.ts +151 -0
- package/lib/types/mount-once.d.ts +55 -0
- package/lib/types/protocol.d.ts +524 -0
- package/lib/types/routes.d.ts +82 -0
- package/lib/types/security/known-hosts.d.ts +153 -0
- package/lib/types/security/master-secret.d.ts +157 -0
- package/lib/types/security/redact.d.ts +44 -0
- package/lib/types/security/secrets.d.ts +70 -0
- package/lib/types/ssh-config.d.ts +42 -0
- package/lib/types/store.d.ts +183 -0
- package/lib/types/tool-surface.d.ts +72 -0
- package/lib/types/tools.d.ts +56 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +126 -6
- package/src/activity/history.ts +117 -0
- package/src/alerting/notify.ts +212 -0
- package/src/alerting/prompt-summary.ts +71 -0
- package/src/alerting/rules.ts +281 -0
- package/src/alerting/tools.ts +263 -0
- package/src/audit/audit-log.ts +199 -0
- package/src/audit/recorder.ts +73 -0
- package/src/client/api.ts +782 -0
- package/src/client/css-modules.d.ts +5 -0
- package/src/client/index.ts +150 -0
- package/src/client/locales.ts +654 -0
- package/src/client/native-panel.tsx +141 -0
- package/src/client/panel/ClusterTab.tsx +141 -0
- package/src/client/panel/DeployTab.tsx +314 -0
- package/src/client/panel/FleetTab.tsx +494 -0
- package/src/client/panel/HostFormDialog.tsx +289 -0
- package/src/client/panel/HostsTab.tsx +395 -0
- package/src/client/panel/LogsTab.tsx +208 -0
- package/src/client/panel/SshPanel.tsx +105 -0
- package/src/client/panel/TerminalTab.tsx +550 -0
- package/src/client/panel/TransferTab.tsx +238 -0
- package/src/client/panel/TunnelEditDialog.tsx +174 -0
- package/src/client/panel/TunnelsTab.tsx +328 -0
- package/src/client/panel/controller.ts +507 -0
- package/src/client/panel/helpers.ts +84 -0
- package/src/client/panel/panel.module.css +1734 -0
- package/src/client/panel/tabs.ts +52 -0
- package/src/client/panel/xterm.css.ts +2 -0
- package/src/client/settings-binding.ts +184 -0
- package/src/deploy/orchestrator.ts +326 -0
- package/src/deploy/tools.ts +225 -0
- package/src/dsh-home.ts +42 -0
- package/src/engine/cluster.ts +60 -0
- package/src/engine/connection-pool.ts +721 -0
- package/src/engine/proxy-command.ts +248 -0
- package/src/engine/pty.ts +78 -0
- package/src/engine/sftp.ts +229 -0
- package/src/engine/terminal-sessions.ts +280 -0
- package/src/engine/tunnel-discovery.ts +353 -0
- package/src/engine/tunnel-remarks.ts +120 -0
- package/src/engine/tunnel.ts +300 -0
- package/src/engine.ts +195 -0
- package/src/http.ts +127 -0
- package/src/index.ts +690 -0
- package/src/inspect/checks.ts +251 -0
- package/src/inspect/inspection.ts +321 -0
- package/src/inspect/tools.ts +366 -0
- package/src/logs/search.ts +251 -0
- package/src/logs/tools.ts +160 -0
- package/src/loopback.ts +119 -0
- package/src/monitor/loop.ts +265 -0
- package/src/monitor/metrics.ts +333 -0
- package/src/mount-once.ts +142 -0
- package/src/protocol.ts +534 -0
- package/src/routes.ts +998 -0
- package/src/security/known-hosts.ts +342 -0
- package/src/security/master-secret.ts +334 -0
- package/src/security/redact.ts +119 -0
- package/src/security/secrets.ts +152 -0
- package/src/ssh-config.ts +182 -0
- package/src/store.ts +757 -0
- package/src/tool-surface.ts +96 -0
- package/src/tools.ts +550 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// Generated by scripts/sync-shared.mjs from shared/host/tool-surface.ts. Do not edit this copy; edit the shared source and run "node scripts/sync-shared.mjs".
|
|
2
|
+
/**
|
|
3
|
+
* Family-shared tool-surface conventions for the packages that register
|
|
4
|
+
* model-facing tools.
|
|
5
|
+
*
|
|
6
|
+
* One home for the prompt-section placement and the visibility gate this
|
|
7
|
+
* family previously restated per package.
|
|
8
|
+
*
|
|
9
|
+
* Modelled on the official tool packages:
|
|
10
|
+
* - `@deepseek-ai/dsh-tool-subagent` renders its section through
|
|
11
|
+
* `tools.get(name, context.scope) === undefined ? '' : text`, so guidance
|
|
12
|
+
* disappears together with a restricted tool.
|
|
13
|
+
* - 21 of the 22 official tool packages register their tools GLOBALLY and gate
|
|
14
|
+
* them with a setting, not per agent. Only the Agent Teams tool package —
|
|
15
|
+
* whose tools belong to one membership — installs into each `agent.ctx`.
|
|
16
|
+
* This family follows the majority: registration stays global so the
|
|
17
|
+
* registry's per-scope `restrict()` mask (which filters inherited global
|
|
18
|
+
* tools and REJECTS scope-local names) can still withhold these tools.
|
|
19
|
+
*
|
|
20
|
+
* Structural types only: this copy lands in packages that do not all depend on
|
|
21
|
+
* the same SDK service packages.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Prompt-section order for each package's tool guidance.
|
|
26
|
+
*
|
|
27
|
+
* These sit in the family band between the deployment persona and the SDK's
|
|
28
|
+
* `PLAN_POLICY` (500). Values are owned here rather than written per package so
|
|
29
|
+
* the host plugins whose guidance shares one prompt cannot drift into an
|
|
30
|
+
* accidental tie.
|
|
31
|
+
*/
|
|
32
|
+
export const PLUGIN_TOOL_SECTION_ORDERS = {
|
|
33
|
+
ssh: 150,
|
|
34
|
+
'task-board': 200,
|
|
35
|
+
agents: 220,
|
|
36
|
+
} as const
|
|
37
|
+
|
|
38
|
+
/** One plugin id keyed by {@link PLUGIN_TOOL_SECTION_ORDERS}. */
|
|
39
|
+
export type PluginToolSectionId = keyof typeof PLUGIN_TOOL_SECTION_ORDERS
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Order of an external provider extension's own guidance section, right after
|
|
43
|
+
* the band of the plugin it extends.
|
|
44
|
+
*
|
|
45
|
+
* An extension names itself, not the plugin it plugs into, so its order stays
|
|
46
|
+
* out of {@link PLUGIN_TOOL_SECTION_ORDERS}: every package's copy of this module
|
|
47
|
+
* then carries no provider id. The slot still has one owner and cannot collide
|
|
48
|
+
* with a host plugin's band.
|
|
49
|
+
*/
|
|
50
|
+
export const EXTENSION_TOOL_SECTION_ORDER = 210
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The scoped tool lookup a section provider needs: the registry read for one
|
|
54
|
+
* assembly's scope. Matches `ctx.tools.get(name, scope)`.
|
|
55
|
+
*/
|
|
56
|
+
export interface ScopedToolLookup {
|
|
57
|
+
get(name: string, scope?: unknown): unknown
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** One assembly context carrying the scope the section is rendered for. */
|
|
61
|
+
export interface SectionScopeContext {
|
|
62
|
+
scope?: unknown
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Build a section provider that renders \`text\` only while at least one of
|
|
67
|
+
* \`toolNames\` is visible to the rendering scope.
|
|
68
|
+
*
|
|
69
|
+
* The native convention: a tool that a restriction, a preset, or a missing
|
|
70
|
+
* registry removed must not leave guidance behind telling the model to call it.
|
|
71
|
+
* An unreadable registry renders the text unchanged — guidance that outlives a
|
|
72
|
+
* momentary read failure is recoverable; guidance for a tool that is gone is
|
|
73
|
+
* not.
|
|
74
|
+
* @param tools - the scoped tool registry, when the deployment serves one.
|
|
75
|
+
* @param toolNames - registered names the guidance describes.
|
|
76
|
+
* @param text - the guidance rendered while they are reachable.
|
|
77
|
+
* @returns the section text provider.
|
|
78
|
+
*/
|
|
79
|
+
export function visibleToolText(
|
|
80
|
+
tools: ScopedToolLookup | undefined,
|
|
81
|
+
toolNames: readonly string[],
|
|
82
|
+
text: string,
|
|
83
|
+
): (context: SectionScopeContext) => string {
|
|
84
|
+
if (tools === undefined) return () => text
|
|
85
|
+
return (context: SectionScopeContext) => {
|
|
86
|
+
for (const name of toolNames) {
|
|
87
|
+
try {
|
|
88
|
+
if (tools.get(name, context.scope) !== undefined) return text
|
|
89
|
+
} catch {
|
|
90
|
+
// A refused lookup is not proof the tool is gone; keep the guidance.
|
|
91
|
+
return text
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return ''
|
|
95
|
+
}
|
|
96
|
+
}
|
package/src/tools.ts
ADDED
|
@@ -0,0 +1,550 @@
|
|
|
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
|
+
import { redactCommand, redactOutput } from './security/redact.ts'
|
|
12
|
+
import { discoverExternalTunnels } from './engine/tunnel-discovery.ts'
|
|
13
|
+
import { readTunnelRemarks, setTunnelRemark } from './engine/tunnel-remarks.ts'
|
|
14
|
+
import type { AuditRecorder } from './audit/recorder.ts'
|
|
15
|
+
|
|
16
|
+
/** One text content block (the only render shape these tools emit). */
|
|
17
|
+
function text(value: string): ContentBlock[] {
|
|
18
|
+
return [{ type: 'text', text: value }]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* One host row for the agent surface. The ProxyCommand STRING is deliberately
|
|
23
|
+
* projected to a boolean: the command may embed credentials for a bastion
|
|
24
|
+
* client, and the model only needs to know that the host goes through one.
|
|
25
|
+
*/
|
|
26
|
+
export interface AgentHostRow {
|
|
27
|
+
alias: string
|
|
28
|
+
host: string
|
|
29
|
+
port: number
|
|
30
|
+
user: string
|
|
31
|
+
auth: SshAuthKind
|
|
32
|
+
keyReady: boolean
|
|
33
|
+
proxyJump: string[]
|
|
34
|
+
proxyCommandConfigured: boolean
|
|
35
|
+
forwardAgent: boolean
|
|
36
|
+
description?: string
|
|
37
|
+
environment?: string
|
|
38
|
+
tags: string[]
|
|
39
|
+
location?: string
|
|
40
|
+
createdAt: number
|
|
41
|
+
updatedAt: number
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Project one summary onto the agent-facing row shape. */
|
|
45
|
+
export function toAgentHostRow(host: SshHostSummary): AgentHostRow {
|
|
46
|
+
const { proxyCommand, ...rest } = host
|
|
47
|
+
return { ...rest, proxyCommandConfigured: proxyCommand !== undefined }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Host table render shared by list surfaces. */
|
|
51
|
+
function renderHosts(hosts: AgentHostRow[]): string {
|
|
52
|
+
if (hosts.length === 0) return 'no hosts configured'
|
|
53
|
+
const rows = hosts.map(host => [
|
|
54
|
+
host.alias,
|
|
55
|
+
host.host,
|
|
56
|
+
String(host.port),
|
|
57
|
+
host.user,
|
|
58
|
+
host.auth,
|
|
59
|
+
host.proxyJump.length > 0 ? 'jump:' + host.proxyJump.join(',') : host.proxyCommandConfigured ? 'proxy-command' : '-',
|
|
60
|
+
// Forwarding is a security-relevant opt-in (the remote can sign with this
|
|
61
|
+
// machine's agent), so the model must see it in the table, not only in JSON.
|
|
62
|
+
host.forwardAgent ? 'yes' : '-',
|
|
63
|
+
host.environment ?? '-',
|
|
64
|
+
(host.tags.length > 0 ? host.tags.join(',') : '-'),
|
|
65
|
+
host.description ?? '',
|
|
66
|
+
].join(' | '))
|
|
67
|
+
return ['alias | host | port | user | auth | proxy | forward | environment | tags | description', '--- | --- | --- | --- | --- | --- | --- | --- | --- | ---', ...rows].join('\n')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Render one exec result (mirrors the bash-tool exit-code convention). */
|
|
71
|
+
function renderExec(result: ExecResult): string {
|
|
72
|
+
const marker = result.timedOut
|
|
73
|
+
? '[timed out]'
|
|
74
|
+
: `[exit code: ${result.exitCode ?? 'null'}]`
|
|
75
|
+
const parts = [marker]
|
|
76
|
+
if (result.stdout !== '') parts.push('stdout:\n' + result.stdout)
|
|
77
|
+
if (result.stderr !== '') parts.push('stderr:\n' + result.stderr)
|
|
78
|
+
if (result.error !== undefined) parts.push('error: ' + result.error)
|
|
79
|
+
parts.push(`duration: ${result.durationMs} ms`)
|
|
80
|
+
return parts.join('\n')
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Render cluster outcomes compactly. */
|
|
84
|
+
function renderCluster(results: ClusterResult[]): string {
|
|
85
|
+
if (results.length === 0) return 'no hosts matched'
|
|
86
|
+
return results.map(result => {
|
|
87
|
+
const status = result.ok ? 'ok' : result.timedOut === true ? 'timed out' : 'failed'
|
|
88
|
+
const tail = result.error !== undefined ? ' (' + result.error + ')' : ''
|
|
89
|
+
return `${result.alias}: ${status} [exit code: ${result.exitCode ?? 'null'}]${tail}`
|
|
90
|
+
}).join('\n')
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** One tunnel line. */
|
|
94
|
+
function renderTunnel(tunnel: TunnelInfo): string {
|
|
95
|
+
// An external forward is not ours to stop, and its liveness is a fact worth
|
|
96
|
+
// stating: "forwarding" alone would hide a declared-but-dead listener.
|
|
97
|
+
const owner = tunnel.source === 'external'
|
|
98
|
+
? ` [external pid ${String(tunnel.pid ?? 0)}${tunnel.live === true ? ', port accepting' : ', port NOT listening'}]`
|
|
99
|
+
: ' [plugin]'
|
|
100
|
+
const meta = [tunnel.name, tunnel.description].filter(Boolean).join(' - ')
|
|
101
|
+
const extra = meta ? ` [${meta}]` : ''
|
|
102
|
+
return `${tunnel.id} ${tunnel.alias} 127.0.0.1:${tunnel.localPort} -> ${tunnel.remoteHost}:${tunnel.remotePort} [${tunnel.state}]${owner}${extra}`
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The host-list tool. */
|
|
106
|
+
export function sshListTool(engine: SshEngine) {
|
|
107
|
+
return defineTool({
|
|
108
|
+
name: 'ssh_list',
|
|
109
|
+
description: 'List configured SSH hosts (alias, host, user, auth, agent forwarding, environment, tags, description). Use ssh_exec etc. with the alias.',
|
|
110
|
+
parameters: {
|
|
111
|
+
query: { type: 'string', description: 'Optional fuzzy match against alias, description, host, and tags.' },
|
|
112
|
+
},
|
|
113
|
+
output: {
|
|
114
|
+
schema: {
|
|
115
|
+
type: 'object',
|
|
116
|
+
additionalProperties: false,
|
|
117
|
+
properties: {
|
|
118
|
+
hosts: {
|
|
119
|
+
type: 'array',
|
|
120
|
+
required: true,
|
|
121
|
+
items: {
|
|
122
|
+
type: 'object',
|
|
123
|
+
additionalProperties: false,
|
|
124
|
+
properties: {
|
|
125
|
+
alias: { type: 'string', required: true },
|
|
126
|
+
host: { type: 'string', required: true },
|
|
127
|
+
port: { type: 'integer', required: true },
|
|
128
|
+
user: { type: 'string', required: true },
|
|
129
|
+
auth: { type: 'string', enum: ['key', 'password', 'agent', 'none'], required: true },
|
|
130
|
+
keyReady: { type: 'boolean', required: true },
|
|
131
|
+
proxyJump: { type: 'array', items: { type: 'string' }, required: true },
|
|
132
|
+
proxyCommandConfigured: { type: 'boolean', required: true },
|
|
133
|
+
forwardAgent: { type: 'boolean', required: true },
|
|
134
|
+
description: { type: 'string' },
|
|
135
|
+
environment: { type: 'string' },
|
|
136
|
+
tags: { type: 'array', items: { type: 'string' }, required: true },
|
|
137
|
+
location: { type: 'string' },
|
|
138
|
+
createdAt: { type: 'integer', required: true },
|
|
139
|
+
updatedAt: { type: 'integer', required: true },
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
render: (_args, value: { hosts?: AgentHostRow[] }) => text(renderHosts(value.hosts ?? [])),
|
|
146
|
+
},
|
|
147
|
+
// Reads the local host registry only: safe to overlap with sibling calls.
|
|
148
|
+
isConcurrencySafe: () => true,
|
|
149
|
+
async execute(args) {
|
|
150
|
+
return { hosts: engine.list(args.query).map(toAgentHostRow) }
|
|
151
|
+
},
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** The command-execution tool. */
|
|
156
|
+
export function sshExecTool(engine: SshEngine, audit?: AuditRecorder) {
|
|
157
|
+
return defineTool({
|
|
158
|
+
name: 'ssh_exec',
|
|
159
|
+
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.',
|
|
160
|
+
parameters: {
|
|
161
|
+
alias: { type: 'string', required: true, description: 'Host alias from ssh_list.' },
|
|
162
|
+
command: { type: 'string', required: true, description: 'The shell command to run remotely.' },
|
|
163
|
+
timeoutMs: { type: 'integer', description: 'Remote command execution timeout in milliseconds (default 60000); it starts after the connection is ready. The connection and authentication ceiling is the dsh-ssh connectTimeoutMs setting, not this argument.' },
|
|
164
|
+
},
|
|
165
|
+
output: {
|
|
166
|
+
schema: {
|
|
167
|
+
type: 'object',
|
|
168
|
+
additionalProperties: false,
|
|
169
|
+
properties: {
|
|
170
|
+
success: { type: 'boolean', required: true },
|
|
171
|
+
exitCode: { oneOf: [{ type: 'integer' }, { type: 'null' }], required: true },
|
|
172
|
+
timedOut: { type: 'boolean', required: true },
|
|
173
|
+
stdout: { type: 'string', required: true },
|
|
174
|
+
stderr: { type: 'string', required: true },
|
|
175
|
+
durationMs: { type: 'integer', required: true },
|
|
176
|
+
error: { type: 'string' },
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
render: (_args, value: ExecResult) => text(renderExec(value)),
|
|
180
|
+
},
|
|
181
|
+
async execute(args, exec) {
|
|
182
|
+
try {
|
|
183
|
+
// Forward the caller's cancellation: aborting closes the remote channel
|
|
184
|
+
// instead of leaving the command running on the host.
|
|
185
|
+
const result = await engine.exec(args.alias, args.command, args.timeoutMs, exec.signal)
|
|
186
|
+
audit?.record('exec', {
|
|
187
|
+
ok: result.success,
|
|
188
|
+
targets: [args.alias],
|
|
189
|
+
summary: `exec on ${args.alias}: ${redactCommand(args.command)}`,
|
|
190
|
+
detail: { exitCode: result.exitCode, durationMs: result.durationMs },
|
|
191
|
+
})
|
|
192
|
+
return redactExecResult(result)
|
|
193
|
+
} catch (error) {
|
|
194
|
+
audit?.record('exec', {
|
|
195
|
+
ok: false,
|
|
196
|
+
targets: [args.alias],
|
|
197
|
+
summary: `exec on ${args.alias} failed: ${redactCommand(args.command)}`,
|
|
198
|
+
})
|
|
199
|
+
return {
|
|
200
|
+
success: false,
|
|
201
|
+
exitCode: null,
|
|
202
|
+
timedOut: false,
|
|
203
|
+
stdout: '',
|
|
204
|
+
stderr: '',
|
|
205
|
+
durationMs: 0,
|
|
206
|
+
error: error instanceof Error ? error.message : String(error),
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
})
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Redact an exec result before it leaves the tool.
|
|
215
|
+
*
|
|
216
|
+
* The output becomes model context and, in DSH, part of the session log —
|
|
217
|
+
* so a secret printed by `env` would be written into a transcript that
|
|
218
|
+
* outlives the session. The redaction is a safety net, not a guarantee:
|
|
219
|
+
* the remote host may hold secrets in shapes this does not recognise.
|
|
220
|
+
*
|
|
221
|
+
* @param result - the raw exec result.
|
|
222
|
+
* @returns the redacted result.
|
|
223
|
+
*/
|
|
224
|
+
export function redactExecResult(result: ExecResult): ExecResult {
|
|
225
|
+
return {
|
|
226
|
+
...result,
|
|
227
|
+
stdout: redactOutput(result.stdout),
|
|
228
|
+
stderr: redactOutput(result.stderr),
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** The upload tool. */
|
|
233
|
+
export function sshUploadTool(engine: SshEngine, audit?: AuditRecorder) {
|
|
234
|
+
return defineTool({
|
|
235
|
+
name: 'ssh_upload',
|
|
236
|
+
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.',
|
|
237
|
+
parameters: {
|
|
238
|
+
alias: { type: 'string', required: true, description: 'Host alias from ssh_list.' },
|
|
239
|
+
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.' },
|
|
240
|
+
remotePath: { type: 'string', required: true, description: 'Absolute destination path on the remote SSH host (parent dirs are created).' },
|
|
241
|
+
},
|
|
242
|
+
output: {
|
|
243
|
+
schema: {
|
|
244
|
+
type: 'object',
|
|
245
|
+
additionalProperties: false,
|
|
246
|
+
properties: {
|
|
247
|
+
ok: { type: 'boolean', required: true },
|
|
248
|
+
transferredBytes: { type: 'integer' },
|
|
249
|
+
files: { type: 'integer' },
|
|
250
|
+
error: { type: 'string' },
|
|
251
|
+
},
|
|
252
|
+
},
|
|
253
|
+
render: (_args, value: { ok: boolean; transferredBytes?: number; files?: number; error?: string }) => text(value.ok
|
|
254
|
+
? `uploaded ${value.files ?? 1} file(s), ${value.transferredBytes ?? 0} bytes`
|
|
255
|
+
: `upload failed: ${value.error ?? 'unknown error'}`),
|
|
256
|
+
},
|
|
257
|
+
async execute(args) {
|
|
258
|
+
try {
|
|
259
|
+
const outcome = await engine.upload(args.alias, args.localPath, args.remotePath, false)
|
|
260
|
+
audit?.record('upload', {
|
|
261
|
+
ok: true,
|
|
262
|
+
targets: [args.alias],
|
|
263
|
+
summary: `upload to ${args.alias}: ${args.remotePath}`,
|
|
264
|
+
detail: { bytes: outcome.bytes, files: outcome.files },
|
|
265
|
+
})
|
|
266
|
+
return { ok: true, transferredBytes: outcome.bytes, files: outcome.files }
|
|
267
|
+
} catch (error) {
|
|
268
|
+
audit?.record('upload', {
|
|
269
|
+
ok: false,
|
|
270
|
+
targets: [args.alias],
|
|
271
|
+
summary: `upload to ${args.alias} failed: ${args.remotePath}`,
|
|
272
|
+
})
|
|
273
|
+
return { ok: false, error: error instanceof Error ? error.message : String(error) }
|
|
274
|
+
}
|
|
275
|
+
},
|
|
276
|
+
})
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** The download tool. */
|
|
280
|
+
export function sshDownloadTool(engine: SshEngine, audit?: AuditRecorder) {
|
|
281
|
+
return defineTool({
|
|
282
|
+
name: 'ssh_download',
|
|
283
|
+
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.',
|
|
284
|
+
parameters: {
|
|
285
|
+
alias: { type: 'string', required: true, description: 'Host alias from ssh_list.' },
|
|
286
|
+
remotePath: { type: 'string', required: true, description: 'Absolute path of the source file on the remote SSH host.' },
|
|
287
|
+
localPath: { type: 'string', required: true, description: 'Absolute destination path on THIS machine (the dsh host).' },
|
|
288
|
+
},
|
|
289
|
+
output: {
|
|
290
|
+
schema: {
|
|
291
|
+
type: 'object',
|
|
292
|
+
additionalProperties: false,
|
|
293
|
+
properties: {
|
|
294
|
+
ok: { type: 'boolean', required: true },
|
|
295
|
+
bytes: { type: 'integer' },
|
|
296
|
+
error: { type: 'string' },
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
render: (_args, value: { ok: boolean; bytes?: number; error?: string }) => text(value.ok
|
|
300
|
+
? `downloaded ${value.bytes ?? 0} bytes`
|
|
301
|
+
: `download failed: ${value.error ?? 'unknown error'}`),
|
|
302
|
+
},
|
|
303
|
+
async execute(args) {
|
|
304
|
+
try {
|
|
305
|
+
const outcome = await engine.download(args.alias, args.remotePath, args.localPath)
|
|
306
|
+
audit?.record('download', {
|
|
307
|
+
ok: true,
|
|
308
|
+
targets: [args.alias],
|
|
309
|
+
summary: `download from ${args.alias}: ${args.remotePath}`,
|
|
310
|
+
detail: { bytes: outcome.bytes },
|
|
311
|
+
})
|
|
312
|
+
return { ok: true, bytes: outcome.bytes }
|
|
313
|
+
} catch (error) {
|
|
314
|
+
audit?.record('download', {
|
|
315
|
+
ok: false,
|
|
316
|
+
targets: [args.alias],
|
|
317
|
+
summary: `download from ${args.alias} failed: ${args.remotePath}`,
|
|
318
|
+
})
|
|
319
|
+
return { ok: false, error: error instanceof Error ? error.message : String(error) }
|
|
320
|
+
}
|
|
321
|
+
},
|
|
322
|
+
})
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** The tunnel tool. */
|
|
326
|
+
export function sshTunnelTool(engine: SshEngine, audit?: AuditRecorder) {
|
|
327
|
+
return defineTool({
|
|
328
|
+
name: 'ssh_tunnel',
|
|
329
|
+
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.',
|
|
330
|
+
parameters: {
|
|
331
|
+
action: { type: 'string', required: true, enum: ['start', 'list', 'stop', 'stop-all', 'update', 'remark'], description: 'start / list / stop / stop-all / update (edit a plugin-owned tunnel) / remark (annotate a tunnel another process owns).' },
|
|
332
|
+
alias: { type: 'string', description: 'Host alias (required for start, optional for stop-all).' },
|
|
333
|
+
remotePort: { type: 'integer', description: 'Port on the remote side (required for start; optional new value for update).' },
|
|
334
|
+
remoteHost: { type: 'string', description: 'Remote host to forward to (default 127.0.0.1 — the server itself; optional new value for update).' },
|
|
335
|
+
localPort: { type: 'integer', description: 'Local listening port (default: auto-assigned; the identity of the forward for remark).' },
|
|
336
|
+
name: { type: 'string', description: 'Optional project or service name for this tunnel (e.g. redis, api). An empty string clears it for update/remark.' },
|
|
337
|
+
description: { type: 'string', description: 'Optional remark or purpose for this tunnel. An empty string clears it for update/remark.' },
|
|
338
|
+
tunnelId: { type: 'string', description: 'Tunnel id (required for stop).' },
|
|
339
|
+
},
|
|
340
|
+
output: {
|
|
341
|
+
schema: {
|
|
342
|
+
type: 'object',
|
|
343
|
+
additionalProperties: false,
|
|
344
|
+
properties: {
|
|
345
|
+
ok: { type: 'boolean', required: true },
|
|
346
|
+
tunnel: {
|
|
347
|
+
type: 'object',
|
|
348
|
+
additionalProperties: false,
|
|
349
|
+
properties: {
|
|
350
|
+
id: { type: 'string', required: true },
|
|
351
|
+
alias: { type: 'string', required: true },
|
|
352
|
+
localPort: { type: 'integer', required: true },
|
|
353
|
+
remoteHost: { type: 'string', required: true },
|
|
354
|
+
remotePort: { type: 'integer', required: true },
|
|
355
|
+
state: { type: 'string', enum: ['forwarding', 'connecting', 'failed'], required: true },
|
|
356
|
+
error: { type: 'string' },
|
|
357
|
+
startedAt: { type: 'integer', required: true },
|
|
358
|
+
name: { type: 'string' },
|
|
359
|
+
description: { type: 'string' },
|
|
360
|
+
},
|
|
361
|
+
},
|
|
362
|
+
tunnels: {
|
|
363
|
+
type: 'array',
|
|
364
|
+
items: {
|
|
365
|
+
type: 'object',
|
|
366
|
+
additionalProperties: false,
|
|
367
|
+
properties: {
|
|
368
|
+
id: { type: 'string', required: true },
|
|
369
|
+
alias: { type: 'string', required: true },
|
|
370
|
+
localPort: { type: 'integer', required: true },
|
|
371
|
+
remoteHost: { type: 'string', required: true },
|
|
372
|
+
remotePort: { type: 'integer', required: true },
|
|
373
|
+
state: { type: 'string', enum: ['forwarding', 'connecting', 'failed'], required: true },
|
|
374
|
+
error: { type: 'string' },
|
|
375
|
+
startedAt: { type: 'integer', required: true },
|
|
376
|
+
source: { type: 'string', enum: ['plugin', 'external'] },
|
|
377
|
+
pid: { type: 'integer' },
|
|
378
|
+
live: { type: 'boolean' },
|
|
379
|
+
kind: { type: 'string', enum: ['local', 'remote', 'dynamic'] },
|
|
380
|
+
name: { type: 'string' },
|
|
381
|
+
description: { type: 'string' },
|
|
382
|
+
},
|
|
383
|
+
},
|
|
384
|
+
},
|
|
385
|
+
stopped: { type: 'integer' },
|
|
386
|
+
error: { type: 'string' },
|
|
387
|
+
},
|
|
388
|
+
},
|
|
389
|
+
render: (_args, value: { ok: boolean; tunnel?: TunnelInfo; tunnels?: TunnelInfo[]; stopped?: number; error?: string }) => {
|
|
390
|
+
if (value.error !== undefined) return text(`tunnel error: ${value.error}`)
|
|
391
|
+
if (value.tunnel !== undefined) {
|
|
392
|
+
if (value.tunnel.state === 'failed') return text(`tunnel failed: ${value.tunnel.error ?? 'unknown error'}`)
|
|
393
|
+
return text(`tunnel started: ${renderTunnel(value.tunnel)}`)
|
|
394
|
+
}
|
|
395
|
+
if (value.tunnels !== undefined) return text(value.tunnels.length === 0 ? 'no active tunnels' : value.tunnels.map(renderTunnel).join('\n'))
|
|
396
|
+
return text(`stopped ${value.stopped ?? 0} tunnel(s)`)
|
|
397
|
+
},
|
|
398
|
+
},
|
|
399
|
+
async execute(args) {
|
|
400
|
+
if (args.action === 'list') {
|
|
401
|
+
const managed = engine.listTunnels()
|
|
402
|
+
// The same answer the panel gives: a forward started outside this
|
|
403
|
+
// plugin is still a forward, and reporting "no active tunnels" while
|
|
404
|
+
// one serves traffic sends the reader looking in the wrong place.
|
|
405
|
+
const external = await discoverExternalTunnels(managed, readTunnelRemarks())
|
|
406
|
+
return { ok: true, tunnels: [...managed, ...external] }
|
|
407
|
+
}
|
|
408
|
+
if (args.action === 'start') {
|
|
409
|
+
if (args.alias === undefined || args.remotePort === undefined) {
|
|
410
|
+
throw new Error('alias and remotePort are required for start')
|
|
411
|
+
}
|
|
412
|
+
try {
|
|
413
|
+
const tunnel = await engine.startTunnel(args.alias, {
|
|
414
|
+
remotePort: args.remotePort,
|
|
415
|
+
remoteHost: args.remoteHost,
|
|
416
|
+
localPort: args.localPort,
|
|
417
|
+
name: args.name,
|
|
418
|
+
description: args.description,
|
|
419
|
+
})
|
|
420
|
+
audit?.record('tunnel.start', {
|
|
421
|
+
ok: true,
|
|
422
|
+
targets: [args.alias],
|
|
423
|
+
summary: `tunnel to ${args.alias}:${args.remotePort}`,
|
|
424
|
+
detail: { localPort: tunnel.localPort },
|
|
425
|
+
})
|
|
426
|
+
return { ok: true, tunnel }
|
|
427
|
+
} catch (error) {
|
|
428
|
+
audit?.record('tunnel.start', {
|
|
429
|
+
ok: false,
|
|
430
|
+
targets: [args.alias],
|
|
431
|
+
summary: `tunnel to ${args.alias}:${args.remotePort} failed`,
|
|
432
|
+
})
|
|
433
|
+
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() } }
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
if (args.action === 'stop') {
|
|
437
|
+
if (args.tunnelId === undefined) throw new Error('tunnelId is required for stop')
|
|
438
|
+
const stopped = engine.stopTunnel(args.tunnelId)
|
|
439
|
+
return stopped
|
|
440
|
+
? { ok: true, stopped: 1 }
|
|
441
|
+
: { ok: false, stopped: 0, error: `tunnel '${args.tunnelId}' not found` }
|
|
442
|
+
}
|
|
443
|
+
if (args.action === 'update') {
|
|
444
|
+
if (args.tunnelId === undefined) throw new Error('tunnelId is required for update')
|
|
445
|
+
try {
|
|
446
|
+
const result = await engine.updateTunnel(args.tunnelId, {
|
|
447
|
+
name: args.name,
|
|
448
|
+
description: args.description,
|
|
449
|
+
remoteHost: args.remoteHost,
|
|
450
|
+
remotePort: args.remotePort,
|
|
451
|
+
localPort: args.localPort,
|
|
452
|
+
})
|
|
453
|
+
audit?.record('tunnel.update', {
|
|
454
|
+
ok: true,
|
|
455
|
+
targets: [result.tunnel.alias],
|
|
456
|
+
summary: `tunnel ${args.tunnelId} updated${result.restarted ? ' (rebuilt)' : ''}`,
|
|
457
|
+
detail: { localPort: result.tunnel.localPort, restarted: result.restarted },
|
|
458
|
+
})
|
|
459
|
+
return { ok: true, tunnel: result.tunnel }
|
|
460
|
+
} catch (error) {
|
|
461
|
+
return { ok: false, error: error instanceof Error ? error.message : String(error) }
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
if (args.action === 'remark') {
|
|
465
|
+
if (args.localPort === undefined) throw new Error('localPort is required for remark')
|
|
466
|
+
try {
|
|
467
|
+
// Two empty fields delete the entry, which is how forwards.conf
|
|
468
|
+
// becomes visible again — the tool reports success either way.
|
|
469
|
+
setTunnelRemark(args.localPort, { name: args.name, description: args.description })
|
|
470
|
+
return { ok: true }
|
|
471
|
+
} catch (error) {
|
|
472
|
+
return { ok: false, error: error instanceof Error ? error.message : String(error) }
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
if (args.action === 'stop-all') {
|
|
476
|
+
const stopped = engine.stopAllTunnels(args.alias)
|
|
477
|
+
return { ok: true, stopped }
|
|
478
|
+
}
|
|
479
|
+
throw new Error(`unknown action '${String(args.action)}'`)
|
|
480
|
+
},
|
|
481
|
+
})
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/** The cluster tool. */
|
|
485
|
+
export function sshClusterTool(engine: SshEngine, audit?: AuditRecorder) {
|
|
486
|
+
return defineTool({
|
|
487
|
+
name: 'ssh_cluster',
|
|
488
|
+
description: 'Run one command concurrently across selected SSH hosts; at least one aliases, environment, or tags filter is required.',
|
|
489
|
+
parameters: {
|
|
490
|
+
command: { type: 'string', required: true, description: 'The shell command to run on every matched host.' },
|
|
491
|
+
aliases: { type: 'array', items: { type: 'string' }, description: 'Optional alias filter; at least one of aliases, environment, or tags is required.' },
|
|
492
|
+
environment: { type: 'string', description: 'Optional environment filter; at least one selector is required.' },
|
|
493
|
+
tags: { type: 'array', items: { type: 'string' }, description: 'Optional ALL-tags filter; at least one selector is required.' },
|
|
494
|
+
timeoutMs: { type: 'integer', description: 'Per-host command execution timeout in milliseconds (default 60000); it does not extend the connection and authentication ceiling (the dsh-ssh connectTimeoutMs setting).' },
|
|
495
|
+
maxWorkers: { type: 'integer', description: 'Concurrency cap (default 8).' },
|
|
496
|
+
},
|
|
497
|
+
output: {
|
|
498
|
+
schema: {
|
|
499
|
+
type: 'object',
|
|
500
|
+
additionalProperties: false,
|
|
501
|
+
properties: {
|
|
502
|
+
results: {
|
|
503
|
+
type: 'array',
|
|
504
|
+
required: true,
|
|
505
|
+
items: {
|
|
506
|
+
type: 'object',
|
|
507
|
+
additionalProperties: false,
|
|
508
|
+
properties: {
|
|
509
|
+
alias: { type: 'string', required: true },
|
|
510
|
+
ok: { type: 'boolean', required: true },
|
|
511
|
+
exitCode: { oneOf: [{ type: 'integer' }, { type: 'null' }] },
|
|
512
|
+
timedOut: { type: 'boolean' },
|
|
513
|
+
stdout: { type: 'string' },
|
|
514
|
+
stderr: { type: 'string' },
|
|
515
|
+
durationMs: { type: 'integer' },
|
|
516
|
+
error: { type: 'string' },
|
|
517
|
+
},
|
|
518
|
+
},
|
|
519
|
+
},
|
|
520
|
+
},
|
|
521
|
+
},
|
|
522
|
+
render: (_args, value: { results?: ClusterResult[] }) => text(renderCluster(value.results ?? [])),
|
|
523
|
+
},
|
|
524
|
+
async execute(args, exec) {
|
|
525
|
+
const hasSelector = (Array.isArray(args.aliases) && args.aliases.some((alias) => typeof alias === 'string' && alias.trim() !== '')) ||
|
|
526
|
+
(typeof args.environment === 'string' && args.environment.trim() !== '') ||
|
|
527
|
+
(Array.isArray(args.tags) && args.tags.some((tag) => typeof tag === 'string' && tag.trim() !== ''))
|
|
528
|
+
if (!hasSelector) throw new Error('ssh_cluster requires aliases, environment, or tags to limit the target set')
|
|
529
|
+
// Cancellation reaches every host's in-flight command.
|
|
530
|
+
const results = await engine.cluster({ ...args, signal: exec.signal })
|
|
531
|
+
const failed = results.filter(result => !result.ok).map(result => result.alias)
|
|
532
|
+
audit?.record('cluster', {
|
|
533
|
+
ok: failed.length === 0,
|
|
534
|
+
targets: results.map(result => result.alias),
|
|
535
|
+
summary: `cluster exec on ${results.length} host(s): ${redactCommand(args.command)}`,
|
|
536
|
+
detail: { failedHosts: failed },
|
|
537
|
+
})
|
|
538
|
+
// Redact every host's output: the cluster result is one blob of
|
|
539
|
+
// output from N hosts, so a single secret in one of them would land
|
|
540
|
+
// in the session log.
|
|
541
|
+
return {
|
|
542
|
+
results: results.map(result => ({
|
|
543
|
+
...result,
|
|
544
|
+
...(result.stdout !== undefined ? { stdout: redactOutput(result.stdout) } : {}),
|
|
545
|
+
...(result.stderr !== undefined ? { stderr: redactOutput(result.stderr) } : {}),
|
|
546
|
+
})),
|
|
547
|
+
}
|
|
548
|
+
},
|
|
549
|
+
})
|
|
550
|
+
}
|