xiaozhi-esp32-server/main/xiaozhi-server/core/events/vision_events.py
google-labs-jules[bot] fbdedcd4c5 feat: Add integrated image recognition and feedback
This feature enables xiaozhi-server to process images you upload via the
/mcp/vision/explain HTTP endpoint and provide spoken feedback based on
the image content.

Key changes:
- Introduced an internal event system (`core/events/vision_events.py`)
  using asyncio.Queue to decouple image processing from dialogue handling.
- Modified `VisionHandler` (`core/api/vision_handler.py`) to publish the
  textual output from the VLLM to this event system, instead of
  returning it directly in the HTTP response. The HTTP response now
  serves as an acknowledgment.
- Enhanced `ConnectionHandler` (`core/connection.py`):
    - It now listens for visual context events relevant to its device_id.
    - Stores the received visual text.
    - Uses an asyncio.Event (`visual_context_ready_event`) to signal
      the arrival of visual context.
    - Integrates the visual context into the prompt sent to the main LLM
      provider, allowing the LLM to generate responses informed by the
      image content.
    - Implements a timeout mechanism to wait for visual context if a
      vision-related query is made before the context is available.
- The spoken feedback is delivered through the existing TTS pipeline,
  processing the LLM's now visually-informed response.

This provides a more integrated way for you to interact with the
system regarding visual information.
2025-06-07 13:14:47 +00:00

46 lines
1.9 KiB
Python

import asyncio
from dataclasses import dataclass
@dataclass
class VisualContextEvent:
"""
Represents an event carrying visual context information.
"""
device_id: str
client_id: str # Added client_id as it's available in VisionHandler and might be useful
text: str
# Global queue to act as a simple event bus for visual context events
visual_context_event_queue = asyncio.Queue()
async def publish_visual_context_event(device_id: str, client_id: str, text: str):
"""
Publishes a visual context event to the queue.
"""
event = VisualContextEvent(device_id=device_id, client_id=client_id, text=text)
await visual_context_event_queue.put(event)
async def consume_visual_context_event() -> VisualContextEvent:
"""
Consumes a visual context event from the queue.
This will block until an event is available.
"""
event = await visual_context_event_queue.get()
return event
# Example of how a consumer might use this:
# async def event_consumer_example(device_id_to_listen_for: str):
# while True:
# print(f"Consumer for {device_id_to_listen_for} waiting for event...")
# event = await consume_visual_context_event()
# if event.device_id == device_id_to_listen_for:
# print(f"Consumer for {device_id_to_listen_for} received: {event.text}")
# else:
# # Not our event, put it back if other consumers might need it
# # or if there's a central dispatcher. For a simple queue with
# # specific consumers, this might not be ideal.
# # A more robust pub/sub would handle routing.
# print(f"Consumer for {device_id_to_listen_for} ignored event for {event.device_id}")
# # For now, we'll assume direct consumption or a dispatcher will handle filtering.
# # If putting back: await visual_context_event_queue.put(event)
# pass