Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/voice/src/speech-output.ts

Raw
export type SpeechOutputState = {
	buffer: string;
	inCodeFence: boolean;
};

export type SpeechModality = "speech" | "transcript";
export type SpeechModalityOptions = {
	maxSentences?: number;
	maxPlaybackMs?: number;
};

export function createSpeechOutputState(): SpeechOutputState {
	return { buffer: "", inCodeFence: false };
}

/** Return stable prose only; unfinished lines wait for their next delta. */
export function stripMarkdownForSpeech(
	input: string,
	state: SpeechOutputState = createSpeechOutputState(),
	final = false,
): string {
	state.buffer += input;
	const lines = state.buffer.split(/\r?\n/u);
	let remainder = lines.pop() ?? "";
	if (!final) {
		// Prose before terminal punctuation is stable even without a newline.
		const boundary = lastSentenceBoundary(remainder);
		if (boundary > 0) {
			lines.push(remainder.slice(0, boundary));
			remainder = remainder.slice(boundary);
		}
	}
	state.buffer = final ? "" : remainder;
	if (final && remainder) lines.push(remainder);
	const speech: string[] = [];
	for (const line of lines) {
		const trimmed = line.trim();
		if (/^\s*```/u.test(line) || /^\s*~~~+/u.test(line)) {
			state.inCodeFence = !state.inCodeFence;
			continue;
		}
		if (state.inCodeFence || !trimmed) continue;
		if (/^\s*(\|.*\|\s*:?-+:?\s*\|?|[-+|]\s*)$/u.test(line)) continue;
		if (/^\s*\|/u.test(line)) continue;
		const prose = line
			.replace(/^\s{0,3}#{1,6}\s+/u, "")
			.replace(/^\s*(?:[-+*]|\d+[.)])\s+/u, "")
			.replace(/^\s*>\s?/u, "")
			.replace(/!?(\[)([^\]]*)\]\([^)]*\)/gu, "$2")
			.replace(/`([^`]*)`/gu, "$1")
			.replace(/\*\*([^*]+)\*\*/gu, "$1")
			.replace(/__([^_]+)__/gu, "$1")
			.replace(/[*_~]/gu, "")
			.trim();
		if (prose) speech.push(prose);
	}
	return speech.join(" ");
}

export function selectSpeechModality(
	text: string,
	playbackMs: number,
	options: SpeechModalityOptions = {},
): SpeechModality {
	// Keep long prose replies in the transcript drawer rather than synthesis.
	if (text.length > 4_000 || hasInherentlyStructuredContent(text))
		return "transcript";
	const sentenceLimit = options.maxSentences ?? 6;
	const playbackLimit = options.maxPlaybackMs ?? 40_000;
	const sentences = text.match(/[.!?](?:["')\]]*)?(?:\s|$)/gu)?.length ?? 0;
	return sentences > sentenceLimit || playbackMs >= playbackLimit
		? "transcript"
		: "speech";
}

function lastSentenceBoundary(text: string): number {
	let boundary = 0;
	for (const match of text.matchAll(/[.!?](?:["')\]]*)?(?=\s|$)/gu))
		boundary = (match.index ?? 0) + match[0].length;
	return boundary;
}

function hasInherentlyStructuredContent(text: string): boolean {
	const lines = text.split(/\r?\n/u);
	let listItems = 0;
	for (const line of lines) {
		if (/^\s*(```|~~~)/u.test(line)) return true;
		if (/^\s*\|.*\|/u.test(line)) return true;
		if (/^\s*(?:[-+*]|\d+[.)])\s+/u.test(line) && ++listItems > 2) return true;
	}
	return false;
}