Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/voice/src/speech-events.ts

Raw
import type { Profile, SpeakClient } from "./speak-client.ts";

export type { Profile } from "./speak-client.ts";

export type PreparedSpeech = {
	client: SpeakClient;
	command: readonly string[];
	profile: Profile;
	preview: boolean;
};

export function supportsSpeechProfile(
	profile: Profile,
	preview: boolean,
): boolean {
	const capabilities = profile.capabilities;
	return (
		capabilities.capture &&
		capabilities.render &&
		capabilities.aec &&
		capabilities.recognition &&
		capabilities.synthesis &&
		capabilities.sounds &&
		(!preview || capabilities.preview)
	);
}

export type SpeechRequest = {
	assistantTurnId: string;
	userTurnId: string;
	/** Prose fragment, not arbitrary model-token bytes. */
	text: string;
	kind?: "status";
};

export type NativeSpeechEvent =
	| { type: "ready"; muted?: boolean }
	| { type: "preview"; text: string; captionId: number; revision: number }
	| { type: "caption-empty"; captionId: number; previewRevision: number }
	| {
			type: "final";
			text: string;
			generation: number;
			requestId: number;
			ordinal?: number;
			captionId?: number;
			previewRevision?: number;
	  }
	| { type: "speech"; active: boolean; captionId?: number }
	| { type: "input-state"; pending: boolean }
	| { type: "empty" }
	| {
			type: "audio-state";
			inputLevel: number;
			outputLevel: number;
			waveform: number[];
			playing: boolean;
			busy?: boolean;
			cueOnly?: boolean;
	  }
	| {
			type: "phrase";
			assistantTurnId: string;
			userTurnId: string;
			text: string;
	  }
	| {
			type: "playback";
			assistantTurnId: string;
			userTurnId: string;
			playedText: string;
			cumulativeMs: number;
	  }
	| { type: "boundary"; assistantTurnId: string }
	| { type: "warning"; message: string }
	| { type: "error"; error: string; recoverable: boolean };

export type NativeSpeechOptions = {
	backend: PreparedSpeech;
	onEvent(event: NativeSpeechEvent): void;
};