2026-08-16 23:34:22 +08:00
|
|
|
|
import { Context } from '@deepseek-ai/cordis'
|
2026-08-17 11:47:35 +08:00
|
|
|
|
import { execFile } from 'node:child_process'
|
2026-08-17 20:45:29 +08:00
|
|
|
|
import { readdirSync, realpathSync } from 'node:fs'
|
|
|
|
|
|
import { readdir, readFile } from 'node:fs/promises'
|
2026-08-17 12:28:32 +08:00
|
|
|
|
import { dirname, resolve as pathResolve, sep } from 'node:path'
|
|
|
|
|
|
import { createRequire } from 'node:module'
|
2026-08-17 12:49:27 +08:00
|
|
|
|
import { homedir } from 'node:os'
|
2026-08-17 12:28:32 +08:00
|
|
|
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
2026-08-17 11:47:35 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 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,缺失时该路由不注册、终端窗降级提示)
|
|
|
|
|
|
*/
|
2026-08-16 23:34:22 +08:00
|
|
|
|
|
|
|
|
|
|
export const name = 'dsh-worktable'
|
2026-08-17 12:19:08 +08:00
|
|
|
|
export const inject = ['webServer', 'sessions']
|
2026-08-16 23:34:22 +08:00
|
|
|
|
|
|
|
|
|
|
export const HEALTH_PATH = '/api/worktable/health'
|
|
|
|
|
|
|
2026-08-17 11:47:35 +08:00
|
|
|
|
const MAX_ENTRIES = 500
|
|
|
|
|
|
|
2026-08-17 23:39:06 +08:00
|
|
|
|
/** 本地文件/站点静态资源的 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'
|
|
|
|
|
|
|
2026-08-17 12:28:32 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 从本插件模块位置向祖先方向查找并加载 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, '..')
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-08-17 12:49:27 +08:00
|
|
|
|
// 兜底: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 {}
|
2026-08-17 12:28:32 +08:00
|
|
|
|
return null
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-17 12:19:08 +08:00
|
|
|
|
/** 解析会话工作目录:服务端 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()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-17 11:47:35 +08:00
|
|
|
|
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) {
|
2026-08-17 12:54:40 +08:00
|
|
|
|
const abs = pathResolve(path)
|
2026-08-17 11:47:35 +08:00
|
|
|
|
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: [] }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-17 12:54:40 +08:00
|
|
|
|
/** 终端 WebSocket 升级路由(同步注册 + ctx.effect,同 better-sidebar;node-pty 缺失时不注册) */
|
|
|
|
|
|
function setupTerminal(webServer: any, ctx: any) {
|
2026-08-17 11:47:35 +08:00
|
|
|
|
if (typeof webServer.registerUpgrade !== 'function') return
|
2026-08-17 12:54:40 +08:00
|
|
|
|
const wsMod = loadPkg('ws')
|
|
|
|
|
|
const ptyMod = loadPkg('node-pty')
|
2026-08-17 12:49:27 +08:00
|
|
|
|
ctx.logger?.info?.('[dsh-worktable] term deps: ws=' + (wsMod ? 'ok' : 'MISSING') + ' node-pty=' + (ptyMod ? 'ok' : 'MISSING'))
|
2026-08-17 12:28:32 +08:00
|
|
|
|
if (!wsMod || !ptyMod) {
|
2026-08-17 12:54:40 +08:00
|
|
|
|
ctx.logger?.warn('[dsh-worktable] 终端路由未注册:ws/node-pty 不可用')
|
2026-08-17 12:28:32 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-08-17 11:47:35 +08:00
|
|
|
|
const WebSocketServer = wsMod.WebSocketServer ?? wsMod.default?.WebSocketServer
|
|
|
|
|
|
if (!WebSocketServer) return
|
|
|
|
|
|
const pty = ptyMod.default ?? ptyMod
|
|
|
|
|
|
const wss = new WebSocketServer({ noServer: true })
|
2026-08-17 20:48:13 +08:00
|
|
|
|
const spawnShell = (): { cmd: string; args: string[] } =>
|
|
|
|
|
|
process.platform === 'win32'
|
|
|
|
|
|
? { cmd: 'powershell.exe', args: ['-NoLogo'] }
|
|
|
|
|
|
: { cmd: process.env.SHELL || '/bin/bash', args: [] }
|
2026-08-17 11:47:35 +08:00
|
|
|
|
const clampDim = (v: number, fallback: number) => Math.min(1024, Math.max(2, Number.isFinite(v) ? v : fallback))
|
|
|
|
|
|
|
2026-08-17 12:54:40 +08:00
|
|
|
|
ctx.effect(() => webServer.registerUpgrade({
|
2026-08-17 11:47:35 +08:00
|
|
|
|
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')
|
2026-08-17 12:19:08 +08:00
|
|
|
|
const cwd = serverCwd(ctx, u.searchParams.get('sessionId') || undefined, u.searchParams.get('cwd') || undefined)
|
2026-08-17 11:47:35 +08:00
|
|
|
|
const cols = clampDim(Number(u.searchParams.get('cols')), 80)
|
|
|
|
|
|
const rows = clampDim(Number(u.searchParams.get('rows')), 24)
|
|
|
|
|
|
let term: any = null
|
|
|
|
|
|
try {
|
2026-08-17 20:48:13 +08:00
|
|
|
|
const shell = spawnShell()
|
|
|
|
|
|
term = pty.spawn(shell.cmd, shell.args, { name: 'xterm-256color', cols, rows, cwd, env: process.env })
|
2026-08-17 11:47:35 +08:00
|
|
|
|
} 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 {} })
|
|
|
|
|
|
})
|
|
|
|
|
|
},
|
2026-08-17 12:54:40 +08:00
|
|
|
|
}), 'dsh-worktable: terminal upgrade')
|
2026-08-17 11:47:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-16 23:34:22 +08:00
|
|
|
|
export function apply(ctx: Context) {
|
|
|
|
|
|
const webServer = (ctx as any).webServer
|
|
|
|
|
|
if (!webServer) {
|
2026-08-17 11:47:35 +08:00
|
|
|
|
ctx.logger?.warn('[dsh-worktable] ctx.webServer 不可用(headless profile?),跳过服务端路由')
|
2026-08-16 23:34:22 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-08-17 11:47:35 +08:00
|
|
|
|
|
2026-08-16 23:34:22 +08:00
|
|
|
|
webServer.register({
|
|
|
|
|
|
kind: 'exact',
|
|
|
|
|
|
path: HEALTH_PATH,
|
|
|
|
|
|
handler: (_req: any, res: any) => {
|
2026-08-17 11:47:35 +08:00
|
|
|
|
json(res, 200, { plugin: 'dsh-worktable', version: '0.2.0', ok: true })
|
2026-08-16 23:34:22 +08:00
|
|
|
|
},
|
|
|
|
|
|
})
|
2026-08-17 11:47:35 +08:00
|
|
|
|
|
2026-08-17 12:49:27 +08:00
|
|
|
|
// 本地文件读取(资源管理器点击 .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 }
|
2026-08-17 12:54:40 +08:00
|
|
|
|
const abs = pathResolve(p)
|
2026-08-17 12:49:27 +08:00
|
|
|
|
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',
|
2026-08-17 22:21:13 +08:00
|
|
|
|
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',
|
2026-08-17 12:49:27 +08:00
|
|
|
|
}
|
2026-08-17 23:39:06 +08:00
|
|
|
|
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)
|
2026-08-18 00:04:20 +08:00
|
|
|
|
const rootToken = decodeURIComponent(segs.shift() ?? '')
|
2026-08-17 23:39:06 +08:00
|
|
|
|
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' })
|
2026-08-17 12:49:27 +08:00
|
|
|
|
res.end(data)
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
json(res, 404, { error: String(err) })
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-08-17 11:47:35 +08:00
|
|
|
|
webServer.register({
|
|
|
|
|
|
kind: 'exact',
|
|
|
|
|
|
path: '/api/worktable/fs',
|
|
|
|
|
|
handler: async (req: any, res: any) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const body = await readJsonBody(req)
|
2026-08-17 12:19:08 +08:00
|
|
|
|
const path = typeof body.path === 'string' && body.path
|
|
|
|
|
|
? body.path
|
|
|
|
|
|
: serverCwd(ctx, body.sessionId, body.cwd)
|
2026-08-17 11:47:35 +08:00
|
|
|
|
json(res, 200, await listDirectory(path))
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
json(res, 500, { path: '', entries: [], truncated: false, error: String(err) })
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-08-18 11:11:36 +08:00
|
|
|
|
// 工作区列表(自定义窗口会话分组用):读宿主 ~/.dsh/storages/workspace.json(只读)
|
|
|
|
|
|
webServer.register({
|
|
|
|
|
|
kind: 'exact',
|
|
|
|
|
|
path: '/api/worktable/workspaces',
|
|
|
|
|
|
handler: async (_req: any, res: any) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const file = pathResolve(homedir(), '.dsh', 'storages', 'workspace.json')
|
|
|
|
|
|
const raw = await readFile(file, 'utf8')
|
|
|
|
|
|
json(res, 200, JSON.parse(raw))
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
json(res, 404, { error: String(err) })
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-08-18 00:44:48 +08:00
|
|
|
|
// 本地文件写入(MD 编辑模式保存回磁盘)
|
|
|
|
|
|
webServer.register({
|
|
|
|
|
|
kind: 'exact',
|
|
|
|
|
|
path: '/api/worktable/write',
|
|
|
|
|
|
handler: async (req: any, res: any) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (req.method !== 'POST') { res.writeHead(405); res.end(); return }
|
|
|
|
|
|
const body = await readJsonBody(req)
|
|
|
|
|
|
const p = typeof body.path === 'string' ? body.path : ''
|
|
|
|
|
|
const content = typeof body.content === 'string' ? body.content : ''
|
|
|
|
|
|
if (!p) { json(res, 400, { error: 'missing path' }); return }
|
|
|
|
|
|
if (content.length > 20 * 1024 * 1024) { json(res, 413, { error: 'content too large' }); return }
|
|
|
|
|
|
const abs = pathResolve(p)
|
|
|
|
|
|
await import('node:fs/promises').then((m) => m.writeFile(abs, content, 'utf8'))
|
|
|
|
|
|
json(res, 200, { ok: true })
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
json(res, 500, { error: String(err) })
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-08-18 23:27:16 +08:00
|
|
|
|
// 新建分组:创建目录(仅当父目录已存在,避免递归误建深层垃圾目录)
|
|
|
|
|
|
webServer.register({
|
|
|
|
|
|
kind: 'exact',
|
|
|
|
|
|
path: '/api/worktable/mkdir',
|
|
|
|
|
|
handler: async (req: any, res: any) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (req.method !== 'POST') { res.writeHead(405); res.end(); return }
|
|
|
|
|
|
const body = await readJsonBody(req)
|
|
|
|
|
|
const p = typeof body.path === 'string' ? body.path.trim() : ''
|
|
|
|
|
|
if (!p) { json(res, 400, { error: 'missing path' }); return }
|
|
|
|
|
|
const abs = pathResolve(p)
|
|
|
|
|
|
const fsx = await import('node:fs/promises')
|
|
|
|
|
|
const parent = abs.split(/[\\/]/).slice(0, -1).join('\\') || '\\'
|
|
|
|
|
|
try { await fsx.access(parent) } catch { json(res, 400, { error: 'parent not found' }); return }
|
|
|
|
|
|
await fsx.mkdir(abs)
|
|
|
|
|
|
json(res, 200, { ok: true, path: abs })
|
|
|
|
|
|
} catch (err: any) {
|
|
|
|
|
|
json(res, err?.code === 'EEXIST' ? 200 : 500, err?.code === 'EEXIST' ? { ok: true, exists: true } : { error: String(err) })
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-08-17 11:47:35 +08:00
|
|
|
|
webServer.register({
|
|
|
|
|
|
kind: 'exact',
|
|
|
|
|
|
path: '/api/worktable/git',
|
|
|
|
|
|
handler: async (req: any, res: any) => {
|
|
|
|
|
|
const body = await readJsonBody(req)
|
2026-08-17 12:19:08 +08:00
|
|
|
|
const cwd = serverCwd(ctx, body.sessionId, body.cwd)
|
2026-08-17 11:47:35 +08:00
|
|
|
|
json(res, 200, await gitStatus(cwd))
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-08-17 12:54:40 +08:00
|
|
|
|
setupTerminal(webServer, ctx)
|
2026-08-16 23:34:22 +08:00
|
|
|
|
}
|