@mariozechner/pi-coding-agent 0.45.3 → 0.45.4

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.
Files changed (49) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +2 -1
  3. package/dist/cli/args.d.ts.map +1 -1
  4. package/dist/cli/args.js +1 -0
  5. package/dist/cli/args.js.map +1 -1
  6. package/dist/core/extensions/loader.d.ts.map +1 -1
  7. package/dist/core/extensions/loader.js +7 -9
  8. package/dist/core/extensions/loader.js.map +1 -1
  9. package/dist/core/model-registry.d.ts +4 -0
  10. package/dist/core/model-registry.d.ts.map +1 -1
  11. package/dist/core/model-registry.js +6 -0
  12. package/dist/core/model-registry.js.map +1 -1
  13. package/dist/core/model-resolver.d.ts.map +1 -1
  14. package/dist/core/model-resolver.js +1 -0
  15. package/dist/core/model-resolver.js.map +1 -1
  16. package/dist/core/sdk.d.ts.map +1 -1
  17. package/dist/core/sdk.js +7 -5
  18. package/dist/core/sdk.js.map +1 -1
  19. package/dist/index.d.ts +1 -1
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +1 -1
  22. package/dist/index.js.map +1 -1
  23. package/dist/modes/interactive/theme/light.json +9 -9
  24. package/dist/utils/image-convert.d.ts.map +1 -1
  25. package/dist/utils/image-convert.js +11 -4
  26. package/dist/utils/image-convert.js.map +1 -1
  27. package/dist/utils/image-resize.d.ts +1 -1
  28. package/dist/utils/image-resize.d.ts.map +1 -1
  29. package/dist/utils/image-resize.js +47 -25
  30. package/dist/utils/image-resize.js.map +1 -1
  31. package/dist/utils/vips.d.ts +11 -0
  32. package/dist/utils/vips.d.ts.map +1 -0
  33. package/dist/utils/vips.js +35 -0
  34. package/dist/utils/vips.js.map +1 -0
  35. package/docs/extensions.md +18 -17
  36. package/docs/sdk.md +21 -48
  37. package/examples/README.md +5 -2
  38. package/examples/extensions/README.md +19 -2
  39. package/examples/extensions/plan-mode/README.md +65 -0
  40. package/examples/extensions/plan-mode/index.ts +340 -0
  41. package/examples/extensions/plan-mode/utils.ts +168 -0
  42. package/examples/extensions/question.ts +211 -13
  43. package/examples/extensions/questionnaire.ts +427 -0
  44. package/examples/extensions/summarize.ts +195 -0
  45. package/examples/extensions/with-deps/package-lock.json +2 -2
  46. package/examples/extensions/with-deps/package.json +1 -1
  47. package/examples/sdk/README.md +3 -4
  48. package/package.json +5 -5
  49. package/examples/extensions/plan-mode.ts +0 -548
@@ -0,0 +1,340 @@
1
+ /**
2
+ * Plan Mode Extension
3
+ *
4
+ * Read-only exploration mode for safe code analysis.
5
+ * When enabled, only read-only tools are available.
6
+ *
7
+ * Features:
8
+ * - /plan command or Shift+P to toggle
9
+ * - Bash restricted to allowlisted read-only commands
10
+ * - Extracts numbered plan steps from "Plan:" sections
11
+ * - [DONE:n] markers to complete steps during execution
12
+ * - Progress tracking widget during execution
13
+ */
14
+
15
+ import type { AgentMessage } from "@mariozechner/pi-agent-core";
16
+ import type { AssistantMessage, TextContent } from "@mariozechner/pi-ai";
17
+ import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
18
+ import { Key } from "@mariozechner/pi-tui";
19
+ import { extractTodoItems, isSafeCommand, markCompletedSteps, type TodoItem } from "./utils.js";
20
+
21
+ // Tools
22
+ const PLAN_MODE_TOOLS = ["read", "bash", "grep", "find", "ls", "questionnaire"];
23
+ const NORMAL_MODE_TOOLS = ["read", "bash", "edit", "write"];
24
+
25
+ // Type guard for assistant messages
26
+ function isAssistantMessage(m: AgentMessage): m is AssistantMessage {
27
+ return m.role === "assistant" && Array.isArray(m.content);
28
+ }
29
+
30
+ // Extract text content from an assistant message
31
+ function getTextContent(message: AssistantMessage): string {
32
+ return message.content
33
+ .filter((block): block is TextContent => block.type === "text")
34
+ .map((block) => block.text)
35
+ .join("\n");
36
+ }
37
+
38
+ export default function planModeExtension(pi: ExtensionAPI): void {
39
+ let planModeEnabled = false;
40
+ let executionMode = false;
41
+ let todoItems: TodoItem[] = [];
42
+
43
+ pi.registerFlag("plan", {
44
+ description: "Start in plan mode (read-only exploration)",
45
+ type: "boolean",
46
+ default: false,
47
+ });
48
+
49
+ function updateStatus(ctx: ExtensionContext): void {
50
+ // Footer status
51
+ if (executionMode && todoItems.length > 0) {
52
+ const completed = todoItems.filter((t) => t.completed).length;
53
+ ctx.ui.setStatus("plan-mode", ctx.ui.theme.fg("accent", `📋 ${completed}/${todoItems.length}`));
54
+ } else if (planModeEnabled) {
55
+ ctx.ui.setStatus("plan-mode", ctx.ui.theme.fg("warning", "⏸ plan"));
56
+ } else {
57
+ ctx.ui.setStatus("plan-mode", undefined);
58
+ }
59
+
60
+ // Widget showing todo list
61
+ if (executionMode && todoItems.length > 0) {
62
+ const lines = todoItems.map((item) => {
63
+ if (item.completed) {
64
+ return (
65
+ ctx.ui.theme.fg("success", "☑ ") + ctx.ui.theme.fg("muted", ctx.ui.theme.strikethrough(item.text))
66
+ );
67
+ }
68
+ return `${ctx.ui.theme.fg("muted", "☐ ")}${item.text}`;
69
+ });
70
+ ctx.ui.setWidget("plan-todos", lines);
71
+ } else {
72
+ ctx.ui.setWidget("plan-todos", undefined);
73
+ }
74
+ }
75
+
76
+ function togglePlanMode(ctx: ExtensionContext): void {
77
+ planModeEnabled = !planModeEnabled;
78
+ executionMode = false;
79
+ todoItems = [];
80
+
81
+ if (planModeEnabled) {
82
+ pi.setActiveTools(PLAN_MODE_TOOLS);
83
+ ctx.ui.notify(`Plan mode enabled. Tools: ${PLAN_MODE_TOOLS.join(", ")}`);
84
+ } else {
85
+ pi.setActiveTools(NORMAL_MODE_TOOLS);
86
+ ctx.ui.notify("Plan mode disabled. Full access restored.");
87
+ }
88
+ updateStatus(ctx);
89
+ }
90
+
91
+ function persistState(): void {
92
+ pi.appendEntry("plan-mode", {
93
+ enabled: planModeEnabled,
94
+ todos: todoItems,
95
+ executing: executionMode,
96
+ });
97
+ }
98
+
99
+ pi.registerCommand("plan", {
100
+ description: "Toggle plan mode (read-only exploration)",
101
+ handler: async (_args, ctx) => togglePlanMode(ctx),
102
+ });
103
+
104
+ pi.registerCommand("todos", {
105
+ description: "Show current plan todo list",
106
+ handler: async (_args, ctx) => {
107
+ if (todoItems.length === 0) {
108
+ ctx.ui.notify("No todos. Create a plan first with /plan", "info");
109
+ return;
110
+ }
111
+ const list = todoItems.map((item, i) => `${i + 1}. ${item.completed ? "✓" : "○"} ${item.text}`).join("\n");
112
+ ctx.ui.notify(`Plan Progress:\n${list}`, "info");
113
+ },
114
+ });
115
+
116
+ pi.registerShortcut(Key.shift("p"), {
117
+ description: "Toggle plan mode",
118
+ handler: async (ctx) => togglePlanMode(ctx),
119
+ });
120
+
121
+ // Block destructive bash commands in plan mode
122
+ pi.on("tool_call", async (event) => {
123
+ if (!planModeEnabled || event.toolName !== "bash") return;
124
+
125
+ const command = event.input.command as string;
126
+ if (!isSafeCommand(command)) {
127
+ return {
128
+ block: true,
129
+ reason: `Plan mode: command blocked (not allowlisted). Use /plan to disable plan mode first.\nCommand: ${command}`,
130
+ };
131
+ }
132
+ });
133
+
134
+ // Filter out stale plan mode context when not in plan mode
135
+ pi.on("context", async (event) => {
136
+ if (planModeEnabled) return;
137
+
138
+ return {
139
+ messages: event.messages.filter((m) => {
140
+ const msg = m as AgentMessage & { customType?: string };
141
+ if (msg.customType === "plan-mode-context") return false;
142
+ if (msg.role !== "user") return true;
143
+
144
+ const content = msg.content;
145
+ if (typeof content === "string") {
146
+ return !content.includes("[PLAN MODE ACTIVE]");
147
+ }
148
+ if (Array.isArray(content)) {
149
+ return !content.some(
150
+ (c) => c.type === "text" && (c as TextContent).text?.includes("[PLAN MODE ACTIVE]"),
151
+ );
152
+ }
153
+ return true;
154
+ }),
155
+ };
156
+ });
157
+
158
+ // Inject plan/execution context before agent starts
159
+ pi.on("before_agent_start", async () => {
160
+ if (planModeEnabled) {
161
+ return {
162
+ message: {
163
+ customType: "plan-mode-context",
164
+ content: `[PLAN MODE ACTIVE]
165
+ You are in plan mode - a read-only exploration mode for safe code analysis.
166
+
167
+ Restrictions:
168
+ - You can only use: read, bash, grep, find, ls, questionnaire
169
+ - You CANNOT use: edit, write (file modifications are disabled)
170
+ - Bash is restricted to an allowlist of read-only commands
171
+
172
+ Ask clarifying questions using the questionnaire tool.
173
+ Use brave-search skill via bash for web research.
174
+
175
+ Create a detailed numbered plan under a "Plan:" header:
176
+
177
+ Plan:
178
+ 1. First step description
179
+ 2. Second step description
180
+ ...
181
+
182
+ Do NOT attempt to make changes - just describe what you would do.`,
183
+ display: false,
184
+ },
185
+ };
186
+ }
187
+
188
+ if (executionMode && todoItems.length > 0) {
189
+ const remaining = todoItems.filter((t) => !t.completed);
190
+ const todoList = remaining.map((t) => `${t.step}. ${t.text}`).join("\n");
191
+ return {
192
+ message: {
193
+ customType: "plan-execution-context",
194
+ content: `[EXECUTING PLAN - Full tool access enabled]
195
+
196
+ Remaining steps:
197
+ ${todoList}
198
+
199
+ Execute each step in order.
200
+ After completing a step, include a [DONE:n] tag in your response.`,
201
+ display: false,
202
+ },
203
+ };
204
+ }
205
+ });
206
+
207
+ // Track progress after each turn
208
+ pi.on("turn_end", async (event, ctx) => {
209
+ if (!executionMode || todoItems.length === 0) return;
210
+ if (!isAssistantMessage(event.message)) return;
211
+
212
+ const text = getTextContent(event.message);
213
+ if (markCompletedSteps(text, todoItems) > 0) {
214
+ updateStatus(ctx);
215
+ }
216
+ persistState();
217
+ });
218
+
219
+ // Handle plan completion and plan mode UI
220
+ pi.on("agent_end", async (event, ctx) => {
221
+ // Check if execution is complete
222
+ if (executionMode && todoItems.length > 0) {
223
+ if (todoItems.every((t) => t.completed)) {
224
+ const completedList = todoItems.map((t) => `~~${t.text}~~`).join("\n");
225
+ pi.sendMessage(
226
+ { customType: "plan-complete", content: `**Plan Complete!** ✓\n\n${completedList}`, display: true },
227
+ { triggerTurn: false },
228
+ );
229
+ executionMode = false;
230
+ todoItems = [];
231
+ pi.setActiveTools(NORMAL_MODE_TOOLS);
232
+ updateStatus(ctx);
233
+ persistState(); // Save cleared state so resume doesn't restore old execution mode
234
+ }
235
+ return;
236
+ }
237
+
238
+ if (!planModeEnabled || !ctx.hasUI) return;
239
+
240
+ // Extract todos from last assistant message
241
+ const lastAssistant = [...event.messages].reverse().find(isAssistantMessage);
242
+ if (lastAssistant) {
243
+ const extracted = extractTodoItems(getTextContent(lastAssistant));
244
+ if (extracted.length > 0) {
245
+ todoItems = extracted;
246
+ }
247
+ }
248
+
249
+ // Show plan steps and prompt for next action
250
+ if (todoItems.length > 0) {
251
+ const todoListText = todoItems.map((t, i) => `${i + 1}. ☐ ${t.text}`).join("\n");
252
+ pi.sendMessage(
253
+ {
254
+ customType: "plan-todo-list",
255
+ content: `**Plan Steps (${todoItems.length}):**\n\n${todoListText}`,
256
+ display: true,
257
+ },
258
+ { triggerTurn: false },
259
+ );
260
+ }
261
+
262
+ const choice = await ctx.ui.select("Plan mode - what next?", [
263
+ todoItems.length > 0 ? "Execute the plan (track progress)" : "Execute the plan",
264
+ "Stay in plan mode",
265
+ "Refine the plan",
266
+ ]);
267
+
268
+ if (choice?.startsWith("Execute")) {
269
+ planModeEnabled = false;
270
+ executionMode = todoItems.length > 0;
271
+ pi.setActiveTools(NORMAL_MODE_TOOLS);
272
+ updateStatus(ctx);
273
+
274
+ const execMessage =
275
+ todoItems.length > 0
276
+ ? `Execute the plan. Start with: ${todoItems[0].text}`
277
+ : "Execute the plan you just created.";
278
+ pi.sendMessage(
279
+ { customType: "plan-mode-execute", content: execMessage, display: true },
280
+ { triggerTurn: true },
281
+ );
282
+ } else if (choice === "Refine the plan") {
283
+ const refinement = await ctx.ui.editor("Refine the plan:", "");
284
+ if (refinement?.trim()) {
285
+ pi.sendUserMessage(refinement.trim());
286
+ }
287
+ }
288
+ });
289
+
290
+ // Restore state on session start/resume
291
+ pi.on("session_start", async (_event, ctx) => {
292
+ if (pi.getFlag("plan") === true) {
293
+ planModeEnabled = true;
294
+ }
295
+
296
+ const entries = ctx.sessionManager.getEntries();
297
+
298
+ // Restore persisted state
299
+ const planModeEntry = entries
300
+ .filter((e: { type: string; customType?: string }) => e.type === "custom" && e.customType === "plan-mode")
301
+ .pop() as { data?: { enabled: boolean; todos?: TodoItem[]; executing?: boolean } } | undefined;
302
+
303
+ if (planModeEntry?.data) {
304
+ planModeEnabled = planModeEntry.data.enabled ?? planModeEnabled;
305
+ todoItems = planModeEntry.data.todos ?? todoItems;
306
+ executionMode = planModeEntry.data.executing ?? executionMode;
307
+ }
308
+
309
+ // On resume: re-scan messages to rebuild completion state
310
+ // Only scan messages AFTER the last "plan-mode-execute" to avoid picking up [DONE:n] from previous plans
311
+ const isResume = planModeEntry !== undefined;
312
+ if (isResume && executionMode && todoItems.length > 0) {
313
+ // Find the index of the last plan-mode-execute entry (marks when current execution started)
314
+ let executeIndex = -1;
315
+ for (let i = entries.length - 1; i >= 0; i--) {
316
+ const entry = entries[i] as { type: string; customType?: string };
317
+ if (entry.customType === "plan-mode-execute") {
318
+ executeIndex = i;
319
+ break;
320
+ }
321
+ }
322
+
323
+ // Only scan messages after the execute marker
324
+ const messages: AssistantMessage[] = [];
325
+ for (let i = executeIndex + 1; i < entries.length; i++) {
326
+ const entry = entries[i];
327
+ if (entry.type === "message" && "message" in entry && isAssistantMessage(entry.message as AgentMessage)) {
328
+ messages.push(entry.message as AssistantMessage);
329
+ }
330
+ }
331
+ const allText = messages.map(getTextContent).join("\n");
332
+ markCompletedSteps(allText, todoItems);
333
+ }
334
+
335
+ if (planModeEnabled) {
336
+ pi.setActiveTools(PLAN_MODE_TOOLS);
337
+ }
338
+ updateStatus(ctx);
339
+ });
340
+ }
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Pure utility functions for plan mode.
3
+ * Extracted for testability.
4
+ */
5
+
6
+ // Destructive commands blocked in plan mode
7
+ const DESTRUCTIVE_PATTERNS = [
8
+ /\brm\b/i,
9
+ /\brmdir\b/i,
10
+ /\bmv\b/i,
11
+ /\bcp\b/i,
12
+ /\bmkdir\b/i,
13
+ /\btouch\b/i,
14
+ /\bchmod\b/i,
15
+ /\bchown\b/i,
16
+ /\bchgrp\b/i,
17
+ /\bln\b/i,
18
+ /\btee\b/i,
19
+ /\btruncate\b/i,
20
+ /\bdd\b/i,
21
+ /\bshred\b/i,
22
+ /(^|[^<])>(?!>)/,
23
+ />>/,
24
+ /\bnpm\s+(install|uninstall|update|ci|link|publish)/i,
25
+ /\byarn\s+(add|remove|install|publish)/i,
26
+ /\bpnpm\s+(add|remove|install|publish)/i,
27
+ /\bpip\s+(install|uninstall)/i,
28
+ /\bapt(-get)?\s+(install|remove|purge|update|upgrade)/i,
29
+ /\bbrew\s+(install|uninstall|upgrade)/i,
30
+ /\bgit\s+(add|commit|push|pull|merge|rebase|reset|checkout|branch\s+-[dD]|stash|cherry-pick|revert|tag|init|clone)/i,
31
+ /\bsudo\b/i,
32
+ /\bsu\b/i,
33
+ /\bkill\b/i,
34
+ /\bpkill\b/i,
35
+ /\bkillall\b/i,
36
+ /\breboot\b/i,
37
+ /\bshutdown\b/i,
38
+ /\bsystemctl\s+(start|stop|restart|enable|disable)/i,
39
+ /\bservice\s+\S+\s+(start|stop|restart)/i,
40
+ /\b(vim?|nano|emacs|code|subl)\b/i,
41
+ ];
42
+
43
+ // Safe read-only commands allowed in plan mode
44
+ const SAFE_PATTERNS = [
45
+ /^\s*cat\b/,
46
+ /^\s*head\b/,
47
+ /^\s*tail\b/,
48
+ /^\s*less\b/,
49
+ /^\s*more\b/,
50
+ /^\s*grep\b/,
51
+ /^\s*find\b/,
52
+ /^\s*ls\b/,
53
+ /^\s*pwd\b/,
54
+ /^\s*echo\b/,
55
+ /^\s*printf\b/,
56
+ /^\s*wc\b/,
57
+ /^\s*sort\b/,
58
+ /^\s*uniq\b/,
59
+ /^\s*diff\b/,
60
+ /^\s*file\b/,
61
+ /^\s*stat\b/,
62
+ /^\s*du\b/,
63
+ /^\s*df\b/,
64
+ /^\s*tree\b/,
65
+ /^\s*which\b/,
66
+ /^\s*whereis\b/,
67
+ /^\s*type\b/,
68
+ /^\s*env\b/,
69
+ /^\s*printenv\b/,
70
+ /^\s*uname\b/,
71
+ /^\s*whoami\b/,
72
+ /^\s*id\b/,
73
+ /^\s*date\b/,
74
+ /^\s*cal\b/,
75
+ /^\s*uptime\b/,
76
+ /^\s*ps\b/,
77
+ /^\s*top\b/,
78
+ /^\s*htop\b/,
79
+ /^\s*free\b/,
80
+ /^\s*git\s+(status|log|diff|show|branch|remote|config\s+--get)/i,
81
+ /^\s*git\s+ls-/i,
82
+ /^\s*npm\s+(list|ls|view|info|search|outdated|audit)/i,
83
+ /^\s*yarn\s+(list|info|why|audit)/i,
84
+ /^\s*node\s+--version/i,
85
+ /^\s*python\s+--version/i,
86
+ /^\s*curl\s/i,
87
+ /^\s*wget\s+-O\s*-/i,
88
+ /^\s*jq\b/,
89
+ /^\s*sed\s+-n/i,
90
+ /^\s*awk\b/,
91
+ /^\s*rg\b/,
92
+ /^\s*fd\b/,
93
+ /^\s*bat\b/,
94
+ /^\s*exa\b/,
95
+ ];
96
+
97
+ export function isSafeCommand(command: string): boolean {
98
+ const isDestructive = DESTRUCTIVE_PATTERNS.some((p) => p.test(command));
99
+ const isSafe = SAFE_PATTERNS.some((p) => p.test(command));
100
+ return !isDestructive && isSafe;
101
+ }
102
+
103
+ export interface TodoItem {
104
+ step: number;
105
+ text: string;
106
+ completed: boolean;
107
+ }
108
+
109
+ export function cleanStepText(text: string): string {
110
+ let cleaned = text
111
+ .replace(/\*{1,2}([^*]+)\*{1,2}/g, "$1") // Remove bold/italic
112
+ .replace(/`([^`]+)`/g, "$1") // Remove code
113
+ .replace(
114
+ /^(Use|Run|Execute|Create|Write|Read|Check|Verify|Update|Modify|Add|Remove|Delete|Install)\s+(the\s+)?/i,
115
+ "",
116
+ )
117
+ .replace(/\s+/g, " ")
118
+ .trim();
119
+
120
+ if (cleaned.length > 0) {
121
+ cleaned = cleaned.charAt(0).toUpperCase() + cleaned.slice(1);
122
+ }
123
+ if (cleaned.length > 50) {
124
+ cleaned = `${cleaned.slice(0, 47)}...`;
125
+ }
126
+ return cleaned;
127
+ }
128
+
129
+ export function extractTodoItems(message: string): TodoItem[] {
130
+ const items: TodoItem[] = [];
131
+ const headerMatch = message.match(/\*{0,2}Plan:\*{0,2}\s*\n/i);
132
+ if (!headerMatch) return items;
133
+
134
+ const planSection = message.slice(message.indexOf(headerMatch[0]) + headerMatch[0].length);
135
+ const numberedPattern = /^\s*(\d+)[.)]\s+\*{0,2}([^*\n]+)/gm;
136
+
137
+ for (const match of planSection.matchAll(numberedPattern)) {
138
+ const text = match[2]
139
+ .trim()
140
+ .replace(/\*{1,2}$/, "")
141
+ .trim();
142
+ if (text.length > 5 && !text.startsWith("`") && !text.startsWith("/") && !text.startsWith("-")) {
143
+ const cleaned = cleanStepText(text);
144
+ if (cleaned.length > 3) {
145
+ items.push({ step: items.length + 1, text: cleaned, completed: false });
146
+ }
147
+ }
148
+ }
149
+ return items;
150
+ }
151
+
152
+ export function extractDoneSteps(message: string): number[] {
153
+ const steps: number[] = [];
154
+ for (const match of message.matchAll(/\[DONE:(\d+)\]/gi)) {
155
+ const step = Number(match[1]);
156
+ if (Number.isFinite(step)) steps.push(step);
157
+ }
158
+ return steps;
159
+ }
160
+
161
+ export function markCompletedSteps(text: string, items: TodoItem[]): number {
162
+ const doneSteps = extractDoneSteps(text);
163
+ for (const step of doneSteps) {
164
+ const item = items.find((t) => t.step === step);
165
+ if (item) item.completed = true;
166
+ }
167
+ return doneSteps.length;
168
+ }