index.ts 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. import { v4 as uuidv4 } from 'uuid';
  2. import sha256 from 'js-sha256';
  3. import dayjs from 'dayjs';
  4. import relativeTime from 'dayjs/plugin/relativeTime';
  5. import isToday from 'dayjs/plugin/isToday';
  6. import isYesterday from 'dayjs/plugin/isYesterday';
  7. import localizedFormat from 'dayjs/plugin/localizedFormat';
  8. dayjs.extend(relativeTime);
  9. dayjs.extend(isToday);
  10. dayjs.extend(isYesterday);
  11. dayjs.extend(localizedFormat);
  12. import { WEBUI_BASE_URL } from '$lib/constants';
  13. import { TTS_RESPONSE_SPLIT } from '$lib/types';
  14. import { marked } from 'marked';
  15. import markedExtension from '$lib/utils/marked/extension';
  16. import markedKatexExtension from '$lib/utils/marked/katex-extension';
  17. import hljs from 'highlight.js';
  18. //////////////////////////
  19. // Helper functions
  20. //////////////////////////
  21. export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
  22. function escapeRegExp(string: string): string {
  23. return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  24. }
  25. export const replaceTokens = (content, sourceIds, char, user) => {
  26. const tokens = [
  27. { regex: /{{char}}/gi, replacement: char },
  28. { regex: /{{user}}/gi, replacement: user },
  29. {
  30. regex: /{{VIDEO_FILE_ID_([a-f0-9-]+)}}/gi,
  31. replacement: (_, fileId) =>
  32. `<video src="${WEBUI_BASE_URL}/api/v1/files/${fileId}/content" controls></video>`
  33. },
  34. {
  35. regex: /{{HTML_FILE_ID_([a-f0-9-]+)}}/gi,
  36. replacement: (_, fileId) =>
  37. `<iframe src="${WEBUI_BASE_URL}/api/v1/files/${fileId}/content/html" width="100%" frameborder="0" onload="this.style.height=(this.contentWindow.document.body.scrollHeight+20)+'px';"></iframe>`
  38. }
  39. ];
  40. // Replace tokens outside code blocks only
  41. const processOutsideCodeBlocks = (text, replacementFn) => {
  42. return text
  43. .split(/(```[\s\S]*?```|`[\s\S]*?`)/)
  44. .map((segment) => {
  45. return segment.startsWith('```') || segment.startsWith('`')
  46. ? segment
  47. : replacementFn(segment);
  48. })
  49. .join('');
  50. };
  51. // Apply replacements
  52. content = processOutsideCodeBlocks(content, (segment) => {
  53. tokens.forEach(({ regex, replacement }) => {
  54. if (replacement !== undefined && replacement !== null) {
  55. segment = segment.replace(regex, replacement);
  56. }
  57. });
  58. if (Array.isArray(sourceIds)) {
  59. sourceIds.forEach((sourceId, idx) => {
  60. const regex = new RegExp(`\\[${idx + 1}\\]`, 'g');
  61. segment = segment.replace(regex, `<source_id data="${idx + 1}" title="${sourceId}" />`);
  62. });
  63. }
  64. return segment;
  65. });
  66. return content;
  67. };
  68. export const sanitizeResponseContent = (content: string) => {
  69. return content
  70. .replace(/<\|[a-z]*$/, '')
  71. .replace(/<\|[a-z]+\|$/, '')
  72. .replace(/<$/, '')
  73. .replaceAll(/<\|[a-z]+\|>/g, ' ')
  74. .replaceAll('<', '&lt;')
  75. .replaceAll('>', '&gt;')
  76. .trim();
  77. };
  78. export const processResponseContent = (content: string) => {
  79. return content.trim();
  80. };
  81. export function unescapeHtml(html: string) {
  82. const doc = new DOMParser().parseFromString(html, 'text/html');
  83. return doc.documentElement.textContent;
  84. }
  85. export const capitalizeFirstLetter = (string) => {
  86. return string.charAt(0).toUpperCase() + string.slice(1);
  87. };
  88. export const splitStream = (splitOn) => {
  89. let buffer = '';
  90. return new TransformStream({
  91. transform(chunk, controller) {
  92. buffer += chunk;
  93. const parts = buffer.split(splitOn);
  94. parts.slice(0, -1).forEach((part) => controller.enqueue(part));
  95. buffer = parts[parts.length - 1];
  96. },
  97. flush(controller) {
  98. if (buffer) controller.enqueue(buffer);
  99. }
  100. });
  101. };
  102. export const convertMessagesToHistory = (messages) => {
  103. const history = {
  104. messages: {},
  105. currentId: null
  106. };
  107. let parentMessageId = null;
  108. let messageId = null;
  109. for (const message of messages) {
  110. messageId = uuidv4();
  111. if (parentMessageId !== null) {
  112. history.messages[parentMessageId].childrenIds = [
  113. ...history.messages[parentMessageId].childrenIds,
  114. messageId
  115. ];
  116. }
  117. history.messages[messageId] = {
  118. ...message,
  119. id: messageId,
  120. parentId: parentMessageId,
  121. childrenIds: []
  122. };
  123. parentMessageId = messageId;
  124. }
  125. history.currentId = messageId;
  126. return history;
  127. };
  128. export const getGravatarURL = (email) => {
  129. // Trim leading and trailing whitespace from
  130. // an email address and force all characters
  131. // to lower case
  132. const address = String(email).trim().toLowerCase();
  133. // Create a SHA256 hash of the final string
  134. const hash = sha256(address);
  135. // Grab the actual image URL
  136. return `https://www.gravatar.com/avatar/${hash}`;
  137. };
  138. export const canvasPixelTest = () => {
  139. // Test a 1x1 pixel to potentially identify browser/plugin fingerprint blocking or spoofing
  140. // Inspiration: https://github.com/kkapsner/CanvasBlocker/blob/master/test/detectionTest.js
  141. const canvas = document.createElement('canvas');
  142. const ctx = canvas.getContext('2d');
  143. canvas.height = 1;
  144. canvas.width = 1;
  145. const imageData = new ImageData(canvas.width, canvas.height);
  146. const pixelValues = imageData.data;
  147. // Generate RGB test data
  148. for (let i = 0; i < imageData.data.length; i += 1) {
  149. if (i % 4 !== 3) {
  150. pixelValues[i] = Math.floor(256 * Math.random());
  151. } else {
  152. pixelValues[i] = 255;
  153. }
  154. }
  155. ctx.putImageData(imageData, 0, 0);
  156. const p = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
  157. // Read RGB data and fail if unmatched
  158. for (let i = 0; i < p.length; i += 1) {
  159. if (p[i] !== pixelValues[i]) {
  160. console.log(
  161. 'canvasPixelTest: Wrong canvas pixel RGB value detected:',
  162. p[i],
  163. 'at:',
  164. i,
  165. 'expected:',
  166. pixelValues[i]
  167. );
  168. console.log('canvasPixelTest: Canvas blocking or spoofing is likely');
  169. return false;
  170. }
  171. }
  172. return true;
  173. };
  174. export const compressImage = async (imageUrl, maxWidth, maxHeight) => {
  175. return new Promise((resolve, reject) => {
  176. const img = new Image();
  177. img.onload = () => {
  178. const canvas = document.createElement('canvas');
  179. let width = img.width;
  180. let height = img.height;
  181. // Maintain aspect ratio while resizing
  182. if (maxWidth && maxHeight) {
  183. // Resize with both dimensions defined (preserves aspect ratio)
  184. if (width <= maxWidth && height <= maxHeight) {
  185. resolve(imageUrl);
  186. return;
  187. }
  188. if (width / height > maxWidth / maxHeight) {
  189. height = Math.round((maxWidth * height) / width);
  190. width = maxWidth;
  191. } else {
  192. width = Math.round((maxHeight * width) / height);
  193. height = maxHeight;
  194. }
  195. } else if (maxWidth) {
  196. // Only maxWidth defined
  197. if (width <= maxWidth) {
  198. resolve(imageUrl);
  199. return;
  200. }
  201. height = Math.round((maxWidth * height) / width);
  202. width = maxWidth;
  203. } else if (maxHeight) {
  204. // Only maxHeight defined
  205. if (height <= maxHeight) {
  206. resolve(imageUrl);
  207. return;
  208. }
  209. width = Math.round((maxHeight * width) / height);
  210. height = maxHeight;
  211. }
  212. canvas.width = width;
  213. canvas.height = height;
  214. const context = canvas.getContext('2d');
  215. context.drawImage(img, 0, 0, width, height);
  216. // Get compressed image URL
  217. const compressedUrl = canvas.toDataURL();
  218. resolve(compressedUrl);
  219. };
  220. img.onerror = (error) => reject(error);
  221. img.src = imageUrl;
  222. });
  223. };
  224. export const generateInitialsImage = (name) => {
  225. const canvas = document.createElement('canvas');
  226. const ctx = canvas.getContext('2d');
  227. canvas.width = 100;
  228. canvas.height = 100;
  229. if (!canvasPixelTest()) {
  230. console.log(
  231. 'generateInitialsImage: failed pixel test, fingerprint evasion is likely. Using default image.'
  232. );
  233. return '/user.png';
  234. }
  235. ctx.fillStyle = '#F39C12';
  236. ctx.fillRect(0, 0, canvas.width, canvas.height);
  237. ctx.fillStyle = '#FFFFFF';
  238. ctx.font = '40px Helvetica';
  239. ctx.textAlign = 'center';
  240. ctx.textBaseline = 'middle';
  241. const sanitizedName = name.trim();
  242. const initials =
  243. sanitizedName.length > 0
  244. ? sanitizedName[0] +
  245. (sanitizedName.split(' ').length > 1
  246. ? sanitizedName[sanitizedName.lastIndexOf(' ') + 1]
  247. : '')
  248. : '';
  249. ctx.fillText(initials.toUpperCase(), canvas.width / 2, canvas.height / 2);
  250. return canvas.toDataURL();
  251. };
  252. export const formatDate = (inputDate) => {
  253. const date = dayjs(inputDate);
  254. const now = dayjs();
  255. if (date.isToday()) {
  256. return `Today at ${date.format('LT')}`;
  257. } else if (date.isYesterday()) {
  258. return `Yesterday at ${date.format('LT')}`;
  259. } else {
  260. return `${date.format('L')} at ${date.format('LT')}`;
  261. }
  262. };
  263. export const copyToClipboard = async (text, formatted = false) => {
  264. if (formatted) {
  265. const options = {
  266. throwOnError: false,
  267. highlight: function (code, lang) {
  268. const language = hljs.getLanguage(lang) ? lang : 'plaintext';
  269. return hljs.highlight(code, { language }).value;
  270. }
  271. };
  272. marked.use(markedKatexExtension(options));
  273. marked.use(markedExtension(options));
  274. const htmlContent = marked.parse(text);
  275. // Add basic styling to make the content look better when pasted
  276. const styledHtml = `
  277. <div>
  278. <style>
  279. pre {
  280. background-color: #f6f8fa;
  281. border-radius: 6px;
  282. padding: 16px;
  283. overflow: auto;
  284. }
  285. code {
  286. font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  287. font-size: 14px;
  288. }
  289. .hljs-keyword { color: #d73a49; }
  290. .hljs-string { color: #032f62; }
  291. .hljs-comment { color: #6a737d; }
  292. .hljs-function { color: #6f42c1; }
  293. .hljs-number { color: #005cc5; }
  294. .hljs-operator { color: #d73a49; }
  295. .hljs-class { color: #6f42c1; }
  296. .hljs-title { color: #6f42c1; }
  297. .hljs-params { color: #24292e; }
  298. .hljs-built_in { color: #005cc5; }
  299. blockquote {
  300. border-left: 4px solid #dfe2e5;
  301. padding-left: 16px;
  302. color: #6a737d;
  303. margin-left: 0;
  304. margin-right: 0;
  305. }
  306. table {
  307. border-collapse: collapse;
  308. width: 100%;
  309. margin-bottom: 16px;
  310. }
  311. table, th, td {
  312. border: 1px solid #dfe2e5;
  313. }
  314. th, td {
  315. padding: 8px 12px;
  316. }
  317. th {
  318. background-color: #f6f8fa;
  319. }
  320. </style>
  321. ${htmlContent}
  322. </div>
  323. `;
  324. // Create a blob with HTML content
  325. const blob = new Blob([styledHtml], { type: 'text/html' });
  326. try {
  327. // Create a ClipboardItem with HTML content
  328. const data = new ClipboardItem({
  329. 'text/html': blob,
  330. 'text/plain': new Blob([text], { type: 'text/plain' })
  331. });
  332. // Write to clipboard
  333. await navigator.clipboard.write([data]);
  334. return true;
  335. } catch (err) {
  336. console.error('Error copying formatted content:', err);
  337. // Fallback to plain text
  338. return await copyToClipboard(text);
  339. }
  340. } else {
  341. let result = false;
  342. if (!navigator.clipboard) {
  343. const textArea = document.createElement('textarea');
  344. textArea.value = text;
  345. // Avoid scrolling to bottom
  346. textArea.style.top = '0';
  347. textArea.style.left = '0';
  348. textArea.style.position = 'fixed';
  349. document.body.appendChild(textArea);
  350. textArea.focus();
  351. textArea.select();
  352. try {
  353. const successful = document.execCommand('copy');
  354. const msg = successful ? 'successful' : 'unsuccessful';
  355. console.log('Fallback: Copying text command was ' + msg);
  356. result = true;
  357. } catch (err) {
  358. console.error('Fallback: Oops, unable to copy', err);
  359. }
  360. document.body.removeChild(textArea);
  361. return result;
  362. }
  363. result = await navigator.clipboard
  364. .writeText(text)
  365. .then(() => {
  366. console.log('Async: Copying to clipboard was successful!');
  367. return true;
  368. })
  369. .catch((error) => {
  370. console.error('Async: Could not copy text: ', error);
  371. return false;
  372. });
  373. return result;
  374. }
  375. };
  376. export const compareVersion = (latest, current) => {
  377. return current === '0.0.0'
  378. ? false
  379. : current.localeCompare(latest, undefined, {
  380. numeric: true,
  381. sensitivity: 'case',
  382. caseFirst: 'upper'
  383. }) < 0;
  384. };
  385. export const extractCurlyBraceWords = (text) => {
  386. const regex = /\{\{([^}]+)\}\}/g;
  387. const matches = [];
  388. let match;
  389. while ((match = regex.exec(text)) !== null) {
  390. matches.push({
  391. word: match[1].trim(),
  392. startIndex: match.index,
  393. endIndex: regex.lastIndex - 1
  394. });
  395. }
  396. return matches;
  397. };
  398. export const removeLastWordFromString = (inputString, wordString) => {
  399. console.log('inputString', inputString);
  400. // Split the string by newline characters to handle lines separately
  401. const lines = inputString.split('\n');
  402. // Take the last line to operate only on it
  403. const lastLine = lines.pop();
  404. // Split the last line into an array of words
  405. const words = lastLine.split(' ');
  406. // Conditional to check for the last word removal
  407. if (words.at(-1) === wordString || (wordString === '' && words.at(-1) === '\\#')) {
  408. words.pop(); // Remove last word if condition is satisfied
  409. }
  410. // Join the remaining words back into a string and handle space correctly
  411. let updatedLastLine = words.join(' ');
  412. // Add a trailing space to the updated last line if there are still words
  413. if (updatedLastLine !== '') {
  414. updatedLastLine += ' ';
  415. }
  416. // Combine the lines together again, placing the updated last line back in
  417. const resultString = [...lines, updatedLastLine].join('\n');
  418. // Return the final string
  419. console.log('resultString', resultString);
  420. return resultString;
  421. };
  422. export const removeFirstHashWord = (inputString) => {
  423. // Split the string into an array of words
  424. const words = inputString.split(' ');
  425. // Find the index of the first word that starts with #
  426. const index = words.findIndex((word) => word.startsWith('#'));
  427. // Remove the first word with #
  428. if (index !== -1) {
  429. words.splice(index, 1);
  430. }
  431. // Join the remaining words back into a string
  432. const resultString = words.join(' ');
  433. return resultString;
  434. };
  435. export const transformFileName = (fileName) => {
  436. // Convert to lowercase
  437. const lowerCaseFileName = fileName.toLowerCase();
  438. // Remove special characters using regular expression
  439. const sanitizedFileName = lowerCaseFileName.replace(/[^\w\s]/g, '');
  440. // Replace spaces with dashes
  441. const finalFileName = sanitizedFileName.replace(/\s+/g, '-');
  442. return finalFileName;
  443. };
  444. export const calculateSHA256 = async (file) => {
  445. // Create a FileReader to read the file asynchronously
  446. const reader = new FileReader();
  447. // Define a promise to handle the file reading
  448. const readFile = new Promise((resolve, reject) => {
  449. reader.onload = () => resolve(reader.result);
  450. reader.onerror = reject;
  451. });
  452. // Read the file as an ArrayBuffer
  453. reader.readAsArrayBuffer(file);
  454. try {
  455. // Wait for the FileReader to finish reading the file
  456. const buffer = await readFile;
  457. // Convert the ArrayBuffer to a Uint8Array
  458. const uint8Array = new Uint8Array(buffer);
  459. // Calculate the SHA-256 hash using Web Crypto API
  460. const hashBuffer = await crypto.subtle.digest('SHA-256', uint8Array);
  461. // Convert the hash to a hexadecimal string
  462. const hashArray = Array.from(new Uint8Array(hashBuffer));
  463. const hashHex = hashArray.map((byte) => byte.toString(16).padStart(2, '0')).join('');
  464. return `${hashHex}`;
  465. } catch (error) {
  466. console.error('Error calculating SHA-256 hash:', error);
  467. throw error;
  468. }
  469. };
  470. export const getImportOrigin = (_chats) => {
  471. // Check what external service chat imports are from
  472. if ('mapping' in _chats[0]) {
  473. return 'openai';
  474. }
  475. return 'webui';
  476. };
  477. export const getUserPosition = async (raw = false) => {
  478. // Get the user's location using the Geolocation API
  479. const position = await new Promise((resolve, reject) => {
  480. navigator.geolocation.getCurrentPosition(resolve, reject);
  481. }).catch((error) => {
  482. console.error('Error getting user location:', error);
  483. throw error;
  484. });
  485. if (!position) {
  486. return 'Location not available';
  487. }
  488. // Extract the latitude and longitude from the position
  489. const { latitude, longitude } = position.coords;
  490. if (raw) {
  491. return { latitude, longitude };
  492. } else {
  493. return `${latitude.toFixed(3)}, ${longitude.toFixed(3)} (lat, long)`;
  494. }
  495. };
  496. const convertOpenAIMessages = (convo) => {
  497. // Parse OpenAI chat messages and create chat dictionary for creating new chats
  498. const mapping = convo['mapping'];
  499. const messages = [];
  500. let currentId = '';
  501. let lastId = null;
  502. for (const message_id in mapping) {
  503. const message = mapping[message_id];
  504. currentId = message_id;
  505. try {
  506. if (
  507. messages.length == 0 &&
  508. (message['message'] == null ||
  509. (message['message']['content']['parts']?.[0] == '' &&
  510. message['message']['content']['text'] == null))
  511. ) {
  512. // Skip chat messages with no content
  513. continue;
  514. } else {
  515. const new_chat = {
  516. id: message_id,
  517. parentId: lastId,
  518. childrenIds: message['children'] || [],
  519. role: message['message']?.['author']?.['role'] !== 'user' ? 'assistant' : 'user',
  520. content:
  521. message['message']?.['content']?.['parts']?.[0] ||
  522. message['message']?.['content']?.['text'] ||
  523. '',
  524. model: 'gpt-3.5-turbo',
  525. done: true,
  526. context: null
  527. };
  528. messages.push(new_chat);
  529. lastId = currentId;
  530. }
  531. } catch (error) {
  532. console.log('Error with', message, '\nError:', error);
  533. }
  534. }
  535. const history: Record<PropertyKey, (typeof messages)[number]> = {};
  536. messages.forEach((obj) => (history[obj.id] = obj));
  537. const chat = {
  538. history: {
  539. currentId: currentId,
  540. messages: history // Need to convert this to not a list and instead a json object
  541. },
  542. models: ['gpt-3.5-turbo'],
  543. messages: messages,
  544. options: {},
  545. timestamp: convo['create_time'],
  546. title: convo['title'] ?? 'New Chat'
  547. };
  548. return chat;
  549. };
  550. const validateChat = (chat) => {
  551. // Because ChatGPT sometimes has features we can't use like DALL-E or might have corrupted messages, need to validate
  552. const messages = chat.messages;
  553. // Check if messages array is empty
  554. if (messages.length === 0) {
  555. return false;
  556. }
  557. // Last message's children should be an empty array
  558. const lastMessage = messages[messages.length - 1];
  559. if (lastMessage.childrenIds.length !== 0) {
  560. return false;
  561. }
  562. // First message's parent should be null
  563. const firstMessage = messages[0];
  564. if (firstMessage.parentId !== null) {
  565. return false;
  566. }
  567. // Every message's content should be a string
  568. for (const message of messages) {
  569. if (typeof message.content !== 'string') {
  570. return false;
  571. }
  572. }
  573. return true;
  574. };
  575. export const convertOpenAIChats = (_chats) => {
  576. // Create a list of dictionaries with each conversation from import
  577. const chats = [];
  578. let failed = 0;
  579. for (const convo of _chats) {
  580. const chat = convertOpenAIMessages(convo);
  581. if (validateChat(chat)) {
  582. chats.push({
  583. id: convo['id'],
  584. user_id: '',
  585. title: convo['title'],
  586. chat: chat,
  587. timestamp: convo['create_time']
  588. });
  589. } else {
  590. failed++;
  591. }
  592. }
  593. console.log(failed, 'Conversations could not be imported');
  594. return chats;
  595. };
  596. export const isValidHttpUrl = (string: string) => {
  597. let url;
  598. try {
  599. url = new URL(string);
  600. } catch (_) {
  601. return false;
  602. }
  603. return url.protocol === 'http:' || url.protocol === 'https:';
  604. };
  605. export const removeEmojis = (str: string) => {
  606. // Regular expression to match emojis
  607. const emojiRegex = /[\uD800-\uDBFF][\uDC00-\uDFFF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g;
  608. // Replace emojis with an empty string
  609. return str.replace(emojiRegex, '');
  610. };
  611. export const removeFormattings = (str: string) => {
  612. return (
  613. str
  614. // Block elements (remove completely)
  615. .replace(/(```[\s\S]*?```)/g, '') // Code blocks
  616. .replace(/^\|.*\|$/gm, '') // Tables
  617. // Inline elements (preserve content)
  618. .replace(/(?:\*\*|__)(.*?)(?:\*\*|__)/g, '$1') // Bold
  619. .replace(/(?:[*_])(.*?)(?:[*_])/g, '$1') // Italic
  620. .replace(/~~(.*?)~~/g, '$1') // Strikethrough
  621. .replace(/`([^`]+)`/g, '$1') // Inline code
  622. // Links and images
  623. .replace(/!?\[([^\]]*)\](?:\([^)]+\)|\[[^\]]*\])/g, '$1') // Links & images
  624. .replace(/^\[[^\]]+\]:\s*.*$/gm, '') // Reference definitions
  625. // Block formatting
  626. .replace(/^#{1,6}\s+/gm, '') // Headers
  627. .replace(/^\s*[-*+]\s+/gm, '') // Lists
  628. .replace(/^\s*(?:\d+\.)\s+/gm, '') // Numbered lists
  629. .replace(/^\s*>[> ]*/gm, '') // Blockquotes
  630. .replace(/^\s*:\s+/gm, '') // Definition lists
  631. // Cleanup
  632. .replace(/\[\^[^\]]*\]/g, '') // Footnotes
  633. .replace(/[-*_~]/g, '') // Remaining markers
  634. .replace(/\n{2,}/g, '\n')
  635. ); // Multiple newlines
  636. };
  637. export const cleanText = (content: string) => {
  638. return removeFormattings(removeEmojis(content.trim()));
  639. };
  640. export const removeDetails = (content, types) => {
  641. for (const type of types) {
  642. content = content.replace(
  643. new RegExp(`<details\\s+type="${type}"[^>]*>.*?<\\/details>`, 'gis'),
  644. ''
  645. );
  646. }
  647. return content;
  648. };
  649. export const removeAllDetails = (content) => {
  650. content = content.replace(/<details[^>]*>.*?<\/details>/gis, '');
  651. return content;
  652. };
  653. export const processDetails = (content) => {
  654. content = removeDetails(content, ['reasoning', 'code_interpreter']);
  655. // This regex matches <details> tags with type="tool_calls" and captures their attributes to convert them to <tool_calls> tags
  656. const detailsRegex = /<details\s+type="tool_calls"([^>]*)>([\s\S]*?)<\/details>/gis;
  657. const matches = content.match(detailsRegex);
  658. if (matches) {
  659. for (const match of matches) {
  660. const attributesRegex = /(\w+)="([^"]*)"/g;
  661. const attributes = {};
  662. let attributeMatch;
  663. while ((attributeMatch = attributesRegex.exec(match)) !== null) {
  664. attributes[attributeMatch[1]] = attributeMatch[2];
  665. }
  666. content = content.replace(
  667. match,
  668. `<tool_calls name="${attributes.name}" result="${attributes.result}"/>`
  669. );
  670. }
  671. }
  672. return content;
  673. };
  674. // This regular expression matches code blocks marked by triple backticks
  675. const codeBlockRegex = /```[\s\S]*?```/g;
  676. export const extractSentences = (text: string) => {
  677. const codeBlocks: string[] = [];
  678. let index = 0;
  679. // Temporarily replace code blocks with placeholders and store the blocks separately
  680. text = text.replace(codeBlockRegex, (match) => {
  681. const placeholder = `\u0000${index}\u0000`; // Use a unique placeholder
  682. codeBlocks[index++] = match;
  683. return placeholder;
  684. });
  685. // Split the modified text into sentences based on common punctuation marks, avoiding these blocks
  686. let sentences = text.split(/(?<=[.!?])\s+/);
  687. // Restore code blocks and process sentences
  688. sentences = sentences.map((sentence) => {
  689. // Check if the sentence includes a placeholder for a code block
  690. return sentence.replace(/\u0000(\d+)\u0000/g, (_, idx) => codeBlocks[idx]);
  691. });
  692. return sentences.map(cleanText).filter(Boolean);
  693. };
  694. export const extractParagraphsForAudio = (text: string) => {
  695. const codeBlocks: string[] = [];
  696. let index = 0;
  697. // Temporarily replace code blocks with placeholders and store the blocks separately
  698. text = text.replace(codeBlockRegex, (match) => {
  699. const placeholder = `\u0000${index}\u0000`; // Use a unique placeholder
  700. codeBlocks[index++] = match;
  701. return placeholder;
  702. });
  703. // Split the modified text into paragraphs based on newlines, avoiding these blocks
  704. let paragraphs = text.split(/\n+/);
  705. // Restore code blocks and process paragraphs
  706. paragraphs = paragraphs.map((paragraph) => {
  707. // Check if the paragraph includes a placeholder for a code block
  708. return paragraph.replace(/\u0000(\d+)\u0000/g, (_, idx) => codeBlocks[idx]);
  709. });
  710. return paragraphs.map(cleanText).filter(Boolean);
  711. };
  712. export const extractSentencesForAudio = (text: string) => {
  713. return extractSentences(text).reduce((mergedTexts, currentText) => {
  714. const lastIndex = mergedTexts.length - 1;
  715. if (lastIndex >= 0) {
  716. const previousText = mergedTexts[lastIndex];
  717. const wordCount = previousText.split(/\s+/).length;
  718. const charCount = previousText.length;
  719. if (wordCount < 4 || charCount < 50) {
  720. mergedTexts[lastIndex] = previousText + ' ' + currentText;
  721. } else {
  722. mergedTexts.push(currentText);
  723. }
  724. } else {
  725. mergedTexts.push(currentText);
  726. }
  727. return mergedTexts;
  728. }, [] as string[]);
  729. };
  730. export const getMessageContentParts = (content: string, split_on: string = 'punctuation') => {
  731. content = removeDetails(content, ['reasoning', 'code_interpreter', 'tool_calls']);
  732. const messageContentParts: string[] = [];
  733. switch (split_on) {
  734. default:
  735. case TTS_RESPONSE_SPLIT.PUNCTUATION:
  736. messageContentParts.push(...extractSentencesForAudio(content));
  737. break;
  738. case TTS_RESPONSE_SPLIT.PARAGRAPHS:
  739. messageContentParts.push(...extractParagraphsForAudio(content));
  740. break;
  741. case TTS_RESPONSE_SPLIT.NONE:
  742. messageContentParts.push(cleanText(content));
  743. break;
  744. }
  745. return messageContentParts;
  746. };
  747. export const blobToFile = (blob, fileName) => {
  748. // Create a new File object from the Blob
  749. const file = new File([blob], fileName, { type: blob.type });
  750. return file;
  751. };
  752. export const getPromptVariables = (user_name, user_location) => {
  753. return {
  754. '{{USER_NAME}}': user_name,
  755. '{{USER_LOCATION}}': user_location || 'Unknown',
  756. '{{CURRENT_DATETIME}}': getCurrentDateTime(),
  757. '{{CURRENT_DATE}}': getFormattedDate(),
  758. '{{CURRENT_TIME}}': getFormattedTime(),
  759. '{{CURRENT_WEEKDAY}}': getWeekday(),
  760. '{{CURRENT_TIMEZONE}}': getUserTimezone(),
  761. '{{USER_LANGUAGE}}': localStorage.getItem('locale') || 'en-US'
  762. };
  763. };
  764. /**
  765. * @param {string} template - The template string containing placeholders.
  766. * @returns {string} The template string with the placeholders replaced by the prompt.
  767. */
  768. export const promptTemplate = (
  769. template: string,
  770. user_name?: string,
  771. user_location?: string
  772. ): string => {
  773. // Get the current date
  774. const currentDate = new Date();
  775. // Format the date to YYYY-MM-DD
  776. const formattedDate =
  777. currentDate.getFullYear() +
  778. '-' +
  779. String(currentDate.getMonth() + 1).padStart(2, '0') +
  780. '-' +
  781. String(currentDate.getDate()).padStart(2, '0');
  782. // Format the time to HH:MM:SS AM/PM
  783. const currentTime = currentDate.toLocaleTimeString('en-US', {
  784. hour: 'numeric',
  785. minute: 'numeric',
  786. second: 'numeric',
  787. hour12: true
  788. });
  789. // Get the current weekday
  790. const currentWeekday = getWeekday();
  791. // Get the user's timezone
  792. const currentTimezone = getUserTimezone();
  793. // Get the user's language
  794. const userLanguage = localStorage.getItem('locale') || 'en-US';
  795. // Replace {{CURRENT_DATETIME}} in the template with the formatted datetime
  796. template = template.replace('{{CURRENT_DATETIME}}', `${formattedDate} ${currentTime}`);
  797. // Replace {{CURRENT_DATE}} in the template with the formatted date
  798. template = template.replace('{{CURRENT_DATE}}', formattedDate);
  799. // Replace {{CURRENT_TIME}} in the template with the formatted time
  800. template = template.replace('{{CURRENT_TIME}}', currentTime);
  801. // Replace {{CURRENT_WEEKDAY}} in the template with the current weekday
  802. template = template.replace('{{CURRENT_WEEKDAY}}', currentWeekday);
  803. // Replace {{CURRENT_TIMEZONE}} in the template with the user's timezone
  804. template = template.replace('{{CURRENT_TIMEZONE}}', currentTimezone);
  805. // Replace {{USER_LANGUAGE}} in the template with the user's language
  806. template = template.replace('{{USER_LANGUAGE}}', userLanguage);
  807. if (user_name) {
  808. // Replace {{USER_NAME}} in the template with the user's name
  809. template = template.replace('{{USER_NAME}}', user_name);
  810. }
  811. if (user_location) {
  812. // Replace {{USER_LOCATION}} in the template with the current location
  813. template = template.replace('{{USER_LOCATION}}', user_location);
  814. } else {
  815. // Replace {{USER_LOCATION}} in the template with 'Unknown' if no location is provided
  816. template = template.replace('{{USER_LOCATION}}', 'LOCATION_UNKNOWN');
  817. }
  818. return template;
  819. };
  820. /**
  821. * This function is used to replace placeholders in a template string with the provided prompt.
  822. * The placeholders can be in the following formats:
  823. * - `{{prompt}}`: This will be replaced with the entire prompt.
  824. * - `{{prompt:start:<length>}}`: This will be replaced with the first <length> characters of the prompt.
  825. * - `{{prompt:end:<length>}}`: This will be replaced with the last <length> characters of the prompt.
  826. * - `{{prompt:middletruncate:<length>}}`: This will be replaced with the prompt truncated to <length> characters, with '...' in the middle.
  827. *
  828. * @param {string} template - The template string containing placeholders.
  829. * @param {string} prompt - The string to replace the placeholders with.
  830. * @returns {string} The template string with the placeholders replaced by the prompt.
  831. */
  832. export const titleGenerationTemplate = (template: string, prompt: string): string => {
  833. template = template.replace(
  834. /{{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}/g,
  835. (match, startLength, endLength, middleLength) => {
  836. if (match === '{{prompt}}') {
  837. return prompt;
  838. } else if (match.startsWith('{{prompt:start:')) {
  839. return prompt.substring(0, startLength);
  840. } else if (match.startsWith('{{prompt:end:')) {
  841. return prompt.slice(-endLength);
  842. } else if (match.startsWith('{{prompt:middletruncate:')) {
  843. if (prompt.length <= middleLength) {
  844. return prompt;
  845. }
  846. const start = prompt.slice(0, Math.ceil(middleLength / 2));
  847. const end = prompt.slice(-Math.floor(middleLength / 2));
  848. return `${start}...${end}`;
  849. }
  850. return '';
  851. }
  852. );
  853. template = promptTemplate(template);
  854. return template;
  855. };
  856. export const approximateToHumanReadable = (nanoseconds: number) => {
  857. const seconds = Math.floor((nanoseconds / 1e9) % 60);
  858. const minutes = Math.floor((nanoseconds / 6e10) % 60);
  859. const hours = Math.floor((nanoseconds / 3.6e12) % 24);
  860. const results: string[] = [];
  861. if (seconds >= 0) {
  862. results.push(`${seconds}s`);
  863. }
  864. if (minutes > 0) {
  865. results.push(`${minutes}m`);
  866. }
  867. if (hours > 0) {
  868. results.push(`${hours}h`);
  869. }
  870. return results.reverse().join(' ');
  871. };
  872. export const getTimeRange = (timestamp) => {
  873. const now = new Date();
  874. const date = new Date(timestamp * 1000); // Convert Unix timestamp to milliseconds
  875. // Calculate the difference in milliseconds
  876. const diffTime = now.getTime() - date.getTime();
  877. const diffDays = diffTime / (1000 * 3600 * 24);
  878. const nowDate = now.getDate();
  879. const nowMonth = now.getMonth();
  880. const nowYear = now.getFullYear();
  881. const dateDate = date.getDate();
  882. const dateMonth = date.getMonth();
  883. const dateYear = date.getFullYear();
  884. if (nowYear === dateYear && nowMonth === dateMonth && nowDate === dateDate) {
  885. return 'Today';
  886. } else if (nowYear === dateYear && nowMonth === dateMonth && nowDate - dateDate === 1) {
  887. return 'Yesterday';
  888. } else if (diffDays <= 7) {
  889. return 'Previous 7 days';
  890. } else if (diffDays <= 30) {
  891. return 'Previous 30 days';
  892. } else if (nowYear === dateYear) {
  893. return date.toLocaleString('default', { month: 'long' });
  894. } else {
  895. return date.getFullYear().toString();
  896. }
  897. };
  898. /**
  899. * Extract frontmatter as a dictionary from the specified content string.
  900. * @param content {string} - The content string with potential frontmatter.
  901. * @returns {Object} - The extracted frontmatter as a dictionary.
  902. */
  903. export const extractFrontmatter = (content) => {
  904. const frontmatter = {};
  905. let frontmatterStarted = false;
  906. let frontmatterEnded = false;
  907. const frontmatterPattern = /^\s*([a-z_]+):\s*(.*)\s*$/i;
  908. // Split content into lines
  909. const lines = content.split('\n');
  910. // Check if the content starts with triple quotes
  911. if (lines[0].trim() !== '"""') {
  912. return {};
  913. }
  914. frontmatterStarted = true;
  915. for (let i = 1; i < lines.length; i++) {
  916. const line = lines[i];
  917. if (line.includes('"""')) {
  918. if (frontmatterStarted) {
  919. frontmatterEnded = true;
  920. break;
  921. }
  922. }
  923. if (frontmatterStarted && !frontmatterEnded) {
  924. const match = frontmatterPattern.exec(line);
  925. if (match) {
  926. const [, key, value] = match;
  927. frontmatter[key.trim()] = value.trim();
  928. }
  929. }
  930. }
  931. return frontmatter;
  932. };
  933. // Function to determine the best matching language
  934. export const bestMatchingLanguage = (supportedLanguages, preferredLanguages, defaultLocale) => {
  935. const languages = supportedLanguages.map((lang) => lang.code);
  936. const match = preferredLanguages
  937. .map((prefLang) => languages.find((lang) => lang.startsWith(prefLang)))
  938. .find(Boolean);
  939. return match || defaultLocale;
  940. };
  941. // Get the date in the format YYYY-MM-DD
  942. export const getFormattedDate = () => {
  943. const date = new Date();
  944. const year = date.getFullYear();
  945. const month = String(date.getMonth() + 1).padStart(2, '0');
  946. const day = String(date.getDate()).padStart(2, '0');
  947. return `${year}-${month}-${day}`;
  948. };
  949. // Get the time in the format HH:MM:SS
  950. export const getFormattedTime = () => {
  951. const date = new Date();
  952. return date.toTimeString().split(' ')[0];
  953. };
  954. // Get the current date and time in the format YYYY-MM-DD HH:MM:SS
  955. export const getCurrentDateTime = () => {
  956. return `${getFormattedDate()} ${getFormattedTime()}`;
  957. };
  958. // Get the user's timezone
  959. export const getUserTimezone = () => {
  960. return Intl.DateTimeFormat().resolvedOptions().timeZone;
  961. };
  962. // Get the weekday
  963. export const getWeekday = () => {
  964. const date = new Date();
  965. const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  966. return weekdays[date.getDay()];
  967. };
  968. export const createMessagesList = (history, messageId) => {
  969. if (messageId === null) {
  970. return [];
  971. }
  972. const message = history.messages[messageId];
  973. if (message?.parentId) {
  974. return [...createMessagesList(history, message.parentId), message];
  975. } else {
  976. return [message];
  977. }
  978. };
  979. export const formatFileSize = (size) => {
  980. if (size == null) return 'Unknown size';
  981. if (typeof size !== 'number' || size < 0) return 'Invalid size';
  982. if (size === 0) return '0 B';
  983. const units = ['B', 'KB', 'MB', 'GB', 'TB'];
  984. let unitIndex = 0;
  985. while (size >= 1024 && unitIndex < units.length - 1) {
  986. size /= 1024;
  987. unitIndex++;
  988. }
  989. return `${size.toFixed(1)} ${units[unitIndex]}`;
  990. };
  991. export const getLineCount = (text) => {
  992. console.log(typeof text);
  993. return text ? text.split('\n').length : 0;
  994. };
  995. // Helper function to recursively resolve OpenAPI schema into JSON schema format
  996. function resolveSchema(schemaRef, components, resolvedSchemas = new Set()) {
  997. if (!schemaRef) return {};
  998. if (schemaRef['$ref']) {
  999. const refPath = schemaRef['$ref'];
  1000. const schemaName = refPath.split('/').pop();
  1001. if (resolvedSchemas.has(schemaName)) {
  1002. // Avoid infinite recursion on circular references
  1003. return {};
  1004. }
  1005. resolvedSchemas.add(schemaName);
  1006. const referencedSchema = components.schemas[schemaName];
  1007. return resolveSchema(referencedSchema, components, resolvedSchemas);
  1008. }
  1009. if (schemaRef.type) {
  1010. const schemaObj = { type: schemaRef.type };
  1011. if (schemaRef.description) {
  1012. schemaObj.description = schemaRef.description;
  1013. }
  1014. switch (schemaRef.type) {
  1015. case 'object':
  1016. schemaObj.properties = {};
  1017. schemaObj.required = schemaRef.required || [];
  1018. for (const [propName, propSchema] of Object.entries(schemaRef.properties || {})) {
  1019. schemaObj.properties[propName] = resolveSchema(propSchema, components);
  1020. }
  1021. break;
  1022. case 'array':
  1023. schemaObj.items = resolveSchema(schemaRef.items, components);
  1024. break;
  1025. default:
  1026. // for primitive types (string, integer, etc.), just use as is
  1027. break;
  1028. }
  1029. return schemaObj;
  1030. }
  1031. // fallback for schemas without explicit type
  1032. return {};
  1033. }
  1034. // Main conversion function
  1035. export const convertOpenApiToToolPayload = (openApiSpec) => {
  1036. const toolPayload = [];
  1037. for (const [path, methods] of Object.entries(openApiSpec.paths)) {
  1038. for (const [method, operation] of Object.entries(methods)) {
  1039. const tool = {
  1040. type: 'function',
  1041. name: operation.operationId,
  1042. description: operation.description || operation.summary || 'No description available.',
  1043. parameters: {
  1044. type: 'object',
  1045. properties: {},
  1046. required: []
  1047. }
  1048. };
  1049. // Extract path and query parameters
  1050. if (operation.parameters) {
  1051. operation.parameters.forEach((param) => {
  1052. let description = param.schema.description || param.description || '';
  1053. if (param.schema.enum && Array.isArray(param.schema.enum)) {
  1054. description += `. Possible values: ${param.schema.enum.join(', ')}`;
  1055. }
  1056. tool.parameters.properties[param.name] = {
  1057. type: param.schema.type,
  1058. description: description
  1059. };
  1060. if (param.required) {
  1061. tool.parameters.required.push(param.name);
  1062. }
  1063. });
  1064. }
  1065. // Extract and recursively resolve requestBody if available
  1066. if (operation.requestBody) {
  1067. const content = operation.requestBody.content;
  1068. if (content && content['application/json']) {
  1069. const requestSchema = content['application/json'].schema;
  1070. const resolvedRequestSchema = resolveSchema(requestSchema, openApiSpec.components);
  1071. if (resolvedRequestSchema.properties) {
  1072. tool.parameters.properties = {
  1073. ...tool.parameters.properties,
  1074. ...resolvedRequestSchema.properties
  1075. };
  1076. if (resolvedRequestSchema.required) {
  1077. tool.parameters.required = [
  1078. ...new Set([...tool.parameters.required, ...resolvedRequestSchema.required])
  1079. ];
  1080. }
  1081. } else if (resolvedRequestSchema.type === 'array') {
  1082. tool.parameters = resolvedRequestSchema; // special case when root schema is an array
  1083. }
  1084. }
  1085. }
  1086. toolPayload.push(tool);
  1087. }
  1088. }
  1089. return toolPayload;
  1090. };