index.ts 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. import { v4 as uuidv4 } from 'uuid';
  2. import sha256 from 'js-sha256';
  3. import dayjs from 'dayjs';
  4. import relativeTime from 'dayjs/plugin/relativeTime';
  5. import isToday from 'dayjs/plugin/isToday';
  6. import isYesterday from 'dayjs/plugin/isYesterday';
  7. dayjs.extend(relativeTime);
  8. dayjs.extend(isToday);
  9. dayjs.extend(isYesterday);
  10. import { WEBUI_BASE_URL } from '$lib/constants';
  11. import { TTS_RESPONSE_SPLIT } from '$lib/types';
  12. //////////////////////////
  13. // Helper functions
  14. //////////////////////////
  15. export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
  16. function escapeRegExp(string: string): string {
  17. return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  18. }
  19. export const replaceTokens = (content, sourceIds, char, user) => {
  20. const charToken = /{{char}}/gi;
  21. const userToken = /{{user}}/gi;
  22. const videoIdToken = /{{VIDEO_FILE_ID_([a-f0-9-]+)}}/gi; // Regex to capture the video ID
  23. const htmlIdToken = /{{HTML_FILE_ID_([a-f0-9-]+)}}/gi; // Regex to capture the HTML ID
  24. // Replace {{char}} if char is provided
  25. if (char !== undefined && char !== null) {
  26. content = content.replace(charToken, char);
  27. }
  28. // Replace {{user}} if user is provided
  29. if (user !== undefined && user !== null) {
  30. content = content.replace(userToken, user);
  31. }
  32. // Replace video ID tags with corresponding <video> elements
  33. content = content.replace(videoIdToken, (match, fileId) => {
  34. const videoUrl = `${WEBUI_BASE_URL}/api/v1/files/${fileId}/content`;
  35. return `<video src="${videoUrl}" controls></video>`;
  36. });
  37. // Replace HTML ID tags with corresponding HTML content
  38. content = content.replace(htmlIdToken, (match, fileId) => {
  39. const htmlUrl = `${WEBUI_BASE_URL}/api/v1/files/${fileId}/content/html`;
  40. return `<iframe src="${htmlUrl}" width="100%" frameborder="0" onload="this.style.height=(this.contentWindow.document.body.scrollHeight+20)+'px';"></iframe>`;
  41. });
  42. // Remove sourceIds from the content and replace them with <source_id>...</source_id>
  43. if (Array.isArray(sourceIds)) {
  44. sourceIds.forEach((sourceId) => {
  45. // Escape special characters in the sourceId
  46. const escapedSourceId = escapeRegExp(sourceId);
  47. // Create a token based on the exact `[sourceId]` string
  48. const sourceToken = `\\[${escapedSourceId}\\]`; // Escape special characters for RegExp
  49. const sourceRegex = new RegExp(sourceToken, 'g'); // Match all occurrences of [sourceId]
  50. content = content.replace(sourceRegex, `<source_id data="${sourceId}" />`);
  51. });
  52. }
  53. return content;
  54. };
  55. export const sanitizeResponseContent = (content: string) => {
  56. return content
  57. .replace(/<\|[a-z]*$/, '')
  58. .replace(/<\|[a-z]+\|$/, '')
  59. .replace(/<$/, '')
  60. .replaceAll(/<\|[a-z]+\|>/g, ' ')
  61. .replaceAll('<', '&lt;')
  62. .replaceAll('>', '&gt;')
  63. .trim();
  64. };
  65. export const processResponseContent = (content: string) => {
  66. return content.trim();
  67. };
  68. export function unescapeHtml(html: string) {
  69. const doc = new DOMParser().parseFromString(html, 'text/html');
  70. return doc.documentElement.textContent;
  71. }
  72. export const capitalizeFirstLetter = (string) => {
  73. return string.charAt(0).toUpperCase() + string.slice(1);
  74. };
  75. export const splitStream = (splitOn) => {
  76. let buffer = '';
  77. return new TransformStream({
  78. transform(chunk, controller) {
  79. buffer += chunk;
  80. const parts = buffer.split(splitOn);
  81. parts.slice(0, -1).forEach((part) => controller.enqueue(part));
  82. buffer = parts[parts.length - 1];
  83. },
  84. flush(controller) {
  85. if (buffer) controller.enqueue(buffer);
  86. }
  87. });
  88. };
  89. export const convertMessagesToHistory = (messages) => {
  90. const history = {
  91. messages: {},
  92. currentId: null
  93. };
  94. let parentMessageId = null;
  95. let messageId = null;
  96. for (const message of messages) {
  97. messageId = uuidv4();
  98. if (parentMessageId !== null) {
  99. history.messages[parentMessageId].childrenIds = [
  100. ...history.messages[parentMessageId].childrenIds,
  101. messageId
  102. ];
  103. }
  104. history.messages[messageId] = {
  105. ...message,
  106. id: messageId,
  107. parentId: parentMessageId,
  108. childrenIds: []
  109. };
  110. parentMessageId = messageId;
  111. }
  112. history.currentId = messageId;
  113. return history;
  114. };
  115. export const getGravatarURL = (email) => {
  116. // Trim leading and trailing whitespace from
  117. // an email address and force all characters
  118. // to lower case
  119. const address = String(email).trim().toLowerCase();
  120. // Create a SHA256 hash of the final string
  121. const hash = sha256(address);
  122. // Grab the actual image URL
  123. return `https://www.gravatar.com/avatar/${hash}`;
  124. };
  125. export const canvasPixelTest = () => {
  126. // Test a 1x1 pixel to potentially identify browser/plugin fingerprint blocking or spoofing
  127. // Inspiration: https://github.com/kkapsner/CanvasBlocker/blob/master/test/detectionTest.js
  128. const canvas = document.createElement('canvas');
  129. const ctx = canvas.getContext('2d');
  130. canvas.height = 1;
  131. canvas.width = 1;
  132. const imageData = new ImageData(canvas.width, canvas.height);
  133. const pixelValues = imageData.data;
  134. // Generate RGB test data
  135. for (let i = 0; i < imageData.data.length; i += 1) {
  136. if (i % 4 !== 3) {
  137. pixelValues[i] = Math.floor(256 * Math.random());
  138. } else {
  139. pixelValues[i] = 255;
  140. }
  141. }
  142. ctx.putImageData(imageData, 0, 0);
  143. const p = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
  144. // Read RGB data and fail if unmatched
  145. for (let i = 0; i < p.length; i += 1) {
  146. if (p[i] !== pixelValues[i]) {
  147. console.log(
  148. 'canvasPixelTest: Wrong canvas pixel RGB value detected:',
  149. p[i],
  150. 'at:',
  151. i,
  152. 'expected:',
  153. pixelValues[i]
  154. );
  155. console.log('canvasPixelTest: Canvas blocking or spoofing is likely');
  156. return false;
  157. }
  158. }
  159. return true;
  160. };
  161. export const compressImage = async (imageUrl, maxWidth, maxHeight) => {
  162. return new Promise((resolve, reject) => {
  163. const img = new Image();
  164. img.onload = () => {
  165. const canvas = document.createElement('canvas');
  166. let width = img.width;
  167. let height = img.height;
  168. // Maintain aspect ratio while resizing
  169. if (maxWidth && maxHeight) {
  170. // Resize with both dimensions defined (preserves aspect ratio)
  171. if (width <= maxWidth && height <= maxHeight) {
  172. resolve(imageUrl);
  173. return;
  174. }
  175. if (width / height > maxWidth / maxHeight) {
  176. height = Math.round((maxWidth * height) / width);
  177. width = maxWidth;
  178. } else {
  179. width = Math.round((maxHeight * width) / height);
  180. height = maxHeight;
  181. }
  182. } else if (maxWidth) {
  183. // Only maxWidth defined
  184. if (width <= maxWidth) {
  185. resolve(imageUrl);
  186. return;
  187. }
  188. height = Math.round((maxWidth * height) / width);
  189. width = maxWidth;
  190. } else if (maxHeight) {
  191. // Only maxHeight defined
  192. if (height <= maxHeight) {
  193. resolve(imageUrl);
  194. return;
  195. }
  196. width = Math.round((maxHeight * width) / height);
  197. height = maxHeight;
  198. }
  199. canvas.width = width;
  200. canvas.height = height;
  201. const context = canvas.getContext('2d');
  202. context.drawImage(img, 0, 0, width, height);
  203. // Get compressed image URL
  204. const compressedUrl = canvas.toDataURL();
  205. resolve(compressedUrl);
  206. };
  207. img.onerror = (error) => reject(error);
  208. img.src = imageUrl;
  209. });
  210. };
  211. export const generateInitialsImage = (name) => {
  212. const canvas = document.createElement('canvas');
  213. const ctx = canvas.getContext('2d');
  214. canvas.width = 100;
  215. canvas.height = 100;
  216. if (!canvasPixelTest()) {
  217. console.log(
  218. 'generateInitialsImage: failed pixel test, fingerprint evasion is likely. Using default image.'
  219. );
  220. return '/user.png';
  221. }
  222. ctx.fillStyle = '#F39C12';
  223. ctx.fillRect(0, 0, canvas.width, canvas.height);
  224. ctx.fillStyle = '#FFFFFF';
  225. ctx.font = '40px Helvetica';
  226. ctx.textAlign = 'center';
  227. ctx.textBaseline = 'middle';
  228. const sanitizedName = name.trim();
  229. const initials =
  230. sanitizedName.length > 0
  231. ? sanitizedName[0] +
  232. (sanitizedName.split(' ').length > 1
  233. ? sanitizedName[sanitizedName.lastIndexOf(' ') + 1]
  234. : '')
  235. : '';
  236. ctx.fillText(initials.toUpperCase(), canvas.width / 2, canvas.height / 2);
  237. return canvas.toDataURL();
  238. };
  239. export const formatDate = (inputDate) => {
  240. const date = dayjs(inputDate);
  241. const now = dayjs();
  242. if (date.isToday()) {
  243. return `Today at ${date.format('HH:mm')}`;
  244. } else if (date.isYesterday()) {
  245. return `Yesterday at ${date.format('HH:mm')}`;
  246. } else {
  247. return `${date.format('DD/MM/YYYY')} at ${date.format('HH:mm')}`;
  248. }
  249. };
  250. export const copyToClipboard = async (text) => {
  251. let result = false;
  252. if (!navigator.clipboard) {
  253. const textArea = document.createElement('textarea');
  254. textArea.value = text;
  255. // Avoid scrolling to bottom
  256. textArea.style.top = '0';
  257. textArea.style.left = '0';
  258. textArea.style.position = 'fixed';
  259. document.body.appendChild(textArea);
  260. textArea.focus();
  261. textArea.select();
  262. try {
  263. const successful = document.execCommand('copy');
  264. const msg = successful ? 'successful' : 'unsuccessful';
  265. console.log('Fallback: Copying text command was ' + msg);
  266. result = true;
  267. } catch (err) {
  268. console.error('Fallback: Oops, unable to copy', err);
  269. }
  270. document.body.removeChild(textArea);
  271. return result;
  272. }
  273. result = await navigator.clipboard
  274. .writeText(text)
  275. .then(() => {
  276. console.log('Async: Copying to clipboard was successful!');
  277. return true;
  278. })
  279. .catch((error) => {
  280. console.error('Async: Could not copy text: ', error);
  281. return false;
  282. });
  283. return result;
  284. };
  285. export const compareVersion = (latest, current) => {
  286. return current === '0.0.0'
  287. ? false
  288. : current.localeCompare(latest, undefined, {
  289. numeric: true,
  290. sensitivity: 'case',
  291. caseFirst: 'upper'
  292. }) < 0;
  293. };
  294. export const findWordIndices = (text) => {
  295. const regex = /\[([^\]]+)\]/g;
  296. const matches = [];
  297. let match;
  298. while ((match = regex.exec(text)) !== null) {
  299. matches.push({
  300. word: match[1],
  301. startIndex: match.index,
  302. endIndex: regex.lastIndex - 1
  303. });
  304. }
  305. return matches;
  306. };
  307. export const removeLastWordFromString = (inputString, wordString) => {
  308. console.log('inputString', inputString);
  309. // Split the string by newline characters to handle lines separately
  310. const lines = inputString.split('\n');
  311. // Take the last line to operate only on it
  312. const lastLine = lines.pop();
  313. // Split the last line into an array of words
  314. const words = lastLine.split(' ');
  315. // Conditional to check for the last word removal
  316. if (words.at(-1) === wordString || (wordString === '' && words.at(-1) === '\\#')) {
  317. words.pop(); // Remove last word if condition is satisfied
  318. }
  319. // Join the remaining words back into a string and handle space correctly
  320. let updatedLastLine = words.join(' ');
  321. // Add a trailing space to the updated last line if there are still words
  322. if (updatedLastLine !== '') {
  323. updatedLastLine += ' ';
  324. }
  325. // Combine the lines together again, placing the updated last line back in
  326. const resultString = [...lines, updatedLastLine].join('\n');
  327. // Return the final string
  328. console.log('resultString', resultString);
  329. return resultString;
  330. };
  331. export const removeFirstHashWord = (inputString) => {
  332. // Split the string into an array of words
  333. const words = inputString.split(' ');
  334. // Find the index of the first word that starts with #
  335. const index = words.findIndex((word) => word.startsWith('#'));
  336. // Remove the first word with #
  337. if (index !== -1) {
  338. words.splice(index, 1);
  339. }
  340. // Join the remaining words back into a string
  341. const resultString = words.join(' ');
  342. return resultString;
  343. };
  344. export const transformFileName = (fileName) => {
  345. // Convert to lowercase
  346. const lowerCaseFileName = fileName.toLowerCase();
  347. // Remove special characters using regular expression
  348. const sanitizedFileName = lowerCaseFileName.replace(/[^\w\s]/g, '');
  349. // Replace spaces with dashes
  350. const finalFileName = sanitizedFileName.replace(/\s+/g, '-');
  351. return finalFileName;
  352. };
  353. export const calculateSHA256 = async (file) => {
  354. // Create a FileReader to read the file asynchronously
  355. const reader = new FileReader();
  356. // Define a promise to handle the file reading
  357. const readFile = new Promise((resolve, reject) => {
  358. reader.onload = () => resolve(reader.result);
  359. reader.onerror = reject;
  360. });
  361. // Read the file as an ArrayBuffer
  362. reader.readAsArrayBuffer(file);
  363. try {
  364. // Wait for the FileReader to finish reading the file
  365. const buffer = await readFile;
  366. // Convert the ArrayBuffer to a Uint8Array
  367. const uint8Array = new Uint8Array(buffer);
  368. // Calculate the SHA-256 hash using Web Crypto API
  369. const hashBuffer = await crypto.subtle.digest('SHA-256', uint8Array);
  370. // Convert the hash to a hexadecimal string
  371. const hashArray = Array.from(new Uint8Array(hashBuffer));
  372. const hashHex = hashArray.map((byte) => byte.toString(16).padStart(2, '0')).join('');
  373. return `${hashHex}`;
  374. } catch (error) {
  375. console.error('Error calculating SHA-256 hash:', error);
  376. throw error;
  377. }
  378. };
  379. export const getImportOrigin = (_chats) => {
  380. // Check what external service chat imports are from
  381. if ('mapping' in _chats[0]) {
  382. return 'openai';
  383. }
  384. return 'webui';
  385. };
  386. export const getUserPosition = async (raw = false) => {
  387. // Get the user's location using the Geolocation API
  388. const position = await new Promise((resolve, reject) => {
  389. navigator.geolocation.getCurrentPosition(resolve, reject);
  390. }).catch((error) => {
  391. console.error('Error getting user location:', error);
  392. throw error;
  393. });
  394. if (!position) {
  395. return 'Location not available';
  396. }
  397. // Extract the latitude and longitude from the position
  398. const { latitude, longitude } = position.coords;
  399. if (raw) {
  400. return { latitude, longitude };
  401. } else {
  402. return `${latitude.toFixed(3)}, ${longitude.toFixed(3)} (lat, long)`;
  403. }
  404. };
  405. const convertOpenAIMessages = (convo) => {
  406. // Parse OpenAI chat messages and create chat dictionary for creating new chats
  407. const mapping = convo['mapping'];
  408. const messages = [];
  409. let currentId = '';
  410. let lastId = null;
  411. for (const message_id in mapping) {
  412. const message = mapping[message_id];
  413. currentId = message_id;
  414. try {
  415. if (
  416. messages.length == 0 &&
  417. (message['message'] == null ||
  418. (message['message']['content']['parts']?.[0] == '' &&
  419. message['message']['content']['text'] == null))
  420. ) {
  421. // Skip chat messages with no content
  422. continue;
  423. } else {
  424. const new_chat = {
  425. id: message_id,
  426. parentId: lastId,
  427. childrenIds: message['children'] || [],
  428. role: message['message']?.['author']?.['role'] !== 'user' ? 'assistant' : 'user',
  429. content:
  430. message['message']?.['content']?.['parts']?.[0] ||
  431. message['message']?.['content']?.['text'] ||
  432. '',
  433. model: 'gpt-3.5-turbo',
  434. done: true,
  435. context: null
  436. };
  437. messages.push(new_chat);
  438. lastId = currentId;
  439. }
  440. } catch (error) {
  441. console.log('Error with', message, '\nError:', error);
  442. }
  443. }
  444. const history: Record<PropertyKey, (typeof messages)[number]> = {};
  445. messages.forEach((obj) => (history[obj.id] = obj));
  446. const chat = {
  447. history: {
  448. currentId: currentId,
  449. messages: history // Need to convert this to not a list and instead a json object
  450. },
  451. models: ['gpt-3.5-turbo'],
  452. messages: messages,
  453. options: {},
  454. timestamp: convo['create_time'],
  455. title: convo['title'] ?? 'New Chat'
  456. };
  457. return chat;
  458. };
  459. const validateChat = (chat) => {
  460. // Because ChatGPT sometimes has features we can't use like DALL-E or might have corrupted messages, need to validate
  461. const messages = chat.messages;
  462. // Check if messages array is empty
  463. if (messages.length === 0) {
  464. return false;
  465. }
  466. // Last message's children should be an empty array
  467. const lastMessage = messages[messages.length - 1];
  468. if (lastMessage.childrenIds.length !== 0) {
  469. return false;
  470. }
  471. // First message's parent should be null
  472. const firstMessage = messages[0];
  473. if (firstMessage.parentId !== null) {
  474. return false;
  475. }
  476. // Every message's content should be a string
  477. for (const message of messages) {
  478. if (typeof message.content !== 'string') {
  479. return false;
  480. }
  481. }
  482. return true;
  483. };
  484. export const convertOpenAIChats = (_chats) => {
  485. // Create a list of dictionaries with each conversation from import
  486. const chats = [];
  487. let failed = 0;
  488. for (const convo of _chats) {
  489. const chat = convertOpenAIMessages(convo);
  490. if (validateChat(chat)) {
  491. chats.push({
  492. id: convo['id'],
  493. user_id: '',
  494. title: convo['title'],
  495. chat: chat,
  496. timestamp: convo['timestamp']
  497. });
  498. } else {
  499. failed++;
  500. }
  501. }
  502. console.log(failed, 'Conversations could not be imported');
  503. return chats;
  504. };
  505. export const isValidHttpUrl = (string: string) => {
  506. let url;
  507. try {
  508. url = new URL(string);
  509. } catch (_) {
  510. return false;
  511. }
  512. return url.protocol === 'http:' || url.protocol === 'https:';
  513. };
  514. export const removeEmojis = (str: string) => {
  515. // Regular expression to match emojis
  516. const emojiRegex = /[\uD800-\uDBFF][\uDC00-\uDFFF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g;
  517. // Replace emojis with an empty string
  518. return str.replace(emojiRegex, '');
  519. };
  520. export const removeFormattings = (str: string) => {
  521. return (
  522. str
  523. // Block elements (remove completely)
  524. .replace(/(```[\s\S]*?```)/g, '') // Code blocks
  525. .replace(/^\|.*\|$/gm, '') // Tables
  526. // Inline elements (preserve content)
  527. .replace(/(?:\*\*|__)(.*?)(?:\*\*|__)/g, '$1') // Bold
  528. .replace(/(?:[*_])(.*?)(?:[*_])/g, '$1') // Italic
  529. .replace(/~~(.*?)~~/g, '$1') // Strikethrough
  530. .replace(/`([^`]+)`/g, '$1') // Inline code
  531. // Links and images
  532. .replace(/!?\[([^\]]*)\](?:\([^)]+\)|\[[^\]]*\])/g, '$1') // Links & images
  533. .replace(/^\[[^\]]+\]:\s*.*$/gm, '') // Reference definitions
  534. // Block formatting
  535. .replace(/^#{1,6}\s+/gm, '') // Headers
  536. .replace(/^\s*[-*+]\s+/gm, '') // Lists
  537. .replace(/^\s*(?:\d+\.)\s+/gm, '') // Numbered lists
  538. .replace(/^\s*>[> ]*/gm, '') // Blockquotes
  539. .replace(/^\s*:\s+/gm, '') // Definition lists
  540. // Cleanup
  541. .replace(/\[\^[^\]]*\]/g, '') // Footnotes
  542. .replace(/[-*_~]/g, '') // Remaining markers
  543. .replace(/\n{2,}/g, '\n')
  544. ); // Multiple newlines
  545. };
  546. export const cleanText = (content: string) => {
  547. return removeFormattings(removeEmojis(content.trim()));
  548. };
  549. export const removeDetailsWithReasoning = (content) => {
  550. return content.replace(/<details\s+type="reasoning"[^>]*>.*?<\/details>/gis, '').trim();
  551. };
  552. // This regular expression matches code blocks marked by triple backticks
  553. const codeBlockRegex = /```[\s\S]*?```/g;
  554. export const extractSentences = (text: string) => {
  555. const codeBlocks: string[] = [];
  556. let index = 0;
  557. // Temporarily replace code blocks with placeholders and store the blocks separately
  558. text = text.replace(codeBlockRegex, (match) => {
  559. const placeholder = `\u0000${index}\u0000`; // Use a unique placeholder
  560. codeBlocks[index++] = match;
  561. return placeholder;
  562. });
  563. // Split the modified text into sentences based on common punctuation marks, avoiding these blocks
  564. let sentences = text.split(/(?<=[.!?])\s+/);
  565. // Restore code blocks and process sentences
  566. sentences = sentences.map((sentence) => {
  567. // Check if the sentence includes a placeholder for a code block
  568. return sentence.replace(/\u0000(\d+)\u0000/g, (_, idx) => codeBlocks[idx]);
  569. });
  570. return sentences.map(cleanText).filter(Boolean);
  571. };
  572. export const extractParagraphsForAudio = (text: string) => {
  573. const codeBlocks: string[] = [];
  574. let index = 0;
  575. // Temporarily replace code blocks with placeholders and store the blocks separately
  576. text = text.replace(codeBlockRegex, (match) => {
  577. const placeholder = `\u0000${index}\u0000`; // Use a unique placeholder
  578. codeBlocks[index++] = match;
  579. return placeholder;
  580. });
  581. // Split the modified text into paragraphs based on newlines, avoiding these blocks
  582. let paragraphs = text.split(/\n+/);
  583. // Restore code blocks and process paragraphs
  584. paragraphs = paragraphs.map((paragraph) => {
  585. // Check if the paragraph includes a placeholder for a code block
  586. return paragraph.replace(/\u0000(\d+)\u0000/g, (_, idx) => codeBlocks[idx]);
  587. });
  588. return paragraphs.map(cleanText).filter(Boolean);
  589. };
  590. export const extractSentencesForAudio = (text: string) => {
  591. return extractSentences(text).reduce((mergedTexts, currentText) => {
  592. const lastIndex = mergedTexts.length - 1;
  593. if (lastIndex >= 0) {
  594. const previousText = mergedTexts[lastIndex];
  595. const wordCount = previousText.split(/\s+/).length;
  596. const charCount = previousText.length;
  597. if (wordCount < 4 || charCount < 50) {
  598. mergedTexts[lastIndex] = previousText + ' ' + currentText;
  599. } else {
  600. mergedTexts.push(currentText);
  601. }
  602. } else {
  603. mergedTexts.push(currentText);
  604. }
  605. return mergedTexts;
  606. }, [] as string[]);
  607. };
  608. export const getMessageContentParts = (content: string, split_on: string = 'punctuation') => {
  609. content = removeDetailsWithReasoning(content);
  610. const messageContentParts: string[] = [];
  611. switch (split_on) {
  612. default:
  613. case TTS_RESPONSE_SPLIT.PUNCTUATION:
  614. messageContentParts.push(...extractSentencesForAudio(content));
  615. break;
  616. case TTS_RESPONSE_SPLIT.PARAGRAPHS:
  617. messageContentParts.push(...extractParagraphsForAudio(content));
  618. break;
  619. case TTS_RESPONSE_SPLIT.NONE:
  620. messageContentParts.push(cleanText(content));
  621. break;
  622. }
  623. return messageContentParts;
  624. };
  625. export const blobToFile = (blob, fileName) => {
  626. // Create a new File object from the Blob
  627. const file = new File([blob], fileName, { type: blob.type });
  628. return file;
  629. };
  630. /**
  631. * @param {string} template - The template string containing placeholders.
  632. * @returns {string} The template string with the placeholders replaced by the prompt.
  633. */
  634. export const promptTemplate = (
  635. template: string,
  636. user_name?: string,
  637. user_location?: string
  638. ): string => {
  639. // Get the current date
  640. const currentDate = new Date();
  641. // Format the date to YYYY-MM-DD
  642. const formattedDate =
  643. currentDate.getFullYear() +
  644. '-' +
  645. String(currentDate.getMonth() + 1).padStart(2, '0') +
  646. '-' +
  647. String(currentDate.getDate()).padStart(2, '0');
  648. // Format the time to HH:MM:SS AM/PM
  649. const currentTime = currentDate.toLocaleTimeString('en-US', {
  650. hour: 'numeric',
  651. minute: 'numeric',
  652. second: 'numeric',
  653. hour12: true
  654. });
  655. // Get the current weekday
  656. const currentWeekday = getWeekday();
  657. // Get the user's timezone
  658. const currentTimezone = getUserTimezone();
  659. // Get the user's language
  660. const userLanguage = localStorage.getItem('locale') || 'en-US';
  661. // Replace {{CURRENT_DATETIME}} in the template with the formatted datetime
  662. template = template.replace('{{CURRENT_DATETIME}}', `${formattedDate} ${currentTime}`);
  663. // Replace {{CURRENT_DATE}} in the template with the formatted date
  664. template = template.replace('{{CURRENT_DATE}}', formattedDate);
  665. // Replace {{CURRENT_TIME}} in the template with the formatted time
  666. template = template.replace('{{CURRENT_TIME}}', currentTime);
  667. // Replace {{CURRENT_WEEKDAY}} in the template with the current weekday
  668. template = template.replace('{{CURRENT_WEEKDAY}}', currentWeekday);
  669. // Replace {{CURRENT_TIMEZONE}} in the template with the user's timezone
  670. template = template.replace('{{CURRENT_TIMEZONE}}', currentTimezone);
  671. // Replace {{USER_LANGUAGE}} in the template with the user's language
  672. template = template.replace('{{USER_LANGUAGE}}', userLanguage);
  673. if (user_name) {
  674. // Replace {{USER_NAME}} in the template with the user's name
  675. template = template.replace('{{USER_NAME}}', user_name);
  676. }
  677. if (user_location) {
  678. // Replace {{USER_LOCATION}} in the template with the current location
  679. template = template.replace('{{USER_LOCATION}}', user_location);
  680. }
  681. return template;
  682. };
  683. /**
  684. * This function is used to replace placeholders in a template string with the provided prompt.
  685. * The placeholders can be in the following formats:
  686. * - `{{prompt}}`: This will be replaced with the entire prompt.
  687. * - `{{prompt:start:<length>}}`: This will be replaced with the first <length> characters of the prompt.
  688. * - `{{prompt:end:<length>}}`: This will be replaced with the last <length> characters of the prompt.
  689. * - `{{prompt:middletruncate:<length>}}`: This will be replaced with the prompt truncated to <length> characters, with '...' in the middle.
  690. *
  691. * @param {string} template - The template string containing placeholders.
  692. * @param {string} prompt - The string to replace the placeholders with.
  693. * @returns {string} The template string with the placeholders replaced by the prompt.
  694. */
  695. export const titleGenerationTemplate = (template: string, prompt: string): string => {
  696. template = template.replace(
  697. /{{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}/g,
  698. (match, startLength, endLength, middleLength) => {
  699. if (match === '{{prompt}}') {
  700. return prompt;
  701. } else if (match.startsWith('{{prompt:start:')) {
  702. return prompt.substring(0, startLength);
  703. } else if (match.startsWith('{{prompt:end:')) {
  704. return prompt.slice(-endLength);
  705. } else if (match.startsWith('{{prompt:middletruncate:')) {
  706. if (prompt.length <= middleLength) {
  707. return prompt;
  708. }
  709. const start = prompt.slice(0, Math.ceil(middleLength / 2));
  710. const end = prompt.slice(-Math.floor(middleLength / 2));
  711. return `${start}...${end}`;
  712. }
  713. return '';
  714. }
  715. );
  716. template = promptTemplate(template);
  717. return template;
  718. };
  719. export const approximateToHumanReadable = (nanoseconds: number) => {
  720. const seconds = Math.floor((nanoseconds / 1e9) % 60);
  721. const minutes = Math.floor((nanoseconds / 6e10) % 60);
  722. const hours = Math.floor((nanoseconds / 3.6e12) % 24);
  723. const results: string[] = [];
  724. if (seconds >= 0) {
  725. results.push(`${seconds}s`);
  726. }
  727. if (minutes > 0) {
  728. results.push(`${minutes}m`);
  729. }
  730. if (hours > 0) {
  731. results.push(`${hours}h`);
  732. }
  733. return results.reverse().join(' ');
  734. };
  735. export const getTimeRange = (timestamp) => {
  736. const now = new Date();
  737. const date = new Date(timestamp * 1000); // Convert Unix timestamp to milliseconds
  738. // Calculate the difference in milliseconds
  739. const diffTime = now.getTime() - date.getTime();
  740. const diffDays = diffTime / (1000 * 3600 * 24);
  741. const nowDate = now.getDate();
  742. const nowMonth = now.getMonth();
  743. const nowYear = now.getFullYear();
  744. const dateDate = date.getDate();
  745. const dateMonth = date.getMonth();
  746. const dateYear = date.getFullYear();
  747. if (nowYear === dateYear && nowMonth === dateMonth && nowDate === dateDate) {
  748. return 'Today';
  749. } else if (nowYear === dateYear && nowMonth === dateMonth && nowDate - dateDate === 1) {
  750. return 'Yesterday';
  751. } else if (diffDays <= 7) {
  752. return 'Previous 7 days';
  753. } else if (diffDays <= 30) {
  754. return 'Previous 30 days';
  755. } else if (nowYear === dateYear) {
  756. return date.toLocaleString('default', { month: 'long' });
  757. } else {
  758. return date.getFullYear().toString();
  759. }
  760. };
  761. /**
  762. * Extract frontmatter as a dictionary from the specified content string.
  763. * @param content {string} - The content string with potential frontmatter.
  764. * @returns {Object} - The extracted frontmatter as a dictionary.
  765. */
  766. export const extractFrontmatter = (content) => {
  767. const frontmatter = {};
  768. let frontmatterStarted = false;
  769. let frontmatterEnded = false;
  770. const frontmatterPattern = /^\s*([a-z_]+):\s*(.*)\s*$/i;
  771. // Split content into lines
  772. const lines = content.split('\n');
  773. // Check if the content starts with triple quotes
  774. if (lines[0].trim() !== '"""') {
  775. return {};
  776. }
  777. frontmatterStarted = true;
  778. for (let i = 1; i < lines.length; i++) {
  779. const line = lines[i];
  780. if (line.includes('"""')) {
  781. if (frontmatterStarted) {
  782. frontmatterEnded = true;
  783. break;
  784. }
  785. }
  786. if (frontmatterStarted && !frontmatterEnded) {
  787. const match = frontmatterPattern.exec(line);
  788. if (match) {
  789. const [, key, value] = match;
  790. frontmatter[key.trim()] = value.trim();
  791. }
  792. }
  793. }
  794. return frontmatter;
  795. };
  796. // Function to determine the best matching language
  797. export const bestMatchingLanguage = (supportedLanguages, preferredLanguages, defaultLocale) => {
  798. const languages = supportedLanguages.map((lang) => lang.code);
  799. const match = preferredLanguages
  800. .map((prefLang) => languages.find((lang) => lang.startsWith(prefLang)))
  801. .find(Boolean);
  802. return match || defaultLocale;
  803. };
  804. // Get the date in the format YYYY-MM-DD
  805. export const getFormattedDate = () => {
  806. const date = new Date();
  807. return date.toISOString().split('T')[0];
  808. };
  809. // Get the time in the format HH:MM:SS
  810. export const getFormattedTime = () => {
  811. const date = new Date();
  812. return date.toTimeString().split(' ')[0];
  813. };
  814. // Get the current date and time in the format YYYY-MM-DD HH:MM:SS
  815. export const getCurrentDateTime = () => {
  816. return `${getFormattedDate()} ${getFormattedTime()}`;
  817. };
  818. // Get the user's timezone
  819. export const getUserTimezone = () => {
  820. return Intl.DateTimeFormat().resolvedOptions().timeZone;
  821. };
  822. // Get the weekday
  823. export const getWeekday = () => {
  824. const date = new Date();
  825. const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  826. return weekdays[date.getDay()];
  827. };
  828. export const createMessagesList = (history, messageId) => {
  829. if (messageId === null) {
  830. return [];
  831. }
  832. const message = history.messages[messageId];
  833. if (message?.parentId) {
  834. return [...createMessagesList(history, message.parentId), message];
  835. } else {
  836. return [message];
  837. }
  838. };
  839. export const formatFileSize = (size) => {
  840. if (size == null) return 'Unknown size';
  841. if (typeof size !== 'number' || size < 0) return 'Invalid size';
  842. if (size === 0) return '0 B';
  843. const units = ['B', 'KB', 'MB', 'GB', 'TB'];
  844. let unitIndex = 0;
  845. while (size >= 1024 && unitIndex < units.length - 1) {
  846. size /= 1024;
  847. unitIndex++;
  848. }
  849. return `${size.toFixed(1)} ${units[unitIndex]}`;
  850. };
  851. export const getLineCount = (text) => {
  852. console.log(typeof text);
  853. return text ? text.split('\n').length : 0;
  854. };