49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/easyai/easyai-ai-gateway/apps/api/internal/store"
|
|
)
|
|
|
|
func (s *Server) platformModelResponse(ctx context.Context, model store.PlatformModel) store.PlatformModel {
|
|
model = s.withEffectiveResponseBillingConfig(ctx, model)
|
|
return store.FilterPlatformModelBillingConfig(model)
|
|
}
|
|
|
|
func (s *Server) platformModelResponses(ctx context.Context, models []store.PlatformModel) []store.PlatformModel {
|
|
items := make([]store.PlatformModel, len(models))
|
|
for i, model := range models {
|
|
items[i] = s.platformModelResponse(ctx, model)
|
|
}
|
|
return items
|
|
}
|
|
|
|
func (s *Server) withEffectiveResponseBillingConfig(ctx context.Context, model store.PlatformModel) store.PlatformModel {
|
|
config := model.BillingConfig
|
|
if model.PricingRuleSetID != "" {
|
|
if ruleSetConfig, err := s.store.PricingRuleSetBillingConfig(ctx, model.PricingRuleSetID); err == nil && len(ruleSetConfig) > 0 {
|
|
config = ruleSetConfig
|
|
}
|
|
}
|
|
if len(model.BillingConfigOverride) > 0 {
|
|
config = mergeResponseBillingConfig(config, model.BillingConfigOverride)
|
|
}
|
|
model.BillingConfig = config
|
|
return model
|
|
}
|
|
|
|
func mergeResponseBillingConfig(base map[string]any, override map[string]any) map[string]any {
|
|
if len(base) == 0 && len(override) == 0 {
|
|
return nil
|
|
}
|
|
out := make(map[string]any, len(base)+len(override))
|
|
for key, value := range base {
|
|
out[key] = value
|
|
}
|
|
for key, value := range override {
|
|
out[key] = value
|
|
}
|
|
return out
|
|
}
|