Skip to content

The agent that turns websites into APIs!

feat: add realtime sync

kalil032188d4e020f38e
diff --git a/pyproject.toml b/pyproject.toml
index d68bd9c..4a22fc5 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -31,6 +31,7 @@ dependencies = [
"anthropic>=0.40.0",
"brotli>=1.2.0",
"aiohttp>=3.12.15",
+ "watchdog>=3.0.0",
]
[project.optional-dependencies]
diff --git a/src/reverse_api/base_engineer.py b/src/reverse_api/base_engineer.py
index e72c19e..2e5063b 100644
--- a/src/reverse_api/base_engineer.py
+++ b/src/reverse_api/base_engineer.py
@@ -4,9 +4,10 @@ from abc import ABC, abstractmethod
from pathlib import Path
from typing import Optional, Dict, Any
-from .utils import get_scripts_dir, get_timestamp
+from .utils import get_scripts_dir, get_timestamp, generate_folder_name
from .tui import ClaudeUI
from .messages import MessageStore
+from .sync import FileSyncWatcher
class BaseEngineer(ABC):
@@ -21,6 +22,8 @@ class BaseEngineer(ABC):
additional_instructions: Optional[str] = None,
output_dir: Optional[str] = None,
verbose: bool = True,
+ enable_sync: bool = False,
+ sdk: str = "claude",
):
self.run_id = run_id
self.har_path = har_path
@@ -31,6 +34,58 @@ class BaseEngineer(ABC):
self.ui = ClaudeUI(verbose=verbose)
self.usage_metadata: Dict[str, Any] = {}
self.message_store = MessageStore(run_id, output_dir)
+ self.enable_sync = enable_sync
+ self.sdk = sdk
+ self.sync_watcher: Optional[FileSyncWatcher] = None
+ self.local_scripts_dir: Optional[Path] = None
+
+ def start_sync(self):
+ """Start real-time file sync if enabled."""
+ if not self.enable_sync:
+ return
+
+ # Generate local directory name
+ base_name = generate_folder_name(self.prompt, sdk=self.sdk)
+ folder_name = base_name
+ local_dir = Path.cwd() / "scripts" / folder_name
+
+ # Handle existing folder - append suffix if needed
+ counter = 2
+ while local_dir.exists():
+ folder_name = f"{base_name}_{counter}"
+ local_dir = Path.cwd() / "scripts" / folder_name
+ counter += 1
+
+ self.local_scripts_dir = local_dir
+
+ # Create sync watcher
+ def on_sync(message):
+ self.ui.sync_flash(message)
+
+ def on_error(message):
+ self.ui.sync_error(message)
+
+ self.sync_watcher = FileSyncWatcher(
+ source_dir=self.scripts_dir,
+ dest_dir=local_dir,
+ on_sync=on_sync,
+ on_error=on_error,
+ debounce_ms=500,
+ )
+ self.sync_watcher.start()
+ self.ui.sync_started(str(local_dir))
+
+ def stop_sync(self):
+ """Stop real-time file sync."""
+ if self.sync_watcher:
+ self.sync_watcher.stop()
+ self.sync_watcher = None
+
+ def get_sync_status(self) -> Optional[dict]:
+ """Get current sync status."""
+ if self.sync_watcher:
+ return self.sync_watcher.get_status()
+ return None
def _build_analysis_prompt(self) -> str:
"""Build the prompt for analyzing the HAR file."""
diff --git a/src/reverse_api/cli.py b/src/reverse_api/cli.py
index 973226b..d3e8d0c 100644
--- a/src/reverse_api/cli.py
+++ b/src/reverse_api/cli.py
@@ -75,26 +75,66 @@ def prompt_interactive_options(
"/commands",
]
- class FilteredCompleter(Completer):
+ class EnhancedCompleter(Completer):
+ """Autocomplete for slash commands and run IDs."""
+
def get_completions(self, document, complete_event):
text = document.text_before_cursor
- if not text.startswith("/"):
- return
- # Only suggest if we are still on the first word (the command)
- if " " in text:
- return
+ # Slash command completion
+ if text.startswith("/"):
+ # Check if we're after /messages command
+ if text.startswith("/messages "):
+ # Run ID completion for /messages
+ run_id_prefix = text[10:] # Everything after "/messages "
+ for run_id in self._get_run_ids():
+ if run_id.startswith(run_id_prefix):
+ yield Completion(
+ run_id,
+ start_position=-len(run_id_prefix),
+ display_meta=self._get_run_meta(run_id),
+ )
+ elif " " not in text:
+ # Regular slash command completion (no space yet)
+ for cmd in commands:
+ if cmd.startswith(text):
+ yield Completion(cmd, start_position=-len(text))
+ # Run ID completion in engineer mode
+ elif mode_state["mode"] == "engineer" and text:
+ for run_id in self._get_run_ids():
+ if run_id.startswith(text):
+ yield Completion(
+ run_id,
+ start_position=-len(text),
+ display_meta=self._get_run_meta(run_id),
+ )
- for cmd in commands:
- if cmd.startswith(text):
- yield Completion(cmd, start_position=-len(text))
+ def _get_run_ids(self):
+ """Get all run IDs from history (newest first)."""
+ try:
+ history = session_manager.get_history(limit=50)
+ return [run["run_id"] for run in history]
+ except:
+ return []
- command_completer = FilteredCompleter()
+ def _get_run_meta(self, run_id):
+ """Get metadata for a run ID (timestamp + prompt snippet)."""
+ try:
+ run = session_manager.get_run(run_id)
+ if run:
+ timestamp = run.get("timestamp", "")[:16] # YYYY-MM-DD HH:MM
+ prompt = run.get("prompt", "")[:30]
+ return f"[{timestamp}] {prompt}"
+ except:
+ pass
+ return ""
+
+ command_completer = EnhancedCompleter()
# Track mode state (mutable container for closure)
mode_state = {"mode": current_mode, "mode_index": MODES.index(current_mode)}
- # Create key bindings for mode cycling
+ # Create key bindings for mode cycling and autocomplete
kb = KeyBindings()
@kb.add("s-tab") # Shift+Tab
@@ -105,6 +145,22 @@ def prompt_interactive_options(
# Force prompt refresh by invalidating the app
event.app.invalidate()
+ @kb.add("right") # Right arrow
+ def accept_completion(event):
+ """Accept the current autocomplete suggestion with right arrow."""
+ buff = event.app.current_buffer
+ if buff.complete_state:
+ # Save the current completion before closing
+ completion = buff.complete_state.current_completion
+ if completion:
+ buff.apply_completion(completion)
+ else:
+ # No completion selected, just move cursor right
+ buff.cursor_right()
+ else:
+ # If no completion, just move cursor right
+ buff.cursor_right()
+
def get_prompt():
"""Generate prompt with current mode indicator."""
mode = mode_state["mode"]
@@ -246,26 +302,32 @@ def repl_loop():
if "command" in options:
cmd = options["command"]
+ mode_color = MODE_COLORS.get(current_mode, THEME_PRIMARY)
+
if cmd == "/empty":
continue
if cmd == "/exit" or cmd == "/quit":
return # Exit the loop and return to main
elif cmd == "/settings":
- handle_settings()
+ handle_settings(mode_color)
elif cmd == "/history":
- handle_history()
+ handle_history(mode_color)
elif cmd == "/help" or cmd == "/commands":
- handle_help()
+ handle_help(mode_color)
elif cmd.startswith("/messages"):
parts = cmd.split(maxsplit=1)
if len(parts) > 1:
- handle_messages(parts[1].strip())
+ handle_messages(parts[1].strip(), mode_color)
else:
console.print(
" [dim]![/dim] [red]usage:[/red] /messages <run_id>"
)
else:
- console.print(f" [dim]![/dim] [red]unknown command:[/red] {cmd}")
+ # Unknown command - show error and available commands
+ console.print(f" [dim]![/dim] [red]Unknown command:[/red] {cmd}")
+ console.print(
+ " [dim]Available commands: /settings, /history, /messages, /help, /exit[/dim]"
+ )
continue
mode = options.get("mode", "manual")
@@ -307,52 +369,81 @@ def repl_loop():
console.print(f" [dim]![/dim] [red]error:[/red] {e}")
-def handle_settings():
- """Display and manage settings."""
+def handle_settings(mode_color=THEME_PRIMARY):
+ """Display and manage settings with improved layout and descriptions."""
+ from rich.table import Table
+ from rich.panel import Panel
+
console.print()
+
+ # Create a table for current configuration
+ config_table = Table(show_header=False, box=None, padding=(0, 1))
+ config_table.add_column(style="dim", justify="left")
+ config_table.add_column(style=mode_color, justify="left")
+
for k, v in config_manager.config.items():
- console.print(f" [dim]>[/dim] {k:25} [white]{v}[/white]")
+ display_val = str(v) if v is not None else "default"
+ # Make the key more readable
+ key_display = k.replace("_", " ").title()
+ config_table.add_row(key_display, display_val)
+
+ # Display in a panel with mode color
+ console.print(Panel(
+ config_table,
+ title="[bold white]⚙ Settings[/bold white]",
+ subtitle="[dim]Current Configuration[/dim]",
+ border_style=mode_color,
+ padding=(1, 2)
+ ))
+ console.print()
+
+ # Settings menu
+ choices = [
+ Choice(title="Claude Code Model", value="claude_code_model"),
+ Choice(title="SDK", value="sdk"),
+ Choice(title="OpenCode Provider", value="opencode_provider"),
+ Choice(title="OpenCode Model", value="opencode_model"),
+ Choice(title="Agent Provider", value="agent_provider"),
+ Choice(title="Browser-Use Model", value="browser_use_model"),
+ Choice(title="Stagehand Model", value="stagehand_model"),
+ Choice(title="Real-time Sync", value="real_time_sync"),
+ Choice(title="Output Directory", value="output_dir"),
+ Choice(title="Back", value="back"),
+ ]
action = questionary.select(
- "",
- choices=[
- Choice(title="> change claude code model", value="claude_code_model"),
- Choice(title="> change sdk", value="sdk"),
- Choice(title="> opencode provider", value="opencode_provider"),
- Choice(title="> opencode model", value="opencode_model"),
- Choice(title="> agent provider", value="agent_provider"),
- Choice(title="> browser-use model", value="browser_use_model"),
- Choice(title="> stagehand model", value="stagehand_model"),
- Choice(title="> output directory", value="output_dir"),
- Choice(title="> back", value="back"),
- ],
- pointer="",
+ " ", # Add padding via the question prompt
+ choices=choices,
+ pointer="❯",
qmark="",
style=questionary.Style(
[
- ("highlighted", f"fg:{THEME_PRIMARY} bold"),
+ ("pointer", f"fg:{mode_color} bold"),
+ ("highlighted", f"fg:{mode_color} bold"),
("selected", "fg:white"),
+ ("question", ""), # Hide the question text styling
]
),
).ask()
if action is None or action == "back":
- return
+ return # Exit settings to main prompt
if action == "claude_code_model":
model_choices = [
- Choice(title=f"> {c['name'].lower()}", value=c["value"])
+ Choice(title=c['name'].lower(), value=c["value"])
for c in get_model_choices()
]
- model_choices.append(Choice(title="> back", value="back"))
+ model_choices.append(Choice(title="back", value="back"))
model = questionary.select(
"",
choices=model_choices,
- pointer="",
+ pointer="❯",
qmark="",
style=questionary.Style(
[
- ("highlighted", f"fg:{THEME_PRIMARY} bold"),
+ ("pointer", f"fg:{mode_color} bold"),
+ ("highlighted", f"fg:{mode_color} bold"),
]
),
).ask()
@@ -362,18 +453,19 @@ def handle_settings():
elif action == "sdk":
sdk_choices = [
- Choice(title="> opencode", value="opencode"),
- Choice(title="> claude", value="claude"),
- Choice(title="> back", value="back"),
+ Choice(title="opencode", value="opencode"),
+ Choice(title="claude", value="claude"),
+ Choice(title="back", value="back"),
]
sdk = questionary.select(
"",
choices=sdk_choices,
- pointer="",
+ pointer="❯",
qmark="",
style=questionary.Style(
[
- ("highlighted", f"fg:{THEME_PRIMARY} bold"),
+ ("pointer", f"fg:{mode_color} bold"),
+ ("highlighted", f"fg:{mode_color} bold"),
]
),
).ask()
@@ -383,18 +475,19 @@ def handle_settings():
elif action == "agent_provider":
provider_choices = [
- Choice(title="> browser-use", value="browser-use"),
- Choice(title="> stagehand", value="stagehand"),
- Choice(title="> back", value="back"),
+ Choice(title="browser-use", value="browser-use"),
+ Choice(title="stagehand", value="stagehand"),
+ Choice(title="back", value="back"),
]
provider = questionary.select(
"",
choices=provider_choices,
- pointer="",
+ pointer="❯",
qmark="",
style=questionary.Style(
[
- ("highlighted", f"fg:{THEME_PRIMARY} bold"),
+ ("pointer", f"fg:{mode_color} bold"),
+ ("highlighted", f"fg:{mode_color} bold"),
]
),
).ask()
@@ -535,6 +628,30 @@ def handle_settings():
" [dim] - anthropic/claude-opus-4-5-20251101[/dim]\n"
)
+ elif action == "real_time_sync":
+ current = config_manager.get("real_time_sync", True)
+ sync_choices = [
+ Choice(title="Enabled", value=True),
+ Choice(title="Disabled", value=False),
+ Choice(title="Back", value="back"),
+ ]
+ sync = questionary.select(
+ "",
+ choices=sync_choices,
+ pointer="❯",
+ qmark="",
+ style=questionary.Style(
+ [
+ ("pointer", f"fg:{mode_color} bold"),
+ ("highlighted", f"fg:{mode_color} bold"),
+ ]
+ ),
+ ).ask()
+ if sync is not None and sync != "back":
+ config_manager.set("real_time_sync", sync)
+ status = "enabled" if sync else "disabled"
+ console.print(f" [dim]updated[/dim] real-time sync: {status}\n")
+
elif action == "output_dir":
current = config_manager.get("output_dir")
new_dir = questionary.text(
@@ -554,7 +671,7 @@ def handle_settings():
console.print(" [dim]updated[/dim] output directory\n")
-def handle_history():
+def handle_history(mode_color=THEME_PRIMARY):
"""Display history of runs."""
history = session_manager.get_history(limit=15)
if not history:
@@ -565,19 +682,20 @@ def handle_history():
for run in history:
cost = run.get("usage", {}).get("estimated_cost_usd", 0)
cost_str = f"${cost:.3f}" if cost > 0 else "-"
- title = f"> {run['run_id']:12} {run['prompt'][:40]:40} {cost_str:>8}"
+ title = f"{run['run_id']:12} {run['prompt'][:40]:40} {cost_str:>8}"
choices.append(Choice(title=title, value=run["run_id"]))
- choices.append(Choice(title="> back", value="back"))
+ choices.append(Choice(title="back", value="back"))
run_id = questionary.select(
"",
choices=choices,
- pointer="",
+ pointer="❯",
qmark="",
style=questionary.Style(
[
- ("highlighted", f"fg:{THEME_PRIMARY} bold"),
+ ("pointer", f"fg:{mode_color} bold"),
+ ("highlighted", f"fg:{mode_color} bold"),
("selected", "fg:white"),
]
),
@@ -588,8 +706,38 @@ def handle_history():
run = session_manager.get_run(run_id)
if run:
- console.print(Panel(json.dumps(run, indent=2), border_style=THEME_DIM))
- if questionary.confirm(" > recode?").ask():
+ from rich.table import Table
+
+ # Create a formatted display of the run details
+ details_table = Table(show_header=False, box=None, padding=(0, 1))
+ details_table.add_column(style="dim", justify="left", width=20)
+ details_table.add_column(style="white", justify="left")
+
+ details_table.add_row("Run ID", run.get("run_id", "-"))
+ details_table.add_row("Timestamp", run.get("timestamp", "-"))
+ details_table.add_row("Prompt", run.get("prompt", "-"))
+ details_table.add_row("Model", run.get("model", "-"))
+ details_table.add_row("Mode", run.get("mode", "-"))
+
+ usage = run.get("usage", {})
+ if usage:
+ cost = usage.get("estimated_cost_usd", 0)
+ details_table.add_row("Cost", f"${cost:.4f}" if cost > 0 else "-")
+ input_tokens = usage.get("input_tokens", 0)
+ output_tokens = usage.get("output_tokens", 0)
+ if input_tokens or output_tokens:
+ details_table.add_row("Tokens", f"{input_tokens:,} in / {output_tokens:,} out")
+
+ console.print()
+ console.print(Panel(
+ details_table,
+ title=f"[bold white]📋 Run Details[/bold white]",
+ border_style=mode_color,
+ padding=(1, 2)
+ ))
+ console.print()
+
+ if questionary.confirm(" > recode?", qmark="").ask():
model = run.get("model") or config_manager.get(
"claude_code_model", "claude-sonnet-4-5"
)
@@ -598,29 +746,78 @@ def handle_history():
console.print(" [dim]> not found[/dim]")
-def handle_help():
- """Show help for slash commands."""
- console.print()
- console.print(" [white]commands[/white]")
- console.print(" [dim]>[/dim] /settings [dim]system[/dim]")
- console.print(" [dim]>[/dim] /history [dim]logs[/dim]")
- console.print(" [dim]>[/dim] /messages [dim]view run messages[/dim]")
- console.print(" [dim]>[/dim] /help [dim]help[/dim]")
- console.print(" [dim]>[/dim] /exit [dim]quit[/dim]")
+def handle_help(mode_color=THEME_PRIMARY):
+ """Show enhanced help with command details and examples."""
+ from rich.table import Table
+ from rich.text import Text
+
console.print()
- console.print(" [white]modes[/white] [dim](shift+tab to cycle)[/dim]")
- console.print(
- " [dim]>[/dim] manual [dim]full pipeline: browser + reverse engineering[/dim]"
+
+ # Commands table
+ commands_table = Table(show_header=False, box=None, padding=(0, 1))
+ commands_table.add_column(style=f"{mode_color} bold", justify="left", width=20)
+ commands_table.add_column(style="white", justify="left")
+
+ commands_table.add_row(
+ "/settings",
+ "Configure model, SDK, agent provider, and sync settings\n[dim]Usage: /settings[/dim]"
)
- console.print(
- " [dim]>[/dim] engineer [dim]reverse engineer only (enter run_id)[/dim]"
+ commands_table.add_row("", "") # Spacing
+
+ commands_table.add_row(
+ "/history",
+ "View past runs with timestamps, costs, and status\n[dim]Usage: /history[/dim]"
)
- console.print(" [dim]>[/dim] agent [dim]autonomous agent + capture[/dim]")
+ commands_table.add_row("", "")
+
+ commands_table.add_row(
+ "/messages <run_id>",
+ "View detailed message logs from a specific run\n[dim]Usage: /messages abc123[/dim]"
+ )
+ commands_table.add_row("", "")
+
+ commands_table.add_row(
+ "/help",
+ "Show this help message\n[dim]Usage: /help[/dim]"
+ )
+ commands_table.add_row("", "")
+
+ commands_table.add_row(
+ "/exit or /quit",
+ "Exit the application\n[dim]Usage: /exit[/dim]"
+ )
+
+ console.print(Panel(
+ commands_table,
+ title="[bold white]📚 Available Commands[/bold white]",
+ border_style=mode_color,
+ padding=(1, 2)
+ ))
+
+ # Modes table
+ console.print()
+ modes_table = Table(show_header=False, box=None, padding=(0, 1))
+ modes_table.add_column(style=f"{mode_color} bold", justify="left", width=15)
+ modes_table.add_column(style="dim", justify="left")
+
+ modes_table.add_row("manual", "Full pipeline: browser + reverse engineering")
+ modes_table.add_row("engineer", "Reverse engineer only (enter run_id)")
+ modes_table.add_row("agent", "Autonomous agent + capture")
+
+ console.print(Panel(
+ modes_table,
+ title="[bold white]🔄 Modes[/bold white]",
+ subtitle="[dim]Shift+Tab to cycle[/dim]",
+ border_style=mode_color,
+ padding=(1, 2)
+ ))
console.print()
-def handle_messages(run_id: str):
+def handle_messages(run_id: str, mode_color=THEME_PRIMARY):
"""Display messages from a previous run."""
+ from rich.table import Table
+
store = MessageStore(run_id)
messages = store.load()
@@ -628,8 +825,19 @@ def handle_messages(run_id: str):
console.print(f" [dim]>[/dim] [red]no messages found for run:[/red] {run_id}")
return
+ # Create header panel
+ header_table = Table(show_header=False, box=None, padding=(0, 0))
+ header_table.add_column(style="white", justify="left")
+ header_table.add_row(f"Run ID: {run_id}")
+ header_table.add_row(f"Total Messages: {len(messages)}")
+
console.print()
- console.print(f" [white]messages for {run_id}[/white]")
+ console.print(Panel(
+ header_table,
+ title="[bold white]💬 Message Log[/bold white]",
+ border_style=mode_color,
+ padding=(1, 2)
+ ))
console.print()
for msg in messages:
@@ -892,6 +1100,8 @@ def run_engineer(run_id, har_path=None, prompt=None, model=None, output_dir=None
har_path = har_dir / "recording.har"
sdk = config_manager.get("sdk", "claude")
+ enable_sync = config_manager.get("real_time_sync", True)
+
if sdk == "opencode":
result = run_reverse_engineering(
run_id=run_id,
@@ -902,6 +1112,7 @@ def run_engineer(run_id, har_path=None, prompt=None, model=None, output_dir=None
sdk=sdk,
opencode_provider=config_manager.get("opencode_provider", "anthropic"),
opencode_model=config_manager.get("opencode_model", "claude-sonnet-4-5"),
+ enable_sync=enable_sync,
)
else:
result = run_reverse_engineering(
@@ -911,35 +1122,42 @@ def run_engineer(run_id, har_path=None, prompt=None, model=None, output_dir=None
model=model or config_manager.get("claude_code_model", "claude-sonnet-4-5"),
output_dir=output_dir,
sdk=sdk,
+ enable_sync=enable_sync,
)
if result:
- # Automatically copy scripts to current directory with a readable name
- scripts_dir = Path(result["script_path"]).parent
- base_name = generate_folder_name(prompt, sdk=sdk)
- folder_name = base_name
- local_dir = Path.cwd() / "scripts" / folder_name
-
- # Handle existing folder - append suffix if needed
- counter = 2
- while local_dir.exists():
- folder_name = f"{base_name}_{counter}"
+ # Skip manual copy if real-time sync is enabled (files already synced)
+ if not enable_sync:
+ # Automatically copy scripts to current directory with a readable name
+ scripts_dir = Path(result["script_path"]).parent
+ base_name = generate_folder_name(prompt, sdk=sdk)
+ folder_name = base_name
local_dir = Path.cwd() / "scripts" / folder_name
- counter += 1
- local_dir.mkdir(parents=True, exist_ok=True)
+ # Handle existing folder - append suffix if needed
+ counter = 2
+ while local_dir.exists():
+ folder_name = f"{base_name}_{counter}"
+ local_dir = Path.cwd() / "scripts" / folder_name
+ counter += 1
- import shutil
+ local_dir.mkdir(parents=True, exist_ok=True)
- for item in scripts_dir.iterdir():
- if item.is_file():
- shutil.copy2(item, local_dir / item.name)
+ import shutil
- console.print(" [dim]>[/dim] [white]decoding complete[/white]")
- console.print(f" [dim]>[/dim] [white]{result['script_path']}[/white]")
- console.print(
- f" [dim]>[/dim] [white]copied to ./scripts/{folder_name}[/white]\n"
- )
+ for item in scripts_dir.iterdir():
+ if item.is_file():
+ shutil.copy2(item, local_dir / item.name)
+
+ console.print(" [dim]>[/dim] [white]decoding complete[/white]")
+ console.print(f" [dim]>[/dim] [white]{result['script_path']}[/white]")
+ console.print(
+ f" [dim]>[/dim] [white]copied to ./scripts/{folder_name}[/white]\n"
+ )
+ else:
+ # With sync enabled, just show completion
+ console.print(" [dim]>[/dim] [white]decoding complete[/white]")
+ console.print(f" [dim]>[/dim] [white]{result['script_path']}[/white]\n")
session_manager.update_run(
run_id=run_id,
diff --git a/src/reverse_api/config.py b/src/reverse_api/config.py
index 153f53b..f690896 100644
--- a/src/reverse_api/config.py
+++ b/src/reverse_api/config.py
@@ -13,6 +13,7 @@ DEFAULT_CONFIG = {
"agent_provider": "browser-use",
"browser_use_model": "bu-llm", # "bu-llm" or "{provider}/{model_name}" (e.g. "openai/gpt-5-mini")
"stagehand_model": "openai/computer-use-preview-2025-03-11", # "{provider}/{model_name}" format
+ "real_time_sync": True, # Enable real-time file sync during engineering
}
diff --git a/src/reverse_api/engineer.py b/src/reverse_api/engineer.py
index 8204ecf..ec5b159 100644
--- a/src/reverse_api/engineer.py
+++ b/src/reverse_api/engineer.py
@@ -97,7 +97,8 @@ class ClaudeEngineer(BaseEngineer):
return None
else:
script_path = str(self.scripts_dir / "api_client.py")
- self.ui.success(script_path)
+ local_path = str(self.local_scripts_dir / "api_client.py") if self.local_scripts_dir else None
+ self.ui.success(script_path, local_path)
# Calculate estimated cost if we have usage data
if self.usage_metadata:
@@ -182,6 +183,7 @@ def run_reverse_engineering(
sdk: str = "claude",
opencode_provider: Optional[str] = None,
opencode_model: Optional[str] = None,
+ enable_sync: bool = False,
) -> Optional[Dict[str, Any]]:
"""Run reverse engineering with the specified SDK.
@@ -189,6 +191,7 @@ def run_reverse_engineering(
sdk: "opencode" or "claude" - determines which SDK to use
opencode_provider: Provider ID for OpenCode (e.g., "anthropic")
opencode_model: Model ID for OpenCode (e.g., "claude-sonnet-4-5")
+ enable_sync: Enable real-time file syncing during engineering
"""
if sdk == "opencode":
from .opencode_engineer import OpenCodeEngineer
@@ -203,6 +206,8 @@ def run_reverse_engineering(
verbose=verbose,
opencode_provider=opencode_provider,
opencode_model=opencode_model,
+ enable_sync=enable_sync,
+ sdk=sdk,
)
else:
engineer = ClaudeEngineer(
@@ -213,6 +218,17 @@ def run_reverse_engineering(
additional_instructions=additional_instructions,
output_dir=output_dir,
verbose=verbose,
+ enable_sync=enable_sync,
+ sdk=sdk,
)
- return asyncio.run(engineer.analyze_and_generate())
+ # Start sync before analysis
+ engineer.start_sync()
+
+ try:
+ result = asyncio.run(engineer.analyze_and_generate())
+ finally:
+ # Always stop sync when done
+ engineer.stop_sync()
+
+ return result
diff --git a/src/reverse_api/opencode_engineer.py b/src/reverse_api/opencode_engineer.py
index be56746..ecaa31b 100644
--- a/src/reverse_api/opencode_engineer.py
+++ b/src/reverse_api/opencode_engineer.py
@@ -153,7 +153,8 @@ class OpenCodeEngineer(BaseEngineer):
# Show session summary before success message
self.opencode_ui.session_summary(self.usage_metadata)
- self.opencode_ui.success(script_path)
+ local_path = str(self.local_scripts_dir / "api_client.py") if self.local_scripts_dir else None
+ self.opencode_ui.success(script_path, local_path)
result_data: Dict[str, Any] = {
"script_path": script_path,
diff --git a/src/reverse_api/opencode_ui.py b/src/reverse_api/opencode_ui.py
index 39ab747..ea7fe1a 100644
--- a/src/reverse_api/opencode_ui.py
+++ b/src/reverse_api/opencode_ui.py
@@ -204,11 +204,13 @@ class OpenCodeUI:
display_text += "..."
self.console.print(f" .. {display_text}", style=THEME_DIM)
- def success(self, script_path: str) -> None:
+ def success(self, script_path: str, local_path: str = None) -> None:
"""Display success message."""
self.console.print()
self.console.print(f" [dim]decoding complete[/dim]")
- self.console.print(f" [white]{script_path}[/white]")
+ self.console.print(f" [dim]internal:[/dim] [white]{script_path}[/white]")
+ if local_path:
+ self.console.print(f" [dim]synced:[/dim] [white]{local_path}[/white]")
self.console.print()
def error(self, message: str) -> None:
@@ -335,3 +337,15 @@ class OpenCodeUI:
if len(path) <= max_len:
return path
return "..." + path[-(max_len - 3) :]
+
+ def sync_started(self, dest_dir: str) -> None:
+ """Display when file sync starts."""
+ self.console.print(f" [dim]⟳ sync: active → {dest_dir}[/dim]")
+
+ def sync_flash(self, message: str) -> None:
+ """Display a brief sync notification."""
+ self.console.print(f" [dim]✓ {message}[/dim]")
+
+ def sync_error(self, message: str) -> None:
+ """Display a sync error."""
+ self.console.print(f" [dim]![/dim] [yellow]sync error:[/yellow] {message}")
diff --git a/src/reverse_api/pricing.py b/src/reverse_api/pricing.py
index 7cdc9a9..c8c30aa 100644
--- a/src/reverse_api/pricing.py
+++ b/src/reverse_api/pricing.py
@@ -189,6 +189,23 @@ def _get_pricing_from_litellm(model_id: str) -> dict[str, float] | None:
return None
+def get_model_pricing(model_id: str) -> dict[str, float] | None:
+ """Get pricing dictionary for a model.
+
+ Args:
+ model_id: Model identifier
+
+ Returns:
+ Pricing dictionary with keys: input, output, cache_creation, cache_read, reasoning
+ Returns None if model not found
+ """
+ if model_id in MODEL_PRICING:
+ return MODEL_PRICING[model_id]
+ elif litellm_pricing := _get_pricing_from_litellm(model_id):
+ return litellm_pricing
+ return None
+
+
def calculate_cost(
model_id: str | None = None,
input_tokens: int = 0,
diff --git a/src/reverse_api/sync.py b/src/reverse_api/sync.py
new file mode 100644
index 0000000..769f0da
--- /dev/null
+++ b/src/reverse_api/sync.py
@@ -0,0 +1,190 @@
+"""Real-time file synchronization with watchdog."""
+
+import time
+import shutil
+from pathlib import Path
+from typing import Optional, Callable
+from threading import Thread, Event
+from watchdog.observers import Observer
+from watchdog.events import FileSystemEventHandler, FileSystemEvent
+
+
+class SyncHandler(FileSystemEventHandler):
+ """Handle file system events for syncing."""
+
+ def __init__(
+ self,
+ source_dir: Path,
+ dest_dir: Path,
+ on_sync: Optional[Callable[[str], None]] = None,
+ on_error: Optional[Callable[[str], None]] = None,
+ debounce_ms: int = 500,
+ ):
+ self.source_dir = source_dir
+ self.dest_dir = dest_dir
+ self.on_sync = on_sync
+ self.on_error = on_error
+ self.debounce_ms = debounce_ms / 1000.0 # Convert to seconds
+ self.pending_events = {}
+ self.last_sync_time = 0
+ self.file_count = 0
+
+ def on_created(self, event: FileSystemEvent):
+ """Handle file creation."""
+ if not event.is_directory:
+ self._queue_sync(event.src_path)
+
+ def on_modified(self, event: FileSystemEvent):
+ """Handle file modification."""
+ if not event.is_directory:
+ self._queue_sync(event.src_path)
+
+ def on_deleted(self, event: FileSystemEvent):
+ """Handle file deletion."""
+ if not event.is_directory:
+ self._queue_sync(event.src_path, is_delete=True)
+
+ def _queue_sync(self, file_path: str, is_delete: bool = False):
+ """Queue a file for syncing with debouncing."""
+ self.pending_events[file_path] = {
+ "time": time.time(),
+ "is_delete": is_delete,
+ }
+
+ def process_pending(self):
+ """Process pending sync events (debounced)."""
+ current_time = time.time()
+ to_sync = []
+
+ # Find events that have passed the debounce period
+ for file_path, event_data in list(self.pending_events.items()):
+ if current_time - event_data["time"] >= self.debounce_ms:
+ to_sync.append((file_path, event_data["is_delete"]))
+ del self.pending_events[file_path]
+
+ # Sync the files
+ for file_path, is_delete in to_sync:
+ try:
+ self._sync_file(file_path, is_delete)
+ except Exception as e:
+ if self.on_error:
+ self.on_error(f"permission denied: {Path(file_path).name}")
+
+ def _sync_file(self, source_path: str, is_delete: bool = False):
+ """Sync a single file from source to destination."""
+ source = Path(source_path)
+ relative = source.relative_to(self.source_dir)
+ dest = self.dest_dir / relative
+
+ if is_delete:
+ # Delete from destination
+ if dest.exists():
+ dest.unlink()
+ self.file_count -= 1
+ else:
+ # Copy to destination
+ dest.parent.mkdir(parents=True, exist_ok=True)
+ shutil.copy2(source, dest)
+ self.file_count += 1
+
+ # Update last sync time
+ self.last_sync_time = time.time()
+
+ # Notify of sync
+ if self.on_sync:
+ action = "Deleted" if is_delete else "Synced"
+ self.on_sync(f"{action} {source.name}")
+
+
+class FileSyncWatcher:
+ """Watch a directory and sync files in real-time."""
+
+ def __init__(
+ self,
+ source_dir: Path,
+ dest_dir: Path,
+ on_sync: Optional[Callable[[str], None]] = None,
+ on_error: Optional[Callable[[str], None]] = None,
+ debounce_ms: int = 500,
+ ):
+ self.source_dir = source_dir
+ self.dest_dir = dest_dir
+ self.debounce_ms = debounce_ms
+
+ # Create handler
+ self.handler = SyncHandler(
+ source_dir=source_dir,
+ dest_dir=dest_dir,
+ on_sync=on_sync,
+ on_error=on_error,
+ debounce_ms=debounce_ms,
+ )
+
+ # Create observer
+ self.observer = Observer()
+ self.observer.schedule(self.handler, str(source_dir), recursive=True)
+
+ # Thread control
+ self.stop_event = Event()
+ self.process_thread: Optional[Thread] = None
+
+ def start(self):
+ """Start watching and syncing."""
+ # Ensure destination exists
+ self.dest_dir.mkdir(parents=True, exist_ok=True)
+
+ # Start observer
+ self.observer.start()
+
+ # Start processing thread for debouncing
+ self.process_thread = Thread(target=self._process_loop, daemon=True)
+ self.process_thread.start()
+
+ def stop(self):
+ """Stop watching and syncing."""
+ self.stop_event.set()
+ self.observer.stop()
+ self.observer.join(timeout=2)
+
+ if self.process_thread:
+ self.process_thread.join(timeout=2)
+
+ def _process_loop(self):
+ """Background loop to process pending sync events."""
+ while not self.stop_event.is_set():
+ self.handler.process_pending()
+ time.sleep(0.1) # Check every 100ms
+
+ def get_status(self) -> dict:
+ """Get current sync status."""
+ if not self.observer.is_alive():
+ return {
+ "active": False,
+ "error": "Observer stopped",
+ }
+
+ seconds_since_sync = (
+ time.time() - self.handler.last_sync_time
+ if self.handler.last_sync_time > 0
+ else 0
+ )
+
+ return {
+ "active": True,
+ "last_sync": f"{int(seconds_since_sync)}s ago"
+ if self.handler.last_sync_time > 0
+ else "never",
+ "file_count": self.handler.file_count,
+ }
+
+
+def sync_directory_once(source_dir: Path, dest_dir: Path):
+ """Perform a one-time sync of a directory."""
+ dest_dir.mkdir(parents=True, exist_ok=True)
+
+ for item in source_dir.rglob("*"):
+ if item.is_file():
+ relative = item.relative_to(source_dir)
+ dest = dest_dir / relative
+ dest.parent.mkdir(parents=True, exist_ok=True)
+ shutil.copy2(item, dest)
\ No newline at end of file
diff --git a/src/reverse_api/tui.py b/src/reverse_api/tui.py
index 529b56a..81297f0 100644
--- a/src/reverse_api/tui.py
+++ b/src/reverse_api/tui.py
@@ -135,11 +135,13 @@ class ClaudeUI:
"""Display a progress message."""
self.console.print(f" [dim italic]{message}[/dim italic]")
- def success(self, script_path: str) -> None:
+ def success(self, script_path: str, local_path: str = None) -> None:
"""Display success message with generated script path."""
self.console.print()
self.console.print(f" [dim]decoding complete[/dim]")
- self.console.print(f" [white]{script_path}[/white]")
+ self.console.print(f" [dim]internal:[/dim] [white]{script_path}[/white]")
+ if local_path:
+ self.console.print(f" [dim]synced:[/dim] [white]{local_path}[/white]")
self.console.print()
def error(self, message: str) -> None:
@@ -179,6 +181,18 @@ class ClaudeUI:
return path
return "..." + path[-(max_len - 3) :]
+ def sync_started(self, dest_dir: str) -> None:
+ """Display when file sync starts."""
+ self.console.print(f" [dim]⟳ sync: active → {dest_dir}[/dim]")
+
+ def sync_flash(self, message: str) -> None:
+ """Display a brief sync notification."""
+ self.console.print(f" [dim]✓ {message}[/dim]")
+
+ def sync_error(self, message: str) -> None:
+ """Display a sync error."""
+ self.console.print(f" [dim]![/dim] [yellow]sync error:[/yellow] {message}")
+
def get_model_choices() -> list[dict]:
"""Get available model choices for questionary."""