Files
monkey-ai-web/src/helpers/render.jsx
T

2122 lines
68 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
Copyright (C) 2025 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 {
OpenAIMono as OpenAI,
ClaudeColor as Claude,
GeminiColor as Gemini,
MoonshotMono as Moonshot,
ZhipuColor as Zhipu,
QwenColor as Qwen,
DeepSeekColor as DeepSeek,
MinimaxColor as Minimax,
WenxinColor as Wenxin,
SparkColor as Spark,
MidjourneyMono as Midjourney,
HunyuanColor as Hunyuan,
CohereColor as Cohere,
CloudflareColor as Cloudflare,
Ai360Color as Ai360,
YiColor as Yi,
JinaMono as Jina,
MistralColor as Mistral,
XAIMono as XAI,
OllamaMono as Ollama,
DoubaoColor as Doubao,
SunoMono as Suno,
XinferenceColor as Xinference,
OpenRouterMono as OpenRouter,
DifyColor as Dify,
CozeMono as Coze,
SiliconCloudColor as SiliconCloud,
KlingColor as Kling,
PerplexityColor as Perplexity,
ReplicateMono as Replicate,
} from '@/assets/brand-icons/primitives';
import React, { lazy, Suspense } from 'react';
import i18next from 'i18next';
// React 18-compatible LobeHub icons lack these brands; use generic icons.
import { Lock, Bot as FastGPT, Image as Jimeng } from 'lucide-react';
import { Modal, Tag, Typography } from '@/components/ui';
import { copy, getCurrencySymbol, showSuccess } from './utils';
import { visit } from 'unist-util-visit';
// Full Avatar/Text/Combine variants are loaded only when configured icons render.
const LobeHubIcon = lazy(() => import('../components/common/LobeHubIcon'));
import {
LayoutDashboard,
TerminalSquare,
MessageSquare,
Key,
BarChart3,
Image as ImageIcon,
CheckSquare,
CreditCard,
Layers,
Gift,
User,
Settings,
CircleUser,
Package,
Activity,
PieChart,
Building2,
Handshake,
Globe,
CalendarDays,
} from 'lucide-react';
// 获取侧边栏Lucide图标组件
export function getLucideIcon(key, selected = false) {
const size = 16;
const strokeWidth = 2;
const SELECTED_COLOR = 'var(--ui-color-primary)';
const iconColor = selected ? SELECTED_COLOR : 'currentColor';
const commonProps = {
size,
strokeWidth,
className: `transition-colors duration-200 ${selected ? 'transition-transform duration-200 scale-105' : ''}`,
};
// 根据不同的key返回不同的图标
switch (key) {
case 'detail':
return <LayoutDashboard {...commonProps} color={iconColor} />;
case 'playground':
return <TerminalSquare {...commonProps} color={iconColor} />;
case 'chat':
return <MessageSquare {...commonProps} color={iconColor} />;
case 'token':
case 'admin-token':
return <Key {...commonProps} color={iconColor} />;
case 'log':
return <BarChart3 {...commonProps} color={iconColor} />;
case 'midjourney':
return <ImageIcon {...commonProps} color={iconColor} />;
case 'task':
return <CheckSquare {...commonProps} color={iconColor} />;
case 'topup':
return <CreditCard {...commonProps} color={iconColor} />;
case 'channel':
return <Layers {...commonProps} color={iconColor} />;
case 'redemption':
return <Gift {...commonProps} color={iconColor} />;
case 'user':
case 'personal':
return <User {...commonProps} color={iconColor} />;
case 'models':
return <Package {...commonProps} color={iconColor} />;
case 'monitor':
return <Activity {...commonProps} color={iconColor} />;
case 'analytics':
return <PieChart {...commonProps} color={iconColor} />;
case 'organizations':
return <Building2 {...commonProps} color={iconColor} />;
case 'partner':
return <Handshake {...commonProps} color={iconColor} />;
case 'setting':
return <Settings {...commonProps} color={iconColor} />;
case 'website-setting':
return <Globe {...commonProps} color={iconColor} />;
case 'demo-reservations':
return <CalendarDays {...commonProps} color={iconColor} />;
default:
return <CircleUser {...commonProps} color={iconColor} />;
}
}
// 获取模型分类
export const getModelCategories = (() => {
let categoriesCache = null;
let lastLocale = null;
return (t) => {
const currentLocale = i18next.language;
if (categoriesCache && lastLocale === currentLocale) {
return categoriesCache;
}
categoriesCache = {
all: {
label: t('全部模型'),
icon: null,
filter: () => true,
},
openai: {
label: 'OpenAI',
icon: <OpenAI />,
filter: (model) =>
model.model_name.toLowerCase().includes('gpt') ||
model.model_name.toLowerCase().includes('dall-e') ||
model.model_name.toLowerCase().includes('whisper') ||
model.model_name.toLowerCase().includes('tts-1') ||
model.model_name.toLowerCase().includes('text-embedding-3') ||
model.model_name.toLowerCase().includes('text-moderation') ||
model.model_name.toLowerCase().includes('babbage') ||
model.model_name.toLowerCase().includes('davinci') ||
model.model_name.toLowerCase().includes('curie') ||
model.model_name.toLowerCase().includes('ada') ||
model.model_name.toLowerCase().includes('o1') ||
model.model_name.toLowerCase().includes('o3') ||
model.model_name.toLowerCase().includes('o4'),
},
anthropic: {
label: 'Anthropic',
icon: <Claude />,
filter: (model) => model.model_name.toLowerCase().includes('claude'),
},
gemini: {
label: 'Gemini',
icon: <Gemini />,
filter: (model) =>
model.model_name.toLowerCase().includes('gemini') ||
model.model_name.toLowerCase().includes('gemma') ||
model.model_name.toLowerCase().includes('learnlm') ||
model.model_name.toLowerCase().startsWith('embedding-') ||
model.model_name.toLowerCase().includes('text-embedding-004') ||
model.model_name.toLowerCase().includes('imagen-4') ||
model.model_name.toLowerCase().includes('veo-') ||
model.model_name.toLowerCase().includes('aqa'),
},
moonshot: {
label: 'Moonshot',
icon: <Moonshot />,
filter: (model) =>
model.model_name.toLowerCase().includes('moonshot') ||
model.model_name.toLowerCase().includes('kimi'),
},
zhipu: {
label: t('智谱'),
icon: <Zhipu />,
filter: (model) =>
model.model_name.toLowerCase().includes('chatglm') ||
model.model_name.toLowerCase().includes('glm-') ||
model.model_name.toLowerCase().includes('cogview') ||
model.model_name.toLowerCase().includes('cogvideo'),
},
qwen: {
label: t('通义千问'),
icon: <Qwen />,
filter: (model) => model.model_name.toLowerCase().includes('qwen'),
},
deepseek: {
label: 'DeepSeek',
icon: <DeepSeek />,
filter: (model) => model.model_name.toLowerCase().includes('deepseek'),
},
minimax: {
label: 'MiniMax',
icon: <Minimax />,
filter: (model) =>
model.model_name.toLowerCase().includes('abab') ||
model.model_name.toLowerCase().includes('minimax'),
},
baidu: {
label: t('文心一言'),
icon: <Wenxin />,
filter: (model) => model.model_name.toLowerCase().includes('ernie'),
},
xunfei: {
label: t('讯飞星火'),
icon: <Spark />,
filter: (model) => model.model_name.toLowerCase().includes('spark'),
},
midjourney: {
label: 'Midjourney',
icon: <Midjourney />,
filter: (model) => model.model_name.toLowerCase().includes('mj_'),
},
tencent: {
label: t('腾讯混元'),
icon: <Hunyuan />,
filter: (model) => model.model_name.toLowerCase().includes('hunyuan'),
},
cohere: {
label: 'Cohere',
icon: <Cohere />,
filter: (model) =>
model.model_name.toLowerCase().includes('command') ||
model.model_name.toLowerCase().includes('c4ai-') ||
model.model_name.toLowerCase().includes('embed-'),
},
cloudflare: {
label: 'Cloudflare',
icon: <Cloudflare />,
filter: (model) => model.model_name.toLowerCase().includes('@cf/'),
},
ai360: {
label: t('360智脑'),
icon: <Ai360 />,
filter: (model) => model.model_name.toLowerCase().includes('360'),
},
jina: {
label: 'Jina',
icon: <Jina />,
filter: (model) => model.model_name.toLowerCase().includes('jina'),
},
mistral: {
label: 'Mistral AI',
icon: <Mistral />,
filter: (model) =>
model.model_name.toLowerCase().includes('mistral') ||
model.model_name.toLowerCase().includes('codestral') ||
model.model_name.toLowerCase().includes('pixtral') ||
model.model_name.toLowerCase().includes('voxtral') ||
model.model_name.toLowerCase().includes('magistral'),
},
xai: {
label: 'xAI',
icon: <XAI />,
filter: (model) => model.model_name.toLowerCase().includes('grok'),
},
llama: {
label: 'Llama',
icon: <Ollama />,
filter: (model) => model.model_name.toLowerCase().includes('llama'),
},
doubao: {
label: t('豆包'),
icon: <Doubao />,
filter: (model) => model.model_name.toLowerCase().includes('doubao'),
},
yi: {
label: t('零一万物'),
icon: <Yi />,
filter: (model) => model.model_name.toLowerCase().includes('yi'),
},
};
lastLocale = currentLocale;
return categoriesCache;
};
})();
/**
* 根据渠道类型返回对应的厂商图标
* @param {number} channelType - 渠道类型值
* @returns {JSX.Element|null} - 对应的厂商图标组件
*/
export function getChannelIcon(channelType) {
const iconSize = 14;
switch (channelType) {
case 1: // OpenAI
case 3: // Azure OpenAI
return <OpenAI size={iconSize} />;
case 2: // Midjourney Proxy
case 5: // Midjourney Proxy Plus
return <Midjourney size={iconSize} />;
case 36: // Suno API
return <Suno size={iconSize} />;
case 4: // Ollama
return <Ollama size={iconSize} />;
case 14: // Anthropic Claude
case 33: // AWS Claude
return <Claude size={iconSize} />;
case 41: // Vertex AI
return <Gemini size={iconSize} />;
case 34: // Cohere
return <Cohere size={iconSize} />;
case 39: // Cloudflare
return <Cloudflare size={iconSize} />;
case 43: // DeepSeek
return <DeepSeek size={iconSize} />;
case 15: // 百度文心千帆
case 46: // 百度文心千帆V2
return <Wenxin size={iconSize} />;
case 17: // 阿里通义千问
return <Qwen size={iconSize} />;
case 18: // 讯飞星火认知
return <Spark size={iconSize} />;
case 16: // 智谱 ChatGLM
case 26: // 智谱 GLM-4V
return <Zhipu size={iconSize} />;
case 24: // Google Gemini
case 11: // Google PaLM2
return <Gemini size={iconSize} />;
case 47: // Xinference
return <Xinference size={iconSize} />;
case 25: // Moonshot
return <Moonshot size={iconSize} />;
case 27: // Perplexity
return <Perplexity size={iconSize} />;
case 20: // OpenRouter
return <OpenRouter size={iconSize} />;
case 19: // 360 智脑
return <Ai360 size={iconSize} />;
case 23: // 腾讯混元
return <Hunyuan size={iconSize} />;
case 31: // 零一万物
return <Yi size={iconSize} />;
case 35: // MiniMax
return <Minimax size={iconSize} />;
case 37: // Dify
return <Dify size={iconSize} />;
case 38: // Jina
return <Jina size={iconSize} />;
case 40: // SiliconCloud
return <SiliconCloud size={iconSize} />;
case 42: // Mistral AI
return <Mistral size={iconSize} />;
case 45: // 字节火山方舟、豆包通用
return <Doubao size={iconSize} />;
case 48: // xAI
return <XAI size={iconSize} />;
case 49: // Coze
return <Coze size={iconSize} />;
case 50: // 可灵 Kling
return <Kling size={iconSize} />;
case 51: // 即梦 Jimeng
return <Jimeng size={iconSize} />;
case 54: // 豆包视频 Doubao Video
return <Doubao size={iconSize} />;
case 56: // Replicate
return <Replicate size={iconSize} />;
case 8: // 自定义渠道
case 22: // 知识库:FastGPT
return <FastGPT size={iconSize} />;
case 21: // 知识库:AI Proxy
case 44: // 嵌入模型:MokaAI M3E
default:
return null; // 未知类型或自定义渠道不显示图标
}
}
/**
* 根据图标名称动态获取 LobeHub 图标组件
* 支持:
* - 基础:"OpenAI"、"OpenAI.Color" 等
* - 额外属性(点号链式):"OpenAI.Avatar.type={'platform'}"、"OpenRouter.Avatar.shape={'square'}"
* - 继续兼容第二参数 size;若字符串里有 size=,以字符串为准
* @param {string} iconName - 图标名称/描述
* @param {number} size - 图标大小,默认为 14
* @returns {JSX.Element} - 对应的图标组件或 Avatar
*/
export function getLobeHubIcon(iconName, size = 14) {
return (
<Suspense
fallback={
<span style={{ display: 'inline-block', width: size, height: size }} />
}
>
<LobeHubIcon iconName={iconName} size={size} />
</Suspense>
);
}
// 颜色列表
const colors = [
'amber',
'blue',
'cyan',
'green',
'grey',
'indigo',
'light-blue',
'lime',
'orange',
'pink',
'purple',
'red',
'teal',
'violet',
'yellow',
];
// 基础10色色板 (N ≤ 10)
const baseColors = [
'#1664FF', // 主色
'#1AC6FF',
'#FF8A00',
'#3CC780',
'#7442D4',
'#FFC400',
'#304D77',
'#B48DEB',
'#009488',
'#FF7DDA',
];
// 扩展20色色板 (10 < N ≤ 20)
const extendedColors = [
'#1664FF',
'#B2CFFF',
'#1AC6FF',
'#94EFFF',
'#FF8A00',
'#FFCE7A',
'#3CC780',
'#B9EDCD',
'#7442D4',
'#DDC5FA',
'#FFC400',
'#FAE878',
'#304D77',
'#8B959E',
'#B48DEB',
'#EFE3FF',
'#009488',
'#59BAA8',
'#FF7DDA',
'#FFCFEE',
];
// 模型颜色映射
export const modelColorMap = {
'dall-e': 'rgb(147,112,219)', // 深紫色
// 'dall-e-2': 'rgb(147,112,219)', // 介于紫色和蓝色之间的色调
'dall-e-3': 'rgb(153,50,204)', // 介于紫罗兰和洋红之间的色调
'gpt-3.5-turbo': 'rgb(184,227,167)', // 浅绿色
// 'gpt-3.5-turbo-0301': 'rgb(131,220,131)', // 亮绿色
'gpt-3.5-turbo-0613': 'rgb(60,179,113)', // 海洋绿
'gpt-3.5-turbo-1106': 'rgb(32,178,170)', // 浅海洋绿
'gpt-3.5-turbo-16k': 'rgb(149,252,206)', // 淡橙色
'gpt-3.5-turbo-16k-0613': 'rgb(119,255,214)', // 淡桃
'gpt-3.5-turbo-instruct': 'rgb(175,238,238)', // 粉蓝色
'gpt-4': 'rgb(135,206,235)', // 天蓝色
// 'gpt-4-0314': 'rgb(70,130,180)', // 钢蓝色
'gpt-4-0613': 'rgb(100,149,237)', // 矢车菊蓝
'gpt-4-1106-preview': 'rgb(30,144,255)', // 道奇蓝
'gpt-4-0125-preview': 'rgb(2,177,236)', // 深天蓝
'gpt-4-turbo-preview': 'rgb(2,177,255)', // 深天蓝
'gpt-4-32k': 'rgb(104,111,238)', // 中紫色
// 'gpt-4-32k-0314': 'rgb(90,105,205)', // 暗灰蓝色
'gpt-4-32k-0613': 'rgb(61,71,139)', // 暗蓝灰色
'gpt-4-all': 'rgb(65,105,225)', // 皇家蓝
'gpt-4-gizmo-*': 'rgb(0,0,255)', // 纯蓝色
'gpt-4-vision-preview': 'rgb(25,25,112)', // 午夜蓝
'text-ada-001': 'rgb(255,192,203)', // 粉红色
'text-babbage-001': 'rgb(255,160,122)', // 浅珊瑚色
'text-curie-001': 'rgb(219,112,147)', // 苍紫罗兰色
// 'text-davinci-002': 'rgb(199,21,133)', // 中紫罗兰红色
'text-davinci-003': 'rgb(219,112,147)', // 苍紫罗兰色(与Curie相同,表示同一个系列)
'text-davinci-edit-001': 'rgb(255,105,180)', // 热粉色
'text-embedding-ada-002': 'rgb(255,182,193)', // 浅粉红
'text-embedding-v1': 'rgb(255,174,185)', // 浅粉红色(略有区别)
'text-moderation-latest': 'rgb(255,130,171)', // 强粉色
'text-moderation-stable': 'rgb(255,160,122)', // 浅珊瑚色(与Babbage相同,表示同一类功能)
'tts-1': 'rgb(255,140,0)', // 深橙色
'tts-1-1106': 'rgb(255,165,0)', // 橙色
'tts-1-hd': 'rgb(255,215,0)', // 金色
'tts-1-hd-1106': 'rgb(255,223,0)', // 金黄色(略有区别)
'whisper-1': 'rgb(245,245,220)', // 米色
'claude-3-opus-20240229': 'rgb(255,132,31)', // 橙红色
'claude-3-sonnet-20240229': 'rgb(253,135,93)', // 橙色
'claude-3-haiku-20240307': 'rgb(255,175,146)', // 浅橙色
'claude-2.1': 'rgb(255,209,190)', // 浅橙色(略有区别)
};
export function modelToColor(modelName) {
// 防止 undefined/null 导致错误
if (!modelName) {
return '#6366f1'; // 默认颜色
}
// 1. 如果模型在预定义的 modelColorMap 中,使用预定义颜色
if (modelColorMap[modelName]) {
return modelColorMap[modelName];
}
// 2. 生成一个稳定的数字作为索引
let hash = 0;
for (let i = 0; i < modelName.length; i++) {
hash = (hash << 5) - hash + modelName.charCodeAt(i);
hash = hash & hash; // Convert to 32-bit integer
}
hash = Math.abs(hash);
// 3. 根据模型名称长度选择不同的色板
const colorPalette = modelName.length > 10 ? extendedColors : baseColors;
// 4. 使用hash值选择颜色
const index = hash % colorPalette.length;
return colorPalette[index];
}
export function stringToColor(str) {
let sum = 0;
for (let i = 0; i < str.length; i++) {
sum += str.charCodeAt(i);
}
let i = sum % colors.length;
return colors[i];
}
// 渲染带有模型图标的标签
export function renderModelTag(modelName, options = {}) {
const {
color,
size = 'default',
shape = 'circle',
onClick,
suffixIcon,
} = options;
const categories = getModelCategories(i18next.t);
let icon = null;
for (const [key, category] of Object.entries(categories)) {
if (key !== 'all' && category.filter({ model_name: modelName })) {
icon = category.icon;
break;
}
}
return (
<Tag
color={color || stringToColor(modelName)}
prefixIcon={icon}
suffixIcon={suffixIcon}
size={size}
shape={shape}
onClick={onClick}
>
{modelName}
</Tag>
);
}
/**
* Render group tags based on the input group string
* @param {string} group - The input group string
* @returns {JSX.Element} - The rendered group tags
*/
export function renderGroup(group) {
if (group === '') {
return (
<Tag key='default' color='white' shape='circle'>
{i18next.t('用户分组')}
</Tag>
);
}
const tagColors = {
vip: 'yellow',
pro: 'yellow',
svip: 'red',
premium: 'red',
};
const groups = group.split(',').sort();
return (
<span key={group}>
{groups.map((group) => (
<Tag
color={tagColors[group] || stringToColor(group)}
key={group}
shape='circle'
onClick={async (event) => {
event.stopPropagation();
if (await copy(group)) {
showSuccess(i18next.t('已复制:') + group);
} else {
Modal.error({
title: i18next.t('无法复制到剪贴板,请手动复制'),
content: group,
});
}
}}
>
{group}
</Tag>
))}
</span>
);
}
export const renderGroupOption = (item) => {
const {
disabled,
selected,
label,
value,
focused,
className,
style,
onMouseEnter,
onClick,
empty,
emptyContent,
...rest
} = item;
const description =
rest.description ?? rest.fullLabel ?? (label !== value ? label : '');
const baseStyle = {
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
padding: '8px 16px',
cursor: disabled ? 'not-allowed' : 'pointer',
backgroundColor: focused ? 'var(--ui-color-fill-0)' : 'transparent',
opacity: disabled ? 0.5 : 1,
...(selected && {
backgroundColor: 'var(--ui-color-primary-light-default)',
}),
'&:hover': {
backgroundColor: !disabled && 'var(--ui-color-fill-1)',
},
};
const handleClick = () => {
if (!disabled && onClick) {
onClick();
}
};
const handleMouseEnter = (e) => {
if (!disabled && onMouseEnter) {
onMouseEnter(e);
}
};
return (
<div
style={baseStyle}
onClick={handleClick}
onMouseEnter={handleMouseEnter}
>
<div style={{ display: 'flex', flexDirection: 'column', gap: '4px' }}>
<Typography.Text strong type={disabled ? 'tertiary' : undefined}>
{value}
</Typography.Text>
{description && (
<Typography.Text type='secondary' size='small'>
{description}
</Typography.Text>
)}
</div>
</div>
);
};
// renderModelOption 渲染体验中心/playground 的模型下拉项,支持等级锁态(level_locked=仅可见)。
export const renderModelOption = (item) => {
const {
disabled,
selected,
value,
focused,
onMouseEnter,
onClick,
level_locked,
} = item;
const locked = disabled || level_locked;
const baseStyle = {
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
padding: '8px 16px',
cursor: locked ? 'not-allowed' : 'pointer',
backgroundColor: focused ? 'var(--ui-color-fill-0)' : 'transparent',
opacity: locked ? 0.6 : 1,
...(selected && {
backgroundColor: 'var(--ui-color-primary-light-default)',
}),
};
const handleClick = () => {
if (!locked && onClick) onClick();
};
const handleMouseEnter = (e) => {
if (!locked && onMouseEnter) onMouseEnter(e);
};
return (
<div
style={baseStyle}
onClick={handleClick}
onMouseEnter={handleMouseEnter}
>
<Typography.Text strong type={locked ? 'tertiary' : undefined}>
{value}
</Typography.Text>
{level_locked && (
<Typography.Text
type='tertiary'
size='small'
style={{ display: 'flex', alignItems: 'center', gap: '4px' }}
>
<Lock size={12} />
{i18next.t('仅可见')}
</Typography.Text>
)}
</div>
);
};
export function renderNumber(num) {
if (num >= 1000000000) {
return (num / 1000000000).toFixed(1) + 'B';
} else if (num >= 1000000) {
return (num / 1000000).toFixed(1) + 'M';
} else if (num >= 10000) {
return (num / 1000).toFixed(1) + 'k';
} else {
return String(num);
}
}
export function getQuotaPerUnit() {
let quotaPerUnit = localStorage.getItem('quota_per_unit');
quotaPerUnit = parseFloat(quotaPerUnit);
return quotaPerUnit;
}
export function renderUnitWithQuota(quota) {
let quotaPerUnit = localStorage.getItem('quota_per_unit');
quotaPerUnit = parseFloat(quotaPerUnit);
quota = parseFloat(quota);
return quotaPerUnit * quota;
}
export function getQuotaWithUnit(quota, digits = 6) {
let quotaPerUnit = localStorage.getItem('quota_per_unit');
quotaPerUnit = parseFloat(quotaPerUnit);
return (quota / quotaPerUnit).toFixed(digits);
}
export function renderQuotaWithAmount(amount) {
const quotaDisplayType = localStorage.getItem('quota_display_type') || 'USD';
if (quotaDisplayType === 'TOKENS') {
return renderNumber(renderUnitWithQuota(amount));
}
if (quotaDisplayType === 'CNY') {
return getCurrencySymbol('CNY') + amount;
} else if (quotaDisplayType === 'CUSTOM') {
return getCurrencySymbol('CUSTOM') + amount;
}
return getCurrencySymbol('USD') + amount;
}
/**
* 获取当前货币配置信息
* @returns {Object} - { symbol, rate, type }
*/
export function getCurrencyConfig() {
const quotaDisplayType = localStorage.getItem('quota_display_type') || 'USD';
const statusStr = localStorage.getItem('status');
let symbol = getCurrencySymbol('USD');
let rate = 1;
if (quotaDisplayType === 'CNY') {
symbol = getCurrencySymbol('CNY');
try {
if (statusStr) {
const s = JSON.parse(statusStr);
// 注意:如果设置了汇率值(包括1),使用设置值;只有未设置(undefined/null)时才用默认值1
rate =
s?.usd_exchange_rate !== undefined && s?.usd_exchange_rate !== null
? s.usd_exchange_rate
: 1;
}
} catch (e) {}
} else if (quotaDisplayType === 'CUSTOM') {
try {
if (statusStr) {
const s = JSON.parse(statusStr);
symbol = getCurrencySymbol('CUSTOM', s?.custom_currency_symbol);
rate = s?.custom_currency_exchange_rate || 1;
}
} catch (e) {
symbol = getCurrencySymbol('CUSTOM');
}
}
return { symbol, rate, type: quotaDisplayType };
}
/**
* 将美元金额转换为当前选择的货币
* @param {number} usdAmount - 美元金额
* @param {number} digits - 小数位数
* @returns {string} - 格式化后的货币字符串
*/
export function convertUSDToCurrency(usdAmount, digits = 2) {
const { symbol, rate } = getCurrencyConfig();
const convertedAmount = usdAmount * rate;
return symbol + convertedAmount.toFixed(digits);
}
export function renderQuota(quota, digits = 2) {
let quotaPerUnit = localStorage.getItem('quota_per_unit');
const quotaDisplayType = localStorage.getItem('quota_display_type') || 'USD';
quotaPerUnit = parseFloat(quotaPerUnit);
if (quotaDisplayType === 'TOKENS') {
return renderNumber(quota);
}
const resultUSD = quota / quotaPerUnit;
let symbol = getCurrencySymbol('USD');
let value = resultUSD;
if (quotaDisplayType === 'CNY') {
const statusStr = localStorage.getItem('status');
let usdRate = 1;
try {
if (statusStr) {
const s = JSON.parse(statusStr);
usdRate = s?.usd_exchange_rate || 1;
}
} catch (e) {}
value = resultUSD * usdRate;
symbol = getCurrencySymbol('CNY');
} else if (quotaDisplayType === 'CUSTOM') {
const statusStr = localStorage.getItem('status');
let symbolCustom = getCurrencySymbol('CUSTOM');
let rate = 1;
try {
if (statusStr) {
const s = JSON.parse(statusStr);
symbolCustom = getCurrencySymbol('CUSTOM', s?.custom_currency_symbol);
rate = s?.custom_currency_exchange_rate || rate;
}
} catch (e) {}
value = resultUSD * rate;
symbol = symbolCustom;
}
const fixedResult = value.toFixed(digits);
if (parseFloat(fixedResult) === 0 && quota > 0 && value > 0) {
const minValue = Math.pow(10, -digits);
return symbol + minValue.toFixed(digits);
}
return symbol + fixedResult;
}
// 额度输入控件的单位信息:跟随站点展示口径(与 renderQuota 一致)。
// isToken=true 表示 TOKENS 展示模式,此时输入/展示均为原始额度单位。
export function getQuotaInputUnit() {
const { symbol, type } = getCurrencyConfig();
return { isToken: type === 'TOKENS', symbol };
}
// renderQuota 的精确逆运算:把用户以「展示货币」输入的金额换算回内部额度单位。
// TOKENS 展示模式下 renderQuota 直接显示原始额度,此时仅取整不换算。
export function displayAmountToQuota(amount) {
const num = Number(amount) || 0;
const { rate, type } = getCurrencyConfig();
if (type === 'TOKENS') return Math.round(num);
const qpu = getQuotaPerUnit() || 500000;
return Math.round((num / (rate || 1)) * qpu);
}
// displayAmountToQuota 的反向:内部额度 → 展示货币数值(用于编辑表单回填)。
// 保留 6 位小数避免浮点噪声,同时保证与 displayAmountToQuota 往返稳定。
export function quotaToDisplayAmount(quota) {
const num = Number(quota) || 0;
const { rate, type } = getCurrencyConfig();
if (type === 'TOKENS') return num;
const qpu = getQuotaPerUnit() || 500000;
return Number(((num / qpu) * (rate || 1)).toFixed(6));
}
// 导出专用:消耗额度始终渲染为「货币金额」(带 ¥ 符号、完整精度)。
// 普通货币模式直接复用 renderQuota(导出与页面显示一致);
// 但 quota_display_type=TOKENS 时 renderQuota 会返回「缩写的 token 数」(有损、且无 ¥ 符号),
// 不符合导出「以人民币金额导出」的预期,这里改为按金额输出(与 USD 模式等价的 ¥ 数值)。
export function renderExportQuota(quota, digits = 6) {
const quotaDisplayType = localStorage.getItem('quota_display_type') || 'USD';
if (quotaDisplayType !== 'TOKENS') {
return renderQuota(quota, digits);
}
let quotaPerUnit = parseFloat(localStorage.getItem('quota_per_unit'));
if (!Number.isFinite(quotaPerUnit) || quotaPerUnit <= 0) {
quotaPerUnit = 500000;
}
return getCurrencySymbol() + (quota / quotaPerUnit).toFixed(digits);
}
function isValidGroupRatio(ratio) {
return Number.isFinite(ratio) && ratio !== -1;
}
/**
* Helper function to get effective ratio and label
* @param {number} groupRatio - The default group ratio
* @param {number} user_group_ratio - The user-specific group ratio
* @returns {Object} - Object containing { ratio, label, useUserGroupRatio }
*/
function getEffectiveRatio(groupRatio, user_group_ratio) {
const useUserGroupRatio = isValidGroupRatio(user_group_ratio);
const ratioLabel = useUserGroupRatio
? i18next.t('专属倍率')
: i18next.t('分组倍率');
const effectiveRatio = useUserGroupRatio ? user_group_ratio : groupRatio;
return {
ratio: effectiveRatio,
label: ratioLabel,
useUserGroupRatio: useUserGroupRatio,
};
}
const isFiniteNumber = (value) =>
typeof value === 'number' && Number.isFinite(value);
// Shared core for simple price rendering (used by OpenAI-like and Claude-like variants)
function renderPriceSimpleCore({
modelRatio,
modelPrice = -1,
groupRatio,
user_group_ratio,
cacheTokens = 0,
cacheRatio = 1.0,
cacheCreationTokens = 0,
cacheCreationRatio = 1.0,
cacheCreationTokens5m = 0,
cacheCreationRatio5m = 1.0,
cacheCreationTokens1h = 0,
cacheCreationRatio1h = 1.0,
image = false,
imageRatio = 1.0,
isSystemPromptOverride = false,
}) {
const { ratio: effectiveGroupRatio, label: ratioLabel } = getEffectiveRatio(
groupRatio,
user_group_ratio,
);
const finalGroupRatio = effectiveGroupRatio;
if (isFiniteNumber(modelPrice) && modelPrice !== -1) {
const { symbol } = getCurrencyConfig();
return i18next.t('模型价格 {{symbol}}{{price}},{{ratioType}} {{ratio}}', {
symbol,
price: modelPrice,
ratioType: ratioLabel,
ratio: finalGroupRatio,
});
}
const hasSplitCacheCreation =
cacheCreationTokens5m > 0 || cacheCreationTokens1h > 0;
const shouldShowLegacyCacheCreation =
!hasSplitCacheCreation && cacheCreationTokens !== 0;
const shouldShowCache = cacheTokens !== 0;
const shouldShowCacheCreation5m =
hasSplitCacheCreation && cacheCreationTokens5m > 0;
const shouldShowCacheCreation1h =
hasSplitCacheCreation && cacheCreationTokens1h > 0;
const parts = [];
// base: model ratio
parts.push(i18next.t('模型: {{ratio}}'));
// cache part (label differs when with image)
if (shouldShowCache) {
parts.push(i18next.t('缓存: {{cacheRatio}}'));
}
if (hasSplitCacheCreation) {
if (shouldShowCacheCreation5m && shouldShowCacheCreation1h) {
parts.push(
i18next.t(
'缓存创建: 5m {{cacheCreationRatio5m}} / 1h {{cacheCreationRatio1h}}',
),
);
} else if (shouldShowCacheCreation5m) {
parts.push(i18next.t('缓存创建: 5m {{cacheCreationRatio5m}}'));
} else if (shouldShowCacheCreation1h) {
parts.push(i18next.t('缓存创建: 1h {{cacheCreationRatio1h}}'));
}
} else if (shouldShowLegacyCacheCreation) {
parts.push(i18next.t('缓存创建: {{cacheCreationRatio}}'));
}
// image part
if (image) {
parts.push(i18next.t('图片输入: {{imageRatio}}'));
}
parts.push(`{{ratioType}}: {{groupRatio}}`);
let result = i18next.t(parts.join(' * '), {
ratio: modelRatio,
ratioType: ratioLabel,
groupRatio: finalGroupRatio,
cacheRatio: cacheRatio,
cacheCreationRatio: cacheCreationRatio,
cacheCreationRatio5m: cacheCreationRatio5m,
cacheCreationRatio1h: cacheCreationRatio1h,
imageRatio: imageRatio,
});
if (isSystemPromptOverride) {
result += '\n\r' + i18next.t('系统提示覆盖');
}
return result;
}
export function renderModelPrice(
inputTokens,
completionTokens,
modelRatio,
modelPrice = -1,
completionRatio,
groupRatio,
user_group_ratio,
cacheTokens = 0,
cacheRatio = 1.0,
image = false,
imageRatio = 1.0,
imageOutputTokens = 0,
webSearch = false,
webSearchCallCount = 0,
webSearchPrice = 0,
fileSearch = false,
fileSearchCallCount = 0,
fileSearchPrice = 0,
audioInputSeperatePrice = false,
audioInputTokens = 0,
audioInputPrice = 0,
imageGenerationCall = false,
imageGenerationCallPrice = 0,
) {
const { ratio: effectiveGroupRatio, label: ratioLabel } = getEffectiveRatio(
groupRatio,
user_group_ratio,
);
groupRatio = effectiveGroupRatio;
// 获取货币配置
const { symbol, rate } = getCurrencyConfig();
if (isFiniteNumber(modelPrice) && modelPrice !== -1) {
const displayPrice = (modelPrice * rate).toFixed(6);
const displayTotal = (modelPrice * groupRatio * rate).toFixed(6);
return i18next.t(
'模型价格:{{symbol}}{{price}} * {{ratioType}}:{{ratio}} = {{symbol}}{{total}}',
{
symbol: symbol,
price: displayPrice,
ratio: groupRatio,
total: displayTotal,
ratioType: ratioLabel,
},
);
} else {
if (!isFiniteNumber(modelRatio)) {
return i18next.t('计费信息缺失,以实际扣费为准');
}
if (completionRatio === undefined) {
completionRatio = 0;
}
let inputRatioPrice = modelRatio * 2.0;
let completionRatioPrice = modelRatio * 2.0 * completionRatio;
let cacheRatioPrice = modelRatio * 2.0 * cacheRatio;
let imageRatioPrice = modelRatio * 2.0 * imageRatio;
// Calculate effective input tokens (non-cached + cached with ratio applied)
// 注意:部分提供商的 inputTokens 不包含 cacheTokens,此时不应再减去
const nonCacheInputTokens =
inputTokens >= cacheTokens ? inputTokens - cacheTokens : inputTokens;
let effectiveInputTokens = nonCacheInputTokens + cacheTokens * cacheRatio;
// Handle image tokens if present
if (image && imageOutputTokens > 0) {
const nonImageInputTokens =
inputTokens >= imageOutputTokens
? inputTokens - imageOutputTokens
: inputTokens;
effectiveInputTokens =
nonImageInputTokens + imageOutputTokens * imageRatio;
}
if (audioInputTokens > 0) {
if (effectiveInputTokens >= audioInputTokens) {
effectiveInputTokens -= audioInputTokens;
}
}
let price =
(effectiveInputTokens / 1000000) * inputRatioPrice * groupRatio +
(audioInputTokens / 1000000) * audioInputPrice * groupRatio +
(completionTokens / 1000000) * completionRatioPrice * groupRatio +
(webSearchCallCount / 1000) * webSearchPrice * groupRatio +
(fileSearchCallCount / 1000) * fileSearchPrice * groupRatio +
imageGenerationCallPrice * groupRatio;
return (
<>
<article>
<p>
{i18next.t(
'输入价格:{{symbol}}{{price}} / 1M tokens{{audioPrice}}',
{
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
audioPrice: audioInputSeperatePrice
? `,音频 ${symbol}${(audioInputPrice * rate).toFixed(6)} / 1M tokens`
: '',
},
)}
</p>
<p>
{i18next.t(
'输出价格:{{symbol}}{{price}} * {{completionRatio}} = {{symbol}}{{total}} / 1M tokens (补全倍率: {{completionRatio}})',
{
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
total: (completionRatioPrice * rate).toFixed(6),
completionRatio: completionRatio,
},
)}
</p>
{cacheTokens > 0 && (
<p>
{i18next.t(
'缓存价格:{{symbol}}{{price}} * {{cacheRatio}} = {{symbol}}{{total}} / 1M tokens (缓存倍率: {{cacheRatio}})',
{
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
total: (inputRatioPrice * cacheRatio * rate).toFixed(6),
cacheRatio: cacheRatio,
},
)}
</p>
)}
{image && imageOutputTokens > 0 && (
<p>
{i18next.t(
'图片输入价格:{{symbol}}{{price}} * {{ratio}} = {{symbol}}{{total}} / 1M tokens (图片倍率: {{imageRatio}})',
{
symbol: symbol,
price: (imageRatioPrice * rate).toFixed(6),
ratio: groupRatio,
total: (imageRatioPrice * groupRatio * rate).toFixed(6),
imageRatio: imageRatio,
},
)}
</p>
)}
{webSearch && webSearchCallCount > 0 && (
<p>
{i18next.t('Web搜索价格:{{symbol}}{{price}} / 1K 次', {
symbol: symbol,
price: (webSearchPrice * rate).toFixed(6),
})}
</p>
)}
{fileSearch && fileSearchCallCount > 0 && (
<p>
{i18next.t('文件搜索价格:{{symbol}}{{price}} / 1K 次', {
symbol: symbol,
price: (fileSearchPrice * rate).toFixed(6),
})}
</p>
)}
{imageGenerationCall && imageGenerationCallPrice > 0 && (
<p>
{i18next.t('图片生成调用:{{symbol}}{{price}} / 1次', {
symbol: symbol,
price: (imageGenerationCallPrice * rate).toFixed(6),
})}
</p>
)}
<p>
{(() => {
// 构建输入部分描述
let inputDesc = '';
if (image && imageOutputTokens > 0) {
inputDesc = i18next.t(
'(输入 {{nonImageInput}} tokens + 图片输入 {{imageInput}} tokens * {{imageRatio}} / 1M tokens * {{symbol}}{{price}}',
{
nonImageInput:
inputTokens >= imageOutputTokens
? inputTokens - imageOutputTokens
: inputTokens,
imageInput: imageOutputTokens,
imageRatio: imageRatio,
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
},
);
} else if (cacheTokens > 0) {
inputDesc = i18next.t(
'(输入 {{nonCacheInput}} tokens / 1M tokens * {{symbol}}{{price}} + 缓存 {{cacheInput}} tokens / 1M tokens * {{symbol}}{{cachePrice}}',
{
nonCacheInput:
inputTokens >= cacheTokens
? inputTokens - cacheTokens
: inputTokens,
cacheInput: cacheTokens,
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
cachePrice: (cacheRatioPrice * rate).toFixed(6),
},
);
} else if (audioInputSeperatePrice && audioInputTokens > 0) {
inputDesc = i18next.t(
'(输入 {{nonAudioInput}} tokens / 1M tokens * {{symbol}}{{price}} + 音频输入 {{audioInput}} tokens / 1M tokens * {{symbol}}{{audioPrice}}',
{
nonAudioInput:
inputTokens >= audioInputTokens
? inputTokens - audioInputTokens
: inputTokens,
audioInput: audioInputTokens,
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
audioPrice: (audioInputPrice * rate).toFixed(6),
},
);
} else {
inputDesc = i18next.t(
'(输入 {{input}} tokens / 1M tokens * {{symbol}}{{price}}',
{
input: inputTokens,
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
},
);
}
// 构建输出部分描述
const outputDesc = i18next.t(
'输出 {{completion}} tokens / 1M tokens * {{symbol}}{{compPrice}}) * {{ratioType}} {{ratio}}',
{
completion: completionTokens,
symbol: symbol,
compPrice: (completionRatioPrice * rate).toFixed(6),
ratio: groupRatio,
ratioType: ratioLabel,
},
);
// 构建额外服务描述
const extraServices = [
webSearch && webSearchCallCount > 0
? i18next.t(
' + Web搜索 {{count}}次 / 1K 次 * {{symbol}}{{price}} * {{ratioType}} {{ratio}}',
{
count: webSearchCallCount,
symbol: symbol,
price: (webSearchPrice * rate).toFixed(6),
ratio: groupRatio,
ratioType: ratioLabel,
},
)
: '',
fileSearch && fileSearchCallCount > 0
? i18next.t(
' + 文件搜索 {{count}}次 / 1K 次 * {{symbol}}{{price}} * {{ratioType}} {{ratio}}',
{
count: fileSearchCallCount,
symbol: symbol,
price: (fileSearchPrice * rate).toFixed(6),
ratio: groupRatio,
ratioType: ratioLabel,
},
)
: '',
imageGenerationCall && imageGenerationCallPrice > 0
? i18next.t(
' + 图片生成调用 {{symbol}}{{price}} / 1次 * {{ratioType}} {{ratio}}',
{
symbol: symbol,
price: (imageGenerationCallPrice * rate).toFixed(6),
ratio: groupRatio,
ratioType: ratioLabel,
},
)
: '',
].join('');
return i18next.t(
'{{inputDesc}} + {{outputDesc}}{{extraServices}} = {{symbol}}{{total}}',
{
inputDesc,
outputDesc,
extraServices,
symbol: symbol,
total: (price * rate).toFixed(6),
},
);
})()}
</p>
<p>{i18next.t('仅供参考,以实际扣费为准')}</p>
</article>
</>
);
}
}
export function renderLogContent(
modelRatio,
completionRatio,
modelPrice = -1,
groupRatio,
user_group_ratio,
cacheRatio = 1.0,
image = false,
imageRatio = 1.0,
webSearch = false,
webSearchCallCount = 0,
fileSearch = false,
fileSearchCallCount = 0,
) {
const { ratio, label: ratioLabel } = getEffectiveRatio(
groupRatio,
user_group_ratio,
);
// 获取货币配置
const { symbol, rate } = getCurrencyConfig();
if (isFiniteNumber(modelPrice) && modelPrice !== -1) {
return i18next.t('模型价格 {{symbol}}{{price}},{{ratioType}} {{ratio}}', {
symbol: symbol,
price: (modelPrice * rate).toFixed(6),
ratioType: ratioLabel,
ratio,
});
} else {
if (!isFiniteNumber(modelRatio)) {
return i18next.t('计费信息缺失,以实际扣费为准');
}
if (image) {
return i18next.t(
'模型倍率 {{modelRatio}},缓存倍率 {{cacheRatio}},输出倍率 {{completionRatio}},图片输入倍率 {{imageRatio}},{{ratioType}} {{ratio}}',
{
modelRatio: modelRatio,
cacheRatio: cacheRatio,
completionRatio: completionRatio,
imageRatio: imageRatio,
ratioType: ratioLabel,
ratio,
},
);
} else if (webSearch) {
return i18next.t(
'模型倍率 {{modelRatio}},缓存倍率 {{cacheRatio}},输出倍率 {{completionRatio}},{{ratioType}} {{ratio}},Web 搜索调用 {{webSearchCallCount}} 次',
{
modelRatio: modelRatio,
cacheRatio: cacheRatio,
completionRatio: completionRatio,
ratioType: ratioLabel,
ratio,
webSearchCallCount,
},
);
} else {
return i18next.t(
'模型倍率 {{modelRatio}},缓存倍率 {{cacheRatio}},输出倍率 {{completionRatio}},{{ratioType}} {{ratio}}',
{
modelRatio: modelRatio,
cacheRatio: cacheRatio,
completionRatio: completionRatio,
ratioType: ratioLabel,
ratio,
},
);
}
}
}
export function renderModelPriceSimple(
modelRatio,
modelPrice = -1,
groupRatio,
user_group_ratio,
cacheTokens = 0,
cacheRatio = 1.0,
cacheCreationTokens = 0,
cacheCreationRatio = 1.0,
cacheCreationTokens5m = 0,
cacheCreationRatio5m = 1.0,
cacheCreationTokens1h = 0,
cacheCreationRatio1h = 1.0,
image = false,
imageRatio = 1.0,
isSystemPromptOverride = false,
provider = 'openai',
) {
return renderPriceSimpleCore({
modelRatio,
modelPrice,
groupRatio,
user_group_ratio,
cacheTokens,
cacheRatio,
cacheCreationTokens,
cacheCreationRatio,
cacheCreationTokens5m,
cacheCreationRatio5m,
cacheCreationTokens1h,
cacheCreationRatio1h,
image,
imageRatio,
isSystemPromptOverride,
});
}
export function renderAudioModelPrice(
inputTokens,
completionTokens,
modelRatio,
modelPrice = -1,
completionRatio,
audioInputTokens,
audioCompletionTokens,
audioRatio,
audioCompletionRatio,
groupRatio,
user_group_ratio,
cacheTokens = 0,
cacheRatio = 1.0,
) {
const { ratio: effectiveGroupRatio, label: ratioLabel } = getEffectiveRatio(
groupRatio,
user_group_ratio,
);
groupRatio = effectiveGroupRatio;
// 获取货币配置
const { symbol, rate } = getCurrencyConfig();
// 1 ratio = $0.002 / 1K tokens
if (isFiniteNumber(modelPrice) && modelPrice !== -1) {
return i18next.t(
'模型价格:{{symbol}}{{price}} * {{ratioType}}:{{ratio}} = {{symbol}}{{total}}',
{
symbol: symbol,
price: (modelPrice * rate).toFixed(6),
ratio: groupRatio,
total: (modelPrice * groupRatio * rate).toFixed(6),
ratioType: ratioLabel,
},
);
} else {
if (!isFiniteNumber(modelRatio)) {
return i18next.t('计费信息缺失,以实际扣费为准');
}
if (completionRatio === undefined) {
completionRatio = 0;
}
// try toFixed audioRatio
audioRatio = parseFloat(audioRatio).toFixed(6);
// 这里的 *2 是因为 1倍率=0.002刀,请勿删除
let inputRatioPrice = modelRatio * 2.0;
let completionRatioPrice = modelRatio * 2.0 * completionRatio;
// Calculate effective input tokens (non-cached + cached with ratio applied)
// 注意:部分提供商的 inputTokens 不包含 cacheTokens,此时不应再减去
const nonCacheInput =
inputTokens >= cacheTokens ? inputTokens - cacheTokens : inputTokens;
const effectiveInputTokens = nonCacheInput + cacheTokens * cacheRatio;
let textPrice =
(effectiveInputTokens / 1000000) * inputRatioPrice * groupRatio +
(completionTokens / 1000000) * completionRatioPrice * groupRatio;
let audioPrice =
(audioInputTokens / 1000000) * inputRatioPrice * audioRatio * groupRatio +
(audioCompletionTokens / 1000000) *
inputRatioPrice *
audioRatio *
audioCompletionRatio *
groupRatio;
let price = textPrice + audioPrice;
return (
<>
<article>
<p>
{i18next.t('提示价格:{{symbol}}{{price}} / 1M tokens', {
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
})}
</p>
<p>
{i18next.t(
'补全价格:{{symbol}}{{price}} * {{completionRatio}} = {{symbol}}{{total}} / 1M tokens (补全倍率: {{completionRatio}})',
{
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
total: (completionRatioPrice * rate).toFixed(6),
completionRatio: completionRatio,
},
)}
</p>
{cacheTokens > 0 && (
<p>
{i18next.t(
'缓存价格:{{symbol}}{{price}} * {{cacheRatio}} = {{symbol}}{{total}} / 1M tokens (缓存倍率: {{cacheRatio}})',
{
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
total: (inputRatioPrice * cacheRatio * rate).toFixed(6),
cacheRatio: cacheRatio,
},
)}
</p>
)}
<p>
{i18next.t(
'音频提示价格:{{symbol}}{{price}} * {{audioRatio}} = {{symbol}}{{total}} / 1M tokens (音频倍率: {{audioRatio}})',
{
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
total: (inputRatioPrice * audioRatio * rate).toFixed(6),
audioRatio: audioRatio,
},
)}
</p>
<p>
{i18next.t(
'音频补全价格:{{symbol}}{{price}} * {{audioRatio}} * {{audioCompRatio}} = {{symbol}}{{total}} / 1M tokens (音频补全倍率: {{audioCompRatio}})',
{
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
total: (
inputRatioPrice *
audioRatio *
audioCompletionRatio *
rate
).toFixed(6),
audioRatio: audioRatio,
audioCompRatio: audioCompletionRatio,
},
)}
</p>
<p>
{cacheTokens > 0
? i18next.t(
'文字提示 {{nonCacheInput}} tokens / 1M tokens * {{symbol}}{{price}} + 缓存 {{cacheInput}} tokens / 1M tokens * {{symbol}}{{cachePrice}} + 文字补全 {{completion}} tokens / 1M tokens * {{symbol}}{{compPrice}} = {{symbol}}{{total}}',
{
nonCacheInput:
inputTokens >= cacheTokens
? inputTokens - cacheTokens
: inputTokens,
cacheInput: cacheTokens,
symbol: symbol,
cachePrice: (inputRatioPrice * cacheRatio * rate).toFixed(
6,
),
price: (inputRatioPrice * rate).toFixed(6),
completion: completionTokens,
compPrice: (completionRatioPrice * rate).toFixed(6),
total: (textPrice * rate).toFixed(6),
},
)
: i18next.t(
'文字提示 {{input}} tokens / 1M tokens * {{symbol}}{{price}} + 文字补全 {{completion}} tokens / 1M tokens * {{symbol}}{{compPrice}} = {{symbol}}{{total}}',
{
input: inputTokens,
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
completion: completionTokens,
compPrice: (completionRatioPrice * rate).toFixed(6),
total: (textPrice * rate).toFixed(6),
},
)}
</p>
<p>
{i18next.t(
'音频提示 {{input}} tokens / 1M tokens * {{symbol}}{{audioInputPrice}} + 音频补全 {{completion}} tokens / 1M tokens * {{symbol}}{{audioCompPrice}} = {{symbol}}{{total}}',
{
input: audioInputTokens,
completion: audioCompletionTokens,
symbol: symbol,
audioInputPrice: (audioRatio * inputRatioPrice * rate).toFixed(
6,
),
audioCompPrice: (
audioRatio *
audioCompletionRatio *
inputRatioPrice *
rate
).toFixed(6),
total: (audioPrice * rate).toFixed(6),
},
)}
</p>
<p>
{i18next.t(
'总价:文字价格 {{textPrice}} + 音频价格 {{audioPrice}} = {{symbol}}{{total}}',
{
symbol: symbol,
total: (price * rate).toFixed(6),
textPrice: (textPrice * rate).toFixed(6),
audioPrice: (audioPrice * rate).toFixed(6),
},
)}
</p>
<p>{i18next.t('仅供参考,以实际扣费为准')}</p>
</article>
</>
);
}
}
export function renderQuotaWithPrompt(quota, digits) {
const quotaDisplayType = localStorage.getItem('quota_display_type') || 'USD';
if (quotaDisplayType !== 'TOKENS') {
return i18next.t('等价金额:') + renderQuota(quota, digits);
}
return '';
}
export function renderClaudeModelPrice(
inputTokens,
completionTokens,
modelRatio,
modelPrice = -1,
completionRatio,
groupRatio,
user_group_ratio,
cacheTokens = 0,
cacheRatio = 1.0,
cacheCreationTokens = 0,
cacheCreationRatio = 1.0,
cacheCreationTokens5m = 0,
cacheCreationRatio5m = 1.0,
cacheCreationTokens1h = 0,
cacheCreationRatio1h = 1.0,
) {
const { ratio: effectiveGroupRatio, label: ratioLabel } = getEffectiveRatio(
groupRatio,
user_group_ratio,
);
groupRatio = effectiveGroupRatio;
// 获取货币配置
const { symbol, rate } = getCurrencyConfig();
if (isFiniteNumber(modelPrice) && modelPrice !== -1) {
return i18next.t(
'模型价格:{{symbol}}{{price}} * {{ratioType}}:{{ratio}} = {{symbol}}{{total}}',
{
symbol: symbol,
price: (modelPrice * rate).toFixed(6),
ratioType: ratioLabel,
ratio: groupRatio,
total: (modelPrice * groupRatio * rate).toFixed(6),
},
);
} else {
if (!isFiniteNumber(modelRatio)) {
return i18next.t('计费信息缺失,以实际扣费为准');
}
if (completionRatio === undefined) {
completionRatio = 0;
}
const completionRatioValue = completionRatio || 0;
const inputRatioPrice = modelRatio * 2.0;
const completionRatioPrice = modelRatio * 2.0 * completionRatioValue;
const cacheRatioPrice = modelRatio * 2.0 * cacheRatio;
const cacheCreationRatioPrice = modelRatio * 2.0 * cacheCreationRatio;
const cacheCreationRatioPrice5m = modelRatio * 2.0 * cacheCreationRatio5m;
const cacheCreationRatioPrice1h = modelRatio * 2.0 * cacheCreationRatio1h;
const hasSplitCacheCreation =
cacheCreationTokens5m > 0 || cacheCreationTokens1h > 0;
const shouldShowCache = cacheTokens > 0;
const shouldShowLegacyCacheCreation =
!hasSplitCacheCreation && cacheCreationTokens > 0;
const shouldShowCacheCreation5m =
hasSplitCacheCreation && cacheCreationTokens5m > 0;
const shouldShowCacheCreation1h =
hasSplitCacheCreation && cacheCreationTokens1h > 0;
// Calculate effective input tokens (non-cached + cached with ratio applied + cache creation with ratio applied)
const nonCachedTokens = inputTokens;
const legacyCacheCreationTokens = hasSplitCacheCreation
? 0
: cacheCreationTokens;
const effectiveInputTokens =
nonCachedTokens +
cacheTokens * cacheRatio +
legacyCacheCreationTokens * cacheCreationRatio +
cacheCreationTokens5m * cacheCreationRatio5m +
cacheCreationTokens1h * cacheCreationRatio1h;
let price =
(effectiveInputTokens / 1000000) * inputRatioPrice * groupRatio +
(completionTokens / 1000000) * completionRatioPrice * groupRatio;
const inputUnitPrice = inputRatioPrice * rate;
const completionUnitPrice = completionRatioPrice * rate;
const cacheUnitPrice = cacheRatioPrice * rate;
const cacheCreationUnitPrice = cacheCreationRatioPrice * rate;
const cacheCreationUnitPrice5m = cacheCreationRatioPrice5m * rate;
const cacheCreationUnitPrice1h = cacheCreationRatioPrice1h * rate;
const cacheCreationUnitPriceTotal =
cacheCreationUnitPrice5m + cacheCreationUnitPrice1h;
const breakdownSegments = [
i18next.t('提示 {{input}} tokens / 1M tokens * {{symbol}}{{price}}', {
input: inputTokens,
symbol,
price: inputUnitPrice.toFixed(6),
}),
];
if (shouldShowCache) {
breakdownSegments.push(
i18next.t(
'缓存 {{tokens}} tokens / 1M tokens * {{symbol}}{{price}} (倍率: {{ratio}})',
{
tokens: cacheTokens,
symbol,
price: cacheUnitPrice.toFixed(6),
ratio: cacheRatio,
},
),
);
}
if (shouldShowLegacyCacheCreation) {
breakdownSegments.push(
i18next.t(
'缓存创建 {{tokens}} tokens / 1M tokens * {{symbol}}{{price}} (倍率: {{ratio}})',
{
tokens: cacheCreationTokens,
symbol,
price: cacheCreationUnitPrice.toFixed(6),
ratio: cacheCreationRatio,
},
),
);
}
if (shouldShowCacheCreation5m) {
breakdownSegments.push(
i18next.t(
'5m缓存创建 {{tokens}} tokens / 1M tokens * {{symbol}}{{price}} (倍率: {{ratio}})',
{
tokens: cacheCreationTokens5m,
symbol,
price: cacheCreationUnitPrice5m.toFixed(6),
ratio: cacheCreationRatio5m,
},
),
);
}
if (shouldShowCacheCreation1h) {
breakdownSegments.push(
i18next.t(
'1h缓存创建 {{tokens}} tokens / 1M tokens * {{symbol}}{{price}} (倍率: {{ratio}})',
{
tokens: cacheCreationTokens1h,
symbol,
price: cacheCreationUnitPrice1h.toFixed(6),
ratio: cacheCreationRatio1h,
},
),
);
}
breakdownSegments.push(
i18next.t(
'补全 {{completion}} tokens / 1M tokens * {{symbol}}{{price}}',
{
completion: completionTokens,
symbol,
price: completionUnitPrice.toFixed(6),
},
),
);
const breakdownText =
breakdownSegments.length > 1
? '(' + breakdownSegments.join(' + ') + ')'
: breakdownSegments.join(' + ');
return (
<>
<article>
<p>
{i18next.t('提示价格:{{symbol}}{{price}} / 1M tokens', {
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
})}
</p>
<p>
{i18next.t(
'补全价格:{{symbol}}{{price}} * {{ratio}} = {{symbol}}{{total}} / 1M tokens',
{
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
ratio: completionRatio,
total: (completionRatioPrice * rate).toFixed(6),
},
)}
</p>
{shouldShowCache && (
<p>
{i18next.t(
'缓存价格:{{symbol}}{{price}} * {{ratio}} = {{symbol}}{{total}} / 1M tokens (缓存倍率: {{cacheRatio}})',
{
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
ratio: cacheRatio,
total: cacheUnitPrice.toFixed(6),
cacheRatio: cacheRatio,
},
)}
</p>
)}
{shouldShowLegacyCacheCreation && (
<p>
{i18next.t(
'缓存创建价格:{{symbol}}{{price}} * {{ratio}} = {{symbol}}{{total}} / 1M tokens (缓存创建倍率: {{cacheCreationRatio}})',
{
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
ratio: cacheCreationRatio,
total: cacheCreationUnitPrice.toFixed(6),
cacheCreationRatio: cacheCreationRatio,
},
)}
</p>
)}
{shouldShowCacheCreation5m && (
<p>
{i18next.t(
'5m缓存创建价格:{{symbol}}{{price}} * {{ratio}} = {{symbol}}{{total}} / 1M tokens (5m缓存创建倍率: {{cacheCreationRatio5m}})',
{
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
ratio: cacheCreationRatio5m,
total: cacheCreationUnitPrice5m.toFixed(6),
cacheCreationRatio5m: cacheCreationRatio5m,
},
)}
</p>
)}
{shouldShowCacheCreation1h && (
<p>
{i18next.t(
'1h缓存创建价格:{{symbol}}{{price}} * {{ratio}} = {{symbol}}{{total}} / 1M tokens (1h缓存创建倍率: {{cacheCreationRatio1h}})',
{
symbol: symbol,
price: (inputRatioPrice * rate).toFixed(6),
ratio: cacheCreationRatio1h,
total: cacheCreationUnitPrice1h.toFixed(6),
cacheCreationRatio1h: cacheCreationRatio1h,
},
)}
</p>
)}
{shouldShowCacheCreation5m && shouldShowCacheCreation1h && (
<p>
{i18next.t(
'缓存创建价格合计:5m {{symbol}}{{five}} + 1h {{symbol}}{{one}} = {{symbol}}{{total}} / 1M tokens',
{
symbol: symbol,
five: cacheCreationUnitPrice5m.toFixed(6),
one: cacheCreationUnitPrice1h.toFixed(6),
total: cacheCreationUnitPriceTotal.toFixed(6),
},
)}
</p>
)}
<p></p>
<p>
{i18next.t(
'{{breakdown}} * {{ratioType}} {{ratio}} = {{symbol}}{{total}}',
{
breakdown: breakdownText,
ratioType: ratioLabel,
ratio: groupRatio,
symbol: symbol,
total: (price * rate).toFixed(6),
},
)}
</p>
<p>{i18next.t('仅供参考,以实际扣费为准')}</p>
</article>
</>
);
}
}
export function renderClaudeLogContent(
modelRatio,
completionRatio,
modelPrice = -1,
groupRatio,
user_group_ratio,
cacheRatio = 1.0,
cacheCreationRatio = 1.0,
cacheCreationTokens5m = 0,
cacheCreationRatio5m = 1.0,
cacheCreationTokens1h = 0,
cacheCreationRatio1h = 1.0,
) {
const { ratio: effectiveGroupRatio, label: ratioLabel } = getEffectiveRatio(
groupRatio,
user_group_ratio,
);
groupRatio = effectiveGroupRatio;
// 获取货币配置
const { symbol, rate } = getCurrencyConfig();
if (isFiniteNumber(modelPrice) && modelPrice !== -1) {
return i18next.t('模型价格 {{symbol}}{{price}},{{ratioType}} {{ratio}}', {
symbol: symbol,
price: (modelPrice * rate).toFixed(6),
ratioType: ratioLabel,
ratio: groupRatio,
});
} else {
if (!isFiniteNumber(modelRatio)) {
return i18next.t('计费信息缺失,以实际扣费为准');
}
const hasSplitCacheCreation =
cacheCreationTokens5m > 0 || cacheCreationTokens1h > 0;
const shouldShowCacheCreation5m =
hasSplitCacheCreation && cacheCreationTokens5m > 0;
const shouldShowCacheCreation1h =
hasSplitCacheCreation && cacheCreationTokens1h > 0;
let cacheCreationPart = null;
if (hasSplitCacheCreation) {
if (shouldShowCacheCreation5m && shouldShowCacheCreation1h) {
cacheCreationPart = i18next.t(
'缓存创建倍率 5m {{cacheCreationRatio5m}} / 1h {{cacheCreationRatio1h}}',
{
cacheCreationRatio5m,
cacheCreationRatio1h,
},
);
} else if (shouldShowCacheCreation5m) {
cacheCreationPart = i18next.t(
'缓存创建倍率 5m {{cacheCreationRatio5m}}',
{
cacheCreationRatio5m,
},
);
} else if (shouldShowCacheCreation1h) {
cacheCreationPart = i18next.t(
'缓存创建倍率 1h {{cacheCreationRatio1h}}',
{
cacheCreationRatio1h,
},
);
}
}
if (!cacheCreationPart) {
cacheCreationPart = i18next.t('缓存创建倍率 {{cacheCreationRatio}}', {
cacheCreationRatio,
});
}
const parts = [
i18next.t('模型倍率 {{modelRatio}}', { modelRatio }),
i18next.t('输出倍率 {{completionRatio}}', { completionRatio }),
i18next.t('缓存倍率 {{cacheRatio}}', { cacheRatio }),
cacheCreationPart,
i18next.t('{{ratioType}} {{ratio}}', {
ratioType: ratioLabel,
ratio: groupRatio,
}),
];
return parts.join(',');
}
}
// 已统一至 renderModelPriceSimple,若仍有遗留引用,请改为传入 provider='claude'
/**
* rehype 插件:将段落等文本节点拆分为逐词 <span>,并添加淡入动画 class。
* 仅在流式渲染阶段使用,避免已渲染文字重复动画。
*/
export function rehypeSplitWordsIntoSpans(options = {}) {
const { previousContentLength = 0 } = options;
return (tree) => {
let currentCharCount = 0; // 当前已处理的字符数
visit(tree, 'element', (node) => {
if (
['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'strong'].includes(
node.tagName,
) &&
node.children
) {
const newChildren = [];
node.children.forEach((child) => {
if (child.type === 'text') {
try {
// 使用 Intl.Segmenter 精准拆分中英文及标点
const segmenter = new Intl.Segmenter('zh', {
granularity: 'word',
});
const segments = segmenter.segment(child.value);
Array.from(segments)
.map((seg) => seg.segment)
.filter(Boolean)
.forEach((word) => {
const wordStartPos = currentCharCount;
const wordEndPos = currentCharCount + word.length;
// 判断这个词是否是新增的(在 previousContentLength 之后)
const isNewContent = wordStartPos >= previousContentLength;
newChildren.push({
type: 'element',
tagName: 'span',
properties: {
className: isNewContent ? ['animate-fade-in'] : [],
},
children: [{ type: 'text', value: word }],
});
currentCharCount = wordEndPos;
});
} catch (_) {
// Fallback:如果浏览器不支持 Segmenter
const textStartPos = currentCharCount;
const isNewContent = textStartPos >= previousContentLength;
if (isNewContent) {
// 新内容,添加动画
newChildren.push({
type: 'element',
tagName: 'span',
properties: {
className: ['animate-fade-in'],
},
children: [{ type: 'text', value: child.value }],
});
} else {
// 旧内容,不添加动画
newChildren.push(child);
}
currentCharCount += child.value.length;
}
} else {
newChildren.push(child);
}
});
node.children = newChildren;
}
});
};
}