pi-agent-ide 0.3.0 → 0.4.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 +21 -0
- package/README.md +1 -0
- package/dist/{chunk-RHV5STR3.js → chunk-3SE372WC.js} +3 -3
- package/dist/{chunk-EWNWKDMP.js → chunk-6DZA4CLO.js} +1 -1
- package/dist/{chunk-VZJIZXSH.js → chunk-7JYQMAEL.js} +1 -1
- package/dist/{chunk-SSHZEWWV.js → chunk-DQF5QMJ2.js} +1 -1
- package/dist/chunk-EJ5OACQM.js +1 -0
- package/dist/{chunk-CW53ZTU3.js → chunk-GFW5QPQV.js} +1 -1
- package/dist/chunk-J34VKQGJ.js +1 -0
- package/dist/{chunk-CEKXBS55.js → chunk-TI5GMJ7Q.js} +1 -1
- package/dist/chunk-XSTHESNR.js +25 -0
- package/dist/{chunk-LRYE2WCN.js → chunk-Y7ZMR4LT.js} +1 -1
- package/dist/entry-10.js +1 -1
- package/dist/entry-14.js +1 -1
- package/dist/entry-21.js +1 -1
- package/dist/entry-26.js +1 -1
- package/dist/entry-28.js +1 -1
- package/dist/entry-32.js +1 -1
- package/dist/entry-37.js +1 -1
- package/dist/entry-40.js +1 -1
- package/dist/entry-46.js +1 -1
- package/dist/entry-49.js +1 -1
- package/dist/entry-51.js +1 -1
- package/dist/entry-53.js +1 -1
- package/dist/entry-54.js +1 -1
- package/dist/entry-56.js +1 -1
- package/dist/entry-59.js +1 -1
- package/dist/entry-62.js +1 -1
- package/dist/entry-69.js +1 -1
- package/dist/entry-70.js +18 -18
- package/dist/entry-71.js +2 -2
- package/dist/entry-72.js +1 -1
- package/dist/entry-75.js +1 -1
- package/dist/pi-agent-ide.js +3 -3
- package/docs/tools.md +11 -0
- package/node_modules/pi-agent-doctor/package.json +1 -1
- package/node_modules/pi-agent-filesystem/package.json +6 -6
- package/node_modules/pi-agent-filesystem-image/package.json +3 -3
- package/node_modules/pi-agent-filesystem-pdf/package.json +3 -3
- package/node_modules/pi-agent-filesystem-text/package.json +3 -3
- package/node_modules/pi-agent-ide-ast/package.json +9 -9
- package/node_modules/pi-agent-ide-changes/package.json +6 -6
- package/node_modules/pi-agent-ide-formatter/package.json +3 -3
- package/node_modules/pi-agent-ide-languages/package.json +2 -2
- package/node_modules/pi-agent-ide-lint/package.json +7 -7
- package/node_modules/pi-agent-ide-lsp/package.json +9 -9
- package/node_modules/pi-agent-image/package.json +2 -2
- package/node_modules/pi-agent-invariant/package.json +1 -1
- package/node_modules/pi-agent-pdf/package.json +2 -2
- package/node_modules/pi-agent-read/package.json +6 -6
- package/node_modules/pi-agent-read/src/core/tools/read/read-renderer.ts +3 -0
- package/node_modules/pi-agent-resource/package.json +1 -1
- package/node_modules/pi-agent-resource/src/resource.ts +2 -0
- package/node_modules/pi-agent-search/package.json +3 -3
- package/node_modules/pi-agent-search-text/package.json +6 -6
- package/node_modules/pi-agent-text/package.json +2 -2
- package/node_modules/pi-agent-text-anchor-constant/package.json +3 -3
- package/node_modules/pi-agent-text-anchor-exact/package.json +3 -3
- package/node_modules/pi-agent-text-anchor-line-hash/package.json +5 -5
- package/node_modules/pi-agent-text-editor/package.json +8 -8
- package/node_modules/pi-agent-text-editor/src/api/plugin-protocol.ts +1 -1
- package/node_modules/pi-agent-text-editor/src/api/tool-renderer.ts +3 -0
- package/node_modules/pi-agent-text-editor/src/core/text-editor-core.ts +41 -4
- package/node_modules/pi-agent-text-editor/src/core/text-mutation.ts +12 -2
- package/node_modules/pi-agent-text-editor-renderer/package.json +5 -5
- package/node_modules/pi-agent-text-editor-renderer/src/renderer.ts +1 -2
- package/node_modules/pi-agent-text-editor-stale-anchor/package.json +4 -4
- package/node_modules/pi-agent-tool-call-interception/package.json +2 -2
- package/node_modules/pi-agent-tool-ui/package.json +1 -1
- package/node_modules/pi-agent-web/package.json +6 -6
- package/node_modules/pi-agent-web-html/package.json +2 -2
- package/node_modules/pi-agent-web-image/package.json +3 -3
- package/node_modules/pi-agent-web-pdf/package.json +3 -3
- package/node_modules/pi-agent-web-text/package.json +3 -3
- package/package.json +36 -33
- package/src/composite/builtin-extensions.ts +6 -0
- package/src/composite/extensions-config.ts +18 -0
- package/src/composite/module-labels.ts +4 -0
- package/src/composite/module-settings-store.ts +17 -1
- package/src/composite/module-settings.ts +34 -4
- package/src/composite/preferences.ts +26 -0
- package/src/composite/settings-panel.ts +62 -30
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/package.json +6 -6
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/package.json +6 -6
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/package.json +2 -2
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/package.json +3 -3
- package/src/extensions/pi-agent-read/package.json +6 -6
- package/src/extensions/pi-agent-read/src/core/tools/read/read-renderer.ts +3 -0
- package/src/extensions/pi-agent-search/package.json +3 -3
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/package.json +6 -6
- package/src/extensions/pi-agent-text-editor/package.json +8 -8
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/package.json +3 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/package.json +3 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/package.json +5 -5
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/package.json +5 -5
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/renderer.ts +1 -2
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/package.json +4 -4
- package/src/extensions/pi-agent-text-editor/src/api/plugin-protocol.ts +1 -1
- package/src/extensions/pi-agent-text-editor/src/api/tool-renderer.ts +3 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-editor-core.ts +41 -4
- package/src/extensions/pi-agent-text-editor/src/core/text-mutation.ts +12 -2
- package/src/pi-agent-ide.ts +2 -1
- package/src/plugins/pi-agent-ide-ast/package.json +9 -9
- package/src/plugins/pi-agent-ide-changes/package.json +6 -6
- package/src/plugins/pi-agent-ide-formatter/package.json +3 -3
- package/src/plugins/pi-agent-ide-languages/package.json +2 -2
- package/src/plugins/pi-agent-ide-lint/package.json +7 -7
- package/src/plugins/pi-agent-ide-lsp/package.json +9 -9
- package/src/plugins/pi-agent-ide-terminal/index.ts +34 -0
- package/src/plugins/pi-agent-ide-terminal/src/renderer.ts +238 -0
- package/src/plugins/pi-agent-ide-terminal/src/resources.ts +461 -0
- package/src/plugins/pi-agent-ide-terminal/src/screen-image.ts +127 -0
- package/src/plugins/pi-agent-ide-terminal/src/search.ts +159 -0
- package/src/plugins/pi-agent-ide-terminal/src/session-manager.ts +529 -0
- package/src/plugins/pi-agent-ide-terminal/src/shell-profile.ts +56 -0
- package/src/plugins/pi-agent-ide-terminal/src/tools.ts +157 -0
- package/src/plugins/pi-agent-ide-terminal/src/types.ts +66 -0
- package/src/plugins/pi-agent-ide-terminal/src/ui.ts +336 -0
- package/dist/chunk-VCCMVQXU.js +0 -1
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import type { ShellProfile } from "#src/plugins/pi-agent-ide-terminal/src/types.js";
|
|
5
|
+
|
|
6
|
+
/** Resolve the shell configured for the current user and describe its command syntax. */
|
|
7
|
+
export function resolveShellProfile(
|
|
8
|
+
platform: NodeJS.Platform = process.platform,
|
|
9
|
+
environment: NodeJS.ProcessEnv = process.env,
|
|
10
|
+
): ShellProfile {
|
|
11
|
+
if (platform === "win32") {
|
|
12
|
+
const executable = environment.SHELL?.trim() || "powershell.exe";
|
|
13
|
+
const basename = path.win32.basename(executable).toLowerCase();
|
|
14
|
+
if (basename === "cmd" || basename === "cmd.exe") {
|
|
15
|
+
return {
|
|
16
|
+
executable,
|
|
17
|
+
displayName: "Command Prompt",
|
|
18
|
+
family: "cmd",
|
|
19
|
+
commandArgs: (command) => ["/d", "/s", "/c", command],
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
executable,
|
|
24
|
+
displayName: basename.startsWith("pwsh") ? "PowerShell" : "Windows PowerShell",
|
|
25
|
+
family: "powershell",
|
|
26
|
+
commandArgs: (command) => ["-NoLogo", "-NoProfile", "-Command", command],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const executable =
|
|
31
|
+
environment.SHELL?.trim() ||
|
|
32
|
+
os.userInfo().shell ||
|
|
33
|
+
(platform === "darwin" ? "/bin/zsh" : "/bin/bash");
|
|
34
|
+
const basename = path.basename(executable);
|
|
35
|
+
return {
|
|
36
|
+
executable,
|
|
37
|
+
displayName: basename === "zsh" ? "zsh" : basename === "bash" ? "Bash" : basename,
|
|
38
|
+
family: "posix",
|
|
39
|
+
commandArgs: (command) => ["-c", command],
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Return concise syntax guidance for the shell exposed to the agent. */
|
|
44
|
+
export function shellSyntaxGuidance(profile: ShellProfile): string {
|
|
45
|
+
switch (profile.family) {
|
|
46
|
+
case "powershell": {
|
|
47
|
+
return "Write PowerShell syntax. Use PowerShell cmdlets, `$env:NAME` for environment variables, and `;` or pipelines to compose commands.";
|
|
48
|
+
}
|
|
49
|
+
case "cmd": {
|
|
50
|
+
return "Write Command Prompt syntax. Use `%NAME%` for environment variables and `&&` to chain successful commands.";
|
|
51
|
+
}
|
|
52
|
+
case "posix": {
|
|
53
|
+
return `Write ${profile.displayName} syntax. Use POSIX-style paths, \`$NAME\` for environment variables, and \`&&\` or pipelines to compose commands.`;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { stripVTControlCharacters } from "node:util";
|
|
3
|
+
|
|
4
|
+
import { defineTool, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { Type } from "typebox";
|
|
6
|
+
|
|
7
|
+
import { COMPACT_READ_ROWS } from "#src/extensions/pi-agent-read/src/core/tools/read/read-renderer.js";
|
|
8
|
+
import { renderRunCall, renderRunResult } from "#src/plugins/pi-agent-ide-terminal/src/renderer.js";
|
|
9
|
+
|
|
10
|
+
import { shellSyntaxGuidance } from "#src/plugins/pi-agent-ide-terminal/src/shell-profile.js";
|
|
11
|
+
import type { TerminalSessionManager } from "#src/plugins/pi-agent-ide-terminal/src/session-manager.js";
|
|
12
|
+
import type { TerminalUi } from "#src/plugins/pi-agent-ide-terminal/src/ui.js";
|
|
13
|
+
import type {
|
|
14
|
+
ShellProfile,
|
|
15
|
+
TerminalSessionSnapshot,
|
|
16
|
+
} from "#src/plugins/pi-agent-ide-terminal/src/types.js";
|
|
17
|
+
|
|
18
|
+
const runParameters = Type.Object(
|
|
19
|
+
{
|
|
20
|
+
command: Type.String({
|
|
21
|
+
minLength: 1,
|
|
22
|
+
description: "Command written in the syntax of the configured system shell",
|
|
23
|
+
}),
|
|
24
|
+
background: Type.Optional(
|
|
25
|
+
Type.Boolean({
|
|
26
|
+
description: "Return immediately while the terminal session continues. Defaults to false.",
|
|
27
|
+
}),
|
|
28
|
+
),
|
|
29
|
+
cwd: Type.Optional(
|
|
30
|
+
Type.String({
|
|
31
|
+
description: "Working directory. Relative paths resolve from the current workspace.",
|
|
32
|
+
}),
|
|
33
|
+
),
|
|
34
|
+
cols: Type.Optional(
|
|
35
|
+
Type.Integer({ minimum: 20, maximum: 300, description: "Virtual terminal width in cells" }),
|
|
36
|
+
),
|
|
37
|
+
rows: Type.Optional(
|
|
38
|
+
Type.Integer({ minimum: 5, maximum: 120, description: "Virtual terminal height in cells" }),
|
|
39
|
+
),
|
|
40
|
+
},
|
|
41
|
+
{ additionalProperties: false },
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
/** Register terminal process lifecycle tools with platform-specific shell guidance. */
|
|
45
|
+
export function registerTerminalTools(
|
|
46
|
+
pi: ExtensionAPI,
|
|
47
|
+
manager: TerminalSessionManager,
|
|
48
|
+
profile: ShellProfile,
|
|
49
|
+
ui: Pick<TerminalUi, "bind">,
|
|
50
|
+
): void {
|
|
51
|
+
pi.registerTool(
|
|
52
|
+
defineTool<typeof runParameters, TerminalSessionSnapshot>({
|
|
53
|
+
name: "run",
|
|
54
|
+
label: `${profile.displayName} run`,
|
|
55
|
+
promptSnippet: `Run ${profile.displayName} commands in synchronous or background terminal sessions`,
|
|
56
|
+
promptGuidelines: [
|
|
57
|
+
`Write commands for ${profile.displayName}; commands are not translated between shell languages.`,
|
|
58
|
+
"Use background for servers, watchers, long builds, and work that can finish while you continue. Use the returned shell: source with read, write, insert, and delete.",
|
|
59
|
+
],
|
|
60
|
+
description: `Use run to execute a command in the user's configured ${profile.displayName} shell (${profile.executable}). Every call creates an addressable terminal session. Set background to true to continue without waiting; completion is delivered automatically and wakes the agent. ${shellSyntaxGuidance(profile)}`,
|
|
61
|
+
parameters: runParameters,
|
|
62
|
+
async execute(_toolCallId, input, signal, onUpdate, context) {
|
|
63
|
+
ui.bind(context);
|
|
64
|
+
const cwd = input.cwd === undefined ? context.cwd : path.resolve(context.cwd, input.cwd);
|
|
65
|
+
const session = manager.start({
|
|
66
|
+
command: input.command,
|
|
67
|
+
background: input.background ?? false,
|
|
68
|
+
cwd,
|
|
69
|
+
shell: profile,
|
|
70
|
+
...(input.cols === undefined ? {} : { cols: input.cols }),
|
|
71
|
+
...(input.rows === undefined ? {} : { rows: input.rows }),
|
|
72
|
+
});
|
|
73
|
+
if (input.background === true && session.status !== "failed") {
|
|
74
|
+
await captureInitialBackgroundPreview(manager, session, onUpdate);
|
|
75
|
+
if (session.status !== "running") session.completionDelivered = true;
|
|
76
|
+
return terminalResult(manager.snapshot(session));
|
|
77
|
+
}
|
|
78
|
+
if (session.status === "failed") return terminalResult(manager.snapshot(session));
|
|
79
|
+
const completed = await manager.wait(session.source, signal);
|
|
80
|
+
return terminalResult(manager.snapshot(completed));
|
|
81
|
+
},
|
|
82
|
+
renderCall(args, theme) {
|
|
83
|
+
const command = typeof args.command === "string" ? args.command : "";
|
|
84
|
+
const cwd =
|
|
85
|
+
typeof args.cwd === "string" ? path.resolve(process.cwd(), args.cwd) : process.cwd();
|
|
86
|
+
return renderRunCall(command, args.background === true, cwd, profile, theme);
|
|
87
|
+
},
|
|
88
|
+
renderResult(result, options, theme) {
|
|
89
|
+
return renderRunResult(
|
|
90
|
+
result.details as Partial<TerminalSessionSnapshot>,
|
|
91
|
+
options.expanded,
|
|
92
|
+
theme,
|
|
93
|
+
);
|
|
94
|
+
},
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function terminalResult(snapshot: TerminalSessionSnapshot) {
|
|
100
|
+
const excerpt = outputExcerpt(snapshot.output);
|
|
101
|
+
const lines = [
|
|
102
|
+
`session: ${snapshot.source}`,
|
|
103
|
+
`status: ${snapshot.status}`,
|
|
104
|
+
`shell: ${snapshot.shell}`,
|
|
105
|
+
`cwd: ${snapshot.cwd}`,
|
|
106
|
+
`elapsedMs: ${snapshot.elapsedMs}`,
|
|
107
|
+
snapshot.exitCode === undefined ? undefined : `exitCode: ${snapshot.exitCode}`,
|
|
108
|
+
snapshot.signal === undefined ? undefined : `signal: ${snapshot.signal}`,
|
|
109
|
+
snapshot.error === undefined ? undefined : `error: ${snapshot.error}`,
|
|
110
|
+
`outputRange: ${snapshot.outputStart}-${snapshot.outputEnd}`,
|
|
111
|
+
snapshot.truncated ? "output: truncated; use read for the retained log" : undefined,
|
|
112
|
+
excerpt.length === 0 ? "output: (empty)" : `output:\n${excerpt}`,
|
|
113
|
+
].filter((line): line is string => line !== undefined);
|
|
114
|
+
return {
|
|
115
|
+
content: [{ type: "text" as const, text: lines.join("\n") }],
|
|
116
|
+
details: snapshot,
|
|
117
|
+
isError: snapshot.status === "failed" || snapshot.status === "lost",
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function outputExcerpt(output: string): string {
|
|
122
|
+
const plain = stripVTControlCharacters(output).trimEnd();
|
|
123
|
+
return plain.length <= 8_000 ? plain : `[earlier output omitted]\n${plain.slice(-8_000)}`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function captureInitialBackgroundPreview(
|
|
127
|
+
manager: TerminalSessionManager,
|
|
128
|
+
session: ReturnType<TerminalSessionManager["start"]>,
|
|
129
|
+
onUpdate: ((result: ReturnType<typeof terminalResult>) => void) | undefined,
|
|
130
|
+
): Promise<void> {
|
|
131
|
+
await new Promise<void>((resolve) => {
|
|
132
|
+
let settled = false;
|
|
133
|
+
let timeout: ReturnType<typeof setTimeout>;
|
|
134
|
+
let unsubscribe = (): void => {};
|
|
135
|
+
const finish = (): void => {
|
|
136
|
+
if (settled) return;
|
|
137
|
+
settled = true;
|
|
138
|
+
clearTimeout(timeout);
|
|
139
|
+
unsubscribe();
|
|
140
|
+
resolve();
|
|
141
|
+
};
|
|
142
|
+
const publish = (): void => {
|
|
143
|
+
const snapshot = manager.snapshot(session);
|
|
144
|
+
onUpdate?.(terminalResult(snapshot));
|
|
145
|
+
const rows = stripVTControlCharacters(snapshot.output)
|
|
146
|
+
.replaceAll("\r", "")
|
|
147
|
+
.split("\n")
|
|
148
|
+
.filter((line) => line.length > 0).length;
|
|
149
|
+
if (rows >= COMPACT_READ_ROWS || snapshot.status !== "running") finish();
|
|
150
|
+
};
|
|
151
|
+
unsubscribe = manager.onDidChange((changed) => {
|
|
152
|
+
if (changed.id === session.id) publish();
|
|
153
|
+
});
|
|
154
|
+
timeout = setTimeout(finish, 2_000);
|
|
155
|
+
publish();
|
|
156
|
+
});
|
|
157
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { IPty } from "node-pty";
|
|
2
|
+
import type { Terminal } from "@xterm/headless";
|
|
3
|
+
|
|
4
|
+
export type TerminalSessionStatus =
|
|
5
|
+
| "running"
|
|
6
|
+
| "completed"
|
|
7
|
+
| "failed"
|
|
8
|
+
| "stopping"
|
|
9
|
+
| "stopped"
|
|
10
|
+
| "cancelled"
|
|
11
|
+
| "lost";
|
|
12
|
+
|
|
13
|
+
export interface ShellProfile {
|
|
14
|
+
readonly executable: string;
|
|
15
|
+
readonly displayName: string;
|
|
16
|
+
readonly family: "posix" | "powershell" | "cmd";
|
|
17
|
+
readonly commandArgs: (command: string) => readonly string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface TerminalSession {
|
|
21
|
+
readonly id: string;
|
|
22
|
+
readonly source: string;
|
|
23
|
+
readonly command: string;
|
|
24
|
+
readonly background: boolean;
|
|
25
|
+
readonly cwd: string;
|
|
26
|
+
readonly shell: ShellProfile;
|
|
27
|
+
readonly startedAt: number;
|
|
28
|
+
readonly cols: number;
|
|
29
|
+
readonly rows: number;
|
|
30
|
+
readonly process?: IPty;
|
|
31
|
+
readonly screen: Terminal;
|
|
32
|
+
status: TerminalSessionStatus;
|
|
33
|
+
endedAt?: number;
|
|
34
|
+
exitCode?: number;
|
|
35
|
+
signal?: number;
|
|
36
|
+
error?: string;
|
|
37
|
+
output: string;
|
|
38
|
+
outputStart: number;
|
|
39
|
+
screenReady: Promise<void>;
|
|
40
|
+
completion: Promise<TerminalSession>;
|
|
41
|
+
resolveCompletion: (session: TerminalSession) => void;
|
|
42
|
+
completionDelivered: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface TerminalSessionSnapshot {
|
|
46
|
+
readonly id: string;
|
|
47
|
+
readonly source: string;
|
|
48
|
+
readonly command: string;
|
|
49
|
+
readonly cwd: string;
|
|
50
|
+
readonly shell: string;
|
|
51
|
+
readonly shellFamily: ShellProfile["family"];
|
|
52
|
+
readonly background: boolean;
|
|
53
|
+
readonly status: TerminalSessionStatus;
|
|
54
|
+
readonly startedAt: number;
|
|
55
|
+
readonly endedAt?: number;
|
|
56
|
+
readonly elapsedMs: number;
|
|
57
|
+
readonly exitCode?: number;
|
|
58
|
+
readonly signal?: number;
|
|
59
|
+
readonly error?: string;
|
|
60
|
+
readonly output: string;
|
|
61
|
+
readonly outputStart: number;
|
|
62
|
+
readonly outputEnd: number;
|
|
63
|
+
readonly truncated: boolean;
|
|
64
|
+
readonly cols: number;
|
|
65
|
+
readonly rows: number;
|
|
66
|
+
}
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DynamicBorder,
|
|
3
|
+
type ExtensionAPI,
|
|
4
|
+
type ExtensionContext,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import {
|
|
7
|
+
Box,
|
|
8
|
+
Container,
|
|
9
|
+
Key,
|
|
10
|
+
matchesKey,
|
|
11
|
+
SelectList,
|
|
12
|
+
Text,
|
|
13
|
+
truncateToWidth,
|
|
14
|
+
type SelectItem,
|
|
15
|
+
} from "@earendil-works/pi-tui";
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
renderTerminalResult,
|
|
19
|
+
renderTerminalWidgetLines,
|
|
20
|
+
} from "#src/plugins/pi-agent-ide-terminal/src/renderer.js";
|
|
21
|
+
import type { TerminalSessionManager } from "#src/plugins/pi-agent-ide-terminal/src/session-manager.js";
|
|
22
|
+
import type {
|
|
23
|
+
TerminalSession,
|
|
24
|
+
TerminalSessionSnapshot,
|
|
25
|
+
} from "#src/plugins/pi-agent-ide-terminal/src/types.js";
|
|
26
|
+
|
|
27
|
+
const COMPLETION_DELAY_MS = 120;
|
|
28
|
+
|
|
29
|
+
/** Keeps background terminal status and completion rendering separate from agent result data. */
|
|
30
|
+
export class TerminalUi {
|
|
31
|
+
readonly #pending = new Map<string, TerminalSession>();
|
|
32
|
+
#context: ExtensionContext | undefined;
|
|
33
|
+
#deliveryTimer: ReturnType<typeof setTimeout> | undefined;
|
|
34
|
+
#elapsedTimer: ReturnType<typeof setInterval> | undefined;
|
|
35
|
+
#closed = false;
|
|
36
|
+
|
|
37
|
+
public constructor(
|
|
38
|
+
private readonly pi: ExtensionAPI,
|
|
39
|
+
private readonly manager: TerminalSessionManager,
|
|
40
|
+
private readonly activityMode: "detailed" | "compact" | "off",
|
|
41
|
+
) {
|
|
42
|
+
pi.registerMessageRenderer("terminal-completion", (message, { expanded }, theme) => {
|
|
43
|
+
const content = new Container();
|
|
44
|
+
for (const session of completionSnapshots(message.details)) {
|
|
45
|
+
content.addChild(renderTerminalResult(session, expanded, theme));
|
|
46
|
+
}
|
|
47
|
+
const card = new Box(1, 1, (line) => theme.bg("toolSuccessBg", line));
|
|
48
|
+
card.addChild(content);
|
|
49
|
+
return card;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
manager.onDidChange(() => this.#renderActive());
|
|
53
|
+
manager.onDidComplete((session) => {
|
|
54
|
+
if (session.background) this.#queueCompletion(session);
|
|
55
|
+
this.#renderActive();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
pi.registerCommand("terminals", {
|
|
59
|
+
description: "Show active and recent terminal sessions",
|
|
60
|
+
handler: async (_args, context) => {
|
|
61
|
+
this.bind(context);
|
|
62
|
+
if (context.mode !== "tui") {
|
|
63
|
+
context.ui.notify("The terminal session overlay requires the TUI.", "info");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
await this.#showOverlay(context);
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public bind(context: ExtensionContext): void {
|
|
72
|
+
if (this.#closed) return;
|
|
73
|
+
this.#context = context;
|
|
74
|
+
this.#renderActive();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public dispose(): void {
|
|
78
|
+
this.#closed = true;
|
|
79
|
+
if (this.#deliveryTimer !== undefined) clearTimeout(this.#deliveryTimer);
|
|
80
|
+
if (this.#elapsedTimer !== undefined) clearInterval(this.#elapsedTimer);
|
|
81
|
+
this.#deliveryTimer = undefined;
|
|
82
|
+
this.#elapsedTimer = undefined;
|
|
83
|
+
this.#context?.ui.setWidget("agent-terminal", undefined);
|
|
84
|
+
this.#context?.ui.setStatus("agent-terminal", undefined);
|
|
85
|
+
this.#pending.clear();
|
|
86
|
+
this.#context = undefined;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
#queueCompletion(session: TerminalSession): void {
|
|
90
|
+
if (this.#closed || session.completionDelivered) return;
|
|
91
|
+
this.#pending.set(session.id, session);
|
|
92
|
+
if (this.#deliveryTimer !== undefined) return;
|
|
93
|
+
this.#deliveryTimer = setTimeout(() => {
|
|
94
|
+
this.#deliveryTimer = undefined;
|
|
95
|
+
const sessions = [...this.#pending.values()].filter(
|
|
96
|
+
(session) => !session.completionDelivered,
|
|
97
|
+
);
|
|
98
|
+
this.#pending.clear();
|
|
99
|
+
if (this.#closed || sessions.length === 0) return;
|
|
100
|
+
for (const completed of sessions) completed.completionDelivered = true;
|
|
101
|
+
const snapshots = sessions.map((completed) => this.manager.snapshot(completed));
|
|
102
|
+
const content = formatCompletionMessage(snapshots);
|
|
103
|
+
this.pi.sendMessage(
|
|
104
|
+
{
|
|
105
|
+
customType: "terminal-completion",
|
|
106
|
+
display: true,
|
|
107
|
+
content,
|
|
108
|
+
details: { sessions: snapshots },
|
|
109
|
+
},
|
|
110
|
+
{ triggerTurn: true, deliverAs: "steer" },
|
|
111
|
+
);
|
|
112
|
+
}, COMPLETION_DELAY_MS);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
#renderActive(): void {
|
|
116
|
+
const context = this.#context;
|
|
117
|
+
if (this.#closed || context === undefined || !context.hasUI) return;
|
|
118
|
+
const active = this.manager
|
|
119
|
+
.list()
|
|
120
|
+
.filter((session) => session.status === "running" || session.status === "stopping");
|
|
121
|
+
if (this.activityMode === "off") {
|
|
122
|
+
context.ui.setWidget("agent-terminal", undefined);
|
|
123
|
+
context.ui.setStatus("agent-terminal", undefined);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (active.length === 0) {
|
|
127
|
+
context.ui.setWidget("agent-terminal", undefined);
|
|
128
|
+
context.ui.setStatus("agent-terminal", undefined);
|
|
129
|
+
if (this.#elapsedTimer !== undefined) clearInterval(this.#elapsedTimer);
|
|
130
|
+
this.#elapsedTimer = undefined;
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (this.activityMode === "compact") {
|
|
134
|
+
context.ui.setWidget("agent-terminal", undefined);
|
|
135
|
+
context.ui.setStatus("agent-terminal", `terminals: ${active.length}`);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
context.ui.setStatus("agent-terminal", undefined);
|
|
139
|
+
context.ui.setWidget(
|
|
140
|
+
"agent-terminal",
|
|
141
|
+
(_tui, theme) => ({
|
|
142
|
+
invalidate() {},
|
|
143
|
+
render: (width: number) => renderTerminalWidgetLines(active, width, theme),
|
|
144
|
+
}),
|
|
145
|
+
{ placement: "belowEditor" },
|
|
146
|
+
);
|
|
147
|
+
if (this.#elapsedTimer === undefined) {
|
|
148
|
+
this.#elapsedTimer = setInterval(() => this.#renderActive(), 1_000);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async #showOverlay(context: ExtensionContext): Promise<void> {
|
|
153
|
+
for (;;) {
|
|
154
|
+
const sessions = this.manager
|
|
155
|
+
.list()
|
|
156
|
+
.filter((session) => session.status === "running" || session.status === "stopping");
|
|
157
|
+
if (sessions.length === 0) {
|
|
158
|
+
context.ui.notify("No active terminal sessions.", "info");
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const action = await context.ui.custom<TerminalOverlayAction | null>(
|
|
162
|
+
(tui, theme, _keybindings, done) => {
|
|
163
|
+
const items: SelectItem[] = sessions.map((session) => ({
|
|
164
|
+
value: session.source,
|
|
165
|
+
label: `${session.status} ${formatElapsed(session.elapsedMs)} ${session.shell}`,
|
|
166
|
+
description: shortCommand(session.command, 90),
|
|
167
|
+
}));
|
|
168
|
+
const list = new SelectList(items, Math.min(items.length, 12), {
|
|
169
|
+
selectedPrefix: (text) => theme.fg("accent", text),
|
|
170
|
+
selectedText: (text) => theme.fg("accent", text),
|
|
171
|
+
description: (text) => theme.fg("muted", text),
|
|
172
|
+
scrollInfo: (text) => theme.fg("dim", text),
|
|
173
|
+
noMatch: (text) => theme.fg("warning", text),
|
|
174
|
+
});
|
|
175
|
+
let page: "list" | "detail" | "confirm" = "list";
|
|
176
|
+
let selectedSource: string | undefined;
|
|
177
|
+
list.onSelect = (item) => {
|
|
178
|
+
selectedSource = item.value;
|
|
179
|
+
page = "detail";
|
|
180
|
+
};
|
|
181
|
+
list.onCancel = () => done(null);
|
|
182
|
+
return {
|
|
183
|
+
render: (width: number) => {
|
|
184
|
+
const container = new Container();
|
|
185
|
+
container.addChild(new DynamicBorder((text: string) => theme.fg("accent", text)));
|
|
186
|
+
if (page === "list") {
|
|
187
|
+
container.addChild(
|
|
188
|
+
new Text(theme.fg("accent", theme.bold("Terminal sessions")), 1, 0),
|
|
189
|
+
);
|
|
190
|
+
container.addChild(list);
|
|
191
|
+
container.addChild(
|
|
192
|
+
new Text(theme.fg("dim", "enter view · d delete · esc close"), 1, 0),
|
|
193
|
+
);
|
|
194
|
+
} else {
|
|
195
|
+
const session =
|
|
196
|
+
selectedSource === undefined ? undefined : this.manager.get(selectedSource);
|
|
197
|
+
if (session === undefined) {
|
|
198
|
+
container.addChild(
|
|
199
|
+
new Text(theme.fg("warning", "Terminal is no longer available."), 1, 0),
|
|
200
|
+
);
|
|
201
|
+
} else {
|
|
202
|
+
const snapshot = this.manager.snapshot(session);
|
|
203
|
+
container.addChild(
|
|
204
|
+
new Text(theme.fg("accent", theme.bold(`${snapshot.shell} terminal`)), 1, 0),
|
|
205
|
+
);
|
|
206
|
+
container.addChild(
|
|
207
|
+
new Text(
|
|
208
|
+
theme.fg(
|
|
209
|
+
"muted",
|
|
210
|
+
truncateToWidth(
|
|
211
|
+
`${snapshot.status} · ${formatElapsed(snapshot.elapsedMs)} · ${snapshot.cwd}`,
|
|
212
|
+
Math.max(1, width - 2),
|
|
213
|
+
),
|
|
214
|
+
),
|
|
215
|
+
1,
|
|
216
|
+
0,
|
|
217
|
+
),
|
|
218
|
+
);
|
|
219
|
+
container.addChild(new Text(theme.fg("text", `$ ${snapshot.command}`), 1, 0));
|
|
220
|
+
const rows = visibleScreenTail(this.manager.screenRows(session), 24);
|
|
221
|
+
container.addChild(
|
|
222
|
+
new Text(
|
|
223
|
+
rows.map((row) => theme.fg("toolOutput", row || " ")).join("\n"),
|
|
224
|
+
1,
|
|
225
|
+
0,
|
|
226
|
+
),
|
|
227
|
+
);
|
|
228
|
+
container.addChild(
|
|
229
|
+
new Text(
|
|
230
|
+
theme.fg(
|
|
231
|
+
page === "confirm" ? "warning" : "dim",
|
|
232
|
+
page === "confirm"
|
|
233
|
+
? "Delete this terminal session? y confirm · n/esc cancel"
|
|
234
|
+
: "esc back · d delete",
|
|
235
|
+
),
|
|
236
|
+
1,
|
|
237
|
+
0,
|
|
238
|
+
),
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
container.addChild(new DynamicBorder((text: string) => theme.fg("accent", text)));
|
|
243
|
+
return container.render(width);
|
|
244
|
+
},
|
|
245
|
+
handleInput: (data: string) => {
|
|
246
|
+
if (page === "list") {
|
|
247
|
+
if (data === "d") {
|
|
248
|
+
selectedSource = list.getSelectedItem()?.value;
|
|
249
|
+
if (selectedSource !== undefined) page = "confirm";
|
|
250
|
+
} else {
|
|
251
|
+
list.handleInput(data);
|
|
252
|
+
}
|
|
253
|
+
} else if (page === "detail") {
|
|
254
|
+
if (matchesKey(data, Key.escape)) page = "list";
|
|
255
|
+
else if (data === "d") page = "confirm";
|
|
256
|
+
} else if (data === "y" && selectedSource !== undefined) {
|
|
257
|
+
done({ kind: "delete", source: selectedSource });
|
|
258
|
+
} else if (data === "n" || matchesKey(data, Key.escape)) {
|
|
259
|
+
page = "detail";
|
|
260
|
+
}
|
|
261
|
+
tui.requestRender();
|
|
262
|
+
},
|
|
263
|
+
invalidate: () => list.invalidate(),
|
|
264
|
+
};
|
|
265
|
+
},
|
|
266
|
+
{ overlay: true, overlayOptions: { width: "75%", maxHeight: "80%", minWidth: 52 } },
|
|
267
|
+
);
|
|
268
|
+
if (action === null) return;
|
|
269
|
+
await this.manager.delete(action.source);
|
|
270
|
+
context.ui.notify("Terminal session deleted.", "info");
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function visibleScreenTail(rows: readonly string[], limit: number): readonly string[] {
|
|
276
|
+
const first = rows.findIndex((row) => row.length > 0);
|
|
277
|
+
if (first === -1) return ["(no visible output)"];
|
|
278
|
+
let last = rows.length - 1;
|
|
279
|
+
while (last > first && rows[last]?.length === 0) last -= 1;
|
|
280
|
+
return rows.slice(Math.max(first, last - limit + 1), last + 1);
|
|
281
|
+
}
|
|
282
|
+
type TerminalOverlayAction = { readonly kind: "delete"; readonly source: string };
|
|
283
|
+
function completionSnapshots(details: unknown): readonly TerminalSessionSnapshot[] {
|
|
284
|
+
if (!isRecord(details) || !Array.isArray(details.sessions)) return [];
|
|
285
|
+
return details.sessions.filter(isTerminalSessionSnapshot);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function isTerminalSessionSnapshot(value: unknown): value is TerminalSessionSnapshot {
|
|
289
|
+
return (
|
|
290
|
+
isRecord(value) &&
|
|
291
|
+
typeof value.id === "string" &&
|
|
292
|
+
typeof value.source === "string" &&
|
|
293
|
+
typeof value.command === "string" &&
|
|
294
|
+
typeof value.cwd === "string" &&
|
|
295
|
+
typeof value.shell === "string" &&
|
|
296
|
+
(value.shellFamily === "posix" ||
|
|
297
|
+
value.shellFamily === "powershell" ||
|
|
298
|
+
value.shellFamily === "cmd") &&
|
|
299
|
+
typeof value.background === "boolean" &&
|
|
300
|
+
typeof value.status === "string" &&
|
|
301
|
+
typeof value.startedAt === "number" &&
|
|
302
|
+
typeof value.elapsedMs === "number" &&
|
|
303
|
+
typeof value.output === "string" &&
|
|
304
|
+
typeof value.outputStart === "number" &&
|
|
305
|
+
typeof value.outputEnd === "number" &&
|
|
306
|
+
typeof value.truncated === "boolean" &&
|
|
307
|
+
typeof value.cols === "number" &&
|
|
308
|
+
typeof value.rows === "number"
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function formatCompletionMessage(sessions: readonly TerminalSessionSnapshot[]): string {
|
|
313
|
+
const heading =
|
|
314
|
+
sessions.length === 1 ? "Background terminal completed:" : "Background terminals completed:";
|
|
315
|
+
return `${heading}\n${sessions
|
|
316
|
+
.map((session) => {
|
|
317
|
+
const exit = session.exitCode === undefined ? "" : `, exit ${session.exitCode}`;
|
|
318
|
+
const excerpt = session.output.trimEnd().slice(-2_000);
|
|
319
|
+
return `- ${session.source}: ${session.status}${exit}, ${formatElapsed(session.elapsedMs)}\n${excerpt || "(no output)"}`;
|
|
320
|
+
})
|
|
321
|
+
.join("\n")}`;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function shortCommand(command: string, max = 60): string {
|
|
325
|
+
const singleLine = command.replaceAll(/\s+/gu, " ").trim();
|
|
326
|
+
return singleLine.length <= max ? singleLine : `${singleLine.slice(0, max - 1)}…`;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function formatElapsed(milliseconds: number): string {
|
|
330
|
+
if (milliseconds < 1_000) return `${milliseconds}ms`;
|
|
331
|
+
return `${(milliseconds / 1_000).toFixed(milliseconds < 10_000 ? 1 : 0)}s`;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
335
|
+
return typeof value === "object" && value !== null;
|
|
336
|
+
}
|
package/dist/chunk-VCCMVQXU.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{a as t}from"./chunk-EI7MMDWY.js";import{Type as e}from"typebox";import{Value as n}from"typebox/value";var r=e.Function([],e.Unknown()),i=e.Object({tool:e.String({pattern:"\\S"}),fallback:e.Optional(e.Boolean()),renderCall:e.Optional(r),renderResult:e.Optional(r),renderShell:e.Optional(e.Union([e.Literal("default"),e.Literal("self")]))});function d(o){return n.Check(i,o)?o.renderCall!==void 0||o.renderResult!==void 0:!1}t(d,"isTextEditorToolRendererRegistration");export{d as a};
|