Enhance text stream processing by adding synchronization for interrupted assistant turns. Introduce sync_streamed_assistant_context to align LLM context with UI text, and modify ProductTextStreamProcessor to handle interrupted text. Update pipeline to utilize the new functionality for managing assistant turn interruptions.

This commit is contained in:
Xin Wang
2026-05-26 08:33:02 +08:00
parent 2edcb51805
commit e7a8cb1faa
2 changed files with 65 additions and 8 deletions

View File

@@ -36,7 +36,7 @@ from .config import EngineConfig
from .product_protocol import ProductWebsocketSerializer
from .services import create_llm_service, create_stt_service, create_tts_service
from .text_input import ProductTextInputProcessor
from .text_stream import ProductTextStreamProcessor
from .text_stream import ProductTextStreamProcessor, sync_streamed_assistant_context
from .transcript_stream import ProductTranscriptStreamProcessor
from .turn_start import InterruptionGateUserTurnStartStrategy
@@ -142,6 +142,8 @@ async def run_pipeline_with_serializer(
),
)
text_stream = ProductTextStreamProcessor()
pipeline = Pipeline(
[
transport.input(),
@@ -150,7 +152,7 @@ async def run_pipeline_with_serializer(
ProductTranscriptStreamProcessor(),
user_aggregator,
llm,
ProductTextStreamProcessor(),
text_stream,
tts,
transport.output(),
assistant_aggregator,
@@ -210,6 +212,14 @@ async def run_pipeline_with_serializer(
@assistant_aggregator.event_handler("on_assistant_turn_stopped")
async def on_assistant_turn_stopped(_aggregator, message: AssistantTurnStoppedMessage):
logger.info(f"Assistant: {message.content}")
if message.interrupted:
streamed = text_stream.take_interrupted_stream_text()
if streamed:
sync_streamed_assistant_context(
_aggregator,
streamed_text=streamed,
committed_text=message.content or "",
)
runner = PipelineRunner(handle_sigint=False)
await runner.run(task)