@ourlu/assistant-sdk 0.2.3 → 0.2.4
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/dist/iife/engine.v1.2b5bb43b.js +735 -0
- package/dist/iife/engine.v1.80d2230f.js +770 -0
- package/dist/iife/engine.v1.9ca6b7ec.js +756 -0
- package/dist/iife/engine.v1.c0c00bd0.js +721 -0
- package/dist/iife/engine.v1.c54c9a1a.js +770 -0
- package/dist/iife/engine.v1.d1052e81.js +770 -0
- package/dist/iife/engine.v1.js +60 -11
- package/dist/iife/loader.v1.js +1 -1
- package/dist/iife/ui.v1.5d2d4504.js +942 -0
- package/dist/iife/ui.v1.6afac75f.js +944 -0
- package/dist/iife/ui.v1.7fb4db0b.js +935 -0
- package/dist/iife/ui.v1.923a4e6d.js +937 -0
- package/dist/iife/ui.v1.9bfe2815.js +942 -0
- package/dist/iife/ui.v1.c58e1d58.js +959 -0
- package/dist/iife/ui.v1.cdfe9a45.js +919 -0
- package/dist/iife/ui.v1.js +116 -76
- package/dist/iife/widget-manifest.json +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,944 @@
|
|
|
1
|
+
(function() {
|
|
2
|
+
function installColorUtils(ui) {
|
|
3
|
+
ui.clampAlpha = function clampAlpha(rawValue) {
|
|
4
|
+
var parsed = Number(rawValue);
|
|
5
|
+
if (!Number.isFinite(parsed)) return 1;
|
|
6
|
+
if (parsed < 0) return 0;
|
|
7
|
+
if (parsed > 1) return 1;
|
|
8
|
+
return parsed;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
ui.normalizeHexColor = function normalizeHexColor(rawValue, fallbackColor) {
|
|
12
|
+
var color = String(rawValue || "").trim().toLowerCase();
|
|
13
|
+
if (!/^#[0-9a-f]{6}$/.test(color)) return fallbackColor;
|
|
14
|
+
return color;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
ui.hexToRgba = function hexToRgba(hexColor, alphaValue) {
|
|
18
|
+
var normalized = ui.normalizeHexColor(hexColor, "#ffffff");
|
|
19
|
+
var alpha = ui.clampAlpha(alphaValue);
|
|
20
|
+
var r = parseInt(normalized.slice(1, 3), 16);
|
|
21
|
+
var g = parseInt(normalized.slice(3, 5), 16);
|
|
22
|
+
var b = parseInt(normalized.slice(5, 7), 16);
|
|
23
|
+
return "rgba(" + r + "," + g + "," + b + "," + alpha + ")";
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
ui.clampColorChannel = function clampColorChannel(channelValue) {
|
|
27
|
+
return Math.max(0, Math.min(255, channelValue));
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
ui.adjustHexBrightness = function adjustHexBrightness(hexColor, percentage) {
|
|
31
|
+
var normalizedColor = ui.normalizeHexColor(hexColor, "#000000");
|
|
32
|
+
var adjustment = Math.round((Number(percentage) / 100) * 255);
|
|
33
|
+
var red = ui.clampColorChannel(parseInt(normalizedColor.slice(1, 3), 16) + adjustment);
|
|
34
|
+
var green = ui.clampColorChannel(parseInt(normalizedColor.slice(3, 5), 16) + adjustment);
|
|
35
|
+
var blue = ui.clampColorChannel(parseInt(normalizedColor.slice(5, 7), 16) + adjustment);
|
|
36
|
+
return "#" + red.toString(16).padStart(2, "0") + green.toString(16).padStart(2, "0") + blue.toString(16).padStart(2, "0");
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function installMascotTheme(ui) {
|
|
41
|
+
ui.mascotSvgSourceCache = {};
|
|
42
|
+
|
|
43
|
+
ui.escapeRegExp = function escapeRegExp(rawValue) {
|
|
44
|
+
return String(rawValue || "").replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
ui.replaceSvgColorTokens = function replaceSvgColorTokens(rawSvgMarkup, colorMap) {
|
|
48
|
+
var themedMarkup = String(rawSvgMarkup || "");
|
|
49
|
+
Object.keys(colorMap).forEach(function(token) {
|
|
50
|
+
var resolvedColor = String(colorMap[token] || "").trim();
|
|
51
|
+
if (!resolvedColor) return;
|
|
52
|
+
themedMarkup = themedMarkup.replace(new RegExp(ui.escapeRegExp(token), "gi"), resolvedColor);
|
|
53
|
+
});
|
|
54
|
+
return themedMarkup;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
ui.assignMascotTokenGroup = function assignMascotTokenGroup(replacementMap, tokens, replacementColor) {
|
|
58
|
+
tokens.forEach(function(token) {
|
|
59
|
+
replacementMap[token] = replacementColor;
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
ui.buildMascotDerivedPalette = function buildMascotDerivedPalette(config) {
|
|
64
|
+
var blue500 = ui.normalizeHexColor(config.mascotSecondaryColor, "#68b1d6");
|
|
65
|
+
var blue700 = ui.normalizeHexColor(config.mascotSecondaryDarkColor, "#1472a8");
|
|
66
|
+
var gold500 = ui.normalizeHexColor(config.mascotGoldColor, "#ffd22e");
|
|
67
|
+
var eye500 = ui.normalizeHexColor(config.mascotEyeColor, "#040402");
|
|
68
|
+
var beak300 = ui.normalizeHexColor(config.mascotBeakColor, "#ab5f30");
|
|
69
|
+
var claw300 = ui.normalizeHexColor(config.mascotClawColor, "#ab5f30");
|
|
70
|
+
var neutral200 = ui.normalizeHexColor(config.mascotNeutralColor, "#e6e6e6");
|
|
71
|
+
var brow500 = ui.normalizeHexColor(config.mascotBrowColor, "#3e3e3e");
|
|
72
|
+
return {
|
|
73
|
+
blue400: ui.adjustHexBrightness(blue500, 8),
|
|
74
|
+
blue500: blue500,
|
|
75
|
+
blue550: ui.adjustHexBrightness(blue500, -6),
|
|
76
|
+
blue580: ui.adjustHexBrightness(blue500, -24),
|
|
77
|
+
blue600: ui.adjustHexBrightness(blue700, 12),
|
|
78
|
+
blue700: blue700,
|
|
79
|
+
blue800: ui.adjustHexBrightness(blue700, -10),
|
|
80
|
+
gold500: gold500,
|
|
81
|
+
gold450: ui.adjustHexBrightness(gold500, -6),
|
|
82
|
+
gold700: ui.adjustHexBrightness(gold500, -24),
|
|
83
|
+
gold900: ui.adjustHexBrightness(gold500, -42),
|
|
84
|
+
eye500: eye500,
|
|
85
|
+
eye900: ui.adjustHexBrightness(eye500, -26),
|
|
86
|
+
beak300: beak300,
|
|
87
|
+
beak400: ui.adjustHexBrightness(beak300, 14),
|
|
88
|
+
beak500: ui.adjustHexBrightness(beak300, 8),
|
|
89
|
+
beak700: ui.adjustHexBrightness(beak300, -24),
|
|
90
|
+
claw300: claw300,
|
|
91
|
+
claw400: ui.adjustHexBrightness(claw300, 18),
|
|
92
|
+
claw700: ui.adjustHexBrightness(claw300, -30),
|
|
93
|
+
brow500: brow500,
|
|
94
|
+
brow400: ui.adjustHexBrightness(brow500, 4),
|
|
95
|
+
neutral200: neutral200,
|
|
96
|
+
neutral300: ui.adjustHexBrightness(neutral200, -18),
|
|
97
|
+
black: ui.adjustHexBrightness(eye500, -40),
|
|
98
|
+
white: "#ffffff"
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
ui.buildMascotColorReplacementMap = function buildMascotColorReplacementMap(config) {
|
|
103
|
+
var palette = ui.buildMascotDerivedPalette(config || {});
|
|
104
|
+
var replacementMap = {};
|
|
105
|
+
ui.assignMascotTokenGroup(replacementMap, ["#70c1ea"], palette.blue400);
|
|
106
|
+
ui.assignMascotTokenGroup(replacementMap, ["#68b1d6"], palette.blue500);
|
|
107
|
+
ui.assignMascotTokenGroup(replacementMap, ["#44aade"], palette.blue550);
|
|
108
|
+
ui.assignMascotTokenGroup(replacementMap, ["#4a9ac3"], palette.blue580);
|
|
109
|
+
ui.assignMascotTokenGroup(replacementMap, ["#2f97cc"], palette.blue600);
|
|
110
|
+
ui.assignMascotTokenGroup(replacementMap, ["#1472a8"], palette.blue700);
|
|
111
|
+
ui.assignMascotTokenGroup(replacementMap, ["#156696"], palette.blue800);
|
|
112
|
+
ui.assignMascotTokenGroup(replacementMap, ["#ffd22e"], palette.gold500);
|
|
113
|
+
ui.assignMascotTokenGroup(replacementMap, ["#f0c31f"], palette.gold450);
|
|
114
|
+
ui.assignMascotTokenGroup(replacementMap, ["#c29500"], palette.gold700);
|
|
115
|
+
ui.assignMascotTokenGroup(replacementMap, ["#946700"], palette.gold900);
|
|
116
|
+
ui.assignMascotTokenGroup(replacementMap, ["#040402", "#00193b"], palette.eye900);
|
|
117
|
+
ui.assignMascotTokenGroup(replacementMap, ["#ab5f30"], palette.beak300);
|
|
118
|
+
ui.assignMascotTokenGroup(replacementMap, ["#dd8e5d", "#dd9060", "#ca7e4f"], palette.beak400);
|
|
119
|
+
ui.assignMascotTokenGroup(replacementMap, ["#d98d5e"], palette.beak500);
|
|
120
|
+
ui.assignMascotTokenGroup(replacementMap, ["#6e2200"], palette.beak700);
|
|
121
|
+
ui.assignMascotTokenGroup(replacementMap, ["#6f2301"], palette.claw700);
|
|
122
|
+
ui.assignMascotTokenGroup(replacementMap, ["#da8e5f"], palette.claw400);
|
|
123
|
+
ui.assignMascotTokenGroup(replacementMap, ["#3e3e3e"], palette.brow500);
|
|
124
|
+
ui.assignMascotTokenGroup(replacementMap, ["#434343"], palette.brow400);
|
|
125
|
+
ui.assignMascotTokenGroup(replacementMap, ["#e6e6e6"], palette.neutral200);
|
|
126
|
+
replacementMap.white = palette.white;
|
|
127
|
+
replacementMap.black = palette.black;
|
|
128
|
+
return replacementMap;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
ui.isLikelySvgAsset = function isLikelySvgAsset(rawUrl) {
|
|
132
|
+
return /\.svg(?:[?#].*)?$/i.test(String(rawUrl || "").trim());
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
ui.resolveAbsoluteMascotUrl = function resolveAbsoluteMascotUrl(mascotUrl, apiBaseUrl) {
|
|
136
|
+
var normalized = String(mascotUrl || "").trim();
|
|
137
|
+
if (!normalized) return "";
|
|
138
|
+
if (/^https?:\/\//i.test(normalized)) return normalized;
|
|
139
|
+
var base = String(apiBaseUrl || "").trim().replace(/\/$/, "");
|
|
140
|
+
if (!base) {
|
|
141
|
+
throw new Error("[OurluMairie] mascot URL relative sans apiBaseUrl: " + normalized);
|
|
142
|
+
}
|
|
143
|
+
return normalized.startsWith("/") ? base + normalized : base + "/" + normalized;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
ui.encodeSvgToDataUrl = function encodeSvgToDataUrl(svgMarkup) {
|
|
147
|
+
return "data:image/svg+xml;charset=utf-8," + encodeURIComponent(String(svgMarkup || ""));
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
ui.resolveMascotUrlWithTheme = function resolveMascotUrlWithTheme(mascotUrl, config) {
|
|
151
|
+
var normalizedMascotUrl = ui.resolveAbsoluteMascotUrl(mascotUrl, (config || {}).apiBaseUrl);
|
|
152
|
+
if (!normalizedMascotUrl || typeof fetch !== "function" || !ui.isLikelySvgAsset(normalizedMascotUrl)) {
|
|
153
|
+
return Promise.resolve(normalizedMascotUrl);
|
|
154
|
+
}
|
|
155
|
+
var replacementMap = ui.buildMascotColorReplacementMap(config || {});
|
|
156
|
+
var cacheKey = normalizedMascotUrl + "::" + [
|
|
157
|
+
ui.normalizeHexColor((config || {}).mascotSecondaryColor, "#68b1d6"),
|
|
158
|
+
ui.normalizeHexColor((config || {}).mascotSecondaryDarkColor, "#1472a8"),
|
|
159
|
+
ui.normalizeHexColor((config || {}).mascotGoldColor, "#ffd22e"),
|
|
160
|
+
ui.normalizeHexColor((config || {}).mascotEyeColor, "#040402"),
|
|
161
|
+
ui.normalizeHexColor((config || {}).mascotBeakColor, "#ab5f30"),
|
|
162
|
+
ui.normalizeHexColor((config || {}).mascotNeutralColor, "#e6e6e6"),
|
|
163
|
+
ui.normalizeHexColor((config || {}).mascotBrowColor, "#3e3e3e"),
|
|
164
|
+
ui.normalizeHexColor((config || {}).mascotClawColor, "#ab5f30")
|
|
165
|
+
].join("::");
|
|
166
|
+
if (ui.mascotSvgSourceCache[cacheKey]) {
|
|
167
|
+
return Promise.resolve(ui.mascotSvgSourceCache[cacheKey]);
|
|
168
|
+
}
|
|
169
|
+
return fetch(normalizedMascotUrl, { method: "GET", credentials: "omit" })
|
|
170
|
+
.then(function(response) {
|
|
171
|
+
if (!response.ok) {
|
|
172
|
+
throw new Error("mascot fetch failed");
|
|
173
|
+
}
|
|
174
|
+
return response.text();
|
|
175
|
+
})
|
|
176
|
+
.then(function(rawSvgMarkup) {
|
|
177
|
+
if (!rawSvgMarkup || rawSvgMarkup.indexOf("<svg") === -1) {
|
|
178
|
+
throw new Error("mascot SVG invalide ou reponse non-SVG pour " + normalizedMascotUrl);
|
|
179
|
+
}
|
|
180
|
+
var themedSvgMarkup = ui.replaceSvgColorTokens(rawSvgMarkup, replacementMap);
|
|
181
|
+
var themedDataUrl = ui.encodeSvgToDataUrl(themedSvgMarkup);
|
|
182
|
+
ui.mascotSvgSourceCache[cacheKey] = themedDataUrl;
|
|
183
|
+
return themedDataUrl;
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function installMarkdownRender(ui) {
|
|
189
|
+
ui.escapeHtml = function escapeHtml(rawValue) {
|
|
190
|
+
return String(rawValue || "")
|
|
191
|
+
.replace(/&/g, "&")
|
|
192
|
+
.replace(/</g, "<")
|
|
193
|
+
.replace(/>/g, ">")
|
|
194
|
+
.replace(/"/g, """)
|
|
195
|
+
.replace(/'/g, "'");
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
ui.escapeHtmlAttribute = function escapeHtmlAttribute(rawValue) {
|
|
199
|
+
return ui.escapeHtml(rawValue).replace(/`/g, "`");
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
ui.renderInlineMarkdown = function renderInlineMarkdown(rawText) {
|
|
203
|
+
var escaped = ui.escapeHtml(rawText);
|
|
204
|
+
escaped = escaped.replace(/\[([^\]\n]+)\]\((https?:\/\/[^\s)]+)\)/g, function(_match, label, url) {
|
|
205
|
+
return '<a href="' + ui.escapeHtmlAttribute(url) + '" target="_blank" rel="noopener noreferrer">' + label + "</a>";
|
|
206
|
+
});
|
|
207
|
+
escaped = escaped.replace(/`([^`\n]+)`/g, "<code>$1</code>");
|
|
208
|
+
escaped = escaped.replace(/\*\*([^*\n]+)\*\*/g, "<strong>$1</strong>");
|
|
209
|
+
escaped = escaped.replace(/\*([^*\n]+)\*/g, "<em>$1</em>");
|
|
210
|
+
return escaped;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
ui.renderAssistantMarkdown = function renderAssistantMarkdown(rawText) {
|
|
214
|
+
var normalized = String(rawText || "").replace(/\r\n?/g, "\n").trim();
|
|
215
|
+
if (!normalized) {
|
|
216
|
+
return "";
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
var codeBlocks = [];
|
|
220
|
+
normalized = normalized.replace(/```([\s\S]*?)```/g, function(_match, codeContent) {
|
|
221
|
+
var placeholder = "@@CM_CODE_BLOCK_" + codeBlocks.length + "@@";
|
|
222
|
+
codeBlocks.push(
|
|
223
|
+
'<pre><code>' + ui.escapeHtml(String(codeContent || "").replace(/^\n+|\n+$/g, "")) + "</code></pre>"
|
|
224
|
+
);
|
|
225
|
+
return placeholder;
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
var htmlParts = [];
|
|
229
|
+
var listType = "";
|
|
230
|
+
var listItems = [];
|
|
231
|
+
var lines = normalized.split("\n");
|
|
232
|
+
|
|
233
|
+
function flushList() {
|
|
234
|
+
if (!listType || listItems.length === 0) {
|
|
235
|
+
listType = "";
|
|
236
|
+
listItems = [];
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
htmlParts.push("<" + listType + ">" + listItems.join("") + "</" + listType + ">");
|
|
240
|
+
listType = "";
|
|
241
|
+
listItems = [];
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
for (var index = 0; index < lines.length; index += 1) {
|
|
245
|
+
var rawLine = lines[index];
|
|
246
|
+
var trimmedLine = rawLine.trim();
|
|
247
|
+
if (!trimmedLine) {
|
|
248
|
+
flushList();
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
var unorderedMatch = trimmedLine.match(/^[-*]\s+(.+)$/);
|
|
253
|
+
if (unorderedMatch) {
|
|
254
|
+
if (listType && listType !== "ul") {
|
|
255
|
+
flushList();
|
|
256
|
+
}
|
|
257
|
+
listType = "ul";
|
|
258
|
+
listItems.push("<li>" + ui.renderInlineMarkdown(unorderedMatch[1]) + "</li>");
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
var orderedMatch = trimmedLine.match(/^\d+\.\s+(.+)$/);
|
|
263
|
+
if (orderedMatch) {
|
|
264
|
+
if (listType && listType !== "ol") {
|
|
265
|
+
flushList();
|
|
266
|
+
}
|
|
267
|
+
listType = "ol";
|
|
268
|
+
listItems.push("<li>" + ui.renderInlineMarkdown(orderedMatch[1]) + "</li>");
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
flushList();
|
|
273
|
+
|
|
274
|
+
var headingMatch = trimmedLine.match(/^(#{1,3})\s+(.+)$/);
|
|
275
|
+
if (headingMatch) {
|
|
276
|
+
var level = Math.min(4, headingMatch[1].length + 2);
|
|
277
|
+
htmlParts.push("<h" + level + ">" + ui.renderInlineMarkdown(headingMatch[2]) + "</h" + level + ">");
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
htmlParts.push("<p>" + ui.renderInlineMarkdown(trimmedLine) + "</p>");
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
flushList();
|
|
285
|
+
|
|
286
|
+
var rendered = htmlParts.join("");
|
|
287
|
+
rendered = rendered.replace(/@@CM_CODE_BLOCK_(\d+)@@/g, function(_match, rawIndex) {
|
|
288
|
+
var blockIndex = Number(rawIndex);
|
|
289
|
+
if (!Number.isInteger(blockIndex) || blockIndex < 0 || blockIndex >= codeBlocks.length) {
|
|
290
|
+
return "";
|
|
291
|
+
}
|
|
292
|
+
return codeBlocks[blockIndex];
|
|
293
|
+
});
|
|
294
|
+
return rendered || "<p>" + ui.renderInlineMarkdown(normalized) + "</p>";
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function installMediaUtils(ui) {
|
|
299
|
+
ui.trimTrailingSlash = function trimTrailingSlash(value) {
|
|
300
|
+
return String(value || "").replace(/\/$/, "");
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
ui.mergeTranscript = function mergeTranscript(currentText, delta) {
|
|
304
|
+
var base = String(currentText || "");
|
|
305
|
+
var incoming = String(delta || "").replace(/^\s+/, "");
|
|
306
|
+
if (!incoming) return base;
|
|
307
|
+
if (!base) return incoming;
|
|
308
|
+
var lastChar = base[base.length - 1];
|
|
309
|
+
var firstChar = incoming[0];
|
|
310
|
+
var noSpaceAfter = [" ", "\n", "'", "\"", "(", "[", "{", "/", "-"];
|
|
311
|
+
var noSpaceBefore = [".", ",", ";", ":", "!", "?", ")", "]", "}", "/", "-"];
|
|
312
|
+
var needsSpace = noSpaceAfter.indexOf(lastChar) === -1 && noSpaceBefore.indexOf(firstChar) === -1;
|
|
313
|
+
return needsSpace ? base + " " + incoming : base + incoming;
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
ui.resolveRecorderMimeType = function resolveRecorderMimeType() {
|
|
317
|
+
if (typeof MediaRecorder === "undefined") return "";
|
|
318
|
+
var preferred = ["audio/webm;codecs=opus", "audio/webm", "audio/ogg;codecs=opus", "audio/ogg", "audio/mp4"];
|
|
319
|
+
for (var i = 0; i < preferred.length; i += 1) {
|
|
320
|
+
if (MediaRecorder.isTypeSupported(preferred[i])) return preferred[i];
|
|
321
|
+
}
|
|
322
|
+
return "";
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
ui.encodeArrayBufferToBase64 = function encodeArrayBufferToBase64(arrayBuffer) {
|
|
326
|
+
var bytes = new Uint8Array(arrayBuffer);
|
|
327
|
+
var chunkSize = 0x8000;
|
|
328
|
+
var chunks = [];
|
|
329
|
+
for (var offset = 0; offset < bytes.length; offset += chunkSize) {
|
|
330
|
+
var part = bytes.subarray(offset, offset + chunkSize);
|
|
331
|
+
chunks.push(String.fromCharCode.apply(null, part));
|
|
332
|
+
}
|
|
333
|
+
return btoa(chunks.join(""));
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function installEventBus(ui) {
|
|
338
|
+
function EventBus() {
|
|
339
|
+
this.events = {};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
EventBus.prototype.on = function(eventName, handler) {
|
|
343
|
+
if (!this.events[eventName]) this.events[eventName] = [];
|
|
344
|
+
this.events[eventName].push(handler);
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
EventBus.prototype.emit = function(eventName, payload) {
|
|
348
|
+
var handlers = this.events[eventName] || [];
|
|
349
|
+
handlers.forEach(function(handler) {
|
|
350
|
+
try { handler(payload); } catch (_) {}
|
|
351
|
+
});
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
ui.EventBus = EventBus;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function installWidgetCssBuilder(ui) {
|
|
358
|
+
function WidgetCssBuilder(config) {
|
|
359
|
+
this.config = config;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
WidgetCssBuilder.prototype.resolveLayout = function() {
|
|
363
|
+
var panelWidth = Number(this.config.panelWidth);
|
|
364
|
+
if (!Number.isFinite(panelWidth)) panelWidth = 420;
|
|
365
|
+
panelWidth = Math.max(360, Math.min(960, Math.round(panelWidth)));
|
|
366
|
+
|
|
367
|
+
var panelHeight = Number(this.config.panelHeight);
|
|
368
|
+
if (!Number.isFinite(panelHeight)) panelHeight = 640;
|
|
369
|
+
panelHeight = Math.max(480, Math.min(1400, Math.round(panelHeight)));
|
|
370
|
+
|
|
371
|
+
var panelMaxHeightVh = Number(this.config.panelMaxHeightVh);
|
|
372
|
+
if (!Number.isFinite(panelMaxHeightVh)) panelMaxHeightVh = 85;
|
|
373
|
+
panelMaxHeightVh = Math.max(60, Math.min(98, Math.round(panelMaxHeightVh)));
|
|
374
|
+
|
|
375
|
+
var borderRadius = Number(this.config.panelBorderRadius);
|
|
376
|
+
if (!Number.isFinite(borderRadius)) borderRadius = 16;
|
|
377
|
+
borderRadius = Math.max(0, Math.min(32, Math.round(borderRadius)));
|
|
378
|
+
|
|
379
|
+
return {
|
|
380
|
+
panelWidth: panelWidth,
|
|
381
|
+
panelHeight: panelHeight,
|
|
382
|
+
panelMaxHeightVh: panelMaxHeightVh,
|
|
383
|
+
borderRadius: borderRadius
|
|
384
|
+
};
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
WidgetCssBuilder.prototype.build = function() {
|
|
388
|
+
var cfg = this.config;
|
|
389
|
+
var side = cfg.position === "bottom-left" ? "left" : "right";
|
|
390
|
+
var resizeCorner = cfg.position === "bottom-left" ? "right" : "left";
|
|
391
|
+
var gradient = "linear-gradient(135deg," + cfg.primaryColor + " 0%,#0047b3 100%)";
|
|
392
|
+
var panelBg = ui.hexToRgba(cfg.panelBackgroundColor, cfg.panelBackgroundAlpha);
|
|
393
|
+
var layout = this.resolveLayout();
|
|
394
|
+
var pw = layout.panelWidth + "px";
|
|
395
|
+
var ph = layout.panelHeight + "px";
|
|
396
|
+
var mhVh = layout.panelMaxHeightVh + "vh";
|
|
397
|
+
var br = layout.borderRadius + "px";
|
|
398
|
+
|
|
399
|
+
return [
|
|
400
|
+
this.buildResetAndBase(),
|
|
401
|
+
this.buildBubble(side, gradient, cfg.primaryColor),
|
|
402
|
+
this.buildPanel(side, pw, ph, mhVh, panelBg, br),
|
|
403
|
+
this.buildHeader(gradient, br),
|
|
404
|
+
this.buildNotes(),
|
|
405
|
+
this.buildMessages(),
|
|
406
|
+
this.buildMessageBubbles(cfg.primaryColor),
|
|
407
|
+
this.buildTypingAndError(),
|
|
408
|
+
this.buildComposer(cfg.primaryColor, br),
|
|
409
|
+
this.buildResizeHandle(resizeCorner),
|
|
410
|
+
this.buildMobileOverrides()
|
|
411
|
+
].join("\n");
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
WidgetCssBuilder.prototype.buildResetAndBase = function() {
|
|
415
|
+
return ":host *{box-sizing:border-box;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,system-ui,sans-serif;-webkit-font-smoothing:antialiased}";
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
WidgetCssBuilder.prototype.buildBubble = function(side, gradient, primaryColor) {
|
|
419
|
+
return [
|
|
420
|
+
"#cm-bubble{position:fixed;" + side + ":24px;bottom:24px;width:64px;height:64px;border-radius:50%;background:" + gradient + ";border:none;cursor:pointer;display:flex;align-items:center;justify-content:center;z-index:2147483000;box-shadow:0 8px 24px rgba(0,0,0,.28),0 2px 8px rgba(0,0,0,.12);transition:transform .2s cubic-bezier(.4,0,.2,1)}",
|
|
421
|
+
"#cm-bubble:hover{transform:scale(1.1)}",
|
|
422
|
+
"#cm-bubble:active{transform:scale(0.95)}",
|
|
423
|
+
"#cm-bubble:focus-visible{outline:3px solid " + primaryColor + ";outline-offset:3px}",
|
|
424
|
+
"#cm-bubble img{width:84px;height:84px;object-fit:contain;position:absolute;top:-14px;filter:drop-shadow(0 4px 8px rgba(0,0,0,.25))}"
|
|
425
|
+
].join("\n");
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
WidgetCssBuilder.prototype.buildPanel = function(side, width, height, maxHeightVh, background, borderRadius) {
|
|
429
|
+
return [
|
|
430
|
+
"#cm-panel{position:fixed;" + side + ":24px;bottom:100px;width:" + width + ";max-width:calc(100vw - 32px);height:" + height + ";max-height:" + maxHeightVh + ";background:" + background + ";border-radius:" + borderRadius + ";box-shadow:0 16px 48px rgba(0,0,0,.22),0 4px 16px rgba(0,0,0,.1);display:none;flex-direction:column;overflow:visible;z-index:2147483000;transition:box-shadow .2s,bottom .25s cubic-bezier(.4,0,.2,1)}",
|
|
431
|
+
"#cm-panel.open{display:flex;bottom:24px}"
|
|
432
|
+
].join("\n");
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
WidgetCssBuilder.prototype.buildHeader = function(gradient, borderRadius) {
|
|
436
|
+
return [
|
|
437
|
+
"#cm-header{display:flex;align-items:center;justify-content:space-between;padding:14px 16px 14px 80px;background:" + gradient + ";color:#fff;position:relative;border-radius:" + borderRadius + " " + borderRadius + " 0 0;min-height:52px}",
|
|
438
|
+
"#cm-header-mascot{width:72px;height:72px;position:absolute;left:4px;top:-12px;filter:drop-shadow(0 3px 6px rgba(0,0,0,.25));pointer-events:none}",
|
|
439
|
+
"#cm-title{margin:0;font-size:16px;font-weight:600;letter-spacing:-0.01em}",
|
|
440
|
+
"#cm-close{background:none;border:none;color:#fff;cursor:pointer;font-size:22px;line-height:1;padding:6px 10px;border-radius:6px;transition:background .15s}",
|
|
441
|
+
"#cm-close:hover{background:rgba(255,255,255,.18)}"
|
|
442
|
+
].join("\n");
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
WidgetCssBuilder.prototype.buildNotes = function() {
|
|
446
|
+
return [
|
|
447
|
+
"#cm-disclaimer,#cm-transparency{font-size:13px;display:flex;align-items:center;gap:8px;padding:8px 14px}",
|
|
448
|
+
"#cm-disclaimer{background:#fff3cd;color:#6b5900;border-bottom:1px solid #ffc107}",
|
|
449
|
+
"#cm-transparency{color:#555;border-top:1px solid #eee}",
|
|
450
|
+
".cm-close-note{background:none;border:none;cursor:pointer;border-radius:4px;padding:2px 6px;opacity:.7;transition:opacity .15s}",
|
|
451
|
+
".cm-close-note:hover{opacity:1;background:rgba(0,0,0,.06)}"
|
|
452
|
+
].join("\n");
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
WidgetCssBuilder.prototype.buildMessages = function() {
|
|
456
|
+
return [
|
|
457
|
+
"#cm-messages{flex:1;overflow-y:auto;padding:16px;display:flex;flex-direction:column;gap:12px;scroll-behavior:smooth;overscroll-behavior:contain}",
|
|
458
|
+
"#cm-messages::-webkit-scrollbar{width:6px}",
|
|
459
|
+
"#cm-messages::-webkit-scrollbar-track{background:transparent}",
|
|
460
|
+
"#cm-messages::-webkit-scrollbar-thumb{background:rgba(0,0,0,.12);border-radius:3px}",
|
|
461
|
+
"#cm-messages::-webkit-scrollbar-thumb:hover{background:rgba(0,0,0,.2)}",
|
|
462
|
+
"#cm-welcome{color:#555;font-size:15px;line-height:1.6;text-align:center;padding:24px 16px}"
|
|
463
|
+
].join("\n");
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
WidgetCssBuilder.prototype.buildMessageBubbles = function(primaryColor) {
|
|
467
|
+
return [
|
|
468
|
+
".cm-msg{max-width:88%;padding:12px 16px;border-radius:16px;font-size:15px;line-height:1.55;word-break:break-word;animation:cm-msg-in .25s cubic-bezier(.4,0,.2,1)}",
|
|
469
|
+
"@keyframes cm-msg-in{from{opacity:0;transform:translateY(8px)}to{opacity:1;transform:translateY(0)}}",
|
|
470
|
+
".cm-msg.assistant{background:#f0f4f8;color:#1a1a2e;border-bottom-left-radius:4px;align-self:flex-start}",
|
|
471
|
+
".cm-msg.assistant p{margin:0 0 10px}",
|
|
472
|
+
".cm-msg.assistant p:last-child{margin-bottom:0}",
|
|
473
|
+
".cm-msg.assistant ul,.cm-msg.assistant ol{margin:0 0 10px 20px;padding:0}",
|
|
474
|
+
".cm-msg.assistant li{margin:0 0 4px}",
|
|
475
|
+
".cm-msg.assistant a{color:#0b57d0;text-decoration:underline;word-break:break-all}",
|
|
476
|
+
".cm-msg.assistant code{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;background:#e2e8f0;padding:2px 6px;border-radius:4px;font-size:13px}",
|
|
477
|
+
".cm-msg.assistant pre{margin:0 0 10px;padding:10px 12px;background:#1f2937;color:#f9fafb;border-radius:10px;overflow-x:auto;font-size:13px}",
|
|
478
|
+
".cm-msg.assistant pre code{background:transparent;color:inherit;padding:0;font-size:inherit}",
|
|
479
|
+
".cm-msg.assistant h3,.cm-msg.assistant h4{margin:0 0 8px;font-size:15px;line-height:1.35;font-weight:600}",
|
|
480
|
+
".cm-msg.user{background:" + primaryColor + ";color:#fff;border-bottom-right-radius:4px;align-self:flex-end}"
|
|
481
|
+
].join("\n");
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
WidgetCssBuilder.prototype.buildTypingAndError = function() {
|
|
485
|
+
return [
|
|
486
|
+
"#cm-typing{padding:8px 16px;font-size:13px;color:#666;display:none}",
|
|
487
|
+
"#cm-typing.visible{display:flex;align-items:center;gap:8px}",
|
|
488
|
+
"#cm-typing::before{content:'';display:inline-block;width:6px;height:6px;background:#666;border-radius:50%;animation:cm-dot-pulse 1.2s infinite}",
|
|
489
|
+
"@keyframes cm-dot-pulse{0%,100%{opacity:.3;transform:scale(.8)}50%{opacity:1;transform:scale(1.1)}}",
|
|
490
|
+
"#cm-error{display:none;background:#f8d7da;color:#58151c;font-size:13px;padding:10px 14px}",
|
|
491
|
+
"#cm-error.visible{display:block}"
|
|
492
|
+
].join("\n");
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
WidgetCssBuilder.prototype.buildComposer = function(primaryColor, borderRadius) {
|
|
496
|
+
return [
|
|
497
|
+
"#cm-form{display:flex;align-items:flex-end;gap:8px;padding:12px 16px;border-top:1px solid #eee;background:#fff;border-radius:0 0 " + borderRadius + " " + borderRadius + "}",
|
|
498
|
+
"#cm-input{flex:1;border:1.5px solid #d1d5db;border-radius:14px;padding:12px 16px;min-height:48px;max-height:200px;resize:none;outline:none;line-height:1.5;font-size:15px;transition:border-color .2s,box-shadow .2s;overflow-y:auto}",
|
|
499
|
+
"#cm-input:focus{border-color:" + primaryColor + ";box-shadow:0 0 0 3px rgba(0,102,255,.15)}",
|
|
500
|
+
"#cm-input::placeholder{color:#9ca3af}",
|
|
501
|
+
"#cm-send,#cm-mic{width:48px;height:48px;border-radius:50%;display:flex;align-items:center;justify-content:center;border:none;cursor:pointer;flex-shrink:0;transition:transform .15s,opacity .15s;font-size:18px}",
|
|
502
|
+
"#cm-send{background:" + primaryColor + ";color:#fff}",
|
|
503
|
+
"#cm-send:hover:not(:disabled){transform:scale(1.08)}",
|
|
504
|
+
"#cm-send:active:not(:disabled){transform:scale(0.94)}",
|
|
505
|
+
"#cm-send:disabled,#cm-mic:disabled{opacity:.4;cursor:not-allowed}",
|
|
506
|
+
"#cm-mic{background:transparent;color:#667085;border:1.5px solid #d1d5db}",
|
|
507
|
+
"#cm-mic:hover:not(:disabled){border-color:#9ca3af;background:rgba(0,0,0,.02)}",
|
|
508
|
+
"#cm-mic.listening{background:#ef4444;color:#fff;border-color:#ef4444;animation:cm-mic-pulse 1.5s infinite}",
|
|
509
|
+
"@keyframes cm-mic-pulse{0%,100%{box-shadow:0 0 0 0 rgba(239,68,68,.35)}50%{box-shadow:0 0 0 8px rgba(239,68,68,0)}}"
|
|
510
|
+
].join("\n");
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
WidgetCssBuilder.prototype.buildResizeHandle = function(corner) {
|
|
514
|
+
var posX = corner === "left" ? "left:0" : "right:0";
|
|
515
|
+
var cursorType = corner === "left" ? "ne-resize" : "nw-resize";
|
|
516
|
+
return [
|
|
517
|
+
"#cm-resize-handle{position:absolute;top:0;" + posX + ";width:28px;height:28px;cursor:" + cursorType + ";z-index:10;display:flex;align-items:center;justify-content:center;opacity:0;transition:opacity .2s}",
|
|
518
|
+
"#cm-panel:hover #cm-resize-handle{opacity:.5}",
|
|
519
|
+
"#cm-resize-handle:hover{opacity:1 !important}",
|
|
520
|
+
"#cm-resize-handle svg{width:14px;height:14px;pointer-events:none}"
|
|
521
|
+
].join("\n");
|
|
522
|
+
};
|
|
523
|
+
|
|
524
|
+
WidgetCssBuilder.prototype.buildMobileOverrides = function() {
|
|
525
|
+
return [
|
|
526
|
+
"@media (max-width:600px){",
|
|
527
|
+
"#cm-bubble{width:56px;height:56px;bottom:16px;right:16px !important;left:auto !important}",
|
|
528
|
+
"#cm-bubble img{width:72px;height:72px;top:-12px}",
|
|
529
|
+
"#cm-panel{left:0 !important;right:0 !important;bottom:0 !important;top:0 !important;width:100% !important;max-width:none !important;height:100dvh !important;height:100vh !important;max-height:none !important;border-radius:0 !important}",
|
|
530
|
+
"#cm-header{border-radius:0 !important;padding:14px 16px 14px 72px;min-height:52px}",
|
|
531
|
+
"#cm-header-mascot{width:56px;height:56px;left:8px;top:-2px}",
|
|
532
|
+
"#cm-title{font-size:16px}",
|
|
533
|
+
"#cm-close{font-size:24px;padding:8px 12px}",
|
|
534
|
+
"#cm-messages{flex:1;padding:12px 16px;gap:12px;overflow-y:auto}",
|
|
535
|
+
".cm-msg{max-width:92%;padding:12px 14px;font-size:15px;border-radius:16px}",
|
|
536
|
+
"#cm-form{padding:10px 12px;padding-bottom:calc(10px + env(safe-area-inset-bottom,0px));border-radius:0 !important;gap:8px}",
|
|
537
|
+
"#cm-input{min-height:44px;font-size:16px;border-radius:14px;padding:10px 14px}",
|
|
538
|
+
"#cm-send,#cm-mic{width:44px;height:44px}",
|
|
539
|
+
"#cm-typing{padding:6px 16px;font-size:13px}",
|
|
540
|
+
"#cm-disclaimer,#cm-transparency{font-size:12px;padding:8px 12px}",
|
|
541
|
+
"#cm-resize-handle{display:none !important}",
|
|
542
|
+
"#cm-welcome{font-size:15px;padding:24px 16px}",
|
|
543
|
+
"}"
|
|
544
|
+
].join("\n");
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
ui.WidgetCssBuilder = WidgetCssBuilder;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function installWidgetUIManager(ui) {
|
|
551
|
+
function WidgetUIManager(config) {
|
|
552
|
+
this.config = config;
|
|
553
|
+
this.root = null;
|
|
554
|
+
this.bubble = null;
|
|
555
|
+
this.bubbleMascot = null;
|
|
556
|
+
this.panel = null;
|
|
557
|
+
this.headerMascot = null;
|
|
558
|
+
this.messages = null;
|
|
559
|
+
this.input = null;
|
|
560
|
+
this.sendButton = null;
|
|
561
|
+
this.micButton = null;
|
|
562
|
+
this.typing = null;
|
|
563
|
+
this.error = null;
|
|
564
|
+
this.welcome = null;
|
|
565
|
+
this.streamingAssistantElement = null;
|
|
566
|
+
this.streamingAssistantBuffer = "";
|
|
567
|
+
this.themeOverrideStyle = null;
|
|
568
|
+
this._resizeDragState = null;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
WidgetUIManager.prototype.mount = function() {
|
|
572
|
+
if (document.getElementById(ui.CONTAINER_ID)) return false;
|
|
573
|
+
var cssBuilder = new ui.WidgetCssBuilder(this.config);
|
|
574
|
+
var css = cssBuilder.build();
|
|
575
|
+
var esc = ui.escapeHtml;
|
|
576
|
+
var escAttr = ui.escapeHtmlAttribute;
|
|
577
|
+
var resizeCorner = this.config.position === "bottom-left" ? "right" : "left";
|
|
578
|
+
var resizeSvg = resizeCorner === "left"
|
|
579
|
+
? '<svg viewBox="0 0 14 14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"><line x1="2" y1="12" x2="12" y2="2"/><line x1="2" y1="7" x2="7" y2="2"/></svg>'
|
|
580
|
+
: '<svg viewBox="0 0 14 14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"><line x1="12" y1="12" x2="2" y2="2"/><line x1="12" y1="7" x2="7" y2="2"/></svg>';
|
|
581
|
+
|
|
582
|
+
var host = document.createElement("div");
|
|
583
|
+
host.id = ui.CONTAINER_ID;
|
|
584
|
+
document.body.appendChild(host);
|
|
585
|
+
var shadow = host.attachShadow({ mode: "open" });
|
|
586
|
+
var root = document.createElement("div");
|
|
587
|
+
root.innerHTML = [
|
|
588
|
+
"<style>" + css + "</style>",
|
|
589
|
+
'<button id="cm-bubble" type="button" aria-label="Ouvrir l\'assistant">',
|
|
590
|
+
'<img id="cm-bubble-mascot" src="' + escAttr(this.config.mascotUrl) + '" alt="Mascotte assistant" />',
|
|
591
|
+
"</button>",
|
|
592
|
+
'<div id="cm-panel" role="dialog" aria-modal="true" aria-label="Chat ' + escAttr(this.config.assistantName) + '">',
|
|
593
|
+
'<div id="cm-resize-handle" aria-hidden="true">' + resizeSvg + '</div>',
|
|
594
|
+
'<div id="cm-header"><img id="cm-header-mascot" src="' + escAttr(this.config.mascotUrl) + '" alt="" /><h3 id="cm-title">' + esc(this.config.assistantName) + '</h3><button id="cm-close" type="button" aria-label="Fermer l\'assistant">\u00d7</button></div>',
|
|
595
|
+
this.config.disclaimer ? '<div id="cm-disclaimer"><span>' + esc(this.config.disclaimerText) + '</span><button class="cm-close-note" id="cm-disclaimer-close" type="button" aria-label="Fermer l\'avertissement">\u00d7</button></div>' : "",
|
|
596
|
+
'<div id="cm-messages" aria-live="polite" aria-relevant="additions"><p id="cm-welcome">' + esc(this.config.welcomeMessage) + "</p></div>",
|
|
597
|
+
'<div id="cm-typing">L\'assistant r\u00e9fl\u00e9chit\u2026</div>',
|
|
598
|
+
'<div id="cm-error"></div>',
|
|
599
|
+
'<div id="cm-transparency"><span>' + esc(this.config.transparencyText) + '</span><button class="cm-close-note" id="cm-transparency-close" type="button" aria-label="Fermer le message de transparence">\u00d7</button></div>',
|
|
600
|
+
'<form id="cm-form"><button id="cm-mic" type="button" aria-label="Dicter un message">\ud83c\udfa4</button><textarea id="cm-input" rows="1" aria-label="Votre message" placeholder="Posez votre question\u2026"></textarea><button id="cm-send" type="submit" aria-label="Envoyer">\u27a4</button></form>',
|
|
601
|
+
"</div>"
|
|
602
|
+
].join("");
|
|
603
|
+
shadow.appendChild(root);
|
|
604
|
+
this.host = host;
|
|
605
|
+
this.root = root;
|
|
606
|
+
this.bubble = root.querySelector("#cm-bubble");
|
|
607
|
+
this.bubbleMascot = root.querySelector("#cm-bubble-mascot");
|
|
608
|
+
this.panel = root.querySelector("#cm-panel");
|
|
609
|
+
this.headerMascot = root.querySelector("#cm-header-mascot");
|
|
610
|
+
this.messages = root.querySelector("#cm-messages");
|
|
611
|
+
this.input = root.querySelector("#cm-input");
|
|
612
|
+
this.sendButton = root.querySelector("#cm-send");
|
|
613
|
+
this.micButton = root.querySelector("#cm-mic");
|
|
614
|
+
this.typing = root.querySelector("#cm-typing");
|
|
615
|
+
this.error = root.querySelector("#cm-error");
|
|
616
|
+
this.welcome = root.querySelector("#cm-welcome");
|
|
617
|
+
this.themeOverrideStyle = document.createElement("style");
|
|
618
|
+
this.themeOverrideStyle.id = "cm-theme-overrides";
|
|
619
|
+
root.appendChild(this.themeOverrideStyle);
|
|
620
|
+
this.bindMascotImageErrorHandlers();
|
|
621
|
+
this.bindAutoGrowInput();
|
|
622
|
+
this.bindResizeHandle();
|
|
623
|
+
this.applyThemeOverrides();
|
|
624
|
+
this.applyMascotTheme();
|
|
625
|
+
return true;
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
WidgetUIManager.prototype.bindAutoGrowInput = function() {
|
|
629
|
+
var inputEl = this.input;
|
|
630
|
+
if (!inputEl) return;
|
|
631
|
+
function adjustHeight() {
|
|
632
|
+
inputEl.style.height = "auto";
|
|
633
|
+
var scrollH = inputEl.scrollHeight;
|
|
634
|
+
var maxH = 200;
|
|
635
|
+
inputEl.style.height = Math.min(scrollH, maxH) + "px";
|
|
636
|
+
inputEl.style.overflowY = scrollH > maxH ? "auto" : "hidden";
|
|
637
|
+
}
|
|
638
|
+
inputEl.addEventListener("input", adjustHeight);
|
|
639
|
+
inputEl.style.overflowY = "hidden";
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
WidgetUIManager.prototype.bindResizeHandle = function() {
|
|
643
|
+
var handle = this.root ? this.root.querySelector("#cm-resize-handle") : null;
|
|
644
|
+
var panel = this.panel;
|
|
645
|
+
if (!handle || !panel) return;
|
|
646
|
+
var self = this;
|
|
647
|
+
var isLeftCorner = this.config.position !== "bottom-left";
|
|
648
|
+
|
|
649
|
+
handle.addEventListener("mousedown", function(startEvt) {
|
|
650
|
+
startEvt.preventDefault();
|
|
651
|
+
startEvt.stopPropagation();
|
|
652
|
+
var rect = panel.getBoundingClientRect();
|
|
653
|
+
self._resizeDragState = {
|
|
654
|
+
startX: startEvt.clientX,
|
|
655
|
+
startY: startEvt.clientY,
|
|
656
|
+
startW: rect.width,
|
|
657
|
+
startH: rect.height
|
|
658
|
+
};
|
|
659
|
+
document.addEventListener("mousemove", onMouseMove);
|
|
660
|
+
document.addEventListener("mouseup", onMouseUp);
|
|
661
|
+
});
|
|
662
|
+
|
|
663
|
+
function onMouseMove(evt) {
|
|
664
|
+
var st = self._resizeDragState;
|
|
665
|
+
if (!st) return;
|
|
666
|
+
var dx = evt.clientX - st.startX;
|
|
667
|
+
var dy = evt.clientY - st.startY;
|
|
668
|
+
var newW = isLeftCorner ? st.startW - dx : st.startW + dx;
|
|
669
|
+
var newH = st.startH - dy;
|
|
670
|
+
newW = Math.max(360, Math.min(960, newW));
|
|
671
|
+
newH = Math.max(480, Math.min(window.innerHeight - 40, newH));
|
|
672
|
+
panel.style.width = newW + "px";
|
|
673
|
+
panel.style.height = newH + "px";
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
function onMouseUp() {
|
|
677
|
+
self._resizeDragState = null;
|
|
678
|
+
document.removeEventListener("mousemove", onMouseMove);
|
|
679
|
+
document.removeEventListener("mouseup", onMouseUp);
|
|
680
|
+
}
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
WidgetUIManager.prototype.bindMascotImageErrorHandlers = function() {
|
|
684
|
+
var self = this;
|
|
685
|
+
[this.bubbleMascot, this.headerMascot].forEach(function(mascotNode) {
|
|
686
|
+
if (!mascotNode) return;
|
|
687
|
+
mascotNode.addEventListener("error", function() {
|
|
688
|
+
var failedUrl = mascotNode.getAttribute("src") || "";
|
|
689
|
+
var errorMessage = failedUrl ? "chargement échoué pour " + failedUrl : "chargement échoué";
|
|
690
|
+
console.error("[OurluMairie] mascot image error:", errorMessage);
|
|
691
|
+
self.showMascotLoadError(errorMessage);
|
|
692
|
+
});
|
|
693
|
+
});
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
WidgetUIManager.prototype.showMascotLoadError = function(message) {
|
|
697
|
+
var resolvedMessage = String(message || "Mascotte indisponible.").trim() || "Mascotte indisponible.";
|
|
698
|
+
this.showError("Mascotte indisponible: " + resolvedMessage);
|
|
699
|
+
if (this.bubbleMascot) {
|
|
700
|
+
this.bubbleMascot.style.display = "none";
|
|
701
|
+
this.bubbleMascot.alt = resolvedMessage;
|
|
702
|
+
}
|
|
703
|
+
if (this.headerMascot) {
|
|
704
|
+
this.headerMascot.style.display = "none";
|
|
705
|
+
}
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
WidgetUIManager.prototype.bind = function(callbacks) {
|
|
709
|
+
this.bubble.addEventListener("click", callbacks.onToggle);
|
|
710
|
+
this.root.querySelector("#cm-close").addEventListener("click", callbacks.onToggle);
|
|
711
|
+
this.root.querySelector("#cm-form").addEventListener("submit", callbacks.onSend);
|
|
712
|
+
this.micButton.addEventListener("click", callbacks.onMicToggle);
|
|
713
|
+
document.addEventListener("keydown", callbacks.onEscape);
|
|
714
|
+
var disclaimerClose = this.root.querySelector("#cm-disclaimer-close");
|
|
715
|
+
var transparencyClose = this.root.querySelector("#cm-transparency-close");
|
|
716
|
+
var rootRef = this.root;
|
|
717
|
+
if (disclaimerClose) disclaimerClose.addEventListener("click", function() { var p = rootRef.querySelector("#cm-disclaimer"); if (p) p.style.display = "none"; });
|
|
718
|
+
if (transparencyClose) transparencyClose.addEventListener("click", function() { var p = rootRef.querySelector("#cm-transparency"); if (p) p.style.display = "none"; });
|
|
719
|
+
};
|
|
720
|
+
|
|
721
|
+
WidgetUIManager.prototype.setMascotSource = function(sourceUrl) {
|
|
722
|
+
var normalizedSourceUrl = String(sourceUrl || "").trim();
|
|
723
|
+
if (!normalizedSourceUrl) return;
|
|
724
|
+
if (this.bubbleMascot) {
|
|
725
|
+
this.bubbleMascot.src = normalizedSourceUrl;
|
|
726
|
+
this.bubbleMascot.style.display = "block";
|
|
727
|
+
}
|
|
728
|
+
if (this.headerMascot) {
|
|
729
|
+
this.headerMascot.src = normalizedSourceUrl;
|
|
730
|
+
this.headerMascot.style.display = "block";
|
|
731
|
+
}
|
|
732
|
+
};
|
|
733
|
+
|
|
734
|
+
WidgetUIManager.prototype.applyMascotTheme = function() {
|
|
735
|
+
var self = this;
|
|
736
|
+
ui.resolveMascotUrlWithTheme(this.config.mascotUrl, this.config).then(function(themedMascotUrl) {
|
|
737
|
+
self.setMascotSource(themedMascotUrl);
|
|
738
|
+
}).catch(function(error) {
|
|
739
|
+
var message = (error && error.message) ? error.message : "Mascotte indisponible.";
|
|
740
|
+
console.error("[OurluMairie]", message, error);
|
|
741
|
+
self.showMascotLoadError(message);
|
|
742
|
+
});
|
|
743
|
+
};
|
|
744
|
+
|
|
745
|
+
WidgetUIManager.prototype.applyThemeOverrides = function() {
|
|
746
|
+
if (!this.root || !this.themeOverrideStyle) return;
|
|
747
|
+
var isDarkTheme = String(this.config.effectiveThemeScheme || "light").toLowerCase() === "dark";
|
|
748
|
+
var primaryColor = ui.normalizeHexColor(this.config.primaryColor, "#0066ff");
|
|
749
|
+
var panelBackgroundBaseColor = isDarkTheme ? "#111827" : this.config.panelBackgroundColor;
|
|
750
|
+
var panelBackgroundAlpha = isDarkTheme ? "0.96" : this.config.panelBackgroundAlpha;
|
|
751
|
+
var panelBackground = ui.hexToRgba(panelBackgroundBaseColor, panelBackgroundAlpha);
|
|
752
|
+
var borderRadius = Number(this.config.panelBorderRadius);
|
|
753
|
+
if (!Number.isFinite(borderRadius)) borderRadius = 16;
|
|
754
|
+
borderRadius = Math.max(0, Math.min(32, Math.round(borderRadius)));
|
|
755
|
+
var borderRadiusPx = borderRadius + "px";
|
|
756
|
+
var gradient = "linear-gradient(135deg," + primaryColor + " 0%,#0047b3 100%)";
|
|
757
|
+
var darkThemeRules = isDarkTheme ? [
|
|
758
|
+
"#cm-panel{color:#f3f4f6 !important}",
|
|
759
|
+
"#cm-messages{background:rgba(2,6,23,0.28) !important}",
|
|
760
|
+
".cm-msg.assistant{background:rgba(30,41,59,0.94) !important;color:#f8fafc !important}",
|
|
761
|
+
".cm-msg.assistant code{background:#334155 !important;color:#f8fafc !important}",
|
|
762
|
+
"#cm-form{background:#0f172a !important;border-top:1px solid #1f2937 !important}",
|
|
763
|
+
"#cm-input{background:#111827 !important;color:#f8fafc !important;border-color:#334155 !important}",
|
|
764
|
+
"#cm-input::placeholder{color:#94a3b8 !important}",
|
|
765
|
+
"#cm-error{background:#7f1d1d !important;color:#fee2e2 !important}",
|
|
766
|
+
"#cm-resize-handle{color:#94a3b8 !important}"
|
|
767
|
+
] : [
|
|
768
|
+
"#cm-messages{background:transparent !important}",
|
|
769
|
+
".cm-msg.assistant{background:#f0f4f8 !important;color:#1a1a2e !important}",
|
|
770
|
+
"#cm-form{background:#ffffff !important;border-top:1px solid #eee !important}",
|
|
771
|
+
"#cm-input{background:#ffffff !important;color:#0f172a !important;border-color:#d1d5db !important}",
|
|
772
|
+
"#cm-error{background:#f8d7da !important;color:#58151c !important}"
|
|
773
|
+
];
|
|
774
|
+
this.themeOverrideStyle.textContent = [
|
|
775
|
+
"#cm-bubble{background:" + gradient + " !important}",
|
|
776
|
+
"#cm-bubble:focus-visible{outline-color:" + primaryColor + " !important}",
|
|
777
|
+
"#cm-panel{background:" + panelBackground + " !important;border-radius:" + borderRadiusPx + " !important}",
|
|
778
|
+
"#cm-header{background:" + gradient + " !important;border-radius:" + borderRadiusPx + " " + borderRadiusPx + " 0 0 !important}",
|
|
779
|
+
"#cm-form{border-radius:0 0 " + borderRadiusPx + " " + borderRadiusPx + " !important}",
|
|
780
|
+
"#cm-send{background:" + primaryColor + " !important}",
|
|
781
|
+
".cm-msg.user{background:" + primaryColor + " !important}",
|
|
782
|
+
"#cm-input:focus{border-color:" + primaryColor + " !important;box-shadow:0 0 0 3px rgba(0,102,255,.15) !important}"
|
|
783
|
+
].concat(darkThemeRules).join("\n");
|
|
784
|
+
};
|
|
785
|
+
|
|
786
|
+
WidgetUIManager.prototype.updateTheme = function(updatedConfig) {
|
|
787
|
+
this.config = Object.assign({}, this.config, updatedConfig || {});
|
|
788
|
+
if (this.root) {
|
|
789
|
+
var titleNode = this.root.querySelector("#cm-title");
|
|
790
|
+
if (titleNode) {
|
|
791
|
+
titleNode.textContent = this.config.assistantName || "Assistant mairie";
|
|
792
|
+
}
|
|
793
|
+
if (this.panel) {
|
|
794
|
+
this.panel.setAttribute("aria-label", "Chat " + (this.config.assistantName || "Assistant mairie"));
|
|
795
|
+
}
|
|
796
|
+
var userMessages = this.root.querySelectorAll(".cm-msg.user");
|
|
797
|
+
var userBubbleColor = ui.normalizeHexColor(this.config.primaryColor, "#0066ff");
|
|
798
|
+
userMessages.forEach(function(node) {
|
|
799
|
+
node.style.background = userBubbleColor;
|
|
800
|
+
});
|
|
801
|
+
if (this.welcome && this.config.welcomeMessage) {
|
|
802
|
+
this.welcome.textContent = this.config.welcomeMessage;
|
|
803
|
+
}
|
|
804
|
+
var disclaimerSpan = this.root.querySelector("#cm-disclaimer > span");
|
|
805
|
+
if (disclaimerSpan && this.config.disclaimerText) {
|
|
806
|
+
disclaimerSpan.textContent = this.config.disclaimerText;
|
|
807
|
+
}
|
|
808
|
+
var headerMascot = this.root.querySelector("#cm-header-mascot");
|
|
809
|
+
if (headerMascot && this.config.mascotUrl) {
|
|
810
|
+
headerMascot.setAttribute("src", this.config.mascotUrl);
|
|
811
|
+
headerMascot.style.display = "";
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
this.applyThemeOverrides();
|
|
815
|
+
this.applyMascotTheme();
|
|
816
|
+
};
|
|
817
|
+
|
|
818
|
+
WidgetUIManager.prototype.setOpen = function(opened) {
|
|
819
|
+
this.panel.classList.toggle("open", opened);
|
|
820
|
+
this.bubble.style.display = opened ? "none" : "flex";
|
|
821
|
+
if (opened) this.input.focus();
|
|
822
|
+
};
|
|
823
|
+
|
|
824
|
+
WidgetUIManager.prototype.pullInput = function() {
|
|
825
|
+
var value = this.input.value.trim();
|
|
826
|
+
this.input.value = "";
|
|
827
|
+
this.input.style.height = "auto";
|
|
828
|
+
return value;
|
|
829
|
+
};
|
|
830
|
+
|
|
831
|
+
WidgetUIManager.prototype.setInput = function(value) { this.input.value = String(value || ""); };
|
|
832
|
+
WidgetUIManager.prototype.inputValue = function() { return this.input.value; };
|
|
833
|
+
WidgetUIManager.prototype.showTyping = function(visible) { this.typing.classList.toggle("visible", Boolean(visible)); };
|
|
834
|
+
|
|
835
|
+
WidgetUIManager.prototype.setComposerDisabled = function(disabled) {
|
|
836
|
+
this.input.disabled = disabled;
|
|
837
|
+
this.sendButton.disabled = disabled;
|
|
838
|
+
this.micButton.disabled = disabled;
|
|
839
|
+
};
|
|
840
|
+
|
|
841
|
+
WidgetUIManager.prototype.showError = function(message) {
|
|
842
|
+
this.error.textContent = message;
|
|
843
|
+
this.error.classList.toggle("visible", Boolean(message));
|
|
844
|
+
this._retryHandler = null;
|
|
845
|
+
};
|
|
846
|
+
|
|
847
|
+
WidgetUIManager.prototype.showRetryableError = function(message, onRetry) {
|
|
848
|
+
this.error.innerHTML = "";
|
|
849
|
+
var textNode = document.createTextNode(message + " ");
|
|
850
|
+
this.error.appendChild(textNode);
|
|
851
|
+
var retryButton = document.createElement("button");
|
|
852
|
+
retryButton.textContent = "Réessayer";
|
|
853
|
+
retryButton.style.cssText = "background:none;border:1px solid #58151c;border-radius:6px;color:#58151c;cursor:pointer;padding:4px 12px;font-size:13px;margin-left:8px;";
|
|
854
|
+
var self = this;
|
|
855
|
+
retryButton.addEventListener("click", function() {
|
|
856
|
+
self.showError("");
|
|
857
|
+
if (typeof onRetry === "function") onRetry();
|
|
858
|
+
});
|
|
859
|
+
this.error.appendChild(retryButton);
|
|
860
|
+
this.error.classList.add("visible");
|
|
861
|
+
this._retryHandler = onRetry;
|
|
862
|
+
};
|
|
863
|
+
|
|
864
|
+
WidgetUIManager.prototype.setAssistantDraftText = function(node, text) {
|
|
865
|
+
if (!node) return;
|
|
866
|
+
node.textContent = String(text || "");
|
|
867
|
+
};
|
|
868
|
+
|
|
869
|
+
WidgetUIManager.prototype.setAssistantFinalText = function(node, text) {
|
|
870
|
+
if (!node) return;
|
|
871
|
+
node.innerHTML = ui.renderAssistantMarkdown(String(text || ""));
|
|
872
|
+
};
|
|
873
|
+
|
|
874
|
+
WidgetUIManager.prototype.addMessage = function(role, text) {
|
|
875
|
+
if (this.welcome) { this.welcome.remove(); this.welcome = null; }
|
|
876
|
+
var node = document.createElement("div");
|
|
877
|
+
node.className = "cm-msg " + role;
|
|
878
|
+
if (role === "assistant") {
|
|
879
|
+
this.setAssistantDraftText(node, text);
|
|
880
|
+
} else {
|
|
881
|
+
node.textContent = text;
|
|
882
|
+
}
|
|
883
|
+
this.messages.appendChild(node);
|
|
884
|
+
this.messages.scrollTop = this.messages.scrollHeight;
|
|
885
|
+
return node;
|
|
886
|
+
};
|
|
887
|
+
|
|
888
|
+
WidgetUIManager.prototype.startAssistantStream = function() {
|
|
889
|
+
this.streamingAssistantElement = this.addMessage("assistant", "");
|
|
890
|
+
this.streamingAssistantBuffer = "";
|
|
891
|
+
};
|
|
892
|
+
|
|
893
|
+
WidgetUIManager.prototype.appendAssistantToken = function(token) {
|
|
894
|
+
if (!this.streamingAssistantElement) this.startAssistantStream();
|
|
895
|
+
var resolvedToken = String(token || "");
|
|
896
|
+
if (!resolvedToken) return;
|
|
897
|
+
this.streamingAssistantBuffer += resolvedToken;
|
|
898
|
+
this.streamingAssistantElement.textContent += resolvedToken;
|
|
899
|
+
this.messages.scrollTop = this.messages.scrollHeight;
|
|
900
|
+
};
|
|
901
|
+
|
|
902
|
+
WidgetUIManager.prototype.finalizeAssistantMessage = function(content) {
|
|
903
|
+
if (!this.streamingAssistantElement) this.startAssistantStream();
|
|
904
|
+
var resolvedContent = content || this.streamingAssistantBuffer || this.streamingAssistantElement.textContent || "";
|
|
905
|
+
this.setAssistantFinalText(this.streamingAssistantElement, resolvedContent);
|
|
906
|
+
this.streamingAssistantElement = null;
|
|
907
|
+
this.streamingAssistantBuffer = "";
|
|
908
|
+
};
|
|
909
|
+
|
|
910
|
+
WidgetUIManager.prototype.setMicListening = function(isListening) {
|
|
911
|
+
this.micButton.classList.toggle("listening", isListening);
|
|
912
|
+
this.micButton.textContent = isListening ? "\u23f9" : "\ud83c\udfa4";
|
|
913
|
+
this.micButton.setAttribute("aria-label", isListening ? "Arrêter la dictée vocale" : "Dicter un message");
|
|
914
|
+
this.input.placeholder = isListening ? "Transcription en cours\u2026" : "Posez votre question\u2026";
|
|
915
|
+
};
|
|
916
|
+
|
|
917
|
+
ui.WidgetUIManager = WidgetUIManager;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
"use strict";
|
|
921
|
+
var runtime = window.__OurluWidgetRuntimeV1 || (window.__OurluWidgetRuntimeV1 = {});
|
|
922
|
+
var ui = {};
|
|
923
|
+
|
|
924
|
+
ui.CONTAINER_ID = "ourlu-widget-root";
|
|
925
|
+
|
|
926
|
+
installColorUtils(ui);
|
|
927
|
+
installMascotTheme(ui);
|
|
928
|
+
installMarkdownRender(ui);
|
|
929
|
+
installMediaUtils(ui);
|
|
930
|
+
installEventBus(ui);
|
|
931
|
+
installWidgetCssBuilder(ui);
|
|
932
|
+
installWidgetUIManager(ui);
|
|
933
|
+
|
|
934
|
+
runtime.constants = { CONTAINER_ID: ui.CONTAINER_ID };
|
|
935
|
+
runtime.EventBus = ui.EventBus;
|
|
936
|
+
runtime.WidgetUIManager = ui.WidgetUIManager;
|
|
937
|
+
runtime.utils = {
|
|
938
|
+
trimTrailingSlash: ui.trimTrailingSlash,
|
|
939
|
+
mergeTranscript: ui.mergeTranscript,
|
|
940
|
+
resolveRecorderMimeType: ui.resolveRecorderMimeType,
|
|
941
|
+
encodeArrayBufferToBase64: ui.encodeArrayBufferToBase64,
|
|
942
|
+
resolveAbsoluteMascotUrl: ui.resolveAbsoluteMascotUrl
|
|
943
|
+
};
|
|
944
|
+
})();
|