dsh-remote-deck 0.0.0-stage → 1.46.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 (118) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +227 -2
  3. package/README.zh-CN.md +201 -0
  4. package/client.js +8645 -0
  5. package/cordis.patch.yml +6 -0
  6. package/icon.svg +19 -0
  7. package/index.js +701 -0
  8. package/lib/api.js +1452 -0
  9. package/lib/bash-result.js +93 -0
  10. package/lib/connection.js +1133 -0
  11. package/lib/delete.js +199 -0
  12. package/lib/drafts.js +299 -0
  13. package/lib/encoding.js +247 -0
  14. package/lib/file-edit.js +339 -0
  15. package/lib/file-ops.js +402 -0
  16. package/lib/forwards.js +186 -0
  17. package/lib/fs-actions.js +235 -0
  18. package/lib/guide.js +162 -0
  19. package/lib/metrics.js +523 -0
  20. package/lib/remote-shells.js +320 -0
  21. package/lib/remote-terminal.js +427 -0
  22. package/lib/routing.js +695 -0
  23. package/lib/ssh-config.js +118 -0
  24. package/lib/terminal.js +123 -0
  25. package/lib/tool-forward.js +185 -0
  26. package/lib/transfer.js +235 -0
  27. package/lib/upload-chunks.js +306 -0
  28. package/lib/util.js +154 -0
  29. package/locale/README.md +160 -0
  30. package/locale/en-ui.json +477 -0
  31. package/locale/en.json +6 -0
  32. package/locale/zh.json +6 -0
  33. package/package.json +63 -4
  34. package/vendor/codemirror/LICENSE +21 -0
  35. package/vendor/codemirror/README.md +18 -0
  36. package/vendor/codemirror/addon/edit/closebrackets.min.js +8 -0
  37. package/vendor/codemirror/addon/edit/matchbrackets.min.js +8 -0
  38. package/vendor/codemirror/addon/search/search.min.js +8 -0
  39. package/vendor/codemirror/addon/search/searchcursor.min.js +8 -0
  40. package/vendor/codemirror/addon/selection/active-line.min.js +8 -0
  41. package/vendor/codemirror/lib/codemirror.css +344 -0
  42. package/vendor/codemirror/lib/codemirror.min.js +8 -0
  43. package/vendor/codemirror/mode/clike/clike.min.js +8 -0
  44. package/vendor/codemirror/mode/css/css.min.js +8 -0
  45. package/vendor/codemirror/mode/go/go.min.js +8 -0
  46. package/vendor/codemirror/mode/htmlmixed/htmlmixed.min.js +8 -0
  47. package/vendor/codemirror/mode/javascript/javascript.min.js +8 -0
  48. package/vendor/codemirror/mode/markdown/markdown.min.js +8 -0
  49. package/vendor/codemirror/mode/python/python.min.js +8 -0
  50. package/vendor/codemirror/mode/rust/rust.min.js +8 -0
  51. package/vendor/codemirror/mode/shell/shell.min.js +8 -0
  52. package/vendor/codemirror/mode/xml/xml.min.js +8 -0
  53. package/vendor/codemirror/mode/yaml/yaml.min.js +8 -0
  54. package/vendor/node_modules/asn1/Jenkinsfile +65 -0
  55. package/vendor/node_modules/asn1/LICENSE +19 -0
  56. package/vendor/node_modules/asn1/README.md +50 -0
  57. package/vendor/node_modules/asn1/lib/ber/errors.js +13 -0
  58. package/vendor/node_modules/asn1/lib/ber/index.js +27 -0
  59. package/vendor/node_modules/asn1/lib/ber/reader.js +262 -0
  60. package/vendor/node_modules/asn1/lib/ber/types.js +36 -0
  61. package/vendor/node_modules/asn1/lib/ber/writer.js +317 -0
  62. package/vendor/node_modules/asn1/lib/index.js +20 -0
  63. package/vendor/node_modules/asn1/package.json +31 -0
  64. package/vendor/node_modules/bcrypt-pbkdf/CONTRIBUTING.md +13 -0
  65. package/vendor/node_modules/bcrypt-pbkdf/LICENSE +66 -0
  66. package/vendor/node_modules/bcrypt-pbkdf/README.md +45 -0
  67. package/vendor/node_modules/bcrypt-pbkdf/index.js +556 -0
  68. package/vendor/node_modules/bcrypt-pbkdf/package.json +15 -0
  69. package/vendor/node_modules/safer-buffer/LICENSE +21 -0
  70. package/vendor/node_modules/safer-buffer/Porting-Buffer.md +268 -0
  71. package/vendor/node_modules/safer-buffer/Readme.md +156 -0
  72. package/vendor/node_modules/safer-buffer/dangerous.js +58 -0
  73. package/vendor/node_modules/safer-buffer/package.json +34 -0
  74. package/vendor/node_modules/safer-buffer/safer.js +77 -0
  75. package/vendor/node_modules/safer-buffer/tests.js +406 -0
  76. package/vendor/node_modules/ssh2/LICENSE +19 -0
  77. package/vendor/node_modules/ssh2/README.md +1529 -0
  78. package/vendor/node_modules/ssh2/SFTP.md +413 -0
  79. package/vendor/node_modules/ssh2/lib/Channel.js +295 -0
  80. package/vendor/node_modules/ssh2/lib/agent.js +1123 -0
  81. package/vendor/node_modules/ssh2/lib/client.js +2176 -0
  82. package/vendor/node_modules/ssh2/lib/http-agents.js +84 -0
  83. package/vendor/node_modules/ssh2/lib/index.js +44 -0
  84. package/vendor/node_modules/ssh2/lib/keygen.js +582 -0
  85. package/vendor/node_modules/ssh2/lib/protocol/Protocol.js +2136 -0
  86. package/vendor/node_modules/ssh2/lib/protocol/SFTP.js +4052 -0
  87. package/vendor/node_modules/ssh2/lib/protocol/constants.js +356 -0
  88. package/vendor/node_modules/ssh2/lib/protocol/crypto/binding.gyp +23 -0
  89. package/vendor/node_modules/ssh2/lib/protocol/crypto/poly1305.js +43 -0
  90. package/vendor/node_modules/ssh2/lib/protocol/crypto/src/binding.cc +2300 -0
  91. package/vendor/node_modules/ssh2/lib/protocol/crypto.js +1602 -0
  92. package/vendor/node_modules/ssh2/lib/protocol/handlers.js +16 -0
  93. package/vendor/node_modules/ssh2/lib/protocol/handlers.misc.js +1285 -0
  94. package/vendor/node_modules/ssh2/lib/protocol/kex.js +1908 -0
  95. package/vendor/node_modules/ssh2/lib/protocol/keyParser.js +1484 -0
  96. package/vendor/node_modules/ssh2/lib/protocol/node-fs-compat.js +115 -0
  97. package/vendor/node_modules/ssh2/lib/protocol/utils.js +356 -0
  98. package/vendor/node_modules/ssh2/lib/protocol/zlib.js +255 -0
  99. package/vendor/node_modules/ssh2/lib/server.js +1380 -0
  100. package/vendor/node_modules/ssh2/lib/utils.js +336 -0
  101. package/vendor/node_modules/ssh2/package.json +49 -0
  102. package/vendor/node_modules/tweetnacl/AUTHORS.md +28 -0
  103. package/vendor/node_modules/tweetnacl/CHANGELOG.md +221 -0
  104. package/vendor/node_modules/tweetnacl/LICENSE +24 -0
  105. package/vendor/node_modules/tweetnacl/PULL_REQUEST_TEMPLATE.md +20 -0
  106. package/vendor/node_modules/tweetnacl/README.md +459 -0
  107. package/vendor/node_modules/tweetnacl/nacl-fast.js +2388 -0
  108. package/vendor/node_modules/tweetnacl/nacl-fast.min.js +2 -0
  109. package/vendor/node_modules/tweetnacl/nacl.d.ts +98 -0
  110. package/vendor/node_modules/tweetnacl/nacl.js +1175 -0
  111. package/vendor/node_modules/tweetnacl/nacl.min.js +1 -0
  112. package/vendor/node_modules/tweetnacl/package.json +58 -0
  113. package/vendor/xterm/LICENSE.addon-fit +19 -0
  114. package/vendor/xterm/LICENSE.xterm +21 -0
  115. package/vendor/xterm/README.md +9 -0
  116. package/vendor/xterm/addon-fit.js +2 -0
  117. package/vendor/xterm/xterm.css +218 -0
  118. package/vendor/xterm/xterm.js +2 -0
@@ -0,0 +1,235 @@
1
+ /**
2
+ * The file page's three single-name actions: rename, new folder, new file.
3
+ *
4
+ * Rename takes an existing file or directory; new folder and new file take an
5
+ * existing directory. All three share the same path and name rules:
6
+ *
7
+ * - **The path is the file page's path.** `isPlainRemotePath` /
8
+ * `canonicalRemotePath` from `lib/file-ops.js` decide what an acceptable
9
+ * remote path is, exactly as copy and move do; nothing is invented here.
10
+ * - **The name is one segment.** No `/`, no NUL, not `.`/`..`, not empty, and
11
+ * no longer than {@link FS_ACTION_MAX_NAME_BYTES} bytes — the usual POSIX
12
+ * `NAME_MAX`. It is trimmed first (outer whitespace is an accident, not a
13
+ * name) and otherwise never rewritten: a bad name is refused so the client
14
+ * can show it back to the reader.
15
+ * - **Nothing is overwritten.** `rename` on OpenSSH replaces the destination and
16
+ * `writeFile` always truncates, so existence is checked before the write and
17
+ * again if the server refuses the operation. The answer is `EXISTS` (409),
18
+ * never a silent replacement.
19
+ * - **A missing source is `NOT_FOUND` (404)**, decided before any write so the
20
+ * reply does not depend on how one SFTP server words its status codes.
21
+ *
22
+ * The route in `api.js` owns the HTTP meaning of these outcomes; this module
23
+ * only validates and talks SFTP.
24
+ */
25
+ import { posix } from 'node:path'
26
+ import { canonicalRemotePath, isPlainRemotePath } from './file-ops.js'
27
+ import { sftpCall } from './util.js'
28
+
29
+ /** The path is not a plain absolute remote POSIX path. */
30
+ export const FS_ACTION_INVALID_PATH_CODE = 'NOT_REMOTE_PATH'
31
+ /** The name is not a plain single path segment. */
32
+ export const FS_ACTION_BAD_NAME_CODE = 'BAD_NAME'
33
+ /** The source path does not exist on the remote host. */
34
+ export const FS_ACTION_MISSING_CODE = 'NOT_FOUND'
35
+ /** An entry with that name is already there. */
36
+ export const FS_ACTION_EXISTS_CODE = 'EXISTS'
37
+ /** Longest accepted name, in UTF-8 bytes (`NAME_MAX`). */
38
+ export const FS_ACTION_MAX_NAME_BYTES = 255
39
+
40
+ /** SSH_FX_NO_SUCH_FILE, as ssh2 reports it. */
41
+ const SFTP_NO_SUCH_FILE = 2
42
+ /** `fsError('FS_NOT_FOUND')` from `sftpCall`, and the raw SFTP status code. */
43
+ const FS_NOT_FOUND = 'FS_NOT_FOUND'
44
+
45
+ function refuse(code, message) {
46
+ return { ok: false, code, message }
47
+ }
48
+
49
+ function isMissing(error) {
50
+ return Boolean(error) && (error.code === FS_NOT_FOUND || error.code === SFTP_NO_SUCH_FILE)
51
+ }
52
+
53
+ /**
54
+ * Validate one remote path. `/` itself is accepted: it is a directory the file
55
+ * page can show, so "new folder in /" must work. `renameRemotePath` is the one
56
+ * caller that refuses it later.
57
+ *
58
+ * @param {unknown} value `path` from the request body
59
+ * @returns {{ ok: true, path: string } | { ok: false, code: string, message: string }}
60
+ */
61
+ function parsePath(value) {
62
+ if (!isPlainRemotePath(value)) return refuse(FS_ACTION_INVALID_PATH_CODE, 'A remote POSIX path is required.')
63
+ return { ok: true, path: canonicalRemotePath(value) }
64
+ }
65
+
66
+ /**
67
+ * Validate one new name.
68
+ *
69
+ * The value is trimmed first — trailing whitespace is an accident, not a name —
70
+ * and the trimmed spelling is what the caller gets back. Anything that is not
71
+ * one plain segment is refused rather than repaired.
72
+ *
73
+ * @param {unknown} value `name` from the request body
74
+ * @returns {{ ok: true, name: string } | { ok: false, code: string, message: string }}
75
+ */
76
+ function parseName(value) {
77
+ if (typeof value !== 'string') return refuse(FS_ACTION_BAD_NAME_CODE, 'A name is required.')
78
+ const name = value.trim()
79
+ if (name.length === 0) return refuse(FS_ACTION_BAD_NAME_CODE, 'A name cannot be empty.')
80
+ if (name === '.' || name === '..') return refuse(FS_ACTION_BAD_NAME_CODE, 'A name cannot be "." or "..".')
81
+ if (name.includes('/')) return refuse(FS_ACTION_BAD_NAME_CODE, 'A name cannot contain "/".')
82
+ if (name.includes('\u0000')) return refuse(FS_ACTION_BAD_NAME_CODE, 'A name cannot contain a NUL byte.')
83
+ if (Buffer.byteLength(name, 'utf8') > FS_ACTION_MAX_NAME_BYTES) {
84
+ return refuse(FS_ACTION_BAD_NAME_CODE, `A name cannot be longer than ${FS_ACTION_MAX_NAME_BYTES} bytes.`)
85
+ }
86
+ return { ok: true, name }
87
+ }
88
+
89
+ /**
90
+ * Validate the shape of a rename/mkdir/touch request — no filesystem access.
91
+ *
92
+ * @param {unknown} body parsed JSON body
93
+ * @returns {{ ok: true, path: string, name: string } | { ok: false, code: string, message: string }}
94
+ */
95
+ export function parseNameRequest(body) {
96
+ const source = body !== null && typeof body === 'object' ? body : {}
97
+ const target = parsePath(source.path)
98
+ if (target.ok !== true) return target
99
+ const name = parseName(source.name)
100
+ if (name.ok !== true) return name
101
+ return { ok: true, path: target.path, name: name.name }
102
+ }
103
+
104
+ /** Whether anything at all sits at `path`, links and dangling links included. */
105
+ async function pathExists(invoke, sftp, path) {
106
+ try {
107
+ await invoke(sftp, 'lstat', path)
108
+ return true
109
+ } catch (error) {
110
+ if (isMissing(error)) return false
111
+ throw error
112
+ }
113
+ }
114
+
115
+ /** Ask for a `lstat`, folding "no such file" into `null`. */
116
+ async function lstatOrNull(invoke, sftp, path) {
117
+ try {
118
+ return await invoke(sftp, 'lstat', path)
119
+ } catch (error) {
120
+ if (isMissing(error)) return null
121
+ throw error
122
+ }
123
+ }
124
+
125
+ function joinName(directory, name) {
126
+ return posix.join(directory, name)
127
+ }
128
+
129
+ /**
130
+ * Rename one entry inside its own directory.
131
+ *
132
+ * `name` is a single segment, so a rename can never move something to another
133
+ * directory. The source is checked first: a rename that the server reports as
134
+ * missing would otherwise be indistinguishable from any other failure on a
135
+ * server that answers `SSH_FX_FAILURE` for everything.
136
+ *
137
+ * The destination is checked before the rename as well as after a failed one —
138
+ * OpenSSH's rename replaces the destination silently, and the rule is that an
139
+ * existing name is reported, never replaced.
140
+ *
141
+ * @param {object} input
142
+ * @param {object} input.sftp connected SFTP session
143
+ * @param {string} input.path existing absolute remote path (file or directory)
144
+ * @param {string} input.name new single-segment name
145
+ * @param {Function} [input.call] `sftpCall`, injectable for tests
146
+ * @returns {Promise<{ ok: true, path: string } | { ok: false, code: string, message: string }>}
147
+ */
148
+ export async function renameRemotePath({ sftp, path, name, call } = {}) {
149
+ const parsed = parseNameRequest({ path, name })
150
+ if (parsed.ok !== true) return parsed
151
+ // "/" has no parent to rename inside of; the name contract cannot express it.
152
+ if (posix.basename(parsed.path) === '') {
153
+ return refuse(FS_ACTION_INVALID_PATH_CODE, 'The filesystem root cannot be renamed.')
154
+ }
155
+ const invoke = typeof call === 'function' ? call : sftpCall
156
+ const source = parsed.path
157
+ const destination = joinName(posix.dirname(source), parsed.name)
158
+
159
+ const sourceAttrs = await lstatOrNull(invoke, sftp, source)
160
+ if (sourceAttrs === null) return refuse(FS_ACTION_MISSING_CODE, `No such remote path: ${source}`)
161
+ if (await pathExists(invoke, sftp, destination)) {
162
+ return refuse(FS_ACTION_EXISTS_CODE, `An entry named "${parsed.name}" is already there.`)
163
+ }
164
+
165
+ try {
166
+ await invoke(sftp, 'rename', source, destination)
167
+ } catch (error) {
168
+ // The rename may have failed for any reason; a destination that appeared
169
+ // meanwhile is the one outcome the client must not read as "server broken".
170
+ if (await pathExists(invoke, sftp, destination)) {
171
+ return refuse(FS_ACTION_EXISTS_CODE, `An entry named "${parsed.name}" is already there.`)
172
+ }
173
+ if (isMissing(error)) return refuse(FS_ACTION_MISSING_CODE, `No such remote path: ${source}`)
174
+ throw error
175
+ }
176
+ return { ok: true, path: destination }
177
+ }
178
+
179
+ /**
180
+ * Create one directory named `name` inside `path`.
181
+ *
182
+ * @param {object} input `{ sftp, path, name, call }`, as {@link renameRemotePath}
183
+ * @returns {Promise<{ ok: true, path: string } | { ok: false, code: string, message: string }>}
184
+ */
185
+ export async function mkdirRemotePath({ sftp, path, name, call } = {}) {
186
+ const parsed = parseNameRequest({ path, name })
187
+ if (parsed.ok !== true) return parsed
188
+ const invoke = typeof call === 'function' ? call : sftpCall
189
+ const destination = joinName(parsed.path, parsed.name)
190
+
191
+ if (await pathExists(invoke, sftp, destination)) {
192
+ return refuse(FS_ACTION_EXISTS_CODE, `An entry named "${parsed.name}" is already there.`)
193
+ }
194
+ try {
195
+ await invoke(sftp, 'mkdir', destination)
196
+ } catch (error) {
197
+ // A server that cannot tell "already there" from a read-only folder gets
198
+ // the existence check; the code is only EXISTS when something really is.
199
+ if (await pathExists(invoke, sftp, destination)) {
200
+ return refuse(FS_ACTION_EXISTS_CODE, `An entry named "${parsed.name}" is already there.`)
201
+ }
202
+ // A missing parent is the one expected refusal; it is a 404, not a fault.
203
+ if (isMissing(error)) return refuse(FS_ACTION_MISSING_CODE, `No such remote folder: ${parsed.path}`)
204
+ throw error
205
+ }
206
+ return { ok: true, path: destination }
207
+ }
208
+
209
+ /**
210
+ * Create one empty file named `name` inside `path`.
211
+ *
212
+ * `writeFile` truncates, so the destination is checked before the write: an
213
+ * existing file must answer `EXISTS`, never "silently emptied". The write is a
214
+ * zero-length buffer — no content, no options, so every SFTP server accepts it.
215
+ *
216
+ * @param {object} input `{ sftp, path, name, call }`, as {@link renameRemotePath}
217
+ * @returns {Promise<{ ok: true, path: string } | { ok: false, code: string, message: string }>}
218
+ */
219
+ export async function touchRemotePath({ sftp, path, name, call } = {}) {
220
+ const parsed = parseNameRequest({ path, name })
221
+ if (parsed.ok !== true) return parsed
222
+ const invoke = typeof call === 'function' ? call : sftpCall
223
+ const destination = joinName(parsed.path, parsed.name)
224
+
225
+ if (await pathExists(invoke, sftp, destination)) {
226
+ return refuse(FS_ACTION_EXISTS_CODE, `An entry named "${parsed.name}" is already there.`)
227
+ }
228
+ try {
229
+ await invoke(sftp, 'writeFile', destination, Buffer.alloc(0))
230
+ } catch (error) {
231
+ if (isMissing(error)) return refuse(FS_ACTION_MISSING_CODE, `No such remote folder: ${parsed.path}`)
232
+ throw error
233
+ }
234
+ return { ok: true, path: destination }
235
+ }
package/lib/guide.js ADDED
@@ -0,0 +1,162 @@
1
+ /**
2
+ * Remote-workspace guidance delivered through the system prompt.
3
+ *
4
+ * Registering a remote workspace used to drop an `AGENTS.md` into the user's
5
+ * project directory. That wrote a file into a tree this plugin does not own, so
6
+ * the same facts now travel through DSH's prompt injection instead: a section
7
+ * frames the Session as remote, and one runtime-context line carries the host
8
+ * and remote path.
9
+ *
10
+ * Verified against `@deepseek-ai/dsh-system-prompt` in the installed app.asar
11
+ * (1.8.1 build) and `@deepseek-ai/dsh-agent`:
12
+ *
13
+ * - `systemPrompt.section({ name, order, text, interpolate })` and
14
+ * `systemPrompt.context({ name, order, text, required, interpolate })` both
15
+ * throw a TypeError unless `order` is a finite number. The named constants are
16
+ * first-party placements: `getSectionOrder(name)` is a plain table lookup, so
17
+ * an unknown name returns `undefined` and `section()` then throws. External
18
+ * contributions are documented as free to use any finite order, so this plugin
19
+ * uses literals (see the two `*_ORDER` values below).
20
+ * - A section text callback receives the assembly context. `assembleContextFor`
21
+ * in dsh-agent sets `{ agent, scope: agent }`, so `context.agent` is the Agent
22
+ * whose prompt is being assembled, for both sections and contexts.
23
+ * - `workingDirectory.get(session)` reads the committed directory with no
24
+ * filesystem I/O, which keeps a prompt callback synchronous.
25
+ * - Empty text is dropped by the pipeline: `renderPrompt` filters sections with
26
+ * `text.length === 0`, and `renderContextSections` filters empty contexts (so
27
+ * `joinContextSections` returns `''` and no user-role snapshot is emitted).
28
+ * A local Session therefore contributes nothing at all.
29
+ */
30
+ import { posix } from 'node:path'
31
+
32
+ /** Section name; unique within the prompt, and stable across reloads. */
33
+ export const WORKSPACE_GUIDE_SECTION = 'remote-deck:workspace-guide'
34
+ /** Inside the environment block, after `HARNESS_SOURCE` (10000) and before `WEB_SURFACE` (10100). */
35
+ export const WORKSPACE_GUIDE_SECTION_ORDER = 10050
36
+ /** Runtime-context name; no other contribution uses it. */
37
+ export const WORKSPACE_GUIDE_CONTEXT = 'remote-deck:workspace'
38
+ /** Right after `WORKING_DIRECTORY` (100) and before `SANDBOX_POLICY` (110). */
39
+ export const WORKSPACE_GUIDE_CONTEXT_ORDER = 105
40
+
41
+ /** The configured connection name, or a neutral stand-in. */
42
+ function hostLabel(target) {
43
+ return typeof target.host === 'string' && target.host.length > 0 ? target.host : 'the remote host'
44
+ }
45
+
46
+ /**
47
+ * The prompt section: everything the model needs to treat this directory as a
48
+ * remote POSIX workspace. Built from the resolved target only, so the text is
49
+ * cache-stable for a given host and path.
50
+ *
51
+ * @param {{ host?: string, path: string }} target resolved remote target
52
+ * @returns {string} section text
53
+ */
54
+ export function workspaceGuideText(target) {
55
+ const host = hostLabel(target)
56
+ const path = typeof target.path === 'string' ? target.path : '/'
57
+ return [
58
+ '## Remote workspace',
59
+ '',
60
+ `This Session runs on the SSH host "${host}" through Remote Deck, and its working directory is \`${path}\` on that host.`,
61
+ '',
62
+ '- Run commands with the `bash` tool. File reads, writes, edits and directory listings already address the remote filesystem.',
63
+ '- Every path is a POSIX path on the remote host, never a path on the machine that runs DeepSeek Harness.',
64
+ '- `grep` and `glob` run ripgrep on the remote host; give them POSIX paths.',
65
+ '- Write any file the user is meant to preview into the working directory above.',
66
+ ].join('\n')
67
+ }
68
+
69
+ /** The runtime-context line: the short host/path pair, refreshed per request. */
70
+ export function workspaceContextText(target) {
71
+ const host = hostLabel(target)
72
+ const path = typeof target.path === 'string' ? target.path : '/'
73
+ return `Remote Deck workspace: SSH host "${host}", remote path \`${path}\` (POSIX).`
74
+ }
75
+
76
+ /** Whether a value can be a Session-bearing Agent. */
77
+ function isAgent(candidate) {
78
+ return candidate !== null && typeof candidate === 'object' && candidate.session !== undefined && candidate.session !== null
79
+ }
80
+
81
+ /**
82
+ * Build the two prompt callbacks.
83
+ *
84
+ * The callbacks are deliberately lazy: they run once per model step, so a
85
+ * Session that moves between local and remote workspaces (or reconnects under a
86
+ * renamed host) follows without re-registering anything.
87
+ *
88
+ * @param {object} deps
89
+ * @param {(agent: object) => string | undefined} deps.directoryOf committed Session directory
90
+ * @param {(cwd: string) => ({ connectionId: string, path: string } | null)} deps.resolveTarget remote target for a path
91
+ * @param {(connectionId: string) => string | undefined} deps.hostNameFor configured connection name
92
+ * @returns {{ section: (context: object) => string, context: (context: object) => string }}
93
+ */
94
+ export function createWorkspacePromptGuide({ directoryOf, resolveTarget, hostNameFor }) {
95
+ const targetOf = (context) => {
96
+ if (context === null || typeof context !== 'object') return null
97
+ // `assembleContextFor` supplies both; `scope` is the same Agent object.
98
+ const agent = isAgent(context.agent) ? context.agent : isAgent(context.scope) ? context.scope : null
99
+ if (agent === null) return null
100
+ let cwd
101
+ try {
102
+ cwd = directoryOf(agent)
103
+ } catch {
104
+ return null
105
+ }
106
+ if (typeof cwd !== 'string' || cwd.length === 0) return null
107
+ let resolved
108
+ try {
109
+ resolved = resolveTarget(cwd)
110
+ } catch {
111
+ return null
112
+ }
113
+ if (resolved === null || typeof resolved !== 'object' || typeof resolved.path !== 'string') return null
114
+ let host = ''
115
+ try {
116
+ const name = hostNameFor(resolved.connectionId)
117
+ if (typeof name === 'string') host = name
118
+ } catch {
119
+ host = ''
120
+ }
121
+ return { connectionId: resolved.connectionId, path: posix.normalize(resolved.path), host }
122
+ }
123
+
124
+ return {
125
+ section: (context) => {
126
+ const target = targetOf(context)
127
+ return target === null ? '' : workspaceGuideText(target)
128
+ },
129
+ context: (context) => {
130
+ const target = targetOf(context)
131
+ return target === null ? '' : workspaceContextText(target)
132
+ },
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Register both contributions on one scope.
138
+ *
139
+ * `interpolate: false` keeps the generated text literal: it embeds a host name
140
+ * and a remote path, and `{{name}}` inside a path must not be read as a prompt
141
+ * variable (an unknown reference would make assembly throw).
142
+ *
143
+ * @param {{ systemPrompt: object }} scope a Context that exposes `systemPrompt`
144
+ * @param {object} deps see {@link createWorkspacePromptGuide}
145
+ * @returns {{ section: (context: object) => string, context: (context: object) => string }}
146
+ */
147
+ export function installWorkspacePromptGuide(scope, deps) {
148
+ const guide = createWorkspacePromptGuide(deps)
149
+ scope.systemPrompt.section({
150
+ name: WORKSPACE_GUIDE_SECTION,
151
+ order: WORKSPACE_GUIDE_SECTION_ORDER,
152
+ interpolate: false,
153
+ text: guide.section,
154
+ })
155
+ scope.systemPrompt.context({
156
+ name: WORKSPACE_GUIDE_CONTEXT,
157
+ order: WORKSPACE_GUIDE_CONTEXT_ORDER,
158
+ interpolate: false,
159
+ text: guide.context,
160
+ })
161
+ return guide
162
+ }