forked from xiaohei/taiji-AI-PAD
更新信息查询
This commit is contained in:
@@ -3820,3 +3820,162 @@ async def update_user_profile(
|
||||
"company": getattr(user, "company", None),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
# ============= 用户资源信息综合查询 =============
|
||||
|
||||
@router.get("/resources/info", response_model=SuccessResponse)
|
||||
async def get_user_resources_info(
|
||||
principal: dict = Depends(require_auth),
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
获取用户的资源信息综合查询
|
||||
|
||||
返回:
|
||||
1. LiteLLM 密钥列表(解密后的完整密钥)
|
||||
2. 已部署的平台 Agent 列表(包含 IP 地址和访问信息)
|
||||
3. 已部署的自定义 Agent 列表(包含 IP 地址和访问信息)
|
||||
|
||||
注意:
|
||||
- LiteLLM 密钥用于调用 AI 模型(OpenAI 兼容格式)
|
||||
- Agent 信息包含实时状态和访问地址
|
||||
"""
|
||||
from app.litellm_client import get_litellm_client
|
||||
from config import settings
|
||||
|
||||
user_id = principal.get("user_id")
|
||||
|
||||
# ========== 1. 获取 LiteLLM 密钥 ==========
|
||||
litellm_keys = []
|
||||
try:
|
||||
# 查询用户的模型密钥
|
||||
result = await db.execute(
|
||||
select(TenantModelKey).where(
|
||||
and_(
|
||||
TenantModelKey.tenant_id == user_id,
|
||||
TenantModelKey.status == "active"
|
||||
)
|
||||
)
|
||||
)
|
||||
tenant_keys = result.scalars().all()
|
||||
|
||||
litellm_client = get_litellm_client()
|
||||
|
||||
for key in tenant_keys:
|
||||
try:
|
||||
# 解密密钥
|
||||
decrypted_key = litellm_client.decrypt_key(key.litellm_key_hash)
|
||||
litellm_keys.append({
|
||||
"modelName": key.model_name,
|
||||
"apiKey": decrypted_key,
|
||||
"apiBase": settings.litellm_url,
|
||||
"rpmLimit": key.rpm_limit,
|
||||
"tpmLimit": key.tpm_limit,
|
||||
"maxBudget": float(key.max_budget) if key.max_budget else None,
|
||||
"budgetDuration": key.budget_duration,
|
||||
"status": key.status,
|
||||
"createdAt": key.created_at.isoformat() if key.created_at else None,
|
||||
})
|
||||
except Exception as e:
|
||||
logger.warning(f"解密 LiteLLM 密钥失败: {e}")
|
||||
litellm_keys.append({
|
||||
"modelName": key.model_name,
|
||||
"apiKey": None,
|
||||
"apiBase": settings.litellm_url,
|
||||
"error": "密钥解密失败",
|
||||
"status": key.status,
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(f"查询 LiteLLM 密钥失败: {e}")
|
||||
|
||||
# ========== 2. 获取已部署的 Agent ==========
|
||||
platform_agents = []
|
||||
custom_agents = []
|
||||
|
||||
# 查询用户的活跃 Agent(未停止的)
|
||||
result = await db.execute(
|
||||
select(AgentBillingRecord).where(
|
||||
and_(
|
||||
AgentBillingRecord.user_id == user_id,
|
||||
AgentBillingRecord.end_time == None # 正在运行
|
||||
)
|
||||
)
|
||||
)
|
||||
billing_records = result.scalars().all()
|
||||
|
||||
# 尝试获取 Agent Manager 客户端
|
||||
agent_manager_available = False
|
||||
client = None
|
||||
try:
|
||||
from app.agent_manager_client import get_agent_manager_client, AgentManagerError
|
||||
client = get_agent_manager_client()
|
||||
agent_manager_available = True
|
||||
except Exception as e:
|
||||
logger.warning(f"Agent Manager 客户端不可用: {e}")
|
||||
|
||||
for record in billing_records:
|
||||
agent_info = {
|
||||
"name": record.agent_name,
|
||||
"template": record.agent_type,
|
||||
"templateName": record.template_name,
|
||||
"status": "unknown",
|
||||
"healthStatus": "unknown",
|
||||
"podIp": None,
|
||||
"accessUrl": None,
|
||||
"servicePort": None,
|
||||
"namespace": "ai-agents",
|
||||
"cpu": record.cpu_used,
|
||||
"memory": record.memory_used,
|
||||
"replicas": record.replicas,
|
||||
"startTime": record.start_time.isoformat() if record.start_time else None,
|
||||
"runningSeconds": int((datetime.utcnow() - record.start_time).total_seconds()) if record.start_time else 0,
|
||||
}
|
||||
|
||||
# 从 Agent Manager 获取详细状态
|
||||
if agent_manager_available and client:
|
||||
try:
|
||||
agent_status = await client.get_agent_status(record.agent_name)
|
||||
agent_info["status"] = agent_status.status
|
||||
agent_info["healthStatus"] = agent_status.health_status
|
||||
agent_info["podIp"] = agent_status.pod_ip
|
||||
agent_info["accessUrl"] = agent_status.access_url
|
||||
agent_info["servicePort"] = agent_status.service_port
|
||||
agent_info["namespace"] = agent_status.namespace
|
||||
agent_info["hostIp"] = agent_status.host_ip
|
||||
agent_info["nodeName"] = agent_status.node_name
|
||||
|
||||
# 端点信息
|
||||
if agent_status.endpoints:
|
||||
agent_info["endpoints"] = [
|
||||
{
|
||||
"name": ep.name,
|
||||
"port": ep.port,
|
||||
"protocol": ep.protocol,
|
||||
"targetPort": ep.target_port,
|
||||
}
|
||||
for ep in agent_status.endpoints
|
||||
]
|
||||
except Exception as e:
|
||||
logger.warning(f"获取 Agent {record.agent_name} 状态失败: {e}")
|
||||
|
||||
# 分类存储
|
||||
if record.is_platform_agent:
|
||||
platform_agents.append(agent_info)
|
||||
else:
|
||||
custom_agents.append(agent_info)
|
||||
|
||||
return SuccessResponse(
|
||||
data={
|
||||
"litellmKeys": litellm_keys,
|
||||
"litellmApiBase": settings.litellm_url,
|
||||
"platformAgents": platform_agents,
|
||||
"customAgents": custom_agents,
|
||||
"summary": {
|
||||
"totalLitellmKeys": len(litellm_keys),
|
||||
"totalPlatformAgents": len(platform_agents),
|
||||
"totalCustomAgents": len(custom_agents),
|
||||
}
|
||||
},
|
||||
message="用户资源信息获取成功"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user