Skip to content

The agent that turns websites into APIs!

fix: stream script output in real-time instead of capturing

kalil03216d0b3a2beffd
diff --git a/src/reverse_api/cli.py b/src/reverse_api/cli.py
index 135ec33..19dc8c5 100644
--- a/src/reverse_api/cli.py
+++ b/src/reverse_api/cli.py
@@ -2105,8 +2105,6 @@ def run_script(ctx, identifier, script_args, file_name, list_scripts):
if not scripts:
console.print(f"[red]No Python scripts found for run {run_id}[/red]")
- prompt_preview = (run.get("prompt") or "")[:60]
- console.print(f" prompt: {prompt_preview}", style="dim")
raise SystemExit(1)
# --ls: just list and exit
@@ -2167,37 +2165,30 @@ def run_script(ctx, identifier, script_args, file_name, list_scripts):
python_path = str(venv_python)
- # Execute (with retry on missing imports)
- result = subprocess.run(
- [python_path, str(script), *script_args],
- capture_output=True, text=True,
- )
+ # Execute with real-time output
+ cmd = [python_path, str(script), *script_args]
+ result = subprocess.run(cmd)
- if result.returncode != 0 and "ModuleNotFoundError: No module named" in (result.stderr or ""):
- import re as _re
+ # On failure, check if it's a missing import and offer to install
+ if result.returncode != 0:
+ probe = subprocess.run(cmd, capture_output=True, text=True)
+ if "ModuleNotFoundError: No module named" in (probe.stderr or ""):
+ import re as _re
- match = _re.search(r"No module named ['\"]([^'\"]+)['\"]", result.stderr)
- if match:
- missing = match.group(1)
- console.print(f"[yellow]Missing dependency: {missing}[/yellow]")
+ match = _re.search(r"No module named ['\"]([^'\"]+)['\"]", probe.stderr)
+ if match:
+ missing = match.group(1)
+ console.print(f"[yellow]Missing dependency: {missing}[/yellow]")
+
+ install = questionary.confirm(
+ f"Install '{missing}' and retry?", default=True
+ ).ask()
+ if install:
+ subprocess.run([str(venv_pip), "install", "-q", missing], check=True)
+ console.print(f"Installed [green]{missing}[/green]. Retrying...")
+ result = subprocess.run(cmd)
+ raise SystemExit(result.returncode)
- install = questionary.confirm(
- f"Install '{missing}' and retry?", default=True
- ).ask()
- if install:
- subprocess.run([str(venv_pip), "install", "-q", missing], check=True)
- console.print(f"Installed [green]{missing}[/green]. Retrying...")
- result = subprocess.run([python_path, str(script), *script_args])
- raise SystemExit(result.returncode)
-
- sys.stderr.write(result.stderr)
- sys.stdout.write(result.stdout)
- raise SystemExit(result.returncode)
-
- if result.stdout:
- sys.stdout.write(result.stdout)
- if result.stderr:
- sys.stderr.write(result.stderr)
raise SystemExit(result.returncode)
diff --git a/tests/test_run_command.py b/tests/test_run_command.py
index 7cd194e..c05dd82 100644
--- a/tests/test_run_command.py
+++ b/tests/test_run_command.py
@@ -557,17 +557,18 @@ class TestRunCommandImportRetry:
def test_offers_install_on_import_error(self, cli_runner, mock_cli_env):
from reverse_api.cli import main
tmp_path = mock_cli_env[0]
- # Pre-create shared venv so setup is skipped
venv = tmp_path / ".venv"
venv.mkdir()
(venv / "bin").mkdir()
(venv / "bin" / "python").write_text("#!/bin/sh")
(venv / "bin" / "pip").write_text("#!/bin/sh")
- failed = MagicMock(returncode=1, stdout="", stderr="Traceback:\nModuleNotFoundError: No module named 'pandas'")
+ # 1st call: real-time run fails, 2nd: probe with captured stderr, 3rd: pip install, 4th: retry succeeds
+ first_run = MagicMock(returncode=1)
+ probe = MagicMock(returncode=1, stdout="", stderr="ModuleNotFoundError: No module named 'pandas'")
pip_ok = MagicMock(returncode=0, stdout="", stderr="")
- success = MagicMock(returncode=0, stdout="ok", stderr="")
- with patch("subprocess.run", side_effect=[failed, pip_ok, success]):
+ retry_ok = MagicMock(returncode=0)
+ with patch("subprocess.run", side_effect=[first_run, probe, pip_ok, retry_ok]):
with patch("questionary.confirm") as mock_confirm:
mock_confirm.return_value.ask.return_value = True
result = cli_runner.invoke(main, ["run", "def789ghi012"])
@@ -583,8 +584,9 @@ class TestRunCommandImportRetry:
(venv / "bin" / "python").write_text("#!/bin/sh")
(venv / "bin" / "pip").write_text("#!/bin/sh")
- failed = MagicMock(returncode=1, stdout="", stderr="ModuleNotFoundError: No module named 'foo'")
- with patch("subprocess.run", return_value=failed):
+ first_run = MagicMock(returncode=1)
+ probe = MagicMock(returncode=1, stdout="", stderr="ModuleNotFoundError: No module named 'foo'")
+ with patch("subprocess.run", side_effect=[first_run, probe]):
with patch("questionary.confirm") as mock_confirm:
mock_confirm.return_value.ask.return_value = False
result = cli_runner.invoke(main, ["run", "def789ghi012"])
@@ -598,11 +600,11 @@ class TestRunCommandImportRetry:
(venv / "bin").mkdir()
(venv / "bin" / "python").write_text("#!/bin/sh")
- failed = MagicMock(returncode=1, stdout="", stderr="TypeError: something else broke")
- with patch("subprocess.run", return_value=failed):
+ first_run = MagicMock(returncode=1)
+ probe = MagicMock(returncode=1, stdout="", stderr="TypeError: something else broke")
+ with patch("subprocess.run", side_effect=[first_run, probe]):
result = cli_runner.invoke(main, ["run", "def789ghi012"])
assert result.exit_code == 1
- assert "TypeError" in result.output
class TestRunCommandOutputMessages: