Files
dsh-worktable/01_content/src/index.ts
T

299 lines
13 KiB
TypeScript
Raw Normal View History

import { Context } from '@deepseek-ai/cordis'
import { execFile } from 'node:child_process'
import { readdirSync, realpathSync } from 'node:fs'
import { readdir, readFile } from 'node:fs/promises'
import { dirname, resolve as pathResolve, sep } from 'node:path'
import { createRequire } from 'node:module'
import { homedir } from 'node:os'
import { fileURLToPath, pathToFileURL } from 'node:url'
/**
* dsh-worktable 服务端:健康路由 + 工作区内容窗的数据路由。
* 参考 dsh-better-sidebar 的架构——内容窗能力由本插件自己的服务端路由提供:
* - POST /api/worktable/fs 目录列表(资源管理器窗)
* - POST /api/worktable/git git 状态(源代码管理窗)
* - WS /api/worktable/term node-pty 终端流(终端窗;依赖宿主 node_modules 中的
* node-pty 与 ws,缺失时该路由不注册、终端窗降级提示)
*/
export const name = 'dsh-worktable'
export const inject = ['webServer', 'sessions']
export const HEALTH_PATH = '/api/worktable/health'
const MAX_ENTRIES = 500
/** 本地文件/站点静态资源的 MIME 映射(file 与 site 两条路由共用) */
const FILE_TYPES: Record<string, string> = {
html: 'text/html; charset=utf-8', htm: 'text/html; charset=utf-8',
css: 'text/css; charset=utf-8', js: 'text/javascript; charset=utf-8', mjs: 'text/javascript; charset=utf-8',
json: 'application/json; charset=utf-8', map: 'application/json; charset=utf-8',
md: 'text/markdown; charset=utf-8', markdown: 'text/markdown; charset=utf-8',
txt: 'text/plain; charset=utf-8', log: 'text/plain; charset=utf-8',
pdf: 'application/pdf', svg: 'image/svg+xml', png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg',
gif: 'image/gif', webp: 'image/webp', bmp: 'image/bmp', ico: 'image/x-icon', avif: 'image/avif',
woff: 'font/woff', woff2: 'font/woff2', ttf: 'font/ttf', otf: 'font/otf',
wasm: 'application/wasm', mp3: 'audio/mpeg', mp4: 'video/mp4', webm: 'video/webm',
}
const SITE_PREFIX = '/api/worktable/site'
/**
* 从本插件模块位置向祖先方向查找并加载 node_modules 包(如 ws / node-pty)。
* 本包经 junction 链接进 profile,普通 import 可能解析不到 profile 级依赖;
* 同时尝试 junction 路径与 realpath 两条祖先链。
*/
function loadPkg(pkg: string): any | null {
const starts = new Set<string>()
try { starts.add(dirname(fileURLToPath(import.meta.url))) } catch {}
try { starts.add(realpathSync(dirname(fileURLToPath(import.meta.url)))) } catch {}
for (const start of starts) {
let dir: string | null = start
while (dir && dir !== pathResolve(dir, '..')) {
try {
const req = createRequire(pathToFileURL(pathResolve(dir, '__wt_probe__.js')).href)
return req(pkg)
} catch {}
dir = pathResolve(dir, '..')
}
}
// 兜底:DSH 标准目录 ~/.dsh/profiles/*/node_modules(宿主按 realpath 加载时前两条链都找不到)
try {
const profilesDir = pathResolve(homedir(), '.dsh', 'profiles')
for (const profile of readdirSync(profilesDir, { withFileTypes: true })) {
if (!profile.isDirectory() && !profile.isSymbolicLink()) continue
const nm = pathResolve(profilesDir, profile.name, 'node_modules')
try {
const req = createRequire(pathToFileURL(pathResolve(nm, '__wt_probe__.js')).href)
return req(pkg)
} catch {}
}
} catch {}
return null
}
/** 解析会话工作目录:服务端 header.cwd 优先,其次客户端传入 cwd,最后进程 cwd */
function serverCwd(ctx: any, sessionId?: string, clientCwd?: string): string {
if (sessionId) {
try {
const headerCwd = ctx.sessions?.get?.(sessionId)?.header?.cwd
if (typeof headerCwd === 'string' && headerCwd) return headerCwd
} catch {}
}
if (typeof clientCwd === 'string' && clientCwd) return clientCwd
return process.cwd()
}
function json(res: any, status: number, body: unknown) {
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
res.end(JSON.stringify(body))
}
async function readJsonBody(req: any): Promise<any> {
const chunks: Buffer[] = []
for await (const chunk of req) chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk)
const text = Buffer.concat(chunks).toString('utf8')
if (!text) return {}
try { return JSON.parse(text) } catch { return {} }
}
/** 列出一个目录层级(目录在前、大小写不敏感排序、上限 500、隐藏项标注) */
async function listDirectory(path: string) {
const abs = pathResolve(path)
const dirents = await readdir(abs, { withFileTypes: true })
const entries = dirents
.map((d) => ({ name: d.name, path: abs + sep + d.name, isDir: d.isDirectory(), hidden: d.name.startsWith('.') }))
.sort((a, b) => {
if (a.isDir !== b.isDir) return a.isDir ? -1 : 1
return a.name.localeCompare(b.name, undefined, { sensitivity: 'base' })
})
const truncated = entries.length > MAX_ENTRIES
return { path: abs, entries: truncated ? entries.slice(0, MAX_ENTRIES) : entries, truncated }
}
function gitExec(args: string[], cwd: string): Promise<string> {
return new Promise((resolvePromise, reject) => {
execFile('git', args, { cwd, windowsHide: true, maxBuffer: 4 * 1024 * 1024 }, (err, stdout) => {
if (err) reject(err)
else resolvePromise(stdout)
})
})
}
/** git 状态快照(porcelain v1 -z;非仓库返回 isRepo:false) */
async function gitStatus(cwd: string) {
try {
const branchRaw = await gitExec(['rev-parse', '--abbrev-ref', 'HEAD'], cwd)
const porcelain = await gitExec(['status', '--porcelain=v1', '-z'], cwd)
const entries = porcelain
.split('\0')
.filter((s) => s.length > 2)
.map((s) => ({ xy: s.slice(0, 2), path: s.slice(3) }))
return { isRepo: true, branch: branchRaw.trim() || 'HEAD', entries }
} catch {
return { isRepo: false, branch: undefined, entries: [] }
}
}
/** 终端 WebSocket 升级路由(同步注册 + ctx.effect,同 better-sidebar;node-pty 缺失时不注册) */
function setupTerminal(webServer: any, ctx: any) {
if (typeof webServer.registerUpgrade !== 'function') return
const wsMod = loadPkg('ws')
const ptyMod = loadPkg('node-pty')
ctx.logger?.info?.('[dsh-worktable] term deps: ws=' + (wsMod ? 'ok' : 'MISSING') + ' node-pty=' + (ptyMod ? 'ok' : 'MISSING'))
if (!wsMod || !ptyMod) {
ctx.logger?.warn('[dsh-worktable] 终端路由未注册:ws/node-pty 不可用')
return
}
const WebSocketServer = wsMod.WebSocketServer ?? wsMod.default?.WebSocketServer
if (!WebSocketServer) return
const pty = ptyMod.default ?? ptyMod
const wss = new WebSocketServer({ noServer: true })
const spawnShell = (): { cmd: string; args: string[] } =>
process.platform === 'win32'
? { cmd: 'powershell.exe', args: ['-NoLogo'] }
: { cmd: process.env.SHELL || '/bin/bash', args: [] }
const clampDim = (v: number, fallback: number) => Math.min(1024, Math.max(2, Number.isFinite(v) ? v : fallback))
ctx.effect(() => webServer.registerUpgrade({
path: '/api/worktable/term',
handler: (req: any, socket: any, head: any) => {
wss.handleUpgrade(req, socket, head, (ws: any) => {
const u = new URL(req.url ?? '/', 'http://dsh.internal')
const cwd = serverCwd(ctx, u.searchParams.get('sessionId') || undefined, u.searchParams.get('cwd') || undefined)
const cols = clampDim(Number(u.searchParams.get('cols')), 80)
const rows = clampDim(Number(u.searchParams.get('rows')), 24)
let term: any = null
try {
const shell = spawnShell()
term = pty.spawn(shell.cmd, shell.args, { name: 'xterm-256color', cols, rows, cwd, env: process.env })
} catch (err) {
try { ws.send('\r\n[worktable] 终端启动失败:' + String(err)) } catch {}
try { ws.close() } catch {}
return
}
term.onData((d: string) => { try { ws.send(d) } catch {} })
term.onExit(() => { try { ws.close() } catch {} })
ws.on('message', (raw: any) => {
const text = String(raw)
try {
const msg = JSON.parse(text)
if (msg && msg.type === 'resize' && Number.isFinite(msg.cols) && Number.isFinite(msg.rows)) {
term.resize(clampDim(msg.cols, cols), clampDim(msg.rows, rows))
return
}
} catch {}
try { term.write(text) } catch {}
})
ws.on('close', () => { try { term.kill() } catch {} })
})
},
}), 'dsh-worktable: terminal upgrade')
}
export function apply(ctx: Context) {
const webServer = (ctx as any).webServer
if (!webServer) {
ctx.logger?.warn('[dsh-worktable] ctx.webServer 不可用(headless profile?),跳过服务端路由')
return
}
webServer.register({
kind: 'exact',
path: HEALTH_PATH,
handler: (_req: any, res: any) => {
json(res, 200, { plugin: 'dsh-worktable', version: '0.2.0', ok: true })
},
})
// 本地文件读取(资源管理器点击 .html 后浏览器标签内打开)
webServer.register({
kind: 'exact',
path: '/api/worktable/file',
handler: async (req: any, res: any) => {
try {
const u = new URL(req.url ?? '/', 'http://dsh.internal')
const p = u.searchParams.get('path') || ''
if (!p) { json(res, 400, { error: 'missing path' }); return }
const abs = pathResolve(p)
const stat = await import('node:fs/promises').then((m) => m.stat(abs))
if (stat.size > 20 * 1024 * 1024) { json(res, 413, { error: 'file too large' }); return }
const data = await readFile(abs)
const ext = (abs.split('.').pop() || '').toLowerCase()
const types: Record<string, string> = {
html: 'text/html; charset=utf-8', htm: 'text/html; charset=utf-8',
css: 'text/css; charset=utf-8', js: 'text/javascript; charset=utf-8', mjs: 'text/javascript; charset=utf-8',
json: 'application/json; charset=utf-8', md: 'text/markdown; charset=utf-8', markdown: 'text/markdown; charset=utf-8', txt: 'text/plain; charset=utf-8', log: 'text/plain; charset=utf-8',
pdf: 'application/pdf', svg: 'image/svg+xml', png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif', webp: 'image/webp', bmp: 'image/bmp', ico: 'image/x-icon',
}
res.writeHead(200, { 'content-type': FILE_TYPES[ext] ?? 'application/octet-stream', 'cache-control': 'no-store' })
res.end(data)
} catch (err) {
json(res, 404, { error: String(err) })
}
},
})
// 本地站点(目录级静态托管):点开 index.html 时挂载整个所在目录,
// 让 ./assets/... 等相对引用正常解析(前缀路由,余下路径 = <rootToken>/<相对路径>)。
webServer.register({
kind: 'prefix',
path: SITE_PREFIX,
handler: async (req: any, res: any) => {
try {
if (req.method !== 'GET') { res.writeHead(405); res.end(); return }
const pathname = new URL(req.url ?? '/', 'http://dsh.internal').pathname
const segs = pathname.slice(SITE_PREFIX.length).split('/').filter(Boolean)
const rootToken = segs.shift() ?? ''
const rel = segs.map((s) => { try { return decodeURIComponent(s) } catch { return s } }).join('/')
if (!rootToken) { json(res, 400, { error: 'missing root' }); return }
const root = pathResolve(rootToken)
let abs = pathResolve(root, rel)
if (abs !== root && !abs.startsWith(root + sep)) { json(res, 403, { error: 'outside root' }); return }
const statMod = await import('node:fs/promises')
let info = await statMod.stat(abs).catch(() => null)
if (info && info.isDirectory()) {
abs = pathResolve(abs, 'index.html')
info = await statMod.stat(abs).catch(() => null)
}
if (!info || !info.isFile()) { json(res, 404, { error: 'not found' }); return }
if (info.size > 40 * 1024 * 1024) { json(res, 413, { error: 'file too large' }); return }
const data = await readFile(abs)
const ext = (abs.split('.').pop() || '').toLowerCase()
res.writeHead(200, { 'content-type': FILE_TYPES[ext] ?? 'application/octet-stream', 'cache-control': 'no-store' })
res.end(data)
} catch (err) {
json(res, 404, { error: String(err) })
}
},
})
webServer.register({
kind: 'exact',
path: '/api/worktable/fs',
handler: async (req: any, res: any) => {
try {
const body = await readJsonBody(req)
const path = typeof body.path === 'string' && body.path
? body.path
: serverCwd(ctx, body.sessionId, body.cwd)
json(res, 200, await listDirectory(path))
} catch (err) {
json(res, 500, { path: '', entries: [], truncated: false, error: String(err) })
}
},
})
webServer.register({
kind: 'exact',
path: '/api/worktable/git',
handler: async (req: any, res: any) => {
const body = await readJsonBody(req)
const cwd = serverCwd(ctx, body.sessionId, body.cwd)
json(res, 200, await gitStatus(cwd))
},
})
setupTerminal(webServer, ctx)
}