dsh-linked-workspace 0.24.1 → 0.26.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/lib/index.js CHANGED
@@ -39,7 +39,7 @@ export const inject = ['webServer', 'tools', 'systemPrompt', 'workingDirectory']
39
39
  * is still running an older copy of the Host — refreshing the page only swaps
40
40
  * the browser bundle, so the two halves can drift by one restart.
41
41
  */
42
- const API_REVISION = 9
42
+ const API_REVISION = 11
43
43
 
44
44
  /** When this copy of the Host was loaded; shown by `meta.info`. */
45
45
  const STARTED_AT = new Date().toISOString()
@@ -65,7 +65,7 @@ const HOST_KEY_POLICIES = new Set(['tofu', 'strict'])
65
65
  const OUTPUT_ENCODINGS = new Set(['utf8', 'gbk'])
66
66
  /** Exec allow-lists travel as arrays, so they are not part of the frozen defaults. */
67
67
  const EMPTY_ID_LIST = Object.freeze([])
68
- const EDITOR_THEME_FAMILIES = new Set(['github', 'solarized', 'one', 'dracula', 'monokai', 'nord', 'vscode', 'xcode', 'catppuccin'])
68
+ const EDITOR_THEME_FAMILIES = new Set(['github', 'solarized', 'one', 'dracula', 'monokai', 'nord', 'vscode', 'xcode', 'catppuccin', 'tokyo', 'gruvbox', 'ayu'])
69
69
  const THEME_FAMILY_OF = (id) => {
70
70
  const value = String(id || '')
71
71
  if (value.startsWith('solarized')) return 'solarized'
@@ -76,10 +76,13 @@ const THEME_FAMILY_OF = (id) => {
76
76
  if (value.startsWith('vs-')) return 'vscode'
77
77
  if (value.startsWith('xcode')) return 'xcode'
78
78
  if (value.startsWith('catppuccin')) return 'catppuccin'
79
+ if (value.startsWith('tokyo')) return 'tokyo'
80
+ if (value.startsWith('gruvbox')) return 'gruvbox'
81
+ if (value.startsWith('ayu')) return 'ayu'
79
82
  return 'github'
80
83
  }
81
84
  const EDITOR_THEME_MODES = new Set(['system', 'light', 'dark'])
82
- const LIGHT_EDITOR_THEMES = new Set(['github-light', 'solarized-light', 'xcode-light'])
85
+ const LIGHT_EDITOR_THEMES = new Set(['github-light', 'solarized-light', 'xcode-light', 'tokyo-night-light', 'gruvbox-light', 'ayu-light'])
83
86
  const LEGACY_THEME_MIGRATION = 'github-dark'
84
87
  const FILE_OPEN_MODES = new Set(['editor', 'system', 'copy'])
85
88
  const WORKSPACE_TITLE_PREFIX = '⇄ '
@@ -1085,6 +1088,9 @@ export function apply(ctx) {
1085
1088
  const settingsStore = new SettingsStore(join(dir, 'settings.json'))
1086
1089
  /** localhost TCP tunnels opened by hosts.forward (id -> { server, ... }) */
1087
1090
  const forwardServers = new Map()
1091
+ /** Tunnels that are still listed but have no listener right now (port taken, ...). */
1092
+ const forwardPending = new Map()
1093
+ const forwardFile = join(dir, 'forwards.json')
1088
1094
  const forwardView = (record) => ({
1089
1095
  id: record.id,
1090
1096
  hostId: record.hostId,
@@ -1095,6 +1101,84 @@ export function apply(ctx) {
1095
1101
  startedAt: record.startedAt,
1096
1102
  url: `http://127.0.0.1:${record.localPort}/`,
1097
1103
  })
1104
+ // 0.25.0: a tunnel is a standing arrangement, not a side effect of one page
1105
+ // session. Definitions live in forwards.json and the listeners are re-bound
1106
+ // on start, so a TensorBoard URL survives a Host restart; only
1107
+ // hosts.unforward (the Stop button) takes an entry away.
1108
+ const readForwardDefs = () => {
1109
+ const data = readJsonFile(forwardFile, null)
1110
+ const list = data && Array.isArray(data.forwards) ? data.forwards : []
1111
+ return list
1112
+ .filter((item) => item && typeof item.id === 'string' && typeof item.hostId === 'string')
1113
+ .map((item) => ({
1114
+ id: item.id,
1115
+ hostId: item.hostId,
1116
+ hostName: typeof item.hostName === 'string' && item.hostName !== '' ? item.hostName : item.hostId,
1117
+ remoteHost: typeof item.remoteHost === 'string' && item.remoteHost !== '' ? item.remoteHost : '127.0.0.1',
1118
+ remotePort: clampInt(item.remotePort, 1, 65535, 0),
1119
+ localPort: clampInt(item.localPort, 0, 65535, 0),
1120
+ startedAt: clampInt(item.startedAt, 0, Number.MAX_SAFE_INTEGER, Date.now()),
1121
+ }))
1122
+ .filter((item) => item.remotePort > 0)
1123
+ }
1124
+ const forwardDefOf = (record) => ({
1125
+ id: record.id,
1126
+ hostId: record.hostId,
1127
+ hostName: record.hostName,
1128
+ remoteHost: record.remoteHost,
1129
+ remotePort: record.remotePort,
1130
+ localPort: record.localPort,
1131
+ startedAt: record.startedAt,
1132
+ })
1133
+ const saveForwardDefs = () => {
1134
+ const forwards = [
1135
+ ...[...forwardServers.values()].map(forwardDefOf),
1136
+ ...[...forwardPending.values()].map((item) => item.def),
1137
+ ]
1138
+ try {
1139
+ writeJsonAtomic(forwardFile, { version: 1, forwards })
1140
+ } catch (error) {
1141
+ log('warn', `could not save the tunnel list: ${error && error.message ? error.message : error}`)
1142
+ }
1143
+ }
1144
+ /**
1145
+ * Bind one tunnel's local listener. `eager` dials the SSH connection first so
1146
+ * the caller sees credential/host-key problems immediately; restored tunnels
1147
+ * dial on the first connection instead, so a machine that is unreachable at
1148
+ * start-up still gets its listener back.
1149
+ */
1150
+ const openForward = async (def, eager) => {
1151
+ const record = { ...def, server: null, remote: null }
1152
+ const remoteFor = async () => {
1153
+ if (record.remote) return record.remote
1154
+ const host = await findHost(record.hostId)
1155
+ record.remote = await pool.remote(host)
1156
+ return record.remote
1157
+ }
1158
+ if (eager) await remoteFor()
1159
+ const server = net.createServer((socket) => {
1160
+ remoteFor()
1161
+ .then((remote) => remote.forwardOut(record.remoteHost, record.remotePort))
1162
+ .then((channel) => {
1163
+ socket.pipe(channel).pipe(socket)
1164
+ channel.on('error', () => socket.destroy())
1165
+ socket.on('error', () => channel.destroy())
1166
+ })
1167
+ .catch((error) => {
1168
+ log('warn', `forward ${record.id} failed: ${error && error.message ? error.message : error}`)
1169
+ socket.destroy()
1170
+ })
1171
+ })
1172
+ await new Promise((resolvePromise, rejectPromise) => {
1173
+ server.once('error', rejectPromise)
1174
+ server.listen(def.localPort, '127.0.0.1', () => resolvePromise())
1175
+ }).catch((error) => {
1176
+ throw new ApiError('FORWARD_FAILED', `无法监听本地端口:${error && error.message ? error.message : error}`)
1177
+ })
1178
+ record.server = server
1179
+ record.localPort = server.address().port
1180
+ return record
1181
+ }
1098
1182
  const linkedStore = new linkedFs.LinkedStore(join(dir, 'linked.json'))
1099
1183
  const remoteStore = new RemoteWorkspaceStore(join(dir, 'remote-workspaces.json'))
1100
1184
  // Old "open in notepad" copies are dead weight; clear them once per start.
@@ -3086,6 +3170,31 @@ export function apply(ctx) {
3086
3170
 
3087
3171
  // ── method table ─────────────────────────────────────────────────────────
3088
3172
 
3173
+ // The trigger hands us a bare session id, the registry stores "session-<id>".
3174
+ function sameSessionIdKey(value) {
3175
+ const text = String(value == null ? '' : value).trim().toLowerCase()
3176
+ return text.startsWith('session-') ? text.slice('session-'.length) : text
3177
+ }
3178
+
3179
+ // Windows paths are case-insensitive and may mix separators; compare the
3180
+ // resolved forms so a session cwd still matches its workspace record.
3181
+ function sameWorkspacePath(left, right) {
3182
+ const a = String(left == null ? '' : left)
3183
+ const b = String(right == null ? '' : right)
3184
+ if (a === '' || b === '') return false
3185
+ const normalize = (value) => {
3186
+ let text = value
3187
+ try {
3188
+ text = resolve(value)
3189
+ } catch (error) {
3190
+ text = value
3191
+ }
3192
+ text = text.replace(/\\/g, '/').replace(/\/+$/, '')
3193
+ return process.platform === 'win32' ? text.toLowerCase() : text
3194
+ }
3195
+ return normalize(a) === normalize(b)
3196
+ }
3197
+
3089
3198
  const methods = {
3090
3199
  async 'hosts.list'(params) {
3091
3200
  const hosts = await refreshHosts(Boolean(params && params.refresh))
@@ -3249,49 +3358,44 @@ export function apply(ctx) {
3249
3358
  const existing = [...forwardServers.values()].find((item) => item.hostId === host.id
3250
3359
  && item.remotePort === remotePort && item.remoteHost === remoteHost)
3251
3360
  if (existing) return { forward: forwardView(existing) }
3252
- const remote = await pool.remote(host)
3253
- const id = randomUUID()
3254
- const server = net.createServer((socket) => {
3255
- remote.forwardOut(remoteHost, remotePort).then((channel) => {
3256
- socket.pipe(channel).pipe(socket)
3257
- channel.on('error', () => socket.destroy())
3258
- socket.on('error', () => channel.destroy())
3259
- }).catch((error) => {
3260
- log('warn', `forward ${id} failed: ${error && error.message ? error.message : error}`)
3261
- socket.destroy()
3262
- })
3263
- })
3264
- await new Promise((resolvePromise, rejectPromise) => {
3265
- server.once('error', rejectPromise)
3266
- server.listen(wantedPort, '127.0.0.1', () => resolvePromise())
3267
- }).catch((error) => {
3268
- throw new ApiError('FORWARD_FAILED', `无法监听本地端口:${error && error.message ? error.message : error}`)
3269
- })
3270
- const record = {
3271
- id,
3361
+ const record = await openForward({
3362
+ id: randomUUID(),
3272
3363
  hostId: host.id,
3273
3364
  hostName: host.name || host.id,
3274
3365
  remoteHost,
3275
3366
  remotePort,
3276
- localPort: server.address().port,
3367
+ localPort: wantedPort,
3277
3368
  startedAt: Date.now(),
3278
- server,
3279
- remote,
3280
- }
3281
- forwardServers.set(id, record)
3369
+ }, true)
3370
+ forwardServers.set(record.id, record)
3371
+ forwardPending.delete(record.id)
3372
+ saveForwardDefs()
3282
3373
  return { forward: forwardView(record) }
3283
3374
  },
3284
3375
 
3285
3376
  async 'hosts.forwards'() {
3286
- return { forwards: [...forwardServers.values()].map(forwardView) }
3377
+ const live = [...forwardServers.values()].map(forwardView)
3378
+ // A tunnel whose port was taken on restart is still a definition the user
3379
+ // owns: list it with the reason instead of hiding it.
3380
+ const stuck = [...forwardPending.values()].map((item) => ({
3381
+ ...forwardView({ ...item.def, localPort: 0 }),
3382
+ url: '',
3383
+ error: item.error,
3384
+ }))
3385
+ return { forwards: [...live, ...stuck] }
3287
3386
  },
3288
3387
 
3289
3388
  async 'hosts.unforward'(params = {}) {
3290
3389
  const id = typeof params.id === 'string' ? params.id.trim() : ''
3291
3390
  const record = forwardServers.get(id)
3292
- if (!record) return { removed: false }
3293
- try { record.server.close() } catch { /* already closed */ }
3294
- forwardServers.delete(id)
3391
+ const stuck = forwardPending.get(id)
3392
+ if (!record && !stuck) return { removed: false }
3393
+ if (record) {
3394
+ try { record.server.close() } catch { /* already closed */ }
3395
+ forwardServers.delete(id)
3396
+ }
3397
+ forwardPending.delete(id)
3398
+ saveForwardDefs()
3295
3399
  return { removed: true }
3296
3400
  },
3297
3401
 
@@ -4621,6 +4725,51 @@ export function apply(ctx) {
4621
4725
  * Path completion for `@`-style references (0.18.0). Returns candidate
4622
4726
  * paths relative to the remote root, best match first. Names only.
4623
4727
  */
4728
+ /**
4729
+ * Map one session to the linked/remote workspace that owns its directory.
4730
+ * The client half has no way to read a session's cwd, so the '@' reference
4731
+ * source asks for it here instead of guessing.
4732
+ */
4733
+ async 'linked.sessionScope'(params = {}) {
4734
+ const sessionId = typeof params.sessionId === 'string' ? params.sessionId.trim() : ''
4735
+ if (sessionId === '') throw new ApiError('BAD_REQUEST', '缺少 sessionId')
4736
+ const registry = typeof ctx.get === 'function' ? ctx.get('workspaceRegistry') : null
4737
+ if (!registry || typeof registry.list !== 'function') return { scope: null }
4738
+ let cwd = ''
4739
+ let workspaces = []
4740
+ try {
4741
+ workspaces = registry.list() || []
4742
+ } catch (error) {
4743
+ workspaces = []
4744
+ }
4745
+ const wanted = sameSessionIdKey(sessionId)
4746
+ for (const workspace of workspaces) {
4747
+ if (!workspace || !Array.isArray(workspace.sessionIds)) continue
4748
+ let owned = false
4749
+ for (const candidate of workspace.sessionIds) {
4750
+ if (sameSessionIdKey(String(candidate == null ? '' : candidate)) === wanted) {
4751
+ owned = true
4752
+ break
4753
+ }
4754
+ }
4755
+ if (!owned) continue
4756
+ cwd = String(workspace.path || '')
4757
+ break
4758
+ }
4759
+ if (cwd === '') return { scope: null }
4760
+ for (const record of linkedStore.list()) {
4761
+ if (sameWorkspacePath(cwd, record.localPath)) {
4762
+ return { scope: { linkedId: record.id, root: record.remotePath || '', cwd } }
4763
+ }
4764
+ }
4765
+ for (const record of remoteStore.list()) {
4766
+ if (sameWorkspacePath(cwd, record.placeholderPath)) {
4767
+ return { scope: { remoteId: record.id, root: record.remotePath || '', cwd } }
4768
+ }
4769
+ }
4770
+ return { scope: null, cwd }
4771
+ },
4772
+
4624
4773
  async 'fs.references'(params = {}) {
4625
4774
  const scope = await requireScope(params)
4626
4775
  const query = typeof params.query === 'string' ? params.query.trim() : ''
@@ -5889,6 +6038,36 @@ export function apply(ctx) {
5889
6038
  ctx.effect(() => () => {
5890
6039
  for (const id of [...autoPushWatchers.keys()]) stopAutoPush(id)
5891
6040
  }, 'dsh-linked-workspace: auto push watchers')
6041
+ // Re-bind the tunnels that were open when this Host last stopped. Deferred so
6042
+ // the whole apply() body (pool, findHost) is initialised first.
6043
+ setTimeout(() => {
6044
+ const defs = readForwardDefs()
6045
+ if (defs.length === 0) return
6046
+ void (async () => {
6047
+ for (const def of defs) {
6048
+ if (forwardServers.has(def.id) || forwardPending.has(def.id)) continue
6049
+ try {
6050
+ const record = await openForward(def, false)
6051
+ forwardServers.set(record.id, record)
6052
+ } catch (error) {
6053
+ const message = error && error.message ? error.message : String(error)
6054
+ if (def.localPort !== 0) {
6055
+ try {
6056
+ const retry = await openForward({ ...def, localPort: 0 }, false)
6057
+ forwardServers.set(retry.id, retry)
6058
+ continue
6059
+ } catch { /* keep the definition, report it as stuck */ }
6060
+ }
6061
+ forwardPending.set(def.id, { def, error: message })
6062
+ log('warn', `could not restore tunnel ${def.id}: ${message}`)
6063
+ }
6064
+ }
6065
+ saveForwardDefs()
6066
+ if (forwardServers.size > 0 || forwardPending.size > 0) {
6067
+ log('info', `tunnels: ${forwardServers.size} re-bound, ${forwardPending.size} waiting`)
6068
+ }
6069
+ })()
6070
+ }, 0)
5892
6071
  ctx.effect(() => () => {
5893
6072
  for (const record of forwardServers.values()) {
5894
6073
  try { record.server.close() } catch { /* already closed */ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-linked-workspace",
3
- "version": "0.24.1",
3
+ "version": "0.26.0",
4
4
  "description": "Linked local ⇄ remote workspaces and remote workspaces for DeepSeek Harness: theme-aware split editor, file-type icons, and agent tools that edit remote files directly.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",