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
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user