Skip to content

The agent that turns websites into APIs!

Merge branch 'main' into feat/agent-friendly-cli

Kalil Bouzigues8ae02e6142a1
diff --cc src/reverse_api/cli.py
index 4850246,0d97a3f..8cc0a14
--- a/src/reverse_api/cli.py
+++ b/src/reverse_api/cli.py
@@@ -1,9 -1,6 +1,8 @@@
import asyncio
import json
import random
- import re
+import sys
+from contextlib import contextmanager
from pathlib import Path
import click
@@@ -1312,43 -928,9 +995,37 @@@ def manual(prompt, url, reverse_enginee
run_manual_capture(prompt, url, reverse_engineer, model, output_dir)
+ epilog="""\b
+Examples:
+ reverse-api-engineer agent
+ reverse-api-engineer agent -p "capture the jobs api" -u https://example.com --no-interactive
+ reverse-api-engineer agent -p "capture the jobs api" --json | jq
+
+\b
+JSON output schema (--json):
+ {
+ "schema_version": 1,
+ "status": "ok" | "error",
+ "run_id": "<id>" | null,
+ "prompt": "...",
+ "url": "..." | null,
+ "mode": "auto" | "chrome-mcp" | null,
+ "har_path": "/abs/path/recording.har" | null,
+ "script_path": "/abs/path/api_client.py" | null,
+ "usage": { "input_tokens": ..., "output_tokens": ..., "total_cost": ... },
+ "error": "<message>" | null
+ }
+
+\b
+Exit codes:
+ 0 success
+ 1 runtime error (capture or engineering failed)
+ 2 misuse (e.g. --prompt missing under --no-interactive / --json)
+"""
+)
@click.option("--prompt", "-p", default=None, help="Instruction for the autonomous agent.")
@click.option("--url", "-u", default=None, help="Optional starting URL.")
- @click.option(
- "--reverse-engineer/--no-engineer",
- "reverse_engineer",
- default=True,
- help="Run reverse engineering after capture.",
- )
@click.option(
"--model",
"-m",
@@@ -1356,62 -938,13 +1033,65 @@@
default=None,
)
@click.option("--output-dir", "-o", default=None, help="Custom output directory.")
-def agent(prompt, url, model, output_dir):
+ "--no-interactive",
+ is_flag=True,
+ help="Fail fast instead of prompting for missing inputs (intended for scripted/agent usage).",
+)
+ "--json",
+ "as_json",
+ is_flag=True,
+ help="Emit a single JSON result on stdout (logs go to stderr). Implies --no-interactive.",
+)
- def agent(prompt, url, reverse_engineer, model, output_dir, no_interactive, as_json):
- """Run autonomous agent browser session."""
++def agent(prompt, url, model, output_dir, no_interactive, as_json):
+ """Run autonomous agent browser session.
+
+ Agent mode runs an integrated capture + reverse-engineering pipeline; if you
+ only want a HAR recording, use `manual --no-engineer` instead.
+ """
- run_agent_capture(prompt=prompt, url=url, model=model, output_dir=output_dir)
+ no_interactive = no_interactive or as_json
+
+ if no_interactive and not (prompt and prompt.strip()):
+ if as_json:
+ misuse = _build_agent_payload(
+ {},
+ prompt=prompt,
+ url=url,
+ output_dir=output_dir,
+ error="--prompt is required in non-interactive/--json mode",
+ )
+ click.echo(json.dumps(misuse))
+ else:
+ click.echo("error: --prompt is required when --no-interactive is set", err=True)
+ sys.exit(2)
+
+ if not as_json:
- run_agent_capture(prompt=prompt, url=url, reverse_engineer=reverse_engineer, model=model, output_dir=output_dir)
++ run_agent_capture(prompt=prompt, url=url, model=model, output_dir=output_dir)
+ return
+
+ payload: dict
+ with _quiet_consoles_for_json() as real_stdout:
+ try:
+ result = run_agent_capture(
+ prompt=prompt,
+ url=url,
- reverse_engineer=reverse_engineer,
+ model=model,
+ output_dir=output_dir,
+ )
+ payload = _build_agent_payload(result, prompt=prompt, url=url, output_dir=output_dir)
+ except KeyboardInterrupt:
+ payload = _build_agent_payload(
+ {}, prompt=prompt, url=url, output_dir=output_dir, error="interrupted"
+ )
+ except Exception as e:
+ payload = _build_agent_payload(
+ {}, prompt=prompt, url=url, output_dir=output_dir, error=str(e)
+ )
+
+ real_stdout.write(json.dumps(payload) + "\n")
+ real_stdout.flush()
+ sys.exit(0 if payload["status"] == "ok" else 1)
def run_manual_capture(prompt=None, url=None, reverse_engineer=True, model=None, output_dir=None):
@@@ -1801,54 -1201,9 +1356,15 @@@ def run_auto_capture(prompt=None, url=N
import traceback
traceback.print_exc()
- return None
+ return {
+ "run_id": run_id,
+ "mode": mode_label,
+ "script_path": None,
+ "usage": {},
+ "error": str(e),
+ }
- def run_playwright_codegen(run_id: str, prompt: str, output_dir: str | None = None, start_url: str | None = None):
- """Generate Playwright script from recorded actions."""
- actions_path = get_actions_path(run_id, output_dir)
- if not actions_path.exists():
- console.print(" [red]error:[/red] no actions recorded")
- return
-
- from .action_recorder import ActionRecorder
-
- actions = ActionRecorder.load(actions_path)
- action_list = actions.get_actions()
-
- # If no explicit start_url, extract from first navigate action
- if not start_url and action_list:
- if action_list[0].type == "navigate" and action_list[0].url:
- start_url = action_list[0].url
-
- generator = PlaywrightCodeGenerator(action_list, start_url=start_url)
- script = generator.generate()
-
- scripts_dir = get_scripts_dir(run_id, output_dir)
-
- # Handle duplicate file paths
- script_path = scripts_dir / "automation.py"
- if script_path.exists():
- i = 1
- while (scripts_dir / f"automation_{i}.py").exists():
- i += 1
- script_path = scripts_dir / f"automation_{i}.py"
-
- script_path.write_text(script)
-
- # Also write requirements.txt
- (scripts_dir / "requirements.txt").write_text("playwright\n")
-
- console.print(" [dim]>[/dim] [white]codegen complete[/white]")
- console.print(f" [dim]>[/dim] [white]{script_path}[/white]")
- console.print(f" [dim]>[/dim] [dim]run with: uv run python {script_path}[/dim]\n")
-
@main.command()
@click.argument("run_id")