// src/index.ts 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"; // template/dshell.css var dshell_default = `/* dsh-worktable \u539F\u751F\u76AE\u80A4 \xB7 DSH \u8BBE\u8BA1\u7CFB\u7EDF\u7EC4\u4EF6\u5E93 \u7528\u6CD5\uFF1A \u6240\u6709\u989C\u8272\u8D70 DSH \u4E3B\u9898\u53D8\u91CF\uFF08--dsw-alias-*\uFF09\uFF0C\u81EA\u52A8\u9002\u914D\u660E\u6697\u4E3B\u9898\u3002 */ :root { color-scheme: dark; } * { box-sizing: border-box; } html, body { margin: 0; padding: 0; } body { background: var(--dsw-alias-bg-base, #0b0e14); color: var(--dsw-alias-label-primary, #e6e8eb); font-family: -apple-system, "Segoe UI", "Microsoft YaHei", sans-serif; font-size: 13px; line-height: 1.6; } .dshell { display: flex; flex-direction: column; gap: 12px; padding: 14px 16px; min-height: 100%; } /* \u6587\u5B57\u5C42\u7EA7 */ .dshell-title { margin: 0; font-size: 16px; font-weight: 600; color: var(--dsw-alias-label-primary, #e6e8eb); } .dshell-sub { margin: 0; font-size: 12px; color: var(--dsw-alias-label-secondary, #9aa4b2); } .dshell-muted { color: var(--dsw-alias-label-tertiary, #6b7280); font-size: 11.5px; } /* \u5361\u7247 */ .dshell-card { border: 1px solid var(--dsw-alias-border-l1, #262b36); border-radius: 10px; background: var(--dsw-alias-fill-l1, rgba(255,255,255,.02)); padding: 12px 14px; } .dshell-card + .dshell-card { margin-top: 10px; } /* \u6309\u94AE\uFF08\u7EFF\u8272\u4E3B\u6309\u94AE / \u5E7D\u7075\u6309\u94AE / \u5371\u9669\uFF09 */ .dshell-btn { display: inline-flex; align-items: center; gap: 6px; padding: 6px 14px; border-radius: 999px; border: 1px solid transparent; background: #3fb950; color: #0b0e14; font: inherit; font-size: 12.5px; font-weight: 600; cursor: pointer; } .dshell-btn:hover { filter: brightness(1.08); } .dshell-btnGhost { background: transparent; border-color: var(--dsw-alias-border-l1, #262b36); color: var(--dsw-alias-label-secondary, #9aa4b2); } .dshell-btnGhost:hover { color: var(--dsw-alias-label-primary, #e6e8eb); border-color: var(--dsw-alias-border-l2, #3a4150); } .dshell-btnDanger { background: transparent; border-color: #f85149; color: #f85149; } /* \u72B6\u6001\u5FBD\u6807\uFF08\u5706\u70B9 + \u6587\u5B57\uFF1B\u7EFF=\u5DF2\u5B8C\u6210 \u9EC4=\u5F85\u529E/\u5F85\u53D1\u5E03 \u7070=\u672A\u5F00\u59CB\uFF09 */ .dshell-badge { display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 999px; border: 1px solid var(--dsw-alias-border-l1, #262b36); font-size: 11.5px; color: var(--dsw-alias-label-secondary, #9aa4b2); background: var(--dsw-alias-fill-l1, rgba(255,255,255,.03)); } .dshell-badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: var(--dsw-alias-label-tertiary, #6b7280); } .dshell-badgeDone { color: #3fb950; border-color: rgba(63,185,80,.4); } .dshell-badgeDone::before { background: #3fb950; box-shadow: 0 0 5px #3fb950; } .dshell-badgeWait { color: #d29922; border-color: rgba(210,153,34,.4); } .dshell-badgeWait::before { background: #d29922; box-shadow: 0 0 5px #d29922; } .dshell-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--dsw-alias-label-tertiary, #6b7280); } .dshell-dotDone { background: #3fb950; box-shadow: 0 0 5px #3fb950; } .dshell-dotWait { background: #d29922; box-shadow: 0 0 5px #d29922; } /* \u6807\u7B7E\u9875 */ .dshell-tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--dsw-alias-border-l1, #262b36); } .dshell-tab { padding: 7px 12px; font-size: 12.5px; color: var(--dsw-alias-label-secondary, #9aa4b2); cursor: pointer; border: none; background: none; font: inherit; border-bottom: 2px solid transparent; margin-bottom: -1px; } .dshell-tabOn { color: var(--dsw-alias-label-primary, #e6e8eb); border-bottom-color: var(--dsw-alias-state-accent-primary, #4f8ef7); } /* \u5217\u8868 */ .dshell-list { display: flex; flex-direction: column; gap: 6px; } .dshell-listItem { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 12px; border: 1px solid var(--dsw-alias-border-l1, #262b36); border-radius: 8px; background: var(--dsw-alias-fill-l1, rgba(255,255,255,.02)); cursor: pointer; } .dshell-listItem:hover { border-color: var(--dsw-alias-border-l2, #3a4150); background: var(--dsw-alias-fill-l1, rgba(255,255,255,.05)); } .dshell-listItemTitle { font-size: 12.5px; color: var(--dsw-alias-label-primary, #e6e8eb); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .dshell-listItemMeta { flex: none; font-size: 11px; color: var(--dsw-alias-label-tertiary, #6b7280); } /* \u7F51\u683C / \u7EDF\u8BA1 */ .dshell-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; } .dshell-stat { padding: 10px 12px; border: 1px solid var(--dsw-alias-border-l1, #262b36); border-radius: 10px; background: var(--dsw-alias-fill-l1, rgba(255,255,255,.02)); } .dshell-statLabel { font-size: 11px; color: var(--dsw-alias-label-secondary, #9aa4b2); } .dshell-statValue { font-size: 20px; font-weight: 600; color: var(--dsw-alias-label-primary, #e6e8eb); } .dshell-statDelta { font-size: 11px; color: #3fb950; } /* \u8FDB\u5EA6\u6761 */ .dshell-progress { height: 6px; border-radius: 3px; background: var(--dsw-alias-fill-l1, rgba(255,255,255,.06)); overflow: hidden; } .dshell-progressBar { height: 100%; border-radius: 3px; background: #3fb950; } /* \u8F93\u5165 */ .dshell-input, .dshell-textarea { width: 100%; padding: 7px 10px; border: 1px solid var(--dsw-alias-border-l1, #262b36); border-radius: 8px; background: var(--dsw-alias-fill-l1, rgba(255,255,255,.03)); color: var(--dsw-alias-label-primary, #e6e8eb); font: inherit; font-size: 12.5px; outline: none; } .dshell-input:focus, .dshell-textarea:focus { border-color: var(--dsw-alias-state-accent-primary, #4f8ef7); } /* \u8868\u683C */ .dshell-table { width: 100%; border-collapse: collapse; font-size: 12px; } .dshell-table th, .dshell-table td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--dsw-alias-border-l1, #262b36); } .dshell-table th { color: var(--dsw-alias-label-secondary, #9aa4b2); font-weight: 500; } /* \u952E\u503C\u5BF9 */ .dshell-kv { display: flex; flex-direction: column; gap: 6px; } .dshell-kvRow { display: flex; justify-content: space-between; gap: 10px; font-size: 12px; } .dshell-kvKey { color: var(--dsw-alias-label-secondary, #9aa4b2); } .dshell-kvValue { color: var(--dsw-alias-label-primary, #e6e8eb); text-align: right; } /* \u5206\u5272\u7EBF */ .dshell-divider { height: 1px; background: var(--dsw-alias-border-l1, #262b36); margin: 6px 0; } /* \u6EDA\u52A8\u6761 */ ::-webkit-scrollbar { width: 10px; height: 10px; } ::-webkit-scrollbar-thumb { background: rgba(255,255,255,.14); border-radius: 5px; } ::-webkit-scrollbar-track { background: transparent; } `; // template/dshell.html var dshell_default2 = '\n\n\n\n \n \n \u6211\u7684\u7A97\u53E3\n \n\n\n
\n \n

\u7A97\u53E3\u6807\u9898

\n

\u4E00\u53E5\u8BDD\u8BF4\u660E\u8FD9\u4E2A\u7A97\u53E3\u505A\u4EC0\u4E48\u3002

\n\n \n
\n \u5DF2\u5B8C\u6210\n \u8FDB\u884C\u4E2D\n \u672A\u5F00\u59CB\n
\n\n \n
\n \n \n \n
\n\n \n
\n
\n
\u603B\u6570
\n
128
\n
+12.4%
\n
\n
\n
\u8FDB\u884C\u4E2D
\n
7
\n
\n
\n
\u5DF2\u5B8C\u6210
\n
121
\n
\n
\n\n \n
\n
\n \u6761\u76EE\u4E00\uFF1A\u793A\u4F8B\u5185\u5BB9\u6807\u9898\n \u6628\u5929\n
\n
\n \u6761\u76EE\u4E8C\uFF1A\u793A\u4F8B\u5185\u5BB9\u6807\u9898\n \u5DF2\u53D1\u5E03\n
\n
\n\n \n
\n

\u8BE6\u60C5

\n
\n
\u5B57\u6BB5 A\u503C A
\n
\u5B57\u6BB5 B\u503C B
\n
\n
\n
\n
\n\n \n
\n \n \n
\n
\n\n\n'; // src/index.ts var name = "dsh-worktable"; var inject = ["webServer", "sessions"]; var HEALTH_PATH = "/api/worktable/health"; var MAX_ENTRIES = 500; var FILE_TYPES = { 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" }; var SITE_PREFIX = "/api/worktable/site"; var TEMPLATE_PREFIX = "/api/worktable/template"; function loadPkg(pkg) { const starts = /* @__PURE__ */ new Set(); 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 = start; while (dir && dir !== pathResolve(dir, "..")) { try { const req = createRequire(pathToFileURL(pathResolve(dir, "__wt_probe__.js")).href); return req(pkg); } catch { } dir = pathResolve(dir, ".."); } } 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; } function serverCwd(ctx, sessionId, clientCwd) { 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, status, body) { res.writeHead(status, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" }); res.end(JSON.stringify(body)); } async function readJsonBody(req) { const chunks = []; 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 {}; } } async function listDirectory(path) { 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, void 0, { sensitivity: "base" }); }); const truncated = entries.length > MAX_ENTRIES; return { path: abs, entries: truncated ? entries.slice(0, MAX_ENTRIES) : entries, truncated }; } function gitExec(args, cwd) { return new Promise((resolvePromise, reject) => { execFile("git", args, { cwd, windowsHide: true, maxBuffer: 4 * 1024 * 1024 }, (err, stdout) => { if (err) reject(err); else resolvePromise(stdout); }); }); } async function gitStatus(cwd) { 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: void 0, entries: [] }; } } function setupTerminal(webServer, ctx) { 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] \u7EC8\u7AEF\u8DEF\u7531\u672A\u6CE8\u518C\uFF1Aws/node-pty \u4E0D\u53EF\u7528"); return; } const WebSocketServer = wsMod.WebSocketServer ?? wsMod.default?.WebSocketServer; if (!WebSocketServer) return; const pty = ptyMod.default ?? ptyMod; const wss = new WebSocketServer({ noServer: true }); const spawnShell = () => process.platform === "win32" ? { cmd: "powershell.exe", args: ["-NoLogo", "-NoProfile"] } : { cmd: process.env.SHELL || "/bin/bash", args: [] }; const clampDim = (v, fallback) => Math.min(1024, Math.max(2, Number.isFinite(v) ? v : fallback)); ctx.effect(() => webServer.registerUpgrade({ path: "/api/worktable/term", handler: (req, socket, head) => { wss.handleUpgrade(req, socket, head, (ws) => { const u = new URL(req.url ?? "/", "http://dsh.internal"); const cwd = serverCwd(ctx, u.searchParams.get("sessionId") || void 0, u.searchParams.get("cwd") || void 0); const cols = clampDim(Number(u.searchParams.get("cols")), 80); const rows = clampDim(Number(u.searchParams.get("rows")), 24); let term = 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] \u7EC8\u7AEF\u542F\u52A8\u5931\u8D25\uFF1A" + String(err)); } catch { } try { ws.close(); } catch { } return; } term.onData((d) => { try { ws.send(d); } catch { } }); term.onExit(() => { try { ws.close(); } catch { } }); ws.on("message", (raw) => { 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"); } function apply(ctx) { const webServer = ctx.webServer; if (!webServer) { ctx.logger?.warn("[dsh-worktable] ctx.webServer \u4E0D\u53EF\u7528\uFF08headless profile\uFF1F\uFF09\uFF0C\u8DF3\u8FC7\u670D\u52A1\u7AEF\u8DEF\u7531"); return; } webServer.register({ kind: "exact", path: HEALTH_PATH, handler: (_req, res) => { json(res, 200, { plugin: "dsh-worktable", version: "0.2.0", ok: true }); } }); webServer.register({ kind: "exact", path: "/api/worktable/file", handler: async (req, res) => { 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 = { 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) }); } } }); webServer.register({ kind: "prefix", path: TEMPLATE_PREFIX, handler: (req, res) => { try { if (req.method !== "GET") { res.writeHead(405); res.end(); return; } const pathname = new URL(req.url ?? "/", "http://dsh.internal").pathname; const rel = pathname.slice(TEMPLATE_PREFIX.length); if (rel === "/dshell.css") { res.writeHead(200, { "content-type": "text/css; charset=utf-8", "cache-control": "no-store" }); res.end(dshell_default); } else { res.writeHead(200, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" }); res.end(dshell_default2); } } catch (err) { res.writeHead(404); res.end(String(err)); } } }); webServer.register({ kind: "prefix", path: SITE_PREFIX, handler: async (req, res) => { 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 = decodeURIComponent(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, res) => { 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/workspaces", handler: async (_req, res) => { try { const file = pathResolve(homedir(), ".dsh", "storages", "workspace.json"); const raw = await readFile(file, "utf8"); json(res, 200, JSON.parse(raw.charCodeAt(0) === 65279 ? raw.slice(1) : raw)); } catch (err) { json(res, 404, { error: String(err) }); } } }); webServer.register({ kind: "exact", path: "/api/worktable/write", handler: async (req, res) => { 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) }); } } }); webServer.register({ kind: "exact", path: "/api/worktable/mkdir", handler: async (req, res) => { 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) { json(res, err?.code === "EEXIST" ? 200 : 500, err?.code === "EEXIST" ? { ok: true, exists: true } : { error: String(err) }); } } }); webServer.register({ kind: "exact", path: "/api/worktable/git", handler: async (req, res) => { const body = await readJsonBody(req); const cwd = serverCwd(ctx, body.sessionId, body.cwd); json(res, 200, await gitStatus(cwd)); } }); setupTerminal(webServer, ctx); } export { HEALTH_PATH, apply, inject, name }; //# sourceMappingURL=index.js.map