(tenant config.TenantConfig, req types.AIRequest, base types.RouteDecision, state types.RuntimeState)
| 107 | } |
| 108 | |
| 109 | func (r *RuleRouter) optimizeByCost(tenant config.TenantConfig, req types.AIRequest, base types.RouteDecision, state types.RuntimeState) types.RouteDecision { |
| 110 | if r.costEngine == nil { |
| 111 | return base |
| 112 | } |
| 113 | if state.OverloadDegrade { |
| 114 | return base |
| 115 | } |
| 116 | |
| 117 | best := base |
| 118 | bestCost := base.EstimatedCost |
| 119 | |
| 120 | for _, backend := range r.cfg.Backends { |
| 121 | if !backendSelectable(state, backend.Name) { |
| 122 | continue |
| 123 | } |
| 124 | if !supportsTask(backend, req.TaskType) { |
| 125 | continue |
| 126 | } |
| 127 | |
| 128 | estimate := r.costEngine.Estimate(req, types.BackendMetadata{ |
| 129 | Name: backend.Name, |
| 130 | Type: backend.Type, |
| 131 | Capabilities: backend.Capabilities, |
| 132 | CostUnit: backend.Cost.Unit, |
| 133 | MaxConcurrency: backend.MaxConcurrency, |
| 134 | }) |
| 135 | if tenant.BudgetPerRequest > 0 && estimate.EstimatedTotal > tenant.BudgetPerRequest { |
| 136 | continue |
| 137 | } |
| 138 | |
| 139 | if estimate.EstimatedTotal < bestCost { |
| 140 | best = types.RouteDecision{ |
| 141 | BackendName: backend.Name, |
| 142 | Reason: "cost-optimized-from-" + base.Reason, |
| 143 | EstimatedCost: estimate.EstimatedTotal, |
| 144 | } |
| 145 | bestCost = estimate.EstimatedTotal |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return best |
| 150 | } |
| 151 | |
| 152 | func supportsTask(backend config.BackendConfig, taskType string) bool { |
| 153 | if taskType == "" { |
no test coverage detected