diff --git a/agents/src/voice/agent_activity.ts b/agents/src/voice/agent_activity.ts index 88cdc8b3..4fddb9b7 100644 --- a/agents/src/voice/agent_activity.ts +++ b/agents/src/voice/agent_activity.ts @@ -333,12 +333,8 @@ export class AgentActivity implements RecognitionHooks { } } - attachAudioInput(audioStream: ReadableStream): void { - if (this.audioStream.isSourceSet) { - this.logger.debug('detaching existing audio input in agent activity'); - this.audioStream.detachSource(); - } - + updateAudioInput(audioStream: ReadableStream): void { + this.detachAudioInput(); /** * We need to add a deferred ReadableStream layer on top of the audioStream from the agent session. * The tee() operation should be applied to the deferred stream, not the original audioStream. @@ -358,7 +354,9 @@ export class AgentActivity implements RecognitionHooks { } detachAudioInput(): void { - this.audioStream.detachSource(); + if (this.audioStream.isSourceSet) { + this.audioStream.detachSource(); + } } commitUserTurn() { diff --git a/agents/src/voice/agent_session.ts b/agents/src/voice/agent_session.ts index 2f15cbc2..fc322aec 100644 --- a/agents/src/voice/agent_session.ts +++ b/agents/src/voice/agent_session.ts @@ -272,7 +272,7 @@ export class AgentSession< await this.activity.start(); if (this._input.audio) { - this.activity.attachAudioInput(this._input.audio.stream); + this.activity.updateAudioInput(this._input.audio.stream); } } @@ -327,17 +327,17 @@ export class AgentSession< } // -- User changed input/output streams/sinks -- - private onAudioInputChanged(): void { + private onAudioInputChanged = (): void => { if (!this.started) { return; } if (this.activity && this._input.audio) { - this.activity.attachAudioInput(this._input.audio.stream); + this.activity.updateAudioInput(this._input.audio.stream); } - } + }; - private onAudioOutputChanged(): void {} + private onAudioOutputChanged = (): void => {}; - private onTextOutputChanged(): void {} + private onTextOutputChanged = (): void => {}; }