-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat(examples/avatar): added animation triggers and trimmed personas #5899
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
Merged
+253
−168
Merged
Changes from all commits
Commits
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
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,140 @@ | ||
| """Avatar pose triggers via LLM tools (wave, dance, turn).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import logging | ||
| import os | ||
| import time | ||
| from dataclasses import dataclass | ||
|
|
||
| import aiohttp | ||
|
|
||
| logger = logging.getLogger("avatar.actions") | ||
|
|
||
| NONE = "none" | ||
|
|
||
| ACTION_PERSONAS = frozenset({"leila", "jess", "mr_fox"}) | ||
|
|
||
| POSE_NAMES: dict[str, dict[str, str]] = { | ||
| "leila": { | ||
| "wave": "wave-2-leila", | ||
| "turn": "turn-leila", | ||
| "dance": "dance-leila", | ||
| }, | ||
| "jess": { | ||
| "wave": "jess_wave", | ||
| "turn": "jess_turn", | ||
| "dance": "jess_dance", | ||
| }, | ||
| "mr_fox": { | ||
| "wave": "fox2_wave", | ||
| "turn": "fox2_turn", | ||
| "dance": "fox2_dance", | ||
| }, | ||
| } | ||
|
|
||
| DEFAULT_POSE_DURATION_S = 6.0 | ||
| OPENING_WAVE_DELAY_S = 0.5 | ||
|
|
||
|
|
||
| def supports_actions(persona_id: str) -> bool: | ||
| return persona_id in ACTION_PERSONAS | ||
|
|
||
|
|
||
| def _control_url(session_id: str) -> str: | ||
| base = os.getenv("LEMONSLICE_API_BASE", "https://lemonslice.com/api").rstrip("/") | ||
| return f"{base}/liveai/sessions/{session_id}/control" | ||
|
|
||
|
|
||
| async def trigger_pose(session_id: str, name: str) -> bool: | ||
| url = _control_url(session_id) | ||
| payload = {"event": "pose-trigger", "pose_trigger": {"name": name}} | ||
| timeout = aiohttp.ClientTimeout(total=30.0) | ||
| async with aiohttp.ClientSession(timeout=timeout) as session: | ||
| async with session.post( | ||
| url, | ||
| headers={ | ||
| "Content-Type": "application/json", | ||
| "X-API-Key": os.environ["LEMONSLICE_API_KEY"], | ||
| }, | ||
| json=payload, | ||
| ) as response: | ||
| return response.ok | ||
|
|
||
|
|
||
| @dataclass | ||
| class _PlayingSlot: | ||
| ends_at: float | ||
|
|
||
|
|
||
| class ActionController: | ||
| """Plays one LemonSlice pose at a time; each blocks others for ``DEFAULT_POSE_DURATION_S``.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| self._lock = asyncio.Lock() | ||
| self._session_id: str | None = None | ||
| self._persona_id: str | None = None | ||
| self._slot: _PlayingSlot | None = None | ||
|
|
||
| def set_session(self, session_id: str, persona_id: str) -> None: | ||
| self._session_id = session_id | ||
| self._persona_id = persona_id | ||
|
|
||
| def clear_session(self) -> None: | ||
| self._session_id = None | ||
| self._persona_id = None | ||
|
|
||
| def _current_slot(self) -> _PlayingSlot | None: | ||
| if self._slot is None: | ||
| return None | ||
| if time.monotonic() >= self._slot.ends_at: | ||
| self._slot = None | ||
| return None | ||
| return self._slot | ||
|
|
||
| async def cancel(self) -> None: | ||
| async with self._lock: | ||
| self._slot = None | ||
| sid = self._session_id | ||
| self.clear_session() | ||
| if sid is not None: | ||
| await trigger_pose(sid, NONE) | ||
|
|
||
| async def shutdown(self, _: str = "") -> None: | ||
| await self.cancel() | ||
|
|
||
| async def play(self, action_id: str) -> str: | ||
| session_id = self._session_id | ||
| persona_id = self._persona_id | ||
| if session_id is None or persona_id is None: | ||
| return "Motion unavailable — avatar session not ready." | ||
|
|
||
| key = action_id.strip().lower() | ||
| pose_name = POSE_NAMES.get(persona_id, {}).get(key) | ||
| if pose_name is None: | ||
| return f"Unknown motion {action_id!r}." | ||
|
|
||
| async with self._lock: | ||
| if self._current_slot() is not None: | ||
| return "That motion is already playing; try again in a moment." | ||
|
|
||
| ok = await trigger_pose(session_id, pose_name) | ||
| if not ok: | ||
| return "Could not trigger the motion on the avatar." | ||
|
|
||
| self._slot = _PlayingSlot( | ||
| ends_at=time.monotonic() + DEFAULT_POSE_DURATION_S, | ||
| ) | ||
| logger.info( | ||
| "pose playing: persona_id=%r action_id=%r pose_name=%r", | ||
| persona_id, | ||
| key, | ||
| pose_name, | ||
| ) | ||
| return f"Playing motion {key}." | ||
|
|
||
| async def opening_wave(self) -> None: | ||
| if OPENING_WAVE_DELAY_S > 0: | ||
| await asyncio.sleep(OPENING_WAVE_DELAY_S) | ||
| await self.play("wave") | ||
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.
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.
🟡 Unhandled network exceptions from
trigger_poseinopening_waveprevent agent greetingThe
trigger_posefunction (actions.py:50-63) makes an HTTP request viaaiohttpbut doesn't catch network-level exceptions (e.g.,aiohttp.ClientError,asyncio.TimeoutError). While it handles non-OK HTTP responses by returningFalse, actual connection failures raise exceptions. Theplay()method (actions.py:107) also doesn't catch these, so exceptions propagate throughopening_wave()to the caller.In the entrypoint (
agent.py:112), ifopening_wave()raises,session.generate_reply()atagent.py:114is never reached — the agent silently fails to greet the user. In theset_avatarRPC handler (agent.py:166), the same failure prevents both the greeting (agent.py:168) and the RPC response (agent.py:181). A transient network error during a cosmetic wave animation should not break the core conversation flow.Was this helpful? React with 👍 or 👎 to provide feedback.