92 lines
2.9 KiB
Python
92 lines
2.9 KiB
Python
"""Monitor 业务工具 - 将效果追踪服务注册为 FunctionTool"""
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from agentkit.tools.function_tool import FunctionTool
|
|
from agentkit.tools.registry import ToolRegistry
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def monitor_check_and_compare(record_id: str) -> dict:
|
|
"""检测并对比监测记录的变化"""
|
|
import uuid
|
|
from app.database import AsyncSessionLocal
|
|
from app.services.monitoring.monitor_service import MonitorService
|
|
|
|
async with AsyncSessionLocal() as db:
|
|
service = MonitorService()
|
|
updated_record = await service.check_and_compare(db, uuid.UUID(record_id))
|
|
if updated_record:
|
|
return {
|
|
"id": str(updated_record.id),
|
|
"change_type": updated_record.change_type,
|
|
"updated": True,
|
|
}
|
|
return {"id": record_id, "updated": False}
|
|
|
|
|
|
async def monitor_generate_report(record_id: str) -> dict:
|
|
"""生成监测变化报告"""
|
|
import uuid
|
|
from app.database import AsyncSessionLocal
|
|
from app.services.monitoring.monitor_service import MonitorService
|
|
|
|
async with AsyncSessionLocal() as db:
|
|
service = MonitorService()
|
|
report = await service.generate_change_report(db, uuid.UUID(record_id))
|
|
return {"report": report} if report else {"report": None}
|
|
|
|
|
|
async def monitor_create_record(
|
|
brand_id: str,
|
|
query_keywords: str | None = None,
|
|
platform: str | None = None,
|
|
check_interval_hours: int = 24,
|
|
) -> dict:
|
|
"""创建监测记录"""
|
|
import uuid
|
|
from app.database import AsyncSessionLocal
|
|
from app.services.monitoring.monitor_service import MonitorService
|
|
|
|
async with AsyncSessionLocal() as db:
|
|
service = MonitorService()
|
|
record = await service.create_monitoring_record(
|
|
db=db,
|
|
brand_id=uuid.UUID(brand_id),
|
|
query_keywords=query_keywords,
|
|
platform=platform,
|
|
check_interval_hours=check_interval_hours,
|
|
)
|
|
return {"id": str(record.id), "status": record.status}
|
|
|
|
|
|
def register_monitor_tools(registry: ToolRegistry) -> None:
|
|
"""注册所有效果追踪相关工具"""
|
|
registry.register(
|
|
FunctionTool(
|
|
name="monitor_check_and_compare",
|
|
description="检测并对比监测记录的变化",
|
|
func=monitor_check_and_compare,
|
|
tags=["monitor", "tracking"],
|
|
)
|
|
)
|
|
registry.register(
|
|
FunctionTool(
|
|
name="monitor_generate_report",
|
|
description="生成监测变化报告",
|
|
func=monitor_generate_report,
|
|
tags=["monitor", "report"],
|
|
)
|
|
)
|
|
registry.register(
|
|
FunctionTool(
|
|
name="monitor_create_record",
|
|
description="创建新的监测记录",
|
|
func=monitor_create_record,
|
|
tags=["monitor", "record"],
|
|
)
|
|
)
|
|
logger.info("Monitor tools registered")
|