@epam/ai-dial-chat-shared 1.3.0-dev.102 → 1.3.0-dev.105
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/utils/latex.d.ts +4 -3
- package/utils/latex.d.ts.map +1 -1
- package/utils/latex.js +48 -30
package/package.json
CHANGED
package/utils/latex.d.ts
CHANGED
|
@@ -13,9 +13,10 @@
|
|
|
13
13
|
* open, not against the few characters after the amount: `$1 - \frac{3}{p} < 1$` reads as
|
|
14
14
|
* a price for one character and as LaTeX for the rest of the line.
|
|
15
15
|
*
|
|
16
|
-
* The `\(...\)`/`\[...\]` delimiters LLMs commonly emit are
|
|
17
|
-
* recognized directly by `micromark-extension-llm-math`, which the consuming app's bundler
|
|
18
|
-
* config aliases in place of the default `micromark-extension-math` used by `remark-math`.
|
|
16
|
+
* The `\(...\)`/`\[...\]` delimiters LLMs commonly emit are left as-is around a formula: they
|
|
17
|
+
* are recognized directly by `micromark-extension-llm-math`, which the consuming app's bundler
|
|
18
|
+
* config aliases in place of the default `micromark-extension-math` used by `remark-math`. A
|
|
19
|
+
* pair wrapping a link or a URL is escaped prose and is turned back into literal brackets.
|
|
19
20
|
*/
|
|
20
21
|
export declare const preprocessLaTeX: (content: string) => string;
|
|
21
22
|
//# sourceMappingURL=latex.d.ts.map
|
package/utils/latex.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"latex.d.ts","sourceRoot":"","sources":["../../src/utils/latex.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"latex.d.ts","sourceRoot":"","sources":["../../src/utils/latex.ts"],"names":[],"mappings":"AAuWA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,eAAe,GAAI,SAAS,MAAM,KAAG,MAoEjD,CAAC"}
|
package/utils/latex.js
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
//#region src/utils/latex.ts
|
|
2
|
-
var e = /\$\\ce\{/g, t = /\$\\pu\{/g, n = /\$\\\\ce\{[^}]*\}\$/g, r = /\$\\\\pu\{[^}]*\}\$/g, i =
|
|
2
|
+
var e = /\$\\ce\{/g, t = /\$\\pu\{/g, n = /\$\\\\ce\{[^}]*\}\$/g, r = /\$\\\\pu\{[^}]*\}\$/g, i = /\]\(|:\/\//, a = {
|
|
3
|
+
"[": "]",
|
|
4
|
+
"(": ")"
|
|
5
|
+
}, o = {
|
|
6
|
+
"[": "[",
|
|
7
|
+
"]": "]",
|
|
8
|
+
"(": "(",
|
|
9
|
+
")": ")"
|
|
10
|
+
}, s = /\$\$([\s\S]*?)\$\$/g, c = /^[ \t]*\r?\n/, l = /\r?\n[ \t]*$/, u = /\d+(?:,\d{3})*(?:\.\d+)?(?:[KMBkmb])?/y, d = /\s|[^a-zA-Z\d]/y, f = /[\^_\\]/y, p = /(?:[<>]|\\(?:le|leq|ll|ge|geq|gg|ne|neq|approx|equiv|sim|simeq|cong|in|notin|subset|subseteq|supset|supseteq|to|rightarrow|leftarrow|mapsto|implies|iff)\b)/y, m = /\\[a-zA-Z]|[\^_]/, h = (i) => {
|
|
3
11
|
let a = i.replace(e, "$\\\\ce{");
|
|
4
12
|
return a = a.replace(t, "$\\\\pu{"), a = a.replace(n, (e) => `$${e}$`), a = a.replace(r, (e) => `$${e}$`), a;
|
|
5
|
-
},
|
|
13
|
+
}, g = (e) => {
|
|
6
14
|
let t = [], n = -1, r = -1;
|
|
7
15
|
for (let i = 0; i < e.length; i++) {
|
|
8
16
|
let a = e[i];
|
|
9
17
|
a === "`" && i + 2 < e.length && e[i + 1] === "`" && e[i + 2] === "`" ? r === -1 ? (r = i, i += 2) : (t.push([r, i + 2]), r = -1, i += 2) : a === "`" && r === -1 && (n === -1 ? n = i : (t.push([n, i]), n = -1));
|
|
10
18
|
}
|
|
11
19
|
return t;
|
|
12
|
-
},
|
|
20
|
+
}, _ = (e, t) => {
|
|
13
21
|
let n = 0, r = t.length - 1;
|
|
14
22
|
for (; n <= r;) {
|
|
15
23
|
let i = Math.floor((n + r) / 2), [a, o] = t[i];
|
|
@@ -17,28 +25,38 @@ var e = /\$\\ce\{/g, t = /\$\\pu\{/g, n = /\$\\\\ce\{[^}]*\}\$/g, r = /\$\\\\pu\
|
|
|
17
25
|
e < a ? r = i - 1 : n = i + 1;
|
|
18
26
|
}
|
|
19
27
|
return !1;
|
|
20
|
-
},
|
|
28
|
+
}, v = (e, t, n) => (e.lastIndex = n, e.exec(t)), y = /\s/, b = (e, t) => {
|
|
21
29
|
let n = t;
|
|
22
|
-
for (; n < e.length &&
|
|
30
|
+
for (; n < e.length && y.test(e[n]);) n++;
|
|
23
31
|
return n;
|
|
24
|
-
},
|
|
32
|
+
}, x = (e) => {
|
|
25
33
|
if (!e.includes("$$")) return e;
|
|
26
|
-
let t =
|
|
27
|
-
for (
|
|
28
|
-
let
|
|
29
|
-
if (
|
|
30
|
-
let u = e.lastIndexOf("\n",
|
|
31
|
-
if (e.slice(u,
|
|
32
|
-
let d = e.indexOf("\n",
|
|
33
|
-
if (!
|
|
34
|
-
let p =
|
|
35
|
-
n.push(e.slice(r,
|
|
34
|
+
let t = g(e), n = [], r = 0, i;
|
|
35
|
+
for (s.lastIndex = 0; (i = s.exec(e)) !== null;) {
|
|
36
|
+
let a = i.index, o = a + i[0].length, s = i[1];
|
|
37
|
+
if (_(a, t)) continue;
|
|
38
|
+
let u = e.lastIndexOf("\n", a - 1) + 1;
|
|
39
|
+
if (e.slice(u, a).trim() !== "") continue;
|
|
40
|
+
let d = e.indexOf("\n", o), f = e.slice(o, d === -1 ? e.length : d);
|
|
41
|
+
if (!s.includes("\n") && (f.trim() !== "" || s.trim() === "")) continue;
|
|
42
|
+
let p = s.replace(c, "").replace(l, "");
|
|
43
|
+
n.push(e.slice(r, a)), n.push(`$$\n${p}\n$$${f.trim() === "" ? "" : "\n"}`), r = o;
|
|
36
44
|
}
|
|
37
45
|
return n.push(e.substring(r)), n.join("");
|
|
38
|
-
},
|
|
39
|
-
|
|
46
|
+
}, S = (e) => {
|
|
47
|
+
if (!e.includes("\\[") && !e.includes("\\(")) return e;
|
|
48
|
+
let t = g(e), n = [], r = 0;
|
|
49
|
+
for (let s = 0; s < e.length - 1; s++) {
|
|
50
|
+
let c = e[s + 1], l = a[c];
|
|
51
|
+
if (e[s] !== "\\" || l == null || s > 0 && e[s - 1] === "\\" || _(s, t)) continue;
|
|
52
|
+
let u = e.indexOf(`\\${l}`, s + 2);
|
|
53
|
+
u !== -1 && i.test(e.slice(s + 2, u)) && (n.push(e.slice(r, s), o[c], e.slice(s + 2, u), o[l]), r = u + 2, s = u + 1);
|
|
54
|
+
}
|
|
55
|
+
return r === 0 ? e : (n.push(e.substring(r)), n.join(""));
|
|
56
|
+
}, C = (e, t) => {
|
|
57
|
+
let n = v(u, e, t + 1);
|
|
40
58
|
return n ? t + 1 + n[0].length : -1;
|
|
41
|
-
},
|
|
59
|
+
}, w = (e, t) => t >= e.length ? !0 : !(!v(d, e, t) || v(f, e, t) || v(p, e, b(e, t))), T = (e, t) => {
|
|
42
60
|
let n = [];
|
|
43
61
|
for (let r = 0; r < e.length; r++) {
|
|
44
62
|
if (e[r] !== "$") continue;
|
|
@@ -48,21 +66,21 @@ var e = /\$\\ce\{/g, t = /\$\\pu\{/g, n = /\$\\\\ce\{[^}]*\}\$/g, r = /\$\\\\pu\
|
|
|
48
66
|
r = i;
|
|
49
67
|
continue;
|
|
50
68
|
}
|
|
51
|
-
r > 0 && e[r - 1] === "\\" ||
|
|
69
|
+
r > 0 && e[r - 1] === "\\" || _(r, t) || n.push(r);
|
|
52
70
|
}
|
|
53
71
|
return n;
|
|
54
|
-
},
|
|
72
|
+
}, E = (e, t, n, r) => {
|
|
55
73
|
if (n <= t + 1 || e[n - 1] === "`") return !1;
|
|
56
74
|
for (let i = t + 1; i < n; i++) if (e[i] === "\n" || e[i] === "$" && !(e[i - 1] === "\\" || r.has(i))) return !1;
|
|
57
75
|
return !0;
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
(
|
|
62
|
-
let n =
|
|
76
|
+
}, D = (e, t, n, r) => n !== void 0 && E(e, t, n, r) && m.test(e.slice(t + 1, n)), O = (e) => {
|
|
77
|
+
let t = S(e);
|
|
78
|
+
if (!t.includes("$")) return t;
|
|
79
|
+
(t.includes("\\ce{") || t.includes("\\pu{")) && (t = h(t)), t = x(t);
|
|
80
|
+
let n = g(t), r = T(t, n), i = /* @__PURE__ */ new Map(), a = -1;
|
|
63
81
|
for (let e = 0; e < r.length; e++) {
|
|
64
|
-
let n = r[e], o =
|
|
65
|
-
if (o !== -1 &&
|
|
82
|
+
let n = r[e], o = C(t, n);
|
|
83
|
+
if (o !== -1 && w(t, o) && (a !== -1 || !D(t, n, r[e + 1], i))) {
|
|
66
84
|
i.set(n, "\\$"), a === -1 && r[e + 1] === o && (i.set(o, "\\$"), e++);
|
|
67
85
|
continue;
|
|
68
86
|
}
|
|
@@ -70,7 +88,7 @@ var e = /\$\\ce\{/g, t = /\$\\pu\{/g, n = /\$\\\\ce\{[^}]*\}\$/g, r = /\$\\\\pu\
|
|
|
70
88
|
a = n;
|
|
71
89
|
continue;
|
|
72
90
|
}
|
|
73
|
-
|
|
91
|
+
E(t, a, n, i) ? (i.set(a, "$$"), i.set(n, "$$"), a = -1) : a = n;
|
|
74
92
|
}
|
|
75
93
|
if (i.size === 0) return t;
|
|
76
94
|
let o = [], s = 0;
|
|
@@ -78,4 +96,4 @@ var e = /\$\\ce\{/g, t = /\$\\pu\{/g, n = /\$\\\\ce\{[^}]*\}\$/g, r = /\$\\\\pu\
|
|
|
78
96
|
return o.push(t.substring(s)), o.join("");
|
|
79
97
|
};
|
|
80
98
|
//#endregion
|
|
81
|
-
export {
|
|
99
|
+
export { O as preprocessLaTeX };
|