hermes-agent/tools/feishu_doc_tool.py
liujinkun 85cdb04bd4 feat: add Feishu document comment intelligent reply with 3-tier access control
- Full comment handler: parse drive.notice.comment_add_v1 events, build
  timeline, run agent, deliver reply with chunking support.
- 5 tools: feishu_doc_read, feishu_drive_list_comments,
  feishu_drive_list_comment_replies, feishu_drive_reply_comment,
  feishu_drive_add_comment.
- 3-tier access control rules (exact doc > wildcard "*" > top-level >
  defaults) with per-field fallback. Config via
  ~/.hermes/feishu_comment_rules.json, mtime-cached hot-reload.
- Self-reply filter using generalized self_open_id (supports future
  user-identity subscriptions). Receiver check: only process events
  where the bot is the @mentioned target.
- Smart timeline selection, long text chunking, semantic text extraction,
  session sharing per document, wiki link resolution.

Change-Id: I31e82fd6355173dbcc400b8934b6d9799e3137b9
2026-04-17 19:04:11 -07:00

136 lines
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 asyncio
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():
try:
import lark_oapi # noqa: F401
return True
except ImportError:
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()
)
try:
response = asyncio.get_event_loop().run_until_complete(
asyncio.to_thread(client.request, request)
)
except RuntimeError:
# No running event loop -- call synchronously
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",
)