hermes-agent/tools/feishu_doc_tool.py
Teknium 0ec052ca24
perf(cli): cut ~19s from 'hermes' cold start (skills cache + lazy Feishu + no Nous HTTP) (#22138)
Interactive `hermes` launch drops from ~21s to ~2.5s. Three independent
fixes, each targets a distinct hot spot in the banner / tool-registration
path that fires on every CLI invocation.

1. `get_external_skills_dirs()` in-process mtime cache (~10s saved)
   The function re-read + YAML-parsed the full ~/.hermes/config.yaml on
   every call. Banner build invokes it once per skill to resolve the
   category column, which on a 120-skill install meant ~120 reparses of
   a 15 KB config (~85 ms each). Added a
   `(config_path, mtime_ns) -> list[Path]` memo; stat() is ~2 us vs
   ~85 ms for the parse. Edits to config.yaml invalidate the cache on
   the next call via mtime.

2. Feishu availability probe uses `importlib.util.find_spec` (~5.2s saved)
   `tools/feishu_doc_tool.py::_check_feishu` and the identical helper in
   `feishu_drive_tool.py` were calling `import lark_oapi` purely to
   detect whether the SDK was installed. Executing the real import pulls
   in websockets + dispatcher + every v2 API model — ~5 seconds of work
   that fires at every tool-registry bootstrap. `find_spec` answers the
   same question ("is lark_oapi importable?") without executing the
   module. The actual tool handlers still do the real import on invoke,
   so runtime behavior is unchanged.

3. `_web_requires_env` no longer triggers Nous portal refresh (~800ms saved)
   `tools/web_tools.py::_web_requires_env` used
   `managed_nous_tools_enabled()` to gate four gateway env-var names in
   the returned list. The gate called `get_nous_auth_status()` ->
   `resolve_nous_runtime_credentials()` -> live HTTP POST to the portal
   on every tool-registry bootstrap. But the list is pure metadata — if
   the env var is set at runtime, the tool lights up; otherwise it
   doesn't. Including the four names unconditionally is harmless for
   unsubscribed users (vars just aren't set) and eliminates the sync
   HTTP round trip from startup.

Test:
- tests/agent/test_external_skills_dirs_cache.py (new, 6 cases):
  returns config'd dir, caches on second call (yaml_load patched to
  raise — never invoked), invalidates on mtime bump, empty when config
  missing, returned list is a defensive copy, per-HERMES_HOME cache key
  isolation.
- Existing tests/agent/test_external_skills.py and tests/tools/
  continue to pass modulo pre-existing flakes on main (test_delegate,
  test_send_message — unrelated, pass in isolation).

Measured: bare `hermes` (cold → REPL ready) 21,519ms -> 2,618ms on
Teknium's install (119 skills, 15 KB config.yaml, Nous auth logged in,
lark_oapi installed). 8x faster.
2026-05-08 16:39:32 -07:00

138 lines
4.4 KiB
Python

"""Feishu Document Tool -- read document content via Feishu/Lark API.
Provides ``feishu_doc_read`` for reading document content as plain text.
Uses the same lazy-import + BaseRequest pattern as feishu_comment.py.
"""
import json
import logging
import threading
from tools.registry import registry, tool_error, tool_result
logger = logging.getLogger(__name__)
# Thread-local storage for the lark client injected by feishu_comment handler.
_local = threading.local()
def set_client(client):
"""Store a lark client for the current thread (called by feishu_comment)."""
_local.client = client
def get_client():
"""Return the lark client for the current thread, or None."""
return getattr(_local, "client", None)
# ---------------------------------------------------------------------------
# feishu_doc_read
# ---------------------------------------------------------------------------
_RAW_CONTENT_URI = "/open-apis/docx/v1/documents/:document_id/raw_content"
FEISHU_DOC_READ_SCHEMA = {
"name": "feishu_doc_read",
"description": (
"Read the full content of a Feishu/Lark document as plain text. "
"Useful when you need more context beyond the quoted text in a comment."
),
"parameters": {
"type": "object",
"properties": {
"doc_token": {
"type": "string",
"description": "The document token (from the document URL or comment context).",
},
},
"required": ["doc_token"],
},
}
def _check_feishu():
# Use ``importlib.util.find_spec`` — it checks whether ``lark_oapi``
# is importable without actually executing its ``__init__``.
# Executing the real import here costs ~5 seconds (the SDK eagerly
# loads websockets, dispatcher, every api/v2 model) and this probe
# fires at every ``hermes`` startup during tool-availability
# evaluation. Correctness is preserved because the actual tool
# handler still does the real import when invoked.
import importlib.util
try:
return importlib.util.find_spec("lark_oapi") is not None
except (ImportError, ValueError):
return False
def _handle_feishu_doc_read(args: dict, **kwargs) -> str:
doc_token = args.get("doc_token", "").strip()
if not doc_token:
return tool_error("doc_token is required")
client = get_client()
if client is None:
return tool_error("Feishu client not available (not in a Feishu comment context)")
try:
from lark_oapi import AccessTokenType
from lark_oapi.core.enum import HttpMethod
from lark_oapi.core.model.base_request import BaseRequest
except ImportError:
return tool_error("lark_oapi not installed")
request = (
BaseRequest.builder()
.http_method(HttpMethod.GET)
.uri(_RAW_CONTENT_URI)
.token_types({AccessTokenType.TENANT})
.paths({"document_id": doc_token})
.build()
)
# Tool handlers run synchronously in a worker thread (no running event
# loop), so call the blocking lark client directly.
response = client.request(request)
code = getattr(response, "code", None)
if code != 0:
msg = getattr(response, "msg", "unknown error")
return tool_error(f"Failed to read document: code={code} msg={msg}")
raw = getattr(response, "raw", None)
if raw and hasattr(raw, "content"):
try:
body = json.loads(raw.content)
content = body.get("data", {}).get("content", "")
return tool_result(success=True, content=content)
except (json.JSONDecodeError, AttributeError):
pass
# Fallback: try response.data
data = getattr(response, "data", None)
if data:
if isinstance(data, dict):
content = data.get("content", "")
else:
content = getattr(data, "content", str(data))
return tool_result(success=True, content=content)
return tool_error("No content returned from document API")
# ---------------------------------------------------------------------------
# Registration
# ---------------------------------------------------------------------------
registry.register(
name="feishu_doc_read",
toolset="feishu_doc",
schema=FEISHU_DOC_READ_SCHEMA,
handler=_handle_feishu_doc_read,
check_fn=_check_feishu,
requires_env=[],
is_async=False,
description="Read Feishu document content",
emoji="\U0001f4c4",
)