refactor(sessions)!: retire the global shared session (#6621)
Co-authored-by: chengyongru <chengyongru.ai@gmail.com>
This commit is contained in:
1 parent
28d73b6942
commit
e6b0aec2a8
77 files changed
+311
-1255
No files matched your search
@@ -44,12 +44,7 @@ from nanobot.config.schema import Config
|
||||
from nanobot.gateway.runtime import GatewayHealthIdentity, GatewayInstance, GatewayRuntime
|
||||
from nanobot.installation_lock import installation_access
|
||||
from nanobot.security.network import is_loopback_host
|
||||
from nanobot.session.keys import (
|
||||
HEARTBEAT_SESSION_KEY,
|
||||
UNIFIED_SESSION_KEY,
|
||||
last_channel_from_metadata,
|
||||
session_key_for_channel,
|
||||
)
|
||||
from nanobot.session.keys import HEARTBEAT_SESSION_KEY
|
||||
from nanobot.session.manager import SessionManager
|
||||
from nanobot.utils.evaluator import evaluate_response, resolve_evaluator_prompt
|
||||
from nanobot.utils.helpers import sync_workspace_templates
|
||||
@@ -73,7 +68,6 @@ async def _deliver_to_channel(
|
||||
*,
|
||||
bus: MessageBus,
|
||||
session_manager: SessionManager,
|
||||
unified_session: bool = False,
|
||||
record: bool = False,
|
||||
session_key: str | None = None,
|
||||
session_generation: str | None = None,
|
||||
@@ -89,9 +83,7 @@ async def _deliver_to_channel(
|
||||
)
|
||||
key = session_key or (raw_key if isinstance(raw_key, str) else None)
|
||||
if key is None:
|
||||
key = session_key_for_channel(
|
||||
msg.channel, msg.chat_id, unified_session=unified_session,
|
||||
)
|
||||
key = f"{msg.channel}:{msg.chat_id}"
|
||||
if msg.channel == "websocket":
|
||||
from nanobot.agent.turn_delivery import TurnRoute
|
||||
from nanobot.session.display_history import HISTORY_GENERATION_METADATA_KEY
|
||||
@@ -274,7 +266,6 @@ def _pick_heartbeat_target_from_sessions(
|
||||
enabled_channels: Iterable[str],
|
||||
sessions: Iterable[dict[str, Any]],
|
||||
archived_keys: Iterable[str],
|
||||
unified_session_metadata: dict[str, Any] | None = None,
|
||||
) -> tuple[str, str]:
|
||||
enabled = set(enabled_channels)
|
||||
archived = set(archived_keys)
|
||||
@@ -282,13 +273,6 @@ def _pick_heartbeat_target_from_sessions(
|
||||
key = item.get("key") or ""
|
||||
if key in archived:
|
||||
continue
|
||||
if key == UNIFIED_SESSION_KEY:
|
||||
route = last_channel_from_metadata(unified_session_metadata)
|
||||
if route is not None:
|
||||
channel, chat_id = route
|
||||
if channel not in {"cli", "system"} and channel in enabled:
|
||||
return channel, chat_id
|
||||
continue
|
||||
if ":" not in key:
|
||||
continue
|
||||
channel, chat_id = key.split(":", 1)
|
||||
@@ -591,7 +575,6 @@ def _run_claimed_gateway(
|
||||
recovery = RecoveryCoordinator(
|
||||
sessions=session_manager,
|
||||
bus=bus,
|
||||
unified_session=config.agents.defaults.unified_session,
|
||||
)
|
||||
|
||||
from nanobot.goal.assembly import install_goal
|
||||
@@ -624,18 +607,10 @@ def _run_claimed_gateway(
|
||||
schedule_background=_schedule_webui_background,
|
||||
recovery=recovery,
|
||||
)
|
||||
def _channel_session_key(channel: str, chat_id: str) -> str:
|
||||
return session_key_for_channel(
|
||||
channel,
|
||||
chat_id,
|
||||
unified_session=config.agents.defaults.unified_session,
|
||||
)
|
||||
|
||||
deliver_to_channel = partial(
|
||||
_deliver_to_channel,
|
||||
bus=bus,
|
||||
session_manager=session_manager,
|
||||
unified_session=config.agents.defaults.unified_session,
|
||||
)
|
||||
|
||||
message_tool = agent.tools.get("message")
|
||||
@@ -719,7 +694,7 @@ def _run_claimed_gateway(
|
||||
channel, chat_id = await _pick_heartbeat_target()
|
||||
if channel == "cli":
|
||||
return None
|
||||
delivery_key = _channel_session_key(channel, chat_id)
|
||||
delivery_key = f"{channel}:{chat_id}"
|
||||
delivery_session = await session_manager.state.read(delivery_key)
|
||||
if delivery_session is None:
|
||||
return None
|
||||
@@ -835,16 +810,10 @@ def _run_claimed_gateway(
|
||||
|
||||
async def _pick_heartbeat_target() -> tuple[str, str]:
|
||||
archived_keys = await ensure_webui_session_archives(session_manager.state)
|
||||
unified_metadata = None
|
||||
if config.agents.defaults.unified_session:
|
||||
record = await session_manager.state.read_metadata(UNIFIED_SESSION_KEY)
|
||||
if isinstance(record, dict) and isinstance(record.get("metadata"), dict):
|
||||
unified_metadata = record["metadata"]
|
||||
return _pick_heartbeat_target_from_sessions(
|
||||
enabled_channels=channels.enabled_channels,
|
||||
sessions=await session_manager.state.list_sessions(),
|
||||
archived_keys=archived_keys,
|
||||
unified_session_metadata=unified_metadata,
|
||||
)
|
||||
|
||||
if channels.enabled_channels:
|
||||
|
||||
Reference in new issue
Block a user