simple_mode_default_groups.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package repository
  2. import (
  3. "context"
  4. "fmt"
  5. dbent "github.com/Wei-Shaw/sub2api/ent"
  6. "github.com/Wei-Shaw/sub2api/ent/group"
  7. "github.com/Wei-Shaw/sub2api/internal/service"
  8. )
  9. const simpleModeDefaultGroupDescription = "Auto-created default group"
  10. func ensureSimpleModeDefaultGroups(ctx context.Context, client *dbent.Client) error {
  11. if client == nil {
  12. return fmt.Errorf("nil ent client")
  13. }
  14. if err := backfillSimpleModeGrokDefaultImageGeneration(ctx, client); err != nil {
  15. return err
  16. }
  17. requiredByPlatform := map[string]int{
  18. service.PlatformAnthropic: 1,
  19. service.PlatformOpenAI: 1,
  20. service.PlatformGemini: 1,
  21. service.PlatformAntigravity: 2,
  22. service.PlatformGrok: 1,
  23. }
  24. for platform, minCount := range requiredByPlatform {
  25. count, err := client.Group.Query().
  26. Where(group.PlatformEQ(platform), group.DeletedAtIsNil()).
  27. Count(ctx)
  28. if err != nil {
  29. return fmt.Errorf("count groups for platform %s: %w", platform, err)
  30. }
  31. if platform == service.PlatformAntigravity {
  32. if count < minCount {
  33. for i := count; i < minCount; i++ {
  34. name := fmt.Sprintf("%s-default-%d", platform, i+1)
  35. if err := createGroupIfNotExists(ctx, client, name, platform); err != nil {
  36. return err
  37. }
  38. }
  39. }
  40. continue
  41. }
  42. // Non-antigravity platforms: ensure <platform>-default exists.
  43. name := platform + "-default"
  44. if err := createGroupIfNotExists(ctx, client, name, platform); err != nil {
  45. return err
  46. }
  47. }
  48. return nil
  49. }
  50. func createGroupIfNotExists(ctx context.Context, client *dbent.Client, name, platform string) error {
  51. exists, err := client.Group.Query().
  52. Where(group.NameEQ(name), group.DeletedAtIsNil()).
  53. Exist(ctx)
  54. if err != nil {
  55. return fmt.Errorf("check group exists %s: %w", name, err)
  56. }
  57. if exists {
  58. return nil
  59. }
  60. _, err = client.Group.Create().
  61. SetName(name).
  62. SetDescription(simpleModeDefaultGroupDescription).
  63. SetPlatform(platform).
  64. SetStatus(service.StatusActive).
  65. SetSubscriptionType(service.SubscriptionTypeStandard).
  66. SetRateMultiplier(1.0).
  67. SetIsExclusive(false).
  68. SetAllowImageGeneration(platform == service.PlatformGrok).
  69. Save(ctx)
  70. if err != nil {
  71. if dbent.IsConstraintError(err) {
  72. // Concurrent server startups may race on creation; treat as success.
  73. return nil
  74. }
  75. return fmt.Errorf("create default group %s: %w", name, err)
  76. }
  77. return nil
  78. }
  79. func backfillSimpleModeGrokDefaultImageGeneration(ctx context.Context, client *dbent.Client) error {
  80. _, err := client.Group.Update().
  81. Where(
  82. group.NameEQ(service.PlatformGrok+"-default"),
  83. group.PlatformEQ(service.PlatformGrok),
  84. group.DescriptionEQ(simpleModeDefaultGroupDescription),
  85. group.StatusEQ(service.StatusActive),
  86. group.AllowImageGenerationEQ(false),
  87. group.DeletedAtIsNil(),
  88. ).
  89. SetAllowImageGeneration(true).
  90. Save(ctx)
  91. if err != nil {
  92. return fmt.Errorf("backfill auto-created grok default image generation: %w", err)
  93. }
  94. return nil
  95. }