|
| 1 | +package scheduler |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/DouDOU-start/airgate-core/ent" |
| 7 | +) |
| 8 | + |
| 9 | +type Workload string |
| 10 | + |
| 11 | +const ( |
| 12 | + WorkloadChat Workload = "chat" |
| 13 | + WorkloadImage Workload = "image" |
| 14 | +) |
| 15 | + |
| 16 | +type ImageProtocol string |
| 17 | + |
| 18 | +const ( |
| 19 | + ImageProtocolImagesAPI ImageProtocol = "images_api" |
| 20 | + ImageProtocolResponsesTool ImageProtocol = "responses_tool" |
| 21 | +) |
| 22 | + |
| 23 | +type AccountRequirements struct { |
| 24 | + Workload Workload |
| 25 | + ImageProtocols []ImageProtocol |
| 26 | +} |
| 27 | + |
| 28 | +func filterAccountsByRequirements(candidates []*ent.Account, req AccountRequirements) []*ent.Account { |
| 29 | + if req.Workload == "" && len(req.ImageProtocols) == 0 { |
| 30 | + return candidates |
| 31 | + } |
| 32 | + filtered := make([]*ent.Account, 0, len(candidates)) |
| 33 | + for _, acc := range candidates { |
| 34 | + if accountMatchesRequirements(acc, req) { |
| 35 | + filtered = append(filtered, acc) |
| 36 | + } |
| 37 | + } |
| 38 | + return filtered |
| 39 | +} |
| 40 | + |
| 41 | +func accountMatchesRequirements(acc *ent.Account, req AccountRequirements) bool { |
| 42 | + if acc == nil { |
| 43 | + return false |
| 44 | + } |
| 45 | + if req.Workload != "" && !accountAllowsWorkload(acc, req.Workload) { |
| 46 | + return false |
| 47 | + } |
| 48 | + if len(req.ImageProtocols) > 0 && !accountAllowsAnyImageProtocol(acc, req.ImageProtocols) { |
| 49 | + return false |
| 50 | + } |
| 51 | + return true |
| 52 | +} |
| 53 | + |
| 54 | +func accountAllowsWorkload(acc *ent.Account, workload Workload) bool { |
| 55 | + allowed := extraStringSet(acc.Extra, "allowed_workloads") |
| 56 | + if len(allowed) == 0 { |
| 57 | + return true |
| 58 | + } |
| 59 | + _, ok := allowed[string(workload)] |
| 60 | + return ok |
| 61 | +} |
| 62 | + |
| 63 | +func accountAllowsAnyImageProtocol(acc *ent.Account, required []ImageProtocol) bool { |
| 64 | + allowed := accountImageProtocolSet(acc) |
| 65 | + for _, protocol := range required { |
| 66 | + if _, ok := allowed[string(protocol)]; ok { |
| 67 | + return true |
| 68 | + } |
| 69 | + } |
| 70 | + return false |
| 71 | +} |
| 72 | + |
| 73 | +func accountImageProtocolSet(acc *ent.Account) map[string]struct{} { |
| 74 | + allowed := extraStringSet(acc.Extra, "image_protocols") |
| 75 | + if len(allowed) > 0 { |
| 76 | + return allowed |
| 77 | + } |
| 78 | + if acc != nil { |
| 79 | + if strings.TrimSpace(acc.Credentials["access_token"]) != "" { |
| 80 | + return map[string]struct{}{string(ImageProtocolResponsesTool): {}} |
| 81 | + } |
| 82 | + if strings.TrimSpace(acc.Credentials["api_key"]) != "" { |
| 83 | + return map[string]struct{}{string(ImageProtocolImagesAPI): {}} |
| 84 | + } |
| 85 | + } |
| 86 | + return map[string]struct{}{ |
| 87 | + string(ImageProtocolImagesAPI): {}, |
| 88 | + string(ImageProtocolResponsesTool): {}, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func AccountImageProtocols(acc *ent.Account) []string { |
| 93 | + set := accountImageProtocolSet(acc) |
| 94 | + if len(set) == 0 { |
| 95 | + return nil |
| 96 | + } |
| 97 | + protocols := make([]string, 0, len(set)) |
| 98 | + for _, protocol := range []string{string(ImageProtocolImagesAPI), string(ImageProtocolResponsesTool)} { |
| 99 | + if _, ok := set[protocol]; ok { |
| 100 | + protocols = append(protocols, protocol) |
| 101 | + delete(set, protocol) |
| 102 | + } |
| 103 | + } |
| 104 | + for protocol := range set { |
| 105 | + protocols = append(protocols, protocol) |
| 106 | + } |
| 107 | + return protocols |
| 108 | +} |
| 109 | + |
| 110 | +func extraStringSet(extra map[string]interface{}, key string) map[string]struct{} { |
| 111 | + if len(extra) == 0 { |
| 112 | + return nil |
| 113 | + } |
| 114 | + raw, ok := extra[key] |
| 115 | + if !ok || raw == nil { |
| 116 | + return nil |
| 117 | + } |
| 118 | + values := make(map[string]struct{}) |
| 119 | + add := func(value string) { |
| 120 | + for _, part := range strings.Split(value, ",") { |
| 121 | + normalized := strings.ToLower(strings.TrimSpace(part)) |
| 122 | + if normalized != "" { |
| 123 | + values[normalized] = struct{}{} |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + switch v := raw.(type) { |
| 128 | + case string: |
| 129 | + add(v) |
| 130 | + case []string: |
| 131 | + for _, item := range v { |
| 132 | + add(item) |
| 133 | + } |
| 134 | + case []interface{}: |
| 135 | + for _, item := range v { |
| 136 | + if s, ok := item.(string); ok { |
| 137 | + add(s) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + if len(values) == 0 { |
| 142 | + return nil |
| 143 | + } |
| 144 | + return values |
| 145 | +} |
0 commit comments