- agent_runtime: orchestrator、core/memory/schemas 调整 - agent_monitoring API、service、agent_llm_log 模型与 database 注册 - 前端 AgentDashboard、AgentConfig、Agents/MainLayout/路由与 AgentChat - 文档:(红头)项目核心文档汇总、自主AI Agent改造完成情况、AI agent改造计划 Made-with: Cursor
75 lines
2.6 KiB
Python
75 lines
2.6 KiB
Python
"""
|
||
Agent 监控 API — 提供 Agent 专属统计数据
|
||
"""
|
||
from fastapi import APIRouter, Depends, Query
|
||
from sqlalchemy.orm import Session
|
||
from typing import Optional
|
||
from app.core.database import get_db
|
||
from app.api.auth import get_current_user
|
||
from app.models.user import User
|
||
from app.services.agent_monitoring_service import AgentMonitoringService
|
||
|
||
router = APIRouter(
|
||
prefix="/api/v1/agent-monitoring",
|
||
tags=["agent-monitoring"],
|
||
responses={
|
||
401: {"description": "未授权"},
|
||
403: {"description": "无权访问"},
|
||
},
|
||
)
|
||
|
||
|
||
@router.get("/overview")
|
||
async def get_agent_overview(
|
||
db: Session = Depends(get_db),
|
||
current_user: User = Depends(get_current_user),
|
||
):
|
||
"""Agent 概览统计:Agent 数、对话次数、LLM 调用次数、Token 用量、工具调用次数。"""
|
||
user_id = None if current_user.role == "admin" else current_user.id
|
||
return AgentMonitoringService.get_overview(db, user_id)
|
||
|
||
|
||
@router.get("/llm-calls")
|
||
async def get_llm_calls(
|
||
days: int = Query(7, ge=1, le=30, description="统计天数"),
|
||
limit: int = Query(50, ge=1, le=200, description="返回条数"),
|
||
db: Session = Depends(get_db),
|
||
current_user: User = Depends(get_current_user),
|
||
):
|
||
"""最近 LLM 调用记录列表。"""
|
||
user_id = None if current_user.role == "admin" else current_user.id
|
||
return AgentMonitoringService.get_llm_calls(db, user_id, days, limit)
|
||
|
||
|
||
@router.get("/agents-stats")
|
||
async def get_agent_stats(
|
||
days: int = Query(7, ge=1, le=30, description="统计天数"),
|
||
db: Session = Depends(get_db),
|
||
current_user: User = Depends(get_current_user),
|
||
):
|
||
"""各 Agent 用量统计(按 Agent 分组)。"""
|
||
user_id = None if current_user.role == "admin" else current_user.id
|
||
return AgentMonitoringService.get_agent_stats(db, user_id, days)
|
||
|
||
|
||
@router.get("/tool-usage")
|
||
async def get_tool_usage(
|
||
days: int = Query(7, ge=1, le=30, description="统计天数"),
|
||
db: Session = Depends(get_db),
|
||
current_user: User = Depends(get_current_user),
|
||
):
|
||
"""工具调用频次统计。"""
|
||
user_id = None if current_user.role == "admin" else current_user.id
|
||
return AgentMonitoringService.get_tool_usage(db, user_id, days)
|
||
|
||
|
||
@router.get("/daily-trend")
|
||
async def get_daily_trend(
|
||
days: int = Query(7, ge=1, le=30, description="统计天数"),
|
||
db: Session = Depends(get_db),
|
||
current_user: User = Depends(get_current_user),
|
||
):
|
||
"""每日 LLM 调用趋势。"""
|
||
user_id = None if current_user.role == "admin" else current_user.id
|
||
return AgentMonitoringService.get_daily_trend(db, user_id, days)
|