2026-01-15 16:04:19 +08:00
|
|
|
|
import os
|
2025-08-28 13:16:19 +08:00
|
|
|
|
import traceback
|
2026-01-15 16:04:19 +08:00
|
|
|
|
import uuid
|
2025-05-23 15:30:14 +08:00
|
|
|
|
from typing import Annotated, Any
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2026-01-15 16:04:19 +08:00
|
|
|
|
import requests
|
2025-10-23 01:39:01 +08:00
|
|
|
|
from langchain.tools import tool
|
2025-10-27 15:32:12 +08:00
|
|
|
|
from langgraph.types import interrupt
|
2025-05-23 15:30:14 +08:00
|
|
|
|
|
2026-03-02 21:00:40 +08:00
|
|
|
|
from src import config, graph_base
|
|
|
|
|
|
from src.agents.common.toolkits.kbs import get_kb_based_tools
|
2026-01-15 16:04:19 +08:00
|
|
|
|
from src.services.mcp_service import get_enabled_mcp_tools
|
|
|
|
|
|
from src.storage.minio import aupload_file_to_minio
|
2025-07-02 02:58:13 +08:00
|
|
|
|
from src.utils import logger
|
2025-03-24 23:00:14 +08:00
|
|
|
|
|
2026-01-07 14:29:06 +08:00
|
|
|
|
# Lazy initialization for TavilySearch (only when TAVILY_API_KEY is available)
|
|
|
|
|
|
_tavily_search_instance = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_tavily_search():
|
|
|
|
|
|
"""Get TavilySearch instance lazily, only when API key is available."""
|
|
|
|
|
|
global _tavily_search_instance
|
|
|
|
|
|
if _tavily_search_instance is None and config.enable_web_search:
|
|
|
|
|
|
from langchain_tavily import TavilySearch
|
|
|
|
|
|
|
|
|
|
|
|
_tavily_search_instance = TavilySearch()
|
|
|
|
|
|
_tavily_search_instance.metadata = {"name": "Tavily 网页搜索"}
|
|
|
|
|
|
return _tavily_search_instance
|
2025-11-12 01:21:38 +08:00
|
|
|
|
|
2025-11-12 11:00:39 +08:00
|
|
|
|
|
2026-01-29 22:49:04 +08:00
|
|
|
|
@tool(name_or_callable="calculator", description="可以对给定的2个数字选择进行 add, subtract, multiply, divide 运算")
|
2025-11-05 02:04:34 +08:00
|
|
|
|
def calculator(a: float, b: float, operation: str) -> float:
|
|
|
|
|
|
try:
|
|
|
|
|
|
if operation == "add":
|
|
|
|
|
|
return a + b
|
|
|
|
|
|
elif operation == "subtract":
|
|
|
|
|
|
return a - b
|
|
|
|
|
|
elif operation == "multiply":
|
|
|
|
|
|
return a * b
|
|
|
|
|
|
elif operation == "divide":
|
|
|
|
|
|
if b == 0:
|
|
|
|
|
|
raise ZeroDivisionError("除数不能为零")
|
|
|
|
|
|
return a / b
|
|
|
|
|
|
else:
|
|
|
|
|
|
raise ValueError(f"不支持的运算类型: {operation},仅支持 add, subtract, multiply, divide")
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"Calculator error: {e}")
|
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-15 16:04:19 +08:00
|
|
|
|
@tool
|
|
|
|
|
|
async def text_to_img_demo(text: str) -> str:
|
|
|
|
|
|
"""【测试用】使用模型生成图片, 会返回图片的URL"""
|
|
|
|
|
|
|
|
|
|
|
|
url = "https://api.siliconflow.cn/v1/images/generations"
|
|
|
|
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
|
|
"model": "Qwen/Qwen-Image",
|
|
|
|
|
|
"prompt": text,
|
|
|
|
|
|
}
|
|
|
|
|
|
headers = {"Authorization": f"Bearer {os.getenv('SILICONFLOW_API_KEY')}", "Content-Type": "application/json"}
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
response = requests.post(url, json=payload, headers=headers)
|
|
|
|
|
|
response_json = response.json()
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"Failed to generate image with: {e}")
|
|
|
|
|
|
raise ValueError(f"Image generation failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
image_url = response_json["images"][0]["url"]
|
|
|
|
|
|
except (KeyError, IndexError, TypeError) as e:
|
|
|
|
|
|
logger.error(f"Failed to parse image URL from response: {e}, {response_json=}")
|
|
|
|
|
|
raise ValueError(f"Image URL extraction failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Upload to MinIO (Simplified)
|
|
|
|
|
|
response = requests.get(image_url)
|
|
|
|
|
|
file_data = response.content
|
|
|
|
|
|
|
|
|
|
|
|
file_name = f"{uuid.uuid4()}.jpg"
|
|
|
|
|
|
image_url = await aupload_file_to_minio(
|
|
|
|
|
|
bucket_name="generated-images", file_name=file_name, data=file_data, file_extension="jpg"
|
|
|
|
|
|
)
|
|
|
|
|
|
logger.info(f"Image uploaded. URL: {image_url}")
|
|
|
|
|
|
return image_url
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-29 22:49:04 +08:00
|
|
|
|
@tool(name_or_callable="human_in_the_loop_debug", description="请求人工审批工具,用于在执行重要操作前获得人类确认。")
|
2025-10-27 15:32:12 +08:00
|
|
|
|
def get_approved_user_goal(
|
|
|
|
|
|
operation_description: str,
|
2025-11-12 11:00:39 +08:00
|
|
|
|
) -> dict:
|
2025-10-27 15:32:12 +08:00
|
|
|
|
"""
|
|
|
|
|
|
请求人工审批,在执行重要操作前获得人类确认。
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
operation_description: 需要审批的操作描述,例如 "调用知识库工具"
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
dict: 包含审批结果的字典,格式为 {"approved": bool, "message": str}
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 构建详细的中断信息
|
|
|
|
|
|
interrupt_info = {
|
2025-11-06 19:47:22 +08:00
|
|
|
|
"question": "是否批准以下操作?",
|
2025-10-27 15:32:12 +08:00
|
|
|
|
"operation": operation_description,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# 触发人工审批
|
|
|
|
|
|
is_approved = interrupt(interrupt_info)
|
|
|
|
|
|
|
|
|
|
|
|
# 返回审批结果
|
|
|
|
|
|
if is_approved:
|
|
|
|
|
|
result = {
|
|
|
|
|
|
"approved": True,
|
|
|
|
|
|
"message": f"✅ 操作已批准:{operation_description}",
|
|
|
|
|
|
}
|
|
|
|
|
|
print(f"✅ 人工审批通过: {operation_description}")
|
|
|
|
|
|
else:
|
|
|
|
|
|
result = {
|
|
|
|
|
|
"approved": False,
|
|
|
|
|
|
"message": f"❌ 操作被拒绝:{operation_description}",
|
|
|
|
|
|
}
|
|
|
|
|
|
print(f"❌ 人工审批被拒绝: {operation_description}")
|
|
|
|
|
|
|
|
|
|
|
|
return result
|
2025-03-24 19:07:51 +08:00
|
|
|
|
|
2025-11-12 01:21:38 +08:00
|
|
|
|
|
|
|
|
|
|
KG_QUERY_DESCRIPTION = """
|
|
|
|
|
|
使用这个工具可以查询知识图谱中包含的三元组信息。
|
|
|
|
|
|
关键词(query),使用可能帮助回答这个问题的关键词进行查询,不要直接使用用户的原始输入去查询。
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
2025-11-12 11:00:39 +08:00
|
|
|
|
|
2025-11-12 01:21:38 +08:00
|
|
|
|
@tool(name_or_callable="查询知识图谱", description=KG_QUERY_DESCRIPTION)
|
2025-08-31 00:34:26 +08:00
|
|
|
|
def query_knowledge_graph(query: Annotated[str, "The keyword to query knowledge graph."]) -> Any:
|
2025-11-12 01:21:38 +08:00
|
|
|
|
"""使用这个工具可以查询知识图谱中包含的三元组信息。关键词(query),使用可能帮助回答这个问题的关键词进行查询,不要直接使用用户的原始输入去查询。"""
|
2025-08-31 00:34:26 +08:00
|
|
|
|
try:
|
|
|
|
|
|
logger.debug(f"Querying knowledge graph with: {query}")
|
2025-09-01 22:37:03 +08:00
|
|
|
|
result = graph_base.query_node(query, hops=2, return_format="triples")
|
|
|
|
|
|
logger.debug(
|
2025-09-02 01:08:42 +08:00
|
|
|
|
f"Knowledge graph query returned "
|
|
|
|
|
|
f"{len(result.get('triples', [])) if isinstance(result, dict) else 'N/A'} triples"
|
2025-09-01 22:37:03 +08:00
|
|
|
|
)
|
2025-08-31 00:34:26 +08:00
|
|
|
|
return result
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"Knowledge graph query error: {e}, {traceback.format_exc()}")
|
|
|
|
|
|
return f"知识图谱查询失败: {str(e)}"
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-09-01 03:38:46 +08:00
|
|
|
|
def gen_tool_info(tools) -> list[dict[str, Any]]:
|
2025-07-24 00:46:15 +08:00
|
|
|
|
"""获取所有工具的信息(用于前端展示)"""
|
2025-08-29 20:54:49 +08:00
|
|
|
|
tools_info = []
|
2025-07-24 00:46:15 +08:00
|
|
|
|
|
2025-08-24 20:12:15 +08:00
|
|
|
|
try:
|
|
|
|
|
|
# 获取注册的工具信息
|
2025-08-29 20:54:49 +08:00
|
|
|
|
for tool_obj in tools:
|
2025-08-24 20:12:15 +08:00
|
|
|
|
try:
|
2025-09-01 22:37:03 +08:00
|
|
|
|
metadata = getattr(tool_obj, "metadata", {}) or {}
|
2025-08-24 20:12:15 +08:00
|
|
|
|
info = {
|
2025-08-29 20:54:49 +08:00
|
|
|
|
"id": tool_obj.name,
|
2025-09-01 22:37:03 +08:00
|
|
|
|
"name": metadata.get("name", tool_obj.name),
|
2025-08-29 20:54:49 +08:00
|
|
|
|
"description": tool_obj.description,
|
2025-09-01 22:37:03 +08:00
|
|
|
|
"metadata": metadata,
|
2025-09-01 03:38:46 +08:00
|
|
|
|
"args": [],
|
|
|
|
|
|
# "is_async": is_async # Include async information
|
2025-08-24 20:12:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
if hasattr(tool_obj, "args_schema") and tool_obj.args_schema:
|
2025-10-25 22:49:20 +08:00
|
|
|
|
if isinstance(tool_obj.args_schema, dict):
|
|
|
|
|
|
schema = tool_obj.args_schema
|
|
|
|
|
|
else:
|
|
|
|
|
|
schema = tool_obj.args_schema.schema()
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
for arg_name, arg_info in schema.get("properties", {}).items():
|
|
|
|
|
|
info["args"].append(
|
|
|
|
|
|
{
|
|
|
|
|
|
"name": arg_name,
|
|
|
|
|
|
"type": arg_info.get("type", ""),
|
|
|
|
|
|
"description": arg_info.get("description", ""),
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
2025-08-29 20:54:49 +08:00
|
|
|
|
|
|
|
|
|
|
tools_info.append(info)
|
2025-08-31 00:34:26 +08:00
|
|
|
|
# logger.debug(f"Successfully processed tool info for {tool_obj.name}")
|
2025-07-24 00:46:15 +08:00
|
|
|
|
|
2025-08-24 20:12:15 +08:00
|
|
|
|
except Exception as e:
|
2025-09-01 22:37:03 +08:00
|
|
|
|
logger.error(
|
2025-10-25 22:49:20 +08:00
|
|
|
|
f"Failed to process tool {getattr(tool_obj, 'name', 'unknown')}: {e}\n{traceback.format_exc()}. "
|
|
|
|
|
|
f"Details: {dict(tool_obj.__dict__)}"
|
2025-09-01 22:37:03 +08:00
|
|
|
|
)
|
2025-08-24 20:12:15 +08:00
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-09-01 03:38:46 +08:00
|
|
|
|
logger.error(f"Failed to get tools info: {e}\n{traceback.format_exc()}")
|
|
|
|
|
|
return []
|
2025-08-24 20:12:15 +08:00
|
|
|
|
|
|
|
|
|
|
logger.info(f"Successfully extracted info for {len(tools_info)} tools")
|
|
|
|
|
|
return tools_info
|
2026-01-15 16:04:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_buildin_tools() -> list:
|
|
|
|
|
|
"""注册静态工具"""
|
|
|
|
|
|
static_tools = [
|
|
|
|
|
|
query_knowledge_graph,
|
|
|
|
|
|
get_approved_user_goal,
|
|
|
|
|
|
calculator,
|
|
|
|
|
|
text_to_img_demo,
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
# subagents 工具
|
|
|
|
|
|
from .subagents import calc_agent_tool
|
|
|
|
|
|
|
|
|
|
|
|
static_tools.append(calc_agent_tool)
|
|
|
|
|
|
|
|
|
|
|
|
# 检查是否启用网页搜索(即是否配置了 API_KEY)
|
|
|
|
|
|
if config.enable_web_search:
|
|
|
|
|
|
tavily_search = get_tavily_search()
|
|
|
|
|
|
if tavily_search:
|
|
|
|
|
|
static_tools.append(tavily_search)
|
|
|
|
|
|
|
|
|
|
|
|
return static_tools
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_tools_from_context(context, extra_tools=None) -> list:
|
|
|
|
|
|
"""从上下文配置中获取工具列表"""
|
|
|
|
|
|
# 1. 基础工具 (从 context.tools 中筛选)
|
|
|
|
|
|
all_basic_tools = get_buildin_tools() + (extra_tools or [])
|
|
|
|
|
|
selected_tools = []
|
|
|
|
|
|
|
|
|
|
|
|
if context.tools:
|
|
|
|
|
|
# 创建工具映射表
|
|
|
|
|
|
tools_map = {t.name: t for t in all_basic_tools}
|
|
|
|
|
|
for tool_name in context.tools:
|
|
|
|
|
|
if tool_name in tools_map:
|
|
|
|
|
|
selected_tools.append(tools_map[tool_name])
|
|
|
|
|
|
|
|
|
|
|
|
# 2. 知识库工具
|
|
|
|
|
|
if context.knowledges:
|
|
|
|
|
|
kb_tools = get_kb_based_tools(db_names=context.knowledges)
|
|
|
|
|
|
selected_tools.extend(kb_tools)
|
|
|
|
|
|
|
|
|
|
|
|
# 3. MCP 工具(使用统一入口,自动过滤 disabled_tools)
|
|
|
|
|
|
if context.mcps:
|
|
|
|
|
|
for server_name in context.mcps:
|
|
|
|
|
|
mcp_tools = await get_enabled_mcp_tools(server_name)
|
|
|
|
|
|
selected_tools.extend(mcp_tools)
|
|
|
|
|
|
|
|
|
|
|
|
return selected_tools
|