All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 5m38s
- 添加MCP Manager统一入口管理 - 实现Contact/Dictionary/News三个适配器 - 三层降级策略:MCP -> Database -> Mock - 保持原有api_client向后兼容 - 添加完整文档和测试
122 lines
3.8 KiB
Python
122 lines
3.8 KiB
Python
"""
|
||
通讯录子图 API 调用工具(使用MCP统一接口)
|
||
"""
|
||
from typing import Dict, Any, Optional, List
|
||
from datetime import datetime
|
||
from dataclasses import dataclass
|
||
|
||
from .state import Contact, Email
|
||
from ...mcp.mcp_manager import mcp_manager
|
||
from ...mcp.adapters import ContactAdapter
|
||
|
||
|
||
@dataclass
|
||
class ContactAPIClient:
|
||
"""
|
||
通讯录 API 客户端 - 使用MCP统一接口
|
||
|
||
保持向后兼容,内部使用MCP适配器
|
||
"""
|
||
|
||
def __init__(self, conn=None):
|
||
"""
|
||
初始化
|
||
|
||
Args:
|
||
conn: 数据库连接(保留用于向后兼容)
|
||
"""
|
||
self.conn = conn
|
||
|
||
# 确保MCP已初始化
|
||
import asyncio
|
||
try:
|
||
asyncio.create_task(self._init_mcp())
|
||
except RuntimeError:
|
||
pass # 没有事件循环时跳过,延迟初始化
|
||
|
||
async def _init_mcp(self):
|
||
"""初始化MCP系统"""
|
||
if not mcp_manager.get_adapter("contact"):
|
||
# 获取repository(如果有)
|
||
repo = None
|
||
if self.conn:
|
||
try:
|
||
from ...db.models import ContactRepository
|
||
repo = ContactRepository(self.conn)
|
||
except Exception:
|
||
pass
|
||
|
||
mcp_manager.register_adapter(ContactAdapter(contact_repo=repo))
|
||
await mcp_manager.initialize()
|
||
|
||
async def list_contacts(self, user_id: str = "default") -> List[Contact]:
|
||
"""获取联系人列表"""
|
||
await self._init_mcp()
|
||
result = await mcp_manager.execute("contact", "list_contacts", user_id=user_id)
|
||
if result.success:
|
||
return result.data
|
||
return []
|
||
|
||
async def add_contact(self, user_id: str, contact: Contact) -> bool:
|
||
"""添加联系人"""
|
||
await self._init_mcp()
|
||
result = await mcp_manager.execute(
|
||
"contact", "add_contact",
|
||
user_id=user_id, contact=contact
|
||
)
|
||
return result.success and result.data
|
||
|
||
async def list_emails(self, user_id: str = "default") -> List[Email]:
|
||
"""查询邮件列表"""
|
||
await self._init_mcp()
|
||
result = await mcp_manager.execute("contact", "list_emails", user_id=user_id)
|
||
if result.success:
|
||
return result.data
|
||
return []
|
||
|
||
async def generate_email_draft(self, query: str) -> Dict[str, str]:
|
||
"""生成邮件草稿"""
|
||
await self._init_mcp()
|
||
result = await mcp_manager.execute(
|
||
"contact", "generate_email_draft", query=query
|
||
)
|
||
if result.success:
|
||
return result.data
|
||
return {
|
||
"subject": f"Re: {query}",
|
||
"recipient": "recipient@example.com",
|
||
"body": "你好,\n\n这是一封自动生成的邮件草稿。"
|
||
}
|
||
|
||
async def send_email(self, user_id: str, recipient: str, subject: str, body: str) -> bool:
|
||
"""发送邮件"""
|
||
await self._init_mcp()
|
||
result = await mcp_manager.execute(
|
||
"contact", "send_email",
|
||
user_id=user_id, recipient=recipient, subject=subject, body=body
|
||
)
|
||
return result.success
|
||
|
||
async def sniff_contacts(self, query: str) -> List[Contact]:
|
||
"""智能嗅探联系人"""
|
||
await self._init_mcp()
|
||
result = await mcp_manager.execute(
|
||
"contact", "sniff_contacts", query=query
|
||
)
|
||
if result.success:
|
||
return result.data
|
||
return []
|
||
|
||
# 保持向后兼容的旧方法
|
||
def list_contacts_mock(self, user_id: str = "default") -> List[Contact]:
|
||
"""模拟查询(保留用于向后兼容)"""
|
||
import asyncio
|
||
try:
|
||
return asyncio.run(self.list_contacts(user_id))
|
||
except Exception:
|
||
return []
|
||
|
||
|
||
# 全局单例(保持向后兼容)
|
||
contact_api = ContactAPIClient()
|