@maggioli-rd/aicore-agent-chat 1.2.0 → 1.3.0
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/README.md +22 -5
- package/dist/aicore-agent-chat.js +52 -34
- package/dist/aicore-agent-chat.js.map +1 -1
- package/dist/bundle/a2ui-C3GhjTrJ.js +113 -0
- package/dist/bundle/abnfDiagram-S7VZAUFO-DxkzIS8G.js +1 -0
- package/dist/bundle/architecture-WOLXFQ4H-BXMyXdT1.js +1 -0
- package/dist/bundle/architectureDiagram-YGSQEFNW-BmsDn6Ho.js +36 -0
- package/dist/bundle/blockDiagram-APKKGZFW-bG_MVPzT.js +140 -0
- package/dist/bundle/bpmnIcons-APHXDYXF-C0jbCElo.js +1 -0
- package/dist/bundle/c4Diagram-KJ2TOQU5-BVad-wQe.js +38 -0
- package/dist/bundle/chunk-2Q5K7J3B-C1jixKkw.js +1 -0
- package/dist/bundle/chunk-5VM5RSS4-CwNG0iBi.js +15 -0
- package/dist/bundle/chunk-6MUBBAK2-CeH4cZO-.js +2 -0
- package/dist/bundle/chunk-AXFCVDMN-D0Vqm50X.js +62 -0
- package/dist/bundle/chunk-BG4553SI-DUBI9JbR.js +127 -0
- package/dist/bundle/chunk-DQASFODM-DBWVlIjs.js +10 -0
- package/dist/bundle/chunk-DUW6YSOI-ChybIQ82.js +1 -0
- package/dist/bundle/chunk-HFQBQIA3-D2xPptW8.js +1 -0
- package/dist/bundle/chunk-J5ZVWO5B-DHB__t6z.js +1 -0
- package/dist/bundle/chunk-JWPE2WC7-DVXcaiue.js +1 -0
- package/dist/bundle/chunk-KQLB2F66-BRGnyo9e.js +1 -0
- package/dist/bundle/chunk-KS47KZWR-DV3Gxewa.js +1 -0
- package/dist/bundle/chunk-LEVMRMFU-BdDTT4pw.js +1 -0
- package/dist/bundle/chunk-M57EVK23-BRqFqW5o.js +1 -0
- package/dist/bundle/chunk-NGNAAXSQ-DreF1ykJ.js +214 -0
- package/dist/bundle/chunk-OY52VR5R-BamrP8d_.js +88 -0
- package/dist/bundle/chunk-QB3YPZ4I-BqUUL29j.js +213 -0
- package/dist/bundle/chunk-R3KSYZAL-gmtEjdKU.js +1 -0
- package/dist/bundle/chunk-STOXU5M4-DdqxXfKq.js +72 -0
- package/dist/bundle/chunk-TU62E6Q5-B4GgWjUW.js +1 -0
- package/dist/bundle/chunk-X4PVCEUW-BcN5Aq5G.js +1 -0
- package/dist/bundle/chunk-XXDRQBXY-BIvjAdC_.js +1 -0
- package/dist/bundle/chunk-Y2CYZVJY-DsF7k-Jl.js +1 -0
- package/dist/bundle/classDiagram-v2-L25IHNXF-Dw7so3mk.js +217 -0
- package/dist/bundle/cose-bilkent-JH36ORCC-DUvnuVrm.js +1 -0
- package/dist/bundle/cynefin-EF2NZ3EQ-CcVPiRLN.js +1 -0
- package/dist/bundle/cynefinDiagram-L36ESFBY-eO6Z3Kvn.js +62 -0
- package/dist/bundle/cytoscape.esm-CsT4DLSP.js +321 -0
- package/dist/bundle/d3-hierarchy-i5r04UUG.js +1 -0
- package/dist/bundle/d3-scale-BEO6F4Sh.js +1 -0
- package/dist/bundle/d3-scale-chromatic-DhHjp77O.js +1 -0
- package/dist/bundle/d3-selection-CBFxn8wf.js +1 -0
- package/dist/bundle/d3-shape-v1Y4qR5e.js +1 -0
- package/dist/bundle/dagre-SPEB5RXH-ByBZqBaT.js +4 -0
- package/dist/bundle/diagram-3B7GL73J-BkQkIpxz.js +200 -0
- package/dist/bundle/diagram-DNEMPZML-DYthXzRv.js +24 -0
- package/dist/bundle/diagram-L3MUGCDN-D4DIzfHH.js +41 -0
- package/dist/bundle/diagram-LA4NIPWU-C2P-hZD1.js +24 -0
- package/dist/bundle/diagram-MHCOC4E2-BwauFnTV.js +3 -0
- package/dist/bundle/diagram-XPFHEONP-DC_v3Llc.js +30 -0
- package/dist/bundle/dist-CwuaKeLF.js +1 -0
- package/dist/bundle/ebnfDiagram-UZSYDL2S-CmwGlC8L.js +1 -0
- package/dist/bundle/elk-XV6XMXQE-CTlty3p3.js +27 -0
- package/dist/bundle/erDiagram-EYBAPDEH-Bu6phvJH.js +99 -0
- package/dist/bundle/eventmodeling-K75KTNOO-B1q0nOJE.js +1 -0
- package/dist/bundle/flowDiagram-MALGAFMO-D9D_AzXh.js +1 -0
- package/dist/bundle/ganttDiagram-23REXITY-COK7rxO4.js +292 -0
- package/dist/bundle/gitGraph-VSP46ZUC-DS7S_jKI.js +1 -0
- package/dist/bundle/gitGraphDiagram-STJD7YCW-DhVK8Hqh.js +106 -0
- package/dist/bundle/graphlib-Bgy7WbC7.js +1 -0
- package/dist/bundle/info-OHQRW6UA-BjEWvFlW.js +1 -0
- package/dist/bundle/infoDiagram-KBXO4MBG-Dg3zWdHP.js +2 -0
- package/dist/bundle/ishikawaDiagram-M35NCXHK-C2g8ghrV.js +70 -0
- package/dist/bundle/journeyDiagram-OPLOXDXO-DMZkL-i1.js +139 -0
- package/dist/bundle/kanban-definition-BZKZHSQY-Dr43IiJD.js +89 -0
- package/dist/bundle/katex-rAoShGnF.js +257 -0
- package/dist/bundle/math-Cx-Q-b8s.js +262 -0
- package/dist/bundle/mermaid-DaDuEHir.js +43 -0
- package/dist/bundle/mermaid-parser.core-Bv_Zx_ZE.js +6 -0
- package/dist/bundle/mindmap-definition-I2QAS2VK-BXCWJ_1R.js +98 -0
- package/dist/bundle/packet-JDAUHWVQ-CgB2qE5t.js +1 -0
- package/dist/bundle/pegDiagram-FD4XPPHP-CR58gOgF.js +1 -0
- package/dist/bundle/pie-XZMESJXO-CvXWTfIG.js +1 -0
- package/dist/bundle/pieDiagram-J4AKIIQI-D0GhbNJg.js +39 -0
- package/dist/bundle/quadrantDiagram-XTYBLID2-BKBEdwvC.js +7 -0
- package/dist/bundle/radar-ABXABTNO-8V_xx_AB.js +1 -0
- package/dist/bundle/railroad-I3PHUGI6-Bvb0GUva.js +1 -0
- package/dist/bundle/railroad-abnf-LNEFI6M7-D8xGcObq.js +1 -0
- package/dist/bundle/railroad-ebnf-DWYD2UWJ-Ch8wBawI.js +1 -0
- package/dist/bundle/railroad-peg-JR7BVNR7-CGndbjed.js +1 -0
- package/dist/bundle/railroadDiagram-MRQ6NCDL-DvqHkO2C.js +1 -0
- package/dist/bundle/requirementDiagram-Y7LWUY3S-bCLAM9sl.js +84 -0
- package/dist/bundle/rolldown-runtime-BNaE-Ejt.js +1 -0
- package/dist/bundle/rough.esm-Dy-Kn_BL.js +1 -0
- package/dist/bundle/sankeyDiagram-WGJFYUXA-8ehuPvHQ.js +40 -0
- package/dist/bundle/sequenceDiagram-5NDRBG5M-Cdg1YzMw.js +169 -0
- package/dist/bundle/sizeCapture-INFHLROL-Ct7XApxm.js +1 -0
- package/dist/bundle/stateDiagram-v2-FJ4FJNEA-B69v2YYC.js +291 -0
- package/dist/bundle/swimlanes-UMQ2AFYF-C_qI6rnK.js +1 -0
- package/dist/bundle/swimlanesDiagram-OMPLPG4O-CS11qSCX.js +8 -0
- package/dist/bundle/timeline-definition-SEUHKTQE-BCU0-H8D.js +120 -0
- package/dist/bundle/treeView-D4JQ5SDB-CpjURS-c.js +1 -0
- package/dist/bundle/treemap-SAJKECNS-CFFTaiIN.js +1 -0
- package/dist/bundle/usecaseDiagram-AFA6YXN4-46V1jsea.js +476 -0
- package/dist/bundle/vega-eBn1AexV.js +33 -0
- package/dist/bundle/vennDiagram-NNMJR3RB-q4Q4x2Q6.js +34 -0
- package/dist/bundle/wardley-7MLQ67FV-DhnATL7b.js +1 -0
- package/dist/bundle/wardleyDiagram-S7YFUSEH-BHIufnZW.js +78 -0
- package/dist/bundle/xychartDiagram-GGNKY7IX-CxAjpN3O.js +7 -0
- package/dist/chunks/a2ui-BLaPxJ9N.js +961 -0
- package/dist/chunks/a2ui-BLaPxJ9N.js.map +1 -0
- package/dist/chunks/math-D5IXRTov.js +86 -0
- package/dist/chunks/math-D5IXRTov.js.map +1 -0
- package/dist/chunks/mermaid-j9RNkOnL.js +197 -0
- package/dist/chunks/mermaid-j9RNkOnL.js.map +1 -0
- package/dist/chunks/rich-C2iN_5zh.js +156 -0
- package/dist/chunks/rich-C2iN_5zh.js.map +1 -0
- package/dist/chunks/sanitize-COLQzplk.js +240 -0
- package/dist/chunks/sanitize-COLQzplk.js.map +1 -0
- package/dist/chunks/{src-CYXMXds2.js → src-Blgo9nVC.js} +360 -68
- package/dist/chunks/src-Blgo9nVC.js.map +1 -0
- package/dist/chunks/vega-BkX1Fjq6.js +301 -0
- package/dist/chunks/vega-BkX1Fjq6.js.map +1 -0
- package/dist/define.js +1 -1
- package/dist/index.js +1 -1
- package/dist/types/element/i18n.d.ts +21 -2
- package/dist/types/element/markdown.d.ts +6 -3
- package/dist/types/element/rich/a2ui-format.d.ts +45 -0
- package/dist/types/element/rich/a2ui-icons.d.ts +1 -0
- package/dist/types/element/rich/a2ui-views.d.ts +37 -0
- package/dist/types/element/rich/a2ui.d.ts +2 -0
- package/dist/types/element/rich/index.d.ts +66 -0
- package/dist/types/element/rich/math.d.ts +4 -0
- package/dist/types/element/rich/mermaid.d.ts +2 -0
- package/dist/types/element/rich/vega.d.ts +2 -0
- package/dist/types/element/sanitize.d.ts +34 -0
- package/dist/types/element/styles.d.ts +1 -1
- package/package.json +9 -3
- package/dist/chunks/src-CYXMXds2.js.map +0 -1
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { i as sanitizeDrawing, n as dropSheet, r as safeSheet, t as adoptSheet } from "./sanitize-COLQzplk.js";
|
|
2
|
+
import mermaid from "mermaid";
|
|
3
|
+
// src/element/rich/mermaid.ts
|
|
4
|
+
var DARK = {
|
|
5
|
+
background: "#121315",
|
|
6
|
+
primaryColor: "#0E2A22",
|
|
7
|
+
primaryTextColor: "#E8E9EB",
|
|
8
|
+
primaryBorderColor: "#10B981",
|
|
9
|
+
lineColor: "#64748B",
|
|
10
|
+
textColor: "#E8E9EB",
|
|
11
|
+
mainBkg: "#18191C",
|
|
12
|
+
secondaryColor: "#0C2438",
|
|
13
|
+
secondaryBorderColor: "#38BDF8",
|
|
14
|
+
secondaryTextColor: "#F1F5F9",
|
|
15
|
+
tertiaryColor: "#2A1D0B",
|
|
16
|
+
tertiaryBorderColor: "#F59E0B",
|
|
17
|
+
tertiaryTextColor: "#FCD34D",
|
|
18
|
+
nodeBorder: "#2A2D32",
|
|
19
|
+
clusterBkg: "#18191C",
|
|
20
|
+
clusterBorder: "#2A3B57",
|
|
21
|
+
titleColor: "#F1F5F9",
|
|
22
|
+
edgeLabelBackground: "#111625",
|
|
23
|
+
fontSize: "13px",
|
|
24
|
+
actorBkg: "#111625",
|
|
25
|
+
actorBorder: "#38BDF8",
|
|
26
|
+
actorTextColor: "#F1F5F9",
|
|
27
|
+
actorLineColor: "#64748B",
|
|
28
|
+
signalColor: "#38BDF8",
|
|
29
|
+
signalTextColor: "#F1F5F9",
|
|
30
|
+
labelBoxBkgColor: "#111625",
|
|
31
|
+
labelBoxBorderColor: "#1E2638",
|
|
32
|
+
labelTextColor: "#F1F5F9",
|
|
33
|
+
loopTextColor: "#F1F5F9",
|
|
34
|
+
noteBkgColor: "#2A1D0B",
|
|
35
|
+
noteBorderColor: "#F59E0B",
|
|
36
|
+
noteTextColor: "#FCD34D",
|
|
37
|
+
activationBkgColor: "#0E2A22",
|
|
38
|
+
activationBorderColor: "#10B981",
|
|
39
|
+
git0: "#10B981",
|
|
40
|
+
git1: "#38BDF8",
|
|
41
|
+
git2: "#A855F7",
|
|
42
|
+
git3: "#F43F5E",
|
|
43
|
+
gitBranchLabel0: "#0B0F19",
|
|
44
|
+
gitBranchLabel1: "#0B0F19",
|
|
45
|
+
gitBranchLabel2: "#0B0F19",
|
|
46
|
+
gitBranchLabel3: "#0B0F19"
|
|
47
|
+
};
|
|
48
|
+
var LIGHT = {
|
|
49
|
+
background: "#FAFAFA",
|
|
50
|
+
primaryColor: "#ECFDF5",
|
|
51
|
+
primaryTextColor: "#16181D",
|
|
52
|
+
primaryBorderColor: "#059669",
|
|
53
|
+
lineColor: "#64748B",
|
|
54
|
+
textColor: "#16181D",
|
|
55
|
+
mainBkg: "#FFFFFF",
|
|
56
|
+
secondaryColor: "#F0F9FF",
|
|
57
|
+
secondaryBorderColor: "#0284C7",
|
|
58
|
+
secondaryTextColor: "#0F172A",
|
|
59
|
+
tertiaryColor: "#FFFBEB",
|
|
60
|
+
tertiaryBorderColor: "#D97706",
|
|
61
|
+
tertiaryTextColor: "#92400E",
|
|
62
|
+
nodeBorder: "#CDD1D7",
|
|
63
|
+
clusterBkg: "#F1F2F4",
|
|
64
|
+
clusterBorder: "#CBD5E1",
|
|
65
|
+
titleColor: "#0F172A",
|
|
66
|
+
edgeLabelBackground: "#FFFFFF",
|
|
67
|
+
fontSize: "13px",
|
|
68
|
+
actorBkg: "#FFFFFF",
|
|
69
|
+
actorBorder: "#0284C7",
|
|
70
|
+
actorTextColor: "#0F172A",
|
|
71
|
+
actorLineColor: "#64748B",
|
|
72
|
+
signalColor: "#0284C7",
|
|
73
|
+
signalTextColor: "#0F172A",
|
|
74
|
+
labelBoxBkgColor: "#FFFFFF",
|
|
75
|
+
labelBoxBorderColor: "#CBD5E1",
|
|
76
|
+
labelTextColor: "#0F172A",
|
|
77
|
+
loopTextColor: "#0F172A",
|
|
78
|
+
noteBkgColor: "#FFFBEB",
|
|
79
|
+
noteBorderColor: "#D97706",
|
|
80
|
+
noteTextColor: "#92400E",
|
|
81
|
+
activationBkgColor: "#D1FAE5",
|
|
82
|
+
activationBorderColor: "#059669",
|
|
83
|
+
git0: "#059669",
|
|
84
|
+
git1: "#0284C7",
|
|
85
|
+
git2: "#7C3AED",
|
|
86
|
+
git3: "#E11D48",
|
|
87
|
+
gitBranchLabel0: "#FFFFFF",
|
|
88
|
+
gitBranchLabel1: "#FFFFFF",
|
|
89
|
+
gitBranchLabel2: "#FFFFFF",
|
|
90
|
+
gitBranchLabel3: "#FFFFFF"
|
|
91
|
+
};
|
|
92
|
+
function configOf(env) {
|
|
93
|
+
return {
|
|
94
|
+
startOnLoad: false,
|
|
95
|
+
securityLevel: "strict",
|
|
96
|
+
theme: env.dark ? "dark" : "default",
|
|
97
|
+
themeVariables: {
|
|
98
|
+
...env.dark ? DARK : LIGHT,
|
|
99
|
+
fontFamily: env.font
|
|
100
|
+
},
|
|
101
|
+
suppressErrorRendering: true,
|
|
102
|
+
htmlLabels: true,
|
|
103
|
+
flowchart: {
|
|
104
|
+
useMaxWidth: true,
|
|
105
|
+
wrappingWidth: 220
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/** Most sources kept, drawn or refused: a streaming answer goes through many. */
|
|
110
|
+
var LIMIT = 64;
|
|
111
|
+
var drawn = /* @__PURE__ */ new Map();
|
|
112
|
+
var drawing = /* @__PURE__ */ new Map();
|
|
113
|
+
/** Mermaid has one configuration: one diagram at a time. */
|
|
114
|
+
var queue = Promise.resolve();
|
|
115
|
+
var counter = 0;
|
|
116
|
+
function remember(key, value) {
|
|
117
|
+
drawn.set(key, value);
|
|
118
|
+
if (drawn.size > LIMIT) drawn.delete(drawn.keys().next().value);
|
|
119
|
+
}
|
|
120
|
+
/** Mermaid cannot draw here: it writes `innerHTML` with strings, and the page
|
|
121
|
+
* enforces Trusted Types. Not tried again, so the page's CSP reports the
|
|
122
|
+
* refusal once, not for every diagram. */
|
|
123
|
+
var refused = false;
|
|
124
|
+
async function render(source, env) {
|
|
125
|
+
if (refused) return null;
|
|
126
|
+
try {
|
|
127
|
+
mermaid.initialize(configOf(env));
|
|
128
|
+
if (!await mermaid.parse(source, { suppressErrors: true })) return null;
|
|
129
|
+
await document.fonts?.ready;
|
|
130
|
+
const id = `aicore-mermaid-${++counter}`;
|
|
131
|
+
const { svg } = await mermaid.render(id, source);
|
|
132
|
+
return {
|
|
133
|
+
id,
|
|
134
|
+
svg
|
|
135
|
+
};
|
|
136
|
+
} catch (error) {
|
|
137
|
+
if (error instanceof TypeError && error.message.includes("TrustedHTML")) refused = true;
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
var sheets = /* @__PURE__ */ new Map();
|
|
142
|
+
var adopted = /* @__PURE__ */ new Map();
|
|
143
|
+
var pruning = false;
|
|
144
|
+
/** Drops, after the render that placed them, the stylesheets of diagrams no
|
|
145
|
+
* longer in their root. */
|
|
146
|
+
function prune() {
|
|
147
|
+
if (pruning) return;
|
|
148
|
+
pruning = true;
|
|
149
|
+
setTimeout(() => {
|
|
150
|
+
pruning = false;
|
|
151
|
+
for (const [root, ids] of adopted) for (const id of ids) {
|
|
152
|
+
const sheet = sheets.get(id);
|
|
153
|
+
if (!sheet || root.querySelector(`[id="${id}"]`)) continue;
|
|
154
|
+
dropSheet(root, sheet);
|
|
155
|
+
ids.delete(id);
|
|
156
|
+
}
|
|
157
|
+
}, 1e3);
|
|
158
|
+
}
|
|
159
|
+
function build(result, env) {
|
|
160
|
+
const { fragment, css } = sanitizeDrawing(result.svg);
|
|
161
|
+
if (!fragment.querySelector("svg")) return null;
|
|
162
|
+
if (!sheets.has(result.id)) sheets.set(result.id, safeSheet(css));
|
|
163
|
+
const sheet = sheets.get(result.id);
|
|
164
|
+
if (sheet) {
|
|
165
|
+
adoptSheet(env.root, sheet);
|
|
166
|
+
let ids = adopted.get(env.root);
|
|
167
|
+
if (!ids) adopted.set(env.root, ids = /* @__PURE__ */ new Set());
|
|
168
|
+
ids.add(result.id);
|
|
169
|
+
prune();
|
|
170
|
+
}
|
|
171
|
+
const box = document.createElement("div");
|
|
172
|
+
box.className = "diagram";
|
|
173
|
+
box.append(fragment);
|
|
174
|
+
return box;
|
|
175
|
+
}
|
|
176
|
+
function draw(source, env) {
|
|
177
|
+
const key = `${env.dark}\n${env.font}\n${source}`;
|
|
178
|
+
if (drawn.has(key)) {
|
|
179
|
+
const result = drawn.get(key);
|
|
180
|
+
return result ? build(result, env) : null;
|
|
181
|
+
}
|
|
182
|
+
let pending = drawing.get(key);
|
|
183
|
+
if (!pending) {
|
|
184
|
+
const next = queue.then(() => render(source, env));
|
|
185
|
+
queue = next;
|
|
186
|
+
pending = next.then((result) => {
|
|
187
|
+
drawing.delete(key);
|
|
188
|
+
remember(key, result);
|
|
189
|
+
return result;
|
|
190
|
+
});
|
|
191
|
+
drawing.set(key, pending);
|
|
192
|
+
}
|
|
193
|
+
return pending.then((result) => result ? build(result, env) : null);
|
|
194
|
+
}
|
|
195
|
+
export { draw };
|
|
196
|
+
|
|
197
|
+
//# sourceMappingURL=mermaid-j9RNkOnL.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mermaid-j9RNkOnL.js","names":[],"sources":["../../src/element/rich/mermaid.ts"],"sourcesContent":["import mermaid, { type MermaidConfig } from \"mermaid\";\n\nimport { adoptSheet, dropSheet, safeSheet, sanitizeDrawing } from \"../sanitize\";\nimport { type Env } from \"./index\";\n\n// ```mermaid fences, drawn as the console draws them (MessageWidgets.tsx):\n// `securityLevel: \"strict\"` (no click handlers, label HTML sanitized by\n// Mermaid itself), labels as HTML so long ones wrap, and the console's two\n// palettes, picked by the element's own colors. A block Mermaid cannot parse\n// stays as source: it is how a fence looks while it streams.\n//\n// Mermaid returns its SVG as markup, which goes through the sanitization of\n// every drawing: no external `url()`, no images, inline styles set through\n// CSSOM. The `<style>` it writes in each diagram (rules scoped to the\n// diagram's id) becomes a stylesheet of the shadow root, without anything\n// that loads a resource, and is dropped with the last copy of the diagram.\n//\n// Under Trusted Types Mermaid cannot draw: it writes `innerHTML` with\n// strings. The block then stays as source.\n\nconst DARK = {\n background: \"#121315\",\n primaryColor: \"#0E2A22\",\n primaryTextColor: \"#E8E9EB\",\n primaryBorderColor: \"#10B981\",\n lineColor: \"#64748B\",\n textColor: \"#E8E9EB\",\n mainBkg: \"#18191C\",\n secondaryColor: \"#0C2438\",\n secondaryBorderColor: \"#38BDF8\",\n secondaryTextColor: \"#F1F5F9\",\n tertiaryColor: \"#2A1D0B\",\n tertiaryBorderColor: \"#F59E0B\",\n tertiaryTextColor: \"#FCD34D\",\n nodeBorder: \"#2A2D32\",\n clusterBkg: \"#18191C\",\n clusterBorder: \"#2A3B57\",\n titleColor: \"#F1F5F9\",\n edgeLabelBackground: \"#111625\",\n fontSize: \"13px\",\n actorBkg: \"#111625\",\n actorBorder: \"#38BDF8\",\n actorTextColor: \"#F1F5F9\",\n actorLineColor: \"#64748B\",\n signalColor: \"#38BDF8\",\n signalTextColor: \"#F1F5F9\",\n labelBoxBkgColor: \"#111625\",\n labelBoxBorderColor: \"#1E2638\",\n labelTextColor: \"#F1F5F9\",\n loopTextColor: \"#F1F5F9\",\n noteBkgColor: \"#2A1D0B\",\n noteBorderColor: \"#F59E0B\",\n noteTextColor: \"#FCD34D\",\n activationBkgColor: \"#0E2A22\",\n activationBorderColor: \"#10B981\",\n git0: \"#10B981\",\n git1: \"#38BDF8\",\n git2: \"#A855F7\",\n git3: \"#F43F5E\",\n gitBranchLabel0: \"#0B0F19\",\n gitBranchLabel1: \"#0B0F19\",\n gitBranchLabel2: \"#0B0F19\",\n gitBranchLabel3: \"#0B0F19\",\n};\n\nconst LIGHT = {\n background: \"#FAFAFA\",\n primaryColor: \"#ECFDF5\",\n primaryTextColor: \"#16181D\",\n primaryBorderColor: \"#059669\",\n lineColor: \"#64748B\",\n textColor: \"#16181D\",\n mainBkg: \"#FFFFFF\",\n secondaryColor: \"#F0F9FF\",\n secondaryBorderColor: \"#0284C7\",\n secondaryTextColor: \"#0F172A\",\n tertiaryColor: \"#FFFBEB\",\n tertiaryBorderColor: \"#D97706\",\n tertiaryTextColor: \"#92400E\",\n nodeBorder: \"#CDD1D7\",\n clusterBkg: \"#F1F2F4\",\n clusterBorder: \"#CBD5E1\",\n titleColor: \"#0F172A\",\n edgeLabelBackground: \"#FFFFFF\",\n fontSize: \"13px\",\n actorBkg: \"#FFFFFF\",\n actorBorder: \"#0284C7\",\n actorTextColor: \"#0F172A\",\n actorLineColor: \"#64748B\",\n signalColor: \"#0284C7\",\n signalTextColor: \"#0F172A\",\n labelBoxBkgColor: \"#FFFFFF\",\n labelBoxBorderColor: \"#CBD5E1\",\n labelTextColor: \"#0F172A\",\n loopTextColor: \"#0F172A\",\n noteBkgColor: \"#FFFBEB\",\n noteBorderColor: \"#D97706\",\n noteTextColor: \"#92400E\",\n activationBkgColor: \"#D1FAE5\",\n activationBorderColor: \"#059669\",\n git0: \"#059669\",\n git1: \"#0284C7\",\n git2: \"#7C3AED\",\n git3: \"#E11D48\",\n gitBranchLabel0: \"#FFFFFF\",\n gitBranchLabel1: \"#FFFFFF\",\n gitBranchLabel2: \"#FFFFFF\",\n gitBranchLabel3: \"#FFFFFF\",\n};\n\nfunction configOf(env: Env): MermaidConfig {\n return {\n startOnLoad: false,\n securityLevel: \"strict\",\n theme: env.dark ? \"dark\" : \"default\",\n themeVariables: { ...(env.dark ? DARK : LIGHT), fontFamily: env.font },\n // Never Mermaid's own \"Syntax error\" drawing: the source shows instead.\n suppressErrorRendering: true,\n htmlLabels: true,\n flowchart: { useMaxWidth: true, wrappingWidth: 220 },\n };\n}\n\ninterface Drawn {\n id: string;\n svg: string;\n}\n\n/** Most sources kept, drawn or refused: a streaming answer goes through many. */\nconst LIMIT = 64;\nconst drawn = new Map<string, Drawn | null>();\nconst drawing = new Map<string, Promise<Drawn | null>>();\n/** Mermaid has one configuration: one diagram at a time. */\nlet queue: Promise<unknown> = Promise.resolve();\nlet counter = 0;\n\nfunction remember(key: string, value: Drawn | null): void {\n drawn.set(key, value);\n if (drawn.size > LIMIT) drawn.delete(drawn.keys().next().value!);\n}\n\n/** Mermaid cannot draw here: it writes `innerHTML` with strings, and the page\n * enforces Trusted Types. Not tried again, so the page's CSP reports the\n * refusal once, not for every diagram. */\nlet refused = false;\n\nasync function render(source: string, env: Env): Promise<Drawn | null> {\n if (refused) return null;\n try {\n mermaid.initialize(configOf(env));\n if (!(await mermaid.parse(source, { suppressErrors: true }))) return null;\n // Mermaid measures each label to wrap it: with the page's fonts loaded.\n await document.fonts?.ready;\n const id = `aicore-mermaid-${++counter}`;\n const { svg } = await mermaid.render(id, source);\n return { id, svg };\n } catch (error) {\n if (error instanceof TypeError && error.message.includes(\"TrustedHTML\"))\n refused = true;\n return null;\n }\n}\n\nconst sheets = new Map<string, CSSStyleSheet | null>();\nconst adopted = new Map<Document | ShadowRoot, Set<string>>();\nlet pruning = false;\n\n/** Drops, after the render that placed them, the stylesheets of diagrams no\n * longer in their root. */\nfunction prune(): void {\n if (pruning) return;\n pruning = true;\n setTimeout(() => {\n pruning = false;\n for (const [root, ids] of adopted)\n for (const id of ids) {\n const sheet = sheets.get(id);\n if (!sheet || root.querySelector(`[id=\"${id}\"]`)) continue;\n dropSheet(root, sheet);\n ids.delete(id);\n }\n }, 1000);\n}\n\nfunction build(result: Drawn, env: Env): Element | null {\n const { fragment, css } = sanitizeDrawing(result.svg);\n if (!fragment.querySelector(\"svg\")) return null;\n if (!sheets.has(result.id)) sheets.set(result.id, safeSheet(css));\n const sheet = sheets.get(result.id);\n if (sheet) {\n adoptSheet(env.root, sheet);\n let ids = adopted.get(env.root);\n if (!ids) adopted.set(env.root, (ids = new Set()));\n ids.add(result.id);\n prune();\n }\n const box = document.createElement(\"div\");\n box.className = \"diagram\";\n box.append(fragment);\n return box;\n}\n\nexport function draw(\n source: string,\n env: Env,\n): Element | null | Promise<Element | null> {\n const key = `${env.dark}\\n${env.font}\\n${source}`;\n if (drawn.has(key)) {\n const result = drawn.get(key);\n return result ? build(result, env) : null;\n }\n let pending = drawing.get(key);\n if (!pending) {\n const next = queue.then(() => render(source, env));\n queue = next;\n pending = next.then((result) => {\n drawing.delete(key);\n remember(key, result);\n return result;\n });\n drawing.set(key, pending);\n }\n return pending.then((result) => (result ? build(result, env) : null));\n}\n"],"mappings":";;;AAoBA,IAAM,OAAO;CACX,YAAY;CACZ,cAAc;CACd,kBAAkB;CAClB,oBAAoB;CACpB,WAAW;CACX,WAAW;CACX,SAAS;CACT,gBAAgB;CAChB,sBAAsB;CACtB,oBAAoB;CACpB,eAAe;CACf,qBAAqB;CACrB,mBAAmB;CACnB,YAAY;CACZ,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O,IAAI,OAAO,EAAE,GAAG,OAAO,IAAI,OAAO,IAAI,UAAU,GAAG,CAAC;CAChE,MAAM,QAAQ,OAAO,IAAI,OAAO,EAAE;CAClC,IAAI,OAAO;EACT,WAAW,IAAI,MAAM,KAAK;EAC1B,IAAI,MAAM,QAAQ,IAAI,IAAI,IAAI;EAC9B,IAAI,CAAC,KAAK,QAAQ,IAAI,IAAI,MAAO,sBAAM,IAAI,IAAI,CAAE;EACjD,IAAI,IAAI,OAAO,EAAE;EACjB,MAAM;CACR;CACA,MAAM,MAAM,SAAS,cAAc,KAAK;CACxC,IAAI,YAAY;CAChB,IAAI,OAAO,QAAQ;CACnB,OAAO;AACT;AAEA,SAAgB,KACd,QACA,KAC0C;CAC1C,MAAM,MAAM,GAAG,IAAI,KAAK,IAAI,IAAI,KAAK,IAAI;CACzC,IAAI,MAAM,IAAI,GAAG,GAAG;EAClB,MAAM,SAAS,MAAM,IAAI,GAAG;EAC5B,OAAO,SAAS,MAAM,QAAQ,GAAG,IAAI;CACvC;CACA,IAAI,UAAU,QAAQ,IAAI,GAAG;CAC7B,IAAI,CAAC,SAAS;EACZ,MAAM,OAAO,MAAM,WAAW,OAAO,QAAQ,GAAG,CAAC;EACjD,QAAQ;EACR,UAAU,KAAK,MAAM,WAAW;GAC9B,QAAQ,OAAO,GAAG;GAClB,SAAS,KAAK,MAAM;GACpB,OAAO;EACT,CAAC;EACD,QAAQ,IAAI,KAAK,OAAO;CAC1B;CACA,OAAO,QAAQ,MAAM,WAAY,SAAS,MAAM,QAAQ,GAAG,IAAI,IAAK;AACtE"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// src/element/rich/index.ts
|
|
2
|
+
/** The fences each renderer draws, by the class marked gives their code. */
|
|
3
|
+
function kindOf(code) {
|
|
4
|
+
const classes = code.classList;
|
|
5
|
+
if (classes.contains("language-mermaid")) return "mermaid";
|
|
6
|
+
if (classes.contains("language-vega-lite") || classes.contains("language-vegalite")) return "vega";
|
|
7
|
+
if (classes.contains("language-a2ui")) return "a2ui";
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
/** A module loaded once, on first use; a failed load is tried again. */
|
|
11
|
+
function lazy(importer) {
|
|
12
|
+
let value = null;
|
|
13
|
+
let loading = null;
|
|
14
|
+
return {
|
|
15
|
+
get: () => value,
|
|
16
|
+
load: () => loading ??= importer().then((module) => value = module, (error) => {
|
|
17
|
+
loading = null;
|
|
18
|
+
throw error;
|
|
19
|
+
})
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
var math = lazy(() => import("./math-D5IXRTov.js"));
|
|
23
|
+
var drawers = {
|
|
24
|
+
mermaid: lazy(() => import("./mermaid-j9RNkOnL.js")),
|
|
25
|
+
vega: lazy(() => import("./vega-BkX1Fjq6.js")),
|
|
26
|
+
a2ui: lazy(() => import("./a2ui-BLaPxJ9N.js"))
|
|
27
|
+
};
|
|
28
|
+
var DRAWING = "rich-drawing";
|
|
29
|
+
/** What each drawing was drawn from, to reuse it in the next render. */
|
|
30
|
+
var keys = /* @__PURE__ */ new WeakMap();
|
|
31
|
+
/** What frees a drawing's resources (a chart's view) when it is dropped. */
|
|
32
|
+
var disposers = /* @__PURE__ */ new WeakMap();
|
|
33
|
+
/** How each interface takes a new state. */
|
|
34
|
+
var interfaces = /* @__PURE__ */ new WeakMap();
|
|
35
|
+
/** Gives the interfaces in `container` their new state. */
|
|
36
|
+
function updateInterfaces(container, state) {
|
|
37
|
+
for (const node of container.querySelectorAll(`.${DRAWING}`)) interfaces.get(node)?.(state);
|
|
38
|
+
}
|
|
39
|
+
/** Frees the resources of the drawings in `container`, about to go. */
|
|
40
|
+
function disposeDrawings(container) {
|
|
41
|
+
for (const node of container.querySelectorAll(`.${DRAWING}`)) disposers.get(node)?.();
|
|
42
|
+
}
|
|
43
|
+
/** The drawings in `container`, by what they were drawn from: taken before a
|
|
44
|
+
* new render of it, which puts back the ones it still has. */
|
|
45
|
+
function keepDrawings(container) {
|
|
46
|
+
const kept = /* @__PURE__ */ new Map();
|
|
47
|
+
for (const node of container.querySelectorAll(`.${DRAWING}`)) {
|
|
48
|
+
const key = keys.get(node);
|
|
49
|
+
if (key === void 0) continue;
|
|
50
|
+
const list = kept.get(key);
|
|
51
|
+
if (list) list.push(node);
|
|
52
|
+
else kept.set(key, [node]);
|
|
53
|
+
}
|
|
54
|
+
return kept;
|
|
55
|
+
}
|
|
56
|
+
function envOf(options) {
|
|
57
|
+
const style = getComputedStyle(options.host);
|
|
58
|
+
return {
|
|
59
|
+
host: options.host,
|
|
60
|
+
root: options.host.shadowRoot ?? document,
|
|
61
|
+
dark: isLight(style.color),
|
|
62
|
+
font: style.fontFamily,
|
|
63
|
+
lang: options.lang,
|
|
64
|
+
live: options.live ?? false,
|
|
65
|
+
strings: options.strings,
|
|
66
|
+
interface: options.interface
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** `color`, a computed CSS color, as sRGB 0-255. Browsers keep the space a
|
|
70
|
+
* color was written in (`oklch(…)`, `lab(…)`, `color(…)`): those are read
|
|
71
|
+
* back from a pixel painted with them. Null when the color cannot be read. */
|
|
72
|
+
function rgbOf(color) {
|
|
73
|
+
const rgb = /^rgba?\(\s*([\d.]+)[\s,]+([\d.]+)[\s,]+([\d.]+)/.exec(color);
|
|
74
|
+
if (rgb) return [
|
|
75
|
+
Number(rgb[1]),
|
|
76
|
+
Number(rgb[2]),
|
|
77
|
+
Number(rgb[3])
|
|
78
|
+
];
|
|
79
|
+
try {
|
|
80
|
+
const canvas = document.createElement("canvas");
|
|
81
|
+
canvas.width = canvas.height = 1;
|
|
82
|
+
const context = canvas.getContext("2d", { willReadFrequently: true });
|
|
83
|
+
if (!context) return null;
|
|
84
|
+
context.fillStyle = color;
|
|
85
|
+
context.fillRect(0, 0, 1, 1);
|
|
86
|
+
const [r = 0, g = 0, b = 0] = context.getImageData(0, 0, 1, 1).data;
|
|
87
|
+
return [
|
|
88
|
+
r,
|
|
89
|
+
g,
|
|
90
|
+
b
|
|
91
|
+
];
|
|
92
|
+
} catch {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/** Whether `color` is light: light text is a dark theme. */
|
|
97
|
+
function isLight(color) {
|
|
98
|
+
const [r, g, b] = rgbOf(color) ?? [
|
|
99
|
+
0,
|
|
100
|
+
0,
|
|
101
|
+
0
|
|
102
|
+
];
|
|
103
|
+
return .2126 * r + .7152 * g + .0722 * b > 140;
|
|
104
|
+
}
|
|
105
|
+
/** Draws what `container`'s markdown left as source. `kept`: the drawings of
|
|
106
|
+
* its previous render, reused when their source is still there. */
|
|
107
|
+
function enhance(container, options, kept = /* @__PURE__ */ new Map()) {
|
|
108
|
+
if (container.querySelector(".math-inline, .math-display")) {
|
|
109
|
+
const draw = () => {
|
|
110
|
+
math.get()?.renderFormulas(container, options.host);
|
|
111
|
+
};
|
|
112
|
+
if (math.get()) draw();
|
|
113
|
+
else math.load().then(draw, () => void 0);
|
|
114
|
+
}
|
|
115
|
+
if (options.widgets) {
|
|
116
|
+
let env = null;
|
|
117
|
+
for (const code of container.querySelectorAll("pre > code")) {
|
|
118
|
+
const kind = kindOf(code);
|
|
119
|
+
const pre = code.parentElement;
|
|
120
|
+
if (!kind || !pre) continue;
|
|
121
|
+
if (options.live && kind !== "a2ui" && pre.hasAttribute("data-open")) continue;
|
|
122
|
+
const source = (code.textContent ?? "").replace(/\n$/, "");
|
|
123
|
+
env ??= envOf(options);
|
|
124
|
+
const key = `${kind}\n${env.dark}\n${source}`;
|
|
125
|
+
const reused = kept.get(key)?.shift();
|
|
126
|
+
if (reused) {
|
|
127
|
+
pre.replaceWith(reused);
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const drawer = drawers[kind];
|
|
131
|
+
const ready = drawer.get();
|
|
132
|
+
const at = env;
|
|
133
|
+
if (ready) place(pre, ready.draw(source, at), key);
|
|
134
|
+
else drawer.load().then((loaded) => {
|
|
135
|
+
if (pre.parentNode) place(pre, loaded.draw(source, at), key);
|
|
136
|
+
}, () => void 0);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
for (const nodes of kept.values()) for (const node of nodes) disposers.get(node)?.();
|
|
140
|
+
}
|
|
141
|
+
function place(pre, drawing, key) {
|
|
142
|
+
const put = (node) => {
|
|
143
|
+
if (!node || !pre.parentNode) {
|
|
144
|
+
if (node) disposers.get(node)?.();
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
node.classList.add(DRAWING);
|
|
148
|
+
keys.set(node, key);
|
|
149
|
+
pre.replaceWith(node);
|
|
150
|
+
};
|
|
151
|
+
if (drawing instanceof Promise) drawing.then(put, () => void 0);
|
|
152
|
+
else put(drawing);
|
|
153
|
+
}
|
|
154
|
+
export { keepDrawings as a, interfaces as i, disposers as n, updateInterfaces as o, enhance as r, disposeDrawings as t };
|
|
155
|
+
|
|
156
|
+
//# sourceMappingURL=rich-C2iN_5zh.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rich-C2iN_5zh.js","names":[],"sources":["../../src/element/rich/index.ts"],"sourcesContent":["// What the markdown leaves as source and the console draws: formulas\n// (KaTeX), and in an answer the fenced blocks of diagrams (```mermaid),\n// charts (```vega-lite) and interfaces (```a2ui). Each renderer is loaded\n// with `import()` by the first answer that needs it, so the element's own\n// chunk, and the standalone bundle, carry none of them. Until a renderer is\n// loaded, and whenever it cannot draw a block (a fence still streaming, a\n// syntax error), the block stays as the markdown left it: its source.\n//\n// What a renderer draws goes through the same sanitization as the markdown\n// (`../sanitize.ts`), and works under a strict CSP and Trusted Types.\n\nimport { type Strings } from \"../i18n\";\nimport { type A2uiAction } from \"./a2ui-format\";\n\nexport interface RichOptions {\n /** The element: its shadow root takes the renderers' styles, its colors\n * and font set the theme of diagrams and charts. */\n host: HTMLElement;\n /** Diagrams, charts and interfaces besides formulas: an answer has them,\n * a cited passage shows its fences as code. */\n widgets: boolean;\n /** The element's language, for the charts' number and date formats. */\n lang: string;\n /** The answer is still streaming: a fence not closed yet stays as source,\n * instead of being drawn again at each part of it. */\n live?: boolean;\n /** The element's texts. */\n strings: Strings;\n /** What an interface (```a2ui) in this answer can do now. */\n interface?: InterfaceState;\n}\n\n/** What an interface in an answer needs from the conversation around it. */\nexport interface InterfaceState {\n /** Sends the user's click as their next message, and says whether it left;\n * absent while it cannot (then the interface shows, and does nothing). */\n send?: (text: string) => boolean;\n /** A turn is running. */\n working: boolean;\n /** The surfaces the user answered later in the conversation, each with its\n * answer: shown as sent, with the values sent. */\n sent: ReadonlyMap<string, A2uiAction>;\n}\n\n/** How a drawing is themed: from the element's own colors and font. */\nexport interface Env {\n host: HTMLElement;\n root: Document | ShadowRoot;\n dark: boolean;\n font: string;\n lang: string;\n live: boolean;\n strings: Strings;\n interface?: InterfaceState;\n}\n\n/** A renderer of fenced blocks. */\nexport interface Drawer {\n /** The drawing of `source`, or null when it cannot be drawn: the block\n * stays as source. Synchronous when it can be, so a re-render of the same\n * answer does not flash the source. */\n draw(source: string, env: Env): Element | null | Promise<Element | null>;\n}\n\ntype Kind = \"mermaid\" | \"vega\" | \"a2ui\";\n\n/** The fences each renderer draws, by the class marked gives their code. */\nfunction kindOf(code: Element): Kind | null {\n const classes = code.classList;\n if (classes.contains(\"language-mermaid\")) return \"mermaid\";\n if (\n classes.contains(\"language-vega-lite\") ||\n classes.contains(\"language-vegalite\")\n )\n return \"vega\";\n if (classes.contains(\"language-a2ui\")) return \"a2ui\";\n return null;\n}\n\ninterface Lazy<T> {\n get(): T | null;\n load(): Promise<T>;\n}\n\n/** A module loaded once, on first use; a failed load is tried again. */\nfunction lazy<T>(importer: () => Promise<T>): Lazy<T> {\n let value: T | null = null;\n let loading: Promise<T> | null = null;\n return {\n get: () => value,\n load: () =>\n (loading ??= importer().then(\n (module) => (value = module),\n (error: unknown) => {\n loading = null;\n throw error;\n },\n )),\n };\n}\n\nconst math = lazy(() => import(\"./math\"));\nconst drawers: Record<Kind, Lazy<Drawer>> = {\n mermaid: lazy(() => import(\"./mermaid\")),\n vega: lazy(() => import(\"./vega\")),\n a2ui: lazy(() => import(\"./a2ui\")),\n};\n\nconst DRAWING = \"rich-drawing\";\n/** What each drawing was drawn from, to reuse it in the next render. */\nconst keys = new WeakMap<Element, string>();\n/** What frees a drawing's resources (a chart's view) when it is dropped. */\nexport const disposers = new WeakMap<Element, () => void>();\n/** How each interface takes a new state. */\nexport const interfaces = new WeakMap<\n Element,\n (state: InterfaceState) => void\n>();\n\n/** Gives the interfaces in `container` their new state. */\nexport function updateInterfaces(\n container: Element,\n state: InterfaceState,\n): void {\n for (const node of container.querySelectorAll(`.${DRAWING}`))\n interfaces.get(node)?.(state);\n}\n\n/** Frees the resources of the drawings in `container`, about to go. */\nexport function disposeDrawings(container: Element): void {\n for (const node of container.querySelectorAll(`.${DRAWING}`))\n disposers.get(node)?.();\n}\n\n/** The drawings in `container`, by what they were drawn from: taken before a\n * new render of it, which puts back the ones it still has. */\nexport function keepDrawings(container: Element): Map<string, Element[]> {\n const kept = new Map<string, Element[]>();\n for (const node of container.querySelectorAll(`.${DRAWING}`)) {\n const key = keys.get(node);\n if (key === undefined) continue;\n const list = kept.get(key);\n if (list) list.push(node);\n else kept.set(key, [node]);\n }\n return kept;\n}\n\nfunction envOf(options: RichOptions): Env {\n const style = getComputedStyle(options.host);\n return {\n host: options.host,\n root: options.host.shadowRoot ?? document,\n dark: isLight(style.color),\n font: style.fontFamily,\n lang: options.lang,\n live: options.live ?? false,\n strings: options.strings,\n interface: options.interface,\n };\n}\n\n/** `color`, a computed CSS color, as sRGB 0-255. Browsers keep the space a\n * color was written in (`oklch(…)`, `lab(…)`, `color(…)`): those are read\n * back from a pixel painted with them. Null when the color cannot be read. */\nexport function rgbOf(color: string): [number, number, number] | null {\n const rgb = /^rgba?\\(\\s*([\\d.]+)[\\s,]+([\\d.]+)[\\s,]+([\\d.]+)/.exec(color);\n if (rgb) return [Number(rgb[1]), Number(rgb[2]), Number(rgb[3])];\n try {\n const canvas = document.createElement(\"canvas\");\n canvas.width = canvas.height = 1;\n const context = canvas.getContext(\"2d\", { willReadFrequently: true });\n if (!context) return null;\n context.fillStyle = color;\n context.fillRect(0, 0, 1, 1);\n const [r = 0, g = 0, b = 0] = context.getImageData(0, 0, 1, 1).data;\n return [r, g, b];\n } catch {\n return null;\n }\n}\n\n/** Whether `color` is light: light text is a dark theme. */\nfunction isLight(color: string): boolean {\n const [r, g, b] = rgbOf(color) ?? [0, 0, 0];\n return 0.2126 * r + 0.7152 * g + 0.0722 * b > 140;\n}\n\n/** Draws what `container`'s markdown left as source. `kept`: the drawings of\n * its previous render, reused when their source is still there. */\nexport function enhance(\n container: Element,\n options: RichOptions,\n kept = new Map<string, Element[]>(),\n): void {\n const formulas = container.querySelector(\".math-inline, .math-display\");\n if (formulas) {\n const draw = () => {\n math.get()?.renderFormulas(container, options.host);\n };\n if (math.get()) draw();\n else void math.load().then(draw, () => undefined);\n }\n\n if (options.widgets) {\n let env: Env | null = null;\n for (const code of container.querySelectorAll(\"pre > code\")) {\n const kind = kindOf(code);\n const pre = code.parentElement;\n if (!kind || !pre) continue;\n // An interface shows a placeholder until it is complete instead.\n if (options.live && kind !== \"a2ui\" && pre.hasAttribute(\"data-open\"))\n continue;\n const source = (code.textContent ?? \"\").replace(/\\n$/, \"\");\n env ??= envOf(options);\n const key = `${kind}\\n${env.dark}\\n${source}`;\n const reused = kept.get(key)?.shift();\n if (reused) {\n pre.replaceWith(reused);\n continue;\n }\n const drawer = drawers[kind];\n const ready = drawer.get();\n const at = env;\n if (ready) place(pre, ready.draw(source, at), key);\n else\n void drawer.load().then(\n (loaded) => {\n if (pre.parentNode) place(pre, loaded.draw(source, at), key);\n },\n () => undefined,\n );\n }\n }\n\n for (const nodes of kept.values())\n for (const node of nodes) disposers.get(node)?.();\n}\n\nfunction place(\n pre: Element,\n drawing: Element | null | Promise<Element | null>,\n key: string,\n): void {\n const put = (node: Element | null) => {\n // The block may have gone meanwhile: a newer render of the answer.\n if (!node || !pre.parentNode) {\n if (node) disposers.get(node)?.();\n return;\n }\n node.classList.add(DRAWING);\n keys.set(node, key);\n pre.replaceWith(node);\n };\n if (drawing instanceof Promise) void drawing.then(put, () => undefined);\n else put(drawing);\n}\n"],"mappings":";;AAmEA,SAAS,OAAO,MAA4B;CAC1C,MAAM,UAAU,KAAK;CACrB,IAAI,QAAQ,SAAS,kBAAkB,GAAG,OAAO;CACjD,IACE,QAAQ,SAAS,oBAAoB,KACrC,QAAQ,SAAS,mBAAmB,GAEpC,OAAO;CACT,IAAI,QAAQ,SAAS,eAAe,GAAG,OAAO;CAC9C,OAAO;AACT;;AAQA,SAAS,KAAQ,UAAqC;CACpD,IAAI,QAAkB;CACtB,IAAI,UAA6B;CACjC,OAAO;EACL,WAAW;EACX,YACG,YAAY,SAAS,CAAC,CAAC,MACrB,WAAY,QAAQ,SACpB,UAAmB;GAClB,UAAU;GACV,MAAM;EACR,CACF;CACJ;AACF;AAEA,IAAM,OAAO,WAAW,OAAO,qBAAS;AACxC,IAAM,UAAsC;CAC1C,SAAS,WAAW,OAAO,wBAAY;CACvC,MAAM,WAAW,OAAO,qBAAS;CACjC,MAAM,WAAW,OAAO,qBAAS;AACnC;AAEA,IAAM,UAAU;;AAEhB,IAAM,uBAAO,IAAI,QAAyB;;AAE1C,IAAa,4BAAY,IAAI,QAA6B;;AAE1D,IAAa,6BAAa,IAAI,QAG5B;;AAGF,SAAgB,iBACd,WACA,OACM;CACN,KAAK,MAAM,QAAQ,UAAU,iBAAiB,IAAI,SAAS,GACzD,WAAW,IAAI,IAAI,CAAC,GAAG,KAAK;AAChC;;AAGA,SAAgB,gBAAgB,WAA0B;CACxD,KAAK,MAAM,QAAQ,UAAU,iBAAiB,IAAI,SAAS,GACzD,UAAU,IAAI,IAAI,CAAC,GAAG;AAC1B;;;AAIA,SAAgB,aAAa,WAA4C;CACvE,MAAM,uBAAO,IAAI,IAAuB;CACxC,KAAK,MAAM,QAAQ,UAAU,iBAAiB,IAAI,SAAS,GAAG;EAC5D,M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IAAI,YAAY;GAC5B,IAAI,MAAM,UAAU,IAAI,IAAI,CAAC,GAAG;GAChC;EACF;EACA,KAAK,UAAU,IAAI,OAAO;EAC1B,KAAK,IAAI,MAAM,GAAG;EAClB,IAAI,YAAY,IAAI;CACtB;CACA,IAAI,mBAAmB,SAAS,QAAa,KAAK,WAAW,KAAA,CAAS;MACjE,IAAI,OAAO;AAClB"}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import createDOMPurify from "dompurify";
|
|
2
|
+
// src/element/sanitize.ts
|
|
3
|
+
var policy;
|
|
4
|
+
/** The `aicore-agent-chat` Trusted Types policy. Null where the browser has
|
|
5
|
+
* none, or the host's CSP refuses the name: then HTML goes in as a string. */
|
|
6
|
+
function policyOf() {
|
|
7
|
+
if (policy !== void 0) return policy;
|
|
8
|
+
const factory = globalThis.trustedTypes;
|
|
9
|
+
try {
|
|
10
|
+
policy = factory?.createPolicy("aicore-agent-chat", {
|
|
11
|
+
createHTML: (input) => input,
|
|
12
|
+
createScriptURL: () => ""
|
|
13
|
+
}) ?? null;
|
|
14
|
+
} catch {
|
|
15
|
+
policy = null;
|
|
16
|
+
}
|
|
17
|
+
return policy;
|
|
18
|
+
}
|
|
19
|
+
/** Tags no answer may carry, in any pass: what loads something by itself,
|
|
20
|
+
* styles, forms. */
|
|
21
|
+
var FORBID_TAGS = [
|
|
22
|
+
"img",
|
|
23
|
+
"image",
|
|
24
|
+
"feImage",
|
|
25
|
+
"picture",
|
|
26
|
+
"video",
|
|
27
|
+
"audio",
|
|
28
|
+
"source",
|
|
29
|
+
"track",
|
|
30
|
+
"iframe",
|
|
31
|
+
"frame",
|
|
32
|
+
"object",
|
|
33
|
+
"embed",
|
|
34
|
+
"link",
|
|
35
|
+
"meta",
|
|
36
|
+
"base",
|
|
37
|
+
"style",
|
|
38
|
+
"form",
|
|
39
|
+
"input",
|
|
40
|
+
"button",
|
|
41
|
+
"textarea",
|
|
42
|
+
"select"
|
|
43
|
+
];
|
|
44
|
+
/** Attributes that load something, or send something, by themselves. */
|
|
45
|
+
var LOADING_ATTRS = /* @__PURE__ */ new Set([
|
|
46
|
+
"src",
|
|
47
|
+
"srcset",
|
|
48
|
+
"poster",
|
|
49
|
+
"background",
|
|
50
|
+
"lowsrc",
|
|
51
|
+
"dynsrc",
|
|
52
|
+
"data",
|
|
53
|
+
"ping",
|
|
54
|
+
"action",
|
|
55
|
+
"formaction"
|
|
56
|
+
]);
|
|
57
|
+
/** Attributes whose value is text or a link, never CSS: a `url(` in them is
|
|
58
|
+
* words, as in a link to a page about CSS. */
|
|
59
|
+
var TEXT_ATTRS = /* @__PURE__ */ new Set([
|
|
60
|
+
"href",
|
|
61
|
+
"xlink:href",
|
|
62
|
+
"title",
|
|
63
|
+
"alt"
|
|
64
|
+
]);
|
|
65
|
+
var MARKDOWN = {
|
|
66
|
+
FORBID_TAGS,
|
|
67
|
+
FORBID_ATTR: ["style"]
|
|
68
|
+
};
|
|
69
|
+
/** What a renderer draws: HTML, SVG (with HTML labels in `foreignObject`, as
|
|
70
|
+
* Mermaid writes them) and MathML (KaTeX's accessible copy of a formula). */
|
|
71
|
+
var DRAWING = {
|
|
72
|
+
USE_PROFILES: {
|
|
73
|
+
html: true,
|
|
74
|
+
svg: true,
|
|
75
|
+
svgFilters: true,
|
|
76
|
+
mathMl: true
|
|
77
|
+
},
|
|
78
|
+
ADD_TAGS: [
|
|
79
|
+
"foreignObject",
|
|
80
|
+
"semantics",
|
|
81
|
+
"annotation"
|
|
82
|
+
],
|
|
83
|
+
HTML_INTEGRATION_POINTS: { foreignobject: true },
|
|
84
|
+
FORBID_TAGS
|
|
85
|
+
};
|
|
86
|
+
/** A `url()` that is not a reference inside the same document (`url(#id)`). */
|
|
87
|
+
var REMOTE_URL = /url\(\s*(?!['"]?#)/i;
|
|
88
|
+
/** CSS functions that load something with no `url()` around it. */
|
|
89
|
+
var REMOTE_CSS = /(?:image-set|image|cross-fade|element)\(/i;
|
|
90
|
+
/** The pass under way: its inline styles and `<style>` texts, taken out. */
|
|
91
|
+
var current = null;
|
|
92
|
+
var purifier = null;
|
|
93
|
+
/** A DOMPurify of our own: a hook on the shared instance would also change
|
|
94
|
+
* how the host app sanitizes. */
|
|
95
|
+
function purifierOf() {
|
|
96
|
+
if (purifier) return purifier;
|
|
97
|
+
purifier = createDOMPurify(window);
|
|
98
|
+
purifier.addHook("uponSanitizeElement", (node, data) => {
|
|
99
|
+
if (current && data.tagName === "style") current.sheets.push(node.textContent ?? "");
|
|
100
|
+
});
|
|
101
|
+
purifier.addHook("uponSanitizeAttribute", (node, data) => {
|
|
102
|
+
const name = data.attrName;
|
|
103
|
+
const value = data.attrValue;
|
|
104
|
+
if (name === "style") {
|
|
105
|
+
if (current) current.styles.set(node, value);
|
|
106
|
+
data.keepAttr = false;
|
|
107
|
+
} else if (LOADING_ATTRS.has(name)) data.keepAttr = false;
|
|
108
|
+
else if (current && !TEXT_ATTRS.has(name) && !name.startsWith("aria-") && REMOTE_URL.test(value)) data.keepAttr = false;
|
|
109
|
+
else if ((name === "href" || name === "xlink:href") && node.nodeName.toLowerCase() !== "a" && !value.startsWith("#")) data.keepAttr = false;
|
|
110
|
+
});
|
|
111
|
+
purifier.addHook("afterSanitizeAttributes", (node) => {
|
|
112
|
+
if (node.nodeName.toLowerCase() === "a") {
|
|
113
|
+
node.setAttribute("target", "_blank");
|
|
114
|
+
node.setAttribute("rel", "noopener noreferrer nofollow");
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
return purifier;
|
|
118
|
+
}
|
|
119
|
+
/** The Trusted Types policy DOMPurify parses with, when there is one. */
|
|
120
|
+
function parsing(config) {
|
|
121
|
+
const trusted = policyOf();
|
|
122
|
+
return trusted ? {
|
|
123
|
+
...config,
|
|
124
|
+
TRUSTED_TYPES_POLICY: trusted
|
|
125
|
+
} : config;
|
|
126
|
+
}
|
|
127
|
+
/** `html` (the markdown's) sanitized, as a string. Browser only. */
|
|
128
|
+
function sanitizeHtml(html) {
|
|
129
|
+
return purifierOf().sanitize(html, parsing(MARKDOWN));
|
|
130
|
+
}
|
|
131
|
+
/** `html` sanitized for `innerHTML`: through the policy when there is one. */
|
|
132
|
+
function trustedHtml(html) {
|
|
133
|
+
const trusted = policyOf();
|
|
134
|
+
return trusted ? trusted.createHTML(html) : html;
|
|
135
|
+
}
|
|
136
|
+
/** What a renderer drew, sanitized: a fragment ready to insert, and the CSS of
|
|
137
|
+
* its `<style>` elements, which a caller adopts as a stylesheet. */
|
|
138
|
+
function sanitizeDrawing(markup) {
|
|
139
|
+
const pass = current = {
|
|
140
|
+
styles: /* @__PURE__ */ new Map(),
|
|
141
|
+
sheets: []
|
|
142
|
+
};
|
|
143
|
+
try {
|
|
144
|
+
const fragment = purifierOf().sanitize(markup, {
|
|
145
|
+
...parsing(DRAWING),
|
|
146
|
+
RETURN_DOM_FRAGMENT: true
|
|
147
|
+
});
|
|
148
|
+
const owned = document.adoptNode(fragment);
|
|
149
|
+
for (const [element, css] of pass.styles) setStyle(element, css);
|
|
150
|
+
return {
|
|
151
|
+
fragment: owned,
|
|
152
|
+
css: pass.sheets.join("\n")
|
|
153
|
+
};
|
|
154
|
+
} finally {
|
|
155
|
+
current = null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/** `node`, a drawing built with DOM calls, sanitized where it stands. */
|
|
159
|
+
function sanitizeNode(node) {
|
|
160
|
+
const pass = current = {
|
|
161
|
+
styles: /* @__PURE__ */ new Map(),
|
|
162
|
+
sheets: []
|
|
163
|
+
};
|
|
164
|
+
try {
|
|
165
|
+
purifierOf().sanitize(node, {
|
|
166
|
+
...parsing(DRAWING),
|
|
167
|
+
IN_PLACE: true
|
|
168
|
+
});
|
|
169
|
+
for (const [element, css] of pass.styles) setStyle(element, css);
|
|
170
|
+
} finally {
|
|
171
|
+
current = null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/** `css` (a `style` attribute's value) set through CSSOM, without any
|
|
175
|
+
* declaration that would load something. */
|
|
176
|
+
function setStyle(element, css) {
|
|
177
|
+
const style = element.style;
|
|
178
|
+
if (!style) return;
|
|
179
|
+
style.cssText = css;
|
|
180
|
+
for (const name of [...style]) {
|
|
181
|
+
const value = style.getPropertyValue(name);
|
|
182
|
+
if (REMOTE_URL.test(value) || REMOTE_CSS.test(value)) style.removeProperty(name);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/** `css` as a constructable stylesheet with only style rules (nested ones
|
|
186
|
+
* too), their groups (`@media`, `@supports`…) and keyframes, and no
|
|
187
|
+
* declaration that would load something, wherever it sits: never an
|
|
188
|
+
* `@import` or a `@font-face` from an answer. Null where the browser has
|
|
189
|
+
* none. */
|
|
190
|
+
function safeSheet(css) {
|
|
191
|
+
let sheet;
|
|
192
|
+
try {
|
|
193
|
+
sheet = new CSSStyleSheet();
|
|
194
|
+
sheet.replaceSync(css);
|
|
195
|
+
} catch {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
clean(sheet);
|
|
199
|
+
return sheet;
|
|
200
|
+
}
|
|
201
|
+
/** The rules a drawing's stylesheet keeps, by their interface's name. */
|
|
202
|
+
var KEPT_RULES = /* @__PURE__ */ new Set([
|
|
203
|
+
"CSSStyleRule",
|
|
204
|
+
"CSSNestedDeclarations",
|
|
205
|
+
"CSSMediaRule",
|
|
206
|
+
"CSSSupportsRule",
|
|
207
|
+
"CSSContainerRule",
|
|
208
|
+
"CSSLayerBlockRule",
|
|
209
|
+
"CSSKeyframesRule",
|
|
210
|
+
"CSSKeyframeRule"
|
|
211
|
+
]);
|
|
212
|
+
function clean(group) {
|
|
213
|
+
for (let i = group.cssRules.length - 1; i >= 0; i--) {
|
|
214
|
+
const rule = group.cssRules[i];
|
|
215
|
+
if (!KEPT_RULES.has(rule.constructor.name)) {
|
|
216
|
+
group.deleteRule(i);
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if ("style" in rule) {
|
|
220
|
+
const style = rule.style;
|
|
221
|
+
for (const name of [...style]) {
|
|
222
|
+
const value = style.getPropertyValue(name);
|
|
223
|
+
if (REMOTE_URL.test(value) || REMOTE_CSS.test(value)) style.removeProperty(name);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if ("cssRules" in rule) clean(rule);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/** Adds `sheet` to the stylesheets of `root` (the element's shadow root, or
|
|
230
|
+
* the document), once. */
|
|
231
|
+
function adoptSheet(root, sheet) {
|
|
232
|
+
if (!root.adoptedStyleSheets.includes(sheet)) root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
|
|
233
|
+
}
|
|
234
|
+
/** Removes `sheet` from the stylesheets of `root`. */
|
|
235
|
+
function dropSheet(root, sheet) {
|
|
236
|
+
if (root.adoptedStyleSheets.includes(sheet)) root.adoptedStyleSheets = root.adoptedStyleSheets.filter((s) => s !== sheet);
|
|
237
|
+
}
|
|
238
|
+
export { sanitizeHtml as a, sanitizeDrawing as i, dropSheet as n, sanitizeNode as o, safeSheet as r, trustedHtml as s, adoptSheet as t };
|
|
239
|
+
|
|
240
|
+
//# sourceMappingURL=sanitize-COLQzplk.js.map
|