feat(voice/avatar): add waitForJoin helper#1594
feat(voice/avatar): add waitForJoin helper#1594rosetta-livekit-bot[bot] wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: a4a25e7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 33 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| * @param timeout - Timeout in milliseconds. Pass `null` to wait indefinitely. | ||
| */ | ||
| async waitForJoin({ timeout = 30000 }: { timeout?: number | null } = {}): Promise<void> { | ||
| if (!this.#waitAvatarJoinPromise) return; |
There was a problem hiding this comment.
🔴 waitForJoin() silently resolves immediately when room is not yet connected at start() time
When start() is called while the room is not yet connected, #waitAvatarJoinPromise is not set — instead, only a ConnectionStateChanged listener is registered (avatar_session.ts:87), and #startWaitAvatarJoin() runs later when the room connects. However, waitForJoin() at line 98 checks if (!this.#waitAvatarJoinPromise) return; and returns immediately (resolves successfully) if the promise hasn't been created yet.
This means the typical usage pattern await avatarSession.start(agentSession, room); await avatarSession.waitForJoin(); will silently succeed without actually waiting for the avatar to join whenever the room isn't connected at start() time. Callers will proceed under the false assumption that the avatar participant is present in the room.
Prompt for agents
The problem is in `waitForJoin()` at line 98 of `agents/src/voice/avatar/avatar_session.ts`. When `start()` is called with a room that is not yet connected, `#waitAvatarJoinPromise` is not set until the `ConnectionStateChanged` event fires (see `start()` lines 84-88 and `#onConnectionStateChanged` at line 221). But `waitForJoin()` returns immediately if `#waitAvatarJoinPromise` is falsy.
To fix this, `waitForJoin` needs to handle the case where the join promise hasn't been created yet. One approach: always create `#waitAvatarJoinPromise` eagerly in `start()` (even when the room isn't connected yet), and have it internally wait for the connection first before waiting for the participant. Another approach: in `waitForJoin`, if `#waitAvatarJoinPromise` is not yet set but `start()` has been called (i.e. `#room` is set), wait for the promise to be created (e.g. using a Future/deferred that resolves when `#startWaitAvatarJoin` runs) before proceeding with the timeout race.
Was this helpful? React with 👍 or 👎 to provide feedback.
No description provided.