group_codex_models_manifest.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors"
  7. )
  8. // maxCodexModelsManifestAccounts is the upper bound of pinned accounts per group.
  9. const maxCodexModelsManifestAccounts = 10
  10. // normalizeCodexModelsManifestConfig normalizes a group's pinned-accounts Codex
  11. // manifest config before persistence:
  12. // - Non-OpenAI platforms always persist a disabled zero config (spec: silently
  13. // normalize instead of rejecting).
  14. // - Account IDs are de-duplicated preserving first-seen order; invalid IDs (<= 0)
  15. // are dropped. The list is kept even when disabled so toggling enabled back
  16. // on does not lose the previous selection.
  17. func normalizeCodexModelsManifestConfig(platform string, cfg GroupCodexModelsManifestConfig) GroupCodexModelsManifestConfig {
  18. if platform != PlatformOpenAI {
  19. return GroupCodexModelsManifestConfig{}
  20. }
  21. out := GroupCodexModelsManifestConfig{
  22. Enabled: cfg.Enabled,
  23. FallbackToScheduler: cfg.FallbackToScheduler,
  24. }
  25. if len(cfg.AccountIDs) == 0 {
  26. return out
  27. }
  28. seen := make(map[int64]struct{}, len(cfg.AccountIDs))
  29. out.AccountIDs = make([]int64, 0, len(cfg.AccountIDs))
  30. for _, id := range cfg.AccountIDs {
  31. if id <= 0 {
  32. continue
  33. }
  34. if _, ok := seen[id]; ok {
  35. continue
  36. }
  37. seen[id] = struct{}{}
  38. out.AccountIDs = append(out.AccountIDs, id)
  39. }
  40. if len(out.AccountIDs) == 0 {
  41. out.AccountIDs = nil
  42. }
  43. return out
  44. }
  45. // validateCodexModelsManifestConfig enforces the admin contract for an enabled
  46. // pinned-accounts config: at least one account, at most
  47. // maxCodexModelsManifestAccounts, and every account must be an active OpenAI
  48. // member of the group. Validation failures return 400 with reason
  49. // INVALID_CODEX_MODELS_MANIFEST_CONFIG and never reach persistence.
  50. func (s *adminServiceImpl) validateCodexModelsManifestConfig(ctx context.Context, groupID int64, cfg GroupCodexModelsManifestConfig) error {
  51. if !cfg.Enabled {
  52. return nil
  53. }
  54. if len(cfg.AccountIDs) == 0 {
  55. return infraerrors.New(http.StatusBadRequest, "INVALID_CODEX_MODELS_MANIFEST_CONFIG", "codex models manifest config requires at least one account")
  56. }
  57. if len(cfg.AccountIDs) > maxCodexModelsManifestAccounts {
  58. return infraerrors.Newf(http.StatusBadRequest, "INVALID_CODEX_MODELS_MANIFEST_CONFIG", "codex models manifest config allows at most %d accounts, got %d", maxCodexModelsManifestAccounts, len(cfg.AccountIDs))
  59. }
  60. accounts, err := s.accountRepo.ListByGroup(ctx, groupID)
  61. if err != nil {
  62. return fmt.Errorf("load group accounts for codex models manifest config: %w", err)
  63. }
  64. members := make(map[int64]struct{}, len(accounts))
  65. for i := range accounts {
  66. acc := &accounts[i]
  67. if acc.IsActive() && acc.Platform == PlatformOpenAI {
  68. members[acc.ID] = struct{}{}
  69. }
  70. }
  71. var invalid []int64
  72. for _, id := range cfg.AccountIDs {
  73. if _, ok := members[id]; !ok {
  74. invalid = append(invalid, id)
  75. }
  76. }
  77. if len(invalid) > 0 {
  78. return infraerrors.Newf(http.StatusBadRequest, "INVALID_CODEX_MODELS_MANIFEST_CONFIG", "codex models manifest config contains accounts not in this group or not active openai accounts: %v", invalid)
  79. }
  80. return nil
  81. }