pi-sub-view 0.0.0-stage → 0.5.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.
@@ -0,0 +1,104 @@
1
+ // 终端 inspector 的纯规划层:探测宿主终端、组装 argv,绝不 spawn(真启动在 lib/inspector-plugin.mjs)。
2
+ //
3
+ // 约定(PLAN §8.2):
4
+ // - 参数一律 argv 数组拼接,不经 shell;唯一例外是 xfce4-terminal 的 -x,需要整条命令字符串。
5
+ // - GUI 终端只有存在图形会话(WAYLAND_DISPLAY 或 DISPLAY 非空)时才算可用;
6
+ // gate/CI 等无图形会话的进程里 available 必须为 false,不能只看二进制存在。
7
+ // - 优先级:ptyxis → gnome-terminal → kgx → kitty → foot → wezterm → alacritty →
8
+ // konsole → xfce4-terminal → x-terminal-emulator。
9
+ import fs from "node:fs";
10
+ import path from "node:path";
11
+
12
+ const SAFE_ARG = /^[A-Za-z0-9_@%+=:,./-]+$/;
13
+
14
+ /** POSIX 单引号转义:安全字符原样返回,空串与含特殊字符的参数用单引号包裹。 */
15
+ export function shellQuote(value) {
16
+ const text = String(value);
17
+ if (text === "") return "''";
18
+ if (SAFE_ARG.test(text)) return text;
19
+ return `'${text.replace(/'/g, "'\\''")}'`;
20
+ }
21
+
22
+ /** 把 argv 还原成可读、可交给 shell 的命令字符串(只用于展示与 xfce4-terminal -x)。 */
23
+ export function formatCommand(argv = []) {
24
+ return argv.map((arg) => shellQuote(arg)).join(" ");
25
+ }
26
+
27
+ /** 未注入 hasBinary 时的本机探测:沿 PATH 找可执行文件(读 fs,不 spawn)。PATH 取传入 env,缺省才回落宿主。 */
28
+ export function defaultHasBinary(bin, env = process.env) {
29
+ if (typeof bin !== "string" || bin === "") return false;
30
+ const searchPath = env?.PATH ?? process.env.PATH ?? "";
31
+ const candidates = bin.includes(path.sep)
32
+ ? [bin]
33
+ : searchPath.split(path.delimiter).filter(Boolean).map((dir) => path.join(dir, bin));
34
+ for (const candidate of candidates) {
35
+ try {
36
+ fs.accessSync(candidate, fs.constants.X_OK);
37
+ return true;
38
+ } catch {
39
+ // 继续找下一个候选目录
40
+ }
41
+ }
42
+ return false;
43
+ }
44
+
45
+ /** 是否存在图形会话:没有它起不了 GUI 终端,任何插件都不可用。 */
46
+ function hasGuiSession(env) {
47
+ return Boolean((env?.WAYLAND_DISPLAY ?? "").trim() || (env?.DISPLAY ?? "").trim());
48
+ }
49
+
50
+ // build 接收完整命令 [executable, ...args],返回终端的 argv(不含终端自身二进制)。
51
+ const TERMINALS = [
52
+ // 标题固定为 sub:<runId 前 8 位>,只在调用方给了 title 时加 --title。
53
+ { id: "ptyxis", bin: "ptyxis", build: (cmd, title) => ["--tab", ...(title ? ["--title", title] : []), "--", ...cmd] },
54
+ { id: "gnome-terminal", bin: "gnome-terminal", build: (cmd) => ["--tab", "--", ...cmd] },
55
+ { id: "kgx", bin: "kgx", build: (cmd) => ["--tab", "--", ...cmd] },
56
+ { id: "kitty", bin: "kitty", build: (cmd, title) => [...(title ? ["--title", title] : []), ...cmd] },
57
+ { id: "foot", bin: "foot", build: (cmd) => [...cmd] },
58
+ { id: "wezterm", bin: "wezterm", build: (cmd) => ["start", "--", ...cmd] },
59
+ { id: "alacritty", bin: "alacritty", build: (cmd) => ["-e", ...cmd] },
60
+ { id: "konsole", bin: "konsole", build: (cmd) => ["--new-tab", "-e", ...cmd] },
61
+ // xfce4-terminal 的 -x 只吃一条命令字符串,这里按契约 formatCommand 成单参数。
62
+ { id: "xfce4-terminal", bin: "xfce4-terminal", build: (cmd) => ["--tab", "-x", formatCommand(cmd)] },
63
+ { id: "x-terminal-emulator", bin: "x-terminal-emulator", build: (cmd) => ["-e", ...cmd] },
64
+ ];
65
+
66
+ /** 按内置优先级找宿主终端;无图形会话或一个都没装时返回 undefined。 */
67
+ export function detectTerminal(env = {}, { hasBinary, platform = process.platform } = {}) {
68
+ if (platform !== "linux" && platform !== "darwin") return undefined;
69
+ if (!hasGuiSession(env)) return undefined;
70
+ const probe = hasBinary ?? ((bin) => defaultHasBinary(bin, env));
71
+ for (const terminal of TERMINALS) {
72
+ if (probe(terminal.bin)) return { id: terminal.id, bin: terminal.bin };
73
+ }
74
+ return undefined;
75
+ }
76
+
77
+ /**
78
+ * 规划一次 inspector 终端启动;纯函数,不 spawn。
79
+ * 返回 { available:true, terminal:{id,bin}, argv, displayCommand } 或 { available:false, reason }。
80
+ * cwd 不进 argv(各终端方言不一),由调用方作为 spawn 的 cwd 传入。
81
+ */
82
+ export function planTerminalLaunch({ env = {}, command, hasBinary, platform, cwd, title } = {}) {
83
+ if (!command || typeof command.executable !== "string" || command.executable === "") {
84
+ return { available: false, reason: "missing inspector command" };
85
+ }
86
+ const terminal = detectTerminal(env, {
87
+ hasBinary: hasBinary ?? ((bin) => defaultHasBinary(bin, env)),
88
+ platform,
89
+ });
90
+ if (!terminal) {
91
+ const reason = hasGuiSession(env) ? "no supported terminal emulator" : "no GUI session (WAYLAND_DISPLAY/DISPLAY unset)";
92
+ return { available: false, reason };
93
+ }
94
+ const full = [command.executable, ...(Array.isArray(command.argv) ? command.argv : [])];
95
+ const spec = TERMINALS.find((entry) => entry.id === terminal.id);
96
+ const argv = spec.build(full, title);
97
+ return {
98
+ available: true,
99
+ terminal,
100
+ argv,
101
+ // displayCommand 反映真实启动的命令(含终端自身),便于日志与人读。
102
+ displayCommand: formatCommand([terminal.bin, ...argv]),
103
+ };
104
+ }
package/package.json CHANGED
@@ -1,6 +1,46 @@
1
1
  {
2
2
  "name": "pi-sub-view",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.5.0",
4
+ "description": "Read-only live renderer and terminal inspector backend for pi-subagents child agent sessions (session.jsonl). Pure CLI, no dependencies.",
5
+ "type": "module",
6
+ "bin": {
7
+ "pi-sub-view": "view.mjs"
8
+ },
9
+ "files": [
10
+ "view.mjs",
11
+ "lib/",
12
+ "extensions/",
13
+ "README.md",
14
+ "LICENSE"
15
+ ],
16
+ "engines": {
17
+ "node": ">=20"
18
+ },
19
+ "keywords": [
20
+ "pi",
21
+ "pi-subagents",
22
+ "subagent",
23
+ "cli",
24
+ "observability",
25
+ "inspector",
26
+ "terminal"
27
+ ],
28
+ "author": "xuqianhui",
29
+ "license": "MIT",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+ssh://git@github.com/xiaquwan1001/pi-sub-view.git"
33
+ },
34
+ "homepage": "https://github.com/xiaquwan1001/pi-sub-view#readme",
35
+ "bugs": {
36
+ "url": "https://github.com/xiaquwan1001/pi-sub-view/issues"
37
+ },
38
+ "pi": {
39
+ "extensions": [
40
+ "./extensions/inspector.js"
41
+ ]
42
+ },
43
+ "scripts": {
44
+ "test": "node --test test/*.test.mjs"
45
+ }
46
+ }