forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic-price.ts
More file actions
182 lines (160 loc) · 5.05 KB
/
Copy pathdynamic-price.ts
File metadata and controls
182 lines (160 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { formatBillingCurrencyFromUSD } from '@/lib/currency'
import { TOKEN_UNIT_DIVISORS } from '../constants'
import type { PricingModel, TokenUnit } from '../types'
import {
BILLING_PRICING_VARS,
parseTiersFromExpr,
splitBillingExprAndRequestRules,
tryParseRequestRuleExpr,
type BillingVar,
type ParsedTier,
} from './billing-expr'
import { getDisplayGroupRatio } from './model-helpers'
type DynamicPriceOptions = {
tokenUnit: TokenUnit
showRechargePrice?: boolean
priceRate?: number
usdExchangeRate?: number
groupRatioMultiplier?: number
}
export type DynamicPriceEntry = {
key: string
field: string
label: string
shortLabel: string
value: number
formatted: string
variable: BillingVar
}
export type DynamicPricingSummary = {
tiers: ParsedTier[]
tier: ParsedTier | null
tierCount: number
hasRequestRules: boolean
isSpecialExpression: boolean
rawExpression: string
entries: DynamicPriceEntry[]
primaryEntries: DynamicPriceEntry[]
secondaryEntries: DynamicPriceEntry[]
}
const PRIMARY_DYNAMIC_FIELDS = new Set(['inputPrice', 'outputPrice'])
export function isDynamicPricingModel(model: PricingModel): boolean {
return model.billing_mode === 'tiered_expr' && Boolean(model.billing_expr)
}
export function getDynamicDisplayGroupRatio(
model: PricingModel,
selectedGroup?: string
): number {
return getDisplayGroupRatio(model, selectedGroup)
}
function applyRechargeRate(
price: number,
showWithRecharge: boolean,
priceRate: number,
usdExchangeRate: number
): number {
if (!showWithRecharge) return price
return (price * priceRate) / usdExchangeRate
}
export function formatDynamicUnitPrice(
valuePerMillionTokens: number,
options: DynamicPriceOptions
): string {
const groupRatio = options.groupRatioMultiplier ?? 1
const priceRate = options.priceRate ?? 1
const usdExchangeRate = options.usdExchangeRate ?? 1
const priceUSD =
(valuePerMillionTokens * groupRatio) /
TOKEN_UNIT_DIVISORS[options.tokenUnit]
const displayPrice = applyRechargeRate(
priceUSD,
options.showRechargePrice ?? false,
priceRate,
usdExchangeRate
)
return formatBillingCurrencyFromUSD(displayPrice, {
digitsLarge: 4,
digitsSmall: 6,
abbreviate: false,
})
}
export function getDynamicPricingTiers(model: PricingModel): ParsedTier[] {
if (!isDynamicPricingModel(model)) return []
const { billingExpr } = splitBillingExprAndRequestRules(
model.billing_expr || ''
)
return parseTiersFromExpr(billingExpr)
}
export function hasDynamicRequestRules(model: PricingModel): boolean {
if (!isDynamicPricingModel(model)) return false
const { requestRuleExpr } = splitBillingExprAndRequestRules(
model.billing_expr || ''
)
return Boolean(tryParseRequestRuleExpr(requestRuleExpr || '')?.length)
}
export function getDynamicPriceEntries(
tier: ParsedTier | null,
options: DynamicPriceOptions
): DynamicPriceEntry[] {
if (!tier) return []
return BILLING_PRICING_VARS.flatMap((variable) => {
if (!variable.field) return []
const value = Number(tier[variable.field])
if (!Number.isFinite(value) || value <= 0) return []
return [
{
key: variable.key,
field: variable.field,
label: variable.label,
shortLabel: variable.shortLabel,
value,
formatted: formatDynamicUnitPrice(value, options),
variable,
},
]
}).sort((a, b) => {
const aPrimary = PRIMARY_DYNAMIC_FIELDS.has(a.field)
const bPrimary = PRIMARY_DYNAMIC_FIELDS.has(b.field)
if (aPrimary !== bPrimary) return aPrimary ? -1 : 1
return 0
})
}
export function getDynamicPricingSummary(
model: PricingModel,
options: DynamicPriceOptions
): DynamicPricingSummary | null {
if (!isDynamicPricingModel(model)) return null
const tiers = getDynamicPricingTiers(model)
const tier = tiers[0] || null
const entries = getDynamicPriceEntries(tier, options)
const rawExpression = model.billing_expr || ''
return {
tiers,
tier,
tierCount: tiers.length,
hasRequestRules: hasDynamicRequestRules(model),
isSpecialExpression: rawExpression.trim().length > 0 && tiers.length === 0,
rawExpression,
entries,
primaryEntries: entries.filter((entry) =>
PRIMARY_DYNAMIC_FIELDS.has(entry.field)
),
secondaryEntries: entries.filter(
(entry) => !PRIMARY_DYNAMIC_FIELDS.has(entry.field)
),
}
}