repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix: compact tool widget output

bf8066ab2b87a4d79f09611ce4913a7c2482bc31

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (5)

AGENTS.md

Modified
42 42 escalate to a human.
43 43 Do not work around `mise` or add ad-hoc scripts.
45 +## Tool Widget Rendering
47 +Tool widgets must match Pi built-ins: compact by default, expanded details on `app.tools.expand` (`ctrl+o` by default).
48 +Collapsed `renderResult` output may show status, counts, paths, IDs, and tiny bounded summaries only.
49 +Full content, previews, snippets, verbose logs, structured bodies, and per-result lists require `expanded === true`.
50 +Expansion hints must use `keyHint("app.tools.expand", "to expand")`; do not use legacy `expandTools`.
45 52 ## Cross-platform Requirements
47 54 Extensions must work on Windows, macOS, and Linux unless their README explicitly scopes them narrower.

CHANGELOG.md

Modified
80 80 Unix `which`, handle Windows `.cmd`/`.bat` shims with spaces, avoid stale
81 81 negative PATH cache entries, notify missing required/optional tools at the
82 82 right severity, and add coverage
83 +- Make web and super tool widgets compact by default, gate previews, analyses,
84 + and result lists behind expansion, and use `app.tools.expand` key hints
83 85 - Handle benign stdin `EPIPE` when web extension helper commands exit before
84 86 consuming input
85 87 - Add the `mode` extension for session-local mode switching with `/mode`,

extensions/super/index.ts

Modified
2 2 import {
3 3 defineTool,
4 4 type ExtensionAPI,
5 5 type ExtensionCommandContext,
6 + keyHint,
6 7 type RegisteredCommand,
7 8 type SessionEntry,
8 9 type SessionMessageEntry,
142 143 displayLines.push(
143 144 theme.fg(
144 145 "muted",
145 - `... (${moreLines} more lines, ${totalLines} total, ctrl+o to expand)`,
146 + `... (${moreLines} more lines, ${totalLines} total, ${keyHint("app.tools.expand", "to expand")})`,

extensions/web/__tests__/harness.test.ts

Modified
1 1 import path from "node:path";
2 -import { afterEach, describe, expect, it } from "vitest";
2 +import { initTheme } from "@earendil-works/pi-coding-agent";
3 +import { afterEach, beforeAll, describe, expect, it } from "vitest";
3 4 import { createTestSession, type TestSession } from "../../../test/harness";
6 +function theme() {
7 + return {
8 + fg: (_style: string, text: string) => text,
9 + bg: (_style: string, text: string) => text,
10 + bold: (text: string) => text,
11 + italic: (text: string) => text,
12 + strikethrough: (text: string) => text,
13 + } as any;
16 +function renderLines(component: { render(width: number): string[] }): string {
17 + return component.render(120).join("\n");
20 +function renderToolResult(
21 + tool: { definition: { renderResult?: Function } },
22 + result: unknown,
23 + expanded: boolean,
24 +): string {
25 + const component = tool.definition.renderResult?.(
26 + result,
27 + { expanded, isPartial: false },
28 + theme(),
29 + {},
30 + );
31 + return component ? renderLines(component) : "";
5 34 describe("web extension harness", () => {
6 35 let t: TestSession | undefined;
36 + beforeAll(() => initTheme("default", false));
7 37 afterEach(() => t?.dispose());
9 39 it("registers web_fetch, web_map, web_search, and understand_image tools", async () => {
16 46 expect([...extension.tools.keys()]).toContain("web_search");
17 47 expect([...extension.tools.keys()]).toContain("understand_image");
50 + it("keeps web_fetch preview hidden until tool output is expanded", async () => {
51 + const webPath = path.resolve(import.meta.dirname, "../index.ts");
52 + t = await createTestSession({ extensions: [webPath] });
53 + const [extension] = t.session.extensionRunner.extensions;
54 + const tool = extension.tools.get("web_fetch");
55 + const result = {
56 + content: [{ type: "text", text: "full saved content" }],
57 + details: {
58 + kind: "html",
59 + format: "markdown",
60 + contentType: "text/html",
61 + bodySize: 261_200,
62 + chars: 10_000,
63 + visibleChars: 2_270,
64 + responseId: "40144e5a-0f06-4e47-b8d9-4a59855239d0",
65 + preview: "# Quick Select\nVerbose page body",
66 + },
67 + };
69 + const collapsed = renderToolResult(tool, result, false);
70 + const expanded = renderToolResult(tool, result, true);
72 + expect(collapsed).toContain("Agent sees:");
73 + expect(collapsed).not.toContain("Quick Select");
74 + expect(collapsed).not.toContain("Verbose page body");
75 + expect(expanded).toContain("Quick Select");
76 + expect(expanded).toContain("Verbose page body");
77 + });
79 + it("shows web_fetch visible content on expand when no preview detail exists", async () => {
80 + const webPath = path.resolve(import.meta.dirname, "../index.ts");
81 + t = await createTestSession({ extensions: [webPath] });
82 + const [extension] = t.session.extensionRunner.extensions;
83 + const tool = extension.tools.get("web_fetch");
84 + const result = {
85 + content: [
86 + {
87 + type: "text",
88 + text: "https://wezterm.org/quickselect.html\n\n# Quick Select Mode\nExpanded markdown body",
89 + },
90 + ],
91 + details: {
92 + kind: "html",
93 + format: "markdown",
94 + contentType: "text/html",
95 + bodySize: 261_200,
96 + chars: 10_000,
97 + visibleChars: 1_014,
98 + responseId: "68a87f48-82c6-4182-b101-4eb07fd62118",
99 + },
100 + };
102 + const collapsed = renderToolResult(tool, result, false);
103 + const expanded = renderToolResult(tool, result, true);
105 + expect(collapsed).toContain("to expand");
106 + expect(collapsed).not.toContain("Quick Select Mode");
107 + expect(expanded).toContain("Quick Select Mode");
108 + expect(expanded).toContain("Expanded markdown body");
109 + });
111 + it("keeps web_search result titles hidden until tool output is expanded", async () => {
112 + const webPath = path.resolve(import.meta.dirname, "../index.ts");
113 + t = await createTestSession({ extensions: [webPath] });
114 + const [extension] = t.session.extensionRunner.extensions;
115 + const tool = extension.tools.get("web_search");
116 + const result = {
117 + content: [
119 + type: "text",
120 + text: "Search: wezterm\n\n[1] Quick Select Mode\n https://wezterm.org/quickselect.html",
121 + },
122 + ],
123 + details: {
124 + query: "wezterm",
125 + source: "codex",
126 + results: [
128 + title: "Quick Select Mode",
129 + url: "https://wezterm.org/quickselect.html",
130 + },
131 + ],
132 + },
133 + };
135 + const collapsed = renderToolResult(tool, result, false);
136 + const expanded = renderToolResult(tool, result, true);
138 + expect(collapsed).toContain("1 result");
139 + expect(collapsed).toContain("codex");
140 + expect(collapsed).not.toContain("Quick Select Mode");
141 + expect(expanded).toContain("Quick Select Mode");
142 + });
144 + it("keeps understand_image analysis hidden until tool output is expanded", async () => {
145 + const webPath = path.resolve(import.meta.dirname, "../index.ts");
146 + t = await createTestSession({ extensions: [webPath] });
147 + const [extension] = t.session.extensionRunner.extensions;
148 + const tool = extension.tools.get("understand_image");
149 + const result = {
150 + content: [
152 + type: "text",
153 + text: "The image shows a black cat on a blue terminal background.",
154 + },
155 + ],
156 + details: { prompt: "describe", image_source: "cat.png" },
157 + };
159 + const collapsed = renderToolResult(tool, result, false);
160 + const expanded = renderToolResult(tool, result, true);
162 + expect(collapsed).toContain("Image analyzed");
163 + expect(collapsed).not.toContain("black cat");
164 + expect(expanded).toContain("black cat");
165 + });

extensions/web/index.ts

Modified
975 975 header += `\n ${theme.fg("muted", "\u21B3 Agent sees:")} ${theme.fg("dim", llmDesc)}`;
976 976 if (responseId)
977 977 header += `\n ${theme.fg("muted", "\u21B3 Saved:")} ${theme.fg("dim", responseId)}`;
978 + const content = result.content[0];
979 + const hasExpandableText =
980 + content?.type === "text" && !!content.text?.trim();
981 + if (!expanded && hasExpandableText)
982 + header += `\n ${theme.fg("muted", "\u21B3")} ${theme.fg("dim", keyHint("app.tools.expand", "to expand"))}`;
979 984 const mdTheme = getMarkdownTheme();
980 985 const box = new Box(1, 0, (t) => theme.bg("toolSuccessBg", t));
1005 1010 return box;
1008 - if (details?.preview) {
1009 - const lines = safeTerminalText(details.preview).split("\n");
1010 - const maxLines = expanded ? lines.length : 30;
1011 - const shown = lines.slice(0, maxLines).join("\n");
1012 - const truncated = lines.length > maxLines;
1013 + if (expanded && details?.preview) {
1013 1014 box.addChild(
1014 - new Markdown(
1015 - shown +
1016 - (truncated
1017 - ? `\n\n\u2026 ${lines.length - maxLines} more lines`
1018 - : ""),
1019 - 1,
1020 - 1,
1021 - mdTheme,
1022 - ),
1015 + new Markdown(safeTerminalText(details.preview), 1, 1, mdTheme),
1016 + );
1017 + return box;
1020 + if (expanded && content?.type === "text") {
1021 + box.addChild(
1022 + new Markdown(safeTerminalText(content.text), 1, 1, mdTheme),
1024 1024 return box;
1364 1364 if (rendered !== null) return new Text(rendered, 0, 0);
1367 - const results = details?.results;
1368 - let text = theme.fg("success", "\u2713");
1369 - if (results && results.length > 0) {
1370 - const top = results.slice(0, 5);
1371 - for (const r of top) {
1372 - text += `\n${theme.fg("dim", "\u2022 " + safeDisplayText(r.title))}`;
1374 - if (results.length > 5) {
1375 - text += `\n${theme.fg("muted", `... +${results.length - 5} more`)}`;
1378 - if (details?.source) {
1367 + const resultCount = details?.results?.length ?? 0;
1368 + let text = theme.fg(
1369 + "success",
1370 + `\u2713 ${resultCount} result${resultCount === 1 ? "" : "s"}`,
1371 + );
1372 + if (details?.source)
1379 1373 text += ` ${theme.fg("muted", `(${details.source})`)}`;
1374 + if (resultCount > 0)
1375 + text += ` ${theme.fg("muted", `(${keyHint("app.tools.expand", "to expand")})`)}`;
1381 1376 return new Text(text, 0, 0);
1533 1528 if (expanded) {
1534 1529 const rendered = expandedText(result, text, theme);
1535 1530 if (rendered !== null) return new Text(rendered, 0, 0);
1536 - } else {
1537 - const content = result.content[0];
1538 - if (content?.type === "text" && content.text) {
1539 - text += `\n${theme.fg("dim", safeDisplayText(content.text).substring(0, 100) + "...")}`;
1541 - text += ` ${theme.fg("muted", `(${keyHint("expandTools" as any, "to expand")})`)}`;
1533 + text += ` ${theme.fg("muted", `(${keyHint("app.tools.expand", "to expand")})`)}`;
1544 1534 return new Text(text, 0, 0);