@omni-commerce/sdk 0.8.0 → 0.9.2
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 +26 -1
- package/dist/embed.cjs +506 -160
- package/dist/embed.cjs.map +1 -1
- package/dist/embed.d.cts +40 -5
- package/dist/embed.d.ts +40 -5
- package/dist/embed.global.js +58 -16
- package/dist/embed.global.js.map +1 -1
- package/dist/embed.js +503 -161
- package/dist/embed.js.map +1 -1
- package/dist/index.cjs +85 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +19168 -20085
- package/dist/index.d.ts +19168 -20085
- package/dist/index.js +85 -9
- package/dist/index.js.map +1 -1
- package/dist/react.cjs +373 -74
- package/dist/react.cjs.map +1 -1
- package/dist/react.d.cts +13 -4
- package/dist/react.d.ts +13 -4
- package/dist/react.js +380 -75
- package/dist/react.js.map +1 -1
- package/package.json +1 -1
package/dist/embed.cjs
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);
|
|
6
|
+
|
|
3
7
|
// src/application.ts
|
|
4
8
|
function boundedJson(value, maximum) {
|
|
5
9
|
const encoded = JSON.stringify(value);
|
|
@@ -83,6 +87,171 @@ function createApplicationMessageHandler(input) {
|
|
|
83
87
|
};
|
|
84
88
|
}
|
|
85
89
|
|
|
90
|
+
// ../../lib/assistant/application-chat/bridge-scope.ts
|
|
91
|
+
function matchesHostedChatScope(message, scope) {
|
|
92
|
+
return scope.deploymentId !== void 0 ? message?.deploymentId === scope.deploymentId && message.chatId === void 0 : message?.chatId === scope.chatId && message.deploymentId === void 0;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// src/hosted-chat.ts
|
|
96
|
+
var ChatSessionError = class extends Error {
|
|
97
|
+
constructor(message, status) {
|
|
98
|
+
super(message);
|
|
99
|
+
__publicField(this, "status", status);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
function mountHostedChat(container, options, target) {
|
|
103
|
+
const document = container.ownerDocument;
|
|
104
|
+
const host = document.defaultView;
|
|
105
|
+
if (!host) throw new Error("A browser document is required to mount chat.");
|
|
106
|
+
const iframe = document.createElement("iframe");
|
|
107
|
+
iframe.src = target.url;
|
|
108
|
+
iframe.title = options.title ?? "OmniCommerce assistant";
|
|
109
|
+
iframe.referrerPolicy = "strict-origin-when-cross-origin";
|
|
110
|
+
iframe.setAttribute(
|
|
111
|
+
"sandbox",
|
|
112
|
+
"allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox"
|
|
113
|
+
);
|
|
114
|
+
Object.assign(iframe.style, {
|
|
115
|
+
width: "100%",
|
|
116
|
+
height: "100%",
|
|
117
|
+
minHeight: "320px",
|
|
118
|
+
border: "0",
|
|
119
|
+
display: "block"
|
|
120
|
+
});
|
|
121
|
+
const chatOrigin2 = new URL(iframe.src).origin;
|
|
122
|
+
let destroyed = false;
|
|
123
|
+
let pending;
|
|
124
|
+
let previousToken;
|
|
125
|
+
const controller = new AbortController();
|
|
126
|
+
const applicationMessage = createApplicationMessageHandler({
|
|
127
|
+
options,
|
|
128
|
+
signal: controller.signal,
|
|
129
|
+
reply: (message) => {
|
|
130
|
+
if (!destroyed)
|
|
131
|
+
iframe.contentWindow?.postMessage(
|
|
132
|
+
{ ...message, ...target.scope },
|
|
133
|
+
chatOrigin2
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
const receive = (event) => {
|
|
138
|
+
if (destroyed || event.source !== iframe.contentWindow || event.origin !== chatOrigin2 || !matchesHostedChatScope(event.data, target.scope))
|
|
139
|
+
return;
|
|
140
|
+
if (event.data.type === "omni-chat:application-request" || event.data.type === "omni-chat:tool-request") {
|
|
141
|
+
void applicationMessage(event.data);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (event.data.type === "omni-chat:close-request") {
|
|
145
|
+
options.onRequestClose?.();
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (event.data.type !== "omni-chat:session-request" || !target.mint) return;
|
|
149
|
+
const visitorId = typeof event.data.visitorId === "string" ? event.data.visitorId : void 0;
|
|
150
|
+
pending ?? (pending = target.mint(previousToken, visitorId, controller.signal));
|
|
151
|
+
void pending.then((session) => {
|
|
152
|
+
if (destroyed) return;
|
|
153
|
+
if (typeof session?.token !== "string" || !session.token || session.token.length > 4096 || typeof session.threadId !== "string" || !session.threadId || !Number.isFinite(session.expiresIn) || session.expiresIn <= 0)
|
|
154
|
+
throw new Error("Invalid chat session response.");
|
|
155
|
+
previousToken = session.token;
|
|
156
|
+
iframe.contentWindow?.postMessage(
|
|
157
|
+
{
|
|
158
|
+
type: "omni-chat:session",
|
|
159
|
+
...target.scope,
|
|
160
|
+
session,
|
|
161
|
+
...options.application ? { applicationEnabled: true } : {}
|
|
162
|
+
},
|
|
163
|
+
chatOrigin2
|
|
164
|
+
);
|
|
165
|
+
}).catch((error) => {
|
|
166
|
+
if (destroyed) return;
|
|
167
|
+
const failure = error instanceof Error ? error : new Error("Could not open chat.");
|
|
168
|
+
if (failure instanceof ChatSessionError && [401, 403].includes(failure.status))
|
|
169
|
+
previousToken = void 0;
|
|
170
|
+
options.onError?.(failure);
|
|
171
|
+
iframe.contentWindow?.postMessage(
|
|
172
|
+
{
|
|
173
|
+
type: "omni-chat:session-error",
|
|
174
|
+
...target.scope,
|
|
175
|
+
error: failure.message,
|
|
176
|
+
...options.application ? { applicationEnabled: true } : {}
|
|
177
|
+
},
|
|
178
|
+
chatOrigin2
|
|
179
|
+
);
|
|
180
|
+
}).finally(() => {
|
|
181
|
+
pending = void 0;
|
|
182
|
+
});
|
|
183
|
+
};
|
|
184
|
+
host.addEventListener("message", receive);
|
|
185
|
+
container.append(iframe);
|
|
186
|
+
return {
|
|
187
|
+
iframe,
|
|
188
|
+
focus() {
|
|
189
|
+
if (destroyed) return;
|
|
190
|
+
iframe.focus();
|
|
191
|
+
iframe.contentWindow?.postMessage(
|
|
192
|
+
{
|
|
193
|
+
type: "omni-chat:focus-composer",
|
|
194
|
+
...target.scope
|
|
195
|
+
},
|
|
196
|
+
chatOrigin2
|
|
197
|
+
);
|
|
198
|
+
},
|
|
199
|
+
destroy() {
|
|
200
|
+
if (destroyed) return;
|
|
201
|
+
destroyed = true;
|
|
202
|
+
controller.abort();
|
|
203
|
+
previousToken = void 0;
|
|
204
|
+
host.removeEventListener("message", receive);
|
|
205
|
+
iframe.remove();
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// ../../lib/assistant/hosted-chat-origin.ts
|
|
211
|
+
function normalizeHostedChatOrigin(value) {
|
|
212
|
+
const url = new URL(value);
|
|
213
|
+
const local = url.hostname === "localhost" || url.hostname.endsWith(".localhost") || url.hostname === "127.0.0.1";
|
|
214
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && local) || url.username || url.password || url.pathname !== "/" || url.search || url.hash)
|
|
215
|
+
throw new Error("Use an HTTPS origin, or HTTP localhost for development.");
|
|
216
|
+
return url.origin;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// src/chat-origin.ts
|
|
220
|
+
function chatOrigin(baseUrl) {
|
|
221
|
+
return normalizeHostedChatOrigin(baseUrl ?? "https://omnicommerce.sg");
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// src/application-embed.ts
|
|
225
|
+
function getOmniApplicationChatUrl(options, parentOrigin, chatId) {
|
|
226
|
+
if (!/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i.test(chatId))
|
|
227
|
+
throw new Error("A valid application chat ID is required.");
|
|
228
|
+
const url = new URL("/sdk/application-chat", chatOrigin(options.baseUrl));
|
|
229
|
+
const parent = chatOrigin(parentOrigin);
|
|
230
|
+
url.searchParams.set("parentOrigin", parent);
|
|
231
|
+
url.searchParams.set("chatId", chatId);
|
|
232
|
+
const appId = options.appId?.trim() ?? parent;
|
|
233
|
+
if (!appId || appId.length > 200)
|
|
234
|
+
throw new Error("appId must contain 1\u2013200 characters.");
|
|
235
|
+
url.searchParams.set("appId", appId);
|
|
236
|
+
for (const key of ["organizationId", "agentId"]) {
|
|
237
|
+
if (options[key] === void 0) continue;
|
|
238
|
+
const value = options[key].trim();
|
|
239
|
+
if (!value || value.length > 120)
|
|
240
|
+
throw new Error(`${key} must contain 1\u2013120 characters.`);
|
|
241
|
+
url.searchParams.set(key, value);
|
|
242
|
+
}
|
|
243
|
+
return url.toString();
|
|
244
|
+
}
|
|
245
|
+
function createOmniApplicationChatEmbed(container, options) {
|
|
246
|
+
const host = container.ownerDocument.defaultView;
|
|
247
|
+
if (!host) throw new Error("A browser document is required to mount chat.");
|
|
248
|
+
const chatId = host.crypto.randomUUID();
|
|
249
|
+
return mountHostedChat(container, options, {
|
|
250
|
+
url: getOmniApplicationChatUrl(options, host.location.origin, chatId),
|
|
251
|
+
scope: { chatId }
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
86
255
|
// src/widget-chrome.ts
|
|
87
256
|
var iconPaths = {
|
|
88
257
|
chat: "M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5Z",
|
|
@@ -103,8 +272,20 @@ function widgetIcon(document, name) {
|
|
|
103
272
|
svg.append(path);
|
|
104
273
|
return svg;
|
|
105
274
|
}
|
|
275
|
+
var widgetMobileQuery = "(max-width: 640px), (max-height: 480px) and (pointer: coarse)";
|
|
106
276
|
var widgetStyles = `
|
|
107
|
-
:host {
|
|
277
|
+
:host {
|
|
278
|
+
all: initial; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; color-scheme: light dark;
|
|
279
|
+
--omni-active-bottom-offset: var(--omni-bottom-offset);
|
|
280
|
+
--omni-active-side-offset: var(--omni-side-offset);
|
|
281
|
+
--omni-side-safe-area: env(safe-area-inset-right, 0px);
|
|
282
|
+
--omni-opposite-safe-area: env(safe-area-inset-left, 0px);
|
|
283
|
+
/* Reserve 240px of panel height, the 66px launcher gap and 20px at the opposite edges. */
|
|
284
|
+
--omni-effective-bottom-offset: clamp(0px, calc(var(--omni-active-bottom-offset) + env(safe-area-inset-bottom, 0px)), max(0px, calc(100dvh - 326px - env(safe-area-inset-top, 0px))));
|
|
285
|
+
--omni-effective-side-offset: clamp(0px, calc(var(--omni-active-side-offset) + var(--omni-side-safe-area)), max(0px, calc(100vw - 260px - var(--omni-opposite-safe-area))));
|
|
286
|
+
bottom: var(--omni-effective-bottom-offset); right: var(--omni-effective-side-offset);
|
|
287
|
+
width: max-content; max-width: calc(100vw - var(--omni-effective-side-offset) - 20px - var(--omni-opposite-safe-area));
|
|
288
|
+
}
|
|
108
289
|
*, *::before, *::after { box-sizing: border-box; }
|
|
109
290
|
button { font: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }
|
|
110
291
|
button:focus-visible { outline: 3px solid var(--omni-launcher-bg); outline-offset: 4px; }
|
|
@@ -117,19 +298,23 @@ var widgetStyles = `
|
|
|
117
298
|
box-shadow: 0 6px 24px rgba(0,0,0,.16), 0 2px 6px rgba(0,0,0,.08);
|
|
118
299
|
transition: transform 160ms ease, box-shadow 160ms ease;
|
|
119
300
|
margin-left: auto;
|
|
301
|
+
max-width: 100%;
|
|
120
302
|
}
|
|
303
|
+
.label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
121
304
|
.launcher:hover { transform: translateY(-2px); box-shadow: 0 10px 28px rgba(0,0,0,.2); }
|
|
122
305
|
.launcher[hidden] { display: none; }
|
|
123
306
|
.launcher[aria-expanded="true"] { width: 52px; padding: 0; }
|
|
124
307
|
.launcher[aria-expanded="true"] .label { display: none; }
|
|
125
308
|
.panel {
|
|
126
|
-
position: absolute; bottom: 66px; right: 0; width: min(392px, calc(100vw -
|
|
127
|
-
|
|
309
|
+
position: absolute; top: auto; bottom: 66px; right: 0; left: auto; width: min(392px, calc(100vw - var(--omni-effective-side-offset) - 20px - var(--omni-opposite-safe-area)));
|
|
310
|
+
margin: 0; padding: 0; max-width: none; max-height: none; color: var(--omni-panel-fg);
|
|
311
|
+
overflow: hidden; border: 0; border-radius: 20px;
|
|
128
312
|
background: var(--omni-panel-bg); box-shadow: 0 24px 80px rgba(0,0,0,.18), 0 4px 16px rgba(0,0,0,.06);
|
|
129
313
|
animation: omni-chat-enter 180ms ease-out;
|
|
130
314
|
}
|
|
131
315
|
.panel[hidden] { display: none; }
|
|
132
|
-
.
|
|
316
|
+
.panel::backdrop { background: transparent; }
|
|
317
|
+
.frame { height: min(780px, calc(100dvh - var(--omni-effective-bottom-offset) - 92px - env(safe-area-inset-top, 0px))); min-height: 240px; }
|
|
133
318
|
.close {
|
|
134
319
|
position: absolute; z-index: 1; top: 18px; right: 14px;
|
|
135
320
|
display: flex; align-items: center; justify-content: center; width: 34px; height: 34px;
|
|
@@ -138,11 +323,16 @@ var widgetStyles = `
|
|
|
138
323
|
}
|
|
139
324
|
.close svg { width: 17px; height: 17px; }
|
|
140
325
|
.close:hover { background: color-mix(in srgb, var(--omni-panel-fg) 6%, var(--omni-panel-bg)); }
|
|
326
|
+
:host([data-position="left"]) {
|
|
327
|
+
--omni-side-safe-area: env(safe-area-inset-left, 0px);
|
|
328
|
+
--omni-opposite-safe-area: env(safe-area-inset-right, 0px);
|
|
329
|
+
right: auto; left: var(--omni-effective-side-offset);
|
|
330
|
+
}
|
|
141
331
|
:host([data-position="left"]) .panel { right: auto; left: 0; }
|
|
142
332
|
:host([data-position="left"]) .launcher { margin-left: 0; }
|
|
143
333
|
:host([data-presentation="sidebar"]) .panel {
|
|
144
334
|
position: fixed; top: 0; bottom: 0; right: 0; left: auto;
|
|
145
|
-
width: min(440px, 100vw); border-radius: 0;
|
|
335
|
+
width: min(440px, 100vw); border-radius: 0;
|
|
146
336
|
animation: omni-sidebar-enter 180ms ease-out;
|
|
147
337
|
}
|
|
148
338
|
:host([data-presentation="sidebar"]) .frame { height: 100dvh; min-height: 0; }
|
|
@@ -152,50 +342,234 @@ var widgetStyles = `
|
|
|
152
342
|
@keyframes omni-sidebar-enter { from { opacity: 0; transform: translateX(100%); } to { opacity: 1; transform: none; } }
|
|
153
343
|
@keyframes omni-sidebar-enter-left { from { opacity: 0; transform: translateX(-100%); } to { opacity: 1; transform: none; } }
|
|
154
344
|
@keyframes omni-chat-enter { from { opacity: 0; transform: translateY(8px) scale(.98); } to { opacity: 1; transform: none; } }
|
|
155
|
-
@
|
|
156
|
-
@media
|
|
345
|
+
@keyframes omni-chat-mobile-enter { from { opacity: 0; } to { opacity: 1; } }
|
|
346
|
+
@media ${widgetMobileQuery} {
|
|
347
|
+
:host {
|
|
348
|
+
--omni-active-bottom-offset: var(--omni-mobile-bottom-offset);
|
|
349
|
+
--omni-active-side-offset: var(--omni-mobile-side-offset);
|
|
350
|
+
/* Mobile panels fill the screen independently; only the closed pill needs to fit. */
|
|
351
|
+
--omni-effective-bottom-offset: clamp(0px, calc(var(--omni-active-bottom-offset) + env(safe-area-inset-bottom, 0px)), max(0px, calc(100dvh - 72px - env(safe-area-inset-top, 0px))));
|
|
352
|
+
--omni-effective-side-offset: clamp(0px, calc(var(--omni-active-side-offset) + var(--omni-side-safe-area)), max(0px, calc(100vw - 112px - var(--omni-opposite-safe-area))));
|
|
353
|
+
}
|
|
354
|
+
:host([data-presentation]) .panel {
|
|
355
|
+
position: fixed; top: 0; right: 0; bottom: auto; left: 0;
|
|
356
|
+
width: 100%; height: 100vh; height: var(--omni-viewport-height, 100dvh);
|
|
357
|
+
transform: translateY(var(--omni-viewport-shift, 0px));
|
|
358
|
+
transition: transform 180ms ease-out;
|
|
359
|
+
border: 0; border-radius: 0; box-shadow: none; animation: omni-chat-mobile-enter 180ms ease-out;
|
|
360
|
+
}
|
|
361
|
+
:host([data-presentation]) .frame {
|
|
362
|
+
height: 100%; min-height: 0;
|
|
363
|
+
padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
|
|
364
|
+
}
|
|
365
|
+
.launcher[aria-expanded="true"] { display: none; }
|
|
366
|
+
.close { top: calc(18px + env(safe-area-inset-top, 0px)); right: calc(14px + env(safe-area-inset-right, 0px)); }
|
|
367
|
+
}
|
|
368
|
+
@media (prefers-reduced-motion: reduce) { :host([data-presentation][data-position]) .panel { animation: none; transition: none; } .launcher { transition: none; } }
|
|
157
369
|
`;
|
|
158
370
|
|
|
371
|
+
// src/widget-placement.ts
|
|
372
|
+
var DEFAULT_WIDGET_OFFSET = 20;
|
|
373
|
+
var MAX_WIDGET_OFFSET = 500;
|
|
374
|
+
function resolveWidgetPlacement(options) {
|
|
375
|
+
const offset = (value) => {
|
|
376
|
+
if (value === void 0) return DEFAULT_WIDGET_OFFSET;
|
|
377
|
+
if (!Number.isInteger(value) || value < 0 || value > MAX_WIDGET_OFFSET)
|
|
378
|
+
throw new Error(
|
|
379
|
+
`Chat spacing must be whole pixels between 0 and ${MAX_WIDGET_OFFSET}.`
|
|
380
|
+
);
|
|
381
|
+
return value;
|
|
382
|
+
};
|
|
383
|
+
return {
|
|
384
|
+
bottomOffset: offset(options.bottomOffset),
|
|
385
|
+
sideOffset: offset(options.sideOffset),
|
|
386
|
+
mobileBottomOffset: offset(options.mobileBottomOffset),
|
|
387
|
+
mobileSideOffset: offset(options.mobileSideOffset)
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// src/widget-config.ts
|
|
392
|
+
async function loadWidgetConfiguration(host, origin, deploymentId, signal) {
|
|
393
|
+
const response = await host.fetch(`${origin}/api/embed/${deploymentId}`, {
|
|
394
|
+
credentials: "include",
|
|
395
|
+
cache: "no-store",
|
|
396
|
+
signal
|
|
397
|
+
});
|
|
398
|
+
const result = await response.json();
|
|
399
|
+
if (!response.ok)
|
|
400
|
+
throw new Error(result.error ?? "Could not load the chat design.");
|
|
401
|
+
const design = result.design;
|
|
402
|
+
const color = /^#[0-9a-f]{6}$/i;
|
|
403
|
+
if (!design || typeof design.title !== "string" || !design.title.trim() || design.title.length > 80 || typeof design.launcherLabel !== "string" || !design.launcherLabel.trim() || design.launcherLabel.length > 60 || !["left", "right"].includes(design.position) || !["floating", "sidebar"].includes(design.presentation) || !design.theme || ![
|
|
404
|
+
design.theme.primary,
|
|
405
|
+
design.theme.primaryForeground,
|
|
406
|
+
design.theme.background,
|
|
407
|
+
design.theme.foreground
|
|
408
|
+
].every((value) => typeof value === "string" && color.test(value)))
|
|
409
|
+
throw new Error("Invalid chat design response.");
|
|
410
|
+
return {
|
|
411
|
+
...resolveWidgetPlacement(design),
|
|
412
|
+
deploymentId,
|
|
413
|
+
title: design.title,
|
|
414
|
+
launcherLabel: design.launcherLabel,
|
|
415
|
+
position: design.position,
|
|
416
|
+
presentation: design.presentation,
|
|
417
|
+
launcherBackground: design.theme.primary,
|
|
418
|
+
launcherForeground: design.theme.primaryForeground,
|
|
419
|
+
panelBackground: design.theme.background,
|
|
420
|
+
panelForeground: design.theme.foreground
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// src/widget-panel.ts
|
|
425
|
+
var scrollLocks = /* @__PURE__ */ new WeakMap();
|
|
426
|
+
function lockScroll(document) {
|
|
427
|
+
let lock = scrollLocks.get(document);
|
|
428
|
+
if (!lock) {
|
|
429
|
+
const styles = [document.documentElement.style, document.body.style];
|
|
430
|
+
const saved = styles.map((style) => ({
|
|
431
|
+
value: style.getPropertyValue("overflow"),
|
|
432
|
+
priority: style.getPropertyPriority("overflow")
|
|
433
|
+
}));
|
|
434
|
+
styles.forEach(
|
|
435
|
+
(style) => style.setProperty("overflow", "hidden", "important")
|
|
436
|
+
);
|
|
437
|
+
lock = {
|
|
438
|
+
count: 0,
|
|
439
|
+
release() {
|
|
440
|
+
styles.forEach((style, index) => {
|
|
441
|
+
const { value, priority } = saved[index];
|
|
442
|
+
if (value) style.setProperty("overflow", value, priority);
|
|
443
|
+
else style.removeProperty("overflow");
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
scrollLocks.set(document, lock);
|
|
448
|
+
}
|
|
449
|
+
lock.count++;
|
|
450
|
+
return () => {
|
|
451
|
+
if (--lock.count === 0) {
|
|
452
|
+
lock.release();
|
|
453
|
+
scrollLocks.delete(document);
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
function createWidgetPanel(root, panel, onRequestClose) {
|
|
458
|
+
const host = root.ownerDocument.defaultView;
|
|
459
|
+
const mobile = host.matchMedia(widgetMobileQuery);
|
|
460
|
+
const viewport = host.visualViewport;
|
|
461
|
+
let modal = false;
|
|
462
|
+
let releaseScroll;
|
|
463
|
+
let viewportFrame;
|
|
464
|
+
const cancelViewportFrame = () => {
|
|
465
|
+
if (viewportFrame === void 0) return;
|
|
466
|
+
host.cancelAnimationFrame(viewportFrame);
|
|
467
|
+
viewportFrame = void 0;
|
|
468
|
+
};
|
|
469
|
+
const clearViewport = () => {
|
|
470
|
+
root.style.removeProperty("--omni-viewport-shift");
|
|
471
|
+
root.style.removeProperty("--omni-viewport-height");
|
|
472
|
+
};
|
|
473
|
+
const resize = () => {
|
|
474
|
+
if (panel.hidden || !mobile.matches || !viewport || viewport.scale !== 1) {
|
|
475
|
+
clearViewport();
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
const document = root.ownerDocument;
|
|
479
|
+
const viewportRoot = document.compatMode === "CSS1Compat" ? document.documentElement : document.body;
|
|
480
|
+
const height = viewportRoot.clientHeight || host.innerHeight;
|
|
481
|
+
const shift = Math.min(0, viewport.offsetTop + viewport.height - height);
|
|
482
|
+
root.style.setProperty("--omni-viewport-shift", `${shift}px`);
|
|
483
|
+
const heightValue = `${height}px`;
|
|
484
|
+
if (root.style.getPropertyValue("--omni-viewport-height") !== heightValue)
|
|
485
|
+
root.style.setProperty("--omni-viewport-height", heightValue);
|
|
486
|
+
};
|
|
487
|
+
const scheduleResize = () => {
|
|
488
|
+
if (viewportFrame !== void 0) return;
|
|
489
|
+
viewportFrame = host.requestAnimationFrame(() => {
|
|
490
|
+
viewportFrame = void 0;
|
|
491
|
+
resize();
|
|
492
|
+
});
|
|
493
|
+
};
|
|
494
|
+
const show = () => {
|
|
495
|
+
if (panel.hidden) return;
|
|
496
|
+
let focused = null;
|
|
497
|
+
if (panel.open && modal !== mobile.matches) {
|
|
498
|
+
focused = root.shadowRoot?.activeElement;
|
|
499
|
+
panel.close();
|
|
500
|
+
}
|
|
501
|
+
modal = mobile.matches;
|
|
502
|
+
if (modal) releaseScroll ?? (releaseScroll = lockScroll(root.ownerDocument));
|
|
503
|
+
else {
|
|
504
|
+
releaseScroll?.();
|
|
505
|
+
releaseScroll = void 0;
|
|
506
|
+
}
|
|
507
|
+
cancelViewportFrame();
|
|
508
|
+
resize();
|
|
509
|
+
if (!panel.open) {
|
|
510
|
+
if (modal) panel.showModal();
|
|
511
|
+
else panel.show();
|
|
512
|
+
}
|
|
513
|
+
if (focused && panel.contains(focused)) focused.focus();
|
|
514
|
+
};
|
|
515
|
+
const cancel = (event) => {
|
|
516
|
+
event.preventDefault();
|
|
517
|
+
onRequestClose();
|
|
518
|
+
};
|
|
519
|
+
const close = () => {
|
|
520
|
+
cancelViewportFrame();
|
|
521
|
+
panel.hidden = true;
|
|
522
|
+
if (panel.open) panel.close();
|
|
523
|
+
releaseScroll?.();
|
|
524
|
+
releaseScroll = void 0;
|
|
525
|
+
clearViewport();
|
|
526
|
+
};
|
|
527
|
+
mobile.addEventListener("change", show);
|
|
528
|
+
viewport?.addEventListener("resize", scheduleResize);
|
|
529
|
+
viewport?.addEventListener("scroll", scheduleResize);
|
|
530
|
+
panel.addEventListener("cancel", cancel);
|
|
531
|
+
return {
|
|
532
|
+
open() {
|
|
533
|
+
panel.hidden = false;
|
|
534
|
+
show();
|
|
535
|
+
},
|
|
536
|
+
close,
|
|
537
|
+
destroy() {
|
|
538
|
+
close();
|
|
539
|
+
mobile.removeEventListener("change", show);
|
|
540
|
+
viewport?.removeEventListener("resize", scheduleResize);
|
|
541
|
+
viewport?.removeEventListener("scroll", scheduleResize);
|
|
542
|
+
panel.removeEventListener("cancel", cancel);
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
|
|
159
547
|
// src/widget.ts
|
|
160
548
|
var widgetSequence = 0;
|
|
161
549
|
function createOmniChatWidget(container, options) {
|
|
162
|
-
getOmniChatEmbedUrl(options);
|
|
550
|
+
const origin = new URL(getOmniChatEmbedUrl(options)).origin;
|
|
163
551
|
const document = container.ownerDocument;
|
|
552
|
+
const host = document.defaultView;
|
|
553
|
+
if (!host) throw new Error("A browser document is required to mount chat.");
|
|
554
|
+
const controller = new AbortController();
|
|
164
555
|
const root = document.createElement("div");
|
|
165
556
|
root.setAttribute("data-omni-chat-widget", "");
|
|
166
|
-
root.setAttribute("data-position", options.position ?? "right");
|
|
167
|
-
root.setAttribute("data-presentation", options.presentation ?? "floating");
|
|
168
557
|
const shadow = root.attachShadow({ mode: "open" });
|
|
169
558
|
const style = document.createElement("style");
|
|
170
559
|
style.textContent = widgetStyles;
|
|
171
560
|
const launcher = document.createElement("button");
|
|
172
|
-
const panel = document.createElement("
|
|
561
|
+
const panel = document.createElement("dialog");
|
|
173
562
|
const close = document.createElement("button");
|
|
174
563
|
const frame = document.createElement("div");
|
|
175
564
|
let embed;
|
|
176
565
|
let destroyed = false;
|
|
566
|
+
let loaded = false;
|
|
567
|
+
let openRequested = false;
|
|
568
|
+
let appearance = options;
|
|
177
569
|
Object.assign(root.style, {
|
|
178
570
|
position: "fixed",
|
|
179
|
-
bottom: "20px",
|
|
180
|
-
[options.position ?? "right"]: "20px",
|
|
181
571
|
zIndex: "2147483000"
|
|
182
572
|
});
|
|
183
|
-
root.style.setProperty(
|
|
184
|
-
"--omni-launcher-bg",
|
|
185
|
-
options.launcherBackground ?? "#2563eb"
|
|
186
|
-
);
|
|
187
|
-
root.style.setProperty(
|
|
188
|
-
"--omni-launcher-fg",
|
|
189
|
-
options.launcherForeground ?? "#ffffff"
|
|
190
|
-
);
|
|
191
|
-
root.style.setProperty(
|
|
192
|
-
"--omni-panel-bg",
|
|
193
|
-
options.panelBackground ?? "#ffffff"
|
|
194
|
-
);
|
|
195
|
-
root.style.setProperty(
|
|
196
|
-
"--omni-panel-fg",
|
|
197
|
-
options.panelForeground ?? "#171717"
|
|
198
|
-
);
|
|
199
573
|
launcher.type = close.type = "button";
|
|
200
574
|
launcher.className = "launcher";
|
|
201
575
|
const label = document.createElement("span");
|
|
@@ -206,7 +580,6 @@ function createOmniChatWidget(container, options) {
|
|
|
206
580
|
launcher.setAttribute("aria-label", label.textContent);
|
|
207
581
|
panel.hidden = true;
|
|
208
582
|
panel.className = "panel";
|
|
209
|
-
panel.setAttribute("role", "region");
|
|
210
583
|
panel.setAttribute("aria-label", options.title ?? "Assistant chat");
|
|
211
584
|
panel.id = `omni-chat-widget-${++widgetSequence}`;
|
|
212
585
|
launcher.setAttribute("aria-controls", panel.id);
|
|
@@ -217,22 +590,28 @@ function createOmniChatWidget(container, options) {
|
|
|
217
590
|
close.append(widgetIcon(document, "close"));
|
|
218
591
|
const closeChat = () => {
|
|
219
592
|
if (destroyed) return;
|
|
220
|
-
|
|
593
|
+
openRequested = false;
|
|
594
|
+
panelController.close();
|
|
221
595
|
launcher.hidden = false;
|
|
222
596
|
launcher.setAttribute("aria-expanded", "false");
|
|
223
597
|
launcher.setAttribute("aria-label", label.textContent);
|
|
224
598
|
launcher.querySelector("svg")?.replaceWith(widgetIcon(document, "chat"));
|
|
225
599
|
launcher.focus();
|
|
226
600
|
};
|
|
601
|
+
const panelController = createWidgetPanel(root, panel, closeChat);
|
|
227
602
|
const openChat = () => {
|
|
228
603
|
if (destroyed) return;
|
|
604
|
+
if (!loaded) {
|
|
605
|
+
openRequested = true;
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
229
608
|
embed ?? (embed = createOmniChatEmbed(frame, {
|
|
230
|
-
...
|
|
609
|
+
...appearance,
|
|
231
610
|
onRequestClose: closeChat
|
|
232
611
|
}));
|
|
233
612
|
embed.iframe.style.minHeight = "0";
|
|
234
|
-
|
|
235
|
-
launcher.hidden =
|
|
613
|
+
panelController.open();
|
|
614
|
+
launcher.hidden = appearance.presentation === "sidebar";
|
|
236
615
|
launcher.setAttribute("aria-expanded", "true");
|
|
237
616
|
launcher.setAttribute("aria-label", "Close chat");
|
|
238
617
|
launcher.querySelector("svg")?.replaceWith(widgetIcon(document, "collapse"));
|
|
@@ -248,12 +627,67 @@ function createOmniChatWidget(container, options) {
|
|
|
248
627
|
});
|
|
249
628
|
panel.append(close, frame);
|
|
250
629
|
shadow.append(style, panel, launcher);
|
|
251
|
-
|
|
630
|
+
const ready = loadWidgetConfiguration(
|
|
631
|
+
host,
|
|
632
|
+
origin,
|
|
633
|
+
options.deploymentId,
|
|
634
|
+
controller.signal
|
|
635
|
+
).then((configuration) => {
|
|
636
|
+
if (destroyed) return;
|
|
637
|
+
appearance = Object.assign(
|
|
638
|
+
configuration,
|
|
639
|
+
Object.fromEntries(
|
|
640
|
+
Object.entries(options).filter(([, value]) => value !== void 0)
|
|
641
|
+
)
|
|
642
|
+
);
|
|
643
|
+
root.dataset.position = appearance.position ?? "right";
|
|
644
|
+
root.dataset.presentation = appearance.presentation ?? "floating";
|
|
645
|
+
const placement = resolveWidgetPlacement(appearance);
|
|
646
|
+
root.style.setProperty(
|
|
647
|
+
"--omni-bottom-offset",
|
|
648
|
+
`${placement.bottomOffset}px`
|
|
649
|
+
);
|
|
650
|
+
root.style.setProperty("--omni-side-offset", `${placement.sideOffset}px`);
|
|
651
|
+
root.style.setProperty(
|
|
652
|
+
"--omni-mobile-bottom-offset",
|
|
653
|
+
`${placement.mobileBottomOffset}px`
|
|
654
|
+
);
|
|
655
|
+
root.style.setProperty(
|
|
656
|
+
"--omni-mobile-side-offset",
|
|
657
|
+
`${placement.mobileSideOffset}px`
|
|
658
|
+
);
|
|
659
|
+
root.style.setProperty(
|
|
660
|
+
"--omni-launcher-bg",
|
|
661
|
+
appearance.launcherBackground
|
|
662
|
+
);
|
|
663
|
+
root.style.setProperty(
|
|
664
|
+
"--omni-launcher-fg",
|
|
665
|
+
appearance.launcherForeground
|
|
666
|
+
);
|
|
667
|
+
root.style.setProperty("--omni-panel-bg", appearance.panelBackground);
|
|
668
|
+
root.style.setProperty("--omni-panel-fg", appearance.panelForeground);
|
|
669
|
+
label.textContent = appearance.launcherLabel;
|
|
670
|
+
launcher.setAttribute("aria-label", label.textContent);
|
|
671
|
+
panel.setAttribute("aria-label", appearance.title);
|
|
672
|
+
loaded = true;
|
|
673
|
+
container.append(root);
|
|
674
|
+
if (openRequested) openChat();
|
|
675
|
+
});
|
|
676
|
+
void ready.catch((error) => {
|
|
677
|
+
if (!destroyed)
|
|
678
|
+
options.onError?.(
|
|
679
|
+
error instanceof Error ? error : new Error("Could not load chat.")
|
|
680
|
+
);
|
|
681
|
+
});
|
|
252
682
|
return {
|
|
683
|
+
/** Resolves once saved appearance is loaded and the launcher is mounted. */
|
|
684
|
+
ready,
|
|
253
685
|
open: openChat,
|
|
254
686
|
close: closeChat,
|
|
255
687
|
destroy() {
|
|
256
688
|
destroyed = true;
|
|
689
|
+
controller.abort();
|
|
690
|
+
panelController.destroy();
|
|
257
691
|
embed?.destroy();
|
|
258
692
|
root.remove();
|
|
259
693
|
}
|
|
@@ -293,144 +727,56 @@ function getOmniChatEmbedUrl(options, parentOrigin) {
|
|
|
293
727
|
options.deploymentId
|
|
294
728
|
))
|
|
295
729
|
throw new Error("A valid chat deployment ID is required.");
|
|
296
|
-
const base =
|
|
297
|
-
|
|
298
|
-
throw new Error("Chat baseUrl must be an HTTPS origin, or HTTP localhost.");
|
|
299
|
-
const url = new URL(`/embed/${options.deploymentId}`, base.origin);
|
|
730
|
+
const base = chatOrigin(options.baseUrl);
|
|
731
|
+
const url = new URL(`/embed/${options.deploymentId}`, base);
|
|
300
732
|
if (parentOrigin)
|
|
301
733
|
url.searchParams.set("parentOrigin", new URL(parentOrigin).origin);
|
|
302
734
|
return url.toString();
|
|
303
735
|
}
|
|
304
736
|
function createOmniChatEmbed(container, options) {
|
|
305
|
-
const
|
|
306
|
-
const host = document.defaultView;
|
|
737
|
+
const host = container.ownerDocument.defaultView;
|
|
307
738
|
if (!host) throw new Error("A browser document is required to mount chat.");
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
width: "100%",
|
|
318
|
-
height: "100%",
|
|
319
|
-
minHeight: "320px",
|
|
320
|
-
border: "0",
|
|
321
|
-
display: "block"
|
|
322
|
-
});
|
|
323
|
-
const chatOrigin = new URL(iframe.src).origin;
|
|
324
|
-
let destroyed = false;
|
|
325
|
-
let pending;
|
|
326
|
-
let previousToken;
|
|
327
|
-
const controller = new AbortController();
|
|
328
|
-
const mint = async () => {
|
|
329
|
-
if (options.getSession) return options.getSession();
|
|
330
|
-
const response = await host.fetch(
|
|
331
|
-
`${chatOrigin}/api/embed/${options.deploymentId}/session`,
|
|
332
|
-
{
|
|
333
|
-
method: "POST",
|
|
334
|
-
credentials: "include",
|
|
335
|
-
signal: controller.signal,
|
|
336
|
-
headers: {
|
|
337
|
-
"Content-Type": "application/json",
|
|
338
|
-
...previousToken ? { Authorization: `Bearer ${previousToken}` } : {}
|
|
339
|
-
},
|
|
340
|
-
body: "{}"
|
|
341
|
-
}
|
|
342
|
-
);
|
|
343
|
-
const result = await response.json();
|
|
344
|
-
if (!response.ok) {
|
|
345
|
-
if (response.status === 401 || response.status === 403)
|
|
346
|
-
previousToken = void 0;
|
|
347
|
-
throw new Error(result.error ?? "Could not open the chat session.");
|
|
348
|
-
}
|
|
349
|
-
return result;
|
|
350
|
-
};
|
|
351
|
-
const applicationMessage = createApplicationMessageHandler({
|
|
352
|
-
options,
|
|
353
|
-
signal: controller.signal,
|
|
354
|
-
reply: (message) => {
|
|
355
|
-
if (!destroyed)
|
|
356
|
-
iframe.contentWindow?.postMessage(
|
|
357
|
-
{ ...message, deploymentId: options.deploymentId },
|
|
358
|
-
chatOrigin
|
|
359
|
-
);
|
|
360
|
-
}
|
|
361
|
-
});
|
|
362
|
-
const receive = (event) => {
|
|
363
|
-
if (destroyed || event.source !== iframe.contentWindow || event.origin !== chatOrigin || event.data?.deploymentId !== options.deploymentId)
|
|
364
|
-
return;
|
|
365
|
-
if (event.data.type === "omni-chat:application-request" || event.data.type === "omni-chat:tool-request") {
|
|
366
|
-
void applicationMessage(event.data);
|
|
367
|
-
return;
|
|
368
|
-
}
|
|
369
|
-
if (event.data.type === "omni-chat:close-request") {
|
|
370
|
-
options.onRequestClose?.();
|
|
371
|
-
return;
|
|
372
|
-
}
|
|
373
|
-
if (event.data.type !== "omni-chat:session-request") return;
|
|
374
|
-
pending ?? (pending = mint());
|
|
375
|
-
void pending.then((session) => {
|
|
376
|
-
if (destroyed) return;
|
|
377
|
-
if (typeof session?.token !== "string" || !session.token || session.token.length > 4096 || typeof session.threadId !== "string" || !session.threadId || !Number.isFinite(session.expiresIn) || session.expiresIn <= 0)
|
|
378
|
-
throw new Error("Invalid chat session response.");
|
|
379
|
-
previousToken = session.token;
|
|
380
|
-
iframe.contentWindow?.postMessage(
|
|
739
|
+
const url = getOmniChatEmbedUrl(options, host.location.origin);
|
|
740
|
+
const chatOrigin2 = new URL(url).origin;
|
|
741
|
+
return mountHostedChat(container, options, {
|
|
742
|
+
url,
|
|
743
|
+
scope: { deploymentId: options.deploymentId },
|
|
744
|
+
mint: async (previousToken, visitorId, signal) => {
|
|
745
|
+
if (options.getSession) return options.getSession();
|
|
746
|
+
const response = await host.fetch(
|
|
747
|
+
`${chatOrigin2}/api/embed/${options.deploymentId}/session`,
|
|
381
748
|
{
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
type: "omni-chat:session-error",
|
|
396
|
-
deploymentId: options.deploymentId,
|
|
397
|
-
error: failure.message,
|
|
398
|
-
...options.application ? { applicationEnabled: true } : {}
|
|
399
|
-
},
|
|
400
|
-
chatOrigin
|
|
401
|
-
);
|
|
402
|
-
}).finally(() => {
|
|
403
|
-
pending = void 0;
|
|
404
|
-
});
|
|
405
|
-
};
|
|
406
|
-
host.addEventListener("message", receive);
|
|
407
|
-
container.append(iframe);
|
|
408
|
-
return {
|
|
409
|
-
iframe,
|
|
410
|
-
focus() {
|
|
411
|
-
if (destroyed) return;
|
|
412
|
-
iframe.focus();
|
|
413
|
-
iframe.contentWindow?.postMessage(
|
|
414
|
-
{
|
|
415
|
-
type: "omni-chat:focus-composer",
|
|
416
|
-
deploymentId: options.deploymentId
|
|
417
|
-
},
|
|
418
|
-
chatOrigin
|
|
749
|
+
method: "POST",
|
|
750
|
+
credentials: "include",
|
|
751
|
+
signal,
|
|
752
|
+
headers: {
|
|
753
|
+
"Content-Type": "application/json",
|
|
754
|
+
...previousToken ? { Authorization: `Bearer ${previousToken}` } : {}
|
|
755
|
+
},
|
|
756
|
+
body: JSON.stringify(
|
|
757
|
+
visitorId && /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(
|
|
758
|
+
visitorId
|
|
759
|
+
) ? { visitorId } : {}
|
|
760
|
+
)
|
|
761
|
+
}
|
|
419
762
|
);
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
iframe.remove();
|
|
763
|
+
const result = await response.json();
|
|
764
|
+
if (!response.ok)
|
|
765
|
+
throw new ChatSessionError(
|
|
766
|
+
result.error ?? "Could not open the chat session.",
|
|
767
|
+
response.status
|
|
768
|
+
);
|
|
769
|
+
return result;
|
|
428
770
|
}
|
|
429
|
-
};
|
|
771
|
+
});
|
|
430
772
|
}
|
|
431
773
|
|
|
774
|
+
exports.DEFAULT_WIDGET_OFFSET = DEFAULT_WIDGET_OFFSET;
|
|
775
|
+
exports.MAX_WIDGET_OFFSET = MAX_WIDGET_OFFSET;
|
|
776
|
+
exports.createOmniApplicationChatEmbed = createOmniApplicationChatEmbed;
|
|
432
777
|
exports.createOmniChatEmbed = createOmniChatEmbed;
|
|
433
778
|
exports.createOmniChatWidget = createOmniChatWidget;
|
|
779
|
+
exports.getOmniApplicationChatUrl = getOmniApplicationChatUrl;
|
|
434
780
|
exports.getOmniChatEmbedCode = getOmniChatEmbedCode;
|
|
435
781
|
exports.getOmniChatEmbedUrl = getOmniChatEmbedUrl;
|
|
436
782
|
exports.getOmniChatReactCode = getOmniChatReactCode;
|