Skip to content

Your lightweight open-source personal agent.

feat(api): load serve settings from config

Xubin Ren5635907e3318
diff --git a/nanobot/api/server.py b/nanobot/api/server.py
index 1dd58d512..2a818667a 100644
--- a/nanobot/api/server.py
+++ b/nanobot/api/server.py
@@ -192,7 +192,7 @@ def create_app(agent_loop, model_name: str = "nanobot", request_timeout: float =
return app
-def run_server(agent_loop, host: str = "0.0.0.0", port: int = 8900,
+def run_server(agent_loop, host: str = "127.0.0.1", port: int = 8900,
model_name: str = "nanobot", request_timeout: float = 120.0) -> None:
"""Create and run the server (blocking)."""
app = create_app(agent_loop, model_name=model_name, request_timeout=request_timeout)
diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py
index d3fc68e8f..7f7d24f39 100644
--- a/nanobot/cli/commands.py
+++ b/nanobot/cli/commands.py
@@ -498,9 +498,9 @@ def _migrate_cron_store(config: "Config") -> None:
@app.command()
def serve(
- port: int = typer.Option(8900, "--port", "-p", help="API server port"),
- host: str = typer.Option("0.0.0.0", "--host", "-H", help="Bind address"),
- timeout: float = typer.Option(120.0, "--timeout", "-t", help="Per-request timeout (seconds)"),
+ port: int | None = typer.Option(None, "--port", "-p", help="API server port"),
+ host: str | None = typer.Option(None, "--host", "-H", help="Bind address"),
+ timeout: float | None = typer.Option(None, "--timeout", "-t", help="Per-request timeout (seconds)"),
verbose: bool = typer.Option(False, "--verbose", "-v", help="Show nanobot runtime logs"),
workspace: str | None = typer.Option(None, "--workspace", "-w", help="Workspace directory"),
config: str | None = typer.Option(None, "--config", "-c", help="Path to config file"),
@@ -524,6 +524,10 @@ def serve(
logger.disable("nanobot")
runtime_config = _load_runtime_config(config, workspace)
+ api_cfg = runtime_config.api
+ host = host if host is not None else api_cfg.host
+ port = port if port is not None else api_cfg.port
+ timeout = timeout if timeout is not None else api_cfg.timeout
sync_workspace_templates(runtime_config.workspace_path)
bus = MessageBus()
provider = _make_provider(runtime_config)
@@ -551,6 +555,11 @@ def serve(
console.print(f" [cyan]Model[/cyan] : {model_name}")
console.print(" [cyan]Session[/cyan] : api:default")
console.print(f" [cyan]Timeout[/cyan] : {timeout}s")
+ if host in {"0.0.0.0", "::"}:
+ console.print(
+ "[yellow]Warning:[/yellow] API is bound to all interfaces. "
+ "Only do this behind a trusted network boundary, firewall, or reverse proxy."
+ )
console.print()
api_app = create_app(agent_loop, model_name=model_name, request_timeout=timeout)
diff --git a/nanobot/config/schema.py b/nanobot/config/schema.py
index c8b69b42e..c4c927afd 100644
--- a/nanobot/config/schema.py
+++ b/nanobot/config/schema.py
@@ -96,6 +96,14 @@ class HeartbeatConfig(Base):
keep_recent_messages: int = 8
+class ApiConfig(Base):
+ """OpenAI-compatible API server configuration."""
+
+ host: str = "127.0.0.1" # Safer default: local-only bind.
+ port: int = 8900
+ timeout: float = 120.0 # Per-request timeout in seconds.
+
+
class GatewayConfig(Base):
"""Gateway/server configuration."""
@@ -156,6 +164,7 @@ class Config(BaseSettings):
agents: AgentsConfig = Field(default_factory=AgentsConfig)
channels: ChannelsConfig = Field(default_factory=ChannelsConfig)
providers: ProvidersConfig = Field(default_factory=ProvidersConfig)
+ api: ApiConfig = Field(default_factory=ApiConfig)
gateway: GatewayConfig = Field(default_factory=GatewayConfig)
tools: ToolsConfig = Field(default_factory=ToolsConfig)
diff --git a/tests/cli/test_commands.py b/tests/cli/test_commands.py
index a8fcc4aa0..735c02a5a 100644
--- a/tests/cli/test_commands.py
+++ b/tests/cli/test_commands.py
@@ -642,27 +642,105 @@ def test_heartbeat_retains_recent_messages_by_default():
assert config.gateway.heartbeat.keep_recent_messages == 8
-def test_gateway_uses_workspace_from_config_by_default(monkeypatch, tmp_path: Path) -> None:
+def _write_instance_config(tmp_path: Path) -> Path:
config_file = tmp_path / "instance" / "config.json"
config_file.parent.mkdir(parents=True)
config_file.write_text("{}")
+ return config_file
- config = Config()
- config.agents.defaults.workspace = str(tmp_path / "config-workspace")
- seen: dict[str, Path] = {}
+def _stop_gateway_provider(_config) -> object:
+ raise _StopGatewayError("stop")
+
+
+def _patch_cli_command_runtime(
+ monkeypatch,
+ config: Config,
+ *,
+ set_config_path=None,
+ sync_templates=None,
+ make_provider=None,
+ message_bus=None,
+ session_manager=None,
+ cron_service=None,
+ get_cron_dir=None,
+) -> None:
monkeypatch.setattr(
"nanobot.config.loader.set_config_path",
- lambda path: seen.__setitem__("config_path", path),
+ set_config_path or (lambda _path: None),
)
monkeypatch.setattr("nanobot.config.loader.load_config", lambda _path=None: config)
monkeypatch.setattr(
"nanobot.cli.commands.sync_workspace_templates",
- lambda path: seen.__setitem__("workspace", path),
+ sync_templates or (lambda _path: None),
)
monkeypatch.setattr(
"nanobot.cli.commands._make_provider",
- lambda _config: (_ for _ in ()).throw(_StopGatewayError("stop")),
+ make_provider or (lambda _config: object()),
+ )
+
+ if message_bus is not None:
+ monkeypatch.setattr("nanobot.bus.queue.MessageBus", message_bus)
+ if session_manager is not None:
+ monkeypatch.setattr("nanobot.session.manager.SessionManager", session_manager)
+ if cron_service is not None:
+ monkeypatch.setattr("nanobot.cron.service.CronService", cron_service)
+ if get_cron_dir is not None:
+ monkeypatch.setattr("nanobot.config.paths.get_cron_dir", get_cron_dir)
+
+
+def _patch_serve_runtime(monkeypatch, config: Config, seen: dict[str, object]) -> None:
+ pytest.importorskip("aiohttp")
+
+ class _FakeApiApp:
+ def __init__(self) -> None:
+ self.on_startup: list[object] = []
+ self.on_cleanup: list[object] = []
+
+ class _FakeAgentLoop:
+ def __init__(self, **kwargs) -> None:
+ seen["workspace"] = kwargs["workspace"]
+
+ async def _connect_mcp(self) -> None:
+ return None
+
+ async def close_mcp(self) -> None:
+ return None
+
+ def _fake_create_app(agent_loop, model_name: str, request_timeout: float):
+ seen["agent_loop"] = agent_loop
+ seen["model_name"] = model_name
+ seen["request_timeout"] = request_timeout
+ return _FakeApiApp()
+
+ def _fake_run_app(api_app, host: str, port: int, print):
+ seen["api_app"] = api_app
+ seen["host"] = host
+ seen["port"] = port
+
+ _patch_cli_command_runtime(
+ monkeypatch,
+ config,
+ message_bus=lambda: object(),
+ session_manager=lambda _workspace: object(),
+ )
+ monkeypatch.setattr("nanobot.agent.loop.AgentLoop", _FakeAgentLoop)
+ monkeypatch.setattr("nanobot.api.server.create_app", _fake_create_app)
+ monkeypatch.setattr("aiohttp.web.run_app", _fake_run_app)
+
+
+def test_gateway_uses_workspace_from_config_by_default(monkeypatch, tmp_path: Path) -> None:
+ config_file = _write_instance_config(tmp_path)
+ config = Config()
+ config.agents.defaults.workspace = str(tmp_path / "config-workspace")
+ seen: dict[str, Path] = {}
+
+ _patch_cli_command_runtime(
+ monkeypatch,
+ config,
+ set_config_path=lambda path: seen.__setitem__("config_path", path),
+ sync_templates=lambda path: seen.__setitem__("workspace", path),
+ make_provider=_stop_gateway_provider,
)
result = runner.invoke(app, ["gateway", "--config", str(config_file)])
@@ -673,24 +751,17 @@ def test_gateway_uses_workspace_from_config_by_default(monkeypatch, tmp_path: Pa
def test_gateway_workspace_option_overrides_config(monkeypatch, tmp_path: Path) -> None:
- config_file = tmp_path / "instance" / "config.json"
- config_file.parent.mkdir(parents=True)
- config_file.write_text("{}")
-
+ config_file = _write_instance_config(tmp_path)
config = Config()
config.agents.defaults.workspace = str(tmp_path / "config-workspace")
override = tmp_path / "override-workspace"
seen: dict[str, Path] = {}
- monkeypatch.setattr("nanobot.config.loader.set_config_path", lambda _path: None)
- monkeypatch.setattr("nanobot.config.loader.load_config", lambda _path=None: config)
- monkeypatch.setattr(
- "nanobot.cli.commands.sync_workspace_templates",
- lambda path: seen.__setitem__("workspace", path),
- )
- monkeypatch.setattr(
- "nanobot.cli.commands._make_provider",
- lambda _config: (_ for _ in ()).throw(_StopGatewayError("stop")),
+ _patch_cli_command_runtime(
+ monkeypatch,
+ config,
+ sync_templates=lambda path: seen.__setitem__("workspace", path),
+ make_provider=_stop_gateway_provider,
)
result = runner.invoke(
@@ -704,27 +775,23 @@ def test_gateway_workspace_option_overrides_config(monkeypatch, tmp_path: Path)
def test_gateway_uses_workspace_directory_for_cron_store(monkeypatch, tmp_path: Path) -> None:
- config_file = tmp_path / "instance" / "config.json"
- config_file.parent.mkdir(parents=True)
- config_file.write_text("{}")
-
+ config_file = _write_instance_config(tmp_path)
config = Config()
config.agents.defaults.workspace = str(tmp_path / "config-workspace")
seen: dict[str, Path] = {}
- monkeypatch.setattr("nanobot.config.loader.set_config_path", lambda _path: None)
- monkeypatch.setattr("nanobot.config.loader.load_config", lambda _path=None: config)
- monkeypatch.setattr("nanobot.cli.commands.sync_workspace_templates", lambda _path: None)
- monkeypatch.setattr("nanobot.cli.commands._make_provider", lambda _config: object())
- monkeypatch.setattr("nanobot.bus.queue.MessageBus", lambda: object())
- monkeypatch.setattr("nanobot.session.manager.SessionManager", lambda _workspace: object())
-
class _StopCron:
def __init__(self, store_path: Path) -> None:
seen["cron_store"] = store_path
raise _StopGatewayError("stop")
- monkeypatch.setattr("nanobot.cron.service.CronService", _StopCron)
+ _patch_cli_command_runtime(
+ monkeypatch,
+ config,
+ message_bus=lambda: object(),
+ session_manager=lambda _workspace: object(),
+ cron_service=_StopCron,
+ )
result = runner.invoke(app, ["gateway", "--config", str(config_file)])
@@ -735,10 +802,7 @@ def test_gateway_uses_workspace_directory_for_cron_store(monkeypatch, tmp_path:
def test_gateway_workspace_override_does_not_migrate_legacy_cron(
monkeypatch, tmp_path: Path
) -> None:
- config_file = tmp_path / "instance" / "config.json"
- config_file.parent.mkdir(parents=True)
- config_file.write_text("{}")
-
+ config_file = _write_instance_config(tmp_path)
legacy_dir = tmp_path / "global" / "cron"
legacy_dir.mkdir(parents=True)
legacy_file = legacy_dir / "jobs.json"
@@ -748,20 +812,19 @@ def test_gateway_workspace_override_does_not_migrate_legacy_cron(
config = Config()
seen: dict[str, Path] = {}
- monkeypatch.setattr("nanobot.config.loader.set_config_path", lambda _path: None)
- monkeypatch.setattr("nanobot.config.loader.load_config", lambda _path=None: config)
- monkeypatch.setattr("nanobot.cli.commands.sync_workspace_templates", lambda _path: None)
- monkeypatch.setattr("nanobot.cli.commands._make_provider", lambda _config: object())
- monkeypatch.setattr("nanobot.bus.queue.MessageBus", lambda: object())
- monkeypatch.setattr("nanobot.session.manager.SessionManager", lambda _workspace: object())
- monkeypatch.setattr("nanobot.config.paths.get_cron_dir", lambda: legacy_dir)
-
class _StopCron:
def __init__(self, store_path: Path) -> None:
seen["cron_store"] = store_path
raise _StopGatewayError("stop")
- monkeypatch.setattr("nanobot.cron.service.CronService", _StopCron)
+ _patch_cli_command_runtime(
+ monkeypatch,
+ config,
+ message_bus=lambda: object(),
+ session_manager=lambda _workspace: object(),
+ cron_service=_StopCron,
+ get_cron_dir=lambda: legacy_dir,
+ )
result = runner.invoke(
app,
@@ -777,10 +840,7 @@ def test_gateway_workspace_override_does_not_migrate_legacy_cron(
def test_gateway_custom_config_workspace_does_not_migrate_legacy_cron(
monkeypatch, tmp_path: Path
) -> None:
- config_file = tmp_path / "instance" / "config.json"
- config_file.parent.mkdir(parents=True)
- config_file.write_text("{}")
-
+ config_file = _write_instance_config(tmp_path)
legacy_dir = tmp_path / "global" / "cron"
legacy_dir.mkdir(parents=True)
legacy_file = legacy_dir / "jobs.json"
@@ -791,20 +851,19 @@ def test_gateway_custom_config_workspace_does_not_migrate_legacy_cron(
config.agents.defaults.workspace = str(custom_workspace)
seen: dict[str, Path] = {}
- monkeypatch.setattr("nanobot.config.loader.set_config_path", lambda _path: None)
- monkeypatch.setattr("nanobot.config.loader.load_config", lambda _path=None: config)
- monkeypatch.setattr("nanobot.cli.commands.sync_workspace_templates", lambda _path: None)
- monkeypatch.setattr("nanobot.cli.commands._make_provider", lambda _config: object())
- monkeypatch.setattr("nanobot.bus.queue.MessageBus", lambda: object())
- monkeypatch.setattr("nanobot.session.manager.SessionManager", lambda _workspace: object())
- monkeypatch.setattr("nanobot.config.paths.get_cron_dir", lambda: legacy_dir)
-
class _StopCron:
def __init__(self, store_path: Path) -> None:
seen["cron_store"] = store_path
raise _StopGatewayError("stop")
- monkeypatch.setattr("nanobot.cron.service.CronService", _StopCron)
+ _patch_cli_command_runtime(
+ monkeypatch,
+ config,
+ message_bus=lambda: object(),
+ session_manager=lambda _workspace: object(),
+ cron_service=_StopCron,
+ get_cron_dir=lambda: legacy_dir,
+ )
result = runner.invoke(app, ["gateway", "--config", str(config_file)])
@@ -856,19 +915,14 @@ def test_migrate_cron_store_skips_when_workspace_file_exists(tmp_path: Path) ->
def test_gateway_uses_configured_port_when_cli_flag_is_missing(monkeypatch, tmp_path: Path) -> None:
- config_file = tmp_path / "instance" / "config.json"
- config_file.parent.mkdir(parents=True)
- config_file.write_text("{}")
-
+ config_file = _write_instance_config(tmp_path)
config = Config()
config.gateway.port = 18791
- monkeypatch.setattr("nanobot.config.loader.set_config_path", lambda _path: None)
- monkeypatch.setattr("nanobot.config.loader.load_config", lambda _path=None: config)
- monkeypatch.setattr("nanobot.cli.commands.sync_workspace_templates", lambda _path: None)
- monkeypatch.setattr(
- "nanobot.cli.commands._make_provider",
- lambda _config: (_ for _ in ()).throw(_StopGatewayError("stop")),
+ _patch_cli_command_runtime(
+ monkeypatch,
+ config,
+ make_provider=_stop_gateway_provider,
)
result = runner.invoke(app, ["gateway", "--config", str(config_file)])
@@ -878,19 +932,14 @@ def test_gateway_uses_configured_port_when_cli_flag_is_missing(monkeypatch, tmp_
def test_gateway_cli_port_overrides_configured_port(monkeypatch, tmp_path: Path) -> None:
- config_file = tmp_path / "instance" / "config.json"
- config_file.parent.mkdir(parents=True)
- config_file.write_text("{}")
-
+ config_file = _write_instance_config(tmp_path)
config = Config()
config.gateway.port = 18791
- monkeypatch.setattr("nanobot.config.loader.set_config_path", lambda _path: None)
- monkeypatch.setattr("nanobot.config.loader.load_config", lambda _path=None: config)
- monkeypatch.setattr("nanobot.cli.commands.sync_workspace_templates", lambda _path: None)
- monkeypatch.setattr(
- "nanobot.cli.commands._make_provider",
- lambda _config: (_ for _ in ()).throw(_StopGatewayError("stop")),
+ _patch_cli_command_runtime(
+ monkeypatch,
+ config,
+ make_provider=_stop_gateway_provider,
)
result = runner.invoke(app, ["gateway", "--config", str(config_file), "--port", "18792"])
@@ -899,6 +948,63 @@ def test_gateway_cli_port_overrides_configured_port(monkeypatch, tmp_path: Path)
assert "port 18792" in result.stdout
+def test_serve_uses_api_config_defaults_and_workspace_override(
+ monkeypatch, tmp_path: Path
+) -> None:
+ config_file = _write_instance_config(tmp_path)
+ config = Config()
+ config.agents.defaults.workspace = str(tmp_path / "config-workspace")
+ config.api.host = "127.0.0.2"
+ config.api.port = 18900
+ config.api.timeout = 45.0
+ override_workspace = tmp_path / "override-workspace"
+ seen: dict[str, object] = {}
+
+ _patch_serve_runtime(monkeypatch, config, seen)
+
+ result = runner.invoke(
+ app,
+ ["serve", "--config", str(config_file), "--workspace", str(override_workspace)],
+ )
+
+ assert result.exit_code == 0
+ assert seen["workspace"] == override_workspace
+ assert seen["host"] == "127.0.0.2"
+ assert seen["port"] == 18900
+ assert seen["request_timeout"] == 45.0
+
+
+def test_serve_cli_options_override_api_config(monkeypatch, tmp_path: Path) -> None:
+ config_file = _write_instance_config(tmp_path)
+ config = Config()
+ config.api.host = "127.0.0.2"
+ config.api.port = 18900
+ config.api.timeout = 45.0
+ seen: dict[str, object] = {}
+
+ _patch_serve_runtime(monkeypatch, config, seen)
+
+ result = runner.invoke(
+ app,
+ [
+ "serve",
+ "--config",
+ str(config_file),
+ "--host",
+ "127.0.0.1",
+ "--port",
+ "18901",
+ "--timeout",
+ "46",
+ ],
+ )
+
+ assert result.exit_code == 0
+ assert seen["host"] == "127.0.0.1"
+ assert seen["port"] == 18901
+ assert seen["request_timeout"] == 46.0
+
+
def test_channels_login_requires_channel_name() -> None:
result = runner.invoke(app, ["channels", "login"])