@crewhaus/barge-in-controller 0.1.1 → 0.1.3

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.
Files changed (2) hide show
  1. package/package.json +7 -12
  2. package/src/index.test.ts +169 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crewhaus/barge-in-controller",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "description": "Barge-in coordinator: watches inbound audio via vad-engine, fires interrupt() on adapter when user speaks during agent playback (Section 24 VOICE)",
6
6
  "main": "src/index.ts",
@@ -12,14 +12,14 @@
12
12
  "test": "bun test src"
13
13
  },
14
14
  "dependencies": {
15
- "@crewhaus/vad-engine": "0.1.1",
16
- "@crewhaus/voice-runtime": "0.1.1"
15
+ "@crewhaus/vad-engine": "0.1.3",
16
+ "@crewhaus/voice-runtime": "0.1.3"
17
17
  },
18
18
  "license": "Apache-2.0",
19
19
  "author": {
20
20
  "name": "Max Meier",
21
- "email": "max@studiomax.io",
22
- "url": "https://studiomax.io"
21
+ "email": "max@crewhaus.ai",
22
+ "url": "https://crewhaus.ai"
23
23
  },
24
24
  "repository": {
25
25
  "type": "git",
@@ -31,12 +31,7 @@
31
31
  "url": "https://github.com/crewhaus/factory/issues"
32
32
  },
33
33
  "publishConfig": {
34
- "access": "restricted"
34
+ "access": "public"
35
35
  },
36
- "files": [
37
- "src",
38
- "README.md",
39
- "LICENSE",
40
- "NOTICE"
41
- ]
36
+ "files": ["src", "README.md", "LICENSE", "NOTICE"]
42
37
  }
package/src/index.test.ts CHANGED
@@ -130,4 +130,173 @@ describe("createBargeInController", () => {
130
130
  expect(interrupts).toHaveLength(0);
131
131
  ctrl.stop();
132
132
  });
133
+
134
+ test("disconnect marks agent idle and resets the speech-frame window", () => {
135
+ const { adapter, fire, interrupts } = makeStubAdapter();
136
+ const ctrl = createBargeInController({ adapter, triggerFrames: 4, windowMs: 200, frameMs: 30 });
137
+ fire({ kind: "audio_chunk", pcm: new Int16Array(0), sampleRate: 24_000 });
138
+ ctrl.feedAudioFrame(speechFrame());
139
+ ctrl.feedAudioFrame(speechFrame());
140
+ fire({ kind: "disconnect", code: 1000, reason: "bye" });
141
+ expect(ctrl.isAgentSpeaking()).toBe(false);
142
+ // Window was reset on disconnect: 2 more frames (had agent stayed speaking)
143
+ // would have hit triggerFrames=4, but the agent is idle so nothing fires.
144
+ ctrl.feedAudioFrame(speechFrame());
145
+ ctrl.feedAudioFrame(speechFrame());
146
+ expect(interrupts).toHaveLength(0);
147
+ ctrl.stop();
148
+ });
149
+
150
+ test("returns the raw VAD verdict for diagnostics (speech + silence)", () => {
151
+ const { adapter, fire } = makeStubAdapter();
152
+ const ctrl = createBargeInController({
153
+ adapter,
154
+ triggerFrames: 99,
155
+ windowMs: 200,
156
+ frameMs: 30,
157
+ });
158
+ fire({ kind: "audio_chunk", pcm: new Int16Array(0), sampleRate: 24_000 });
159
+ // Agent is speaking; a silent frame must not be counted as speech, and the
160
+ // verdict is surfaced verbatim.
161
+ expect(ctrl.feedAudioFrame(silentFrame())).toBe("silence");
162
+ expect(ctrl.feedAudioFrame(speechFrame())).toBe("speech");
163
+ ctrl.stop();
164
+ });
165
+
166
+ test("does NOT interrupt when speech frames age out of the sliding window", () => {
167
+ const { adapter, fire, interrupts } = makeStubAdapter();
168
+ // windowMs (60) only holds ~2 frames at frameMs 30, so a steady stream of
169
+ // speech never accumulates the 4 required *within the window*.
170
+ const ctrl = createBargeInController({ adapter, triggerFrames: 4, windowMs: 60, frameMs: 30 });
171
+ fire({ kind: "audio_chunk", pcm: new Int16Array(0), sampleRate: 24_000 });
172
+ for (let i = 0; i < 20; i++) {
173
+ expect(ctrl.feedAudioFrame(speechFrame())).toBe("speech");
174
+ }
175
+ expect(interrupts).toHaveLength(0);
176
+ expect(ctrl.isAgentSpeaking()).toBe(true);
177
+ ctrl.stop();
178
+ });
179
+
180
+ test("idle agent: speech frames also age out of the window (no stale carry-over)", () => {
181
+ const { adapter, fire, interrupts } = makeStubAdapter();
182
+ const ctrl = createBargeInController({ adapter, triggerFrames: 4, windowMs: 60, frameMs: 30 });
183
+ // Agent idle: feed a long stream of speech. The window trims as it grows so
184
+ // it never accumulates, then a later audio_chunk + brief speech is still
185
+ // governed solely by the in-window count.
186
+ for (let i = 0; i < 20; i++) ctrl.feedAudioFrame(speechFrame());
187
+ expect(interrupts).toHaveLength(0);
188
+ fire({ kind: "audio_chunk", pcm: new Int16Array(0), sampleRate: 24_000 });
189
+ ctrl.feedAudioFrame(speechFrame());
190
+ expect(interrupts).toHaveLength(0);
191
+ ctrl.stop();
192
+ });
193
+
194
+ test("fires onBargeIn with the in-window speech-frame count", () => {
195
+ const { adapter, fire, interrupts } = makeStubAdapter();
196
+ const seen: Array<{ reason: string; speechFrames: number }> = [];
197
+ const ctrl = createBargeInController({
198
+ adapter,
199
+ triggerFrames: 4,
200
+ windowMs: 200,
201
+ frameMs: 30,
202
+ onBargeIn: (info) => seen.push(info),
203
+ });
204
+ fire({ kind: "audio_chunk", pcm: new Int16Array(0), sampleRate: 24_000 });
205
+ for (let i = 0; i < 4; i++) ctrl.feedAudioFrame(speechFrame());
206
+ expect(interrupts).toEqual(["user barged in"]);
207
+ expect(seen).toEqual([{ reason: "user barged in", speechFrames: 4 }]);
208
+ ctrl.stop();
209
+ });
210
+
211
+ test("interrupts even when no onBargeIn callback is supplied", () => {
212
+ const { adapter, fire, interrupts } = makeStubAdapter();
213
+ const ctrl = createBargeInController({ adapter, triggerFrames: 2, windowMs: 200, frameMs: 30 });
214
+ fire({ kind: "audio_chunk", pcm: new Int16Array(0), sampleRate: 24_000 });
215
+ ctrl.feedAudioFrame(speechFrame());
216
+ ctrl.feedAudioFrame(speechFrame());
217
+ expect(interrupts).toEqual(["user barged in"]);
218
+ ctrl.stop();
219
+ });
220
+
221
+ test("tolerates an adapter whose interrupt() throws mid-barge-in", () => {
222
+ // Custom adapter: interrupt() throws (e.g. socket already closed). The
223
+ // controller must swallow it, still mark the agent idle, and still fire
224
+ // the onBargeIn observer.
225
+ const handlers = new Set<RealtimeEventHandler>();
226
+ let interruptCalls = 0;
227
+ const adapter: RealtimeAdapter = {
228
+ providerId: "openai",
229
+ sampleRate: 24_000,
230
+ get connected() {
231
+ return true;
232
+ },
233
+ async connect() {},
234
+ sendAudio() {},
235
+ sendText() {},
236
+ interrupt() {
237
+ interruptCalls += 1;
238
+ throw new Error("socket closed");
239
+ },
240
+ on(h) {
241
+ handlers.add(h);
242
+ return () => {
243
+ handlers.delete(h);
244
+ };
245
+ },
246
+ async disconnect() {},
247
+ };
248
+ const fire = (event: RealtimeEvent): void => {
249
+ for (const h of handlers) h(event);
250
+ };
251
+ let info: { reason: string; speechFrames: number } | undefined;
252
+ const ctrl = createBargeInController({
253
+ adapter,
254
+ triggerFrames: 4,
255
+ onBargeIn: (i) => {
256
+ info = i;
257
+ },
258
+ });
259
+ fire({ kind: "audio_chunk", pcm: new Int16Array(0), sampleRate: 24_000 });
260
+ expect(() => {
261
+ for (let i = 0; i < 4; i++) ctrl.feedAudioFrame(speechFrame());
262
+ }).not.toThrow();
263
+ expect(interruptCalls).toBe(1);
264
+ expect(info).toEqual({ reason: "user barged in", speechFrames: 4 });
265
+ expect(ctrl.isAgentSpeaking()).toBe(false);
266
+ ctrl.stop();
267
+ });
268
+
269
+ test("uses an injected detector instead of the default", () => {
270
+ const { adapter, fire, interrupts } = makeStubAdapter();
271
+ let detectCalls = 0;
272
+ // Deterministic detector that always classifies frames as speech.
273
+ const detector = {
274
+ aggressiveness: 2 as const,
275
+ detect(): "speech" | "silence" | "transitioning" {
276
+ detectCalls += 1;
277
+ return "speech";
278
+ },
279
+ };
280
+ const ctrl = createBargeInController({ adapter, detector, triggerFrames: 2 });
281
+ fire({ kind: "audio_chunk", pcm: new Int16Array(0), sampleRate: 24_000 });
282
+ // Even silent frames count as speech because the injected detector says so.
283
+ expect(ctrl.feedAudioFrame(silentFrame())).toBe("speech");
284
+ expect(ctrl.feedAudioFrame(silentFrame())).toBe("speech");
285
+ expect(detectCalls).toBe(2);
286
+ expect(interrupts).toEqual(["user barged in"]);
287
+ ctrl.stop();
288
+ });
289
+
290
+ test("stop() detaches the adapter listener (later events are ignored)", () => {
291
+ const { adapter, fire, interrupts } = makeStubAdapter();
292
+ const ctrl = createBargeInController({ adapter, triggerFrames: 2 });
293
+ ctrl.stop();
294
+ // After stop(), an audio_chunk must not flip the agent to speaking, so a
295
+ // subsequent speech burst cannot barge in.
296
+ fire({ kind: "audio_chunk", pcm: new Int16Array(0), sampleRate: 24_000 });
297
+ expect(ctrl.isAgentSpeaking()).toBe(false);
298
+ ctrl.feedAudioFrame(speechFrame());
299
+ ctrl.feedAudioFrame(speechFrame());
300
+ expect(interrupts).toHaveLength(0);
301
+ });
133
302
  });