-
Notifications
You must be signed in to change notification settings - Fork 257
Reuse STT Pipeline Across Agent Handoff #1177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
toubatbrian
wants to merge
7
commits into
main
Choose a base branch
from
brian/reuse-stt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9910340
Update .gitignore
toubatbrian 4950f1a
Update audio_recognition.ts
toubatbrian 0edef57
save changes
toubatbrian eebbdb3
completed tests
toubatbrian d9066f3
Create tasty-pillows-serve.md
toubatbrian b8c3546
fix tests
toubatbrian ed40f9b
Merge branch 'main' into brian/reuse-stt
toubatbrian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@livekit/agents": patch | ||
| --- | ||
|
|
||
| Reuse STT Pipeline Across Agent Handoff |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -203,3 +203,4 @@ examples/src/test_*.ts | |
| # OpenTelemetry trace test output | ||
| .traces/ | ||
| *.traces.json | ||
| .worktrees/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // SPDX-FileCopyrightText: 2026 LiveKit, Inc. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import type { AudioFrame } from '@livekit/rtc-node'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { type SpeechEvent } from '../stt/stt.js'; | ||
| import { Agent } from './agent.js'; | ||
| import { AgentActivity } from './agent_activity.js'; | ||
|
|
||
| type FakeActivity = { | ||
| agent: Agent; | ||
| audioRecognition: { detachSttPipeline: ReturnType<typeof vi.fn> } | undefined; | ||
| stt: unknown; | ||
| }; | ||
|
|
||
| function createFakeActivity(agent: Agent, stt: unknown) { | ||
| const detachedPipeline = { id: Symbol('pipeline') }; | ||
| const activity = { | ||
| agent, | ||
| audioRecognition: { | ||
| detachSttPipeline: vi.fn(async () => detachedPipeline), | ||
| }, | ||
| stt, | ||
| } as FakeActivity; | ||
|
|
||
| return { activity, detachedPipeline }; | ||
| } | ||
|
|
||
| async function detachIfReusable(oldActivity: FakeActivity, newActivity: FakeActivity) { | ||
| return await (AgentActivity.prototype as any)._detachSttPipelineIfReusable.call( | ||
| oldActivity, | ||
| newActivity, | ||
| ); | ||
| } | ||
|
|
||
| describe('AgentActivity STT handoff reuse eligibility', () => { | ||
| it('reuses the pipeline when both activities share the same STT instance and sttNode', async () => { | ||
| const sharedStt = { id: 'shared-stt' }; | ||
| const oldActivity = createFakeActivity(new Agent({ instructions: 'a' }), sharedStt); | ||
| const newActivity = createFakeActivity(new Agent({ instructions: 'b' }), sharedStt); | ||
|
|
||
| const result = await detachIfReusable(oldActivity.activity, newActivity.activity); | ||
|
|
||
| expect(result).toBe(oldActivity.detachedPipeline); | ||
| expect(oldActivity.activity.audioRecognition?.detachSttPipeline).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not reuse when the STT instances differ', async () => { | ||
| const oldActivity = createFakeActivity(new Agent({ instructions: 'a' }), { id: 'stt-a' }); | ||
| const newActivity = createFakeActivity(new Agent({ instructions: 'b' }), { id: 'stt-b' }); | ||
|
|
||
| const result = await detachIfReusable(oldActivity.activity, newActivity.activity); | ||
|
|
||
| expect(result).toBeUndefined(); | ||
| expect(oldActivity.activity.audioRecognition?.detachSttPipeline).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not reuse when either activity has no STT', async () => { | ||
| const sharedStt = { id: 'shared-stt' }; | ||
| const oldActivity = createFakeActivity(new Agent({ instructions: 'a' }), undefined); | ||
| const newActivity = createFakeActivity(new Agent({ instructions: 'b' }), sharedStt); | ||
|
|
||
| const result = await detachIfReusable(oldActivity.activity, newActivity.activity); | ||
|
|
||
| expect(result).toBeUndefined(); | ||
| expect(oldActivity.activity.audioRecognition?.detachSttPipeline).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not reuse when the agents override sttNode differently', async () => { | ||
| const sharedStt = { id: 'shared-stt' }; | ||
|
|
||
| class AgentA extends Agent { | ||
| async sttNode(_audio: ReadableStream<AudioFrame>, _modelSettings: any) { | ||
| return null as ReadableStream<SpeechEvent | string> | null; | ||
| } | ||
| } | ||
|
|
||
| class AgentB extends Agent { | ||
| async sttNode(_audio: ReadableStream<AudioFrame>, _modelSettings: any) { | ||
| return null as ReadableStream<SpeechEvent | string> | null; | ||
| } | ||
| } | ||
|
|
||
| const oldActivity = createFakeActivity(new AgentA({ instructions: 'a' }), sharedStt); | ||
| const newActivity = createFakeActivity(new AgentB({ instructions: 'b' }), sharedStt); | ||
|
|
||
| const result = await detachIfReusable(oldActivity.activity, newActivity.activity); | ||
|
|
||
| expect(result).toBeUndefined(); | ||
| expect(oldActivity.activity.audioRecognition?.detachSttPipeline).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('reuses when the new agent inherits the same sttNode implementation', async () => { | ||
| const sharedStt = { id: 'shared-stt' }; | ||
|
|
||
| class AgentA extends Agent { | ||
| async sttNode(_audio: ReadableStream<AudioFrame>, _modelSettings: any) { | ||
| return null as ReadableStream<SpeechEvent | string> | null; | ||
| } | ||
| } | ||
|
|
||
| class AgentB extends AgentA {} | ||
|
|
||
| const oldActivity = createFakeActivity(new AgentA({ instructions: 'a' }), sharedStt); | ||
| const newActivity = createFakeActivity(new AgentB({ instructions: 'b' }), sharedStt); | ||
|
|
||
| const result = await detachIfReusable(oldActivity.activity, newActivity.activity); | ||
|
|
||
| expect(result).toBe(oldActivity.detachedPipeline); | ||
| expect(oldActivity.activity.audioRecognition?.detachSttPipeline).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not reuse when the old activity has no audioRecognition', async () => { | ||
| const sharedStt = { id: 'shared-stt' }; | ||
| const oldActivity = createFakeActivity(new Agent({ instructions: 'a' }), sharedStt); | ||
| const newActivity = createFakeActivity(new Agent({ instructions: 'b' }), sharedStt); | ||
| oldActivity.activity.audioRecognition = undefined; | ||
|
|
||
| const result = await detachIfReusable(oldActivity.activity, newActivity.activity); | ||
|
|
||
| expect(result).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.