Compare commits
15 Commits
v0.13.1-pa
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bee339d279 | ||
|
|
4e93148d9e | ||
|
|
e36d191c2e | ||
|
|
34afe9b426 | ||
|
|
d604f48c06 | ||
|
|
86cfb3920e | ||
|
|
097a50ebdc | ||
|
|
f424f906d8 | ||
|
|
cc4ad6c39e | ||
|
|
4c21c4c43b | ||
|
|
db89b57e1c | ||
|
|
62d4b63fc3 | ||
|
|
355307223a | ||
|
|
02aacb38a2 | ||
|
|
095e1920f1 |
@ -43,3 +43,19 @@ func GetJsonType(data json.RawMessage) string {
|
|||||||
return "number"
|
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
43
common/json_test.go
Normal 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))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -32,6 +32,26 @@ const (
|
|||||||
channelUpstreamModelUpdateNotifyMaxFailedChannelIDs = 10
|
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 (
|
var (
|
||||||
channelUpstreamModelUpdateTaskOnce sync.Once
|
channelUpstreamModelUpdateTaskOnce sync.Once
|
||||||
channelUpstreamModelUpdateTaskRunning atomic.Bool
|
channelUpstreamModelUpdateTaskRunning atomic.Bool
|
||||||
@ -521,7 +541,7 @@ func runChannelUpstreamModelUpdateTaskOnce() {
|
|||||||
for {
|
for {
|
||||||
var channels []*model.Channel
|
var channels []*model.Channel
|
||||||
query := model.DB.
|
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).
|
Where("status = ?", common.ChannelStatusEnabled).
|
||||||
Order("id asc").
|
Order("id asc").
|
||||||
Limit(channelUpstreamModelUpdateTaskBatchSize)
|
Limit(channelUpstreamModelUpdateTaskBatchSize)
|
||||||
@ -814,7 +834,7 @@ func collectPendingApplyUpstreamModelChanges(settings dto.ChannelOtherSettings)
|
|||||||
func findEnabledChannelsAfterID(lastID int, batchSize int) ([]*model.Channel, error) {
|
func findEnabledChannelsAfterID(lastID int, batchSize int) ([]*model.Channel, error) {
|
||||||
var channels []*model.Channel
|
var channels []*model.Channel
|
||||||
query := model.DB.
|
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).
|
Where("status = ?", common.ChannelStatusEnabled).
|
||||||
Order("id asc").
|
Order("id asc").
|
||||||
Limit(batchSize)
|
Limit(batchSize)
|
||||||
|
|||||||
@ -81,6 +81,10 @@ func TestCollectPendingApplyUpstreamModelChanges(t *testing.T) {
|
|||||||
require.Equal(t, []string{"old-model"}, pendingRemoveModels)
|
require.Equal(t, []string{"old-model"}, pendingRemoveModels)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestChannelUpstreamModelUpdateSelectFieldsIncludeModelMapping(t *testing.T) {
|
||||||
|
require.Contains(t, channelUpstreamModelUpdateSelectFields, "model_mapping")
|
||||||
|
}
|
||||||
|
|
||||||
func TestNormalizeChannelModelMapping(t *testing.T) {
|
func TestNormalizeChannelModelMapping(t *testing.T) {
|
||||||
modelMapping := `{
|
modelMapping := `{
|
||||||
" alias-model ": " upstream-model ",
|
" alias-model ": " upstream-model ",
|
||||||
|
|||||||
@ -21,14 +21,16 @@ import (
|
|||||||
|
|
||||||
"github.com/QuantumNous/new-api/dto"
|
"github.com/QuantumNous/new-api/dto"
|
||||||
"github.com/QuantumNous/new-api/model"
|
"github.com/QuantumNous/new-api/model"
|
||||||
|
"github.com/QuantumNous/new-api/setting/billing_setting"
|
||||||
"github.com/QuantumNous/new-api/setting/ratio_setting"
|
"github.com/QuantumNous/new-api/setting/ratio_setting"
|
||||||
|
"github.com/samber/lo"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultTimeoutSeconds = 10
|
defaultTimeoutSeconds = 10
|
||||||
defaultEndpoint = "/api/ratio_config"
|
defaultEndpoint = "/api/pricing"
|
||||||
maxConcurrentFetches = 8
|
maxConcurrentFetches = 8
|
||||||
maxRatioConfigBytes = 10 << 20 // 10MB
|
maxRatioConfigBytes = 10 << 20 // 10MB
|
||||||
floatEpsilon = 1e-9
|
floatEpsilon = 1e-9
|
||||||
@ -59,7 +61,29 @@ func valuesEqual(a, b interface{}) bool {
|
|||||||
return a == b
|
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 {
|
type upstreamResult struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@ -67,6 +91,54 @@ type upstreamResult struct {
|
|||||||
Err string `json:"err,omitempty"`
|
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) {
|
func FetchUpstreamRatios(c *gin.Context) {
|
||||||
var req dto.UpstreamRequest
|
var req dto.UpstreamRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
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 {
|
if err := common.Unmarshal(body.Data, &type1Data); err == nil {
|
||||||
// 如果包含至少一个 ratioTypes 字段,则认为是 type1
|
// 如果包含至少一个 ratioTypes 字段,则认为是 type1
|
||||||
isType1 := false
|
isType1 := false
|
||||||
for _, rt := range ratioTypes {
|
for _, rt := range pricingSyncFields {
|
||||||
if _, ok := type1Data[rt]; ok {
|
if _, ok := type1Data[rt]; ok {
|
||||||
isType1 = true
|
isType1 = true
|
||||||
break
|
break
|
||||||
@ -307,11 +379,18 @@ func FetchUpstreamRatios(c *gin.Context) {
|
|||||||
|
|
||||||
// 如果不是 type1,则尝试按 type2 (/api/pricing) 解析
|
// 如果不是 type1,则尝试按 type2 (/api/pricing) 解析
|
||||||
var pricingItems []struct {
|
var pricingItems []struct {
|
||||||
ModelName string `json:"model_name"`
|
ModelName string `json:"model_name"`
|
||||||
QuotaType int `json:"quota_type"`
|
QuotaType int `json:"quota_type"`
|
||||||
ModelRatio float64 `json:"model_ratio"`
|
ModelRatio float64 `json:"model_ratio"`
|
||||||
ModelPrice float64 `json:"model_price"`
|
ModelPrice float64 `json:"model_price"`
|
||||||
CompletionRatio float64 `json:"completion_ratio"`
|
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 {
|
if err := common.Unmarshal(body.Data, &pricingItems); err != nil {
|
||||||
logger.LogWarn(c.Request.Context(), "unrecognized data format from "+chItem.Name+": "+err.Error())
|
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)
|
modelRatioMap := make(map[string]float64)
|
||||||
completionRatioMap := 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)
|
modelPriceMap := make(map[string]float64)
|
||||||
|
billingModeMap := make(map[string]string)
|
||||||
|
billingExprMap := make(map[string]string)
|
||||||
|
|
||||||
for _, item := range pricingItems {
|
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 {
|
if item.QuotaType == 1 {
|
||||||
modelPriceMap[item.ModelName] = item.ModelPrice
|
modelPriceMap[item.ModelName] = item.ModelPrice
|
||||||
} else {
|
} else {
|
||||||
@ -331,6 +424,21 @@ func FetchUpstreamRatios(c *gin.Context) {
|
|||||||
// completionRatio 可能为 0,此时也直接赋值,保持与上游一致
|
// completionRatio 可能为 0,此时也直接赋值,保持与上游一致
|
||||||
completionRatioMap[item.ModelName] = item.CompletionRatio
|
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)
|
converted := make(map[string]any)
|
||||||
@ -350,6 +458,21 @@ func FetchUpstreamRatios(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
converted["completion_ratio"] = compAny
|
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 {
|
if len(modelPriceMap) > 0 {
|
||||||
priceAny := make(map[string]any, len(modelPriceMap))
|
priceAny := make(map[string]any, len(modelPriceMap))
|
||||||
@ -358,6 +481,12 @@ func FetchUpstreamRatios(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
converted["model_price"] = priceAny
|
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}
|
ch <- upstreamResult{Name: uniqueName, Data: converted}
|
||||||
}(chn)
|
}(chn)
|
||||||
@ -366,7 +495,7 @@ func FetchUpstreamRatios(c *gin.Context) {
|
|||||||
wg.Wait()
|
wg.Wait()
|
||||||
close(ch)
|
close(ch)
|
||||||
|
|
||||||
localData := ratio_setting.GetExposedData()
|
localData := getLocalPricingSyncData()
|
||||||
|
|
||||||
var testResults []dto.TestResult
|
var testResults []dto.TestResult
|
||||||
var successfulChannels []struct {
|
var successfulChannels []struct {
|
||||||
@ -412,22 +541,16 @@ func buildDifferences(localData map[string]any, successfulChannels []struct {
|
|||||||
|
|
||||||
allModels := make(map[string]struct{})
|
allModels := make(map[string]struct{})
|
||||||
|
|
||||||
for _, ratioType := range ratioTypes {
|
for _, field := range pricingSyncFields {
|
||||||
if localRatioAny, ok := localData[ratioType]; ok {
|
for modelName := range valueMap(localData[field]) {
|
||||||
if localRatio, ok := localRatioAny.(map[string]float64); ok {
|
allModels[modelName] = struct{}{}
|
||||||
for modelName := range localRatio {
|
|
||||||
allModels[modelName] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, channel := range successfulChannels {
|
for _, channel := range successfulChannels {
|
||||||
for _, ratioType := range ratioTypes {
|
for _, field := range pricingSyncFields {
|
||||||
if upstreamRatio, ok := channel.data[ratioType].(map[string]any); ok {
|
for modelName := range valueMap(channel.data[field]) {
|
||||||
for modelName := range upstreamRatio {
|
allModels[modelName] = struct{}{}
|
||||||
allModels[modelName] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -438,10 +561,10 @@ func buildDifferences(localData map[string]any, successfulChannels []struct {
|
|||||||
for _, channel := range successfulChannels {
|
for _, channel := range successfulChannels {
|
||||||
confidenceMap[channel.name] = make(map[string]bool)
|
confidenceMap[channel.name] = make(map[string]bool)
|
||||||
|
|
||||||
modelRatios, hasModelRatio := channel.data["model_ratio"].(map[string]any)
|
modelRatios := valueMap(channel.data["model_ratio"])
|
||||||
completionRatios, hasCompletionRatio := channel.data["completion_ratio"].(map[string]any)
|
completionRatios := valueMap(channel.data["completion_ratio"])
|
||||||
|
|
||||||
if hasModelRatio && hasCompletionRatio {
|
if len(modelRatios) > 0 && len(completionRatios) > 0 {
|
||||||
// 遍历所有模型,检查是否满足不可信条件
|
// 遍历所有模型,检查是否满足不可信条件
|
||||||
for modelName := range allModels {
|
for modelName := range allModels {
|
||||||
// 默认为可信
|
// 默认为可信
|
||||||
@ -451,12 +574,10 @@ func buildDifferences(localData map[string]any, successfulChannels []struct {
|
|||||||
if modelRatioVal, ok := modelRatios[modelName]; ok {
|
if modelRatioVal, ok := modelRatios[modelName]; ok {
|
||||||
if completionRatioVal, ok := completionRatios[modelName]; ok {
|
if completionRatioVal, ok := completionRatios[modelName]; ok {
|
||||||
// 转换为float64进行比较
|
// 转换为float64进行比较
|
||||||
if modelRatioFloat, ok := modelRatioVal.(float64); ok {
|
modelRatioFloat, modelRatioOK := asFloat64(modelRatioVal)
|
||||||
if completionRatioFloat, ok := completionRatioVal.(float64); ok {
|
completionRatioFloat, completionRatioOK := asFloat64(completionRatioVal)
|
||||||
if modelRatioFloat == 37.5 && completionRatioFloat == 1.0 {
|
if modelRatioOK && completionRatioOK && nearlyEqual(modelRatioFloat, 37.5) && nearlyEqual(completionRatioFloat, 1.0) {
|
||||||
confidenceMap[channel.name][modelName] = false
|
confidenceMap[channel.name][modelName] = false
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -470,14 +591,10 @@ func buildDifferences(localData map[string]any, successfulChannels []struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for modelName := range allModels {
|
for modelName := range allModels {
|
||||||
for _, ratioType := range ratioTypes {
|
for _, ratioType := range pricingSyncFields {
|
||||||
var localValue interface{} = nil
|
var localValue interface{} = nil
|
||||||
if localRatioAny, ok := localData[ratioType]; ok {
|
if val, exists := valueMap(localData[ratioType])[modelName]; exists {
|
||||||
if localRatio, ok := localRatioAny.(map[string]float64); ok {
|
localValue = normalizeSyncValue(ratioType, val)
|
||||||
if val, exists := localRatio[modelName]; exists {
|
|
||||||
localValue = val
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
upstreamValues := make(map[string]interface{})
|
upstreamValues := make(map[string]interface{})
|
||||||
@ -488,16 +605,14 @@ func buildDifferences(localData map[string]any, successfulChannels []struct {
|
|||||||
for _, channel := range successfulChannels {
|
for _, channel := range successfulChannels {
|
||||||
var upstreamValue interface{} = nil
|
var upstreamValue interface{} = nil
|
||||||
|
|
||||||
if upstreamRatio, ok := channel.data[ratioType].(map[string]any); ok {
|
if val, exists := valueMap(channel.data[ratioType])[modelName]; exists {
|
||||||
if val, exists := upstreamRatio[modelName]; exists {
|
upstreamValue = normalizeSyncValue(ratioType, val)
|
||||||
upstreamValue = val
|
hasUpstreamValue = true
|
||||||
hasUpstreamValue = true
|
|
||||||
|
|
||||||
if localValue != nil && !valuesEqual(localValue, val) {
|
if localValue != nil && !valuesEqual(localValue, upstreamValue) {
|
||||||
hasDifference = true
|
hasDifference = true
|
||||||
} else if valuesEqual(localValue, val) {
|
} else if valuesEqual(localValue, upstreamValue) {
|
||||||
upstreamValue = "same"
|
upstreamValue = "same"
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if upstreamValue == nil && localValue == nil {
|
if upstreamValue == nil && localValue == nil {
|
||||||
|
|||||||
@ -91,6 +91,7 @@ func Login(c *gin.Context) {
|
|||||||
|
|
||||||
// setup session & cookies and then return user info
|
// setup session & cookies and then return user info
|
||||||
func setupLogin(user *model.User, c *gin.Context) {
|
func setupLogin(user *model.User, c *gin.Context) {
|
||||||
|
model.UpdateUserLastLoginAt(user.Id)
|
||||||
session := sessions.Default(c)
|
session := sessions.Default(c)
|
||||||
session.Set("id", user.Id)
|
session.Set("id", user.Id)
|
||||||
session.Set("username", user.Username)
|
session.Set("username", user.Username)
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/QuantumNous/new-api/common"
|
||||||
"github.com/QuantumNous/new-api/types"
|
"github.com/QuantumNous/new-api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -346,7 +347,20 @@ type ResponsesOutput struct {
|
|||||||
Size string `json:"size"`
|
Size string `json:"size"`
|
||||||
CallId string `json:"call_id,omitempty"`
|
CallId string `json:"call_id,omitempty"`
|
||||||
Name string `json:"name,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 {
|
type ResponsesOutputContent struct {
|
||||||
|
|||||||
25
i18n/keys.go
25
i18n/keys.go
@ -304,18 +304,19 @@ const (
|
|||||||
|
|
||||||
// Distributor related messages
|
// Distributor related messages
|
||||||
const (
|
const (
|
||||||
MsgDistributorInvalidRequest = "distributor.invalid_request"
|
MsgDistributorInvalidRequest = "distributor.invalid_request"
|
||||||
MsgDistributorInvalidChannelId = "distributor.invalid_channel_id"
|
MsgDistributorInvalidChannelId = "distributor.invalid_channel_id"
|
||||||
MsgDistributorChannelDisabled = "distributor.channel_disabled"
|
MsgDistributorChannelDisabled = "distributor.channel_disabled"
|
||||||
MsgDistributorTokenNoModelAccess = "distributor.token_no_model_access"
|
MsgDistributorAffinityChannelDisabled = "distributor.affinity_channel_disabled"
|
||||||
MsgDistributorTokenModelForbidden = "distributor.token_model_forbidden"
|
MsgDistributorTokenNoModelAccess = "distributor.token_no_model_access"
|
||||||
MsgDistributorModelNameRequired = "distributor.model_name_required"
|
MsgDistributorTokenModelForbidden = "distributor.token_model_forbidden"
|
||||||
MsgDistributorInvalidPlayground = "distributor.invalid_playground_request"
|
MsgDistributorModelNameRequired = "distributor.model_name_required"
|
||||||
MsgDistributorGroupAccessDenied = "distributor.group_access_denied"
|
MsgDistributorInvalidPlayground = "distributor.invalid_playground_request"
|
||||||
MsgDistributorGetChannelFailed = "distributor.get_channel_failed"
|
MsgDistributorGroupAccessDenied = "distributor.group_access_denied"
|
||||||
MsgDistributorNoAvailableChannel = "distributor.no_available_channel"
|
MsgDistributorGetChannelFailed = "distributor.get_channel_failed"
|
||||||
MsgDistributorInvalidMidjourney = "distributor.invalid_midjourney_request"
|
MsgDistributorNoAvailableChannel = "distributor.no_available_channel"
|
||||||
MsgDistributorInvalidParseModel = "distributor.invalid_request_parse_model"
|
MsgDistributorInvalidMidjourney = "distributor.invalid_midjourney_request"
|
||||||
|
MsgDistributorInvalidParseModel = "distributor.invalid_request_parse_model"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Custom OAuth provider related messages
|
// Custom OAuth provider related messages
|
||||||
|
|||||||
@ -257,6 +257,7 @@ common.invalid_input: "Invalid input"
|
|||||||
distributor.invalid_request: "Invalid request: {{.Error}}"
|
distributor.invalid_request: "Invalid request: {{.Error}}"
|
||||||
distributor.invalid_channel_id: "Invalid channel ID"
|
distributor.invalid_channel_id: "Invalid channel ID"
|
||||||
distributor.channel_disabled: "This channel has been disabled"
|
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_no_model_access: "This token has no access to any models"
|
||||||
distributor.token_model_forbidden: "This token has no access to model {{.Model}}"
|
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"
|
distributor.model_name_required: "Model name not specified, model name cannot be empty"
|
||||||
|
|||||||
@ -258,6 +258,7 @@ common.invalid_input: "输入不合法"
|
|||||||
distributor.invalid_request: "无效的请求,{{.Error}}"
|
distributor.invalid_request: "无效的请求,{{.Error}}"
|
||||||
distributor.invalid_channel_id: "无效的渠道 Id"
|
distributor.invalid_channel_id: "无效的渠道 Id"
|
||||||
distributor.channel_disabled: "该渠道已被禁用"
|
distributor.channel_disabled: "该渠道已被禁用"
|
||||||
|
distributor.affinity_channel_disabled: "渠道亲和性命中的渠道已被禁用,已按规则停止重试,请联系管理员处理"
|
||||||
distributor.token_no_model_access: "该令牌无权访问任何模型"
|
distributor.token_no_model_access: "该令牌无权访问任何模型"
|
||||||
distributor.token_model_forbidden: "该令牌无权访问模型 {{.Model}}"
|
distributor.token_model_forbidden: "该令牌无权访问模型 {{.Model}}"
|
||||||
distributor.model_name_required: "未指定模型名称,模型名称不能为空"
|
distributor.model_name_required: "未指定模型名称,模型名称不能为空"
|
||||||
|
|||||||
@ -258,6 +258,7 @@ common.invalid_input: "輸入不合法"
|
|||||||
distributor.invalid_request: "無效的請求,{{.Error}}"
|
distributor.invalid_request: "無效的請求,{{.Error}}"
|
||||||
distributor.invalid_channel_id: "無效的管道 Id"
|
distributor.invalid_channel_id: "無效的管道 Id"
|
||||||
distributor.channel_disabled: "該管道已被禁用"
|
distributor.channel_disabled: "該管道已被禁用"
|
||||||
|
distributor.affinity_channel_disabled: "管道親和性命中的管道已被禁用,已按規則停止重試,請聯絡管理員處理"
|
||||||
distributor.token_no_model_access: "該令牌無權存取任何模型"
|
distributor.token_no_model_access: "該令牌無權存取任何模型"
|
||||||
distributor.token_model_forbidden: "該令牌無權存取模型 {{.Model}}"
|
distributor.token_model_forbidden: "該令牌無權存取模型 {{.Model}}"
|
||||||
distributor.model_name_required: "未指定模型名稱,模型名稱不能為空"
|
distributor.model_name_required: "未指定模型名稱,模型名稱不能為空"
|
||||||
|
|||||||
@ -104,7 +104,7 @@ func Distribute() func(c *gin.Context) {
|
|||||||
if err == nil && preferred != nil {
|
if err == nil && preferred != nil {
|
||||||
if preferred.Status != common.ChannelStatusEnabled {
|
if preferred.Status != common.ChannelStatusEnabled {
|
||||||
if service.ShouldSkipRetryAfterChannelAffinityFailure(c) {
|
if service.ShouldSkipRetryAfterChannelAffinityFailure(c) {
|
||||||
abortWithOpenAiMessage(c, http.StatusForbidden, i18n.T(c, i18n.MsgDistributorChannelDisabled))
|
abortWithOpenAiMessage(c, http.StatusForbidden, i18n.T(c, i18n.MsgDistributorAffinityChannelDisabled))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else if usingGroup == "auto" {
|
} else if usingGroup == "auto" {
|
||||||
|
|||||||
@ -50,6 +50,8 @@ type User struct {
|
|||||||
Setting string `json:"setting" gorm:"type:text;column:setting"`
|
Setting string `json:"setting" gorm:"type:text;column:setting"`
|
||||||
Remark string `json:"remark,omitempty" gorm:"type:varchar(255)" validate:"max=255"`
|
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"`
|
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 {
|
func (user *User) ToBaseUser() *UserBase {
|
||||||
@ -951,6 +953,12 @@ func GetRootUser() (user *User) {
|
|||||||
return 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) {
|
func UpdateUserUsedQuotaAndRequestCount(id int, quota int) {
|
||||||
if common.BatchUpdateEnabled {
|
if common.BatchUpdateEnabled {
|
||||||
addNewRecord(BatchUpdateTypeUsedQuota, id, quota)
|
addNewRecord(BatchUpdateTypeUsedQuota, id, quota)
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/QuantumNous/new-api/common"
|
||||||
"github.com/QuantumNous/new-api/dto"
|
"github.com/QuantumNous/new-api/dto"
|
||||||
"github.com/QuantumNous/new-api/relay/channel"
|
"github.com/QuantumNous/new-api/relay/channel"
|
||||||
"github.com/QuantumNous/new-api/relay/channel/claude"
|
"github.com/QuantumNous/new-api/relay/channel/claude"
|
||||||
@ -18,12 +19,16 @@ import (
|
|||||||
"github.com/QuantumNous/new-api/types"
|
"github.com/QuantumNous/new-api/types"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Adaptor struct {
|
type Adaptor struct {
|
||||||
IsSyncImageModel bool
|
IsSyncImageModel bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const aliAnthropicMessagesModelsEnv = "ALI_ANTHROPIC_MESSAGES_MODELS"
|
||||||
|
const defaultAliAnthropicMessagesModels = "qwen,deepseek-v4,kimi,glm,minimax-m"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
var syncModels = []string{
|
var syncModels = []string{
|
||||||
"z-image",
|
"z-image",
|
||||||
@ -32,8 +37,22 @@ type Adaptor struct {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
func supportsAliAnthropicMessages(modelName string) bool {
|
func supportsAliAnthropicMessages(modelName string) bool {
|
||||||
// Only models with the "qwen" designation can use the Claude-compatible interface; others require conversion.
|
normalizedModelName := strings.ToLower(strings.TrimSpace(modelName))
|
||||||
return strings.Contains(strings.ToLower(modelName), "qwen")
|
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{
|
var syncModels = []string{
|
||||||
|
|||||||
@ -408,7 +408,7 @@ func OaiResponsesToChatStreamHandler(c *gin.Context, info *relaycommon.RelayInfo
|
|||||||
toolCallNameByID[callID] = name
|
toolCallNameByID[callID] = name
|
||||||
}
|
}
|
||||||
|
|
||||||
newArgs := streamResp.Item.Arguments
|
newArgs := streamResp.Item.ArgumentsString()
|
||||||
prevArgs := toolCallArgsByID[callID]
|
prevArgs := toolCallArgsByID[callID]
|
||||||
argsDelta := ""
|
argsDelta := ""
|
||||||
if newArgs != "" {
|
if newArgs != "" {
|
||||||
|
|||||||
@ -60,7 +60,7 @@ func ResponsesResponseToChatCompletionsResponse(resp *dto.OpenAIResponsesRespons
|
|||||||
Type: "function",
|
Type: "function",
|
||||||
Function: dto.FunctionResponse{
|
Function: dto.FunctionResponse{
|
||||||
Name: name,
|
Name: name,
|
||||||
Arguments: out.Arguments,
|
Arguments: out.ArgumentsString(),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,11 +5,14 @@ import (
|
|||||||
|
|
||||||
"github.com/QuantumNous/new-api/pkg/billingexpr"
|
"github.com/QuantumNous/new-api/pkg/billingexpr"
|
||||||
"github.com/QuantumNous/new-api/setting/config"
|
"github.com/QuantumNous/new-api/setting/config"
|
||||||
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
BillingModeRatio = "ratio"
|
BillingModeRatio = "ratio"
|
||||||
BillingModeTieredExpr = "tiered_expr"
|
BillingModeTieredExpr = "tiered_expr"
|
||||||
|
BillingModeField = "billing_mode"
|
||||||
|
BillingExprField = "billing_expr"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BillingSetting is managed by config.GlobalConfig.Register.
|
// BillingSetting is managed by config.GlobalConfig.Register.
|
||||||
@ -44,6 +47,25 @@ func GetBillingExpr(model string) (string, bool) {
|
|||||||
return expr, ok
|
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)
|
// Smoke test (called externally for validation before save)
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|||||||
@ -252,8 +252,16 @@ func updateConfigFromMap(config interface{}, configMap map[string]string) error
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Map, reflect.Slice, reflect.Struct:
|
case reflect.Map:
|
||||||
// 复杂类型使用JSON反序列化
|
// 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())
|
err := json.Unmarshal([]byte(strValue), field.Addr().Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
|
|||||||
96
setting/config/config_test.go
Normal file
96
setting/config/config_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -709,6 +709,18 @@ func GetCompletionRatioCopy() map[string]float64 {
|
|||||||
return completionRatioMap.ReadAll()
|
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 {
|
func FormatMatchingModelName(name string) string {
|
||||||
|
|
||||||
|
|||||||
@ -155,8 +155,8 @@ const ChannelSelectorModal = forwardRef(
|
|||||||
onChange={handleTypeChange}
|
onChange={handleTypeChange}
|
||||||
style={{ width: 120 }}
|
style={{ width: 120 }}
|
||||||
optionList={[
|
optionList={[
|
||||||
{ label: 'ratio_config', value: 'ratio_config' },
|
|
||||||
{ label: 'pricing', value: 'pricing' },
|
{ label: 'pricing', value: 'pricing' },
|
||||||
|
{ label: 'ratio_config', value: 'ratio_config' },
|
||||||
{ label: 'OpenRouter', value: 'openrouter' },
|
{ label: 'OpenRouter', value: 'openrouter' },
|
||||||
{ label: 'custom', value: 'custom' },
|
{ label: 'custom', value: 'custom' },
|
||||||
]}
|
]}
|
||||||
|
|||||||
@ -106,7 +106,7 @@ const RatioSetting = () => {
|
|||||||
<Tabs.TabPane tab={t('未设置价格模型')} itemKey='unset_models'>
|
<Tabs.TabPane tab={t('未设置价格模型')} itemKey='unset_models'>
|
||||||
<ModelRatioNotSetEditor options={inputs} refresh={onRefresh} />
|
<ModelRatioNotSetEditor options={inputs} refresh={onRefresh} />
|
||||||
</Tabs.TabPane>
|
</Tabs.TabPane>
|
||||||
<Tabs.TabPane tab={t('上游倍率同步')} itemKey='upstream_sync'>
|
<Tabs.TabPane tab={t('上游价格同步')} itemKey='upstream_sync'>
|
||||||
<UpstreamRatioSync options={inputs} refresh={onRefresh} />
|
<UpstreamRatioSync options={inputs} refresh={onRefresh} />
|
||||||
</Tabs.TabPane>
|
</Tabs.TabPane>
|
||||||
<Tabs.TabPane tab={t('工具调用定价')} itemKey='tool_price'>
|
<Tabs.TabPane tab={t('工具调用定价')} itemKey='tool_price'>
|
||||||
|
|||||||
@ -269,6 +269,24 @@ const EditChannelModal = (props) => {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}, [inputs.model_mapping]);
|
}, [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(
|
const upstreamDetectedModels = useMemo(
|
||||||
() =>
|
() =>
|
||||||
Array.from(
|
Array.from(
|
||||||
@ -3842,6 +3860,7 @@ const EditChannelModal = (props) => {
|
|||||||
models={fetchedModels}
|
models={fetchedModels}
|
||||||
selected={inputs.models}
|
selected={inputs.models}
|
||||||
redirectModels={redirectModelList}
|
redirectModels={redirectModelList}
|
||||||
|
redirectSourceModels={redirectModelKeyList}
|
||||||
onConfirm={(selectedModels) => {
|
onConfirm={(selectedModels) => {
|
||||||
handleInputChange('models', selectedModels);
|
handleInputChange('models', selectedModels);
|
||||||
showSuccess(t('模型列表已更新'));
|
showSuccess(t('模型列表已更新'));
|
||||||
|
|||||||
@ -43,6 +43,7 @@ const ModelSelectModal = ({
|
|||||||
models = [],
|
models = [],
|
||||||
selected = [],
|
selected = [],
|
||||||
redirectModels = [],
|
redirectModels = [],
|
||||||
|
redirectSourceModels = [],
|
||||||
onConfirm,
|
onConfirm,
|
||||||
onCancel,
|
onCancel,
|
||||||
}) => {
|
}) => {
|
||||||
@ -54,6 +55,14 @@ const ModelSelectModal = ({
|
|||||||
if (typeof model === 'object' && model.model_name) return model.model_name;
|
if (typeof model === 'object' && model.model_name) return model.model_name;
|
||||||
return String(model ?? '');
|
return String(model ?? '');
|
||||||
};
|
};
|
||||||
|
const normalizeModelList = (modelList = []) =>
|
||||||
|
Array.from(
|
||||||
|
new Set(
|
||||||
|
(modelList || [])
|
||||||
|
.map((model) => getModelName(model).trim())
|
||||||
|
.filter(Boolean),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
const normalizedSelected = useMemo(
|
const normalizedSelected = useMemo(
|
||||||
() => (selected || []).map(getModelName),
|
() => (selected || []).map(getModelName),
|
||||||
@ -78,6 +87,10 @@ const ModelSelectModal = ({
|
|||||||
),
|
),
|
||||||
[redirectModels],
|
[redirectModels],
|
||||||
);
|
);
|
||||||
|
const normalizedRedirectSourceSet = useMemo(
|
||||||
|
() => new Set(normalizeModelList(redirectSourceModels)),
|
||||||
|
[redirectSourceModels],
|
||||||
|
);
|
||||||
const normalizedSelectedSet = useMemo(() => {
|
const normalizedSelectedSet = useMemo(() => {
|
||||||
const set = new Set();
|
const set = new Set();
|
||||||
(selected || []).forEach((model) => {
|
(selected || []).forEach((model) => {
|
||||||
@ -116,6 +129,16 @@ const ModelSelectModal = ({
|
|||||||
const existingModels = filteredModels.filter((model) =>
|
const existingModels = filteredModels.filter((model) =>
|
||||||
isExistingModel(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(() => {
|
useEffect(() => {
|
||||||
@ -127,11 +150,15 @@ const ModelSelectModal = ({
|
|||||||
// 当模型列表变化时,设置默认tab
|
// 当模型列表变化时,设置默认tab
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (visible) {
|
if (visible) {
|
||||||
// 默认显示新获取模型tab,如果没有新模型则显示已有模型
|
if (newModels.length > 0) {
|
||||||
const hasNewModels = newModels.length > 0;
|
setActiveTab('new');
|
||||||
setActiveTab(hasNewModels ? 'new' : 'existing');
|
} else if (removedModels.length > 0) {
|
||||||
|
setActiveTab('removed');
|
||||||
|
} else {
|
||||||
|
setActiveTab('existing');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [visible, newModels.length, selected]);
|
}, [visible, newModels.length, removedModels.length, selected]);
|
||||||
|
|
||||||
const handleOk = () => {
|
const handleOk = () => {
|
||||||
onConfirm && onConfirm(checkedList);
|
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
|
showClear
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Spin spinning={!models || models.length === 0}>
|
<Spin
|
||||||
|
spinning={!models || (models.length === 0 && removedModels.length === 0)}
|
||||||
|
>
|
||||||
<div style={{ maxHeight: 400, overflowY: 'auto', paddingRight: 8 }}>
|
<div style={{ maxHeight: 400, overflowY: 'auto', paddingRight: 8 }}>
|
||||||
{filteredModels.length === 0 ? (
|
{filteredModels.length === 0 && removedModels.length === 0 ? (
|
||||||
<Empty
|
<Empty
|
||||||
image={
|
image={
|
||||||
<IllustrationNoResult style={{ width: 150, height: 150 }} />
|
<IllustrationNoResult style={{ width: 150, height: 150 }} />
|
||||||
@ -369,6 +406,14 @@ const ModelSelectModal = ({
|
|||||||
{renderModelsByCategory(existingModelsByCategory, 'existing')}
|
{renderModelsByCategory(existingModelsByCategory, 'existing')}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{activeTab === 'removed' && removedModels.length > 0 && (
|
||||||
|
<div>
|
||||||
|
{renderModelsByCategory(
|
||||||
|
categorizeModels(removedModels),
|
||||||
|
'removed',
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</Checkbox.Group>
|
</Checkbox.Group>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
@ -382,7 +427,11 @@ const ModelSelectModal = ({
|
|||||||
<div className='flex items-center justify-end gap-2'>
|
<div className='flex items-center justify-end gap-2'>
|
||||||
{(() => {
|
{(() => {
|
||||||
const currentModels =
|
const currentModels =
|
||||||
activeTab === 'new' ? newModels : existingModels;
|
activeTab === 'new'
|
||||||
|
? newModels
|
||||||
|
: activeTab === 'removed'
|
||||||
|
? removedModels
|
||||||
|
: existingModels;
|
||||||
const currentSelected = currentModels.filter((model) =>
|
const currentSelected = currentModels.filter((model) =>
|
||||||
checkedList.includes(model),
|
checkedList.includes(model),
|
||||||
).length;
|
).length;
|
||||||
|
|||||||
@ -29,7 +29,14 @@ import {
|
|||||||
Dropdown,
|
Dropdown,
|
||||||
} from '@douyinfe/semi-ui';
|
} from '@douyinfe/semi-ui';
|
||||||
import { IconMore } from '@douyinfe/semi-icons';
|
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
|
* Render user role
|
||||||
@ -350,6 +357,16 @@ export const getUsersColumns = ({
|
|||||||
dataIndex: 'invite',
|
dataIndex: 'invite',
|
||||||
render: (text, record, index) => renderInviteInfo(text, record, t),
|
render: (text, record, index) => renderInviteInfo(text, record, t),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: t('创建时间'),
|
||||||
|
dataIndex: 'created_at',
|
||||||
|
render: renderTimestamp,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: t('最后登录'),
|
||||||
|
dataIndex: 'last_login_at',
|
||||||
|
render: renderTimestamp,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
title: '',
|
title: '',
|
||||||
dataIndex: 'operate',
|
dataIndex: 'operate',
|
||||||
|
|||||||
2
web/src/constants/common.constant.js
vendored
2
web/src/constants/common.constant.js
vendored
@ -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 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';
|
export const TABLE_COMPACT_MODES_KEY = 'table_compact_modes';
|
||||||
|
|
||||||
|
|||||||
@ -29,17 +29,14 @@ import {
|
|||||||
Tooltip,
|
Tooltip,
|
||||||
Select,
|
Select,
|
||||||
Modal,
|
Modal,
|
||||||
|
Spin,
|
||||||
} from '@douyinfe/semi-ui';
|
} from '@douyinfe/semi-ui';
|
||||||
import { IconSearch } from '@douyinfe/semi-icons';
|
import { IconSearch } from '@douyinfe/semi-icons';
|
||||||
import {
|
import { RefreshCcw, CheckSquare, AlertTriangle } from 'lucide-react';
|
||||||
RefreshCcw,
|
|
||||||
CheckSquare,
|
|
||||||
AlertTriangle,
|
|
||||||
CheckCircle,
|
|
||||||
} from 'lucide-react';
|
|
||||||
import {
|
import {
|
||||||
API,
|
API,
|
||||||
showError,
|
showError,
|
||||||
|
showInfo,
|
||||||
showSuccess,
|
showSuccess,
|
||||||
showWarning,
|
showWarning,
|
||||||
stringToColor,
|
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_BASE_URL = 'https://models.dev';
|
||||||
const MODELS_DEV_PRESET_ENDPOINT = 'https://models.dev/api.json';
|
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 isMobile = useIsMobile();
|
||||||
const columns = [
|
const columns = [
|
||||||
{ title: t('渠道'), dataIndex: 'channel' },
|
{ title: t('渠道'), dataIndex: 'channel' },
|
||||||
@ -84,7 +81,10 @@ function ConflictConfirmModal({ t, visible, items, onOk, onCancel }) {
|
|||||||
<Modal
|
<Modal
|
||||||
title={t('确认冲突项修改')}
|
title={t('确认冲突项修改')}
|
||||||
visible={visible}
|
visible={visible}
|
||||||
onCancel={onCancel}
|
confirmLoading={loading}
|
||||||
|
cancelButtonProps={{ disabled: loading }}
|
||||||
|
maskClosable={!loading}
|
||||||
|
onCancel={loading ? undefined : onCancel}
|
||||||
onOk={onOk}
|
onOk={onOk}
|
||||||
size={isMobile ? 'full-width' : 'large'}
|
size={isMobile ? 'full-width' : 'large'}
|
||||||
>
|
>
|
||||||
@ -103,6 +103,7 @@ export default function UpstreamRatioSync(props) {
|
|||||||
const [modalVisible, setModalVisible] = useState(false);
|
const [modalVisible, setModalVisible] = useState(false);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [syncLoading, setSyncLoading] = useState(false);
|
const [syncLoading, setSyncLoading] = useState(false);
|
||||||
|
const [confirmLoading, setConfirmLoading] = useState(false);
|
||||||
const isMobile = useIsMobile();
|
const isMobile = useIsMobile();
|
||||||
|
|
||||||
// 渠道选择相关
|
// 渠道选择相关
|
||||||
@ -251,7 +252,7 @@ export default function UpstreamRatioSync(props) {
|
|||||||
setHasSynced(true);
|
setHasSynced(true);
|
||||||
|
|
||||||
if (Object.keys(differences).length === 0) {
|
if (Object.keys(differences).length === 0) {
|
||||||
showSuccess(t('未找到差异化倍率,无需同步'));
|
showSuccess(t('未找到差异化价格,无需同步'));
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
showError(t('请求后端接口失败:') + e.message);
|
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) {
|
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(
|
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);
|
const category = getBillingCategory(ratioType);
|
||||||
|
|
||||||
setResolutions((prev) => {
|
setResolutions((prev) => {
|
||||||
const newModelRes = { ...(prev[model] || {}) };
|
const newModelRes = { ...(prev[model] || {}) };
|
||||||
|
|
||||||
Object.keys(newModelRes).forEach((rt) => {
|
Object.keys(newModelRes).forEach((rt) => {
|
||||||
if (getBillingCategory(rt) !== category) {
|
if (
|
||||||
|
category !== 'tiered' &&
|
||||||
|
getBillingCategory(rt) !== 'tiered' &&
|
||||||
|
getBillingCategory(rt) !== category
|
||||||
|
) {
|
||||||
delete newModelRes[rt];
|
delete newModelRes[rt];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
newModelRes[ratioType] = value;
|
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 {
|
return {
|
||||||
...prev,
|
...prev,
|
||||||
[model]: newModelRes,
|
[model]: newModelRes,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[setResolutions],
|
[setResolutions, differences],
|
||||||
);
|
);
|
||||||
|
|
||||||
const applySync = async () => {
|
const applySync = async () => {
|
||||||
@ -293,7 +427,19 @@ export default function UpstreamRatioSync(props) {
|
|||||||
ModelRatio: JSON.parse(props.options.ModelRatio || '{}'),
|
ModelRatio: JSON.parse(props.options.ModelRatio || '{}'),
|
||||||
CompletionRatio: JSON.parse(props.options.CompletionRatio || '{}'),
|
CompletionRatio: JSON.parse(props.options.CompletionRatio || '{}'),
|
||||||
CacheRatio: JSON.parse(props.options.CacheRatio || '{}'),
|
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 || '{}'),
|
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 = [];
|
const conflicts = [];
|
||||||
@ -303,7 +449,11 @@ export default function UpstreamRatioSync(props) {
|
|||||||
if (
|
if (
|
||||||
currentRatios.ModelRatio[model] !== undefined ||
|
currentRatios.ModelRatio[model] !== undefined ||
|
||||||
currentRatios.CompletionRatio[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 'ratio';
|
||||||
return null;
|
return null;
|
||||||
@ -320,9 +470,14 @@ export default function UpstreamRatioSync(props) {
|
|||||||
|
|
||||||
Object.entries(resolutions).forEach(([model, ratios]) => {
|
Object.entries(resolutions).forEach(([model, ratios]) => {
|
||||||
const localCat = getLocalBillingCategory(model);
|
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 =
|
const currentDesc =
|
||||||
localCat === 'price'
|
localCat === 'price'
|
||||||
? `${t('固定价格')} : ${currentRatios.ModelPrice[model]}`
|
? `${t('固定价格')} : ${currentRatios.ModelPrice[model]}`
|
||||||
@ -366,33 +521,50 @@ export default function UpstreamRatioSync(props) {
|
|||||||
ModelRatio: { ...currentRatios.ModelRatio },
|
ModelRatio: { ...currentRatios.ModelRatio },
|
||||||
CompletionRatio: { ...currentRatios.CompletionRatio },
|
CompletionRatio: { ...currentRatios.CompletionRatio },
|
||||||
CacheRatio: { ...currentRatios.CacheRatio },
|
CacheRatio: { ...currentRatios.CacheRatio },
|
||||||
|
CreateCacheRatio: { ...currentRatios.CreateCacheRatio },
|
||||||
|
ImageRatio: { ...currentRatios.ImageRatio },
|
||||||
|
AudioRatio: { ...currentRatios.AudioRatio },
|
||||||
|
AudioCompletionRatio: { ...currentRatios.AudioCompletionRatio },
|
||||||
ModelPrice: { ...currentRatios.ModelPrice },
|
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]) => {
|
Object.entries(resolutions).forEach(([model, ratios]) => {
|
||||||
const selectedTypes = Object.keys(ratios);
|
const selectedTypes = Object.keys(ratios);
|
||||||
const hasPrice = selectedTypes.includes('model_price');
|
const hasPrice = selectedTypes.includes('model_price');
|
||||||
const hasRatio = selectedTypes.some((rt) => rt !== 'model_price');
|
const hasRatio = selectedTypes.some((rt) =>
|
||||||
|
ratioSyncFields.includes(rt),
|
||||||
|
);
|
||||||
|
|
||||||
if (hasPrice) {
|
if (hasPrice) {
|
||||||
delete finalRatios.ModelRatio[model];
|
delete finalRatios.ModelRatio[model];
|
||||||
delete finalRatios.CompletionRatio[model];
|
delete finalRatios.CompletionRatio[model];
|
||||||
delete finalRatios.CacheRatio[model];
|
delete finalRatios.CacheRatio[model];
|
||||||
|
delete finalRatios.CreateCacheRatio[model];
|
||||||
|
delete finalRatios.ImageRatio[model];
|
||||||
|
delete finalRatios.AudioRatio[model];
|
||||||
|
delete finalRatios.AudioCompletionRatio[model];
|
||||||
}
|
}
|
||||||
if (hasRatio) {
|
if (hasRatio) {
|
||||||
delete finalRatios.ModelPrice[model];
|
delete finalRatios.ModelPrice[model];
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.entries(ratios).forEach(([ratioType, value]) => {
|
Object.entries(ratios).forEach(([ratioType, value]) => {
|
||||||
const optionKey = ratioType
|
const optionKey = optionKeyBySyncField(ratioType);
|
||||||
.split('_')
|
finalRatios[optionKey][model] = numericSyncFields.has(ratioType)
|
||||||
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
? parseFloat(value)
|
||||||
.join('');
|
: value;
|
||||||
finalRatios[optionKey][model] = parseFloat(value);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
showInfo(t('正在同步价格,请稍候'));
|
||||||
|
let success = false;
|
||||||
try {
|
try {
|
||||||
const updates = Object.entries(finalRatios).map(([key, value]) =>
|
const updates = Object.entries(finalRatios).map(([key, value]) =>
|
||||||
API.put('/api/option/', {
|
API.put('/api/option/', {
|
||||||
@ -426,6 +598,7 @@ export default function UpstreamRatioSync(props) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
setResolutions({});
|
setResolutions({});
|
||||||
|
success = true;
|
||||||
} else {
|
} else {
|
||||||
showError(t('部分保存失败'));
|
showError(t('部分保存失败'));
|
||||||
}
|
}
|
||||||
@ -434,6 +607,7 @@ export default function UpstreamRatioSync(props) {
|
|||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
|
return success;
|
||||||
},
|
},
|
||||||
[resolutions, props.options, props.refresh],
|
[resolutions, props.options, props.refresh],
|
||||||
);
|
);
|
||||||
@ -451,6 +625,7 @@ export default function UpstreamRatioSync(props) {
|
|||||||
<Button
|
<Button
|
||||||
icon={<RefreshCcw size={14} />}
|
icon={<RefreshCcw size={14} />}
|
||||||
className='w-full md:w-auto mt-2'
|
className='w-full md:w-auto mt-2'
|
||||||
|
disabled={loading || syncLoading || confirmLoading}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setModalVisible(true);
|
setModalVisible(true);
|
||||||
if (allChannels.length === 0) {
|
if (allChannels.length === 0) {
|
||||||
@ -469,7 +644,10 @@ export default function UpstreamRatioSync(props) {
|
|||||||
icon={<CheckSquare size={14} />}
|
icon={<CheckSquare size={14} />}
|
||||||
type='secondary'
|
type='secondary'
|
||||||
onClick={applySync}
|
onClick={applySync}
|
||||||
disabled={!hasSelections}
|
loading={loading || confirmLoading}
|
||||||
|
disabled={
|
||||||
|
!hasSelections || loading || syncLoading || confirmLoading
|
||||||
|
}
|
||||||
className='w-full md:w-auto mt-2'
|
className='w-full md:w-auto mt-2'
|
||||||
>
|
>
|
||||||
{t('应用同步')}
|
{t('应用同步')}
|
||||||
@ -484,14 +662,16 @@ export default function UpstreamRatioSync(props) {
|
|||||||
value={searchKeyword}
|
value={searchKeyword}
|
||||||
onChange={setSearchKeyword}
|
onChange={setSearchKeyword}
|
||||||
className='w-full sm:w-64'
|
className='w-full sm:w-64'
|
||||||
|
disabled={loading || syncLoading || confirmLoading}
|
||||||
showClear
|
showClear
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Select
|
<Select
|
||||||
placeholder={t('按倍率类型筛选')}
|
placeholder={t('按价格字段筛选')}
|
||||||
value={ratioTypeFilter}
|
value={ratioTypeFilter}
|
||||||
onChange={setRatioTypeFilter}
|
onChange={setRatioTypeFilter}
|
||||||
className='w-full sm:w-48'
|
className='w-full sm:w-48'
|
||||||
|
disabled={loading || syncLoading || confirmLoading}
|
||||||
showClear
|
showClear
|
||||||
onClear={() => setRatioTypeFilter('')}
|
onClear={() => setRatioTypeFilter('')}
|
||||||
>
|
>
|
||||||
@ -500,7 +680,18 @@ export default function UpstreamRatioSync(props) {
|
|||||||
{t('补全倍率')}
|
{t('补全倍率')}
|
||||||
</Select.Option>
|
</Select.Option>
|
||||||
<Select.Option value='cache_ratio'>{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='model_price'>{t('固定价格')}</Select.Option>
|
||||||
|
<Select.Option value='billing_expr'>
|
||||||
|
{t('表达式计费')}
|
||||||
|
</Select.Option>
|
||||||
</Select>
|
</Select>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -510,31 +701,17 @@ export default function UpstreamRatioSync(props) {
|
|||||||
|
|
||||||
const renderDifferenceTable = () => {
|
const renderDifferenceTable = () => {
|
||||||
const dataSource = useMemo(() => {
|
const dataSource = useMemo(() => {
|
||||||
const tmp = [];
|
return Object.entries(differences).map(([model, ratioTypes]) => {
|
||||||
|
|
||||||
Object.entries(differences).forEach(([model, ratioTypes]) => {
|
|
||||||
const hasPrice = 'model_price' in ratioTypes;
|
const hasPrice = 'model_price' in ratioTypes;
|
||||||
const hasOtherRatio = [
|
const hasOtherRatio = ratioSyncFields.some((rt) => rt in ratioTypes);
|
||||||
'model_ratio',
|
|
||||||
'completion_ratio',
|
|
||||||
'cache_ratio',
|
|
||||||
].some((rt) => rt in ratioTypes);
|
|
||||||
const billingConflict = hasPrice && hasOtherRatio;
|
|
||||||
|
|
||||||
Object.entries(ratioTypes).forEach(([ratioType, diff]) => {
|
return {
|
||||||
tmp.push({
|
key: model,
|
||||||
key: `${model}_${ratioType}`,
|
model,
|
||||||
model,
|
ratioTypes,
|
||||||
ratioType,
|
billingConflict: hasPrice && hasOtherRatio,
|
||||||
current: diff.current,
|
};
|
||||||
upstreams: diff.upstreams,
|
|
||||||
confidence: diff.confidence || {},
|
|
||||||
billingConflict,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return tmp;
|
|
||||||
}, [differences]);
|
}, [differences]);
|
||||||
|
|
||||||
const filteredDataSource = useMemo(() => {
|
const filteredDataSource = useMemo(() => {
|
||||||
@ -548,7 +725,7 @@ export default function UpstreamRatioSync(props) {
|
|||||||
item.model.toLowerCase().includes(searchKeyword.toLowerCase().trim());
|
item.model.toLowerCase().includes(searchKeyword.toLowerCase().trim());
|
||||||
|
|
||||||
const matchesRatioType =
|
const matchesRatioType =
|
||||||
!ratioTypeFilter || item.ratioType === ratioTypeFilter;
|
!ratioTypeFilter || ratioTypeFilter in item.ratioTypes;
|
||||||
|
|
||||||
return matchesKeyword && matchesRatioType;
|
return matchesKeyword && matchesRatioType;
|
||||||
});
|
});
|
||||||
@ -557,12 +734,162 @@ export default function UpstreamRatioSync(props) {
|
|||||||
const upstreamNames = useMemo(() => {
|
const upstreamNames = useMemo(() => {
|
||||||
const set = new Set();
|
const set = new Set();
|
||||||
filteredDataSource.forEach((row) => {
|
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);
|
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 (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 (
|
return (
|
||||||
<Empty
|
<Empty
|
||||||
image={<IllustrationNoResult style={{ width: 150, height: 150 }} />}
|
image={<IllustrationNoResult style={{ width: 150, height: 150 }} />}
|
||||||
@ -574,7 +901,7 @@ export default function UpstreamRatioSync(props) {
|
|||||||
? t('未找到匹配的模型')
|
? t('未找到匹配的模型')
|
||||||
: Object.keys(differences).length === 0
|
: Object.keys(differences).length === 0
|
||||||
? hasSynced
|
? hasSynced
|
||||||
? t('暂无差异化倍率显示')
|
? t('暂无差异化价格显示')
|
||||||
: t('请先选择同步渠道')
|
: t('请先选择同步渠道')
|
||||||
: t('请先选择同步渠道')
|
: t('请先选择同步渠道')
|
||||||
}
|
}
|
||||||
@ -588,95 +915,24 @@ export default function UpstreamRatioSync(props) {
|
|||||||
title: t('模型'),
|
title: t('模型'),
|
||||||
dataIndex: 'model',
|
dataIndex: 'model',
|
||||||
fixed: 'left',
|
fixed: 'left',
|
||||||
},
|
render: (text, record) => (
|
||||||
{
|
<div className='flex min-w-[180px] items-center gap-2'>
|
||||||
title: t('倍率类型'),
|
<span className='font-medium'>{text}</span>
|
||||||
dataIndex: 'ratioType',
|
{record.billingConflict && (
|
||||||
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 (
|
|
||||||
<Tooltip
|
<Tooltip
|
||||||
content={t('以下上游数据可能不可信:') + untrustedSources}
|
position='top'
|
||||||
|
content={t('该模型存在固定价格与倍率计费方式冲突,请确认选择')}
|
||||||
>
|
>
|
||||||
<Tag
|
<AlertTriangle size={14} className='shrink-0 text-yellow-500' />
|
||||||
color='yellow'
|
|
||||||
shape='circle'
|
|
||||||
type='light'
|
|
||||||
prefixIcon={<AlertTriangle size={14} />}
|
|
||||||
>
|
|
||||||
{t('谨慎')}
|
|
||||||
</Tag>
|
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
);
|
)}
|
||||||
}
|
</div>
|
||||||
},
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: t('当前值'),
|
title: t('当前价格'),
|
||||||
dataIndex: 'current',
|
dataIndex: 'current',
|
||||||
render: (text) => (
|
render: (_, record) => renderCurrentFields(record),
|
||||||
<Tag
|
|
||||||
color={text !== null && text !== undefined ? 'blue' : 'default'}
|
|
||||||
shape='circle'
|
|
||||||
>
|
|
||||||
{text !== null && text !== undefined ? String(text) : t('未设置')}
|
|
||||||
</Tag>
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
...upstreamNames.map((upName) => {
|
...upstreamNames.map((upName) => {
|
||||||
const channelStats = (() => {
|
const channelStats = (() => {
|
||||||
@ -684,19 +940,20 @@ export default function UpstreamRatioSync(props) {
|
|||||||
let selectedCount = 0;
|
let selectedCount = 0;
|
||||||
|
|
||||||
filteredDataSource.forEach((row) => {
|
filteredDataSource.forEach((row) => {
|
||||||
const upstreamVal = row.upstreams?.[upName];
|
getOrderedRatioTypes(row.ratioTypes).forEach((ratioType) => {
|
||||||
if (
|
const upstreamVal =
|
||||||
upstreamVal !== null &&
|
row.ratioTypes[ratioType]?.upstreams?.[upName];
|
||||||
upstreamVal !== undefined &&
|
if (
|
||||||
upstreamVal !== 'same'
|
getPreferredSyncField(row.model, ratioType, upName) ===
|
||||||
) {
|
ratioType &&
|
||||||
selectableCount++;
|
isSelectableUpstreamValue(upstreamVal)
|
||||||
const isSelected =
|
) {
|
||||||
resolutions[row.model]?.[row.ratioType] === upstreamVal;
|
selectableCount++;
|
||||||
if (isSelected) {
|
if (resolutions[row.model]?.[ratioType] === upstreamVal) {
|
||||||
selectedCount++;
|
selectedCount++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -713,25 +970,29 @@ export default function UpstreamRatioSync(props) {
|
|||||||
const handleBulkSelect = (checked) => {
|
const handleBulkSelect = (checked) => {
|
||||||
if (checked) {
|
if (checked) {
|
||||||
filteredDataSource.forEach((row) => {
|
filteredDataSource.forEach((row) => {
|
||||||
const upstreamVal = row.upstreams?.[upName];
|
getOrderedRatioTypes(row.ratioTypes).forEach((ratioType) => {
|
||||||
if (
|
const upstreamVal =
|
||||||
upstreamVal !== null &&
|
row.ratioTypes[ratioType]?.upstreams?.[upName];
|
||||||
upstreamVal !== undefined &&
|
if (
|
||||||
upstreamVal !== 'same'
|
getPreferredSyncField(row.model, ratioType, upName) ===
|
||||||
) {
|
ratioType &&
|
||||||
selectValue(row.model, row.ratioType, upstreamVal);
|
isSelectableUpstreamValue(upstreamVal)
|
||||||
}
|
) {
|
||||||
|
selectValue(row.model, ratioType, upstreamVal, upName);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
setResolutions((prev) => {
|
setResolutions((prev) => {
|
||||||
const newRes = { ...prev };
|
const newRes = { ...prev };
|
||||||
filteredDataSource.forEach((row) => {
|
filteredDataSource.forEach((row) => {
|
||||||
if (newRes[row.model]) {
|
getOrderedRatioTypes(row.ratioTypes).forEach((ratioType) => {
|
||||||
delete newRes[row.model][row.ratioType];
|
if (
|
||||||
if (Object.keys(newRes[row.model]).length === 0) {
|
row.ratioTypes[ratioType]?.upstreams?.[upName] !== undefined
|
||||||
delete newRes[row.model];
|
) {
|
||||||
|
deleteResolutionField(newRes, row.model, ratioType);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
});
|
||||||
return newRes;
|
return newRes;
|
||||||
});
|
});
|
||||||
@ -743,6 +1004,7 @@ export default function UpstreamRatioSync(props) {
|
|||||||
<Checkbox
|
<Checkbox
|
||||||
checked={channelStats.allSelected}
|
checked={channelStats.allSelected}
|
||||||
indeterminate={channelStats.partiallySelected}
|
indeterminate={channelStats.partiallySelected}
|
||||||
|
disabled={loading || syncLoading || confirmLoading}
|
||||||
onChange={(e) => handleBulkSelect(e.target.checked)}
|
onChange={(e) => handleBulkSelect(e.target.checked)}
|
||||||
>
|
>
|
||||||
{upName}
|
{upName}
|
||||||
@ -751,64 +1013,7 @@ export default function UpstreamRatioSync(props) {
|
|||||||
<span>{upName}</span>
|
<span>{upName}</span>
|
||||||
),
|
),
|
||||||
dataIndex: upName,
|
dataIndex: upName,
|
||||||
render: (_, record) => {
|
render: (_, record) => renderUpstreamFields(record, upName),
|
||||||
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>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
@ -874,15 +1079,37 @@ export default function UpstreamRatioSync(props) {
|
|||||||
t={t}
|
t={t}
|
||||||
visible={confirmVisible}
|
visible={confirmVisible}
|
||||||
items={conflictItems}
|
items={conflictItems}
|
||||||
|
loading={confirmLoading}
|
||||||
onOk={async () => {
|
onOk={async () => {
|
||||||
setConfirmVisible(false);
|
setConfirmLoading(true);
|
||||||
const curRatios = {
|
const curRatios = {
|
||||||
ModelRatio: JSON.parse(props.options.ModelRatio || '{}'),
|
ModelRatio: JSON.parse(props.options.ModelRatio || '{}'),
|
||||||
CompletionRatio: JSON.parse(props.options.CompletionRatio || '{}'),
|
CompletionRatio: JSON.parse(props.options.CompletionRatio || '{}'),
|
||||||
CacheRatio: JSON.parse(props.options.CacheRatio || '{}'),
|
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 || '{}'),
|
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)}
|
onCancel={() => setConfirmVisible(false)}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@ -1050,16 +1050,23 @@ export function useModelPricingEditorState({
|
|||||||
tieredOutput['billing_setting.billing_expr'][model.name] = finalBillingExpr;
|
tieredOutput['billing_setting.billing_expr'][model.name] = finalBillingExpr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (model.billingMode === 'tiered_expr') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const serialized = serializeModel(model, t);
|
// Always serialize ratio/price values for all models (including
|
||||||
Object.entries(serialized).forEach(([key, value]) => {
|
// tiered_expr) so they serve as fallback during multi-instance sync
|
||||||
if (value !== null) {
|
// delay. ModelPriceHelper checks billing_mode first, so these values
|
||||||
output[key][model.name] = value;
|
// 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 = [
|
const requestQueue = [
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user