Merge pull request #49 from xmindlab-heicode/feat/agent-usage-budget-remaining
feat(usage): expose budget_remaining on agent usage endpoint (#9)
This commit is contained in:
@@ -490,11 +490,18 @@ func HeicodeGetAgentUsage(c *gin.Context) {
|
||||
}
|
||||
start, _ := strconv.ParseInt(strings.TrimSpace(c.Query("start")), 10, 64)
|
||||
end, _ := strconv.ParseInt(strings.TrimSpace(c.Query("end")), 10, 64)
|
||||
usage, err := model.SumAgentUsage(c.GetInt("id"), "agent:"+row.DeploymentID, start, end)
|
||||
userID := c.GetInt("id")
|
||||
usage, err := model.SumAgentUsage(userID, "agent:"+row.DeploymentID, start, end)
|
||||
if err != nil {
|
||||
agentError(c, "DEPLOYMENT_CONFLICT", "failed to aggregate agent usage")
|
||||
return
|
||||
}
|
||||
// #9: budget_remaining — 用户钱包剩余额度。Agent 模型调用经隐藏 token 计费到
|
||||
// user.Quota(见 #30),故"预算剩余"= 用户剩余额度。-1 表示读取失败(不阻断用量展示)。
|
||||
budgetRemaining := int64(-1)
|
||||
if u, uErr := model.GetUserById(userID, false); uErr == nil && u != nil {
|
||||
budgetRemaining = int64(u.Quota)
|
||||
}
|
||||
common.ApiSuccess(c, gin.H{
|
||||
"agent_id": row.DeploymentID,
|
||||
"quota": usage.Quota,
|
||||
@@ -502,6 +509,7 @@ func HeicodeGetAgentUsage(c *gin.Context) {
|
||||
"completion_tokens": usage.CompletionTokens,
|
||||
"call_count": usage.CallCount,
|
||||
"quota_per_unit": common.QuotaPerUnit,
|
||||
"budget_remaining": budgetRemaining,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user