Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/voice/__e2e__/fake-native.mjs

Raw
#!/usr/bin/env node
// Test-only logical peer: no device, model, network, asset writes, or PCM work.
// --wire-trace is an explicit test-owned OS-temp diagnostic exception.
import {
	appendFileSync,
	closeSync,
	existsSync,
	fstatSync,
	openSync,
	readSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { isAbsolute, relative, resolve } from "node:path";

if (process.argv.slice(2).filter((arg) => arg === "serve").length !== 1)
	throw new Error("expected exactly one serve subcommand");

const traceAt = process.argv.indexOf("--wire-trace");
const trace = traceAt < 0 ? undefined : process.argv[traceAt + 1];
if (
	trace &&
	(!isAbsolute(trace) ||
		relative(resolve(tmpdir()), resolve(trace)).startsWith(".."))
)
	throw new Error("wire trace must be under OS temp");
function capture(direction, value) {
	if (trace)
		appendFileSync(trace, `${JSON.stringify({ direction, value })}\n`, {
			mode: 0o600,
		});
}
const scenarioAt = process.argv.indexOf("--scenario");
const scenario =
	scenarioAt < 0 ? "quiet" : (process.argv[scenarioAt + 1] ?? "quiet");
const scenarioFileAt = process.argv.indexOf("--scenario-file");
const scenarioFile =
	scenarioFileAt < 0 ? undefined : process.argv[scenarioFileAt + 1];
function readScenarioFile(path) {
	const fd = openSync(path, "r");
	try {
		const size = fstatSync(fd).size;
		if (size > 65536) throw new Error("scenario file exceeds 64 KiB");
		const buffer = Buffer.alloc(65537);
		const bytes = readSync(fd, buffer, 0, buffer.length, 0);
		if (bytes > 65536) throw new Error("scenario file exceeds 64 KiB");
		return JSON.parse(buffer.toString("utf8", 0, bytes));
	} finally {
		closeSync(fd);
	}
}
const scenarioData = scenarioFile ? readScenarioFile(scenarioFile) : undefined;
const scriptedJourney = scenarioData?.journeys?.[scenario];
if (scriptedJourney) {
	const {
		caption,
		reply: response,
		duration_ms,
		endpoint_pause_ms,
		levels,
	} = scriptedJourney;
	if (
		typeof caption !== "string" ||
		caption.length < 1 ||
		caption.length > 8000 ||
		typeof response !== "string" ||
		response.length < 1 ||
		response.length > 8000
	)
		throw new Error(
			"scenario journey must contain bounded string caption and reply",
		);
	if (scenario === "recorded-speech") {
		if (
			!Number.isInteger(duration_ms) ||
			duration_ms < 1 ||
			duration_ms > 120000 ||
			!Number.isInteger(endpoint_pause_ms) ||
			endpoint_pause_ms < 2000 ||
			endpoint_pause_ms > 120000 ||
			!Array.isArray(levels) ||
			levels.length < 1 ||
			levels.length > 1200
		)
			throw new Error("recorded speech journey exceeds supported bounds");
		let previousAt = -100;
		for (const level of levels) {
			if (
				!Number.isInteger(level.at_ms) ||
				level.at_ms !== previousAt + 100 ||
				level.at_ms < 0 ||
				level.at_ms >= duration_ms ||
				!Number.isFinite(level.input) ||
				level.input < 0 ||
				level.input > 1 ||
				!Array.isArray(level.waveform) ||
				level.waveform.length > 32 ||
				!level.waveform.every(
					(bin) => Number.isFinite(bin) && bin >= 0 && bin <= 1,
				)
			)
				throw new Error("scenario journey contains invalid audio levels");
			previousAt = level.at_ms;
		}
	}
}
const releaseAt = process.argv.indexOf("--preview-release");
const previewRelease = releaseAt < 0 ? undefined : process.argv[releaseAt + 1];
if (
	scenario === "ui-preview" &&
	(!previewRelease ||
		!isAbsolute(previewRelease) ||
		relative(resolve(tmpdir()), resolve(previewRelease)).startsWith(".."))
)
	throw new Error(
		"ui-preview requires an explicit OS-temp preview release path",
	);
const instance = `fake${process.pid}`;
let seq = 0;
let resource;
let epoch = 1;
let ready = false;
let muted = false;
let greeted = false;
let renderFrame = 0;
let nextOperation = 0;
let installed = !["needs-setup", "consent", "license"].includes(scenario);
const operations = new Map();
const timers = new Set();
capture("lifecycle", { event: "start", pid: process.pid });
process.on("exit", (code) => capture("lifecycle", { event: "exit", code }));

function send(value, mode = "normal") {
	capture("outbound", value);
	const line = `${JSON.stringify(value)}\n`;
	if (mode === "fragment") {
		process.stdout.write(line.slice(0, 7));
		process.stdout.write(line.slice(7));
	} else process.stdout.write(line);
}
function event(method, operation, data) {
	return {
		jsonrpc: "2.0",
		method,
		params: {
			instance,
			seq: ++seq,
			resource: resource ?? null,
			epoch: resource ? epoch : null,
			operation: operation ?? null,
			data,
		},
	};
}
function emit(method, operation, data) {
	send(event(method, operation, data));
}
function later(fn, ms = 2, operation) {
	const timer = setTimeout(() => {
		timers.delete(timer);
		if (!operation || !operations.get(operation)?.ended) fn();
	}, ms);
	timers.add(timer);
}
function reply(id, result) {
	send(
		{ jsonrpc: "2.0", id, result },
		scenario === "fragment" ? "fragment" : "normal",
	);
}
function error(id, code, message) {
	send({ jsonrpc: "2.0", id, error: { code, message } });
}
function op(kind) {
	const id = `${kind}${++nextOperation}`;
	operations.set(id, { kind, ended: false, chunks: [] });
	return id;
}
function end(
	operation,
	outcome = "completed",
	reason = "done",
	stop_frame = null,
) {
	const state = operations.get(operation);
	if (!state) return outcome;
	if (state.ended) return state.outcome;
	state.ended = true;
	state.outcome = outcome;
	state.stop_frame = stop_frame;
	emit("operation.ended", operation, {
		outcome,
		reason,
		stop_frame,
		error:
			outcome === "failed"
				? {
						kind: "internal",
						retryable: false,
						field: null,
						resource,
						operation,
						detail: reason,
					}
				: null,
	});
	return outcome;
}
function live(kind) {
	return [...operations].find(
		([, state]) => state.kind === kind && !state.ended,
	)?.[0];
}
function progress(operation, frames, text = "") {
	const state = operations.get(operation);
	if (!state || state.ended) return;
	renderFrame += frames;
	state.consumed = (state.consumed ?? 0) + frames;
	emit("playback.progress", operation, {
		through_frame: renderFrame,
		consumed_frames: state.consumed,
		played_text: text,
	});
}
function closeResource() {
	if (!resource) return;
	for (const [operation, state] of operations) {
		if (!state.ended)
			end(
				operation,
				"cancelled",
				"closed",
				["sound", "synthesis"].includes(state.kind) ? renderFrame : null,
			);
	}
	emit("resource.closed", null, {
		capture_frame: 48000,
		render_frame: renderFrame,
	});
	resource = undefined;
	ready = false;
}
function caps() {
	return {
		capture: true,
		render: true,
		aec: true,
		recognition: true,
		preview: true,
		hotwords: true,
		synthesis: true,
		sounds: scenario !== "no-sounds",
		test_io: true,
	};
}
function info() {
	return {
		instance,
		io_mode: "fake",
		profiles: [
			{
				id: "basic",
				label: "Fake speech",
				description: "Logical test peer",
				installed,
				capabilities: caps(),
			},
		],
		devices: [],
		// Opaque generic recordings. bell remains a one-shot alias for existing callers.
		sounds: [
			[
				"bell",
				"3b0b086feb76ce9520e2f3074c49a97128c6b8a3aad780479aaf65ee494ec41d",
			],
			[
				"ready",
				"000d3c3803a9aa55295df80174d2b72b4abe2191ceb746f0ad446a1ab25d02ae",
			],
			[
				"error",
				"94b21d78b2a907e17a41e1509b58c2c701997bd5ba41b10a23d9f113f9533b47",
			],
			[
				"done",
				"9237a5856d97f7f1a2eadd7e6047b7a3357ea5273a63382fbeba64477f42722b",
			],
		].map(([id, sha256]) => ({ id, label: id, installed: true, sha256 })),
		capabilities: caps(),
		limits: {
			line_bytes: 65536,
			pending_requests: 32,
			active_operations: 8,
			resources: 1,
			text_chars: 8000,
			queued_text_chars: 16000,
			unacked_segments: 32,
			hotwords: 20,
			hotword_chars: 256,
			waveform_bins: 32,
			output_queue_bytes: 262144,
			instance_requests: 16384,
			instance_operations: 4096,
		},
	};
}
function recordedSpeech(operation) {
	const { caption, duration_ms, endpoint_pause_ms, levels } = scriptedJourney;
	const words = caption.split(/\s+/);
	const startedAt = Date.now();
	let revision = 0;
	let nextPreviewAt = 0;
	let nextLevel = 0;
	emit("recognition.activity", operation, { active: true, segment: "segment" });
	const tick = () => {
		if (operations.get(operation)?.ended) return;
		const elapsed = Math.min(duration_ms, Date.now() - startedAt);
		while (nextLevel < levels.length && levels[nextLevel].at_ms <= elapsed) {
			const level = levels[nextLevel++];
			emit("audio.level", operation, {
				input: level.input,
				output: 0,
				waveform: level.waveform,
				playing: false,
				busy: true,
			});
		}
		if (elapsed >= nextPreviewAt && elapsed < duration_ms) {
			const count = Math.max(
				1,
				Math.min(
					words.length,
					Math.ceil((elapsed / duration_ms) * words.length),
				),
			);
			emit("recognition.preview", operation, {
				segment: "segment",
				revision: ++revision,
				text: words.slice(0, count).join(" ").toLowerCase(),
			});
			nextPreviewAt = elapsed + 500;
		}
		if (elapsed >= duration_ms) {
			emit("recognition.activity", operation, {
				active: false,
				segment: "segment",
			});
			emit("recognition.preview_cleared", operation, {
				segment: "segment",
				through_revision: revision,
			});
			emit("recognition.pending", operation, { pending: true });
			return later(
				() => {
					emit("recognition.final", operation, {
						segment: "segment",
						text: caption,
						start_frame: 0,
						end_frame: Math.round((duration_ms * 48000) / 1000),
					});
					emit("recognition.pending", operation, { pending: false });
				},
				endpoint_pause_ms,
				operation,
			);
		}
		later(tick, Math.max(1, Math.min(100, duration_ms - elapsed)), operation);
	};
	tick();
}

function greeting(operation) {
	const caption = scriptedJourney?.caption ?? "Hello.";
	emit("recognition.activity", operation, { active: true, segment: "segment" });
	emit("audio.level", operation, {
		input: 0.4,
		output: 0,
		waveform: [0.2, 0.4],
		playing: false,
		busy: true,
	});
	if (scenario === "silence") {
		emit("recognition.preview", operation, {
			segment: "segment",
			revision: 1,
			text: "uncommitted background speech",
		});
		return later(
			() => {
				emit("recognition.activity", operation, {
					active: false,
					segment: "segment",
				});
				emit("recognition.preview_cleared", operation, {
					segment: "segment",
					through_revision: 1,
				});
				emit("recognition.empty", operation, {
					segment: "segment",
					start_frame: 0,
					end_frame: 48000,
				});
			},
			15,
			operation,
		);
	}
	emit("recognition.preview", operation, {
		segment: "segment",
		revision: 1,
		text: scriptedJourney ? caption.toLowerCase() : "hello",
	});
	const revised = ["preview-race", "ui-preview"].includes(scenario);
	if (revised)
		emit("recognition.preview", operation, {
			segment: "segment",
			revision: 2,
			text: "hello world",
		});
	const finish = () => {
		emit("recognition.activity", operation, {
			active: false,
			segment: "segment",
		});
		emit("recognition.preview_cleared", operation, {
			segment: "segment",
			through_revision: revised ? 2 : 1,
		});
		later(
			() =>
				emit("recognition.final", operation, {
					segment: "segment",
					text: caption,
					start_frame: 0,
					end_frame: 48000,
				}),
			scenario === "preview-race" ? 8 : 0,
			operation,
		);
	};
	if (scenario === "ui-preview") {
		const awaitRelease = () => {
			if (existsSync(previewRelease)) finish();
			else later(awaitRelease, 10, operation);
		};
		later(awaitRelease, 10, operation);
	} else finish();
}
function handle(request) {
	capture("inbound", request);
	const { id, method, params = {} } = request;
	if (scenario === "malformed") return process.stdout.write("not-json\n");
	if (scenario === "exit") return process.exit(1);
	if (scenario === "preabort" && method === "assets.plan")
		return process.exit(9);
	if (
		scenario === "stall" &&
		method !== "service.info" &&
		method !== "service.shutdown"
	)
		return;
	if (method === "service.info") {
		const result = info();
		if (scenario === "malformed-info") result.sounds[0].sha256 = "bad";
		reply(id, result);
		if (scenario === "idle-exit") later(() => process.exit(1), 10);
		return;
	}
	if (method === "service.ping") {
		if (scenario === "batched") {
			const response = { jsonrpc: "2.0", id, result: { instance } };
			const notice = event("service.warning", null, {
				kind: "busy",
				message: "after result",
			});
			process.stdout.write(
				`${JSON.stringify(response)}\n${JSON.stringify(notice)}\n`,
			);
			return;
		}
		return reply(id, { instance });
	}
	if (method === "assets.plan")
		return reply(id, {
			plan: {
				id: "plan",
				profile: "basic",
				ready: installed,
				writes_required: !installed,
				download_bytes: 0,
				installed_bytes: 0,
				items:
					scenario === "license"
						? [
								{
									id: "licensed-model",
									kind: "model",
									destination: "/native-owned/fake-model",
									source: "https://example.invalid/fake-model",
									sha256:
										"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
									download_bytes: 0,
									installed_bytes: 0,
									cached: true,
									license: {
										name: "Fake Model License",
										url: "https://example.invalid/license",
										acceptance_required: true,
									},
								},
							]
						: [],
				blockers: scenario === "consent" ? ["approval required"] : [],
				expires_in_ms: 300000,
			},
		});
	if (method === "assets.install") {
		if (scenario === "consent") return error(id, -32003, "Consent required");
		if (
			scenario === "license" &&
			(!Array.isArray(params.accepted_licenses) ||
				params.accepted_licenses.length !== 1 ||
				params.accepted_licenses[0] !== "licensed-model")
		)
			return error(id, -32003, "Consent required");
		const operation = op("install");
		reply(id, { operation });
		return later(() => {
			emit("operation.progress", operation, {
				phase: "installing",
				done_bytes: 0,
				total_bytes: 0,
			});
			installed = true;
			end(operation, "completed", "installed");
		});
	}
	if (method === "resource.open") {
		if (resource) return error(id, -32002, "Busy");
		resource = "resource";
		epoch = 1;
		muted = params.muted ?? false;
		ready = false;
		const operation = op("open");
		reply(id, { resource, epoch, operation });
		return later(
			() => {
				if (scenario === "fault") {
					epoch = 2;
					end(operation, "failed", "device failed");
					emit("resource.health", null, {
						status: "unavailable",
						capture_frame: 0,
						render_frame: 0,
						error: {
							kind: "device",
							retryable: false,
							field: null,
							resource,
							operation,
							detail: "fake fault",
						},
					});
					return;
				}
				ready = true;
				emit("resource.ready", operation, {
					capture: params.capture !== null,
					render: params.render !== null,
					speech: true,
					capture_rate: params.capture === null ? null : 48000,
					render_rate: params.render === null ? null : 48000,
				});
				end(operation, "completed", "ready");
			},
			2,
			operation,
		);
	}
	if (method === "resource.set_muted") {
		if (params.muted !== muted) {
			for (const [operation, state] of operations)
				if (!state.ended && state.kind !== "open")
					end(
						operation,
						"cancelled",
						"muted",
						["sound", "synthesis"].includes(state.kind) ? renderFrame : null,
					);
			muted = params.muted;
			epoch += 1;
		}
		return reply(id, { epoch, muted });
	}
	if (method === "resource.close") {
		closeResource();
		return reply(id, { closed: true });
	}
	if (method === "recognition.start") {
		if (!ready || muted) return error(id, -32004, "Resource not ready");
		if (live("recognition")) return error(id, -32002, "Busy");
		const operation = op("recognition");
		reply(id, { operation });
		if (scenario === "recorded-speech" && !greeted) {
			greeted = true;
			return later(() => recordedSpeech(operation), 2, operation);
		}
		if (
			(!greeted &&
				[
					"greeting",
					"barge-in",
					"preview-race",
					"ui-preview",
					"silence",
				].includes(scenario)) ||
			scriptedJourney
		) {
			greeted = true;
			later(() => greeting(operation), 2, operation);
		}
		return;
	}
	if (method === "recognition.set_hotwords")
		return reply(id, { accepted: true });
	if (method === "recognition.ack") return reply(id, { acknowledged: true });
	if (method === "recognition.finish") {
		reply(id, { accepted: true });
		return later(
			() => end(params.operation, "completed", "input_finished"),
			2,
			params.operation,
		);
	}
	if (method === "synthesis.start") {
		if (!ready || muted) return error(id, -32004, "Resource not ready");
		if (live("synthesis")) return error(id, -32002, "Busy");
		const sound = live("sound");
		if (sound) end(sound, "cancelled", "speech", renderFrame);
		return reply(id, { operation: op("synthesis") });
	}
	if (method === "synthesis.append") {
		const state = operations.get(params.operation);
		if (!state || state.ended) return error(id, -32004, "Stale");
		state.chunks[params.index] = params.text;
		return reply(id, { accepted_index: params.index });
	}
	if (method === "synthesis.finish") {
		const state = operations.get(params.operation);
		if (!state) return error(id, -32005, "Not found");
		reply(id, { accepted: true });
		return later(
			() => {
				const spoken = state.chunks.join("");
				emit("synthesis.phrase", params.operation, { index: 0, text: spoken });
				progress(
					params.operation,
					24000,
					scenario === "partial-playback" ? "" : spoken,
				);
				if (scenario === "barge-in") {
					emit("recognition.activity", live("recognition"), {
						active: true,
						segment: "interrupt",
					});
					end(params.operation, "cancelled", "speech", renderFrame);
					return;
				}
				progress(params.operation, 24000, spoken);
				emit("playback.boundary", params.operation, {
					through_frame: renderFrame,
				});
				end(params.operation, "completed", "drained", renderFrame);
			},
			2,
			params.operation,
		);
	}
	if (method === "sound.play") {
		if (!resource || muted) return error(id, -32004, "Resource unavailable");
		if (live("synthesis") || live("sound")) return error(id, -32002, "Busy");
		const operation = op("sound");
		reply(id, { operation });
		return later(
			() => {
				progress(operation, 1);
				if (params.repeat_ms != null) {
					emit("audio.level", operation, {
						input: 0,
						output: 0,
						waveform: [],
						playing: false,
						busy: true,
					});
				} else end(operation, "completed", "played", renderFrame);
			},
			15,
			operation,
		);
	}
	if (method === "operation.cancel") {
		const state = operations.get(params.operation);
		if (!state) return error(id, -32005, "Not found");
		const outcome = end(
			params.operation,
			"cancelled",
			"requested",
			["sound", "synthesis"].includes(state.kind) ? renderFrame : null,
		);
		return reply(id, { outcome, stop_frame: state.stop_frame });
	}
	if (method === "service.shutdown") {
		closeResource();
		for (const [operation, state] of operations)
			if (!state.ended) end(operation, "cancelled", "shutdown");
		for (const timer of timers) clearTimeout(timer);
		timers.clear();
		reply(id, { stopped: true });
		return later(() => process.exit(0), 1);
	}
	error(id, -32601, "Method not found");
}
let buffer = "";
process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk) => {
	buffer += chunk;
	for (;;) {
		const at = buffer.indexOf("\n");
		if (at < 0) return;
		const line = buffer.slice(0, at);
		buffer = buffer.slice(at + 1);
		try {
			handle(JSON.parse(line));
		} catch {
			process.exitCode = 2;
		}
	}
});
process.stdin.on("end", () => process.exit(0));