""" 子图工具 - 通讯录、词典、新闻分析等 """ from langchain_core.tools import tool from backend.app.logger import info async def _call_subgraph(builder_fn, state_cls, query: str) -> str: """通用子图调用""" try: graph = builder_fn().compile() state = state_cls(user_query=query) result = await graph.ainvoke(state) return result.get("final_result", "执行完成") except Exception as e: info(f"[Tool] 子图调用失败: {e}") return f"执行失败: {str(e)}" @tool async def contact_lookup(query: str) -> str: """查询通讯录""" from backend.app.subgraphs.contact.graph import build_contact_subgraph from backend.app.subgraphs.contact.state import ContactState return await _call_subgraph(build_contact_subgraph, ContactState, query) @tool async def dictionary_lookup(word: str) -> str: """查询词典/翻译""" from backend.app.subgraphs.dictionary.graph import build_dictionary_subgraph from backend.app.subgraphs.dictionary.state import DictionaryState return await _call_subgraph(build_dictionary_subgraph, DictionaryState, word) @tool async def news_analysis(topic: str) -> str: """分析新闻热点""" from backend.app.subgraphs.news_analysis.graph import build_news_analysis_subgraph from backend.app.subgraphs.news_analysis.state import NewsAnalysisState return await _call_subgraph(build_news_analysis_subgraph, NewsAnalysisState, topic)