pi-web-ui 0.102.0 → 0.103.1
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.
- package/CHANGELOG.md +29 -0
- package/README.md +1 -1
- package/README.zh-CN.md +1 -1
- package/dist/server/agent-service-daemon.js +17 -10
- package/dist/server/agent-service.js +125 -49
- package/dist/server/bash-tool.js +19 -1
- package/dist/server/client-state.js +9 -1
- package/dist/server/dispatch-domain-handlers.js +66 -0
- package/dist/server/edit-soft-tool.js +15 -3
- package/dist/server/files-service.js +636 -14
- package/dist/server/index.js +7 -1
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/read-tool.js +11 -4
- package/dist/server/remote-ssh-service.js +1314 -0
- package/dist/server/scm.js +16 -0
- package/dist/server/terminals.js +65 -21
- package/dist/server/tool-guards.js +18 -6
- package/dist/server/virtual-tui.js +258 -0
- package/dist/server/webui-context.js +109 -4
- package/package.json +7 -1
- package/web/dist/assets/TerminalPanel-Dk8E4DaC.js +3 -0
- package/web/dist/assets/index-CYQdZEbx.css +1 -0
- package/web/dist/assets/index-Pl0V0tEL.js +680 -0
- package/web/dist/index.html +4 -2
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/TerminalPanel-CYGOzbPT.js +0 -3
- package/web/dist/assets/index-CP71PHb6.js +0 -680
- package/web/dist/assets/index-ytRwL6j0.css +0 -1
package/dist/server/scm.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
import { execFile } from "node:child_process";
|
|
10
10
|
import { promisify } from "node:util";
|
|
11
11
|
import { pick } from "./i18n.js";
|
|
12
|
+
import { getGlobalRemoteSshService, isRemoteWorkspaceUri, shellQuotePosix } from "./remote-ssh-service.js";
|
|
12
13
|
const exec = promisify(execFile);
|
|
13
14
|
/** Per-command timeout / output cap — a stuck repo must not hang the panel. */
|
|
14
15
|
const GIT_TIMEOUT_MS = 15_000;
|
|
@@ -16,6 +17,19 @@ const MAX_GIT_OUTPUT = 16 * 1024 * 1024;
|
|
|
16
17
|
/** Run one git command; throws Error with a readable message on failure. */
|
|
17
18
|
async function git(cwd, args, lang) {
|
|
18
19
|
const l = lang?.() ?? "en";
|
|
20
|
+
if (isRemoteWorkspaceUri(cwd)) {
|
|
21
|
+
const sshSvc = getGlobalRemoteSshService();
|
|
22
|
+
if (!sshSvc) {
|
|
23
|
+
throw new Error("Remote SSH service is not initialized");
|
|
24
|
+
}
|
|
25
|
+
const cmd = `git -c core.quotepath=false ${args.map(shellQuotePosix).join(" ")}`;
|
|
26
|
+
const res = await sshSvc.execInWorkspace(cwd, cmd, { timeoutMs: GIT_TIMEOUT_MS });
|
|
27
|
+
if (res.exitCode !== 0) {
|
|
28
|
+
const detail = (res.stderr || res.stdout || "").trim().split("\n")[0];
|
|
29
|
+
throw new Error(detail || pick(l, "git 命令失败", "git command failed", "scm.git.failed"));
|
|
30
|
+
}
|
|
31
|
+
return res.stdout;
|
|
32
|
+
}
|
|
19
33
|
try {
|
|
20
34
|
const { stdout } = await exec("git", ["-c", "core.quotepath=false", ...args], {
|
|
21
35
|
cwd,
|
|
@@ -38,6 +52,8 @@ async function git(cwd, args, lang) {
|
|
|
38
52
|
/** Absolute path of the repo's git dir (handles worktrees/submodules), or
|
|
39
53
|
* null when cwd isn't inside a repository. */
|
|
40
54
|
export async function gitDirOf(cwd) {
|
|
55
|
+
if (isRemoteWorkspaceUri(cwd))
|
|
56
|
+
return null;
|
|
41
57
|
try {
|
|
42
58
|
const { stdout } = await exec("git", ["rev-parse", "--absolute-git-dir"], {
|
|
43
59
|
cwd,
|
package/dist/server/terminals.js
CHANGED
|
@@ -27,8 +27,13 @@ import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
|
27
27
|
import { Type } from "typebox";
|
|
28
28
|
import { BASH_DESCRIPTION, BASH_PARAMETERS, BASH_PROMPT_GUIDELINES, BASH_PROMPT_SNIPPET } from "./tool-prompts.js";
|
|
29
29
|
import { pick } from "./i18n.js";
|
|
30
|
+
import { getGlobalRemoteSshService, isRemoteWorkspaceUri, resolveWorkspaceSessionDir } from "./remote-ssh-service.js";
|
|
30
31
|
/** Location of the command list for a project: <workspaceRoot>/.pi/commands.json */
|
|
31
32
|
export function commandsFilePath(workspaceRoot) {
|
|
33
|
+
if (isRemoteWorkspaceUri(workspaceRoot)) {
|
|
34
|
+
const dataDir = process.env.PI_WEB_DATA_DIR || join(homedir(), ".pi-web");
|
|
35
|
+
return join(resolveWorkspaceSessionDir(dataDir, workspaceRoot), ".pi", "commands.json");
|
|
36
|
+
}
|
|
32
37
|
return join(workspaceRoot, ".pi", "commands.json");
|
|
33
38
|
}
|
|
34
39
|
/** Expand ${pwd} (and ~) in a cwd/command string against the session's cwd. */
|
|
@@ -42,7 +47,9 @@ export function expandPwd(input, pwd) {
|
|
|
42
47
|
}
|
|
43
48
|
/** Resolve a command's directory: default to the session cwd, expand ${pwd}/~, resolve relative paths. */
|
|
44
49
|
export function resolveCommandCwd(cwd, pwd) {
|
|
45
|
-
if (!cwd || cwd.trim() === "")
|
|
50
|
+
if (!cwd || cwd.trim() === "" || cwd.trim() === "${pwd}")
|
|
51
|
+
return pwd;
|
|
52
|
+
if (isRemoteWorkspaceUri(pwd))
|
|
46
53
|
return pwd;
|
|
47
54
|
const expanded = expandPwd(cwd.trim(), pwd);
|
|
48
55
|
return isAbsolute(expanded) ? expanded : resolve(pwd, expanded);
|
|
@@ -931,33 +938,71 @@ export class TerminalManager {
|
|
|
931
938
|
/** Spawn the user's shell as a PTY. Returns false when the spawn failed. */
|
|
932
939
|
spawnShell(id, cwd, cols, rows, title, command, forceBash, agentBash = false, locale) {
|
|
933
940
|
let abs = cwd;
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
941
|
+
let shell;
|
|
942
|
+
let args;
|
|
943
|
+
const targetUri = cwd && cwd.startsWith("ssh://")
|
|
944
|
+
? cwd
|
|
945
|
+
: this.workspaceRoot && this.workspaceRoot.startsWith("ssh://")
|
|
946
|
+
? this.workspaceRoot
|
|
947
|
+
: null;
|
|
948
|
+
if (targetUri) {
|
|
949
|
+
const match = /^ssh:\/\/([^@]+)@([^:]+):(\d+)(\/.*)$/.exec(targetUri);
|
|
950
|
+
if (match) {
|
|
951
|
+
const username = match[1];
|
|
952
|
+
const host = match[2];
|
|
953
|
+
const port = match[3];
|
|
954
|
+
const remotePath = match[4];
|
|
955
|
+
abs = homedir();
|
|
956
|
+
shell = process.platform === "win32" ? "ssh.exe" : "ssh";
|
|
957
|
+
args = ["-t", "-p", port, `${username}@${host}`, `cd "${remotePath}" && exec bash -l || exec sh`];
|
|
958
|
+
}
|
|
959
|
+
else {
|
|
960
|
+
if (!abs)
|
|
961
|
+
abs = homedir();
|
|
962
|
+
else if (!isAbsolute(abs))
|
|
963
|
+
abs = resolve(abs);
|
|
964
|
+
const s = forceBash ? resolveBashShell() : resolveShell();
|
|
965
|
+
shell = s.shell;
|
|
966
|
+
args = s.args;
|
|
942
967
|
}
|
|
943
968
|
}
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
969
|
+
else {
|
|
970
|
+
if (!abs)
|
|
971
|
+
abs = homedir();
|
|
972
|
+
else if (!isAbsolute(abs))
|
|
973
|
+
abs = resolve(abs);
|
|
974
|
+
try {
|
|
975
|
+
if (!existsSync(abs) || !statSync(abs).isDirectory()) {
|
|
976
|
+
this.fail(id, `目录不存在或不是目录:${abs}`, `Directory does not exist or is not a directory: ${abs}`);
|
|
977
|
+
return false;
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
catch {
|
|
981
|
+
this.fail(id, `无法访问终端目录:${abs}`, `Cannot access terminal directory: ${abs}`);
|
|
982
|
+
return false;
|
|
983
|
+
}
|
|
984
|
+
const s = forceBash ? resolveBashShell() : resolveShell();
|
|
985
|
+
shell = s.shell;
|
|
986
|
+
args = s.args;
|
|
947
987
|
}
|
|
948
988
|
// node-pty's spawn-helper may have lost its +x bit since the last repair
|
|
949
989
|
// (e.g. a global npm install replaced the helper while this server runs).
|
|
950
990
|
repairSpawnHelperPermissions();
|
|
951
991
|
let pty;
|
|
952
992
|
try {
|
|
953
|
-
const
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
993
|
+
const sshSvc = targetUri ? getGlobalRemoteSshService() : null;
|
|
994
|
+
if (targetUri && sshSvc) {
|
|
995
|
+
pty = sshSvc.createVirtualPty(targetUri, cols, rows);
|
|
996
|
+
}
|
|
997
|
+
else {
|
|
998
|
+
pty = spawn(shell, args, {
|
|
999
|
+
name: "xterm-256color",
|
|
1000
|
+
cols: Math.max(2, Math.floor(cols) || 80),
|
|
1001
|
+
rows: Math.max(2, Math.floor(rows) || 24),
|
|
1002
|
+
cwd: abs,
|
|
1003
|
+
env: shellEnv(),
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
961
1006
|
}
|
|
962
1007
|
catch (err) {
|
|
963
1008
|
const helper = brokenSpawnHelper();
|
|
@@ -1010,7 +1055,6 @@ export class TerminalManager {
|
|
|
1010
1055
|
this.queueOut(entry, data);
|
|
1011
1056
|
});
|
|
1012
1057
|
pty.onExit(({ exitCode }) => {
|
|
1013
|
-
entry.exited = true;
|
|
1014
1058
|
if (this.terms.get(id) !== entry)
|
|
1015
1059
|
return;
|
|
1016
1060
|
this.exit(id, exitCode);
|
|
@@ -19,6 +19,7 @@ import { delegationDenial } from "./delegate-mode.js";
|
|
|
19
19
|
import { isPathInsideRoot, extractTargetPath } from "./approval-rules.js";
|
|
20
20
|
import { checkDangerousToolCall, pluginApprovalCategory } from "./tool-approval.js";
|
|
21
21
|
import { denialText, } from "./plugin-tool-guard.js";
|
|
22
|
+
import { createRemoteSdkOperations, isRemoteWorkspaceUri, parseRemoteWorkspaceUri, resolveRemoteWorkspacePath, restoreWorkspaceUriFromSessionDir, } from "./remote-ssh-service.js";
|
|
22
23
|
/**
|
|
23
24
|
* 计划模式闸门装饰(通用):套在任何工具定义外层,只在**执行前**判定一次。
|
|
24
25
|
* 与权限沙箱/插件守卫平行且更靠外 —— 计划模式是会话级硬约束,不给任何
|
|
@@ -236,6 +237,13 @@ export function withToolGuard(def, opts) {
|
|
|
236
237
|
}
|
|
237
238
|
/** 校验目标路径是否在工作区(或多根工作区)内。严格规范化防止 ".." 逃逸与 Windows 盘符大小写不一致。 */
|
|
238
239
|
export function isInsideWorkspaceRoots(targetPath, cwd, roots = []) {
|
|
240
|
+
const effCwd = restoreWorkspaceUriFromSessionDir(cwd);
|
|
241
|
+
if (isRemoteWorkspaceUri(effCwd)) {
|
|
242
|
+
const parsed = parseRemoteWorkspaceUri(effCwd);
|
|
243
|
+
if (!parsed)
|
|
244
|
+
return false;
|
|
245
|
+
return resolveRemoteWorkspacePath(parsed.remotePath, targetPath) !== null;
|
|
246
|
+
}
|
|
239
247
|
const abs = resolve(cwd, targetPath);
|
|
240
248
|
const allRoots = [resolve(cwd), ...roots.map((r) => resolve(r))];
|
|
241
249
|
return allRoots.some((r) => isPathInsideRoot(abs, r));
|
|
@@ -245,9 +253,11 @@ export function isInsideWorkspaceRoots(targetPath, cwd, roots = []) {
|
|
|
245
253
|
* `base` = 覆盖基底:第三方扩展注册的同名 write 优先(见 tool-overrides.ts),
|
|
246
254
|
* 省略则是 SDK 内置实现 —— 于是权限门禁叠在扩展实现之上,而不是把它顶掉。
|
|
247
255
|
*/
|
|
248
|
-
export function wrapWriteToolWithPermission(cwd, getPermission, getRoots, getLang, askApproval, getConversationId, getRules, base
|
|
256
|
+
export function wrapWriteToolWithPermission(cwd, getPermission, getRoots, getLang, askApproval, getConversationId, getRules, base) {
|
|
257
|
+
const remoteOps = !base ? createRemoteSdkOperations(cwd) : null;
|
|
258
|
+
const effectiveBase = base ?? createWriteToolDefinition(cwd, remoteOps ? { operations: remoteOps.write } : undefined);
|
|
249
259
|
return {
|
|
250
|
-
...
|
|
260
|
+
...effectiveBase,
|
|
251
261
|
execute: async (toolCallId, params, signal, onUpdate, ctx) => {
|
|
252
262
|
const perm = getPermission();
|
|
253
263
|
if (perm === "read-only") {
|
|
@@ -312,7 +322,7 @@ export function wrapWriteToolWithPermission(cwd, getPermission, getRoots, getLan
|
|
|
312
322
|
}
|
|
313
323
|
}
|
|
314
324
|
}
|
|
315
|
-
const result = (await
|
|
325
|
+
const result = (await effectiveBase.execute(toolCallId, effectiveParams, signal, onUpdate, ctx));
|
|
316
326
|
if (userEdited && result && typeof result === "object") {
|
|
317
327
|
result.details = {
|
|
318
328
|
...result.details,
|
|
@@ -343,9 +353,11 @@ export const EDIT_GUIDELINES = [
|
|
|
343
353
|
/**
|
|
344
354
|
* 为 edit 工具包装会话级权限沙箱与人机协同审批(基底语义同 write)。
|
|
345
355
|
*/
|
|
346
|
-
export function wrapEditToolWithPermission(cwd, getPermission, getRoots, getLang, askApproval, getConversationId, getRules, base
|
|
356
|
+
export function wrapEditToolWithPermission(cwd, getPermission, getRoots, getLang, askApproval, getConversationId, getRules, base) {
|
|
357
|
+
const remoteOps = !base ? createRemoteSdkOperations(cwd) : null;
|
|
358
|
+
const effectiveBase = base ?? createEditToolDefinition(cwd, remoteOps ? { operations: remoteOps.edit } : undefined);
|
|
347
359
|
return {
|
|
348
|
-
...
|
|
360
|
+
...effectiveBase,
|
|
349
361
|
execute: async (toolCallId, params, signal, onUpdate, ctx) => {
|
|
350
362
|
const perm = getPermission();
|
|
351
363
|
if (perm === "read-only") {
|
|
@@ -410,7 +422,7 @@ export function wrapEditToolWithPermission(cwd, getPermission, getRoots, getLang
|
|
|
410
422
|
}
|
|
411
423
|
}
|
|
412
424
|
}
|
|
413
|
-
const result = (await
|
|
425
|
+
const result = (await effectiveBase.execute(toolCallId, effectiveParams, signal, onUpdate, ctx));
|
|
414
426
|
if (userEdited && result && typeof result === "object") {
|
|
415
427
|
result.details = {
|
|
416
428
|
...result.details,
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { CURSOR_MARKER, KeybindingsManager, TUI_KEYBINDINGS, visibleWidth, } from "@earendil-works/pi-tui";
|
|
2
|
+
export { CURSOR_MARKER };
|
|
3
|
+
/**
|
|
4
|
+
* Extract CURSOR_MARKER from rendered lines, calculate row and visual column,
|
|
5
|
+
* and strip the marker from lines. Returns cursor position if found.
|
|
6
|
+
*/
|
|
7
|
+
export function extractCursorPosition(lines) {
|
|
8
|
+
for (let row = 0; row < lines.length; row++) {
|
|
9
|
+
const line = lines[row];
|
|
10
|
+
const markerIndex = line.indexOf(CURSOR_MARKER);
|
|
11
|
+
if (markerIndex !== -1) {
|
|
12
|
+
const beforeMarker = line.slice(0, markerIndex);
|
|
13
|
+
const col = visibleWidth(beforeMarker);
|
|
14
|
+
lines[row] = line.slice(0, markerIndex) + line.slice(markerIndex + CURSOR_MARKER.length);
|
|
15
|
+
return { row, col };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Format rendered component lines into a flicker-free ANSI screen frame for xterm.js.
|
|
22
|
+
* \x1b[H resets cursor to top-left, \x1b[?25l hides cursor during paint,
|
|
23
|
+
* \x1b[K clears line trail, \x1b[J clears below content,
|
|
24
|
+
* and hardware cursor is restored if CURSOR_MARKER was emitted.
|
|
25
|
+
*/
|
|
26
|
+
export function formatAnsiScreen(rawLines, _cols, _rows) {
|
|
27
|
+
const lines = [...rawLines];
|
|
28
|
+
const cursor = extractCursorPosition(lines);
|
|
29
|
+
// Move cursor to (1,1), hide cursor during line updates, clear each line tail,
|
|
30
|
+
// and clear any remaining lines below viewport.
|
|
31
|
+
let out = "\x1b[H\x1b[?25l";
|
|
32
|
+
out += lines.map((l) => l + "\x1b[K").join("\r\n");
|
|
33
|
+
out += "\x1b[J";
|
|
34
|
+
if (cursor) {
|
|
35
|
+
out += `\x1b[${cursor.row + 1};${cursor.col + 1}H\x1b[?25h`;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
out += "\x1b[?25l";
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Minimal in-memory Terminal interface for driving TUI components without an OS PTY.
|
|
44
|
+
*/
|
|
45
|
+
export class VirtualTerminal {
|
|
46
|
+
columns = 80;
|
|
47
|
+
rows = 24;
|
|
48
|
+
kittyProtocolActive = false;
|
|
49
|
+
onInputCb;
|
|
50
|
+
onResizeCb;
|
|
51
|
+
directWrites = [];
|
|
52
|
+
onWriteCb;
|
|
53
|
+
constructor(cols = 80, rows = 24, onWrite) {
|
|
54
|
+
this.columns = cols;
|
|
55
|
+
this.rows = rows;
|
|
56
|
+
this.onWriteCb = onWrite;
|
|
57
|
+
}
|
|
58
|
+
start(onInput, onResize) {
|
|
59
|
+
this.onInputCb = onInput;
|
|
60
|
+
this.onResizeCb = onResize;
|
|
61
|
+
}
|
|
62
|
+
stop() { }
|
|
63
|
+
async drainInput() { }
|
|
64
|
+
write(data) {
|
|
65
|
+
this.directWrites.push(data);
|
|
66
|
+
this.onWriteCb?.(data);
|
|
67
|
+
}
|
|
68
|
+
moveBy() { }
|
|
69
|
+
hideCursor() { }
|
|
70
|
+
showCursor() { }
|
|
71
|
+
clearLine() { }
|
|
72
|
+
clearFromCursor() { }
|
|
73
|
+
clearScreen() { }
|
|
74
|
+
setTitle() { }
|
|
75
|
+
setProgress() { }
|
|
76
|
+
setProgramStatus() { }
|
|
77
|
+
feedInput(data) {
|
|
78
|
+
this.onInputCb?.(data);
|
|
79
|
+
}
|
|
80
|
+
resize(cols, rows) {
|
|
81
|
+
if (this.columns === cols && this.rows === rows)
|
|
82
|
+
return;
|
|
83
|
+
this.columns = cols;
|
|
84
|
+
this.rows = rows;
|
|
85
|
+
this.onResizeCb?.();
|
|
86
|
+
}
|
|
87
|
+
takeDirectWrites() {
|
|
88
|
+
const out = this.directWrites.join("");
|
|
89
|
+
this.directWrites = [];
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Bridges a single `ctx.ui.custom()` component to the web client via WebSocket.
|
|
95
|
+
*/
|
|
96
|
+
export class VirtualTuiBridge {
|
|
97
|
+
id;
|
|
98
|
+
title;
|
|
99
|
+
terminal;
|
|
100
|
+
keybindings;
|
|
101
|
+
component;
|
|
102
|
+
closed = false;
|
|
103
|
+
onRenderCb;
|
|
104
|
+
onCloseCb;
|
|
105
|
+
resolvePromise;
|
|
106
|
+
rejectPromise;
|
|
107
|
+
promise;
|
|
108
|
+
renderScheduled = false;
|
|
109
|
+
constructor(options) {
|
|
110
|
+
this.id = options.id;
|
|
111
|
+
this.title = options.title;
|
|
112
|
+
this.onRenderCb = options.onRender;
|
|
113
|
+
this.onCloseCb = options.onClose;
|
|
114
|
+
this.terminal = new VirtualTerminal(options.cols ?? 80, options.rows ?? 24, (data) => {
|
|
115
|
+
if (!this.closed) {
|
|
116
|
+
this.onRenderCb(data);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
this.keybindings = new KeybindingsManager(TUI_KEYBINDINGS);
|
|
120
|
+
this.promise = new Promise((resolve, reject) => {
|
|
121
|
+
this.resolvePromise = resolve;
|
|
122
|
+
this.rejectPromise = reject;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
get cols() {
|
|
126
|
+
return this.terminal.columns;
|
|
127
|
+
}
|
|
128
|
+
get rows() {
|
|
129
|
+
return this.terminal.rows;
|
|
130
|
+
}
|
|
131
|
+
get isClosed() {
|
|
132
|
+
return this.closed;
|
|
133
|
+
}
|
|
134
|
+
/** Create the TUI facade expected by ExtensionUIContext.custom() factories. */
|
|
135
|
+
createTuiFacade() {
|
|
136
|
+
return {
|
|
137
|
+
terminal: this.terminal,
|
|
138
|
+
mode: "fullscreen",
|
|
139
|
+
requestRender: () => this.requestRender(),
|
|
140
|
+
renderNow: () => this.renderNow(),
|
|
141
|
+
setFocus: (_comp) => { },
|
|
142
|
+
getFocusedComponent: () => this.component ?? null,
|
|
143
|
+
showOverlay: (_comp, _opts) => this.createOverlayHandle(),
|
|
144
|
+
hideOverlay: () => { },
|
|
145
|
+
hasOverlay: () => false,
|
|
146
|
+
invalidate: () => this.component?.invalidate?.(),
|
|
147
|
+
stop: () => this.terminal.stop(),
|
|
148
|
+
start: () => { },
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/** Create OverlayHandle passed to options.onHandle */
|
|
152
|
+
createOverlayHandle() {
|
|
153
|
+
return {
|
|
154
|
+
hide: () => this.cancel(),
|
|
155
|
+
setHidden: () => { },
|
|
156
|
+
isHidden: () => false,
|
|
157
|
+
focus: () => { },
|
|
158
|
+
unfocus: () => { },
|
|
159
|
+
isFocused: () => true,
|
|
160
|
+
getBounds: () => ({
|
|
161
|
+
row: 0,
|
|
162
|
+
col: 0,
|
|
163
|
+
width: this.terminal.columns,
|
|
164
|
+
height: this.terminal.rows,
|
|
165
|
+
}),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
setComponent(comp) {
|
|
169
|
+
this.component = comp;
|
|
170
|
+
}
|
|
171
|
+
renderNow() {
|
|
172
|
+
if (this.closed)
|
|
173
|
+
return;
|
|
174
|
+
this.renderScheduled = false;
|
|
175
|
+
if (!this.component)
|
|
176
|
+
return;
|
|
177
|
+
try {
|
|
178
|
+
const lines = this.component.render(this.terminal.columns);
|
|
179
|
+
const ansi = formatAnsiScreen(lines, this.terminal.columns, this.terminal.rows);
|
|
180
|
+
this.onRenderCb(ansi);
|
|
181
|
+
}
|
|
182
|
+
catch (err) {
|
|
183
|
+
// Best-effort render error protection
|
|
184
|
+
console.error("[virtual-tui] Component render error:", err);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
requestRender() {
|
|
188
|
+
if (this.closed || this.renderScheduled)
|
|
189
|
+
return;
|
|
190
|
+
this.renderScheduled = true;
|
|
191
|
+
queueMicrotask(() => {
|
|
192
|
+
if (this.renderScheduled) {
|
|
193
|
+
this.renderNow();
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
getInitialAnsi() {
|
|
198
|
+
if (!this.component)
|
|
199
|
+
return "";
|
|
200
|
+
try {
|
|
201
|
+
const lines = this.component.render(this.terminal.columns);
|
|
202
|
+
return formatAnsiScreen(lines, this.terminal.columns, this.terminal.rows);
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
return "";
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
feedInput(data) {
|
|
209
|
+
if (this.closed)
|
|
210
|
+
return;
|
|
211
|
+
try {
|
|
212
|
+
this.component?.handleInput?.(data);
|
|
213
|
+
this.terminal.feedInput(data);
|
|
214
|
+
this.requestRender();
|
|
215
|
+
}
|
|
216
|
+
catch (err) {
|
|
217
|
+
console.error("[virtual-tui] Component input error:", err);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
resize(cols, rows) {
|
|
221
|
+
if (this.closed)
|
|
222
|
+
return;
|
|
223
|
+
this.terminal.resize(cols, rows);
|
|
224
|
+
this.component?.invalidate?.();
|
|
225
|
+
this.requestRender();
|
|
226
|
+
}
|
|
227
|
+
done(result) {
|
|
228
|
+
if (this.closed)
|
|
229
|
+
return;
|
|
230
|
+
this.closed = true;
|
|
231
|
+
try {
|
|
232
|
+
this.component?.dispose?.();
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
// ignore dispose errors
|
|
236
|
+
}
|
|
237
|
+
this.onCloseCb();
|
|
238
|
+
this.resolvePromise(result);
|
|
239
|
+
}
|
|
240
|
+
cancel() {
|
|
241
|
+
if (this.closed)
|
|
242
|
+
return;
|
|
243
|
+
this.done(undefined);
|
|
244
|
+
}
|
|
245
|
+
fail(err) {
|
|
246
|
+
if (this.closed)
|
|
247
|
+
return;
|
|
248
|
+
this.closed = true;
|
|
249
|
+
try {
|
|
250
|
+
this.component?.dispose?.();
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
// ignore dispose errors
|
|
254
|
+
}
|
|
255
|
+
this.onCloseCb();
|
|
256
|
+
this.rejectPromise(err);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* webui-context — 扩展 UI 桥:把扩展的 setWidget/setStatus/notify/select/
|
|
3
|
+
* confirm/input 等调用桥接到浏览器(widgets/statuses/notice/dialog 消息)。
|
|
4
|
+
* TUI 专属能力(终端输入、footer/header、自定义组件)为惰性 no-op;
|
|
5
|
+
* select/confirm/input 弹窗经 dialog_response 回传,Esc 视为取消。
|
|
6
|
+
* headless 实例(子代理会话)不接任何浏览器:见 WebUIContext.headless()。
|
|
7
|
+
*
|
|
8
|
+
* 从 agent-service.ts 抽出,行为保持不变。
|
|
9
|
+
*/
|
|
10
|
+
import { initTheme, Theme } from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import { VirtualTuiBridge } from "./virtual-tui.js";
|
|
12
|
+
// Ensure global theme is initialized so extensions calling getSettingsListTheme(),
|
|
13
|
+
// getMarkdownTheme(), or touching global `theme` proxy don't throw.
|
|
14
|
+
try {
|
|
15
|
+
initTheme("dark", false);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
// best effort
|
|
19
|
+
}
|
|
1
20
|
const WIDGET_WIDTH = 80;
|
|
2
21
|
/** ANSI 转义序列(CSI + OSC 两类):扩展 widget/status 文本里常混有 TUI 颜色码
|
|
3
22
|
* (如 pi-powerline-footer),浏览器会把它渲染成字面 `[38;5;244m` 乱码(issue #16)。 */
|
|
@@ -41,12 +60,18 @@ const mockTui = new Proxy({
|
|
|
41
60
|
* 子代理会话用——见该方法的说明)。
|
|
42
61
|
*/
|
|
43
62
|
export class WebUIContext {
|
|
44
|
-
theme
|
|
63
|
+
get theme() {
|
|
64
|
+
const key = Symbol.for("@earendil-works/pi-coding-agent:theme");
|
|
65
|
+
const current = globalThis[key];
|
|
66
|
+
return current ?? mockThemeProxy;
|
|
67
|
+
}
|
|
45
68
|
widgets = new Map();
|
|
46
69
|
lastLines = new Map();
|
|
47
70
|
emit;
|
|
48
71
|
/** headless 实例没有浏览器面板:输出全部丢弃、弹窗直接按取消返回。 */
|
|
49
72
|
headless;
|
|
73
|
+
tuiOverlaySeq = 0;
|
|
74
|
+
activeTuiOverlays = new Map();
|
|
50
75
|
constructor(emit, options = {}) {
|
|
51
76
|
this.headless = options.headless === true;
|
|
52
77
|
this.emit = this.headless ? () => { } : emit;
|
|
@@ -215,6 +240,70 @@ export class WebUIContext {
|
|
|
215
240
|
this.emit({ type: "dialog_closed", id });
|
|
216
241
|
}
|
|
217
242
|
}
|
|
243
|
+
// -- TUI overlay & customs ----------------------------------------------
|
|
244
|
+
custom = async (factory, options) => {
|
|
245
|
+
// headless (subagent / background): no browser is listening.
|
|
246
|
+
// Immediately resolve without hanging to prevent stuck overlayOpen guards.
|
|
247
|
+
if (this.headless) {
|
|
248
|
+
options?.onHandle?.({
|
|
249
|
+
hide: () => { },
|
|
250
|
+
setHidden: () => { },
|
|
251
|
+
isHidden: () => false,
|
|
252
|
+
focus: () => { },
|
|
253
|
+
unfocus: () => { },
|
|
254
|
+
isFocused: () => false,
|
|
255
|
+
getBounds: () => undefined,
|
|
256
|
+
});
|
|
257
|
+
return Promise.resolve(undefined);
|
|
258
|
+
}
|
|
259
|
+
const id = ++this.tuiOverlaySeq;
|
|
260
|
+
const bridge = new VirtualTuiBridge({
|
|
261
|
+
id,
|
|
262
|
+
onRender: (ansi) => {
|
|
263
|
+
this.emit({ type: "tui_overlay_render", id, ansi });
|
|
264
|
+
},
|
|
265
|
+
onClose: () => {
|
|
266
|
+
this.activeTuiOverlays.delete(id);
|
|
267
|
+
this.emit({ type: "tui_overlay_close", id });
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
this.activeTuiOverlays.set(id, bridge);
|
|
271
|
+
if (options?.onHandle) {
|
|
272
|
+
options.onHandle(bridge.createOverlayHandle());
|
|
273
|
+
}
|
|
274
|
+
try {
|
|
275
|
+
const compResult = factory(bridge.createTuiFacade(), this.theme, bridge.keybindings, (result) => bridge.done(result));
|
|
276
|
+
const comp = compResult instanceof Promise ? await compResult : compResult;
|
|
277
|
+
if (comp) {
|
|
278
|
+
bridge.setComponent(comp);
|
|
279
|
+
}
|
|
280
|
+
if (!bridge.isClosed) {
|
|
281
|
+
this.emit({
|
|
282
|
+
type: "tui_overlay_open",
|
|
283
|
+
id,
|
|
284
|
+
cols: bridge.cols,
|
|
285
|
+
rows: bridge.rows,
|
|
286
|
+
initialAnsi: bridge.getInitialAnsi(),
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
catch (err) {
|
|
291
|
+
bridge.fail(err);
|
|
292
|
+
}
|
|
293
|
+
return bridge.promise;
|
|
294
|
+
};
|
|
295
|
+
handleTuiOverlayInput(id, data) {
|
|
296
|
+
const bridge = this.activeTuiOverlays.get(id);
|
|
297
|
+
bridge?.feedInput(data);
|
|
298
|
+
}
|
|
299
|
+
handleTuiOverlayResize(id, cols, rows) {
|
|
300
|
+
const bridge = this.activeTuiOverlays.get(id);
|
|
301
|
+
bridge?.resize(cols, rows);
|
|
302
|
+
}
|
|
303
|
+
handleTuiOverlayCancel(id) {
|
|
304
|
+
const bridge = this.activeTuiOverlays.get(id);
|
|
305
|
+
bridge?.cancel();
|
|
306
|
+
}
|
|
218
307
|
// -- inert TUI-only affordances ------------------------------------------
|
|
219
308
|
onTerminalInput = () => () => { };
|
|
220
309
|
setWorkingMessage = () => { };
|
|
@@ -224,7 +313,6 @@ export class WebUIContext {
|
|
|
224
313
|
setFooter = () => { };
|
|
225
314
|
setHeader = () => { };
|
|
226
315
|
setTitle = () => { };
|
|
227
|
-
custom = (_factory, _done) => new Promise(() => { });
|
|
228
316
|
pasteToEditor = () => { };
|
|
229
317
|
setEditorText = () => { };
|
|
230
318
|
getEditorText = () => "";
|
|
@@ -233,8 +321,20 @@ export class WebUIContext {
|
|
|
233
321
|
setEditorComponent = () => { };
|
|
234
322
|
getEditorComponent = () => undefined;
|
|
235
323
|
getAllThemes = () => [];
|
|
236
|
-
getTheme = () => undefined;
|
|
237
|
-
setTheme = () =>
|
|
324
|
+
getTheme = (_name) => undefined;
|
|
325
|
+
setTheme = (themeOrName) => {
|
|
326
|
+
try {
|
|
327
|
+
const name = typeof themeOrName === "string" ? themeOrName : themeOrName.name;
|
|
328
|
+
if (name) {
|
|
329
|
+
initTheme(name, false);
|
|
330
|
+
return { success: true };
|
|
331
|
+
}
|
|
332
|
+
return { success: false, error: "Theme name missing" };
|
|
333
|
+
}
|
|
334
|
+
catch (err) {
|
|
335
|
+
return { success: false, error: err.message };
|
|
336
|
+
}
|
|
337
|
+
};
|
|
238
338
|
getToolsExpanded = () => false;
|
|
239
339
|
setToolsExpanded = () => { };
|
|
240
340
|
/** Dispose all widgets (extension reload / session teardown). */
|
|
@@ -255,5 +355,10 @@ export class WebUIContext {
|
|
|
255
355
|
this.emit({ type: "dialog_closed", id });
|
|
256
356
|
}
|
|
257
357
|
this.pendingDialogs.clear();
|
|
358
|
+
// Cancel any pending TUI overlays.
|
|
359
|
+
for (const bridge of this.activeTuiOverlays.values()) {
|
|
360
|
+
bridge.cancel();
|
|
361
|
+
}
|
|
362
|
+
this.activeTuiOverlays.clear();
|
|
258
363
|
}
|
|
259
364
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-web-ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.103.1",
|
|
4
4
|
"description": "Just open your browser to get all your work done — browser cockpit for AI coding agents (chat, code, files, terminal, Git in one tab)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
"email": "xingshuyin@users.noreply.github.com"
|
|
9
9
|
},
|
|
10
10
|
"type": "module",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"registry": "https://registry.npmjs.org/"
|
|
14
|
+
},
|
|
11
15
|
"bin": {
|
|
12
16
|
"pi-web-ui": "bin/pi-web-ui.mjs"
|
|
13
17
|
},
|
|
@@ -89,6 +93,7 @@
|
|
|
89
93
|
"electron-updater": "^6.8.9",
|
|
90
94
|
"express": "^5.2.1",
|
|
91
95
|
"node-pty": "^1.1.0",
|
|
96
|
+
"ssh2": "^1.17.0",
|
|
92
97
|
"tar": "^7.5.22",
|
|
93
98
|
"undici": "^8.9.0",
|
|
94
99
|
"ws": "^8.21.3",
|
|
@@ -116,6 +121,7 @@
|
|
|
116
121
|
"@types/node": "^26.6.1",
|
|
117
122
|
"@types/react": "^19.3.0",
|
|
118
123
|
"@types/react-dom": "^19.3.0",
|
|
124
|
+
"@types/ssh2": "^1.15.6",
|
|
119
125
|
"@types/ws": "^8.18.1",
|
|
120
126
|
"@types/yauzl": "^3.4.0",
|
|
121
127
|
"@vitejs/plugin-react": "^6.1.1",
|