Handle messages received from the observer connection. This callback processes messages received from the control plane observer connection. It receives the same event types and shapes as the reference Chat socket.
(message: dict)
| 121 | |
| 122 | |
| 123 | async def observer_message_handler(message: dict) -> None: |
| 124 | """Handle messages received from the observer connection. |
| 125 | |
| 126 | This callback processes messages received from the control plane observer |
| 127 | connection. It receives the same event types and shapes as the reference |
| 128 | Chat socket. |
| 129 | """ |
| 130 | msg_type = message.get("type", "unknown") |
| 131 | |
| 132 | if msg_type == "chat_metadata": |
| 133 | print( |
| 134 | f"[OBSERVER] Chat ID: {message.get('chat_id')}, Chat Group ID: {message.get('chat_group_id')}" |
| 135 | ) |
| 136 | # Uncomment the line below for full message details |
| 137 | # print(f"[OBSERVER] Full message: {json.dumps(message, indent=2)}") |
| 138 | elif msg_type in ["user_message", "assistant_message"]: |
| 139 | role = message.get("message", {}).get("role", "unknown").upper() |
| 140 | content = message.get("message", {}).get("content", "") |
| 141 | print(f"[OBSERVER] {role}: {content}") |
| 142 | elif msg_type == "audio_output": |
| 143 | # Audio output messages contain large base64-encoded data |
| 144 | # Only print a summary to avoid cluttering the terminal |
| 145 | data_length = len(message.get("data", "")) |
| 146 | is_final = message.get("is_final_chunk", False) |
| 147 | print(f"[OBSERVER] Audio output: {data_length} bytes, final_chunk={is_final}") |
| 148 | # Uncomment the line below to see full audio message (very verbose!) |
| 149 | # print(f"[OBSERVER] Full message: {json.dumps(message, indent=2)}") |
| 150 | elif msg_type in ["user_interruption", "assistant_end"]: |
| 151 | # These are expected message types, just acknowledge them silently |
| 152 | # Uncomment the line below to see these messages |
| 153 | # print(f"[OBSERVER] Received: {msg_type}") |
| 154 | pass |
| 155 | elif msg_type == "error": |
| 156 | error_code = message.get("code", "unknown") |
| 157 | error_msg = message.get("message", "unknown error") |
| 158 | print(f"[OBSERVER] Error ({error_code}): {error_msg}") |
| 159 | print(f"[OBSERVER] Full message: {json.dumps(message, indent=2)}") |
| 160 | else: |
| 161 | print(f"[OBSERVER] Unknown message type: <{msg_type}>") |
| 162 | # Uncomment the line below for full message details |
| 163 | # print(f"[OBSERVER] Full message: {json.dumps(message, indent=2)}") |
| 164 | |
| 165 | |
| 166 | async def control_plane_demo( |
nothing calls this directly
no outgoing calls
no test coverage detected