@maxbill/dsh-file-explorer 1.0.1 → 1.1.1
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/README.en.md +410 -146
- package/README.md +397 -136
- package/docs/panel.png +0 -0
- package/docs/preview-sheet.png +0 -0
- package/docs/quick-look.png +0 -0
- package/docs/search.png +0 -0
- package/lib/client.js +1154 -110
- package/lib/index.js +247 -9
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -43,6 +43,29 @@ const PREFIX = '/api/file-explorer'
|
|
|
43
43
|
/** Browsing root: the user's home directory. */
|
|
44
44
|
const ROOT = homedir()
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* DSH's filesystem service when the host mounts one. The plugin never requires
|
|
48
|
+
* it: a sandboxed backend may refuse paths outside its root, and browsing stays
|
|
49
|
+
* possible through the plain node:fs fallbacks.
|
|
50
|
+
*/
|
|
51
|
+
let fsApi = null
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Ask DSH's filesystem service about one path.
|
|
55
|
+
* @param {string} target - absolute path
|
|
56
|
+
* @returns {Promise<{ target: object, info: object } | null>} target and metadata, or null
|
|
57
|
+
*/
|
|
58
|
+
async function fsPeek(target) {
|
|
59
|
+
if (!fsApi) return null
|
|
60
|
+
try {
|
|
61
|
+
const resolved = await fsApi.resolve(target)
|
|
62
|
+
const info = await fsApi.stat(resolved)
|
|
63
|
+
return { target: resolved, info }
|
|
64
|
+
} catch {
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
46
69
|
/**
|
|
47
70
|
* The platform this process is really on, unless a test says otherwise.
|
|
48
71
|
* Exposed through __test so the shells for the other two operating systems can
|
|
@@ -1004,6 +1027,8 @@ const ROUTES = [
|
|
|
1004
1027
|
{ suffix: '/archive', methods: ['POST'] },
|
|
1005
1028
|
{ suffix: '/extract', methods: ['POST'] },
|
|
1006
1029
|
{ suffix: '/git', methods: ['GET'] },
|
|
1030
|
+
{ suffix: '/watch', methods: ['GET'] },
|
|
1031
|
+
{ suffix: '/search', methods: ['GET'] },
|
|
1007
1032
|
]
|
|
1008
1033
|
|
|
1009
1034
|
/**
|
|
@@ -1089,11 +1114,6 @@ async function sendRaw(res, target) {
|
|
|
1089
1114
|
res.end(data)
|
|
1090
1115
|
}
|
|
1091
1116
|
|
|
1092
|
-
/**
|
|
1093
|
-
* Content type for a file, guessed from its extension.
|
|
1094
|
-
* @param {string} target - file path
|
|
1095
|
-
* @returns {string} mime type
|
|
1096
|
-
*/
|
|
1097
1117
|
function mimeOf(target) {
|
|
1098
1118
|
const ext = extname(target).toLowerCase()
|
|
1099
1119
|
const table = {
|
|
@@ -1109,6 +1129,11 @@ function mimeOf(target) {
|
|
|
1109
1129
|
'.ppt': 'application/vnd.ms-powerpoint',
|
|
1110
1130
|
'.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
1111
1131
|
'.htm': 'text/html; charset=utf-8', '.html': 'text/html; charset=utf-8',
|
|
1132
|
+
'.css': 'text/css; charset=utf-8', '.js': 'text/javascript; charset=utf-8',
|
|
1133
|
+
'.mjs': 'text/javascript; charset=utf-8', '.jsx': 'text/javascript; charset=utf-8',
|
|
1134
|
+
'.xml': 'application/xml; charset=utf-8', '.svgz': 'image/svg+xml',
|
|
1135
|
+
'.woff': 'font/woff', '.woff2': 'font/woff2', '.ttf': 'font/ttf', '.otf': 'font/otf',
|
|
1136
|
+
'.wasm': 'application/wasm', '.map': 'application/json; charset=utf-8',
|
|
1112
1137
|
'.tsv': 'text/tab-separated-values; charset=utf-8',
|
|
1113
1138
|
'.mp3': 'audio/mpeg', '.wav': 'audio/wav', '.m4a': 'audio/mp4', '.aac': 'audio/aac',
|
|
1114
1139
|
'.flac': 'audio/flac', '.ogg': 'audio/ogg', '.oga': 'audio/ogg', '.opus': 'audio/opus',
|
|
@@ -1180,7 +1205,11 @@ async function handleRequest(req, res) {
|
|
|
1180
1205
|
return
|
|
1181
1206
|
}
|
|
1182
1207
|
if (route === '/read' && method === 'GET') {
|
|
1183
|
-
|
|
1208
|
+
const target = await safePath(path)
|
|
1209
|
+
const payload = await readPreview(target)
|
|
1210
|
+
const peek = await fsPeek(target)
|
|
1211
|
+
payload.version = peek && peek.info && peek.info.version ? String(peek.info.version) : null
|
|
1212
|
+
send(res, 200, payload)
|
|
1184
1213
|
return
|
|
1185
1214
|
}
|
|
1186
1215
|
if (route === '/stat' && method === 'GET') {
|
|
@@ -1240,7 +1269,20 @@ async function handleRequest(req, res) {
|
|
|
1240
1269
|
}
|
|
1241
1270
|
if (route === '/write' && method === 'POST') {
|
|
1242
1271
|
const body = await readJsonBody(req)
|
|
1243
|
-
send(res, 200, await writeTextRoute(path, body.text))
|
|
1272
|
+
send(res, 200, await writeTextRoute(path, body.text, body ? body.version : null))
|
|
1273
|
+
return
|
|
1274
|
+
}
|
|
1275
|
+
if (route === '/watch' && method === 'GET') {
|
|
1276
|
+
send(res, 200, await watchRoute(path, url.searchParams.get('preview')))
|
|
1277
|
+
return
|
|
1278
|
+
}
|
|
1279
|
+
if (route === '/search' && method === 'GET') {
|
|
1280
|
+
send(res, 200, await searchRoute(
|
|
1281
|
+
path,
|
|
1282
|
+
url.searchParams.get('query') || '',
|
|
1283
|
+
url.searchParams.get('mode') === 'content' ? 'content' : 'name',
|
|
1284
|
+
url.searchParams.get('hidden') === '1',
|
|
1285
|
+
))
|
|
1244
1286
|
return
|
|
1245
1287
|
}
|
|
1246
1288
|
if (route === '/download' && method === 'GET') {
|
|
@@ -1586,16 +1628,203 @@ async function sendAttachment(res, target) {
|
|
|
1586
1628
|
* @param {unknown} text - new file contents
|
|
1587
1629
|
* @returns {Promise<object>} { ok, path, size, mtime }
|
|
1588
1630
|
*/
|
|
1589
|
-
async function writeTextRoute(rawPath, text) {
|
|
1631
|
+
async function writeTextRoute(rawPath, text, version) {
|
|
1590
1632
|
const target = await safePath(rawPath)
|
|
1591
1633
|
const info = await stat(target)
|
|
1592
1634
|
if (info.isDirectory()) throw new RouteError(400, 'path is a folder')
|
|
1593
1635
|
if (typeof text !== 'string') throw new RouteError(400, 'text is required')
|
|
1594
1636
|
const bytes = Buffer.byteLength(text, 'utf8')
|
|
1595
1637
|
if (bytes > MAX_TEXT_BYTES * 8) throw new RouteError(413, 'text is too large')
|
|
1638
|
+
// Prefer DSH's filesystem service: it publishes atomically and rejects a
|
|
1639
|
+
// stale version, so the panel and the host agree on what changed.
|
|
1640
|
+
const peek = await fsPeek(target)
|
|
1641
|
+
if (peek) {
|
|
1642
|
+
try {
|
|
1643
|
+
// The panel is a file manager: its writes are explicit user actions, not
|
|
1644
|
+
// agent tool calls, so they run outside the workspace sandbox. Passing the
|
|
1645
|
+
// policy explicitly keeps the guard/atomic publish without fencing to the
|
|
1646
|
+
// workspace root.
|
|
1647
|
+
const intent = version ? { kind: 'replaceIfVersion', version } : undefined
|
|
1648
|
+
const outcome = await fsApi.writeText(peek.target, text, intent, undefined, {
|
|
1649
|
+
mode: 'danger-full-access',
|
|
1650
|
+
workspaceRoot: ROOT,
|
|
1651
|
+
})
|
|
1652
|
+
const after = await stat(target)
|
|
1653
|
+
return {
|
|
1654
|
+
ok: true,
|
|
1655
|
+
path: target,
|
|
1656
|
+
size: after.size,
|
|
1657
|
+
mtime: after.mtimeMs,
|
|
1658
|
+
version: outcome && outcome.version ? String(outcome.version) : null,
|
|
1659
|
+
via: 'fs',
|
|
1660
|
+
}
|
|
1661
|
+
} catch (error) {
|
|
1662
|
+
if (error && (error.code === 'FS_STALE_VERSION' || error.code === 'FS_NOT_OBSERVED')) {
|
|
1663
|
+
throw new RouteError(409, '文件已被其它程序修改,请重新打开后再保存')
|
|
1664
|
+
}
|
|
1665
|
+
// Any other refusal still falls back to the plain node:fs write, so the
|
|
1666
|
+
// panel never becomes unable to save a file it can read.
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1596
1669
|
await writeFile(target, text, 'utf8')
|
|
1597
1670
|
const after = await stat(target)
|
|
1598
|
-
|
|
1671
|
+
const fresh = await fsPeek(target)
|
|
1672
|
+
return {
|
|
1673
|
+
ok: true,
|
|
1674
|
+
path: target,
|
|
1675
|
+
size: after.size,
|
|
1676
|
+
mtime: after.mtimeMs,
|
|
1677
|
+
version: fresh && fresh.info && fresh.info.version ? String(fresh.info.version) : null,
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
|
|
1681
|
+
/** Search limits: a bounded walk keeps one keystroke from freezing the host. */
|
|
1682
|
+
const SEARCH_MAX_ENTRIES = 4000
|
|
1683
|
+
const SEARCH_MAX_HITS = 200
|
|
1684
|
+
const SEARCH_MAX_DEPTH = 12
|
|
1685
|
+
const SEARCH_TIMEOUT_MS = 4000
|
|
1686
|
+
const SEARCH_MAX_FILE_BYTES = 512 * 1024
|
|
1687
|
+
const SEARCH_HITS_PER_FILE = 20
|
|
1688
|
+
const SEARCH_SKIP_DIRS = new Set(['.git', 'node_modules', '.svn', '.hg', '.Trash', '.cache', 'dist', 'build'])
|
|
1689
|
+
const SEARCH_TEXT_EXT = /\.(txt|md|markdown|json|jsonc|json5|ya?ml|toml|ini|conf|cfg|env|log|csv|tsv|js|mjs|cjs|jsx|ts|tsx|vue|svelte|css|scss|less|html?|xml|svg|sh|bash|zsh|fish|py|rb|go|rs|java|kt|swift|c|h|cc|cpp|hpp|cs|php|sql|graphql|lua|pl|r|tex|dockerfile|gitignore|editorconfig)$/i
|
|
1690
|
+
|
|
1691
|
+
/**
|
|
1692
|
+
* A cheap fingerprint of a directory and one previewed file, so the panel can
|
|
1693
|
+
* notice that an agent (or another program) changed something.
|
|
1694
|
+
* @param {string} rawDir - directory on screen
|
|
1695
|
+
* @param {string | null} rawPreview - file being previewed
|
|
1696
|
+
* @returns {Promise<object>} { dir, preview, at }
|
|
1697
|
+
*/
|
|
1698
|
+
async function watchRoute(rawDir, rawPreview) {
|
|
1699
|
+
const dir = await safePath(rawDir)
|
|
1700
|
+
const dirInfo = await stat(dir).catch(() => null)
|
|
1701
|
+
let count = 0
|
|
1702
|
+
try {
|
|
1703
|
+
count = (await readdir(dir)).length
|
|
1704
|
+
} catch {
|
|
1705
|
+
count = 0
|
|
1706
|
+
}
|
|
1707
|
+
let preview = null
|
|
1708
|
+
if (rawPreview) {
|
|
1709
|
+
try {
|
|
1710
|
+
const file = await safePath(rawPreview)
|
|
1711
|
+
const fileInfo = await stat(file)
|
|
1712
|
+
preview = fileInfo.isDirectory() ? 'dir:' + fileInfo.mtimeMs : fileInfo.mtimeMs + ':' + fileInfo.size
|
|
1713
|
+
} catch {
|
|
1714
|
+
preview = 'gone'
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1717
|
+
return {
|
|
1718
|
+
dir: dirInfo ? dirInfo.mtimeMs + ':' + dirInfo.size + ':' + count : 'gone',
|
|
1719
|
+
preview,
|
|
1720
|
+
at: Date.now(),
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
/**
|
|
1725
|
+
* Read one file's matching lines for a content search.
|
|
1726
|
+
* @param {string} file - absolute path
|
|
1727
|
+
* @param {string} needle - lower-case needle
|
|
1728
|
+
* @param {number} left - hit budget left for the whole search
|
|
1729
|
+
* @returns {Promise<object[]>} hits
|
|
1730
|
+
*/
|
|
1731
|
+
async function searchFileContent(file, needle, left) {
|
|
1732
|
+
let info
|
|
1733
|
+
try {
|
|
1734
|
+
info = await stat(file)
|
|
1735
|
+
} catch {
|
|
1736
|
+
return []
|
|
1737
|
+
}
|
|
1738
|
+
if (!info.isFile() || info.size > SEARCH_MAX_FILE_BYTES) return []
|
|
1739
|
+
let text
|
|
1740
|
+
try {
|
|
1741
|
+
text = await readFile(file, 'utf8')
|
|
1742
|
+
} catch {
|
|
1743
|
+
return []
|
|
1744
|
+
}
|
|
1745
|
+
if (text.indexOf(String.fromCharCode(0)) !== -1) return []
|
|
1746
|
+
const hits = []
|
|
1747
|
+
const lines = text.split(/\r?\n/)
|
|
1748
|
+
for (let i = 0; i < lines.length && hits.length < Math.min(left, SEARCH_HITS_PER_FILE); i += 1) {
|
|
1749
|
+
const line = lines[i]
|
|
1750
|
+
if (!line || line.length > 4000) continue
|
|
1751
|
+
if (line.toLowerCase().indexOf(needle) === -1) continue
|
|
1752
|
+
hits.push({
|
|
1753
|
+
path: file,
|
|
1754
|
+
parent: dirname(file),
|
|
1755
|
+
name: basename(file),
|
|
1756
|
+
line: i + 1,
|
|
1757
|
+
text: line.trim().slice(0, 200),
|
|
1758
|
+
size: info.size,
|
|
1759
|
+
mtime: info.mtimeMs,
|
|
1760
|
+
})
|
|
1761
|
+
}
|
|
1762
|
+
return hits
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
/**
|
|
1766
|
+
* Walk one directory tree for names or contents.
|
|
1767
|
+
* @param {string} rawDir - directory to start from
|
|
1768
|
+
* @param {string} rawQuery - text to look for
|
|
1769
|
+
* @param {'name' | 'content'} mode - what to match
|
|
1770
|
+
* @param {boolean} hidden - include dot entries
|
|
1771
|
+
* @returns {Promise<object>} { hits, truncated, scanned, tookMs }
|
|
1772
|
+
*/
|
|
1773
|
+
async function searchRoute(rawDir, rawQuery, mode, hidden) {
|
|
1774
|
+
const start = await safePath(rawDir)
|
|
1775
|
+
const info = await stat(start)
|
|
1776
|
+
if (!info.isDirectory()) throw new RouteError(400, 'path is not a folder')
|
|
1777
|
+
const query = String(rawQuery || '').trim()
|
|
1778
|
+
if (query.length === 0) return { hits: [], truncated: false, scanned: 0, tookMs: 0, mode: mode }
|
|
1779
|
+
const needle = query.toLowerCase()
|
|
1780
|
+
const hits = []
|
|
1781
|
+
let scanned = 0
|
|
1782
|
+
let stopped = false
|
|
1783
|
+
const began = Date.now()
|
|
1784
|
+
const stop = () => stopped || hits.length >= SEARCH_MAX_HITS || scanned >= SEARCH_MAX_ENTRIES || Date.now() - began > SEARCH_TIMEOUT_MS
|
|
1785
|
+
const walk = async (folder, depth) => {
|
|
1786
|
+
if (stop()) { stopped = true; return }
|
|
1787
|
+
let entries
|
|
1788
|
+
try {
|
|
1789
|
+
entries = await readdir(folder, { withFileTypes: true })
|
|
1790
|
+
} catch {
|
|
1791
|
+
return
|
|
1792
|
+
}
|
|
1793
|
+
for (const dirent of entries) {
|
|
1794
|
+
if (stop()) { stopped = true; return }
|
|
1795
|
+
if (SEARCH_SKIP_DIRS.has(dirent.name)) continue
|
|
1796
|
+
if (!hidden && dirent.name.startsWith('.')) continue
|
|
1797
|
+
scanned += 1
|
|
1798
|
+
const child = join(folder, dirent.name)
|
|
1799
|
+
const isDir = dirent.isDirectory()
|
|
1800
|
+
if (mode === 'content') {
|
|
1801
|
+
if (!isDir && SEARCH_TEXT_EXT.test(dirent.name)) {
|
|
1802
|
+
const found = await searchFileContent(child, needle, SEARCH_MAX_HITS - hits.length)
|
|
1803
|
+
for (const hit of found) {
|
|
1804
|
+
hits.push(hit)
|
|
1805
|
+
if (hits.length >= SEARCH_MAX_HITS) { stopped = true; break }
|
|
1806
|
+
}
|
|
1807
|
+
}
|
|
1808
|
+
} else if (dirent.name.toLowerCase().indexOf(needle) !== -1) {
|
|
1809
|
+
const childInfo = await lstat(child).catch(() => null)
|
|
1810
|
+
if (childInfo) {
|
|
1811
|
+
hits.push({
|
|
1812
|
+
path: child,
|
|
1813
|
+
parent: folder,
|
|
1814
|
+
name: dirent.name,
|
|
1815
|
+
dir: isDir,
|
|
1816
|
+
size: childInfo.size,
|
|
1817
|
+
mtime: childInfo.mtimeMs,
|
|
1818
|
+
hidden: dirent.name.startsWith('.'),
|
|
1819
|
+
})
|
|
1820
|
+
if (hits.length >= SEARCH_MAX_HITS) { stopped = true; break }
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
if (isDir && depth < SEARCH_MAX_DEPTH) await walk(child, depth + 1)
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
await walk(start, 0)
|
|
1827
|
+
return { hits, truncated: stopped, scanned, tookMs: Date.now() - began, mode }
|
|
1599
1828
|
}
|
|
1600
1829
|
|
|
1601
1830
|
/** Most git entries the status route reports. */
|
|
@@ -1662,6 +1891,15 @@ async function gitStatusRoute(dir) {
|
|
|
1662
1891
|
* @param {import('@deepseek-ai/cordis').Context} ctx - host context
|
|
1663
1892
|
*/
|
|
1664
1893
|
export function apply(ctx) {
|
|
1894
|
+
// The filesystem service is optional: a host without it keeps the plain
|
|
1895
|
+
// node:fs behaviour. A nested inject scope activates only once it exists.
|
|
1896
|
+
try {
|
|
1897
|
+
ctx.inject(['fs'], function (fsCtx) {
|
|
1898
|
+
fsApi = fsCtx.fs || null
|
|
1899
|
+
})
|
|
1900
|
+
} catch {
|
|
1901
|
+
fsApi = null
|
|
1902
|
+
}
|
|
1665
1903
|
for (const route of ROUTES) {
|
|
1666
1904
|
ctx.effect(
|
|
1667
1905
|
() => ctx.connection.fetch.register({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maxbill/dsh-file-explorer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "文件管理器 — 浏览、预览并直接用起来:文件夹一键变成工作区,文档一键加进当前对话 · A file manager for DeepSeek Harness that turns folders into workspaces and documents into conversation attachments",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|