Skip to content

Commit fc8e254

Browse files
refactor(voice): rename CustomEvent -> DebugMessage; expose internal _emit_debug_message
Picks up livekit/protocol#1593 which renamed the agent-session event before any consumer shipped it. The message is repositioned as an internal debug/trace channel surfaced only to the debugger/recorder (e.g. agents-cli), not to user code. Proto change (mechanical rename): - AgentSessionEvent.custom_event -> AgentSessionEvent.debug_message (field 21) - agent_pb.CustomEvent (str type, Struct payload) -> agent_pb.DebugMessage (Struct payload) Surface change: - AgentSession.emit_custom_event(event_type, payload) -> AgentSession._emit_debug_message(payload) - underscore prefix + `:meta private:` docstring marker signal: not for user code - type discriminator dropped; callers just emit a JSON payload - EventTypes literal "custom_event" -> "debug_message" - SessionHost._on_custom_event -> _on_debug_message Pin bump: `livekit-protocol>=1.1.9` -> `>=1.1.10` to pick up DebugMessage. (CI type-check is blocked until livekit/python-sdks#689 merges and a new livekit-protocol release is cut to PyPI.) Drive-by deslop on the helper body: - `from google.protobuf...` imports lifted to module scope (avoids the no-inline-imports convention warning). - Dropped the defensive `if payload:` + `dict(payload)` cast and the `ignore_unknown_fields=True` kwarg (Struct fields aren't user fields). - Trimmed the 12-line docstring + 3-line inline comment down to the one non-obvious line that documents *why* we use `super().emit`. ruff check + ruff format --check clean. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 357f498 commit fc8e254

4 files changed

Lines changed: 14 additions & 29 deletions

File tree

livekit-agents/livekit/agents/voice/agent_session.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
runtime_checkable,
2020
)
2121

22+
from google.protobuf.json_format import ParseDict
23+
from google.protobuf.struct_pb2 import Struct
2224
from opentelemetry import context as otel_context, trace
2325
from typing_extensions import TypedDict
2426

@@ -1222,30 +1224,13 @@ def interrupt(self, *, force: bool = False) -> asyncio.Future[None]:
12221224

12231225
return self._activity.interrupt(force=force)
12241226

1225-
def emit_custom_event(self, event_type: str, payload: Mapping[str, Any] | None = None) -> None:
1226-
"""Emit an application-defined ``custom_event`` over the remote-session wire.
1227-
1228-
Sugar over constructing ``agent_pb.CustomEvent`` and emitting it: wraps
1229-
``payload`` (a JSON-compatible mapping) into a ``google.protobuf.Struct`` and
1230-
emits the proto. Listeners registered via
1231-
``session.on("custom_event", handler)`` receive the same
1232-
``agent_pb.CustomEvent``.
1233-
1234-
Args:
1235-
event_type: Application-defined type tag (maps to ``CustomEvent.type``).
1236-
payload: JSON-compatible mapping (maps to ``CustomEvent.payload``). Defaults
1237-
to an empty struct.
1238-
"""
1239-
from google.protobuf.json_format import ParseDict
1240-
from google.protobuf.struct_pb2 import Struct
1241-
1227+
def _emit_debug_message(self, payload: Mapping[str, Any]) -> None:
1228+
""":meta private: internal — emit a debug/trace payload to the debugger/recorder."""
12421229
st = Struct()
1243-
if payload:
1244-
ParseDict(dict(payload), st, ignore_unknown_fields=True)
1245-
# bypass the typed `AgentSession.emit` override (which narrows to AgentEvent
1246-
# for the event-trace report): custom events ride the proto, not the
1247-
# Pydantic union, and aren't part of the typed report.
1248-
super().emit("custom_event", agent_pb.CustomEvent(type=event_type, payload=st))
1230+
ParseDict(payload, st)
1231+
# super().emit bypasses AgentSession.emit's narrowed AgentEvent type;
1232+
# debug messages ride the proto, not the Pydantic event union.
1233+
super().emit("debug_message", agent_pb.DebugMessage(payload=st))
12491234

12501235
def clear_user_turn(self) -> None:
12511236
# clear the transcription or input audio buffer of the user turn

livekit-agents/livekit/agents/voice/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ async def wait_for_playout(self) -> None:
101101
"speech_created",
102102
"error",
103103
"close",
104-
"custom_event",
104+
"debug_message",
105105
]
106106

107107
UserState = Literal["speaking", "listening", "away"]

livekit-agents/livekit/agents/voice/remote_session.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ def register_session(self, session: AgentSession) -> None:
375375
session.on("session_usage_updated", self._on_session_usage_updated)
376376
session.on("overlapping_speech", self._on_overlapping_speech)
377377
session.on("error", self._on_error)
378-
session.on("custom_event", self._on_custom_event)
378+
session.on("debug_message", self._on_debug_message)
379379

380380
def register_text_input(self, text_input_cb: TextInputCallback) -> None:
381381
self._text_input_cb = text_input_cb
@@ -402,7 +402,7 @@ async def aclose(self) -> None:
402402
self._session.off("session_usage_updated", self._on_session_usage_updated)
403403
self._session.off("overlapping_speech", self._on_overlapping_speech)
404404
self._session.off("error", self._on_error)
405-
self._session.off("custom_event", self._on_custom_event)
405+
self._session.off("debug_message", self._on_debug_message)
406406

407407
if self._recv_task:
408408
await utils.aio.cancel_and_wait(self._recv_task)
@@ -576,8 +576,8 @@ def _on_error(self, event: ErrorEvent) -> None:
576576
)
577577
)
578578

579-
def _on_custom_event(self, event: agent_pb.CustomEvent) -> None:
580-
self._send_event(agent_pb.AgentSessionEvent(custom_event=event))
579+
def _on_debug_message(self, event: agent_pb.DebugMessage) -> None:
580+
self._send_event(agent_pb.AgentSessionEvent(debug_message=event))
581581

582582
async def _handle_request_safe(self, req: agent_pb.SessionRequest) -> None:
583583
try:

livekit-agents/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies = [
3131
"certifi>=2025.6.15",
3232
"livekit==1.1.8",
3333
"livekit-api>=1.0.7,<2",
34-
"livekit-protocol>=1.1.9,<2",
34+
"livekit-protocol>=1.1.10,<2",
3535
"livekit-blingfire~=1.1,<2",
3636
"protobuf>=3",
3737
"pyjwt>=2.0",

0 commit comments

Comments
 (0)