forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprice.ts
More file actions
272 lines (238 loc) · 7.27 KB
/
Copy pathprice.ts
File metadata and controls
272 lines (238 loc) · 7.27 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
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 { formatCurrencyFromUSD } from '@/lib/currency'
import { QUOTA_TYPE_VALUES, TOKEN_UNIT_DIVISORS } from '../constants'
import type { PricingModel, TokenUnit, PriceType } from '../types'
import { getConfiguredGroupRatio, getDisplayGroupRatio } from './model-helpers'
// ----------------------------------------------------------------------------
// Price Calculation Utilities
// ----------------------------------------------------------------------------
/**
* Strip trailing zeros from formatted price string while preserving currency symbols
*/
export function stripTrailingZeros(formatted: string): string {
// Match currency symbol at start, number, and potential 'k' suffix
const match = formatted.match(/^([^\d-]*)([-\d,]+\.?\d*)(k?)$/)
if (!match) return formatted
const [, symbol, number, suffix] = match
// Remove commas for processing
const cleanNumber = number.replaceAll(',', '')
// Convert to number and back to remove trailing zeros
const parsed = Number.parseFloat(cleanNumber)
if (Number.isNaN(parsed)) return formatted
// Convert to string, which automatically removes trailing zeros
let result = parsed.toString()
// If the result is in scientific notation, format it properly
if (result.includes('e')) {
result = parsed.toFixed(20).replace(/\.?0+$/, '')
}
return `${symbol}${result}${suffix}`
}
/**
* Calculate token price in USD.
*
* Returns NaN when the required ratio field is missing/null so callers can
* skip rendering that price type.
*/
function calculateTokenPrice(
model: PricingModel,
type: PriceType,
ratio: number
): number {
const base = model.model_ratio * 2 * ratio
switch (type) {
case 'input':
return base
case 'output':
return base * model.completion_ratio
case 'cache':
return hasRatio(model.cache_ratio)
? base * Number(model.cache_ratio)
: Number.NaN
case 'create_cache':
return hasRatio(model.create_cache_ratio)
? base * Number(model.create_cache_ratio)
: Number.NaN
case 'image':
return hasRatio(model.image_ratio)
? base * Number(model.image_ratio)
: Number.NaN
case 'audio_input':
return hasRatio(model.audio_ratio)
? base * Number(model.audio_ratio)
: Number.NaN
case 'audio_output':
return hasRatio(model.audio_ratio) &&
hasRatio(model.audio_completion_ratio)
? base *
Number(model.audio_ratio) *
Number(model.audio_completion_ratio)
: Number.NaN
}
}
function hasRatio(value: number | null | undefined): boolean {
return value !== undefined && value !== null && Number.isFinite(Number(value))
}
/**
* Apply recharge rate to price
*
* priceRate represents how much users need to recharge (in the display currency)
* to get 1 USD credit. usdExchangeRate is the real exchange rate.
*
* The returned value will be formatted by formatCurrencyFromUSD, which will
* multiply by the display currency's exchange rate.
*
* Examples:
*
* 1. Display currency = USD:
* - Model: 1 USD
* - priceRate = 0.5 (recharge $0.5 to get $1 credit)
* - usdExchangeRate = 1
* - Return: 1 × 0.5 / 1 = 0.5
* - formatCurrencyFromUSD(0.5) → $0.5 ✓
*
* 2. Display currency = CNY:
* - Model: 1 USD
* - priceRate = 4 (recharge ¥4 to get $1 credit)
* - usdExchangeRate = 7 (real rate: 1 USD = ¥7)
* - Return: 1 × 4 / 7 = 0.571
* - formatCurrencyFromUSD(0.571) → 0.571 × 7 = ¥4 ✓
* - Normal price: ¥7, Recharge price: ¥4 (cheaper!)
*/
function applyRechargeRate(
price: number,
showWithRecharge: boolean,
priceRate: number,
usdExchangeRate: number
): number {
if (!showWithRecharge) return price
return (price * priceRate) / usdExchangeRate
}
/**
* Format token-based price for display
*/
export function formatPrice(
model: PricingModel,
type: PriceType,
tokenUnit: TokenUnit,
showWithRecharge = false,
priceRate = 1,
usdExchangeRate = 1,
selectedGroup?: string
): string {
if (model.quota_type === QUOTA_TYPE_VALUES.REQUEST) {
return '-'
}
const displayGroupRatio = getDisplayGroupRatio(model, selectedGroup)
let priceInUSD = calculateTokenPrice(model, type, displayGroupRatio)
priceInUSD = applyRechargeRate(
priceInUSD,
showWithRecharge,
priceRate,
usdExchangeRate
)
const price = priceInUSD / TOKEN_UNIT_DIVISORS[tokenUnit]
return formatCurrencyFromUSD(price, {
digitsLarge: 4,
digitsSmall: 6,
abbreviate: false,
})
}
/**
* Format price for a specific group (token-based)
*/
export function formatGroupPrice(
model: PricingModel,
group: string,
type: PriceType,
tokenUnit: TokenUnit,
showWithRecharge = false,
priceRate = 1,
usdExchangeRate = 1,
groupRatio: Record<string, number>
): string {
if (model.quota_type === QUOTA_TYPE_VALUES.REQUEST) {
return '-'
}
const ratio = getConfiguredGroupRatio(groupRatio, group)
let priceInUSD = calculateTokenPrice(model, type, ratio)
priceInUSD = applyRechargeRate(
priceInUSD,
showWithRecharge,
priceRate,
usdExchangeRate
)
const price = priceInUSD / TOKEN_UNIT_DIVISORS[tokenUnit]
return formatCurrencyFromUSD(price, {
digitsLarge: 4,
digitsSmall: 6,
abbreviate: false,
})
}
/**
* Format fixed price for pay-per-request models (with specific group)
*/
export function formatFixedPrice(
model: PricingModel,
group: string,
showWithRecharge = false,
priceRate = 1,
usdExchangeRate = 1,
groupRatio: Record<string, number>
): string {
if (model.quota_type !== QUOTA_TYPE_VALUES.REQUEST) {
return '-'
}
const ratio = getConfiguredGroupRatio(groupRatio, group)
let priceInUSD = (model.model_price || 0) * ratio
priceInUSD = applyRechargeRate(
priceInUSD,
showWithRecharge,
priceRate,
usdExchangeRate
)
return formatCurrencyFromUSD(priceInUSD, {
digitsLarge: 4,
digitsSmall: 4,
abbreviate: false,
})
}
/**
* Format fixed price for pay-per-request models (minimum price from all groups)
*/
export function formatRequestPrice(
model: PricingModel,
showWithRecharge = false,
priceRate = 1,
usdExchangeRate = 1,
selectedGroup?: string
): string {
if (model.quota_type !== QUOTA_TYPE_VALUES.REQUEST) {
return '-'
}
const displayGroupRatio = getDisplayGroupRatio(model, selectedGroup)
let priceInUSD = (model.model_price || 0) * displayGroupRatio
priceInUSD = applyRechargeRate(
priceInUSD,
showWithRecharge,
priceRate,
usdExchangeRate
)
return formatCurrencyFromUSD(priceInUSD, {
digitsLarge: 4,
digitsSmall: 4,
abbreviate: false,
})
}