Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/agent/pi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// minimal structural declarations — the bundled artifact imports NOTHING
// from the host at runtime (the host duck-types us in).

import type { MessageRenderer } from "@earendil-works/pi-coding-agent";
import { Box, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
Expand Down Expand Up @@ -76,6 +78,7 @@ type ExtensionAPI = {
on: (event: string, handler: (event: never, ctx: Ctx) => unknown) => void;
registerTool: (tool: ToolDefinition) => void;
registerCommand?: (name: string, options: { description?: string; handler: (args: string, ctx: CommandCtx) => void | Promise<void> }) => void;
registerMessageRenderer?: (customType: string, renderer: MessageRenderer) => void;
// #2186: acp_delegate (inlined from billion-context-pi-subagents) injects
// completion notifications through the host's persistent transcript
// channel; optional because older hosts and non-pi hosts lack it.
Expand Down Expand Up @@ -1035,6 +1038,39 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM
pi.on("session_start", (_event, ctx) => repin(ctx));
pi.on("session_switch", (_event, ctx) => repin(ctx));
}
if (agent === "pi" && typeof pi.registerMessageRenderer === "function") {
pi.registerMessageRenderer("bili-acp-status", (message, { expanded, outputPad }, theme) => {
if (expanded) return undefined;
const text = typeof message.content === "string" ? message.content
: message.content.filter((part) => part.type === "text").map((part) => part.text).join("\n");
const lines = text.split("\n");
// Block-detail line shape is owned by the kernel's buildStatusPanel
// (kernel/src/panel/panel.ts); if it ever changes, folding degrades to full display.
const collapsed = lines.filter((line) => !/^ \[b\d+\] T\d+ /.test(line));
if (collapsed.length === lines.length) return undefined;
const box = new Box(outputPad ?? 1, 1, (line) => theme.bg("customMessageBg", line));
box.addChild(new Text(theme.fg("customMessageLabel", "\x1b[1m[bili-acp-status]\x1b[22m"), 0, 0));
box.addChild(new Spacer(1));
box.addChild(new Markdown(collapsed.join("\n"), 0, 0, {
heading: (value) => theme.fg("mdHeading", value),
link: (value) => theme.fg("mdLink", value),
linkUrl: (value) => theme.fg("mdLinkUrl", value),
code: (value) => theme.fg("mdCode", value),
codeBlock: (value) => theme.fg("mdCodeBlock", value),
codeBlockBorder: (value) => theme.fg("mdCodeBlockBorder", value),
quote: (value) => theme.fg("mdQuote", value),
quoteBorder: (value) => theme.fg("mdQuoteBorder", value),
hr: (value) => theme.fg("mdHr", value),
listBullet: (value) => theme.fg("mdListBullet", value),
bold: (value) => theme.bold(value),
italic: (value) => theme.italic(value),
underline: (value) => theme.underline(value),
strikethrough: (value) => theme.strikethrough(value),
}, { color: (value) => theme.fg("customMessageText", value) }));
box.addChild(new Text(theme.fg("dim", "Ctrl+O to expand full ACP report"), 0, 0));
return box;
});
}
if (typeof pi.registerCommand === "function") {
pi.registerCommand("acp", {
description: "Show ACP context-compression status for this session",
Expand Down
46 changes: 46 additions & 0 deletions tests/plugin-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,52 @@ test("/acp emits a persistent custom message via pi.sendMessage when available (
}
});

test("/acp renderer collapses large reports and restores every block when expanded", () => {
type Renderer = NonNullable<Parameters<ReturnType<typeof createBiliPlugin>>[0]["registerMessageRenderer"]>;
let render: Parameters<Renderer>[1] | undefined;
biliPlugin({ on() {}, registerTool() {}, registerMessageRenderer: (name, renderer) => {
assert.equal(name, "bili-acp-status");
render = renderer;
} });
assert.ok(render, "register a renderer so Ctrl+O can control the report");
const text = ["╭─────────────────────────────────────────────╮", "│ ACP Context Analysis │",
"╰─────────────────────────────────────────────╯", "billion-context@0.1.202", "storage: full", "",
"Context (session accounting): 50%", "", "Sent to LLM (after compression, est.): 50k",
"Prompt cache (provider-reported): 90%", "", "Blocks: 250 active / 250 total",
...Array.from({ length: 250 }, (_, i) => ` [b${i}] T1 10k→1k: topic ${i}`),
"Web UI: http://127.0.0.1:8787/__bili/", "Tag visibility: footer"].join("\n");
const message = { role: "custom" as const, customType: "bili-acp-status", content: text, display: true, timestamp: 0 };
const backgrounds: string[] = [];
const theme = {
fg: (_color: string, value: string) => value,
bg: (color: string, value: string) => { backgrounds.push(color); return value; },
bold: (value: string) => value,
italic: (value: string) => value,
underline: (value: string) => value,
strikethrough: (value: string) => value,
} as Parameters<typeof render>[2];
const collapsed = render(message, { expanded: false, outputPad: 1 }, theme)!.render(120).join("\n");
assert.ok(collapsed.split("\n").length < 25);
assert.match(collapsed, /╭─/);
assert.match(collapsed, /ACP Context Analysis/);
assert.match(collapsed, /storage: full/);
assert.match(collapsed, /storage: full[^\n]*\n\s*\n/);
assert.match(collapsed, /Tag visibility: footer/);
assert.match(collapsed, /Blocks: 250 active/);
assert.match(collapsed, /Context .*50%/);
assert.match(collapsed, /Prompt cache .*90%/);
assert.match(collapsed, /Web UI:/);
assert.match(collapsed, /Ctrl\+O/);
assert.ok(!collapsed.includes("topic 249"));
assert.match(collapsed, /\[bili-acp-status\]/);
assert.ok(backgrounds.length > 0 && backgrounds.every((color) => color === "customMessageBg"));
assert.equal(render(message, { expanded: true, outputPad: 1 }, theme), undefined,
"expanded reports use pi's original styled Markdown renderer");
assert.equal(message.content, text, "display folding must preserve persisted report content");
assert.equal(render({ ...message, content: "Context: 0%\n\nBlocks: none" }, { expanded: false, outputPad: 1 }, theme),
undefined, "reports without block details keep pi's original renderer");
});

test("/acp sends renderAcpStatus fallback content via sendMessage when no panel string", async () => {
const server = http.createServer((req, res) => {
if (req.url?.startsWith("/__bili/plugin/status")) {
Expand Down
Loading