mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-20 15:06:36 +00:00
revert: remove affinity scheduling, Claude Max cache simulation, Sora async tasks, and GDrive remnants
Remove 4 features from the release branch to keep it focused on channel management + upstream: 1. Affinity scheduling: gateway affinity flow, Redis Lua scripts, account methods, handler endpoints, frontend components (AffinityBadge, AffinityConfigCard) 2. Claude Max cache simulation: billing policy, tokenizer, response helpers, Group.SimulateClaudeMaxEnabled field, ent schema changes, frontend toggles 3. Sora async tasks: task service/worker/repo, videos handler, object storage abstraction, frontend components (SoraGeneratePage, SoraProgressCard, etc.), S3 storage admin settings 4. Google Drive remnants: gdrive constant cleanup (base revert was already done) All tests pass. Channel management code preserved intact.
This commit is contained in:
1 parent
d1499ee56f
commit
c790b808dd
94 files changed
+79
-11529
No files matched your search
@@ -1229,29 +1229,6 @@ func (a *Account) IsSessionIDMaskingEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsAffinityEnabled 检查是否启用亲和调度(统一入口)
|
||||
// 仅适用于 Anthropic 账号,同时检查新字段 affinity_enabled 和旧字段 client_affinity_enabled(向后兼容)
|
||||
func (a *Account) IsAffinityEnabled() bool {
|
||||
if a.Platform != PlatformAnthropic {
|
||||
return false
|
||||
}
|
||||
if a.Extra == nil {
|
||||
return false
|
||||
}
|
||||
if v, ok := a.Extra["affinity_enabled"]; ok {
|
||||
if enabled, ok := v.(bool); ok {
|
||||
return enabled
|
||||
}
|
||||
return false
|
||||
}
|
||||
if v, ok := a.Extra["client_affinity_enabled"]; ok {
|
||||
if enabled, ok := v.(bool); ok {
|
||||
return enabled
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsCustomBaseURLEnabled 检查是否启用自定义 base URL 中继转发
|
||||
// 仅适用于 Anthropic OAuth/SetupToken 类型账号
|
||||
func (a *Account) IsCustomBaseURLEnabled() bool {
|
||||
@@ -1269,221 +1246,6 @@ func (a *Account) IsCustomBaseURLEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsClientAffinityEnabled 向后兼容别名,内部调用 IsAffinityEnabled
|
||||
func (a *Account) IsClientAffinityEnabled() bool {
|
||||
return a.IsAffinityEnabled()
|
||||
}
|
||||
|
||||
// AffinityZone 表示账号的客户端亲和分区
|
||||
type AffinityZone int
|
||||
|
||||
const (
|
||||
AffinityZoneGreen AffinityZone = iota // 绿区:允许绑定新客户端,优先调度
|
||||
AffinityZoneYellow // 黄区:允许绑定,仅在无绿区账号时降级调度
|
||||
AffinityZoneRed // 红区:禁止调度
|
||||
)
|
||||
|
||||
// GetAffinityBase 获取亲和基础限制(绿区上限),0 表示未配置
|
||||
func (a *Account) GetAffinityBase() int {
|
||||
if a.Extra == nil {
|
||||
return 0
|
||||
}
|
||||
if v, ok := a.Extra["affinity_base"]; ok {
|
||||
return parseExtraInt(v)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetAffinityBuffer 获取亲和缓冲区大小(黄区范围)
|
||||
// 返回 (value, configured):
|
||||
// - (0, false): 未配置 → 无限黄区(超过 base 永远黄区,永不红区)
|
||||
// - (0, true): 显式设为 0 → 无黄区,超过 base 直接红区
|
||||
// - (n, true): n > 0 → 黄区范围为 base+1 到 base+n
|
||||
func (a *Account) GetAffinityBuffer() (int, bool) {
|
||||
if a.Extra == nil {
|
||||
return 0, false
|
||||
}
|
||||
v, ok := a.Extra["affinity_buffer"]
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
// 显式设为 null/nil → 视为未配置
|
||||
if v == nil {
|
||||
return 0, false
|
||||
}
|
||||
return parseExtraInt(v), true
|
||||
}
|
||||
|
||||
// GetAffinityZone 根据当前绑定的客户端数量计算账号的亲和分区。
|
||||
// 未开启亲和或未配置 base 的账号永远返回绿区。
|
||||
func (a *Account) GetAffinityZone(clientCount int64) AffinityZone {
|
||||
if !a.IsClientAffinityEnabled() {
|
||||
return AffinityZoneGreen
|
||||
}
|
||||
base := a.GetAffinityBase()
|
||||
if base <= 0 {
|
||||
return AffinityZoneGreen
|
||||
}
|
||||
if clientCount <= int64(base) {
|
||||
return AffinityZoneGreen
|
||||
}
|
||||
buffer, configured := a.GetAffinityBuffer()
|
||||
if !configured {
|
||||
return AffinityZoneYellow // 未配置 buffer → 无限黄区
|
||||
}
|
||||
if buffer == 0 {
|
||||
return AffinityZoneRed // buffer=0 → 无黄区,直接红区
|
||||
}
|
||||
if clientCount <= int64(base+buffer) {
|
||||
return AffinityZoneYellow
|
||||
}
|
||||
return AffinityZoneRed
|
||||
}
|
||||
|
||||
// IsAffinityAllowSwitch 检查亲和账号是否允许在无绿区时切换到其他账号
|
||||
// 默认 true(允许切换),设为 false 时即使全部红区也不切换
|
||||
func (a *Account) IsAffinityAllowSwitch() bool {
|
||||
if a.Extra == nil {
|
||||
return true
|
||||
}
|
||||
if v, ok := a.Extra["affinity_allow_switch"]; ok {
|
||||
if allow, ok := v.(bool); ok {
|
||||
return allow
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// GetPinnedUsers 获取指定亲和用户列表
|
||||
func (a *Account) GetPinnedUsers() []int64 {
|
||||
if a.Extra == nil {
|
||||
return nil
|
||||
}
|
||||
v, ok := a.Extra["pinned_users"]
|
||||
if !ok || v == nil {
|
||||
return nil
|
||||
}
|
||||
arr, ok := v.([]any)
|
||||
if !ok || len(arr) == 0 {
|
||||
return nil
|
||||
}
|
||||
result := make([]int64, 0, len(arr))
|
||||
for _, item := range arr {
|
||||
switch id := item.(type) {
|
||||
case float64:
|
||||
result = append(result, int64(id))
|
||||
case int64:
|
||||
result = append(result, id)
|
||||
case int:
|
||||
result = append(result, int64(id))
|
||||
case json.Number:
|
||||
if i, err := id.Int64(); err == nil {
|
||||
result = append(result, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// IsPinnedUser 检查 userID 是否在指定亲和用户列表中
|
||||
func (a *Account) IsPinnedUser(userID int64) bool {
|
||||
for _, id := range a.GetPinnedUsers() {
|
||||
if id == userID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// GetAffinityUserBase 获取用户维度亲和基础限制(绿区上限),0 表示未配置
|
||||
func (a *Account) GetAffinityUserBase() int {
|
||||
if a.Extra == nil {
|
||||
return 0
|
||||
}
|
||||
if v, ok := a.Extra["affinity_user_base"]; ok {
|
||||
return parseExtraInt(v)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetAffinityUserBuffer 获取用户维度亲和缓冲区大小(黄区范围)
|
||||
// 返回 (value, configured),语义与 GetAffinityBuffer 相同
|
||||
func (a *Account) GetAffinityUserBuffer() (int, bool) {
|
||||
if a.Extra == nil {
|
||||
return 0, false
|
||||
}
|
||||
v, ok := a.Extra["affinity_user_buffer"]
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
if v == nil {
|
||||
return 0, false
|
||||
}
|
||||
return parseExtraInt(v), true
|
||||
}
|
||||
|
||||
// GetPerUserClientLimit 获取每用户客户端限制,0 表示不限制
|
||||
func (a *Account) GetPerUserClientLimit() int {
|
||||
if a.Extra == nil {
|
||||
return 0
|
||||
}
|
||||
if v, ok := a.Extra["per_user_client_limit"]; ok {
|
||||
return parseExtraInt(v)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// getAffinityZoneForDim 根据单一维度的计数和三区参数计算亲和分区
|
||||
func getAffinityZoneForDim(count int64, base int, buffer int, bufferConfigured bool) AffinityZone {
|
||||
if base <= 0 {
|
||||
return AffinityZoneGreen
|
||||
}
|
||||
if count <= int64(base) {
|
||||
return AffinityZoneGreen
|
||||
}
|
||||
if !bufferConfigured {
|
||||
return AffinityZoneYellow
|
||||
}
|
||||
if buffer == 0 {
|
||||
return AffinityZoneRed
|
||||
}
|
||||
if count <= int64(base+buffer) {
|
||||
return AffinityZoneYellow
|
||||
}
|
||||
return AffinityZoneRed
|
||||
}
|
||||
|
||||
// GetMultiDimAffinityZone 根据多维度计数计算亲和分区,取所有维度中最严格的区域。
|
||||
// 未开启亲和的账号永远返回绿区。
|
||||
func (a *Account) GetMultiDimAffinityZone(userCount, clientCount, perUserCount int64) AffinityZone {
|
||||
if !a.IsAffinityEnabled() {
|
||||
return AffinityZoneGreen
|
||||
}
|
||||
worst := AffinityZoneGreen
|
||||
|
||||
// 客户端维度
|
||||
clientBase := a.GetAffinityBase()
|
||||
clientBuffer, clientBufCfg := a.GetAffinityBuffer()
|
||||
if z := getAffinityZoneForDim(clientCount, clientBase, clientBuffer, clientBufCfg); z > worst {
|
||||
worst = z
|
||||
}
|
||||
|
||||
// 用户维度
|
||||
userBase := a.GetAffinityUserBase()
|
||||
userBuffer, userBufCfg := a.GetAffinityUserBuffer()
|
||||
if z := getAffinityZoneForDim(userCount, userBase, userBuffer, userBufCfg); z > worst {
|
||||
worst = z
|
||||
}
|
||||
|
||||
// 每用户客户端维度
|
||||
perUserLimit := a.GetPerUserClientLimit()
|
||||
if perUserLimit > 0 && perUserCount > int64(perUserLimit) {
|
||||
worst = AffinityZoneRed
|
||||
}
|
||||
|
||||
return worst
|
||||
}
|
||||
|
||||
// GetCustomBaseURL 返回自定义中继服务的 base URL
|
||||
func (a *Account) GetCustomBaseURL() string {
|
||||
return a.GetExtraString("custom_base_url")
|
||||
|
||||
@@ -155,7 +155,6 @@ type CreateGroupInput struct {
|
||||
ModelRouting map[string][]int64
|
||||
ModelRoutingEnabled bool // 是否启用模型路由
|
||||
MCPXMLInject *bool
|
||||
SimulateClaudeMaxEnabled *bool
|
||||
// 支持的模型系列(仅 antigravity 平台使用)
|
||||
SupportedModelScopes []string
|
||||
// Sora 存储配额
|
||||
@@ -195,7 +194,6 @@ type UpdateGroupInput struct {
|
||||
ModelRouting map[string][]int64
|
||||
ModelRoutingEnabled *bool // 是否启用模型路由
|
||||
MCPXMLInject *bool
|
||||
SimulateClaudeMaxEnabled *bool
|
||||
// 支持的模型系列(仅 antigravity 平台使用)
|
||||
SupportedModelScopes *[]string
|
||||
// Sora 存储配额
|
||||
@@ -884,13 +882,6 @@ func (s *adminServiceImpl) CreateGroup(ctx context.Context, input *CreateGroupIn
|
||||
if input.MCPXMLInject != nil {
|
||||
mcpXMLInject = *input.MCPXMLInject
|
||||
}
|
||||
simulateClaudeMaxEnabled := false
|
||||
if input.SimulateClaudeMaxEnabled != nil {
|
||||
if platform != PlatformAnthropic && *input.SimulateClaudeMaxEnabled {
|
||||
return nil, fmt.Errorf("simulate_claude_max_enabled only supported for anthropic groups")
|
||||
}
|
||||
simulateClaudeMaxEnabled = *input.SimulateClaudeMaxEnabled
|
||||
}
|
||||
|
||||
// 如果指定了复制账号的源分组,先获取账号 ID 列表
|
||||
var accountIDsToCopy []int64
|
||||
@@ -947,7 +938,6 @@ func (s *adminServiceImpl) CreateGroup(ctx context.Context, input *CreateGroupIn
|
||||
FallbackGroupIDOnInvalidRequest: fallbackOnInvalidRequest,
|
||||
ModelRouting: input.ModelRouting,
|
||||
MCPXMLInject: mcpXMLInject,
|
||||
SimulateClaudeMaxEnabled: simulateClaudeMaxEnabled,
|
||||
SupportedModelScopes: input.SupportedModelScopes,
|
||||
SoraStorageQuotaBytes: input.SoraStorageQuotaBytes,
|
||||
AllowMessagesDispatch: input.AllowMessagesDispatch,
|
||||
@@ -1154,15 +1144,6 @@ func (s *adminServiceImpl) UpdateGroup(ctx context.Context, id int64, input *Upd
|
||||
if input.MCPXMLInject != nil {
|
||||
group.MCPXMLInject = *input.MCPXMLInject
|
||||
}
|
||||
if input.SimulateClaudeMaxEnabled != nil {
|
||||
if group.Platform != PlatformAnthropic && *input.SimulateClaudeMaxEnabled {
|
||||
return nil, fmt.Errorf("simulate_claude_max_enabled only supported for anthropic groups")
|
||||
}
|
||||
group.SimulateClaudeMaxEnabled = *input.SimulateClaudeMaxEnabled
|
||||
}
|
||||
if group.Platform != PlatformAnthropic {
|
||||
group.SimulateClaudeMaxEnabled = false
|
||||
}
|
||||
|
||||
// 支持的模型系列(仅 antigravity 平台使用)
|
||||
if input.SupportedModelScopes != nil {
|
||||
|
||||
@@ -785,57 +785,3 @@ func TestAdminService_UpdateGroup_InvalidRequestFallbackAllowsAntigravity(t *tes
|
||||
require.NotNil(t, repo.updated)
|
||||
require.Equal(t, fallbackID, *repo.updated.FallbackGroupIDOnInvalidRequest)
|
||||
}
|
||||
|
||||
func TestAdminService_CreateGroup_SimulateClaudeMaxRequiresAnthropic(t *testing.T) {
|
||||
repo := &groupRepoStubForAdmin{}
|
||||
svc := &adminServiceImpl{groupRepo: repo}
|
||||
|
||||
enabled := true
|
||||
_, err := svc.CreateGroup(context.Background(), &CreateGroupInput{
|
||||
Name: "openai-group",
|
||||
Platform: PlatformOpenAI,
|
||||
SimulateClaudeMaxEnabled: &enabled,
|
||||
})
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "simulate_claude_max_enabled only supported for anthropic groups")
|
||||
require.Nil(t, repo.created)
|
||||
}
|
||||
|
||||
func TestAdminService_UpdateGroup_SimulateClaudeMaxRequiresAnthropic(t *testing.T) {
|
||||
existingGroup := &Group{
|
||||
ID: 1,
|
||||
Name: "openai-group",
|
||||
Platform: PlatformOpenAI,
|
||||
Status: StatusActive,
|
||||
}
|
||||
repo := &groupRepoStubForAdmin{getByID: existingGroup}
|
||||
svc := &adminServiceImpl{groupRepo: repo}
|
||||
|
||||
enabled := true
|
||||
_, err := svc.UpdateGroup(context.Background(), 1, &UpdateGroupInput{
|
||||
SimulateClaudeMaxEnabled: &enabled,
|
||||
})
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "simulate_claude_max_enabled only supported for anthropic groups")
|
||||
require.Nil(t, repo.updated)
|
||||
}
|
||||
|
||||
func TestAdminService_UpdateGroup_ClearsSimulateClaudeMaxWhenPlatformChanges(t *testing.T) {
|
||||
existingGroup := &Group{
|
||||
ID: 1,
|
||||
Name: "anthropic-group",
|
||||
Platform: PlatformAnthropic,
|
||||
Status: StatusActive,
|
||||
SimulateClaudeMaxEnabled: true,
|
||||
}
|
||||
repo := &groupRepoStubForAdmin{getByID: existingGroup}
|
||||
svc := &adminServiceImpl{groupRepo: repo}
|
||||
|
||||
group, err := svc.UpdateGroup(context.Background(), 1, &UpdateGroupInput{
|
||||
Platform: PlatformOpenAI,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, group)
|
||||
require.NotNil(t, repo.updated)
|
||||
require.False(t, repo.updated.SimulateClaudeMaxEnabled)
|
||||
}
|
||||
@@ -1763,9 +1763,6 @@ func (s *AntigravityGatewayService) Forward(ctx context.Context, c *gin.Context,
|
||||
firstTokenMs = streamRes.firstTokenMs
|
||||
}
|
||||
|
||||
// Claude Max cache billing: 同步 ForwardResult.Usage 与客户端响应体一致
|
||||
applyClaudeMaxCacheBillingPolicyToUsage(usage, parsedRequestFromGinContext(c), claudeMaxGroupFromGinContext(c), originalModel, account.ID)
|
||||
|
||||
return &ForwardResult{
|
||||
RequestID: requestID,
|
||||
Usage: *usage,
|
||||
@@ -3863,9 +3860,6 @@ returnResponse:
|
||||
return nil, s.writeClaudeError(c, http.StatusBadGateway, "upstream_error", "Failed to parse upstream response")
|
||||
}
|
||||
|
||||
// Claude Max cache billing simulation (non-streaming)
|
||||
claudeResp = applyClaudeMaxNonStreamingRewrite(c, claudeResp, agUsage, originalModel, accountID)
|
||||
|
||||
c.Data(http.StatusOK, "application/json", claudeResp)
|
||||
|
||||
// 转换为 service.ClaudeUsage
|
||||
@@ -3893,7 +3887,6 @@ func (s *AntigravityGatewayService) handleClaudeStreamingResponse(c *gin.Context
|
||||
}
|
||||
|
||||
processor := antigravity.NewStreamingProcessor(originalModel)
|
||||
setupClaudeMaxStreamingHook(c, processor, originalModel, accountID)
|
||||
|
||||
var firstTokenMs *int
|
||||
// 使用 Scanner 并限制单行大小,避免 ReadString 无上限导致 OOM
|
||||
|
||||
@@ -31,28 +31,6 @@ func (c *stubSmartRetryCache) DeleteSessionAccountID(_ context.Context, groupID
|
||||
c.deleteCalls = append(c.deleteCalls, deleteSessionCall{groupID: groupID, sessionHash: sessionHash})
|
||||
return nil
|
||||
}
|
||||
func (c *stubSmartRetryCache) GetAffinityAccounts(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (c *stubSmartRetryCache) UpdateAffinity(_ context.Context, _ int64, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (c *stubSmartRetryCache) GetAffinityMultiCount(_ context.Context, _ int64, _ int64, _ int64, _ time.Duration) (int64, int64, int64, error) {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
func (c *stubSmartRetryCache) GetAccountAffinityCountBatch(_ context.Context, _ int64, _ []int64, _ time.Duration) (map[int64]int64, error) {
|
||||
return map[int64]int64{}, nil
|
||||
}
|
||||
func (c *stubSmartRetryCache) GetAccountAffinityClientsBatch(_ context.Context, _ map[int64][]int64, _ time.Duration) (map[int64][]string, error) {
|
||||
return map[int64][]string{}, nil
|
||||
}
|
||||
func (c *stubSmartRetryCache) GetAccountAffinityClientsWithScores(_ context.Context, _ int64, _ []int64, _ time.Duration) ([]AffinityClient, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (c *stubSmartRetryCache) ClearAccountAffinity(_ context.Context, _ int64, _ []int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// mockSmartRetryUpstream 用于 handleSmartRetry 测试的 mock upstream
|
||||
type mockSmartRetryUpstream struct {
|
||||
responses []*http.Response
|
||||
|
||||
@@ -62,7 +62,6 @@ type APIKeyAuthGroupSnapshot struct {
|
||||
ModelRouting map[string][]int64 `json:"model_routing,omitempty"`
|
||||
ModelRoutingEnabled bool `json:"model_routing_enabled"`
|
||||
MCPXMLInject bool `json:"mcp_xml_inject"`
|
||||
SimulateClaudeMaxEnabled bool `json:"simulate_claude_max_enabled"`
|
||||
|
||||
// 支持的模型系列(仅 antigravity 平台使用)
|
||||
SupportedModelScopes []string `json:"supported_model_scopes,omitempty"`
|
||||
|
||||
@@ -244,7 +244,6 @@ func (s *APIKeyService) snapshotFromAPIKey(apiKey *APIKey) *APIKeyAuthSnapshot {
|
||||
ModelRouting: apiKey.Group.ModelRouting,
|
||||
ModelRoutingEnabled: apiKey.Group.ModelRoutingEnabled,
|
||||
MCPXMLInject: apiKey.Group.MCPXMLInject,
|
||||
SimulateClaudeMaxEnabled: apiKey.Group.SimulateClaudeMaxEnabled,
|
||||
SupportedModelScopes: apiKey.Group.SupportedModelScopes,
|
||||
AllowMessagesDispatch: apiKey.Group.AllowMessagesDispatch,
|
||||
DefaultMappedModel: apiKey.Group.DefaultMappedModel,
|
||||
@@ -304,7 +303,6 @@ func (s *APIKeyService) snapshotToAPIKey(key string, snapshot *APIKeyAuthSnapsho
|
||||
ModelRouting: snapshot.Group.ModelRouting,
|
||||
ModelRoutingEnabled: snapshot.Group.ModelRoutingEnabled,
|
||||
MCPXMLInject: snapshot.Group.MCPXMLInject,
|
||||
SimulateClaudeMaxEnabled: snapshot.Group.SimulateClaudeMaxEnabled,
|
||||
SupportedModelScopes: snapshot.Group.SupportedModelScopes,
|
||||
AllowMessagesDispatch: snapshot.Group.AllowMessagesDispatch,
|
||||
DefaultMappedModel: snapshot.Group.DefaultMappedModel,
|
||||
|
||||
@@ -1,450 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/claude"
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/logger"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
type claudeMaxCacheBillingOutcome struct {
|
||||
Simulated bool
|
||||
}
|
||||
|
||||
func applyClaudeMaxCacheBillingPolicyToUsage(usage *ClaudeUsage, parsed *ParsedRequest, group *Group, model string, accountID int64) claudeMaxCacheBillingOutcome {
|
||||
var out claudeMaxCacheBillingOutcome
|
||||
if usage == nil || !shouldApplyClaudeMaxBillingRulesForUsage(group, model, parsed) {
|
||||
return out
|
||||
}
|
||||
|
||||
resolvedModel := strings.TrimSpace(model)
|
||||
if resolvedModel == "" && parsed != nil {
|
||||
resolvedModel = strings.TrimSpace(parsed.Model)
|
||||
}
|
||||
|
||||
if hasCacheCreationTokens(*usage) {
|
||||
// Upstream already returned cache creation usage; keep original usage.
|
||||
return out
|
||||
}
|
||||
|
||||
if !shouldSimulateClaudeMaxUsageForUsage(*usage, parsed) {
|
||||
return out
|
||||
}
|
||||
beforeInputTokens := usage.InputTokens
|
||||
out.Simulated = safelyProjectUsageToClaudeMax1H(usage, parsed)
|
||||
if out.Simulated {
|
||||
logger.LegacyPrintf("service.gateway", "simulate_claude_max_usage: model=%s account=%d input_tokens:%d->%d cache_creation_1h=%d",
|
||||
resolvedModel,
|
||||
accountID,
|
||||
beforeInputTokens,
|
||||
usage.InputTokens,
|
||||
usage.CacheCreation1hTokens,
|
||||
)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func isClaudeFamilyModel(model string) bool {
|
||||
normalized := strings.ToLower(strings.TrimSpace(claude.NormalizeModelID(model)))
|
||||
if normalized == "" {
|
||||
return false
|
||||
}
|
||||
return strings.Contains(normalized, "claude-")
|
||||
}
|
||||
|
||||
func shouldApplyClaudeMaxBillingRules(input *RecordUsageInput) bool {
|
||||
if input == nil || input.Result == nil || input.APIKey == nil || input.APIKey.Group == nil {
|
||||
return false
|
||||
}
|
||||
return shouldApplyClaudeMaxBillingRulesForUsage(input.APIKey.Group, input.Result.Model, input.ParsedRequest)
|
||||
}
|
||||
|
||||
func shouldApplyClaudeMaxBillingRulesForUsage(group *Group, model string, parsed *ParsedRequest) bool {
|
||||
if group == nil {
|
||||
return false
|
||||
}
|
||||
if !group.SimulateClaudeMaxEnabled || group.Platform != PlatformAnthropic {
|
||||
return false
|
||||
}
|
||||
|
||||
resolvedModel := model
|
||||
if resolvedModel == "" && parsed != nil {
|
||||
resolvedModel = parsed.Model
|
||||
}
|
||||
if !isClaudeFamilyModel(resolvedModel) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func hasCacheCreationTokens(usage ClaudeUsage) bool {
|
||||
return usage.CacheCreationInputTokens > 0 || usage.CacheCreation5mTokens > 0 || usage.CacheCreation1hTokens > 0
|
||||
}
|
||||
|
||||
func shouldSimulateClaudeMaxUsage(input *RecordUsageInput) bool {
|
||||
if input == nil || input.Result == nil {
|
||||
return false
|
||||
}
|
||||
if !shouldApplyClaudeMaxBillingRules(input) {
|
||||
return false
|
||||
}
|
||||
return shouldSimulateClaudeMaxUsageForUsage(input.Result.Usage, input.ParsedRequest)
|
||||
}
|
||||
|
||||
func shouldSimulateClaudeMaxUsageForUsage(usage ClaudeUsage, parsed *ParsedRequest) bool {
|
||||
if usage.InputTokens <= 0 {
|
||||
return false
|
||||
}
|
||||
if hasCacheCreationTokens(usage) {
|
||||
return false
|
||||
}
|
||||
if !hasClaudeCacheSignals(parsed) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func safelyProjectUsageToClaudeMax1H(usage *ClaudeUsage, parsed *ParsedRequest) (changed bool) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logger.LegacyPrintf("service.gateway", "simulate_claude_max_usage skipped: panic=%v", r)
|
||||
changed = false
|
||||
}
|
||||
}()
|
||||
return projectUsageToClaudeMax1H(usage, parsed)
|
||||
}
|
||||
|
||||
func projectUsageToClaudeMax1H(usage *ClaudeUsage, parsed *ParsedRequest) bool {
|
||||
if usage == nil {
|
||||
return false
|
||||
}
|
||||
totalWindowTokens := usage.InputTokens + usage.CacheCreation5mTokens + usage.CacheCreation1hTokens
|
||||
if totalWindowTokens <= 1 {
|
||||
return false
|
||||
}
|
||||
|
||||
simulatedInputTokens := computeClaudeMaxProjectedInputTokens(totalWindowTokens, parsed)
|
||||
if simulatedInputTokens <= 0 {
|
||||
simulatedInputTokens = 1
|
||||
}
|
||||
if simulatedInputTokens >= totalWindowTokens {
|
||||
simulatedInputTokens = totalWindowTokens - 1
|
||||
}
|
||||
|
||||
cacheCreation1hTokens := totalWindowTokens - simulatedInputTokens
|
||||
if usage.InputTokens == simulatedInputTokens &&
|
||||
usage.CacheCreation5mTokens == 0 &&
|
||||
usage.CacheCreation1hTokens == cacheCreation1hTokens &&
|
||||
usage.CacheCreationInputTokens == cacheCreation1hTokens {
|
||||
return false
|
||||
}
|
||||
|
||||
usage.InputTokens = simulatedInputTokens
|
||||
usage.CacheCreation5mTokens = 0
|
||||
usage.CacheCreation1hTokens = cacheCreation1hTokens
|
||||
usage.CacheCreationInputTokens = cacheCreation1hTokens
|
||||
return true
|
||||
}
|
||||
|
||||
type claudeCacheProjection struct {
|
||||
HasBreakpoint bool
|
||||
BreakpointCount int
|
||||
TotalEstimatedTokens int
|
||||
TailEstimatedTokens int
|
||||
}
|
||||
|
||||
func computeClaudeMaxProjectedInputTokens(totalWindowTokens int, parsed *ParsedRequest) int {
|
||||
if totalWindowTokens <= 1 {
|
||||
return totalWindowTokens
|
||||
}
|
||||
|
||||
projection := analyzeClaudeCacheProjection(parsed)
|
||||
if !projection.HasBreakpoint || projection.TotalEstimatedTokens <= 0 || projection.TailEstimatedTokens <= 0 {
|
||||
return totalWindowTokens
|
||||
}
|
||||
|
||||
totalEstimate := int64(projection.TotalEstimatedTokens)
|
||||
tailEstimate := int64(projection.TailEstimatedTokens)
|
||||
if tailEstimate > totalEstimate {
|
||||
tailEstimate = totalEstimate
|
||||
}
|
||||
|
||||
scaled := (int64(totalWindowTokens)*tailEstimate + totalEstimate/2) / totalEstimate
|
||||
if scaled <= 0 {
|
||||
scaled = 1
|
||||
}
|
||||
if scaled >= int64(totalWindowTokens) {
|
||||
scaled = int64(totalWindowTokens - 1)
|
||||
}
|
||||
return int(scaled)
|
||||
}
|
||||
|
||||
func hasClaudeCacheSignals(parsed *ParsedRequest) bool {
|
||||
if parsed == nil {
|
||||
return false
|
||||
}
|
||||
if hasTopLevelEphemeralCacheControl(parsed) {
|
||||
return true
|
||||
}
|
||||
return countExplicitCacheBreakpoints(parsed) > 0
|
||||
}
|
||||
|
||||
func hasTopLevelEphemeralCacheControl(parsed *ParsedRequest) bool {
|
||||
if parsed == nil || len(parsed.Body) == 0 {
|
||||
return false
|
||||
}
|
||||
cacheType := strings.TrimSpace(gjson.GetBytes(parsed.Body, "cache_control.type").String())
|
||||
return strings.EqualFold(cacheType, "ephemeral")
|
||||
}
|
||||
|
||||
func analyzeClaudeCacheProjection(parsed *ParsedRequest) claudeCacheProjection {
|
||||
var projection claudeCacheProjection
|
||||
if parsed == nil {
|
||||
return projection
|
||||
}
|
||||
|
||||
total := 0
|
||||
lastBreakpointAt := -1
|
||||
|
||||
switch system := parsed.System.(type) {
|
||||
case string:
|
||||
total += claudeMaxMessageOverheadTokens + estimateClaudeTextTokens(system)
|
||||
case []any:
|
||||
for _, raw := range system {
|
||||
block, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
total += claudeMaxUnknownContentTokens
|
||||
continue
|
||||
}
|
||||
total += estimateClaudeBlockTokens(block)
|
||||
if hasEphemeralCacheControl(block) {
|
||||
lastBreakpointAt = total
|
||||
projection.BreakpointCount++
|
||||
projection.HasBreakpoint = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, rawMsg := range parsed.Messages {
|
||||
total += claudeMaxMessageOverheadTokens
|
||||
msg, ok := rawMsg.(map[string]any)
|
||||
if !ok {
|
||||
total += claudeMaxUnknownContentTokens
|
||||
continue
|
||||
}
|
||||
content, exists := msg["content"]
|
||||
if !exists {
|
||||
continue
|
||||
}
|
||||
msgTokens, msgLastBreak, msgBreakCount := estimateClaudeContentTokens(content)
|
||||
total += msgTokens
|
||||
if msgBreakCount > 0 {
|
||||
lastBreakpointAt = total - msgTokens + msgLastBreak
|
||||
projection.BreakpointCount += msgBreakCount
|
||||
projection.HasBreakpoint = true
|
||||
}
|
||||
}
|
||||
|
||||
if total <= 0 {
|
||||
total = 1
|
||||
}
|
||||
projection.TotalEstimatedTokens = total
|
||||
|
||||
if projection.HasBreakpoint && lastBreakpointAt >= 0 {
|
||||
tail := total - lastBreakpointAt
|
||||
if tail <= 0 {
|
||||
tail = 1
|
||||
}
|
||||
projection.TailEstimatedTokens = tail
|
||||
return projection
|
||||
}
|
||||
|
||||
if hasTopLevelEphemeralCacheControl(parsed) {
|
||||
tail := estimateLastUserMessageTokens(parsed)
|
||||
if tail <= 0 {
|
||||
tail = 1
|
||||
}
|
||||
projection.HasBreakpoint = true
|
||||
projection.BreakpointCount = 1
|
||||
projection.TailEstimatedTokens = tail
|
||||
}
|
||||
return projection
|
||||
}
|
||||
|
||||
func countExplicitCacheBreakpoints(parsed *ParsedRequest) int {
|
||||
if parsed == nil {
|
||||
return 0
|
||||
}
|
||||
total := 0
|
||||
if system, ok := parsed.System.([]any); ok {
|
||||
for _, raw := range system {
|
||||
if block, ok := raw.(map[string]any); ok && hasEphemeralCacheControl(block) {
|
||||
total++
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, rawMsg := range parsed.Messages {
|
||||
msg, ok := rawMsg.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
content, ok := msg["content"].([]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for _, raw := range content {
|
||||
if block, ok := raw.(map[string]any); ok && hasEphemeralCacheControl(block) {
|
||||
total++
|
||||
}
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func hasEphemeralCacheControl(block map[string]any) bool {
|
||||
if block == nil {
|
||||
return false
|
||||
}
|
||||
raw, ok := block["cache_control"]
|
||||
if !ok || raw == nil {
|
||||
return false
|
||||
}
|
||||
switch cc := raw.(type) {
|
||||
case map[string]any:
|
||||
cacheType, _ := cc["type"].(string)
|
||||
return strings.EqualFold(strings.TrimSpace(cacheType), "ephemeral")
|
||||
case map[string]string:
|
||||
return strings.EqualFold(strings.TrimSpace(cc["type"]), "ephemeral")
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func estimateClaudeContentTokens(content any) (tokens int, lastBreakAt int, breakpointCount int) {
|
||||
switch value := content.(type) {
|
||||
case string:
|
||||
return estimateClaudeTextTokens(value), -1, 0
|
||||
case []any:
|
||||
total := 0
|
||||
lastBreak := -1
|
||||
breaks := 0
|
||||
for _, raw := range value {
|
||||
block, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
total += claudeMaxUnknownContentTokens
|
||||
continue
|
||||
}
|
||||
total += estimateClaudeBlockTokens(block)
|
||||
if hasEphemeralCacheControl(block) {
|
||||
lastBreak = total
|
||||
breaks++
|
||||
}
|
||||
}
|
||||
return total, lastBreak, breaks
|
||||
default:
|
||||
return estimateStructuredTokens(value), -1, 0
|
||||
}
|
||||
}
|
||||
|
||||
func estimateClaudeBlockTokens(block map[string]any) int {
|
||||
if block == nil {
|
||||
return claudeMaxUnknownContentTokens
|
||||
}
|
||||
tokens := claudeMaxBlockOverheadTokens
|
||||
blockType, _ := block["type"].(string)
|
||||
switch blockType {
|
||||
case "text":
|
||||
if text, ok := block["text"].(string); ok {
|
||||
tokens += estimateClaudeTextTokens(text)
|
||||
}
|
||||
case "tool_result":
|
||||
if content, ok := block["content"]; ok {
|
||||
nested, _, _ := estimateClaudeContentTokens(content)
|
||||
tokens += nested
|
||||
}
|
||||
case "tool_use":
|
||||
if name, ok := block["name"].(string); ok {
|
||||
tokens += estimateClaudeTextTokens(name)
|
||||
}
|
||||
if input, ok := block["input"]; ok {
|
||||
tokens += estimateStructuredTokens(input)
|
||||
}
|
||||
default:
|
||||
if text, ok := block["text"].(string); ok {
|
||||
tokens += estimateClaudeTextTokens(text)
|
||||
} else if content, ok := block["content"]; ok {
|
||||
nested, _, _ := estimateClaudeContentTokens(content)
|
||||
tokens += nested
|
||||
}
|
||||
}
|
||||
if tokens <= claudeMaxBlockOverheadTokens {
|
||||
tokens += claudeMaxUnknownContentTokens
|
||||
}
|
||||
return tokens
|
||||
}
|
||||
|
||||
func estimateLastUserMessageTokens(parsed *ParsedRequest) int {
|
||||
if parsed == nil || len(parsed.Messages) == 0 {
|
||||
return 0
|
||||
}
|
||||
for i := len(parsed.Messages) - 1; i >= 0; i-- {
|
||||
msg, ok := parsed.Messages[i].(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
role, _ := msg["role"].(string)
|
||||
if !strings.EqualFold(strings.TrimSpace(role), "user") {
|
||||
continue
|
||||
}
|
||||
tokens, _, _ := estimateClaudeContentTokens(msg["content"])
|
||||
return claudeMaxMessageOverheadTokens + tokens
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func estimateStructuredTokens(v any) int {
|
||||
if v == nil {
|
||||
return 0
|
||||
}
|
||||
raw, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return claudeMaxUnknownContentTokens
|
||||
}
|
||||
return estimateClaudeTextTokens(string(raw))
|
||||
}
|
||||
|
||||
func estimateClaudeTextTokens(text string) int {
|
||||
if tokens, ok := estimateTokensByThirdPartyTokenizer(text); ok {
|
||||
return tokens
|
||||
}
|
||||
return estimateClaudeTextTokensHeuristic(text)
|
||||
}
|
||||
|
||||
func estimateClaudeTextTokensHeuristic(text string) int {
|
||||
normalized := strings.Join(strings.Fields(strings.TrimSpace(text)), " ")
|
||||
if normalized == "" {
|
||||
return 0
|
||||
}
|
||||
asciiChars := 0
|
||||
nonASCIIChars := 0
|
||||
for _, r := range normalized {
|
||||
if r <= 127 {
|
||||
asciiChars++
|
||||
} else {
|
||||
nonASCIIChars++
|
||||
}
|
||||
}
|
||||
tokens := nonASCIIChars
|
||||
if asciiChars > 0 {
|
||||
tokens += (asciiChars + 3) / 4
|
||||
}
|
||||
if words := len(strings.Fields(normalized)); words > tokens {
|
||||
tokens = words
|
||||
}
|
||||
if tokens <= 0 {
|
||||
return 1
|
||||
}
|
||||
return tokens
|
||||
}
|
||||
@@ -1,156 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestProjectUsageToClaudeMax1H_Conservation(t *testing.T) {
|
||||
usage := &ClaudeUsage{
|
||||
InputTokens: 1200,
|
||||
CacheCreationInputTokens: 0,
|
||||
CacheCreation5mTokens: 0,
|
||||
CacheCreation1hTokens: 0,
|
||||
}
|
||||
parsed := &ParsedRequest{
|
||||
Model: "claude-sonnet-4-5",
|
||||
Messages: []any{
|
||||
map[string]any{
|
||||
"role": "user",
|
||||
"content": []any{
|
||||
map[string]any{
|
||||
"type": "text",
|
||||
"text": strings.Repeat("cached context ", 200),
|
||||
"cache_control": map[string]any{"type": "ephemeral"},
|
||||
},
|
||||
map[string]any{
|
||||
"type": "text",
|
||||
"text": "summarize quickly",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
changed := projectUsageToClaudeMax1H(usage, parsed)
|
||||
if !changed {
|
||||
t.Fatalf("expected usage to be projected")
|
||||
}
|
||||
|
||||
total := usage.InputTokens + usage.CacheCreation5mTokens + usage.CacheCreation1hTokens
|
||||
if total != 1200 {
|
||||
t.Fatalf("total tokens changed: got=%d want=%d", total, 1200)
|
||||
}
|
||||
if usage.CacheCreation5mTokens != 0 {
|
||||
t.Fatalf("cache_creation_5m should be 0, got=%d", usage.CacheCreation5mTokens)
|
||||
}
|
||||
if usage.InputTokens <= 0 || usage.InputTokens >= 1200 {
|
||||
t.Fatalf("simulated input out of range, got=%d", usage.InputTokens)
|
||||
}
|
||||
if usage.InputTokens > 100 {
|
||||
t.Fatalf("simulated input should stay near cache breakpoint tail, got=%d", usage.InputTokens)
|
||||
}
|
||||
if usage.CacheCreation1hTokens <= 0 {
|
||||
t.Fatalf("cache_creation_1h should be > 0, got=%d", usage.CacheCreation1hTokens)
|
||||
}
|
||||
if usage.CacheCreationInputTokens != usage.CacheCreation1hTokens {
|
||||
t.Fatalf("cache_creation_input_tokens mismatch: got=%d want=%d", usage.CacheCreationInputTokens, usage.CacheCreation1hTokens)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComputeClaudeMaxProjectedInputTokens_Deterministic(t *testing.T) {
|
||||
parsed := &ParsedRequest{
|
||||
Model: "claude-opus-4-5",
|
||||
Messages: []any{
|
||||
map[string]any{
|
||||
"role": "user",
|
||||
"content": []any{
|
||||
map[string]any{
|
||||
"type": "text",
|
||||
"text": "build context",
|
||||
"cache_control": map[string]any{"type": "ephemeral"},
|
||||
},
|
||||
map[string]any{
|
||||
"type": "text",
|
||||
"text": "what is failing now",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
got1 := computeClaudeMaxProjectedInputTokens(4096, parsed)
|
||||
got2 := computeClaudeMaxProjectedInputTokens(4096, parsed)
|
||||
if got1 != got2 {
|
||||
t.Fatalf("non-deterministic input tokens: %d != %d", got1, got2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldSimulateClaudeMaxUsage(t *testing.T) {
|
||||
group := &Group{
|
||||
Platform: PlatformAnthropic,
|
||||
SimulateClaudeMaxEnabled: true,
|
||||
}
|
||||
input := &RecordUsageInput{
|
||||
Result: &ForwardResult{
|
||||
Model: "claude-sonnet-4-5",
|
||||
Usage: ClaudeUsage{
|
||||
InputTokens: 3000,
|
||||
CacheCreationInputTokens: 0,
|
||||
CacheCreation5mTokens: 0,
|
||||
CacheCreation1hTokens: 0,
|
||||
},
|
||||
},
|
||||
ParsedRequest: &ParsedRequest{
|
||||
Messages: []any{
|
||||
map[string]any{
|
||||
"role": "user",
|
||||
"content": []any{
|
||||
map[string]any{
|
||||
"type": "text",
|
||||
"text": "cached",
|
||||
"cache_control": map[string]any{"type": "ephemeral"},
|
||||
},
|
||||
map[string]any{
|
||||
"type": "text",
|
||||
"text": "tail",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
APIKey: &APIKey{Group: group},
|
||||
}
|
||||
|
||||
if !shouldSimulateClaudeMaxUsage(input) {
|
||||
t.Fatalf("expected simulate=true for claude group with cache signal")
|
||||
}
|
||||
|
||||
input.ParsedRequest = &ParsedRequest{
|
||||
Messages: []any{
|
||||
map[string]any{"role": "user", "content": "no cache signal"},
|
||||
},
|
||||
}
|
||||
if shouldSimulateClaudeMaxUsage(input) {
|
||||
t.Fatalf("expected simulate=false when request has no cache signal")
|
||||
}
|
||||
|
||||
input.ParsedRequest = &ParsedRequest{
|
||||
Messages: []any{
|
||||
map[string]any{
|
||||
"role": "user",
|
||||
"content": []any{
|
||||
map[string]any{
|
||||
"type": "text",
|
||||
"text": "cached",
|
||||
"cache_control": map[string]any{"type": "ephemeral"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
input.Result.Usage.CacheCreationInputTokens = 100
|
||||
if shouldSimulateClaudeMaxUsage(input) {
|
||||
t.Fatalf("expected simulate=false when cache creation already exists")
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
tiktoken "github.com/pkoukk/tiktoken-go"
|
||||
tiktokenloader "github.com/pkoukk/tiktoken-go-loader"
|
||||
)
|
||||
|
||||
var (
|
||||
claudeTokenizerOnce sync.Once
|
||||
claudeTokenizer *tiktoken.Tiktoken
|
||||
)
|
||||
|
||||
func getClaudeTokenizer() *tiktoken.Tiktoken {
|
||||
claudeTokenizerOnce.Do(func() {
|
||||
// Use offline loader to avoid runtime dictionary download.
|
||||
tiktoken.SetBpeLoader(tiktokenloader.NewOfflineLoader())
|
||||
// Use a high-capacity tokenizer as the default approximation for Claude payloads.
|
||||
enc, err := tiktoken.GetEncoding(tiktoken.MODEL_O200K_BASE)
|
||||
if err != nil {
|
||||
enc, err = tiktoken.GetEncoding(tiktoken.MODEL_CL100K_BASE)
|
||||
}
|
||||
if err == nil {
|
||||
claudeTokenizer = enc
|
||||
}
|
||||
})
|
||||
return claudeTokenizer
|
||||
}
|
||||
|
||||
func estimateTokensByThirdPartyTokenizer(text string) (int, bool) {
|
||||
enc := getClaudeTokenizer()
|
||||
if enc == nil {
|
||||
return 0, false
|
||||
}
|
||||
tokens := len(enc.EncodeOrdinary(text))
|
||||
if tokens <= 0 {
|
||||
return 0, false
|
||||
}
|
||||
return tokens, true
|
||||
}
|
||||
@@ -1,239 +0,0 @@
|
||||
package service
|
||||
|
||||
import "context"
|
||||
|
||||
// gatewayAffinityFlow encapsulates affinity-specific scheduling steps so the
|
||||
// main account selection flow can stay focused on generic scheduling.
|
||||
type gatewayAffinityFlow struct {
|
||||
svc *GatewayService
|
||||
ctx context.Context
|
||||
groupID *int64
|
||||
sessionHash string
|
||||
requestedModel string
|
||||
affinityClientID string
|
||||
affinityUserID int64
|
||||
platform string
|
||||
useMixed bool
|
||||
accountByID map[int64]*Account
|
||||
isExcluded func(int64) bool
|
||||
}
|
||||
|
||||
type affinityWaitCandidate struct {
|
||||
account *Account
|
||||
}
|
||||
|
||||
func newGatewayAffinityFlow(
|
||||
svc *GatewayService,
|
||||
ctx context.Context,
|
||||
groupID *int64,
|
||||
sessionHash string,
|
||||
requestedModel string,
|
||||
affinityClientID string,
|
||||
affinityUserID int64,
|
||||
platform string,
|
||||
useMixed bool,
|
||||
accountByID map[int64]*Account,
|
||||
isExcluded func(int64) bool,
|
||||
) *gatewayAffinityFlow {
|
||||
return &gatewayAffinityFlow{
|
||||
svc: svc,
|
||||
ctx: ctx,
|
||||
groupID: groupID,
|
||||
sessionHash: sessionHash,
|
||||
requestedModel: requestedModel,
|
||||
affinityClientID: affinityClientID,
|
||||
affinityUserID: affinityUserID,
|
||||
platform: platform,
|
||||
useMixed: useMixed,
|
||||
accountByID: accountByID,
|
||||
isExcluded: isExcluded,
|
||||
}
|
||||
}
|
||||
|
||||
// shouldFilterAccountWithoutClientID excludes affinity-enabled Anthropic accounts
|
||||
// when metadata.user_id does not provide a usable client_id.
|
||||
func shouldFilterAccountWithoutClientID(account *Account, affinityClientID string) bool {
|
||||
if account == nil || affinityClientID != "" {
|
||||
return false
|
||||
}
|
||||
if account.Platform != PlatformAnthropic {
|
||||
return false
|
||||
}
|
||||
return account.IsAffinityEnabled()
|
||||
}
|
||||
|
||||
func filterAccountsWithoutClientID(accounts []Account, affinityClientID string) []Account {
|
||||
if affinityClientID != "" {
|
||||
return accounts
|
||||
}
|
||||
filtered := make([]Account, 0, len(accounts))
|
||||
for _, acc := range accounts {
|
||||
if shouldFilterAccountWithoutClientID(&acc, affinityClientID) {
|
||||
continue
|
||||
}
|
||||
filtered = append(filtered, acc)
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
func (f *gatewayAffinityFlow) preprocessPinnedUsers(accounts []Account) {
|
||||
if f.affinityUserID <= 0 || f.affinityClientID == "" || f.svc.cache == nil {
|
||||
return
|
||||
}
|
||||
for i := range accounts {
|
||||
if accounts[i].IsPinnedUser(f.affinityUserID) && accounts[i].IsAffinityEnabled() {
|
||||
_ = f.svc.cache.UpdateAffinity(
|
||||
f.ctx,
|
||||
derefGroupID(f.groupID),
|
||||
f.affinityUserID,
|
||||
f.affinityClientID,
|
||||
accounts[i].ID,
|
||||
ClientAffinityTTL,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// trySelectAffinityAccount runs Layer 1.4 and returns:
|
||||
// - result != nil: affinity path selected an account or wait plan
|
||||
// - affinityHit == true: an effective affinity-enabled record was considered and should suppress sticky fallback
|
||||
func (f *gatewayAffinityFlow) trySelectAffinityAccount() (*AccountSelectionResult, bool, error) {
|
||||
if f.affinityClientID == "" || f.affinityUserID <= 0 || f.svc.cache == nil {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
gid := derefGroupID(f.groupID)
|
||||
affinityAccountIDs, err := f.svc.cache.GetAffinityAccounts(f.ctx, gid, f.affinityUserID, f.affinityClientID, ClientAffinityTTL)
|
||||
if err != nil || len(affinityAccountIDs) == 0 {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
noSwitchBlocked := false
|
||||
anyAllowSwitch := false
|
||||
effectiveAffinityHit := false
|
||||
waitCandidates := make(map[int64]*affinityWaitCandidate)
|
||||
|
||||
for _, affinityAccID := range affinityAccountIDs {
|
||||
account, ok := f.accountByID[affinityAccID]
|
||||
|
||||
if f.isExcluded != nil && f.isExcluded(affinityAccID) {
|
||||
checkAcc := account
|
||||
if !ok && f.svc.accountRepo != nil {
|
||||
if acc, repoErr := f.svc.accountRepo.GetByID(f.ctx, affinityAccID); repoErr == nil && acc != nil {
|
||||
checkAcc = acc
|
||||
}
|
||||
}
|
||||
if checkAcc != nil && checkAcc.IsAffinityEnabled() {
|
||||
effectiveAffinityHit = true
|
||||
if !checkAcc.IsAffinityAllowSwitch() {
|
||||
noSwitchBlocked = true
|
||||
} else {
|
||||
anyAllowSwitch = true
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if !ok || !f.svc.isAccountSchedulableForSelection(account) {
|
||||
checkAcc := account
|
||||
if !ok && f.svc.accountRepo != nil {
|
||||
if acc, repoErr := f.svc.accountRepo.GetByID(f.ctx, affinityAccID); repoErr == nil && acc != nil {
|
||||
checkAcc = acc
|
||||
}
|
||||
}
|
||||
if checkAcc != nil && checkAcc.IsAffinityEnabled() {
|
||||
effectiveAffinityHit = true
|
||||
if !checkAcc.IsAffinityAllowSwitch() {
|
||||
noSwitchBlocked = true
|
||||
} else {
|
||||
anyAllowSwitch = true
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if !account.IsAffinityEnabled() {
|
||||
continue
|
||||
}
|
||||
effectiveAffinityHit = true
|
||||
if account.IsAffinityAllowSwitch() {
|
||||
anyAllowSwitch = true
|
||||
}
|
||||
|
||||
if !f.svc.isAccountAllowedForPlatform(account, f.platform, f.useMixed) ||
|
||||
(f.requestedModel != "" && !f.svc.isModelSupportedByAccountWithContext(f.ctx, account, f.requestedModel)) ||
|
||||
!f.svc.isAccountSchedulableForModelSelection(f.ctx, account, f.requestedModel) ||
|
||||
!f.svc.isAccountSchedulableForQuota(account) ||
|
||||
!f.svc.isAccountSchedulableForWindowCost(f.ctx, account, false) ||
|
||||
!f.svc.isAccountSchedulableForRPM(f.ctx, account, false) {
|
||||
if !account.IsAffinityAllowSwitch() {
|
||||
noSwitchBlocked = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
userCount, clientCount, perUserCount, multiErr := f.svc.cache.GetAffinityMultiCount(
|
||||
f.ctx, gid, affinityAccID, f.affinityUserID, ClientAffinityTTL,
|
||||
)
|
||||
if multiErr == nil {
|
||||
zone := account.GetMultiDimAffinityZone(userCount, clientCount, perUserCount)
|
||||
if zone == AffinityZoneRed {
|
||||
if !account.IsAffinityAllowSwitch() {
|
||||
noSwitchBlocked = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
result, acquireErr := f.svc.tryAcquireAccountSlot(f.ctx, affinityAccID, account.Concurrency)
|
||||
if acquireErr == nil && result.Acquired {
|
||||
if !f.svc.checkAndRegisterSession(f.ctx, account, f.sessionHash) {
|
||||
result.ReleaseFunc()
|
||||
continue
|
||||
}
|
||||
_ = f.svc.cache.UpdateAffinity(f.ctx, gid, f.affinityUserID, f.affinityClientID, affinityAccID, ClientAffinityTTL)
|
||||
if f.sessionHash != "" {
|
||||
_ = f.svc.cache.SetSessionAccountID(f.ctx, gid, f.sessionHash, affinityAccID, stickySessionTTL)
|
||||
}
|
||||
return &AccountSelectionResult{
|
||||
Account: account,
|
||||
Acquired: true,
|
||||
ReleaseFunc: result.ReleaseFunc,
|
||||
}, true, nil
|
||||
}
|
||||
if acquireErr == nil && !result.Acquired && !account.IsAffinityAllowSwitch() {
|
||||
noSwitchBlocked = true
|
||||
waitCandidates[affinityAccID] = &affinityWaitCandidate{account: account}
|
||||
}
|
||||
}
|
||||
|
||||
if noSwitchBlocked && !anyAllowSwitch && f.svc.concurrencyService != nil {
|
||||
for _, waitAccID := range affinityAccountIDs {
|
||||
candidate, ok := waitCandidates[waitAccID]
|
||||
if !ok || candidate == nil || candidate.account == nil {
|
||||
continue
|
||||
}
|
||||
acc := candidate.account
|
||||
waitingCount, _ := f.svc.concurrencyService.GetAccountWaitingCount(f.ctx, waitAccID)
|
||||
if waitingCount >= f.svc.schedulingConfig().StickySessionMaxWaiting {
|
||||
continue
|
||||
}
|
||||
if !f.svc.checkAndRegisterSession(f.ctx, acc, f.sessionHash) {
|
||||
continue
|
||||
}
|
||||
cfg := f.svc.schedulingConfig()
|
||||
return &AccountSelectionResult{
|
||||
Account: acc,
|
||||
WaitPlan: &AccountWaitPlan{
|
||||
AccountID: waitAccID,
|
||||
MaxConcurrency: acc.Concurrency,
|
||||
Timeout: cfg.StickySessionWaitTimeout,
|
||||
MaxWaiting: cfg.StickySessionMaxWaiting,
|
||||
},
|
||||
}, true, nil
|
||||
}
|
||||
return nil, effectiveAffinityHit, ErrAffinityNoSwitch
|
||||
}
|
||||
|
||||
return nil, effectiveAffinityHit, nil
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,196 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/antigravity"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
||||
type claudeMaxResponseRewriteContext struct {
|
||||
Parsed *ParsedRequest
|
||||
Group *Group
|
||||
}
|
||||
|
||||
type claudeMaxResponseRewriteContextKeyType struct{}
|
||||
|
||||
var claudeMaxResponseRewriteContextKey = claudeMaxResponseRewriteContextKeyType{}
|
||||
|
||||
func withClaudeMaxResponseRewriteContext(ctx context.Context, c *gin.Context, parsed *ParsedRequest) context.Context {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
value := claudeMaxResponseRewriteContext{
|
||||
Parsed: parsed,
|
||||
Group: claudeMaxGroupFromGinContext(c),
|
||||
}
|
||||
return context.WithValue(ctx, claudeMaxResponseRewriteContextKey, value)
|
||||
}
|
||||
|
||||
func claudeMaxResponseRewriteContextFromContext(ctx context.Context) claudeMaxResponseRewriteContext {
|
||||
if ctx == nil {
|
||||
return claudeMaxResponseRewriteContext{}
|
||||
}
|
||||
value, _ := ctx.Value(claudeMaxResponseRewriteContextKey).(claudeMaxResponseRewriteContext)
|
||||
return value
|
||||
}
|
||||
|
||||
func claudeMaxGroupFromGinContext(c *gin.Context) *Group {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
raw, exists := c.Get("api_key")
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
apiKey, ok := raw.(*APIKey)
|
||||
if !ok || apiKey == nil {
|
||||
return nil
|
||||
}
|
||||
return apiKey.Group
|
||||
}
|
||||
|
||||
func parsedRequestFromGinContext(c *gin.Context) *ParsedRequest {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
raw, exists := c.Get("parsed_request")
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
parsed, _ := raw.(*ParsedRequest)
|
||||
return parsed
|
||||
}
|
||||
|
||||
func applyClaudeMaxSimulationToUsage(ctx context.Context, usage *ClaudeUsage, model string, accountID int64) claudeMaxCacheBillingOutcome {
|
||||
var out claudeMaxCacheBillingOutcome
|
||||
if usage == nil {
|
||||
return out
|
||||
}
|
||||
rewriteCtx := claudeMaxResponseRewriteContextFromContext(ctx)
|
||||
return applyClaudeMaxCacheBillingPolicyToUsage(usage, rewriteCtx.Parsed, rewriteCtx.Group, model, accountID)
|
||||
}
|
||||
|
||||
func applyClaudeMaxSimulationToUsageJSONMap(ctx context.Context, usageObj map[string]any, model string, accountID int64) claudeMaxCacheBillingOutcome {
|
||||
var out claudeMaxCacheBillingOutcome
|
||||
if usageObj == nil {
|
||||
return out
|
||||
}
|
||||
usage := claudeUsageFromJSONMap(usageObj)
|
||||
out = applyClaudeMaxSimulationToUsage(ctx, &usage, model, accountID)
|
||||
if out.Simulated {
|
||||
rewriteClaudeUsageJSONMap(usageObj, usage)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func rewriteClaudeUsageJSONBytes(body []byte, usage ClaudeUsage) []byte {
|
||||
updated := body
|
||||
var err error
|
||||
|
||||
updated, err = sjson.SetBytes(updated, "usage.input_tokens", usage.InputTokens)
|
||||
if err != nil {
|
||||
return body
|
||||
}
|
||||
updated, err = sjson.SetBytes(updated, "usage.cache_creation_input_tokens", usage.CacheCreationInputTokens)
|
||||
if err != nil {
|
||||
return body
|
||||
}
|
||||
updated, err = sjson.SetBytes(updated, "usage.cache_creation.ephemeral_5m_input_tokens", usage.CacheCreation5mTokens)
|
||||
if err != nil {
|
||||
return body
|
||||
}
|
||||
updated, err = sjson.SetBytes(updated, "usage.cache_creation.ephemeral_1h_input_tokens", usage.CacheCreation1hTokens)
|
||||
if err != nil {
|
||||
return body
|
||||
}
|
||||
return updated
|
||||
}
|
||||
|
||||
func claudeUsageFromJSONMap(usageObj map[string]any) ClaudeUsage {
|
||||
var usage ClaudeUsage
|
||||
if usageObj == nil {
|
||||
return usage
|
||||
}
|
||||
|
||||
usage.InputTokens = usageIntFromAny(usageObj["input_tokens"])
|
||||
usage.OutputTokens = usageIntFromAny(usageObj["output_tokens"])
|
||||
usage.CacheCreationInputTokens = usageIntFromAny(usageObj["cache_creation_input_tokens"])
|
||||
usage.CacheReadInputTokens = usageIntFromAny(usageObj["cache_read_input_tokens"])
|
||||
|
||||
if ccObj, ok := usageObj["cache_creation"].(map[string]any); ok {
|
||||
usage.CacheCreation5mTokens = usageIntFromAny(ccObj["ephemeral_5m_input_tokens"])
|
||||
usage.CacheCreation1hTokens = usageIntFromAny(ccObj["ephemeral_1h_input_tokens"])
|
||||
}
|
||||
return usage
|
||||
}
|
||||
|
||||
func rewriteClaudeUsageJSONMap(usageObj map[string]any, usage ClaudeUsage) {
|
||||
if usageObj == nil {
|
||||
return
|
||||
}
|
||||
usageObj["input_tokens"] = usage.InputTokens
|
||||
usageObj["cache_creation_input_tokens"] = usage.CacheCreationInputTokens
|
||||
|
||||
ccObj, _ := usageObj["cache_creation"].(map[string]any)
|
||||
if ccObj == nil {
|
||||
ccObj = make(map[string]any, 2)
|
||||
usageObj["cache_creation"] = ccObj
|
||||
}
|
||||
ccObj["ephemeral_5m_input_tokens"] = usage.CacheCreation5mTokens
|
||||
ccObj["ephemeral_1h_input_tokens"] = usage.CacheCreation1hTokens
|
||||
}
|
||||
|
||||
func usageIntFromAny(v any) int {
|
||||
switch value := v.(type) {
|
||||
case int:
|
||||
return value
|
||||
case int64:
|
||||
return int(value)
|
||||
case float64:
|
||||
return int(value)
|
||||
case json.Number:
|
||||
if n, err := value.Int64(); err == nil {
|
||||
return int(n)
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// setupClaudeMaxStreamingHook 为 Antigravity 流式路径设置 SSE usage 改写 hook。
|
||||
func setupClaudeMaxStreamingHook(c *gin.Context, processor *antigravity.StreamingProcessor, originalModel string, accountID int64) {
|
||||
group := claudeMaxGroupFromGinContext(c)
|
||||
parsed := parsedRequestFromGinContext(c)
|
||||
if !shouldApplyClaudeMaxBillingRulesForUsage(group, originalModel, parsed) {
|
||||
return
|
||||
}
|
||||
processor.SetUsageMapHook(func(usageMap map[string]any) {
|
||||
svcUsage := claudeUsageFromJSONMap(usageMap)
|
||||
outcome := applyClaudeMaxCacheBillingPolicyToUsage(&svcUsage, parsed, group, originalModel, accountID)
|
||||
if outcome.Simulated {
|
||||
rewriteClaudeUsageJSONMap(usageMap, svcUsage)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// applyClaudeMaxNonStreamingRewrite 为 Antigravity 非流式路径改写响应体中的 usage。
|
||||
func applyClaudeMaxNonStreamingRewrite(c *gin.Context, claudeResp []byte, agUsage *antigravity.ClaudeUsage, originalModel string, accountID int64) []byte {
|
||||
group := claudeMaxGroupFromGinContext(c)
|
||||
parsed := parsedRequestFromGinContext(c)
|
||||
if !shouldApplyClaudeMaxBillingRulesForUsage(group, originalModel, parsed) {
|
||||
return claudeResp
|
||||
}
|
||||
svcUsage := &ClaudeUsage{
|
||||
InputTokens: agUsage.InputTokens,
|
||||
OutputTokens: agUsage.OutputTokens,
|
||||
CacheCreationInputTokens: agUsage.CacheCreationInputTokens,
|
||||
CacheReadInputTokens: agUsage.CacheReadInputTokens,
|
||||
}
|
||||
outcome := applyClaudeMaxCacheBillingPolicyToUsage(svcUsage, parsed, group, originalModel, accountID)
|
||||
if outcome.Simulated {
|
||||
return rewriteClaudeUsageJSONBytes(claudeResp, *svcUsage)
|
||||
}
|
||||
return claudeResp
|
||||
}
|
||||
@@ -143,28 +143,6 @@ func (s *stickyGatewayCacheHotpathStub) RefreshSessionTTL(ctx context.Context, g
|
||||
func (s *stickyGatewayCacheHotpathStub) DeleteSessionAccountID(ctx context.Context, groupID int64, sessionHash string) error {
|
||||
return nil
|
||||
}
|
||||
func (s *stickyGatewayCacheHotpathStub) GetAffinityAccounts(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (s *stickyGatewayCacheHotpathStub) UpdateAffinity(_ context.Context, _ int64, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (s *stickyGatewayCacheHotpathStub) GetAffinityMultiCount(_ context.Context, _ int64, _ int64, _ int64, _ time.Duration) (int64, int64, int64, error) {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
func (s *stickyGatewayCacheHotpathStub) GetAccountAffinityCountBatch(_ context.Context, _ int64, _ []int64, _ time.Duration) (map[int64]int64, error) {
|
||||
return map[int64]int64{}, nil
|
||||
}
|
||||
func (s *stickyGatewayCacheHotpathStub) GetAccountAffinityClientsBatch(_ context.Context, _ map[int64][]int64, _ time.Duration) (map[int64][]string, error) {
|
||||
return map[int64][]string{}, nil
|
||||
}
|
||||
func (s *stickyGatewayCacheHotpathStub) GetAccountAffinityClientsWithScores(_ context.Context, _ int64, _ []int64, _ time.Duration) ([]AffinityClient, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (s *stickyGatewayCacheHotpathStub) ClearAccountAffinity(_ context.Context, _ int64, _ []int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *modelsListAccountRepoStub) ListSchedulableByGroupID(ctx context.Context, groupID int64) ([]Account, error) {
|
||||
s.listByGroupCalls.Add(1)
|
||||
if s.err != nil {
|
||||
|
||||
@@ -235,28 +235,6 @@ func (m *mockGatewayCacheForPlatform) DeleteSessionAccountID(ctx context.Context
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockGatewayCacheForPlatform) GetAffinityAccounts(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (m *mockGatewayCacheForPlatform) UpdateAffinity(_ context.Context, _ int64, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (m *mockGatewayCacheForPlatform) GetAffinityMultiCount(_ context.Context, _ int64, _ int64, _ int64, _ time.Duration) (int64, int64, int64, error) {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
func (m *mockGatewayCacheForPlatform) GetAccountAffinityCountBatch(_ context.Context, _ int64, _ []int64, _ time.Duration) (map[int64]int64, error) {
|
||||
return map[int64]int64{}, nil
|
||||
}
|
||||
func (m *mockGatewayCacheForPlatform) GetAccountAffinityClientsBatch(_ context.Context, _ map[int64][]int64, _ time.Duration) (map[int64][]string, error) {
|
||||
return map[int64][]string{}, nil
|
||||
}
|
||||
func (m *mockGatewayCacheForPlatform) GetAccountAffinityClientsWithScores(_ context.Context, _ int64, _ []int64, _ time.Duration) ([]AffinityClient, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (m *mockGatewayCacheForPlatform) ClearAccountAffinity(_ context.Context, _ int64, _ []int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type mockGroupRepoForGateway struct {
|
||||
groups map[int64]*Group
|
||||
getByIDCalls int
|
||||
|
||||
@@ -1,199 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/config"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type usageLogRepoRecordUsageStub struct {
|
||||
UsageLogRepository
|
||||
|
||||
last *UsageLog
|
||||
inserted bool
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *usageLogRepoRecordUsageStub) Create(_ context.Context, log *UsageLog) (bool, error) {
|
||||
copied := *log
|
||||
s.last = &copied
|
||||
return s.inserted, s.err
|
||||
}
|
||||
|
||||
func newGatewayServiceForRecordUsageTest(repo UsageLogRepository) *GatewayService {
|
||||
return &GatewayService{
|
||||
usageLogRepo: repo,
|
||||
billingService: NewBillingService(&config.Config{}, nil),
|
||||
cfg: &config.Config{RunMode: config.RunModeSimple},
|
||||
deferredService: &DeferredService{},
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecordUsage_SimulateClaudeMaxEnabled_ProjectsUsageAndSkipsTTLOverride(t *testing.T) {
|
||||
repo := &usageLogRepoRecordUsageStub{inserted: true}
|
||||
svc := newGatewayServiceForRecordUsageTest(repo)
|
||||
|
||||
groupID := int64(11)
|
||||
input := &RecordUsageInput{
|
||||
Result: &ForwardResult{
|
||||
RequestID: "req-sim-1",
|
||||
Model: "claude-sonnet-4",
|
||||
Duration: time.Second,
|
||||
Usage: ClaudeUsage{
|
||||
InputTokens: 160,
|
||||
},
|
||||
},
|
||||
ParsedRequest: &ParsedRequest{
|
||||
Model: "claude-sonnet-4",
|
||||
Messages: []any{
|
||||
map[string]any{
|
||||
"role": "user",
|
||||
"content": []any{
|
||||
map[string]any{
|
||||
"type": "text",
|
||||
"text": "long cached context for prior turns",
|
||||
"cache_control": map[string]any{"type": "ephemeral"},
|
||||
},
|
||||
map[string]any{
|
||||
"type": "text",
|
||||
"text": "please summarize the logs and provide root cause analysis",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
APIKey: &APIKey{
|
||||
ID: 1,
|
||||
GroupID: &groupID,
|
||||
Group: &Group{
|
||||
ID: groupID,
|
||||
Platform: PlatformAnthropic,
|
||||
RateMultiplier: 1,
|
||||
SimulateClaudeMaxEnabled: true,
|
||||
},
|
||||
},
|
||||
User: &User{ID: 2},
|
||||
Account: &Account{
|
||||
ID: 3,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Extra: map[string]any{
|
||||
"cache_ttl_override_enabled": true,
|
||||
"cache_ttl_override_target": "5m",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := svc.RecordUsage(context.Background(), input)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, repo.last)
|
||||
|
||||
log := repo.last
|
||||
require.Equal(t, 80, log.InputTokens)
|
||||
require.Equal(t, 80, log.CacheCreationTokens)
|
||||
require.Equal(t, 0, log.CacheCreation5mTokens)
|
||||
require.Equal(t, 80, log.CacheCreation1hTokens)
|
||||
require.False(t, log.CacheTTLOverridden, "simulate outcome should skip account ttl override")
|
||||
}
|
||||
|
||||
func TestRecordUsage_SimulateClaudeMaxDisabled_AppliesTTLOverride(t *testing.T) {
|
||||
repo := &usageLogRepoRecordUsageStub{inserted: true}
|
||||
svc := newGatewayServiceForRecordUsageTest(repo)
|
||||
|
||||
groupID := int64(12)
|
||||
input := &RecordUsageInput{
|
||||
Result: &ForwardResult{
|
||||
RequestID: "req-sim-2",
|
||||
Model: "claude-sonnet-4",
|
||||
Duration: time.Second,
|
||||
Usage: ClaudeUsage{
|
||||
InputTokens: 40,
|
||||
CacheCreationInputTokens: 120,
|
||||
CacheCreation1hTokens: 120,
|
||||
},
|
||||
},
|
||||
APIKey: &APIKey{
|
||||
ID: 2,
|
||||
GroupID: &groupID,
|
||||
Group: &Group{
|
||||
ID: groupID,
|
||||
Platform: PlatformAnthropic,
|
||||
RateMultiplier: 1,
|
||||
SimulateClaudeMaxEnabled: false,
|
||||
},
|
||||
},
|
||||
User: &User{ID: 3},
|
||||
Account: &Account{
|
||||
ID: 4,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Extra: map[string]any{
|
||||
"cache_ttl_override_enabled": true,
|
||||
"cache_ttl_override_target": "5m",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := svc.RecordUsage(context.Background(), input)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, repo.last)
|
||||
|
||||
log := repo.last
|
||||
require.Equal(t, 120, log.CacheCreationTokens)
|
||||
require.Equal(t, 120, log.CacheCreation5mTokens)
|
||||
require.Equal(t, 0, log.CacheCreation1hTokens)
|
||||
require.True(t, log.CacheTTLOverridden)
|
||||
}
|
||||
|
||||
func TestRecordUsage_SimulateClaudeMaxEnabled_ExistingCacheCreationBypassesSimulation(t *testing.T) {
|
||||
repo := &usageLogRepoRecordUsageStub{inserted: true}
|
||||
svc := newGatewayServiceForRecordUsageTest(repo)
|
||||
|
||||
groupID := int64(13)
|
||||
input := &RecordUsageInput{
|
||||
Result: &ForwardResult{
|
||||
RequestID: "req-sim-3",
|
||||
Model: "claude-sonnet-4",
|
||||
Duration: time.Second,
|
||||
Usage: ClaudeUsage{
|
||||
InputTokens: 20,
|
||||
CacheCreationInputTokens: 120,
|
||||
CacheCreation5mTokens: 120,
|
||||
},
|
||||
},
|
||||
APIKey: &APIKey{
|
||||
ID: 3,
|
||||
GroupID: &groupID,
|
||||
Group: &Group{
|
||||
ID: groupID,
|
||||
Platform: PlatformAnthropic,
|
||||
RateMultiplier: 1,
|
||||
SimulateClaudeMaxEnabled: true,
|
||||
},
|
||||
},
|
||||
User: &User{ID: 4},
|
||||
Account: &Account{
|
||||
ID: 5,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Extra: map[string]any{
|
||||
"cache_ttl_override_enabled": true,
|
||||
"cache_ttl_override_target": "5m",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := svc.RecordUsage(context.Background(), input)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, repo.last)
|
||||
|
||||
log := repo.last
|
||||
require.Equal(t, 20, log.InputTokens)
|
||||
require.Equal(t, 120, log.CacheCreation5mTokens)
|
||||
require.Equal(t, 0, log.CacheCreation1hTokens)
|
||||
require.Equal(t, 120, log.CacheCreationTokens)
|
||||
require.False(t, log.CacheTTLOverridden, "existing cache_creation with SimulateClaudeMax enabled should skip account ttl override")
|
||||
}
|
||||
@@ -1,170 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/config"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func TestHandleNonStreamingResponse_UsageAlignedWithClaudeMaxSimulation(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
svc := &GatewayService{
|
||||
cfg: &config.Config{},
|
||||
rateLimitService: &RateLimitService{},
|
||||
}
|
||||
|
||||
account := &Account{
|
||||
ID: 11,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Extra: map[string]any{
|
||||
"cache_ttl_override_enabled": true,
|
||||
"cache_ttl_override_target": "5m",
|
||||
},
|
||||
}
|
||||
group := &Group{
|
||||
ID: 99,
|
||||
Platform: PlatformAnthropic,
|
||||
SimulateClaudeMaxEnabled: true,
|
||||
}
|
||||
parsed := &ParsedRequest{
|
||||
Model: "claude-sonnet-4",
|
||||
Messages: []any{
|
||||
map[string]any{
|
||||
"role": "user",
|
||||
"content": []any{
|
||||
map[string]any{
|
||||
"type": "text",
|
||||
"text": "long cached context",
|
||||
"cache_control": map[string]any{"type": "ephemeral"},
|
||||
},
|
||||
map[string]any{
|
||||
"type": "text",
|
||||
"text": "new user question",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
upstreamBody := []byte(`{"id":"msg_1","model":"claude-sonnet-4","usage":{"input_tokens":120,"output_tokens":8}}`)
|
||||
resp := &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
Body: ioNopCloserBytes(upstreamBody),
|
||||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
c.Request = httptest.NewRequest(http.MethodPost, "/v1/messages", bytes.NewReader(nil))
|
||||
c.Set("api_key", &APIKey{Group: group})
|
||||
requestCtx := withClaudeMaxResponseRewriteContext(context.Background(), c, parsed)
|
||||
|
||||
usage, err := svc.handleNonStreamingResponse(requestCtx, resp, c, account, "claude-sonnet-4", "claude-sonnet-4")
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, usage)
|
||||
|
||||
var rendered struct {
|
||||
Usage ClaudeUsage `json:"usage"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &rendered))
|
||||
rendered.Usage.CacheCreation5mTokens = int(gjson.GetBytes(rec.Body.Bytes(), "usage.cache_creation.ephemeral_5m_input_tokens").Int())
|
||||
rendered.Usage.CacheCreation1hTokens = int(gjson.GetBytes(rec.Body.Bytes(), "usage.cache_creation.ephemeral_1h_input_tokens").Int())
|
||||
|
||||
require.Equal(t, rendered.Usage.InputTokens, usage.InputTokens)
|
||||
require.Equal(t, rendered.Usage.OutputTokens, usage.OutputTokens)
|
||||
require.Equal(t, rendered.Usage.CacheCreationInputTokens, usage.CacheCreationInputTokens)
|
||||
require.Equal(t, rendered.Usage.CacheCreation5mTokens, usage.CacheCreation5mTokens)
|
||||
require.Equal(t, rendered.Usage.CacheCreation1hTokens, usage.CacheCreation1hTokens)
|
||||
require.Equal(t, rendered.Usage.CacheReadInputTokens, usage.CacheReadInputTokens)
|
||||
|
||||
require.Greater(t, usage.CacheCreation1hTokens, 0)
|
||||
require.Equal(t, 0, usage.CacheCreation5mTokens)
|
||||
require.Less(t, usage.InputTokens, 120)
|
||||
}
|
||||
|
||||
func TestHandleNonStreamingResponse_ClaudeMaxDisabled_NoSimulationIntercept(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
svc := &GatewayService{
|
||||
cfg: &config.Config{},
|
||||
rateLimitService: &RateLimitService{},
|
||||
}
|
||||
|
||||
account := &Account{
|
||||
ID: 12,
|
||||
Platform: PlatformAnthropic,
|
||||
Type: AccountTypeOAuth,
|
||||
Extra: map[string]any{
|
||||
"cache_ttl_override_enabled": true,
|
||||
"cache_ttl_override_target": "5m",
|
||||
},
|
||||
}
|
||||
group := &Group{
|
||||
ID: 100,
|
||||
Platform: PlatformAnthropic,
|
||||
SimulateClaudeMaxEnabled: false,
|
||||
}
|
||||
parsed := &ParsedRequest{
|
||||
Model: "claude-sonnet-4",
|
||||
Messages: []any{
|
||||
map[string]any{
|
||||
"role": "user",
|
||||
"content": []any{
|
||||
map[string]any{
|
||||
"type": "text",
|
||||
"text": "long cached context",
|
||||
"cache_control": map[string]any{"type": "ephemeral"},
|
||||
},
|
||||
map[string]any{
|
||||
"type": "text",
|
||||
"text": "new user question",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
upstreamBody := []byte(`{"id":"msg_2","model":"claude-sonnet-4","usage":{"input_tokens":120,"output_tokens":8}}`)
|
||||
resp := &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
Body: ioNopCloserBytes(upstreamBody),
|
||||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
c.Request = httptest.NewRequest(http.MethodPost, "/v1/messages", bytes.NewReader(nil))
|
||||
c.Set("api_key", &APIKey{Group: group})
|
||||
requestCtx := withClaudeMaxResponseRewriteContext(context.Background(), c, parsed)
|
||||
|
||||
usage, err := svc.handleNonStreamingResponse(requestCtx, resp, c, account, "claude-sonnet-4", "claude-sonnet-4")
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, usage)
|
||||
|
||||
require.Equal(t, 120, usage.InputTokens)
|
||||
require.Equal(t, 0, usage.CacheCreationInputTokens)
|
||||
require.Equal(t, 0, usage.CacheCreation5mTokens)
|
||||
require.Equal(t, 0, usage.CacheCreation1hTokens)
|
||||
}
|
||||
|
||||
func ioNopCloserBytes(b []byte) *readCloserFromBytes {
|
||||
return &readCloserFromBytes{Reader: bytes.NewReader(b)}
|
||||
}
|
||||
|
||||
type readCloserFromBytes struct {
|
||||
*bytes.Reader
|
||||
}
|
||||
|
||||
func (r *readCloserFromBytes) Close() error {
|
||||
return nil
|
||||
}
|
||||
@@ -42,9 +42,8 @@ import (
|
||||
const (
|
||||
claudeAPIURL = "https://api.anthropic.com/v1/messages?beta=true"
|
||||
claudeAPICountTokensURL = "https://api.anthropic.com/v1/messages/count_tokens?beta=true"
|
||||
stickySessionTTL = time.Hour // 粘性会话TTL
|
||||
ClientAffinityTTL = 24 * time.Hour // 客户端亲和TTL
|
||||
defaultMaxLineSize = 500 * 1024 * 1024
|
||||
stickySessionTTL = time.Hour // 粘性会话TTL
|
||||
defaultMaxLineSize = 500 * 1024 * 1024
|
||||
// Canonical Claude Code banner. Keep it EXACT (no trailing whitespace/newlines)
|
||||
// to match real Claude CLI traffic as closely as possible. When we need a visual
|
||||
// separator between system blocks, we add "\n\n" at concatenation time.
|
||||
@@ -68,21 +67,14 @@ const (
|
||||
MediaTypePrompt = "prompt"
|
||||
)
|
||||
|
||||
const (
|
||||
claudeMaxMessageOverheadTokens = 3
|
||||
claudeMaxBlockOverheadTokens = 1
|
||||
claudeMaxUnknownContentTokens = 4
|
||||
)
|
||||
|
||||
// ForceCacheBillingContextKey 强制缓存计费上下文键
|
||||
// 用于粘性会话切换时,将 input_tokens 转为 cache_read_input_tokens 计费
|
||||
type forceCacheBillingKeyType struct{}
|
||||
|
||||
// accountWithLoad 账号与负载信息的组合,用于负载感知调度
|
||||
type accountWithLoad struct {
|
||||
account *Account
|
||||
loadInfo *AccountLoadInfo
|
||||
affinityCount int64 // 亲和客户端数量(反向索引),越少越优先
|
||||
account *Account
|
||||
loadInfo *AccountLoadInfo
|
||||
}
|
||||
|
||||
var ForceCacheBillingContextKey = forceCacheBillingKeyType{}
|
||||
@@ -346,10 +338,6 @@ var (
|
||||
sseDataRe = regexp.MustCompile(`^data:\s*`)
|
||||
claudeCliUserAgentRe = regexp.MustCompile(`^claude-cli/\d+\.\d+\.\d+`)
|
||||
|
||||
// clientIDFromMetadataRegex 从 metadata.user_id 中提取客户端 ID(64位 hex)
|
||||
// 格式: user_{64位hex}_account_...
|
||||
clientIDFromMetadataRegex = regexp.MustCompile(`^user_([a-f0-9]{64})_account_`)
|
||||
|
||||
// claudeCodePromptPrefixes 用于检测 Claude Code 系统提示词的前缀列表
|
||||
// 支持多种变体:标准版、Agent SDK 版、Explore Agent 版、Compact 版等
|
||||
// 注意:前缀之间不应存在包含关系,否则会导致冗余匹配
|
||||
@@ -367,12 +355,6 @@ var ErrNoAvailableAccounts = errors.New("no available accounts")
|
||||
// ErrClaudeCodeOnly 表示分组仅允许 Claude Code 客户端访问
|
||||
var ErrClaudeCodeOnly = errors.New("this group only allows Claude Code clients")
|
||||
|
||||
// ErrAffinityNoSwitch 表示亲和账号不可用且不允许切换到其他账号
|
||||
var ErrAffinityNoSwitch = errors.New("affinity account unavailable and switching is disabled")
|
||||
|
||||
// ErrAffinityLimitExceeded 表示亲和客户端限制已达上限
|
||||
var ErrAffinityLimitExceeded = errors.New("affinity client limit exceeded")
|
||||
|
||||
// allowedHeaders 白名单headers(参考CRS项目)
|
||||
var allowedHeaders = map[string]bool{
|
||||
"accept": true,
|
||||
@@ -416,39 +398,6 @@ type GatewayCache interface {
|
||||
// DeleteSessionAccountID 删除粘性会话绑定,用于账号不可用时主动清理
|
||||
// Delete sticky session binding, used to proactively clean up when account becomes unavailable
|
||||
DeleteSessionAccountID(ctx context.Context, groupID int64, sessionHash string) error
|
||||
|
||||
// GetAffinityAccounts 获取亲和账号列表(按最近使用降序),同时清理过期成员
|
||||
GetAffinityAccounts(ctx context.Context, groupID int64, userID int64, clientID string, ttl time.Duration) ([]int64, error)
|
||||
// UpdateAffinity 添加/更新亲和关系(更新 score 为当前时间戳,刷新 key TTL)
|
||||
UpdateAffinity(ctx context.Context, groupID int64, userID int64, clientID string, accountID int64, ttl time.Duration) error
|
||||
// GetAccountAffinityCountBatch 批量获取账号的亲和成员数量(惰性清理过期成员)
|
||||
GetAccountAffinityCountBatch(ctx context.Context, groupID int64, accountIDs []int64, ttl time.Duration) (map[int64]int64, error)
|
||||
// GetAccountAffinityClientsBatch 批量获取每个账号跨所有分组的亲和成员列表(去重)
|
||||
// accountGroups: map[accountID][]groupID
|
||||
// 返回值成员格式为 {userID}/{clientID}
|
||||
GetAccountAffinityClientsBatch(ctx context.Context, accountGroups map[int64][]int64, ttl time.Duration) (map[int64][]string, error)
|
||||
// GetAccountAffinityClientsWithScores 获取单个账号跨所有分组的亲和客户端列表(含最后活跃时间)
|
||||
GetAccountAffinityClientsWithScores(ctx context.Context, accountID int64, groupIDs []int64, ttl time.Duration) ([]AffinityClient, error)
|
||||
// ClearAccountAffinity 清除指定账号在所有分组的亲和记录(正向+反向索引)
|
||||
// 用于账号关闭亲和时立即清理旧绑定
|
||||
ClearAccountAffinity(ctx context.Context, accountID int64, groupIDs []int64) error
|
||||
// GetAffinityMultiCount 获取账号的多维度亲和计数
|
||||
// 返回: uniqueUsers, uniqueClients, perUserClients
|
||||
GetAffinityMultiCount(ctx context.Context, groupID int64, accountID int64, targetUserID int64, ttl time.Duration) (users, clients, perUser int64, err error)
|
||||
}
|
||||
|
||||
// AffinityClient 亲和客户端信息(含用户 ID 和最后活跃时间)
|
||||
type AffinityClient struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
ClientID string `json:"client_id"`
|
||||
LastActive time.Time `json:"last_active"`
|
||||
}
|
||||
|
||||
// SortAffinityClients 按最后活跃时间降序排序
|
||||
func SortAffinityClients(clients []AffinityClient) {
|
||||
sort.Slice(clients, func(i, j int) bool {
|
||||
return clients[i].LastActive.After(clients[j].LastActive)
|
||||
})
|
||||
}
|
||||
|
||||
// derefGroupID safely dereferences *int64 to int64, returning 0 if nil
|
||||
@@ -519,20 +468,6 @@ func shouldClearStickySession(account *Account, requestedModel string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// extractClientIDFromMetadata 从 metadata.user_id 中提取客户端 ID(64位 hex)。
|
||||
// 格式: user_{64位hex}_account_..._session_...
|
||||
// 返回空字符串表示无法提取(非 Claude Code/Console 客户端)。
|
||||
func extractClientIDFromMetadata(metadataUserID string) string {
|
||||
if metadataUserID == "" {
|
||||
return ""
|
||||
}
|
||||
matches := clientIDFromMetadataRegex.FindStringSubmatch(metadataUserID)
|
||||
if matches == nil {
|
||||
return ""
|
||||
}
|
||||
return matches[1]
|
||||
}
|
||||
|
||||
type AccountWaitPlan struct {
|
||||
AccountID int64
|
||||
MaxConcurrency int
|
||||
@@ -1318,10 +1253,6 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
}
|
||||
}
|
||||
|
||||
// 提取客户端 ID(用于客户端亲和调度)
|
||||
affinityClientID := extractClientIDFromMetadata(metadataUserID)
|
||||
affinityUserID := sub2apiUserID
|
||||
|
||||
if s.debugModelRoutingEnabled() && requestedModel != "" {
|
||||
groupPlatform := ""
|
||||
if group != nil {
|
||||
@@ -1343,10 +1274,6 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if shouldFilterAccountWithoutClientID(account, affinityClientID) {
|
||||
localExcluded[account.ID] = struct{}{}
|
||||
continue
|
||||
}
|
||||
|
||||
result, err := s.tryAcquireAccountSlot(ctx, account.ID, account.Concurrency)
|
||||
if err == nil && result.Acquired {
|
||||
@@ -1408,7 +1335,6 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
accounts = filterAccountsWithoutClientID(accounts, affinityClientID)
|
||||
if len(accounts) == 0 {
|
||||
return nil, ErrNoAvailableAccounts
|
||||
}
|
||||
@@ -1427,19 +1353,6 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
_, excluded := excludedIDs[accountID]
|
||||
return excluded
|
||||
}
|
||||
affinityFlow := newGatewayAffinityFlow(
|
||||
s,
|
||||
ctx,
|
||||
groupID,
|
||||
sessionHash,
|
||||
requestedModel,
|
||||
affinityClientID,
|
||||
affinityUserID,
|
||||
platform,
|
||||
useMixed,
|
||||
accountByID,
|
||||
isExcluded,
|
||||
)
|
||||
|
||||
// 获取模型路由配置(仅 anthropic 平台)
|
||||
var routingAccountIDs []int64
|
||||
@@ -1602,10 +1515,7 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
}
|
||||
|
||||
if len(routingAvailable) > 0 {
|
||||
// 批量获取亲和客户端数量
|
||||
s.populateAffinityCounts(ctx, routingAvailable, derefGroupID(groupID))
|
||||
|
||||
// 排序:优先级 > 负载率 > 亲和客户端数 > 最后使用时间
|
||||
// 排序:优先级 > 负载率 > 最后使用时间
|
||||
sort.SliceStable(routingAvailable, func(i, j int) bool {
|
||||
a, b := routingAvailable[i], routingAvailable[j]
|
||||
if a.account.Priority != b.account.Priority {
|
||||
@@ -1614,9 +1524,6 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
if a.loadInfo.LoadRate != b.loadInfo.LoadRate {
|
||||
return a.loadInfo.LoadRate < b.loadInfo.LoadRate
|
||||
}
|
||||
if a.affinityCount != b.affinityCount {
|
||||
return a.affinityCount < b.affinityCount
|
||||
}
|
||||
switch {
|
||||
case a.account.LastUsedAt == nil && b.account.LastUsedAt != nil:
|
||||
return true
|
||||
@@ -1642,9 +1549,6 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
if sessionHash != "" && s.cache != nil {
|
||||
_ = s.cache.SetSessionAccountID(ctx, derefGroupID(groupID), sessionHash, item.account.ID, stickySessionTTL)
|
||||
}
|
||||
if affinityClientID != "" && affinityUserID > 0 && s.cache != nil && item.account.IsAffinityEnabled() {
|
||||
_ = s.cache.UpdateAffinity(ctx, derefGroupID(groupID), affinityUserID, affinityClientID, item.account.ID, ClientAffinityTTL)
|
||||
}
|
||||
if s.debugModelRoutingEnabled() {
|
||||
logger.LegacyPrintf("service.gateway", "[ModelRoutingDebug] routed select: group_id=%v model=%s session=%s account=%d", derefGroupID(groupID), requestedModel, shortSessionHash(sessionHash), item.account.ID)
|
||||
}
|
||||
@@ -1682,22 +1586,8 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
}
|
||||
}
|
||||
|
||||
// ============ Layer 1.3: 用户亲和预处理(pinned_users 自动注入) ============
|
||||
affinityFlow.preprocessPinnedUsers(accounts)
|
||||
|
||||
// ============ Layer 1.4: 客户端亲和调度(优先于粘性会话) ============
|
||||
affinityHit := false
|
||||
if affinityResult, hit, err := affinityFlow.trySelectAffinityAccount(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
affinityHit = hit
|
||||
if affinityResult != nil {
|
||||
return affinityResult, nil
|
||||
}
|
||||
}
|
||||
|
||||
// ============ Layer 1.5: 粘性会话(仅在无模型路由配置 且 亲和未命中时生效) ============
|
||||
if !affinityHit && len(routingAccountIDs) == 0 && sessionHash != "" && stickyAccountID > 0 && !isExcluded(stickyAccountID) {
|
||||
// ============ Layer 1.5: 粘性会话(仅在无模型路由配置时生效) ============
|
||||
if len(routingAccountIDs) == 0 && sessionHash != "" && stickyAccountID > 0 && !isExcluded(stickyAccountID) {
|
||||
accountID := stickyAccountID
|
||||
if accountID > 0 && !isExcluded(accountID) {
|
||||
account, ok := accountByID[accountID]
|
||||
@@ -1803,9 +1693,6 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
loadMap, err := s.concurrencyService.GetAccountsLoadBatch(ctx, accountLoads)
|
||||
if err != nil {
|
||||
if result, ok := s.tryAcquireByLegacyOrder(ctx, candidates, groupID, sessionHash, preferOAuth); ok {
|
||||
if affinityClientID != "" && affinityUserID > 0 && s.cache != nil && result.Account != nil && result.Account.IsAffinityEnabled() {
|
||||
_ = s.cache.UpdateAffinity(ctx, derefGroupID(groupID), affinityUserID, affinityClientID, result.Account.ID, ClientAffinityTTL)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
} else {
|
||||
@@ -1823,37 +1710,13 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
}
|
||||
}
|
||||
|
||||
// 批量获取亲和客户端数量(用于均衡分配新客户端)
|
||||
s.populateAffinityCounts(ctx, available, derefGroupID(groupID))
|
||||
|
||||
// 分层过滤选择:优先级 → 亲和三区 → 负载率 → 亲和客户端数 → LRU
|
||||
// 分层过滤选择:优先级 → 负载率 → LRU
|
||||
for len(available) > 0 {
|
||||
// 1. 取优先级最小的集合
|
||||
candidates := filterByMinPriority(available)
|
||||
// 2. 按亲和三区过滤:绿区优先 → 黄区降级 → 红区移除(在同优先级内)
|
||||
candidates = classifyByAffinityZone(candidates)
|
||||
if len(candidates) == 0 {
|
||||
// 当前优先级组全部在红区,移除后回退到下一优先级组
|
||||
minPri := available[0].account.Priority
|
||||
for _, a := range available[1:] {
|
||||
if a.account.Priority < minPri {
|
||||
minPri = a.account.Priority
|
||||
}
|
||||
}
|
||||
newAvailable := make([]accountWithLoad, 0, len(available))
|
||||
for _, a := range available {
|
||||
if a.account.Priority != minPri {
|
||||
newAvailable = append(newAvailable, a)
|
||||
}
|
||||
}
|
||||
available = newAvailable
|
||||
continue
|
||||
}
|
||||
// 3. 取负载率最低的集合
|
||||
// 2. 取负载率最低的集合
|
||||
candidates = filterByMinLoadRate(candidates)
|
||||
// 3. 取亲和客户端数最少的集合
|
||||
candidates = filterByMinAffinityCount(candidates)
|
||||
// 4. LRU 选择最久未用的账号
|
||||
// 3. LRU 选择最久未用的账号
|
||||
selected := selectByLRU(candidates, preferOAuth)
|
||||
if selected == nil {
|
||||
break
|
||||
@@ -1868,10 +1731,6 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
||||
if sessionHash != "" && s.cache != nil {
|
||||
_ = s.cache.SetSessionAccountID(ctx, derefGroupID(groupID), sessionHash, selected.account.ID, stickySessionTTL)
|
||||
}
|
||||
// 更新亲和关系
|
||||
if affinityClientID != "" && affinityUserID > 0 && s.cache != nil && selected.account.IsAffinityEnabled() {
|
||||
_ = s.cache.UpdateAffinity(ctx, derefGroupID(groupID), affinityUserID, affinityClientID, selected.account.ID, ClientAffinityTTL)
|
||||
}
|
||||
return &AccountSelectionResult{
|
||||
Account: selected.account,
|
||||
Acquired: true,
|
||||
@@ -2629,36 +2488,6 @@ func (s *GatewayService) getSchedulableAccount(ctx context.Context, accountID in
|
||||
return s.accountRepo.GetByID(ctx, accountID)
|
||||
}
|
||||
|
||||
// populateAffinityCounts 批量获取账号的亲和客户端数量并填入 accountWithLoad 切片。
|
||||
// 仅当存在开启了客户端亲和的账号时才查询 Redis,否则跳过。
|
||||
func (s *GatewayService) populateAffinityCounts(ctx context.Context, accounts []accountWithLoad, groupID int64) {
|
||||
if s.cache == nil || len(accounts) == 0 {
|
||||
return
|
||||
}
|
||||
// 快速检查:是否有任何账号开启了亲和
|
||||
hasAffinity := false
|
||||
for _, acc := range accounts {
|
||||
if acc.account.IsAffinityEnabled() {
|
||||
hasAffinity = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasAffinity {
|
||||
return
|
||||
}
|
||||
accountIDs := make([]int64, len(accounts))
|
||||
for i, acc := range accounts {
|
||||
accountIDs[i] = acc.account.ID
|
||||
}
|
||||
countMap, err := s.cache.GetAccountAffinityCountBatch(ctx, groupID, accountIDs, ClientAffinityTTL)
|
||||
if err != nil {
|
||||
return // 查询失败不影响调度,affinityCount 保持 0
|
||||
}
|
||||
for i := range accounts {
|
||||
accounts[i].affinityCount = countMap[accounts[i].account.ID]
|
||||
}
|
||||
}
|
||||
|
||||
// filterByMinPriority 过滤出优先级最小的账号集合
|
||||
func filterByMinPriority(accounts []accountWithLoad) []accountWithLoad {
|
||||
if len(accounts) == 0 {
|
||||
@@ -2699,64 +2528,6 @@ func filterByMinLoadRate(accounts []accountWithLoad) []accountWithLoad {
|
||||
return result
|
||||
}
|
||||
|
||||
// filterByMinAffinityCount 过滤出亲和客户端数最少的账号集合
|
||||
func filterByMinAffinityCount(accounts []accountWithLoad) []accountWithLoad {
|
||||
if len(accounts) == 0 {
|
||||
return accounts
|
||||
}
|
||||
minCount := accounts[0].affinityCount
|
||||
for _, acc := range accounts[1:] {
|
||||
if acc.affinityCount < minCount {
|
||||
minCount = acc.affinityCount
|
||||
}
|
||||
}
|
||||
result := make([]accountWithLoad, 0, len(accounts))
|
||||
for _, acc := range accounts {
|
||||
if acc.affinityCount == minCount {
|
||||
result = append(result, acc)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// classifyByAffinityZone 按亲和分区对候选账号进行分类。
|
||||
// 返回值:仅绿区账号(有绿区时),否则返回黄区账号。红区账号被移除。
|
||||
// 如果没有任何账号开启了亲和三区配置(即 affinity_base <= 0),则原样返回所有账号。
|
||||
func classifyByAffinityZone(accounts []accountWithLoad) []accountWithLoad {
|
||||
if len(accounts) == 0 {
|
||||
return accounts
|
||||
}
|
||||
// 快速检查:是否有任何账号配置了 affinity_base
|
||||
hasZoneConfig := false
|
||||
for _, acc := range accounts {
|
||||
if acc.account.IsAffinityEnabled() && acc.account.GetAffinityBase() > 0 {
|
||||
hasZoneConfig = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasZoneConfig {
|
||||
return accounts
|
||||
}
|
||||
|
||||
greens := make([]accountWithLoad, 0, len(accounts))
|
||||
yellows := make([]accountWithLoad, 0, len(accounts))
|
||||
for _, acc := range accounts {
|
||||
zone := acc.account.GetAffinityZone(acc.affinityCount)
|
||||
switch zone {
|
||||
case AffinityZoneGreen:
|
||||
greens = append(greens, acc)
|
||||
case AffinityZoneYellow:
|
||||
yellows = append(yellows, acc)
|
||||
case AffinityZoneRed:
|
||||
// 红区:移除,不参与调度
|
||||
}
|
||||
}
|
||||
if len(greens) > 0 {
|
||||
return greens
|
||||
}
|
||||
return yellows
|
||||
}
|
||||
|
||||
// selectByLRU 从集合中选择最久未用的账号
|
||||
// 如果有多个账号具有相同的最小 LastUsedAt,则随机选择一个
|
||||
func selectByLRU(accounts []accountWithLoad, preferOAuth bool) *accountWithLoad {
|
||||
@@ -4829,7 +4600,6 @@ func (s *GatewayService) Forward(ctx context.Context, c *gin.Context, account *A
|
||||
}
|
||||
|
||||
// 处理正常响应
|
||||
ctx = withClaudeMaxResponseRewriteContext(ctx, c, parsed)
|
||||
|
||||
// 触发上游接受回调(提前释放串行锁,不等流完成)
|
||||
if parsed.OnUpstreamAccepted != nil {
|
||||
@@ -7029,7 +6799,6 @@ func (s *GatewayService) handleStreamingResponse(ctx context.Context, resp *http
|
||||
needModelReplace := originalModel != mappedModel
|
||||
clientDisconnected := false // 客户端断开标志,断开后继续读取上游以获取完整usage
|
||||
sawTerminalEvent := false
|
||||
skipAccountTTLOverride := false
|
||||
|
||||
pendingEventLines := make([]string, 0, 4)
|
||||
|
||||
@@ -7091,25 +6860,17 @@ func (s *GatewayService) handleStreamingResponse(ctx context.Context, resp *http
|
||||
if msg, ok := event["message"].(map[string]any); ok {
|
||||
if u, ok := msg["usage"].(map[string]any); ok {
|
||||
eventChanged = reconcileCachedTokens(u) || eventChanged
|
||||
claudeMaxOutcome := applyClaudeMaxSimulationToUsageJSONMap(ctx, u, originalModel, account.ID)
|
||||
if claudeMaxOutcome.Simulated {
|
||||
skipAccountTTLOverride = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if eventType == "message_delta" {
|
||||
if u, ok := event["usage"].(map[string]any); ok {
|
||||
eventChanged = reconcileCachedTokens(u) || eventChanged
|
||||
claudeMaxOutcome := applyClaudeMaxSimulationToUsageJSONMap(ctx, u, originalModel, account.ID)
|
||||
if claudeMaxOutcome.Simulated {
|
||||
skipAccountTTLOverride = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cache TTL Override: 重写 SSE 事件中的 cache_creation 分类
|
||||
if account.IsCacheTTLOverrideEnabled() && !skipAccountTTLOverride {
|
||||
if account.IsCacheTTLOverrideEnabled() {
|
||||
overrideTarget := account.GetCacheTTLOverrideTarget()
|
||||
if eventType == "message_start" {
|
||||
if msg, ok := event["message"].(map[string]any); ok {
|
||||
@@ -7541,13 +7302,8 @@ func (s *GatewayService) handleNonStreamingResponse(ctx context.Context, resp *h
|
||||
}
|
||||
}
|
||||
|
||||
claudeMaxOutcome := applyClaudeMaxSimulationToUsage(ctx, &response.Usage, originalModel, account.ID)
|
||||
if claudeMaxOutcome.Simulated {
|
||||
body = rewriteClaudeUsageJSONBytes(body, response.Usage)
|
||||
}
|
||||
|
||||
// Cache TTL Override: 重写 non-streaming 响应中的 cache_creation 分类
|
||||
if account.IsCacheTTLOverrideEnabled() && !claudeMaxOutcome.Simulated {
|
||||
if account.IsCacheTTLOverrideEnabled() {
|
||||
overrideTarget := account.GetCacheTTLOverrideTarget()
|
||||
if applyCacheTTLOverride(&response.Usage, overrideTarget) {
|
||||
// 同步更新 body JSON 中的嵌套 cache_creation 对象
|
||||
@@ -7925,11 +7681,10 @@ func writeUsageLogBestEffort(ctx context.Context, repo UsageLogRepository, usage
|
||||
|
||||
// recordUsageOpts 内部选项,参数化 RecordUsage 与 RecordUsageWithLongContext 的差异点。
|
||||
type recordUsageOpts struct {
|
||||
// Claude Max 策略所需的 ParsedRequest(可选,仅 Claude 路径传入)
|
||||
// ParsedRequest(可选,仅 Claude 路径传入)
|
||||
ParsedRequest *ParsedRequest
|
||||
|
||||
// EnableClaudePath 启用 Claude 路径特有逻辑:
|
||||
// - Claude Max 缓存计费策略
|
||||
// - Sora 媒体类型分支(image/video/prompt)
|
||||
// - MediaType 字段写入使用日志
|
||||
EnableClaudePath bool
|
||||
@@ -8022,7 +7777,6 @@ type recordUsageCoreInput struct {
|
||||
|
||||
// recordUsageCore 是 RecordUsage 和 RecordUsageWithLongContext 的统一实现。
|
||||
// opts 中的字段控制两者之间的差异行为:
|
||||
// - ParsedRequest != nil → 启用 Claude Max 缓存计费策略
|
||||
// - EnableSoraMedia → 启用 Sora MediaType 分支(image/video/prompt)
|
||||
// - LongContextThreshold > 0 → Token 计费回退走 CalculateCostWithLongContext
|
||||
func (s *GatewayService) recordUsageCore(ctx context.Context, input *recordUsageCoreInput, opts *recordUsageOpts) error {
|
||||
@@ -8041,21 +7795,9 @@ func (s *GatewayService) recordUsageCore(ctx context.Context, input *recordUsage
|
||||
result.Usage.InputTokens = 0
|
||||
}
|
||||
|
||||
// Claude Max cache billing policy(仅 Claude 路径启用)
|
||||
cacheTTLOverridden := false
|
||||
simulatedClaudeMax := false
|
||||
if opts.EnableClaudePath {
|
||||
var apiKeyGroup *Group
|
||||
if apiKey != nil {
|
||||
apiKeyGroup = apiKey.Group
|
||||
}
|
||||
claudeMaxOutcome := applyClaudeMaxCacheBillingPolicyToUsage(&result.Usage, opts.ParsedRequest, apiKeyGroup, result.Model, account.ID)
|
||||
simulatedClaudeMax = claudeMaxOutcome.Simulated ||
|
||||
(shouldApplyClaudeMaxBillingRulesForUsage(apiKeyGroup, result.Model, opts.ParsedRequest) && hasCacheCreationTokens(result.Usage))
|
||||
}
|
||||
|
||||
// Cache TTL Override: 确保计费时 token 分类与账号设置一致
|
||||
if account.IsCacheTTLOverrideEnabled() && !simulatedClaudeMax {
|
||||
cacheTTLOverridden := false
|
||||
if account.IsCacheTTLOverrideEnabled() {
|
||||
applyCacheTTLOverride(&result.Usage, account.GetCacheTTLOverrideTarget())
|
||||
cacheTTLOverridden = (result.Usage.CacheCreation5mTokens + result.Usage.CacheCreation1hTokens) > 0
|
||||
}
|
||||
|
||||
@@ -1,122 +0,0 @@
|
||||
//go:build unit
|
||||
|
||||
// Package service 提供 API 网关核心服务。
|
||||
// 本文件包含 SortAffinityClients 函数的单元测试,
|
||||
// 验证 AffinityClient 切片排序逻辑在各种输入条件下的正确行为。
|
||||
//
|
||||
// This file contains unit tests for the SortAffinityClients function,
|
||||
// verifying correct sorting behavior for AffinityClient slices under
|
||||
// various input conditions including empty, single, sorted, reverse,
|
||||
// and duplicate-timestamp scenarios.
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSortAffinityClients_Empty(t *testing.T) {
|
||||
var clients []AffinityClient
|
||||
SortAffinityClients(clients)
|
||||
require.Empty(t, clients)
|
||||
|
||||
clients = []AffinityClient{}
|
||||
SortAffinityClients(clients)
|
||||
require.Empty(t, clients)
|
||||
}
|
||||
|
||||
func TestSortAffinityClients_SingleElement(t *testing.T) {
|
||||
now := time.Now()
|
||||
clients := []AffinityClient{
|
||||
{ClientID: "client-1", LastActive: now},
|
||||
}
|
||||
SortAffinityClients(clients)
|
||||
require.Len(t, clients, 1)
|
||||
require.Equal(t, "client-1", clients[0].ClientID)
|
||||
require.Equal(t, now, clients[0].LastActive)
|
||||
}
|
||||
|
||||
func TestSortAffinityClients_AlreadySorted(t *testing.T) {
|
||||
now := time.Now()
|
||||
clients := []AffinityClient{
|
||||
{ClientID: "newest", LastActive: now},
|
||||
{ClientID: "middle", LastActive: now.Add(-1 * time.Hour)},
|
||||
{ClientID: "oldest", LastActive: now.Add(-2 * time.Hour)},
|
||||
}
|
||||
SortAffinityClients(clients)
|
||||
|
||||
require.Equal(t, "newest", clients[0].ClientID)
|
||||
require.Equal(t, "middle", clients[1].ClientID)
|
||||
require.Equal(t, "oldest", clients[2].ClientID)
|
||||
}
|
||||
|
||||
func TestSortAffinityClients_ReverseOrder(t *testing.T) {
|
||||
now := time.Now()
|
||||
clients := []AffinityClient{
|
||||
{ClientID: "oldest", LastActive: now.Add(-2 * time.Hour)},
|
||||
{ClientID: "middle", LastActive: now.Add(-1 * time.Hour)},
|
||||
{ClientID: "newest", LastActive: now},
|
||||
}
|
||||
SortAffinityClients(clients)
|
||||
|
||||
require.Equal(t, "newest", clients[0].ClientID)
|
||||
require.Equal(t, "middle", clients[1].ClientID)
|
||||
require.Equal(t, "oldest", clients[2].ClientID)
|
||||
}
|
||||
|
||||
func TestSortAffinityClients_SameTimestamps(t *testing.T) {
|
||||
now := time.Now()
|
||||
clients := []AffinityClient{
|
||||
{ClientID: "c1", LastActive: now},
|
||||
{ClientID: "c2", LastActive: now},
|
||||
{ClientID: "c3", LastActive: now},
|
||||
}
|
||||
SortAffinityClients(clients)
|
||||
|
||||
// 所有时间戳相同时,排序结果应保持稳定(sort.Slice 不保证稳定性,
|
||||
// 但只要结果是某种确定的顺序即可)。
|
||||
// 验证所有元素仍然存在且时间相同。
|
||||
require.Len(t, clients, 3)
|
||||
ids := map[string]bool{}
|
||||
for _, c := range clients {
|
||||
ids[c.ClientID] = true
|
||||
require.Equal(t, now, c.LastActive)
|
||||
}
|
||||
require.True(t, ids["c1"])
|
||||
require.True(t, ids["c2"])
|
||||
require.True(t, ids["c3"])
|
||||
}
|
||||
|
||||
func TestSortAffinityClients_MixedOrder(t *testing.T) {
|
||||
now := time.Now()
|
||||
clients := []AffinityClient{
|
||||
{ClientID: "c3", LastActive: now.Add(-30 * time.Minute)},
|
||||
{ClientID: "c1", LastActive: now},
|
||||
{ClientID: "c5", LastActive: now.Add(-2 * time.Hour)},
|
||||
{ClientID: "c2", LastActive: now.Add(-10 * time.Minute)},
|
||||
{ClientID: "c4", LastActive: now.Add(-1 * time.Hour)},
|
||||
}
|
||||
SortAffinityClients(clients)
|
||||
|
||||
// 按 LastActive 降序排列
|
||||
require.Equal(t, "c1", clients[0].ClientID) // now
|
||||
require.Equal(t, "c2", clients[1].ClientID) // -10m
|
||||
require.Equal(t, "c3", clients[2].ClientID) // -30m
|
||||
require.Equal(t, "c4", clients[3].ClientID) // -1h
|
||||
require.Equal(t, "c5", clients[4].ClientID) // -2h
|
||||
}
|
||||
|
||||
func TestSortAffinityClients_SubSecondDifferences(t *testing.T) {
|
||||
base := time.Now()
|
||||
clients := []AffinityClient{
|
||||
{ClientID: "early", LastActive: base},
|
||||
{ClientID: "late", LastActive: base.Add(500 * time.Millisecond)},
|
||||
}
|
||||
SortAffinityClients(clients)
|
||||
|
||||
// 500ms 差异也应正确排序(更晚的在前)
|
||||
require.Equal(t, "late", clients[0].ClientID)
|
||||
require.Equal(t, "early", clients[1].ClientID)
|
||||
}
|
||||
@@ -288,28 +288,6 @@ func (m *mockGatewayCacheForGemini) DeleteSessionAccountID(ctx context.Context,
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockGatewayCacheForGemini) GetAffinityAccounts(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (m *mockGatewayCacheForGemini) UpdateAffinity(_ context.Context, _ int64, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (m *mockGatewayCacheForGemini) GetAffinityMultiCount(_ context.Context, _ int64, _ int64, _ int64, _ time.Duration) (int64, int64, int64, error) {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
func (m *mockGatewayCacheForGemini) GetAccountAffinityCountBatch(_ context.Context, _ int64, _ []int64, _ time.Duration) (map[int64]int64, error) {
|
||||
return map[int64]int64{}, nil
|
||||
}
|
||||
func (m *mockGatewayCacheForGemini) GetAccountAffinityClientsBatch(_ context.Context, _ map[int64][]int64, _ time.Duration) (map[int64][]string, error) {
|
||||
return map[int64][]string{}, nil
|
||||
}
|
||||
func (m *mockGatewayCacheForGemini) GetAccountAffinityClientsWithScores(_ context.Context, _ int64, _ []int64, _ time.Duration) ([]AffinityClient, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (m *mockGatewayCacheForGemini) ClearAccountAffinity(_ context.Context, _ int64, _ []int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestGeminiMessagesCompatService_SelectAccountForModelWithExclusions_GeminiPlatform 测试 Gemini 单平台选择
|
||||
func TestGeminiMessagesCompatService_SelectAccountForModelWithExclusions_GeminiPlatform(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -50,9 +50,6 @@ type Group struct {
|
||||
// MCP XML 协议注入开关(仅 antigravity 平台使用)
|
||||
MCPXMLInject bool
|
||||
|
||||
// Claude usage 模拟开关:将无写缓存 usage 模拟为 claude-max 风格
|
||||
SimulateClaudeMaxEnabled bool
|
||||
|
||||
// 支持的模型系列(仅 antigravity 平台使用)
|
||||
// 可选值: claude, gemini_text, gemini_image
|
||||
SupportedModelScopes []string
|
||||
|
||||
@@ -282,28 +282,6 @@ func (c *stubGatewayCache) DeleteSessionAccountID(ctx context.Context, groupID i
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *stubGatewayCache) GetAffinityAccounts(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (c *stubGatewayCache) UpdateAffinity(_ context.Context, _ int64, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (c *stubGatewayCache) GetAffinityMultiCount(_ context.Context, _ int64, _ int64, _ int64, _ time.Duration) (int64, int64, int64, error) {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
func (c *stubGatewayCache) GetAccountAffinityCountBatch(_ context.Context, _ int64, _ []int64, _ time.Duration) (map[int64]int64, error) {
|
||||
return map[int64]int64{}, nil
|
||||
}
|
||||
func (c *stubGatewayCache) GetAccountAffinityClientsBatch(_ context.Context, _ map[int64][]int64, _ time.Duration) (map[int64][]string, error) {
|
||||
return map[int64][]string{}, nil
|
||||
}
|
||||
func (c *stubGatewayCache) GetAccountAffinityClientsWithScores(_ context.Context, _ int64, _ []int64, _ time.Duration) ([]AffinityClient, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (c *stubGatewayCache) ClearAccountAffinity(_ context.Context, _ int64, _ []int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestOpenAISelectAccountWithLoadAwareness_FiltersUnschedulable(t *testing.T) {
|
||||
now := time.Now()
|
||||
resetAt := now.Add(10 * time.Minute)
|
||||
|
||||
@@ -193,28 +193,6 @@ func (c *openAIWSStateStoreTimeoutProbeCache) DeleteSessionAccountID(ctx context
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *openAIWSStateStoreTimeoutProbeCache) GetAffinityAccounts(_ context.Context, _ int64, _ int64, _ string, _ time.Duration) ([]int64, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (c *openAIWSStateStoreTimeoutProbeCache) UpdateAffinity(_ context.Context, _ int64, _ int64, _ string, _ int64, _ time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (c *openAIWSStateStoreTimeoutProbeCache) GetAffinityMultiCount(_ context.Context, _ int64, _ int64, _ int64, _ time.Duration) (int64, int64, int64, error) {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
func (c *openAIWSStateStoreTimeoutProbeCache) GetAccountAffinityCountBatch(_ context.Context, _ int64, _ []int64, _ time.Duration) (map[int64]int64, error) {
|
||||
return map[int64]int64{}, nil
|
||||
}
|
||||
func (c *openAIWSStateStoreTimeoutProbeCache) GetAccountAffinityClientsBatch(_ context.Context, _ map[int64][]int64, _ time.Duration) (map[int64][]string, error) {
|
||||
return map[int64][]string{}, nil
|
||||
}
|
||||
func (c *openAIWSStateStoreTimeoutProbeCache) GetAccountAffinityClientsWithScores(_ context.Context, _ int64, _ []int64, _ time.Duration) ([]AffinityClient, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (c *openAIWSStateStoreTimeoutProbeCache) ClearAccountAffinity(_ context.Context, _ int64, _ []int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestOpenAIWSStateStore_RedisOpsUseShortTimeout(t *testing.T) {
|
||||
probe := &openAIWSStateStoreTimeoutProbeCache{}
|
||||
store := NewOpenAIWSStateStore(probe)
|
||||
|
||||
@@ -37,7 +37,6 @@ const (
|
||||
// Sora 存储类型常量
|
||||
const (
|
||||
SoraStorageTypeS3 = "s3"
|
||||
SoraStorageTypeGDrive = "gdrive"
|
||||
SoraStorageTypeLocal = "local"
|
||||
SoraStorageTypeUpstream = "upstream"
|
||||
SoraStorageTypeNone = "none"
|
||||
@@ -61,5 +60,4 @@ type SoraGenerationRepository interface {
|
||||
Delete(ctx context.Context, id int64) error
|
||||
List(ctx context.Context, params SoraGenerationListParams) ([]*SoraGeneration, int64, error)
|
||||
CountByUserAndStatus(ctx context.Context, userID int64, statuses []string) (int64, error)
|
||||
CountByStorageType(ctx context.Context, storageType string, statuses []string) (int64, error)
|
||||
}
|
||||
@@ -35,21 +35,21 @@ type soraGenerationRepoConditionalUpdater interface {
|
||||
|
||||
// SoraGenerationService 管理 Sora 客户端的生成记录 CRUD。
|
||||
type SoraGenerationService struct {
|
||||
genRepo SoraGenerationRepository
|
||||
objectStorage SoraObjectStorage
|
||||
quotaService *SoraQuotaService
|
||||
genRepo SoraGenerationRepository
|
||||
s3Storage *SoraS3Storage
|
||||
quotaService *SoraQuotaService
|
||||
}
|
||||
|
||||
// NewSoraGenerationService 创建生成记录服务。
|
||||
func NewSoraGenerationService(
|
||||
genRepo SoraGenerationRepository,
|
||||
objectStorage SoraObjectStorage,
|
||||
s3Storage *SoraS3Storage,
|
||||
quotaService *SoraQuotaService,
|
||||
) *SoraGenerationService {
|
||||
return &SoraGenerationService{
|
||||
genRepo: genRepo,
|
||||
objectStorage: objectStorage,
|
||||
quotaService: quotaService,
|
||||
genRepo: genRepo,
|
||||
s3Storage: s3Storage,
|
||||
quotaService: quotaService,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,15 +268,15 @@ func (s *SoraGenerationService) Delete(ctx context.Context, id, userID int64) er
|
||||
return fmt.Errorf("无权删除此生成记录")
|
||||
}
|
||||
|
||||
// 清理存储文件(S3 / Google Drive)
|
||||
if IsObjectStorageType(gen.StorageType) && len(gen.S3ObjectKeys) > 0 && s.objectStorage != nil {
|
||||
if err := s.objectStorage.DeleteObjects(ctx, gen.S3ObjectKeys); err != nil {
|
||||
logger.LegacyPrintf("service.sora_gen", "[SoraGen] 存储清理失败 id=%d type=%s err=%v", id, gen.StorageType, err)
|
||||
// 清理 S3 文件
|
||||
if gen.StorageType == SoraStorageTypeS3 && len(gen.S3ObjectKeys) > 0 && s.s3Storage != nil {
|
||||
if err := s.s3Storage.DeleteObjects(ctx, gen.S3ObjectKeys); err != nil {
|
||||
logger.LegacyPrintf("service.sora_gen", "[SoraGen] S3 清理失败 id=%d err=%v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
// 释放配额(对象存储/本地均释放)
|
||||
if gen.FileSizeBytes > 0 && (IsObjectStorageType(gen.StorageType) || gen.StorageType == SoraStorageTypeLocal) && s.quotaService != nil {
|
||||
// 释放配额(S3/本地均释放)
|
||||
if gen.FileSizeBytes > 0 && (gen.StorageType == SoraStorageTypeS3 || gen.StorageType == SoraStorageTypeLocal) && s.quotaService != nil {
|
||||
if err := s.quotaService.ReleaseUsage(ctx, userID, gen.FileSizeBytes); err != nil {
|
||||
logger.LegacyPrintf("service.sora_gen", "[SoraGen] 配额释放失败 id=%d err=%v", id, err)
|
||||
}
|
||||
@@ -290,9 +290,9 @@ func (s *SoraGenerationService) CountActiveByUser(ctx context.Context, userID in
|
||||
return s.genRepo.CountByUserAndStatus(ctx, userID, []string{SoraGenStatusPending, SoraGenStatusGenerating})
|
||||
}
|
||||
|
||||
// ResolveMediaURLs 为对象存储记录动态生成访问 URL。
|
||||
// ResolveMediaURLs 为 S3 记录动态生成预签名 URL。
|
||||
func (s *SoraGenerationService) ResolveMediaURLs(ctx context.Context, gen *SoraGeneration) error {
|
||||
if gen == nil || !IsObjectStorageType(gen.StorageType) || s.objectStorage == nil {
|
||||
if gen == nil || gen.StorageType != SoraStorageTypeS3 || s.s3Storage == nil {
|
||||
return nil
|
||||
}
|
||||
if len(gen.S3ObjectKeys) == 0 {
|
||||
@@ -308,7 +308,7 @@ func (s *SoraGenerationService) ResolveMediaURLs(ctx context.Context, gen *SoraG
|
||||
wg.Add(1)
|
||||
go func(i int, objectKey string) {
|
||||
defer wg.Done()
|
||||
url, err := s.objectStorage.GetAccessURL(ctx, objectKey)
|
||||
url, err := s.s3Storage.GetAccessURL(ctx, objectKey)
|
||||
if err != nil {
|
||||
errMu.Lock()
|
||||
if firstErr == nil {
|
||||
@@ -330,22 +330,3 @@ func (s *SoraGenerationService) ResolveMediaURLs(ctx context.Context, gen *SoraG
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// StorageVideoStats 各存储类型的视频统计。
|
||||
type StorageVideoStats struct {
|
||||
Completed int64 `json:"completed"`
|
||||
InProgress int64 `json:"in_progress"`
|
||||
}
|
||||
|
||||
// CountByStorageType 按存储类型统计视频数量(completed 和 in_progress)。
|
||||
func (s *SoraGenerationService) CountByStorageType(ctx context.Context, storageType string) (completed, inProgress int64, err error) {
|
||||
completed, err = s.genRepo.CountByStorageType(ctx, storageType, []string{SoraGenStatusCompleted})
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("count completed: %w", err)
|
||||
}
|
||||
inProgress, err = s.genRepo.CountByStorageType(ctx, storageType, []string{SoraGenStatusPending, SoraGenStatusGenerating})
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("count in_progress: %w", err)
|
||||
}
|
||||
return completed, inProgress, nil
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package service
|
||||
|
||||
import "context"
|
||||
|
||||
// SoraObjectStorage 是 Sora 媒体文件的通用对象存储接口。
|
||||
type SoraObjectStorage interface {
|
||||
// Enabled 返回存储是否已启用且配置有效。
|
||||
Enabled(ctx context.Context) bool
|
||||
|
||||
// IsHealthy 返回存储健康状态(带短缓存)。
|
||||
IsHealthy(ctx context.Context) bool
|
||||
|
||||
// TestConnection 测试存储连接。
|
||||
TestConnection(ctx context.Context) error
|
||||
|
||||
// UploadFromURL 从上游 URL 下载并上传到存储。
|
||||
// 返回 object key、文件大小、实际使用的存储类型。
|
||||
UploadFromURL(ctx context.Context, userID int64, sourceURL string) (objectKey string, sizeBytes int64, storageType string, err error)
|
||||
|
||||
// DeleteObjects 删除一组存储对象。
|
||||
DeleteObjects(ctx context.Context, objectKeys []string) error
|
||||
|
||||
// GetAccessURL 获取存储文件的访问 URL。
|
||||
GetAccessURL(ctx context.Context, objectKey string) (string, error)
|
||||
|
||||
// RefreshClient 清除缓存客户端,配置变更时调用。
|
||||
RefreshClient()
|
||||
|
||||
// StorageType 返回存储类型标识。
|
||||
StorageType() string
|
||||
}
|
||||
|
||||
// IsObjectStorageType 判断是否为对象存储类型。
|
||||
func IsObjectStorageType(t string) bool {
|
||||
return t == SoraStorageTypeS3
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ── 任务状态常量 ──
|
||||
|
||||
const (
|
||||
SoraTaskQueued = "queued"
|
||||
SoraTaskInProgress = "in_progress"
|
||||
SoraTaskCompleted = "completed"
|
||||
SoraTaskFailed = "failed"
|
||||
)
|
||||
|
||||
// ── 对象类型常量 ──
|
||||
|
||||
const (
|
||||
SoraObjectVideo = "video"
|
||||
SoraObjectCharacter = "character"
|
||||
SoraObjectImage = "image"
|
||||
)
|
||||
|
||||
// SoraTask 表示一个 Sora 异步任务记录。
|
||||
type SoraTask struct {
|
||||
ID string
|
||||
AccountID int64
|
||||
APIKeyID *int64
|
||||
UpstreamTaskID string
|
||||
ObjectType string
|
||||
Model string
|
||||
Prompt string
|
||||
Status string
|
||||
Progress int
|
||||
VideoURL string // 原始上游 URL
|
||||
StoredKey string // 存储后的 key(本地路径或 S3 key)
|
||||
StorageType string // local / s3 / gdrive / 空
|
||||
ShareID string
|
||||
CharacterInfo *SoraCharacter
|
||||
ErrorMessage string
|
||||
ErrorType string
|
||||
RequestBody []byte
|
||||
Seconds string
|
||||
Size string
|
||||
CreatedAt time.Time
|
||||
CompletedAt *time.Time
|
||||
}
|
||||
|
||||
// SoraCharacter 角色信息。
|
||||
type SoraCharacter struct {
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
}
|
||||
|
||||
// SoraTaskRepository 持久层接口。
|
||||
type SoraTaskRepository interface {
|
||||
Create(ctx context.Context, task *SoraTask) error
|
||||
GetByID(ctx context.Context, id string) (*SoraTask, error)
|
||||
GetByIDAndAPIKey(ctx context.Context, id string, apiKeyID int64) (*SoraTask, error)
|
||||
Update(ctx context.Context, task *SoraTask) error
|
||||
ListPending(ctx context.Context) ([]*SoraTask, error)
|
||||
}
|
||||
@@ -1,546 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/logger"
|
||||
)
|
||||
|
||||
// SoraTaskService manages Sora async task creation and status queries.
|
||||
type SoraTaskService struct {
|
||||
repo SoraTaskRepository
|
||||
accountRepo AccountRepository
|
||||
soraClient SoraClient
|
||||
httpUpstream HTTPUpstream
|
||||
}
|
||||
|
||||
func NewSoraTaskService(
|
||||
repo SoraTaskRepository,
|
||||
accountRepo AccountRepository,
|
||||
soraClient SoraClient,
|
||||
httpUpstream HTTPUpstream,
|
||||
) *SoraTaskService {
|
||||
return &SoraTaskService{
|
||||
repo: repo,
|
||||
accountRepo: accountRepo,
|
||||
soraClient: soraClient,
|
||||
httpUpstream: httpUpstream,
|
||||
}
|
||||
}
|
||||
|
||||
// ── Request structs ──
|
||||
|
||||
type CreateVideoRequest struct {
|
||||
Model string `json:"model"`
|
||||
Prompt string `json:"prompt"`
|
||||
StyleID string `json:"style_id,omitempty"`
|
||||
Orientation string `json:"orientation,omitempty"`
|
||||
Image string `json:"image,omitempty"`
|
||||
Video string `json:"video,omitempty"`
|
||||
RemixTargetID string `json:"remix_target_id,omitempty"`
|
||||
Size string `json:"size,omitempty"`
|
||||
}
|
||||
|
||||
type CreateImageRequest struct {
|
||||
Model string `json:"model,omitempty"`
|
||||
Prompt string `json:"prompt"`
|
||||
Image string `json:"image,omitempty"`
|
||||
Size string `json:"size,omitempty"`
|
||||
ResponseFormat string `json:"response_format,omitempty"`
|
||||
N int `json:"n,omitempty"`
|
||||
}
|
||||
|
||||
type EditImageRequest struct {
|
||||
Image string `json:"image"`
|
||||
Prompt string `json:"prompt"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Size string `json:"size,omitempty"`
|
||||
ResponseFormat string `json:"response_format,omitempty"`
|
||||
}
|
||||
|
||||
type RemixRequest struct {
|
||||
Prompt string `json:"prompt"`
|
||||
}
|
||||
|
||||
// ── Response structs ──
|
||||
|
||||
type SoraTaskResponse struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
Status string `json:"status"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Prompt string `json:"prompt,omitempty"`
|
||||
Progress int `json:"progress"`
|
||||
VideoURL string `json:"video_url,omitempty"`
|
||||
ShareID string `json:"share_id,omitempty"`
|
||||
Seconds string `json:"seconds,omitempty"`
|
||||
Size string `json:"size,omitempty"`
|
||||
Character *SoraCharacter `json:"character,omitempty"`
|
||||
URL string `json:"url,omitempty"`
|
||||
CompletedAt *int64 `json:"completed_at,omitempty"`
|
||||
Error *SoraTaskError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type SoraTaskError struct {
|
||||
Message string `json:"message"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
func TaskToResponse(t *SoraTask) *SoraTaskResponse {
|
||||
resp := &SoraTaskResponse{
|
||||
ID: t.ID,
|
||||
Object: t.ObjectType,
|
||||
CreatedAt: t.CreatedAt.Unix(),
|
||||
Status: t.Status,
|
||||
Model: t.Model,
|
||||
Prompt: t.Prompt,
|
||||
Progress: t.Progress,
|
||||
Seconds: t.Seconds,
|
||||
Size: t.Size,
|
||||
}
|
||||
if t.VideoURL != "" {
|
||||
resp.VideoURL = t.VideoURL
|
||||
}
|
||||
if t.ShareID != "" {
|
||||
resp.ShareID = t.ShareID
|
||||
}
|
||||
if t.CharacterInfo != nil {
|
||||
resp.Character = t.CharacterInfo
|
||||
}
|
||||
if t.CompletedAt != nil {
|
||||
ts := t.CompletedAt.Unix()
|
||||
resp.CompletedAt = &ts
|
||||
}
|
||||
if t.Status == SoraTaskFailed && t.ErrorMessage != "" {
|
||||
resp.Error = &SoraTaskError{
|
||||
Message: t.ErrorMessage,
|
||||
Type: t.ErrorType,
|
||||
}
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
// ── Task creation ──
|
||||
|
||||
func (s *SoraTaskService) CreateVideoTask(
|
||||
ctx context.Context,
|
||||
apiKeyID int64,
|
||||
account *Account,
|
||||
req *CreateVideoRequest,
|
||||
body []byte,
|
||||
) (*SoraTask, error) {
|
||||
modelCfg, ok := GetSoraModelConfig(req.Model)
|
||||
|
||||
objectType := SoraObjectVideo
|
||||
if req.Video != "" && req.Prompt == "" {
|
||||
objectType = SoraObjectCharacter
|
||||
}
|
||||
|
||||
taskID := generateTaskID(objectType)
|
||||
|
||||
seconds := ""
|
||||
size := ""
|
||||
if ok && modelCfg.Type == "video" {
|
||||
seconds = fmt.Sprintf("%d", modelCfg.Frames/30)
|
||||
if modelCfg.Orientation == "landscape" {
|
||||
size = "1920x1080"
|
||||
} else {
|
||||
size = "1080x1920"
|
||||
}
|
||||
}
|
||||
|
||||
task := &SoraTask{
|
||||
ID: taskID,
|
||||
AccountID: account.ID,
|
||||
APIKeyID: &apiKeyID,
|
||||
ObjectType: objectType,
|
||||
Model: req.Model,
|
||||
Prompt: req.Prompt,
|
||||
Status: SoraTaskQueued,
|
||||
Progress: 0,
|
||||
Seconds: seconds,
|
||||
Size: size,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if account.Type == AccountTypeAPIKey {
|
||||
task.RequestBody = body
|
||||
upstreamResp, err := s.forwardCreateToUpstream(ctx, account, "/v1/videos", body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("forward to upstream: %w", err)
|
||||
}
|
||||
s.applyUpstreamResponse(task, upstreamResp)
|
||||
} else {
|
||||
if s.soraClient == nil || !s.soraClient.Enabled() {
|
||||
return nil, fmt.Errorf("sora SDK client not configured")
|
||||
}
|
||||
upstreamID, err := s.createViaSdk(ctx, account, req, modelCfg, objectType)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sdk create task: %w", err)
|
||||
}
|
||||
task.UpstreamTaskID = upstreamID
|
||||
}
|
||||
|
||||
if err := s.repo.Create(ctx, task); err != nil {
|
||||
return nil, fmt.Errorf("save task: %w", err)
|
||||
}
|
||||
return task, nil
|
||||
}
|
||||
|
||||
func (s *SoraTaskService) CreateImageGeneration(
|
||||
ctx context.Context,
|
||||
apiKeyID int64,
|
||||
account *Account,
|
||||
req *CreateImageRequest,
|
||||
body []byte,
|
||||
) (*SoraTask, error) {
|
||||
taskID := generateTaskID(SoraObjectImage)
|
||||
|
||||
task := &SoraTask{
|
||||
ID: taskID,
|
||||
AccountID: account.ID,
|
||||
APIKeyID: &apiKeyID,
|
||||
ObjectType: SoraObjectImage,
|
||||
Model: req.Model,
|
||||
Prompt: req.Prompt,
|
||||
Status: SoraTaskQueued,
|
||||
Progress: 0,
|
||||
Size: req.Size,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if account.Type == AccountTypeAPIKey {
|
||||
task.RequestBody = body
|
||||
upstreamResp, err := s.forwardCreateToUpstream(ctx, account, "/v1/images/generations", body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("forward to upstream: %w", err)
|
||||
}
|
||||
s.applyUpstreamResponse(task, upstreamResp)
|
||||
} else {
|
||||
if s.soraClient == nil || !s.soraClient.Enabled() {
|
||||
return nil, fmt.Errorf("sora SDK client not configured")
|
||||
}
|
||||
modelCfg, _ := GetSoraModelConfig(req.Model)
|
||||
width, height := modelCfg.Width, modelCfg.Height
|
||||
if width == 0 {
|
||||
width = 360
|
||||
}
|
||||
if height == 0 {
|
||||
height = 360
|
||||
}
|
||||
upstreamID, err := s.soraClient.CreateImageTask(ctx, account, SoraImageRequest{
|
||||
Prompt: req.Prompt,
|
||||
Width: width,
|
||||
Height: height,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sdk create image task: %w", err)
|
||||
}
|
||||
task.UpstreamTaskID = upstreamID
|
||||
}
|
||||
|
||||
if err := s.repo.Create(ctx, task); err != nil {
|
||||
return nil, fmt.Errorf("save task: %w", err)
|
||||
}
|
||||
return task, nil
|
||||
}
|
||||
|
||||
func (s *SoraTaskService) GetTask(ctx context.Context, taskID string, apiKeyID int64) (*SoraTask, error) {
|
||||
return s.repo.GetByIDAndAPIKey(ctx, taskID, apiKeyID)
|
||||
}
|
||||
|
||||
func (s *SoraTaskService) GetTaskByID(ctx context.Context, taskID string) (*SoraTask, error) {
|
||||
return s.repo.GetByID(ctx, taskID)
|
||||
}
|
||||
|
||||
// ListPendingTasks returns tasks in queued or in_progress status.
|
||||
func (s *SoraTaskService) ListPendingTasks(ctx context.Context) ([]*SoraTask, error) {
|
||||
return s.repo.ListPending(ctx)
|
||||
}
|
||||
|
||||
// UpdateTask persists task state changes.
|
||||
func (s *SoraTaskService) UpdateTask(ctx context.Context, task *SoraTask) error {
|
||||
return s.repo.Update(ctx, task)
|
||||
}
|
||||
|
||||
func (s *SoraTaskService) GetAccountByID(ctx context.Context, accountID int64) (*Account, error) {
|
||||
return s.accountRepo.GetByID(ctx, accountID)
|
||||
}
|
||||
|
||||
// ── Internal methods ──
|
||||
|
||||
func (s *SoraTaskService) createViaSdk(
|
||||
ctx context.Context,
|
||||
account *Account,
|
||||
req *CreateVideoRequest,
|
||||
modelCfg SoraModelConfig,
|
||||
objectType string,
|
||||
) (string, error) {
|
||||
if objectType == SoraObjectCharacter {
|
||||
return "", fmt.Errorf("character creation via /v1/videos is not yet supported for OAuth accounts")
|
||||
}
|
||||
|
||||
orientation := modelCfg.Orientation
|
||||
if req.Orientation != "" {
|
||||
orientation = req.Orientation
|
||||
}
|
||||
|
||||
videoReq := SoraVideoRequest{
|
||||
Prompt: req.Prompt,
|
||||
Orientation: orientation,
|
||||
Frames: modelCfg.Frames,
|
||||
Model: modelCfg.Model,
|
||||
Size: modelCfg.Size,
|
||||
RemixTargetID: req.RemixTargetID,
|
||||
}
|
||||
return s.soraClient.CreateVideoTask(ctx, account, videoReq)
|
||||
}
|
||||
|
||||
func (s *SoraTaskService) forwardCreateToUpstream(
|
||||
ctx context.Context,
|
||||
account *Account,
|
||||
path string,
|
||||
body []byte,
|
||||
) (map[string]any, error) {
|
||||
apiKey := account.GetCredential("api_key")
|
||||
baseURL := account.GetBaseURL()
|
||||
if apiKey == "" || baseURL == "" {
|
||||
return nil, fmt.Errorf("account %d missing api_key or base_url", account.ID)
|
||||
}
|
||||
|
||||
upstreamURL := strings.TrimRight(baseURL, "/") + path
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, upstreamURL, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
|
||||
proxyURL := ""
|
||||
if account.ProxyID != nil && account.Proxy != nil {
|
||||
proxyURL = account.Proxy.URL()
|
||||
}
|
||||
|
||||
resp, err := s.httpUpstream.Do(req, proxyURL, account.ID, account.Concurrency)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("upstream request: %w", err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 256*1024))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read upstream response: %w", err)
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
return nil, &SoraUpstreamError{StatusCode: resp.StatusCode, Body: respBody}
|
||||
}
|
||||
|
||||
var result map[string]any
|
||||
if err := json.Unmarshal(respBody, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse upstream response: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *SoraTaskService) forwardGetToUpstream(
|
||||
ctx context.Context,
|
||||
account *Account,
|
||||
path string,
|
||||
) (map[string]any, error) {
|
||||
apiKey := account.GetCredential("api_key")
|
||||
baseURL := account.GetBaseURL()
|
||||
if apiKey == "" || baseURL == "" {
|
||||
return nil, fmt.Errorf("account %d missing api_key or base_url", account.ID)
|
||||
}
|
||||
|
||||
upstreamURL := strings.TrimRight(baseURL, "/") + path
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, upstreamURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
|
||||
proxyURL := ""
|
||||
if account.ProxyID != nil && account.Proxy != nil {
|
||||
proxyURL = account.Proxy.URL()
|
||||
}
|
||||
|
||||
resp, err := s.httpUpstream.Do(req, proxyURL, account.ID, account.Concurrency)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("upstream request: %w", err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 256*1024))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read upstream response: %w", err)
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
return nil, fmt.Errorf("upstream error %d: %s", resp.StatusCode, string(respBody))
|
||||
}
|
||||
|
||||
var result map[string]any
|
||||
if err := json.Unmarshal(respBody, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse upstream response: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *SoraTaskService) applyUpstreamResponse(task *SoraTask, resp map[string]any) {
|
||||
if id, ok := resp["id"].(string); ok && id != "" {
|
||||
task.UpstreamTaskID = id
|
||||
}
|
||||
if status, ok := resp["status"].(string); ok && status != "" {
|
||||
task.Status = status
|
||||
}
|
||||
if progress, ok := resp["progress"].(float64); ok {
|
||||
task.Progress = int(progress)
|
||||
}
|
||||
if videoURL, ok := resp["video_url"].(string); ok {
|
||||
task.VideoURL = videoURL
|
||||
}
|
||||
if shareID, ok := resp["share_id"].(string); ok {
|
||||
task.ShareID = shareID
|
||||
}
|
||||
if seconds, ok := resp["seconds"].(string); ok {
|
||||
task.Seconds = seconds
|
||||
}
|
||||
if size, ok := resp["size"].(string); ok {
|
||||
task.Size = size
|
||||
}
|
||||
if obj, ok := resp["object"].(string); ok && obj != "" {
|
||||
task.ObjectType = obj
|
||||
}
|
||||
}
|
||||
|
||||
// PollTask polls a single task's latest status (called by worker).
|
||||
func (s *SoraTaskService) PollTask(ctx context.Context, task *SoraTask, account *Account) error {
|
||||
if account.Type == AccountTypeAPIKey {
|
||||
return s.pollUpstreamTask(ctx, task, account)
|
||||
}
|
||||
return s.pollSdkTask(ctx, task, account)
|
||||
}
|
||||
|
||||
func (s *SoraTaskService) pollUpstreamTask(ctx context.Context, task *SoraTask, account *Account) error {
|
||||
upstreamID := task.UpstreamTaskID
|
||||
if upstreamID == "" {
|
||||
upstreamID = task.ID
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("/v1/videos/%s", upstreamID)
|
||||
resp, err := s.forwardGetToUpstream(ctx, account, path)
|
||||
if err != nil {
|
||||
logger.LegacyPrintf("service.sora_task", "[PollUpstream] task=%s error=%v", task.ID, err)
|
||||
return err
|
||||
}
|
||||
|
||||
s.applyUpstreamResponse(task, resp)
|
||||
now := time.Now()
|
||||
if task.Status == SoraTaskCompleted || task.Status == SoraTaskFailed {
|
||||
task.CompletedAt = &now
|
||||
}
|
||||
|
||||
if errObj, ok := resp["error"].(map[string]any); ok {
|
||||
if msg, ok := errObj["message"].(string); ok {
|
||||
task.ErrorMessage = msg
|
||||
}
|
||||
if typ, ok := errObj["type"].(string); ok {
|
||||
task.ErrorType = typ
|
||||
}
|
||||
}
|
||||
|
||||
return s.repo.Update(ctx, task)
|
||||
}
|
||||
|
||||
func (s *SoraTaskService) pollSdkTask(ctx context.Context, task *SoraTask, account *Account) error {
|
||||
if s.soraClient == nil {
|
||||
return fmt.Errorf("sora SDK client not configured")
|
||||
}
|
||||
upstreamID := task.UpstreamTaskID
|
||||
if upstreamID == "" {
|
||||
return fmt.Errorf("task %s has no upstream_task_id", task.ID)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
|
||||
switch task.ObjectType {
|
||||
case SoraObjectImage:
|
||||
status, err := s.soraClient.GetImageTask(ctx, account, upstreamID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
task.Progress = int(status.ProgressPct)
|
||||
switch status.Status {
|
||||
case "complete", "completed":
|
||||
task.Status = SoraTaskCompleted
|
||||
task.Progress = 100
|
||||
task.CompletedAt = &now
|
||||
if len(status.URLs) > 0 {
|
||||
task.VideoURL = status.URLs[0]
|
||||
}
|
||||
case "failed":
|
||||
task.Status = SoraTaskFailed
|
||||
task.CompletedAt = &now
|
||||
task.ErrorMessage = status.ErrorMsg
|
||||
task.ErrorType = "server_error"
|
||||
default:
|
||||
task.Status = SoraTaskInProgress
|
||||
}
|
||||
|
||||
case SoraObjectVideo, SoraObjectCharacter:
|
||||
status, err := s.soraClient.GetVideoTask(ctx, account, upstreamID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
task.Progress = status.ProgressPct
|
||||
switch status.Status {
|
||||
case "complete", "completed":
|
||||
task.Status = SoraTaskCompleted
|
||||
task.Progress = 100
|
||||
task.CompletedAt = &now
|
||||
if len(status.URLs) > 0 {
|
||||
task.VideoURL = status.URLs[0]
|
||||
}
|
||||
if status.GenerationID != "" {
|
||||
task.ShareID = status.GenerationID
|
||||
}
|
||||
case "failed":
|
||||
task.Status = SoraTaskFailed
|
||||
task.CompletedAt = &now
|
||||
task.ErrorMessage = status.ErrorMsg
|
||||
task.ErrorType = "server_error"
|
||||
default:
|
||||
task.Status = SoraTaskInProgress
|
||||
}
|
||||
|
||||
default:
|
||||
return fmt.Errorf("unknown object type: %s", task.ObjectType)
|
||||
}
|
||||
|
||||
return s.repo.Update(ctx, task)
|
||||
}
|
||||
|
||||
func generateTaskID(objectType string) string {
|
||||
b := make([]byte, 8)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
b = []byte(fmt.Sprintf("%x", time.Now().UnixNano()))
|
||||
}
|
||||
switch objectType {
|
||||
case SoraObjectCharacter:
|
||||
return "char_" + hex.EncodeToString(b)
|
||||
case SoraObjectImage:
|
||||
return "img_" + hex.EncodeToString(b)
|
||||
default:
|
||||
return "video_" + hex.EncodeToString(b)
|
||||
}
|
||||
}
|
||||
@@ -1,183 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/logger"
|
||||
)
|
||||
|
||||
// SoraTaskWorker polls unfinished Sora tasks in the background.
|
||||
// When a task completes, it downloads media to configured storage if available.
|
||||
type SoraTaskWorker struct {
|
||||
taskService *SoraTaskService
|
||||
accountRepo AccountRepository
|
||||
objectStorage SoraObjectStorage
|
||||
mediaStorage *SoraMediaStorage
|
||||
interval time.Duration
|
||||
pollTimeout time.Duration
|
||||
stopCh chan struct{}
|
||||
stopOnce sync.Once
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func NewSoraTaskWorker(
|
||||
taskService *SoraTaskService,
|
||||
accountRepo AccountRepository,
|
||||
objectStorage SoraObjectStorage,
|
||||
mediaStorage *SoraMediaStorage,
|
||||
interval time.Duration,
|
||||
) *SoraTaskWorker {
|
||||
if interval <= 0 {
|
||||
interval = 60 * time.Second
|
||||
}
|
||||
return &SoraTaskWorker{
|
||||
taskService: taskService,
|
||||
accountRepo: accountRepo,
|
||||
objectStorage: objectStorage,
|
||||
mediaStorage: mediaStorage,
|
||||
interval: interval,
|
||||
pollTimeout: 30 * time.Second,
|
||||
stopCh: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (w *SoraTaskWorker) Start() {
|
||||
if w == nil || w.taskService == nil {
|
||||
return
|
||||
}
|
||||
w.wg.Add(1)
|
||||
go func() {
|
||||
defer w.wg.Done()
|
||||
ticker := time.NewTicker(w.interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
logger.LegacyPrintf("service.sora_task_worker", "[Start] polling interval=%s", w.interval)
|
||||
w.pollAll()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
w.pollAll()
|
||||
case <-w.stopCh:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (w *SoraTaskWorker) Stop() {
|
||||
if w == nil {
|
||||
return
|
||||
}
|
||||
w.stopOnce.Do(func() {
|
||||
close(w.stopCh)
|
||||
})
|
||||
w.wg.Wait()
|
||||
}
|
||||
|
||||
func (w *SoraTaskWorker) pollAll() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
tasks, err := w.taskService.ListPendingTasks(ctx)
|
||||
if err != nil {
|
||||
logger.LegacyPrintf("service.sora_task_worker", "[PollAll] list pending tasks error: %v", err)
|
||||
return
|
||||
}
|
||||
if len(tasks) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
logger.LegacyPrintf("service.sora_task_worker", "[PollAll] found %d pending tasks", len(tasks))
|
||||
|
||||
for _, task := range tasks {
|
||||
select {
|
||||
case <-w.stopCh:
|
||||
return
|
||||
default:
|
||||
}
|
||||
w.pollOne(ctx, task)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *SoraTaskWorker) pollOne(ctx context.Context, task *SoraTask) {
|
||||
pollCtx, cancel := context.WithTimeout(ctx, w.pollTimeout)
|
||||
defer cancel()
|
||||
|
||||
account, err := w.accountRepo.GetByID(pollCtx, task.AccountID)
|
||||
if err != nil {
|
||||
logger.LegacyPrintf("service.sora_task_worker",
|
||||
"[PollOne] task=%s get account=%d error: %v", task.ID, task.AccountID, err)
|
||||
w.markTaskFailed(pollCtx, task, "account not found")
|
||||
return
|
||||
}
|
||||
|
||||
if err := w.taskService.PollTask(pollCtx, task, account); err != nil {
|
||||
logger.LegacyPrintf("service.sora_task_worker",
|
||||
"[PollOne] task=%s poll error: %v", task.ID, err)
|
||||
return
|
||||
}
|
||||
|
||||
logger.LegacyPrintf("service.sora_task_worker",
|
||||
"[PollOne] task=%s status=%s progress=%d", task.ID, task.Status, task.Progress)
|
||||
|
||||
if task.Status == SoraTaskCompleted && task.VideoURL != "" && task.StoredKey == "" {
|
||||
w.tryStoreMedia(pollCtx, task)
|
||||
}
|
||||
}
|
||||
|
||||
// tryStoreMedia downloads media to configured storage.
|
||||
// Priority: S3 > local disk > keep upstream URL.
|
||||
func (w *SoraTaskWorker) tryStoreMedia(ctx context.Context, task *SoraTask) {
|
||||
mediaType := "video"
|
||||
if task.ObjectType == SoraObjectImage {
|
||||
mediaType = "image"
|
||||
}
|
||||
|
||||
if w.objectStorage != nil && w.objectStorage.Enabled(ctx) {
|
||||
key, _, storageType, err := w.objectStorage.UploadFromURL(ctx, 0, task.VideoURL)
|
||||
if err != nil {
|
||||
logger.LegacyPrintf("service.sora_task_worker",
|
||||
"[StoreMedia] task=%s object storage upload error: %v", task.ID, err)
|
||||
} else {
|
||||
task.StoredKey = key
|
||||
task.StorageType = storageType
|
||||
if updateErr := w.taskService.UpdateTask(ctx, task); updateErr != nil {
|
||||
logger.LegacyPrintf("service.sora_task_worker",
|
||||
"[StoreMedia] task=%s update stored_key error: %v", task.ID, updateErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if w.mediaStorage != nil && w.mediaStorage.Enabled() {
|
||||
stored, err := w.mediaStorage.StoreFromURLs(ctx, mediaType, []string{task.VideoURL})
|
||||
if err != nil {
|
||||
logger.LegacyPrintf("service.sora_task_worker",
|
||||
"[StoreMedia] task=%s local storage error: %v", task.ID, err)
|
||||
return
|
||||
}
|
||||
if len(stored) > 0 && stored[0] != task.VideoURL {
|
||||
task.StoredKey = stored[0]
|
||||
task.StorageType = "local"
|
||||
if updateErr := w.taskService.UpdateTask(ctx, task); updateErr != nil {
|
||||
logger.LegacyPrintf("service.sora_task_worker",
|
||||
"[StoreMedia] task=%s update stored_key error: %v", task.ID, updateErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *SoraTaskWorker) markTaskFailed(ctx context.Context, task *SoraTask, message string) {
|
||||
now := time.Now()
|
||||
task.Status = SoraTaskFailed
|
||||
task.ErrorMessage = message
|
||||
task.ErrorType = "server_error"
|
||||
task.CompletedAt = &now
|
||||
if updateErr := w.taskService.UpdateTask(ctx, task); updateErr != nil {
|
||||
logger.LegacyPrintf("service.sora_task_worker",
|
||||
"[PollOne] task=%s update failed: %v", task.ID, updateErr)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user