feat: sync deployed v1.0.0-rc.38-custom.42
Sync production source including routing, billing, Astra detection and public uptime. Update custom build instructions and license packaging.
This commit is contained in:
1 parent
aa6c3f50f4
commit
ceddfd3d6b
828 files changed
+122253
-13702
No files matched your search
@@ -3,6 +3,11 @@ package billing_setting
|
||||
// Built-in token prices use actual USD per million tokens. Keep new model
|
||||
// defaults here instead of splitting them across the legacy ratio tables.
|
||||
var builtinBillingExpr = map[string]string{
|
||||
// https://developers.openai.com/api/docs/pricing (Standard, 2026-09-09).
|
||||
// The Images API reports image output in output_tokens, normalized to c.
|
||||
"gpt-image-2": `tier("standard", p * 5 + cr * 1.25 + img * 8 + img_cr * 2 + c * 30)`,
|
||||
"gpt-image-2.5-sunburst": `tier("standard", p * 5 + cr * 1.25 + img * 8 + img_cr * 2 + c * 30)`,
|
||||
"gpt-image-2.5-flare": `tier("standard", p * 5 + cr * 1.25 + img * 8 + img_cr * 2 + c * 30)`,
|
||||
// https://developers.openai.com/api/docs/models/gpt-6-astra
|
||||
// Standard pricing; the long-context rates apply to the whole request.
|
||||
// Do not infer service-tier discounts from incoming request parameters:
|
||||
|
||||
@@ -109,3 +109,42 @@ func TestGPT6AstraBuiltinBilling(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageModelBuiltinPricesAndOverrides(t *testing.T) {
|
||||
settings := config.GlobalConfig.Get("billing_setting").(*billing_setting.BillingSetting)
|
||||
saved := *settings
|
||||
savedRatios, savedPrices := ratio_setting.ModelRatio2JSONString(), ratio_setting.ModelPrice2JSONString()
|
||||
t.Cleanup(func() {
|
||||
*settings = saved
|
||||
require.NoError(t, ratio_setting.UpdateModelRatioByJSONString(savedRatios))
|
||||
require.NoError(t, ratio_setting.UpdateModelPriceByJSONString(savedPrices))
|
||||
})
|
||||
for _, name := range []string{"gpt-image-2", "gpt-image-2.5-sunburst", "gpt-image-2.5-flare"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
*settings = billing_setting.BillingSetting{BillingMode: map[string]string{}, BillingExpr: map[string]string{}}
|
||||
require.NoError(t, ratio_setting.UpdateModelRatioByJSONString(`{}`))
|
||||
require.NoError(t, ratio_setting.UpdateModelPriceByJSONString(`{}`))
|
||||
expression, ok := billing_setting.GetBillingExpr(name)
|
||||
require.True(t, ok)
|
||||
usage := &dto.Usage{PromptTokens: 1000, CompletionTokens: 100, PromptTokensDetails: dto.InputTokenDetails{
|
||||
CachedTokens: 300, ImageTokens: 600, CachedTokensDetails: &dto.CachedTokenDetails{ImageTokens: common.GetPointer(200)},
|
||||
}}
|
||||
result, err := billingexpr.ComputeTieredQuota(&billingexpr.BillingSnapshot{ExprString: expression, ExprHash: billingexpr.ExprHashString(expression), GroupRatio: 1, QuotaPerUnit: 500000},
|
||||
service.BuildTieredTokenParams(usage, false, billingexpr.UsedVars(expression)))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 4113, result.ActualQuotaAfterGroup)
|
||||
encoded, err := common.Marshal(map[string]float64{name: 0})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, ratio_setting.UpdateModelRatioByJSONString(string(encoded)))
|
||||
assert.Equal(t, "ratio", billing_setting.GetBillingMode(name))
|
||||
require.NoError(t, ratio_setting.UpdateModelRatioByJSONString(`{}`))
|
||||
require.NoError(t, ratio_setting.UpdateModelPriceByJSONString(string(encoded)))
|
||||
assert.Equal(t, "ratio", billing_setting.GetBillingMode(name))
|
||||
settings.BillingMode[name] = "tiered_expr"
|
||||
settings.BillingExpr[name] = `tier("custom", p * 7)`
|
||||
actual, ok := billing_setting.GetBillingExpr(name)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, settings.BillingExpr[name], actual)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"maps"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/pkg/billingexpr"
|
||||
@@ -17,25 +18,29 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
BillingModeRatio = "ratio"
|
||||
BillingModeTieredExpr = "tiered_expr"
|
||||
BillingModeField = "billing_mode"
|
||||
BillingExprField = "billing_expr"
|
||||
maxTaskExprSmokeTests = 64
|
||||
BillingModeRatio = "ratio"
|
||||
BillingModeTieredExpr = "tiered_expr"
|
||||
BillingModeField = "billing_mode"
|
||||
BillingExprField = "billing_expr"
|
||||
PluginBillingExprOption = "billing_setting.plugin_billing_expr"
|
||||
maxTaskExprSmokeTests = 64
|
||||
)
|
||||
|
||||
// BillingSetting is managed by config.GlobalConfig.Register.
|
||||
// DB keys: billing_setting.billing_mode, billing_setting.billing_expr
|
||||
// DB keys: billing_setting.billing_mode, billing_setting.billing_expr,
|
||||
// billing_setting.plugin_billing_expr
|
||||
type BillingSetting struct {
|
||||
BillingMode map[string]string `json:"billing_mode"`
|
||||
BillingExpr map[string]string `json:"billing_expr"`
|
||||
GroupBillingExpr map[string]map[string]string `json:"group_billing_expr"`
|
||||
BillingMode map[string]string `json:"billing_mode"`
|
||||
BillingExpr map[string]string `json:"billing_expr"`
|
||||
PluginBillingExpr map[string]string `json:"plugin_billing_expr"`
|
||||
}
|
||||
|
||||
var billingSetting = BillingSetting{
|
||||
BillingMode: make(map[string]string),
|
||||
BillingExpr: make(map[string]string),
|
||||
GroupBillingExpr: make(map[string]map[string]string),
|
||||
BillingMode: make(map[string]string),
|
||||
BillingExpr: make(map[string]string),
|
||||
PluginBillingExpr: make(map[string]string),
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -80,6 +85,67 @@ func GetBuiltinBillingExpr(model string) (string, bool) {
|
||||
return expression, ok
|
||||
}
|
||||
|
||||
func PluginBillingExprKey(pluginKey, model string) string {
|
||||
return pluginKey + "::" + model
|
||||
}
|
||||
|
||||
func SplitPluginBillingExprKey(key string) (plugin, model string, ok bool) {
|
||||
plugin, model, ok = strings.Cut(key, "::")
|
||||
if !ok || !jsplugin.ValidPluginKey(plugin) || strings.TrimSpace(model) == "" {
|
||||
return "", "", false
|
||||
}
|
||||
return plugin, model, true
|
||||
}
|
||||
|
||||
func GetPluginBillingExprCopy() map[string]string {
|
||||
return maps.Clone(billingSetting.PluginBillingExpr)
|
||||
}
|
||||
|
||||
func GetPluginBillingExpr(pluginKey, model string) (string, bool) {
|
||||
expression, ok := billingSetting.PluginBillingExpr[PluginBillingExprKey(pluginKey, model)]
|
||||
return expression, ok
|
||||
}
|
||||
|
||||
// ResolveTaskBillingExpr selects the executing plugin's override before the
|
||||
// model expression, retaining the model alias fallback and explicit modes.
|
||||
func ResolveTaskBillingExpr(pluginKey, model, mappedModel string) (string, bool) {
|
||||
if pluginKey != "" {
|
||||
if expr, ok := GetPluginBillingExpr(pluginKey, model); ok {
|
||||
return expr, true
|
||||
}
|
||||
if mappedModel != "" && mappedModel != model {
|
||||
if expr, ok := GetPluginBillingExpr(pluginKey, mappedModel); ok {
|
||||
return expr, true
|
||||
}
|
||||
}
|
||||
}
|
||||
if GetBillingMode(model) == BillingModeTieredExpr {
|
||||
return GetBillingExpr(model)
|
||||
}
|
||||
if mappedModel != "" && mappedModel != model && GetBillingMode(mappedModel) == BillingModeTieredExpr {
|
||||
expression, ok := GetBillingExpr(mappedModel)
|
||||
return expression, ok && strings.TrimSpace(expression) != ""
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// TaskExprCompatible checks the schema contract even for usage references in
|
||||
// branches that the current request would not evaluate.
|
||||
func TaskExprCompatible(expression string, schema map[string]jsplugin.UsageFieldSchema) bool {
|
||||
if strings.TrimSpace(expression) == "" {
|
||||
return false
|
||||
}
|
||||
if _, err := billingexpr.CompileFromCache(expression); err != nil {
|
||||
return false
|
||||
}
|
||||
for key := range billingexpr.UsedUsageKeys(expression) {
|
||||
if _, exists := schema[key]; !exists {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return !billingexpr.UsesFixedPricing(expression)
|
||||
}
|
||||
|
||||
func GetBuiltinBillingExprCopy() map[string]string {
|
||||
return lo.Assign(builtinBillingExpr)
|
||||
}
|
||||
@@ -157,6 +223,9 @@ func smokeTestExpr(exprStr string) error {
|
||||
{P: 1000, C: 1000, Len: 1000},
|
||||
{P: 100000, C: 100000, Len: 100000},
|
||||
{P: 1000000, C: 1000000, Len: 1000000},
|
||||
{P: 300, C: 100, Len: 1000, CR: 100, Img: 400, ImgCR: 200},
|
||||
{P: 800, C: 50, Len: 1000, AI: 200, AO: 50},
|
||||
{Len: math.MaxInt32, ImgCR: math.MaxInt32},
|
||||
}
|
||||
|
||||
for _, v := range vectors {
|
||||
@@ -180,6 +249,9 @@ func SmokeTestTaskExpr(exprStr string, schema map[string]jsplugin.UsageFieldSche
|
||||
if _, err := billingexpr.CompileFromCache(exprStr); err != nil {
|
||||
return err
|
||||
}
|
||||
if billingexpr.UsesFixedPricing(exprStr) {
|
||||
return fmt.Errorf("fixed pricing is not supported for task usage expressions")
|
||||
}
|
||||
for key := range billingexpr.UsedUsageKeys(exprStr) {
|
||||
if _, declared := schema[key]; !declared {
|
||||
return fmt.Errorf("usage key %q is not declared by the task plugin", key)
|
||||
|
||||
@@ -25,6 +25,12 @@ func TestSmokeTestTaskExprValidatesDeclaredUsageVectors(t *testing.T) {
|
||||
expression string
|
||||
expectedError string
|
||||
}{
|
||||
{
|
||||
name: "fixed prices are not task usage prices",
|
||||
schema: videoSchema,
|
||||
expression: `true ? tier("normal", u("seconds") * 0.4) : tier("fixed", fixed(0.01))`,
|
||||
expectedError: "fixed pricing is not supported for task usage expressions",
|
||||
},
|
||||
{
|
||||
name: "declared numeric and enum facts",
|
||||
schema: videoSchema,
|
||||
|
||||
Reference in new issue
Block a user