fix text input no context update

This commit is contained in:
Xin Wang
2026-05-22 21:30:54 +08:00
parent 7267c06552
commit 1ffc716575
2 changed files with 41 additions and 11 deletions

View File

@@ -1,5 +1,9 @@
from __future__ import annotations
from dataclasses import dataclass
from loguru import logger
from pipecat.frames.frames import (
Frame,
InterruptionFrame,
@@ -9,7 +13,16 @@ from pipecat.frames.frames import (
OutputTransportMessageUrgentFrame,
TTSSpeakFrame,
)
from pipecat.processors.aggregators.llm_context import LLMContext
from pipecat.processors.frame_processor import FrameDirection, FrameProcessor
from pipecat.utils.time import time_now_iso8601
@dataclass
class ProductAssistantTurnStoppedMessage:
content: str
interrupted: bool
timestamp: str
class ProductTextStreamProcessor(FrameProcessor):
@@ -29,10 +42,13 @@ class ProductTextStreamProcessor(FrameProcessor):
started/delta/final sequence for its fixed text.
"""
def __init__(self) -> None:
def __init__(self, context: LLMContext | None = None) -> None:
super().__init__()
self._context = context
self._aggregation: list[str] = []
self._turn_active = False
self._turn_start_timestamp = ""
self._register_event_handler("on_assistant_turn_stopped")
async def process_frame(self, frame: Frame, direction: FrameDirection) -> None:
await super().process_frame(frame, direction)
@@ -62,6 +78,7 @@ class ProductTextStreamProcessor(FrameProcessor):
return
self._turn_active = True
self._aggregation = []
self._turn_start_timestamp = time_now_iso8601()
await self._emit("response.text.started")
async def _delta(self, text: str) -> None:
@@ -78,11 +95,26 @@ class ProductTextStreamProcessor(FrameProcessor):
full_text = "".join(self._aggregation)
self._turn_active = False
self._aggregation = []
if self._context and full_text:
self._context.add_message({"role": "assistant", "content": full_text})
logger.info(
"Assistant committed to LLM context before TTS: "
f"{full_text[:120]}"
)
await self._emit(
"response.text.final",
text=full_text,
interrupted=interrupted,
)
await self._call_event_handler(
"on_assistant_turn_stopped",
ProductAssistantTurnStoppedMessage(
content=full_text,
interrupted=interrupted,
timestamp=self._turn_start_timestamp or time_now_iso8601(),
),
)
self._turn_start_timestamp = ""
async def _emit(self, event_type: str, **payload: object) -> None:
await self.push_frame(