From 5ac52c756daec41ec069ec9d72eb8dbde8b2730c Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 7 Jul 2026 04:45:13 -0500 Subject: [PATCH] refac --- cptr/utils/agents/claude_code.py | 27 +++++++++++++++++++++------ cptr/utils/agents/events.py | 6 ++++++ cptr/utils/chat_task.py | 13 ++++++++++--- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/cptr/utils/agents/claude_code.py b/cptr/utils/agents/claude_code.py index 1292ca9..5806d01 100644 --- a/cptr/utils/agents/claude_code.py +++ b/cptr/utils/agents/claude_code.py @@ -13,6 +13,7 @@ from cptr.utils.agents.events import ( AgentDone, AgentError, AgentEvent, + AgentReasoningDone, AgentReasoningDelta, AgentTextDelta, AgentToolUpdate, @@ -160,7 +161,9 @@ async def run_claude_code_agent( usage: dict[str, Any] | None = None observed_session_id = session_id tool_calls: dict[int, AgentToolUpdate] = {} - received_content_delta = False + thinking_blocks: set[int] = set() + received_text_delta = False + received_thinking_delta = False async for message in stream: event = getattr(message, "event", None) @@ -174,23 +177,33 @@ async def run_claude_code_agent( ): text = delta["text"] if text: - received_content_delta = True + received_text_delta = True yield AgentTextDelta(text) elif delta.get("type") == "thinking_delta" and isinstance( delta.get("thinking"), str ): + index = event.get("index") + if isinstance(index, int): + thinking_blocks.add(index) thinking = delta["thinking"] + yield AgentReasoningDelta(thinking) if thinking: - received_content_delta = True - yield AgentReasoningDelta(thinking) + received_thinking_delta = True elif event_type == "content_block_start": index, tool = _tool_update_from_claude_start(event) if tool: if index is not None: tool_calls[index] = tool yield tool + elif isinstance(index, int): + block = event.get("content_block") + if isinstance(block, dict) and block.get("type") == "thinking": + thinking_blocks.add(index) elif event_type == "content_block_stop": index = event.get("index") + if isinstance(index, int) and index in thinking_blocks: + thinking_blocks.discard(index) + yield AgentReasoningDone() tool = tool_calls.get(index) if isinstance(index, int) else None if tool: yield AgentToolUpdate( @@ -204,14 +217,16 @@ async def run_claude_code_agent( class_name = message.__class__.__name__ if class_name == "AssistantMessage": - if received_content_delta: - continue for block in getattr(message, "content", []) or []: if block.__class__.__name__ == "TextBlock": + if received_text_delta: + continue text = getattr(block, "text", "") if text: yield AgentTextDelta(text) elif block.__class__.__name__ == "ThinkingBlock": + if received_thinking_delta: + continue text = getattr(block, "thinking", "") if text: yield AgentReasoningDelta(text) diff --git a/cptr/utils/agents/events.py b/cptr/utils/agents/events.py index d0dcd6d..f1adb88 100644 --- a/cptr/utils/agents/events.py +++ b/cptr/utils/agents/events.py @@ -16,6 +16,11 @@ class AgentReasoningDelta: text: str +@dataclass +class AgentReasoningDone: + pass + + @dataclass class AgentToolUpdate: call_id: str @@ -46,6 +51,7 @@ class AgentError: AgentEvent = ( AgentTextDelta | AgentReasoningDelta + | AgentReasoningDone | AgentToolUpdate | AgentToolOutputDelta | AgentDone diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index 9df126b..47924db 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -49,6 +49,7 @@ from cptr.utils.agents.events import ( AgentDone, AgentError, AgentReasoningDelta, + AgentReasoningDone, AgentTextDelta, AgentToolOutputDelta, AgentToolUpdate, @@ -1522,13 +1523,17 @@ async def run_chat_task( if runner is None: raise RuntimeError(f"Unsupported agent type: {agent_target.agent}") reasoning_buffer = "" + reasoning_item_id = f"reasoning-{message_id}" async def _finish_reasoning_item(): - if not reasoning_buffer: + existing = next( + (item for item in output_items if item.get("id") == reasoning_item_id), None + ) + if not existing or existing.get("status") == "completed": return item = { "type": "reasoning", - "id": f"reasoning-{message_id}", + "id": reasoning_item_id, "status": "completed", "content": [{"type": "reasoning_text", "text": reasoning_buffer}], } @@ -1555,13 +1560,15 @@ async def run_chat_task( reasoning_buffer += event.text item = { "type": "reasoning", - "id": f"reasoning-{message_id}", + "id": reasoning_item_id, "status": "in_progress", "content": [{"type": "reasoning_text", "text": reasoning_buffer}], } _upsert_output_item(output_items, item) await emit(output=item) _sync_state() + elif isinstance(event, AgentReasoningDone): + await _finish_reasoning_item() elif isinstance(event, AgentToolUpdate): flushed_item = _flush_text() if flushed_item: