2026-01-15 16:04:19 +08:00
|
|
|
from dataclasses import dataclass, field
|
|
|
|
|
from typing import Annotated
|
|
|
|
|
|
2025-10-25 22:49:20 +08:00
|
|
|
from langchain.agents import create_agent
|
|
|
|
|
|
2026-01-15 16:04:19 +08:00
|
|
|
from src.agents.common import BaseAgent, BaseContext, load_chat_model
|
2026-01-22 06:54:14 +08:00
|
|
|
from src.agents.common.middlewares import (
|
|
|
|
|
RuntimeConfigMiddleware,
|
|
|
|
|
)
|
2025-10-25 22:49:20 +08:00
|
|
|
from src.agents.common.toolkits.mysql import get_mysql_tools
|
2026-01-22 06:54:14 +08:00
|
|
|
from src.agents.common.tools import gen_tool_info, get_buildin_tools
|
|
|
|
|
from src.services.mcp_service import get_tools_from_all_servers
|
2025-10-25 22:49:20 +08:00
|
|
|
from src.utils import logger
|
|
|
|
|
|
2025-11-01 21:34:16 +08:00
|
|
|
|
2026-01-15 16:04:19 +08:00
|
|
|
@dataclass(kw_only=True)
|
|
|
|
|
class ReporterContext(BaseContext):
|
|
|
|
|
# 覆盖默认的工具列表,添加 MySQL 工具包
|
|
|
|
|
tools: Annotated[list[dict], {"__template_metadata__": {"kind": "tools"}}] = field(
|
|
|
|
|
default_factory=lambda: [t.name for t in get_mysql_tools()],
|
|
|
|
|
metadata={
|
|
|
|
|
"name": "工具",
|
|
|
|
|
# 添加额外的 MySQL 工具包选项
|
|
|
|
|
"options": lambda: gen_tool_info(get_buildin_tools() + get_mysql_tools()),
|
|
|
|
|
"description": "包含内置的工具,以及用于数据库报表生成的 MySQL 工具包。",
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def __post_init__(self):
|
|
|
|
|
self.mcps = ["mcp-server-chart"] # 默认启用 Charts MCPs
|
|
|
|
|
|
|
|
|
|
|
2025-10-25 22:49:20 +08:00
|
|
|
class SqlReporterAgent(BaseAgent):
|
2025-11-02 01:15:36 +08:00
|
|
|
name = "数据库报表助手"
|
2025-10-25 22:49:20 +08:00
|
|
|
description = "一个能够生成 SQL 查询报告的智能体助手。同时调用 Charts MCP 生成图表。"
|
2026-01-15 16:04:19 +08:00
|
|
|
context_schema = ReporterContext
|
2025-10-25 22:49:20 +08:00
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
|
|
async def get_graph(self, **kwargs):
|
2026-01-22 06:54:14 +08:00
|
|
|
"""构建图"""
|
2025-12-30 19:19:49 +08:00
|
|
|
context = self.context_schema.from_file(module_name=self.module_name)
|
2026-01-22 06:54:14 +08:00
|
|
|
all_mcp_tools = await get_tools_from_all_servers()
|
|
|
|
|
# 合并 MySQL 工具和 MCP 工具
|
|
|
|
|
extra_tools = get_mysql_tools() + all_mcp_tools
|
2025-12-30 19:19:49 +08:00
|
|
|
|
2025-10-25 22:49:20 +08:00
|
|
|
graph = create_agent(
|
2026-01-22 06:54:14 +08:00
|
|
|
model=load_chat_model(context.model),
|
2025-12-30 19:19:49 +08:00
|
|
|
system_prompt=context.system_prompt,
|
2026-01-22 06:54:14 +08:00
|
|
|
middleware=[
|
|
|
|
|
RuntimeConfigMiddleware(extra_tools=extra_tools),
|
|
|
|
|
],
|
2025-10-25 22:49:20 +08:00
|
|
|
checkpointer=await self._get_checkpointer(),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
logger.info("SqlReporterAgent 构建成功")
|
2025-10-31 14:16:07 +08:00
|
|
|
return graph
|