mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-09-04 10:11:46 +03:00
feat(cli): make nanobot launch the terminal agent
This commit is contained in:
+12
-3
@@ -87,7 +87,12 @@ app = typer.Typer(
|
||||
name="nanobot",
|
||||
context_settings={"help_option_names": ["-h", "--help"]},
|
||||
help=f"{__logo__} nanobot - Personal AI Assistant",
|
||||
no_args_is_help=True,
|
||||
epilog=(
|
||||
"Run `nanobot` without a subcommand to start the terminal agent. "
|
||||
"Use `nanobot agent --help` for agent options."
|
||||
),
|
||||
invoke_without_command=True,
|
||||
no_args_is_help=False,
|
||||
)
|
||||
|
||||
console = Console()
|
||||
@@ -98,7 +103,7 @@ def version_callback(value: bool):
|
||||
raise typer.Exit()
|
||||
|
||||
|
||||
@app.callback()
|
||||
@app.callback(invoke_without_command=True)
|
||||
def main(
|
||||
ctx: typer.Context,
|
||||
version: bool = typer.Option(
|
||||
@@ -110,7 +115,11 @@ def main(
|
||||
# imports this Typer app directly instead of ``nanobot.cli.entry``. Keep the
|
||||
# role identity correct until that launcher is regenerated.
|
||||
command = ctx.invoked_subcommand
|
||||
set_cli_process_identity([command] if command else sys.argv[1:])
|
||||
set_cli_process_identity([command] if command else ["agent"])
|
||||
if command is None:
|
||||
from nanobot.cli.entry import _run_agent
|
||||
|
||||
_run_agent([], prog_name="nanobot")
|
||||
|
||||
|
||||
# ============================================================================
|
||||
|
||||
+44
-10
@@ -8,6 +8,28 @@ from contextlib import suppress
|
||||
|
||||
from nanobot.cli.process_identity import set_cli_process_identity
|
||||
|
||||
_ROOT_OPTIONS = frozenset(
|
||||
{
|
||||
"-h",
|
||||
"--help",
|
||||
"-v",
|
||||
"--version",
|
||||
"--install-completion",
|
||||
"--show-completion",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _agent_invocation_args(args: list[str]) -> list[str] | None:
|
||||
"""Return agent arguments when the root command should act as ``agent``."""
|
||||
if not args:
|
||||
return []
|
||||
if args[0] == "agent":
|
||||
return args[1:]
|
||||
if args[0].startswith("-") and args[0].split("=", 1)[0] not in _ROOT_OPTIONS:
|
||||
return args
|
||||
return None
|
||||
|
||||
|
||||
def _native_tui_candidate(args: list[str]) -> bool:
|
||||
"""Return whether ``agent`` can start without the classic agent stack."""
|
||||
@@ -34,19 +56,31 @@ def _configure_windows_console() -> None:
|
||||
reconfigure(encoding="utf-8", errors="replace")
|
||||
|
||||
|
||||
def _run_agent(args: list[str], *, prog_name: str) -> None:
|
||||
"""Run the shared agent command without importing the complete CLI graph."""
|
||||
import typer
|
||||
|
||||
from nanobot.cli.agent import agent
|
||||
|
||||
agent_app = typer.Typer(add_completion=False)
|
||||
agent_app.command()(agent)
|
||||
command = typer.main.get_command(agent_app)
|
||||
command.main(args=args, prog_name=prog_name)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Dispatch native TUI startup without importing the complete CLI graph."""
|
||||
set_cli_process_identity(sys.argv[1:])
|
||||
raw_args = sys.argv[1:]
|
||||
agent_args = _agent_invocation_args(raw_args)
|
||||
dispatch_args = ["agent", *agent_args] if agent_args is not None else raw_args
|
||||
set_cli_process_identity(dispatch_args)
|
||||
_configure_windows_console()
|
||||
if _native_tui_candidate(sys.argv[1:]):
|
||||
import typer
|
||||
|
||||
from nanobot.cli.agent import agent
|
||||
|
||||
fast_app = typer.Typer(add_completion=False)
|
||||
fast_app.command()(agent)
|
||||
command = typer.main.get_command(fast_app)
|
||||
command.main(args=sys.argv[2:], prog_name="nanobot agent")
|
||||
root_agent_alias = agent_args is not None and raw_args[:1] != ["agent"]
|
||||
if agent_args is not None and (
|
||||
root_agent_alias or _native_tui_candidate(dispatch_args)
|
||||
):
|
||||
prog_name = "nanobot" if root_agent_alias else "nanobot agent"
|
||||
_run_agent(agent_args, prog_name=prog_name)
|
||||
return
|
||||
|
||||
from nanobot.cli.commands import app
|
||||
|
||||
Reference in New Issue
Block a user