From 3f50e3c7e9c18ee56728c356e99efcf94b8d37c7 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 9 Jul 2026 22:40:33 -0500 Subject: [PATCH] refac --- cptr/utils/agents/claude_code.py | 14 ++++++-- cptr/utils/chat_task.py | 55 +++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/cptr/utils/agents/claude_code.py b/cptr/utils/agents/claude_code.py index fc16951..68a23ba 100644 --- a/cptr/utils/agents/claude_code.py +++ b/cptr/utils/agents/claude_code.py @@ -35,7 +35,9 @@ def _prompt_from_messages(messages: list[dict[str, Any]]) -> str: return "\n\n".join(parts) -def _claude_query_input(prompt: str, attachments: PreparedAgentAttachments) -> Any: +def _claude_query_input( + prompt: str, attachments: PreparedAgentAttachments +) -> str | AsyncIterator[dict[str, Any]]: if not attachments.images: return prompt content: list[dict[str, Any]] = [] @@ -52,7 +54,15 @@ def _claude_query_input(prompt: str, attachments: PreparedAgentAttachments) -> A }, } ) - return content + + async def messages() -> AsyncIterator[dict[str, Any]]: + yield { + "type": "user", + "message": {"role": "user", "content": content}, + "parent_tool_use_id": None, + } + + return messages() def _chat_approval_mode(chat_params: dict[str, Any]) -> str: diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index 9672e9e..bba5aa7 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -1521,21 +1521,22 @@ async def run_chat_task( runner = runners.get(agent_target.agent) if runner is None: raise RuntimeError(f"Unsupported agent type: {agent_target.agent}") - reasoning_buffer = "" - reasoning_item_id = f"reasoning-{message_id}" + + def _active_reasoning_item(): + return next( + ( + item + for item in reversed(output_items) + if item.get("type") == "reasoning" and item.get("status") == "in_progress" + ), + None, + ) async def _finish_reasoning_item(): - existing = next( - (item for item in output_items if item.get("id") == reasoning_item_id), None - ) - if not existing or existing.get("status") == "completed": + existing = _active_reasoning_item() + if not existing: return - item = { - "type": "reasoning", - "id": reasoning_item_id, - "status": "completed", - "content": [{"type": "reasoning_text", "text": reasoning_buffer}], - } + item = {**existing, "status": "completed"} _upsert_output_item(output_items, item) await emit(output=item) _sync_state() @@ -1557,13 +1558,29 @@ async def run_chat_task( await emit(delta=event.text) _sync_state() elif isinstance(event, AgentReasoningDelta): - reasoning_buffer += event.text - item = { - "type": "reasoning", - "id": reasoning_item_id, - "status": "in_progress", - "content": [{"type": "reasoning_text", "text": reasoning_buffer}], - } + existing = _active_reasoning_item() + if existing: + blocks = existing.get("content") or [] + text = next( + ( + block.get("text", "") + for block in blocks + if isinstance(block, dict) and block.get("type") == "reasoning_text" + ), + "", + ) + item = { + **existing, + "content": [{"type": "reasoning_text", "text": f"{text}{event.text}"}], + } + else: + section = sum(item.get("type") == "reasoning" for item in output_items) + 1 + item = { + "type": "reasoning", + "id": f"reasoning-{message_id}-{section}", + "status": "in_progress", + "content": [{"type": "reasoning_text", "text": event.text}], + } _upsert_output_item(output_items, item) await emit(output=item) _sync_state()