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
Show 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 (
...
@@ -28,6 +28,7 @@ import (
"github.com/QuantumNous/new-api/service"
"github.com/QuantumNous/new-api/service"
"github.com/QuantumNous/new-api/setting/operation_setting"
"github.com/QuantumNous/new-api/setting/operation_setting"
"github.com/QuantumNous/new-api/setting/ratio_setting"
"github.com/QuantumNous/new-api/setting/ratio_setting"
hosttypes
"github.com/QuantumNous/new-api/types"
"github.com/samber/lo"
"github.com/samber/lo"
"github.com/tidwall/gjson"
"github.com/tidwall/gjson"
...
@@ -530,7 +531,7 @@ func attachTestBillingRequestInput(info *relaycommon.RelayInfo, request dto.Requ
...
@@ -530,7 +531,7 @@ func attachTestBillingRequestInput(info *relaycommon.RelayInfo, request dto.Requ
return
nil
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
{
if
usage
!=
nil
&&
info
!=
nil
&&
info
.
TieredBillingSnapshot
!=
nil
{
isClaudeUsageSemantic
:=
usage
.
UsageSemantic
==
"anthropic"
||
info
.
GetFinalRequestRelayFormat
()
==
types
.
RelayFormatClaude
isClaudeUsageSemantic
:=
usage
.
UsageSemantic
==
"anthropic"
||
info
.
GetFinalRequestRelayFormat
()
==
types
.
RelayFormatClaude
usedVars
:=
billingexpr
.
UsedVars
(
info
.
TieredBillingSnapshot
.
ExprString
)
usedVars
:=
billingexpr
.
UsedVars
(
info
.
TieredBillingSnapshot
.
ExprString
)
...
@@ -552,7 +553,7 @@ func settleTestQuota(info *relaycommon.RelayInfo, priceData types.PriceData, usa
...
@@ -552,7 +553,7 @@ func settleTestQuota(info *relaycommon.RelayInfo, priceData types.PriceData, usa
return
int
(
priceData
.
ModelPrice
*
common
.
QuotaPerUnit
),
nil
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
,
other
:=
service
.
GenerateTextOtherInfo
(
c
,
info
,
priceData
.
ModelRatio
,
priceData
.
GroupRatioInfo
.
GroupRatio
,
priceData
.
CompletionRatio
,
usage
.
PromptTokensDetails
.
CachedTokens
,
priceData
.
CacheRatio
,
priceData
.
ModelPrice
,
priceData
.
GroupRatioInfo
.
GroupSpecialRatio
)
usage
.
PromptTokensDetails
.
CachedTokens
,
priceData
.
CacheRatio
,
priceData
.
ModelPrice
,
priceData
.
GroupRatioInfo
.
GroupSpecialRatio
)
if
tieredResult
!=
nil
{
if
tieredResult
!=
nil
{
...
...
controller/channel_test_internal_test.go
View file @
b8bb3f40
...
@@ -13,9 +13,9 @@ import (
...
@@ -13,9 +13,9 @@ import (
"github.com/QuantumNous/new-api/pkg/billingexpr"
"github.com/QuantumNous/new-api/pkg/billingexpr"
relaycommon
"github.com/QuantumNous/new-api/relay/common"
relaycommon
"github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/relaykit/dto"
"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/service"
"github.com/QuantumNous/new-api/setting/operation_setting"
"github.com/QuantumNous/new-api/setting/operation_setting"
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/require"
...
...
controller/task.go
View file @
b8bb3f40
...
@@ -8,7 +8,7 @@ import (
...
@@ -8,7 +8,7 @@ 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/relay"
"github.com/QuantumNous/new-api/relay"
"github.com/QuantumNous/new-api/
relaykit/
types"
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
)
)
...
...
model/log.go
View file @
b8bb3f40
...
@@ -9,7 +9,7 @@ import (
...
@@ -9,7 +9,7 @@ import (
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/logger"
"github.com/QuantumNous/new-api/logger"
"github.com/QuantumNous/new-api/
relaykit/
types"
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
...
...
model/pricing.go
View file @
b8bb3f40
...
@@ -10,9 +10,9 @@ import (
...
@@ -10,9 +10,9 @@ import (
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/relaykit/dto"
"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/billing_setting"
"github.com/QuantumNous/new-api/setting/ratio_setting"
"github.com/QuantumNous/new-api/setting/ratio_setting"
"github.com/QuantumNous/new-api/types"
)
)
type
Pricing
struct
{
type
Pricing
struct
{
...
...
relay/chat_completions_via_responses_test.go
View file @
b8bb3f40
...
@@ -5,7 +5,7 @@ import (
...
@@ -5,7 +5,7 @@ import (
"testing"
"testing"
relaycommon
"github.com/QuantumNous/new-api/relay/common"
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/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/require"
)
)
...
...
relay/common/relay_info.go
View file @
b8bb3f40
...
@@ -16,6 +16,7 @@ import (
...
@@ -16,6 +16,7 @@ import (
"github.com/QuantumNous/new-api/relaykit/relayconvert/convmeta"
"github.com/QuantumNous/new-api/relaykit/relayconvert/convmeta"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/setting/model_setting"
"github.com/QuantumNous/new-api/setting/model_setting"
hosttypes
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/gorilla/websocket"
...
@@ -155,7 +156,7 @@ type RelayInfo struct {
...
@@ -155,7 +156,7 @@ type RelayInfo struct {
// *bytes.Reader/Buffer/strings.Reader). 0 means "let net/http decide".
// *bytes.Reader/Buffer/strings.Reader). 0 means "let net/http decide".
UpstreamRequestBodySize
int64
UpstreamRequestBodySize
int64
PriceData
types
.
PriceData
PriceData
host
types
.
PriceData
// QuotaClamp is set (non-nil) when a quota conversion saturated at the
// QuotaClamp is set (non-nil) when a quota conversion saturated at the
// int32 bound (or NaN fallback) while computing this request's charge.
// int32 bound (or NaN fallback) while computing this request's charge.
...
...
relay/helper/price.go
View file @
b8bb3f40
...
@@ -13,6 +13,7 @@ import (
...
@@ -13,6 +13,7 @@ import (
"github.com/QuantumNous/new-api/setting/billing_setting"
"github.com/QuantumNous/new-api/setting/billing_setting"
"github.com/QuantumNous/new-api/setting/operation_setting"
"github.com/QuantumNous/new-api/setting/operation_setting"
"github.com/QuantumNous/new-api/setting/ratio_setting"
"github.com/QuantumNous/new-api/setting/ratio_setting"
hosttypes
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
)
)
...
@@ -41,8 +42,8 @@ const claudeCacheCreation1hMultiplier = 6 / 3.75
...
@@ -41,8 +42,8 @@ const claudeCacheCreation1hMultiplier = 6 / 3.75
const
defaultTieredPreConsumeMaxTokens
=
8192
const
defaultTieredPreConsumeMaxTokens
=
8192
// HandleGroupRatio checks for "auto_group" in the context and updates the group ratio and relayInfo.UsingGroup if present
// 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
{
func
HandleGroupRatio
(
ctx
*
gin
.
Context
,
relayInfo
*
relaycommon
.
RelayInfo
)
host
types
.
GroupRatioInfo
{
groupRatioInfo
:=
types
.
GroupRatioInfo
{
groupRatioInfo
:=
host
types
.
GroupRatioInfo
{
GroupRatio
:
1.0
,
// default ratio
GroupRatio
:
1.0
,
// default ratio
GroupSpecialRatio
:
-
1
,
GroupSpecialRatio
:
-
1
,
}
}
...
@@ -69,7 +70,7 @@ func HandleGroupRatio(ctx *gin.Context, relayInfo *relaycommon.RelayInfo) types.
...
@@ -69,7 +70,7 @@ func HandleGroupRatio(ctx *gin.Context, relayInfo *relaycommon.RelayInfo) types.
return
groupRatioInfo
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
)
modelPrice
,
usePrice
:=
ratio_setting
.
GetModelPrice
(
info
.
OriginModelName
,
false
)
groupRatioInfo
:=
HandleGroupRatio
(
c
,
info
)
groupRatioInfo
:=
HandleGroupRatio
(
c
,
info
)
...
@@ -104,7 +105,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
...
@@ -104,7 +105,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
acceptUnsetRatio
=
true
acceptUnsetRatio
=
true
}
}
if
!
acceptUnsetRatio
{
if
!
acceptUnsetRatio
{
return
types
.
PriceData
{},
modelPriceNotConfiguredError
(
matchName
,
info
.
UserId
)
return
host
types
.
PriceData
{},
modelPriceNotConfiguredError
(
matchName
,
info
.
UserId
)
}
}
}
}
completionRatio
=
ratio_setting
.
GetCompletionRatio
(
info
.
OriginModelName
)
completionRatio
=
ratio_setting
.
GetCompletionRatio
(
info
.
OriginModelName
)
...
@@ -119,7 +120,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
...
@@ -119,7 +120,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
ratio
:=
modelRatio
*
groupRatioInfo
.
GroupRatio
ratio
:=
modelRatio
*
groupRatioInfo
.
GroupRatio
quota
,
err
:=
common
.
QuotaFromFloatStrict
(
float64
(
preConsumedTokens
)
*
ratio
)
quota
,
err
:=
common
.
QuotaFromFloatStrict
(
float64
(
preConsumedTokens
)
*
ratio
)
if
err
!=
nil
{
if
err
!=
nil
{
return
types
.
PriceData
{},
err
return
host
types
.
PriceData
{},
err
}
}
preConsumedQuota
=
quota
preConsumedQuota
=
quota
}
else
{
}
else
{
...
@@ -147,7 +148,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
...
@@ -147,7 +148,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
}
}
}
}
priceData
:=
types
.
PriceData
{
priceData
:=
host
types
.
PriceData
{
FreeModel
:
freeModel
,
FreeModel
:
freeModel
,
ModelPrice
:
modelPrice
,
ModelPrice
:
modelPrice
,
ModelRatio
:
modelRatio
,
ModelRatio
:
modelRatio
,
...
@@ -170,7 +171,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
...
@@ -170,7 +171,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
quotaToPreConsume
:=
priceData
.
ApplyOtherRatiosToFloat
(
modelPrice
*
common
.
QuotaPerUnit
*
groupRatioInfo
.
GroupRatio
)
quotaToPreConsume
:=
priceData
.
ApplyOtherRatiosToFloat
(
modelPrice
*
common
.
QuotaPerUnit
*
groupRatioInfo
.
GroupRatio
)
quota
,
err
:=
common
.
QuotaFromFloatStrict
(
quotaToPreConsume
)
quota
,
err
:=
common
.
QuotaFromFloatStrict
(
quotaToPreConsume
)
if
err
!=
nil
{
if
err
!=
nil
{
return
types
.
PriceData
{},
err
return
host
types
.
PriceData
{},
err
}
}
priceData
.
QuotaToPreConsume
=
quota
priceData
.
QuotaToPreConsume
=
quota
}
}
...
@@ -183,7 +184,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
...
@@ -183,7 +184,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
}
}
// ModelPriceHelperPerCall 按次/按量计费的 PriceHelper (MJ、Task)
// 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
)
groupRatioInfo
:=
HandleGroupRatio
(
c
,
info
)
modelPrice
,
success
:=
ratio_setting
.
GetModelPrice
(
info
.
OriginModelName
,
true
)
modelPrice
,
success
:=
ratio_setting
.
GetModelPrice
(
info
.
OriginModelName
,
true
)
...
@@ -204,7 +205,7 @@ func ModelPriceHelperPerCall(c *gin.Context, info *relaycommon.RelayInfo) (types
...
@@ -204,7 +205,7 @@ func ModelPriceHelperPerCall(c *gin.Context, info *relaycommon.RelayInfo) (types
acceptUnsetRatio
=
true
acceptUnsetRatio
=
true
}
}
if
!
ratioSuccess
&&
!
acceptUnsetRatio
{
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
...
@@ -216,7 +217,7 @@ func ModelPriceHelperPerCall(c *gin.Context, info *relaycommon.RelayInfo) (types
var
err
error
var
err
error
quota
,
err
=
common
.
QuotaFromFloatStrict
(
modelPrice
*
common
.
QuotaPerUnit
*
groupRatioInfo
.
GroupRatio
)
quota
,
err
=
common
.
QuotaFromFloatStrict
(
modelPrice
*
common
.
QuotaPerUnit
*
groupRatioInfo
.
GroupRatio
)
if
err
!=
nil
{
if
err
!=
nil
{
return
types
.
PriceData
{},
err
return
host
types
.
PriceData
{},
err
}
}
if
!
operation_setting
.
GetQuotaSetting
()
.
EnableFreeModelPreConsume
{
if
!
operation_setting
.
GetQuotaSetting
()
.
EnableFreeModelPreConsume
{
if
groupRatioInfo
.
GroupRatio
==
0
||
modelPrice
==
0
{
if
groupRatioInfo
.
GroupRatio
==
0
||
modelPrice
==
0
{
...
@@ -229,7 +230,7 @@ func ModelPriceHelperPerCall(c *gin.Context, info *relaycommon.RelayInfo) (types
...
@@ -229,7 +230,7 @@ func ModelPriceHelperPerCall(c *gin.Context, info *relaycommon.RelayInfo) (types
var
err
error
var
err
error
quota
,
err
=
common
.
QuotaFromFloatStrict
(
modelRatio
/
2
*
common
.
QuotaPerUnit
*
groupRatioInfo
.
GroupRatio
)
quota
,
err
=
common
.
QuotaFromFloatStrict
(
modelRatio
/
2
*
common
.
QuotaPerUnit
*
groupRatioInfo
.
GroupRatio
)
if
err
!=
nil
{
if
err
!=
nil
{
return
types
.
PriceData
{},
err
return
host
types
.
PriceData
{},
err
}
}
modelPrice
=
-
1
modelPrice
=
-
1
if
!
operation_setting
.
GetQuotaSetting
()
.
EnableFreeModelPreConsume
{
if
!
operation_setting
.
GetQuotaSetting
()
.
EnableFreeModelPreConsume
{
...
@@ -240,7 +241,7 @@ func ModelPriceHelperPerCall(c *gin.Context, info *relaycommon.RelayInfo) (types
...
@@ -240,7 +241,7 @@ func ModelPriceHelperPerCall(c *gin.Context, info *relaycommon.RelayInfo) (types
}
}
}
}
priceData
:=
types
.
PriceData
{
priceData
:=
host
types
.
PriceData
{
FreeModel
:
freeModel
,
FreeModel
:
freeModel
,
ModelPrice
:
modelPrice
,
ModelPrice
:
modelPrice
,
ModelRatio
:
modelRatio
,
ModelRatio
:
modelRatio
,
...
@@ -265,10 +266,10 @@ func HasModelBillingConfig(modelName string) bool {
...
@@ -265,10 +266,10 @@ func HasModelBillingConfig(modelName string) bool {
return
ok
&&
strings
.
TrimSpace
(
expr
)
!=
""
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
)
exprStr
,
ok
:=
billing_setting
.
GetBillingExpr
(
info
.
OriginModelName
)
if
!
ok
{
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
estimatedCompletionTokens
:=
meta
.
MaxTokens
...
@@ -278,7 +279,7 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
...
@@ -278,7 +279,7 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
requestInput
,
err
:=
ResolveIncomingBillingExprRequestInput
(
c
,
info
)
requestInput
,
err
:=
ResolveIncomingBillingExprRequestInput
(
c
,
info
)
if
err
!=
nil
{
if
err
!=
nil
{
return
types
.
PriceData
{},
err
return
host
types
.
PriceData
{},
err
}
}
rawCost
,
trace
,
err
:=
billingexpr
.
RunExprWithRequest
(
exprStr
,
billingexpr
.
TokenParams
{
rawCost
,
trace
,
err
:=
billingexpr
.
RunExprWithRequest
(
exprStr
,
billingexpr
.
TokenParams
{
...
@@ -287,14 +288,14 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
...
@@ -287,14 +288,14 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
Len
:
float64
(
promptTokens
),
Len
:
float64
(
promptTokens
),
},
requestInput
)
},
requestInput
)
if
err
!=
nil
{
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.
// Expression coefficients are $/1M tokens prices; convert to quota the same way per-call billing does.
quotaBeforeGroup
:=
rawCost
/
1
_000_000
*
common
.
QuotaPerUnit
quotaBeforeGroup
:=
rawCost
/
1
_000_000
*
common
.
QuotaPerUnit
preConsumedQuota
,
err
:=
billingexpr
.
QuotaRoundStrict
(
quotaBeforeGroup
*
groupRatioInfo
.
GroupRatio
)
preConsumedQuota
,
err
:=
billingexpr
.
QuotaRoundStrict
(
quotaBeforeGroup
*
groupRatioInfo
.
GroupRatio
)
if
err
!=
nil
{
if
err
!=
nil
{
return
types
.
PriceData
{},
err
return
host
types
.
PriceData
{},
err
}
}
freeModel
:=
false
freeModel
:=
false
...
@@ -323,7 +324,7 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
...
@@ -323,7 +324,7 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
info
.
TieredBillingSnapshot
=
snapshot
info
.
TieredBillingSnapshot
=
snapshot
info
.
BillingRequestInput
=
&
requestInput
info
.
BillingRequestInput
=
&
requestInput
priceData
:=
types
.
PriceData
{
priceData
:=
host
types
.
PriceData
{
FreeModel
:
freeModel
,
FreeModel
:
freeModel
,
GroupRatioInfo
:
groupRatioInfo
,
GroupRatioInfo
:
groupRatioInfo
,
QuotaToPreConsume
:
preConsumedQuota
,
QuotaToPreConsume
:
preConsumedQuota
,
...
...
relaykit/go.mod
View file @
b8bb3f40
...
@@ -5,7 +5,6 @@ go 1.25.1
...
@@ -5,7 +5,6 @@ go 1.25.1
require (
require (
github.com/google/uuid v1.6.0
github.com/google/uuid v1.6.0
github.com/samber/lo v1.53.0
github.com/samber/lo v1.53.0
github.com/shopspring/decimal v1.4.0
github.com/stretchr/testify v1.11.1
github.com/stretchr/testify v1.11.1
github.com/tidwall/gjson v1.19.0
github.com/tidwall/gjson v1.19.0
github.com/tidwall/sjson v1.2.5
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
...
@@ -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/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 h1:t975lj2py4kJPQ6haz1QMgtId2gtmfktACxIXArw3HM=
github.com/samber/lo v1.53.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
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 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
...
...
service/log_info_generate.go
View file @
b8bb3f40
...
@@ -12,6 +12,7 @@ import (
...
@@ -12,6 +12,7 @@ import (
relaycommon
"github.com/QuantumNous/new-api/relay/common"
relaycommon
"github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/relaykit/dto"
"github.com/QuantumNous/new-api/relaykit/dto"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/relaykit/types"
hosttypes
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
)
)
...
@@ -289,7 +290,7 @@ func GenerateClaudeOtherInfo(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
...
@@ -289,7 +290,7 @@ func GenerateClaudeOtherInfo(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
return
info
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
:=
make
(
map
[
string
]
interface
{})
other
[
"model_price"
]
=
priceData
.
ModelPrice
other
[
"model_price"
]
=
priceData
.
ModelPrice
other
[
"group_ratio"
]
=
priceData
.
GroupRatioInfo
.
GroupRatio
other
[
"group_ratio"
]
=
priceData
.
GroupRatioInfo
.
GroupRatio
...
...
service/quota.go
View file @
b8bb3f40
...
@@ -15,8 +15,8 @@ import (
...
@@ -15,8 +15,8 @@ import (
perfmetrics
"github.com/QuantumNous/new-api/pkg/perf_metrics"
perfmetrics
"github.com/QuantumNous/new-api/pkg/perf_metrics"
relaycommon
"github.com/QuantumNous/new-api/relay/common"
relaycommon
"github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/relaykit/dto"
"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/setting/ratio_setting"
"github.com/QuantumNous/new-api/types"
"github.com/bytedance/gopkg/util/gopool"
"github.com/bytedance/gopkg/util/gopool"
...
...
service/task_billing.go
View file @
b8bb3f40
...
@@ -10,8 +10,8 @@ import (
...
@@ -10,8 +10,8 @@ import (
"github.com/QuantumNous/new-api/logger"
"github.com/QuantumNous/new-api/logger"
"github.com/QuantumNous/new-api/model"
"github.com/QuantumNous/new-api/model"
relaycommon
"github.com/QuantumNous/new-api/relay/common"
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/setting/ratio_setting"
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
)
)
...
...
service/task_billing_test.go
View file @
b8bb3f40
...
@@ -12,7 +12,7 @@ import (
...
@@ -12,7 +12,7 @@ import (
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/model"
"github.com/QuantumNous/new-api/model"
relaycommon
"github.com/QuantumNous/new-api/relay/common"
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/glebarez/sqlite"
"github.com/shopspring/decimal"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
...
...
service/text_quota_test.go
View file @
b8bb3f40
...
@@ -14,6 +14,7 @@ import (
...
@@ -14,6 +14,7 @@ import (
"github.com/QuantumNous/new-api/relaykit/dto"
"github.com/QuantumNous/new-api/relaykit/dto"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/setting/operation_setting"
"github.com/QuantumNous/new-api/setting/operation_setting"
hosttypes
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"github.com/shopspring/decimal"
"github.com/shopspring/decimal"
...
@@ -37,14 +38,14 @@ func TestCalculateTextQuotaSummaryUnifiedForClaudeSemantic(t *testing.T) {
...
@@ -37,14 +38,14 @@ func TestCalculateTextQuotaSummaryUnifiedForClaudeSemantic(t *testing.T) {
ClaudeCacheCreation1hTokens
:
20
,
ClaudeCacheCreation1hTokens
:
20
,
}
}
priceData
:=
types
.
PriceData
{
priceData
:=
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
2
,
CompletionRatio
:
2
,
CacheRatio
:
0.1
,
CacheRatio
:
0.1
,
CacheCreationRatio
:
1.25
,
CacheCreationRatio
:
1.25
,
CacheCreation5mRatio
:
1.25
,
CacheCreation5mRatio
:
1.25
,
CacheCreation1hRatio
:
2
,
CacheCreation1hRatio
:
2
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
,
GroupRatio
:
1
,
},
},
}
}
...
@@ -83,14 +84,14 @@ func TestCalculateTextQuotaSummaryUsesSplitClaudeCacheCreationRatios(t *testing.
...
@@ -83,14 +84,14 @@ func TestCalculateTextQuotaSummaryUsesSplitClaudeCacheCreationRatios(t *testing.
RelayFormat
:
types
.
RelayFormatOpenAI
,
RelayFormat
:
types
.
RelayFormatOpenAI
,
FinalRequestRelayFormat
:
types
.
RelayFormatClaude
,
FinalRequestRelayFormat
:
types
.
RelayFormatClaude
,
OriginModelName
:
"claude-3-7-sonnet"
,
OriginModelName
:
"claude-3-7-sonnet"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
1
,
CompletionRatio
:
1
,
CacheRatio
:
0
,
CacheRatio
:
0
,
CacheCreationRatio
:
1
,
CacheCreationRatio
:
1
,
CacheCreation5mRatio
:
2
,
CacheCreation5mRatio
:
2
,
CacheCreation1hRatio
:
3
,
CacheCreation1hRatio
:
3
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
,
GroupRatio
:
1
,
},
},
},
},
...
@@ -121,14 +122,14 @@ func TestCalculateTextQuotaSummaryUsesAnthropicUsageSemanticFromUpstreamUsage(t
...
@@ -121,14 +122,14 @@ func TestCalculateTextQuotaSummaryUsesAnthropicUsageSemanticFromUpstreamUsage(t
relayInfo
:=
&
relaycommon
.
RelayInfo
{
relayInfo
:=
&
relaycommon
.
RelayInfo
{
RelayFormat
:
types
.
RelayFormatOpenAI
,
RelayFormat
:
types
.
RelayFormatOpenAI
,
OriginModelName
:
"claude-3-7-sonnet"
,
OriginModelName
:
"claude-3-7-sonnet"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
2
,
CompletionRatio
:
2
,
CacheRatio
:
0.1
,
CacheRatio
:
0.1
,
CacheCreationRatio
:
1.25
,
CacheCreationRatio
:
1.25
,
CacheCreation5mRatio
:
1.25
,
CacheCreation5mRatio
:
1.25
,
CacheCreation1hRatio
:
2
,
CacheCreation1hRatio
:
2
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
,
GroupRatio
:
1
,
},
},
},
},
...
@@ -162,14 +163,14 @@ func TestCalculateTextQuotaSummaryUsesClaudeBillingUsageBeforeTopLevelUsage(t *t
...
@@ -162,14 +163,14 @@ func TestCalculateTextQuotaSummaryUsesClaudeBillingUsageBeforeTopLevelUsage(t *t
relayInfo
:=
&
relaycommon
.
RelayInfo
{
relayInfo
:=
&
relaycommon
.
RelayInfo
{
RelayFormat
:
types
.
RelayFormatOpenAI
,
RelayFormat
:
types
.
RelayFormatOpenAI
,
OriginModelName
:
"claude-3-7-sonnet"
,
OriginModelName
:
"claude-3-7-sonnet"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
2
,
CompletionRatio
:
2
,
CacheRatio
:
0.1
,
CacheRatio
:
0.1
,
CacheCreationRatio
:
1.25
,
CacheCreationRatio
:
1.25
,
CacheCreation5mRatio
:
1.25
,
CacheCreation5mRatio
:
1.25
,
CacheCreation1hRatio
:
2
,
CacheCreation1hRatio
:
2
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
},
StartTime
:
time
.
Now
(),
StartTime
:
time
.
Now
(),
}
}
...
@@ -211,11 +212,11 @@ func TestCalculateTextQuotaSummaryUsesGeminiBillingUsageBeforeTopLevelUsage(t *t
...
@@ -211,11 +212,11 @@ func TestCalculateTextQuotaSummaryUsesGeminiBillingUsageBeforeTopLevelUsage(t *t
relayInfo
:=
&
relaycommon
.
RelayInfo
{
relayInfo
:=
&
relaycommon
.
RelayInfo
{
RelayFormat
:
types
.
RelayFormatOpenAI
,
RelayFormat
:
types
.
RelayFormatOpenAI
,
OriginModelName
:
"gemini-2.5-flash"
,
OriginModelName
:
"gemini-2.5-flash"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
2
,
CompletionRatio
:
2
,
CacheRatio
:
0.1
,
CacheRatio
:
0.1
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
},
StartTime
:
time
.
Now
(),
StartTime
:
time
.
Now
(),
}
}
...
@@ -253,10 +254,10 @@ func TestCalculateTextQuotaSummaryUsesOpenAIBillingUsageBeforeTopLevelUsage(t *t
...
@@ -253,10 +254,10 @@ func TestCalculateTextQuotaSummaryUsesOpenAIBillingUsageBeforeTopLevelUsage(t *t
relayInfo
:=
&
relaycommon
.
RelayInfo
{
relayInfo
:=
&
relaycommon
.
RelayInfo
{
RelayFormat
:
types
.
RelayFormatClaude
,
RelayFormat
:
types
.
RelayFormatClaude
,
OriginModelName
:
"gpt-4o"
,
OriginModelName
:
"gpt-4o"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
2
,
CompletionRatio
:
2
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
},
StartTime
:
time
.
Now
(),
StartTime
:
time
.
Now
(),
}
}
...
@@ -352,14 +353,14 @@ func TestCalculateTextQuotaSummaryHandlesLegacyClaudeDerivedOpenAIUsage(t *testi
...
@@ -352,14 +353,14 @@ func TestCalculateTextQuotaSummaryHandlesLegacyClaudeDerivedOpenAIUsage(t *testi
relayInfo
:=
&
relaycommon
.
RelayInfo
{
relayInfo
:=
&
relaycommon
.
RelayInfo
{
RelayFormat
:
types
.
RelayFormatOpenAI
,
RelayFormat
:
types
.
RelayFormatOpenAI
,
OriginModelName
:
"claude-3-7-sonnet"
,
OriginModelName
:
"claude-3-7-sonnet"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
5
,
CompletionRatio
:
5
,
CacheRatio
:
0.1
,
CacheRatio
:
0.1
,
CacheCreationRatio
:
1.25
,
CacheCreationRatio
:
1.25
,
CacheCreation5mRatio
:
1.25
,
CacheCreation5mRatio
:
1.25
,
CacheCreation1hRatio
:
2
,
CacheCreation1hRatio
:
2
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
},
StartTime
:
time
.
Now
(),
StartTime
:
time
.
Now
(),
}
}
...
@@ -387,12 +388,12 @@ func TestCalculateTextQuotaSummaryBillsOpenAICacheWriteTokens(t *testing.T) {
...
@@ -387,12 +388,12 @@ func TestCalculateTextQuotaSummaryBillsOpenAICacheWriteTokens(t *testing.T) {
relayInfo
:=
&
relaycommon
.
RelayInfo
{
relayInfo
:=
&
relaycommon
.
RelayInfo
{
RelayFormat
:
types
.
RelayFormatOpenAI
,
RelayFormat
:
types
.
RelayFormatOpenAI
,
OriginModelName
:
"gpt-5.1"
,
OriginModelName
:
"gpt-5.1"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
2
,
CompletionRatio
:
2
,
CacheRatio
:
0.1
,
CacheRatio
:
0.1
,
CacheCreationRatio
:
1.25
,
CacheCreationRatio
:
1.25
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
},
StartTime
:
time
.
Now
(),
StartTime
:
time
.
Now
(),
}
}
...
@@ -445,12 +446,12 @@ func TestCalculateTextQuotaSummarySeparatesOpenRouterCacheReadFromPromptBilling(
...
@@ -445,12 +446,12 @@ func TestCalculateTextQuotaSummarySeparatesOpenRouterCacheReadFromPromptBilling(
ChannelMeta
:
&
relaycommon
.
ChannelMeta
{
ChannelMeta
:
&
relaycommon
.
ChannelMeta
{
ChannelType
:
constant
.
ChannelTypeOpenRouter
,
ChannelType
:
constant
.
ChannelTypeOpenRouter
,
},
},
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
1
,
CompletionRatio
:
1
,
CacheRatio
:
0.1
,
CacheRatio
:
0.1
,
CacheCreationRatio
:
1.25
,
CacheCreationRatio
:
1.25
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
},
StartTime
:
time
.
Now
(),
StartTime
:
time
.
Now
(),
}
}
...
@@ -482,11 +483,11 @@ func TestCalculateTextQuotaSummarySeparatesOpenRouterCacheCreationFromPromptBill
...
@@ -482,11 +483,11 @@ func TestCalculateTextQuotaSummarySeparatesOpenRouterCacheCreationFromPromptBill
ChannelMeta
:
&
relaycommon
.
ChannelMeta
{
ChannelMeta
:
&
relaycommon
.
ChannelMeta
{
ChannelType
:
constant
.
ChannelTypeOpenRouter
,
ChannelType
:
constant
.
ChannelTypeOpenRouter
,
},
},
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
1
,
CompletionRatio
:
1
,
CacheCreationRatio
:
1.25
,
CacheCreationRatio
:
1.25
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
},
StartTime
:
time
.
Now
(),
StartTime
:
time
.
Now
(),
}
}
...
@@ -518,12 +519,12 @@ func TestCalculateTextQuotaSummaryKeepsPrePRClaudeOpenRouterBilling(t *testing.T
...
@@ -518,12 +519,12 @@ func TestCalculateTextQuotaSummaryKeepsPrePRClaudeOpenRouterBilling(t *testing.T
ChannelMeta
:
&
relaycommon
.
ChannelMeta
{
ChannelMeta
:
&
relaycommon
.
ChannelMeta
{
ChannelType
:
constant
.
ChannelTypeOpenRouter
,
ChannelType
:
constant
.
ChannelTypeOpenRouter
,
},
},
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
1
,
CompletionRatio
:
1
,
CacheRatio
:
0.1
,
CacheRatio
:
0.1
,
CacheCreationRatio
:
1.25
,
CacheCreationRatio
:
1.25
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
},
StartTime
:
time
.
Now
(),
StartTime
:
time
.
Now
(),
}
}
...
@@ -559,10 +560,10 @@ func TestComposeTieredTextQuotaKeepsToolCallSurcharges(t *testing.T) {
...
@@ -559,10 +560,10 @@ func TestComposeTieredTextQuotaKeepsToolCallSurcharges(t *testing.T) {
relayInfo
:=
&
relaycommon
.
RelayInfo
{
relayInfo
:=
&
relaycommon
.
RelayInfo
{
OriginModelName
:
"o1"
,
OriginModelName
:
"o1"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
1
,
CompletionRatio
:
1
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
},
ResponsesUsageInfo
:
&
relaycommon
.
ResponsesUsageInfo
{
ResponsesUsageInfo
:
&
relaycommon
.
ResponsesUsageInfo
{
BuiltInTools
:
map
[
string
]
*
relaycommon
.
BuildInToolInfo
{
BuiltInTools
:
map
[
string
]
*
relaycommon
.
BuildInToolInfo
{
...
@@ -609,10 +610,10 @@ func TestComposeTieredTextQuotaFallbackKeepsToolCallSurcharges(t *testing.T) {
...
@@ -609,10 +610,10 @@ func TestComposeTieredTextQuotaFallbackKeepsToolCallSurcharges(t *testing.T) {
relayInfo
:=
&
relaycommon
.
RelayInfo
{
relayInfo
:=
&
relaycommon
.
RelayInfo
{
OriginModelName
:
"claude-3-7-sonnet"
,
OriginModelName
:
"claude-3-7-sonnet"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
1
,
CompletionRatio
:
1
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1.25
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1.25
},
},
},
TieredBillingSnapshot
:
&
billingexpr
.
BillingSnapshot
{
TieredBillingSnapshot
:
&
billingexpr
.
BillingSnapshot
{
BillingMode
:
"tiered_expr"
,
BillingMode
:
"tiered_expr"
,
...
@@ -643,10 +644,10 @@ func TestComposeTieredTextQuotaErrorFallbackUsesPreConsumedQuota(t *testing.T) {
...
@@ -643,10 +644,10 @@ func TestComposeTieredTextQuotaErrorFallbackUsesPreConsumedQuota(t *testing.T) {
relayInfo
:=
&
relaycommon
.
RelayInfo
{
relayInfo
:=
&
relaycommon
.
RelayInfo
{
OriginModelName
:
"claude-3-7-sonnet"
,
OriginModelName
:
"claude-3-7-sonnet"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
1
,
CompletionRatio
:
1
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1.25
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1.25
},
},
},
TieredBillingSnapshot
:
&
billingexpr
.
BillingSnapshot
{
TieredBillingSnapshot
:
&
billingexpr
.
BillingSnapshot
{
BillingMode
:
"tiered_expr"
,
BillingMode
:
"tiered_expr"
,
...
@@ -726,10 +727,10 @@ func TestTryTieredSettleNoClampInRange(t *testing.T) {
...
@@ -726,10 +727,10 @@ func TestTryTieredSettleNoClampInRange(t *testing.T) {
func
TestCalculateTextQuotaSummaryFixedPriceAppliesImageCountOnceAndAllowsOverride
(
t
*
testing
.
T
)
{
func
TestCalculateTextQuotaSummaryFixedPriceAppliesImageCountOnceAndAllowsOverride
(
t
*
testing
.
T
)
{
gin
.
SetMode
(
gin
.
TestMode
)
gin
.
SetMode
(
gin
.
TestMode
)
ctx
,
_
:=
gin
.
CreateTestContext
(
httptest
.
NewRecorder
())
ctx
,
_
:=
gin
.
CreateTestContext
(
httptest
.
NewRecorder
())
priceData
:=
types
.
PriceData
{
priceData
:=
host
types
.
PriceData
{
ModelPrice
:
0.12
,
ModelPrice
:
0.12
,
UsePrice
:
true
,
UsePrice
:
true
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
,
GroupRatio
:
1
,
},
},
}
}
...
@@ -911,10 +912,10 @@ func TestCalculateTextQuotaSummaryDoesNotApplyRequestMultipliersToToolSurcharge(
...
@@ -911,10 +912,10 @@ func TestCalculateTextQuotaSummaryDoesNotApplyRequestMultipliersToToolSurcharge(
relayInfo
:=
&
relaycommon
.
RelayInfo
{
relayInfo
:=
&
relaycommon
.
RelayInfo
{
OriginModelName
:
"o1"
,
OriginModelName
:
"o1"
,
PriceData
:
types
.
PriceData
{
PriceData
:
host
types
.
PriceData
{
ModelRatio
:
1
,
ModelRatio
:
1
,
CompletionRatio
:
1
,
CompletionRatio
:
1
,
GroupRatioInfo
:
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
GroupRatioInfo
:
host
types
.
GroupRatioInfo
{
GroupRatio
:
1
},
},
},
ResponsesUsageInfo
:
&
relaycommon
.
ResponsesUsageInfo
{
ResponsesUsageInfo
:
&
relaycommon
.
ResponsesUsageInfo
{
BuiltInTools
:
map
[
string
]
*
relaycommon
.
BuildInToolInfo
{
BuiltInTools
:
map
[
string
]
*
relaycommon
.
BuildInToolInfo
{
...
...
setting/ratio_setting/cache_ratio.go
View file @
b8bb3f40
package
ratio_setting
package
ratio_setting
import
(
import
(
"github.com/QuantumNous/new-api/
relaykit/
types"
"github.com/QuantumNous/new-api/types"
)
)
var
defaultCacheRatio
=
map
[
string
]
float64
{
var
defaultCacheRatio
=
map
[
string
]
float64
{
...
...
setting/ratio_setting/group_ratio.go
View file @
b8bb3f40
...
@@ -5,8 +5,8 @@ import (
...
@@ -5,8 +5,8 @@ import (
"errors"
"errors"
"github.com/QuantumNous/new-api/common"
"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/setting/config"
"github.com/QuantumNous/new-api/types"
)
)
var
defaultGroupRatio
=
map
[
string
]
float64
{
var
defaultGroupRatio
=
map
[
string
]
float64
{
...
...
setting/ratio_setting/model_ratio.go
View file @
b8bb3f40
...
@@ -4,8 +4,8 @@ import (
...
@@ -4,8 +4,8 @@ import (
"strings"
"strings"
"github.com/QuantumNous/new-api/common"
"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/setting/operation_setting"
"github.com/QuantumNous/new-api/types"
)
)
// from songquanpeng/one-api
// 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 {
...
@@ -104,8 +104,6 @@ func (p *PriceData) RemoveOtherRatiosFromFloat(value float64) float64 {
}
}
func
isValidOtherRatio
(
ratio
float64
)
bool
{
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
)
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
...
@@ -3,7 +3,7 @@ package types
import
(
import
(
"sync"
"sync"
kitutil
"github.com/QuantumNous/new-api/relaykit/relayconvert/kitutil
"
"github.com/QuantumNous/new-api/common
"
)
)
type
RWMap
[
K
comparable
,
V
any
]
struct
{
type
RWMap
[
K
comparable
,
V
any
]
struct
{
...
@@ -15,13 +15,13 @@ func (m *RWMap[K, V]) UnmarshalJSON(b []byte) error {
...
@@ -15,13 +15,13 @@ func (m *RWMap[K, V]) UnmarshalJSON(b []byte) error {
m
.
mutex
.
Lock
()
m
.
mutex
.
Lock
()
defer
m
.
mutex
.
Unlock
()
defer
m
.
mutex
.
Unlock
()
m
.
data
=
make
(
map
[
K
]
V
)
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
)
{
func
(
m
*
RWMap
[
K
,
V
])
MarshalJSON
()
([]
byte
,
error
)
{
m
.
mutex
.
RLock
()
m
.
mutex
.
RLock
()
defer
m
.
mutex
.
RUnlock
()
defer
m
.
mutex
.
RUnlock
()
return
kitutil
.
Marshal
(
m
.
data
)
return
common
.
Marshal
(
m
.
data
)
}
}
func
NewRWMap
[
K
comparable
,
V
any
]()
*
RWMap
[
K
,
V
]
{
func
NewRWMap
[
K
comparable
,
V
any
]()
*
RWMap
[
K
,
V
]
{
...
@@ -57,7 +57,6 @@ func (m *RWMap[K, V]) Clear() {
...
@@ -57,7 +57,6 @@ func (m *RWMap[K, V]) Clear() {
m
.
data
=
make
(
map
[
K
]
V
)
m
.
data
=
make
(
map
[
K
]
V
)
}
}
// ReadAll returns a copy of the entire map.
func
(
m
*
RWMap
[
K
,
V
])
ReadAll
()
map
[
K
]
V
{
func
(
m
*
RWMap
[
K
,
V
])
ReadAll
()
map
[
K
]
V
{
m
.
mutex
.
RLock
()
m
.
mutex
.
RLock
()
defer
m
.
mutex
.
RUnlock
()
defer
m
.
mutex
.
RUnlock
()
...
@@ -78,22 +77,20 @@ func LoadFromJsonString[K comparable, V any](m *RWMap[K, V], jsonStr string) err
...
@@ -78,22 +77,20 @@ func LoadFromJsonString[K comparable, V any](m *RWMap[K, V], jsonStr string) err
m
.
mutex
.
Lock
()
m
.
mutex
.
Lock
()
defer
m
.
mutex
.
Unlock
()
defer
m
.
mutex
.
Unlock
()
m
.
data
=
make
(
map
[
K
]
V
)
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
{
func
LoadFromJsonStringWithCallback
[
K
comparable
,
V
any
](
m
*
RWMap
[
K
,
V
],
jsonStr
string
,
onSuccess
func
())
error
{
m
.
mutex
.
Lock
()
m
.
mutex
.
Lock
()
defer
m
.
mutex
.
Unlock
()
defer
m
.
mutex
.
Unlock
()
m
.
data
=
make
(
map
[
K
]
V
)
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
{
if
err
==
nil
&&
onSuccess
!=
nil
{
onSuccess
()
onSuccess
()
}
}
return
err
return
err
}
}
// MarshalJSONString returns the JSON string representation of the RWMap.
func
(
m
*
RWMap
[
K
,
V
])
MarshalJSONString
()
string
{
func
(
m
*
RWMap
[
K
,
V
])
MarshalJSONString
()
string
{
bytes
,
err
:=
m
.
MarshalJSON
()
bytes
,
err
:=
m
.
MarshalJSON
()
if
err
!=
nil
{
if
err
!=
nil
{
...
...
relaykit/
types/set.go
→
types/set.go
View file @
b8bb3f40
...
@@ -4,7 +4,6 @@ type Set[T comparable] struct {
...
@@ -4,7 +4,6 @@ type Set[T comparable] struct {
items
map
[
T
]
struct
{}
items
map
[
T
]
struct
{}
}
}
// NewSet 创建并返回一个新的 Set
func
NewSet
[
T
comparable
]()
*
Set
[
T
]
{
func
NewSet
[
T
comparable
]()
*
Set
[
T
]
{
return
&
Set
[
T
]{
return
&
Set
[
T
]{
items
:
make
(
map
[
T
]
struct
{}),
items
:
make
(
map
[
T
]
struct
{}),
...
@@ -15,24 +14,19 @@ func (s *Set[T]) Add(item T) {
...
@@ -15,24 +14,19 @@ func (s *Set[T]) Add(item T) {
s
.
items
[
item
]
=
struct
{}{}
s
.
items
[
item
]
=
struct
{}{}
}
}
// Remove 从 Set 中移除一个元素
func
(
s
*
Set
[
T
])
Remove
(
item
T
)
{
func
(
s
*
Set
[
T
])
Remove
(
item
T
)
{
delete
(
s
.
items
,
item
)
delete
(
s
.
items
,
item
)
}
}
// Contains 检查 Set 是否包含某个元素
func
(
s
*
Set
[
T
])
Contains
(
item
T
)
bool
{
func
(
s
*
Set
[
T
])
Contains
(
item
T
)
bool
{
_
,
exists
:=
s
.
items
[
item
]
_
,
exists
:=
s
.
items
[
item
]
return
exists
return
exists
}
}
// Len 返回 Set 中元素的数量
func
(
s
*
Set
[
T
])
Len
()
int
{
func
(
s
*
Set
[
T
])
Len
()
int
{
return
len
(
s
.
items
)
return
len
(
s
.
items
)
}
}
// Items 返回 Set 中所有元素组成的切片
// 注意:由于 map 的无序性,返回的切片元素顺序是随机的
func
(
s
*
Set
[
T
])
Items
()
[]
T
{
func
(
s
*
Set
[
T
])
Items
()
[]
T
{
items
:=
make
([]
T
,
0
,
s
.
Len
())
items
:=
make
([]
T
,
0
,
s
.
Len
())
for
item
:=
range
s
.
items
{
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