
    `gj                         d Z ddlZddlZddlZddlmZmZmZ  ej                  e	      Z
 ej                         Zd Zd ZdZddd	d
dddid
gddZd ZdedefdZ ej*                  ddeeeg ddd	       y)zFeishu 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.
    N)registry
tool_errortool_resultc                     | t         _        y)zFStore a lark client for the current thread (called by feishu_comment).N)_localclient)r   s    H/root/.hermes/venv/lib/python3.12/site-packages/tools/feishu_doc_tool.py
set_clientr
      s	    FM    c                  $    t        t        dd      S )z7Return the lark client for the current thread, or None.r   N)getattrr    r   r	   
get_clientr      s    68T**r   z5/open-apis/docx/v1/documents/:document_id/raw_contentfeishu_doc_readzRead the full content of a Feishu/Lark document as plain text. Useful when you need more context beyond the quoted text in a comment.object	doc_tokenstringz>The document token (from the document URL or comment context).)typedescription)r   
propertiesrequired)namer   
parametersc                  p    dd l } 	 | j                  j                  d      d uS # t        t        f$ r Y yw xY w)Nr   	lark_oapiF)importlib.utilutil	find_specImportError
ValueError)	importlibs    r	   _check_feishur"   6   s=     ~~''4D@@$ s   # 55argsreturnc                 "   | j                  dd      j                         }|st        d      S t               }|t        d      S 	 ddlm} ddlm} ddlm	} |j                         j                  |j                        j                  t              j!                  |j"                  h      j%                  d
|i      j'                         }|j)                  |      }t+        |dd       }	|	dk7  rt+        |dd      }
t        d|	 d|
       S t+        |dd       }|r[t-        |d      rO	 t/        j0                  |j2                        }|j                  di       j                  dd      }t5        d|      S t+        |dd       }|rFt;        |t<              r|j                  dd      }nt+        |dt?        |            }t5        d|      S t        d      S # t        $ r t        d	      cY S w xY w# t.        j6                  t8        f$ r Y w xY w)Nr    zdoc_token is requiredz=Feishu client not available (not in a Feishu comment context)r   )AccessTokenType)
HttpMethod)BaseRequestzlark_oapi not installeddocument_idcodemsgzunknown errorzFailed to read document: code=z msg=rawcontentdataT)successr.   z%No content returned from document API) getstripr   r   r   r'   lark_oapi.core.enumr(   !lark_oapi.core.model.base_requestr)   r   builderhttp_methodGETuri_RAW_CONTENT_URItoken_typesTENANTpathsbuildrequestr   hasattrjsonloadsr.   r   JSONDecodeErrorAttributeError
isinstancedictstr)r#   kwargsr   r   r'   r(   r)   r>   responser+   r,   r-   bodyr.   r/   s                  r	   _handle_feishu_doc_readrJ   E   s   b)//1I122\F~YZZ5-2A
 		Z^^	$			o,,-	.	y)	*	  ~~g&H8VT*Dqyh7:4&cUKLL
(E4
(C
wsI&	::ckk*Dhhvr*..y"=GtW==
 8VT*DdD!hhy"-GdIs4y9G499=>>O  534456 $$n5 		s%   G *AG2 G/.G/2HH
feishu_docFzRead Feishu document contentu   📄)	r   toolsetschemahandlercheck_fnrequires_envis_asyncr   emoji)__doc__r@   logging	threadingtools.registryr   r   r   	getLogger__name__loggerlocalr   r
   r   r9   FEISHU_DOC_READ_SCHEMAr"   rE   rF   rJ   registerr   r   r	   <module>r]      s       < <			8	$ 
	
+ K  	Q  _
 !M	 &4?$ 4?S 4?v   	!#.

r   