repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

pi/agent/extensions/thinking/index.ts

Raw
File outline
/**
 * Thinking level footer segment producer.
 *
 * Shows current thinking level with per-level colors.
 * Polls at 500ms since thinking level can change mid-turn.
 * Emits footer:segment with zone "llm".
 */

import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";

const LEVEL_COLORS: Record<string, string> = {
	off: "thinkingOff",
	minimal: "thinkingMinimal",
	low: "thinkingLow",
	medium: "thinkingMedium",
	high: "thinkingHigh",
	xhigh: "thinkingXhigh",
};

const LEVEL_LABELS: Record<string, string> = {
	off: "think:off",
	minimal: "think:min",
	low: "think:low",
	medium: "think:med",
	high: "think:high",
	xhigh: "think:xhigh",
};

const POLL_MS = 500;

export default function thinkingExtension(pi: ExtensionAPI) {
	let timer: ReturnType<typeof setInterval> | null = null;
	let lastLevel = "";
	let hasUI = false;

	const emit = () => {
		const level = pi.getThinkingLevel?.() ?? "off";
		if (level === lastLevel) return;
		lastLevel = level;

		pi.events.emit("footer:segment", {
			id: "thinking",
			text: LEVEL_LABELS[level] ?? `think:${level}`,
			color: LEVEL_COLORS[level] ?? "muted",
			zone: "llm",
			order: 0.5,
		});
	};

	pi.on("session_start", async (_e, ctx) => {
		hasUI = ctx.hasUI;
		if (!hasUI) return;
		lastLevel = "";
		emit();
		// Poll for mid-turn changes
		timer = setInterval(emit, POLL_MS);
	});

	pi.on("agent_end", async () => { if (hasUI) try { emit(); } catch {} });

	pi.on("session_shutdown", async () => {
		if (timer) { clearInterval(timer); timer = null; }
		pi.events.emit("footer:segment", { id: "thinking", text: undefined });
	});
}