UNPKG

@promptbook/remote-client

Version:

Promptbook: Create persistent AI agents that turn your company's scattered knowledge into action

106 lines (105 loc) 3.59 kB
import type { SpeechRecognition, SpeechRecognitionEvent, SpeechRecognitionStartOptions, SpeechRecognitionState } from '../types/SpeechRecognition'; /** * Options for OpenAiSpeechRecognition */ export type OpenAiSpeechRecognitionOptions = { /** * OpenAI API base URL or proxy endpoint * @default '/api/openai/v1' */ readonly baseUrl?: string; }; /** * Speech recognition using the OpenAI transcription API to transcribe audio into text * * Note: This implementation uses a server-side proxy to avoid exposing the OpenAI API key on the client. * * @private because it requires server-client communication with a proxy endpoint */ export declare class OpenAiSpeechRecognition implements SpeechRecognition { private readonly options; private mediaRecorder; private mediaStream; private audioContext; private mediaStreamSource; private analyser; private silenceCheckAnimationFrameId; private audioChunks; private callbacks; private _state; private pendingStopDuringStart; private isStopping; private recordingStartedAt; private lastSpeechDetectedAt; private ambientNoiseLevel; private voiceLevelMultiplier; private silenceAutoStopDelayMs; private recordingMimeType; get state(): SpeechRecognitionState; constructor(options?: OpenAiSpeechRecognitionOptions); $start(options?: SpeechRecognitionStartOptions): Promise<void>; $stop(): void; private transcribe; /** * Handles `MediaRecorder.onstop` by releasing audio resources and forwarding the clip to transcription. * * @param language Optional language hint for OpenAI transcription. */ private handleRecorderStop; /** * Starts continuous silence detection and auto-stops recording shortly after speech ends. */ private startSilenceDetection; /** * Stops the silence-detection animation loop. */ private stopSilenceDetection; /** * Measures current voice intensity from the waveform (`0` silent .. `1` loud). * * @param sampleBuffer Buffer reused across animation frames. * @returns RMS-based volume level. */ private measureVoiceLevel; /** * Returns an adaptive speech threshold based on recently observed ambient noise. */ private resolveAdaptiveVoiceThreshold; /** * Updates the rolling ambient noise estimate during silent frames. * * @param voiceLevel Observed frame loudness. */ private updateAmbientNoiseLevel; /** * Releases browser audio resources so the microphone is fully freed for other apps. */ private releaseRecordingResources; /** * Stops all microphone tracks if an active media stream exists. */ private stopMediaStreamTracks; subscribe(callback: (event: SpeechRecognitionEvent) => void): () => void; private emit; } /** * Resolves the preferred recorder output format supported by the current browser. * * @private internal utility of `OpenAiSpeechRecognition` */ export declare function resolveOpenAiSpeechRecognitionPreferredRecordingFormat(): { readonly mimeType: string; readonly fileExtension: string; } | undefined; /** * Resolves the MIME type and filename for one recorded audio upload. * * @private internal utility of `OpenAiSpeechRecognition` */ export declare function resolveOpenAiSpeechRecognitionAudioFileDescriptor(options: { readonly recorderMimeType?: string; readonly audioChunks: ReadonlyArray<Blob>; }): { readonly mimeType: string; readonly fileName: string; };