Skip to content

Your lightweight open-source personal agent.

Merge origin/main into webui-settings

Xubin Ren65b0ae81afd0
diff --cc nanobot/cli/commands.py
index 088433055,e1b317ed1..22bb8b825
--- a/nanobot/cli/commands.py
+++ b/nanobot/cli/commands.py
@@@ -596,7 -657,8 +597,9 @@@ def _run_gateway
) -> None:
"""Shared gateway runtime; ``open_browser_url`` opens a tab once channels are up."""
from nanobot.agent.loop import AgentLoop
+ from nanobot.agent.runtime import build_agent_runtime, load_agent_runtime
+ from nanobot.agent.tools.cron import CronTool
+ from nanobot.agent.tools.message import MessageTool
from nanobot.bus.queue import MessageBus
from nanobot.channels.manager import ChannelManager
from nanobot.cron.service import CronService
@@@ -647,11 -704,50 +650,52 @@@
unified_session=config.agents.defaults.unified_session,
disabled_skills=config.agents.defaults.disabled_skills,
session_ttl_minutes=config.agents.defaults.session_ttl_minutes,
+ consolidation_ratio=config.agents.defaults.consolidation_ratio,
tools_config=config.tools,
+ runtime_loader=load_agent_runtime,
+ runtime_signature=runtime.signature,
)
+ from nanobot.agent.loop import UNIFIED_SESSION_KEY
+ from nanobot.bus.events import OutboundMessage
+
+ def _channel_session_key(channel: str, chat_id: str) -> str:
+ return (
+ UNIFIED_SESSION_KEY
+ if config.agents.defaults.unified_session
+ else f"{channel}:{chat_id}"
+ )
+
+ async def _deliver_to_channel(msg: OutboundMessage, *, record: bool = False) -> None:
+ """Publish a user-visible message and mirror it into that channel's session."""
+ metadata = dict(msg.metadata or {})
+ record = record or bool(metadata.pop("_record_channel_delivery", False))
+ if metadata != (msg.metadata or {}):
+ msg = OutboundMessage(
+ channel=msg.channel,
+ chat_id=msg.chat_id,
+ content=msg.content,
+ reply_to=msg.reply_to,
+ media=msg.media,
+ metadata=metadata,
+ buttons=msg.buttons,
+ )
+ if (
+ record
+ and msg.channel != "cli"
+ and msg.content.strip()
+ and hasattr(session_manager, "get_or_create")
+ and hasattr(session_manager, "save")
+ ):
+ session = session_manager.get_or_create(_channel_session_key(msg.channel, msg.chat_id))
+ session.add_message("assistant", msg.content, _channel_delivery=True)
+ session_manager.save(session)
+ await bus.publish_outbound(msg)
+
+ message_tool = getattr(agent, "tools", {}).get("message")
+ if isinstance(message_tool, MessageTool):
+ message_tool.set_send_callback(_deliver_to_channel)
+
# Set cron callback (needs agent)
async def on_cron_job(job: CronJob) -> str | None:
"""Execute a cron job through the agent."""
diff --cc tests/cli/test_commands.py
index 2719beed1,403f53fb3..8c86b32ac
--- a/tests/cli/test_commands.py
+++ b/tests/cli/test_commands.py
@@@ -8,6 -8,6 +8,7 @@@ from unittest.mock import AsyncMock, Ma
import pytest
from typer.testing import CliRunner
++from nanobot.agent.runtime import AgentRuntime
from nanobot.bus.events import OutboundMessage
from nanobot.cli.commands import _make_provider, app
from nanobot.config.schema import Config
@@@ -776,6 -776,6 +777,15 @@@ def _stop_gateway_provider(_config) ->
raise _StopGatewayError("stop")
++def _test_agent_runtime(provider: object, config: Config) -> AgentRuntime:
++ return AgentRuntime(
++ provider=provider,
++ model=config.agents.defaults.model,
++ context_window_tokens=config.agents.defaults.context_window_tokens,
++ signature=("test",),
++ )
++
++
def _patch_cli_command_runtime(
monkeypatch,
config: Config,
@@@ -788,6 -788,6 +798,8 @@@
cron_service=None,
get_cron_dir=None,
) -> None:
++ provider_factory = make_provider or (lambda _config: object())
++
monkeypatch.setattr(
"nanobot.config.loader.set_config_path",
set_config_path or (lambda _path: None),
@@@ -800,7 -800,7 +812,15 @@@
)
monkeypatch.setattr(
"nanobot.cli.commands._make_provider",
-- make_provider or (lambda _config: object()),
++ provider_factory,
++ )
++ monkeypatch.setattr(
++ "nanobot.agent.runtime.build_agent_runtime",
++ lambda _config: _test_agent_runtime(provider_factory(_config), _config),
++ )
++ monkeypatch.setattr(
++ "nanobot.agent.runtime.load_agent_runtime",
++ lambda _config_path=None: _test_agent_runtime(provider_factory(config), config),
)
if message_bus is not None:
@@@ -941,8 -941,28 +961,36 @@@ def test_gateway_cron_evaluator_receive
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: provider)
++ monkeypatch.setattr(
++ "nanobot.agent.runtime.build_agent_runtime",
++ lambda _config: _test_agent_runtime(provider, _config),
++ )
++ monkeypatch.setattr(
++ "nanobot.agent.runtime.load_agent_runtime",
++ lambda _config_path=None: _test_agent_runtime(provider, config),
++ )
monkeypatch.setattr("nanobot.bus.queue.MessageBus", lambda: bus)
- monkeypatch.setattr("nanobot.session.manager.SessionManager", lambda _workspace: object())
+
+ class _FakeSession:
+ def __init__(self) -> None:
+ self.messages = []
+
+ def add_message(self, role: str, content: str, **kwargs) -> None:
+ self.messages.append({"role": role, "content": content, **kwargs})
+
+ class _FakeSessionManager:
+ def __init__(self, _workspace: Path) -> None:
+ self.session = _FakeSession()
+ seen["session_manager"] = self
+
+ def get_or_create(self, key: str) -> _FakeSession:
+ seen["session_key"] = key
+ return self.session
+
+ def save(self, session: _FakeSession) -> None:
+ seen["saved_session"] = session
+
+ monkeypatch.setattr("nanobot.session.manager.SessionManager", _FakeSessionManager)
class _FakeCron:
def __init__(self, _store_path: Path) -> None:
@@@ -1052,6 -1082,6 +1110,14 @@@ def test_gateway_cron_job_suppresses_in
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.agent.runtime.build_agent_runtime",
++ lambda _config: _test_agent_runtime(object(), _config),
++ )
++ monkeypatch.setattr(
++ "nanobot.agent.runtime.load_agent_runtime",
++ lambda _config_path=None: _test_agent_runtime(object(), config),
++ )
monkeypatch.setattr("nanobot.bus.queue.MessageBus", lambda: bus)
monkeypatch.setattr("nanobot.session.manager.SessionManager", lambda _workspace: object())