co-mono/packages/coding-agent/examples/extensions/message-renderer.ts
Mario Zechner 930207130b docs(coding-agent): improve extensions.md, add missing examples
- Add cross-links to session.md, keybindings.md, themes.md
- Document truncateLine, highlightCode, getLanguageFromPath utilities
- Add examples for previously undocumented APIs:
  - message-renderer.ts: registerMessageRenderer with Box styling
  - session-name.ts: setSessionName/getSessionName
  - event-bus.ts: pi.events for inter-extension communication
  - bookmark.ts: setLabel for entry bookmarking
- Update examples table in extensions.md and README.md
2026-01-26 11:43:01 +01:00

59 lines
1.9 KiB
TypeScript

/**
* Custom message rendering example.
*
* Shows how to use registerMessageRenderer to control how custom messages
* appear in the TUI, with colors, formatting, and expandable details.
*
* Usage: /status [message] - sends a status message with custom rendering
*/
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
import { Box, Text } from "@mariozechner/pi-tui";
export default function (pi: ExtensionAPI) {
// Register custom renderer for "status-update" messages
pi.registerMessageRenderer("status-update", (message, { expanded }, theme) => {
const details = message.details as { level: string; timestamp: number } | undefined;
const level = details?.level ?? "info";
// Color based on level
const color = level === "error" ? "error" : level === "warn" ? "warning" : "success";
const prefix = theme.fg(color, `[${level.toUpperCase()}]`);
let text = `${prefix} ${message.content}`;
// Show timestamp when expanded
if (expanded && details?.timestamp) {
const time = new Date(details.timestamp).toLocaleTimeString();
text += `\n${theme.fg("dim", ` at ${time}`)}`;
}
// Use Box with customMessageBg for consistent styling
const box = new Box(1, 1, (t) => theme.bg("customMessageBg", t));
box.addChild(new Text(text, 0, 0));
return box;
});
// Command to send status messages
pi.registerCommand("status", {
description: "Send a status message (usage: /status [warn|error] message)",
handler: async (args, _ctx) => {
const parts = args.trim().split(/\s+/);
let level = "info";
let content = args.trim();
// Check for level prefix
if (parts[0] === "warn" || parts[0] === "error") {
level = parts[0];
content = parts.slice(1).join(" ") || "Status update";
}
pi.sendMessage({
customType: "status-update",
content,
display: true,
details: { level, timestamp: Date.now() },
});
},
});
}