Files
aiagent/backend/app/api/agent_monitoring.py
renjianbo 036f533881 feat: Agent 监控与编排、仪表盘/配置页及文档更新
- 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
2026-05-01 19:32:59 +08:00

75 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
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)