index.ts 28 KB

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