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