feat: 五个功能窗第一版(资源管理器/SCM/任务/终端/浏览器,照搬 better-sidebar 架构)

This commit is contained in:
Aisland
2026-08-17 11:47:35 +08:00
parent 86c9904b42
commit 981c2acfb3
10 changed files with 463 additions and 19 deletions
+145 -5
View File
@@ -1,24 +1,164 @@
import { Context } from '@deepseek-ai/cordis'
import { execFile } from 'node:child_process'
import { readdir } from 'node:fs/promises'
import { resolve, sep } from 'node:path'
/**
* 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'
/** 依赖 webServer 服务(web profile 由 dsh-web-app 挂载) */
export const inject = ['webServer']
export const HEALTH_PATH = '/api/worktable/health'
const MAX_ENTRIES = 500
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 = resolve(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 升级路由(node-pty 缺失时不注册,终端窗降级为提示) */
async function setupTerminal(webServer: any) {
if (typeof webServer.registerUpgrade !== 'function') return
let wsMod: any = null
let ptyMod: any = null
try { wsMod = await import('ws') } catch {}
try { ptyMod = await import('node-pty') } catch {}
if (!wsMod || !ptyMod) return
const WebSocketServer = wsMod.WebSocketServer ?? wsMod.default?.WebSocketServer
if (!WebSocketServer) return
const pty = ptyMod.default ?? ptyMod
const wss = new WebSocketServer({ noServer: true })
const spawnShell = (): string =>
process.platform === 'win32' ? (process.env.ComSpec || 'cmd.exe') : (process.env.SHELL || '/bin/bash')
const clampDim = (v: number, fallback: number) => Math.min(1024, Math.max(2, Number.isFinite(v) ? v : fallback))
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 = u.searchParams.get('cwd') || process.cwd()
const cols = clampDim(Number(u.searchParams.get('cols')), 80)
const rows = clampDim(Number(u.searchParams.get('rows')), 24)
let term: any = null
try {
term = pty.spawn(spawnShell(), [], { 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 {} })
})
},
})
}
export function apply(ctx: Context) {
const webServer = (ctx as any).webServer
if (!webServer) {
ctx.logger?.warn('[dsh-worktable] ctx.webServer 不可用(headless profile?),跳过健康路由')
ctx.logger?.warn('[dsh-worktable] ctx.webServer 不可用(headless profile?),跳过服务端路由')
return
}
webServer.register({
kind: 'exact',
path: HEALTH_PATH,
handler: (_req: any, res: any) => {
const body = JSON.stringify({ plugin: 'dsh-worktable', version: '0.1.0', ok: true })
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
res.end(body)
json(res, 200, { plugin: 'dsh-worktable', version: '0.2.0', ok: true })
},
})
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 : process.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 = typeof body.cwd === 'string' && body.cwd ? body.cwd : process.cwd()
json(res, 200, await gitStatus(cwd))
},
})
setupTerminal(webServer).catch(() => {})
}