Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.
fix(voice-runtime): listen from the first microphone frame before the provider opens
muterC0691b987886e
diff --git a/apps/voice-runtime/src/live-worker.test.ts b/apps/voice-runtime/src/live-worker.test.tsindex 551efc33..a2330798 100644--- a/apps/voice-runtime/src/live-worker.test.ts+++ b/apps/voice-runtime/src/live-worker.test.ts@@ -525,7 +525,7 @@ describe("LiveVoiceWorker", () => {expect(test.session().close).toHaveBeenCalledOnce();});- it("waits for the provider even when PCM has already arrived", async () => {+ it("listens before the provider opens and sends that speech once it does", async () => {let ready!: () => void;const providerOpening = new Promise<void>((resolve) => {ready = resolve;@@ -534,16 +534,21 @@ describe("LiveVoiceWorker", () => {providerOpening,beforeReady: async ({ track, readyAttributes }) => {track.frame(0);- await new Promise((resolve) => setTimeout(resolve, 20));- expect(readyAttributes()).toEqual([]);+ // The microphone alone makes the call ready to hear the user.+ await vi.waitFor(() =>+ expect(readyAttributes()).toContainEqual(+ expect.objectContaining({ "linger.voice_ready_generation": "1" }),+ ),+ );+ track.frame(1);ready();},});+ // What was said while the provider opened reaches it, in order.await vi.waitFor(() =>- expect(test.localParticipant.setAttributes).toHaveBeenCalledWith(- expect.objectContaining({ "linger.voice_ready_generation": "1" }),- ),+ expect(test.session().audio.length).toBeGreaterThanOrEqual(2),);+ expect(test.session().audio[0]![0]).toBe(0);await test.shutdown();});diff --git a/apps/voice-runtime/src/live-worker.ts b/apps/voice-runtime/src/live-worker.tsindex 66ac2648..847f90a2 100644--- a/apps/voice-runtime/src/live-worker.ts+++ b/apps/voice-runtime/src/live-worker.ts@@ -2005,30 +2005,37 @@ export class LiveVoiceWorker {Math.round(firstFrameAt - this.sessionCreatedAtMs),);this.reportBoundary(epoch, "track_started", startMs, startMs);+ // The call listens once the microphone arrives, before the provider+ // session opens: speech before then waits in the track's bounded input+ // buffer and goes to the provider in order once it opens. Its+ // transcript shows up then.if (Number.isSafeInteger(generation) && generation > 0 && track.sid) {const trackSid = track.sid;- void this.sessionReady.then(() => {- if (!consuming || signal.aborted || this.stopped || this.failed)- return;- const renew = () =>- withTimeout(- this.ctx.room.localParticipant!.setAttributes({- "linger.voice_ready_generation": String(generation),- "linger.voice_ready_track": trackSid,- "linger.voice_ready_at": String(Date.now()),- "linger.voice_purpose": "chat_live",- }),- 1_000,- "Voice input readiness timed out",- )- .then(() => this.timing("voice_ready_set"))- .catch(() => undefined);- void renew();- readinessTimer = setInterval(() => {+ const renew = () =>+ withTimeout(+ this.ctx.room.localParticipant!.setAttributes({+ "linger.voice_ready_generation": String(generation),+ "linger.voice_ready_track": trackSid,+ "linger.voice_ready_at": String(Date.now()),+ "linger.voice_purpose": "chat_live",+ }),+ 1_000,+ "Voice input readiness timed out",+ )+ .then(() => this.timing("voice_ready_set"))+ .catch(() => undefined);+ void renew();+ readinessTimer = setInterval(() => {+ if (consuming && !signal.aborted && !this.stopped && !this.failed)void renew();- }, 2_000);- });+ }, 2_000);}+ await Promise.race([+ this.sessionReady,+ new Promise<void>((resolve) =>+ signal.addEventListener("abort", () => resolve(), { once: true }),+ ),+ ]);for (;;) {const queuedAt = input.oldestRetainedAtMs();const frame = await input.next(signal);@@ -2039,7 +2046,7 @@ export class LiveVoiceWorker {Date.now() - queuedAt,);processedMs += (frame.samplesPerChannel / frame.sampleRate) * 1_000;- // Audio before the provider session is ready predates readiness.+ // A provider session that is reconnecting drops what arrives meanwhile.if (!this.session) continue;const session = this.session;await this.detectSpeech(toDetectorRate(frame.data), signal);