Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
b8bb3f40
authored
Jul 27, 2026
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: update import paths to use new types package
parent
60a1acb7
Hide whitespace changes
Inline
Side-by-side
Showing
21 changed files
with
75 additions
and
84 deletions
+75
-84
controller/channel-test.go
+3
-2
controller/channel_test_internal_test.go
+1
-1
controller/task.go
+1
-1
model/log.go
+1
-1
model/pricing.go
+1
-1
relay/chat_completions_via_responses_test.go
+1
-1
relay/common/relay_info.go
+2
-1
relay/helper/price.go
+19
-18
relaykit/go.mod
+0
-1
relaykit/go.sum
+0
-2
service/log_info_generate.go
+2
-1
service/quota.go
+1
-1
service/task_billing.go
+1
-1
service/task_billing_test.go
+1
-1
service/text_quota_test.go
+33
-32
setting/ratio_setting/cache_ratio.go
+1
-1
setting/ratio_setting/group_ratio.go
+1
-1
setting/ratio_setting/model_ratio.go
+1
-1
types/price_data.go
+0
-2
types/rw_map.go
+5
-8
types/set.go
+0
-6
No files found.
controller/channel-test.go
View file @
b8bb3f40
...
...
@@ -28,6 +28,7 @@ import (
"github.com/QuantumNous/new-api/service"
"github.com/QuantumNous/new-api/setting/operation_setting"
"github.com/QuantumNous/new-api/setting/ratio_setting"
hosttypes
"github.com/QuantumNous/new-api/types"
"github.com/samber/lo"
"github.com/tidwall/gjson"
...
...
@@ -530,7 +531,7 @@ func attachTestBillingRequestInput(info *relaycommon.RelayInfo, request dto.Requ
return
nil
}
func
settleTestQuota
(
info
*
relaycommon
.
RelayInfo
,
priceData
types
.
PriceData
,
usage
*
dto
.
Usage
)
(
int
,
*
billingexpr
.
TieredResult
)
{
func
settleTestQuota
(
info
*
relaycommon
.
RelayInfo
,
priceData
host
types
.
PriceData
,
usage
*
dto
.
Usage
)
(
int
,
*
billingexpr
.
TieredResult
)
{
if
usage
!=
nil
&&
info
!=
nil
&&
info
.
TieredBillingSnapshot
!=
nil
{
isClaudeUsageSemantic
:=
usage
.
UsageSemantic
==
"anthropic"
||
info
.
GetFinalRequestRelayFormat
()
==
types
.
RelayFormatClaude
usedVars
:=
billingexpr
.
UsedVars
(
info
.
TieredBillingSnapshot
.
ExprString
)
...
...
@@ -552,7 +553,7 @@ func settleTestQuota(info *relaycommon.RelayInfo, priceData types.PriceData, usa
return
int
(
priceData
.
ModelPrice
*
common
.
QuotaPerUnit
),
nil
}
func
buildTestLogOther
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
priceData
types
.
PriceData
,
usage
*
dto
.
Usage
,
tieredResult
*
billingexpr
.
TieredResult
)
map
[
string
]
interface
{}
{
func
buildTestLogOther
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
priceData
host
types
.
PriceData
,
usage
*
dto
.
Usage
,
tieredResult
*
billingexpr
.
TieredResult
)
map
[
string
]
interface
{}
{
other
:=
service
.
GenerateTextOtherInfo
(
c
,
info
,
priceData
.
ModelRatio
,
priceData
.
GroupRatioInfo
.
GroupRatio
,
priceData
.
CompletionRatio
,
usage
.
PromptTokensDetails
.
CachedTokens
,
priceData
.
CacheRatio
,
priceData
.
ModelPrice
,
priceData
.
GroupRatioInfo
.
GroupSpecialRatio
)
if
tieredResult
!=
nil
{
...
...
controller/channel_test_internal_test.go
View file @
b8bb3f40
...
...
@@ -13,9 +13,9 @@ import (
"github.com/QuantumNous/new-api/pkg/billingexpr"
relaycommon
"github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/relaykit/dto"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/service"
"github.com/QuantumNous/new-api/setting/operation_setting"
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
...
...
controller/task.go
View file @
b8bb3f40
...
...
@@ -8,7 +8,7 @@ import (
"github.com/QuantumNous/new-api/dto"
"github.com/QuantumNous/new-api/model"
"github.com/QuantumNous/new-api/relay"
"github.com/QuantumNous/new-api/
relaykit/
types"
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
)
...
...
model/log.go
View file @
b8bb3f40
...
...
@@ -9,7 +9,7 @@ import (
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/logger"
"github.com/QuantumNous/new-api/
relaykit/
types"
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
...
...
model/pricing.go
View file @
b8bb3f40
...
...
@@ -10,9 +10,9 @@ import (
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/relaykit/dto"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/setting/billing_setting"
"github.com/QuantumNous/new-api/setting/ratio_setting"
"github.com/QuantumNous/new-api/types"
)
type
Pricing
struct
{
...
...
relay/chat_completions_via_responses_test.go
View file @
b8bb3f40
...
...
@@ -5,7 +5,7 @@ import (
"testing"
relaycommon
"github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/
relaykit/
types"
"github.com/QuantumNous/new-api/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
...
...
relay/common/relay_info.go
View file @
b8bb3f40
...
...
@@ -16,6 +16,7 @@ import (
"github.com/QuantumNous/new-api/relaykit/relayconvert/convmeta"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/setting/model_setting"
hosttypes
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
...
...
@@ -155,7 +156,7 @@ type RelayInfo struct {
// *bytes.Reader/Buffer/strings.Reader). 0 means "let net/http decide".
UpstreamRequestBodySize
int64
PriceData
types
.
PriceData
PriceData
host
types
.
PriceData
// QuotaClamp is set (non-nil) when a quota conversion saturated at the
// int32 bound (or NaN fallback) while computing this request's charge.
...
...
relay/helper/price.go
View file @
b8bb3f40
...
...
@@ -13,6 +13,7 @@ import (
"github.com/QuantumNous/new-api/setting/billing_setting"
"github.com/QuantumNous/new-api/setting/operation_setting"
"github.com/QuantumNous/new-api/setting/ratio_setting"
hosttypes
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
)
...
...
@@ -41,8 +42,8 @@ const claudeCacheCreation1hMultiplier = 6 / 3.75
const
defaultTieredPreConsumeMaxTokens
=
8192
// HandleGroupRatio checks for "auto_group" in the context and updates the group ratio and relayInfo.UsingGroup if present
func
HandleGroupRatio
(
ctx
*
gin
.
Context
,
relayInfo
*
relaycommon
.
RelayInfo
)
types
.
GroupRatioInfo
{
groupRatioInfo
:=
types
.
GroupRatioInfo
{
func
HandleGroupRatio
(
ctx
*
gin
.
Context
,
relayInfo
*
relaycommon
.
RelayInfo
)
host
types
.
GroupRatioInfo
{
groupRatioInfo
:=
host
types
.
GroupRatioInfo
{
GroupRatio
:
1.0
,
// default ratio
GroupSpecialRatio
:
-
1
,
}
...
...
@@ -69,7 +70,7 @@ func HandleGroupRatio(ctx *gin.Context, relayInfo *relaycommon.RelayInfo) types.
return
groupRatioInfo
}
func
ModelPriceHelper
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
promptTokens
int
,
meta
*
types
.
TokenCountMeta
)
(
types
.
PriceData
,
error
)
{
func
ModelPriceHelper
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
promptTokens
int
,
meta
*
types
.
TokenCountMeta
)
(
host
types
.
PriceData
,
error
)
{
modelPrice
,
usePrice
:=
ratio_setting
.
GetModelPrice
(
info
.
OriginModelName
,
false
)
groupRatioInfo
:=
HandleGroupRatio
(
c
,
info
)
...
...
@@ -104,7 +105,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
acceptUnsetRatio
=
true
}
if
!
acceptUnsetRatio
{
return
types
.
PriceData
{},
modelPriceNotConfiguredError
(
matchName
,
info
.
UserId
)
return
host
types
.
PriceData
{},
modelPriceNotConfiguredError
(
matchName
,
info
.
UserId
)
}
}
completionRatio
=
ratio_setting
.
GetCompletionRatio
(
info
.
OriginModelName
)
...
...
@@ -119,7 +120,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
ratio
:=
modelRatio
*
groupRatioInfo
.
GroupRatio
quota
,
err
:=
common
.
QuotaFromFloatStrict
(
float64
(
preConsumedTokens
)
*
ratio
)
if
err
!=
nil
{
return
types
.
PriceData
{},
err
return
host
types
.
PriceData
{},
err
}
preConsumedQuota
=
quota
}
else
{
...
...
@@ -147,7 +148,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
}
}
priceData
:=
types
.
PriceData
{
priceData
:=
host
types
.
PriceData
{
FreeModel
:
freeModel
,
ModelPrice
:
modelPrice
,
ModelRatio
:
modelRatio
,
...
...
@@ -170,7 +171,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
quotaToPreConsume
:=
priceData
.
ApplyOtherRatiosToFloat
(
modelPrice
*
common
.
QuotaPerUnit
*
groupRatioInfo
.
GroupRatio
)
quota
,
err
:=
common
.
QuotaFromFloatStrict
(
quotaToPreConsume
)
if
err
!=
nil
{
return
types
.
PriceData
{},
err
return
host
types
.
PriceData
{},
err
}
priceData
.
QuotaToPreConsume
=
quota
}
...
...
@@ -183,7 +184,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
}
// ModelPriceHelperPerCall 按次/按量计费的 PriceHelper (MJ、Task)
func
ModelPriceHelperPerCall
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
(
types
.
PriceData
,
error
)
{
func
ModelPriceHelperPerCall
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
(
host
types
.
PriceData
,
error
)
{
groupRatioInfo
:=
HandleGroupRatio
(
c
,
info
)
modelPrice
,
success
:=
ratio_setting
.
GetModelPrice
(
info
.
OriginModelName
,
true
)
...
...
@@ -204,7 +205,7 @@ func ModelPriceHelperPerCall(c *gin.Context, info *relaycommon.RelayInfo) (types
acceptUnsetRatio
=
true
}
if
!
ratioSuccess
&&
!
acceptUnsetRatio
{
return
types
.
PriceData
{},
modelPriceNotConfiguredError
(
matchName
,
info
.
UserId
)
return
host
types
.
PriceData
{},
modelPriceNotConfiguredError
(
matchName
,
info
.
UserId
)
}
}
}
...
...
@@ -216,7 +217,7 @@ func ModelPriceHelperPerCall(c *gin.Context, info *relaycommon.RelayInfo) (types
var
err
error
quota
,
err
=
common
.
QuotaFromFloatStrict
(
modelPrice
*
common
.
QuotaPerUnit
*
groupRatioInfo
.
GroupRatio
)
if
err
!=
nil
{
return
types
.
PriceData
{},
err
return
host
types
.
PriceData
{},
err
}
if
!
operation_setting
.
GetQuotaSetting
()
.
EnableFreeModelPreConsume
{
if
groupRatioInfo
.
GroupRatio
==
0
||
modelPrice
==
0
{
...
...
@@ -229,7 +230,7 @@ func ModelPriceHelperPerCall(c *gin.Context, info *relaycommon.RelayInfo) (types
var
err
error
quota
,
err
=
common
.
QuotaFromFloatStrict
(
modelRatio
/
2
*
common
.
QuotaPerUnit
*
groupRatioInfo
.
GroupRatio
)
if
err
!=
nil
{
return
types
.
PriceData
{},
err
return
host
types
.
PriceData
{},
err
}
modelPrice
=
-
1
if
!
operation_setting
.
GetQuotaSetting
()
.
EnableFreeModelPreConsume
{
...
...
@@ -240,7 +241,7 @@ func ModelPriceHelperPerCall(c *gin.Context, info *relaycommon.RelayInfo) (types
}
}
priceData
:=
types
.
PriceData
{
priceData
:=
host
types
.
PriceData
{
FreeModel
:
freeModel
,
ModelPrice
:
modelPrice
,
ModelRatio
:
modelRatio
,
...
...
@@ -265,10 +266,10 @@ func HasModelBillingConfig(modelName string) bool {
return
ok
&&
strings
.
TrimSpace
(
expr
)
!=
""
}
func
modelPriceHelperTiered
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
promptTokens
int
,
meta
*
types
.
TokenCountMeta
,
groupRatioInfo
types
.
GroupRatioInfo
)
(
types
.
PriceData
,
error
)
{
func
modelPriceHelperTiered
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
promptTokens
int
,
meta
*
types
.
TokenCountMeta
,
groupRatioInfo
hosttypes
.
GroupRatioInfo
)
(
host
types
.
PriceData
,
error
)
{
exprStr
,
ok
:=
billing_setting
.
GetBillingExpr
(
info
.
OriginModelName
)
if
!
ok
{
return
types
.
PriceData
{},
fmt
.
Errorf
(
"model %s is configured as tiered_expr but has no billing expression"
,
info
.
OriginModelName
)
return
host
types
.
PriceData
{},
fmt
.
Errorf
(
"model %s is configured as tiered_expr but has no billing expression"
,
info
.
OriginModelName
)
}
estimatedCompletionTokens
:=
meta
.
MaxTokens
...
...
@@ -278,7 +279,7 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
requestInput
,
err
:=
ResolveIncomingBillingExprRequestInput
(
c
,
info
)
if
err
!=
nil
{
return
types
.
PriceData
{},
err
return
host
types
.
PriceData
{},
err
}
rawCost
,
trace
,
err
:=
billingexpr
.
RunExprWithRequest
(
exprStr
,
billingexpr
.
TokenParams
{
...
...
@@ -287,14 +288,14 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
Len
:
float64
(
promptTokens
),
},
requestInput
)
if
err
!=
nil
{
return
types
.
PriceData
{},
fmt
.
Errorf
(
"model %s tiered expr run failed: %w"
,
info
.
OriginModelName
,
err
)
return
host
types
.
PriceData
{},
fmt
.
Errorf
(
"model %s tiered expr run failed: %w"
,
info
.
OriginModelName
,
err
)
}
// Expression coefficients are $/1M tokens prices; convert to quota the same way per-call billing does.
quotaBeforeGroup
:=
rawCost
/
1
_000_000
*
common
.
QuotaPerUnit
preConsumedQuota
,
err
:=
billingexpr
.
QuotaRoundStrict
(
quotaBeforeGroup
*
groupRatioInfo
.
GroupRatio
)
if
err
!=
nil
{
return
types
.
PriceData
{},
err
return
host
types
.
PriceData
{},
err
}
freeModel
:=
false
...
...
@@ -323,7 +324,7 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
info
.
TieredBillingSnapshot
=
snapshot
info
.
BillingRequestInput
=
&
requestInput
priceData
:=
types
.
PriceData
{
priceData
:=
host
types
.
PriceData
{
FreeModel
:
freeModel
,
GroupRatioInfo
:
groupRatioInfo
,
QuotaToPreConsume
:
preConsumedQuota
,
...
...
relaykit/go.mod
View file @
b8bb3f40
...
...
@@ -5,7 +5,6 @@ go 1.25.1
require (
github.com/google/uuid v1.6.0
github.com/samber/lo v1.53.0
github.com/shopspring/decimal v1.4.0
github.com/stretchr/testify v1.11.1
github.com/tidwall/gjson v1.19.0
github.com/tidwall/sjson v1.2.5
...
...
relaykit/go.sum
View file @
b8bb3f40
...
...
@@ -18,8 +18,6 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/samber/lo v1.53.0 h1:t975lj2py4kJPQ6haz1QMgtId2gtmfktACxIXArw3HM=
github.com/samber/lo v1.53.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
...
...
service/log_info_generate.go
View file @
b8bb3f40
...
...
@@ -12,6 +12,7 @@ import (
relaycommon
"github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/relaykit/dto"
"github.com/QuantumNous/new-api/relaykit/types"
hosttypes
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
)
...
...
@@ -289,7 +290,7 @@ func GenerateClaudeOtherInfo(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
return
info
}
func
GenerateMjOtherInfo
(
relayInfo
*
relaycommon
.
RelayInfo
,
priceData
types
.
PriceData
)
map
[
string
]
interface
{}
{
func
GenerateMjOtherInfo
(
relayInfo
*
relaycommon
.
RelayInfo
,
priceData
host
types
.
PriceData
)
map
[
string
]
interface
{}
{
other
:=
make
(
map
[
string
]
interface
{})
other
[
"model_price"
]
=
priceData
.
ModelPrice
other
[
"group_ratio"
]
=
priceData
.
GroupRatioInfo
.
GroupRatio
...
...
service/quota.go
View file @
b8bb3f40
...
...
@@ -15,8 +15,8 @@ import (
perfmetrics
"github.com/QuantumNous/new-api/pkg/perf_metrics"
relaycommon
"github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/relaykit/dto"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/setting/ratio_setting"
"github.com/QuantumNous/new-api/types"
"github.com/bytedance/gopkg/util/gopool"
...
...
service/task_billing.go
View file @
b8bb3f40
...
...
@@ -10,8 +10,8 @@ import (
"github.com/QuantumNous/new-api/logger"
"github.com/QuantumNous/new-api/model"
relaycommon
"github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/setting/ratio_setting"
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
)
...
...
service/task_billing_test.go
View file @
b8bb3f40
...
...
@@ -12,7 +12,7 @@ import (
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/model"
relaycommon
"github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/
relaykit/
types"
"github.com/QuantumNous/new-api/types"
"github.com/glebarez/sqlite"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
...
...
service/text_quota_test.go
View file @
b8bb3f40
...
...
@@ -14,6 +14,7 @@ import (
"github.com/QuantumNous/new-api/relaykit/dto"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/setting/operation_setting"
hosttypes
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/shopspring/decimal"
...
...
@@ -37,14 +38,14 @@ func TestCalculateTextQuotaSummaryUnifiedForClaudeSemantic(t *testing.T) {
ClaudeCacheCreation1hTokens
:
20
,
}
priceData
:=
types
.
PriceData
{
priceData
:=
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
2
,
CacheRatio
:
0.1
,
CacheCreationRatio
:
1.25
,
CacheCreation5mRatio
:
1.25
,
CacheCreation1hRatio
:
2
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
,
},
}
...
...
@@ -83,14 +84,14 @@ func TestCalculateTextQuotaSummaryUsesSplitClaudeCacheCreationRatios(t *testing.
RelayFormat
:
types
.
RelayFormatOpenAI
,
FinalRequestRelayFormat
:
types
.
RelayFormatClaude
,
OriginModelName
:
"claude-3-7-sonnet"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
1
,
CacheRatio
:
0
,
CacheCreationRatio
:
1
,
CacheCreation5mRatio
:
2
,
CacheCreation1hRatio
:
3
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
,
},
},
...
...
@@ -121,14 +122,14 @@ func TestCalculateTextQuotaSummaryUsesAnthropicUsageSemanticFromUpstreamUsage(t
relayInfo
:=
&
relaycommon
.
RelayInfo
{
RelayFormat
:
types
.
RelayFormatOpenAI
,
OriginModelName
:
"claude-3-7-sonnet"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
2
,
CacheRatio
:
0.1
,
CacheCreationRatio
:
1.25
,
CacheCreation5mRatio
:
1.25
,
CacheCreation1hRatio
:
2
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
,
},
},
...
...
@@ -162,14 +163,14 @@ func TestCalculateTextQuotaSummaryUsesClaudeBillingUsageBeforeTopLevelUsage(t *t
relayInfo
:=
&
relaycommon
.
RelayInfo
{
RelayFormat
:
types
.
RelayFormatOpenAI
,
OriginModelName
:
"claude-3-7-sonnet"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
2
,
CacheRatio
:
0.1
,
CacheCreationRatio
:
1.25
,
CacheCreation5mRatio
:
1.25
,
CacheCreation1hRatio
:
2
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
StartTime
:
time
.
Now
(),
}
...
...
@@ -211,11 +212,11 @@ func TestCalculateTextQuotaSummaryUsesGeminiBillingUsageBeforeTopLevelUsage(t *t
relayInfo
:=
&
relaycommon
.
RelayInfo
{
RelayFormat
:
types
.
RelayFormatOpenAI
,
OriginModelName
:
"gemini-2.5-flash"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
2
,
CacheRatio
:
0.1
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
StartTime
:
time
.
Now
(),
}
...
...
@@ -253,10 +254,10 @@ func TestCalculateTextQuotaSummaryUsesOpenAIBillingUsageBeforeTopLevelUsage(t *t
relayInfo
:=
&
relaycommon
.
RelayInfo
{
RelayFormat
:
types
.
RelayFormatClaude
,
OriginModelName
:
"gpt-4o"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
2
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
StartTime
:
time
.
Now
(),
}
...
...
@@ -352,14 +353,14 @@ func TestCalculateTextQuotaSummaryHandlesLegacyClaudeDerivedOpenAIUsage(t *testi
relayInfo
:=
&
relaycommon
.
RelayInfo
{
RelayFormat
:
types
.
RelayFormatOpenAI
,
OriginModelName
:
"claude-3-7-sonnet"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
5
,
CacheRatio
:
0.1
,
CacheCreationRatio
:
1.25
,
CacheCreation5mRatio
:
1.25
,
CacheCreation1hRatio
:
2
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
StartTime
:
time
.
Now
(),
}
...
...
@@ -387,12 +388,12 @@ func TestCalculateTextQuotaSummaryBillsOpenAICacheWriteTokens(t *testing.T) {
relayInfo
:=
&
relaycommon
.
RelayInfo
{
RelayFormat
:
types
.
RelayFormatOpenAI
,
OriginModelName
:
"gpt-5.1"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
2
,
CacheRatio
:
0.1
,
CacheCreationRatio
:
1.25
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
StartTime
:
time
.
Now
(),
}
...
...
@@ -445,12 +446,12 @@ func TestCalculateTextQuotaSummarySeparatesOpenRouterCacheReadFromPromptBilling(
ChannelMeta
:
&
relaycommon
.
ChannelMeta
{
ChannelType
:
constant
.
ChannelTypeOpenRouter
,
},
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
1
,
CacheRatio
:
0.1
,
CacheCreationRatio
:
1.25
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
StartTime
:
time
.
Now
(),
}
...
...
@@ -482,11 +483,11 @@ func TestCalculateTextQuotaSummarySeparatesOpenRouterCacheCreationFromPromptBill
ChannelMeta
:
&
relaycommon
.
ChannelMeta
{
ChannelType
:
constant
.
ChannelTypeOpenRouter
,
},
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
1
,
CacheCreationRatio
:
1.25
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
StartTime
:
time
.
Now
(),
}
...
...
@@ -518,12 +519,12 @@ func TestCalculateTextQuotaSummaryKeepsPrePRClaudeOpenRouterBilling(t *testing.T
ChannelMeta
:
&
relaycommon
.
ChannelMeta
{
ChannelType
:
constant
.
ChannelTypeOpenRouter
,
},
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
1
,
CacheRatio
:
0.1
,
CacheCreationRatio
:
1.25
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
StartTime
:
time
.
Now
(),
}
...
...
@@ -559,10 +560,10 @@ func TestComposeTieredTextQuotaKeepsToolCallSurcharges(t *testing.T) {
relayInfo
:=
&
relaycommon
.
RelayInfo
{
OriginModelName
:
"o1"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
1
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
ResponsesUsageInfo
:
&
relaycommon
.
ResponsesUsageInfo
{
BuiltInTools
:
map
[
string
]
*
relaycommon
.
BuildInToolInfo
{
...
...
@@ -609,10 +610,10 @@ func TestComposeTieredTextQuotaFallbackKeepsToolCallSurcharges(t *testing.T) {
relayInfo
:=
&
relaycommon
.
RelayInfo
{
OriginModelName
:
"claude-3-7-sonnet"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
1
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1.25
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1.25
},
},
TieredBillingSnapshot
:
&
billingexpr
.
BillingSnapshot
{
BillingMode
:
"tiered_expr"
,
...
...
@@ -643,10 +644,10 @@ func TestComposeTieredTextQuotaErrorFallbackUsesPreConsumedQuota(t *testing.T) {
relayInfo
:=
&
relaycommon
.
RelayInfo
{
OriginModelName
:
"claude-3-7-sonnet"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
1
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1.25
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1.25
},
},
TieredBillingSnapshot
:
&
billingexpr
.
BillingSnapshot
{
BillingMode
:
"tiered_expr"
,
...
...
@@ -726,10 +727,10 @@ func TestTryTieredSettleNoClampInRange(t *testing.T) {
func
TestCalculateTextQuotaSummaryFixedPriceAppliesImageCountOnceAndAllowsOverride
(
t
*
testing
.
T
)
{
gin
.
SetMode
(
gin
.
TestMode
)
ctx
,
_
:=
gin
.
CreateTestContext
(
httptest
.
NewRecorder
())
priceData
:=
types
.
PriceData
{
priceData
:=
host
types
.
PriceData
{
ModelPrice
:
0.12
,
UsePrice
:
true
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
,
},
}
...
...
@@ -911,10 +912,10 @@ func TestCalculateTextQuotaSummaryDoesNotApplyRequestMultipliersToToolSurcharge(
relayInfo
:=
&
relaycommon
.
RelayInfo
{
OriginModelName
:
"o1"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
CompletionRatio
:
1
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
ResponsesUsageInfo
:
&
relaycommon
.
ResponsesUsageInfo
{
BuiltInTools
:
map
[
string
]
*
relaycommon
.
BuildInToolInfo
{
...
...
setting/ratio_setting/cache_ratio.go
View file @
b8bb3f40
package
ratio_setting
import
(
"github.com/QuantumNous/new-api/
relaykit/
types"
"github.com/QuantumNous/new-api/types"
)
var
defaultCacheRatio
=
map
[
string
]
float64
{
...
...
setting/ratio_setting/group_ratio.go
View file @
b8bb3f40
...
...
@@ -5,8 +5,8 @@ import (
"errors"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/setting/config"
"github.com/QuantumNous/new-api/types"
)
var
defaultGroupRatio
=
map
[
string
]
float64
{
...
...
setting/ratio_setting/model_ratio.go
View file @
b8bb3f40
...
...
@@ -4,8 +4,8 @@ import (
"strings"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/setting/operation_setting"
"github.com/QuantumNous/new-api/types"
)
// from songquanpeng/one-api
...
...
relaykit/
types/price_data.go
→
types/price_data.go
View file @
b8bb3f40
...
...
@@ -104,8 +104,6 @@ func (p *PriceData) RemoveOtherRatiosFromFloat(value float64) float64 {
}
func
isValidOtherRatio
(
ratio
float64
)
bool
{
// NaN/Inf would poison every downstream quota multiplication
// (int(NaN * quota) wraps to a negative charge).
return
ratio
>
0
&&
!
math
.
IsInf
(
ratio
,
1
)
}
...
...
relaykit/
types/rw_map.go
→
types/rw_map.go
View file @
b8bb3f40
...
...
@@ -3,7 +3,7 @@ package types
import
(
"sync"
kitutil
"github.com/QuantumNous/new-api/relaykit/relayconvert/kitutil
"
"github.com/QuantumNous/new-api/common
"
)
type
RWMap
[
K
comparable
,
V
any
]
struct
{
...
...
@@ -15,13 +15,13 @@ func (m *RWMap[K, V]) UnmarshalJSON(b []byte) error {
m
.
mutex
.
Lock
()
defer
m
.
mutex
.
Unlock
()
m
.
data
=
make
(
map
[
K
]
V
)
return
kitutil
.
Unmarshal
(
b
,
&
m
.
data
)
return
common
.
Unmarshal
(
b
,
&
m
.
data
)
}
func
(
m
*
RWMap
[
K
,
V
])
MarshalJSON
()
([]
byte
,
error
)
{
m
.
mutex
.
RLock
()
defer
m
.
mutex
.
RUnlock
()
return
kitutil
.
Marshal
(
m
.
data
)
return
common
.
Marshal
(
m
.
data
)
}
func
NewRWMap
[
K
comparable
,
V
any
]()
*
RWMap
[
K
,
V
]
{
...
...
@@ -57,7 +57,6 @@ func (m *RWMap[K, V]) Clear() {
m
.
data
=
make
(
map
[
K
]
V
)
}
// ReadAll returns a copy of the entire map.
func
(
m
*
RWMap
[
K
,
V
])
ReadAll
()
map
[
K
]
V
{
m
.
mutex
.
RLock
()
defer
m
.
mutex
.
RUnlock
()
...
...
@@ -78,22 +77,20 @@ func LoadFromJsonString[K comparable, V any](m *RWMap[K, V], jsonStr string) err
m
.
mutex
.
Lock
()
defer
m
.
mutex
.
Unlock
()
m
.
data
=
make
(
map
[
K
]
V
)
return
kitutil
.
Unmarshal
([]
byte
(
jsonStr
),
&
m
.
data
)
return
common
.
Unmarshal
([]
byte
(
jsonStr
),
&
m
.
data
)
}
// LoadFromJsonStringWithCallback loads a JSON string into the RWMap and calls the callback on success.
func
LoadFromJsonStringWithCallback
[
K
comparable
,
V
any
](
m
*
RWMap
[
K
,
V
],
jsonStr
string
,
onSuccess
func
())
error
{
m
.
mutex
.
Lock
()
defer
m
.
mutex
.
Unlock
()
m
.
data
=
make
(
map
[
K
]
V
)
err
:=
kitutil
.
Unmarshal
([]
byte
(
jsonStr
),
&
m
.
data
)
err
:=
common
.
Unmarshal
([]
byte
(
jsonStr
),
&
m
.
data
)
if
err
==
nil
&&
onSuccess
!=
nil
{
onSuccess
()
}
return
err
}
// MarshalJSONString returns the JSON string representation of the RWMap.
func
(
m
*
RWMap
[
K
,
V
])
MarshalJSONString
()
string
{
bytes
,
err
:=
m
.
MarshalJSON
()
if
err
!=
nil
{
...
...
relaykit/
types/set.go
→
types/set.go
View file @
b8bb3f40
...
...
@@ -4,7 +4,6 @@ type Set[T comparable] struct {
items
map
[
T
]
struct
{}
}
// NewSet 创建并返回一个新的 Set
func
NewSet
[
T
comparable
]()
*
Set
[
T
]
{
return
&
Set
[
T
]{
items
:
make
(
map
[
T
]
struct
{}),
...
...
@@ -15,24 +14,19 @@ func (s *Set[T]) Add(item T) {
s
.
items
[
item
]
=
struct
{}{}
}
// Remove 从 Set 中移除一个元素
func
(
s
*
Set
[
T
])
Remove
(
item
T
)
{
delete
(
s
.
items
,
item
)
}
// Contains 检查 Set 是否包含某个元素
func
(
s
*
Set
[
T
])
Contains
(
item
T
)
bool
{
_
,
exists
:=
s
.
items
[
item
]
return
exists
}
// Len 返回 Set 中元素的数量
func
(
s
*
Set
[
T
])
Len
()
int
{
return
len
(
s
.
items
)
}
// Items 返回 Set 中所有元素组成的切片
// 注意:由于 map 的无序性,返回的切片元素顺序是随机的
func
(
s
*
Set
[
T
])
Items
()
[]
T
{
items
:=
make
([]
T
,
0
,
s
.
Len
())
for
item
:=
range
s
.
items
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment