index.ts 26 KB

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