Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/the-system/tool-rendering.ts

Raw
import { keyHint } from "@earendil-works/pi-coding-agent";

interface TextContent {
	type: string;
	text?: string;
}

interface RenderTheme {
	fg(style: string, text: string): string;
}

export function expandableFallback(
	content: TextContent[],
	expanded: boolean,
	theme: RenderTheme,
	style: "error" | "muted",
): string {
	const full = content
		.filter(
			(item): item is TextContent & { text: string } =>
				item.type === "text" && typeof item.text === "string",
		)
		.map((item) => item.text)
		.join("\n")
		.trim();
	if (expanded) return theme.fg(style, full);

	const firstLine = full.split(/\r?\n/, 1)[0] ?? "";
	const normalized = firstLine.replace(/\s+/g, " ").trim();
	const summary =
		normalized.length > 160 ? `${normalized.slice(0, 159)}…` : normalized;
	let output = theme.fg(style, summary);
	if (full !== firstLine || summary !== normalized)
		output += `\n${theme.fg("muted", "↳")} ${keyHint("app.tools.expand", "to expand")}`;
	return output;
}