index.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. import { v4 as uuidv4 } from 'uuid';
  2. import sha256 from 'js-sha256';
  3. //////////////////////////
  4. // Helper functions
  5. //////////////////////////
  6. export const sanitizeResponseContent = (content: string) => {
  7. return content
  8. .replace(/<\|[a-z]*$/, '')
  9. .replace(/<\|[a-z]+\|$/, '')
  10. .replace(/<$/, '')
  11. .replaceAll(/<\|[a-z]+\|>/g, ' ')
  12. .replaceAll('<', '&lt;')
  13. .replaceAll('>', '&gt;')
  14. .trim();
  15. };
  16. export const revertSanitizedResponseContent = (content: string) => {
  17. return content.replaceAll('&lt;', '<').replaceAll('&gt;', '>');
  18. };
  19. export const capitalizeFirstLetter = (string) => {
  20. return string.charAt(0).toUpperCase() + string.slice(1);
  21. };
  22. export const splitStream = (splitOn) => {
  23. let buffer = '';
  24. return new TransformStream({
  25. transform(chunk, controller) {
  26. buffer += chunk;
  27. const parts = buffer.split(splitOn);
  28. parts.slice(0, -1).forEach((part) => controller.enqueue(part));
  29. buffer = parts[parts.length - 1];
  30. },
  31. flush(controller) {
  32. if (buffer) controller.enqueue(buffer);
  33. }
  34. });
  35. };
  36. export const convertMessagesToHistory = (messages) => {
  37. const history = {
  38. messages: {},
  39. currentId: null
  40. };
  41. let parentMessageId = null;
  42. let messageId = null;
  43. for (const message of messages) {
  44. messageId = uuidv4();
  45. if (parentMessageId !== null) {
  46. history.messages[parentMessageId].childrenIds = [
  47. ...history.messages[parentMessageId].childrenIds,
  48. messageId
  49. ];
  50. }
  51. history.messages[messageId] = {
  52. ...message,
  53. id: messageId,
  54. parentId: parentMessageId,
  55. childrenIds: []
  56. };
  57. parentMessageId = messageId;
  58. }
  59. history.currentId = messageId;
  60. return history;
  61. };
  62. export const getGravatarURL = (email) => {
  63. // Trim leading and trailing whitespace from
  64. // an email address and force all characters
  65. // to lower case
  66. const address = String(email).trim().toLowerCase();
  67. // Create a SHA256 hash of the final string
  68. const hash = sha256(address);
  69. // Grab the actual image URL
  70. return `https://www.gravatar.com/avatar/${hash}`;
  71. };
  72. export const canvasPixelTest = () => {
  73. // Test a 1x1 pixel to potentially identify browser/plugin fingerprint blocking or spoofing
  74. // Inspiration: https://github.com/kkapsner/CanvasBlocker/blob/master/test/detectionTest.js
  75. const canvas = document.createElement('canvas');
  76. const ctx = canvas.getContext('2d');
  77. canvas.height = 1;
  78. canvas.width = 1;
  79. const imageData = new ImageData(canvas.width, canvas.height);
  80. const pixelValues = imageData.data;
  81. // Generate RGB test data
  82. for (let i = 0; i < imageData.data.length; i += 1) {
  83. if (i % 4 !== 3) {
  84. pixelValues[i] = Math.floor(256 * Math.random());
  85. } else {
  86. pixelValues[i] = 255;
  87. }
  88. }
  89. ctx.putImageData(imageData, 0, 0);
  90. const p = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
  91. // Read RGB data and fail if unmatched
  92. for (let i = 0; i < p.length; i += 1) {
  93. if (p[i] !== pixelValues[i]) {
  94. console.log(
  95. 'canvasPixelTest: Wrong canvas pixel RGB value detected:',
  96. p[i],
  97. 'at:',
  98. i,
  99. 'expected:',
  100. pixelValues[i]
  101. );
  102. console.log('canvasPixelTest: Canvas blocking or spoofing is likely');
  103. return false;
  104. }
  105. }
  106. return true;
  107. };
  108. export const generateInitialsImage = (name) => {
  109. const canvas = document.createElement('canvas');
  110. const ctx = canvas.getContext('2d');
  111. canvas.width = 100;
  112. canvas.height = 100;
  113. if (!canvasPixelTest()) {
  114. console.log(
  115. 'generateInitialsImage: failed pixel test, fingerprint evasion is likely. Using default image.'
  116. );
  117. return '/user.png';
  118. }
  119. ctx.fillStyle = '#F39C12';
  120. ctx.fillRect(0, 0, canvas.width, canvas.height);
  121. ctx.fillStyle = '#FFFFFF';
  122. ctx.font = '40px Helvetica';
  123. ctx.textAlign = 'center';
  124. ctx.textBaseline = 'middle';
  125. const sanitizedName = name.trim();
  126. const initials =
  127. sanitizedName.length > 0
  128. ? sanitizedName[0] +
  129. (sanitizedName.split(' ').length > 1
  130. ? sanitizedName[sanitizedName.lastIndexOf(' ') + 1]
  131. : '')
  132. : '';
  133. ctx.fillText(initials.toUpperCase(), canvas.width / 2, canvas.height / 2);
  134. return canvas.toDataURL();
  135. };
  136. export const copyToClipboard = async (text) => {
  137. let result = false;
  138. if (!navigator.clipboard) {
  139. const textArea = document.createElement('textarea');
  140. textArea.value = text;
  141. // Avoid scrolling to bottom
  142. textArea.style.top = '0';
  143. textArea.style.left = '0';
  144. textArea.style.position = 'fixed';
  145. document.body.appendChild(textArea);
  146. textArea.focus();
  147. textArea.select();
  148. try {
  149. const successful = document.execCommand('copy');
  150. const msg = successful ? 'successful' : 'unsuccessful';
  151. console.log('Fallback: Copying text command was ' + msg);
  152. result = true;
  153. } catch (err) {
  154. console.error('Fallback: Oops, unable to copy', err);
  155. }
  156. document.body.removeChild(textArea);
  157. return result;
  158. }
  159. result = await navigator.clipboard
  160. .writeText(text)
  161. .then(() => {
  162. console.log('Async: Copying to clipboard was successful!');
  163. return true;
  164. })
  165. .catch((error) => {
  166. console.error('Async: Could not copy text: ', error);
  167. return false;
  168. });
  169. return result;
  170. };
  171. export const compareVersion = (latest, current) => {
  172. return current === '0.0.0'
  173. ? false
  174. : current.localeCompare(latest, undefined, {
  175. numeric: true,
  176. sensitivity: 'case',
  177. caseFirst: 'upper'
  178. }) < 0;
  179. };
  180. export const findWordIndices = (text) => {
  181. const regex = /\[([^\]]+)\]/g;
  182. const matches = [];
  183. let match;
  184. while ((match = regex.exec(text)) !== null) {
  185. matches.push({
  186. word: match[1],
  187. startIndex: match.index,
  188. endIndex: regex.lastIndex - 1
  189. });
  190. }
  191. return matches;
  192. };
  193. export const removeFirstHashWord = (inputString) => {
  194. // Split the string into an array of words
  195. const words = inputString.split(' ');
  196. // Find the index of the first word that starts with #
  197. const index = words.findIndex((word) => word.startsWith('#'));
  198. // Remove the first word with #
  199. if (index !== -1) {
  200. words.splice(index, 1);
  201. }
  202. // Join the remaining words back into a string
  203. const resultString = words.join(' ');
  204. return resultString;
  205. };
  206. export const transformFileName = (fileName) => {
  207. // Convert to lowercase
  208. const lowerCaseFileName = fileName.toLowerCase();
  209. // Remove special characters using regular expression
  210. const sanitizedFileName = lowerCaseFileName.replace(/[^\w\s]/g, '');
  211. // Replace spaces with dashes
  212. const finalFileName = sanitizedFileName.replace(/\s+/g, '-');
  213. return finalFileName;
  214. };
  215. export const calculateSHA256 = async (file) => {
  216. // Create a FileReader to read the file asynchronously
  217. const reader = new FileReader();
  218. // Define a promise to handle the file reading
  219. const readFile = new Promise((resolve, reject) => {
  220. reader.onload = () => resolve(reader.result);
  221. reader.onerror = reject;
  222. });
  223. // Read the file as an ArrayBuffer
  224. reader.readAsArrayBuffer(file);
  225. try {
  226. // Wait for the FileReader to finish reading the file
  227. const buffer = await readFile;
  228. // Convert the ArrayBuffer to a Uint8Array
  229. const uint8Array = new Uint8Array(buffer);
  230. // Calculate the SHA-256 hash using Web Crypto API
  231. const hashBuffer = await crypto.subtle.digest('SHA-256', uint8Array);
  232. // Convert the hash to a hexadecimal string
  233. const hashArray = Array.from(new Uint8Array(hashBuffer));
  234. const hashHex = hashArray.map((byte) => byte.toString(16).padStart(2, '0')).join('');
  235. return `${hashHex}`;
  236. } catch (error) {
  237. console.error('Error calculating SHA-256 hash:', error);
  238. throw error;
  239. }
  240. };
  241. export const getImportOrigin = (_chats) => {
  242. // Check what external service chat imports are from
  243. if ('mapping' in _chats[0]) {
  244. return 'openai';
  245. }
  246. return 'webui';
  247. };
  248. export const getUserPosition = async (raw = false) => {
  249. // Get the user's location using the Geolocation API
  250. const position = await new Promise((resolve, reject) => {
  251. navigator.geolocation.getCurrentPosition(resolve, reject);
  252. }).catch((error) => {
  253. console.error('Error getting user location:', error);
  254. throw error;
  255. });
  256. if (!position) {
  257. return 'Location not available';
  258. }
  259. // Extract the latitude and longitude from the position
  260. const { latitude, longitude } = position.coords;
  261. if (raw) {
  262. return { latitude, longitude };
  263. } else {
  264. return `${latitude.toFixed(3)}, ${longitude.toFixed(3)} (lat, long)`;
  265. }
  266. };
  267. const convertOpenAIMessages = (convo) => {
  268. // Parse OpenAI chat messages and create chat dictionary for creating new chats
  269. const mapping = convo['mapping'];
  270. const messages = [];
  271. let currentId = '';
  272. let lastId = null;
  273. for (let message_id in mapping) {
  274. const message = mapping[message_id];
  275. currentId = message_id;
  276. try {
  277. if (
  278. messages.length == 0 &&
  279. (message['message'] == null ||
  280. (message['message']['content']['parts']?.[0] == '' &&
  281. message['message']['content']['text'] == null))
  282. ) {
  283. // Skip chat messages with no content
  284. continue;
  285. } else {
  286. const new_chat = {
  287. id: message_id,
  288. parentId: lastId,
  289. childrenIds: message['children'] || [],
  290. role: message['message']?.['author']?.['role'] !== 'user' ? 'assistant' : 'user',
  291. content:
  292. message['message']?.['content']?.['parts']?.[0] ||
  293. message['message']?.['content']?.['text'] ||
  294. '',
  295. model: 'gpt-3.5-turbo',
  296. done: true,
  297. context: null
  298. };
  299. messages.push(new_chat);
  300. lastId = currentId;
  301. }
  302. } catch (error) {
  303. console.log('Error with', message, '\nError:', error);
  304. }
  305. }
  306. let history = {};
  307. messages.forEach((obj) => (history[obj.id] = obj));
  308. const chat = {
  309. history: {
  310. currentId: currentId,
  311. messages: history // Need to convert this to not a list and instead a json object
  312. },
  313. models: ['gpt-3.5-turbo'],
  314. messages: messages,
  315. options: {},
  316. timestamp: convo['create_time'],
  317. title: convo['title'] ?? 'New Chat'
  318. };
  319. return chat;
  320. };
  321. const validateChat = (chat) => {
  322. // Because ChatGPT sometimes has features we can't use like DALL-E or migh have corrupted messages, need to validate
  323. const messages = chat.messages;
  324. // Check if messages array is empty
  325. if (messages.length === 0) {
  326. return false;
  327. }
  328. // Last message's children should be an empty array
  329. const lastMessage = messages[messages.length - 1];
  330. if (lastMessage.childrenIds.length !== 0) {
  331. return false;
  332. }
  333. // First message's parent should be null
  334. const firstMessage = messages[0];
  335. if (firstMessage.parentId !== null) {
  336. return false;
  337. }
  338. // Every message's content should be a string
  339. for (let message of messages) {
  340. if (typeof message.content !== 'string') {
  341. return false;
  342. }
  343. }
  344. return true;
  345. };
  346. export const convertOpenAIChats = (_chats) => {
  347. // Create a list of dictionaries with each conversation from import
  348. const chats = [];
  349. let failed = 0;
  350. for (let convo of _chats) {
  351. const chat = convertOpenAIMessages(convo);
  352. if (validateChat(chat)) {
  353. chats.push({
  354. id: convo['id'],
  355. user_id: '',
  356. title: convo['title'],
  357. chat: chat,
  358. timestamp: convo['timestamp']
  359. });
  360. } else {
  361. failed++;
  362. }
  363. }
  364. console.log(failed, 'Conversations could not be imported');
  365. return chats;
  366. };
  367. export const isValidHttpUrl = (string) => {
  368. let url;
  369. try {
  370. url = new URL(string);
  371. } catch (_) {
  372. return false;
  373. }
  374. return url.protocol === 'http:' || url.protocol === 'https:';
  375. };
  376. export const removeEmojis = (str) => {
  377. // Regular expression to match emojis
  378. const emojiRegex = /[\uD800-\uDBFF][\uDC00-\uDFFF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g;
  379. // Replace emojis with an empty string
  380. return str.replace(emojiRegex, '');
  381. };
  382. export const extractSentences = (text) => {
  383. // Split the paragraph into sentences based on common punctuation marks
  384. const sentences = text.split(/(?<=[.!?])\s+/);
  385. return sentences
  386. .map((sentence) => removeEmojis(sentence.trim()))
  387. .filter((sentence) => sentence !== '');
  388. };
  389. export const extractSentencesForAudio = (text) => {
  390. return extractSentences(text).reduce((mergedTexts, currentText) => {
  391. const lastIndex = mergedTexts.length - 1;
  392. if (lastIndex >= 0) {
  393. const previousText = mergedTexts[lastIndex];
  394. const wordCount = previousText.split(/\s+/).length;
  395. if (wordCount < 2) {
  396. mergedTexts[lastIndex] = previousText + ' ' + currentText;
  397. } else {
  398. mergedTexts.push(currentText);
  399. }
  400. } else {
  401. mergedTexts.push(currentText);
  402. }
  403. return mergedTexts;
  404. }, []);
  405. };
  406. export const blobToFile = (blob, fileName) => {
  407. // Create a new File object from the Blob
  408. const file = new File([blob], fileName, { type: blob.type });
  409. return file;
  410. };
  411. /**
  412. * @param {string} template - The template string containing placeholders.
  413. * @returns {string} The template string with the placeholders replaced by the prompt.
  414. */
  415. export const promptTemplate = (
  416. template: string,
  417. user_name?: string,
  418. user_location?: string
  419. ): string => {
  420. // Get the current date
  421. const currentDate = new Date();
  422. // Format the date to YYYY-MM-DD
  423. const formattedDate =
  424. currentDate.getFullYear() +
  425. '-' +
  426. String(currentDate.getMonth() + 1).padStart(2, '0') +
  427. '-' +
  428. String(currentDate.getDate()).padStart(2, '0');
  429. // Format the time to HH:MM:SS AM/PM
  430. const currentTime = currentDate.toLocaleTimeString('en-US', {
  431. hour: 'numeric',
  432. minute: 'numeric',
  433. second: 'numeric',
  434. hour12: true
  435. });
  436. // Replace {{CURRENT_DATETIME}} in the template with the formatted datetime
  437. template = template.replace('{{CURRENT_DATETIME}}', `${formattedDate} ${currentTime}`);
  438. // Replace {{CURRENT_DATE}} in the template with the formatted date
  439. template = template.replace('{{CURRENT_DATE}}', formattedDate);
  440. // Replace {{CURRENT_TIME}} in the template with the formatted time
  441. template = template.replace('{{CURRENT_TIME}}', currentTime);
  442. if (user_name) {
  443. // Replace {{USER_NAME}} in the template with the user's name
  444. template = template.replace('{{USER_NAME}}', user_name);
  445. }
  446. if (user_location) {
  447. // Replace {{USER_LOCATION}} in the template with the current location
  448. template = template.replace('{{USER_LOCATION}}', user_location);
  449. }
  450. return template;
  451. };
  452. /**
  453. * This function is used to replace placeholders in a template string with the provided prompt.
  454. * The placeholders can be in the following formats:
  455. * - `{{prompt}}`: This will be replaced with the entire prompt.
  456. * - `{{prompt:start:<length>}}`: This will be replaced with the first <length> characters of the prompt.
  457. * - `{{prompt:end:<length>}}`: This will be replaced with the last <length> characters of the prompt.
  458. * - `{{prompt:middletruncate:<length>}}`: This will be replaced with the prompt truncated to <length> characters, with '...' in the middle.
  459. *
  460. * @param {string} template - The template string containing placeholders.
  461. * @param {string} prompt - The string to replace the placeholders with.
  462. * @returns {string} The template string with the placeholders replaced by the prompt.
  463. */
  464. export const titleGenerationTemplate = (template: string, prompt: string): string => {
  465. template = template.replace(
  466. /{{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}/g,
  467. (match, startLength, endLength, middleLength) => {
  468. if (match === '{{prompt}}') {
  469. return prompt;
  470. } else if (match.startsWith('{{prompt:start:')) {
  471. return prompt.substring(0, startLength);
  472. } else if (match.startsWith('{{prompt:end:')) {
  473. return prompt.slice(-endLength);
  474. } else if (match.startsWith('{{prompt:middletruncate:')) {
  475. if (prompt.length <= middleLength) {
  476. return prompt;
  477. }
  478. const start = prompt.slice(0, Math.ceil(middleLength / 2));
  479. const end = prompt.slice(-Math.floor(middleLength / 2));
  480. return `${start}...${end}`;
  481. }
  482. return '';
  483. }
  484. );
  485. template = promptTemplate(template);
  486. return template;
  487. };
  488. export const approximateToHumanReadable = (nanoseconds: number) => {
  489. const seconds = Math.floor((nanoseconds / 1e9) % 60);
  490. const minutes = Math.floor((nanoseconds / 6e10) % 60);
  491. const hours = Math.floor((nanoseconds / 3.6e12) % 24);
  492. const results: string[] = [];
  493. if (seconds >= 0) {
  494. results.push(`${seconds}s`);
  495. }
  496. if (minutes > 0) {
  497. results.push(`${minutes}m`);
  498. }
  499. if (hours > 0) {
  500. results.push(`${hours}h`);
  501. }
  502. return results.reverse().join(' ');
  503. };
  504. export const getTimeRange = (timestamp) => {
  505. const now = new Date();
  506. const date = new Date(timestamp * 1000); // Convert Unix timestamp to milliseconds
  507. // Calculate the difference in milliseconds
  508. const diffTime = now.getTime() - date.getTime();
  509. const diffDays = diffTime / (1000 * 3600 * 24);
  510. const nowDate = now.getDate();
  511. const nowMonth = now.getMonth();
  512. const nowYear = now.getFullYear();
  513. const dateDate = date.getDate();
  514. const dateMonth = date.getMonth();
  515. const dateYear = date.getFullYear();
  516. if (nowYear === dateYear && nowMonth === dateMonth && nowDate === dateDate) {
  517. return 'Today';
  518. } else if (nowYear === dateYear && nowMonth === dateMonth && nowDate - dateDate === 1) {
  519. return 'Yesterday';
  520. } else if (diffDays <= 7) {
  521. return 'Previous 7 days';
  522. } else if (diffDays <= 30) {
  523. return 'Previous 30 days';
  524. } else if (nowYear === dateYear) {
  525. return date.toLocaleString('default', { month: 'long' });
  526. } else {
  527. return date.getFullYear().toString();
  528. }
  529. };