index.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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`;
  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. // Split the string into an array of words
  229. const words = inputString.split(' ');
  230. if (words.at(-1) === wordString) {
  231. words.pop();
  232. }
  233. // Join the remaining words back into a string
  234. let resultString = words.join(' ');
  235. if (resultString !== '') {
  236. resultString += ' ';
  237. }
  238. return resultString;
  239. };
  240. export const removeFirstHashWord = (inputString) => {
  241. // Split the string into an array of words
  242. const words = inputString.split(' ');
  243. // Find the index of the first word that starts with #
  244. const index = words.findIndex((word) => word.startsWith('#'));
  245. // Remove the first word with #
  246. if (index !== -1) {
  247. words.splice(index, 1);
  248. }
  249. // Join the remaining words back into a string
  250. const resultString = words.join(' ');
  251. return resultString;
  252. };
  253. export const transformFileName = (fileName) => {
  254. // Convert to lowercase
  255. const lowerCaseFileName = fileName.toLowerCase();
  256. // Remove special characters using regular expression
  257. const sanitizedFileName = lowerCaseFileName.replace(/[^\w\s]/g, '');
  258. // Replace spaces with dashes
  259. const finalFileName = sanitizedFileName.replace(/\s+/g, '-');
  260. return finalFileName;
  261. };
  262. export const calculateSHA256 = async (file) => {
  263. // Create a FileReader to read the file asynchronously
  264. const reader = new FileReader();
  265. // Define a promise to handle the file reading
  266. const readFile = new Promise((resolve, reject) => {
  267. reader.onload = () => resolve(reader.result);
  268. reader.onerror = reject;
  269. });
  270. // Read the file as an ArrayBuffer
  271. reader.readAsArrayBuffer(file);
  272. try {
  273. // Wait for the FileReader to finish reading the file
  274. const buffer = await readFile;
  275. // Convert the ArrayBuffer to a Uint8Array
  276. const uint8Array = new Uint8Array(buffer);
  277. // Calculate the SHA-256 hash using Web Crypto API
  278. const hashBuffer = await crypto.subtle.digest('SHA-256', uint8Array);
  279. // Convert the hash to a hexadecimal string
  280. const hashArray = Array.from(new Uint8Array(hashBuffer));
  281. const hashHex = hashArray.map((byte) => byte.toString(16).padStart(2, '0')).join('');
  282. return `${hashHex}`;
  283. } catch (error) {
  284. console.error('Error calculating SHA-256 hash:', error);
  285. throw error;
  286. }
  287. };
  288. export const getImportOrigin = (_chats) => {
  289. // Check what external service chat imports are from
  290. if ('mapping' in _chats[0]) {
  291. return 'openai';
  292. }
  293. return 'webui';
  294. };
  295. export const getUserPosition = async (raw = false) => {
  296. // Get the user's location using the Geolocation API
  297. const position = await new Promise((resolve, reject) => {
  298. navigator.geolocation.getCurrentPosition(resolve, reject);
  299. }).catch((error) => {
  300. console.error('Error getting user location:', error);
  301. throw error;
  302. });
  303. if (!position) {
  304. return 'Location not available';
  305. }
  306. // Extract the latitude and longitude from the position
  307. const { latitude, longitude } = position.coords;
  308. if (raw) {
  309. return { latitude, longitude };
  310. } else {
  311. return `${latitude.toFixed(3)}, ${longitude.toFixed(3)} (lat, long)`;
  312. }
  313. };
  314. const convertOpenAIMessages = (convo) => {
  315. // Parse OpenAI chat messages and create chat dictionary for creating new chats
  316. const mapping = convo['mapping'];
  317. const messages = [];
  318. let currentId = '';
  319. let lastId = null;
  320. for (const message_id in mapping) {
  321. const message = mapping[message_id];
  322. currentId = message_id;
  323. try {
  324. if (
  325. messages.length == 0 &&
  326. (message['message'] == null ||
  327. (message['message']['content']['parts']?.[0] == '' &&
  328. message['message']['content']['text'] == null))
  329. ) {
  330. // Skip chat messages with no content
  331. continue;
  332. } else {
  333. const new_chat = {
  334. id: message_id,
  335. parentId: lastId,
  336. childrenIds: message['children'] || [],
  337. role: message['message']?.['author']?.['role'] !== 'user' ? 'assistant' : 'user',
  338. content:
  339. message['message']?.['content']?.['parts']?.[0] ||
  340. message['message']?.['content']?.['text'] ||
  341. '',
  342. model: 'gpt-3.5-turbo',
  343. done: true,
  344. context: null
  345. };
  346. messages.push(new_chat);
  347. lastId = currentId;
  348. }
  349. } catch (error) {
  350. console.log('Error with', message, '\nError:', error);
  351. }
  352. }
  353. const history: Record<PropertyKey, (typeof messages)[number]> = {};
  354. messages.forEach((obj) => (history[obj.id] = obj));
  355. const chat = {
  356. history: {
  357. currentId: currentId,
  358. messages: history // Need to convert this to not a list and instead a json object
  359. },
  360. models: ['gpt-3.5-turbo'],
  361. messages: messages,
  362. options: {},
  363. timestamp: convo['create_time'],
  364. title: convo['title'] ?? 'New Chat'
  365. };
  366. return chat;
  367. };
  368. const validateChat = (chat) => {
  369. // Because ChatGPT sometimes has features we can't use like DALL-E or migh have corrupted messages, need to validate
  370. const messages = chat.messages;
  371. // Check if messages array is empty
  372. if (messages.length === 0) {
  373. return false;
  374. }
  375. // Last message's children should be an empty array
  376. const lastMessage = messages[messages.length - 1];
  377. if (lastMessage.childrenIds.length !== 0) {
  378. return false;
  379. }
  380. // First message's parent should be null
  381. const firstMessage = messages[0];
  382. if (firstMessage.parentId !== null) {
  383. return false;
  384. }
  385. // Every message's content should be a string
  386. for (const message of messages) {
  387. if (typeof message.content !== 'string') {
  388. return false;
  389. }
  390. }
  391. return true;
  392. };
  393. export const convertOpenAIChats = (_chats) => {
  394. // Create a list of dictionaries with each conversation from import
  395. const chats = [];
  396. let failed = 0;
  397. for (const convo of _chats) {
  398. const chat = convertOpenAIMessages(convo);
  399. if (validateChat(chat)) {
  400. chats.push({
  401. id: convo['id'],
  402. user_id: '',
  403. title: convo['title'],
  404. chat: chat,
  405. timestamp: convo['timestamp']
  406. });
  407. } else {
  408. failed++;
  409. }
  410. }
  411. console.log(failed, 'Conversations could not be imported');
  412. return chats;
  413. };
  414. export const isValidHttpUrl = (string: string) => {
  415. let url;
  416. try {
  417. url = new URL(string);
  418. } catch (_) {
  419. return false;
  420. }
  421. return url.protocol === 'http:' || url.protocol === 'https:';
  422. };
  423. export const removeEmojis = (str: string) => {
  424. // Regular expression to match emojis
  425. const emojiRegex = /[\uD800-\uDBFF][\uDC00-\uDFFF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g;
  426. // Replace emojis with an empty string
  427. return str.replace(emojiRegex, '');
  428. };
  429. export const removeFormattings = (str: string) => {
  430. return str.replace(/(\*)(.*?)\1/g, '').replace(/(```)(.*?)\1/gs, '');
  431. };
  432. export const cleanText = (content: string) => {
  433. return removeFormattings(removeEmojis(content.trim()));
  434. };
  435. // This regular expression matches code blocks marked by triple backticks
  436. const codeBlockRegex = /```[\s\S]*?```/g;
  437. export const extractSentences = (text: string) => {
  438. const codeBlocks: string[] = [];
  439. let index = 0;
  440. // Temporarily replace code blocks with placeholders and store the blocks separately
  441. text = text.replace(codeBlockRegex, (match) => {
  442. const placeholder = `\u0000${index}\u0000`; // Use a unique placeholder
  443. codeBlocks[index++] = match;
  444. return placeholder;
  445. });
  446. // Split the modified text into sentences based on common punctuation marks, avoiding these blocks
  447. let sentences = text.split(/(?<=[.!?])\s+/);
  448. // Restore code blocks and process sentences
  449. sentences = sentences.map((sentence) => {
  450. // Check if the sentence includes a placeholder for a code block
  451. return sentence.replace(/\u0000(\d+)\u0000/g, (_, idx) => codeBlocks[idx]);
  452. });
  453. return sentences.map(cleanText).filter(Boolean);
  454. };
  455. export const extractParagraphsForAudio = (text: string) => {
  456. const codeBlocks: string[] = [];
  457. let index = 0;
  458. // Temporarily replace code blocks with placeholders and store the blocks separately
  459. text = text.replace(codeBlockRegex, (match) => {
  460. const placeholder = `\u0000${index}\u0000`; // Use a unique placeholder
  461. codeBlocks[index++] = match;
  462. return placeholder;
  463. });
  464. // Split the modified text into paragraphs based on newlines, avoiding these blocks
  465. let paragraphs = text.split(/\n+/);
  466. // Restore code blocks and process paragraphs
  467. paragraphs = paragraphs.map((paragraph) => {
  468. // Check if the paragraph includes a placeholder for a code block
  469. return paragraph.replace(/\u0000(\d+)\u0000/g, (_, idx) => codeBlocks[idx]);
  470. });
  471. return paragraphs.map(cleanText).filter(Boolean);
  472. };
  473. export const extractSentencesForAudio = (text: string) => {
  474. return extractSentences(text).reduce((mergedTexts, currentText) => {
  475. const lastIndex = mergedTexts.length - 1;
  476. if (lastIndex >= 0) {
  477. const previousText = mergedTexts[lastIndex];
  478. const wordCount = previousText.split(/\s+/).length;
  479. const charCount = previousText.length;
  480. if (wordCount < 4 || charCount < 50) {
  481. mergedTexts[lastIndex] = previousText + ' ' + currentText;
  482. } else {
  483. mergedTexts.push(currentText);
  484. }
  485. } else {
  486. mergedTexts.push(currentText);
  487. }
  488. return mergedTexts;
  489. }, [] as string[]);
  490. };
  491. export const getMessageContentParts = (content: string, split_on: string = 'punctuation') => {
  492. const messageContentParts: string[] = [];
  493. switch (split_on) {
  494. default:
  495. case TTS_RESPONSE_SPLIT.PUNCTUATION:
  496. messageContentParts.push(...extractSentencesForAudio(content));
  497. break;
  498. case TTS_RESPONSE_SPLIT.PARAGRAPHS:
  499. messageContentParts.push(...extractParagraphsForAudio(content));
  500. break;
  501. case TTS_RESPONSE_SPLIT.NONE:
  502. messageContentParts.push(cleanText(content));
  503. break;
  504. }
  505. return messageContentParts;
  506. };
  507. export const blobToFile = (blob, fileName) => {
  508. // Create a new File object from the Blob
  509. const file = new File([blob], fileName, { type: blob.type });
  510. return file;
  511. };
  512. /**
  513. * @param {string} template - The template string containing placeholders.
  514. * @returns {string} The template string with the placeholders replaced by the prompt.
  515. */
  516. export const promptTemplate = (
  517. template: string,
  518. user_name?: string,
  519. user_location?: string
  520. ): string => {
  521. // Get the current date
  522. const currentDate = new Date();
  523. // Format the date to YYYY-MM-DD
  524. const formattedDate =
  525. currentDate.getFullYear() +
  526. '-' +
  527. String(currentDate.getMonth() + 1).padStart(2, '0') +
  528. '-' +
  529. String(currentDate.getDate()).padStart(2, '0');
  530. // Format the time to HH:MM:SS AM/PM
  531. const currentTime = currentDate.toLocaleTimeString('en-US', {
  532. hour: 'numeric',
  533. minute: 'numeric',
  534. second: 'numeric',
  535. hour12: true
  536. });
  537. // Get the current weekday
  538. const currentWeekday = getWeekday();
  539. // Get the user's timezone
  540. const currentTimezone = getUserTimezone();
  541. // Get the user's language
  542. const userLanguage = localStorage.getItem('locale') || 'en-US';
  543. // Replace {{CURRENT_DATETIME}} in the template with the formatted datetime
  544. template = template.replace('{{CURRENT_DATETIME}}', `${formattedDate} ${currentTime}`);
  545. // Replace {{CURRENT_DATE}} in the template with the formatted date
  546. template = template.replace('{{CURRENT_DATE}}', formattedDate);
  547. // Replace {{CURRENT_TIME}} in the template with the formatted time
  548. template = template.replace('{{CURRENT_TIME}}', currentTime);
  549. // Replace {{CURRENT_WEEKDAY}} in the template with the current weekday
  550. template = template.replace('{{CURRENT_WEEKDAY}}', currentWeekday);
  551. // Replace {{CURRENT_TIMEZONE}} in the template with the user's timezone
  552. template = template.replace('{{CURRENT_TIMEZONE}}', currentTimezone);
  553. // Replace {{USER_LANGUAGE}} in the template with the user's language
  554. template = template.replace('{{USER_LANGUAGE}}', userLanguage);
  555. if (user_name) {
  556. // Replace {{USER_NAME}} in the template with the user's name
  557. template = template.replace('{{USER_NAME}}', user_name);
  558. }
  559. if (user_location) {
  560. // Replace {{USER_LOCATION}} in the template with the current location
  561. template = template.replace('{{USER_LOCATION}}', user_location);
  562. }
  563. return template;
  564. };
  565. /**
  566. * This function is used to replace placeholders in a template string with the provided prompt.
  567. * The placeholders can be in the following formats:
  568. * - `{{prompt}}`: This will be replaced with the entire prompt.
  569. * - `{{prompt:start:<length>}}`: This will be replaced with the first <length> characters of the prompt.
  570. * - `{{prompt:end:<length>}}`: This will be replaced with the last <length> characters of the prompt.
  571. * - `{{prompt:middletruncate:<length>}}`: This will be replaced with the prompt truncated to <length> characters, with '...' in the middle.
  572. *
  573. * @param {string} template - The template string containing placeholders.
  574. * @param {string} prompt - The string to replace the placeholders with.
  575. * @returns {string} The template string with the placeholders replaced by the prompt.
  576. */
  577. export const titleGenerationTemplate = (template: string, prompt: string): string => {
  578. template = template.replace(
  579. /{{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}/g,
  580. (match, startLength, endLength, middleLength) => {
  581. if (match === '{{prompt}}') {
  582. return prompt;
  583. } else if (match.startsWith('{{prompt:start:')) {
  584. return prompt.substring(0, startLength);
  585. } else if (match.startsWith('{{prompt:end:')) {
  586. return prompt.slice(-endLength);
  587. } else if (match.startsWith('{{prompt:middletruncate:')) {
  588. if (prompt.length <= middleLength) {
  589. return prompt;
  590. }
  591. const start = prompt.slice(0, Math.ceil(middleLength / 2));
  592. const end = prompt.slice(-Math.floor(middleLength / 2));
  593. return `${start}...${end}`;
  594. }
  595. return '';
  596. }
  597. );
  598. template = promptTemplate(template);
  599. return template;
  600. };
  601. export const approximateToHumanReadable = (nanoseconds: number) => {
  602. const seconds = Math.floor((nanoseconds / 1e9) % 60);
  603. const minutes = Math.floor((nanoseconds / 6e10) % 60);
  604. const hours = Math.floor((nanoseconds / 3.6e12) % 24);
  605. const results: string[] = [];
  606. if (seconds >= 0) {
  607. results.push(`${seconds}s`);
  608. }
  609. if (minutes > 0) {
  610. results.push(`${minutes}m`);
  611. }
  612. if (hours > 0) {
  613. results.push(`${hours}h`);
  614. }
  615. return results.reverse().join(' ');
  616. };
  617. export const getTimeRange = (timestamp) => {
  618. const now = new Date();
  619. const date = new Date(timestamp * 1000); // Convert Unix timestamp to milliseconds
  620. // Calculate the difference in milliseconds
  621. const diffTime = now.getTime() - date.getTime();
  622. const diffDays = diffTime / (1000 * 3600 * 24);
  623. const nowDate = now.getDate();
  624. const nowMonth = now.getMonth();
  625. const nowYear = now.getFullYear();
  626. const dateDate = date.getDate();
  627. const dateMonth = date.getMonth();
  628. const dateYear = date.getFullYear();
  629. if (nowYear === dateYear && nowMonth === dateMonth && nowDate === dateDate) {
  630. return 'Today';
  631. } else if (nowYear === dateYear && nowMonth === dateMonth && nowDate - dateDate === 1) {
  632. return 'Yesterday';
  633. } else if (diffDays <= 7) {
  634. return 'Previous 7 days';
  635. } else if (diffDays <= 30) {
  636. return 'Previous 30 days';
  637. } else if (nowYear === dateYear) {
  638. return date.toLocaleString('default', { month: 'long' });
  639. } else {
  640. return date.getFullYear().toString();
  641. }
  642. };
  643. /**
  644. * Extract frontmatter as a dictionary from the specified content string.
  645. * @param content {string} - The content string with potential frontmatter.
  646. * @returns {Object} - The extracted frontmatter as a dictionary.
  647. */
  648. export const extractFrontmatter = (content) => {
  649. const frontmatter = {};
  650. let frontmatterStarted = false;
  651. let frontmatterEnded = false;
  652. const frontmatterPattern = /^\s*([a-z_]+):\s*(.*)\s*$/i;
  653. // Split content into lines
  654. const lines = content.split('\n');
  655. // Check if the content starts with triple quotes
  656. if (lines[0].trim() !== '"""') {
  657. return {};
  658. }
  659. frontmatterStarted = true;
  660. for (let i = 1; i < lines.length; i++) {
  661. const line = lines[i];
  662. if (line.includes('"""')) {
  663. if (frontmatterStarted) {
  664. frontmatterEnded = true;
  665. break;
  666. }
  667. }
  668. if (frontmatterStarted && !frontmatterEnded) {
  669. const match = frontmatterPattern.exec(line);
  670. if (match) {
  671. const [, key, value] = match;
  672. frontmatter[key.trim()] = value.trim();
  673. }
  674. }
  675. }
  676. return frontmatter;
  677. };
  678. // Function to determine the best matching language
  679. export const bestMatchingLanguage = (supportedLanguages, preferredLanguages, defaultLocale) => {
  680. const languages = supportedLanguages.map((lang) => lang.code);
  681. const match = preferredLanguages
  682. .map((prefLang) => languages.find((lang) => lang.startsWith(prefLang)))
  683. .find(Boolean);
  684. return match || defaultLocale;
  685. };
  686. // Get the date in the format YYYY-MM-DD
  687. export const getFormattedDate = () => {
  688. const date = new Date();
  689. return date.toISOString().split('T')[0];
  690. };
  691. // Get the time in the format HH:MM:SS
  692. export const getFormattedTime = () => {
  693. const date = new Date();
  694. return date.toTimeString().split(' ')[0];
  695. };
  696. // Get the current date and time in the format YYYY-MM-DD HH:MM:SS
  697. export const getCurrentDateTime = () => {
  698. return `${getFormattedDate()} ${getFormattedTime()}`;
  699. };
  700. // Get the user's timezone
  701. export const getUserTimezone = () => {
  702. return Intl.DateTimeFormat().resolvedOptions().timeZone;
  703. };
  704. // Get the weekday
  705. export const getWeekday = () => {
  706. const date = new Date();
  707. const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  708. return weekdays[date.getDay()];
  709. };
  710. export const createMessagesList = (history, messageId) => {
  711. if (messageId === null) {
  712. return [];
  713. }
  714. const message = history.messages[messageId];
  715. if (message?.parentId) {
  716. return [...createMessagesList(history, message.parentId), message];
  717. } else {
  718. return [message];
  719. }
  720. };