import type { AuthorizationStatus } from "./types.js" import { SpeechRecognitionTask } from "./SpeechRecognitionTask.js" export class SpeechRecognizer { private endpoint: string private activeTask: SpeechRecognitionTask | null = null constructor(config?: { endpoint?: string }) { this.endpoint = config?.endpoint ?? "/api/transcribe" } static async requestAuthorization(): Promise { try { const stream = await navigator.mediaDevices.getUserMedia({ audio: true }) stream.getTracks().forEach((t) => t.stop()) return "authorized" } catch (err: any) { if (err.name === "NotAllowedError") return "denied" if (err.name === "NotFoundError") return "restricted" return "denied" } } recognitionTask(): SpeechRecognitionTask { if (this.activeTask) { this.activeTask.cancel() } const task = new SpeechRecognitionTask(this.endpoint, () => { if (this.activeTask === task) this.activeTask = null }) this.activeTask = task return task } get isAvailable(): boolean { return ( typeof navigator !== "undefined" && !!navigator.mediaDevices && !!navigator.mediaDevices.getUserMedia ) } }