mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
- 门禁作用于展示该开关的 Gemini 系分组(gemini/antigravity/composite);无分组 Key、 anthropic 等其他平台分组不受影响,OpenAI/Grok 仍走各自门禁 - 生图判定:复用图片接口的 gemini-*-image 判定(isGeminiCompatibleImageModel),或请求体 generationConfig.responseModalities 含 IMAGE(camelCase/snake_case、字符串/整数枚举、 重复键逐个检查);countTokens 不拦 - 两道检查:/v1/messages、/v1/chat/completions、/v1/responses、/v1beta/models 入口在 选号前按客户端请求名与渠道映射名预检;Gemini 兼容与 Antigravity 的 6 个转发函数在 账号模型映射后再按上游模型名检查,覆盖账号级别名 - 拒绝统一返回 400(Gemini 原生 INVALID_ARGUMENT,其余 invalid_request_error), 不触发换号与账号处罚,下游 sub2api 把本实例当上游时不会因此停用账号; 请求标记为本地功能门禁 - 升级兼容:迁移 246 把存量 gemini/antigravity 分组的生图开关置为开启;新建分组与简易 模式默认分组对 gemini/antigravity 默认开启(与 grok 同为 OR 语义,创建时传 false 不生效), 前端创建表单同步默认值;composite 不回填(同一开关还控制 OpenAI 生图),未开启的 composite 分组升级后 Gemini 生图返回 400 - 未覆盖:最终模型名不符合 gemini-*-image 形态(如上游中转自定义的 nano-banana 别名) 且请求体未声明 IMAGE 模态;门禁按 Key 所属分组判断,Claude Code 专用分组降级到 其他分组时仍按原分组
109 lines
2.9 KiB
Go
109 lines
2.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
dbent "github.com/Wei-Shaw/sub2api/ent"
|
|
"github.com/Wei-Shaw/sub2api/ent/group"
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
)
|
|
|
|
const simpleModeDefaultGroupDescription = "Auto-created default group"
|
|
|
|
func ensureSimpleModeDefaultGroups(ctx context.Context, client *dbent.Client) error {
|
|
if client == nil {
|
|
return fmt.Errorf("nil ent client")
|
|
}
|
|
|
|
if err := backfillSimpleModeGrokDefaultImageGeneration(ctx, client); err != nil {
|
|
return err
|
|
}
|
|
|
|
requiredByPlatform := map[string]int{
|
|
service.PlatformAnthropic: 1,
|
|
service.PlatformOpenAI: 1,
|
|
service.PlatformGemini: 1,
|
|
service.PlatformAntigravity: 2,
|
|
service.PlatformGrok: 1,
|
|
}
|
|
|
|
for platform, minCount := range requiredByPlatform {
|
|
count, err := client.Group.Query().
|
|
Where(group.PlatformEQ(platform), group.DeletedAtIsNil()).
|
|
Count(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("count groups for platform %s: %w", platform, err)
|
|
}
|
|
|
|
if platform == service.PlatformAntigravity {
|
|
if count < minCount {
|
|
for i := count; i < minCount; i++ {
|
|
name := fmt.Sprintf("%s-default-%d", platform, i+1)
|
|
if err := createGroupIfNotExists(ctx, client, name, platform); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
|
|
// Non-antigravity platforms: ensure <platform>-default exists.
|
|
name := platform + "-default"
|
|
if err := createGroupIfNotExists(ctx, client, name, platform); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func createGroupIfNotExists(ctx context.Context, client *dbent.Client, name, platform string) error {
|
|
exists, err := client.Group.Query().
|
|
Where(group.NameEQ(name), group.DeletedAtIsNil()).
|
|
Exist(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("check group exists %s: %w", name, err)
|
|
}
|
|
if exists {
|
|
return nil
|
|
}
|
|
|
|
_, err = client.Group.Create().
|
|
SetName(name).
|
|
SetDescription(simpleModeDefaultGroupDescription).
|
|
SetPlatform(platform).
|
|
SetStatus(service.StatusActive).
|
|
SetSubscriptionType(service.SubscriptionTypeStandard).
|
|
SetRateMultiplier(1.0).
|
|
SetIsExclusive(false).
|
|
SetAllowImageGeneration(service.DefaultAllowImageGenerationForPlatform(platform)).
|
|
Save(ctx)
|
|
if err != nil {
|
|
if dbent.IsConstraintError(err) {
|
|
// Concurrent server startups may race on creation; treat as success.
|
|
return nil
|
|
}
|
|
return fmt.Errorf("create default group %s: %w", name, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func backfillSimpleModeGrokDefaultImageGeneration(ctx context.Context, client *dbent.Client) error {
|
|
_, err := client.Group.Update().
|
|
Where(
|
|
group.NameEQ(service.PlatformGrok+"-default"),
|
|
group.PlatformEQ(service.PlatformGrok),
|
|
group.DescriptionEQ(simpleModeDefaultGroupDescription),
|
|
group.StatusEQ(service.StatusActive),
|
|
group.AllowImageGenerationEQ(false),
|
|
group.DeletedAtIsNil(),
|
|
).
|
|
SetAllowImageGeneration(true).
|
|
Save(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("backfill auto-created grok default image generation: %w", err)
|
|
}
|
|
return nil
|
|
}
|