-
Notifications
You must be signed in to change notification settings - Fork 173
Add support for away timeout #801
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
base: main
Are you sure you want to change the base?
Changes from all commits
906cfae
16164a6
71f8ff8
2b51ecd
500a5f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@livekit/agents': patch | ||
| --- | ||
|
|
||
| Emit away events for User |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,7 @@ export interface VoiceOptions { | |
| maxEndpointingDelay: number; | ||
| maxToolSteps: number; | ||
| preemptiveGeneration: boolean; | ||
| userAwayTimeout?: number | null; | ||
| } | ||
|
|
||
| const defaultVoiceOptions: VoiceOptions = { | ||
|
|
@@ -69,6 +70,7 @@ const defaultVoiceOptions: VoiceOptions = { | |
| maxEndpointingDelay: 6000, | ||
| maxToolSteps: 3, | ||
| preemptiveGeneration: false, | ||
| userAwayTimeout: 15.0, | ||
| } as const; | ||
|
|
||
| export type TurnDetectionMode = 'stt' | 'vad' | 'realtime_llm' | 'manual' | _TurnDetector; | ||
|
|
@@ -123,6 +125,7 @@ export class AgentSession< | |
| private _output: AgentOutput; | ||
|
|
||
| private closingTask: Promise<void> | null = null; | ||
| private userAwayTimer: NodeJS.Timeout | null = null; | ||
|
|
||
| constructor(opts: AgentSessionOptions<UserData>) { | ||
| super(); | ||
|
|
@@ -167,6 +170,8 @@ export class AgentSession< | |
| // This is the "global" chat context, it holds the entire conversation history | ||
| this._chatCtx = ChatContext.empty(); | ||
| this.options = { ...defaultVoiceOptions, ...voiceOptions }; | ||
|
|
||
| this.on(AgentSessionEventTypes.UserInputTranscribed, this._onUserInputTranscribed.bind(this)); | ||
| } | ||
|
|
||
| get input(): AgentInput { | ||
|
|
@@ -416,6 +421,14 @@ export class AgentSession< | |
|
|
||
| const oldState = this._agentState; | ||
| this._agentState = state; | ||
|
|
||
| // Handle user away timer based on state changes | ||
| if (state === 'listening' && this.userState === 'listening') { | ||
| this._setUserAwayTimer(); | ||
| } else { | ||
| this._cancelUserAwayTimer(); | ||
| } | ||
|
|
||
| this.emit( | ||
| AgentSessionEventTypes.AgentStateChanged, | ||
| createAgentStateChangedEvent(oldState, state), | ||
|
|
@@ -430,6 +443,14 @@ export class AgentSession< | |
|
|
||
| const oldState = this.userState; | ||
| this.userState = state; | ||
|
|
||
| // Handle user away timer based on state changes | ||
| if (state === 'listening' && this._agentState === 'listening') { | ||
| this._setUserAwayTimer(); | ||
| } else { | ||
| this._cancelUserAwayTimer(); | ||
| } | ||
|
|
||
| this.emit( | ||
| AgentSessionEventTypes.UserStateChanged, | ||
| createUserStateChangedEvent(oldState, state), | ||
|
|
@@ -451,6 +472,37 @@ export class AgentSession< | |
|
|
||
| private onTextOutputChanged(): void {} | ||
|
|
||
| private _setUserAwayTimer(): void { | ||
| this._cancelUserAwayTimer(); | ||
|
|
||
| if (this.options.userAwayTimeout === null || this.options.userAwayTimeout === undefined) { | ||
| return; | ||
| } | ||
|
|
||
| if (this.roomIO && !this.roomIO.isParticipantAvailable) { | ||
| return; | ||
| } | ||
|
|
||
| this.userAwayTimer = setTimeout(() => { | ||
| this.logger.debug('User away timeout triggered'); | ||
| this._updateUserState('away'); | ||
| }, this.options.userAwayTimeout * 1000); | ||
| } | ||
|
|
||
| private _cancelUserAwayTimer(): void { | ||
| if (this.userAwayTimer !== null) { | ||
| clearTimeout(this.userAwayTimer); | ||
| this.userAwayTimer = null; | ||
| } | ||
| } | ||
|
|
||
| private _onUserInputTranscribed(ev: UserInputTranscribedEvent): void { | ||
| if (this.userState === 'away' && ev.isFinal) { | ||
| this.logger.debug('User returned from away state due to speech input'); | ||
| this._updateUserState('listening'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be "speaking"?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re this and the above comment, happy to make this change. Do you feel like we should still be adjusting?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh I see, in this case let's keep this in parity with python. |
||
| } | ||
| } | ||
|
|
||
| private async closeImpl( | ||
| reason: CloseReason, | ||
| error: RealtimeModelError | LLMError | TTSError | STTError | null = null, | ||
|
|
@@ -460,6 +512,8 @@ export class AgentSession< | |
| return; | ||
| } | ||
|
|
||
| this._cancelUserAwayTimer(); | ||
|
|
||
| if (this.activity) { | ||
| if (!drain) { | ||
| try { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // SPDX-FileCopyrightText: 2025 LiveKit, Inc. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| /** | ||
| * Minimal example demonstrating idle user timeout functionality. | ||
| * Direct port of: https://github.com/livekit/agents/blob/main/examples/voice_agents/inactive_user.py | ||
| */ | ||
| import { | ||
| type JobContext, | ||
| type JobProcess, | ||
| Task, | ||
| WorkerOptions, | ||
| cli, | ||
| defineAgent, | ||
| delay, | ||
| log, | ||
| voice, | ||
| } from '@livekit/agents'; | ||
| import * as openai from '@livekit/agents-plugin-openai'; | ||
| import * as silero from '@livekit/agents-plugin-silero'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| export default defineAgent({ | ||
| prewarm: async (proc: JobProcess) => { | ||
| proc.userData.vad = await silero.VAD.load(); | ||
| }, | ||
| entry: async (ctx: JobContext) => { | ||
| const logger = log(); | ||
| const vad = ctx.proc.userData.vad! as silero.VAD; | ||
|
|
||
| const session = new voice.AgentSession({ | ||
| vad, | ||
| llm: new openai.LLM({ model: 'gpt-4o-mini' }), | ||
| stt: 'assemblyai/universal-streaming:en', | ||
| tts: 'cartesia/sonic-2:9626c31c-bec5-4cca-baa8-f8ba9e84c8bc', | ||
|
|
||
| voiceOptions: { | ||
| userAwayTimeout: 12.5, | ||
| }, | ||
| }); | ||
|
|
||
| let task: Task<void> | null = null; | ||
|
|
||
| const userPresenceTask = async (controller: AbortController): Promise<void> => { | ||
| for (let i = 0; i < 3; i++) { | ||
| if (controller.signal.aborted) return; | ||
|
|
||
| const reply = await session.generateReply({ | ||
| instructions: 'The user has been inactive. Politely check if the user is still present.', | ||
| }); | ||
|
|
||
| await reply.waitForPlayout(); | ||
|
|
||
| try { | ||
| await delay(10000, { signal: controller.signal }); | ||
| } catch { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (!controller.signal.aborted) { | ||
| await session.close(); | ||
| } | ||
| }; | ||
|
|
||
| session.on(voice.AgentSessionEventTypes.UserStateChanged, (event) => { | ||
| logger.info({ event }, 'User state changed'); | ||
|
|
||
| if (task) { | ||
| task.cancel(); | ||
| } | ||
|
Comment on lines
+45
to
+72
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks pretty good! |
||
|
|
||
| if (event.newState === 'away') { | ||
| task = Task.from(userPresenceTask); | ||
| return; | ||
| } | ||
| }); | ||
|
|
||
| const agent = new voice.Agent({ | ||
| instructions: 'You are a helpful assistant.', | ||
| }); | ||
|
|
||
| await session.start({ agent, room: ctx.room }); | ||
| }, | ||
| }); | ||
|
|
||
| cli.runApp(new WorkerOptions({ agent: fileURLToPath(import.meta.url) })); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we discard checking
ev.isFinal? So we can turn away state back much quicker as soon as user starts speaking