feat(ollama): 支持 Cloud 官方用量自动刷新

This commit is contained in:
alfadb
2026-07-23 15:50:44 +08:00
parent 2c76506e07
commit 5ac4a9fac2
47 changed files with 5571 additions and 85 deletions
@@ -63,6 +63,7 @@ type AccountHandler struct {
tokenCacheInvalidator service.TokenCacheInvalidator
grokImportProber grokImportProber
upstreamBillingProbe *service.UpstreamBillingProbeService
ollamaCloudUsage *service.OllamaCloudUsageService
}
// SetUpstreamBillingProbeService attaches the optional remote billing probe service.
@@ -70,6 +71,10 @@ func (h *AccountHandler) SetUpstreamBillingProbeService(probe *service.UpstreamB
h.upstreamBillingProbe = probe
}
func (h *AccountHandler) SetOllamaCloudUsageService(usage *service.OllamaCloudUsageService) {
h.ollamaCloudUsage = usage
}
// NewAccountHandler creates a new admin account handler
func NewAccountHandler(
adminService service.AdminService,
@@ -208,9 +213,17 @@ type AccountSchedulerGroupScore struct {
const accountListGroupUngroupedQueryValue = "ungrouped"
func (h *AccountHandler) accountResponseFromService(account *service.Account) *dto.Account {
out := dto.AccountFromService(account)
if h != nil && h.ollamaCloudUsage != nil && out != nil {
h.ollamaCloudUsage.EnrichState(out.OllamaCloudUsage)
}
return out
}
func (h *AccountHandler) buildAccountResponseWithRuntime(ctx context.Context, account *service.Account) AccountWithConcurrency {
item := AccountWithConcurrency{
Account: dto.AccountFromService(account),
Account: h.accountResponseFromService(account),
CurrentConcurrency: 0,
}
if account == nil {
@@ -523,6 +536,16 @@ func (h *AccountHandler) List(c *gin.Context) {
response.ErrorFrom(c, err)
return
}
if h.ollamaCloudUsage != nil && len(accounts) > 0 {
accountPointers := make([]*service.Account, len(accounts))
for index := range accounts {
accountPointers[index] = &accounts[index]
}
if err := h.ollamaCloudUsage.ResolveAccounts(c.Request.Context(), accountPointers); err != nil {
response.ErrorFrom(c, err)
return
}
}
// Get current concurrency counts for all accounts
accountIDs := make([]int64, len(accounts))
@@ -626,7 +649,7 @@ func (h *AccountHandler) List(c *gin.Context) {
for i := range accounts {
acc := &accounts[i]
item := AccountWithConcurrency{
Account: dto.AccountFromService(acc),
Account: h.accountResponseFromService(acc),
CurrentConcurrency: concurrencyCounts[acc.ID],
SchedulerScore: schedulerScores[acc.ID],
SchedulerScores: schedulerGroupScores[acc.ID],
@@ -740,6 +763,12 @@ func (h *AccountHandler) GetByID(c *gin.Context) {
response.ErrorFrom(c, err)
return
}
if h.ollamaCloudUsage != nil {
if err := h.ollamaCloudUsage.ResolveAccounts(c.Request.Context(), []*service.Account{account}); err != nil {
response.ErrorFrom(c, err)
return
}
}
response.Success(c, h.buildAccountResponseWithRuntime(c.Request.Context(), account))
}