@linxiraos/pi-messenger 1.1.0 → 1.3.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.
- package/CHANGELOG.md +741 -676
- package/LICENSE +45 -0
- package/README.md +424 -424
- package/THIRD-PARTY-NOTICES.txt +22951 -0
- package/config-overlay.ts +175 -171
- package/config.ts +221 -209
- package/crew/agents/crew-plan-sync.md +66 -64
- package/crew/agents/crew-planner.md +173 -172
- package/crew/agents/crew-reviewer.md +58 -58
- package/crew/agents/crew-worker.md +130 -129
- package/crew/agents.ts +507 -434
- package/crew/handlers/coordination.ts +350 -332
- package/crew/handlers/plan.ts +988 -906
- package/crew/handlers/review.ts +354 -333
- package/crew/handlers/revise.ts +400 -376
- package/crew/handlers/status.ts +303 -295
- package/crew/handlers/sync.ts +237 -233
- package/crew/handlers/task.ts +820 -792
- package/crew/handlers/team.ts +294 -235
- package/crew/handlers/work.ts +467 -425
- package/crew/id-allocator.ts +30 -30
- package/crew/index.ts +297 -257
- package/crew/live-progress.ts +58 -58
- package/crew/lobby.ts +504 -473
- package/crew/prompt.ts +262 -252
- package/crew/registry.ts +114 -114
- package/crew/spawn.ts +135 -111
- package/crew/state-autonomous.ts +163 -164
- package/crew/state-planning.ts +264 -263
- package/crew/state.ts +20 -20
- package/crew/store.ts +646 -642
- package/crew/task-actions.ts +162 -131
- package/crew/team/store.ts +572 -479
- package/crew/team/subagent-roles.ts +105 -87
- package/crew/team/types.ts +50 -50
- package/crew/types.ts +194 -194
- package/crew/utils/artifacts.ts +57 -62
- package/crew/utils/config.ts +158 -156
- package/crew/utils/discover.ts +221 -219
- package/crew/utils/install.ts +94 -85
- package/crew/utils/migrations.ts +126 -0
- package/crew/utils/progress.ts +191 -187
- package/crew/utils/result.ts +16 -16
- package/crew/utils/risk-labels.ts +5 -7
- package/crew/utils/task-format.ts +14 -14
- package/crew/utils/team-roles.ts +44 -28
- package/crew/utils/truncate.ts +76 -79
- package/crew/utils/verdict.ts +55 -55
- package/editor-binary.ts +150 -0
- package/editor-command.ts +107 -0
- package/editor-target.ts +40 -0
- package/feed.ts +321 -268
- package/handlers.ts +1041 -1107
- package/index.ts +1314 -1206
- package/install.mjs +187 -189
- package/lib.ts +587 -467
- package/overlay-actions.ts +519 -511
- package/overlay-coordinator.ts +150 -150
- package/overlay-render.ts +770 -676
- package/overlay.ts +872 -830
- package/package.json +64 -60
- package/paths.ts +60 -0
- package/skills/pi-messenger-crew/SKILL.md +375 -347
- package/store.ts +1147 -1139
- package/teamagent-command.ts +321 -0
- package/vitest.config.ts +10 -10
package/crew/utils/truncate.ts
CHANGED
|
@@ -1,79 +1,76 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Crew - Output Truncation
|
|
3
|
-
*
|
|
4
|
-
* Prevents token explosion from verbose agent outputs.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
export interface MaxOutputConfig {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface TruncationResult {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const DEFAULT_MAX_OUTPUT: Required<MaxOutputConfig> = {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export function formatBytes(bytes: number): string {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Truncate output to fit within limits.
|
|
33
|
-
*/
|
|
34
|
-
export function truncateOutput(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
artifactPath,
|
|
78
|
-
};
|
|
79
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Crew - Output Truncation
|
|
3
|
+
*
|
|
4
|
+
* Prevents token explosion from verbose agent outputs.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface MaxOutputConfig {
|
|
8
|
+
bytes?: number;
|
|
9
|
+
lines?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface TruncationResult {
|
|
13
|
+
text: string;
|
|
14
|
+
truncated: boolean;
|
|
15
|
+
originalBytes?: number;
|
|
16
|
+
originalLines?: number;
|
|
17
|
+
artifactPath?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const DEFAULT_MAX_OUTPUT: Required<MaxOutputConfig> = {
|
|
21
|
+
bytes: 200 * 1024, // 200KB
|
|
22
|
+
lines: 5000,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export function formatBytes(bytes: number): string {
|
|
26
|
+
if (bytes < 1024) return `${bytes}B`;
|
|
27
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`;
|
|
28
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Truncate output to fit within limits.
|
|
33
|
+
*/
|
|
34
|
+
export function truncateOutput(output: string, config: MaxOutputConfig, artifactPath?: string): TruncationResult {
|
|
35
|
+
const maxBytes = config.bytes ?? DEFAULT_MAX_OUTPUT.bytes;
|
|
36
|
+
const maxLines = config.lines ?? DEFAULT_MAX_OUTPUT.lines;
|
|
37
|
+
|
|
38
|
+
const lines = output.split("\n");
|
|
39
|
+
const bytes = Buffer.byteLength(output, "utf-8");
|
|
40
|
+
|
|
41
|
+
if (bytes <= maxBytes && lines.length <= maxLines) {
|
|
42
|
+
return { text: output, truncated: false };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Truncate by lines first
|
|
46
|
+
const truncatedLines = lines.length > maxLines ? lines.slice(0, maxLines) : lines;
|
|
47
|
+
let result = truncatedLines.join("\n");
|
|
48
|
+
|
|
49
|
+
// Then truncate by bytes if still too large
|
|
50
|
+
if (Buffer.byteLength(result, "utf-8") > maxBytes) {
|
|
51
|
+
// Binary search for the right cut point
|
|
52
|
+
let low = 0,
|
|
53
|
+
high = result.length;
|
|
54
|
+
while (low < high) {
|
|
55
|
+
const mid = Math.floor((low + high + 1) / 2);
|
|
56
|
+
if (Buffer.byteLength(result.slice(0, mid), "utf-8") <= maxBytes) {
|
|
57
|
+
low = mid;
|
|
58
|
+
} else {
|
|
59
|
+
high = mid - 1;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
result = result.slice(0, low);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const keptLines = result.split("\n").length;
|
|
66
|
+
const fullOutputHint = artifactPath ? ` - full output at ${artifactPath}` : "";
|
|
67
|
+
const marker = `[TRUNCATED: ${keptLines}/${lines.length} lines, ${formatBytes(Buffer.byteLength(result))}/${formatBytes(bytes)}${fullOutputHint}]\n`;
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
text: marker + result,
|
|
71
|
+
truncated: true,
|
|
72
|
+
originalBytes: bytes,
|
|
73
|
+
originalLines: lines.length,
|
|
74
|
+
artifactPath,
|
|
75
|
+
};
|
|
76
|
+
}
|
package/crew/utils/verdict.ts
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Crew - Review Verdict Parsing
|
|
3
|
-
*
|
|
4
|
-
* Shared verdict parsing utility for reviewer outputs.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
export interface ParsedReview {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function parseVerdict(output: string): ParsedReview {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Crew - Review Verdict Parsing
|
|
3
|
+
*
|
|
4
|
+
* Shared verdict parsing utility for reviewer outputs.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface ParsedReview {
|
|
8
|
+
verdict: "SHIP" | "NEEDS_WORK" | "MAJOR_RETHINK";
|
|
9
|
+
summary: string;
|
|
10
|
+
issues: string[];
|
|
11
|
+
suggestions: string[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function parseVerdict(output: string): ParsedReview {
|
|
15
|
+
const result: ParsedReview = {
|
|
16
|
+
verdict: "NEEDS_WORK",
|
|
17
|
+
summary: "",
|
|
18
|
+
issues: [],
|
|
19
|
+
suggestions: [],
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// Extract verdict
|
|
23
|
+
const verdictMatch = output.match(/##\s*Verdict:\s*(SHIP|NEEDS_WORK|MAJOR_RETHINK)/i);
|
|
24
|
+
if (verdictMatch) {
|
|
25
|
+
result.verdict = verdictMatch[1].toUpperCase() as ParsedReview["verdict"];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Extract summary (text between Verdict and next ##)
|
|
29
|
+
const summaryMatch = output.match(/##\s*Verdict:.*?\n([\s\S]*?)(?=\n##|$)/i);
|
|
30
|
+
if (summaryMatch) {
|
|
31
|
+
result.summary = summaryMatch[1].trim();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Extract issues
|
|
35
|
+
const issuesMatch = output.match(/##\s*Issues?\s*\n([\s\S]*?)(?=\n##|$)/i);
|
|
36
|
+
if (issuesMatch) {
|
|
37
|
+
result.issues = issuesMatch[1]
|
|
38
|
+
.split("\n")
|
|
39
|
+
.filter(line => line.trim().startsWith("-") || line.trim().startsWith("*"))
|
|
40
|
+
.map(line => line.replace(/^[\s\-*]+/, "").trim())
|
|
41
|
+
.filter(Boolean);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Extract suggestions
|
|
45
|
+
const suggestionsMatch = output.match(/##\s*Suggestions?\s*\n([\s\S]*?)(?=\n##|$)/i);
|
|
46
|
+
if (suggestionsMatch) {
|
|
47
|
+
result.suggestions = suggestionsMatch[1]
|
|
48
|
+
.split("\n")
|
|
49
|
+
.filter(line => line.trim().startsWith("-") || line.trim().startsWith("*"))
|
|
50
|
+
.map(line => line.replace(/^[\s\-*]+/, "").trim())
|
|
51
|
+
.filter(Boolean);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return result;
|
|
55
|
+
}
|
package/editor-binary.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Editor binary resolution — resolve the platform executable of
|
|
3
|
+
* `@linxiraos/editor`, install it when missing.
|
|
4
|
+
*
|
|
5
|
+
* The editor ships as its own npm package (`@linxiraos/editor`) with
|
|
6
|
+
* per-platform binary packages, so a fresh zeta install does not have it.
|
|
7
|
+
* Refusing the switch with "install it yourself" is a dead end for the user,
|
|
8
|
+
* so the default behaviour installs the package on demand.
|
|
9
|
+
*
|
|
10
|
+
* There is no editor *command* to probe: the one command surface is
|
|
11
|
+
* zetawork / zetacode / zetaide, and `@linxiraos/editor` deliberately ships no
|
|
12
|
+
* `bin`. The platform leaf's executable (`<leaf>/bin/termide-editor[.exe]`) is
|
|
13
|
+
* therefore resolved by path — the built-in editor's editor-only entry, which
|
|
14
|
+
* the package's own `launcher.js` resolves the same way.
|
|
15
|
+
*
|
|
16
|
+
* The Linux case needs care: a system Node often has a root-owned global
|
|
17
|
+
* prefix, so `npm i -g` fails with EACCES. Retrying under sudo is the
|
|
18
|
+
* documented remedy, but only when we are not already root and sudo exists —
|
|
19
|
+
* silently prompting from a non-interactive spawn would hang.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import * as cp from "node:child_process";
|
|
23
|
+
import * as fs from "node:fs";
|
|
24
|
+
import { createRequire } from "node:module";
|
|
25
|
+
import * as os from "node:os";
|
|
26
|
+
import * as path from "node:path";
|
|
27
|
+
|
|
28
|
+
/** npm package that carries the editor binary for the current platform. */
|
|
29
|
+
const EDITOR_PACKAGE = "@linxiraos/editor";
|
|
30
|
+
|
|
31
|
+
/** npm names use "windows"; Node's `process.platform` is "win32". */
|
|
32
|
+
const PKG_PLATFORM = process.platform === "win32" ? "windows" : process.platform;
|
|
33
|
+
|
|
34
|
+
/** The platform leaf that carries the executable. */
|
|
35
|
+
const LEAF_PACKAGE = `${EDITOR_PACKAGE}-${PKG_PLATFORM}-${process.arch}`;
|
|
36
|
+
|
|
37
|
+
/** The executable's file name inside the leaf package. */
|
|
38
|
+
const LEAF_EXE = process.platform === "win32" ? "termide-editor.exe" : "termide-editor-linux-x64";
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Node module-resolution roots for the leaf package, most specific first:
|
|
42
|
+
* npm's global prefix (Windows keeps `node_modules` beside `node.exe`, POSIX
|
|
43
|
+
* under `lib/`), zeta's own plugin tree (the editor can be installed as a
|
|
44
|
+
* plugin dependency) and the project.
|
|
45
|
+
*/
|
|
46
|
+
export function editorLeafRoots(): string[] {
|
|
47
|
+
const home = os.homedir();
|
|
48
|
+
const execDir = path.dirname(process.execPath);
|
|
49
|
+
return [
|
|
50
|
+
path.join(execDir, "node_modules"),
|
|
51
|
+
path.join(execDir, "..", "lib", "node_modules"),
|
|
52
|
+
path.join(home, ".zeta", "plugins", "node_modules"),
|
|
53
|
+
path.join(process.cwd(), "node_modules"),
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Absolute path to the platform editor executable, or null when the package
|
|
59
|
+
* is not installed. Resolution goes through Node's own algorithm so a leaf
|
|
60
|
+
* installed flat or nested both resolve.
|
|
61
|
+
*/
|
|
62
|
+
export function findEditorBinary(): string | null {
|
|
63
|
+
const require_ = createRequire(import.meta.url);
|
|
64
|
+
try {
|
|
65
|
+
const resolved = require_.resolve(`${LEAF_PACKAGE}/bin/${LEAF_EXE}`, { paths: editorLeafRoots() });
|
|
66
|
+
fs.accessSync(resolved, fs.constants.X_OK);
|
|
67
|
+
return resolved;
|
|
68
|
+
} catch {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface InstallResult {
|
|
74
|
+
ok: boolean;
|
|
75
|
+
/** Human-readable outcome for the status line. */
|
|
76
|
+
message: string;
|
|
77
|
+
/** Set when the install needs elevation the caller should surface. */
|
|
78
|
+
needsSudo?: boolean;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Install the editor package globally. `npm` is used rather than `bun add -g`
|
|
83
|
+
* because npm is the one guaranteed present wherever zeta itself was installed
|
|
84
|
+
* with npm, and the editor's optionalDependencies resolve identically.
|
|
85
|
+
*/
|
|
86
|
+
export function installEditorPackage(timeoutMs = 180_000): InstallResult {
|
|
87
|
+
const attempt = (argv: string[]): cp.SpawnSyncReturns<string> =>
|
|
88
|
+
cp.spawnSync(argv[0], argv.slice(1), { encoding: "utf8", timeout: timeoutMs });
|
|
89
|
+
|
|
90
|
+
let result = attempt(["npm", "install", "-g", EDITOR_PACKAGE]);
|
|
91
|
+
if (result.status === 0) {
|
|
92
|
+
return { ok: true, message: `Installed ${EDITOR_PACKAGE}` };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const combined = `${result.stdout ?? ""}${result.stderr ?? ""}`;
|
|
96
|
+
const permissionDenied = /EACCES|EPERM|permission denied|is not in the sudoers/i.test(combined);
|
|
97
|
+
|
|
98
|
+
// Linux/macOS with a root-owned global prefix: retry elevated, but only
|
|
99
|
+
// when sudo exists and we are not already root (sudo as root is a no-op
|
|
100
|
+
// that masks the real error).
|
|
101
|
+
if (permissionDenied && process.platform !== "win32" && process.getuid?.() !== 0) {
|
|
102
|
+
const sudoExists = cp.spawnSync("sh", ["-c", "command -v sudo"], { encoding: "utf8" }).status === 0;
|
|
103
|
+
if (sudoExists) {
|
|
104
|
+
result = attempt(["sudo", "-n", "npm", "install", "-g", EDITOR_PACKAGE]);
|
|
105
|
+
if (result.status === 0) {
|
|
106
|
+
return { ok: true, message: `Installed ${EDITOR_PACKAGE} (elevated)` };
|
|
107
|
+
}
|
|
108
|
+
// -n fails when a password would be needed; tell the user the exact
|
|
109
|
+
// command instead of hanging on an invisible prompt.
|
|
110
|
+
return {
|
|
111
|
+
ok: false,
|
|
112
|
+
needsSudo: true,
|
|
113
|
+
message: `Global install needs elevation. Run: sudo npm install -g ${EDITOR_PACKAGE}`,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
ok: false,
|
|
118
|
+
message: `Global npm prefix is not writable and sudo is unavailable. Run: npm install -g ${EDITOR_PACKAGE} from an account that can write it, or set a user-level prefix (npm config set prefix ~/.npm-global).`,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const firstError = combined.split(/\r?\n/).find(line => line.trim().length > 0) ?? "unknown npm error";
|
|
123
|
+
return { ok: false, message: `Could not install ${EDITOR_PACKAGE}: ${firstError}` };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Resolve the editor binary, installing the package first when the caller
|
|
128
|
+
* allows it. Returns the path plus what happened, so the caller can report an
|
|
129
|
+
* install that succeeded (or explain one that did not).
|
|
130
|
+
*/
|
|
131
|
+
export function ensureEditorBinary(autoInstall: boolean): { binary: string | null; note?: string; error?: string } {
|
|
132
|
+
const existing = findEditorBinary();
|
|
133
|
+
if (existing) return { binary: existing };
|
|
134
|
+
if (!autoInstall) {
|
|
135
|
+
return {
|
|
136
|
+
binary: null,
|
|
137
|
+
error: `The editor is not installed. Run: npm install -g ${EDITOR_PACKAGE} (or enable Editor → Auto-install Editor).`,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
const install = installEditorPackage();
|
|
141
|
+
if (!install.ok) return { binary: null, error: install.message };
|
|
142
|
+
// Re-resolve right away instead of hoping a fresh shell's PATH catches up:
|
|
143
|
+
// the leaf lands under a known root, and a miss is worth reporting.
|
|
144
|
+
const installed = findEditorBinary();
|
|
145
|
+
if (installed) return { binary: installed, note: install.message };
|
|
146
|
+
return {
|
|
147
|
+
binary: null,
|
|
148
|
+
error: `${install.message}, but ${LEAF_PACKAGE} was not found under ${editorLeafRoots().join(", ")}.`,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/editor` — hand the current session to the editor.
|
|
3
|
+
*
|
|
4
|
+
* The agent CLI and the editor are two processes over the same workspace, so
|
|
5
|
+
* the switch has to carry the context the user was in: where they were, and
|
|
6
|
+
* what they were looking at. It carries it the way any other editor does —
|
|
7
|
+
* on the command line, as the editor's own positional protocol:
|
|
8
|
+
*
|
|
9
|
+
* termide-editor FILE[:line[:col]]... [DIRECTORY]
|
|
10
|
+
*
|
|
11
|
+
* The workspace directory goes last as the directory positional, which the
|
|
12
|
+
* editor takes as its project root (LSP, git and config resolve there), and
|
|
13
|
+
* the requested target (if any) goes first, so the cursor lands on the line
|
|
14
|
+
* the user asked for. Nothing is written to `~/.zeta` — the previous
|
|
15
|
+
* `handoff.json` contract existed because the editor of the day could not be
|
|
16
|
+
* told anything else; the built-in editor's positional protocol replaces it.
|
|
17
|
+
*
|
|
18
|
+
* The editor is spawned detached: the CLI stays alive so returning is just
|
|
19
|
+
* another switch, and a crashed editor never takes the session with it.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { cfgEditorAutoInstall } from "@linxiraos/zeta/session/settings";
|
|
23
|
+
import {
|
|
24
|
+
createDefaultTerminalLaunchRequest,
|
|
25
|
+
terminalLaunchCapabilities,
|
|
26
|
+
} from "@linxiraos/zeta/subprocess/terminal-launch";
|
|
27
|
+
import { classifyTerminalMultiplexer } from "@linxiraos/pi-tui/terminal-multiplexer";
|
|
28
|
+
import * as cp from "node:child_process";
|
|
29
|
+
import type { ExtensionAPI, ExtensionCommandContext } from "@linxiraos/zeta";
|
|
30
|
+
import { ensureEditorBinary } from "./editor-binary.ts";
|
|
31
|
+
import { editorArgs } from "./editor-target.ts";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Register /editor. Safe to call on every extension load: the runtime
|
|
35
|
+
* replaces a same-named registration.
|
|
36
|
+
*/
|
|
37
|
+
export function registerEditorCommand(pi: ExtensionAPI): void {
|
|
38
|
+
pi.registerCommand("editor", {
|
|
39
|
+
description: "Open the built-in editor on this workspace, optionally at FILE[:line[:col]]",
|
|
40
|
+
handler: async (args: string, ctx: ExtensionCommandContext) => {
|
|
41
|
+
await switchToEditor(ctx, args);
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Perform the switch: resolve the editor and spawn it on the workspace.
|
|
48
|
+
* Shared by the /editor command and the header button so both honour the
|
|
49
|
+
* same settings and report the same way.
|
|
50
|
+
*/
|
|
51
|
+
export async function switchToEditor(ctx: ExtensionCommandContext, args = ""): Promise<void> {
|
|
52
|
+
const cwd = ctx.cwd;
|
|
53
|
+
const autoInstall = ctx.settings ? cfgEditorAutoInstall.get(ctx.settings) : true;
|
|
54
|
+
const resolved = ensureEditorBinary(autoInstall === true);
|
|
55
|
+
if (!resolved.binary) {
|
|
56
|
+
ctx.ui?.notify?.(resolved.error ?? "The editor is unavailable.", "error");
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const argv = editorArgs(args, cwd);
|
|
61
|
+
|
|
62
|
+
// Inside a multiplexer the editor belongs beside the CLI, as a pane: the
|
|
63
|
+
// session stays visible and the editor is not a separate detached process
|
|
64
|
+
// the terminal knows nothing about. The host owns the launch (see
|
|
65
|
+
// `ctx.ui.openTerminal`); when it cannot place the pane we fall through to
|
|
66
|
+
// the detached spawn rather than leaving the user without an editor.
|
|
67
|
+
if (process.platform !== "win32" && ctx.ui?.openTerminal) {
|
|
68
|
+
const multiplexer = classifyTerminalMultiplexer(process.env);
|
|
69
|
+
if (multiplexer !== null) {
|
|
70
|
+
const plan = createDefaultTerminalLaunchRequest(multiplexer, "pane", [resolved.binary, ...argv], cwd);
|
|
71
|
+
if ("request" in plan) {
|
|
72
|
+
try {
|
|
73
|
+
const result = await ctx.ui.openTerminal(plan.request);
|
|
74
|
+
const provider = terminalLaunchCapabilities[result.multiplexer].displayName;
|
|
75
|
+
ctx.ui.notify?.(
|
|
76
|
+
result.warning
|
|
77
|
+
? `Opened the editor in ${provider}, but ${result.warning}`
|
|
78
|
+
: `Opened the editor in ${provider}.`,
|
|
79
|
+
result.warning ? "warning" : "info",
|
|
80
|
+
);
|
|
81
|
+
return;
|
|
82
|
+
} catch {
|
|
83
|
+
// A multiplexer that cannot launch must not cost the user the editor.
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Detached so the CLI survives the switch and a crashed editor cannot take
|
|
90
|
+
// the session with it. On Windows a detached child gets its own console,
|
|
91
|
+
// which is where the editor then runs; the editor discovers everything else
|
|
92
|
+
// it needs from the argv above (the workspace directory is the last entry).
|
|
93
|
+
const child = cp.spawn(resolved.binary, argv, { detached: true, stdio: "ignore", cwd });
|
|
94
|
+
child.unref();
|
|
95
|
+
|
|
96
|
+
// `argv` is the resolved targets followed by the project root; the first
|
|
97
|
+
// entries are what the user asked to open, the last is always cwd.
|
|
98
|
+
const targets = argv.slice(0, -1);
|
|
99
|
+
const opened =
|
|
100
|
+
targets.length > 0 ? `Opened the editor at ${targets.join(", ")}` : `Opened the editor on ${cwd}`;
|
|
101
|
+
ctx.ui?.notify?.(resolved.note ? `${opened} — ${resolved.note}.` : `${opened}.`, "info");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Entry point for the header button (no slash-command arguments). */
|
|
105
|
+
export async function switchToEditorFromUi(ctx: ExtensionCommandContext): Promise<void> {
|
|
106
|
+
await switchToEditor(ctx);
|
|
107
|
+
}
|
package/editor-target.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `/editor` switch's command line — what the editor payload is told.
|
|
3
|
+
*
|
|
4
|
+
* The built-in editor takes its context on the command line
|
|
5
|
+
* (`termide-editor FILE[:line[:col]]... [DIRECTORY]`), so the switch needs no
|
|
6
|
+
* side-channel file: the requested targets come first, in argument order, and
|
|
7
|
+
* the workspace directory comes last as the editor's project root (LSP, git
|
|
8
|
+
* and the layered config resolve against it).
|
|
9
|
+
*
|
|
10
|
+
* Kept apart from `editor-command.ts` on purpose: this is the pair's protocol,
|
|
11
|
+
* pure string/path work with no host imports, so it is unit-testable in the
|
|
12
|
+
* plugin's standalone vitest tree.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import * as path from "node:path";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Build the editor's argv for a switch.
|
|
19
|
+
*
|
|
20
|
+
* - `args` is the raw `/editor` argument string; each whitespace-separated
|
|
21
|
+
* token is one target (`FILE`, `FILE:line` or `FILE:line:col`).
|
|
22
|
+
* - Relative targets resolve against `cwd`, because the editor would otherwise
|
|
23
|
+
* resolve them against its own process working directory. The position
|
|
24
|
+
* suffix survives resolution — the editor parses `path:line:col` itself —
|
|
25
|
+
* and a Windows drive letter is never mistaken for a position.
|
|
26
|
+
* - The trailing entry is always the workspace directory (the project root).
|
|
27
|
+
*/
|
|
28
|
+
export function editorArgs(args: string, cwd: string): string[] {
|
|
29
|
+
const targets = args
|
|
30
|
+
.trim()
|
|
31
|
+
.split(/\s+/)
|
|
32
|
+
.filter(token => token.length > 0)
|
|
33
|
+
.map(token => {
|
|
34
|
+
const match = /^(.*?)((?::\d+)+(?::)?)$/.exec(token);
|
|
35
|
+
if (!match) return path.resolve(cwd, token);
|
|
36
|
+
const [, filePath, position] = match;
|
|
37
|
+
return path.resolve(cwd, filePath) + position;
|
|
38
|
+
});
|
|
39
|
+
return [...targets, cwd];
|
|
40
|
+
}
|