Commit b8c56ff9 authored by erio's avatar erio
Browse files

fix: validate prices must be >= 0, remove debug logs

parent 38da737e
...@@ -226,14 +226,34 @@ func pricingRequestToService(reqs []channelModelPricingRequest) []service.Channe ...@@ -226,14 +226,34 @@ func pricingRequestToService(reqs []channelModelPricingRequest) []service.Channe
return result return result
} }
// validatePricingBillingMode 校验按次/图片计费模式必须配置 PerRequestPrice 或 Intervals // validatePricingBillingMode 校验计费配置
func validatePricingBillingMode(pricing []service.ChannelModelPricing) error { func validatePricingBillingMode(pricing []service.ChannelModelPricing) error {
for _, p := range pricing { for _, p := range pricing {
// 按次/图片模式必须配置默认价格或区间
if p.BillingMode == service.BillingModePerRequest || p.BillingMode == service.BillingModeImage { if p.BillingMode == service.BillingModePerRequest || p.BillingMode == service.BillingModeImage {
if p.PerRequestPrice == nil && len(p.Intervals) == 0 { if p.PerRequestPrice == nil && len(p.Intervals) == 0 {
return errors.New("per-request price or intervals required for per_request/image billing mode") return errors.New("per-request price or intervals required for per_request/image billing mode")
} }
} }
// 校验价格不能为负
if err := validatePriceNotNegative("input_price", p.InputPrice); err != nil {
return err
}
if err := validatePriceNotNegative("output_price", p.OutputPrice); err != nil {
return err
}
if err := validatePriceNotNegative("cache_write_price", p.CacheWritePrice); err != nil {
return err
}
if err := validatePriceNotNegative("cache_read_price", p.CacheReadPrice); err != nil {
return err
}
if err := validatePriceNotNegative("image_output_price", p.ImageOutputPrice); err != nil {
return err
}
if err := validatePriceNotNegative("per_request_price", p.PerRequestPrice); err != nil {
return err
}
// 校验 interval:至少有一个价格字段非空 // 校验 interval:至少有一个价格字段非空
for _, iv := range p.Intervals { for _, iv := range p.Intervals {
if iv.InputPrice == nil && iv.OutputPrice == nil && if iv.InputPrice == nil && iv.OutputPrice == nil &&
...@@ -247,6 +267,13 @@ func validatePricingBillingMode(pricing []service.ChannelModelPricing) error { ...@@ -247,6 +267,13 @@ func validatePricingBillingMode(pricing []service.ChannelModelPricing) error {
return nil return nil
} }
func validatePriceNotNegative(field string, val *float64) error {
if val != nil && *val < 0 {
return fmt.Errorf("%s must be >= 0", field)
}
return nil
}
func formatMaxTokens(max *int) string { func formatMaxTokens(max *int) string {
if max == nil { if max == nil {
return "∞" return "∞"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment