Welcome to the **RATT Agent Library **! 🎧⚡ This tiny TypeScript client streams microphone (or external) audio over WebSocket, handles reconnection and heartbeats, and emits typed events for live transcription and UI state. It works in browsers (with AudioWorklets) and in Node (via external PCM).
- Low-latency audio streaming (16 kHz, 16-bit PCM) over WebSocket
- Browser + Node: use the mic in browsers or push external PCM in any runtime
- Typed events: ready, mic state, amplitude (VAD energy), transcription, socket messages, errors
- Prebuffering & gating: capture early, start sending when the server says
start_audio - Resilient WS: single-flight connect, StrictMode-safe reuse, heartbeats & auto-reconnect
- Safe defaults: echo cancellation, AGC, noise suppression (browser)
- Tiny API: a single class
AssistantClientyou can drop into your app
npm i ratt-lib
# or
pnpm add ratt-libimport{AssistantClient,AssistantOptions,AssistantEvent}from"ratt-lib";// Simple mutable ref for requestId (works well with React useRef) and this id will be unique for each reqconstrequestId={current: ""};constchatSessionId="test";//Unique for whole sessionconstclientId="test";//Unique for whole session// ✅ Build the REQUIRED rattAgentDetails objectconstrattAgentDetails={conciergeId: chatbotData?.id,// Your chatbot IdconciergeName: chatbotData?.name??chatbotData?.assistantDetails?.name,organizationId: chatbotData?.organization,organizationName: chatbotData?.organizationName,requestId: requestId.current,// will be overridden with a fresh value when session startsagentSettings: {voiceAgentMongoId: chatbotData?.agents?.filter((a: any)=>a.title==="RATTAgent")?.[0]?._id,},username: user?.provider?.name,// user nameuseremailId: user?.email,// user emailchatSessionId: chatSessionId,rlefVoiceTaskId: chatbotData?.audioTranscription?.modelId||CREATE_AUDIO_RELF_VOICE_TASK_ID_DEFAULT_VALUE,// rlef model idassistant_type: chatbotData?.assistant_type,// your assistant typeisAudioRequest: true,//always trueclient_id: clientId,userId: encodeParam(USER_ID_DEFAULT_VALUE),// your user id// keep below three as it is if test instant learning is not there , otherwise please send these values as well as per the requirements.testQuestion: "",testAnswer: "",testVariants: JSON.stringify({Edit: [],Add: [],Delete: []}),}asconst;constclient=newAssistantClient({url: "wss://dev-egpt.techo.camp/audioStreamingWebsocket?clientId=${clientId}&sessionId=${chatSessionId}",// Required: your WS endpoint
requestId,// Required: { current: string }
rattAgentDetails,// ✅ pass the full required objectonSend: ()=>console.log("Transcript submitted"),showToast: (type,title,msg)=>console.log(type,title,msg),// optional tunables:pingIntervalMs: 5000,maxMissedPongs: 2,workletBasePath: "/",// where recorder/vad worklets are served from});// Listen to eventsclient.on(AssistantEvent.READY,()=>{console.log("WS ready:",client.wsReady);});client.on(AssistantEvent.MIC_CONNECTING,({ detail })=>{console.log("Mic connecting:",detail.connecting);});client.on(AssistantEvent.MIC_OPEN,({ detail })=>{console.log("Mic open:",detail.open);});client.on(AssistantEvent.AMPLITUDE,({ detail })=>{// 0..~1 energy (not dB) to drive a mic meter UIconsole.log("Amplitude:",detail.value);});client.on(AssistantEvent.TRANSCRIPTION,({ detail })=>{// progressive or final textconsole.log("Transcript:",detail.text);});client.on(AssistantEvent.ERROR,({ detail })=>{console.error("Assistant error:",detail.error);});// Start a session (ask server to begin, then it will reply with start_audio)document.querySelector("#start")!.addEventListener("click",()=>{client.startSession();// toggles ON (opens mic flow) basically handleMicClick function});// Stop (ask server to disconnect and teardown locally)document.querySelector("#stop")!.addEventListener("click",()=>{client.stopAudio();// toggles OFF});startSession()sends yourrattAgentDetails+ a newrequestId.When your server responds with
{ "start_audio": true }, the client:- marks the mic as open,
- starts converting to PCM16,
- and begins streaming.
As your server streams partial ASR, send either:
{"streaming_data":{"previous_transcription":"...", "new_transcription":"..."}}(chunked delta) or{"transcription":"final text"}(full updates)
End with
{"stop_audio": true}and/or{"disconnect": true}when you’re done.
useEffect(()=>{constunsub=client.on(AssistantEvent.TRANSCRIPTION,({ detail })=>{setText(detail.text);});returnunsub;// cleanly removes listener},[]);If you're not in a browser (or you have your own capture pipeline), set externalAudio: true and push PCM yourself.
import{AssistantClient}from"ratt-lib";importfsfrom"node:fs";constrequestId={current: ""};constclient=newAssistantClient({url: "wss://dev-egpt.techo.camp/audioStreamingWebsocket?clientId=${clientId}&sessionId=${chatSessionId}",
requestId,externalAudio: true,// ⬅️ no AudioContext, no micexternalAmplitudeRms: true,// optional: compute amplitude from PCM});// Start session -> wait for server {"start_audio": true}awaitclient.connect();awaitclient.startSession();// Now stream your PCM16 mono 16kHz dataconstpcm=fs.readFileSync("./audio.raw");// Int16 little-endian mono 16kHzclient.pushPCM16(newInt16Array(pcm.buffer,pcm.byteOffset,pcm.byteLength/2));// ...push more chunks as they arrive...// When done: or it will be auto stop by server when it detects some silence is thereawaitclient.stopAudio();PCM format: 16 kHz, 16-bit, mono, little-endian. If you have
Float32Array [-1..1], callpushFloat32()instead.
All events are emitted as standard CustomEvents and strongly typed via AssistantEvents type.
READY— WebSocket is ready (connected & open)MIC_CONNECTING—{ connecting: boolean }while we prep/prompt for micMIC_OPEN—{ open: boolean }mic flow is active/inactiveAMPLITUDE—{ value: number }live energy (for a mic meter)TRANSCRIPTION—{ text: string, delta?: string }progressive or finalSOCKET_MESSAGE—{ raw: MessageEvent, parsed?: any }every incoming WS messageERROR—{ error: unknown }any operational error
constoff=client.on(AssistantEvent.TRANSCRIPTION,({ detail })=>{console.log(detail.text);});off();// unsubscribetypeAssistantOptions={url: string;// WS endpointrequestId: {current: string};// mutable ref; client writes new ID per sessionrattAgentDetails?: Record<string,any>;onSend?: ()=>void;// called when server requests "disconnect"showToast?: (type: "error"|"info"|"success",title: string,msg: string)=>void;// Connection / heartbeatpingIntervalMs?: number;// default 5000maxMissedPongs?: number;// default 2// Audio (browser)workletBasePath?: string;// default "/"mediaStreamProvider?: ()=>Promise<MediaStream>;// default: getUserMedia (1ch, 16k, AGC/NS/EC enabled)audioContextFactory?: ()=>AudioContext|null;// default: new AudioContext() in browser, null in NodeworkletLoader?: (base: string)=>Promise<AudioContext|null>;// default: ensureAudioContextAndWorklets// External audio (Node or custom capture)externalAudio?: boolean;// default: false in browser, true in NodeexternalAmplitudeRms?: boolean;// default: truepcmChunkSize?: number;// default: TARGET_SAMPLES (typically 16000)};awaitclient.connect();// single-flight; reuses active WS if presentawaitclient.startSession();// begin mic flow or external audio sessionawaitclient.stopAudio();// stop current session (sends {disconnect:true})client.disconnect();// local teardown (no forced WS close)client.teardown();// local teardown helpersclient.closeSocket();// forcibly close WS and detach handlers// Mic helpersawaitclient.beginPrebuffering();// start capturing locally; don't send yetclient.stopPrebuffering();// stop & clear buffered audioawaitclient.startMic();// explicitly start mic captureclient.stopMic();// explicitly stop mic (and send {disconnect:true})// External audioclient.pushPCM16(int16ArrayOrBuffer);client.pushFloat32(float32Array);// State gettersclient.wsReady;// booleanclient.micOpen;// booleanclient.micConnecting;// booleanclient.amplitude;// number (0..~1)client.transcription;// latest accumulated text- No audio sent
Ensure your server replies with
{"start_audio": true}. The client buffers until the gate opens. - Mic blocked
Browser will throw
NotAllowedError. The client emitsERRORand callsshowToast(...). - Noisy audio / echo
The default constraints enable echo cancellation, AGC, and noise suppression. Override
mediaStreamProviderif needed. - Multiple connects in React StrictMode
This client reuses a global
ACTIVE_WSand a singleCONNECT_PROMISE—you’re safe. - Heartbeat timeouts
Increase
pingIntervalMsormaxMissedPongsif your WS hops are choppy.
- Browsers require a user gesture to start the microphone.
- Only minimal audio data is sent; handle it securely on your server. Use
wss://in production.
Questions, bugs, or ideas? Open an issue in your repo or ping your team chat. Happy streaming! 🎙️💬