The agent that turns websites into APIs!
Non-interactive AskUserQuestion auto-reply and --json-stream (#87)
kalil032152428fd59433
diff --git a/README.md b/README.mdindex 14cda7c..67e4375 100644--- a/README.md+++ b/README.md@@ -86,7 +86,7 @@ Settings live in `~/.reverse-api/config.json` and can be edited via `/settings`"opencode_model": "claude-sonnet-4-6","opencode_provider": "anthropic","copilot_model": "gpt-5",- "cursor_model": "composer-2",+ "cursor_model": "composer-2.5","output_dir": null,"output_language": "python","real_time_sync": true,diff --git a/src/reverse_api/auto_engineer.py b/src/reverse_api/auto_engineer.pyindex 0cf2ba3..818bc35 100644--- a/src/reverse_api/auto_engineer.py+++ b/src/reverse_api/auto_engineer.py@@ -141,7 +141,7 @@ class ClaudeAutoEngineer(ClaudeEngineer):},)questions = input_data.get("questions", [])- answers = await self._ask_user_interactive(questions)+ answers = await self._ask_user_questions(questions)return PermissionResultAllow(updated_input={"questions": questions, "answers": answers},)diff --git a/src/reverse_api/base_engineer.py b/src/reverse_api/base_engineer.pyindex 0f83ae1..c0af4de 100644--- a/src/reverse_api/base_engineer.py+++ b/src/reverse_api/base_engineer.py@@ -18,6 +18,13 @@ DEBUG = os.environ.get("DEBUG", "0") == "1"OTHER_OPTION = "Other (type your answer)"+NON_INTERACTIVE_ASK_USER_MESSAGE = (+ "The user is running the CLI in non-interactive mode and cannot answer. "+ "Assume the best reasonable answer from context and continue. "+ "If you truly cannot proceed without a human choice, stop and instruct the caller "+ "to start a new session with a clearer, more specific prompt."+)+class BaseEngineer(ABC):"""Abstract base class for API reverse engineering implementations."""@@ -72,6 +79,19 @@ class BaseEngineer(ABC):# conversation loop in subclasses ends after the first generation.# Set this from --json / --no-interactive entry points.self.interactive = interactive+ self._json_event_sink: Any = None++ def _emit_json_event(self, event: dict[str, Any]) -> None:+ sink = self._json_event_sink+ if sink:+ sink(event)++ @staticmethod+ def _is_ask_user_tool_name(tool_name: str) -> bool:+ n = tool_name.lower().replace("-", "_")+ if n == "askuserquestion":+ return True+ return "ask" in n and "user" in n and "question" in ndef _handle_cli_stderr(self, line: str) -> None:"""Filter CLI subprocess stderr. Shows full output in DEBUG mode, otherwise shows a single clean error."""@@ -147,6 +167,25 @@ class BaseEngineer(ABC):return self.sync_watcher.get_status()return None+ async def _ask_user_questions(self, questions: list[dict[str, Any]]) -> dict[str, str]:+ """Resolve AskUserQuestion prompts (interactive UI or non-interactive stub)."""+ if not self.interactive:+ answers: dict[str, str] = {}+ count = 0+ for q in questions:+ question_text = q.get("question", "") if isinstance(q, dict) else getattr(q, "question", "")+ if not question_text:+ continue+ answers[question_text] = NON_INTERACTIVE_ASK_USER_MESSAGE+ count += 1+ if count:+ self.ui.console.print(+ f" [dim]AskUserQuestion skipped ({count} question(s); non-interactive mode)[/dim]"+ )+ self._emit_json_event({"event": "ask_user_skipped", "count": count})+ return answers+ return await self._ask_user_interactive(questions)+async def _ask_user_interactive(self, questions: list[dict[str, Any]]) -> dict[str, str]:"""Prompt the user interactively for answers to questions.diff --git a/src/reverse_api/cli.py b/src/reverse_api/cli.pyindex 48686e8..f12c341 100644--- a/src/reverse_api/cli.py+++ b/src/reverse_api/cli.py@@ -64,7 +64,7 @@ def default_model_for_configured_sdk(sdk: str | None = None) -> str:if s == "copilot":return config_manager.get("copilot_model", "gpt-5")if s == "cursor":- return config_manager.get("cursor_model", "composer-2")+ return config_manager.get("cursor_model", "composer-2.5")return config_manager.get("claude_code_model", "claude-sonnet-4-6")@@ -189,6 +189,13 @@ def _quiet_consoles_for_json():c._file = original_inner+def _write_json_stdout(real_stdout, payload: dict, *, json_stream: bool) -> None:+ """Write final CLI JSON payload (single doc or NDJSON terminal event)."""+ out = {"event": "result", **payload} if json_stream else payload+ real_stdout.write(json.dumps(out) + "\n")+ real_stdout.flush()++def _build_dry_run_payload(*,prompt: str | None,@@ -383,7 +390,7 @@ def _build_dry_run_payload("claude": "claude-sonnet-4-6","opencode": "claude-opus-4-6","copilot": "gpt-5",- "cursor": "composer-2",+ "cursor": "composer-2.5",}.get(sdk, "claude-sonnet-4-6")resolved_model = model or config_manager.get(sdk_model_key, sdk_default_model)@@ -1142,11 +1149,11 @@ def handle_settings(mode_color=THEME_PRIMARY):console.print(f" [dim]updated[/dim] copilot model: {new_model}\n")elif action == "cursor_model":- current = config_manager.get("cursor_model", "composer-2")+ current = config_manager.get("cursor_model", "composer-2.5")new_model = questionary.text(" > cursor model",- default=current or "composer-2",- instruction="(e.g., 'composer-2', 'auto' — see Cursor SDK docs)",+ default=current or "composer-2.5",+ instruction="(e.g., 'composer-2.5', 'composer-2' — see Cursor SDK docs)",qmark="",style=questionary.Style([@@ -1514,6 +1521,12 @@ Exit codes:is_flag=True,help="Emit a single JSON result on stdout (logs go to stderr). Implies --no-interactive.",)+ "--json-stream",+ "json_stream",+ is_flag=True,+ help="Emit NDJSON progress events on stdout during the run, then a final {\"event\":\"result\",...} line. Implies --no-interactive.",+)@click.option("--headless",is_flag=True,@@ -1524,7 +1537,7 @@ Exit codes:is_flag=True,help="Validate prompt/url/config/env without launching the browser. Emits a manifest of what would run + check results. Implies --json. Exits 0 if all checks pass, 1 if any error.",)-def agent(prompt, url, model, output_dir, no_interactive, as_json, headless, dry_run):+def agent(prompt, url, model, output_dir, no_interactive, as_json, json_stream, headless, dry_run):"""Run autonomous agent browser session.Agent mode runs an integrated capture + reverse-engineering pipeline; if you@@ -1541,10 +1554,11 @@ def agent(prompt, url, model, output_dir, no_interactive, as_json, headless, dryreal_stdout.flush()sys.exit(0 if payload["status"] == "ok" else 1)- no_interactive = no_interactive or as_json+ no_interactive = no_interactive or as_json or json_stream+ machine_output = as_json or json_streamif no_interactive and not (prompt and prompt.strip()):- if as_json:+ if machine_output:misuse = _build_agent_payload({},prompt=prompt,@@ -1553,6 +1567,8 @@ def agent(prompt, url, model, output_dir, no_interactive, as_json, headless, dryerror="--prompt is required in non-interactive/--json mode",error_kind_hint="misuse",)+ if json_stream:+ misuse = {"event": "result", **misuse}click.echo(json.dumps(misuse))else:click.echo("error: --prompt is required when --no-interactive is set", err=True)@@ -1560,9 +1576,9 @@ def agent(prompt, url, model, output_dir, no_interactive, as_json, headless, dry# Either flag must suppress the post-generation follow-up prompt that would# otherwise block on stdin (`input(" > ")`) inside ClaudeAutoEngineer.- interactive = not (as_json or no_interactive)+ interactive = not no_interactive- if not as_json:+ if not machine_output:run_agent_capture(prompt=prompt,url=url,@@ -1573,8 +1589,15 @@ def agent(prompt, url, model, output_dir, no_interactive, as_json, headless, dry)return+ from .json_stream import make_json_stream_sink+payload: dictwith _quiet_consoles_for_json() as real_stdout:+ sink = (+ make_json_stream_sink(lambda line: (real_stdout.write(line + "\n"), real_stdout.flush()))+ if json_stream+ else None+ )try:result = run_agent_capture(prompt=prompt,@@ -1583,6 +1606,7 @@ def agent(prompt, url, model, output_dir, no_interactive, as_json, headless, dryoutput_dir=output_dir,interactive=interactive,headless=headless,+ json_event_sink=sink,)payload = _build_agent_payload(result, prompt=prompt, url=url, output_dir=output_dir)except KeyboardInterrupt as e:@@ -1596,8 +1620,7 @@ def agent(prompt, url, model, output_dir, no_interactive, as_json, headless, dry{}, prompt=prompt, url=url, output_dir=output_dir, error=e)- real_stdout.write(json.dumps(payload) + "\n")- real_stdout.flush()+ _write_json_stdout(real_stdout, payload, json_stream=json_stream)sys.exit(0 if payload["status"] == "ok" else 1)@@ -1660,7 +1683,15 @@ def run_manual_capture(prompt=None, url=None, reverse_engineer=True, model=None,console.print(f" [dim]>[/dim] [dim]use 'reverse-api-engineer engineer {run_id}' to engineer later[/dim]\n")-def run_agent_capture(prompt=None, url=None, model=None, output_dir=None, interactive=True, headless=False):+def run_agent_capture(+ prompt=None,+ url=None,+ model=None,+ output_dir=None,+ interactive=True,+ headless=False,+ json_event_sink=None,+):"""Shared logic for agent capture mode."""output_dir = output_dir or config_manager.get("output_dir")@@ -1686,6 +1717,7 @@ def run_agent_capture(prompt=None, url=None, model=None, output_dir=None, interaagent_provider=agent_provider,interactive=interactive,headless=headless,+ json_event_sink=json_event_sink,)@@ -1746,6 +1778,7 @@ def run_auto_capture(agent_provider="auto",interactive=True,headless=False,+ json_event_sink=None,):"""Auto mode: LLM-driven browser automation + real-time reverse engineering."""output_dir = output_dir or config_manager.get("output_dir")@@ -1871,7 +1904,7 @@ def run_auto_capture(output_language=output_language,interactive=interactive,headless=headless,- cursor_model=model or config_manager.get("cursor_model", "composer-2"),+ cursor_model=model or config_manager.get("cursor_model", "composer-2.5"),cursor_web_search=bool(config_manager.get("cursor_web_search", True)),cursor_setting_sources=_cursor_src,)@@ -1891,6 +1924,17 @@ def run_auto_capture(headless=headless,)+ if json_event_sink is not None:+ from .json_stream import attach_json_stream_to_engineer++ attach_json_stream_to_engineer(+ engineer,+ json_event_sink,+ mode=mode_label,+ url=url,+ command="agent",+ )+# Start sync before analysisengineer.start_sync()@@ -1997,14 +2041,21 @@ Exit codes:is_flag=True,help="Emit a single JSON result on stdout (logs go to stderr). Implies --no-interactive.",)-def engineer(run_id, prompt, fresh, model, output_dir, no_interactive, as_json):+ "--json-stream",+ "json_stream",+ is_flag=True,+ help="Emit NDJSON progress events on stdout during the run, then a final {\"event\":\"result\",...} line. Implies --no-interactive.",+)+def engineer(run_id, prompt, fresh, model, output_dir, no_interactive, as_json, json_stream):"""Run reverse engineering on a previous run."""# `run_id` is declared optional at the click level so that wrappers using# --json get a JSON misuse payload instead of Click's plain-text "missing# argument" error. We re-validate inline to preserve the same exit-2 UX# for non-JSON invocations.if not run_id:- if as_json:+ machine_output = as_json or json_stream+ if machine_output:misuse = _build_engineer_payload(None,run_id="",@@ -2013,7 +2064,11 @@ def engineer(run_id, prompt, fresh, model, output_dir, no_interactive, as_json):error="RUN_ID is required",error_kind_hint="misuse",)- click.echo(json.dumps(misuse))+ if json_stream:+ with _quiet_consoles_for_json() as real_stdout:+ _write_json_stdout(real_stdout, misuse, json_stream=True)+ else:+ click.echo(json.dumps(misuse))else:click.echo("Usage: reverse-api-engineer engineer [OPTIONS] RUN_ID", err=True)click.echo("\nError: Missing argument 'RUN_ID'.", err=True)@@ -2025,9 +2080,11 @@ def engineer(run_id, prompt, fresh, model, output_dir, no_interactive, as_json):additional = prompt if (prompt and not fresh) else None# Either flag must suppress the post-generation follow-up prompt that# would otherwise block on stdin (`input(" > ")`) inside ClaudeEngineer.- interactive = not (as_json or no_interactive)+ no_interactive = no_interactive or as_json or json_stream+ interactive = not no_interactive+ machine_output = as_json or json_stream- if not as_json:+ if not machine_output:run_engineer(run_id,prompt=main_prompt,@@ -2039,8 +2096,15 @@ def engineer(run_id, prompt, fresh, model, output_dir, no_interactive, as_json):)return+ from .json_stream import make_json_stream_sink+payload: dictwith _quiet_consoles_for_json() as real_stdout:+ sink = (+ make_json_stream_sink(lambda line: (real_stdout.write(line + "\n"), real_stdout.flush()))+ if json_stream+ else None+ )try:result = run_engineer(run_id,@@ -2050,6 +2114,7 @@ def engineer(run_id, prompt, fresh, model, output_dir, no_interactive, as_json):output_dir=output_dir,is_fresh=fresh,interactive=interactive,+ json_event_sink=sink,)payload = _build_engineer_payload(result, run_id=run_id, prompt=prompt, fresh=fresh)except KeyboardInterrupt as e:@@ -2062,8 +2127,7 @@ def engineer(run_id, prompt, fresh, model, output_dir, no_interactive, as_json):None, run_id=run_id, prompt=prompt, fresh=fresh, error=e)- real_stdout.write(json.dumps(payload) + "\n")- real_stdout.flush()+ _write_json_stdout(real_stdout, payload, json_stream=json_stream)sys.exit(0 if payload["status"] == "ok" else 1)@@ -2077,6 +2141,7 @@ def run_engineer(is_fresh=False,output_mode="client",interactive=True,+ json_event_sink=None,):"""Shared logic for reverse engineering."""if not har_path or not prompt:@@ -2119,6 +2184,7 @@ def run_engineer(output_language=output_language,output_mode=output_mode,interactive=interactive,+ json_event_sink=json_event_sink,)elif sdk == "copilot":result = run_reverse_engineering(@@ -2135,11 +2201,12 @@ def run_engineer(output_language=output_language,output_mode=output_mode,interactive=interactive,+ json_event_sink=json_event_sink,)elif sdk == "cursor":_cs = config_manager.get("cursor_setting_sources")_cursor_src = _cs if isinstance(_cs, list) and all(isinstance(x, str) for x in _cs) else None- _cm = model or config_manager.get("cursor_model", "composer-2")+ _cm = model or config_manager.get("cursor_model", "composer-2.5")result = run_reverse_engineering(run_id=run_id,har_path=har_path,@@ -2156,6 +2223,7 @@ def run_engineer(output_language=output_language,output_mode=output_mode,interactive=interactive,+ json_event_sink=json_event_sink,)else:result = run_reverse_engineering(@@ -2171,6 +2239,7 @@ def run_engineer(output_language=output_language,output_mode=output_mode,interactive=interactive,+ json_event_sink=json_event_sink,)if result:diff --git a/src/reverse_api/config.py b/src/reverse_api/config.pyindex aed033c..7740f70 100644--- a/src/reverse_api/config.py+++ b/src/reverse_api/config.py@@ -10,7 +10,7 @@ DEFAULT_CONFIG = {"agent_browser_npx_package": "agent-browser@0","claude_code_model": "claude-sonnet-4-6","collector_model": "claude-sonnet-4-6", # Model for collector mode- "cursor_model": "composer-2", # Model id for Cursor SDK (see Cursor.models.list())+ "cursor_model": "composer-2.5", # Model id for Cursor SDK (see Cursor.models.list())# When True, local agents load broader Cursor setting layers (plugins/team) so WebFetch/WebSearch# and other IDE tools match Cursor desktop behavior. Set False for minimal "project+user" only."cursor_web_search": True,diff --git a/src/reverse_api/copilot_engineer.py b/src/reverse_api/copilot_engineer.pyindex 4c9dc90..6eb7756 100644--- a/src/reverse_api/copilot_engineer.py+++ b/src/reverse_api/copilot_engineer.py@@ -54,7 +54,7 @@ class CopilotEngineer(BaseEngineer):async def ask_user_question(params: AskUserParams) -> str:# Convert pydantic models to dicts for the shared methodquestion_dicts = [q.model_dump() for q in params.questions]- answers = await engineer._ask_user_interactive(question_dicts)+ answers = await engineer._ask_user_questions(question_dicts)return "\n".join(f"{k}: {v}" for k, v in answers.items())return ask_user_questiondiff --git a/src/reverse_api/cursor_bridge/run.mjs b/src/reverse_api/cursor_bridge/run.mjsindex 74865db..769f56c 100644--- a/src/reverse_api/cursor_bridge/run.mjs+++ b/src/reverse_api/cursor_bridge/run.mjs@@ -33,6 +33,7 @@ function summarizeEvent(ev) {type: t,name: ev.name,status: ev.status,+ callId: ev.call_id ?? ev.callId,args: ev.args,result: ev.status !== "running" ? ev.result : undefined,};@@ -64,7 +65,7 @@ if (!cwd || typeof cwd !== "string") {process.exit(1);}-const modelId = input.modelId || "composer-2";+const modelId = input.modelId || "composer-2.5";const mcpServers = input.mcpServers && typeof input.mcpServers === "object" ? input.mcpServers : undefined;const resumeAgentId = input.resumeAgentId || null;const prompt = input.prompt;diff --git a/src/reverse_api/cursor_engineer.py b/src/reverse_api/cursor_engineer.pyindex d7483ea..78a6d80 100644--- a/src/reverse_api/cursor_engineer.py+++ b/src/reverse_api/cursor_engineer.py@@ -74,7 +74,7 @@ class CursorEngineer(BaseEngineer):cursor_setting_sources: list[str] | None = None,**kwargs: Any,):- cm = cursor_model or model or "composer-2"+ cm = cursor_model or model or "composer-2.5"super().__init__(run_id=run_id, har_path=har_path, prompt=prompt, model=cm, **kwargs)self.cursor_model = cmself.cursor_web_search = cursor_web_search@@ -83,6 +83,10 @@ class CursorEngineer(BaseEngineer):self.ui = CursorStreamUI(self, verbose=vb)self._cursor_thinking_acc = ""self._cursor_assistant_acc = ""+ # Cursor streams several `tool_call`/running deltas per call as the args+ # fill in; track which call_ids we've already announced so the UI and+ # json-stream emit exactly one tool_start per logical call.+ self._cursor_started_calls: set[str] = set()@staticmethoddef _cursor_coerce_args(raw: Any) -> dict[str, Any]:@@ -118,6 +122,10 @@ class CursorEngineer(BaseEngineer):def _cursor_reset_stream_buffers(self) -> None:self._cursor_thinking_acc = ""self._cursor_assistant_acc = ""+ # Each turn is a fresh agent stream; clear announced call IDs so a turn+ # interrupted before a tool's terminal event can't leave stale IDs that+ # suppress tool_start on a later turn (and so the set can't grow forever).+ self._cursor_started_calls.clear()def _cursor_feed_thinking(self, fragment: str) -> None:self._cursor_thinking_acc += fragment@@ -162,18 +170,33 @@ class CursorEngineer(BaseEngineer):elif et == "tool_call":name = str(event.get("name") or "tool")status = event.get("status")+ call_id = event.get("callId") or event.get("call_id")if status == "running":- self._cursor_flush_narrative()args = self._cursor_coerce_args(event.get("args"))+ # Live todo board reflects the latest snapshot on every delta;+ # this is a TUI-only render and never reaches the json-stream.self._cursor_emit_todo_ui(name, args)- self.ui.tool_start(name, args)+ # Announce the tool exactly once, on the first running delta.+ if call_id and call_id in self._cursor_started_calls:+ return+ if call_id:+ self._cursor_started_calls.add(call_id)+ self._cursor_flush_narrative()+ if not self.interactive and self._is_ask_user_tool_name(name):+ self._emit_json_event({"event": "ask_user_skipped", "count": 1, "tool": name})+ self.ui.console.print(+ f" [dim]AskUserQuestion skipped ({name}; non-interactive mode)[/dim]"+ )+ self.ui.tool_start(name, args, call_id=call_id)self.message_store.save_tool_start(name, args)else:is_err = status == "error"res = event.get("result")out = str(res) if res is not None else None- self.ui.tool_result(name, is_err, out)+ self.ui.tool_result(name, is_err, out, call_id=call_id)self.message_store.save_tool_result(name, is_err, out)+ if call_id:+ self._cursor_started_calls.discard(call_id)async def _one_turn( # noqa: C901 - subprocess + NDJSON; splitting obscures control flowself,diff --git a/src/reverse_api/engineer.py b/src/reverse_api/engineer.pyindex 2ad5dde..f936e7c 100644--- a/src/reverse_api/engineer.py+++ b/src/reverse_api/engineer.py@@ -31,7 +31,7 @@ class ClaudeEngineer(BaseEngineer):"""Handle tool permission requests, with interactive UI for AskUserQuestion."""if tool_name == "AskUserQuestion":questions = input_data.get("questions", [])- answers = await self._ask_user_interactive(questions)+ answers = await self._ask_user_questions(questions)return PermissionResultAllow(updated_input={"questions": questions, "answers": answers},)@@ -218,6 +218,7 @@ def run_reverse_engineering(output_language: str = "python",output_mode: str = "client",interactive: bool = True,+ json_event_sink: Any = None,) -> dict[str, Any] | None:"""Run reverse engineering with the specified SDK.@@ -226,7 +227,7 @@ def run_reverse_engineering(opencode_provider: Provider ID for OpenCode (e.g., "anthropic")opencode_model: Model ID for OpenCode (e.g., "claude-sonnet-4-6")copilot_model: Model ID for Copilot (e.g., "gpt-5")- cursor_model: Model id for Cursor SDK (e.g., "composer-2")+ cursor_model: Model id for Cursor SDK (e.g., "composer-2.5")cursor_web_search: When True, load extra Cursor setting layers so WebFetch/WebSearch and plugins apply.cursor_setting_sources: Optional explicit list (overrides cursor_web_search), e.g. ["project","user","all"].enable_sync: Enable real-time file syncing during engineering@@ -311,6 +312,11 @@ def run_reverse_engineering(interactive=interactive,)+ if json_event_sink is not None:+ from .json_stream import attach_json_stream_to_engineer++ attach_json_stream_to_engineer(engineer, json_event_sink)+# Start sync before analysisengineer.start_sync()diff --git a/src/reverse_api/json_stream.py b/src/reverse_api/json_stream.pynew file mode 100644index 0000000..76c82ea--- /dev/null+++ b/src/reverse_api/json_stream.py@@ -0,0 +1,124 @@+"""NDJSON event streaming for scripted CLI invocations (--json-stream)."""++from __future__ import annotations++import inspect+from collections.abc import Callable+from typing import Any+++def make_json_stream_sink(write_line: Callable[[str], None]) -> Callable[[dict[str, Any]], None]:+ """Build a sink that writes one JSON object per line."""++ import json++ def sink(event: dict[str, Any]) -> None:+ write_line(json.dumps(event, default=str))++ return sink+++class StreamingUIWrapper:+ """Delegate to an inner UI and emit json-stream events for key lifecycle hooks."""++ def __init__(self, inner: Any, sink: Callable[[dict[str, Any]], None]) -> None:+ self._inner = inner+ self._sink = sink++ def __getattr__(self, name: str) -> Any:+ return getattr(self._inner, name)++ def header(self, run_id: str, prompt: str, model: str | None = None, sdk: str | None = None, mode: str | None = None) -> None:+ self._sink(+ {+ "event": "header",+ "run_id": run_id,+ "prompt": prompt,+ "model": model,+ "sdk": sdk,+ "mode": mode,+ }+ )+ self._inner.header(run_id, prompt, model=model, sdk=sdk, mode=mode)++ def start_analysis(self) -> None:+ self._sink({"event": "progress", "message": "analysis_started"})+ self._inner.start_analysis()++ def tool_start(+ self, tool_name: str, tool_input: dict | Any = None, call_id: str | None = None+ ) -> None:+ event: dict[str, Any] = {"event": "tool_start", "name": tool_name}+ if call_id:+ event["call_id"] = call_id+ # Emit the real structured input (json.dumps serializes it); a stringified+ # repr would not be parseable as JSON by downstream consumers.+ if tool_input is not None:+ event["input"] = tool_input+ self._sink(event)+ # Inner UIs render only; not all accept call_id, so don't forward it.+ self._inner.tool_start(tool_name, tool_input)++ def tool_result(+ self,+ tool_name: str,+ is_error: bool = False,+ output: str | None = None,+ call_id: str | None = None,+ ) -> None:+ event: dict[str, Any] = {+ "event": "tool_end",+ "name": tool_name,+ "is_error": is_error,+ "output_preview": (output[:200] if output else None),+ }+ if call_id:+ event["call_id"] = call_id+ self._sink(event)+ self._inner.tool_result(tool_name, is_error, output)++ def thinking(self, text: str, max_length: int = 500) -> None:+ preview = text[:500] if text else ""+ if preview.strip():+ self._sink({"event": "thinking", "text": preview})+ if hasattr(self._inner, "thinking"):+ sig = inspect.signature(self._inner.thinking)+ if "max_length" in sig.parameters:+ self._inner.thinking(text, max_length=max_length)+ else:+ self._inner.thinking(text)+ else:+ self._inner.thinking(text)++ def thinking_block(self, text: str, max_chars: int = 8000) -> None:+ preview = text[:800] if text else ""+ if preview.strip():+ self._sink({"event": "thinking_block", "text": preview})+ self._inner.thinking_block(text, max_chars=max_chars)++ def success(self, script_path: str, local_path: str | None = None) -> None:+ self._sink({"event": "success", "script_path": script_path, "local_path": local_path})+ self._inner.success(script_path, local_path)++ def error(self, message: str) -> None:+ self._sink({"event": "error", "message": message})+ self._inner.error(message)+++def attach_json_stream_to_engineer(+ engineer: Any,+ sink: Callable[[dict[str, Any]], None],+ **run_started_extra: Any,+) -> None:+ """Enable NDJSON UI events on an engineer instance."""+ engineer._json_event_sink = sink+ engineer.ui = StreamingUIWrapper(engineer.ui, sink)+ started: dict[str, Any] = {+ "event": "run_started",+ "run_id": engineer.run_id,+ "sdk": getattr(engineer, "sdk", None),+ }+ for key, value in run_started_extra.items():+ if value is not None:+ started[key] = value+ sink(started)diff --git a/src/reverse_api/prompts/engineer/system.md b/src/reverse_api/prompts/engineer/system.mdindex 6c3a82b..ce4a600 100644--- a/src/reverse_api/prompts/engineer/system.md+++ b/src/reverse_api/prompts/engineer/system.md@@ -2,7 +2,7 @@ You are tasked with analyzing a HAR (HTTP Archive) file to {mode_description} th**Core principle:** The generated output must work immediately with zero user effort. Hardcode all credentials, cookies, tokens, and session data found in the traffic. No env vars, no config files, no manual setup. If the traffic reveals a token refresh or login flow, implement automatic re-authentication so the script doesn't go stale when cookies or tokens expire.-You have access to the AskUserQuestion tool to ask clarifying questions during your analysis. Use it when you need to clarify requirements, prioritize features, or choose between approaches. It supports single-select, multi-select, and free-form questions.+You have access to the AskUserQuestion tool to ask clarifying questions during your analysis when running interactively. Use it when you need to clarify requirements, prioritize features, or choose between approaches. It supports single-select, multi-select, and free-form questions. In non-interactive or scripted CLI runs, do not use AskUserQuestion — make reasonable assumptions from the HAR and prompt instead.## Analysis guidelines@@ -11,7 +11,7 @@ These are guidelines for your analysis, not a rigid checklist — use your judgm1. **Read the HAR file** — understand the API surface: endpoints, methods, headers, request/response shapes, status codes2. **Identify auth patterns** — cookies, Bearer tokens, API keys, CSRF tokens, session tokens. Hardcode whatever you find3. **Extract endpoint patterns** — required vs optional params, data formats, query vs body params-4. **Ask the user** if anything is ambiguous (which auth to prioritize, feature priorities, implementation approach)+4. **Ask the user** if anything is ambiguous and you are in an interactive session; otherwise document assumptions in your summary{codegen_instructions}diff --git a/src/reverse_api/tui.py b/src/reverse_api/tui.pyindex d9c14a4..a540226 100644--- a/src/reverse_api/tui.py+++ b/src/reverse_api/tui.py@@ -78,8 +78,9 @@ class ClaudeUI:self.console.print(" [dim]decoding starting...[/dim]")self.console.print()- def tool_start(self, tool_name: str, tool_input: dict | Any) -> None:+ def tool_start(self, tool_name: str, tool_input: dict | Any, call_id: str | None = None) -> None:"""Display when a tool starts execution."""+ _ = call_id # accepted for parity with json-stream; not renderedself._tool_count += 1self._tools_used.append(tool_name)@@ -92,8 +93,9 @@ class ClaudeUI:if input_summary:self.console.print(f" {input_summary}")- def tool_result(self, tool_name: str, is_error: bool = False, output: str | None = None) -> None:+ def tool_result(self, tool_name: str, is_error: bool = False, output: str | None = None, call_id: str | None = None) -> None:"""Display when a tool completes."""+ _ = call_id # accepted for parity with json-stream; not renderedtl = tool_name.lower()if is_error:self.console.print(f" [dim]![/dim] [red]{self._tool_header_label(tool_name)} failed[/red]")diff --git a/tests/test_cli_json_stream.py b/tests/test_cli_json_stream.pynew file mode 100644index 0000000..e85ae14--- /dev/null+++ b/tests/test_cli_json_stream.py@@ -0,0 +1,179 @@+"""Tests for --json-stream NDJSON CLI output."""++import asyncio+import json+from unittest.mock import patch++from click.testing import CliRunner++from reverse_api.cli import agent, engineer, main+from reverse_api.json_stream import StreamingUIWrapper+++class TestJsonStreamAgent:+ def test_agent_json_stream_emits_ndjson_and_result(self):+ runner = CliRunner()+ fake_result = {+ "run_id": "abc123",+ "mode": "auto",+ "script_path": "/tmp/api_client.py",+ "usage": {},+ }++ def fake_capture(**kwargs):+ sink = kwargs.get("json_event_sink")+ if sink:+ sink({"event": "run_started", "run_id": "abc123", "sdk": "claude"})+ return fake_result++ with patch("reverse_api.cli.run_agent_capture", side_effect=fake_capture):+ result = runner.invoke(+ agent,+ ["--json-stream", "-p", "capture api", "-u", "https://example.com"],+ )++ assert result.exit_code == 0+ lines = [ln for ln in result.output.strip().split("\n") if ln]+ assert len(lines) >= 2+ events = [json.loads(ln) for ln in lines]+ assert events[0]["event"] == "run_started"+ assert events[-1]["event"] == "result"+ assert events[-1]["status"] == "ok"+ assert events[-1]["run_id"] == "abc123"++ def test_agent_json_stream_implies_no_interactive(self):+ runner = CliRunner()+ with patch("reverse_api.cli.run_agent_capture") as mock_run:+ mock_run.return_value = {"run_id": "x", "mode": "auto", "usage": {}}+ runner.invoke(agent, ["--json-stream", "-p", "x"])+ assert mock_run.call_args.kwargs["interactive"] is False+++class TestJsonStreamEngineer:+ def test_engineer_json_stream_emits_result_event(self):+ runner = CliRunner()+ def fake_engineer(run_id, **kwargs):+ sink = kwargs.get("json_event_sink")+ if sink:+ sink({"event": "run_started", "run_id": run_id, "sdk": "claude"})+ return {"script_path": "/tmp/c.py", "usage": {}}++ with patch("reverse_api.cli.run_engineer", side_effect=fake_engineer):+ result = runner.invoke(engineer, ["abc123", "--json-stream", "-p", "add tests"])++ assert result.exit_code == 0+ lines = [ln for ln in result.output.strip().split("\n") if ln]+ events = [json.loads(ln) for ln in lines]+ assert events[-1]["event"] == "result"+ assert events[-1]["run_id"] == "abc123"++ def test_help_lists_json_stream(self):+ runner = CliRunner()+ result = runner.invoke(main, ["agent", "--help"])+ assert "--json-stream" in result.output++class TestJsonStreamContract:+ """The event payloads must stay machine-parseable."""++ def _wrapper(self):+ events: list[dict] = []++ class _Inner:+ def tool_start(self, *a, **k):+ pass++ def tool_result(self, *a, **k):+ pass++ return StreamingUIWrapper(_Inner(), events.append), events++ def test_tool_start_input_is_structured_json_not_repr(self):+ wrapper, events = self._wrapper()+ payload = {"todos": [{"content": "x", "status": "pending"}], "flag": False}+ wrapper.tool_start("updateTodos", payload, call_id="C1")++ # The event survives a JSON round-trip with input as a nested object,+ # not a str(dict) repr (which would carry single quotes / Python False).+ roundtripped = json.loads(json.dumps(events[0]))+ assert roundtripped["input"] == payload+ assert isinstance(roundtripped["input"], dict)+ assert roundtripped["call_id"] == "C1"++ def test_tool_start_and_end_share_call_id(self):+ wrapper, events = self._wrapper()+ wrapper.tool_start("shell", {"command": "ls"}, call_id="abc")+ wrapper.tool_result("shell", False, "ok", call_id="abc")+ assert events[0]["event"] == "tool_start" and events[0]["call_id"] == "abc"+ assert events[1]["event"] == "tool_end" and events[1]["call_id"] == "abc"++ def test_cursor_collapses_running_deltas_to_one_tool_start(self):+ from reverse_api.cursor_engineer import CursorEngineer++ eng = CursorEngineer.__new__(CursorEngineer)+ eng._cursor_started_calls = set()+ eng.interactive = False+ eng._json_event_sink = None+ eng._cursor_thinking_acc = ""+ eng._cursor_assistant_acc = ""+ captured: list[tuple] = []++ class _UI:+ verbose = False++ class console:+ @staticmethod+ def print(*a, **k):+ pass++ def todo_updated(self, todos):+ pass++ def tool_start(self, name, args, call_id=None):+ captured.append(("start", call_id))++ def tool_result(self, name, is_err, out, call_id=None):+ captured.append(("end", call_id))++ class _Store:+ def save_tool_start(self, *a):+ pass++ def save_tool_result(self, *a):+ pass++ def save_todos(self, *a):+ pass++ eng.ui = _UI()+ eng.message_store = _Store()++ async def run():+ for n in (1, 2, 3): # growing args, same call+ await eng._dispatch_stream_event(+ {+ "type": "tool_call",+ "name": "updateTodos",+ "status": "running",+ "callId": "T1",+ "args": {"todos": [{"content": str(i)} for i in range(n)]},+ }+ )+ await eng._dispatch_stream_event(+ {"type": "tool_call", "name": "updateTodos", "status": "completed", "callId": "T1", "result": "ok"}+ )++ asyncio.run(run())+ assert [c[0] for c in captured] == ["start", "end"]+ assert eng._cursor_started_calls == set() # cleaned up on terminal event+++class TestJsonStreamEngineerMisuse:+ def test_engineer_json_stream_missing_run_id_emits_result_event(self):+ runner = CliRunner()+ result = runner.invoke(engineer, ["--json-stream"])+ assert result.exit_code == 2+ payload = json.loads(result.output.strip())+ assert payload["event"] == "result"+ assert payload["status"] == "error"+ assert payload["error_kind"] == "misuse"+ assert "RUN_ID" in payload["error"]diff --git a/tests/test_config.py b/tests/test_config.pyindex 6a9c6e8..046591b 100644--- a/tests/test_config.py+++ b/tests/test_config.py@@ -166,6 +166,6 @@ class TestDefaultConfig:assert DEFAULT_CONFIG["output_dir"] is Noneassert DEFAULT_CONFIG["output_language"] == "python"assert DEFAULT_CONFIG["real_time_sync"] is True- assert DEFAULT_CONFIG["cursor_model"] == "composer-2"+ assert DEFAULT_CONFIG["cursor_model"] == "composer-2.5"assert DEFAULT_CONFIG["cursor_web_search"] is Trueassert DEFAULT_CONFIG["cursor_setting_sources"] is Nonediff --git a/tests/test_cursor_engineer.py b/tests/test_cursor_engineer.pyindex c1bfb31..1a67e35 100644--- a/tests/test_cursor_engineer.py+++ b/tests/test_cursor_engineer.py@@ -127,3 +127,20 @@ def test_cursor_stream_buffers_merge_assistant(tmp_path: Path) -> None:eng._cursor_feed_thinking(" t1")eng._cursor_feed_thinking(" t2")assert eng._cursor_thinking_acc == " t1 t2"+++def test_cursor_reset_clears_started_calls(har_path: Path) -> None:+ """_cursor_started_calls is cleared each turn so stale ids can't suppress tool_start."""+ with patch.dict("os.environ", {"CURSOR_API_KEY": "x"}):+ with patch("reverse_api.cursor_engineer._ensure_cursor_bridge_deps", return_value=None):+ eng = CursorEngineer(+ run_id="r4",+ har_path=har_path,+ prompt="p",+ sdk="cursor",+ interactive=False,+ verbose=False,+ )+ eng._cursor_started_calls.add("stale-call-id")+ eng._cursor_reset_stream_buffers()+ assert eng._cursor_started_calls == set()diff --git a/tests/test_engineer.py b/tests/test_engineer.pyindex 06b9bbb..1f4cb27 100644--- a/tests/test_engineer.py+++ b/tests/test_engineer.py@@ -402,6 +402,51 @@ class TestHandleToolPermission:)assert result.updated_input["answers"]["What?"] == "user answer"+ @pytest.mark.asyncio+ async def test_ask_user_question_non_interactive_stub(self, tmp_path):+ """AskUserQuestion returns a fixed message when interactive=False."""+ from reverse_api.base_engineer import NON_INTERACTIVE_ASK_USER_MESSAGE++ eng = self._make_engineer(tmp_path)+ eng.interactive = False++ with patch("reverse_api.base_engineer.questionary.text") as mock_text:+ result = await eng._handle_tool_permission(+ "AskUserQuestion",+ {"questions": [{"question": "What?", "header": "", "multiSelect": False, "options": []}]},+ None,+ )+ mock_text.assert_not_called()+ assert result.updated_input["answers"]["What?"] == NON_INTERACTIVE_ASK_USER_MESSAGE+++class TestAskUserQuestions:+ """Test _ask_user_questions routing."""++ def _make_engineer(self, tmp_path, *, interactive: bool = True):+ har_path = tmp_path / "test.har"+ har_path.touch()+ with patch("reverse_api.base_engineer.get_scripts_dir", return_value=tmp_path / "scripts"):+ with patch("reverse_api.base_engineer.MessageStore"):+ eng = ClaudeEngineer(+ run_id="test123",+ har_path=har_path,+ prompt="test prompt",+ output_dir=str(tmp_path),+ interactive=interactive,+ )+ return eng++ @pytest.mark.asyncio+ async def test_non_interactive_no_questionary(self, tmp_path):+ from reverse_api.base_engineer import NON_INTERACTIVE_ASK_USER_MESSAGE++ eng = self._make_engineer(tmp_path, interactive=False)+ answers = await eng._ask_user_questions(+ [{"question": "Pick auth?", "header": "", "multiSelect": False, "options": []}]+ )+ assert answers["Pick auth?"] == NON_INTERACTIVE_ASK_USER_MESSAGE+class TestClaudeEngineerAnalyzeAndGenerate:"""Test analyze_and_generate method."""diff --git a/website/content/docs/cli/scripted-usage.mdx b/website/content/docs/cli/scripted-usage.mdxindex 79b4838..87c857c 100644--- a/website/content/docs/cli/scripted-usage.mdx+++ b/website/content/docs/cli/scripted-usage.mdx@@ -14,6 +14,12 @@ When `--json` is set:- **stderr** receives Rich logs and progress updates.- The process exits with a **stable exit code**.+When `--json-stream` is set (on `agent` and `engineer`):++- **stdout** receives **NDJSON**: one JSON object per line while the run progresses, then a final `{"event":"result",...}` line with the same fields as `--json`.+- **stderr** receives Rich logs (same as `--json`).+- Implies **non-interactive** mode; `AskUserQuestion` is auto-answered with a fixed message instead of prompting on stdin.+## Examples### Run an autonomous capture and read the result as JSON@@ -25,6 +31,17 @@ reverse-api-engineer agent \--json | jq```+### Stream progress as NDJSON (wrapper agents)++```bash+reverse-api-engineer agent \+ --prompt "capture the public jobs api" \+ --url https://example.com/jobs \+ --json-stream+```++Common event types: `run_started` (includes `run_id`, `sdk`; agent runs also include `mode`, `url`, `command`), `tool_start`, `tool_end`, `thinking`, `ask_user_skipped`, `result`.+### List and inspect runs```bashdiff --git a/website/content/docs/configuration/models.mdx b/website/content/docs/configuration/models.mdxindex 173f917..2c4514f 100644--- a/website/content/docs/configuration/models.mdx+++ b/website/content/docs/configuration/models.mdx@@ -42,7 +42,7 @@ The full default config:"agent_provider": "auto","claude_code_model": "claude-sonnet-4-6","collector_model": "claude-sonnet-4-6",- "cursor_model": "composer-2",+ "cursor_model": "composer-2.5","cursor_web_search": true,"cursor_setting_sources": null,"copilot_model": "gpt-5",@@ -64,5 +64,5 @@ for the available model IDs. Set them via the `opencode_model` and## Copilot and Cursor usersCopilot uses `copilot_model` (default `gpt-5`). Cursor uses `cursor_model`-(default `composer-2`) plus `cursor_web_search` / `cursor_setting_sources`+(default `composer-2.5`) plus `cursor_web_search` / `cursor_setting_sources`for controlling which Cursor settings layers are loaded.diff --git a/website/content/docs/configuration/sdks.mdx b/website/content/docs/configuration/sdks.mdxindex e1b4f78..2b13489 100644--- a/website/content/docs/configuration/sdks.mdx+++ b/website/content/docs/configuration/sdks.mdx@@ -55,7 +55,7 @@ so WebFetch/WebSearch and configured tools match your desktop setup.```json{"sdk": "cursor",- "cursor_model": "composer-2",+ "cursor_model": "composer-2.5","cursor_web_search": true,"cursor_setting_sources": null}