index.ts 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. import { v4 as uuidv4 } from 'uuid';
  2. import sha256 from 'js-sha256';
  3. import { WEBUI_BASE_URL } from '$lib/constants';
  4. import dayjs from 'dayjs';
  5. import relativeTime from 'dayjs/plugin/relativeTime';
  6. import isToday from 'dayjs/plugin/isToday';
  7. import isYesterday from 'dayjs/plugin/isYesterday';
  8. import localizedFormat from 'dayjs/plugin/localizedFormat';
  9. dayjs.extend(relativeTime);
  10. dayjs.extend(isToday);
  11. dayjs.extend(isYesterday);
  12. dayjs.extend(localizedFormat);
  13. import { TTS_RESPONSE_SPLIT } from '$lib/types';
  14. import pdfWorkerUrl from 'pdfjs-dist/build/pdf.worker.mjs?url';
  15. import { marked } from 'marked';
  16. import markedExtension from '$lib/utils/marked/extension';
  17. import markedKatexExtension from '$lib/utils/marked/katex-extension';
  18. import hljs from 'highlight.js';
  19. //////////////////////////
  20. // Helper functions
  21. //////////////////////////
  22. export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
  23. function escapeRegExp(string: string): string {
  24. return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  25. }
  26. export const replaceTokens = (content, sourceIds, char, user) => {
  27. const tokens = [
  28. { regex: /{{char}}/gi, replacement: char },
  29. { regex: /{{user}}/gi, replacement: user },
  30. {
  31. regex: /{{VIDEO_FILE_ID_([a-f0-9-]+)}}/gi,
  32. replacement: (_, fileId) =>
  33. `<video src="${WEBUI_BASE_URL}/api/v1/files/${fileId}/content" controls></video>`
  34. },
  35. {
  36. regex: /{{HTML_FILE_ID_([a-f0-9-]+)}}/gi,
  37. replacement: (_, fileId) => `<file type="html" id="${fileId}" />`
  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(
  62. regex,
  63. `<source_id data="${idx + 1}" title="${encodeURIComponent(sourceId)}" />`
  64. );
  65. });
  66. }
  67. return segment;
  68. });
  69. return content;
  70. };
  71. export const sanitizeResponseContent = (content: string) => {
  72. return content
  73. .replace(/<\|[a-z]*$/, '')
  74. .replace(/<\|[a-z]+\|$/, '')
  75. .replace(/<$/, '')
  76. .replaceAll('<', '&lt;')
  77. .replaceAll('>', '&gt;')
  78. .replaceAll(/<\|[a-z]+\|>/g, ' ')
  79. .trim();
  80. };
  81. export const processResponseContent = (content: string) => {
  82. content = processChineseContent(content);
  83. return content.trim();
  84. };
  85. function isChineseChar(char: string): boolean {
  86. return /\p{Script=Han}/u.test(char);
  87. }
  88. // Tackle "Model output issue not following the standard Markdown/LaTeX format" in Chinese.
  89. function processChineseContent(content: string): string {
  90. // This function is used to process the response content before the response content is rendered.
  91. const lines = content.split('\n');
  92. const processedLines = lines.map((line) => {
  93. if (/[\u4e00-\u9fa5]/.test(line)) {
  94. // Problems caused by Chinese parentheses
  95. /* Discription:
  96. * When `*` has Chinese delimiters on the inside, markdown parser ignore bold or italic style.
  97. * - e.g. `**中文名(English)**中文内容` will be parsed directly,
  98. * instead of `<strong>中文名(English)</strong>中文内容`.
  99. * Solution:
  100. * Adding a `space` before and after the bold/italic part can solve the problem.
  101. * - e.g. `**中文名(English)**中文内容` -> ` **中文名(English)** 中文内容`
  102. * Note:
  103. * Similar problem was found with English parentheses and other full delimiters,
  104. * but they are not handled here because they are less likely to appear in LLM output.
  105. * Change the behavior in future if needed.
  106. */
  107. if (line.includes('*')) {
  108. // Handle **bold** and *italic*
  109. // 1. With Chinese parentheses
  110. if (/(|)/.test(line)) {
  111. line = processChineseDelimiters(line, '**', '(', ')');
  112. line = processChineseDelimiters(line, '*', '(', ')');
  113. }
  114. // 2. With Chinese quotations
  115. if (/“|”/.test(line)) {
  116. line = processChineseDelimiters(line, '**', '“', '”');
  117. line = processChineseDelimiters(line, '*', '“', '”');
  118. }
  119. }
  120. }
  121. return line;
  122. });
  123. content = processedLines.join('\n');
  124. return content;
  125. }
  126. // Helper function for `processChineseContent`
  127. function processChineseDelimiters(
  128. line: string,
  129. symbol: string,
  130. leftSymbol: string,
  131. rightSymbol: string
  132. ): string {
  133. // NOTE: If needed, with a little modification, this function can be applied to more cases.
  134. const escapedSymbol = escapeRegExp(symbol);
  135. const regex = new RegExp(
  136. `(.?)(?<!${escapedSymbol})(${escapedSymbol})([^${escapedSymbol}]+)(${escapedSymbol})(?!${escapedSymbol})(.)`,
  137. 'g'
  138. );
  139. return line.replace(regex, (match, l, left, content, right, r) => {
  140. const result =
  141. (content.startsWith(leftSymbol) && l && l.length > 0 && isChineseChar(l[l.length - 1])) ||
  142. (content.endsWith(rightSymbol) && r && r.length > 0 && isChineseChar(r[0]));
  143. if (result) {
  144. return `${l} ${left}${content}${right} ${r}`;
  145. } else {
  146. return match;
  147. }
  148. });
  149. }
  150. export function unescapeHtml(html: string) {
  151. const doc = new DOMParser().parseFromString(html, 'text/html');
  152. return doc.documentElement.textContent;
  153. }
  154. export const capitalizeFirstLetter = (string) => {
  155. return string.charAt(0).toUpperCase() + string.slice(1);
  156. };
  157. export const splitStream = (splitOn) => {
  158. let buffer = '';
  159. return new TransformStream({
  160. transform(chunk, controller) {
  161. buffer += chunk;
  162. const parts = buffer.split(splitOn);
  163. parts.slice(0, -1).forEach((part) => controller.enqueue(part));
  164. buffer = parts[parts.length - 1];
  165. },
  166. flush(controller) {
  167. if (buffer) controller.enqueue(buffer);
  168. }
  169. });
  170. };
  171. export const convertMessagesToHistory = (messages) => {
  172. const history = {
  173. messages: {},
  174. currentId: null
  175. };
  176. let parentMessageId = null;
  177. let messageId = null;
  178. for (const message of messages) {
  179. messageId = uuidv4();
  180. if (parentMessageId !== null) {
  181. history.messages[parentMessageId].childrenIds = [
  182. ...history.messages[parentMessageId].childrenIds,
  183. messageId
  184. ];
  185. }
  186. history.messages[messageId] = {
  187. ...message,
  188. id: messageId,
  189. parentId: parentMessageId,
  190. childrenIds: []
  191. };
  192. parentMessageId = messageId;
  193. }
  194. history.currentId = messageId;
  195. return history;
  196. };
  197. export const getGravatarURL = (email) => {
  198. // Trim leading and trailing whitespace from
  199. // an email address and force all characters
  200. // to lower case
  201. const address = String(email).trim().toLowerCase();
  202. // Create a SHA256 hash of the final string
  203. const hash = sha256(address);
  204. // Grab the actual image URL
  205. return `https://www.gravatar.com/avatar/${hash}`;
  206. };
  207. export const canvasPixelTest = () => {
  208. // Test a 1x1 pixel to potentially identify browser/plugin fingerprint blocking or spoofing
  209. // Inspiration: https://github.com/kkapsner/CanvasBlocker/blob/master/test/detectionTest.js
  210. const canvas = document.createElement('canvas');
  211. const ctx = canvas.getContext('2d');
  212. canvas.height = 1;
  213. canvas.width = 1;
  214. const imageData = new ImageData(canvas.width, canvas.height);
  215. const pixelValues = imageData.data;
  216. // Generate RGB test data
  217. for (let i = 0; i < imageData.data.length; i += 1) {
  218. if (i % 4 !== 3) {
  219. pixelValues[i] = Math.floor(256 * Math.random());
  220. } else {
  221. pixelValues[i] = 255;
  222. }
  223. }
  224. ctx.putImageData(imageData, 0, 0);
  225. const p = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
  226. // Read RGB data and fail if unmatched
  227. for (let i = 0; i < p.length; i += 1) {
  228. if (p[i] !== pixelValues[i]) {
  229. console.log(
  230. 'canvasPixelTest: Wrong canvas pixel RGB value detected:',
  231. p[i],
  232. 'at:',
  233. i,
  234. 'expected:',
  235. pixelValues[i]
  236. );
  237. console.log('canvasPixelTest: Canvas blocking or spoofing is likely');
  238. return false;
  239. }
  240. }
  241. return true;
  242. };
  243. export const compressImage = async (imageUrl, maxWidth, maxHeight) => {
  244. return new Promise((resolve, reject) => {
  245. const img = new Image();
  246. img.onload = () => {
  247. const canvas = document.createElement('canvas');
  248. let width = img.width;
  249. let height = img.height;
  250. // Maintain aspect ratio while resizing
  251. if (maxWidth && maxHeight) {
  252. // Resize with both dimensions defined (preserves aspect ratio)
  253. if (width <= maxWidth && height <= maxHeight) {
  254. resolve(imageUrl);
  255. return;
  256. }
  257. if (width / height > maxWidth / maxHeight) {
  258. height = Math.round((maxWidth * height) / width);
  259. width = maxWidth;
  260. } else {
  261. width = Math.round((maxHeight * width) / height);
  262. height = maxHeight;
  263. }
  264. } else if (maxWidth) {
  265. // Only maxWidth defined
  266. if (width <= maxWidth) {
  267. resolve(imageUrl);
  268. return;
  269. }
  270. height = Math.round((maxWidth * height) / width);
  271. width = maxWidth;
  272. } else if (maxHeight) {
  273. // Only maxHeight defined
  274. if (height <= maxHeight) {
  275. resolve(imageUrl);
  276. return;
  277. }
  278. width = Math.round((maxHeight * width) / height);
  279. height = maxHeight;
  280. }
  281. canvas.width = width;
  282. canvas.height = height;
  283. const context = canvas.getContext('2d');
  284. context.drawImage(img, 0, 0, width, height);
  285. // Get compressed image URL
  286. const compressedUrl = canvas.toDataURL();
  287. resolve(compressedUrl);
  288. };
  289. img.onerror = (error) => reject(error);
  290. img.src = imageUrl;
  291. });
  292. };
  293. export const generateInitialsImage = (name) => {
  294. const canvas = document.createElement('canvas');
  295. const ctx = canvas.getContext('2d');
  296. canvas.width = 100;
  297. canvas.height = 100;
  298. if (!canvasPixelTest()) {
  299. console.log(
  300. 'generateInitialsImage: failed pixel test, fingerprint evasion is likely. Using default image.'
  301. );
  302. return `${WEBUI_BASE_URL}/user.png`;
  303. }
  304. ctx.fillStyle = '#F39C12';
  305. ctx.fillRect(0, 0, canvas.width, canvas.height);
  306. ctx.fillStyle = '#FFFFFF';
  307. ctx.font = '40px Helvetica';
  308. ctx.textAlign = 'center';
  309. ctx.textBaseline = 'middle';
  310. const sanitizedName = name.trim();
  311. const initials =
  312. sanitizedName.length > 0
  313. ? sanitizedName[0] +
  314. (sanitizedName.split(' ').length > 1
  315. ? sanitizedName[sanitizedName.lastIndexOf(' ') + 1]
  316. : '')
  317. : '';
  318. ctx.fillText(initials.toUpperCase(), canvas.width / 2, canvas.height / 2);
  319. return canvas.toDataURL();
  320. };
  321. export const formatDate = (inputDate) => {
  322. const date = dayjs(inputDate);
  323. if (date.isToday()) {
  324. return `Today at {{LOCALIZED_TIME}}`;
  325. } else if (date.isYesterday()) {
  326. return `Yesterday at {{LOCALIZED_TIME}}`;
  327. } else {
  328. return `{{LOCALIZED_DATE}} at {{LOCALIZED_TIME}}`;
  329. }
  330. };
  331. export const copyToClipboard = async (text, html = null, formatted = false) => {
  332. if (formatted) {
  333. let styledHtml = '';
  334. if (!html) {
  335. const options = {
  336. throwOnError: false,
  337. highlight: function (code, lang) {
  338. const language = hljs.getLanguage(lang) ? lang : 'plaintext';
  339. return hljs.highlight(code, { language }).value;
  340. }
  341. };
  342. marked.use(markedKatexExtension(options));
  343. marked.use(markedExtension(options));
  344. // DEVELOPER NOTE: Go to `$lib/components/chat/Messages/Markdown.svelte` to add extra markdown extensions for rendering.
  345. const htmlContent = marked.parse(text);
  346. // Add basic styling to make the content look better when pasted
  347. styledHtml = `
  348. <div>
  349. <style>
  350. pre {
  351. background-color: #f6f8fa;
  352. border-radius: 6px;
  353. padding: 16px;
  354. overflow: auto;
  355. }
  356. code {
  357. font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  358. font-size: 14px;
  359. }
  360. .hljs-keyword { color: #d73a49; }
  361. .hljs-string { color: #032f62; }
  362. .hljs-comment { color: #6a737d; }
  363. .hljs-function { color: #6f42c1; }
  364. .hljs-number { color: #005cc5; }
  365. .hljs-operator { color: #d73a49; }
  366. .hljs-class { color: #6f42c1; }
  367. .hljs-title { color: #6f42c1; }
  368. .hljs-params { color: #24292e; }
  369. .hljs-built_in { color: #005cc5; }
  370. blockquote {
  371. border-left: 4px solid #dfe2e5;
  372. padding-left: 16px;
  373. color: #6a737d;
  374. margin-left: 0;
  375. margin-right: 0;
  376. }
  377. table {
  378. border-collapse: collapse;
  379. width: 100%;
  380. margin-bottom: 16px;
  381. }
  382. table, th, td {
  383. border: 1px solid #dfe2e5;
  384. }
  385. th, td {
  386. padding: 8px 12px;
  387. }
  388. th {
  389. background-color: #f6f8fa;
  390. }
  391. </style>
  392. ${htmlContent}
  393. </div>
  394. `;
  395. } else {
  396. // If HTML is provided, use it directly
  397. styledHtml = html;
  398. }
  399. // Create a blob with HTML content
  400. const blob = new Blob([styledHtml], { type: 'text/html' });
  401. try {
  402. // Create a ClipboardItem with HTML content
  403. const data = new ClipboardItem({
  404. 'text/html': blob,
  405. 'text/plain': new Blob([text], { type: 'text/plain' })
  406. });
  407. // Write to clipboard
  408. await navigator.clipboard.write([data]);
  409. return true;
  410. } catch (err) {
  411. console.error('Error copying formatted content:', err);
  412. // Fallback to plain text
  413. return await copyToClipboard(text);
  414. }
  415. } else {
  416. let result = false;
  417. if (!navigator.clipboard) {
  418. const textArea = document.createElement('textarea');
  419. textArea.value = text;
  420. // Avoid scrolling to bottom
  421. textArea.style.top = '0';
  422. textArea.style.left = '0';
  423. textArea.style.position = 'fixed';
  424. document.body.appendChild(textArea);
  425. textArea.focus();
  426. textArea.select();
  427. try {
  428. const successful = document.execCommand('copy');
  429. const msg = successful ? 'successful' : 'unsuccessful';
  430. console.log('Fallback: Copying text command was ' + msg);
  431. result = true;
  432. } catch (err) {
  433. console.error('Fallback: Oops, unable to copy', err);
  434. }
  435. document.body.removeChild(textArea);
  436. return result;
  437. }
  438. result = await navigator.clipboard
  439. .writeText(text)
  440. .then(() => {
  441. console.log('Async: Copying to clipboard was successful!');
  442. return true;
  443. })
  444. .catch((error) => {
  445. console.error('Async: Could not copy text: ', error);
  446. return false;
  447. });
  448. return result;
  449. }
  450. };
  451. export const compareVersion = (latest, current) => {
  452. return current === '0.0.0'
  453. ? false
  454. : current.localeCompare(latest, undefined, {
  455. numeric: true,
  456. sensitivity: 'case',
  457. caseFirst: 'upper'
  458. }) < 0;
  459. };
  460. export const extractCurlyBraceWords = (text) => {
  461. const regex = /\{\{([^}]+)\}\}/g;
  462. const matches = [];
  463. let match;
  464. while ((match = regex.exec(text)) !== null) {
  465. matches.push({
  466. word: match[1].trim(),
  467. startIndex: match.index,
  468. endIndex: regex.lastIndex - 1
  469. });
  470. }
  471. return matches;
  472. };
  473. export const removeLastWordFromString = (inputString, wordString) => {
  474. console.log('inputString', inputString);
  475. // Split the string by newline characters to handle lines separately
  476. const lines = inputString.split('\n');
  477. // Take the last line to operate only on it
  478. const lastLine = lines.pop();
  479. // Split the last line into an array of words
  480. const words = lastLine.split(' ');
  481. // Conditional to check for the last word removal
  482. if (words.at(-1) === wordString || (wordString === '' && words.at(-1) === '\\#')) {
  483. words.pop(); // Remove last word if condition is satisfied
  484. }
  485. // Join the remaining words back into a string and handle space correctly
  486. let updatedLastLine = words.join(' ');
  487. // Add a trailing space to the updated last line if there are still words
  488. if (updatedLastLine !== '') {
  489. updatedLastLine += ' ';
  490. }
  491. // Combine the lines together again, placing the updated last line back in
  492. const resultString = [...lines, updatedLastLine].join('\n');
  493. // Return the final string
  494. console.log('resultString', resultString);
  495. return resultString;
  496. };
  497. export const removeFirstHashWord = (inputString) => {
  498. // Split the string into an array of words
  499. const words = inputString.split(' ');
  500. // Find the index of the first word that starts with #
  501. const index = words.findIndex((word) => word.startsWith('#'));
  502. // Remove the first word with #
  503. if (index !== -1) {
  504. words.splice(index, 1);
  505. }
  506. // Join the remaining words back into a string
  507. const resultString = words.join(' ');
  508. return resultString;
  509. };
  510. export const transformFileName = (fileName) => {
  511. // Convert to lowercase
  512. const lowerCaseFileName = fileName.toLowerCase();
  513. // Remove special characters using regular expression
  514. const sanitizedFileName = lowerCaseFileName.replace(/[^\w\s]/g, '');
  515. // Replace spaces with dashes
  516. const finalFileName = sanitizedFileName.replace(/\s+/g, '-');
  517. return finalFileName;
  518. };
  519. export const calculateSHA256 = async (file) => {
  520. // Create a FileReader to read the file asynchronously
  521. const reader = new FileReader();
  522. // Define a promise to handle the file reading
  523. const readFile = new Promise((resolve, reject) => {
  524. reader.onload = () => resolve(reader.result);
  525. reader.onerror = reject;
  526. });
  527. // Read the file as an ArrayBuffer
  528. reader.readAsArrayBuffer(file);
  529. try {
  530. // Wait for the FileReader to finish reading the file
  531. const buffer = await readFile;
  532. // Convert the ArrayBuffer to a Uint8Array
  533. const uint8Array = new Uint8Array(buffer);
  534. // Calculate the SHA-256 hash using Web Crypto API
  535. const hashBuffer = await crypto.subtle.digest('SHA-256', uint8Array);
  536. // Convert the hash to a hexadecimal string
  537. const hashArray = Array.from(new Uint8Array(hashBuffer));
  538. const hashHex = hashArray.map((byte) => byte.toString(16).padStart(2, '0')).join('');
  539. return `${hashHex}`;
  540. } catch (error) {
  541. console.error('Error calculating SHA-256 hash:', error);
  542. throw error;
  543. }
  544. };
  545. export const getImportOrigin = (_chats) => {
  546. // Check what external service chat imports are from
  547. if ('mapping' in _chats[0]) {
  548. return 'openai';
  549. }
  550. return 'webui';
  551. };
  552. export const getUserPosition = async (raw = false) => {
  553. // Get the user's location using the Geolocation API
  554. const position = await new Promise((resolve, reject) => {
  555. navigator.geolocation.getCurrentPosition(resolve, reject);
  556. }).catch((error) => {
  557. console.error('Error getting user location:', error);
  558. throw error;
  559. });
  560. if (!position) {
  561. return 'Location not available';
  562. }
  563. // Extract the latitude and longitude from the position
  564. const { latitude, longitude } = position.coords;
  565. if (raw) {
  566. return { latitude, longitude };
  567. } else {
  568. return `${latitude.toFixed(3)}, ${longitude.toFixed(3)} (lat, long)`;
  569. }
  570. };
  571. const convertOpenAIMessages = (convo) => {
  572. // Parse OpenAI chat messages and create chat dictionary for creating new chats
  573. const mapping = convo['mapping'];
  574. const messages = [];
  575. let currentId = '';
  576. let lastId = null;
  577. for (const message_id in mapping) {
  578. const message = mapping[message_id];
  579. currentId = message_id;
  580. try {
  581. if (
  582. messages.length == 0 &&
  583. (message['message'] == null ||
  584. (message['message']['content']['parts']?.[0] == '' &&
  585. message['message']['content']['text'] == null))
  586. ) {
  587. // Skip chat messages with no content
  588. continue;
  589. } else {
  590. const new_chat = {
  591. id: message_id,
  592. parentId: lastId,
  593. childrenIds: message['children'] || [],
  594. role: message['message']?.['author']?.['role'] !== 'user' ? 'assistant' : 'user',
  595. content:
  596. message['message']?.['content']?.['parts']?.[0] ||
  597. message['message']?.['content']?.['text'] ||
  598. '',
  599. model: 'gpt-3.5-turbo',
  600. done: true,
  601. context: null
  602. };
  603. messages.push(new_chat);
  604. lastId = currentId;
  605. }
  606. } catch (error) {
  607. console.log('Error with', message, '\nError:', error);
  608. }
  609. }
  610. const history: Record<PropertyKey, (typeof messages)[number]> = {};
  611. messages.forEach((obj) => (history[obj.id] = obj));
  612. const chat = {
  613. history: {
  614. currentId: currentId,
  615. messages: history // Need to convert this to not a list and instead a json object
  616. },
  617. models: ['gpt-3.5-turbo'],
  618. messages: messages,
  619. options: {},
  620. timestamp: convo['create_time'],
  621. title: convo['title'] ?? 'New Chat'
  622. };
  623. return chat;
  624. };
  625. const validateChat = (chat) => {
  626. // Because ChatGPT sometimes has features we can't use like DALL-E or might have corrupted messages, need to validate
  627. const messages = chat.messages;
  628. // Check if messages array is empty
  629. if (messages.length === 0) {
  630. return false;
  631. }
  632. // Last message's children should be an empty array
  633. const lastMessage = messages[messages.length - 1];
  634. if (lastMessage.childrenIds.length !== 0) {
  635. return false;
  636. }
  637. // First message's parent should be null
  638. const firstMessage = messages[0];
  639. if (firstMessage.parentId !== null) {
  640. return false;
  641. }
  642. // Every message's content should be a string
  643. for (const message of messages) {
  644. if (typeof message.content !== 'string') {
  645. return false;
  646. }
  647. }
  648. return true;
  649. };
  650. export const convertOpenAIChats = (_chats) => {
  651. // Create a list of dictionaries with each conversation from import
  652. const chats = [];
  653. let failed = 0;
  654. for (const convo of _chats) {
  655. const chat = convertOpenAIMessages(convo);
  656. if (validateChat(chat)) {
  657. chats.push({
  658. id: convo['id'],
  659. user_id: '',
  660. title: convo['title'],
  661. chat: chat,
  662. timestamp: convo['create_time']
  663. });
  664. } else {
  665. failed++;
  666. }
  667. }
  668. console.log(failed, 'Conversations could not be imported');
  669. return chats;
  670. };
  671. export const isValidHttpUrl = (string: string) => {
  672. let url;
  673. try {
  674. url = new URL(string);
  675. } catch (_) {
  676. return false;
  677. }
  678. return url.protocol === 'http:' || url.protocol === 'https:';
  679. };
  680. export const isYoutubeUrl = (url: string) => {
  681. return (
  682. url.startsWith('https://www.youtube.com') ||
  683. url.startsWith('https://youtu.be') ||
  684. url.startsWith('https://youtube.com') ||
  685. url.startsWith('https://m.youtube.com')
  686. );
  687. };
  688. export const removeEmojis = (str: string) => {
  689. // Regular expression to match emojis
  690. const emojiRegex = /[\uD800-\uDBFF][\uDC00-\uDFFF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g;
  691. // Replace emojis with an empty string
  692. return str.replace(emojiRegex, '');
  693. };
  694. export const removeFormattings = (str: string) => {
  695. return (
  696. str
  697. // Block elements (remove completely)
  698. .replace(/(```[\s\S]*?```)/g, '') // Code blocks
  699. .replace(/^\|.*\|$/gm, '') // Tables
  700. // Inline elements (preserve content)
  701. .replace(/(?:\*\*|__)(.*?)(?:\*\*|__)/g, '$1') // Bold
  702. .replace(/(?:[*_])(.*?)(?:[*_])/g, '$1') // Italic
  703. .replace(/~~(.*?)~~/g, '$1') // Strikethrough
  704. .replace(/`([^`]+)`/g, '$1') // Inline code
  705. // Links and images
  706. .replace(/!?\[([^\]]*)\](?:\([^)]+\)|\[[^\]]*\])/g, '$1') // Links & images
  707. .replace(/^\[[^\]]+\]:\s*.*$/gm, '') // Reference definitions
  708. // Block formatting
  709. .replace(/^#{1,6}\s+/gm, '') // Headers
  710. .replace(/^\s*[-*+]\s+/gm, '') // Lists
  711. .replace(/^\s*(?:\d+\.)\s+/gm, '') // Numbered lists
  712. .replace(/^\s*>[> ]*/gm, '') // Blockquotes
  713. .replace(/^\s*:\s+/gm, '') // Definition lists
  714. // Cleanup
  715. .replace(/\[\^[^\]]*\]/g, '') // Footnotes
  716. .replace(/\n{2,}/g, '\n')
  717. ); // Multiple newlines
  718. };
  719. export const cleanText = (content: string) => {
  720. return removeFormattings(removeEmojis(content.trim()));
  721. };
  722. export const removeDetails = (content, types) => {
  723. for (const type of types) {
  724. content = content.replace(
  725. new RegExp(`<details\\s+type="${type}"[^>]*>.*?<\\/details>`, 'gis'),
  726. ''
  727. );
  728. }
  729. return content;
  730. };
  731. export const removeAllDetails = (content) => {
  732. content = content.replace(/<details[^>]*>.*?<\/details>/gis, '');
  733. return content;
  734. };
  735. export const processDetails = (content) => {
  736. content = removeDetails(content, ['reasoning', 'code_interpreter']);
  737. // This regex matches <details> tags with type="tool_calls" and captures their attributes to convert them to a string
  738. const detailsRegex = /<details\s+type="tool_calls"([^>]*)>([\s\S]*?)<\/details>/gis;
  739. const matches = content.match(detailsRegex);
  740. if (matches) {
  741. for (const match of matches) {
  742. const attributesRegex = /(\w+)="([^"]*)"/g;
  743. const attributes = {};
  744. let attributeMatch;
  745. while ((attributeMatch = attributesRegex.exec(match)) !== null) {
  746. attributes[attributeMatch[1]] = attributeMatch[2];
  747. }
  748. content = content.replace(match, `"${attributes.result}"`);
  749. }
  750. }
  751. return content;
  752. };
  753. // This regular expression matches code blocks marked by triple backticks
  754. const codeBlockRegex = /```[\s\S]*?```/g;
  755. export const extractSentences = (text: string) => {
  756. const codeBlocks: string[] = [];
  757. let index = 0;
  758. // Temporarily replace code blocks with placeholders and store the blocks separately
  759. text = text.replace(codeBlockRegex, (match) => {
  760. const placeholder = `\u0000${index}\u0000`; // Use a unique placeholder
  761. codeBlocks[index++] = match;
  762. return placeholder;
  763. });
  764. // Split the modified text into sentences based on common punctuation marks, avoiding these blocks
  765. let sentences = text.split(/(?<=[.!?])\s+/);
  766. // Restore code blocks and process sentences
  767. sentences = sentences.map((sentence) => {
  768. // Check if the sentence includes a placeholder for a code block
  769. return sentence.replace(/\u0000(\d+)\u0000/g, (_, idx) => codeBlocks[idx]);
  770. });
  771. return sentences.map(cleanText).filter(Boolean);
  772. };
  773. export const extractParagraphsForAudio = (text: string) => {
  774. const codeBlocks: string[] = [];
  775. let index = 0;
  776. // Temporarily replace code blocks with placeholders and store the blocks separately
  777. text = text.replace(codeBlockRegex, (match) => {
  778. const placeholder = `\u0000${index}\u0000`; // Use a unique placeholder
  779. codeBlocks[index++] = match;
  780. return placeholder;
  781. });
  782. // Split the modified text into paragraphs based on newlines, avoiding these blocks
  783. let paragraphs = text.split(/\n+/);
  784. // Restore code blocks and process paragraphs
  785. paragraphs = paragraphs.map((paragraph) => {
  786. // Check if the paragraph includes a placeholder for a code block
  787. return paragraph.replace(/\u0000(\d+)\u0000/g, (_, idx) => codeBlocks[idx]);
  788. });
  789. return paragraphs.map(cleanText).filter(Boolean);
  790. };
  791. export const extractSentencesForAudio = (text: string) => {
  792. return extractSentences(text).reduce((mergedTexts, currentText) => {
  793. const lastIndex = mergedTexts.length - 1;
  794. if (lastIndex >= 0) {
  795. const previousText = mergedTexts[lastIndex];
  796. const wordCount = previousText.split(/\s+/).length;
  797. const charCount = previousText.length;
  798. if (wordCount < 4 || charCount < 50) {
  799. mergedTexts[lastIndex] = previousText + ' ' + currentText;
  800. } else {
  801. mergedTexts.push(currentText);
  802. }
  803. } else {
  804. mergedTexts.push(currentText);
  805. }
  806. return mergedTexts;
  807. }, [] as string[]);
  808. };
  809. export const getMessageContentParts = (content: string, splitOn: string = 'punctuation') => {
  810. const messageContentParts: string[] = [];
  811. switch (splitOn) {
  812. default:
  813. case TTS_RESPONSE_SPLIT.PUNCTUATION:
  814. messageContentParts.push(...extractSentencesForAudio(content));
  815. break;
  816. case TTS_RESPONSE_SPLIT.PARAGRAPHS:
  817. messageContentParts.push(...extractParagraphsForAudio(content));
  818. break;
  819. case TTS_RESPONSE_SPLIT.NONE:
  820. messageContentParts.push(cleanText(content));
  821. break;
  822. }
  823. return messageContentParts;
  824. };
  825. export const blobToFile = (blob, fileName) => {
  826. // Create a new File object from the Blob
  827. const file = new File([blob], fileName, { type: blob.type });
  828. return file;
  829. };
  830. export const getPromptVariables = (user_name, user_location) => {
  831. return {
  832. '{{USER_NAME}}': user_name,
  833. '{{USER_LOCATION}}': user_location || 'Unknown',
  834. '{{CURRENT_DATETIME}}': getCurrentDateTime(),
  835. '{{CURRENT_DATE}}': getFormattedDate(),
  836. '{{CURRENT_TIME}}': getFormattedTime(),
  837. '{{CURRENT_WEEKDAY}}': getWeekday(),
  838. '{{CURRENT_TIMEZONE}}': getUserTimezone(),
  839. '{{USER_LANGUAGE}}': localStorage.getItem('locale') || 'en-US'
  840. };
  841. };
  842. /**
  843. * This function is used to replace placeholders in a template string with the provided prompt.
  844. * The placeholders can be in the following formats:
  845. * - `{{prompt}}`: This will be replaced with the entire prompt.
  846. * - `{{prompt:start:<length>}}`: This will be replaced with the first <length> characters of the prompt.
  847. * - `{{prompt:end:<length>}}`: This will be replaced with the last <length> characters of the prompt.
  848. * - `{{prompt:middletruncate:<length>}}`: This will be replaced with the prompt truncated to <length> characters, with '...' in the middle.
  849. *
  850. * @param {string} template - The template string containing placeholders.
  851. * @param {string} prompt - The string to replace the placeholders with.
  852. * @returns {string} The template string with the placeholders replaced by the prompt.
  853. */
  854. export const titleGenerationTemplate = (template: string, prompt: string): string => {
  855. template = template.replace(
  856. /{{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}/g,
  857. (match, startLength, endLength, middleLength) => {
  858. if (match === '{{prompt}}') {
  859. return prompt;
  860. } else if (match.startsWith('{{prompt:start:')) {
  861. return prompt.substring(0, startLength);
  862. } else if (match.startsWith('{{prompt:end:')) {
  863. return prompt.slice(-endLength);
  864. } else if (match.startsWith('{{prompt:middletruncate:')) {
  865. if (prompt.length <= middleLength) {
  866. return prompt;
  867. }
  868. const start = prompt.slice(0, Math.ceil(middleLength / 2));
  869. const end = prompt.slice(-Math.floor(middleLength / 2));
  870. return `${start}...${end}`;
  871. }
  872. return '';
  873. }
  874. );
  875. return template;
  876. };
  877. export const approximateToHumanReadable = (nanoseconds: number) => {
  878. const seconds = Math.floor((nanoseconds / 1e9) % 60);
  879. const minutes = Math.floor((nanoseconds / 6e10) % 60);
  880. const hours = Math.floor((nanoseconds / 3.6e12) % 24);
  881. const results: string[] = [];
  882. if (seconds >= 0) {
  883. results.push(`${seconds}s`);
  884. }
  885. if (minutes > 0) {
  886. results.push(`${minutes}m`);
  887. }
  888. if (hours > 0) {
  889. results.push(`${hours}h`);
  890. }
  891. return results.reverse().join(' ');
  892. };
  893. export const getTimeRange = (timestamp) => {
  894. const now = new Date();
  895. const date = new Date(timestamp * 1000); // Convert Unix timestamp to milliseconds
  896. // Calculate the difference in milliseconds
  897. const diffTime = now.getTime() - date.getTime();
  898. const diffDays = diffTime / (1000 * 3600 * 24);
  899. const nowDate = now.getDate();
  900. const nowMonth = now.getMonth();
  901. const nowYear = now.getFullYear();
  902. const dateDate = date.getDate();
  903. const dateMonth = date.getMonth();
  904. const dateYear = date.getFullYear();
  905. if (nowYear === dateYear && nowMonth === dateMonth && nowDate === dateDate) {
  906. return 'Today';
  907. } else if (nowYear === dateYear && nowMonth === dateMonth && nowDate - dateDate === 1) {
  908. return 'Yesterday';
  909. } else if (diffDays <= 7) {
  910. return 'Previous 7 days';
  911. } else if (diffDays <= 30) {
  912. return 'Previous 30 days';
  913. } else if (nowYear === dateYear) {
  914. return date.toLocaleString('default', { month: 'long' });
  915. } else {
  916. return date.getFullYear().toString();
  917. }
  918. };
  919. /**
  920. * Extract frontmatter as a dictionary from the specified content string.
  921. * @param content {string} - The content string with potential frontmatter.
  922. * @returns {Object} - The extracted frontmatter as a dictionary.
  923. */
  924. export const extractFrontmatter = (content) => {
  925. const frontmatter = {};
  926. let frontmatterStarted = false;
  927. let frontmatterEnded = false;
  928. const frontmatterPattern = /^\s*([a-z_]+):\s*(.*)\s*$/i;
  929. // Split content into lines
  930. const lines = content.split('\n');
  931. // Check if the content starts with triple quotes
  932. if (lines[0].trim() !== '"""') {
  933. return {};
  934. }
  935. frontmatterStarted = true;
  936. for (let i = 1; i < lines.length; i++) {
  937. const line = lines[i];
  938. if (line.includes('"""')) {
  939. if (frontmatterStarted) {
  940. frontmatterEnded = true;
  941. break;
  942. }
  943. }
  944. if (frontmatterStarted && !frontmatterEnded) {
  945. const match = frontmatterPattern.exec(line);
  946. if (match) {
  947. const [, key, value] = match;
  948. frontmatter[key.trim()] = value.trim();
  949. }
  950. }
  951. }
  952. return frontmatter;
  953. };
  954. // Function to determine the best matching language
  955. export const bestMatchingLanguage = (supportedLanguages, preferredLanguages, defaultLocale) => {
  956. const languages = supportedLanguages.map((lang) => lang.code);
  957. const match = preferredLanguages
  958. .map((prefLang) => languages.find((lang) => lang.startsWith(prefLang)))
  959. .find(Boolean);
  960. return match || defaultLocale;
  961. };
  962. // Get the date in the format YYYY-MM-DD
  963. export const getFormattedDate = () => {
  964. const date = new Date();
  965. const year = date.getFullYear();
  966. const month = String(date.getMonth() + 1).padStart(2, '0');
  967. const day = String(date.getDate()).padStart(2, '0');
  968. return `${year}-${month}-${day}`;
  969. };
  970. // Get the time in the format HH:MM:SS
  971. export const getFormattedTime = () => {
  972. const date = new Date();
  973. return date.toTimeString().split(' ')[0];
  974. };
  975. // Get the current date and time in the format YYYY-MM-DD HH:MM:SS
  976. export const getCurrentDateTime = () => {
  977. return `${getFormattedDate()} ${getFormattedTime()}`;
  978. };
  979. // Get the user's timezone
  980. export const getUserTimezone = () => {
  981. return Intl.DateTimeFormat().resolvedOptions().timeZone;
  982. };
  983. // Get the weekday
  984. export const getWeekday = () => {
  985. const date = new Date();
  986. const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  987. return weekdays[date.getDay()];
  988. };
  989. export const createMessagesList = (history, messageId) => {
  990. if (messageId === null) {
  991. return [];
  992. }
  993. const message = history.messages[messageId];
  994. if (message === undefined) {
  995. return [];
  996. }
  997. if (message?.parentId) {
  998. return [...createMessagesList(history, message.parentId), message];
  999. } else {
  1000. return [message];
  1001. }
  1002. };
  1003. export const formatFileSize = (size) => {
  1004. if (size == null) return 'Unknown size';
  1005. if (typeof size !== 'number' || size < 0) return 'Invalid size';
  1006. if (size === 0) return '0 B';
  1007. const units = ['B', 'KB', 'MB', 'GB', 'TB'];
  1008. let unitIndex = 0;
  1009. while (size >= 1024 && unitIndex < units.length - 1) {
  1010. size /= 1024;
  1011. unitIndex++;
  1012. }
  1013. return `${size.toFixed(1)} ${units[unitIndex]}`;
  1014. };
  1015. export const getLineCount = (text) => {
  1016. console.log(typeof text);
  1017. return text ? text.split('\n').length : 0;
  1018. };
  1019. // Helper function to recursively resolve OpenAPI schema into JSON schema format
  1020. function resolveSchema(schemaRef, components, resolvedSchemas = new Set()) {
  1021. if (!schemaRef) return {};
  1022. if (schemaRef['$ref']) {
  1023. const refPath = schemaRef['$ref'];
  1024. const schemaName = refPath.split('/').pop();
  1025. if (resolvedSchemas.has(schemaName)) {
  1026. // Avoid infinite recursion on circular references
  1027. return {};
  1028. }
  1029. resolvedSchemas.add(schemaName);
  1030. const referencedSchema = components.schemas[schemaName];
  1031. return resolveSchema(referencedSchema, components, resolvedSchemas);
  1032. }
  1033. if (schemaRef.type) {
  1034. const schemaObj = { type: schemaRef.type };
  1035. if (schemaRef.description) {
  1036. schemaObj.description = schemaRef.description;
  1037. }
  1038. switch (schemaRef.type) {
  1039. case 'object':
  1040. schemaObj.properties = {};
  1041. schemaObj.required = schemaRef.required || [];
  1042. for (const [propName, propSchema] of Object.entries(schemaRef.properties || {})) {
  1043. schemaObj.properties[propName] = resolveSchema(propSchema, components);
  1044. }
  1045. break;
  1046. case 'array':
  1047. schemaObj.items = resolveSchema(schemaRef.items, components);
  1048. break;
  1049. default:
  1050. // for primitive types (string, integer, etc.), just use as is
  1051. break;
  1052. }
  1053. return schemaObj;
  1054. }
  1055. // fallback for schemas without explicit type
  1056. return {};
  1057. }
  1058. // Main conversion function
  1059. export const convertOpenApiToToolPayload = (openApiSpec) => {
  1060. const toolPayload = [];
  1061. for (const [path, methods] of Object.entries(openApiSpec.paths)) {
  1062. for (const [method, operation] of Object.entries(methods)) {
  1063. if (operation?.operationId) {
  1064. const tool = {
  1065. name: operation.operationId,
  1066. description: operation.description || operation.summary || 'No description available.',
  1067. parameters: {
  1068. type: 'object',
  1069. properties: {},
  1070. required: []
  1071. }
  1072. };
  1073. // Extract path and query parameters
  1074. if (operation.parameters) {
  1075. operation.parameters.forEach((param) => {
  1076. let description = param.schema.description || param.description || '';
  1077. if (param.schema.enum && Array.isArray(param.schema.enum)) {
  1078. description += `. Possible values: ${param.schema.enum.join(', ')}`;
  1079. }
  1080. tool.parameters.properties[param.name] = {
  1081. type: param.schema.type,
  1082. description: description
  1083. };
  1084. if (param.required) {
  1085. tool.parameters.required.push(param.name);
  1086. }
  1087. });
  1088. }
  1089. // Extract and recursively resolve requestBody if available
  1090. if (operation.requestBody) {
  1091. const content = operation.requestBody.content;
  1092. if (content && content['application/json']) {
  1093. const requestSchema = content['application/json'].schema;
  1094. const resolvedRequestSchema = resolveSchema(requestSchema, openApiSpec.components);
  1095. if (resolvedRequestSchema.properties) {
  1096. tool.parameters.properties = {
  1097. ...tool.parameters.properties,
  1098. ...resolvedRequestSchema.properties
  1099. };
  1100. if (resolvedRequestSchema.required) {
  1101. tool.parameters.required = [
  1102. ...new Set([...tool.parameters.required, ...resolvedRequestSchema.required])
  1103. ];
  1104. }
  1105. } else if (resolvedRequestSchema.type === 'array') {
  1106. tool.parameters = resolvedRequestSchema; // special case when root schema is an array
  1107. }
  1108. }
  1109. }
  1110. toolPayload.push(tool);
  1111. }
  1112. }
  1113. }
  1114. return toolPayload;
  1115. };
  1116. export const slugify = (str: string): string => {
  1117. return (
  1118. str
  1119. // 1. Normalize: separate accented letters into base + combining marks
  1120. .normalize('NFD')
  1121. // 2. Remove all combining marks (the accents)
  1122. .replace(/[\u0300-\u036f]/g, '')
  1123. // 3. Replace any sequence of whitespace with a single hyphen
  1124. .replace(/\s+/g, '-')
  1125. // 4. Remove all characters except alphanumeric characters, hyphens, and underscores
  1126. .replace(/[^a-zA-Z0-9-_]/g, '')
  1127. // 5. Convert to lowercase
  1128. .toLowerCase()
  1129. );
  1130. };
  1131. export const extractInputVariables = (text: string): Record<string, any> => {
  1132. const regex = /{{\s*([^|}\s]+)\s*\|\s*([^}]+)\s*}}/g;
  1133. const regularRegex = /{{\s*([^|}\s]+)\s*}}/g;
  1134. const variables: Record<string, any> = {};
  1135. let match;
  1136. // Use exec() loop instead of matchAll() for better compatibility
  1137. while ((match = regex.exec(text)) !== null) {
  1138. const varName = match[1].trim();
  1139. const definition = match[2].trim();
  1140. variables[varName] = parseVariableDefinition(definition);
  1141. }
  1142. // Then, extract regular variables (without pipe) - only if not already processed
  1143. while ((match = regularRegex.exec(text)) !== null) {
  1144. const varName = match[1].trim();
  1145. // Only add if not already processed as custom variable
  1146. if (!variables.hasOwnProperty(varName)) {
  1147. variables[varName] = { type: 'text' }; // Default type for regular variables
  1148. }
  1149. }
  1150. return variables;
  1151. };
  1152. export const splitProperties = (str: string, delimiter: string): string[] => {
  1153. const result: string[] = [];
  1154. let current = '';
  1155. let depth = 0;
  1156. let inString = false;
  1157. let escapeNext = false;
  1158. for (let i = 0; i < str.length; i++) {
  1159. const char = str[i];
  1160. if (escapeNext) {
  1161. current += char;
  1162. escapeNext = false;
  1163. continue;
  1164. }
  1165. if (char === '\\') {
  1166. current += char;
  1167. escapeNext = true;
  1168. continue;
  1169. }
  1170. if (char === '"' && !escapeNext) {
  1171. inString = !inString;
  1172. current += char;
  1173. continue;
  1174. }
  1175. if (!inString) {
  1176. if (char === '{' || char === '[') {
  1177. depth++;
  1178. } else if (char === '}' || char === ']') {
  1179. depth--;
  1180. }
  1181. if (char === delimiter && depth === 0) {
  1182. result.push(current.trim());
  1183. current = '';
  1184. continue;
  1185. }
  1186. }
  1187. current += char;
  1188. }
  1189. if (current.trim()) {
  1190. result.push(current.trim());
  1191. }
  1192. return result;
  1193. };
  1194. export const parseVariableDefinition = (definition: string): Record<string, any> => {
  1195. // Use splitProperties for the main colon delimiter to handle quoted strings
  1196. const parts = splitProperties(definition, ':');
  1197. const [firstPart, ...propertyParts] = parts;
  1198. // Parse type (explicit or implied)
  1199. const type = firstPart.startsWith('type=') ? firstPart.slice(5) : firstPart;
  1200. // Parse properties; support both key=value and bare flags (e.g., ":required")
  1201. const properties = propertyParts.reduce(
  1202. (props, part) => {
  1203. const trimmed = part.trim();
  1204. if (!trimmed) return props;
  1205. // Use splitProperties for the equals sign as well, in case there are nested quotes
  1206. const equalsParts = splitProperties(trimmed, '=');
  1207. if (equalsParts.length === 1) {
  1208. // It's a flag with no value, e.g. "required" -> true
  1209. const flagName = equalsParts[0].trim();
  1210. if (flagName.length > 0) {
  1211. return { ...props, [flagName]: true };
  1212. }
  1213. return props;
  1214. }
  1215. const [propertyName, ...valueParts] = equalsParts;
  1216. const propertyValueRaw = valueParts.join('='); // Handle values with extra '='
  1217. if (!propertyName || propertyValueRaw == null) return props;
  1218. return {
  1219. ...props,
  1220. [propertyName.trim()]: parseJsonValue(propertyValueRaw.trim())
  1221. };
  1222. },
  1223. {} as Record<string, any>
  1224. );
  1225. return { type, ...properties };
  1226. };
  1227. export const parseJsonValue = (value: string): any => {
  1228. // Remove surrounding quotes if present (for string values)
  1229. if (value.startsWith('"') && value.endsWith('"')) {
  1230. return value.slice(1, -1);
  1231. }
  1232. // Check if it starts with square or curly brackets (JSON)
  1233. if (/^[\[{]/.test(value)) {
  1234. try {
  1235. return JSON.parse(value);
  1236. } catch {
  1237. return value; // Return as string if JSON parsing fails
  1238. }
  1239. }
  1240. return value;
  1241. };
  1242. async function ensurePDFjsLoaded() {
  1243. if (!window.pdfjsLib) {
  1244. const pdfjs = await import('pdfjs-dist');
  1245. pdfjs.GlobalWorkerOptions.workerSrc = pdfWorkerUrl;
  1246. if (!window.pdfjsLib) {
  1247. throw new Error('pdfjsLib is required for PDF extraction');
  1248. }
  1249. }
  1250. return window.pdfjsLib;
  1251. }
  1252. export const extractContentFromFile = async (file: File) => {
  1253. // Known text file extensions for extra fallback
  1254. const textExtensions = [
  1255. '.txt',
  1256. '.md',
  1257. '.csv',
  1258. '.json',
  1259. '.js',
  1260. '.ts',
  1261. '.css',
  1262. '.html',
  1263. '.xml',
  1264. '.yaml',
  1265. '.yml',
  1266. '.rtf'
  1267. ];
  1268. function getExtension(filename: string) {
  1269. const dot = filename.lastIndexOf('.');
  1270. return dot === -1 ? '' : filename.substr(dot).toLowerCase();
  1271. }
  1272. // Uses pdfjs to extract text from PDF
  1273. async function extractPdfText(file: File) {
  1274. const pdfjsLib = await ensurePDFjsLoaded();
  1275. const arrayBuffer = await file.arrayBuffer();
  1276. const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise;
  1277. let allText = '';
  1278. for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
  1279. const page = await pdf.getPage(pageNum);
  1280. const content = await page.getTextContent();
  1281. const strings = content.items.map((item: any) => item.str);
  1282. allText += strings.join(' ') + '\n';
  1283. }
  1284. return allText;
  1285. }
  1286. // Reads file as text using FileReader
  1287. function readAsText(file: File) {
  1288. return new Promise((resolve, reject) => {
  1289. const reader = new FileReader();
  1290. reader.onload = () => resolve(reader.result);
  1291. reader.onerror = reject;
  1292. reader.readAsText(file);
  1293. });
  1294. }
  1295. const type = file.type || '';
  1296. const ext = getExtension(file.name);
  1297. // PDF check
  1298. if (type === 'application/pdf' || ext === '.pdf') {
  1299. return await extractPdfText(file);
  1300. }
  1301. // Text check (plain or common text-based)
  1302. if (type.startsWith('text/') || textExtensions.includes(ext)) {
  1303. return await readAsText(file);
  1304. }
  1305. // Fallback: try to read as text, if decodable
  1306. try {
  1307. return await readAsText(file);
  1308. } catch (err) {
  1309. throw new Error('Unsupported or non-text file type: ' + (file.name || type));
  1310. }
  1311. };
  1312. export const getAge = (birthDate) => {
  1313. const today = new Date();
  1314. const bDate = new Date(birthDate);
  1315. let age = today.getFullYear() - bDate.getFullYear();
  1316. const m = today.getMonth() - bDate.getMonth();
  1317. if (m < 0 || (m === 0 && today.getDate() < bDate.getDate())) {
  1318. age--;
  1319. }
  1320. return age.toString();
  1321. };
  1322. export const convertHeicToJpeg = async (file: File) => {
  1323. const { default: heic2any } = await import('heic2any');
  1324. try {
  1325. return await heic2any({ blob: file, toType: 'image/jpeg' });
  1326. } catch (err: any) {
  1327. if (err?.message?.includes('already browser readable')) {
  1328. return file;
  1329. }
  1330. throw err;
  1331. }
  1332. };
  1333. export const decodeString = (str: string) => {
  1334. try {
  1335. return decodeURIComponent(str);
  1336. } catch (e) {
  1337. return str;
  1338. }
  1339. };
  1340. export const renderMermaidDiagram = async (code: string) => {
  1341. try {
  1342. const { default: mermaid } = await import('mermaid');
  1343. mermaid.initialize({
  1344. startOnLoad: true,
  1345. theme: document.documentElement.classList.contains('dark') ? 'dark' : 'default',
  1346. securityLevel: 'loose'
  1347. });
  1348. if (await mermaid.parse(code)) {
  1349. const { svg } = await mermaid.render(`mermaid-${uuidv4()}`, code);
  1350. return svg;
  1351. }
  1352. } catch (error) {
  1353. console.log('Failed to render mermaid diagram:', error);
  1354. return '';
  1355. }
  1356. };
  1357. export const renderVegaVisualization = async (spec: string) => {
  1358. try {
  1359. const vega = await import('vega');
  1360. const parsedSpec = JSON.parse(spec);
  1361. let vegaSpec;
  1362. if (parsedSpec.$schema && parsedSpec.$schema.includes('vega-lite')) {
  1363. const vegaLite = await import('vega-lite');
  1364. vegaSpec = vegaLite.compile(parsedSpec).spec;
  1365. } else {
  1366. vegaSpec = parsedSpec;
  1367. }
  1368. const view = new vega.View(vega.parse(vegaSpec), {renderer: 'none'});
  1369. const svg = await view.toSVG();
  1370. return svg;
  1371. } catch (error) {
  1372. console.log('Failed to render Vega visualization:', error);
  1373. return '';
  1374. }
  1375. };