Repository navigation
Expand file tree
/
Copy pathminimal-mode.ts
More file actions
353 lines (297 loc) · 10.6 KB
/
Copy pathminimal-mode.ts
File metadata and controls
353 lines (297 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/**
* Minimal Mode Example - Demonstrates a "minimal" tool display mode
*
* This extension overrides built-in tools to provide custom rendering:
* - Collapsed mode: Only shows the tool call (command/path), no output
* - Expanded mode: Shows full output like the built-in renderers
*
* This demonstrates how a "minimal mode" could work, where ctrl+o cycles through:
* - Standard: Shows truncated output (current default)
* - Expanded: Shows full output (current expanded)
* - Minimal: Shows only tool call, no output (this extension's collapsed mode)
*
* Usage:
* pi -e ./minimal-mode.ts
*
* Then use ctrl+o to toggle between minimal (collapsed) and full (expanded) views.
*/
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import {
createBashToolDefinition,
createEditToolDefinition,
createFindToolDefinition,
createGrepToolDefinition,
createLsToolDefinition,
createReadToolDefinition,
createWriteToolDefinition,
formatReadLineRange,
} from "@earendil-works/pi-coding-agent";
import { Text } from "@earendil-works/pi-tui";
import { homedir } from "os";
/**
* Shorten a path by replacing home directory with ~
*/
function shortenPath(path: string): string {
const home = homedir();
if (path.startsWith(home)) {
return `~${path.slice(home.length)}`;
}
return path;
}
function createBuiltInTools(cwd: string) {
return {
read: createReadToolDefinition(cwd),
bash: createBashToolDefinition(cwd),
edit: createEditToolDefinition(cwd),
write: createWriteToolDefinition(cwd),
find: createFindToolDefinition(cwd),
grep: createGrepToolDefinition(cwd),
ls: createLsToolDefinition(cwd),
};
}
export default function (pi: ExtensionAPI) {
const tools = createBuiltInTools(process.cwd());
// =========================================================================
// Read Tool
// =========================================================================
pi.registerTool({
...tools.read,
renderCall(args, theme, _context) {
const path = shortenPath(args.path || "");
let pathDisplay = path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
const lineRange = formatReadLineRange(args.offset, args.limit);
if (lineRange) {
pathDisplay += theme.fg("warning", lineRange);
}
return new Text(`${theme.fg("toolTitle", theme.bold("read"))} ${pathDisplay}`, 0, 0);
},
renderResult(result, { expanded }, theme, _context) {
// Minimal mode: show nothing in collapsed state
if (!expanded) {
return new Text("", 0, 0);
}
// Expanded mode: show full output
const textContent = result.content.find((c) => c.type === "text");
if (!textContent || textContent.type !== "text") {
return new Text("", 0, 0);
}
const lines = textContent.text.split("\n");
const output = lines.map((line) => theme.fg("toolOutput", line)).join("\n");
return new Text(`\n${output}`, 0, 0);
},
});
// =========================================================================
// Bash Tool
// =========================================================================
pi.registerTool({
...tools.bash,
renderCall(args, theme, _context) {
const command = args.command || "...";
const timeout = args.timeout as number | undefined;
const timeoutSuffix = timeout ? theme.fg("muted", ` (timeout ${timeout}s)`) : "";
return new Text(theme.fg("toolTitle", theme.bold(`$ ${command}`)) + timeoutSuffix, 0, 0);
},
renderResult(result, { expanded }, theme, _context) {
// Minimal mode: show nothing in collapsed state
if (!expanded) {
return new Text("", 0, 0);
}
// Expanded mode: show full output
const textContent = result.content.find((c) => c.type === "text");
if (!textContent || textContent.type !== "text") {
return new Text("", 0, 0);
}
const output = textContent.text
.trim()
.split("\n")
.map((line) => theme.fg("toolOutput", line))
.join("\n");
if (!output) {
return new Text("", 0, 0);
}
return new Text(`\n${output}`, 0, 0);
},
});
// =========================================================================
// Write Tool
// =========================================================================
pi.registerTool({
...tools.write,
renderCall(args, theme, _context) {
const path = shortenPath(args.path || "");
const pathDisplay = path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
const lineCount = args.content ? args.content.split("\n").length : 0;
const lineInfo = lineCount > 0 ? theme.fg("muted", ` (${lineCount} lines)`) : "";
return new Text(`${theme.fg("toolTitle", theme.bold("write"))} ${pathDisplay}${lineInfo}`, 0, 0);
},
renderResult(result, { expanded }, theme, _context) {
// Minimal mode: show nothing (file was written)
if (!expanded) {
return new Text("", 0, 0);
}
// Expanded mode: show error if any
if (result.content.some((c) => c.type === "text" && c.text)) {
const textContent = result.content.find((c) => c.type === "text");
if (textContent?.type === "text" && textContent.text) {
return new Text(`\n${theme.fg("error", textContent.text)}`, 0, 0);
}
}
return new Text("", 0, 0);
},
});
// =========================================================================
// Edit Tool
// =========================================================================
pi.registerTool({
...tools.edit,
renderShell: "default",
renderCall(args, theme, _context) {
const path = shortenPath(args.path || "");
const pathDisplay = path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
return new Text(`${theme.fg("toolTitle", theme.bold("edit"))} ${pathDisplay}`, 0, 0);
},
renderResult(result, { expanded }, theme, _context) {
// Minimal mode: show nothing in collapsed state
if (!expanded) {
return new Text("", 0, 0);
}
// Expanded mode: show diff or error
const textContent = result.content.find((c) => c.type === "text");
if (!textContent || textContent.type !== "text") {
return new Text("", 0, 0);
}
// For errors, show the error message
const text = textContent.text;
if (text.includes("Error") || text.includes("error")) {
return new Text(`\n${theme.fg("error", text)}`, 0, 0);
}
// Otherwise show the text (would be nice to show actual diff here)
return new Text(`\n${theme.fg("toolOutput", text)}`, 0, 0);
},
});
// =========================================================================
// Find Tool
// =========================================================================
pi.registerTool({
...tools.find,
renderCall(args, theme, _context) {
const pattern = args.pattern || "";
const path = shortenPath(args.path || ".");
const limit = args.limit;
let text = `${theme.fg("toolTitle", theme.bold("find"))} ${theme.fg("accent", pattern)}`;
text += theme.fg("toolOutput", ` in ${path}`);
if (limit !== undefined) {
text += theme.fg("toolOutput", ` (limit ${limit})`);
}
return new Text(text, 0, 0);
},
renderResult(result, { expanded }, theme, _context) {
if (!expanded) {
// Minimal: just show count
const textContent = result.content.find((c) => c.type === "text");
if (textContent?.type === "text") {
const count = textContent.text.trim().split("\n").filter(Boolean).length;
if (count > 0) {
return new Text(theme.fg("muted", ` → ${count} files`), 0, 0);
}
}
return new Text("", 0, 0);
}
// Expanded: show full results
const textContent = result.content.find((c) => c.type === "text");
if (!textContent || textContent.type !== "text") {
return new Text("", 0, 0);
}
const output = textContent.text
.trim()
.split("\n")
.map((line) => theme.fg("toolOutput", line))
.join("\n");
return new Text(`\n${output}`, 0, 0);
},
});
// =========================================================================
// Grep Tool
// =========================================================================
pi.registerTool({
...tools.grep,
renderCall(args, theme, _context) {
const pattern = args.pattern || "";
const path = shortenPath(args.path || ".");
const glob = args.glob;
const limit = args.limit;
let text = `${theme.fg("toolTitle", theme.bold("grep"))} ${theme.fg("accent", `/${pattern}/`)}`;
text += theme.fg("toolOutput", ` in ${path}`);
if (glob) {
text += theme.fg("toolOutput", ` (${glob})`);
}
if (limit !== undefined) {
text += theme.fg("toolOutput", ` limit ${limit}`);
}
return new Text(text, 0, 0);
},
renderResult(result, { expanded }, theme, _context) {
if (!expanded) {
// Minimal: just show match count
const textContent = result.content.find((c) => c.type === "text");
if (textContent?.type === "text") {
const count = textContent.text.trim().split("\n").filter(Boolean).length;
if (count > 0) {
return new Text(theme.fg("muted", ` → ${count} matches`), 0, 0);
}
}
return new Text("", 0, 0);
}
// Expanded: show full results
const textContent = result.content.find((c) => c.type === "text");
if (!textContent || textContent.type !== "text") {
return new Text("", 0, 0);
}
const output = textContent.text
.trim()
.split("\n")
.map((line) => theme.fg("toolOutput", line))
.join("\n");
return new Text(`\n${output}`, 0, 0);
},
});
// =========================================================================
// Ls Tool
// =========================================================================
pi.registerTool({
...tools.ls,
renderCall(args, theme, _context) {
const path = shortenPath(args.path || ".");
const limit = args.limit;
let text = `${theme.fg("toolTitle", theme.bold("ls"))} ${theme.fg("accent", path)}`;
if (limit !== undefined) {
text += theme.fg("toolOutput", ` (limit ${limit})`);
}
return new Text(text, 0, 0);
},
renderResult(result, { expanded }, theme, _context) {
if (!expanded) {
// Minimal: just show entry count
const textContent = result.content.find((c) => c.type === "text");
if (textContent?.type === "text") {
const count = textContent.text.trim().split("\n").filter(Boolean).length;
if (count > 0) {
return new Text(theme.fg("muted", ` → ${count} entries`), 0, 0);
}
}
return new Text("", 0, 0);
}
// Expanded: show full listing
const textContent = result.content.find((c) => c.type === "text");
if (!textContent || textContent.type !== "text") {
return new Text("", 0, 0);
}
const output = textContent.text
.trim()
.split("\n")
.map((line) => theme.fg("toolOutput", line))
.join("\n");
return new Text(`\n${output}`, 0, 0);
},
});
}