The agent that turns websites into APIs!
fix: revert to ClaudeSDKClient for reliable stream handling (#51)
kalil03214c8252c9f544
diff --git a/src/reverse_api/auto_engineer.py b/src/reverse_api/auto_engineer.pyindex aa9b05e..da096be 100644--- a/src/reverse_api/auto_engineer.py+++ b/src/reverse_api/auto_engineer.py@@ -11,14 +11,13 @@ import httpxfrom claude_agent_sdk import (AssistantMessage,ClaudeAgentOptions,- HookMatcher,+ ClaudeSDKClient,PermissionResultAllow,ResultMessage,TextBlock,ToolPermissionContext,ToolResultBlock,ToolUseBlock,- query,)from .engineer import ClaudeEngineer@@ -257,105 +256,93 @@ Your final response should confirm the files were created and provide a brief su],}- # Required: dummy hook keeps the stream open for can_use_tool- async def _dummy_hook(input_data: dict[str, Any], tool_use_id: str | None, context: Any) -> dict[str, Any]:- return {"continue_": True}-- async def _prompt_stream():- yield {- "type": "user",- "message": {"role": "user", "content": self._build_auto_prompt()},- }-options = ClaudeAgentOptions(mcp_servers={"playwright": mcp_config},+ permission_mode="bypassPermissions",can_use_tool=self._handle_tool_permission,- hooks={"PreToolUse": [HookMatcher(matcher=None, hooks=[_dummy_hook])]},cwd=str(self.scripts_dir.parent.parent), # Project rootmodel=self.model,- env={"CLAUDECODE": "", "CLAUDE_CODE_STREAM_CLOSE_TIMEOUT": "1800000"},+ env={"CLAUDECODE": ""},stderr=self._handle_cli_stderr,)- final_result: dict[str, Any] | None = None-try:- # Do not break/return inside this loop — the SDK requires the- # async generator to be fully consumed to avoid cancel-scope errors.- async for message in query(prompt=_prompt_stream(), options=options):- # Check for usage metadata- if hasattr(message, "usage") and isinstance(message.usage, dict):- self.usage_metadata.update(message.usage)-- if isinstance(message, AssistantMessage):- last_tool_name = None- for block in message.content:- if isinstance(block, ToolUseBlock):- last_tool_name = block.name- self.ui.tool_start(block.name, block.input)- self.message_store.save_tool_start(block.name, block.input)- elif isinstance(block, ToolResultBlock):- is_error = block.is_error if block.is_error else False-- # Extract output from ToolResultBlock- output = None- if hasattr(block, "content"):- output = block.content- elif hasattr(block, "result"):- output = block.result- elif hasattr(block, "output"):- output = block.output-- tool_name = last_tool_name or "Tool"- self.ui.tool_result(tool_name, is_error, output)- self.message_store.save_tool_result(tool_name, is_error, str(output) if output else None)- elif isinstance(block, TextBlock):- self.ui.thinking(block.text)- self.message_store.save_thinking(block.text)-- elif isinstance(message, ResultMessage):- if message.is_error:- self.ui.error(message.result or "Unknown error")- self.message_store.save_error(message.result or "Unknown error")- else:- script_path = str(self.scripts_dir / self._get_client_filename())- local_path = str(self.local_scripts_dir / self._get_client_filename()) if self.local_scripts_dir else None- self.ui.success(script_path, local_path)-- # Calculate estimated cost if we have usage data- if self.usage_metadata:- input_tokens = self.usage_metadata.get("input_tokens", 0)- output_tokens = self.usage_metadata.get("output_tokens", 0)- cache_creation_tokens = self.usage_metadata.get("cache_creation_input_tokens", 0)- cache_read_tokens = self.usage_metadata.get("cache_read_input_tokens", 0)-- from .pricing import calculate_cost-- cost = calculate_cost(- model_id=self.model,- input_tokens=input_tokens,- output_tokens=output_tokens,- cache_creation_tokens=cache_creation_tokens,- cache_read_tokens=cache_read_tokens,- )- self.usage_metadata["estimated_cost_usd"] = cost-- self.ui.console.print(" [dim]Usage:[/dim]")- if input_tokens > 0:- self.ui.console.print(f" [dim] input: {input_tokens:,} tokens[/dim]")- if cache_creation_tokens > 0:- self.ui.console.print(f" [dim] cache creation: {cache_creation_tokens:,} tokens[/dim]")- if cache_read_tokens > 0:- self.ui.console.print(f" [dim] cache read: {cache_read_tokens:,} tokens[/dim]")- if output_tokens > 0:- self.ui.console.print(f" [dim] output: {output_tokens:,} tokens[/dim]")- self.ui.console.print(f" [dim] total cost: ${cost:.4f}[/dim]")-- final_result = {- "script_path": script_path,- "usage": self.usage_metadata,- }- self.message_store.save_result(final_result)+ async with ClaudeSDKClient(options=options) as client:+ await client.query(self._build_auto_prompt())++ async for message in client.receive_response():+ if hasattr(message, "usage") and isinstance(message.usage, dict):+ self.usage_metadata.update(message.usage)++ if isinstance(message, AssistantMessage):+ last_tool_name = None+ for block in message.content:+ if isinstance(block, ToolUseBlock):+ last_tool_name = block.name+ self.ui.tool_start(block.name, block.input)+ self.message_store.save_tool_start(block.name, block.input)+ elif isinstance(block, ToolResultBlock):+ is_error = block.is_error if block.is_error else False++ output = None+ if hasattr(block, "content"):+ output = block.content+ elif hasattr(block, "result"):+ output = block.result+ elif hasattr(block, "output"):+ output = block.output++ tool_name = last_tool_name or "Tool"+ self.ui.tool_result(tool_name, is_error, output)+ self.message_store.save_tool_result(tool_name, is_error, str(output) if output else None)+ elif isinstance(block, TextBlock):+ self.ui.thinking(block.text)+ self.message_store.save_thinking(block.text)++ elif isinstance(message, ResultMessage):+ if message.is_error:+ self.ui.error(message.result or "Unknown error")+ self.message_store.save_error(message.result or "Unknown error")+ return None+ else:+ script_path = str(self.scripts_dir / self._get_client_filename())+ local_path = str(self.local_scripts_dir / self._get_client_filename()) if self.local_scripts_dir else None+ self.ui.success(script_path, local_path)++ if self.usage_metadata:+ input_tokens = self.usage_metadata.get("input_tokens", 0)+ output_tokens = self.usage_metadata.get("output_tokens", 0)+ cache_creation_tokens = self.usage_metadata.get("cache_creation_input_tokens", 0)+ cache_read_tokens = self.usage_metadata.get("cache_read_input_tokens", 0)++ from .pricing import calculate_cost++ cost = calculate_cost(+ model_id=self.model,+ input_tokens=input_tokens,+ output_tokens=output_tokens,+ cache_creation_tokens=cache_creation_tokens,+ cache_read_tokens=cache_read_tokens,+ )+ self.usage_metadata["estimated_cost_usd"] = cost++ self.ui.console.print(" [dim]Usage:[/dim]")+ if input_tokens > 0:+ self.ui.console.print(f" [dim] input: {input_tokens:,} tokens[/dim]")+ if cache_creation_tokens > 0:+ self.ui.console.print(f" [dim] cache creation: {cache_creation_tokens:,} tokens[/dim]")+ if cache_read_tokens > 0:+ self.ui.console.print(f" [dim] cache read: {cache_read_tokens:,} tokens[/dim]")+ if output_tokens > 0:+ self.ui.console.print(f" [dim] output: {output_tokens:,} tokens[/dim]")+ self.ui.console.print(f" [dim] total cost: ${cost:.4f}[/dim]")++ result: dict[str, Any] = {+ "script_path": script_path,+ "usage": self.usage_metadata,+ }+ self.message_store.save_result(result)+ return resultexcept Exception as e:error_msg = str(e)@@ -374,7 +361,7 @@ Your final response should confirm the files were created and provide a brief suself.ui.console.print("\n[dim]Make sure Claude Code CLI is installed: npm install -g @anthropic-ai/claude-code[/dim]")return None- return final_result+ return Noneclass OpenCodeAutoEngineer(OpenCodeEngineer):diff --git a/src/reverse_api/engineer.py b/src/reverse_api/engineer.pyindex 6a239d1..a422d6b 100644--- a/src/reverse_api/engineer.py+++ b/src/reverse_api/engineer.py@@ -8,14 +8,13 @@ from typing import Anyfrom claude_agent_sdk import (AssistantMessage,ClaudeAgentOptions,- HookMatcher,+ ClaudeSDKClient,PermissionResultAllow,ResultMessage,TextBlock,ToolPermissionContext,ToolResultBlock,ToolUseBlock,- query,)from .base_engineer import BaseEngineer@@ -46,105 +45,92 @@ class ClaudeEngineer(BaseEngineer):self.ui.start_analysis()self.message_store.save_prompt(self._build_analysis_prompt())- # Required: dummy hook keeps the stream open for can_use_tool- # See: https://platform.claude.com/docs/en/agent-sdk/user-input- async def _dummy_hook(input_data: dict[str, Any], tool_use_id: str | None, context: Any) -> dict[str, Any]:- return {"continue_": True}-- async def _prompt_stream():- yield {- "type": "user",- "message": {"role": "user", "content": self._build_analysis_prompt()},- }-options = ClaudeAgentOptions(+ permission_mode="acceptEdits",can_use_tool=self._handle_tool_permission,- hooks={"PreToolUse": [HookMatcher(matcher=None, hooks=[_dummy_hook])]},cwd=str(self.scripts_dir.parent.parent), # Project rootmodel=self.model,- env={"CLAUDECODE": "", "CLAUDE_CODE_STREAM_CLOSE_TIMEOUT": "1800000"},+ env={"CLAUDECODE": ""},stderr=self._handle_cli_stderr,)- final_result: dict[str, Any] | None = None-try:- # Do not break/return inside this loop — the SDK requires the- # async generator to be fully consumed to avoid cancel-scope errors.- async for message in query(prompt=_prompt_stream(), options=options):- # Check for usage metadata in message if applicable- if hasattr(message, "usage") and isinstance(message.usage, dict):- self.usage_metadata.update(message.usage)-- if isinstance(message, AssistantMessage):- last_tool_name = None- for block in message.content:- if isinstance(block, ToolUseBlock):- last_tool_name = block.name- self.ui.tool_start(block.name, block.input)- self.message_store.save_tool_start(block.name, block.input)- elif isinstance(block, ToolResultBlock):- is_error = block.is_error if block.is_error else False-- # Extract output from ToolResultBlock- output = None- if hasattr(block, "content"):- output = block.content- elif hasattr(block, "result"):- output = block.result- elif hasattr(block, "output"):- output = block.output-- tool_name = last_tool_name or "Tool"- self.ui.tool_result(tool_name, is_error, output)- self.message_store.save_tool_result(tool_name, is_error, str(output) if output else None)- elif isinstance(block, TextBlock):- self.ui.thinking(block.text)- self.message_store.save_thinking(block.text)-- elif isinstance(message, ResultMessage):- if message.is_error:- self.ui.error(message.result or "Unknown error")- self.message_store.save_error(message.result or "Unknown error")- else:- script_path = str(self.scripts_dir / self._get_client_filename())- local_path = str(self.local_scripts_dir / self._get_client_filename()) if self.local_scripts_dir else None- self.ui.success(script_path, local_path)-- # Calculate estimated cost if we have usage data- if self.usage_metadata:- input_tokens = self.usage_metadata.get("input_tokens", 0)- output_tokens = self.usage_metadata.get("output_tokens", 0)- cache_creation_tokens = self.usage_metadata.get("cache_creation_input_tokens", 0)- cache_read_tokens = self.usage_metadata.get("cache_read_input_tokens", 0)-- from .pricing import calculate_cost-- cost = calculate_cost(- model_id=self.model,- input_tokens=input_tokens,- output_tokens=output_tokens,- cache_creation_tokens=cache_creation_tokens,- cache_read_tokens=cache_read_tokens,- )- self.usage_metadata["estimated_cost_usd"] = cost-- self.ui.console.print(" [dim]Usage:[/dim]")- if input_tokens > 0:- self.ui.console.print(f" [dim] input: {input_tokens:,} tokens[/dim]")- if cache_creation_tokens > 0:- self.ui.console.print(f" [dim] cache creation: {cache_creation_tokens:,} tokens[/dim]")- if cache_read_tokens > 0:- self.ui.console.print(f" [dim] cache read: {cache_read_tokens:,} tokens[/dim]")- if output_tokens > 0:- self.ui.console.print(f" [dim] output: {output_tokens:,} tokens[/dim]")- self.ui.console.print(f" [dim] total cost: ${cost:.4f}[/dim]")-- final_result = {- "script_path": script_path,- "usage": self.usage_metadata,- }- self.message_store.save_result(final_result)+ async with ClaudeSDKClient(options=options) as client:+ await client.query(self._build_analysis_prompt())++ async for message in client.receive_response():+ if hasattr(message, "usage") and isinstance(message.usage, dict):+ self.usage_metadata.update(message.usage)++ if isinstance(message, AssistantMessage):+ last_tool_name = None+ for block in message.content:+ if isinstance(block, ToolUseBlock):+ last_tool_name = block.name+ self.ui.tool_start(block.name, block.input)+ self.message_store.save_tool_start(block.name, block.input)+ elif isinstance(block, ToolResultBlock):+ is_error = block.is_error if block.is_error else False++ output = None+ if hasattr(block, "content"):+ output = block.content+ elif hasattr(block, "result"):+ output = block.result+ elif hasattr(block, "output"):+ output = block.output++ tool_name = last_tool_name or "Tool"+ self.ui.tool_result(tool_name, is_error, output)+ self.message_store.save_tool_result(tool_name, is_error, str(output) if output else None)+ elif isinstance(block, TextBlock):+ self.ui.thinking(block.text)+ self.message_store.save_thinking(block.text)++ elif isinstance(message, ResultMessage):+ if message.is_error:+ self.ui.error(message.result or "Unknown error")+ self.message_store.save_error(message.result or "Unknown error")+ return None+ else:+ script_path = str(self.scripts_dir / self._get_client_filename())+ local_path = str(self.local_scripts_dir / self._get_client_filename()) if self.local_scripts_dir else None+ self.ui.success(script_path, local_path)++ if self.usage_metadata:+ input_tokens = self.usage_metadata.get("input_tokens", 0)+ output_tokens = self.usage_metadata.get("output_tokens", 0)+ cache_creation_tokens = self.usage_metadata.get("cache_creation_input_tokens", 0)+ cache_read_tokens = self.usage_metadata.get("cache_read_input_tokens", 0)++ from .pricing import calculate_cost++ cost = calculate_cost(+ model_id=self.model,+ input_tokens=input_tokens,+ output_tokens=output_tokens,+ cache_creation_tokens=cache_creation_tokens,+ cache_read_tokens=cache_read_tokens,+ )+ self.usage_metadata["estimated_cost_usd"] = cost++ self.ui.console.print(" [dim]Usage:[/dim]")+ if input_tokens > 0:+ self.ui.console.print(f" [dim] input: {input_tokens:,} tokens[/dim]")+ if cache_creation_tokens > 0:+ self.ui.console.print(f" [dim] cache creation: {cache_creation_tokens:,} tokens[/dim]")+ if cache_read_tokens > 0:+ self.ui.console.print(f" [dim] cache read: {cache_read_tokens:,} tokens[/dim]")+ if output_tokens > 0:+ self.ui.console.print(f" [dim] output: {output_tokens:,} tokens[/dim]")+ self.ui.console.print(f" [dim] total cost: ${cost:.4f}[/dim]")++ result: dict[str, Any] = {+ "script_path": script_path,+ "usage": self.usage_metadata,+ }+ self.message_store.save_result(result)+ return resultexcept Exception as e:self.ui.error(str(e))@@ -152,7 +138,7 @@ class ClaudeEngineer(BaseEngineer):self.ui.console.print("\n[dim]Make sure Claude Code CLI is installed: npm install -g @anthropic-ai/claude-code[/dim]")return None- return final_result+ return None# Keep old class name for backwards compatibility