Compare commits

...

16 Commits

Author SHA1 Message Date
CaIon
bee339d279
fix: always serialize ratio/price values for all models to ensure fallback during sync delays
Some checks failed
Publish Docker image (Multi Registries, native amd64+arm64) / Build & push (amd64) [native] (push) Has been cancelled
Publish Docker image (Multi Registries, native amd64+arm64) / Build & push (arm64) [native] (push) Has been cancelled
Publish Docker image (Multi Registries, native amd64+arm64) / Create multi-arch manifests (Docker Hub) (push) Has been cancelled
Build Electron App / build (windows-latest) (push) Has been cancelled
Build Electron App / release (push) Has been cancelled
Release (Linux, macOS, Windows) / Linux Release (push) Has been cancelled
Release (Linux, macOS, Windows) / macOS Release (push) Has been cancelled
Release (Linux, macOS, Windows) / Windows Release (push) Has been cancelled
2026-04-27 22:07:46 +08:00
CaIon
4e93148d9e
fix: ensure proper handling of JSON unmarshalling for maps in config update 2026-04-27 22:07:46 +08:00
Calcium-Ion
e36d191c2e
Merge pull request #4450 from feitianbubu/pr/7fa4a87ad953642a2f454ad0813a0c8b6ac361c6
增加用户创建时间和最后登录时间
2026-04-26 22:12:22 +08:00
Calcium-Ion
34afe9b426
Merge pull request #4470 from seefs001/feature/show-removed-upstream-models
feat: show removed upstream models in fetch models modal
2026-04-26 20:20:21 +08:00
Calcium-Ion
d604f48c06
Merge pull request #4469 from seefs001/fix/tool-arguments-object
fix: support raw JSON response tool arguments
2026-04-26 20:20:03 +08:00
Calcium-Ion
86cfb3920e
Merge pull request #4468 from seefs001/feature/ali-anthropic-messsages-model-configure
feat: configure native messages model matching for ali
2026-04-26 20:19:37 +08:00
Calcium-Ion
097a50ebdc
fix: clarify affinity disabled channel retry message (#4453) 2026-04-26 20:18:02 +08:00
Seefs
f424f906d8
feat: sync upstream pricing from pricing endpoint (#4452)
* feat: sync upstream pricing from pricing endpoint

* feat: sync upstream pricing with expression priority

* fix: add feedback while syncing upstream pricing

* fix: show loading state for empty upstream pricing sync
2026-04-26 20:17:35 +08:00
Calcium-Ion
cc4ad6c39e
Merge pull request #4437 from seefs001/fix/channel-upstream-model-sync
fix(channel): load model mapping during upstream model checks
2026-04-26 20:17:14 +08:00
Seefs
4c21c4c43b
feat: show removed upstream models in fetch models modal 2026-04-26 14:24:43 +08:00
Seefs
db89b57e1c
fix: support raw JSON response tool arguments 2026-04-26 13:47:37 +08:00
Seefs
62d4b63fc3
feat: configure native messages model matching 2026-04-26 13:37:59 +08:00
Seefs
355307223a
fix: clarify affinity disabled channel retry message 2026-04-25 17:43:42 +08:00
CaIon
f2f3410dcf
feat: add len variable for tier conditions and LLM prompt helper
Some checks failed
Publish Docker image (Multi Registries, native amd64+arm64) / Build & push (amd64) [native] (push) Has been cancelled
Publish Docker image (Multi Registries, native amd64+arm64) / Build & push (arm64) [native] (push) Has been cancelled
Publish Docker image (Multi Registries, native amd64+arm64) / Create multi-arch manifests (Docker Hub) (push) Has been cancelled
Release (Linux, macOS, Windows) / Linux Release (push) Has been cancelled
Release (Linux, macOS, Windows) / macOS Release (push) Has been cancelled
Release (Linux, macOS, Windows) / Windows Release (push) Has been cancelled
2026-04-25 13:24:20 +08:00
feitianbubu
02aacb38a2
feat: add user created_at and last_login_at 2026-04-25 12:44:44 +08:00
Seefs
095e1920f1
fix(channel): load model mapping during upstream model checks 2026-04-24 17:51:46 +08:00
42 changed files with 1399 additions and 352 deletions

View File

@ -43,3 +43,19 @@ func GetJsonType(data json.RawMessage) string {
return "number"
}
}
// JsonRawMessageToString returns JSON strings as their decoded value and other JSON values as raw text.
func JsonRawMessageToString(data json.RawMessage) string {
trimmed := bytes.TrimSpace(data)
if len(trimmed) == 0 || bytes.Equal(trimmed, []byte("null")) {
return ""
}
if trimmed[0] != '"' {
return string(trimmed)
}
var value string
if err := Unmarshal(trimmed, &value); err != nil {
return string(trimmed)
}
return value
}

43
common/json_test.go Normal file
View File

@ -0,0 +1,43 @@
package common
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
func TestJsonRawMessageToString(t *testing.T) {
tests := []struct {
name string
data json.RawMessage
want string
}{
{
name: "object",
data: json.RawMessage(`{"city":"Paris","days":0,"strict":false}`),
want: `{"city":"Paris","days":0,"strict":false}`,
},
{
name: "string",
data: json.RawMessage(`"{\"city\":\"Paris\",\"days\":0,\"strict\":false}"`),
want: `{"city":"Paris","days":0,"strict":false}`,
},
{
name: "null",
data: json.RawMessage(`null`),
want: "",
},
{
name: "empty",
data: nil,
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, JsonRawMessageToString(tt.data))
})
}
}

View File

@ -32,6 +32,26 @@ const (
channelUpstreamModelUpdateNotifyMaxFailedChannelIDs = 10
)
var channelUpstreamModelUpdateSelectFields = []string{
"id",
"name",
"type",
"key",
"status",
"base_url",
"models",
"model_mapping",
"settings",
"setting",
"other",
"group",
"priority",
"weight",
"tag",
"channel_info",
"header_override",
}
var (
channelUpstreamModelUpdateTaskOnce sync.Once
channelUpstreamModelUpdateTaskRunning atomic.Bool
@ -521,7 +541,7 @@ func runChannelUpstreamModelUpdateTaskOnce() {
for {
var channels []*model.Channel
query := model.DB.
Select("id", "name", "type", "key", "status", "base_url", "models", "settings", "setting", "other", "group", "priority", "weight", "tag", "channel_info", "header_override").
Select(channelUpstreamModelUpdateSelectFields).
Where("status = ?", common.ChannelStatusEnabled).
Order("id asc").
Limit(channelUpstreamModelUpdateTaskBatchSize)
@ -814,7 +834,7 @@ func collectPendingApplyUpstreamModelChanges(settings dto.ChannelOtherSettings)
func findEnabledChannelsAfterID(lastID int, batchSize int) ([]*model.Channel, error) {
var channels []*model.Channel
query := model.DB.
Select("id", "name", "type", "key", "status", "base_url", "models", "settings", "setting", "other", "group", "priority", "weight", "tag", "channel_info", "header_override").
Select(channelUpstreamModelUpdateSelectFields).
Where("status = ?", common.ChannelStatusEnabled).
Order("id asc").
Limit(batchSize)

View File

@ -81,6 +81,10 @@ func TestCollectPendingApplyUpstreamModelChanges(t *testing.T) {
require.Equal(t, []string{"old-model"}, pendingRemoveModels)
}
func TestChannelUpstreamModelUpdateSelectFieldsIncludeModelMapping(t *testing.T) {
require.Contains(t, channelUpstreamModelUpdateSelectFields, "model_mapping")
}
func TestNormalizeChannelModelMapping(t *testing.T) {
modelMapping := `{
" alias-model ": " upstream-model ",

View File

@ -21,14 +21,16 @@ import (
"github.com/QuantumNous/new-api/dto"
"github.com/QuantumNous/new-api/model"
"github.com/QuantumNous/new-api/setting/billing_setting"
"github.com/QuantumNous/new-api/setting/ratio_setting"
"github.com/samber/lo"
"github.com/gin-gonic/gin"
)
const (
defaultTimeoutSeconds = 10
defaultEndpoint = "/api/ratio_config"
defaultEndpoint = "/api/pricing"
maxConcurrentFetches = 8
maxRatioConfigBytes = 10 << 20 // 10MB
floatEpsilon = 1e-9
@ -59,7 +61,29 @@ func valuesEqual(a, b interface{}) bool {
return a == b
}
var ratioTypes = []string{"model_ratio", "completion_ratio", "cache_ratio", "model_price"}
var pricingSyncFields = []string{
"model_ratio",
"completion_ratio",
"cache_ratio",
"create_cache_ratio",
"image_ratio",
"audio_ratio",
"audio_completion_ratio",
"model_price",
billing_setting.BillingModeField,
billing_setting.BillingExprField,
}
var numericPricingSyncFields = map[string]bool{
"model_ratio": true,
"completion_ratio": true,
"cache_ratio": true,
"create_cache_ratio": true,
"image_ratio": true,
"audio_ratio": true,
"audio_completion_ratio": true,
"model_price": true,
}
type upstreamResult struct {
Name string `json:"name"`
@ -67,6 +91,54 @@ type upstreamResult struct {
Err string `json:"err,omitempty"`
}
func valueMap(value any) map[string]any {
switch typed := value.(type) {
case map[string]any:
return typed
case map[string]float64:
return lo.MapValues(typed, func(value float64, _ string) any { return value })
case map[string]string:
return lo.MapValues(typed, func(value string, _ string) any { return value })
default:
return nil
}
}
func asFloat64(value any) (float64, bool) {
switch typed := value.(type) {
case float64:
return typed, true
case float32:
return float64(typed), true
case int:
return float64(typed), true
case int64:
return float64(typed), true
case json.Number:
parsed, err := typed.Float64()
return parsed, err == nil
default:
return 0, false
}
}
func normalizeSyncValue(field string, value any) any {
if numericPricingSyncFields[field] {
if parsed, ok := asFloat64(value); ok {
return parsed
}
}
return value
}
func getLocalPricingSyncData() map[string]any {
data := billing_setting.GetPricingSyncData(map[string]any(ratio_setting.GetExposedData()))
data["image_ratio"] = ratio_setting.GetImageRatioCopy()
data["audio_ratio"] = ratio_setting.GetAudioRatioCopy()
data["audio_completion_ratio"] = ratio_setting.GetAudioCompletionRatioCopy()
return data
}
func FetchUpstreamRatios(c *gin.Context) {
var req dto.UpstreamRequest
if err := c.ShouldBindJSON(&req); err != nil {
@ -293,7 +365,7 @@ func FetchUpstreamRatios(c *gin.Context) {
if err := common.Unmarshal(body.Data, &type1Data); err == nil {
// 如果包含至少一个 ratioTypes 字段,则认为是 type1
isType1 := false
for _, rt := range ratioTypes {
for _, rt := range pricingSyncFields {
if _, ok := type1Data[rt]; ok {
isType1 = true
break
@ -307,11 +379,18 @@ func FetchUpstreamRatios(c *gin.Context) {
// 如果不是 type1则尝试按 type2 (/api/pricing) 解析
var pricingItems []struct {
ModelName string `json:"model_name"`
QuotaType int `json:"quota_type"`
ModelRatio float64 `json:"model_ratio"`
ModelPrice float64 `json:"model_price"`
CompletionRatio float64 `json:"completion_ratio"`
ModelName string `json:"model_name"`
QuotaType int `json:"quota_type"`
ModelRatio float64 `json:"model_ratio"`
ModelPrice float64 `json:"model_price"`
CompletionRatio float64 `json:"completion_ratio"`
CacheRatio *float64 `json:"cache_ratio"`
CreateCacheRatio *float64 `json:"create_cache_ratio"`
ImageRatio *float64 `json:"image_ratio"`
AudioRatio *float64 `json:"audio_ratio"`
AudioCompletionRatio *float64 `json:"audio_completion_ratio"`
BillingMode string `json:"billing_mode"`
BillingExpr string `json:"billing_expr"`
}
if err := common.Unmarshal(body.Data, &pricingItems); err != nil {
logger.LogWarn(c.Request.Context(), "unrecognized data format from "+chItem.Name+": "+err.Error())
@ -321,9 +400,23 @@ func FetchUpstreamRatios(c *gin.Context) {
modelRatioMap := make(map[string]float64)
completionRatioMap := make(map[string]float64)
cacheRatioMap := make(map[string]float64)
createCacheRatioMap := make(map[string]float64)
imageRatioMap := make(map[string]float64)
audioRatioMap := make(map[string]float64)
audioCompletionRatioMap := make(map[string]float64)
modelPriceMap := make(map[string]float64)
billingModeMap := make(map[string]string)
billingExprMap := make(map[string]string)
for _, item := range pricingItems {
if item.ModelName == "" {
continue
}
if item.BillingMode == billing_setting.BillingModeTieredExpr && strings.TrimSpace(item.BillingExpr) != "" {
billingModeMap[item.ModelName] = billing_setting.BillingModeTieredExpr
billingExprMap[item.ModelName] = item.BillingExpr
}
if item.QuotaType == 1 {
modelPriceMap[item.ModelName] = item.ModelPrice
} else {
@ -331,6 +424,21 @@ func FetchUpstreamRatios(c *gin.Context) {
// completionRatio 可能为 0此时也直接赋值保持与上游一致
completionRatioMap[item.ModelName] = item.CompletionRatio
}
if item.CacheRatio != nil {
cacheRatioMap[item.ModelName] = *item.CacheRatio
}
if item.CreateCacheRatio != nil {
createCacheRatioMap[item.ModelName] = *item.CreateCacheRatio
}
if item.ImageRatio != nil {
imageRatioMap[item.ModelName] = *item.ImageRatio
}
if item.AudioRatio != nil {
audioRatioMap[item.ModelName] = *item.AudioRatio
}
if item.AudioCompletionRatio != nil {
audioCompletionRatioMap[item.ModelName] = *item.AudioCompletionRatio
}
}
converted := make(map[string]any)
@ -350,6 +458,21 @@ func FetchUpstreamRatios(c *gin.Context) {
}
converted["completion_ratio"] = compAny
}
if len(cacheRatioMap) > 0 {
converted["cache_ratio"] = valueMap(cacheRatioMap)
}
if len(createCacheRatioMap) > 0 {
converted["create_cache_ratio"] = valueMap(createCacheRatioMap)
}
if len(imageRatioMap) > 0 {
converted["image_ratio"] = valueMap(imageRatioMap)
}
if len(audioRatioMap) > 0 {
converted["audio_ratio"] = valueMap(audioRatioMap)
}
if len(audioCompletionRatioMap) > 0 {
converted["audio_completion_ratio"] = valueMap(audioCompletionRatioMap)
}
if len(modelPriceMap) > 0 {
priceAny := make(map[string]any, len(modelPriceMap))
@ -358,6 +481,12 @@ func FetchUpstreamRatios(c *gin.Context) {
}
converted["model_price"] = priceAny
}
if len(billingModeMap) > 0 {
converted[billing_setting.BillingModeField] = valueMap(billingModeMap)
}
if len(billingExprMap) > 0 {
converted[billing_setting.BillingExprField] = valueMap(billingExprMap)
}
ch <- upstreamResult{Name: uniqueName, Data: converted}
}(chn)
@ -366,7 +495,7 @@ func FetchUpstreamRatios(c *gin.Context) {
wg.Wait()
close(ch)
localData := ratio_setting.GetExposedData()
localData := getLocalPricingSyncData()
var testResults []dto.TestResult
var successfulChannels []struct {
@ -412,22 +541,16 @@ func buildDifferences(localData map[string]any, successfulChannels []struct {
allModels := make(map[string]struct{})
for _, ratioType := range ratioTypes {
if localRatioAny, ok := localData[ratioType]; ok {
if localRatio, ok := localRatioAny.(map[string]float64); ok {
for modelName := range localRatio {
allModels[modelName] = struct{}{}
}
}
for _, field := range pricingSyncFields {
for modelName := range valueMap(localData[field]) {
allModels[modelName] = struct{}{}
}
}
for _, channel := range successfulChannels {
for _, ratioType := range ratioTypes {
if upstreamRatio, ok := channel.data[ratioType].(map[string]any); ok {
for modelName := range upstreamRatio {
allModels[modelName] = struct{}{}
}
for _, field := range pricingSyncFields {
for modelName := range valueMap(channel.data[field]) {
allModels[modelName] = struct{}{}
}
}
}
@ -438,10 +561,10 @@ func buildDifferences(localData map[string]any, successfulChannels []struct {
for _, channel := range successfulChannels {
confidenceMap[channel.name] = make(map[string]bool)
modelRatios, hasModelRatio := channel.data["model_ratio"].(map[string]any)
completionRatios, hasCompletionRatio := channel.data["completion_ratio"].(map[string]any)
modelRatios := valueMap(channel.data["model_ratio"])
completionRatios := valueMap(channel.data["completion_ratio"])
if hasModelRatio && hasCompletionRatio {
if len(modelRatios) > 0 && len(completionRatios) > 0 {
// 遍历所有模型,检查是否满足不可信条件
for modelName := range allModels {
// 默认为可信
@ -451,12 +574,10 @@ func buildDifferences(localData map[string]any, successfulChannels []struct {
if modelRatioVal, ok := modelRatios[modelName]; ok {
if completionRatioVal, ok := completionRatios[modelName]; ok {
// 转换为float64进行比较
if modelRatioFloat, ok := modelRatioVal.(float64); ok {
if completionRatioFloat, ok := completionRatioVal.(float64); ok {
if modelRatioFloat == 37.5 && completionRatioFloat == 1.0 {
confidenceMap[channel.name][modelName] = false
}
}
modelRatioFloat, modelRatioOK := asFloat64(modelRatioVal)
completionRatioFloat, completionRatioOK := asFloat64(completionRatioVal)
if modelRatioOK && completionRatioOK && nearlyEqual(modelRatioFloat, 37.5) && nearlyEqual(completionRatioFloat, 1.0) {
confidenceMap[channel.name][modelName] = false
}
}
}
@ -470,14 +591,10 @@ func buildDifferences(localData map[string]any, successfulChannels []struct {
}
for modelName := range allModels {
for _, ratioType := range ratioTypes {
for _, ratioType := range pricingSyncFields {
var localValue interface{} = nil
if localRatioAny, ok := localData[ratioType]; ok {
if localRatio, ok := localRatioAny.(map[string]float64); ok {
if val, exists := localRatio[modelName]; exists {
localValue = val
}
}
if val, exists := valueMap(localData[ratioType])[modelName]; exists {
localValue = normalizeSyncValue(ratioType, val)
}
upstreamValues := make(map[string]interface{})
@ -488,16 +605,14 @@ func buildDifferences(localData map[string]any, successfulChannels []struct {
for _, channel := range successfulChannels {
var upstreamValue interface{} = nil
if upstreamRatio, ok := channel.data[ratioType].(map[string]any); ok {
if val, exists := upstreamRatio[modelName]; exists {
upstreamValue = val
hasUpstreamValue = true
if val, exists := valueMap(channel.data[ratioType])[modelName]; exists {
upstreamValue = normalizeSyncValue(ratioType, val)
hasUpstreamValue = true
if localValue != nil && !valuesEqual(localValue, val) {
hasDifference = true
} else if valuesEqual(localValue, val) {
upstreamValue = "same"
}
if localValue != nil && !valuesEqual(localValue, upstreamValue) {
hasDifference = true
} else if valuesEqual(localValue, upstreamValue) {
upstreamValue = "same"
}
}
if upstreamValue == nil && localValue == nil {

View File

@ -91,6 +91,7 @@ func Login(c *gin.Context) {
// setup session & cookies and then return user info
func setupLogin(user *model.User, c *gin.Context) {
model.UpdateUserLastLoginAt(user.Id)
session := sessions.Default(c)
session.Set("id", user.Id)
session.Set("username", user.Username)

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/types"
)
@ -346,7 +347,20 @@ type ResponsesOutput struct {
Size string `json:"size"`
CallId string `json:"call_id,omitempty"`
Name string `json:"name,omitempty"`
Arguments string `json:"arguments,omitempty"`
Arguments json.RawMessage `json:"arguments,omitempty"`
}
// ArgumentsString returns function call arguments in the string form expected by Chat Completions.
func (r *ResponsesOutput) ArgumentsString() string {
if r == nil {
return ""
}
return ResponsesArgumentsString(r.Arguments)
}
// ResponsesArgumentsString returns function call arguments in the string form expected by Chat Completions.
func ResponsesArgumentsString(arguments json.RawMessage) string {
return common.JsonRawMessageToString(arguments)
}
type ResponsesOutputContent struct {

View File

@ -304,18 +304,19 @@ const (
// Distributor related messages
const (
MsgDistributorInvalidRequest = "distributor.invalid_request"
MsgDistributorInvalidChannelId = "distributor.invalid_channel_id"
MsgDistributorChannelDisabled = "distributor.channel_disabled"
MsgDistributorTokenNoModelAccess = "distributor.token_no_model_access"
MsgDistributorTokenModelForbidden = "distributor.token_model_forbidden"
MsgDistributorModelNameRequired = "distributor.model_name_required"
MsgDistributorInvalidPlayground = "distributor.invalid_playground_request"
MsgDistributorGroupAccessDenied = "distributor.group_access_denied"
MsgDistributorGetChannelFailed = "distributor.get_channel_failed"
MsgDistributorNoAvailableChannel = "distributor.no_available_channel"
MsgDistributorInvalidMidjourney = "distributor.invalid_midjourney_request"
MsgDistributorInvalidParseModel = "distributor.invalid_request_parse_model"
MsgDistributorInvalidRequest = "distributor.invalid_request"
MsgDistributorInvalidChannelId = "distributor.invalid_channel_id"
MsgDistributorChannelDisabled = "distributor.channel_disabled"
MsgDistributorAffinityChannelDisabled = "distributor.affinity_channel_disabled"
MsgDistributorTokenNoModelAccess = "distributor.token_no_model_access"
MsgDistributorTokenModelForbidden = "distributor.token_model_forbidden"
MsgDistributorModelNameRequired = "distributor.model_name_required"
MsgDistributorInvalidPlayground = "distributor.invalid_playground_request"
MsgDistributorGroupAccessDenied = "distributor.group_access_denied"
MsgDistributorGetChannelFailed = "distributor.get_channel_failed"
MsgDistributorNoAvailableChannel = "distributor.no_available_channel"
MsgDistributorInvalidMidjourney = "distributor.invalid_midjourney_request"
MsgDistributorInvalidParseModel = "distributor.invalid_request_parse_model"
)
// Custom OAuth provider related messages

View File

@ -257,6 +257,7 @@ common.invalid_input: "Invalid input"
distributor.invalid_request: "Invalid request: {{.Error}}"
distributor.invalid_channel_id: "Invalid channel ID"
distributor.channel_disabled: "This channel has been disabled"
distributor.affinity_channel_disabled: "The channel selected by channel affinity has been disabled, and retry was stopped by rule. Please contact the administrator"
distributor.token_no_model_access: "This token has no access to any models"
distributor.token_model_forbidden: "This token has no access to model {{.Model}}"
distributor.model_name_required: "Model name not specified, model name cannot be empty"

View File

@ -258,6 +258,7 @@ common.invalid_input: "输入不合法"
distributor.invalid_request: "无效的请求,{{.Error}}"
distributor.invalid_channel_id: "无效的渠道 Id"
distributor.channel_disabled: "该渠道已被禁用"
distributor.affinity_channel_disabled: "渠道亲和性命中的渠道已被禁用,已按规则停止重试,请联系管理员处理"
distributor.token_no_model_access: "该令牌无权访问任何模型"
distributor.token_model_forbidden: "该令牌无权访问模型 {{.Model}}"
distributor.model_name_required: "未指定模型名称,模型名称不能为空"

View File

@ -258,6 +258,7 @@ common.invalid_input: "輸入不合法"
distributor.invalid_request: "無效的請求,{{.Error}}"
distributor.invalid_channel_id: "無效的管道 Id"
distributor.channel_disabled: "該管道已被禁用"
distributor.affinity_channel_disabled: "管道親和性命中的管道已被禁用,已按規則停止重試,請聯絡管理員處理"
distributor.token_no_model_access: "該令牌無權存取任何模型"
distributor.token_model_forbidden: "該令牌無權存取模型 {{.Model}}"
distributor.model_name_required: "未指定模型名稱,模型名稱不能為空"

View File

@ -104,7 +104,7 @@ func Distribute() func(c *gin.Context) {
if err == nil && preferred != nil {
if preferred.Status != common.ChannelStatusEnabled {
if service.ShouldSkipRetryAfterChannelAffinityFailure(c) {
abortWithOpenAiMessage(c, http.StatusForbidden, i18n.T(c, i18n.MsgDistributorChannelDisabled))
abortWithOpenAiMessage(c, http.StatusForbidden, i18n.T(c, i18n.MsgDistributorAffinityChannelDisabled))
return
}
} else if usingGroup == "auto" {

View File

@ -50,6 +50,8 @@ type User struct {
Setting string `json:"setting" gorm:"type:text;column:setting"`
Remark string `json:"remark,omitempty" gorm:"type:varchar(255)" validate:"max=255"`
StripeCustomer string `json:"stripe_customer" gorm:"type:varchar(64);column:stripe_customer;index"`
CreatedAt int64 `json:"created_at" gorm:"autoCreateTime;column:created_at"`
LastLoginAt int64 `json:"last_login_at" gorm:"default:0;column:last_login_at"`
}
func (user *User) ToBaseUser() *UserBase {
@ -951,6 +953,12 @@ func GetRootUser() (user *User) {
return user
}
func UpdateUserLastLoginAt(id int) {
if err := DB.Model(&User{}).Where("id = ?", id).Update("last_login_at", common.GetTimestamp()).Error; err != nil {
common.SysLog("failed to update user last_login_at: " + err.Error())
}
}
func UpdateUserUsedQuotaAndRequestCount(id int, quota int) {
if common.BatchUpdateEnabled {
addNewRecord(BatchUpdateTypeUsedQuota, id, quota)

View File

@ -1000,11 +1000,82 @@ func TestImageAudioZero(t *testing.T) {
}
}
// ---------------------------------------------------------------------------
// len variable tests — tier conditions based on context length
// ---------------------------------------------------------------------------
const lenTieredExpr = `len <= 200000 ? tier("standard", p * 3 + c * 15 + cr * 0.3) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6)`
func TestLen_StandardTier(t *testing.T) {
params := billingexpr.TokenParams{P: 80000, C: 5000, Len: 100000, CR: 20000}
cost, trace, err := billingexpr.RunExpr(lenTieredExpr, params)
if err != nil {
t.Fatal(err)
}
want := 80000*3 + 5000*15 + 20000*0.3
if math.Abs(cost-want) > 1e-6 {
t.Errorf("cost = %f, want %f", cost, want)
}
if trace.MatchedTier != "standard" {
t.Errorf("tier = %q, want standard", trace.MatchedTier)
}
}
func TestLen_LongContextTier(t *testing.T) {
// p is low (cache subtracted), but len is high (full context)
params := billingexpr.TokenParams{P: 50000, C: 5000, Len: 300000, CR: 250000}
cost, trace, err := billingexpr.RunExpr(lenTieredExpr, params)
if err != nil {
t.Fatal(err)
}
want := 50000*6 + 5000*22.5 + 250000*0.6
if math.Abs(cost-want) > 1e-6 {
t.Errorf("cost = %f, want %f", cost, want)
}
if trace.MatchedTier != "long_context" {
t.Errorf("tier = %q, want long_context (len=300000 > 200000)", trace.MatchedTier)
}
}
func TestLen_BoundaryExact(t *testing.T) {
params := billingexpr.TokenParams{P: 100000, C: 1000, Len: 200000, CR: 100000}
_, trace, err := billingexpr.RunExpr(lenTieredExpr, params)
if err != nil {
t.Fatal(err)
}
if trace.MatchedTier != "standard" {
t.Errorf("tier = %q, want standard (len=200000 <= 200000)", trace.MatchedTier)
}
}
func TestLen_BoundaryPlusOne(t *testing.T) {
params := billingexpr.TokenParams{P: 100000, C: 1000, Len: 200001, CR: 100001}
_, trace, err := billingexpr.RunExpr(lenTieredExpr, params)
if err != nil {
t.Fatal(err)
}
if trace.MatchedTier != "long_context" {
t.Errorf("tier = %q, want long_context (len=200001 > 200000)", trace.MatchedTier)
}
}
func TestLen_ZeroDefaultsToZero(t *testing.T) {
// len defaults to 0 when not set
params := billingexpr.TokenParams{P: 1000, C: 500}
_, trace, err := billingexpr.RunExpr(lenTieredExpr, params)
if err != nil {
t.Fatal(err)
}
if trace.MatchedTier != "standard" {
t.Errorf("tier = %q, want standard (len=0 <= 200000)", trace.MatchedTier)
}
}
// ---------------------------------------------------------------------------
// Benchmarks: compile vs cached execution
// ---------------------------------------------------------------------------
const benchComplexExpr = `p <= 200000 ? tier("standard", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6 + img * 3 + img_o * 30 + ai * 10 + ao * 40) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6 + cc * 7.5 + cc1h * 12 + img * 6 + img_o * 60 + ai * 20 + ao * 80)`
const benchComplexExpr = `len <= 200000 ? tier("standard", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6 + img * 3 + img_o * 30 + ai * 10 + ao * 40) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6 + cc * 7.5 + cc1h * 12 + img * 6 + img_o * 60 + ai * 20 + ao * 80)`
func BenchmarkExprCompile(b *testing.B) {
for i := 0; i < b.N; i++ {
@ -1015,7 +1086,7 @@ func BenchmarkExprCompile(b *testing.B) {
func BenchmarkExprRunCached(b *testing.B) {
billingexpr.CompileFromCache(benchComplexExpr)
params := billingexpr.TokenParams{P: 150000, C: 10000, CR: 30000, CC: 5000, Img: 2000, AI: 1000, AO: 500}
params := billingexpr.TokenParams{P: 150000, C: 10000, Len: 188000, CR: 30000, CC: 5000, Img: 2000, AI: 1000, AO: 500}
b.ResetTimer()
for i := 0; i < b.N; i++ {
billingexpr.RunExpr(benchComplexExpr, params)

View File

@ -41,6 +41,7 @@ var (
var compileEnvPrototypeV1 = map[string]interface{}{
"p": float64(0),
"c": float64(0),
"len": float64(0),
"cr": float64(0),
"cc": float64(0),
"cc1h": float64(0),

View File

@ -30,7 +30,8 @@ Powered by [expr-lang/expr](https://github.com/expr-lang/expr). Expressions are
| 变量 | 含义 |
|------|------|
| `p` | 输入 token 数。**自动排除**表达式中单独计价的子类别(见下方说明) |
| `p` | 输入 token 数(**计价用**)。**自动排除**表达式中单独计价的子类别(见下方说明) |
| `len` | 输入上下文总长度(**条件判断用**)。不受自动排除影响,始终反映完整输入长度。非 Claude等于原始 `prompt_tokens`Claude等于文本输入 + 缓存读取 + 缓存创建 |
| `cr` | 缓存命中读取token 数 |
| `cc` | 缓存创建 token 数Claude 5分钟 TTL / 通用) |
| `cc1h` | 缓存创建 token 数 — 1小时 TTLClaude 专用) |
@ -51,6 +52,8 @@ Powered by [expr-lang/expr](https://github.com/expr-lang/expr). Expressions are
**规则:如果表达式使用了某个子类别变量,对应的 token 就从 `p``c` 中扣除;如果没使用,那些 token 就留在 `p``c` 里按基础价格计费。**
> **重要:`len` 不受自动排除影响。** `len` 始终代表完整的输入上下文长度,不管表达式是否单独对缓存/图片/音频定价。因此**阶梯条件应使用 `len` 而非 `p`**,以避免缓存命中导致 `p` 降低而误判档位。
举例说明假设上游返回的原始数据prompt_tokens=1000其中包含 200 cache read、100 image
| 表达式 | `p` 的值 | 说明 |
@ -93,8 +96,8 @@ Powered by [expr-lang/expr](https://github.com/expr-lang/expr). Expressions are
# Simple flat pricing
tier("base", p * 2.5 + c * 15 + cr * 0.25)
# Multi-tier (Claude Sonnet style)
p <= 200000
# Multi-tier (Claude Sonnet style) — use len for tier conditions
len <= 200000
? tier("standard", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6)
: tier("long_context", p * 6 + c * 22.5 + cr * 0.6 + cc * 7.5 + cc1h * 12)
@ -199,6 +202,16 @@ Example: `p * 2.5 + c * 15 + cr * 0.25`
- Expression uses `cr` → cache read tokens subtracted from `p`
- Expression doesn't use `img` → image tokens stay in `p`, priced at $2.50
### `len` — Context Length Variable
`len` represents the total input context length, designed for **tier condition evaluation** (e.g. `len <= 200000 ? ...`). Unlike `p`, `len` is never reduced by sub-category exclusion.
**Computation rules:**
- **Non-Claude (GPT/OpenAI format)**: `len = prompt_tokens` (the raw total from the upstream response)
- **Claude format**: `len = input_tokens + cache_read_tokens + cache_creation_tokens` (since Claude's `input_tokens` is text-only, cache must be added back to reflect full context length)
This ensures that heavy cache usage doesn't cause the tier condition to incorrectly evaluate to a lower tier. For example, if a request has 300K total context but 250K is cached, `p` with cache subtracted would be only 50K (standard tier), while `len` correctly reports 300K (long-context tier).
### Quota Conversion
Expression coefficients are $/1M tokens. Conversion to internal quota:

View File

@ -13,7 +13,8 @@ import (
// RunExpr compiles (with cache) and executes an expression string.
// The environment exposes:
// - p, c — prompt / completion tokens
// - p, c — prompt / completion tokens (auto-excluding separately-priced sub-categories)
// - len — total input context length for tier conditions (never reduced by sub-category exclusion)
// - cr, cc, cc1h — cache read / creation / creation-1h tokens
// - tier(name, value) — trace callback that records which tier matched
// - max, min, abs, ceil, floor — standard math helpers
@ -54,6 +55,7 @@ func runProgram(prog *vm.Program, params TokenParams, request RequestInput) (flo
env := map[string]interface{}{
"p": params.P,
"c": params.C,
"len": params.Len,
"cr": params.CR,
"cc": params.CC,
"cc1h": params.CC1h,

View File

@ -14,8 +14,9 @@ type RequestInput struct {
// Fields beyond P and C are optional — when absent they default to 0,
// which means cache-unaware expressions keep working unchanged.
type TokenParams struct {
P float64 // prompt tokens (text)
C float64 // completion tokens (text)
P float64 // prompt tokens (text) — auto-excludes sub-categories priced separately
C float64 // completion tokens (text) — auto-excludes sub-categories priced separately
Len float64 // total input context length for tier conditions (non-Claude: raw prompt_tokens; Claude: text + cache read + cache creation)
CR float64 // cache read (hit) tokens
CC float64 // cache creation tokens (5-min TTL for Claude, generic for others)
CC1h float64 // cache creation tokens — 1-hour TTL (Claude only)

View File

@ -7,6 +7,7 @@ import (
"net/http"
"strings"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/dto"
"github.com/QuantumNous/new-api/relay/channel"
"github.com/QuantumNous/new-api/relay/channel/claude"
@ -18,12 +19,16 @@ import (
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/samber/lo"
)
type Adaptor struct {
IsSyncImageModel bool
}
const aliAnthropicMessagesModelsEnv = "ALI_ANTHROPIC_MESSAGES_MODELS"
const defaultAliAnthropicMessagesModels = "qwen,deepseek-v4,kimi,glm,minimax-m"
/*
var syncModels = []string{
"z-image",
@ -32,8 +37,22 @@ type Adaptor struct {
}
*/
func supportsAliAnthropicMessages(modelName string) bool {
// Only models with the "qwen" designation can use the Claude-compatible interface; others require conversion.
return strings.Contains(strings.ToLower(modelName), "qwen")
normalizedModelName := strings.ToLower(strings.TrimSpace(modelName))
if normalizedModelName == "" {
return false
}
return lo.SomeBy(aliAnthropicMessagesModelPatterns(), func(pattern string) bool {
return strings.Contains(normalizedModelName, pattern)
})
}
func aliAnthropicMessagesModelPatterns() []string {
configuredModels := common.GetEnvOrDefaultString(aliAnthropicMessagesModelsEnv, defaultAliAnthropicMessagesModels)
return lo.FilterMap(strings.Split(configuredModels, ","), func(item string, _ int) (string, bool) {
pattern := strings.ToLower(strings.TrimSpace(item))
return pattern, pattern != ""
})
}
var syncModels = []string{

View File

@ -408,7 +408,7 @@ func OaiResponsesToChatStreamHandler(c *gin.Context, info *relaycommon.RelayInfo
toolCallNameByID[callID] = name
}
newArgs := streamResp.Item.Arguments
newArgs := streamResp.Item.ArgumentsString()
prevArgs := toolCallArgsByID[callID]
argsDelta := ""
if newArgs != "" {

View File

@ -255,8 +255,9 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
}
rawCost, trace, err := billingexpr.RunExprWithRequest(exprStr, billingexpr.TokenParams{
P: float64(promptTokens),
C: float64(estimatedCompletionTokens),
P: float64(promptTokens),
C: float64(estimatedCompletionTokens),
Len: float64(promptTokens),
}, requestInput)
if err != nil {
return types.PriceData{}, fmt.Errorf("model %s tiered expr run failed: %w", info.OriginModelName, err)

View File

@ -60,7 +60,7 @@ func ResponsesResponseToChatCompletionsResponse(resp *dto.OpenAIResponsesRespons
Type: "function",
Function: dto.FunctionResponse{
Name: name,
Arguments: out.Arguments,
Arguments: out.ArgumentsString(),
},
})
}

View File

@ -160,8 +160,9 @@ func PostWssConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, mod
var tieredResult *billingexpr.TieredResult
tieredOk, tieredQuota, tieredRes := TryTieredSettle(relayInfo, billingexpr.TokenParams{
P: float64(usage.InputTokens),
C: float64(usage.OutputTokens),
P: float64(usage.InputTokens),
C: float64(usage.OutputTokens),
Len: float64(usage.InputTokens),
})
if tieredOk {
tieredResult = tieredRes

View File

@ -35,6 +35,14 @@ func BuildTieredTokenParams(usage *dto.Usage, isClaudeUsageSemantic bool, usedVa
imgO := float64(usage.CompletionTokenDetails.ImageTokens)
ao := float64(usage.CompletionTokenDetails.AudioTokens)
// len = total input context length for tier condition evaluation.
// Non-Claude: prompt_tokens already includes everything.
// Claude: input_tokens is text-only, so add cache read + cache creation.
inputLen := p
if isClaudeUsageSemantic {
inputLen = p + cr + cc5m + cc1h
}
if !isClaudeUsageSemantic {
if usedVars["cr"] {
p -= cr
@ -69,6 +77,7 @@ func BuildTieredTokenParams(usage *dto.Usage, isClaudeUsageSemantic bool, usedVa
return billingexpr.TokenParams{
P: p,
C: c,
Len: inputLen,
CR: cr,
CC: cc5m,
CC1h: cc1h,

View File

@ -604,6 +604,97 @@ func TestBuildTieredTokenParams_ParityWithRatio_Image(t *testing.T) {
}
}
// ---------------------------------------------------------------------------
// BuildTieredTokenParams: Len computation tests
// ---------------------------------------------------------------------------
func TestBuildTieredTokenParams_Len_GPT(t *testing.T) {
usage := &dto.Usage{
PromptTokens: 10000,
CompletionTokens: 2000,
PromptTokensDetails: dto.InputTokenDetails{
CachedTokens: 3000,
TextTokens: 7000,
},
}
expr := `tier("base", p * 2.5 + c * 15 + cr * 0.25)`
usedVars := billingexpr.UsedVars(expr)
params := BuildTieredTokenParams(usage, false, usedVars)
// Non-Claude: Len = raw PromptTokens
if params.Len != 10000 {
t.Fatalf("Len = %f, want 10000 (raw PromptTokens)", params.Len)
}
// P should be reduced by cache
if params.P != 7000 {
t.Fatalf("P = %f, want 7000 (PromptTokens - CachedTokens)", params.P)
}
}
func TestBuildTieredTokenParams_Len_Claude(t *testing.T) {
usage := &dto.Usage{
PromptTokens: 5000,
CompletionTokens: 2000,
UsageSemantic: "anthropic",
PromptTokensDetails: dto.InputTokenDetails{
CachedTokens: 3000,
TextTokens: 5000,
},
ClaudeCacheCreation5mTokens: 1000,
ClaudeCacheCreation1hTokens: 500,
}
expr := `tier("base", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6)`
usedVars := billingexpr.UsedVars(expr)
params := BuildTieredTokenParams(usage, true, usedVars)
// Claude: Len = PromptTokens + CachedTokens + CacheCreation5m + CacheCreation1h
wantLen := float64(5000 + 3000 + 1000 + 500)
if params.Len != wantLen {
t.Fatalf("Len = %f, want %f (text + cache read + cache creation)", params.Len, wantLen)
}
// Claude: P is not reduced (isClaudeUsageSemantic = true)
if params.P != 5000 {
t.Fatalf("P = %f, want 5000 (no subtraction for Claude)", params.P)
}
}
func TestBuildTieredTokenParams_Len_TierCondition(t *testing.T) {
// Test that len-based tier conditions work correctly when p is reduced by cache
usage := &dto.Usage{
PromptTokens: 300000,
CompletionTokens: 5000,
PromptTokensDetails: dto.InputTokenDetails{
CachedTokens: 250000,
TextTokens: 50000,
},
}
expr := `len <= 200000 ? tier("standard", p * 3 + c * 15 + cr * 0.3) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6)`
usedVars := billingexpr.UsedVars(expr)
params := BuildTieredTokenParams(usage, false, usedVars)
// Len = 300000 (raw prompt), P = 50000 (300000 - 250000 cache)
if params.Len != 300000 {
t.Fatalf("Len = %f, want 300000", params.Len)
}
if params.P != 50000 {
t.Fatalf("P = %f, want 50000", params.P)
}
// Run expression: len=300000 > 200000, so long_context tier
cost, trace, err := billingexpr.RunExpr(expr, params)
if err != nil {
t.Fatal(err)
}
if trace.MatchedTier != "long_context" {
t.Fatalf("tier = %s, want long_context (len=300000 but p=50000)", trace.MatchedTier)
}
// long_context: 50000*6 + 5000*22.5 + 250000*0.6
wantCost := 50000.0*6 + 5000*22.5 + 250000*0.6
if math.Abs(cost-wantCost) > 1e-6 {
t.Fatalf("cost = %f, want %f", cost, wantCost)
}
}
// ---------------------------------------------------------------------------
// Stress test: 1000 concurrent goroutines, complex tiered expr vs ratio,
// random token counts, verify correctness and measure performance

View File

@ -5,11 +5,14 @@ import (
"github.com/QuantumNous/new-api/pkg/billingexpr"
"github.com/QuantumNous/new-api/setting/config"
"github.com/samber/lo"
)
const (
BillingModeRatio = "ratio"
BillingModeTieredExpr = "tiered_expr"
BillingModeField = "billing_mode"
BillingExprField = "billing_expr"
)
// BillingSetting is managed by config.GlobalConfig.Register.
@ -44,6 +47,25 @@ func GetBillingExpr(model string) (string, bool) {
return expr, ok
}
func GetBillingModeCopy() map[string]string {
return lo.Assign(billingSetting.BillingMode)
}
func GetBillingExprCopy() map[string]string {
return lo.Assign(billingSetting.BillingExpr)
}
func GetPricingSyncData(base map[string]any) map[string]any {
extra := make(map[string]any, 2)
if modes := GetBillingModeCopy(); len(modes) > 0 {
extra[BillingModeField] = modes
}
if exprs := GetBillingExprCopy(); len(exprs) > 0 {
extra[BillingExprField] = exprs
}
return lo.Assign(base, extra)
}
// ---------------------------------------------------------------------------
// Smoke test (called externally for validation before save)
// ---------------------------------------------------------------------------
@ -54,10 +76,10 @@ func SmokeTestExpr(exprStr string) error {
func smokeTestExpr(exprStr string) error {
vectors := []billingexpr.TokenParams{
{P: 0, C: 0},
{P: 1000, C: 1000},
{P: 100000, C: 100000},
{P: 1000000, C: 1000000},
{P: 0, C: 0, Len: 0},
{P: 1000, C: 1000, Len: 1000},
{P: 100000, C: 100000, Len: 100000},
{P: 1000000, C: 1000000, Len: 1000000},
}
requests := []billingexpr.RequestInput{
{},

View File

@ -252,8 +252,16 @@ func updateConfigFromMap(config interface{}, configMap map[string]string) error
continue
}
}
case reflect.Map, reflect.Slice, reflect.Struct:
// 复杂类型使用JSON反序列化
case reflect.Map:
// json.Unmarshal merges into existing maps (keeps old keys that are
// absent from the new JSON). Allocate a fresh map so removed keys
// are properly cleared.
fresh := reflect.New(field.Type())
if err := json.Unmarshal([]byte(strValue), fresh.Interface()); err != nil {
continue
}
field.Set(fresh.Elem())
case reflect.Slice, reflect.Struct:
err := json.Unmarshal([]byte(strValue), field.Addr().Interface())
if err != nil {
continue

View File

@ -0,0 +1,96 @@
package config
import (
"testing"
)
type testConfigWithMap struct {
Modes map[string]string `json:"modes"`
Exprs map[string]string `json:"exprs"`
Name string `json:"name"`
}
func TestUpdateConfigFromMap_MapReplacement(t *testing.T) {
cfg := &testConfigWithMap{
Modes: map[string]string{
"model-a": "tiered_expr",
"model-b": "tiered_expr",
},
Exprs: map[string]string{
"model-a": "p * 5 + c * 25",
"model-b": "p * 10 + c * 50",
},
Name: "billing",
}
// Simulate removing model-a: new value only has model-b
err := UpdateConfigFromMap(cfg, map[string]string{
"modes": `{"model-b": "tiered_expr"}`,
"exprs": `{"model-b": "p * 10 + c * 50"}`,
})
if err != nil {
t.Fatalf("UpdateConfigFromMap failed: %v", err)
}
if _, ok := cfg.Modes["model-a"]; ok {
t.Errorf("Modes still contains model-a after it was removed from the update; got %v", cfg.Modes)
}
if _, ok := cfg.Exprs["model-a"]; ok {
t.Errorf("Exprs still contains model-a after it was removed from the update; got %v", cfg.Exprs)
}
if cfg.Modes["model-b"] != "tiered_expr" {
t.Errorf("Modes[model-b] = %q, want %q", cfg.Modes["model-b"], "tiered_expr")
}
if cfg.Exprs["model-b"] != "p * 10 + c * 50" {
t.Errorf("Exprs[model-b] = %q, want %q", cfg.Exprs["model-b"], "p * 10 + c * 50")
}
}
func TestUpdateConfigFromMap_EmptyMapClearsAll(t *testing.T) {
cfg := &testConfigWithMap{
Modes: map[string]string{
"model-a": "tiered_expr",
},
Exprs: map[string]string{
"model-a": "p * 5 + c * 25",
},
}
err := UpdateConfigFromMap(cfg, map[string]string{
"modes": `{}`,
"exprs": `{}`,
})
if err != nil {
t.Fatalf("UpdateConfigFromMap failed: %v", err)
}
if len(cfg.Modes) != 0 {
t.Errorf("Modes should be empty after updating with {}, got %v", cfg.Modes)
}
if len(cfg.Exprs) != 0 {
t.Errorf("Exprs should be empty after updating with {}, got %v", cfg.Exprs)
}
}
func TestUpdateConfigFromMap_ScalarFieldsUnchanged(t *testing.T) {
cfg := &testConfigWithMap{
Modes: map[string]string{"m": "v"},
Name: "old",
}
err := UpdateConfigFromMap(cfg, map[string]string{
"name": "new",
})
if err != nil {
t.Fatalf("UpdateConfigFromMap failed: %v", err)
}
if cfg.Name != "new" {
t.Errorf("Name = %q, want %q", cfg.Name, "new")
}
// modes was not in configMap, should remain unchanged
if cfg.Modes["m"] != "v" {
t.Errorf("Modes should be unchanged, got %v", cfg.Modes)
}
}

View File

@ -709,6 +709,18 @@ func GetCompletionRatioCopy() map[string]float64 {
return completionRatioMap.ReadAll()
}
func GetImageRatioCopy() map[string]float64 {
return imageRatioMap.ReadAll()
}
func GetAudioRatioCopy() map[string]float64 {
return audioRatioMap.ReadAll()
}
func GetAudioCompletionRatioCopy() map[string]float64 {
return audioCompletionRatioMap.ReadAll()
}
// 转换模型名,减少渠道必须配置各种带参数模型
func FormatMatchingModelName(name string) string {

View File

@ -155,8 +155,8 @@ const ChannelSelectorModal = forwardRef(
onChange={handleTypeChange}
style={{ width: 120 }}
optionList={[
{ label: 'ratio_config', value: 'ratio_config' },
{ label: 'pricing', value: 'pricing' },
{ label: 'ratio_config', value: 'ratio_config' },
{ label: 'OpenRouter', value: 'openrouter' },
{ label: 'custom', value: 'custom' },
]}

View File

@ -106,7 +106,7 @@ const RatioSetting = () => {
<Tabs.TabPane tab={t('未设置价格模型')} itemKey='unset_models'>
<ModelRatioNotSetEditor options={inputs} refresh={onRefresh} />
</Tabs.TabPane>
<Tabs.TabPane tab={t('上游倍率同步')} itemKey='upstream_sync'>
<Tabs.TabPane tab={t('上游价格同步')} itemKey='upstream_sync'>
<UpstreamRatioSync options={inputs} refresh={onRefresh} />
</Tabs.TabPane>
<Tabs.TabPane tab={t('工具调用定价')} itemKey='tool_price'>

View File

@ -269,6 +269,24 @@ const EditChannelModal = (props) => {
return [];
}
}, [inputs.model_mapping]);
const redirectModelKeyList = useMemo(() => {
const mapping = inputs.model_mapping;
if (typeof mapping !== 'string') return [];
const trimmed = mapping.trim();
if (!trimmed) return [];
try {
const parsed = JSON.parse(trimmed);
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
return [];
}
const keys = Object.keys(parsed)
.map((key) => key.trim())
.filter((key) => key);
return Array.from(new Set(keys));
} catch (error) {
return [];
}
}, [inputs.model_mapping]);
const upstreamDetectedModels = useMemo(
() =>
Array.from(
@ -3842,6 +3860,7 @@ const EditChannelModal = (props) => {
models={fetchedModels}
selected={inputs.models}
redirectModels={redirectModelList}
redirectSourceModels={redirectModelKeyList}
onConfirm={(selectedModels) => {
handleInputChange('models', selectedModels);
showSuccess(t('模型列表已更新'));

View File

@ -43,6 +43,7 @@ const ModelSelectModal = ({
models = [],
selected = [],
redirectModels = [],
redirectSourceModels = [],
onConfirm,
onCancel,
}) => {
@ -54,6 +55,14 @@ const ModelSelectModal = ({
if (typeof model === 'object' && model.model_name) return model.model_name;
return String(model ?? '');
};
const normalizeModelList = (modelList = []) =>
Array.from(
new Set(
(modelList || [])
.map((model) => getModelName(model).trim())
.filter(Boolean),
),
);
const normalizedSelected = useMemo(
() => (selected || []).map(getModelName),
@ -78,6 +87,10 @@ const ModelSelectModal = ({
),
[redirectModels],
);
const normalizedRedirectSourceSet = useMemo(
() => new Set(normalizeModelList(redirectSourceModels)),
[redirectSourceModels],
);
const normalizedSelectedSet = useMemo(() => {
const set = new Set();
(selected || []).forEach((model) => {
@ -116,6 +129,16 @@ const ModelSelectModal = ({
const existingModels = filteredModels.filter((model) =>
isExistingModel(model),
);
const fetchedModelSet = useMemo(
() => new Set(normalizeModelList(models)),
[models],
);
const removedModels = normalizeModelList(selected).filter(
(model) =>
!fetchedModelSet.has(model) &&
!normalizedRedirectSourceSet.has(model) &&
model.toLowerCase().includes(keyword.toLowerCase()),
);
//
useEffect(() => {
@ -127,11 +150,15 @@ const ModelSelectModal = ({
// tab
useEffect(() => {
if (visible) {
// tab
const hasNewModels = newModels.length > 0;
setActiveTab(hasNewModels ? 'new' : 'existing');
if (newModels.length > 0) {
setActiveTab('new');
} else if (removedModels.length > 0) {
setActiveTab('removed');
} else {
setActiveTab('existing');
}
}
}, [visible, newModels.length, selected]);
}, [visible, newModels.length, removedModels.length, selected]);
const handleOk = () => {
onConfirm && onConfirm(checkedList);
@ -197,6 +224,14 @@ const ModelSelectModal = ({
},
]
: []),
...(removedModels.length > 0
? [
{
tab: `${t('上游已删除的模型')} (${removedModels.length})`,
itemKey: 'removed',
},
]
: []),
];
// /
@ -343,9 +378,11 @@ const ModelSelectModal = ({
showClear
/>
<Spin spinning={!models || models.length === 0}>
<Spin
spinning={!models || (models.length === 0 && removedModels.length === 0)}
>
<div style={{ maxHeight: 400, overflowY: 'auto', paddingRight: 8 }}>
{filteredModels.length === 0 ? (
{filteredModels.length === 0 && removedModels.length === 0 ? (
<Empty
image={
<IllustrationNoResult style={{ width: 150, height: 150 }} />
@ -369,6 +406,14 @@ const ModelSelectModal = ({
{renderModelsByCategory(existingModelsByCategory, 'existing')}
</div>
)}
{activeTab === 'removed' && removedModels.length > 0 && (
<div>
{renderModelsByCategory(
categorizeModels(removedModels),
'removed',
)}
</div>
)}
</Checkbox.Group>
)}
</div>
@ -382,7 +427,11 @@ const ModelSelectModal = ({
<div className='flex items-center justify-end gap-2'>
{(() => {
const currentModels =
activeTab === 'new' ? newModels : existingModels;
activeTab === 'new'
? newModels
: activeTab === 'removed'
? removedModels
: existingModels;
const currentSelected = currentModels.filter((model) =>
checkedList.includes(model),
).length;

View File

@ -21,7 +21,7 @@ import React from 'react';
import { Avatar, Tag, Table, Typography } from '@douyinfe/semi-ui';
import { IconPriceTag } from '@douyinfe/semi-icons';
import { parseTiersFromExpr, getCurrencyConfig } from '../../../../../helpers';
import { BILLING_VARS } from '../../../../../constants';
import { BILLING_PRICING_VARS } from '../../../../../constants';
import {
splitBillingExprAndRequestRules,
tryParseRequestRuleExpr,
@ -113,7 +113,7 @@ export default function DynamicPricingBreakdown({ billingExpr, t }) {
);
}
const priceFields = BILLING_VARS.map((v) => [v.field, v.shortLabel]);
const priceFields = BILLING_PRICING_VARS.map((v) => [v.field, v.shortLabel]);
const tierColumns = [
{

View File

@ -29,7 +29,14 @@ import {
Dropdown,
} from '@douyinfe/semi-ui';
import { IconMore } from '@douyinfe/semi-icons';
import { renderGroup, renderNumber, renderQuota } from '../../../helpers';
import {
renderGroup,
renderNumber,
renderQuota,
timestamp2string,
} from '../../../helpers';
const renderTimestamp = (text) => (text ? timestamp2string(text) : '-');
/**
* Render user role
@ -350,6 +357,16 @@ export const getUsersColumns = ({
dataIndex: 'invite',
render: (text, record, index) => renderInviteInfo(text, record, t),
},
{
title: t('创建时间'),
dataIndex: 'created_at',
render: renderTimestamp,
},
{
title: t('最后登录'),
dataIndex: 'last_login_at',
render: renderTimestamp,
},
{
title: '',
dataIndex: 'operate',

View File

@ -13,6 +13,7 @@
export const BILLING_VARS = [
{ key: 'p', field: 'inputPrice', tierField: 'input_unit_cost', label: '输入价格', shortLabel: '输入', side: 'input', isBase: true },
{ key: 'c', field: 'outputPrice', tierField: 'output_unit_cost', label: '补全价格', shortLabel: '补全', side: 'output', isBase: true },
{ key: 'len', field: null, tierField: null, label: '输入长度', shortLabel: '长度', side: 'condition', isConditionOnly: true },
{ key: 'cr', field: 'cacheReadPrice', tierField: 'cache_read_unit_cost', label: '缓存读取价格', shortLabel: '缓存读', side: 'input', group: 'cache' },
{ key: 'cc', field: 'cacheCreatePrice', tierField: 'cache_create_unit_cost', label: '缓存创建价格', shortLabel: '缓存创建', side: 'input', group: 'cache' },
{ key: 'cc1h', field: 'cacheCreate1hPrice', tierField: 'cache_create_1h_unit_cost', label: '1h缓存创建价格', shortLabel: '1h缓存创建', side: 'input', group: 'cache' },
@ -24,18 +25,20 @@ export const BILLING_VARS = [
export const BILLING_VAR_KEYS = BILLING_VARS.map((v) => v.key);
export const BILLING_EXTRA_VARS = BILLING_VARS.filter((v) => !v.isBase);
export const BILLING_PRICING_VARS = BILLING_VARS.filter((v) => !v.isConditionOnly);
export const BILLING_EXTRA_VARS = BILLING_VARS.filter((v) => !v.isBase && !v.isConditionOnly);
export const BILLING_VAR_KEY_TO_FIELD = Object.fromEntries(
BILLING_VARS.map((v) => [v.key, v.field]),
BILLING_PRICING_VARS.map((v) => [v.key, v.field]),
);
export const BILLING_VAR_FIELD_TO_LABEL = Object.fromEntries(
BILLING_VARS.map((v) => [v.field, v.label]),
BILLING_PRICING_VARS.map((v) => [v.field, v.label]),
);
export const BILLING_VAR_FIELD_TO_SHORT_LABEL = Object.fromEntries(
BILLING_VARS.map((v) => [v.field, v.shortLabel]),
BILLING_PRICING_VARS.map((v) => [v.field, v.shortLabel]),
);
export const BILLING_CACHE_VAR_MAP = BILLING_EXTRA_VARS.map((v) => ({
@ -44,6 +47,10 @@ export const BILLING_CACHE_VAR_MAP = BILLING_EXTRA_VARS.map((v) => ({
}));
export const BILLING_VAR_REGEX = new RegExp(
`\\b(${BILLING_VAR_KEYS.join('|')})\\s*\\*\\s*([\\d.eE+-]+)`,
`\\b(${BILLING_PRICING_VARS.map((v) => v.key).join('|')})\\s*\\*\\s*([\\d.eE+-]+)`,
'g',
);
export const BILLING_CONDITION_VARS = BILLING_VARS.filter(
(v) => v.isBase || v.isConditionOnly,
).map((v) => v.key);

View File

@ -19,7 +19,7 @@ For commercial licensing, please contact support@quantumnous.com
export const ITEMS_PER_PAGE = 10; // this value must keep same as the one defined in backend!
export const DEFAULT_ENDPOINT = '/api/ratio_config';
export const DEFAULT_ENDPOINT = '/api/pricing';
export const TABLE_COMPACT_MODES_KEY = 'table_compact_modes';

View File

@ -22,7 +22,7 @@ import { Modal, Tag, Typography, Avatar } from '@douyinfe/semi-ui';
import { copy, showSuccess } from './utils';
import { MOBILE_BREAKPOINT } from '../hooks/common/useIsMobile';
import {
BILLING_VARS,
BILLING_PRICING_VARS,
BILLING_VAR_KEY_TO_FIELD,
BILLING_VAR_REGEX,
} from '../constants';
@ -2246,7 +2246,7 @@ export function parseTiersFromExpr(exprStr) {
if (!exprStr) return [];
try {
const { body } = stripExprVersion(exprStr);
const condGroup = `((?:(?:p|c)\\s*(?:<|<=|>|>=)\\s*[\\d.eE+]+)(?:\\s*&&\\s*(?:p|c)\\s*(?:<|<=|>|>=)\\s*[\\d.eE+]+)*)`;
const condGroup = `((?:(?:p|c|len)\\s*(?:<|<=|>|>=)\\s*[\\d.eE+]+)(?:\\s*&&\\s*(?:p|c|len)\\s*(?:<|<=|>|>=)\\s*[\\d.eE+]+)*)`;
const tierRe = new RegExp(`(?:${condGroup}\\s*\\?\\s*)?tier\\("([^"]*)",\\s*([^)]+)\\)`, 'g');
const tiers = [];
let m;
@ -2255,7 +2255,7 @@ export function parseTiersFromExpr(exprStr) {
const conditions = [];
if (condStr) {
for (const cp of condStr.split(/\s*&&\s*/)) {
const cm = cp.trim().match(/^(p|c)\s*(<|<=|>|>=)\s*([\d.eE+]+)$/);
const cm = cp.trim().match(/^(p|c|len)\s*(<|<=|>|>=)\s*([\d.eE+]+)$/);
if (cm) conditions.push({ var: cm[1], op: cm[2], value: Number(cm[3]) });
}
}
@ -2293,7 +2293,7 @@ export function renderTieredModelPrice(opts) {
const { symbol, rate } = getCurrencyConfig();
const gr = groupRatio || 1;
const priceLines = BILLING_VARS.map((v) => [v.field, v.label]);
const priceLines = BILLING_PRICING_VARS.map((v) => [v.field, v.label]);
const lines = [
buildBillingText('命中档位:{{tier}}', { tier: matchedTier || tier.label }),
@ -2334,7 +2334,7 @@ export function renderTieredModelPriceSimple(opts) {
];
if (tier && isPriceDisplayMode(displayMode)) {
const priceSegments = BILLING_VARS.map((v) => [v.field, v.shortLabel]);
const priceSegments = BILLING_PRICING_VARS.map((v) => [v.field, v.shortLabel]);
for (const [field, label] of priceSegments) {
if (tier[field] > 0) {
segments.push({

View File

@ -18,7 +18,7 @@ For commercial licensing, please contact support@quantumnous.com
*/
import { Toast, Pagination } from '@douyinfe/semi-ui';
import { toastConstants, BILLING_VARS, BILLING_VAR_REGEX } from '../constants';
import { toastConstants, BILLING_PRICING_VARS, BILLING_VAR_REGEX } from '../constants';
import React from 'react';
import { toast } from 'react-toastify';
import {
@ -927,7 +927,7 @@ export const formatDynamicPriceSummary = (billingExpr, t, groupRatio = 1) => {
}
const hasCoeffs = 'p' in varCoeffs || 'c' in varCoeffs;
const varLabels = BILLING_VARS.map((v) => [v.key, v.label]);
const varLabels = BILLING_PRICING_VARS.map((v) => [v.key, v.label]);
const hasTimeCondition = /\b(?:hour|minute|weekday|month|day)\(/.test(exprBody);
const hasRequestCondition = /\b(?:param|header)\(/.test(exprBody);

View File

@ -29,17 +29,14 @@ import {
Tooltip,
Select,
Modal,
Spin,
} from '@douyinfe/semi-ui';
import { IconSearch } from '@douyinfe/semi-icons';
import {
RefreshCcw,
CheckSquare,
AlertTriangle,
CheckCircle,
} from 'lucide-react';
import { RefreshCcw, CheckSquare, AlertTriangle } from 'lucide-react';
import {
API,
showError,
showInfo,
showSuccess,
showWarning,
stringToColor,
@ -63,7 +60,7 @@ const MODELS_DEV_PRESET_NAME = 'models.dev 价格预设';
const MODELS_DEV_PRESET_BASE_URL = 'https://models.dev';
const MODELS_DEV_PRESET_ENDPOINT = 'https://models.dev/api.json';
function ConflictConfirmModal({ t, visible, items, onOk, onCancel }) {
function ConflictConfirmModal({ t, visible, items, loading, onOk, onCancel }) {
const isMobile = useIsMobile();
const columns = [
{ title: t('渠道'), dataIndex: 'channel' },
@ -84,7 +81,10 @@ function ConflictConfirmModal({ t, visible, items, onOk, onCancel }) {
<Modal
title={t('确认冲突项修改')}
visible={visible}
onCancel={onCancel}
confirmLoading={loading}
cancelButtonProps={{ disabled: loading }}
maskClosable={!loading}
onCancel={loading ? undefined : onCancel}
onOk={onOk}
size={isMobile ? 'full-width' : 'large'}
>
@ -103,6 +103,7 @@ export default function UpstreamRatioSync(props) {
const [modalVisible, setModalVisible] = useState(false);
const [loading, setLoading] = useState(false);
const [syncLoading, setSyncLoading] = useState(false);
const [confirmLoading, setConfirmLoading] = useState(false);
const isMobile = useIsMobile();
//
@ -251,7 +252,7 @@ export default function UpstreamRatioSync(props) {
setHasSynced(true);
if (Object.keys(differences).length === 0) {
showSuccess(t('未找到差异化倍率,无需同步'));
showSuccess(t('未找到差异化价格,无需同步'));
}
} catch (e) {
showError(t('请求后端接口失败:') + e.message);
@ -260,32 +261,165 @@ export default function UpstreamRatioSync(props) {
}
};
const ratioSyncFields = [
'model_ratio',
'completion_ratio',
'cache_ratio',
'create_cache_ratio',
'image_ratio',
'audio_ratio',
'audio_completion_ratio',
];
const numericSyncFields = new Set([...ratioSyncFields, 'model_price']);
const syncFieldOrder = [
...ratioSyncFields,
'model_price',
'billing_mode',
'billing_expr',
];
function getSyncFieldLabel(ratioType) {
const typeMap = {
model_ratio: t('模型倍率'),
completion_ratio: t('补全倍率'),
cache_ratio: t('缓存倍率'),
create_cache_ratio: t('缓存创建倍率'),
image_ratio: t('图片倍率'),
audio_ratio: t('音频倍率'),
audio_completion_ratio: t('音频补全倍率'),
model_price: t('固定价格'),
billing_mode: t('计费模式'),
billing_expr: t('表达式计费'),
};
return typeMap[ratioType] || ratioType;
}
function getOrderedRatioTypes(ratioTypes) {
const keys = Object.keys(ratioTypes || {});
const ordered = [
...syncFieldOrder.filter((field) => keys.includes(field)),
...keys.filter((field) => !syncFieldOrder.includes(field)),
];
return ratioTypeFilter
? ordered.filter((field) => field === ratioTypeFilter)
: ordered;
}
function deleteResolutionField(newRes, model, ratioType) {
if (!newRes[model]) return;
delete newRes[model][ratioType];
if (ratioType === 'billing_expr') {
delete newRes[model].billing_mode;
}
if (ratioType === 'billing_mode') {
delete newRes[model].billing_expr;
}
if (Object.keys(newRes[model]).length === 0) {
delete newRes[model];
}
}
function getBillingCategory(ratioType) {
return ratioType === 'model_price' ? 'price' : 'ratio';
if (ratioType === 'model_price') return 'price';
if (ratioType === 'billing_mode' || ratioType === 'billing_expr') {
return 'tiered';
}
return 'ratio';
}
function optionKeyBySyncField(ratioType) {
const explicit = {
billing_mode: 'billing_setting.billing_mode',
billing_expr: 'billing_setting.billing_expr',
};
if (explicit[ratioType]) return explicit[ratioType];
return ratioType
.split('_')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join('');
}
function getUpstreamValue(model, ratioType, sourceName) {
return differences[model]?.[ratioType]?.upstreams?.[sourceName];
}
function isSelectableUpstreamValue(value) {
return value !== null && value !== undefined && value !== 'same';
}
function getPreferredSyncField(model, ratioType, sourceName) {
const exprValue = getUpstreamValue(model, 'billing_expr', sourceName);
if (ratioType !== 'billing_expr' && isSelectableUpstreamValue(exprValue)) {
return 'billing_expr';
}
return ratioType;
}
function shouldShowSyncField(model, ratioType, sourceName) {
if (!sourceName) return true;
return getPreferredSyncField(model, ratioType, sourceName) === ratioType;
}
const selectValue = useCallback(
(model, ratioType, value) => {
(model, ratioType, value, sourceName) => {
const preferredRatioType = sourceName
? getPreferredSyncField(model, ratioType, sourceName)
: ratioType;
const preferredValue =
preferredRatioType === ratioType
? value
: getUpstreamValue(model, preferredRatioType, sourceName);
ratioType = preferredRatioType;
value = preferredValue;
const category = getBillingCategory(ratioType);
setResolutions((prev) => {
const newModelRes = { ...(prev[model] || {}) };
Object.keys(newModelRes).forEach((rt) => {
if (getBillingCategory(rt) !== category) {
if (
category !== 'tiered' &&
getBillingCategory(rt) !== 'tiered' &&
getBillingCategory(rt) !== category
) {
delete newModelRes[rt];
}
});
newModelRes[ratioType] = value;
if (category === 'tiered' && sourceName) {
const modeValue =
differences[model]?.billing_mode?.upstreams?.[sourceName];
const exprValue =
differences[model]?.billing_expr?.upstreams?.[sourceName];
if (
modeValue !== undefined &&
modeValue !== null &&
modeValue !== 'same'
) {
newModelRes.billing_mode = modeValue;
} else if (ratioType === 'billing_expr') {
newModelRes.billing_mode = 'tiered_expr';
}
if (
exprValue !== undefined &&
exprValue !== null &&
exprValue !== 'same'
) {
newModelRes.billing_expr = exprValue;
}
}
return {
...prev,
[model]: newModelRes,
};
});
},
[setResolutions],
[setResolutions, differences],
);
const applySync = async () => {
@ -293,7 +427,19 @@ export default function UpstreamRatioSync(props) {
ModelRatio: JSON.parse(props.options.ModelRatio || '{}'),
CompletionRatio: JSON.parse(props.options.CompletionRatio || '{}'),
CacheRatio: JSON.parse(props.options.CacheRatio || '{}'),
CreateCacheRatio: JSON.parse(props.options.CreateCacheRatio || '{}'),
ImageRatio: JSON.parse(props.options.ImageRatio || '{}'),
AudioRatio: JSON.parse(props.options.AudioRatio || '{}'),
AudioCompletionRatio: JSON.parse(
props.options.AudioCompletionRatio || '{}',
),
ModelPrice: JSON.parse(props.options.ModelPrice || '{}'),
'billing_setting.billing_mode': JSON.parse(
props.options['billing_setting.billing_mode'] || '{}',
),
'billing_setting.billing_expr': JSON.parse(
props.options['billing_setting.billing_expr'] || '{}',
),
};
const conflicts = [];
@ -303,7 +449,11 @@ export default function UpstreamRatioSync(props) {
if (
currentRatios.ModelRatio[model] !== undefined ||
currentRatios.CompletionRatio[model] !== undefined ||
currentRatios.CacheRatio[model] !== undefined
currentRatios.CacheRatio[model] !== undefined ||
currentRatios.CreateCacheRatio[model] !== undefined ||
currentRatios.ImageRatio[model] !== undefined ||
currentRatios.AudioRatio[model] !== undefined ||
currentRatios.AudioCompletionRatio[model] !== undefined
)
return 'ratio';
return null;
@ -320,9 +470,14 @@ export default function UpstreamRatioSync(props) {
Object.entries(resolutions).forEach(([model, ratios]) => {
const localCat = getLocalBillingCategory(model);
const newCat = 'model_price' in ratios ? 'price' : 'ratio';
const newCat =
'model_price' in ratios
? 'price'
: ratioSyncFields.some((rt) => rt in ratios)
? 'ratio'
: 'tiered';
if (localCat && localCat !== newCat) {
if (localCat && newCat !== 'tiered' && localCat !== newCat) {
const currentDesc =
localCat === 'price'
? `${t('固定价格')} : ${currentRatios.ModelPrice[model]}`
@ -366,33 +521,50 @@ export default function UpstreamRatioSync(props) {
ModelRatio: { ...currentRatios.ModelRatio },
CompletionRatio: { ...currentRatios.CompletionRatio },
CacheRatio: { ...currentRatios.CacheRatio },
CreateCacheRatio: { ...currentRatios.CreateCacheRatio },
ImageRatio: { ...currentRatios.ImageRatio },
AudioRatio: { ...currentRatios.AudioRatio },
AudioCompletionRatio: { ...currentRatios.AudioCompletionRatio },
ModelPrice: { ...currentRatios.ModelPrice },
'billing_setting.billing_mode': {
...currentRatios['billing_setting.billing_mode'],
},
'billing_setting.billing_expr': {
...currentRatios['billing_setting.billing_expr'],
},
};
Object.entries(resolutions).forEach(([model, ratios]) => {
const selectedTypes = Object.keys(ratios);
const hasPrice = selectedTypes.includes('model_price');
const hasRatio = selectedTypes.some((rt) => rt !== 'model_price');
const hasRatio = selectedTypes.some((rt) =>
ratioSyncFields.includes(rt),
);
if (hasPrice) {
delete finalRatios.ModelRatio[model];
delete finalRatios.CompletionRatio[model];
delete finalRatios.CacheRatio[model];
delete finalRatios.CreateCacheRatio[model];
delete finalRatios.ImageRatio[model];
delete finalRatios.AudioRatio[model];
delete finalRatios.AudioCompletionRatio[model];
}
if (hasRatio) {
delete finalRatios.ModelPrice[model];
}
Object.entries(ratios).forEach(([ratioType, value]) => {
const optionKey = ratioType
.split('_')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join('');
finalRatios[optionKey][model] = parseFloat(value);
const optionKey = optionKeyBySyncField(ratioType);
finalRatios[optionKey][model] = numericSyncFields.has(ratioType)
? parseFloat(value)
: value;
});
});
setLoading(true);
showInfo(t('正在同步价格,请稍候'));
let success = false;
try {
const updates = Object.entries(finalRatios).map(([key, value]) =>
API.put('/api/option/', {
@ -426,6 +598,7 @@ export default function UpstreamRatioSync(props) {
});
setResolutions({});
success = true;
} else {
showError(t('部分保存失败'));
}
@ -434,6 +607,7 @@ export default function UpstreamRatioSync(props) {
} finally {
setLoading(false);
}
return success;
},
[resolutions, props.options, props.refresh],
);
@ -451,6 +625,7 @@ export default function UpstreamRatioSync(props) {
<Button
icon={<RefreshCcw size={14} />}
className='w-full md:w-auto mt-2'
disabled={loading || syncLoading || confirmLoading}
onClick={() => {
setModalVisible(true);
if (allChannels.length === 0) {
@ -469,7 +644,10 @@ export default function UpstreamRatioSync(props) {
icon={<CheckSquare size={14} />}
type='secondary'
onClick={applySync}
disabled={!hasSelections}
loading={loading || confirmLoading}
disabled={
!hasSelections || loading || syncLoading || confirmLoading
}
className='w-full md:w-auto mt-2'
>
{t('应用同步')}
@ -484,14 +662,16 @@ export default function UpstreamRatioSync(props) {
value={searchKeyword}
onChange={setSearchKeyword}
className='w-full sm:w-64'
disabled={loading || syncLoading || confirmLoading}
showClear
/>
<Select
placeholder={t('按倍率类型筛选')}
placeholder={t('按价格字段筛选')}
value={ratioTypeFilter}
onChange={setRatioTypeFilter}
className='w-full sm:w-48'
disabled={loading || syncLoading || confirmLoading}
showClear
onClear={() => setRatioTypeFilter('')}
>
@ -500,7 +680,18 @@ export default function UpstreamRatioSync(props) {
{t('补全倍率')}
</Select.Option>
<Select.Option value='cache_ratio'>{t('缓存倍率')}</Select.Option>
<Select.Option value='create_cache_ratio'>
{t('缓存创建倍率')}
</Select.Option>
<Select.Option value='image_ratio'>{t('图片倍率')}</Select.Option>
<Select.Option value='audio_ratio'>{t('音频倍率')}</Select.Option>
<Select.Option value='audio_completion_ratio'>
{t('音频补全倍率')}
</Select.Option>
<Select.Option value='model_price'>{t('固定价格')}</Select.Option>
<Select.Option value='billing_expr'>
{t('表达式计费')}
</Select.Option>
</Select>
</div>
</div>
@ -510,31 +701,17 @@ export default function UpstreamRatioSync(props) {
const renderDifferenceTable = () => {
const dataSource = useMemo(() => {
const tmp = [];
Object.entries(differences).forEach(([model, ratioTypes]) => {
return Object.entries(differences).map(([model, ratioTypes]) => {
const hasPrice = 'model_price' in ratioTypes;
const hasOtherRatio = [
'model_ratio',
'completion_ratio',
'cache_ratio',
].some((rt) => rt in ratioTypes);
const billingConflict = hasPrice && hasOtherRatio;
const hasOtherRatio = ratioSyncFields.some((rt) => rt in ratioTypes);
Object.entries(ratioTypes).forEach(([ratioType, diff]) => {
tmp.push({
key: `${model}_${ratioType}`,
model,
ratioType,
current: diff.current,
upstreams: diff.upstreams,
confidence: diff.confidence || {},
billingConflict,
});
});
return {
key: model,
model,
ratioTypes,
billingConflict: hasPrice && hasOtherRatio,
};
});
return tmp;
}, [differences]);
const filteredDataSource = useMemo(() => {
@ -548,7 +725,7 @@ export default function UpstreamRatioSync(props) {
item.model.toLowerCase().includes(searchKeyword.toLowerCase().trim());
const matchesRatioType =
!ratioTypeFilter || item.ratioType === ratioTypeFilter;
!ratioTypeFilter || ratioTypeFilter in item.ratioTypes;
return matchesKeyword && matchesRatioType;
});
@ -557,12 +734,162 @@ export default function UpstreamRatioSync(props) {
const upstreamNames = useMemo(() => {
const set = new Set();
filteredDataSource.forEach((row) => {
Object.keys(row.upstreams || {}).forEach((name) => set.add(name));
getOrderedRatioTypes(row.ratioTypes).forEach((ratioType) => {
Object.keys(row.ratioTypes[ratioType]?.upstreams || {}).forEach(
(name) => set.add(name),
);
});
});
return Array.from(set);
}, [filteredDataSource]);
}, [filteredDataSource, ratioTypeFilter]);
const renderValueTag = (value, color = 'default') => {
if (value === null || value === undefined) {
return (
<Tag color='default' shape='circle'>
{t('未设置')}
</Tag>
);
}
const text = String(value);
return (
<Tooltip content={text}>
<Tag color={color} shape='circle'>
<span className='inline-block max-w-[360px] truncate align-bottom'>
{text}
</span>
</Tag>
</Tooltip>
);
};
const renderCurrentFields = (record) => {
const fields = getOrderedRatioTypes(record.ratioTypes);
return (
<div className='flex min-w-[260px] flex-col gap-2'>
{fields.map((ratioType) => (
<div
key={ratioType}
className='flex min-w-0 flex-wrap items-center gap-2'
>
<Tag color={stringToColor(ratioType)} shape='circle'>
{getSyncFieldLabel(ratioType)}
</Tag>
{renderValueTag(record.ratioTypes[ratioType]?.current, 'blue')}
</div>
))}
</div>
);
};
const renderUpstreamField = (record, ratioType, upName) => {
const diff = record.ratioTypes[ratioType] || {};
const upstreamVal = diff.upstreams?.[upName];
const isConfident = diff.confidence?.[upName] !== false;
const isPreferredField =
getPreferredSyncField(record.model, ratioType, upName) === ratioType;
if (upstreamVal === null || upstreamVal === undefined) {
return renderValueTag(undefined);
}
if (upstreamVal === 'same') {
return (
<Tag color='blue' shape='circle'>
{t('与本地相同')}
</Tag>
);
}
const text = String(upstreamVal);
const isSelected =
isPreferredField &&
resolutions[record.model]?.[ratioType] === upstreamVal;
const valueNode = isPreferredField ? (
<Checkbox
checked={isSelected}
disabled={loading || syncLoading || confirmLoading}
onChange={(e) => {
const isChecked = e.target.checked;
if (isChecked) {
selectValue(record.model, ratioType, upstreamVal, upName);
} else {
setResolutions((prev) => {
const newRes = { ...prev };
deleteResolutionField(newRes, record.model, ratioType);
return newRes;
});
}
}}
>
<Tooltip content={text}>
<span className='inline-block max-w-[360px] truncate align-bottom'>
{text}
</span>
</Tooltip>
</Checkbox>
) : (
<Tooltip content={text}>
<Tag color='default' shape='circle' type='light'>
<span className='inline-block max-w-[360px] truncate align-bottom'>
{text}
</span>
</Tag>
</Tooltip>
);
return (
<div className='flex min-w-0 items-center gap-2'>
{valueNode}
{!isConfident && (
<Tooltip
position='left'
content={t('该数据可能不可信,请谨慎使用')}
>
<AlertTriangle size={16} className='shrink-0 text-yellow-500' />
</Tooltip>
)}
</div>
);
};
const renderUpstreamFields = (record, upName) => {
const fields = getOrderedRatioTypes(record.ratioTypes).filter(
(ratioType) => shouldShowSyncField(record.model, ratioType, upName),
);
return (
<div className='flex min-w-[280px] flex-col gap-2'>
{fields.map((ratioType) => (
<div key={ratioType} className='flex min-w-0 items-start gap-2'>
<Tag
color={stringToColor(ratioType)}
shape='circle'
className='shrink-0'
>
{getSyncFieldLabel(ratioType)}
</Tag>
<div className='min-w-0 flex-1'>
{renderUpstreamField(record, ratioType, upName)}
</div>
</div>
))}
</div>
);
};
if (filteredDataSource.length === 0) {
if (syncLoading) {
return (
<div className='flex min-h-[260px] flex-col items-center justify-center gap-3'>
<Spin size='large' />
<div className='text-sm text-gray-500'>
{t('正在同步上游价格,请稍候')}
</div>
</div>
);
}
return (
<Empty
image={<IllustrationNoResult style={{ width: 150, height: 150 }} />}
@ -574,7 +901,7 @@ export default function UpstreamRatioSync(props) {
? t('未找到匹配的模型')
: Object.keys(differences).length === 0
? hasSynced
? t('暂无差异化倍率显示')
? t('暂无差异化价格显示')
: t('请先选择同步渠道')
: t('请先选择同步渠道')
}
@ -588,95 +915,24 @@ export default function UpstreamRatioSync(props) {
title: t('模型'),
dataIndex: 'model',
fixed: 'left',
},
{
title: t('倍率类型'),
dataIndex: 'ratioType',
render: (text, record) => {
const typeMap = {
model_ratio: t('模型倍率'),
completion_ratio: t('补全倍率'),
cache_ratio: t('缓存倍率'),
model_price: t('固定价格'),
};
const baseTag = (
<Tag color={stringToColor(text)} shape='circle'>
{typeMap[text] || text}
</Tag>
);
if (record?.billingConflict) {
return (
<div className='flex items-center gap-1'>
{baseTag}
<Tooltip
position='top'
content={t(
'该模型存在固定价格与倍率计费方式冲突,请确认选择',
)}
>
<AlertTriangle size={14} className='text-yellow-500' />
</Tooltip>
</div>
);
}
return baseTag;
},
},
{
title: t('置信度'),
dataIndex: 'confidence',
render: (_, record) => {
const allConfident = Object.values(record.confidence || {}).every(
(v) => v !== false,
);
if (allConfident) {
return (
<Tooltip content={t('所有上游数据均可信')}>
<Tag
color='green'
shape='circle'
type='light'
prefixIcon={<CheckCircle size={14} />}
>
{t('可信')}
</Tag>
</Tooltip>
);
} else {
const untrustedSources = Object.entries(record.confidence || {})
.filter(([_, isConfident]) => isConfident === false)
.map(([name]) => name)
.join(', ');
return (
render: (text, record) => (
<div className='flex min-w-[180px] items-center gap-2'>
<span className='font-medium'>{text}</span>
{record.billingConflict && (
<Tooltip
content={t('以下上游数据可能不可信:') + untrustedSources}
position='top'
content={t('该模型存在固定价格与倍率计费方式冲突,请确认选择')}
>
<Tag
color='yellow'
shape='circle'
type='light'
prefixIcon={<AlertTriangle size={14} />}
>
{t('谨慎')}
</Tag>
<AlertTriangle size={14} className='shrink-0 text-yellow-500' />
</Tooltip>
);
}
},
)}
</div>
),
},
{
title: t('当前'),
title: t('当前价格'),
dataIndex: 'current',
render: (text) => (
<Tag
color={text !== null && text !== undefined ? 'blue' : 'default'}
shape='circle'
>
{text !== null && text !== undefined ? String(text) : t('未设置')}
</Tag>
),
render: (_, record) => renderCurrentFields(record),
},
...upstreamNames.map((upName) => {
const channelStats = (() => {
@ -684,19 +940,20 @@ export default function UpstreamRatioSync(props) {
let selectedCount = 0;
filteredDataSource.forEach((row) => {
const upstreamVal = row.upstreams?.[upName];
if (
upstreamVal !== null &&
upstreamVal !== undefined &&
upstreamVal !== 'same'
) {
selectableCount++;
const isSelected =
resolutions[row.model]?.[row.ratioType] === upstreamVal;
if (isSelected) {
selectedCount++;
getOrderedRatioTypes(row.ratioTypes).forEach((ratioType) => {
const upstreamVal =
row.ratioTypes[ratioType]?.upstreams?.[upName];
if (
getPreferredSyncField(row.model, ratioType, upName) ===
ratioType &&
isSelectableUpstreamValue(upstreamVal)
) {
selectableCount++;
if (resolutions[row.model]?.[ratioType] === upstreamVal) {
selectedCount++;
}
}
}
});
});
return {
@ -713,25 +970,29 @@ export default function UpstreamRatioSync(props) {
const handleBulkSelect = (checked) => {
if (checked) {
filteredDataSource.forEach((row) => {
const upstreamVal = row.upstreams?.[upName];
if (
upstreamVal !== null &&
upstreamVal !== undefined &&
upstreamVal !== 'same'
) {
selectValue(row.model, row.ratioType, upstreamVal);
}
getOrderedRatioTypes(row.ratioTypes).forEach((ratioType) => {
const upstreamVal =
row.ratioTypes[ratioType]?.upstreams?.[upName];
if (
getPreferredSyncField(row.model, ratioType, upName) ===
ratioType &&
isSelectableUpstreamValue(upstreamVal)
) {
selectValue(row.model, ratioType, upstreamVal, upName);
}
});
});
} else {
setResolutions((prev) => {
const newRes = { ...prev };
filteredDataSource.forEach((row) => {
if (newRes[row.model]) {
delete newRes[row.model][row.ratioType];
if (Object.keys(newRes[row.model]).length === 0) {
delete newRes[row.model];
getOrderedRatioTypes(row.ratioTypes).forEach((ratioType) => {
if (
row.ratioTypes[ratioType]?.upstreams?.[upName] !== undefined
) {
deleteResolutionField(newRes, row.model, ratioType);
}
}
});
});
return newRes;
});
@ -743,6 +1004,7 @@ export default function UpstreamRatioSync(props) {
<Checkbox
checked={channelStats.allSelected}
indeterminate={channelStats.partiallySelected}
disabled={loading || syncLoading || confirmLoading}
onChange={(e) => handleBulkSelect(e.target.checked)}
>
{upName}
@ -751,64 +1013,7 @@ export default function UpstreamRatioSync(props) {
<span>{upName}</span>
),
dataIndex: upName,
render: (_, record) => {
const upstreamVal = record.upstreams?.[upName];
const isConfident = record.confidence?.[upName] !== false;
if (upstreamVal === null || upstreamVal === undefined) {
return (
<Tag color='default' shape='circle'>
{t('未设置')}
</Tag>
);
}
if (upstreamVal === 'same') {
return (
<Tag color='blue' shape='circle'>
{t('与本地相同')}
</Tag>
);
}
const isSelected =
resolutions[record.model]?.[record.ratioType] === upstreamVal;
return (
<div className='flex items-center gap-2'>
<Checkbox
checked={isSelected}
onChange={(e) => {
const isChecked = e.target.checked;
if (isChecked) {
selectValue(record.model, record.ratioType, upstreamVal);
} else {
setResolutions((prev) => {
const newRes = { ...prev };
if (newRes[record.model]) {
delete newRes[record.model][record.ratioType];
if (Object.keys(newRes[record.model]).length === 0) {
delete newRes[record.model];
}
}
return newRes;
});
}
}}
>
{String(upstreamVal)}
</Checkbox>
{!isConfident && (
<Tooltip
position='left'
content={t('该数据可能不可信,请谨慎使用')}
>
<AlertTriangle size={16} className='text-yellow-500' />
</Tooltip>
)}
</div>
);
},
render: (_, record) => renderUpstreamFields(record, upName),
};
}),
];
@ -874,15 +1079,37 @@ export default function UpstreamRatioSync(props) {
t={t}
visible={confirmVisible}
items={conflictItems}
loading={confirmLoading}
onOk={async () => {
setConfirmVisible(false);
setConfirmLoading(true);
const curRatios = {
ModelRatio: JSON.parse(props.options.ModelRatio || '{}'),
CompletionRatio: JSON.parse(props.options.CompletionRatio || '{}'),
CacheRatio: JSON.parse(props.options.CacheRatio || '{}'),
CreateCacheRatio: JSON.parse(
props.options.CreateCacheRatio || '{}',
),
ImageRatio: JSON.parse(props.options.ImageRatio || '{}'),
AudioRatio: JSON.parse(props.options.AudioRatio || '{}'),
AudioCompletionRatio: JSON.parse(
props.options.AudioCompletionRatio || '{}',
),
ModelPrice: JSON.parse(props.options.ModelPrice || '{}'),
'billing_setting.billing_mode': JSON.parse(
props.options['billing_setting.billing_mode'] || '{}',
),
'billing_setting.billing_expr': JSON.parse(
props.options['billing_setting.billing_expr'] || '{}',
),
};
await performSync(curRatios);
try {
const success = await performSync(curRatios);
if (success) {
setConfirmVisible(false);
}
} finally {
setConfirmLoading(false);
}
}}
onCancel={() => setConfirmVisible(false)}
/>

View File

@ -31,9 +31,10 @@ import {
TextArea,
Typography,
} from '@douyinfe/semi-ui';
import { IconDelete, IconPlus } from '@douyinfe/semi-icons';
import { IconCopy, IconDelete, IconPlus } from '@douyinfe/semi-icons';
import { renderQuota } from '../../../../helpers/render';
import { BILLING_EXTRA_VARS, BILLING_CACHE_VAR_MAP } from '../../../../constants';
import { copy, showSuccess } from '../../../../helpers';
import { BILLING_EXTRA_VARS, BILLING_CACHE_VAR_MAP, BILLING_CONDITION_VARS } from '../../../../constants';
import {
createEmptyCondition,
createEmptyTimeCondition,
@ -70,6 +71,7 @@ function priceToUnitCost(price) {
const OPS = ['<', '<=', '>', '>='];
const VAR_OPTIONS = [
{ value: 'len', label: 'len (长度)' },
{ value: 'p', label: 'p (输入)' },
{ value: 'c', label: 'c (输出)' },
];
@ -224,7 +226,7 @@ function tryParseVisualConfig(exprStr) {
}
// Multi-tier: cond1 ? tier(body) : cond2 ? tier(body) : tier(body)
const condGroup = `((?:(?:p|c)\\s*(?:<|<=|>|>=)\\s*[\\d.eE+]+)(?:\\s*&&\\s*(?:p|c)\\s*(?:<|<=|>|>=)\\s*[\\d.eE+]+)*)`;
const condGroup = `((?:(?:p|c|len)\\s*(?:<|<=|>|>=)\\s*[\\d.eE+]+)(?:\\s*&&\\s*(?:p|c|len)\\s*(?:<|<=|>|>=)\\s*[\\d.eE+]+)*)`;
const tierRe = new RegExp(
`(?:${condGroup}\\s*\\?\\s*)?tier\\("([^"]*)",\\s*${bodyPat}\\)`,
'g',
@ -237,7 +239,7 @@ function tryParseVisualConfig(exprStr) {
if (condStr) {
const condParts = condStr.split(/\s*&&\s*/);
for (const cp of condParts) {
const cm = cp.trim().match(/^(p|c)\s*(<|<=|>|>=)\s*([\d.eE+]+)$/);
const cm = cp.trim().match(/^(p|c|len)\s*(<|<=|>|>=)\s*([\d.eE+]+)$/);
if (cm) {
conditions.push({ var: cm[1], op: cm[2], value: Number(cm[3]) });
}
@ -283,7 +285,7 @@ function ConditionRow({ cond, onChange, onRemove, t }) {
}}>
<Select
size='small'
value={cond.var || 'p'}
value={cond.var || 'len'}
onChange={(val) => onChange({ ...cond, var: val })}
>
{VAR_OPTIONS.map((v) => (
@ -500,7 +502,7 @@ function ExtendedPriceBlock({ tier, index, onUpdate, t }) {
function VisualTierCard({ tier, index, isLast, isOnly, onUpdate, onRemove, t }) {
const conditions = tier.conditions || [];
const varLabel = { p: t('输入'), c: t('输出') };
const varLabel = { len: t('长度'), p: t('输入'), c: t('输出') };
const condSummary = useMemo(() => {
if (conditions.length === 0) return t('无条件(兜底档)');
return conditions
@ -525,7 +527,7 @@ function VisualTierCard({ tier, index, isLast, isOnly, onUpdate, onRemove, t })
const addCondition = () => {
if (conditions.length >= 2) return;
const usedVars = conditions.map((c) => c.var);
const nextVar = usedVars.includes('p') ? 'c' : 'p';
const nextVar = usedVars.includes('len') ? 'c' : 'len';
onUpdate(index, 'conditions', [
...conditions,
{ var: nextVar, op: '<', value: 200000 },
@ -694,7 +696,7 @@ function VisualEditor({ visualConfig, onChange, t }) {
) {
newTiers[newTiers.length - 1] = {
...newTiers[newTiers.length - 1],
conditions: [{ var: 'p', op: '<', value: 200000 }],
conditions: [{ var: 'len', op: '<', value: 200000 }],
};
}
newTiers.push({
@ -723,7 +725,7 @@ function VisualEditor({ visualConfig, onChange, t }) {
<div>
<Banner
type='info'
description={t('每个档位可设置 0~2 个条件(对 p 和 c最后一档为兜底档无需条件。')}
description={t('每个档位可设置 0~2 个条件(对 len、p 和 c最后一档为兜底档无需条件。len 为输入上下文总长度(含缓存),推荐用于阶梯条件。')}
style={{ marginBottom: 12 }}
/>
@ -762,16 +764,16 @@ const PRESET_GROUPS = [
presets: [
{ key: 'flat', label: 'Flat', expr: 'tier("base", p * 2 + c * 4)' },
{ key: 'claude-opus', label: 'Claude Opus 4.6', expr: 'tier("base", p * 5 + c * 25 + cr * 0.5 + cc * 6.25 + cc1h * 10)' },
{ key: 'gpt-5.4', label: 'GPT-5.4', expr: 'p <= 272000 ? tier("standard", p * 2.5 + c * 15 + cr * 0.25) : tier("long_context", p * 5 + c * 22.5 + cr * 0.5)' },
{ key: 'gpt-5.4', label: 'GPT-5.4', expr: 'len <= 272000 ? tier("standard", p * 2.5 + c * 15 + cr * 0.25) : tier("long_context", p * 5 + c * 22.5 + cr * 0.5)' },
],
},
{
group: '阶梯计费',
presets: [
{ key: 'claude-sonnet', label: 'Claude Sonnet 4.5', expr: 'p <= 200000 ? tier("standard", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6 + cc * 7.5 + cc1h * 12)' },
{ key: 'qwen3-max', label: 'Qwen3 Max', expr: 'p <= 32000 ? tier("short", p * 1.2 + c * 6 + cr * 0.24 + cc * 1.5) : p <= 128000 ? tier("mid", p * 2.4 + c * 12 + cr * 0.48 + cc * 3) : tier("long", p * 3 + c * 15 + cr * 0.6 + cc * 3.75)' },
{ key: 'glm-4.5-air', label: 'GLM-4.5 Air', expr: 'p < 32000 && c < 200 ? tier("short_output", p * 0.8 + c * 2 + cr * 0.16) : p < 32000 && c >= 200 ? tier("long_output", p * 0.8 + c * 6 + cr * 0.16) : tier("mid_context", p * 1.2 + c * 8 + cr * 0.24)' },
{ key: 'doubao-seed-1.8', label: 'Doubao Seed 1.8', expr: 'p <= 32000 && c <= 200 ? tier("discount", p * 0.8 + c * 2 + cr * 0.16 + cc * 0.17) : p <= 32000 ? tier("short", p * 0.8 + c * 8 + cr * 0.16 + cc * 0.17) : p <= 128000 ? tier("mid", p * 1.2 + c * 16 + cr * 0.16 + cc * 0.17) : tier("long", p * 2.4 + c * 24 + cr * 0.16 + cc * 0.17)' },
{ key: 'claude-sonnet', label: 'Claude Sonnet 4.5', expr: 'len <= 200000 ? tier("standard", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6 + cc * 7.5 + cc1h * 12)' },
{ key: 'qwen3-max', label: 'Qwen3 Max', expr: 'len <= 32000 ? tier("short", p * 1.2 + c * 6 + cr * 0.24 + cc * 1.5) : len <= 128000 ? tier("mid", p * 2.4 + c * 12 + cr * 0.48 + cc * 3) : tier("long", p * 3 + c * 15 + cr * 0.6 + cc * 3.75)' },
{ key: 'glm-4.5-air', label: 'GLM-4.5 Air', expr: 'len < 32000 && c < 200 ? tier("short_output", p * 0.8 + c * 2 + cr * 0.16) : len < 32000 && c >= 200 ? tier("long_output", p * 0.8 + c * 6 + cr * 0.16) : tier("mid_context", p * 1.2 + c * 8 + cr * 0.24)' },
{ key: 'doubao-seed-1.8', label: 'Doubao Seed 1.8', expr: 'len <= 32000 && c <= 200 ? tier("discount", p * 0.8 + c * 2 + cr * 0.16 + cc * 0.17) : len <= 32000 ? tier("short", p * 0.8 + c * 8 + cr * 0.16 + cc * 0.17) : len <= 128000 ? tier("mid", p * 1.2 + c * 16 + cr * 0.16 + cc * 0.17) : tier("long", p * 2.4 + c * 24 + cr * 0.16 + cc * 0.17)' },
],
},
{
@ -793,7 +795,7 @@ const PRESET_GROUPS = [
},
{
key: 'gpt-5.4-tiers', label: 'GPT-5.4 Priority/Flex',
expr: 'p <= 272000 ? tier("standard", p * 2.5 + c * 15 + cr * 0.25) : tier("long_context", p * 5 + c * 22.5 + cr * 0.5)',
expr: 'len <= 272000 ? tier("standard", p * 2.5 + c * 15 + cr * 0.25) : tier("long_context", p * 5 + c * 22.5 + cr * 0.5)',
requestRules: [
{ conditions: [{ source: SOURCE_PARAM, path: 'service_tier', mode: MATCH_EQ, value: 'priority' }], multiplier: '2' },
{ conditions: [{ source: SOURCE_PARAM, path: 'service_tier', mode: MATCH_EQ, value: 'flex' }], multiplier: '0.5' },
@ -880,7 +882,8 @@ function RawExprEditor({ exprString, onChange, t }) {
<div>
<div>
{t('变量')}: <code>p</code> ({t('输入 Token')}), <code>c</code> (
{t('输出 Token')}), <code>cr</code> ({t('缓存读取')}),{' '}
{t('输出 Token')}), <code>len</code> ({t('输入长度')}),{' '}
<code>cr</code> ({t('缓存读取')}),{' '}
<code>cc</code> ({t('缓存创建')}),{' '}
<code>cc1h</code> ({t('缓存创建-1小时')})
</div>
@ -968,7 +971,11 @@ function evalExprLocally(exprStr, p, c, extraTokenValues) {
matchedTier = name;
return value;
};
const env = { p, c, tier: tierFn, max: Math.max, min: Math.min, abs: Math.abs, ceil: Math.ceil, floor: Math.floor };
const cacheReadTokens = extraTokenValues.cacheReadTokens || 0;
const cacheCreateTokens = extraTokenValues.cacheCreateTokens || 0;
const cacheCreate1hTokens = extraTokenValues.cacheCreate1hTokens || 0;
const len = p + cacheReadTokens + cacheCreateTokens + cacheCreate1hTokens;
const env = { p, c, len, tier: tierFn, max: Math.max, min: Math.min, abs: Math.abs, ceil: Math.ceil, floor: Math.floor };
for (const field of EXTRA_ESTIMATOR_FIELDS) {
env[field.var] = extraTokenValues[field.stateKey] || 0;
}
@ -1220,6 +1227,146 @@ function RuleGroupCard({ group, index, onChange, onRemove, t }) {
);
}
// ---------------------------------------------------------------------------
// LLM prompt helper copyable prompt for LLM-assisted expression design
// ---------------------------------------------------------------------------
const LLM_PROMPT_TEMPLATE = `你是一个 AI API 计费表达式设计助手。用户需要你帮忙设计一个计费表达式billing expression用于 AI API 网关的模型计费。
## 表达式语言
表达式基于 expr-lang/expr支持标准算术运算和三元运算符
### Token 变量
输入侧
- p 输入 token 计价用系统会自动排除表达式中单独计价的子类别如用了 cr缓存 token 就从 p 中扣除
- len 输入上下文总长度条件判断用不受自动排除影响始终反映完整输入长度用于阶梯条件判断
- cr 缓存命中读取token
- cc 缓存创建 token 5分钟 TTL
- cc1h 缓存创建 token 1小时 TTLClaude 专用
- img 图片输入 token
- ai 音频输入 token
输出侧
- c 输出 token 同样会自动排除单独计价的子类别
- img_o 图片输出 token
- ao 音频输出 token
### p/c 自动排除机制
p c 是兜底变量代表所有没有被表达式单独定价的 token如果表达式使用了某个子类别变量 cr对应 token 就从 p 中扣除避免重复计费没用到的子类别 token 则留在 p/c 中按基础价格计费
重要len 不受自动排除影响阶梯条件应使用 len 而非 p以避免缓存命中导致 p 降低而误判档位
### 内置函数
- tier(name, value) 标记计费档位名称必须包裹费用表达式
- max(a, b)min(a, b) 取大/小值
- ceil(x)floor(x)abs(x) 向上取整向下取整绝对值
- header(name) 读取请求头
- param(path) 读取请求体 JSON 路径gjson 语法
- has(source, substr) 子字符串检查
- hour(tz)minute(tz)weekday(tz)month(tz)day(tz) 时间函数tz 为时区如 "Asia/Shanghai"
### 价格系数
表达式中的数字系数是 $/1M tokens 的价格例如 p * 2.5 表示输入 $2.50/1M tokens
## 表达式示例
简单定价
tier("base", p * 2.5 + c * 15)
带缓存的定价
tier("base", p * 2.5 + c * 15 + cr * 0.25)
多档阶梯 len 做条件
len <= 200000
? tier("standard", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6)
: tier("long_context", p * 6 + c * 22.5 + cr * 0.6 + cc * 7.5 + cc1h * 12)
图片模型
tier("base", p * 2 + c * 8 + img * 2.5)
多模态含音频
tier("base", p * 0.43 + c * 3.06 + img * 0.78 + ai * 3.81 + ao * 15.11)
三档阶梯示例
len <= 128000
? tier("standard", p * 1.1 + c * 4.4)
: (len <= 1000000
? tier("medium", p * 2.2 + c * 8.8)
: tier("long", p * 4.4 + c * 17.6))
## 规则
1. 每个叶子分支必须用 tier("名称", 费用表达式) 包裹
2. tier 名称用英文 "base""standard""long_context"
3. 阶梯条件用 len不要用 p支持 <<=>>=
4. 多档用嵌套三元运算符条件1 ? tier(...) : (条件2 ? tier(...) : tier(...))
5. 价格系数直接写供应商官方 $/1M tokens 价格
6. 不需要缓存/图片/音频单独定价时可以不写对应变量它们的 token 会自动包含在 p/c
请根据用户提供的模型信息和定价需求生成计费表达式`;
function LlmPromptHelper({ t, model }) {
const [open, setOpen] = useState(false);
const modelName = model?.name || '';
const prompt = useMemo(() => {
if (modelName) {
return LLM_PROMPT_TEMPLATE + `\n\n当前模型${modelName}`;
}
return LLM_PROMPT_TEMPLATE;
}, [modelName]);
const handleCopy = useCallback(async () => {
const ok = await copy(prompt);
if (ok) showSuccess(t('已复制到剪贴板'));
}, [prompt, t]);
return (
<div style={{ marginBottom: 12 }}>
<Button
theme='borderless'
size='small'
icon={<IconCopy />}
onClick={() => setOpen(!open)}
style={{ color: 'var(--semi-color-tertiary)' }}
>
{t('LLM 辅助设计提示词')}
</Button>
<Collapsible isOpen={open}>
<Card
bodyStyle={{ padding: 12 }}
style={{ marginTop: 8, background: 'var(--semi-color-fill-0)' }}
>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 8 }}>
<Text size='small' type='secondary'>
{t('复制以下提示词发送给 LLM如 ChatGPT / Claude让它帮你设计计费表达式')}
</Text>
<Button
icon={<IconCopy />}
size='small'
theme='light'
onClick={handleCopy}
>
{t('复制提示词')}
</Button>
</div>
<TextArea
value={prompt}
readonly
autosize={{ minRows: 6, maxRows: 20 }}
style={{ fontFamily: 'monospace', fontSize: 12 }}
/>
</Card>
</Collapsible>
</div>
);
}
// ---------------------------------------------------------------------------
// Main component
// ---------------------------------------------------------------------------
@ -1543,6 +1690,8 @@ export default function TieredPricingEditor({ model, onExprChange, requestRuleEx
</div>
</Card>
<LlmPromptHelper t={t} model={model} />
</div>
);
}

View File

@ -1050,16 +1050,23 @@ export function useModelPricingEditorState({
tieredOutput['billing_setting.billing_expr'][model.name] = finalBillingExpr;
}
}
if (model.billingMode === 'tiered_expr') {
continue;
}
const serialized = serializeModel(model, t);
Object.entries(serialized).forEach(([key, value]) => {
if (value !== null) {
output[key][model.name] = value;
// Always serialize ratio/price values for all models (including
// tiered_expr) so they serve as fallback during multi-instance sync
// delay. ModelPriceHelper checks billing_mode first, so these values
// are only used when billing_setting hasn't propagated yet.
try {
const serialized = serializeModel(model, t);
Object.entries(serialized).forEach(([key, value]) => {
if (value !== null) {
output[key][model.name] = value;
}
});
} catch (e) {
if (model.billingMode !== 'tiered_expr') {
throw e;
}
});
}
}
const requestQueue = [