index.ts 25 KB

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