@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.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,50 +340,234 @@ 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
|
+
|
|
389
|
+
// src/widget-config.ts
|
|
390
|
+
async function loadWidgetConfiguration(host, origin, deploymentId, signal) {
|
|
391
|
+
const response = await host.fetch(`${origin}/api/embed/${deploymentId}`, {
|
|
392
|
+
credentials: "include",
|
|
393
|
+
cache: "no-store",
|
|
394
|
+
signal
|
|
395
|
+
});
|
|
396
|
+
const result = await response.json();
|
|
397
|
+
if (!response.ok)
|
|
398
|
+
throw new Error(result.error ?? "Could not load the chat design.");
|
|
399
|
+
const design = result.design;
|
|
400
|
+
const color = /^#[0-9a-f]{6}$/i;
|
|
401
|
+
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 || ![
|
|
402
|
+
design.theme.primary,
|
|
403
|
+
design.theme.primaryForeground,
|
|
404
|
+
design.theme.background,
|
|
405
|
+
design.theme.foreground
|
|
406
|
+
].every((value) => typeof value === "string" && color.test(value)))
|
|
407
|
+
throw new Error("Invalid chat design response.");
|
|
408
|
+
return {
|
|
409
|
+
...resolveWidgetPlacement(design),
|
|
410
|
+
deploymentId,
|
|
411
|
+
title: design.title,
|
|
412
|
+
launcherLabel: design.launcherLabel,
|
|
413
|
+
position: design.position,
|
|
414
|
+
presentation: design.presentation,
|
|
415
|
+
launcherBackground: design.theme.primary,
|
|
416
|
+
launcherForeground: design.theme.primaryForeground,
|
|
417
|
+
panelBackground: design.theme.background,
|
|
418
|
+
panelForeground: design.theme.foreground
|
|
419
|
+
};
|
|
420
|
+
}
|
|
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
|
+
|
|
157
545
|
// src/widget.ts
|
|
158
546
|
var widgetSequence = 0;
|
|
159
547
|
function createOmniChatWidget(container, options) {
|
|
160
|
-
getOmniChatEmbedUrl(options);
|
|
548
|
+
const origin = new URL(getOmniChatEmbedUrl(options)).origin;
|
|
161
549
|
const document = container.ownerDocument;
|
|
550
|
+
const host = document.defaultView;
|
|
551
|
+
if (!host) throw new Error("A browser document is required to mount chat.");
|
|
552
|
+
const controller = new AbortController();
|
|
162
553
|
const root = document.createElement("div");
|
|
163
554
|
root.setAttribute("data-omni-chat-widget", "");
|
|
164
|
-
root.setAttribute("data-position", options.position ?? "right");
|
|
165
|
-
root.setAttribute("data-presentation", options.presentation ?? "floating");
|
|
166
555
|
const shadow = root.attachShadow({ mode: "open" });
|
|
167
556
|
const style = document.createElement("style");
|
|
168
557
|
style.textContent = widgetStyles;
|
|
169
558
|
const launcher = document.createElement("button");
|
|
170
|
-
const panel = document.createElement("
|
|
559
|
+
const panel = document.createElement("dialog");
|
|
171
560
|
const close = document.createElement("button");
|
|
172
561
|
const frame = document.createElement("div");
|
|
173
562
|
let embed;
|
|
174
563
|
let destroyed = false;
|
|
564
|
+
let loaded = false;
|
|
565
|
+
let openRequested = false;
|
|
566
|
+
let appearance = options;
|
|
175
567
|
Object.assign(root.style, {
|
|
176
568
|
position: "fixed",
|
|
177
|
-
bottom: "20px",
|
|
178
|
-
[options.position ?? "right"]: "20px",
|
|
179
569
|
zIndex: "2147483000"
|
|
180
570
|
});
|
|
181
|
-
root.style.setProperty(
|
|
182
|
-
"--omni-launcher-bg",
|
|
183
|
-
options.launcherBackground ?? "#2563eb"
|
|
184
|
-
);
|
|
185
|
-
root.style.setProperty(
|
|
186
|
-
"--omni-launcher-fg",
|
|
187
|
-
options.launcherForeground ?? "#ffffff"
|
|
188
|
-
);
|
|
189
|
-
root.style.setProperty(
|
|
190
|
-
"--omni-panel-bg",
|
|
191
|
-
options.panelBackground ?? "#ffffff"
|
|
192
|
-
);
|
|
193
|
-
root.style.setProperty(
|
|
194
|
-
"--omni-panel-fg",
|
|
195
|
-
options.panelForeground ?? "#171717"
|
|
196
|
-
);
|
|
197
571
|
launcher.type = close.type = "button";
|
|
198
572
|
launcher.className = "launcher";
|
|
199
573
|
const label = document.createElement("span");
|
|
@@ -204,7 +578,6 @@ function createOmniChatWidget(container, options) {
|
|
|
204
578
|
launcher.setAttribute("aria-label", label.textContent);
|
|
205
579
|
panel.hidden = true;
|
|
206
580
|
panel.className = "panel";
|
|
207
|
-
panel.setAttribute("role", "region");
|
|
208
581
|
panel.setAttribute("aria-label", options.title ?? "Assistant chat");
|
|
209
582
|
panel.id = `omni-chat-widget-${++widgetSequence}`;
|
|
210
583
|
launcher.setAttribute("aria-controls", panel.id);
|
|
@@ -215,22 +588,28 @@ function createOmniChatWidget(container, options) {
|
|
|
215
588
|
close.append(widgetIcon(document, "close"));
|
|
216
589
|
const closeChat = () => {
|
|
217
590
|
if (destroyed) return;
|
|
218
|
-
|
|
591
|
+
openRequested = false;
|
|
592
|
+
panelController.close();
|
|
219
593
|
launcher.hidden = false;
|
|
220
594
|
launcher.setAttribute("aria-expanded", "false");
|
|
221
595
|
launcher.setAttribute("aria-label", label.textContent);
|
|
222
596
|
launcher.querySelector("svg")?.replaceWith(widgetIcon(document, "chat"));
|
|
223
597
|
launcher.focus();
|
|
224
598
|
};
|
|
599
|
+
const panelController = createWidgetPanel(root, panel, closeChat);
|
|
225
600
|
const openChat = () => {
|
|
226
601
|
if (destroyed) return;
|
|
602
|
+
if (!loaded) {
|
|
603
|
+
openRequested = true;
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
227
606
|
embed ?? (embed = createOmniChatEmbed(frame, {
|
|
228
|
-
...
|
|
607
|
+
...appearance,
|
|
229
608
|
onRequestClose: closeChat
|
|
230
609
|
}));
|
|
231
610
|
embed.iframe.style.minHeight = "0";
|
|
232
|
-
|
|
233
|
-
launcher.hidden =
|
|
611
|
+
panelController.open();
|
|
612
|
+
launcher.hidden = appearance.presentation === "sidebar";
|
|
234
613
|
launcher.setAttribute("aria-expanded", "true");
|
|
235
614
|
launcher.setAttribute("aria-label", "Close chat");
|
|
236
615
|
launcher.querySelector("svg")?.replaceWith(widgetIcon(document, "collapse"));
|
|
@@ -246,12 +625,67 @@ function createOmniChatWidget(container, options) {
|
|
|
246
625
|
});
|
|
247
626
|
panel.append(close, frame);
|
|
248
627
|
shadow.append(style, panel, launcher);
|
|
249
|
-
|
|
628
|
+
const ready = loadWidgetConfiguration(
|
|
629
|
+
host,
|
|
630
|
+
origin,
|
|
631
|
+
options.deploymentId,
|
|
632
|
+
controller.signal
|
|
633
|
+
).then((configuration) => {
|
|
634
|
+
if (destroyed) return;
|
|
635
|
+
appearance = Object.assign(
|
|
636
|
+
configuration,
|
|
637
|
+
Object.fromEntries(
|
|
638
|
+
Object.entries(options).filter(([, value]) => value !== void 0)
|
|
639
|
+
)
|
|
640
|
+
);
|
|
641
|
+
root.dataset.position = appearance.position ?? "right";
|
|
642
|
+
root.dataset.presentation = appearance.presentation ?? "floating";
|
|
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
|
+
);
|
|
657
|
+
root.style.setProperty(
|
|
658
|
+
"--omni-launcher-bg",
|
|
659
|
+
appearance.launcherBackground
|
|
660
|
+
);
|
|
661
|
+
root.style.setProperty(
|
|
662
|
+
"--omni-launcher-fg",
|
|
663
|
+
appearance.launcherForeground
|
|
664
|
+
);
|
|
665
|
+
root.style.setProperty("--omni-panel-bg", appearance.panelBackground);
|
|
666
|
+
root.style.setProperty("--omni-panel-fg", appearance.panelForeground);
|
|
667
|
+
label.textContent = appearance.launcherLabel;
|
|
668
|
+
launcher.setAttribute("aria-label", label.textContent);
|
|
669
|
+
panel.setAttribute("aria-label", appearance.title);
|
|
670
|
+
loaded = true;
|
|
671
|
+
container.append(root);
|
|
672
|
+
if (openRequested) openChat();
|
|
673
|
+
});
|
|
674
|
+
void ready.catch((error) => {
|
|
675
|
+
if (!destroyed)
|
|
676
|
+
options.onError?.(
|
|
677
|
+
error instanceof Error ? error : new Error("Could not load chat.")
|
|
678
|
+
);
|
|
679
|
+
});
|
|
250
680
|
return {
|
|
681
|
+
/** Resolves once saved appearance is loaded and the launcher is mounted. */
|
|
682
|
+
ready,
|
|
251
683
|
open: openChat,
|
|
252
684
|
close: closeChat,
|
|
253
685
|
destroy() {
|
|
254
686
|
destroyed = true;
|
|
687
|
+
controller.abort();
|
|
688
|
+
panelController.destroy();
|
|
255
689
|
embed?.destroy();
|
|
256
690
|
root.remove();
|
|
257
691
|
}
|
|
@@ -291,142 +725,50 @@ function getOmniChatEmbedUrl(options, parentOrigin) {
|
|
|
291
725
|
options.deploymentId
|
|
292
726
|
))
|
|
293
727
|
throw new Error("A valid chat deployment ID is required.");
|
|
294
|
-
const base =
|
|
295
|
-
|
|
296
|
-
throw new Error("Chat baseUrl must be an HTTPS origin, or HTTP localhost.");
|
|
297
|
-
const url = new URL(`/embed/${options.deploymentId}`, base.origin);
|
|
728
|
+
const base = chatOrigin(options.baseUrl);
|
|
729
|
+
const url = new URL(`/embed/${options.deploymentId}`, base);
|
|
298
730
|
if (parentOrigin)
|
|
299
731
|
url.searchParams.set("parentOrigin", new URL(parentOrigin).origin);
|
|
300
732
|
return url.toString();
|
|
301
733
|
}
|
|
302
734
|
function createOmniChatEmbed(container, options) {
|
|
303
|
-
const
|
|
304
|
-
const host = document.defaultView;
|
|
735
|
+
const host = container.ownerDocument.defaultView;
|
|
305
736
|
if (!host) throw new Error("A browser document is required to mount chat.");
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
width: "100%",
|
|
316
|
-
height: "100%",
|
|
317
|
-
minHeight: "320px",
|
|
318
|
-
border: "0",
|
|
319
|
-
display: "block"
|
|
320
|
-
});
|
|
321
|
-
const chatOrigin = new URL(iframe.src).origin;
|
|
322
|
-
let destroyed = false;
|
|
323
|
-
let pending;
|
|
324
|
-
let previousToken;
|
|
325
|
-
const controller = new AbortController();
|
|
326
|
-
const mint = async () => {
|
|
327
|
-
if (options.getSession) return options.getSession();
|
|
328
|
-
const response = await host.fetch(
|
|
329
|
-
`${chatOrigin}/api/embed/${options.deploymentId}/session`,
|
|
330
|
-
{
|
|
331
|
-
method: "POST",
|
|
332
|
-
credentials: "include",
|
|
333
|
-
signal: controller.signal,
|
|
334
|
-
headers: {
|
|
335
|
-
"Content-Type": "application/json",
|
|
336
|
-
...previousToken ? { Authorization: `Bearer ${previousToken}` } : {}
|
|
337
|
-
},
|
|
338
|
-
body: "{}"
|
|
339
|
-
}
|
|
340
|
-
);
|
|
341
|
-
const result = await response.json();
|
|
342
|
-
if (!response.ok) {
|
|
343
|
-
if (response.status === 401 || response.status === 403)
|
|
344
|
-
previousToken = void 0;
|
|
345
|
-
throw new Error(result.error ?? "Could not open the chat session.");
|
|
346
|
-
}
|
|
347
|
-
return result;
|
|
348
|
-
};
|
|
349
|
-
const applicationMessage = createApplicationMessageHandler({
|
|
350
|
-
options,
|
|
351
|
-
signal: controller.signal,
|
|
352
|
-
reply: (message) => {
|
|
353
|
-
if (!destroyed)
|
|
354
|
-
iframe.contentWindow?.postMessage(
|
|
355
|
-
{ ...message, deploymentId: options.deploymentId },
|
|
356
|
-
chatOrigin
|
|
357
|
-
);
|
|
358
|
-
}
|
|
359
|
-
});
|
|
360
|
-
const receive = (event) => {
|
|
361
|
-
if (destroyed || event.source !== iframe.contentWindow || event.origin !== chatOrigin || event.data?.deploymentId !== options.deploymentId)
|
|
362
|
-
return;
|
|
363
|
-
if (event.data.type === "omni-chat:application-request" || event.data.type === "omni-chat:tool-request") {
|
|
364
|
-
void applicationMessage(event.data);
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
if (event.data.type === "omni-chat:close-request") {
|
|
368
|
-
options.onRequestClose?.();
|
|
369
|
-
return;
|
|
370
|
-
}
|
|
371
|
-
if (event.data.type !== "omni-chat:session-request") return;
|
|
372
|
-
pending ?? (pending = mint());
|
|
373
|
-
void pending.then((session) => {
|
|
374
|
-
if (destroyed) return;
|
|
375
|
-
if (typeof session?.token !== "string" || !session.token || session.token.length > 4096 || typeof session.threadId !== "string" || !session.threadId || !Number.isFinite(session.expiresIn) || session.expiresIn <= 0)
|
|
376
|
-
throw new Error("Invalid chat session response.");
|
|
377
|
-
previousToken = session.token;
|
|
378
|
-
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`,
|
|
379
746
|
{
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
type: "omni-chat:session-error",
|
|
394
|
-
deploymentId: options.deploymentId,
|
|
395
|
-
error: failure.message,
|
|
396
|
-
...options.application ? { applicationEnabled: true } : {}
|
|
397
|
-
},
|
|
398
|
-
chatOrigin
|
|
399
|
-
);
|
|
400
|
-
}).finally(() => {
|
|
401
|
-
pending = void 0;
|
|
402
|
-
});
|
|
403
|
-
};
|
|
404
|
-
host.addEventListener("message", receive);
|
|
405
|
-
container.append(iframe);
|
|
406
|
-
return {
|
|
407
|
-
iframe,
|
|
408
|
-
focus() {
|
|
409
|
-
if (destroyed) return;
|
|
410
|
-
iframe.focus();
|
|
411
|
-
iframe.contentWindow?.postMessage(
|
|
412
|
-
{
|
|
413
|
-
type: "omni-chat:focus-composer",
|
|
414
|
-
deploymentId: options.deploymentId
|
|
415
|
-
},
|
|
416
|
-
chatOrigin
|
|
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
|
+
}
|
|
417
760
|
);
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
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;
|
|
426
768
|
}
|
|
427
|
-
};
|
|
769
|
+
});
|
|
428
770
|
}
|
|
429
771
|
|
|
430
|
-
export { createOmniChatEmbed, createOmniChatWidget, getOmniChatEmbedCode, getOmniChatEmbedUrl, getOmniChatReactCode };
|
|
772
|
+
export { DEFAULT_WIDGET_OFFSET, MAX_WIDGET_OFFSET, createOmniApplicationChatEmbed, createOmniChatEmbed, createOmniChatWidget, getOmniApplicationChatUrl, getOmniChatEmbedCode, getOmniChatEmbedUrl, getOmniChatReactCode };
|
|
431
773
|
//# sourceMappingURL=embed.js.map
|
|
432
774
|
//# sourceMappingURL=embed.js.map
|