Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/loop/src/render.ts

Raw
import {
	keyHint,
	type MessageRenderer,
	type Theme,
} from "@earendil-works/pi-coding-agent";
import { stripTerminalSequences, Text } from "@earendil-works/pi-tui";
import type { PromptDetails } from "./controller.ts";
import { ICON } from "./icons.ts";

type CustomMessage<T = unknown> = Parameters<MessageRenderer<T>>[0];

function bodyText(content: CustomMessage["content"]): string {
	return typeof content === "string"
		? content
		: content
				.flatMap((part) => (part.type === "text" ? [part.text] : []))
				.join("\n");
}

/** Compact loop prompt row; the full prompt needs the native tool expansion key. */
export function renderLoopPrompt(
	message: CustomMessage<PromptDetails>,
	{
		expanded,
		keyHint: hostKeyHint,
	}: {
		expanded: boolean;
		keyHint?: typeof keyHint;
	},
	theme: Theme,
): Text {
	const summary = message.details?.stub
		? message.details.stub.replace(/ Superseded by a later loop prompt\.$/u, "")
		: "Loop prompt";
	const head = theme.fg(
		"customMessageLabel",
		`${ICON.loop} ${stripTerminalSequences(summary)}`,
	);
	return new Text(
		expanded
			? `${head}\n${theme.fg("customMessageText", stripTerminalSequences(bodyText(message.content)))}`
			: `${head} ${(hostKeyHint ?? keyHint)("app.tools.expand", "to expand")}`,
		0,
		0,
	);
}