index.ts 14 KB

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