fix: reconcile source currencies and retail token pricing
This commit is contained in:
1 parent
ea9069e332
commit
512ca52262
12 files changed
+458
-12
No files matched your search
@@ -44,6 +44,7 @@ export interface CatalogModel {
|
||||
upstreamCredits?: number
|
||||
standardPrice?: { price?: number; unit?: string; credits?: number; priceSource?: string }
|
||||
listPriceUsd?: { input?: number; output?: number; cachedInput?: number }
|
||||
listPriceCny?: { input?: number; output?: number; cachedInput?: number }
|
||||
}
|
||||
pricingConfigured?: boolean
|
||||
billingReady?: boolean
|
||||
|
||||
@@ -11,6 +11,7 @@ export interface DisplayPricing {
|
||||
upstreamCredits?: number
|
||||
standardPrice?: { price?: number; unit?: string; credits?: number; priceSource?: string }
|
||||
listPriceUsd?: { input?: number; output?: number; cachedInput?: number }
|
||||
listPriceCny?: { input?: number; output?: number; cachedInput?: number }
|
||||
}
|
||||
|
||||
function numberLabel(value: number | undefined, currency = '¥') {
|
||||
@@ -51,8 +52,11 @@ export function standardPriceLabel(pricing?: DisplayPricing) {
|
||||
if (!pricing) return '标准价未提供'
|
||||
const fixed = pricing.standardPrice ? fixedStandardPriceLabel(pricing.standardPrice) : undefined
|
||||
if (fixed) return fixed
|
||||
if (pricing.listPriceCny) {
|
||||
return `${numberLabel(pricing.listPriceCny.input)} 输入 / ${numberLabel(pricing.listPriceCny.output)} 输出 · CNY / 1M(一百万 Token)`
|
||||
}
|
||||
if (pricing.listPriceUsd) {
|
||||
return `${formatUsd(pricing.listPriceUsd.input)} 输入 / ${formatUsd(pricing.listPriceUsd.output)} 输出 · USD / 1M`
|
||||
return `${formatUsd(pricing.listPriceUsd.input)} 输入 / ${formatUsd(pricing.listPriceUsd.output)} 输出 · USD / 1M(一百万 Token)`
|
||||
}
|
||||
const credits = pricing.upstreamCredits ?? pricing.standardPrice?.credits
|
||||
if (credits !== undefined && Number.isFinite(credits)) {
|
||||
@@ -64,10 +68,10 @@ export function standardPriceLabel(pricing?: DisplayPricing) {
|
||||
/** A logical model can have several routes. Do not present one route's list price as universal when they differ. */
|
||||
export function modelStandardPriceLabel(pricings: Array<DisplayPricing | undefined>) {
|
||||
const values = pricings.filter((pricing): pricing is DisplayPricing => !!pricing)
|
||||
const listed = values.filter((pricing) => !!pricing.listPriceUsd || !!pricing.standardPrice)
|
||||
const listed = values.filter((pricing) => !!pricing.listPriceUsd || !!pricing.listPriceCny || !!pricing.standardPrice)
|
||||
if (listed.length > 0 && listed.length < values.length) return '部分分组标准价见详情'
|
||||
if (listed.length > 1) {
|
||||
const keys = new Set(listed.map((pricing) => JSON.stringify({ listPriceUsd: pricing.listPriceUsd, standardPrice: pricing.standardPrice })))
|
||||
const keys = new Set(listed.map((pricing) => JSON.stringify({ listPriceUsd: pricing.listPriceUsd, listPriceCny: pricing.listPriceCny, standardPrice: pricing.standardPrice })))
|
||||
if (keys.size > 1) return '各分组标准价见详情'
|
||||
}
|
||||
return standardPriceLabel(listed[0] || values[0])
|
||||
@@ -76,12 +80,32 @@ export function modelStandardPriceLabel(pricings: Array<DisplayPricing | undefin
|
||||
export function platformPriceLabel(pricing?: DisplayPricing) {
|
||||
if (!pricing) return '价格待配置'
|
||||
if (isTokenUnit(pricing.unit)) {
|
||||
return `${numberLabel(pricing.input)} 输入 / ${numberLabel(pricing.output)} 输出 · ${pricing.unit || 'CNY / 1M tokens'}`
|
||||
const unit = pricing.unit === 'CNY / 1M tokens' ? '人民币 / 1M(一百万 Token)' : pricing.unit || '人民币 / 1M(一百万 Token)'
|
||||
return `${numberLabel(pricing.input)} 输入 / ${numberLabel(pricing.output)} 输出 · ${unit}`
|
||||
}
|
||||
const price = pricing.price ?? pricing.input
|
||||
return price === undefined ? `计费单位:${unitLabel(pricing.unit)}` : `平台售价 ${numberLabel(price)} / ${unitSuffix(pricing)}`
|
||||
}
|
||||
|
||||
/** Explicit admin action. Settlement still uses saved CNY rates, never applies a second discount. */
|
||||
export function calculateTokenRetailPricing(currency: 'CNY' | 'USD', list: { input: number; output: number; cachedInput?: number }, discount: number, exchangeRate?: number) {
|
||||
if (currency !== 'CNY' && currency !== 'USD') throw new Error('请选择人民币或美元原价')
|
||||
if (!Number.isFinite(discount) || discount < 0 || discount > 10) throw new Error('对外折扣须在 0–10 折之间')
|
||||
const fx = currency === 'CNY' ? 1 : exchangeRate
|
||||
if (fx === undefined || !Number.isFinite(fx) || fx <= 0 || fx > 1000) throw new Error('美元原价需要有效的人民币汇率')
|
||||
const scale = 100000000n
|
||||
const scaled = (value: number) => BigInt(value.toFixed(8).replace('.', ''))
|
||||
const amount = (value: number) => {
|
||||
if (!Number.isFinite(value) || value < 0 || value > 1000000) throw new Error('原价须在 0–100 万之间')
|
||||
// Integer arithmetic with HALF_UP rounding to the same eight places as ChatMeter.
|
||||
const divisor = scale * scale * 10n
|
||||
const price = Number((scaled(value) * scaled(discount) * scaled(fx) + divisor / 2n) / divisor) / Number(scale)
|
||||
if (price > 1000000) throw new Error('人民币售价不能超过 100 万')
|
||||
return price
|
||||
}
|
||||
return { input: amount(list.input), output: amount(list.output), cachedInput: list.cachedInput === undefined ? undefined : amount(list.cachedInput), unit: 'CNY / 1M tokens', discount, discountLabel: `${discount}折`, exchangeRate: fx }
|
||||
}
|
||||
|
||||
export function groupDiscountLabel(pricing?: DisplayPricing, configured = true) {
|
||||
if (!configured || !pricing) return '价格待配置'
|
||||
if (pricing.discountLabel && pricing.discountLabel !== '标准价') return pricing.discountLabel
|
||||
|
||||
Reference in new issue
Block a user