voice-router-dev 0.2.1 → 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,8 +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
- ws.send(chunk.data);
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
+ }
2923
2936
  if (chunk.isLast) {
2937
+ flushAudioBuffer();
2924
2938
  ws.send(
2925
2939
  JSON.stringify({
2926
2940
  terminate_session: true
@@ -2933,6 +2947,7 @@ var AssemblyAIAdapter = class extends BaseAdapter {
2933
2947
  return;
2934
2948
  }
2935
2949
  sessionStatus = "closing";
2950
+ flushAudioBuffer();
2936
2951
  if (ws.readyState === WebSocket2.OPEN) {
2937
2952
  ws.send(
2938
2953
  JSON.stringify({