@omni-commerce/sdk 0.8.1 → 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 +24 -1
- package/dist/embed.cjs +419 -140
- package/dist/embed.cjs.map +1 -1
- package/dist/embed.d.cts +37 -4
- package/dist/embed.d.ts +37 -4
- package/dist/embed.global.js +58 -16
- package/dist/embed.global.js.map +1 -1
- package/dist/embed.js +416 -141
- 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,10 +342,52 @@ 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
|
+
|
|
159
391
|
// src/widget-config.ts
|
|
160
392
|
async function loadWidgetConfiguration(host, origin, deploymentId, signal) {
|
|
161
393
|
const response = await host.fetch(`${origin}/api/embed/${deploymentId}`, {
|
|
@@ -176,6 +408,7 @@ async function loadWidgetConfiguration(host, origin, deploymentId, signal) {
|
|
|
176
408
|
].every((value) => typeof value === "string" && color.test(value)))
|
|
177
409
|
throw new Error("Invalid chat design response.");
|
|
178
410
|
return {
|
|
411
|
+
...resolveWidgetPlacement(design),
|
|
179
412
|
deploymentId,
|
|
180
413
|
title: design.title,
|
|
181
414
|
launcherLabel: design.launcherLabel,
|
|
@@ -188,6 +421,129 @@ async function loadWidgetConfiguration(host, origin, deploymentId, signal) {
|
|
|
188
421
|
};
|
|
189
422
|
}
|
|
190
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
|
+
|
|
191
547
|
// src/widget.ts
|
|
192
548
|
var widgetSequence = 0;
|
|
193
549
|
function createOmniChatWidget(container, options) {
|
|
@@ -202,7 +558,7 @@ function createOmniChatWidget(container, options) {
|
|
|
202
558
|
const style = document.createElement("style");
|
|
203
559
|
style.textContent = widgetStyles;
|
|
204
560
|
const launcher = document.createElement("button");
|
|
205
|
-
const panel = document.createElement("
|
|
561
|
+
const panel = document.createElement("dialog");
|
|
206
562
|
const close = document.createElement("button");
|
|
207
563
|
const frame = document.createElement("div");
|
|
208
564
|
let embed;
|
|
@@ -212,7 +568,6 @@ function createOmniChatWidget(container, options) {
|
|
|
212
568
|
let appearance = options;
|
|
213
569
|
Object.assign(root.style, {
|
|
214
570
|
position: "fixed",
|
|
215
|
-
bottom: "20px",
|
|
216
571
|
zIndex: "2147483000"
|
|
217
572
|
});
|
|
218
573
|
launcher.type = close.type = "button";
|
|
@@ -225,7 +580,6 @@ function createOmniChatWidget(container, options) {
|
|
|
225
580
|
launcher.setAttribute("aria-label", label.textContent);
|
|
226
581
|
panel.hidden = true;
|
|
227
582
|
panel.className = "panel";
|
|
228
|
-
panel.setAttribute("role", "region");
|
|
229
583
|
panel.setAttribute("aria-label", options.title ?? "Assistant chat");
|
|
230
584
|
panel.id = `omni-chat-widget-${++widgetSequence}`;
|
|
231
585
|
launcher.setAttribute("aria-controls", panel.id);
|
|
@@ -237,13 +591,14 @@ function createOmniChatWidget(container, options) {
|
|
|
237
591
|
const closeChat = () => {
|
|
238
592
|
if (destroyed) return;
|
|
239
593
|
openRequested = false;
|
|
240
|
-
|
|
594
|
+
panelController.close();
|
|
241
595
|
launcher.hidden = false;
|
|
242
596
|
launcher.setAttribute("aria-expanded", "false");
|
|
243
597
|
launcher.setAttribute("aria-label", label.textContent);
|
|
244
598
|
launcher.querySelector("svg")?.replaceWith(widgetIcon(document, "chat"));
|
|
245
599
|
launcher.focus();
|
|
246
600
|
};
|
|
601
|
+
const panelController = createWidgetPanel(root, panel, closeChat);
|
|
247
602
|
const openChat = () => {
|
|
248
603
|
if (destroyed) return;
|
|
249
604
|
if (!loaded) {
|
|
@@ -255,7 +610,7 @@ function createOmniChatWidget(container, options) {
|
|
|
255
610
|
onRequestClose: closeChat
|
|
256
611
|
}));
|
|
257
612
|
embed.iframe.style.minHeight = "0";
|
|
258
|
-
|
|
613
|
+
panelController.open();
|
|
259
614
|
launcher.hidden = appearance.presentation === "sidebar";
|
|
260
615
|
launcher.setAttribute("aria-expanded", "true");
|
|
261
616
|
launcher.setAttribute("aria-label", "Close chat");
|
|
@@ -287,9 +642,20 @@ function createOmniChatWidget(container, options) {
|
|
|
287
642
|
);
|
|
288
643
|
root.dataset.position = appearance.position ?? "right";
|
|
289
644
|
root.dataset.presentation = appearance.presentation ?? "floating";
|
|
290
|
-
|
|
291
|
-
root.style.
|
|
292
|
-
|
|
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
|
+
);
|
|
293
659
|
root.style.setProperty(
|
|
294
660
|
"--omni-launcher-bg",
|
|
295
661
|
appearance.launcherBackground
|
|
@@ -321,6 +687,7 @@ function createOmniChatWidget(container, options) {
|
|
|
321
687
|
destroy() {
|
|
322
688
|
destroyed = true;
|
|
323
689
|
controller.abort();
|
|
690
|
+
panelController.destroy();
|
|
324
691
|
embed?.destroy();
|
|
325
692
|
root.remove();
|
|
326
693
|
}
|
|
@@ -360,144 +727,56 @@ function getOmniChatEmbedUrl(options, parentOrigin) {
|
|
|
360
727
|
options.deploymentId
|
|
361
728
|
))
|
|
362
729
|
throw new Error("A valid chat deployment ID is required.");
|
|
363
|
-
const base =
|
|
364
|
-
|
|
365
|
-
throw new Error("Chat baseUrl must be an HTTPS origin, or HTTP localhost.");
|
|
366
|
-
const url = new URL(`/embed/${options.deploymentId}`, base.origin);
|
|
730
|
+
const base = chatOrigin(options.baseUrl);
|
|
731
|
+
const url = new URL(`/embed/${options.deploymentId}`, base);
|
|
367
732
|
if (parentOrigin)
|
|
368
733
|
url.searchParams.set("parentOrigin", new URL(parentOrigin).origin);
|
|
369
734
|
return url.toString();
|
|
370
735
|
}
|
|
371
736
|
function createOmniChatEmbed(container, options) {
|
|
372
|
-
const
|
|
373
|
-
const host = document.defaultView;
|
|
737
|
+
const host = container.ownerDocument.defaultView;
|
|
374
738
|
if (!host) throw new Error("A browser document is required to mount chat.");
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
width: "100%",
|
|
385
|
-
height: "100%",
|
|
386
|
-
minHeight: "320px",
|
|
387
|
-
border: "0",
|
|
388
|
-
display: "block"
|
|
389
|
-
});
|
|
390
|
-
const chatOrigin = new URL(iframe.src).origin;
|
|
391
|
-
let destroyed = false;
|
|
392
|
-
let pending;
|
|
393
|
-
let previousToken;
|
|
394
|
-
const controller = new AbortController();
|
|
395
|
-
const mint = async () => {
|
|
396
|
-
if (options.getSession) return options.getSession();
|
|
397
|
-
const response = await host.fetch(
|
|
398
|
-
`${chatOrigin}/api/embed/${options.deploymentId}/session`,
|
|
399
|
-
{
|
|
400
|
-
method: "POST",
|
|
401
|
-
credentials: "include",
|
|
402
|
-
signal: controller.signal,
|
|
403
|
-
headers: {
|
|
404
|
-
"Content-Type": "application/json",
|
|
405
|
-
...previousToken ? { Authorization: `Bearer ${previousToken}` } : {}
|
|
406
|
-
},
|
|
407
|
-
body: "{}"
|
|
408
|
-
}
|
|
409
|
-
);
|
|
410
|
-
const result = await response.json();
|
|
411
|
-
if (!response.ok) {
|
|
412
|
-
if (response.status === 401 || response.status === 403)
|
|
413
|
-
previousToken = void 0;
|
|
414
|
-
throw new Error(result.error ?? "Could not open the chat session.");
|
|
415
|
-
}
|
|
416
|
-
return result;
|
|
417
|
-
};
|
|
418
|
-
const applicationMessage = createApplicationMessageHandler({
|
|
419
|
-
options,
|
|
420
|
-
signal: controller.signal,
|
|
421
|
-
reply: (message) => {
|
|
422
|
-
if (!destroyed)
|
|
423
|
-
iframe.contentWindow?.postMessage(
|
|
424
|
-
{ ...message, deploymentId: options.deploymentId },
|
|
425
|
-
chatOrigin
|
|
426
|
-
);
|
|
427
|
-
}
|
|
428
|
-
});
|
|
429
|
-
const receive = (event) => {
|
|
430
|
-
if (destroyed || event.source !== iframe.contentWindow || event.origin !== chatOrigin || event.data?.deploymentId !== options.deploymentId)
|
|
431
|
-
return;
|
|
432
|
-
if (event.data.type === "omni-chat:application-request" || event.data.type === "omni-chat:tool-request") {
|
|
433
|
-
void applicationMessage(event.data);
|
|
434
|
-
return;
|
|
435
|
-
}
|
|
436
|
-
if (event.data.type === "omni-chat:close-request") {
|
|
437
|
-
options.onRequestClose?.();
|
|
438
|
-
return;
|
|
439
|
-
}
|
|
440
|
-
if (event.data.type !== "omni-chat:session-request") return;
|
|
441
|
-
pending ?? (pending = mint());
|
|
442
|
-
void pending.then((session) => {
|
|
443
|
-
if (destroyed) return;
|
|
444
|
-
if (typeof session?.token !== "string" || !session.token || session.token.length > 4096 || typeof session.threadId !== "string" || !session.threadId || !Number.isFinite(session.expiresIn) || session.expiresIn <= 0)
|
|
445
|
-
throw new Error("Invalid chat session response.");
|
|
446
|
-
previousToken = session.token;
|
|
447
|
-
iframe.contentWindow?.postMessage(
|
|
448
|
-
{
|
|
449
|
-
type: "omni-chat:session",
|
|
450
|
-
deploymentId: options.deploymentId,
|
|
451
|
-
session,
|
|
452
|
-
...options.application ? { applicationEnabled: true } : {}
|
|
453
|
-
},
|
|
454
|
-
chatOrigin
|
|
455
|
-
);
|
|
456
|
-
}).catch((error) => {
|
|
457
|
-
if (destroyed) return;
|
|
458
|
-
const failure = error instanceof Error ? error : new Error("Could not open chat.");
|
|
459
|
-
options.onError?.(failure);
|
|
460
|
-
iframe.contentWindow?.postMessage(
|
|
461
|
-
{
|
|
462
|
-
type: "omni-chat:session-error",
|
|
463
|
-
deploymentId: options.deploymentId,
|
|
464
|
-
error: failure.message,
|
|
465
|
-
...options.application ? { applicationEnabled: true } : {}
|
|
466
|
-
},
|
|
467
|
-
chatOrigin
|
|
468
|
-
);
|
|
469
|
-
}).finally(() => {
|
|
470
|
-
pending = void 0;
|
|
471
|
-
});
|
|
472
|
-
};
|
|
473
|
-
host.addEventListener("message", receive);
|
|
474
|
-
container.append(iframe);
|
|
475
|
-
return {
|
|
476
|
-
iframe,
|
|
477
|
-
focus() {
|
|
478
|
-
if (destroyed) return;
|
|
479
|
-
iframe.focus();
|
|
480
|
-
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`,
|
|
481
748
|
{
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
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
|
+
}
|
|
486
762
|
);
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
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;
|
|
495
770
|
}
|
|
496
|
-
};
|
|
771
|
+
});
|
|
497
772
|
}
|
|
498
773
|
|
|
774
|
+
exports.DEFAULT_WIDGET_OFFSET = DEFAULT_WIDGET_OFFSET;
|
|
775
|
+
exports.MAX_WIDGET_OFFSET = MAX_WIDGET_OFFSET;
|
|
776
|
+
exports.createOmniApplicationChatEmbed = createOmniApplicationChatEmbed;
|
|
499
777
|
exports.createOmniChatEmbed = createOmniChatEmbed;
|
|
500
778
|
exports.createOmniChatWidget = createOmniChatWidget;
|
|
779
|
+
exports.getOmniApplicationChatUrl = getOmniApplicationChatUrl;
|
|
501
780
|
exports.getOmniChatEmbedCode = getOmniChatEmbedCode;
|
|
502
781
|
exports.getOmniChatEmbedUrl = getOmniChatEmbedUrl;
|
|
503
782
|
exports.getOmniChatReactCode = getOmniChatReactCode;
|