feat(network): centralize proxy settings and add MCP routing (NAN-315) (#6372)

* feat(network): share proxy settings across features and MCP (NAN-315)

* fix(network): address shared proxy review regressions

* test(network): align rebased OAuth fixtures with runtime admission

* fix(webui): configure shared proxy without leaving connection drafts

* feat(webui): add optional shared proxy authentication editor

* fix(webui): align proxy save tests and loading skeleton
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-22 15:49:27 +08:00
1 parent 872a83340d
commit 084b6bb394
181 files changed
+3740 -807

No files matched your search

+1 -1
View File
@@ -22,7 +22,7 @@ SETUP_SPEC = ChannelSetupSpec(
"groupAllowFrom": field("list"),
"allowRoomMentions": field("bool", default=False),
"streaming": field("bool", default=False),
"proxy": field(),
"useProxy": field("bool", default=False),
},
required=(
*required_fields("homeserver", "userId"),
+8 -6
View File
@@ -63,6 +63,7 @@ from nanobot.bus.events import OutboundMessage
from nanobot.bus.outbound_events import ProgressEvent
from nanobot.bus.queue import MessageBus
from nanobot.channels.base import BaseChannel
from nanobot.config.network import NetworkConfig
from nanobot.config.paths import get_data_dir, get_media_dir
from nanobot.config_base import Base
from nanobot.events import ContextCompactionEvent
@@ -303,7 +304,7 @@ class MatrixConfig(Base):
group_allow_from: list[str] = Field(default_factory=list)
allow_room_mentions: bool = False
streaming: bool = False
proxy: str | None = None
use_proxy: bool = False
class MatrixChannel(BaseChannel):
@@ -311,6 +312,7 @@ class MatrixChannel(BaseChannel):
name = "matrix"
display_name = "Matrix"
uses_shared_proxy = True
_STREAM_EDIT_INTERVAL = 2 # min seconds between edit_message_text calls
monotonic_time: Callable[[], float] = staticmethod(time.monotonic)
@@ -323,12 +325,15 @@ class MatrixChannel(BaseChannel):
config: Any,
bus: MessageBus,
*,
network: NetworkConfig | None = None,
restrict_to_workspace: bool = False,
workspace: str | Path | None = None,
):
if isinstance(config, dict):
config = MatrixConfig.model_validate(config)
super().__init__(config, bus)
super().__init__(config, bus, network=network)
self.config: MatrixConfig = config
self._proxy = self.network.resolve_proxy(self.config.use_proxy)
self.client: AsyncClient | None = None
self._sync_task: asyncio.Task[None] | None = None
self._typing_tasks: dict[str, asyncio.Task[None]] = {}
@@ -359,14 +364,11 @@ class MatrixChannel(BaseChannel):
# Replace ':' with '_' to produce a Windows-safe filename
safe_store_name = self.config.user_id.replace(":", "_") + f"_{self.config.device_id}.db"
proxy = self.config.proxy.strip() if self.config.proxy else ""
if proxy and "://" not in proxy:
proxy = f"http://{proxy}"
self.client = AsyncClient(
homeserver=self.config.homeserver,
user=self.config.user_id,
store_path=str(self.store_path),
proxy=proxy or None,
proxy=self._proxy,
config=AsyncClientConfig(
store_sync_tokens=True,
encryption_enabled=self.config.e2ee_enabled,
@@ -25,6 +25,7 @@ from nanobot.channels.matrix.runtime import (
MatrixConfig,
_build_matrix_text_content,
)
from nanobot.config.network import NetworkConfig
_ROOM_SEND_UNSET = object()
@@ -328,8 +329,9 @@ async def test_start_skips_load_store_when_device_id_missing(
)
channel = MatrixChannel(
_make_config(device_id="", e2ee_enabled=True, proxy="127.0.0.1:7890"),
_make_config(device_id="", e2ee_enabled=True, use_proxy=True),
MessageBus(),
network=NetworkConfig(proxy="127.0.0.1:7890"),
)
await channel.start()
+13 -2
View File
@@ -3,11 +3,22 @@
from typing import Any
from nanobot.channels.contracts import ChannelValidationContext
from nanobot.channels.validation import check, required_checks, status_from_checks, string_value
from nanobot.channels.validation import (
check,
required_checks,
resolve_proxy,
status_from_checks,
string_value,
)
def validate(values: dict[str, Any], _context: ChannelValidationContext) -> dict[str, Any]:
def validate(values: dict[str, Any], context: ChannelValidationContext) -> dict[str, Any]:
checks, missing = required_checks("matrix", values)
try:
resolve_proxy(values, context)
except ValueError as exc:
checks.append(check("network_proxy", "Network proxy", "fail", str(exc)))
return status_from_checks("matrix", checks, missing)
password = string_value(values.get("password"))
access_token = string_value(values.get("accessToken"))
device_id = string_value(values.get("deviceId"))
+1
View File
@@ -12,6 +12,7 @@ export default {
docsUrl: chatAppGuideUrl("matrix"),
fields: [
{ key: "channels.matrix.homeserver", section: "connection" },
{ key: "channels.matrix.useProxy", section: "connection", control: "switch" },
{ key: "channels.matrix.userId", section: "account" },
{ key: "channels.matrix.password", section: "credentials" },
{ key: "channels.matrix.accessToken", section: "credentials" },
@@ -74,9 +74,8 @@
"false": "Off"
}
},
"proxy": {
"label": "Network proxy",
"placeholder": "http://127.0.0.1:7890"
"useProxy": {
"label": "Use proxy"
}
}
}
@@ -74,9 +74,8 @@
"false": "Apagado"
}
},
"proxy": {
"label": "Proxy de red",
"placeholder": "http://127.0.0.1:7890"
"useProxy": {
"label": "Usar proxy"
}
}
}
@@ -74,9 +74,8 @@
"false": "Désactivé"
}
},
"proxy": {
"label": "Proxy réseau",
"placeholder": "http://127.0.0.1:7890"
"useProxy": {
"label": "Utiliser le proxy"
}
}
}
@@ -74,9 +74,8 @@
"false": "Mati"
}
},
"proxy": {
"label": "Proxy jaringan",
"placeholder": "http://127.0.0.1:7890"
"useProxy": {
"label": "Gunakan proxy"
}
}
}
@@ -74,9 +74,8 @@
"false": "オフ"
}
},
"proxy": {
"label": "ネットワークプロキシ",
"placeholder": "http://127.0.0.1:7890"
"useProxy": {
"label": "プロキシを使う"
}
}
}
@@ -74,9 +74,8 @@
"false": "끄기"
}
},
"proxy": {
"label": "네트워크 프록시",
"placeholder": "http://127.0.0.1:7890"
"useProxy": {
"label": "프록시 사용"
}
}
}
@@ -74,9 +74,8 @@
"false": "Desligado"
}
},
"proxy": {
"label": "Proxy de rede",
"placeholder": "http://127.0.0.1:7890"
"useProxy": {
"label": "Usar proxy"
}
}
}
@@ -74,9 +74,8 @@
"false": "Tắt"
}
},
"proxy": {
"label": "Proxy mạng",
"placeholder": "http://127.0.0.1:7890"
"useProxy": {
"label": "Dùng proxy"
}
}
}
@@ -74,9 +74,8 @@
"false": "关闭"
}
},
"proxy": {
"label": "网络代理",
"placeholder": "http://127.0.0.1:7890"
"useProxy": {
"label": "使用代理"
}
}
}
@@ -74,9 +74,8 @@
"false": "關閉"
}
},
"proxy": {
"label": "網路代理",
"placeholder": "http://127.0.0.1:7890"
"useProxy": {
"label": "使用代理"
}
}
}