fix: show task model list prices accurately
This commit is contained in:
1 parent
b2266d7e9d
commit
b455d8d79f
9 files changed
+165
-30
No files matched your search
@@ -39,6 +39,8 @@ export interface CatalogModel {
|
||||
discountLabel?: string
|
||||
exchangeRate?: number
|
||||
priceSource?: string
|
||||
upstreamCredits?: number
|
||||
standardPrice?: { price?: number; unit?: string; credits?: number; priceSource?: string }
|
||||
listPriceUsd?: { input?: number; output?: number; cachedInput?: number }
|
||||
}
|
||||
pricingConfigured?: boolean
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
export interface DisplayPricing {
|
||||
price?: number
|
||||
input?: number
|
||||
output?: number
|
||||
cachedInput?: number
|
||||
unit?: string
|
||||
discount?: number
|
||||
discountLabel?: string
|
||||
exchangeRate?: number
|
||||
priceSource?: string
|
||||
upstreamCredits?: number
|
||||
standardPrice?: { price?: number; unit?: string; credits?: number; priceSource?: string }
|
||||
listPriceUsd?: { input?: number; output?: number; cachedInput?: number }
|
||||
}
|
||||
|
||||
function numberLabel(value: number | undefined, currency = '¥') {
|
||||
if (value === undefined || value === null || !Number.isFinite(value)) return '待配置'
|
||||
return `${currency}${value.toLocaleString('zh-CN', { maximumFractionDigits: 8 })}`
|
||||
}
|
||||
|
||||
function unitLabel(unit: string | undefined) {
|
||||
const value = String(unit || '').trim()
|
||||
return value.replace(/^cny\s*\/\s*/i, '') || '计费单位'
|
||||
}
|
||||
|
||||
function unitSuffix(pricing: DisplayPricing) {
|
||||
return unitLabel(pricing.unit).replace(/^1m\s+/i, '')
|
||||
}
|
||||
|
||||
function isTokenUnit(unit: string | undefined) {
|
||||
return String(unit || '').toLowerCase().includes('token')
|
||||
}
|
||||
|
||||
function formatUsd(value: number | undefined) {
|
||||
return numberLabel(value, '$')
|
||||
}
|
||||
|
||||
function fixedStandardPriceLabel(price: NonNullable<DisplayPricing['standardPrice']>) {
|
||||
if (price.price === undefined || !Number.isFinite(price.price)) return undefined
|
||||
const rawUnit = String(price.unit || 'USD / task').trim()
|
||||
const match = rawUnit.match(/^([A-Za-z]{3})\s*\/\s*(.+)$/)
|
||||
const currency = match?.[1]?.toUpperCase()
|
||||
const symbol = currency === 'USD' ? '$' : currency === 'CNY' ? '¥' : currency ? `${currency} ` : ''
|
||||
const unit = match?.[2] || rawUnit
|
||||
const source = price.priceSource ? ` · ${price.priceSource}` : ''
|
||||
return `${numberLabel(price.price, symbol)} / ${unit}${source}`
|
||||
}
|
||||
|
||||
/** Standard/list price is shown only when it is explicitly supplied by the source. */
|
||||
export function standardPriceLabel(pricing?: DisplayPricing) {
|
||||
if (!pricing) return '标准价未提供'
|
||||
const fixed = pricing.standardPrice ? fixedStandardPriceLabel(pricing.standardPrice) : undefined
|
||||
if (fixed) return fixed
|
||||
if (pricing.listPriceUsd) {
|
||||
return `${formatUsd(pricing.listPriceUsd.input)} 输入 / ${formatUsd(pricing.listPriceUsd.output)} 输出 · USD / 1M`
|
||||
}
|
||||
const credits = pricing.upstreamCredits ?? pricing.standardPrice?.credits
|
||||
if (credits !== undefined && Number.isFinite(credits)) {
|
||||
return `标准价未提供 · 上游 ${credits} credits / ${unitSuffix(pricing)}`
|
||||
}
|
||||
return '标准价未提供'
|
||||
}
|
||||
|
||||
/** 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)
|
||||
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 })))
|
||||
if (keys.size > 1) return '各分组标准价见详情'
|
||||
}
|
||||
return standardPriceLabel(listed[0] || values[0])
|
||||
}
|
||||
|
||||
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 price = pricing.price ?? pricing.input
|
||||
return price === undefined ? `计费单位:${unitLabel(pricing.unit)}` : `平台售价 ${numberLabel(price)} / ${unitSuffix(pricing)}`
|
||||
}
|
||||
|
||||
export function groupDiscountLabel(pricing?: DisplayPricing, configured = true) {
|
||||
if (!configured || !pricing) return '价格待配置'
|
||||
if (pricing.discountLabel && pricing.discountLabel !== '标准价') return pricing.discountLabel
|
||||
if (pricing.discount !== undefined && Number.isFinite(pricing.discount)) return `${pricing.discount.toLocaleString('zh-CN', { maximumFractionDigits: 1 })}折`
|
||||
return '折扣未提供'
|
||||
}
|
||||
Reference in new issue
Block a user