Interactive code editor with LLM assistance and auto-backup. Enter instructions in natural language. The LLM reads your code, makes changes, and auto-backs up before each edit. Commands during interactive mode: preview <...> --- dry-run: show proposed edits without applying
(
dir: Path = typer.Option(
..., "--dir", "-d",
help="Path to the code directory to edit",
),
config_path: Path = typer.Option(None, "--config", "-c", help="Path to config file"),
instruction: str = typer.Option(
None, "--instruction", "-i",
help="One-shot instruction (non-interactive mode)",
),
)
| 14 | |
| 15 | @app.command("code") |
| 16 | def code_edit( |
| 17 | dir: Path = typer.Option( |
| 18 | ..., "--dir", "-d", |
| 19 | help="Path to the code directory to edit", |
| 20 | ), |
| 21 | config_path: Path = typer.Option(None, "--config", "-c", help="Path to config file"), |
| 22 | instruction: str = typer.Option( |
| 23 | None, "--instruction", "-i", |
| 24 | help="One-shot instruction (non-interactive mode)", |
| 25 | ), |
| 26 | ) -> None: |
| 27 | """Interactive code editor with LLM assistance and auto-backup. |
| 28 | |
| 29 | Enter instructions in natural language. The LLM reads your code, |
| 30 | makes changes, and auto-backs up before each edit. |
| 31 | |
| 32 | Commands during interactive mode: |
| 33 | preview <...> --- dry-run: show proposed edits without applying |
| 34 | undo --- rollback to the state before the last edit |
| 35 | rollback --- show all snapshots and pick one to restore |
| 36 | history --- show edit history |
| 37 | files --- list current code files |
| 38 | exit / quit --- exit the editor |
| 39 | """ |
| 40 | from rich.panel import Panel |
| 41 | |
| 42 | from nanoresearch.agents.code_editor import ( |
| 43 | CodeSnapshotManager, |
| 44 | InteractiveCodeEditor, |
| 45 | read_code_context, |
| 46 | ) |
| 47 | |
| 48 | code_dir = Path(dir).resolve() |
| 49 | if not code_dir.is_dir(): |
| 50 | console.print(f"[red]Directory not found:[/red] {code_dir}") |
| 51 | raise typer.Exit(1) |
| 52 | |
| 53 | config = _load_config_safe(config_path) |
| 54 | editor = InteractiveCodeEditor( |
| 55 | code_dir, config, log_fn=lambda msg: console.print(f" [dim]{msg}[/dim]"), |
| 56 | ) |
| 57 | |
| 58 | # -- One-shot mode -- |
| 59 | if instruction: |
| 60 | result = asyncio.run(_code_apply(editor, instruction)) |
| 61 | _print_edit_result(result) |
| 62 | return |
| 63 | |
| 64 | # -- Interactive mode -- |
| 65 | console.print(Panel( |
| 66 | f"[bold]Code directory:[/bold] {code_dir}\n" |
| 67 | f"[bold]Files:[/bold] {len(read_code_context(code_dir))}\n\n" |
| 68 | "Type your instructions in natural language.\n" |
| 69 | "Commands: [cyan]undo[/cyan] | [cyan]rollback[/cyan] | " |
| 70 | "[cyan]history[/cyan] | [cyan]files[/cyan] | [cyan]exit[/cyan]", |
| 71 | title="NanoResearch Code Editor", |
| 72 | border_style="blue", |
| 73 | )) |
nothing calls this directly
no test coverage detected