Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/voice/src/voice-widget.ts

Raw
import type { Theme } from "@earendil-works/pi-coding-agent";
import {
	type Component,
	ScrollView,
	sliceByColumn,
	stripTerminalSequences,
	type TUI,
	truncateToWidth,
	visibleWidth,
	wrapTextWithAnsi,
} from "@earendil-works/pi-tui";

export type NativeVoiceWidgetState = {
	status: string;
	activity: string;
	error: string | null;
	voiceError?: string | null;
	partial: string;
	spoken: string;
	waveform: readonly number[];
	inputLevel: number;
	outputLevel: number;
	playing: boolean;
	muted: boolean;
	humanSpeaking?: boolean;
	provisional?: boolean;
	captionRevision?: number;
};

export type NativeVoiceWidget = Component & {
	scroll(channel: "heard" | "spoken", lines: number): void;
	invalidate(): void;
	dispose(): void;
};

const MAX_WAVEFORM_BINS = 32;
const MAX_CAPTION_CHARS = 8192;
const MAX_WIDGET_ROWS = 7;
const BARS = " ▁▂▃▄▅▆▇█";
// Nerd Font: nf-fa-microphone, nf-fa-microphone_slash, nf-fa-volume_up, nf-md-brain.
const MICROPHONE = "\uf130";
const MUTED_MICROPHONE = "\uf131";
const SPEAKER = "\uf028";
const THINKING = "\u{f09d1}";

type WidgetTheme = Pick<Theme, "fg" | "bold">;

export function createNativeVoiceWidget(
	tui: TUI,
	theme: WidgetTheme,
	state: () => NativeVoiceWidgetState,
): NativeVoiceWidget {
	let disposed = false;
	let lastRequest = -100;
	let scheduled: ReturnType<typeof setTimeout> | undefined;
	let sweepTimer: ReturnType<typeof setTimeout> | undefined;
	let sweepFrame = 0;
	let previousProvisional = false;
	let previousRevision: number | undefined;
	const cancelSweep = () => {
		clearTimeout(sweepTimer);
		sweepTimer = undefined;
		sweepFrame = 0;
	};
	const startSweep = () => {
		cancelSweep();
		sweepFrame = 1;
		const tick = () => {
			if (disposed) return cancelSweep();
			if (sweepFrame >= 5) {
				cancelSweep();
				tui.requestRender();
				return;
			}
			sweepFrame++;
			tui.requestRender();
			sweepTimer = setTimeout(tick, 50);
			sweepTimer.unref?.();
		};
		tui.requestRender();
		sweepTimer = setTimeout(tick, 50);
		sweepTimer.unref?.();
	};
	const request = () => {
		scheduled = undefined;
		if (!disposed) {
			lastRequest = Date.now();
			tui.requestRender();
		}
	};
	const invalidate = () => {
		if (disposed || scheduled) return;
		const remaining = Math.max(0, 100 - (Date.now() - lastRequest));
		if (remaining === 0) request();
		else {
			scheduled = setTimeout(request, remaining);
			scheduled.unref?.();
		}
	};
	const captions = {
		heard: captionViewport(invalidate),
		spoken: captionViewport(invalidate),
	};
	let heardRows = 0;
	let renderedRows = 0;
	return {
		render(width: number): string[] {
			const rows = tui.terminal.rows;
			const budget = Number.isFinite(rows)
				? Math.min(MAX_WIDGET_ROWS, Math.max(3, Math.floor(rows / 3)))
				: MAX_WIDGET_ROWS;
			heardRows = Math.floor((budget - 1) / 2);
			const input = state();
			const revisionChanged = input.captionRevision !== previousRevision;
			const unavailable =
				!!(input.error ?? input.voiceError) ||
				input.muted ||
				input.status !== "ready";
			if (unavailable || input.provisional) cancelSweep();
			else if (previousProvisional && revisionChanged) startSweep();
			previousProvisional = !!input.provisional;
			previousRevision = input.captionRevision;
			// One column of padding on each side.
			const lines = renderFrame(
				input,
				theme,
				Math.max(0, width - 2),
				budget,
				captions,
				sweepFrame / 5,
			).map((line) => ` ${line}`);
			renderedRows = lines.length;
			return lines;
		},
		scroll(channel, lines): void {
			if (!disposed) captions[channel].scroll.scrollBy(lines);
		},
		handleMouse(event) {
			if (
				disposed ||
				renderedRows < 3 ||
				event.type !== "wheel" ||
				!Number.isFinite(event.wheelDelta) ||
				event.x < 0 ||
				event.x >= event.width ||
				event.y < 1 ||
				event.y >= renderedRows
			)
				return undefined;
			const channel = event.y <= heardRows ? "heard" : "spoken";
			captions[channel].scroll.scrollBy(event.wheelDelta ?? 0);
			// Contain wheel events at the text boundary without moving editor focus.
			return { handled: true, render: true };
		},
		invalidate,
		dispose(): void {
			disposed = true;
			clearTimeout(scheduled);
			cancelSweep();
		},
	};
}

function captionViewport(requestRender: () => void) {
	let previous = "";
	let previousWidth = -1;
	let previousClipped = false;
	let lines: string[] = [];
	const scroll = new ScrollView(
		{ render: () => lines, invalidate() {} },
		{ follow: "end", scrollbar: "hidden", overscroll: "contain" },
	);
	return {
		scroll,
		render(
			text: string,
			icon: string,
			theme: WidgetTheme,
			width: number,
			height: number,
			provisional = false,
			sweep = 0,
		): string[] {
			const prefix = `${icon} `;
			const indent = " ".repeat(visibleWidth(prefix));
			const textWidth = Math.max(1, width - visibleWidth(prefix) - 1);
			const raw = text.slice(0, MAX_CAPTION_CHARS);
			const display = provisional ? raw.toLowerCase() : raw;
			const bounded = display.slice(0, MAX_CAPTION_CHARS);
			const clipped =
				text.length > MAX_CAPTION_CHARS || display.length > MAX_CAPTION_CHARS;
			if (
				bounded !== previous ||
				textWidth !== previousWidth ||
				clipped !== previousClipped
			) {
				previous = bounded;
				previousWidth = textWidth;
				previousClipped = clipped;
				lines = wrapTextWithAnsi(
					clean(bounded, MAX_CAPTION_CHARS, true) + (clipped ? "…" : ""),
					textWidth,
				);
			}
			// setWidget's enclosing Container is opaque to Pi's layout traversal.
			// Establish geometry explicitly, retaining native follow/end/resize behavior.
			scroll.updateLayout(lines.length, height, requestRender);
			const start = scroll.scrollTop;
			return Array.from({ length: height }, (_, row) => {
				const above = row === 0 && start > 0;
				const below = row === height - 1 && start + height < lines.length;
				const marker = above && below ? "↕" : above ? "↑" : below ? "↓" : " ";
				const line = lines[start + row] ?? "";
				const head =
					sweep > 0
						? sliceByColumn(line, 0, Math.ceil(visibleWidth(line) * sweep))
						: "";
				const styled = provisional
					? theme.fg("muted", line)
					: head
						? theme.fg("accent", head) +
							sliceByColumn(line, visibleWidth(head), visibleWidth(line))
						: line;
				const body = truncateToWidth(
					`${row === 0 ? theme.fg("accent", prefix) : indent}${styled}`,
					Math.max(0, width - 1),
					"",
				);
				return truncateToWidth(
					`${body}${" ".repeat(Math.max(0, width - 1 - visibleWidth(body)))}${theme.fg("muted", marker)}`,
					width,
					"",
				);
			});
		},
	};
}

export function renderNativeVoiceWidget(
	input: NativeVoiceWidgetState,
	theme: WidgetTheme,
	width: number,
): string[] {
	return renderFrame(input, theme, width, MAX_WIDGET_ROWS, {
		heard: captionViewport(() => {}),
		spoken: captionViewport(() => {}),
	});
}

function renderFrame(
	input: NativeVoiceWidgetState,
	theme: WidgetTheme,
	width: number,
	rows: number,
	captions: {
		heard: ReturnType<typeof captionViewport>;
		spoken: ReturnType<typeof captionViewport>;
	},
	sweep = 0,
): string[] {
	const safeWidth = Number.isFinite(width)
		? Math.max(0, Math.min(16384, Math.floor(width)))
		: 0;
	if (safeWidth === 0) return [];
	const error = input.error ?? input.voiceError;
	const thinking =
		input.activity === "thinking" &&
		!error &&
		!input.muted &&
		input.status === "ready";
	const status = error
		? `Error: ${clean(error, 512)}`
		: input.muted
			? `${MUTED_MICROPHONE} Muted`
			: thinking && !input.playing
				? `${THINKING} Thinking`
				: statusLabel(input.status, input.playing);
	const activity = thinking && input.playing ? ` · ${THINKING} Thinking` : "";
	const speech =
		input.humanSpeaking && !input.muted && input.status === "ready";
	const wave = speech
		? theme.fg(
				"accent",
				theme.bold(
					waveform(input.waveform, input.inputLevel, input.outputLevel),
				),
			)
		: theme.fg(
				"muted",
				"─".repeat(
					Math.min(MAX_WAVEFORM_BINS, Math.max(2, input.waveform.length)),
				),
			);
	const first = `${theme.fg("accent", theme.bold("Voice"))}  ${theme.fg(error ? "error" : "muted", `${status}${activity}`)}  ${wave}`;
	if (
		!error &&
		(input.status === "loading" || input.status === "reconnecting") &&
		!input.partial &&
		!input.spoken
	) {
		return [
			truncateToWidth(first, safeWidth, "…"),
			truncateToWidth(
				theme.fg("muted", "Starting speech… · /voice stop cancels"),
				safeWidth,
				"…",
			),
		];
	}
	const heardRows = Math.floor((rows - 1) / 2);
	return [
		truncateToWidth(first, safeWidth, "…"),
		...captions.heard.render(
			input.partial ||
				(input.status === "ready" && !input.muted
					? "Waiting for speech"
					: "Paused"),
			input.muted ? MUTED_MICROPHONE : MICROPHONE,
			theme,
			safeWidth,
			heardRows,
			!!input.provisional,
			sweep,
		),
		...captions.spoken.render(
			input.spoken || "Nothing spoken yet",
			SPEAKER,
			theme,
			safeWidth,
			rows - 1 - heardRows,
		),
	];
}

function statusLabel(status: string, playing: boolean): string {
	if (status === "ready")
		return playing ? `${SPEAKER} Speaking` : `${MICROPHONE} Listening`;
	if (status === "loading") return "Loading";
	if (status === "reconnecting") return "Reconnecting";
	return clean(status || "Unavailable", 48);
}

function waveform(
	values: readonly number[],
	inputLevel: number,
	outputLevel: number,
): string {
	const samples = values.slice(0, MAX_WAVEFORM_BINS).filter(Number.isFinite);
	if (samples.length === 0)
		samples.push(...[inputLevel, outputLevel].filter(Number.isFinite));
	return samples
		.map((value) => {
			// Display-only 3x gain emphasizes detected speech; capture PCM stays unchanged.
			const db = value === 0 ? -Infinity : 20 * Math.log10(Math.abs(value) * 3);
			const index = Math.min(
				BARS.length - 1,
				Math.max(0, Math.round((db + 60) / 6.25)),
			);
			return BARS.charAt(index);
		})
		.join("");
}

function clean(value: string, maximum: number, multiline = false): string {
	const text = stripTerminalSequences(value.slice(0, maximum))
		.replace(/\r\n?/gu, "\n")
		.replace(/[\u0000-\u0009\u000b-\u001f\u007f-\u009f]/gu, " ");
	return text.replace(multiline ? /[^\S\n]+/gu : /\s+/gu, " ").trim();
}

export function widgetVisibleWidth(line: string): number {
	return visibleWidth(line);
}