From 7ee31a3961d298c20722fd5b54c5a33dd12d550f Mon Sep 17 00:00:00 2001 From: helloopenworld <496467672@qq.com> Date: Thu, 16 Jul 2026 14:28:59 +0800 Subject: [PATCH] Runner: avoid data copy --- trpc_agent_sdk/runners.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/trpc_agent_sdk/runners.py b/trpc_agent_sdk/runners.py index ffc40914..88e2f7e9 100644 --- a/trpc_agent_sdk/runners.py +++ b/trpc_agent_sdk/runners.py @@ -453,7 +453,7 @@ async def run_async( last_non_streaming_event = None # Track accumulated partial text for cancellation handling - temp_text = "" + temp_text_parts: list[str] = [] try: # Support multiple levels of agent transfers @@ -463,15 +463,15 @@ async def run_async( transfer_requested = False async for event in current_agent.run_async(invocation_context): - # Track partial text accumulation + # Track partial text accumulation (store refs; join only on cancel) if event.partial: if event.content and event.content.parts: for part in event.content.parts: if part.text: - temp_text += part.text + temp_text_parts.append(part.text) else: - # Clear temp_text on full event - temp_text = "" + # Clear accumulated parts on full event + temp_text_parts.clear() if not event.partial: await self.session_service.append_event(session=session, event=event) @@ -583,6 +583,7 @@ async def run_async( # Capture partial state at cancellation point state_partial = dict(session.state) + temp_text = "".join(temp_text_parts) # Handle session cleanup for cancellation (two scenarios: streaming vs non-streaming) await cancel.handle_cancellation_session_cleanup(