The agent that turns websites into APIs!
fix: address PR #44 review comments
Claude1e8a16d509b5
diff --git a/src/reverse_api/auto_engineer.py b/src/reverse_api/auto_engineer.pyindex 66eb770..4229afe 100644--- a/src/reverse_api/auto_engineer.py+++ b/src/reverse_api/auto_engineer.py@@ -652,6 +652,7 @@ class CopilotAutoEngineer:elif event_type == "session.idle":done_event.set()+ client = Nonetry:client = CopilotClient({@@ -683,7 +684,14 @@ class CopilotAutoEngineer:session.on(on_event)await session.send({"prompt": auto_prompt})- await done_event.wait()++ # Wait with timeout protection (10 minutes)+ try:+ await asyncio.wait_for(done_event.wait(), timeout=600)+ except TimeoutError:+ eng.ui.error("Session timed out (10 min)")+ eng.message_store.save_error("Session timed out")+ return Noneif accumulated_text:eng.message_store.save_thinking("".join(accumulated_text))@@ -711,3 +719,11 @@ class CopilotAutoEngineer:else:eng.ui.console.print("\n[dim]Make sure GitHub Copilot CLI is installed and you are logged in: gh auth login[/dim]")return None++ finally:+ # Always stop the client to avoid resource leaks+ if client is not None:+ try:+ await client.stop()+ except Exception:+ passdiff --git a/src/reverse_api/base_engineer.py b/src/reverse_api/base_engineer.pyindex 35d4189..e2c7d95 100644--- a/src/reverse_api/base_engineer.py+++ b/src/reverse_api/base_engineer.py@@ -4,9 +4,11 @@ from abc import ABC, abstractmethodfrom pathlib import Pathfrom typing import Any+import questionary+from .messages import MessageStorefrom .sync import FileSyncWatcher, get_available_directory-from .tui import ClaudeUI+from .tui import THEME_PRIMARY, THEME_SECONDARY, ClaudeUIfrom .utils import generate_folder_name, get_docs_dir, get_scripts_dir@@ -103,6 +105,122 @@ class BaseEngineer(ABC):return self.sync_watcher.get_status()return None+ async def _ask_user_interactive(self, questions: list[dict[str, Any]]) -> dict[str, str]:+ """Prompt the user interactively for answers to questions.++ Shared logic used by both ClaudeEngineer and CopilotEngineer.++ Args:+ questions: List of question dicts with keys: question, header, options, multiSelect++ Returns:+ Dict mapping question text to user's answer string.+ """+ answers: dict[str, str] = {}++ self.ui.console.print()+ self.ui.console.print(f" [{THEME_PRIMARY}]?[/{THEME_PRIMARY}] [bold white]Agent Question[/bold white]")+ self.ui.console.print()++ for q in questions:+ question_text = q.get("question", "") if isinstance(q, dict) else getattr(q, "question", "")+ header = q.get("header", "") if isinstance(q, dict) else getattr(q, "header", "")+ options = q.get("options", []) if isinstance(q, dict) else getattr(q, "options", [])+ multi_select = q.get("multiSelect", False) if isinstance(q, dict) else getattr(q, "multiSelect", False)++ if not question_text:+ continue++ if header:+ self.ui.console.print(f" [dim]{header}[/dim]")++ try:+ if multi_select:+ choices = [+ f"{self._get_opt_field(opt, 'label')} - {self._get_opt_field(opt, 'description')}"+ if self._get_opt_field(opt, "description")+ else self._get_opt_field(opt, "label")+ for opt in options+ ]+ if choices:+ selected = await questionary.checkbox(+ f" > {question_text}",+ choices=choices,+ qmark="",+ style=questionary.Style(+ [+ ("pointer", f"fg:{THEME_PRIMARY} bold"),+ ("highlighted", f"fg:{THEME_PRIMARY} bold"),+ ("selected", f"fg:{THEME_PRIMARY}"),+ ]+ ),+ ).ask_async()++ if selected is None:+ raise KeyboardInterrupt++ labels = [s.split(" - ")[0] if " - " in s else s for s in selected]+ answers[question_text] = ", ".join(labels)+ else:+ answer = await questionary.text(+ f" > {question_text}",+ qmark="",+ style=questionary.Style([("question", f"fg:{THEME_SECONDARY}")]),+ ).ask_async()+ if answer is None:+ raise KeyboardInterrupt+ answers[question_text] = answer.strip()+ else:+ choices = [+ f"{self._get_opt_field(opt, 'label')} - {self._get_opt_field(opt, 'description')}"+ if self._get_opt_field(opt, "description")+ else self._get_opt_field(opt, "label")+ for opt in options+ ]+ if choices:+ answer = await questionary.select(+ f" > {question_text}",+ choices=choices,+ qmark="",+ style=questionary.Style(+ [+ ("pointer", f"fg:{THEME_PRIMARY} bold"),+ ("highlighted", f"fg:{THEME_PRIMARY} bold"),+ ]+ ),+ ).ask_async()++ if answer is None:+ raise KeyboardInterrupt++ label = answer.split(" - ")[0] if " - " in answer else answer+ answers[question_text] = label+ else:+ answer = await questionary.text(+ f" > {question_text}",+ qmark="",+ style=questionary.Style([("question", f"fg:{THEME_SECONDARY}")]),+ ).ask_async()+ if answer is None:+ raise KeyboardInterrupt+ answers[question_text] = answer.strip()++ self.ui.console.print(f" [dim]→ {answers[question_text]}[/dim]")++ except KeyboardInterrupt:+ self.ui.console.print(" [dim]User cancelled question[/dim]")+ answers[question_text] = ""++ self.ui.console.print()+ return answers++ @staticmethod+ def _get_opt_field(opt: Any, field: str) -> str:+ """Get a field from an option, supporting both dict and object access."""+ if isinstance(opt, dict):+ return opt.get(field, "")+ return getattr(opt, field, "")+def _get_output_extension(self) -> str:"""Return file extension based on output language."""return {@@ -333,7 +451,9 @@ Here is the output directory where you should save your generated files:</output_dir>**IMPORTANT: You have access to the AskUserQuestion tool to ask clarifying questions during your analysis.**-Use this tool when you need to clarify functional requirements, prioritize features, choose between implementation approaches, or gather any other information that would help you generate better {task_description}.+Use this tool when you need to clarify functional requirements, prioritize features, choose between implementation approaches, or gather any other information that would help you generate better {+ task_description+ }.Your task is to:@@ -378,7 +498,10 @@ In your scratchpad:- Identify any ambiguities or questions you should ask the user using AskUserQuestion</scratchpad>-{"" if self.output_mode == "docs" else """If your first attempt doesn't work, analyze what went wrong and try again. Document each attempt and what you learned.+{+ ""+ if self.output_mode == "docs"+ else '''If your first attempt doesn't work, analyze what went wrong and try again. Document each attempt and what you learned.<attempt_log>For each attempt (up to 5), document:@@ -388,7 +511,8 @@ For each attempt (up to 5), document:- What you changed for the next attempt</attempt_log>-"""}After {"documenting" if self.output_mode == "docs" else "testing"}, provide your final response with:+'''+ }After {"documenting" if self.output_mode == "docs" else "testing"}, provide your final response with:- A summary of the APIs discovered- The authentication method used- {"The completeness and accuracy of the OpenAPI spec" if self.output_mode == "docs" else "Whether the implementation works"}@@ -396,7 +520,9 @@ For each attempt (up to 5), document:- The paths to the generated filesYour final output should confirm that the files have been created and provide a brief summary of what was accomplished.-Do not include the full {"spec" if self.output_mode == "docs" else "code"} in your response - just confirm the files were saved and summarize the key findings.+Do not include the full {+ "spec" if self.output_mode == "docs" else "code"+ } in your response - just confirm the files were saved and summarize the key findings."""if self.additional_instructions:base_prompt += f"\n\nAdditional instructions:\n{self.additional_instructions}"diff --git a/src/reverse_api/copilot_engineer.py b/src/reverse_api/copilot_engineer.pyindex 7c1e546..4a5005b 100644--- a/src/reverse_api/copilot_engineer.py+++ b/src/reverse_api/copilot_engineer.py@@ -3,10 +3,10 @@import asynciofrom typing import Any-import questionary-from .base_engineer import BaseEngineer-from .tui import THEME_PRIMARY, THEME_SECONDARY++# Timeout for waiting on Copilot session completion (10 minutes)+_SESSION_TIMEOUT = 600class CopilotEngineer(BaseEngineer):@@ -45,93 +45,14 @@ class CopilotEngineer(BaseEngineer):class AskUserParams(BaseModel):questions: list[Question] = Field(description="Questions to ask the user")- ui = self.ui+ # Capture self for use in the tool handler+ engineer = self@define_tool(description="Ask the user a clarifying question. Use when you need user input to proceed.") # type: ignore[misc]async def ask_user_question(params: AskUserParams) -> str:- answers: dict[str, str] = {}-- ui.console.print()- ui.console.print(f" [{THEME_PRIMARY}]?[/{THEME_PRIMARY}] [bold white]Agent Question[/bold white]")- ui.console.print()-- for q in params.questions:- if not q.question:- continue-- if q.header:- ui.console.print(f" [dim]{q.header}[/dim]")-- try:- if q.multiSelect:- choices = [f"{opt.label} - {opt.description}" if opt.description else opt.label for opt in q.options]- if choices:- selected = await questionary.checkbox(- f" > {q.question}",- choices=choices,- qmark="",- style=questionary.Style(- [- ("pointer", f"fg:{THEME_PRIMARY} bold"),- ("highlighted", f"fg:{THEME_PRIMARY} bold"),- ("selected", f"fg:{THEME_PRIMARY}"),- ]- ),- ).ask_async()-- if selected is None:- raise KeyboardInterrupt-- labels = [s.split(" - ")[0] if " - " in s else s for s in selected]- answers[q.question] = ", ".join(labels)- else:- answer = await questionary.text(- f" > {q.question}",- qmark="",- style=questionary.Style([("question", f"fg:{THEME_SECONDARY}")]),- ).ask_async()- if answer is None:- raise KeyboardInterrupt- answers[q.question] = answer.strip()- else:- choices = [f"{opt.label} - {opt.description}" if opt.description else opt.label for opt in q.options]- if choices:- answer = await questionary.select(- f" > {q.question}",- choices=choices,- qmark="",- style=questionary.Style(- [- ("pointer", f"fg:{THEME_PRIMARY} bold"),- ("highlighted", f"fg:{THEME_PRIMARY} bold"),- ]- ),- ).ask_async()-- if answer is None:- raise KeyboardInterrupt-- label = answer.split(" - ")[0] if " - " in answer else answer- answers[q.question] = label- else:- answer = await questionary.text(- f" > {q.question}",- qmark="",- style=questionary.Style([("question", f"fg:{THEME_SECONDARY}")]),- ).ask_async()- if answer is None:- raise KeyboardInterrupt- answers[q.question] = answer.strip()-- ui.console.print(f" [dim]→ {answers[q.question]}[/dim]")-- except KeyboardInterrupt:- ui.console.print(" [dim]User cancelled question[/dim]")- answers[q.question] = ""-- ui.console.print()-- # Return answers as formatted string+ # Convert pydantic models to dicts for the shared method+ question_dicts = [q.model_dump() for q in params.questions]+ answers = await engineer._ask_user_interactive(question_dicts)return "\n".join(f"{k}: {v}" for k, v in answers.items())return ask_user_question@@ -181,6 +102,7 @@ class CopilotEngineer(BaseEngineer):elif event_type == "session.compaction_complete":self.ui.console.print(" [dim]session compaction complete[/dim]")+ client = Nonetry:ask_user_tool = self._build_ask_user_tool()@@ -205,8 +127,13 @@ class CopilotEngineer(BaseEngineer):await session.send({"prompt": prompt})- # Wait for session to complete- await done_event.wait()+ # Wait for session to complete with timeout protection+ try:+ await asyncio.wait_for(done_event.wait(), timeout=_SESSION_TIMEOUT)+ except TimeoutError:+ self.ui.error(f"Session timed out after {_SESSION_TIMEOUT // 60} minutes")+ self.message_store.save_error("Session timed out")+ return None# Save accumulated thinking textif accumulated_text:@@ -217,7 +144,7 @@ class CopilotEngineer(BaseEngineer):local_path = str(self.local_scripts_dir / self._get_client_filename()) if self.local_scripts_dir else Noneself.ui.success(script_path, local_path)- # GitHub subscription: cost is 0 (included in Copilot subscription)+ # GitHub subscription: cost is $0 (included in Copilot subscription)self.usage_metadata["estimated_cost_usd"] = 0.0# Display usage if available@@ -243,3 +170,11 @@ class CopilotEngineer(BaseEngineer):self.message_store.save_error(str(e))self.ui.console.print("\n[dim]Make sure GitHub Copilot CLI is installed and you are logged in: gh auth login[/dim]")return None++ finally:+ # Always stop the client to avoid resource leaks+ if client is not None:+ try:+ await client.stop()+ except Exception:+ passdiff --git a/src/reverse_api/engineer.py b/src/reverse_api/engineer.pyindex 6ab8374..858331a 100644--- a/src/reverse_api/engineer.py+++ b/src/reverse_api/engineer.py@@ -15,10 +15,7 @@ from claude_agent_sdk import (ToolUseBlock,)-import questionary-from .base_engineer import BaseEngineer-from .tui import THEME_PRIMARY, THEME_SECONDARY# Suppress claude_agent_sdk logslogging.getLogger("claude_agent_sdk").setLevel(logging.WARNING)@@ -35,107 +32,7 @@ class ClaudeEngineer(BaseEngineer):return {"behavior": "allow", "updatedInput": input_params}questions = input_params.get("questions", [])- answers = {}-- # Display header- self.ui.console.print()- self.ui.console.print(f" [{THEME_PRIMARY}]?[/{THEME_PRIMARY}] [bold white]Agent Question[/bold white]")- self.ui.console.print()-- for q in questions:- question_text = q.get("question", "")- header = q.get("header", "")- options = q.get("options", [])- multi_select = q.get("multiSelect", False)-- if not question_text:- continue-- # Show context if header exists- if header:- self.ui.console.print(f" [dim]{header}[/dim]")-- try:- if multi_select:- # Multi-select question- choices = [- f"{opt.get('label', '')} - {opt.get('description', '')}" if opt.get("description") else opt.get("label", "")- for opt in options- ]- if choices:- selected = await questionary.checkbox(- f" > {question_text}",- choices=choices,- qmark="",- style=questionary.Style(- [- ("pointer", f"fg:{THEME_PRIMARY} bold"),- ("highlighted", f"fg:{THEME_PRIMARY} bold"),- ("selected", f"fg:{THEME_PRIMARY}"),- ]- ),- ).ask_async()-- if selected is None:- raise KeyboardInterrupt-- # Extract just the labels (before the " - " separator)- labels = [s.split(" - ")[0] if " - " in s else s for s in selected]- answers[question_text] = ", ".join(labels)- else:- # Text input fallback- answer = await questionary.text(- f" > {question_text}",- qmark="",- style=questionary.Style([("question", f"fg:{THEME_SECONDARY}")]),- ).ask_async()- if answer is None:- raise KeyboardInterrupt- answers[question_text] = answer.strip()-- else:- # Single select question- choices = [- f"{opt.get('label', '')} - {opt.get('description', '')}" if opt.get("description") else opt.get("label", "")- for opt in options- ]- if choices:- answer = await questionary.select(- f" > {question_text}",- choices=choices,- qmark="",- style=questionary.Style(- [- ("pointer", f"fg:{THEME_PRIMARY} bold"),- ("highlighted", f"fg:{THEME_PRIMARY} bold"),- ]- ),- ).ask_async()-- if answer is None:- raise KeyboardInterrupt-- # Extract just the label (before the " - " separator)- label = answer.split(" - ")[0] if " - " in answer else answer- answers[question_text] = label- else:- # Text input fallback- answer = await questionary.text(- f" > {question_text}",- qmark="",- style=questionary.Style([("question", f"fg:{THEME_SECONDARY}")]),- ).ask_async()- if answer is None:- raise KeyboardInterrupt- answers[question_text] = answer.strip()-- self.ui.console.print(f" [dim]→ {answers[question_text]}[/dim]")-- except KeyboardInterrupt:- self.ui.console.print(f" [dim]User cancelled question[/dim]")- answers[question_text] = ""-- self.ui.console.print()+ answers = await self._ask_user_interactive(questions)return {"behavior": "allow",diff --git a/src/reverse_api/pricing.py b/src/reverse_api/pricing.pyindex 6c05219..f3038a8 100644--- a/src/reverse_api/pricing.py+++ b/src/reverse_api/pricing.py@@ -94,25 +94,25 @@ MODEL_PRICING = {},# GPT models (for Copilot SDK - cost is $0 with GitHub subscription)"gpt-5": {- "input": 2.00,- "output": 8.00,+ "input": 0,+ "output": 0,"cache_creation": 0,"cache_read": 0,- "reasoning": 8.00,+ "reasoning": 0,},"gpt-4.1": {- "input": 2.00,- "output": 8.00,+ "input": 0,+ "output": 0,"cache_creation": 0,"cache_read": 0,- "reasoning": 8.00,+ "reasoning": 0,},"gpt-4.1-mini": {- "input": 0.40,- "output": 1.60,+ "input": 0,+ "output": 0,"cache_creation": 0,"cache_read": 0,- "reasoning": 1.60,+ "reasoning": 0,},}