refactor(api)!: remove the standalone API server (#6614)

This commit is contained in:
Xubin Ren authored and GitHub committed 2026-11-10 10:46:14 +08:00
1 parent 5c72ced2fe
commit 220d5e6970
99 files changed
+240 -4133

No files matched your search

@@ -38,7 +38,5 @@
"network.proxy",
"tools.web.use_proxy",
"tools.web.user_agent",
"tools.image_generation.save_dir",
"api.host",
"api.timeout"
"tools.image_generation.save_dir"
]
+16
View File
@@ -229,6 +229,11 @@ async def test_gateway_webui_bootstrap_message_and_thread_hydration(tmp_path: Pa
gateway_port=gateway_port,
)
legacy_config = json.loads(config_path.read_text(encoding="utf-8"))
legacy_config["api"] = {"host": "0.0.0.0", "apiKey": "${RETIRED_SMOKE_TOKEN}"}
config_path.write_text(json.dumps(legacy_config), encoding="utf-8")
before = config_path.read_bytes()
process = _start_gateway(config_path, log_path)
base_url = f"http://127.0.0.1:{ws_port}"
try:
@@ -240,6 +245,17 @@ async def test_gateway_webui_bootstrap_message_and_thread_hydration(tmp_path: Pa
ready = await _recv_until(ws, "ready")
assert ready["client_id"] == "smoke"
# An old WebUI can still connect, but cannot start the retired listener.
await ws.send(json.dumps({
"type": "webui_request", "request_id": "old-api-start",
"action": "settings.api_service.start",
"payload": {"host": "127.0.0.1", "port": _free_port(), "timeout": 120},
}))
rejected = await _recv_until(ws, "webui_response")
assert rejected["request_id"] == "old-api-start"
assert rejected["ok"] is False and rejected["error"]["status"] == 404
assert config_path.read_bytes() == before
await ws.send(json.dumps({"type": "new_chat"}))
attached = await _recv_until(ws, "attached")
chat_id = attached["chat_id"]
-54
View File
@@ -34,7 +34,6 @@ from nanobot.webui.settings_api import (
settings_payload,
settings_usage_payload,
update_agent_settings,
update_api_settings,
update_model_call_order,
update_model_configuration,
update_network_safety_settings,
@@ -169,7 +168,6 @@ def test_settings_payload_includes_relocated_capabilities(
) -> None:
config_path = tmp_path / "config.json"
config = Config()
config.api.port = 9910
save_config(config, config_path)
monkeypatch.setattr("nanobot.config.loader._current_config_path", config_path)
monkeypatch.setenv("LANGFUSE_SECRET_KEY", "secret")
@@ -177,8 +175,6 @@ def test_settings_payload_includes_relocated_capabilities(
payload = settings_payload()
assert payload["api"]["port"] == 9910
assert payload["api"]["api_key_hint"] is None
assert payload["observability"]["provider"] == "langfuse"
assert payload["observability"]["configured"] is True
@@ -198,56 +194,6 @@ def test_settings_payload_exposes_modelscope_image_model(
assert providers["modelscope"]["default_model"] == "Qwen/Qwen-Image-2512"
def test_update_api_settings_requires_key_for_network_access(
tmp_path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
config_path = tmp_path / "config.json"
save_config(Config(), config_path)
monkeypatch.setattr("nanobot.config.loader._current_config_path", config_path)
with pytest.raises(WebUISettingsError, match="API key"):
update_api_settings({"host": ["0.0.0.0"], "port": ["8900"]})
payload = update_api_settings(
{
"host": ["0.0.0.0"],
"port": ["9900"],
"api_key": ["secret-token"],
}
)
saved = load_config(config_path)
assert saved.api.host == "0.0.0.0"
assert saved.api.port == 9900
assert saved.api.api_key == "secret-token"
assert payload["api"]["api_key_hint"]
def test_update_api_settings_requires_key_for_specific_network_interface(
tmp_path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
config_path = tmp_path / "config.json"
save_config(Config(), config_path)
monkeypatch.setattr("nanobot.config.loader._current_config_path", config_path)
with pytest.raises(WebUISettingsError, match="API key"):
update_api_settings({"host": ["192.168.1.10"], "port": ["8900"]})
def test_update_api_settings_allows_alternate_loopback_without_key(
tmp_path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
config_path = tmp_path / "config.json"
save_config(Config(), config_path)
monkeypatch.setattr("nanobot.config.loader._current_config_path", config_path)
update_api_settings({"host": ["127.0.0.2"], "port": ["8900"]})
assert load_config(config_path).api.host == "127.0.0.2"
def _dynamic_provider_config(
*,
api_base: str = DYNAMIC_PROVIDER_API_BASE,
@@ -7,7 +7,6 @@ import pytest
from nanobot.config.schema import Config
from nanobot.webui.settings_capabilities import (
capability_settings_payload,
update_api_settings,
update_image_generation_settings,
update_network_safety_settings,
update_transcription_settings,
@@ -75,10 +74,6 @@ def test_capability_domain_updates_representative_settings() -> None:
"use_jina_reader": ["false"],
},
)
update_api_settings(
config,
{"host": ["127.0.0.2"], "port": ["8900"], "timeout": ["90"]},
)
image_changed = update_image_generation_settings(
config,
{"enabled": ["true"], "provider": ["openrouter"]},
@@ -102,7 +97,5 @@ def test_capability_domain_updates_representative_settings() -> None:
assert transcription_changed is True
assert (network_changed, access_mode) == (True, "default")
assert payload["web_search"]["max_results"] == 7
assert payload["api"]["host"] == "127.0.0.2"
assert payload["api"]["port"] == 8900
assert payload["image_generation"]["enabled"] is True
assert payload["transcription"]["provider"] == "openrouter"
+5 -8
View File
@@ -974,19 +974,16 @@ async def test_runtime_config_route_authentication_validation_and_restart(tmp_pa
assert json.loads(refreshed.body)["restart_required_sections"] == ["runtime"]
async def test_runtime_settings_only_save_without_refreshing_or_rebinding(tmp_path, monkeypatch):
async def test_runtime_settings_only_save_without_refreshing_or_rebinding(tmp_path):
refresh = MagicMock()
runtime = MagicMock()
monkeypatch.setattr(WebUISettingsRouter, "_api_runtime", lambda _self: runtime)
router = _router(config_path=tmp_path / "config.json", refresh_runtime_config=refresh)
path = "/api/settings/runtime-config/update"
response = await router.dispatch(None, _mutation_request(path, {
"values": {"agents.defaults.dream.enabled": False, "api.timeout": 45},
"values": {"agents.defaults.dream.enabled": False, "gateway.port": 19001},
}), path)
assert response.status_code == 200
assert json.loads(response.body)["requires_restart"]
refresh.assert_not_called()
runtime.restart.assert_not_called()
async def test_reverting_runtime_switch_clears_restart_but_preserves_other_changes(tmp_path):
@@ -1015,7 +1012,7 @@ async def test_reverting_web_reader_preserves_unrelated_restart_reason(tmp_path)
router = _router(config_path=tmp_path / "config.json")
config = router.settings.config.load()
original_reader = config.tools.web.fetch.use_jina_reader
original_timeout = config.api.timeout
original_port = config.gateway.port
path = "/api/settings/web-search/update"
first = await router.dispatch(None, _mutation_request(path, {
"provider": "duckduckgo", "use_jina_reader": not original_reader,
@@ -1025,7 +1022,7 @@ async def test_reverting_web_reader_preserves_unrelated_restart_reason(tmp_path)
runtime_path = "/api/settings/runtime-config/update"
changed = await router.dispatch(None, _mutation_request(runtime_path, {
"values": {"api.timeout": original_timeout + 1},
"values": {"gateway.port": original_port + 1},
}), runtime_path)
assert changed.status_code == 200
assert set(json.loads(changed.body)["restart_required_sections"]) == {"browser", "runtime"}
@@ -1038,7 +1035,7 @@ async def test_reverting_web_reader_preserves_unrelated_restart_reason(tmp_path)
refreshed = await router.dispatch(None, SimpleNamespace(path="/api/settings", headers=Headers()), "/api/settings")
assert json.loads(refreshed.body)["restart_required_sections"] == ["runtime"]
reverted = await router.dispatch(None, _mutation_request(runtime_path, {
"values": {"api.timeout": original_timeout},
"values": {"gateway.port": original_port},
}), runtime_path)
assert reverted.status_code == 200
assert json.loads(reverted.body)["restart_required_sections"] == []
-10
View File
@@ -121,7 +121,6 @@ def test_runtime_patch_persists_and_preserves_other_domains(tmp_path):
assert saved.channels.telegram == {"token": "keep-me", "streaming": False}
assert payload["requires_restart"]
assert payload["runtime_config"]["tools.image_generation.save_dir"] == "images"
assert "api.api_key" not in runtime_config_payload(saved)
assert "PRIVATE_API_KEY" not in json.dumps(runtime_config_payload(saved))
@@ -153,8 +152,6 @@ def test_runtime_patch_persists_and_preserves_other_domains(tmp_path):
{"agents.defaults.timezone_mode": "manual", "agents.defaults.timezone": "Mars/Olympus"},
{"gateway.port": 65536},
{"gateway.heartbeat.interval_s": 0},
{"api.host": "192.168.1.2"},
{"api.timeout": float("nan")},
{"tools.image_generation.save_dir": ""},
{"tools.image_generation.save_dir": "../outside"},
{"tools.image_generation.save_dir": "C:/outside"},
@@ -230,13 +227,6 @@ def test_timezone_auto_and_nullable_fields():
assert not update_runtime_config(config, {}, local_browser=True)
def test_api_changes_require_explicit_restart(tmp_path):
path = tmp_path / "config.json"
payload = update_runtime_config_settings({"api.timeout": 222.5}, config_path=path)
assert payload["api"]["timeout"] == 222.5
assert payload["requires_restart"]
def test_runtime_payload_matches_shared_catalogue_contract():
fixture = Path(__file__).with_name("fixtures") / "runtime-config-keys.json"
paths = json.loads(fixture.read_text(encoding="utf-8"))
+21 -17
View File
@@ -11,7 +11,11 @@ from nanobot.channels.websocket.runtime import WebSocketConfig
from nanobot.config.loader import load_config, save_config
from nanobot.config.schema import Config
from nanobot.webui.gateway_services import build_gateway_services
from nanobot.webui.settings_api import settings_payload, update_agent_settings, update_api_settings
from nanobot.webui.settings_api import (
settings_payload,
update_agent_settings,
update_runtime_config_settings,
)
from nanobot.webui.settings_services import (
WebUIOAuthFlowRegistry,
WebUISettingsServices,
@@ -56,9 +60,9 @@ def test_gateway_settings_services_isolate_config_paths_and_oauth_flows(
first_path = tmp_path / "first" / "config.json"
second_path = tmp_path / "second" / "config.json"
first_config = Config()
first_config.api.host = "127.0.0.2"
first_config.gateway.host = "127.0.0.2"
second_config = Config()
second_config.api.host = "127.0.0.3"
second_config.gateway.host = "127.0.0.3"
save_config(first_config, first_path)
save_config(second_config, second_path)
@@ -69,14 +73,14 @@ def test_gateway_settings_services_isolate_config_paths_and_oauth_flows(
assert second.settings.config.path == second_path.resolve()
assert first.http.settings_routes.settings is first.settings
assert second.http.settings_routes.settings is second.settings
assert first.settings.config.load().api.host == "127.0.0.2"
assert second.settings.config.load().api.host == "127.0.0.3"
assert first.settings.read(settings_payload)["api"]["host"] == "127.0.0.2"
assert second.settings.read(settings_payload)["api"]["host"] == "127.0.0.3"
assert first.settings.config.load().gateway.host == "127.0.0.2"
assert second.settings.config.load().gateway.host == "127.0.0.3"
assert first.settings.read(settings_payload)["runtime_config"]["gateway.host"] == "127.0.0.2"
assert second.settings.read(settings_payload)["runtime_config"]["gateway.host"] == "127.0.0.3"
first.settings.mutate(update_api_settings, {"port": ["19001"]})
assert load_config(first_path).api.port == 19001
assert load_config(second_path).api.port != 19001
first.settings.mutate(update_runtime_config_settings, {"gateway.port": 19001})
assert load_config(first_path).gateway.port == 19001
assert load_config(second_path).gateway.port != 19001
first_flow = _Flow()
second_flow = _Flow()
@@ -94,9 +98,9 @@ def test_settings_service_supports_a_new_config_directory(tmp_path: Path) -> Non
config_path = tmp_path / "new" / "nested" / "config.json"
services = WebUISettingsServices.create(config_path)
services.mutate(update_api_settings, {"port": ["19001"]})
services.mutate(update_runtime_config_settings, {"gateway.port": 19001})
assert load_config(config_path).api.port == 19001
assert load_config(config_path).gateway.port == 19001
def test_settings_mutations_serialize_read_modify_write(
@@ -137,7 +141,7 @@ def test_settings_mutations_serialize_read_modify_write(
def run_second() -> None:
try:
second_started.set()
services.mutate(update_api_settings, {"host": ["127.0.0.9"]})
services.mutate(update_runtime_config_settings, {"gateway.host": "127.0.0.9"})
except BaseException as exc: # noqa: BLE001 - re-raised in the test thread
errors.append(exc)
@@ -157,7 +161,7 @@ def test_settings_mutations_serialize_read_modify_write(
assert not errors
saved = load_config(config_path)
assert saved.agents.defaults.timezone == "Asia/Tokyo"
assert saved.api.host == "127.0.0.9"
assert saved.gateway.host == "127.0.0.9"
def test_distinct_gateways_serialize_mutations_for_the_same_config(
@@ -192,7 +196,7 @@ def test_distinct_gateways_serialize_mutations_for_the_same_config(
def mutate(
services: WebUISettingsServices,
operation: Callable[..., object],
query: dict[str, list[str]],
query: dict[str, str],
) -> None:
try:
services.mutate(operation, query)
@@ -206,7 +210,7 @@ def test_distinct_gateways_serialize_mutations_for_the_same_config(
)
second = threading.Thread(
target=mutate,
args=(second_services, update_api_settings, {"host": ["127.0.0.9"]}),
args=(second_services, update_runtime_config_settings, {"gateway.host": "127.0.0.9"}),
name="gateway-second",
)
first.start()
@@ -222,7 +226,7 @@ def test_distinct_gateways_serialize_mutations_for_the_same_config(
assert not errors
saved = load_config(config_path)
assert saved.agents.defaults.timezone == "Asia/Tokyo"
assert saved.api.host == "127.0.0.9"
assert saved.gateway.host == "127.0.0.9"
def test_oauth_registry_preserves_expiry_capacity_completion_and_cancel() -> None: