voice-router-dev 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -2831,6 +2831,15 @@ var AssemblyAIAdapter = class extends BaseAdapter {
2831
2831
  });
2832
2832
  let sessionStatus = "connecting";
2833
2833
  const sessionId = `assemblyai-${Date.now()}-${Math.random().toString(36).substring(7)}`;
2834
+ let audioBuffer = Buffer.alloc(0);
2835
+ const MIN_CHUNK_SIZE = 1600;
2836
+ const MAX_CHUNK_SIZE = 32e3;
2837
+ const flushAudioBuffer = () => {
2838
+ if (audioBuffer.length > 0 && ws.readyState === WebSocket2.OPEN) {
2839
+ ws.send(audioBuffer);
2840
+ audioBuffer = Buffer.alloc(0);
2841
+ }
2842
+ };
2834
2843
  ws.on("open", () => {
2835
2844
  sessionStatus = "open";
2836
2845
  callbacks?.onOpen?.();
@@ -2919,13 +2928,13 @@ var AssemblyAIAdapter = class extends BaseAdapter {
2919
2928
  if (ws.readyState !== WebSocket2.OPEN) {
2920
2929
  throw new Error("WebSocket is not open");
2921
2930
  }
2922
- const base64Audio = chunk.data.toString("base64");
2923
- ws.send(
2924
- JSON.stringify({
2925
- audio_data: base64Audio
2926
- })
2927
- );
2931
+ audioBuffer = Buffer.concat([audioBuffer, chunk.data]);
2932
+ if (audioBuffer.length >= MIN_CHUNK_SIZE || audioBuffer.length >= MAX_CHUNK_SIZE) {
2933
+ ws.send(audioBuffer);
2934
+ audioBuffer = Buffer.alloc(0);
2935
+ }
2928
2936
  if (chunk.isLast) {
2937
+ flushAudioBuffer();
2929
2938
  ws.send(
2930
2939
  JSON.stringify({
2931
2940
  terminate_session: true
@@ -2938,6 +2947,7 @@ var AssemblyAIAdapter = class extends BaseAdapter {
2938
2947
  return;
2939
2948
  }
2940
2949
  sessionStatus = "closing";
2950
+ flushAudioBuffer();
2941
2951
  if (ws.readyState === WebSocket2.OPEN) {
2942
2952
  ws.send(
2943
2953
  JSON.stringify({