dsh-linked-workspace 0.8.3 → 0.9.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
@@ -16,7 +16,7 @@
16
16
  * addresses and workspace paths.
17
17
  */
18
18
  import { randomUUID } from 'node:crypto'
19
- import { chmodSync, closeSync, cpSync, existsSync, mkdirSync, mkdtempSync, openSync, readFileSync, readSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs'
19
+ import { chmodSync, closeSync, cpSync, existsSync, mkdirSync, mkdtempSync, openSync, readFileSync, readSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs'
20
20
  import { spawn } from 'node:child_process'
21
21
  import { mkdir, readdir, stat as fsStat } from 'node:fs/promises'
22
22
  import { homedir, tmpdir } from 'node:os'
@@ -2367,6 +2367,46 @@ export function apply(ctx) {
2367
2367
  }
2368
2368
  },
2369
2369
 
2370
+ /** Copy dropped OS files into a local directory (a local drop). */
2371
+ async 'local.import'(params = {}) {
2372
+ const rawDir = typeof params.dir === 'string' && params.dir.trim() !== '' ? params.dir.trim() : homedir()
2373
+ if (!isAbsolute(rawDir)) throw new ApiError('BAD_REQUEST', '本地路径必须是绝对路径')
2374
+ const dir = resolve(rawDir)
2375
+ let info
2376
+ try {
2377
+ info = await fsStat(dir)
2378
+ } catch (error) {
2379
+ throw mapLocalFsError(error)
2380
+ }
2381
+ if (!info.isDirectory()) throw new ApiError('FS_NOT_DIR', '目标不是目录')
2382
+ const rawPaths = Array.isArray(params.paths) ? params.paths : [params.path]
2383
+ const paths = rawPaths.filter((item) => typeof item === 'string' && item.trim() !== '').map((item) => resolve(item.trim()))
2384
+ if (paths.length === 0) throw new ApiError('BAD_REQUEST', '没有可导入的文件')
2385
+ if (paths.length > 50) throw new ApiError('BAD_REQUEST', '一次最多导入 50 个文件')
2386
+ let copied = 0
2387
+ let skipped = 0
2388
+ for (const source of paths) {
2389
+ let sourceInfo
2390
+ try {
2391
+ sourceInfo = await fsStat(source)
2392
+ } catch (error) {
2393
+ skipped += 1
2394
+ continue
2395
+ }
2396
+ if (!sourceInfo.isFile() || sourceInfo.size > LINKED_OPEN_FILE_CAP) {
2397
+ skipped += 1
2398
+ continue
2399
+ }
2400
+ try {
2401
+ cpSync(source, uniqueLocalPath(dir, basename(source)))
2402
+ copied += 1
2403
+ } catch (error) {
2404
+ skipped += 1
2405
+ }
2406
+ }
2407
+ return { dir, copied, skipped }
2408
+ },
2409
+
2370
2410
  async 'local.stat'(params = {}) {
2371
2411
  const target = typeof params.path === 'string' ? params.path.trim() : ''
2372
2412
  if (target === '' || !isAbsolute(target)) throw new ApiError('BAD_REQUEST', '本地路径必须是绝对路径')
@@ -2647,7 +2687,9 @@ export function apply(ctx) {
2647
2687
  const snapshot = scope.kind === 'linked' ? (scope.record.hostSnapshot || {}) : (scope.workspace.hostSnapshot || {})
2648
2688
  if (side === 'local') {
2649
2689
  if (scope.kind !== 'linked') throw new ApiError('BAD_REQUEST', '该工作区没有本地目录')
2650
- const target = scope.record.localPath
2690
+ const localRoot = resolve(scope.record.localPath)
2691
+ const target = typeof params.path === 'string' && params.path.trim() !== '' ? resolve(params.path.trim()) : localRoot
2692
+ if (!isInsideLocal(localRoot, target)) throw new ApiError('BAD_REQUEST', '路径超出该联动工作区的本地根目录')
2651
2693
  let info
2652
2694
  try {
2653
2695
  info = await fsStat(target)
@@ -2659,14 +2701,15 @@ export function apply(ctx) {
2659
2701
  if (!(await openLocalTerminal(target))) throw new ApiError('INTERNAL', '无法启动本地终端')
2660
2702
  return { opened: true, side, path: target }
2661
2703
  }
2662
- const stat = await scope.remote.stat(scope.root)
2704
+ const target = typeof params.path === 'string' && params.path.trim() !== '' ? scopeTarget(scope, params.path) : scope.root
2705
+ const stat = await scope.remote.stat(target)
2663
2706
  if (stat.type !== 'dir') throw new ApiError('FS_NOT_DIR', '远程目录不存在')
2664
2707
  if (!snapshot.host || !snapshot.username) throw new ApiError('AUTH_FAILED', '缺少 SSH 主机或用户名,无法打开终端')
2665
- if (testMode) return { opened: true, side, path: scope.root, host: snapshot.host, test: true }
2666
- if (!(await openRemoteTerminal(snapshot.host, snapshot.port, snapshot.username, scope.root))) {
2708
+ if (testMode) return { opened: true, side, path: target, host: snapshot.host, test: true }
2709
+ if (!(await openRemoteTerminal(snapshot.host, snapshot.port, snapshot.username, target))) {
2667
2710
  throw new ApiError('INTERNAL', '无法启动远程终端(找不到可用的终端程序)')
2668
2711
  }
2669
- return { opened: true, side, path: scope.root, host: snapshot.host }
2712
+ return { opened: true, side, path: target, host: snapshot.host }
2670
2713
  },
2671
2714
 
2672
2715
  async 'session.info'(params = {}) {
@@ -2714,6 +2757,211 @@ export function apply(ctx) {
2714
2757
  return await git.localSearch(scope.record.localPath, query, { maxResults })
2715
2758
  },
2716
2759
 
2760
+ /**
2761
+ * Read-only comparison of one directory pair. Lists files that exist on one
2762
+ * side only and files whose size or mtime disagree; it never touches data.
2763
+ */
2764
+ async 'fs.compare'(params = {}) {
2765
+ const scope = await requireScope(params)
2766
+ if (scope.kind !== 'linked') throw new ApiError('BAD_REQUEST', '只有联动工作区支持本地与远端对比')
2767
+ const root = params.path ? scopeTarget(scope, params.path) : scope.root
2768
+ const rootStat = await scope.remote.stat(root)
2769
+ if (rootStat.type !== 'dir') throw new ApiError('FS_NOT_DIR', '只能对比目录')
2770
+ const relativeRoot = linkedRelative(scope.record, root)
2771
+ const localPathRoot = resolve(scope.record.localPath)
2772
+ const localRoot = join(localPathRoot, ...(relativeRoot === '' ? [] : relativeRoot.split('/')))
2773
+ if (!isInsideLocal(localPathRoot, localRoot)) throw new ApiError('BAD_REQUEST', '路径超出该联动工作区的本地根目录')
2774
+ const MAX_FILES = 4000
2775
+ const MAX_DEPTH = 12
2776
+ const MAX_LIST = 200
2777
+ const excluded = (entryName) => LINKED_SYNC_EXCLUDES.has(entryName) || entryName === '.local' || entryName === '.remote'
2778
+ const remoteMap = new Map()
2779
+ const localMap = new Map()
2780
+ const state = { files: 0, truncated: false }
2781
+ const walkRemote = async (directory, prefix, depth) => {
2782
+ if (depth > MAX_DEPTH || state.files >= MAX_FILES) {
2783
+ state.truncated = true
2784
+ return
2785
+ }
2786
+ let listing
2787
+ try {
2788
+ listing = await scope.remote.listDir(directory)
2789
+ } catch (error) {
2790
+ state.truncated = true
2791
+ return
2792
+ }
2793
+ for (const entry of listing.entries) {
2794
+ if (excluded(entry.name)) continue
2795
+ if (state.files >= MAX_FILES) {
2796
+ state.truncated = true
2797
+ return
2798
+ }
2799
+ const relative = prefix === '' ? entry.name : `${prefix}/${entry.name}`
2800
+ if (entry.type === 'dir') {
2801
+ await walkRemote(entry.path, relative, depth + 1)
2802
+ continue
2803
+ }
2804
+ if (entry.type !== 'file') continue
2805
+ remoteMap.set(relative, { size: entry.size, mtime: entry.mtime })
2806
+ state.files += 1
2807
+ }
2808
+ }
2809
+ const walkLocal = (directory, prefix, depth) => {
2810
+ if (depth > MAX_DEPTH || state.files >= MAX_FILES) {
2811
+ state.truncated = true
2812
+ return
2813
+ }
2814
+ let entries
2815
+ try {
2816
+ entries = readdirSync(directory, { withFileTypes: true })
2817
+ } catch (error) {
2818
+ state.truncated = true
2819
+ return
2820
+ }
2821
+ for (const entry of entries) {
2822
+ if (excluded(entry.name)) continue
2823
+ if (state.files >= MAX_FILES) {
2824
+ state.truncated = true
2825
+ return
2826
+ }
2827
+ const relative = prefix === '' ? entry.name : `${prefix}/${entry.name}`
2828
+ const absolute = join(directory, entry.name)
2829
+ if (entry.isDirectory()) {
2830
+ walkLocal(absolute, relative, depth + 1)
2831
+ continue
2832
+ }
2833
+ if (!entry.isFile()) continue
2834
+ let info
2835
+ try {
2836
+ info = statSync(absolute)
2837
+ } catch (error) {
2838
+ state.truncated = true
2839
+ continue
2840
+ }
2841
+ localMap.set(relative, { size: info.size, mtime: info.mtimeMs })
2842
+ state.files += 1
2843
+ }
2844
+ }
2845
+ await walkRemote(root, '', 0)
2846
+ const localExists = existsSync(localRoot)
2847
+ if (localExists) walkLocal(localRoot, '', 0)
2848
+ const onlyLocal = []
2849
+ const changed = []
2850
+ for (const [relative, localInfo] of localMap) {
2851
+ const remoteInfo = remoteMap.get(relative)
2852
+ if (!remoteInfo) {
2853
+ onlyLocal.push({ path: relative, size: localInfo.size, mtime: localInfo.mtime })
2854
+ continue
2855
+ }
2856
+ const sizeDiffers = remoteInfo.size !== localInfo.size
2857
+ const mtimeDiffers = Math.abs(Number(remoteInfo.mtime) - localInfo.mtime) > 2000
2858
+ if (sizeDiffers || mtimeDiffers) {
2859
+ changed.push({
2860
+ path: relative,
2861
+ localSize: localInfo.size,
2862
+ remoteSize: remoteInfo.size,
2863
+ localMtime: localInfo.mtime,
2864
+ remoteMtime: remoteInfo.mtime,
2865
+ mtimeDiffers,
2866
+ })
2867
+ }
2868
+ }
2869
+ const onlyRemote = []
2870
+ for (const [relative, remoteInfo] of remoteMap) {
2871
+ if (!localMap.has(relative)) onlyRemote.push({ path: relative, size: remoteInfo.size, mtime: remoteInfo.mtime })
2872
+ }
2873
+ const sorted = (list) => list.sort((left, right) => left.path.localeCompare(right.path))
2874
+ const same = localMap.size - onlyLocal.length - changed.length
2875
+ return {
2876
+ path: root,
2877
+ relative: relativeRoot,
2878
+ localPath: localRoot,
2879
+ localMissing: !localExists,
2880
+ counts: {
2881
+ onlyLocal: onlyLocal.length,
2882
+ onlyRemote: onlyRemote.length,
2883
+ changed: changed.length,
2884
+ same: Math.max(same, 0),
2885
+ },
2886
+ onlyLocal: sorted(onlyLocal).slice(0, MAX_LIST),
2887
+ onlyRemote: sorted(onlyRemote).slice(0, MAX_LIST),
2888
+ changed: sorted(changed).slice(0, MAX_LIST),
2889
+ truncated: state.truncated,
2890
+ }
2891
+ },
2892
+
2893
+ /** Copy one remote file into the linked local directory (a download). */
2894
+ async 'fs.pullToLocal'(params = {}) {
2895
+ const scope = await requireScope(params)
2896
+ if (scope.kind !== 'linked') throw new ApiError('BAD_REQUEST', '只有联动工作区支持下载到本地目录')
2897
+ const target = scopeTarget(scope, params.path)
2898
+ const relative = linkedRelative(scope.record, target)
2899
+ if (relative === '') throw new ApiError('BAD_REQUEST', '不能下载联动工作区的远端根目录')
2900
+ const stat = await scope.remote.stat(target)
2901
+ if (stat.type !== 'file') throw new ApiError('BAD_REQUEST', '只能下载单个文件')
2902
+ if (stat.size > LINKED_OPEN_FILE_CAP) throw new ApiError('FS_TOO_LARGE', '文件超过 64 MB,无法下载到本地')
2903
+ const read = await scope.remote.readFile(target, { maxBytes: LINKED_OPEN_FILE_CAP })
2904
+ if (read.truncated || read.data.length !== stat.size) throw new ApiError('FS_INCOMPLETE', '远端文件读取不完整,已取消下载')
2905
+ const localRoot = resolve(scope.record.localPath)
2906
+ const requestedDir = typeof params.targetDir === 'string' && params.targetDir.trim() !== '' ? resolve(params.targetDir.trim()) : ''
2907
+ let localTarget
2908
+ if (requestedDir !== '') {
2909
+ // Drag-and-drop lets the user pick an arbitrary drop folder inside the
2910
+ // linked local root; the plain "download to local" path keeps mirroring
2911
+ // the remote relative path.
2912
+ if (!isInsideLocal(localRoot, requestedDir)) throw new ApiError('BAD_REQUEST', '路径超出该联动工作区的本地根目录')
2913
+ const dirInfo = await fsStat(requestedDir).catch(() => null)
2914
+ if (!dirInfo || !dirInfo.isDirectory()) throw new ApiError('FS_NOT_DIR', '目标不是目录')
2915
+ localTarget = join(requestedDir, basename(target))
2916
+ } else {
2917
+ localTarget = join(localRoot, ...relative.split('/'))
2918
+ }
2919
+ mkdirSync(dirname(localTarget), { recursive: true })
2920
+ const temp = join(dirname(localTarget), `.~${basename(localTarget)}.${process.pid}.${Date.now()}.tmp`)
2921
+ try {
2922
+ writeFileSync(temp, read.data)
2923
+ renameSync(temp, localTarget)
2924
+ } catch (error) {
2925
+ try { rmSync(temp, { force: true }) } catch {}
2926
+ throw mapLocalFsError(error)
2927
+ }
2928
+ const next = await fsStat(localTarget)
2929
+ forgetListings()
2930
+ return { path: localTarget, relative, size: next.size, mtime: next.mtimeMs }
2931
+ },
2932
+
2933
+ /** Upload dropped local files into a remote directory. */
2934
+ async 'fs.upload'(params = {}) {
2935
+ const scope = await requireScope(params)
2936
+ const targetDir = scopeTarget(scope, params.path)
2937
+ const stat = await scope.remote.stat(targetDir)
2938
+ if (stat.type !== 'dir') throw new ApiError('FS_NOT_DIR', '目标不是目录')
2939
+ const rawPaths = Array.isArray(params.paths) ? params.paths : [params.path]
2940
+ const paths = rawPaths.filter((item) => typeof item === 'string' && item.trim() !== '').map((item) => resolve(item.trim()))
2941
+ if (paths.length === 0) throw new ApiError('BAD_REQUEST', '没有可上传的文件')
2942
+ if (paths.length > 50) throw new ApiError('BAD_REQUEST', '一次最多上传 50 个文件')
2943
+ const limits = { files: 0, bytes: 0, skipped: 0 }
2944
+ for (const source of paths) {
2945
+ let info
2946
+ try {
2947
+ info = await fsStat(source)
2948
+ } catch (error) {
2949
+ limits.skipped += 1
2950
+ continue
2951
+ }
2952
+ if (!info.isFile() || info.size > LINKED_OPEN_FILE_CAP) {
2953
+ limits.skipped += 1
2954
+ continue
2955
+ }
2956
+ const data = readFileSync(source)
2957
+ await scope.remote.writeFileAtomic(posix.join(targetDir, basename(source)), data)
2958
+ limits.files += 1
2959
+ limits.bytes += data.length
2960
+ }
2961
+ forgetListings()
2962
+ return { side: 'remote', target: targetDir, files: limits.files, bytes: limits.bytes, skipped: limits.skipped }
2963
+ },
2964
+
2717
2965
  // ── linked workspaces ──────────────────────────────────────────────────
2718
2966
 
2719
2967
  async 'linked.list'() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-linked-workspace",
3
- "version": "0.8.3",
3
+ "version": "0.9.0",
4
4
  "description": "Linked local ⇄ remote workspaces and SSH-backed remote directories for DeepSeek Harness: theme-aware split editor, file-type icons, and an agent tool that edits remote files directly.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",