render.js 48 KB

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