pi-web-ui 0.104.4 → 0.105.0

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.
Files changed (52) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/README.md +1 -1
  3. package/README.zh-CN.md +2 -2
  4. package/bin/pi-web-ui.mjs +9 -3
  5. package/dist/server/agent-service-daemon.js +7 -7
  6. package/dist/server/agent-service.js +497 -109
  7. package/dist/server/approval-default-rules.js +15 -0
  8. package/dist/server/client-state.js +29 -0
  9. package/dist/server/dispatch-domain-handlers.js +35 -7
  10. package/dist/server/dsh/dsh-agent-service.js +88 -33
  11. package/dist/server/dsh/dsh-serialize.js +16 -1
  12. package/dist/server/ensure-bash.js +7 -2
  13. package/dist/server/file-transfer-routes.js +114 -1
  14. package/dist/server/index.js +347 -68
  15. package/dist/server/load-tools-tool.js +2 -1
  16. package/dist/server/locales.js +5 -0
  17. package/dist/server/markers/marker.js +28 -0
  18. package/dist/server/mcp-bridge.js +20 -6
  19. package/dist/server/pkg-info.js +69 -0
  20. package/dist/server/plugin-facilities.js +18 -9
  21. package/dist/server/plugin-manifest-validate.js +3 -0
  22. package/dist/server/plugin-updater.js +11 -5
  23. package/dist/server/plugins.js +3 -12
  24. package/dist/server/present-files-tool.js +47 -1
  25. package/dist/server/preset-fields.js +14 -0
  26. package/dist/server/preset-share.js +5 -12
  27. package/dist/server/prompt-composer.js +1 -1
  28. package/dist/server/remote-ssh-service.js +158 -5
  29. package/dist/server/serialize.js +15 -4
  30. package/dist/server/session-parser.js +138 -2
  31. package/dist/server/settings-service.js +19 -0
  32. package/dist/server/terminals.js +7 -1
  33. package/dist/server/text-quote.js +64 -2
  34. package/dist/server/tool-manager.js +10 -0
  35. package/dist/server/tool-prompts.js +1 -0
  36. package/dist/server/update-check.js +98 -24
  37. package/dist/server/workspace-snapshot.js +251 -13
  38. package/dist/server/worktree-service.js +262 -0
  39. package/dist/server/worktree-tool.js +173 -0
  40. package/package.json +3 -1
  41. package/plugin-sdk/index.d.ts +14 -1
  42. package/themes/zhujian-dark.css +16 -4
  43. package/themes/zhujian.css +16 -4
  44. package/themes/zhupi-dark.css +16 -4
  45. package/themes/zhupi.css +16 -4
  46. package/web/dist/assets/{TerminalPanel-B7x99tkg.js → TerminalPanel-B9exeXrl.js} +1 -1
  47. package/web/dist/assets/index-BQC6lADz.css +1 -0
  48. package/web/dist/assets/index-D79IKG33.js +690 -0
  49. package/web/dist/index.html +21 -21
  50. package/web/dist/sw.js +1 -1
  51. package/web/dist/assets/index-DOrLcjLY.js +0 -680
  52. package/web/dist/assets/index-_H2BS7OX.css +0 -1
@@ -1,9 +1,10 @@
1
1
  import express, {} from "express";
2
2
  import { mkdtemp, mkdir, open, realpath, rm, stat } from "node:fs/promises";
3
- import { join, dirname } from "node:path";
3
+ import { join, dirname, posix } from "node:path";
4
4
  import { tmpdir } from "node:os";
5
5
  import { createArchive, extractArchive, safeDestination, safeEntryPath } from "./file-archives.js";
6
6
  import { isAbsoluteWirePath, wireToAbs, workspacePath } from "./files-service.js";
7
+ import { getGlobalRemoteSshService, isRemoteWorkspaceUri, parseRemoteWorkspaceUri, resolveRemoteWorkspacePath, shellQuotePosix, } from "./remote-ssh-service.js";
7
8
  /** Registered behind the host's authentication middleware; shared by Pi and DSH. */
8
9
  export function registerFileTransferRoutes(app, getCwd, temporaryRoot = tmpdir()) {
9
10
  const busy = new Set();
@@ -24,6 +25,29 @@ export function registerFileTransferRoutes(app, getCwd, temporaryRoot = tmpdir()
24
25
  throw new Error("Path outside workspace");
25
26
  return resolved.abs;
26
27
  }
28
+ function remotePath(cwd, raw, allowRoot = false) {
29
+ if (typeof raw !== "string" || raw === "@root")
30
+ throw new Error("Invalid path");
31
+ const parsed = parseRemoteWorkspaceUri(cwd);
32
+ if (!parsed)
33
+ throw new Error("Invalid remote workspace");
34
+ const trimmed = raw.trim();
35
+ if (!trimmed) {
36
+ if (!allowRoot)
37
+ throw new Error("Invalid path");
38
+ return parsed.remotePath;
39
+ }
40
+ if (trimmed.startsWith("/")) {
41
+ const abs = posix.normalize(trimmed);
42
+ if (abs === "/" && !allowRoot)
43
+ throw new Error("Invalid path");
44
+ return abs;
45
+ }
46
+ const w = resolveRemoteWorkspacePath(parsed.remotePath, trimmed);
47
+ if (!w || (!allowRoot && w.rel === ""))
48
+ throw new Error("Path outside workspace");
49
+ return w.abs;
50
+ }
27
51
  // A cross-origin form cannot set this header. No CORS permission is granted.
28
52
  app.use("/api/file-transfer", (req, res, next) => {
29
53
  if (req.headers["x-file-operation"] !== "1") {
@@ -44,6 +68,69 @@ export function registerFileTransferRoutes(app, getCwd, temporaryRoot = tmpdir()
44
68
  }
45
69
  busy.add(cwd);
46
70
  acquired = true;
71
+ if (isRemoteWorkspaceUri(cwd)) {
72
+ const sshSvc = getGlobalRemoteSshService();
73
+ if (!sshSvc)
74
+ throw new Error("Remote SSH service not available");
75
+ const remoteSource = remotePath(cwd, req.body?.path);
76
+ if (req.body?.action === "extract") {
77
+ const remoteDest = remotePath(cwd, req.body?.destination, true);
78
+ const lower = remoteSource.toLowerCase();
79
+ const cmd = lower.endsWith(".zip")
80
+ ? `mkdir -p ${shellQuotePosix(remoteDest)} && unzip -o ${shellQuotePosix(remoteSource)} -d ${shellQuotePosix(remoteDest)}`
81
+ : `mkdir -p ${shellQuotePosix(remoteDest)} && tar -xf ${shellQuotePosix(remoteSource)} -C ${shellQuotePosix(remoteDest)}`;
82
+ const r = await sshSvc.execInWorkspace(cwd, cmd, { cwd: "/" });
83
+ if (r.exitCode !== 0)
84
+ throw new Error(r.stderr || r.stdout || "Remote extract failed");
85
+ res.json({ extracted: 1, skipped: [] });
86
+ return;
87
+ }
88
+ if (req.body?.action === "compress") {
89
+ const parentDir = posix.dirname(remoteSource) || "/";
90
+ const baseName = posix.basename(remoteSource);
91
+ const archiveName = `${baseName}.tar.gz`;
92
+ const remoteDest = posix.join(parentDir, archiveName);
93
+ const cmd = `tar -czf ${shellQuotePosix(remoteDest)} -C ${shellQuotePosix(parentDir)} ${shellQuotePosix(baseName)}`;
94
+ const r = await sshSvc.execInWorkspace(cwd, cmd, { cwd: "/" });
95
+ if (r.exitCode !== 0)
96
+ throw new Error(r.stderr || r.stdout || "Remote compress failed");
97
+ res.json({ name: archiveName });
98
+ return;
99
+ }
100
+ if (req.body?.action === "download") {
101
+ const parentDir = posix.dirname(remoteSource) || "/";
102
+ const baseName = posix.basename(remoteSource);
103
+ const archiveName = `${baseName}.tar.gz`;
104
+ const remoteTmp = `/tmp/pi-web-dl-${Date.now()}-${Math.random().toString(36).slice(2, 8)}.tar.gz`;
105
+ try {
106
+ const cmd = `tar -czf ${shellQuotePosix(remoteTmp)} -C ${shellQuotePosix(parentDir)} ${shellQuotePosix(baseName)}`;
107
+ const r = await sshSvc.execInWorkspace(cwd, cmd, { cwd: "/" });
108
+ if (r.exitCode !== 0)
109
+ throw new Error(r.stderr || r.stdout || "Remote archive creation failed");
110
+ const st = await sshSvc.sftpStat(cwd, remoteTmp);
111
+ if (st.size > 200 * 1024 * 1024) {
112
+ throw new Error("Temporary download exceeds 200 MiB; compress in place and download the archive instead");
113
+ }
114
+ const asciiFallback = archiveName.replace(/[^\x20-\x7e]|["\\]/g, "_");
115
+ const encoded = encodeURIComponent(archiveName).replace(/['()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
116
+ res.type(archiveName);
117
+ res.setHeader("Content-Disposition", `attachment; filename="${asciiFallback}"; filename*=UTF-8''${encoded}`);
118
+ res.setHeader("Content-Length", String(st.size));
119
+ const stream = await sshSvc.sftpCreateReadStream(cwd, remoteTmp);
120
+ await new Promise((ok, fail) => {
121
+ stream.on("error", fail);
122
+ res.on("finish", ok);
123
+ res.on("close", ok);
124
+ stream.pipe(res);
125
+ });
126
+ }
127
+ finally {
128
+ await sshSvc.execInWorkspace(cwd, `rm -f ${shellQuotePosix(remoteTmp)}`, { cwd: "/" }).catch(() => { });
129
+ }
130
+ return;
131
+ }
132
+ throw new Error("Invalid archive action");
133
+ }
47
134
  const source = path(cwd, req.body?.path);
48
135
  if (req.body?.action === "extract") {
49
136
  const result = await extractArchive(source, path(cwd, req.body.destination), req.body.policy);
@@ -85,6 +172,32 @@ export function registerFileTransferRoutes(app, getCwd, temporaryRoot = tmpdir()
85
172
  app.post("/api/file-transfer/upload", express.raw({ type: "application/octet-stream", limit: "32mb" }), async (req, res) => {
86
173
  try {
87
174
  const cwd = root(req);
175
+ if (isRemoteWorkspaceUri(cwd)) {
176
+ const sshSvc = getGlobalRemoteSshService();
177
+ if (!sshSvc)
178
+ throw new Error("Remote SSH service not available");
179
+ const remoteDir = remotePath(cwd, typeof req.query.dir === "string" ? req.query.dir : "", true);
180
+ const name = safeEntryPath(typeof req.query.name === "string" ? req.query.name : "");
181
+ const remoteDest = posix.join(remoteDir, name.replace(/\\/g, "/"));
182
+ if (!Buffer.isBuffer(req.body))
183
+ throw new Error("Expected a binary file");
184
+ if (req.query.kind === "directory") {
185
+ if (req.body.length)
186
+ throw new Error("Directory requests cannot contain file data");
187
+ await sshSvc.sftpMkdirRecursive(cwd, remoteDest);
188
+ res.json({ name });
189
+ return;
190
+ }
191
+ const exists = await sshSvc.sftpStat(cwd, remoteDest).catch(() => null);
192
+ if (exists) {
193
+ const e = new Error("File already exists");
194
+ e.code = "EEXIST";
195
+ throw e;
196
+ }
197
+ await sshSvc.sftpWriteFile(cwd, remoteDest, req.body, { createParents: true });
198
+ res.json({ name });
199
+ return;
200
+ }
88
201
  const dir = await realpath(path(cwd, req.query.dir));
89
202
  const name = safeEntryPath(typeof req.query.name === "string" ? req.query.name : "");
90
203
  const dest = await safeDestination(dir, name);