@mariozechner/pi-coding-agent 0.38.0 → 0.39.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 +38 -0
- package/README.md +1 -0
- package/dist/cli/args.d.ts +1 -0
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +5 -1
- package/dist/cli/args.js.map +1 -1
- package/dist/core/agent-session.d.ts +23 -0
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +62 -34
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/bash-executor.d.ts +6 -0
- package/dist/core/bash-executor.d.ts.map +1 -1
- package/dist/core/bash-executor.js +77 -0
- package/dist/core/bash-executor.js.map +1 -1
- package/dist/core/extensions/index.d.ts +1 -1
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +4 -3
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +44 -7
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +39 -3
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/sdk.d.ts +5 -2
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +20 -12
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +1 -5
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/tools/bash.d.ts +25 -1
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +103 -73
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/edit.d.ts +17 -1
- package/dist/core/tools/edit.d.ts.map +1 -1
- package/dist/core/tools/edit.js +12 -5
- package/dist/core/tools/edit.js.map +1 -1
- package/dist/core/tools/find.d.ts +18 -1
- package/dist/core/tools/find.d.ts.map +1 -1
- package/dist/core/tools/find.js +68 -18
- package/dist/core/tools/find.js.map +1 -1
- package/dist/core/tools/grep.d.ts +15 -1
- package/dist/core/tools/grep.d.ts.map +1 -1
- package/dist/core/tools/grep.js +22 -10
- package/dist/core/tools/grep.js.map +1 -1
- package/dist/core/tools/index.d.ts +7 -7
- package/dist/core/tools/index.d.ts.map +1 -1
- package/dist/core/tools/index.js +1 -1
- package/dist/core/tools/index.js.map +1 -1
- package/dist/core/tools/ls.d.ts +21 -1
- package/dist/core/tools/ls.d.ts.map +1 -1
- package/dist/core/tools/ls.js +80 -72
- package/dist/core/tools/ls.js.map +1 -1
- package/dist/core/tools/read.d.ts +14 -0
- package/dist/core/tools/read.d.ts.map +1 -1
- package/dist/core/tools/read.js +12 -5
- package/dist/core/tools/read.js.map +1 -1
- package/dist/core/tools/write.d.ts +15 -1
- package/dist/core/tools/write.d.ts.map +1 -1
- package/dist/core/tools/write.js +9 -4
- package/dist/core/tools/write.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +11 -1
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/assistant-message.js +7 -3
- package/dist/modes/interactive/components/assistant-message.js.map +1 -1
- package/dist/modes/interactive/components/tool-execution.d.ts +6 -0
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution.js +50 -23
- package/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +153 -46
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/theme/theme.d.ts +7 -0
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
- package/dist/modes/interactive/theme/theme.js +40 -0
- package/dist/modes/interactive/theme/theme.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +10 -0
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/utils/clipboard.d.ts.map +1 -1
- package/dist/utils/clipboard.js +35 -7
- package/dist/utils/clipboard.js.map +1 -1
- package/docs/extensions.md +101 -6
- package/docs/sdk.md +3 -3
- package/examples/extensions/README.md +2 -0
- package/examples/extensions/claude-rules.ts +5 -2
- package/examples/extensions/interactive-shell.ts +196 -0
- package/examples/extensions/mac-system-theme.ts +45 -0
- package/examples/extensions/overlay-test.ts +145 -0
- package/examples/extensions/pirate.ts +7 -4
- package/examples/extensions/preset.ts +2 -2
- package/examples/extensions/ssh.ts +220 -0
- package/examples/extensions/tool-override.ts +143 -0
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/examples/sdk/04-skills.ts +4 -1
- package/package.json +5 -5
|
@@ -61,7 +61,7 @@ export default function claudeRulesExtension(pi: ExtensionAPI) {
|
|
|
61
61
|
});
|
|
62
62
|
|
|
63
63
|
// Append available rules to system prompt
|
|
64
|
-
pi.on("before_agent_start", async () => {
|
|
64
|
+
pi.on("before_agent_start", async (event) => {
|
|
65
65
|
if (ruleFiles.length === 0) {
|
|
66
66
|
return;
|
|
67
67
|
}
|
|
@@ -69,7 +69,10 @@ export default function claudeRulesExtension(pi: ExtensionAPI) {
|
|
|
69
69
|
const rulesList = ruleFiles.map((f) => `- .claude/rules/${f}`).join("\n");
|
|
70
70
|
|
|
71
71
|
return {
|
|
72
|
-
|
|
72
|
+
systemPrompt:
|
|
73
|
+
event.systemPrompt +
|
|
74
|
+
`
|
|
75
|
+
|
|
73
76
|
## Project Rules
|
|
74
77
|
|
|
75
78
|
The following project rules are available in .claude/rules/:
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interactive Shell Commands Extension
|
|
3
|
+
*
|
|
4
|
+
* Enables running interactive commands (vim, git rebase -i, htop, etc.)
|
|
5
|
+
* with full terminal access. The TUI suspends while they run.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* pi -e examples/extensions/interactive-shell.ts
|
|
9
|
+
*
|
|
10
|
+
* !vim file.txt # Auto-detected as interactive
|
|
11
|
+
* !i any-command # Force interactive mode with !i prefix
|
|
12
|
+
* !git rebase -i HEAD~3
|
|
13
|
+
* !htop
|
|
14
|
+
*
|
|
15
|
+
* Configuration via environment variables:
|
|
16
|
+
* INTERACTIVE_COMMANDS - Additional commands (comma-separated)
|
|
17
|
+
* INTERACTIVE_EXCLUDE - Commands to exclude (comma-separated)
|
|
18
|
+
*
|
|
19
|
+
* Note: This only intercepts user `!` commands, not agent bash tool calls.
|
|
20
|
+
* If the agent runs an interactive command, it will fail (which is fine).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { spawnSync } from "node:child_process";
|
|
24
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
25
|
+
|
|
26
|
+
// Default interactive commands - editors, pagers, git ops, TUIs
|
|
27
|
+
const DEFAULT_INTERACTIVE_COMMANDS = [
|
|
28
|
+
// Editors
|
|
29
|
+
"vim",
|
|
30
|
+
"nvim",
|
|
31
|
+
"vi",
|
|
32
|
+
"nano",
|
|
33
|
+
"emacs",
|
|
34
|
+
"pico",
|
|
35
|
+
"micro",
|
|
36
|
+
"helix",
|
|
37
|
+
"hx",
|
|
38
|
+
"kak",
|
|
39
|
+
// Pagers
|
|
40
|
+
"less",
|
|
41
|
+
"more",
|
|
42
|
+
"most",
|
|
43
|
+
// Git interactive
|
|
44
|
+
"git commit",
|
|
45
|
+
"git rebase",
|
|
46
|
+
"git merge",
|
|
47
|
+
"git cherry-pick",
|
|
48
|
+
"git revert",
|
|
49
|
+
"git add -p",
|
|
50
|
+
"git add --patch",
|
|
51
|
+
"git add -i",
|
|
52
|
+
"git add --interactive",
|
|
53
|
+
"git stash -p",
|
|
54
|
+
"git stash --patch",
|
|
55
|
+
"git reset -p",
|
|
56
|
+
"git reset --patch",
|
|
57
|
+
"git checkout -p",
|
|
58
|
+
"git checkout --patch",
|
|
59
|
+
"git difftool",
|
|
60
|
+
"git mergetool",
|
|
61
|
+
// System monitors
|
|
62
|
+
"htop",
|
|
63
|
+
"top",
|
|
64
|
+
"btop",
|
|
65
|
+
"glances",
|
|
66
|
+
// File managers
|
|
67
|
+
"ranger",
|
|
68
|
+
"nnn",
|
|
69
|
+
"lf",
|
|
70
|
+
"mc",
|
|
71
|
+
"vifm",
|
|
72
|
+
// Git TUIs
|
|
73
|
+
"tig",
|
|
74
|
+
"lazygit",
|
|
75
|
+
"gitui",
|
|
76
|
+
// Fuzzy finders
|
|
77
|
+
"fzf",
|
|
78
|
+
"sk",
|
|
79
|
+
// Remote sessions
|
|
80
|
+
"ssh",
|
|
81
|
+
"telnet",
|
|
82
|
+
"mosh",
|
|
83
|
+
// Database clients
|
|
84
|
+
"psql",
|
|
85
|
+
"mysql",
|
|
86
|
+
"sqlite3",
|
|
87
|
+
"mongosh",
|
|
88
|
+
"redis-cli",
|
|
89
|
+
// Kubernetes/Docker
|
|
90
|
+
"kubectl edit",
|
|
91
|
+
"kubectl exec -it",
|
|
92
|
+
"docker exec -it",
|
|
93
|
+
"docker run -it",
|
|
94
|
+
// Other
|
|
95
|
+
"tmux",
|
|
96
|
+
"screen",
|
|
97
|
+
"ncdu",
|
|
98
|
+
];
|
|
99
|
+
|
|
100
|
+
function getInteractiveCommands(): string[] {
|
|
101
|
+
const additional =
|
|
102
|
+
process.env.INTERACTIVE_COMMANDS?.split(",")
|
|
103
|
+
.map((s) => s.trim())
|
|
104
|
+
.filter(Boolean) ?? [];
|
|
105
|
+
const excluded = new Set(process.env.INTERACTIVE_EXCLUDE?.split(",").map((s) => s.trim().toLowerCase()) ?? []);
|
|
106
|
+
return [...DEFAULT_INTERACTIVE_COMMANDS, ...additional].filter((cmd) => !excluded.has(cmd.toLowerCase()));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function isInteractiveCommand(command: string): boolean {
|
|
110
|
+
const trimmed = command.trim().toLowerCase();
|
|
111
|
+
const commands = getInteractiveCommands();
|
|
112
|
+
|
|
113
|
+
for (const cmd of commands) {
|
|
114
|
+
const cmdLower = cmd.toLowerCase();
|
|
115
|
+
// Match at start
|
|
116
|
+
if (trimmed === cmdLower || trimmed.startsWith(`${cmdLower} `) || trimmed.startsWith(`${cmdLower}\t`)) {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
// Match after pipe: "cat file | less"
|
|
120
|
+
const pipeIdx = trimmed.lastIndexOf("|");
|
|
121
|
+
if (pipeIdx !== -1) {
|
|
122
|
+
const afterPipe = trimmed.slice(pipeIdx + 1).trim();
|
|
123
|
+
if (afterPipe === cmdLower || afterPipe.startsWith(`${cmdLower} `)) {
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export default function (pi: ExtensionAPI) {
|
|
132
|
+
pi.on("user_bash", async (event, ctx) => {
|
|
133
|
+
let command = event.command;
|
|
134
|
+
let forceInteractive = false;
|
|
135
|
+
|
|
136
|
+
// Check for !i prefix (command comes without the leading !)
|
|
137
|
+
// The prefix parsing happens before this event, so we check if command starts with "i "
|
|
138
|
+
if (command.startsWith("i ") || command.startsWith("i\t")) {
|
|
139
|
+
forceInteractive = true;
|
|
140
|
+
command = command.slice(2).trim();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const shouldBeInteractive = forceInteractive || isInteractiveCommand(command);
|
|
144
|
+
if (!shouldBeInteractive) {
|
|
145
|
+
return; // Let normal handling proceed
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// No UI available (print mode, RPC, etc.)
|
|
149
|
+
if (!ctx.hasUI) {
|
|
150
|
+
return {
|
|
151
|
+
result: { output: "(interactive commands require TUI)", exitCode: 1, cancelled: false, truncated: false },
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Use ctx.ui.custom() to get TUI access, then run the command
|
|
156
|
+
const exitCode = await ctx.ui.custom<number | null>((tui, _theme, _kb, done) => {
|
|
157
|
+
// Stop TUI to release terminal
|
|
158
|
+
tui.stop();
|
|
159
|
+
|
|
160
|
+
// Clear screen
|
|
161
|
+
process.stdout.write("\x1b[2J\x1b[H");
|
|
162
|
+
|
|
163
|
+
// Run command with full terminal access
|
|
164
|
+
const shell = process.env.SHELL || "/bin/sh";
|
|
165
|
+
const result = spawnSync(shell, ["-c", command], {
|
|
166
|
+
stdio: "inherit",
|
|
167
|
+
env: process.env,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Restart TUI
|
|
171
|
+
tui.start();
|
|
172
|
+
tui.requestRender(true);
|
|
173
|
+
|
|
174
|
+
// Signal completion
|
|
175
|
+
done(result.status);
|
|
176
|
+
|
|
177
|
+
// Return empty component (immediately disposed since done() was called)
|
|
178
|
+
return { render: () => [], invalidate: () => {} };
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Return result to prevent default bash handling
|
|
182
|
+
const output =
|
|
183
|
+
exitCode === 0
|
|
184
|
+
? "(interactive command completed successfully)"
|
|
185
|
+
: `(interactive command exited with code ${exitCode})`;
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
result: {
|
|
189
|
+
output,
|
|
190
|
+
exitCode: exitCode ?? 1,
|
|
191
|
+
cancelled: false,
|
|
192
|
+
truncated: false,
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
});
|
|
196
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Syncs pi theme with macOS system appearance (dark/light mode).
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* pi -e examples/extensions/mac-system-theme.ts
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { execSync } from "node:child_process";
|
|
9
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
10
|
+
|
|
11
|
+
function isDarkMode(): boolean {
|
|
12
|
+
try {
|
|
13
|
+
const result = execSync(
|
|
14
|
+
"osascript -e 'tell application \"System Events\" to tell appearance preferences to return dark mode'",
|
|
15
|
+
{ encoding: "utf-8" },
|
|
16
|
+
);
|
|
17
|
+
return result.trim() === "true";
|
|
18
|
+
} catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default function (pi: ExtensionAPI) {
|
|
24
|
+
let intervalId: ReturnType<typeof setInterval> | null = null;
|
|
25
|
+
|
|
26
|
+
pi.on("session_start", (_event, ctx) => {
|
|
27
|
+
let currentTheme = isDarkMode() ? "dark" : "light";
|
|
28
|
+
ctx.ui.setTheme(currentTheme);
|
|
29
|
+
|
|
30
|
+
intervalId = setInterval(() => {
|
|
31
|
+
const newTheme = isDarkMode() ? "dark" : "light";
|
|
32
|
+
if (newTheme !== currentTheme) {
|
|
33
|
+
currentTheme = newTheme;
|
|
34
|
+
ctx.ui.setTheme(currentTheme);
|
|
35
|
+
}
|
|
36
|
+
}, 2000);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
pi.on("session_shutdown", () => {
|
|
40
|
+
if (intervalId) {
|
|
41
|
+
clearInterval(intervalId);
|
|
42
|
+
intervalId = null;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Overlay Test - validates overlay compositing with inline text inputs
|
|
3
|
+
*
|
|
4
|
+
* Usage: pi --extension ./examples/extensions/overlay-test.ts
|
|
5
|
+
*
|
|
6
|
+
* Run /overlay-test to show a floating overlay with:
|
|
7
|
+
* - Inline text inputs within menu items
|
|
8
|
+
* - Edge case tests (wide chars, styled text, emoji)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { ExtensionAPI, ExtensionCommandContext, Theme } from "@mariozechner/pi-coding-agent";
|
|
12
|
+
import { matchesKey, visibleWidth } from "@mariozechner/pi-tui";
|
|
13
|
+
|
|
14
|
+
export default function (pi: ExtensionAPI) {
|
|
15
|
+
pi.registerCommand("overlay-test", {
|
|
16
|
+
description: "Test overlay rendering with edge cases",
|
|
17
|
+
handler: async (_args: string, ctx: ExtensionCommandContext) => {
|
|
18
|
+
const result = await ctx.ui.custom<{ action: string; query?: string } | undefined>(
|
|
19
|
+
(_tui, theme, _keybindings, done) => new OverlayTestComponent(theme, done),
|
|
20
|
+
{ overlay: true },
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
if (result) {
|
|
24
|
+
const msg = result.query ? `${result.action}: "${result.query}"` : result.action;
|
|
25
|
+
ctx.ui.notify(msg, "info");
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
class OverlayTestComponent {
|
|
32
|
+
readonly width = 70;
|
|
33
|
+
|
|
34
|
+
private selected = 0;
|
|
35
|
+
private items = [
|
|
36
|
+
{ label: "Search", hasInput: true, text: "", cursor: 0 },
|
|
37
|
+
{ label: "Run", hasInput: true, text: "", cursor: 0 },
|
|
38
|
+
{ label: "Settings", hasInput: false, text: "", cursor: 0 },
|
|
39
|
+
{ label: "Cancel", hasInput: false, text: "", cursor: 0 },
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
constructor(
|
|
43
|
+
private theme: Theme,
|
|
44
|
+
private done: (result: { action: string; query?: string } | undefined) => void,
|
|
45
|
+
) {}
|
|
46
|
+
|
|
47
|
+
handleInput(data: string): void {
|
|
48
|
+
if (matchesKey(data, "escape")) {
|
|
49
|
+
this.done(undefined);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const current = this.items[this.selected]!;
|
|
54
|
+
|
|
55
|
+
if (matchesKey(data, "return")) {
|
|
56
|
+
this.done({ action: current.label, query: current.hasInput ? current.text : undefined });
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (matchesKey(data, "up")) {
|
|
61
|
+
this.selected = Math.max(0, this.selected - 1);
|
|
62
|
+
} else if (matchesKey(data, "down")) {
|
|
63
|
+
this.selected = Math.min(this.items.length - 1, this.selected + 1);
|
|
64
|
+
} else if (current.hasInput) {
|
|
65
|
+
if (matchesKey(data, "backspace")) {
|
|
66
|
+
if (current.cursor > 0) {
|
|
67
|
+
current.text = current.text.slice(0, current.cursor - 1) + current.text.slice(current.cursor);
|
|
68
|
+
current.cursor--;
|
|
69
|
+
}
|
|
70
|
+
} else if (matchesKey(data, "left")) {
|
|
71
|
+
current.cursor = Math.max(0, current.cursor - 1);
|
|
72
|
+
} else if (matchesKey(data, "right")) {
|
|
73
|
+
current.cursor = Math.min(current.text.length, current.cursor + 1);
|
|
74
|
+
} else if (data.length === 1 && data.charCodeAt(0) >= 32) {
|
|
75
|
+
current.text = current.text.slice(0, current.cursor) + data + current.text.slice(current.cursor);
|
|
76
|
+
current.cursor++;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
render(_width: number): string[] {
|
|
82
|
+
const w = this.width;
|
|
83
|
+
const th = this.theme;
|
|
84
|
+
const innerW = w - 2;
|
|
85
|
+
const lines: string[] = [];
|
|
86
|
+
|
|
87
|
+
const pad = (s: string, len: number) => {
|
|
88
|
+
const vis = visibleWidth(s);
|
|
89
|
+
return s + " ".repeat(Math.max(0, len - vis));
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const row = (content: string) => th.fg("border", "│") + pad(content, innerW) + th.fg("border", "│");
|
|
93
|
+
|
|
94
|
+
lines.push(th.fg("border", `╭${"─".repeat(innerW)}╮`));
|
|
95
|
+
lines.push(row(` ${th.fg("accent", "🧪 Overlay Test")}`));
|
|
96
|
+
lines.push(row(""));
|
|
97
|
+
|
|
98
|
+
// Edge cases - full width lines to test compositing at boundaries
|
|
99
|
+
lines.push(row(` ${th.fg("dim", "─── Edge Cases (borders should align) ───")}`));
|
|
100
|
+
lines.push(row(` Wide: ${th.fg("warning", "中文日本語한글テスト漢字繁體简体ひらがなカタカナ가나다라마바")}`));
|
|
101
|
+
lines.push(
|
|
102
|
+
row(
|
|
103
|
+
` Styled: ${th.fg("error", "RED")} ${th.fg("success", "GREEN")} ${th.fg("warning", "YELLOW")} ${th.fg("accent", "ACCENT")} ${th.fg("dim", "DIM")} ${th.fg("error", "more")} ${th.fg("success", "colors")}`,
|
|
104
|
+
),
|
|
105
|
+
);
|
|
106
|
+
lines.push(row(" Emoji: 👨👩👧👦 🇯🇵 🚀 💻 🎉 🔥 😀 🎯 🌟 💡 🎨 🔧 📦 🏆 🌈 🎪 🎭 🎬 🎮 🎲"));
|
|
107
|
+
lines.push(row(""));
|
|
108
|
+
|
|
109
|
+
// Menu with inline inputs
|
|
110
|
+
lines.push(row(` ${th.fg("dim", "─── Actions ───")}`));
|
|
111
|
+
|
|
112
|
+
for (let i = 0; i < this.items.length; i++) {
|
|
113
|
+
const item = this.items[i]!;
|
|
114
|
+
const isSelected = i === this.selected;
|
|
115
|
+
const prefix = isSelected ? " ▶ " : " ";
|
|
116
|
+
|
|
117
|
+
let content: string;
|
|
118
|
+
if (item.hasInput) {
|
|
119
|
+
const label = isSelected ? th.fg("accent", `${item.label}:`) : th.fg("text", `${item.label}:`);
|
|
120
|
+
|
|
121
|
+
let inputDisplay = item.text;
|
|
122
|
+
if (isSelected) {
|
|
123
|
+
const before = inputDisplay.slice(0, item.cursor);
|
|
124
|
+
const cursorChar = item.cursor < inputDisplay.length ? inputDisplay[item.cursor] : " ";
|
|
125
|
+
const after = inputDisplay.slice(item.cursor + 1);
|
|
126
|
+
inputDisplay = `${before}\x1b[7m${cursorChar}\x1b[27m${after}`;
|
|
127
|
+
}
|
|
128
|
+
content = `${prefix + label} ${inputDisplay}`;
|
|
129
|
+
} else {
|
|
130
|
+
content = prefix + (isSelected ? th.fg("accent", item.label) : th.fg("text", item.label));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
lines.push(row(content));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
lines.push(row(""));
|
|
137
|
+
lines.push(row(` ${th.fg("dim", "↑↓ navigate • type to input • Enter select • Esc cancel")}`));
|
|
138
|
+
lines.push(th.fg("border", `╰${"─".repeat(innerW)}╯`));
|
|
139
|
+
|
|
140
|
+
return lines;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
invalidate(): void {}
|
|
144
|
+
dispose(): void {}
|
|
145
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Pirate Extension
|
|
3
3
|
*
|
|
4
|
-
* Demonstrates
|
|
5
|
-
*
|
|
4
|
+
* Demonstrates modifying the system prompt in before_agent_start to dynamically
|
|
5
|
+
* change agent behavior based on extension state.
|
|
6
6
|
*
|
|
7
7
|
* Usage:
|
|
8
8
|
* 1. Copy this file to ~/.pi/agent/extensions/ or your project's .pi/extensions/
|
|
@@ -25,10 +25,13 @@ export default function pirateExtension(pi: ExtensionAPI) {
|
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
// Append to system prompt when pirate mode is enabled
|
|
28
|
-
pi.on("before_agent_start", async () => {
|
|
28
|
+
pi.on("before_agent_start", async (event) => {
|
|
29
29
|
if (pirateMode) {
|
|
30
30
|
return {
|
|
31
|
-
|
|
31
|
+
systemPrompt:
|
|
32
|
+
event.systemPrompt +
|
|
33
|
+
`
|
|
34
|
+
|
|
32
35
|
IMPORTANT: You are now in PIRATE MODE. You must:
|
|
33
36
|
- Speak like a stereotypical pirate in all responses
|
|
34
37
|
- Use phrases like "Arrr!", "Ahoy!", "Shiver me timbers!", "Avast!", "Ye scurvy dog!"
|
|
@@ -345,10 +345,10 @@ export default function presetExtension(pi: ExtensionAPI) {
|
|
|
345
345
|
});
|
|
346
346
|
|
|
347
347
|
// Inject preset instructions into system prompt
|
|
348
|
-
pi.on("before_agent_start", async () => {
|
|
348
|
+
pi.on("before_agent_start", async (event) => {
|
|
349
349
|
if (activePreset?.instructions) {
|
|
350
350
|
return {
|
|
351
|
-
|
|
351
|
+
systemPrompt: `${event.systemPrompt}\n\n${activePreset.instructions}`,
|
|
352
352
|
};
|
|
353
353
|
}
|
|
354
354
|
});
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSH Remote Execution Example
|
|
3
|
+
*
|
|
4
|
+
* Demonstrates delegating tool operations to a remote machine via SSH.
|
|
5
|
+
* When --ssh is provided, read/write/edit/bash run on the remote.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* pi -e ./ssh.ts --ssh user@host
|
|
9
|
+
* pi -e ./ssh.ts --ssh user@host:/remote/path
|
|
10
|
+
*
|
|
11
|
+
* Requirements:
|
|
12
|
+
* - SSH key-based auth (no password prompts)
|
|
13
|
+
* - bash on remote
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { spawn } from "node:child_process";
|
|
17
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
18
|
+
import {
|
|
19
|
+
type BashOperations,
|
|
20
|
+
createBashTool,
|
|
21
|
+
createEditTool,
|
|
22
|
+
createReadTool,
|
|
23
|
+
createWriteTool,
|
|
24
|
+
type EditOperations,
|
|
25
|
+
type ReadOperations,
|
|
26
|
+
type WriteOperations,
|
|
27
|
+
} from "@mariozechner/pi-coding-agent";
|
|
28
|
+
|
|
29
|
+
function sshExec(remote: string, command: string): Promise<Buffer> {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const child = spawn("ssh", [remote, command], { stdio: ["ignore", "pipe", "pipe"] });
|
|
32
|
+
const chunks: Buffer[] = [];
|
|
33
|
+
const errChunks: Buffer[] = [];
|
|
34
|
+
child.stdout.on("data", (data) => chunks.push(data));
|
|
35
|
+
child.stderr.on("data", (data) => errChunks.push(data));
|
|
36
|
+
child.on("error", reject);
|
|
37
|
+
child.on("close", (code) => {
|
|
38
|
+
if (code !== 0) {
|
|
39
|
+
reject(new Error(`SSH failed (${code}): ${Buffer.concat(errChunks).toString()}`));
|
|
40
|
+
} else {
|
|
41
|
+
resolve(Buffer.concat(chunks));
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function createRemoteReadOps(remote: string, remoteCwd: string, localCwd: string): ReadOperations {
|
|
48
|
+
const toRemote = (p: string) => p.replace(localCwd, remoteCwd);
|
|
49
|
+
return {
|
|
50
|
+
readFile: (p) => sshExec(remote, `cat ${JSON.stringify(toRemote(p))}`),
|
|
51
|
+
access: (p) => sshExec(remote, `test -r ${JSON.stringify(toRemote(p))}`).then(() => {}),
|
|
52
|
+
detectImageMimeType: async (p) => {
|
|
53
|
+
try {
|
|
54
|
+
const r = await sshExec(remote, `file --mime-type -b ${JSON.stringify(toRemote(p))}`);
|
|
55
|
+
const m = r.toString().trim();
|
|
56
|
+
return ["image/jpeg", "image/png", "image/gif", "image/webp"].includes(m) ? m : null;
|
|
57
|
+
} catch {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function createRemoteWriteOps(remote: string, remoteCwd: string, localCwd: string): WriteOperations {
|
|
65
|
+
const toRemote = (p: string) => p.replace(localCwd, remoteCwd);
|
|
66
|
+
return {
|
|
67
|
+
writeFile: async (p, content) => {
|
|
68
|
+
const b64 = Buffer.from(content).toString("base64");
|
|
69
|
+
await sshExec(remote, `echo ${JSON.stringify(b64)} | base64 -d > ${JSON.stringify(toRemote(p))}`);
|
|
70
|
+
},
|
|
71
|
+
mkdir: (dir) => sshExec(remote, `mkdir -p ${JSON.stringify(toRemote(dir))}`).then(() => {}),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function createRemoteEditOps(remote: string, remoteCwd: string, localCwd: string): EditOperations {
|
|
76
|
+
const r = createRemoteReadOps(remote, remoteCwd, localCwd);
|
|
77
|
+
const w = createRemoteWriteOps(remote, remoteCwd, localCwd);
|
|
78
|
+
return { readFile: r.readFile, access: r.access, writeFile: w.writeFile };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function createRemoteBashOps(remote: string, remoteCwd: string, localCwd: string): BashOperations {
|
|
82
|
+
const toRemote = (p: string) => p.replace(localCwd, remoteCwd);
|
|
83
|
+
return {
|
|
84
|
+
exec: (command, cwd, { onData, signal, timeout }) =>
|
|
85
|
+
new Promise((resolve, reject) => {
|
|
86
|
+
const cmd = `cd ${JSON.stringify(toRemote(cwd))} && ${command}`;
|
|
87
|
+
const child = spawn("ssh", [remote, cmd], { stdio: ["ignore", "pipe", "pipe"] });
|
|
88
|
+
let timedOut = false;
|
|
89
|
+
const timer = timeout
|
|
90
|
+
? setTimeout(() => {
|
|
91
|
+
timedOut = true;
|
|
92
|
+
child.kill();
|
|
93
|
+
}, timeout * 1000)
|
|
94
|
+
: undefined;
|
|
95
|
+
child.stdout.on("data", onData);
|
|
96
|
+
child.stderr.on("data", onData);
|
|
97
|
+
child.on("error", (e) => {
|
|
98
|
+
if (timer) clearTimeout(timer);
|
|
99
|
+
reject(e);
|
|
100
|
+
});
|
|
101
|
+
const onAbort = () => child.kill();
|
|
102
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
103
|
+
child.on("close", (code) => {
|
|
104
|
+
if (timer) clearTimeout(timer);
|
|
105
|
+
signal?.removeEventListener("abort", onAbort);
|
|
106
|
+
if (signal?.aborted) reject(new Error("aborted"));
|
|
107
|
+
else if (timedOut) reject(new Error(`timeout:${timeout}`));
|
|
108
|
+
else resolve({ exitCode: code });
|
|
109
|
+
});
|
|
110
|
+
}),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export default function (pi: ExtensionAPI) {
|
|
115
|
+
pi.registerFlag("ssh", { description: "SSH remote: user@host or user@host:/path", type: "string" });
|
|
116
|
+
|
|
117
|
+
const localCwd = process.cwd();
|
|
118
|
+
const localRead = createReadTool(localCwd);
|
|
119
|
+
const localWrite = createWriteTool(localCwd);
|
|
120
|
+
const localEdit = createEditTool(localCwd);
|
|
121
|
+
const localBash = createBashTool(localCwd);
|
|
122
|
+
|
|
123
|
+
// Resolved lazily on session_start (CLI flags not available during factory)
|
|
124
|
+
let resolvedSsh: { remote: string; remoteCwd: string } | null = null;
|
|
125
|
+
|
|
126
|
+
const getSsh = () => resolvedSsh;
|
|
127
|
+
|
|
128
|
+
pi.registerTool({
|
|
129
|
+
...localRead,
|
|
130
|
+
async execute(id, params, onUpdate, _ctx, signal) {
|
|
131
|
+
const ssh = getSsh();
|
|
132
|
+
if (ssh) {
|
|
133
|
+
const tool = createReadTool(localCwd, {
|
|
134
|
+
operations: createRemoteReadOps(ssh.remote, ssh.remoteCwd, localCwd),
|
|
135
|
+
});
|
|
136
|
+
return tool.execute(id, params, signal, onUpdate);
|
|
137
|
+
}
|
|
138
|
+
return localRead.execute(id, params, signal, onUpdate);
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
pi.registerTool({
|
|
143
|
+
...localWrite,
|
|
144
|
+
async execute(id, params, onUpdate, _ctx, signal) {
|
|
145
|
+
const ssh = getSsh();
|
|
146
|
+
if (ssh) {
|
|
147
|
+
const tool = createWriteTool(localCwd, {
|
|
148
|
+
operations: createRemoteWriteOps(ssh.remote, ssh.remoteCwd, localCwd),
|
|
149
|
+
});
|
|
150
|
+
return tool.execute(id, params, signal, onUpdate);
|
|
151
|
+
}
|
|
152
|
+
return localWrite.execute(id, params, signal, onUpdate);
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
pi.registerTool({
|
|
157
|
+
...localEdit,
|
|
158
|
+
async execute(id, params, onUpdate, _ctx, signal) {
|
|
159
|
+
const ssh = getSsh();
|
|
160
|
+
if (ssh) {
|
|
161
|
+
const tool = createEditTool(localCwd, {
|
|
162
|
+
operations: createRemoteEditOps(ssh.remote, ssh.remoteCwd, localCwd),
|
|
163
|
+
});
|
|
164
|
+
return tool.execute(id, params, signal, onUpdate);
|
|
165
|
+
}
|
|
166
|
+
return localEdit.execute(id, params, signal, onUpdate);
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
pi.registerTool({
|
|
171
|
+
...localBash,
|
|
172
|
+
async execute(id, params, onUpdate, _ctx, signal) {
|
|
173
|
+
const ssh = getSsh();
|
|
174
|
+
if (ssh) {
|
|
175
|
+
const tool = createBashTool(localCwd, {
|
|
176
|
+
operations: createRemoteBashOps(ssh.remote, ssh.remoteCwd, localCwd),
|
|
177
|
+
});
|
|
178
|
+
return tool.execute(id, params, signal, onUpdate);
|
|
179
|
+
}
|
|
180
|
+
return localBash.execute(id, params, signal, onUpdate);
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
185
|
+
// Resolve SSH config now that CLI flags are available
|
|
186
|
+
const arg = pi.getFlag("ssh") as string | undefined;
|
|
187
|
+
if (arg) {
|
|
188
|
+
if (arg.includes(":")) {
|
|
189
|
+
const [remote, path] = arg.split(":");
|
|
190
|
+
resolvedSsh = { remote, remoteCwd: path };
|
|
191
|
+
} else {
|
|
192
|
+
// No path given, evaluate pwd on remote
|
|
193
|
+
const remote = arg;
|
|
194
|
+
const pwd = (await sshExec(remote, "pwd")).toString().trim();
|
|
195
|
+
resolvedSsh = { remote, remoteCwd: pwd };
|
|
196
|
+
}
|
|
197
|
+
ctx.ui.setStatus("ssh", ctx.ui.theme.fg("accent", `SSH: ${resolvedSsh.remote}:${resolvedSsh.remoteCwd}`));
|
|
198
|
+
ctx.ui.notify(`SSH mode: ${resolvedSsh.remote}:${resolvedSsh.remoteCwd}`, "info");
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// Handle user ! commands via SSH
|
|
203
|
+
pi.on("user_bash", (_event) => {
|
|
204
|
+
const ssh = getSsh();
|
|
205
|
+
if (!ssh) return; // No SSH, use local execution
|
|
206
|
+
return { operations: createRemoteBashOps(ssh.remote, ssh.remoteCwd, localCwd) };
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
// Replace local cwd with remote cwd in system prompt
|
|
210
|
+
pi.on("before_agent_start", async (event) => {
|
|
211
|
+
const ssh = getSsh();
|
|
212
|
+
if (ssh) {
|
|
213
|
+
const modified = event.systemPrompt.replace(
|
|
214
|
+
`Current working directory: ${localCwd}`,
|
|
215
|
+
`Current working directory: ${ssh.remoteCwd} (via SSH: ${ssh.remote})`,
|
|
216
|
+
);
|
|
217
|
+
return { systemPrompt: modified };
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|