Browse Source

fix: tools fall back to load_gateway_config() in fresh cron sessions (no token in scope)

Liv Klumpp 2 weeks ago
parent
commit
3e2cd10840
1 changed files with 39 additions and 7 deletions
  1. 39 7
      adapter.py

+ 39 - 7
adapter.py

@@ -3608,6 +3608,34 @@ CHATTO_SEND_ROOM_SCHEMA = {
 }
 
 
+def _chatto_config_for_tools() -> "ChattoConfiguration | None":
+    """Resolve the Chatto configuration for plugin tool handlers.
+
+    Three tiers, most specific first:
+    1. ``_resolved_chatto_config`` — set by ``hermes_validate_config`` at
+       gateway load; the authoritative snapshot in a running gateway.
+    2. ``load_gateway_config()`` — re-parse of config.yaml + .env seeds,
+       available in fresh cron/standalone sessions where the gateway load
+       hooks never ran. Mirrors how the built-in send_message tool resolves
+       its pconfig (tools/send_message_tool.py).
+    3. ``None`` — nothing configured; the caller reports the error.
+    """
+    global _resolved_chatto_config
+    if _resolved_chatto_config is not None:
+        return _resolved_chatto_config
+    try:
+        from gateway.config import load_gateway_config
+
+        config = load_gateway_config()
+        pconfig = config.platforms.get(ChattoConstants.PLATFORM_NAME)
+        if pconfig is not None:
+            _resolved_chatto_config = ChattoConfiguration(pconfig=pconfig)
+            return _resolved_chatto_config
+    except Exception as exc:
+        logger.warning("Chatto tools: gateway config fallback failed: %s", exc)
+    return None
+
+
 async def _handle_chatto_send_dm(args: dict) -> dict:
     """Tool handler for chatto_send_dm: resolve user, get/create DM room, send message."""
     user_login = args.get("user_login") or ""
@@ -3624,17 +3652,19 @@ async def _handle_chatto_send_dm(args: dict) -> dict:
     explicit_base_url = args.get("base_url") or ""
     explicit_token = args.get("token") or ""
 
+    cfg = _chatto_config_for_tools()
+
     if explicit_token:
         token = explicit_token
-    elif _resolved_chatto_config is not None:
-        token = _resolved_chatto_config.token.value or ""
+    elif cfg is not None:
+        token = cfg.token.value or ""
     else:
         token = ""
 
     if explicit_base_url:
         base_url = explicit_base_url
-    elif _resolved_chatto_config is not None:
-        base_url = _resolved_chatto_config.base_url.value or "https://chat.chatto.run"
+    elif cfg is not None:
+        base_url = cfg.base_url.value or "https://chat.chatto.run"
     else:
         base_url = "https://chat.chatto.run"
     if not token:
@@ -3750,16 +3780,18 @@ async def _handle_chatto_send_room(args: dict) -> dict:
     # --- Read config: explicit args first, then fall back to resolved config ---
     explicit_token = args.get("token") or ""
 
+    cfg = _chatto_config_for_tools()
+
     if explicit_token:
         token = explicit_token
-    elif _resolved_chatto_config is not None:
-        token = _resolved_chatto_config.token.value or ""
+    elif cfg is not None:
+        token = cfg.token.value or ""
     else:
         token = ""
 
     base_url = args.get("base_url") or ""
     if not base_url:
-        base_url = _resolved_chatto_config.base_url.value or "https://chat.chatto.run" if _resolved_chatto_config is not None else "https://chat.chatto.run"
+        base_url = cfg.base_url.value or "https://chat.chatto.run" if cfg is not None else "https://chat.chatto.run"
     if not token:
         return json.dumps({"success": False, "error": "Chatto token not configured"})