render.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  1. import i18next from 'i18next';
  2. import { Modal, Tag, Typography } from '@douyinfe/semi-ui';
  3. import { copy, isMobile, showSuccess } from './utils';
  4. import { visit } from 'unist-util-visit';
  5. import {
  6. OpenAI,
  7. Claude,
  8. Gemini,
  9. Moonshot,
  10. Zhipu,
  11. Qwen,
  12. DeepSeek,
  13. Minimax,
  14. Wenxin,
  15. Spark,
  16. Midjourney,
  17. Hunyuan,
  18. Cohere,
  19. Cloudflare,
  20. Ai360,
  21. Yi,
  22. Jina,
  23. Mistral,
  24. XAI,
  25. Ollama,
  26. Doubao,
  27. Suno,
  28. Xinference,
  29. OpenRouter,
  30. Dify,
  31. Coze,
  32. SiliconCloud,
  33. FastGPT,
  34. } from '@lobehub/icons';
  35. import {
  36. LayoutDashboard,
  37. TerminalSquare,
  38. MessageSquare,
  39. Key,
  40. BarChart3,
  41. Image as ImageIcon,
  42. CheckSquare,
  43. CreditCard,
  44. Layers,
  45. Gift,
  46. User,
  47. Settings,
  48. CircleUser,
  49. } from 'lucide-react';
  50. // 侧边栏图标颜色映射
  51. export const sidebarIconColors = {
  52. dashboard: '#4F46E5', // 紫蓝色
  53. terminal: '#10B981', // 绿色
  54. message: '#06B6D4', // 青色
  55. key: '#3B82F6', // 蓝色
  56. chart: '#8B5CF6', // 紫色
  57. image: '#EC4899', // 粉色
  58. check: '#F59E0B', // 琥珀色
  59. credit: '#F97316', // 橙色
  60. layers: '#EF4444', // 红色
  61. gift: '#F43F5E', // 玫红色
  62. user: '#6366F1', // 靛蓝色
  63. settings: '#6B7280', // 灰色
  64. };
  65. // 获取侧边栏Lucide图标组件
  66. export function getLucideIcon(key, selected = false) {
  67. const size = 16;
  68. const strokeWidth = 2;
  69. const commonProps = {
  70. size,
  71. strokeWidth,
  72. className: `transition-colors duration-200 ${selected ? 'transition-transform duration-200 scale-105' : ''}`,
  73. };
  74. // 根据不同的key返回不同的图标
  75. switch (key) {
  76. case 'detail':
  77. return (
  78. <LayoutDashboard
  79. {...commonProps}
  80. color={selected ? sidebarIconColors.dashboard : 'currentColor'}
  81. />
  82. );
  83. case 'playground':
  84. return (
  85. <TerminalSquare
  86. {...commonProps}
  87. color={selected ? sidebarIconColors.terminal : 'currentColor'}
  88. />
  89. );
  90. case 'chat':
  91. return (
  92. <MessageSquare
  93. {...commonProps}
  94. color={selected ? sidebarIconColors.message : 'currentColor'}
  95. />
  96. );
  97. case 'token':
  98. return (
  99. <Key
  100. {...commonProps}
  101. color={selected ? sidebarIconColors.key : 'currentColor'}
  102. />
  103. );
  104. case 'log':
  105. return (
  106. <BarChart3
  107. {...commonProps}
  108. color={selected ? sidebarIconColors.chart : 'currentColor'}
  109. />
  110. );
  111. case 'midjourney':
  112. return (
  113. <ImageIcon
  114. {...commonProps}
  115. color={selected ? sidebarIconColors.image : 'currentColor'}
  116. />
  117. );
  118. case 'task':
  119. return (
  120. <CheckSquare
  121. {...commonProps}
  122. color={selected ? sidebarIconColors.check : 'currentColor'}
  123. />
  124. );
  125. case 'topup':
  126. return (
  127. <CreditCard
  128. {...commonProps}
  129. color={selected ? sidebarIconColors.credit : 'currentColor'}
  130. />
  131. );
  132. case 'channel':
  133. return (
  134. <Layers
  135. {...commonProps}
  136. color={selected ? sidebarIconColors.layers : 'currentColor'}
  137. />
  138. );
  139. case 'redemption':
  140. return (
  141. <Gift
  142. {...commonProps}
  143. color={selected ? sidebarIconColors.gift : 'currentColor'}
  144. />
  145. );
  146. case 'user':
  147. case 'personal':
  148. return (
  149. <User
  150. {...commonProps}
  151. color={selected ? sidebarIconColors.user : 'currentColor'}
  152. />
  153. );
  154. case 'setting':
  155. return (
  156. <Settings
  157. {...commonProps}
  158. color={selected ? sidebarIconColors.settings : 'currentColor'}
  159. />
  160. );
  161. default:
  162. return (
  163. <CircleUser
  164. {...commonProps}
  165. color={selected ? sidebarIconColors.user : 'currentColor'}
  166. />
  167. );
  168. }
  169. }
  170. // 获取模型分类
  171. export const getModelCategories = (() => {
  172. let categoriesCache = null;
  173. let lastLocale = null;
  174. return (t) => {
  175. const currentLocale = i18next.language;
  176. if (categoriesCache && lastLocale === currentLocale) {
  177. return categoriesCache;
  178. }
  179. categoriesCache = {
  180. all: {
  181. label: t('全部模型'),
  182. icon: null,
  183. filter: () => true,
  184. },
  185. openai: {
  186. label: 'OpenAI',
  187. icon: <OpenAI />,
  188. filter: (model) =>
  189. model.model_name.toLowerCase().includes('gpt') ||
  190. model.model_name.toLowerCase().includes('dall-e') ||
  191. model.model_name.toLowerCase().includes('whisper') ||
  192. model.model_name.toLowerCase().includes('tts') ||
  193. model.model_name.toLowerCase().includes('text-') ||
  194. model.model_name.toLowerCase().includes('babbage') ||
  195. model.model_name.toLowerCase().includes('davinci') ||
  196. model.model_name.toLowerCase().includes('curie') ||
  197. model.model_name.toLowerCase().includes('ada') ||
  198. model.model_name.toLowerCase().includes('o1') ||
  199. model.model_name.toLowerCase().includes('o3') ||
  200. model.model_name.toLowerCase().includes('o4'),
  201. },
  202. anthropic: {
  203. label: 'Anthropic',
  204. icon: <Claude.Color />,
  205. filter: (model) => model.model_name.toLowerCase().includes('claude'),
  206. },
  207. gemini: {
  208. label: 'Gemini',
  209. icon: <Gemini.Color />,
  210. filter: (model) => model.model_name.toLowerCase().includes('gemini'),
  211. },
  212. moonshot: {
  213. label: 'Moonshot',
  214. icon: <Moonshot />,
  215. filter: (model) => model.model_name.toLowerCase().includes('moonshot'),
  216. },
  217. zhipu: {
  218. label: t('智谱'),
  219. icon: <Zhipu.Color />,
  220. filter: (model) =>
  221. model.model_name.toLowerCase().includes('chatglm') ||
  222. model.model_name.toLowerCase().includes('glm-'),
  223. },
  224. qwen: {
  225. label: t('通义千问'),
  226. icon: <Qwen.Color />,
  227. filter: (model) => model.model_name.toLowerCase().includes('qwen'),
  228. },
  229. deepseek: {
  230. label: 'DeepSeek',
  231. icon: <DeepSeek.Color />,
  232. filter: (model) => model.model_name.toLowerCase().includes('deepseek'),
  233. },
  234. minimax: {
  235. label: 'MiniMax',
  236. icon: <Minimax.Color />,
  237. filter: (model) => model.model_name.toLowerCase().includes('abab'),
  238. },
  239. baidu: {
  240. label: t('文心一言'),
  241. icon: <Wenxin.Color />,
  242. filter: (model) => model.model_name.toLowerCase().includes('ernie'),
  243. },
  244. xunfei: {
  245. label: t('讯飞星火'),
  246. icon: <Spark.Color />,
  247. filter: (model) => model.model_name.toLowerCase().includes('spark'),
  248. },
  249. midjourney: {
  250. label: 'Midjourney',
  251. icon: <Midjourney />,
  252. filter: (model) => model.model_name.toLowerCase().includes('mj_'),
  253. },
  254. tencent: {
  255. label: t('腾讯混元'),
  256. icon: <Hunyuan.Color />,
  257. filter: (model) => model.model_name.toLowerCase().includes('hunyuan'),
  258. },
  259. cohere: {
  260. label: 'Cohere',
  261. icon: <Cohere.Color />,
  262. filter: (model) => model.model_name.toLowerCase().includes('command'),
  263. },
  264. cloudflare: {
  265. label: 'Cloudflare',
  266. icon: <Cloudflare.Color />,
  267. filter: (model) => model.model_name.toLowerCase().includes('@cf/'),
  268. },
  269. ai360: {
  270. label: t('360智脑'),
  271. icon: <Ai360.Color />,
  272. filter: (model) => model.model_name.toLowerCase().includes('360'),
  273. },
  274. yi: {
  275. label: t('零一万物'),
  276. icon: <Yi.Color />,
  277. filter: (model) => model.model_name.toLowerCase().includes('yi'),
  278. },
  279. jina: {
  280. label: 'Jina',
  281. icon: <Jina />,
  282. filter: (model) => model.model_name.toLowerCase().includes('jina'),
  283. },
  284. mistral: {
  285. label: 'Mistral AI',
  286. icon: <Mistral.Color />,
  287. filter: (model) => model.model_name.toLowerCase().includes('mistral'),
  288. },
  289. xai: {
  290. label: 'xAI',
  291. icon: <XAI />,
  292. filter: (model) => model.model_name.toLowerCase().includes('grok'),
  293. },
  294. llama: {
  295. label: 'Llama',
  296. icon: <Ollama />,
  297. filter: (model) => model.model_name.toLowerCase().includes('llama'),
  298. },
  299. doubao: {
  300. label: t('豆包'),
  301. icon: <Doubao.Color />,
  302. filter: (model) => model.model_name.toLowerCase().includes('doubao'),
  303. },
  304. };
  305. lastLocale = currentLocale;
  306. return categoriesCache;
  307. };
  308. })();
  309. /**
  310. * 根据渠道类型返回对应的厂商图标
  311. * @param {number} channelType - 渠道类型值
  312. * @returns {JSX.Element|null} - 对应的厂商图标组件
  313. */
  314. export function getChannelIcon(channelType) {
  315. const iconSize = 14;
  316. switch (channelType) {
  317. case 1: // OpenAI
  318. case 3: // Azure OpenAI
  319. return <OpenAI size={iconSize} />;
  320. case 2: // Midjourney Proxy
  321. case 5: // Midjourney Proxy Plus
  322. return <Midjourney size={iconSize} />;
  323. case 36: // Suno API
  324. return <Suno size={iconSize} />;
  325. case 4: // Ollama
  326. return <Ollama size={iconSize} />;
  327. case 14: // Anthropic Claude
  328. case 33: // AWS Claude
  329. return <Claude.Color size={iconSize} />;
  330. case 41: // Vertex AI
  331. return <Gemini.Color size={iconSize} />;
  332. case 34: // Cohere
  333. return <Cohere.Color size={iconSize} />;
  334. case 39: // Cloudflare
  335. return <Cloudflare.Color size={iconSize} />;
  336. case 43: // DeepSeek
  337. return <DeepSeek.Color size={iconSize} />;
  338. case 15: // 百度文心千帆
  339. case 46: // 百度文心千帆V2
  340. return <Wenxin.Color size={iconSize} />;
  341. case 17: // 阿里通义千问
  342. return <Qwen.Color size={iconSize} />;
  343. case 18: // 讯飞星火认知
  344. return <Spark.Color size={iconSize} />;
  345. case 16: // 智谱 ChatGLM
  346. case 26: // 智谱 GLM-4V
  347. return <Zhipu.Color size={iconSize} />;
  348. case 24: // Google Gemini
  349. case 11: // Google PaLM2
  350. return <Gemini.Color size={iconSize} />;
  351. case 47: // Xinference
  352. return <Xinference.Color size={iconSize} />;
  353. case 25: // Moonshot
  354. return <Moonshot size={iconSize} />;
  355. case 20: // OpenRouter
  356. return <OpenRouter size={iconSize} />;
  357. case 19: // 360 智脑
  358. return <Ai360.Color size={iconSize} />;
  359. case 23: // 腾讯混元
  360. return <Hunyuan.Color size={iconSize} />;
  361. case 31: // 零一万物
  362. return <Yi.Color size={iconSize} />;
  363. case 35: // MiniMax
  364. return <Minimax.Color size={iconSize} />;
  365. case 37: // Dify
  366. return <Dify.Color size={iconSize} />;
  367. case 38: // Jina
  368. return <Jina size={iconSize} />;
  369. case 40: // SiliconCloud
  370. return <SiliconCloud.Color size={iconSize} />;
  371. case 42: // Mistral AI
  372. return <Mistral.Color size={iconSize} />;
  373. case 45: // 字节火山方舟、豆包通用
  374. return <Doubao.Color size={iconSize} />;
  375. case 48: // xAI
  376. return <XAI size={iconSize} />;
  377. case 49: // Coze
  378. return <Coze size={iconSize} />;
  379. case 8: // 自定义渠道
  380. case 22: // 知识库:FastGPT
  381. return <FastGPT.Color size={iconSize} />;
  382. case 21: // 知识库:AI Proxy
  383. case 44: // 嵌入模型:MokaAI M3E
  384. default:
  385. return null; // 未知类型或自定义渠道不显示图标
  386. }
  387. }
  388. // 颜色列表
  389. const colors = [
  390. 'amber',
  391. 'blue',
  392. 'cyan',
  393. 'green',
  394. 'grey',
  395. 'indigo',
  396. 'light-blue',
  397. 'lime',
  398. 'orange',
  399. 'pink',
  400. 'purple',
  401. 'red',
  402. 'teal',
  403. 'violet',
  404. 'yellow',
  405. ];
  406. // 基础10色色板 (N ≤ 10)
  407. const baseColors = [
  408. '#1664FF', // 主色
  409. '#1AC6FF',
  410. '#FF8A00',
  411. '#3CC780',
  412. '#7442D4',
  413. '#FFC400',
  414. '#304D77',
  415. '#B48DEB',
  416. '#009488',
  417. '#FF7DDA',
  418. ];
  419. // 扩展20色色板 (10 < N ≤ 20)
  420. const extendedColors = [
  421. '#1664FF',
  422. '#B2CFFF',
  423. '#1AC6FF',
  424. '#94EFFF',
  425. '#FF8A00',
  426. '#FFCE7A',
  427. '#3CC780',
  428. '#B9EDCD',
  429. '#7442D4',
  430. '#DDC5FA',
  431. '#FFC400',
  432. '#FAE878',
  433. '#304D77',
  434. '#8B959E',
  435. '#B48DEB',
  436. '#EFE3FF',
  437. '#009488',
  438. '#59BAA8',
  439. '#FF7DDA',
  440. '#FFCFEE',
  441. ];
  442. // 模型颜色映射
  443. export const modelColorMap = {
  444. 'dall-e': 'rgb(147,112,219)', // 深紫色
  445. // 'dall-e-2': 'rgb(147,112,219)', // 介于紫色和蓝色之间的色调
  446. 'dall-e-3': 'rgb(153,50,204)', // 介于紫罗兰和洋红之间的色调
  447. 'gpt-3.5-turbo': 'rgb(184,227,167)', // 浅绿色
  448. // 'gpt-3.5-turbo-0301': 'rgb(131,220,131)', // 亮绿色
  449. 'gpt-3.5-turbo-0613': 'rgb(60,179,113)', // 海洋绿
  450. 'gpt-3.5-turbo-1106': 'rgb(32,178,170)', // 浅海洋绿
  451. 'gpt-3.5-turbo-16k': 'rgb(149,252,206)', // 淡橙色
  452. 'gpt-3.5-turbo-16k-0613': 'rgb(119,255,214)', // 淡桃
  453. 'gpt-3.5-turbo-instruct': 'rgb(175,238,238)', // 粉蓝色
  454. 'gpt-4': 'rgb(135,206,235)', // 天蓝色
  455. // 'gpt-4-0314': 'rgb(70,130,180)', // 钢蓝色
  456. 'gpt-4-0613': 'rgb(100,149,237)', // 矢车菊蓝
  457. 'gpt-4-1106-preview': 'rgb(30,144,255)', // 道奇蓝
  458. 'gpt-4-0125-preview': 'rgb(2,177,236)', // 深天蓝
  459. 'gpt-4-turbo-preview': 'rgb(2,177,255)', // 深天蓝
  460. 'gpt-4-32k': 'rgb(104,111,238)', // 中紫色
  461. // 'gpt-4-32k-0314': 'rgb(90,105,205)', // 暗灰蓝色
  462. 'gpt-4-32k-0613': 'rgb(61,71,139)', // 暗蓝灰色
  463. 'gpt-4-all': 'rgb(65,105,225)', // 皇家蓝
  464. 'gpt-4-gizmo-*': 'rgb(0,0,255)', // 纯蓝色
  465. 'gpt-4-vision-preview': 'rgb(25,25,112)', // 午夜蓝
  466. 'text-ada-001': 'rgb(255,192,203)', // 粉红色
  467. 'text-babbage-001': 'rgb(255,160,122)', // 浅珊瑚色
  468. 'text-curie-001': 'rgb(219,112,147)', // 苍紫罗兰色
  469. // 'text-davinci-002': 'rgb(199,21,133)', // 中紫罗兰红色
  470. 'text-davinci-003': 'rgb(219,112,147)', // 苍紫罗兰色(与Curie相同,表示同一个系列)
  471. 'text-davinci-edit-001': 'rgb(255,105,180)', // 热粉色
  472. 'text-embedding-ada-002': 'rgb(255,182,193)', // 浅粉红
  473. 'text-embedding-v1': 'rgb(255,174,185)', // 浅粉红色(略有区别)
  474. 'text-moderation-latest': 'rgb(255,130,171)', // 强粉色
  475. 'text-moderation-stable': 'rgb(255,160,122)', // 浅珊瑚色(与Babbage相同,表示同一类功能)
  476. 'tts-1': 'rgb(255,140,0)', // 深橙色
  477. 'tts-1-1106': 'rgb(255,165,0)', // 橙色
  478. 'tts-1-hd': 'rgb(255,215,0)', // 金色
  479. 'tts-1-hd-1106': 'rgb(255,223,0)', // 金黄色(略有区别)
  480. 'whisper-1': 'rgb(245,245,220)', // 米色
  481. 'claude-3-opus-20240229': 'rgb(255,132,31)', // 橙红色
  482. 'claude-3-sonnet-20240229': 'rgb(253,135,93)', // 橙色
  483. 'claude-3-haiku-20240307': 'rgb(255,175,146)', // 浅橙色
  484. 'claude-2.1': 'rgb(255,209,190)', // 浅橙色(略有区别)
  485. };
  486. export function modelToColor(modelName) {
  487. // 1. 如果模型在预定义的 modelColorMap 中,使用预定义颜色
  488. if (modelColorMap[modelName]) {
  489. return modelColorMap[modelName];
  490. }
  491. // 2. 生成一个稳定的数字作为索引
  492. let hash = 0;
  493. for (let i = 0; i < modelName.length; i++) {
  494. hash = (hash << 5) - hash + modelName.charCodeAt(i);
  495. hash = hash & hash; // Convert to 32-bit integer
  496. }
  497. hash = Math.abs(hash);
  498. // 3. 根据模型名称长度选择不同的色板
  499. const colorPalette = modelName.length > 10 ? extendedColors : baseColors;
  500. // 4. 使用hash值选择颜色
  501. const index = hash % colorPalette.length;
  502. return colorPalette[index];
  503. }
  504. export function stringToColor(str) {
  505. let sum = 0;
  506. for (let i = 0; i < str.length; i++) {
  507. sum += str.charCodeAt(i);
  508. }
  509. let i = sum % colors.length;
  510. return colors[i];
  511. }
  512. // 渲染带有模型图标的标签
  513. export function renderModelTag(modelName, options = {}) {
  514. const {
  515. color,
  516. size = 'large',
  517. shape = 'circle',
  518. onClick,
  519. suffixIcon,
  520. } = options;
  521. const categories = getModelCategories(i18next.t);
  522. let icon = null;
  523. for (const [key, category] of Object.entries(categories)) {
  524. if (key !== 'all' && category.filter({ model_name: modelName })) {
  525. icon = category.icon;
  526. break;
  527. }
  528. }
  529. return (
  530. <Tag
  531. color={color || stringToColor(modelName)}
  532. prefixIcon={icon}
  533. suffixIcon={suffixIcon}
  534. size={size}
  535. shape={shape}
  536. onClick={onClick}
  537. >
  538. {modelName}
  539. </Tag>
  540. );
  541. }
  542. export function renderText(text, limit) {
  543. if (text.length > limit) {
  544. return text.slice(0, limit - 3) + '...';
  545. }
  546. return text;
  547. }
  548. /**
  549. * Render group tags based on the input group string
  550. * @param {string} group - The input group string
  551. * @returns {JSX.Element} - The rendered group tags
  552. */
  553. export function renderGroup(group) {
  554. if (group === '') {
  555. return (
  556. <Tag size='large' key='default' color='orange' shape='circle'>
  557. {i18next.t('用户分组')}
  558. </Tag>
  559. );
  560. }
  561. const tagColors = {
  562. vip: 'yellow',
  563. pro: 'yellow',
  564. svip: 'red',
  565. premium: 'red',
  566. };
  567. const groups = group.split(',').sort();
  568. return (
  569. <span key={group}>
  570. {groups.map((group) => (
  571. <Tag
  572. size='large'
  573. color={tagColors[group] || stringToColor(group)}
  574. key={group}
  575. shape='circle'
  576. onClick={async (event) => {
  577. event.stopPropagation();
  578. if (await copy(group)) {
  579. showSuccess(i18next.t('已复制:') + group);
  580. } else {
  581. Modal.error({
  582. title: i18next.t('无法复制到剪贴板,请手动复制'),
  583. content: group,
  584. });
  585. }
  586. }}
  587. >
  588. {group}
  589. </Tag>
  590. ))}
  591. </span>
  592. );
  593. }
  594. export function renderRatio(ratio) {
  595. let color = 'green';
  596. if (ratio > 5) {
  597. color = 'red';
  598. } else if (ratio > 3) {
  599. color = 'orange';
  600. } else if (ratio > 1) {
  601. color = 'blue';
  602. }
  603. return (
  604. <Tag color={color}>
  605. {ratio}x {i18next.t('倍率')}
  606. </Tag>
  607. );
  608. }
  609. const measureTextWidth = (
  610. text,
  611. style = {
  612. fontSize: '14px',
  613. fontFamily:
  614. '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
  615. },
  616. containerWidth,
  617. ) => {
  618. const span = document.createElement('span');
  619. span.style.visibility = 'hidden';
  620. span.style.position = 'absolute';
  621. span.style.whiteSpace = 'nowrap';
  622. span.style.fontSize = style.fontSize;
  623. span.style.fontFamily = style.fontFamily;
  624. span.textContent = text;
  625. document.body.appendChild(span);
  626. const width = span.offsetWidth;
  627. document.body.removeChild(span);
  628. return width;
  629. };
  630. export function truncateText(text, maxWidth = 200) {
  631. if (!isMobile()) {
  632. return text;
  633. }
  634. if (!text) return text;
  635. try {
  636. // Handle percentage-based maxWidth
  637. let actualMaxWidth = maxWidth;
  638. if (typeof maxWidth === 'string' && maxWidth.endsWith('%')) {
  639. const percentage = parseFloat(maxWidth) / 100;
  640. // Use window width as fallback container width
  641. actualMaxWidth = window.innerWidth * percentage;
  642. }
  643. const width = measureTextWidth(text);
  644. if (width <= actualMaxWidth) return text;
  645. let left = 0;
  646. let right = text.length;
  647. let result = text;
  648. while (left <= right) {
  649. const mid = Math.floor((left + right) / 2);
  650. const truncated = text.slice(0, mid) + '...';
  651. const currentWidth = measureTextWidth(truncated);
  652. if (currentWidth <= actualMaxWidth) {
  653. result = truncated;
  654. left = mid + 1;
  655. } else {
  656. right = mid - 1;
  657. }
  658. }
  659. return result;
  660. } catch (error) {
  661. console.warn(
  662. 'Text measurement failed, falling back to character count',
  663. error,
  664. );
  665. if (text.length > 20) {
  666. return text.slice(0, 17) + '...';
  667. }
  668. return text;
  669. }
  670. }
  671. export const renderGroupOption = (item) => {
  672. const {
  673. disabled,
  674. selected,
  675. label,
  676. value,
  677. focused,
  678. className,
  679. style,
  680. onMouseEnter,
  681. onClick,
  682. empty,
  683. emptyContent,
  684. ...rest
  685. } = item;
  686. const baseStyle = {
  687. display: 'flex',
  688. justifyContent: 'space-between',
  689. alignItems: 'center',
  690. padding: '8px 16px',
  691. cursor: disabled ? 'not-allowed' : 'pointer',
  692. backgroundColor: focused ? 'var(--semi-color-fill-0)' : 'transparent',
  693. opacity: disabled ? 0.5 : 1,
  694. ...(selected && {
  695. backgroundColor: 'var(--semi-color-primary-light-default)',
  696. }),
  697. '&:hover': {
  698. backgroundColor: !disabled && 'var(--semi-color-fill-1)',
  699. },
  700. };
  701. const handleClick = () => {
  702. if (!disabled && onClick) {
  703. onClick();
  704. }
  705. };
  706. const handleMouseEnter = (e) => {
  707. if (!disabled && onMouseEnter) {
  708. onMouseEnter(e);
  709. }
  710. };
  711. return (
  712. <div
  713. style={baseStyle}
  714. onClick={handleClick}
  715. onMouseEnter={handleMouseEnter}
  716. >
  717. <div style={{ display: 'flex', flexDirection: 'column', gap: '4px' }}>
  718. <Typography.Text strong type={disabled ? 'tertiary' : undefined}>
  719. {value}
  720. </Typography.Text>
  721. <Typography.Text type='secondary' size='small'>
  722. {label}
  723. </Typography.Text>
  724. </div>
  725. {item.ratio && renderRatio(item.ratio)}
  726. </div>
  727. );
  728. };
  729. export function renderNumber(num) {
  730. if (num >= 1000000000) {
  731. return (num / 1000000000).toFixed(1) + 'B';
  732. } else if (num >= 1000000) {
  733. return (num / 1000000).toFixed(1) + 'M';
  734. } else if (num >= 10000) {
  735. return (num / 1000).toFixed(1) + 'k';
  736. } else {
  737. return num;
  738. }
  739. }
  740. export function renderQuotaNumberWithDigit(num, digits = 2) {
  741. if (typeof num !== 'number' || isNaN(num)) {
  742. return 0;
  743. }
  744. let displayInCurrency = localStorage.getItem('display_in_currency');
  745. num = num.toFixed(digits);
  746. if (displayInCurrency) {
  747. return '$' + num;
  748. }
  749. return num;
  750. }
  751. export function renderNumberWithPoint(num) {
  752. if (num === undefined) return '';
  753. num = num.toFixed(2);
  754. if (num >= 100000) {
  755. // Convert number to string to manipulate it
  756. let numStr = num.toString();
  757. // Find the position of the decimal point
  758. let decimalPointIndex = numStr.indexOf('.');
  759. let wholePart = numStr;
  760. let decimalPart = '';
  761. // If there is a decimal point, split the number into whole and decimal parts
  762. if (decimalPointIndex !== -1) {
  763. wholePart = numStr.slice(0, decimalPointIndex);
  764. decimalPart = numStr.slice(decimalPointIndex);
  765. }
  766. // Take the first two and last two digits of the whole number part
  767. let shortenedWholePart = wholePart.slice(0, 2) + '..' + wholePart.slice(-2);
  768. // Return the formatted number
  769. return shortenedWholePart + decimalPart;
  770. }
  771. // If the number is less than 100,000, return it unmodified
  772. return num;
  773. }
  774. export function getQuotaPerUnit() {
  775. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  776. quotaPerUnit = parseFloat(quotaPerUnit);
  777. return quotaPerUnit;
  778. }
  779. export function renderUnitWithQuota(quota) {
  780. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  781. quotaPerUnit = parseFloat(quotaPerUnit);
  782. quota = parseFloat(quota);
  783. return quotaPerUnit * quota;
  784. }
  785. export function getQuotaWithUnit(quota, digits = 6) {
  786. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  787. quotaPerUnit = parseFloat(quotaPerUnit);
  788. return (quota / quotaPerUnit).toFixed(digits);
  789. }
  790. export function renderQuotaWithAmount(amount) {
  791. let displayInCurrency = localStorage.getItem('display_in_currency');
  792. displayInCurrency = displayInCurrency === 'true';
  793. if (displayInCurrency) {
  794. return '$' + amount;
  795. } else {
  796. return renderNumber(renderUnitWithQuota(amount));
  797. }
  798. }
  799. export function renderQuota(quota, digits = 2) {
  800. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  801. let displayInCurrency = localStorage.getItem('display_in_currency');
  802. quotaPerUnit = parseFloat(quotaPerUnit);
  803. displayInCurrency = displayInCurrency === 'true';
  804. if (displayInCurrency) {
  805. return '$' + (quota / quotaPerUnit).toFixed(digits);
  806. }
  807. return renderNumber(quota);
  808. }
  809. function isValidGroupRatio(ratio) {
  810. return ratio !== undefined && ratio !== null && ratio !== -1;
  811. }
  812. export function renderModelPrice(
  813. inputTokens,
  814. completionTokens,
  815. modelRatio,
  816. modelPrice = -1,
  817. completionRatio,
  818. groupRatio,
  819. user_group_ratio,
  820. cacheTokens = 0,
  821. cacheRatio = 1.0,
  822. image = false,
  823. imageRatio = 1.0,
  824. imageOutputTokens = 0,
  825. webSearch = false,
  826. webSearchCallCount = 0,
  827. webSearchPrice = 0,
  828. fileSearch = false,
  829. fileSearchCallCount = 0,
  830. fileSearchPrice = 0,
  831. audioInputSeperatePrice = false,
  832. audioInputTokens = 0,
  833. audioInputPrice = 0,
  834. ) {
  835. const useUserGroupRatio = isValidGroupRatio(user_group_ratio);
  836. const ratioLabel = useUserGroupRatio
  837. ? i18next.t('专属倍率')
  838. : i18next.t('分组倍率');
  839. groupRatio = useUserGroupRatio ? user_group_ratio : groupRatio;
  840. if (modelPrice !== -1) {
  841. return i18next.t(
  842. '模型价格:${{price}} * {{ratioType}}:{{ratio}} = ${{total}}',
  843. {
  844. price: modelPrice,
  845. ratio: groupRatio,
  846. total: modelPrice * groupRatio,
  847. ratioType: ratioLabel,
  848. },
  849. );
  850. } else {
  851. if (completionRatio === undefined) {
  852. completionRatio = 0;
  853. }
  854. let inputRatioPrice = modelRatio * 2.0;
  855. let completionRatioPrice = modelRatio * 2.0 * completionRatio;
  856. let cacheRatioPrice = modelRatio * 2.0 * cacheRatio;
  857. let imageRatioPrice = modelRatio * 2.0 * imageRatio;
  858. // Calculate effective input tokens (non-cached + cached with ratio applied)
  859. let effectiveInputTokens =
  860. inputTokens - cacheTokens + cacheTokens * cacheRatio;
  861. // Handle image tokens if present
  862. if (image && imageOutputTokens > 0) {
  863. effectiveInputTokens =
  864. inputTokens - imageOutputTokens + imageOutputTokens * imageRatio;
  865. }
  866. if (audioInputTokens > 0) {
  867. effectiveInputTokens -= audioInputTokens;
  868. }
  869. let price =
  870. (effectiveInputTokens / 1000000) * inputRatioPrice * groupRatio +
  871. (audioInputTokens / 1000000) * audioInputPrice * groupRatio +
  872. (completionTokens / 1000000) * completionRatioPrice * groupRatio +
  873. (webSearchCallCount / 1000) * webSearchPrice * groupRatio +
  874. (fileSearchCallCount / 1000) * fileSearchPrice * groupRatio;
  875. return (
  876. <>
  877. <article>
  878. <p>
  879. {i18next.t('输入价格:${{price}} / 1M tokens{{audioPrice}}', {
  880. price: inputRatioPrice,
  881. audioPrice: audioInputSeperatePrice
  882. ? `,音频 $${audioInputPrice} / 1M tokens`
  883. : '',
  884. })}
  885. </p>
  886. <p>
  887. {i18next.t(
  888. '输出价格:${{price}} * {{completionRatio}} = ${{total}} / 1M tokens (补全倍率: {{completionRatio}})',
  889. {
  890. price: inputRatioPrice,
  891. total: completionRatioPrice,
  892. completionRatio: completionRatio,
  893. },
  894. )}
  895. </p>
  896. {cacheTokens > 0 && (
  897. <p>
  898. {i18next.t(
  899. '缓存价格:${{price}} * {{cacheRatio}} = ${{total}} / 1M tokens (缓存倍率: {{cacheRatio}})',
  900. {
  901. price: inputRatioPrice,
  902. total: inputRatioPrice * cacheRatio,
  903. cacheRatio: cacheRatio,
  904. },
  905. )}
  906. </p>
  907. )}
  908. {image && imageOutputTokens > 0 && (
  909. <p>
  910. {i18next.t(
  911. '图片输入价格:${{price}} * {{ratio}} = ${{total}} / 1M tokens (图片倍率: {{imageRatio}})',
  912. {
  913. price: imageRatioPrice,
  914. ratio: groupRatio,
  915. total: imageRatioPrice * groupRatio,
  916. imageRatio: imageRatio,
  917. },
  918. )}
  919. </p>
  920. )}
  921. {webSearch && webSearchCallCount > 0 && (
  922. <p>
  923. {i18next.t('Web搜索价格:${{price}} / 1K 次', {
  924. price: webSearchPrice,
  925. })}
  926. </p>
  927. )}
  928. {fileSearch && fileSearchCallCount > 0 && (
  929. <p>
  930. {i18next.t('文件搜索价格:${{price}} / 1K 次', {
  931. price: fileSearchPrice,
  932. })}
  933. </p>
  934. )}
  935. <p></p>
  936. <p>
  937. {(() => {
  938. // 构建输入部分描述
  939. let inputDesc = '';
  940. if (image && imageOutputTokens > 0) {
  941. inputDesc = i18next.t(
  942. '(输入 {{nonImageInput}} tokens + 图片输入 {{imageInput}} tokens * {{imageRatio}} / 1M tokens * ${{price}}',
  943. {
  944. nonImageInput: inputTokens - imageOutputTokens,
  945. imageInput: imageOutputTokens,
  946. imageRatio: imageRatio,
  947. price: inputRatioPrice,
  948. },
  949. );
  950. } else if (cacheTokens > 0) {
  951. inputDesc = i18next.t(
  952. '(输入 {{nonCacheInput}} tokens / 1M tokens * ${{price}} + 缓存 {{cacheInput}} tokens / 1M tokens * ${{cachePrice}}',
  953. {
  954. nonCacheInput: inputTokens - cacheTokens,
  955. cacheInput: cacheTokens,
  956. price: inputRatioPrice,
  957. cachePrice: cacheRatioPrice,
  958. },
  959. );
  960. } else if (audioInputSeperatePrice && audioInputTokens > 0) {
  961. inputDesc = i18next.t(
  962. '(输入 {{nonAudioInput}} tokens / 1M tokens * ${{price}} + 音频输入 {{audioInput}} tokens / 1M tokens * ${{audioPrice}}',
  963. {
  964. nonAudioInput: inputTokens - audioInputTokens,
  965. audioInput: audioInputTokens,
  966. price: inputRatioPrice,
  967. audioPrice: audioInputPrice,
  968. },
  969. );
  970. } else {
  971. inputDesc = i18next.t(
  972. '(输入 {{input}} tokens / 1M tokens * ${{price}}',
  973. {
  974. input: inputTokens,
  975. price: inputRatioPrice,
  976. },
  977. );
  978. }
  979. // 构建输出部分描述
  980. const outputDesc = i18next.t(
  981. '输出 {{completion}} tokens / 1M tokens * ${{compPrice}}) * {{ratioType}} {{ratio}}',
  982. {
  983. completion: completionTokens,
  984. compPrice: completionRatioPrice,
  985. ratio: groupRatio,
  986. ratioType: ratioLabel,
  987. },
  988. );
  989. // 构建额外服务描述
  990. const extraServices = [
  991. webSearch && webSearchCallCount > 0
  992. ? i18next.t(
  993. ' + Web搜索 {{count}}次 / 1K 次 * ${{price}} * {{ratioType}} {{ratio}}',
  994. {
  995. count: webSearchCallCount,
  996. price: webSearchPrice,
  997. ratio: groupRatio,
  998. ratioType: ratioLabel,
  999. },
  1000. )
  1001. : '',
  1002. fileSearch && fileSearchCallCount > 0
  1003. ? i18next.t(
  1004. ' + 文件搜索 {{count}}次 / 1K 次 * ${{price}} * {{ratioType}} {{ratio}}',
  1005. {
  1006. count: fileSearchCallCount,
  1007. price: fileSearchPrice,
  1008. ratio: groupRatio,
  1009. ratioType: ratioLabel,
  1010. },
  1011. )
  1012. : '',
  1013. ].join('');
  1014. return i18next.t(
  1015. '{{inputDesc}} + {{outputDesc}}{{extraServices}} = ${{total}}',
  1016. {
  1017. inputDesc,
  1018. outputDesc,
  1019. extraServices,
  1020. total: price.toFixed(6),
  1021. },
  1022. );
  1023. })()}
  1024. </p>
  1025. <p>{i18next.t('仅供参考,以实际扣费为准')}</p>
  1026. </article>
  1027. </>
  1028. );
  1029. }
  1030. }
  1031. export function renderLogContent(
  1032. modelRatio,
  1033. completionRatio,
  1034. modelPrice = -1,
  1035. groupRatio,
  1036. user_group_ratio,
  1037. image = false,
  1038. imageRatio = 1.0,
  1039. useUserGroupRatio = undefined,
  1040. webSearch = false,
  1041. webSearchCallCount = 0,
  1042. fileSearch = false,
  1043. fileSearchCallCount = 0,
  1044. ) {
  1045. useUserGroupRatio = isValidGroupRatio(user_group_ratio);
  1046. const ratioLabel = useUserGroupRatio
  1047. ? i18next.t('专属倍率')
  1048. : i18next.t('分组倍率');
  1049. const ratio = useUserGroupRatio ? user_group_ratio : groupRatio;
  1050. if (modelPrice !== -1) {
  1051. return i18next.t('模型价格 ${{price}},{{ratioType}} {{ratio}}', {
  1052. price: modelPrice,
  1053. ratioType: ratioLabel,
  1054. ratio,
  1055. });
  1056. } else {
  1057. if (image) {
  1058. return i18next.t(
  1059. '模型倍率 {{modelRatio}},输出倍率 {{completionRatio}},图片输入倍率 {{imageRatio}},{{ratioType}} {{ratio}}',
  1060. {
  1061. modelRatio: modelRatio,
  1062. completionRatio: completionRatio,
  1063. imageRatio: imageRatio,
  1064. ratioType: ratioLabel,
  1065. ratio,
  1066. },
  1067. );
  1068. } else if (webSearch) {
  1069. return i18next.t(
  1070. '模型倍率 {{modelRatio}},输出倍率 {{completionRatio}},{{ratioType}} {{ratio}},Web 搜索调用 {{webSearchCallCount}} 次',
  1071. {
  1072. modelRatio: modelRatio,
  1073. completionRatio: completionRatio,
  1074. ratioType: ratioLabel,
  1075. ratio,
  1076. webSearchCallCount,
  1077. },
  1078. );
  1079. } else {
  1080. return i18next.t(
  1081. '模型倍率 {{modelRatio}},输出倍率 {{completionRatio}},{{ratioType}} {{ratio}}',
  1082. {
  1083. modelRatio: modelRatio,
  1084. completionRatio: completionRatio,
  1085. ratioType: ratioLabel,
  1086. ratio,
  1087. },
  1088. );
  1089. }
  1090. }
  1091. }
  1092. export function renderModelPriceSimple(
  1093. modelRatio,
  1094. modelPrice = -1,
  1095. groupRatio,
  1096. user_group_ratio,
  1097. cacheTokens = 0,
  1098. cacheRatio = 1.0,
  1099. image = false,
  1100. imageRatio = 1.0,
  1101. ) {
  1102. const useUserGroupRatio = isValidGroupRatio(user_group_ratio);
  1103. const ratioLabel = useUserGroupRatio
  1104. ? i18next.t('专属倍率')
  1105. : i18next.t('分组倍率');
  1106. groupRatio = useUserGroupRatio ? user_group_ratio : groupRatio;
  1107. if (modelPrice !== -1) {
  1108. return i18next.t('价格:${{price}} * {{ratioType}}:{{ratio}}', {
  1109. price: modelPrice,
  1110. ratioType: ratioLabel,
  1111. ratio: groupRatio,
  1112. });
  1113. } else {
  1114. if (image && cacheTokens !== 0) {
  1115. return i18next.t(
  1116. '模型: {{ratio}} * {{ratioType}}: {{groupRatio}} * 缓存倍率: {{cacheRatio}} * 图片输入倍率: {{imageRatio}}',
  1117. {
  1118. ratio: modelRatio,
  1119. ratioType: ratioLabel,
  1120. groupRatio: groupRatio,
  1121. cacheRatio: cacheRatio,
  1122. imageRatio: imageRatio,
  1123. },
  1124. );
  1125. } else if (image) {
  1126. return i18next.t(
  1127. '模型: {{ratio}} * {{ratioType}}: {{groupRatio}} * 图片输入倍率: {{imageRatio}}',
  1128. {
  1129. ratio: modelRatio,
  1130. ratioType: ratioLabel,
  1131. groupRatio: groupRatio,
  1132. imageRatio: imageRatio,
  1133. },
  1134. );
  1135. } else if (cacheTokens !== 0) {
  1136. return i18next.t(
  1137. '模型: {{ratio}} * 分组: {{groupRatio}} * 缓存: {{cacheRatio}}',
  1138. {
  1139. ratio: modelRatio,
  1140. groupRatio: groupRatio,
  1141. cacheRatio: cacheRatio,
  1142. },
  1143. );
  1144. } else {
  1145. return i18next.t('模型: {{ratio}} * {{ratioType}}:{{groupRatio}}', {
  1146. ratio: modelRatio,
  1147. ratioType: ratioLabel,
  1148. groupRatio: groupRatio,
  1149. });
  1150. }
  1151. }
  1152. }
  1153. export function renderAudioModelPrice(
  1154. inputTokens,
  1155. completionTokens,
  1156. modelRatio,
  1157. modelPrice = -1,
  1158. completionRatio,
  1159. audioInputTokens,
  1160. audioCompletionTokens,
  1161. audioRatio,
  1162. audioCompletionRatio,
  1163. groupRatio,
  1164. user_group_ratio,
  1165. cacheTokens = 0,
  1166. cacheRatio = 1.0,
  1167. ) {
  1168. const useUserGroupRatio = isValidGroupRatio(user_group_ratio);
  1169. const ratioLabel = useUserGroupRatio
  1170. ? i18next.t('专属倍率')
  1171. : i18next.t('分组倍率');
  1172. groupRatio = useUserGroupRatio ? user_group_ratio : groupRatio;
  1173. // 1 ratio = $0.002 / 1K tokens
  1174. if (modelPrice !== -1) {
  1175. return i18next.t(
  1176. '模型价格:${{price}} * {{ratioType}}:{{ratio}} = ${{total}}',
  1177. {
  1178. price: modelPrice,
  1179. ratio: groupRatio,
  1180. total: modelPrice * groupRatio,
  1181. ratioType: ratioLabel,
  1182. },
  1183. );
  1184. } else {
  1185. if (completionRatio === undefined) {
  1186. completionRatio = 0;
  1187. }
  1188. // try toFixed audioRatio
  1189. audioRatio = parseFloat(audioRatio).toFixed(6);
  1190. // 这里的 *2 是因为 1倍率=0.002刀,请勿删除
  1191. let inputRatioPrice = modelRatio * 2.0;
  1192. let completionRatioPrice = modelRatio * 2.0 * completionRatio;
  1193. let cacheRatioPrice = modelRatio * 2.0 * cacheRatio;
  1194. // Calculate effective input tokens (non-cached + cached with ratio applied)
  1195. const effectiveInputTokens =
  1196. inputTokens - cacheTokens + cacheTokens * cacheRatio;
  1197. let textPrice =
  1198. (effectiveInputTokens / 1000000) * inputRatioPrice * groupRatio +
  1199. (completionTokens / 1000000) * completionRatioPrice * groupRatio;
  1200. let audioPrice =
  1201. (audioInputTokens / 1000000) * inputRatioPrice * audioRatio * groupRatio +
  1202. (audioCompletionTokens / 1000000) *
  1203. inputRatioPrice *
  1204. audioRatio *
  1205. audioCompletionRatio *
  1206. groupRatio;
  1207. let price = textPrice + audioPrice;
  1208. return (
  1209. <>
  1210. <article>
  1211. <p>
  1212. {i18next.t('提示价格:${{price}} / 1M tokens', {
  1213. price: inputRatioPrice,
  1214. })}
  1215. </p>
  1216. <p>
  1217. {i18next.t(
  1218. '补全价格:${{price}} * {{completionRatio}} = ${{total}} / 1M tokens (补全倍率: {{completionRatio}})',
  1219. {
  1220. price: inputRatioPrice,
  1221. total: completionRatioPrice,
  1222. completionRatio: completionRatio,
  1223. },
  1224. )}
  1225. </p>
  1226. {cacheTokens > 0 && (
  1227. <p>
  1228. {i18next.t(
  1229. '缓存价格:${{price}} * {{cacheRatio}} = ${{total}} / 1M tokens (缓存倍率: {{cacheRatio}})',
  1230. {
  1231. price: inputRatioPrice,
  1232. total: inputRatioPrice * cacheRatio,
  1233. cacheRatio: cacheRatio,
  1234. },
  1235. )}
  1236. </p>
  1237. )}
  1238. <p>
  1239. {i18next.t(
  1240. '音频提示价格:${{price}} * {{audioRatio}} = ${{total}} / 1M tokens (音频倍率: {{audioRatio}})',
  1241. {
  1242. price: inputRatioPrice,
  1243. total: inputRatioPrice * audioRatio,
  1244. audioRatio: audioRatio,
  1245. },
  1246. )}
  1247. </p>
  1248. <p>
  1249. {i18next.t(
  1250. '音频补全价格:${{price}} * {{audioRatio}} * {{audioCompRatio}} = ${{total}} / 1M tokens (音频补全倍率: {{audioCompRatio}})',
  1251. {
  1252. price: inputRatioPrice,
  1253. total: inputRatioPrice * audioRatio * audioCompletionRatio,
  1254. audioRatio: audioRatio,
  1255. audioCompRatio: audioCompletionRatio,
  1256. },
  1257. )}
  1258. </p>
  1259. <p>
  1260. {cacheTokens > 0
  1261. ? i18next.t(
  1262. '文字提示 {{nonCacheInput}} tokens / 1M tokens * ${{price}} + 缓存 {{cacheInput}} tokens / 1M tokens * ${{cachePrice}} + 文字补全 {{completion}} tokens / 1M tokens * ${{compPrice}} = ${{total}}',
  1263. {
  1264. nonCacheInput: inputTokens - cacheTokens,
  1265. cacheInput: cacheTokens,
  1266. cachePrice: inputRatioPrice * cacheRatio,
  1267. price: inputRatioPrice,
  1268. completion: completionTokens,
  1269. compPrice: completionRatioPrice,
  1270. total: textPrice.toFixed(6),
  1271. },
  1272. )
  1273. : i18next.t(
  1274. '文字提示 {{input}} tokens / 1M tokens * ${{price}} + 文字补全 {{completion}} tokens / 1M tokens * ${{compPrice}} = ${{total}}',
  1275. {
  1276. input: inputTokens,
  1277. price: inputRatioPrice,
  1278. completion: completionTokens,
  1279. compPrice: completionRatioPrice,
  1280. total: textPrice.toFixed(6),
  1281. },
  1282. )}
  1283. </p>
  1284. <p>
  1285. {i18next.t(
  1286. '音频提示 {{input}} tokens / 1M tokens * ${{audioInputPrice}} + 音频补全 {{completion}} tokens / 1M tokens * ${{audioCompPrice}} = ${{total}}',
  1287. {
  1288. input: audioInputTokens,
  1289. completion: audioCompletionTokens,
  1290. audioInputPrice: audioRatio * inputRatioPrice,
  1291. audioCompPrice:
  1292. audioRatio * audioCompletionRatio * inputRatioPrice,
  1293. total: audioPrice.toFixed(6),
  1294. },
  1295. )}
  1296. </p>
  1297. <p>
  1298. {i18next.t(
  1299. '总价:文字价格 {{textPrice}} + 音频价格 {{audioPrice}} = ${{total}}',
  1300. {
  1301. total: price.toFixed(6),
  1302. textPrice: textPrice.toFixed(6),
  1303. audioPrice: audioPrice.toFixed(6),
  1304. },
  1305. )}
  1306. </p>
  1307. <p>{i18next.t('仅供参考,以实际扣费为准')}</p>
  1308. </article>
  1309. </>
  1310. );
  1311. }
  1312. }
  1313. export function renderQuotaWithPrompt(quota, digits) {
  1314. let displayInCurrency = localStorage.getItem('display_in_currency');
  1315. displayInCurrency = displayInCurrency === 'true';
  1316. if (displayInCurrency) {
  1317. return (
  1318. ' | ' + i18next.t('等价金额') + ': ' + renderQuota(quota, digits) + ''
  1319. );
  1320. }
  1321. return '';
  1322. }
  1323. export function renderClaudeModelPrice(
  1324. inputTokens,
  1325. completionTokens,
  1326. modelRatio,
  1327. modelPrice = -1,
  1328. completionRatio,
  1329. groupRatio,
  1330. user_group_ratio,
  1331. cacheTokens = 0,
  1332. cacheRatio = 1.0,
  1333. cacheCreationTokens = 0,
  1334. cacheCreationRatio = 1.0,
  1335. ) {
  1336. const useUserGroupRatio = isValidGroupRatio(user_group_ratio);
  1337. const ratioLabel = useUserGroupRatio
  1338. ? i18next.t('专属倍率')
  1339. : i18next.t('分组倍率');
  1340. groupRatio = useUserGroupRatio ? user_group_ratio : groupRatio;
  1341. if (modelPrice !== -1) {
  1342. return i18next.t(
  1343. '模型价格:${{price}} * {{ratioType}}:{{ratio}} = ${{total}}',
  1344. {
  1345. price: modelPrice,
  1346. ratioType: ratioLabel,
  1347. ratio: groupRatio,
  1348. total: modelPrice * groupRatio,
  1349. },
  1350. );
  1351. } else {
  1352. if (completionRatio === undefined) {
  1353. completionRatio = 0;
  1354. }
  1355. const completionRatioValue = completionRatio || 0;
  1356. const inputRatioPrice = modelRatio * 2.0;
  1357. const completionRatioPrice = modelRatio * 2.0 * completionRatioValue;
  1358. let cacheRatioPrice = (modelRatio * 2.0 * cacheRatio).toFixed(2);
  1359. let cacheCreationRatioPrice = modelRatio * 2.0 * cacheCreationRatio;
  1360. // Calculate effective input tokens (non-cached + cached with ratio applied + cache creation with ratio applied)
  1361. const nonCachedTokens = inputTokens;
  1362. const effectiveInputTokens =
  1363. nonCachedTokens +
  1364. cacheTokens * cacheRatio +
  1365. cacheCreationTokens * cacheCreationRatio;
  1366. let price =
  1367. (effectiveInputTokens / 1000000) * inputRatioPrice * groupRatio +
  1368. (completionTokens / 1000000) * completionRatioPrice * groupRatio;
  1369. return (
  1370. <>
  1371. <article>
  1372. <p>
  1373. {i18next.t('提示价格:${{price}} / 1M tokens', {
  1374. price: inputRatioPrice,
  1375. })}
  1376. </p>
  1377. <p>
  1378. {i18next.t(
  1379. '补全价格:${{price}} * {{ratio}} = ${{total}} / 1M tokens',
  1380. {
  1381. price: inputRatioPrice,
  1382. ratio: completionRatio,
  1383. total: completionRatioPrice,
  1384. },
  1385. )}
  1386. </p>
  1387. {cacheTokens > 0 && (
  1388. <p>
  1389. {i18next.t(
  1390. '缓存价格:${{price}} * {{ratio}} = ${{total}} / 1M tokens (缓存倍率: {{cacheRatio}})',
  1391. {
  1392. price: inputRatioPrice,
  1393. ratio: cacheRatio,
  1394. total: cacheRatioPrice,
  1395. cacheRatio: cacheRatio,
  1396. },
  1397. )}
  1398. </p>
  1399. )}
  1400. {cacheCreationTokens > 0 && (
  1401. <p>
  1402. {i18next.t(
  1403. '缓存创建价格:${{price}} * {{ratio}} = ${{total}} / 1M tokens (缓存创建倍率: {{cacheCreationRatio}})',
  1404. {
  1405. price: inputRatioPrice,
  1406. ratio: cacheCreationRatio,
  1407. total: cacheCreationRatioPrice,
  1408. cacheCreationRatio: cacheCreationRatio,
  1409. },
  1410. )}
  1411. </p>
  1412. )}
  1413. <p></p>
  1414. <p>
  1415. {cacheTokens > 0 || cacheCreationTokens > 0
  1416. ? i18next.t(
  1417. '提示 {{nonCacheInput}} tokens / 1M tokens * ${{price}} + 缓存 {{cacheInput}} tokens / 1M tokens * ${{cachePrice}} + 缓存创建 {{cacheCreationInput}} tokens / 1M tokens * ${{cacheCreationPrice}} + 补全 {{completion}} tokens / 1M tokens * ${{compPrice}} * {{ratioType}} {{ratio}} = ${{total}}',
  1418. {
  1419. nonCacheInput: nonCachedTokens,
  1420. cacheInput: cacheTokens,
  1421. cacheRatio: cacheRatio,
  1422. cacheCreationInput: cacheCreationTokens,
  1423. cacheCreationRatio: cacheCreationRatio,
  1424. cachePrice: cacheRatioPrice,
  1425. cacheCreationPrice: cacheCreationRatioPrice,
  1426. price: inputRatioPrice,
  1427. completion: completionTokens,
  1428. compPrice: completionRatioPrice,
  1429. ratio: groupRatio,
  1430. ratioType: ratioLabel,
  1431. total: price.toFixed(6),
  1432. },
  1433. )
  1434. : i18next.t(
  1435. '提示 {{input}} tokens / 1M tokens * ${{price}} + 补全 {{completion}} tokens / 1M tokens * ${{compPrice}} * {{ratioType}} {{ratio}} = ${{total}}',
  1436. {
  1437. input: inputTokens,
  1438. price: inputRatioPrice,
  1439. completion: completionTokens,
  1440. compPrice: completionRatioPrice,
  1441. ratio: groupRatio,
  1442. ratioType: ratioLabel,
  1443. total: price.toFixed(6),
  1444. },
  1445. )}
  1446. </p>
  1447. <p>{i18next.t('仅供参考,以实际扣费为准')}</p>
  1448. </article>
  1449. </>
  1450. );
  1451. }
  1452. }
  1453. export function renderClaudeLogContent(
  1454. modelRatio,
  1455. completionRatio,
  1456. modelPrice = -1,
  1457. groupRatio,
  1458. user_group_ratio,
  1459. cacheRatio = 1.0,
  1460. cacheCreationRatio = 1.0,
  1461. ) {
  1462. const useUserGroupRatio = isValidGroupRatio(user_group_ratio);
  1463. const ratioLabel = useUserGroupRatio
  1464. ? i18next.t('专属倍率')
  1465. : i18next.t('分组倍率');
  1466. groupRatio = useUserGroupRatio ? user_group_ratio : groupRatio;
  1467. if (modelPrice !== -1) {
  1468. return i18next.t('模型价格 ${{price}},{{ratioType}} {{ratio}}', {
  1469. price: modelPrice,
  1470. ratioType: ratioLabel,
  1471. ratio: groupRatio,
  1472. });
  1473. } else {
  1474. return i18next.t(
  1475. '模型倍率 {{modelRatio}},输出倍率 {{completionRatio}},缓存倍率 {{cacheRatio}},缓存创建倍率 {{cacheCreationRatio}},{{ratioType}} {{ratio}}',
  1476. {
  1477. modelRatio: modelRatio,
  1478. completionRatio: completionRatio,
  1479. cacheRatio: cacheRatio,
  1480. cacheCreationRatio: cacheCreationRatio,
  1481. ratioType: ratioLabel,
  1482. ratio: groupRatio,
  1483. },
  1484. );
  1485. }
  1486. }
  1487. export function renderClaudeModelPriceSimple(
  1488. modelRatio,
  1489. modelPrice = -1,
  1490. groupRatio,
  1491. user_group_ratio,
  1492. cacheTokens = 0,
  1493. cacheRatio = 1.0,
  1494. cacheCreationTokens = 0,
  1495. cacheCreationRatio = 1.0,
  1496. ) {
  1497. const useUserGroupRatio = isValidGroupRatio(user_group_ratio);
  1498. const ratioLabel = useUserGroupRatio
  1499. ? i18next.t('专属倍率')
  1500. : i18next.t('分组倍率');
  1501. groupRatio = useUserGroupRatio ? user_group_ratio : groupRatio;
  1502. if (modelPrice !== -1) {
  1503. return i18next.t('价格:${{price}} * {{ratioType}}:{{ratio}}', {
  1504. price: modelPrice,
  1505. ratioType: ratioLabel,
  1506. ratio: groupRatio,
  1507. });
  1508. } else {
  1509. if (cacheTokens !== 0 || cacheCreationTokens !== 0) {
  1510. return i18next.t(
  1511. '模型: {{ratio}} * {{ratioType}}: {{groupRatio}} * 缓存: {{cacheRatio}}',
  1512. {
  1513. ratio: modelRatio,
  1514. ratioType: ratioLabel,
  1515. groupRatio: groupRatio,
  1516. cacheRatio: cacheRatio,
  1517. cacheCreationRatio: cacheCreationRatio,
  1518. },
  1519. );
  1520. } else {
  1521. return i18next.t('模型: {{ratio}} * {{ratioType}}: {{groupRatio}}', {
  1522. ratio: modelRatio,
  1523. ratioType: ratioLabel,
  1524. groupRatio: groupRatio,
  1525. });
  1526. }
  1527. }
  1528. }
  1529. /**
  1530. * rehype 插件:将段落等文本节点拆分为逐词 <span>,并添加淡入动画 class。
  1531. * 仅在流式渲染阶段使用,避免已渲染文字重复动画。
  1532. */
  1533. export function rehypeSplitWordsIntoSpans(options = {}) {
  1534. const { previousContentLength = 0 } = options;
  1535. return (tree) => {
  1536. let currentCharCount = 0; // 当前已处理的字符数
  1537. visit(tree, 'element', (node) => {
  1538. if (
  1539. ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'strong'].includes(
  1540. node.tagName,
  1541. ) &&
  1542. node.children
  1543. ) {
  1544. const newChildren = [];
  1545. node.children.forEach((child) => {
  1546. if (child.type === 'text') {
  1547. try {
  1548. // 使用 Intl.Segmenter 精准拆分中英文及标点
  1549. const segmenter = new Intl.Segmenter('zh', {
  1550. granularity: 'word',
  1551. });
  1552. const segments = segmenter.segment(child.value);
  1553. Array.from(segments)
  1554. .map((seg) => seg.segment)
  1555. .filter(Boolean)
  1556. .forEach((word) => {
  1557. const wordStartPos = currentCharCount;
  1558. const wordEndPos = currentCharCount + word.length;
  1559. // 判断这个词是否是新增的(在 previousContentLength 之后)
  1560. const isNewContent = wordStartPos >= previousContentLength;
  1561. newChildren.push({
  1562. type: 'element',
  1563. tagName: 'span',
  1564. properties: {
  1565. className: isNewContent ? ['animate-fade-in'] : [],
  1566. },
  1567. children: [{ type: 'text', value: word }],
  1568. });
  1569. currentCharCount = wordEndPos;
  1570. });
  1571. } catch (_) {
  1572. // Fallback:如果浏览器不支持 Segmenter
  1573. const textStartPos = currentCharCount;
  1574. const isNewContent = textStartPos >= previousContentLength;
  1575. if (isNewContent) {
  1576. // 新内容,添加动画
  1577. newChildren.push({
  1578. type: 'element',
  1579. tagName: 'span',
  1580. properties: {
  1581. className: ['animate-fade-in'],
  1582. },
  1583. children: [{ type: 'text', value: child.value }],
  1584. });
  1585. } else {
  1586. // 旧内容,不添加动画
  1587. newChildren.push(child);
  1588. }
  1589. currentCharCount += child.value.length;
  1590. }
  1591. } else {
  1592. newChildren.push(child);
  1593. }
  1594. });
  1595. node.children = newChildren;
  1596. }
  1597. });
  1598. };
  1599. }