Skip to content

The agent that turns websites into APIs!

refactor: centralize latest run resolution in parser

kalil0321bbe7ba06d754
diff --git a/src/reverse_api/cli.py b/src/reverse_api/cli.py
index 931cb83..7480d52 100644
--- a/src/reverse_api/cli.py
+++ b/src/reverse_api/cli.py
@@ -378,8 +378,13 @@ def repl_loop():
console.print(" [dim] [/dim] <run_id> (to switch context)")
continue
- # Parse tag
- parsed = parse_engineer_prompt(raw_input)
+ # Parse tag with session_manager to resolve latest run centrally
+ parsed = parse_engineer_prompt(raw_input, session_manager)
+
+ # Handle parser errors
+ if parsed["error"]:
+ console.print(f" [red]error:[/red] {parsed['error']}")
+ continue
target_run_id = parsed["run_id"]
is_fresh = parsed["fresh"]
@@ -391,15 +396,8 @@ def repl_loop():
if parsed["is_tag_command"]:
# Explicit @id or @docs command
- # If @docs without @id, use latest run
- if is_docs and not target_run_id:
- latest_runs = session_manager.get_history(limit=1)
- if not latest_runs:
- console.print(" [red]error:[/red] no runs found in history")
- continue
- target_run_id = latest_runs[0]["run_id"]
-
- # Validate that the latest run has a HAR file
+ # Validate HAR file exists for @docs mode
+ if is_docs and target_run_id:
run_data = session_manager.get_run(target_run_id)
if run_data:
paths = run_data.get("paths", {})
@@ -409,7 +407,7 @@ def repl_loop():
har_path = har_dir / "recording.har"
if not har_path.exists():
- console.print(f" [red]error:[/red] latest run ({target_run_id}) has no HAR file")
+ console.print(f" [red]error:[/red] run {target_run_id} has no HAR file")
console.print(" [dim]tip:[/dim] use @id <run_id> @docs to specify a run with captured traffic")
continue
@@ -424,20 +422,13 @@ def repl_loop():
add_instr = user_text if user_text else None
else:
- # Implicit mode
- # Check if input is just a run_id
+ # Implicit mode - parser already resolved latest run
+ # Check if input is just a run_id (switching context)
if session_manager.get_run(user_text):
target_run_id = user_text
# Just switching run, no new instructions
else:
- # Implicit @id on latest run
- latest_runs = session_manager.get_history(limit=1)
- if not latest_runs:
- console.print(" [red]error:[/red] no runs found in history")
- continue
- target_run_id = latest_runs[0]["run_id"]
-
- # Treat input as additive instructions
+ # Parser resolved latest run, treat input as additive instructions
add_instr = user_text
run_engineer(
diff --git a/src/reverse_api/utils.py b/src/reverse_api/utils.py
index 6087ea5..a6215bd 100644
--- a/src/reverse_api/utils.py
+++ b/src/reverse_api/utils.py
@@ -215,16 +215,21 @@ def _slugify(text: str) -> str:
return "_".join(words)[:50]
-def parse_engineer_prompt(input_text: str) -> dict:
+def parse_engineer_prompt(input_text: str, session_manager=None) -> dict:
"""Parse engineer mode input for tags.
+ Args:
+ input_text: The raw input text to parse
+ session_manager: Optional SessionManager to resolve latest run_id when needed
+
Returns:
dict: {
"run_id": str | None,
"fresh": bool,
"docs": bool,
"prompt": str,
- "is_tag_command": bool
+ "is_tag_command": bool,
+ "error": str | None # Error message if validation failed
}
"""
if not input_text:
@@ -234,16 +239,28 @@ def parse_engineer_prompt(input_text: str) -> dict:
"docs": False,
"prompt": "",
"is_tag_command": False,
+ "error": None,
}
# Check for standalone @docs first (no prompt parameter)
if input_text.strip() == "@docs":
+ # Resolve latest run if session_manager provided
+ run_id = None
+ error = None
+ if session_manager:
+ latest_runs = session_manager.get_history(limit=1)
+ if not latest_runs:
+ error = "no runs found in history"
+ else:
+ run_id = latest_runs[0]["run_id"]
+
return {
- "run_id": None, # Will use latest run
+ "run_id": run_id,
"fresh": False,
"docs": True,
"prompt": "",
"is_tag_command": True,
+ "error": error,
}
# Enhanced regex for @id <run_id> [--fresh] [@docs] <prompt>
@@ -265,14 +282,26 @@ def parse_engineer_prompt(input_text: str) -> dict:
"docs": docs,
"prompt": remaining_prompt,
"is_tag_command": True,
+ "error": None,
}
+ # Implicit mode - resolve latest run if session_manager provided
+ run_id = None
+ error = None
+ if session_manager:
+ latest_runs = session_manager.get_history(limit=1)
+ if not latest_runs:
+ error = "no runs found in history"
+ else:
+ run_id = latest_runs[0]["run_id"]
+
return {
- "run_id": None,
+ "run_id": run_id,
"fresh": False,
"docs": False,
"prompt": input_text.strip(),
"is_tag_command": False,
+ "error": error,
}