index.ts 12 KB

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