Enhance greeting context management in Brain classes
- Introduce greeting context handling in BaseBrain and WorkflowBrain to manage assistant greetings effectively. - Implement prepare_greeting_context method to add greeting messages to the local context while preserving playback order. - Update pipeline event handling to ensure greeting timestamps are maintained until the client is ready. - Enhance tests to verify the correct behavior of greeting context management in various scenarios.
This commit is contained in:
@@ -32,6 +32,7 @@ def bind_cascade_pipeline_events(
|
||||
|
||||
pending_text_inputs: list[str] = []
|
||||
greeting_transcript_sent = False
|
||||
greeting_timestamp = ""
|
||||
|
||||
async def queue_transcript(role: str, content: str, timestamp: str) -> None:
|
||||
if not content:
|
||||
@@ -122,11 +123,16 @@ def bind_cascade_pipeline_events(
|
||||
nonlocal greeting_transcript_sent
|
||||
if greeting and not greeting_transcript_sent:
|
||||
greeting_transcript_sent = True
|
||||
await queue_transcript("assistant", greeting, time_now_iso8601())
|
||||
await queue_transcript(
|
||||
"assistant",
|
||||
greeting,
|
||||
greeting_timestamp or time_now_iso8601(),
|
||||
)
|
||||
await brain.on_client_ready()
|
||||
|
||||
@transport.event_handler("on_client_connected")
|
||||
async def on_client_connected(_transport, _client):
|
||||
nonlocal greeting_timestamp
|
||||
if vision_enabled:
|
||||
try:
|
||||
vision_state["client_id"] = get_transport_client_id(
|
||||
@@ -140,8 +146,11 @@ def bind_cascade_pipeline_events(
|
||||
except Exception as exc: # noqa: BLE001 - media availability is optional
|
||||
logger.warning(f"视觉理解摄像头捕获初始化失败: {exc}")
|
||||
if greeting:
|
||||
# Preserve the actual playback order. The transcript is delivered
|
||||
# later on client-ready, but the preview sorts by this timestamp.
|
||||
greeting_timestamp = greeting_timestamp or time_now_iso8601()
|
||||
if brain.spec.owns_context:
|
||||
context.add_message({"role": "assistant", "content": greeting})
|
||||
brain.prepare_greeting_context(greeting, context)
|
||||
await worker.queue_frame(
|
||||
TTSSpeakFrame(greeting, append_to_context=False)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user