67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package controllers
|
||
|
||
import (
|
||
"strings"
|
||
"time"
|
||
|
||
"server/models"
|
||
"server/pkg/tokenprobe"
|
||
)
|
||
|
||
func poolTableName(module string) string {
|
||
switch module {
|
||
case "cursor":
|
||
return new(models.PlatformAccountPoolCursor).TableName()
|
||
case "windsurf":
|
||
return new(models.PlatformAccountPoolWindsurf).TableName()
|
||
case "krio":
|
||
return new(models.PlatformAccountPoolKiro).TableName()
|
||
case "codex":
|
||
return new(models.PlatformAccountPoolCodex).TableName()
|
||
default:
|
||
|
||
return ""
|
||
}
|
||
}
|
||
|
||
// poolNeedsTokenProbe account 类型无 Token,无需探测;tk / account_tk 需探测。
|
||
func poolNeedsTokenProbe(dataType, token string) bool {
|
||
if strings.TrimSpace(token) == "" {
|
||
return false
|
||
}
|
||
return dataType != "account"
|
||
}
|
||
|
||
func poolSaveCursorIsUsed(id uint64, isUsed int8) {
|
||
_, _ = models.Orm.QueryTable(new(models.PlatformAccountPoolCursor)).
|
||
Filter("id", id).
|
||
Update(map[string]interface{}{
|
||
"is_used": isUsed,
|
||
"update_time": time.Now(),
|
||
})
|
||
}
|
||
|
||
// poolProbeToken 探测 Token;cursor 模块会回写 is_used。
|
||
func poolProbeToken(module, dataType, token string, rowID uint64) bool {
|
||
if !poolNeedsTokenProbe(dataType, token) {
|
||
return true
|
||
}
|
||
r := tokenprobe.ProbeOfficial(module, token)
|
||
if module == "cursor" && rowID > 0 {
|
||
var isUsed int8
|
||
if r.OK {
|
||
isUsed = 1
|
||
}
|
||
poolSaveCursorIsUsed(rowID, isUsed)
|
||
}
|
||
return r.OK
|
||
}
|
||
|
||
// poolIsUsedAvailable 已有探测结论时:1=可用,0=不可用,nil=未探测。
|
||
func poolIsUsedAvailable(isUsed *int8) (known bool, available bool) {
|
||
if isUsed == nil {
|
||
return false, false
|
||
}
|
||
return true, *isUsed == 1
|
||
}
|