@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/react.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"use client";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
2
5
|
|
|
3
6
|
// src/react.tsx
|
|
4
7
|
import {
|
|
5
|
-
createContext,
|
|
8
|
+
createContext as createContext2,
|
|
6
9
|
useContext,
|
|
7
|
-
useEffect,
|
|
8
|
-
useRef
|
|
10
|
+
useEffect as useEffect2,
|
|
11
|
+
useRef as useRef2
|
|
9
12
|
} from "react";
|
|
10
13
|
|
|
11
14
|
// src/application.ts
|
|
@@ -91,26 +94,24 @@ function createApplicationMessageHandler(input) {
|
|
|
91
94
|
};
|
|
92
95
|
}
|
|
93
96
|
|
|
94
|
-
//
|
|
95
|
-
function
|
|
96
|
-
|
|
97
|
-
options.deploymentId
|
|
98
|
-
))
|
|
99
|
-
throw new Error("A valid chat deployment ID is required.");
|
|
100
|
-
const base = new URL(options.baseUrl ?? "https://omnicommerce.sg");
|
|
101
|
-
if (base.protocol !== "https:" && !(base.protocol === "http:" && ["localhost", "127.0.0.1"].includes(base.hostname)) || base.username || base.password || base.pathname !== "/" || base.search || base.hash)
|
|
102
|
-
throw new Error("Chat baseUrl must be an HTTPS origin, or HTTP localhost.");
|
|
103
|
-
const url = new URL(`/embed/${options.deploymentId}`, base.origin);
|
|
104
|
-
if (parentOrigin)
|
|
105
|
-
url.searchParams.set("parentOrigin", new URL(parentOrigin).origin);
|
|
106
|
-
return url.toString();
|
|
97
|
+
// ../../lib/assistant/application-chat/bridge-scope.ts
|
|
98
|
+
function matchesHostedChatScope(message, scope) {
|
|
99
|
+
return scope.deploymentId !== void 0 ? message?.deploymentId === scope.deploymentId && message.chatId === void 0 : message?.chatId === scope.chatId && message.deploymentId === void 0;
|
|
107
100
|
}
|
|
108
|
-
|
|
101
|
+
|
|
102
|
+
// src/hosted-chat.ts
|
|
103
|
+
var ChatSessionError = class extends Error {
|
|
104
|
+
constructor(message, status) {
|
|
105
|
+
super(message);
|
|
106
|
+
__publicField(this, "status", status);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
function mountHostedChat(container, options, target) {
|
|
109
110
|
const document = container.ownerDocument;
|
|
110
111
|
const host = document.defaultView;
|
|
111
112
|
if (!host) throw new Error("A browser document is required to mount chat.");
|
|
112
113
|
const iframe = document.createElement("iframe");
|
|
113
|
-
iframe.src =
|
|
114
|
+
iframe.src = target.url;
|
|
114
115
|
iframe.title = options.title ?? "OmniCommerce assistant";
|
|
115
116
|
iframe.referrerPolicy = "strict-origin-when-cross-origin";
|
|
116
117
|
iframe.setAttribute(
|
|
@@ -124,47 +125,24 @@ function createOmniChatEmbed(container, options) {
|
|
|
124
125
|
border: "0",
|
|
125
126
|
display: "block"
|
|
126
127
|
});
|
|
127
|
-
const
|
|
128
|
+
const chatOrigin2 = new URL(iframe.src).origin;
|
|
128
129
|
let destroyed = false;
|
|
129
130
|
let pending;
|
|
130
131
|
let previousToken;
|
|
131
132
|
const controller = new AbortController();
|
|
132
|
-
const mint = async () => {
|
|
133
|
-
if (options.getSession) return options.getSession();
|
|
134
|
-
const response = await host.fetch(
|
|
135
|
-
`${chatOrigin}/api/embed/${options.deploymentId}/session`,
|
|
136
|
-
{
|
|
137
|
-
method: "POST",
|
|
138
|
-
credentials: "include",
|
|
139
|
-
signal: controller.signal,
|
|
140
|
-
headers: {
|
|
141
|
-
"Content-Type": "application/json",
|
|
142
|
-
...previousToken ? { Authorization: `Bearer ${previousToken}` } : {}
|
|
143
|
-
},
|
|
144
|
-
body: "{}"
|
|
145
|
-
}
|
|
146
|
-
);
|
|
147
|
-
const result = await response.json();
|
|
148
|
-
if (!response.ok) {
|
|
149
|
-
if (response.status === 401 || response.status === 403)
|
|
150
|
-
previousToken = void 0;
|
|
151
|
-
throw new Error(result.error ?? "Could not open the chat session.");
|
|
152
|
-
}
|
|
153
|
-
return result;
|
|
154
|
-
};
|
|
155
133
|
const applicationMessage = createApplicationMessageHandler({
|
|
156
134
|
options,
|
|
157
135
|
signal: controller.signal,
|
|
158
136
|
reply: (message) => {
|
|
159
137
|
if (!destroyed)
|
|
160
138
|
iframe.contentWindow?.postMessage(
|
|
161
|
-
{ ...message,
|
|
162
|
-
|
|
139
|
+
{ ...message, ...target.scope },
|
|
140
|
+
chatOrigin2
|
|
163
141
|
);
|
|
164
142
|
}
|
|
165
143
|
});
|
|
166
144
|
const receive = (event) => {
|
|
167
|
-
if (destroyed || event.source !== iframe.contentWindow || event.origin !==
|
|
145
|
+
if (destroyed || event.source !== iframe.contentWindow || event.origin !== chatOrigin2 || !matchesHostedChatScope(event.data, target.scope))
|
|
168
146
|
return;
|
|
169
147
|
if (event.data.type === "omni-chat:application-request" || event.data.type === "omni-chat:tool-request") {
|
|
170
148
|
void applicationMessage(event.data);
|
|
@@ -174,8 +152,9 @@ function createOmniChatEmbed(container, options) {
|
|
|
174
152
|
options.onRequestClose?.();
|
|
175
153
|
return;
|
|
176
154
|
}
|
|
177
|
-
if (event.data.type !== "omni-chat:session-request") return;
|
|
178
|
-
|
|
155
|
+
if (event.data.type !== "omni-chat:session-request" || !target.mint) return;
|
|
156
|
+
const visitorId = typeof event.data.visitorId === "string" ? event.data.visitorId : void 0;
|
|
157
|
+
pending ?? (pending = target.mint(previousToken, visitorId, controller.signal));
|
|
179
158
|
void pending.then((session) => {
|
|
180
159
|
if (destroyed) return;
|
|
181
160
|
if (typeof session?.token !== "string" || !session.token || session.token.length > 4096 || typeof session.threadId !== "string" || !session.threadId || !Number.isFinite(session.expiresIn) || session.expiresIn <= 0)
|
|
@@ -184,24 +163,26 @@ function createOmniChatEmbed(container, options) {
|
|
|
184
163
|
iframe.contentWindow?.postMessage(
|
|
185
164
|
{
|
|
186
165
|
type: "omni-chat:session",
|
|
187
|
-
|
|
166
|
+
...target.scope,
|
|
188
167
|
session,
|
|
189
168
|
...options.application ? { applicationEnabled: true } : {}
|
|
190
169
|
},
|
|
191
|
-
|
|
170
|
+
chatOrigin2
|
|
192
171
|
);
|
|
193
172
|
}).catch((error) => {
|
|
194
173
|
if (destroyed) return;
|
|
195
174
|
const failure = error instanceof Error ? error : new Error("Could not open chat.");
|
|
175
|
+
if (failure instanceof ChatSessionError && [401, 403].includes(failure.status))
|
|
176
|
+
previousToken = void 0;
|
|
196
177
|
options.onError?.(failure);
|
|
197
178
|
iframe.contentWindow?.postMessage(
|
|
198
179
|
{
|
|
199
180
|
type: "omni-chat:session-error",
|
|
200
|
-
|
|
181
|
+
...target.scope,
|
|
201
182
|
error: failure.message,
|
|
202
183
|
...options.application ? { applicationEnabled: true } : {}
|
|
203
184
|
},
|
|
204
|
-
|
|
185
|
+
chatOrigin2
|
|
205
186
|
);
|
|
206
187
|
}).finally(() => {
|
|
207
188
|
pending = void 0;
|
|
@@ -217,9 +198,9 @@ function createOmniChatEmbed(container, options) {
|
|
|
217
198
|
iframe.contentWindow?.postMessage(
|
|
218
199
|
{
|
|
219
200
|
type: "omni-chat:focus-composer",
|
|
220
|
-
|
|
201
|
+
...target.scope
|
|
221
202
|
},
|
|
222
|
-
|
|
203
|
+
chatOrigin2
|
|
223
204
|
);
|
|
224
205
|
},
|
|
225
206
|
destroy() {
|
|
@@ -233,49 +214,372 @@ function createOmniChatEmbed(container, options) {
|
|
|
233
214
|
};
|
|
234
215
|
}
|
|
235
216
|
|
|
217
|
+
// ../../lib/assistant/hosted-chat-origin.ts
|
|
218
|
+
function normalizeHostedChatOrigin(value) {
|
|
219
|
+
const url = new URL(value);
|
|
220
|
+
const local = url.hostname === "localhost" || url.hostname.endsWith(".localhost") || url.hostname === "127.0.0.1";
|
|
221
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && local) || url.username || url.password || url.pathname !== "/" || url.search || url.hash)
|
|
222
|
+
throw new Error("Use an HTTPS origin, or HTTP localhost for development.");
|
|
223
|
+
return url.origin;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// src/chat-origin.ts
|
|
227
|
+
function chatOrigin(baseUrl) {
|
|
228
|
+
return normalizeHostedChatOrigin(baseUrl ?? "https://omnicommerce.sg");
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// src/application-embed.ts
|
|
232
|
+
function getOmniApplicationChatUrl(options, parentOrigin, chatId) {
|
|
233
|
+
if (!/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i.test(chatId))
|
|
234
|
+
throw new Error("A valid application chat ID is required.");
|
|
235
|
+
const url = new URL("/sdk/application-chat", chatOrigin(options.baseUrl));
|
|
236
|
+
const parent = chatOrigin(parentOrigin);
|
|
237
|
+
url.searchParams.set("parentOrigin", parent);
|
|
238
|
+
url.searchParams.set("chatId", chatId);
|
|
239
|
+
const appId = options.appId?.trim() ?? parent;
|
|
240
|
+
if (!appId || appId.length > 200)
|
|
241
|
+
throw new Error("appId must contain 1\u2013200 characters.");
|
|
242
|
+
url.searchParams.set("appId", appId);
|
|
243
|
+
for (const key of ["organizationId", "agentId"]) {
|
|
244
|
+
if (options[key] === void 0) continue;
|
|
245
|
+
const value = options[key].trim();
|
|
246
|
+
if (!value || value.length > 120)
|
|
247
|
+
throw new Error(`${key} must contain 1\u2013120 characters.`);
|
|
248
|
+
url.searchParams.set(key, value);
|
|
249
|
+
}
|
|
250
|
+
return url.toString();
|
|
251
|
+
}
|
|
252
|
+
function createOmniApplicationChatEmbed(container, options) {
|
|
253
|
+
const host = container.ownerDocument.defaultView;
|
|
254
|
+
if (!host) throw new Error("A browser document is required to mount chat.");
|
|
255
|
+
const chatId = host.crypto.randomUUID();
|
|
256
|
+
return mountHostedChat(container, options, {
|
|
257
|
+
url: getOmniApplicationChatUrl(options, host.location.origin, chatId),
|
|
258
|
+
scope: { chatId }
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// src/embed.ts
|
|
263
|
+
function getOmniChatEmbedUrl(options, parentOrigin) {
|
|
264
|
+
if (!/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i.test(
|
|
265
|
+
options.deploymentId
|
|
266
|
+
))
|
|
267
|
+
throw new Error("A valid chat deployment ID is required.");
|
|
268
|
+
const base = chatOrigin(options.baseUrl);
|
|
269
|
+
const url = new URL(`/embed/${options.deploymentId}`, base);
|
|
270
|
+
if (parentOrigin)
|
|
271
|
+
url.searchParams.set("parentOrigin", new URL(parentOrigin).origin);
|
|
272
|
+
return url.toString();
|
|
273
|
+
}
|
|
274
|
+
function createOmniChatEmbed(container, options) {
|
|
275
|
+
const host = container.ownerDocument.defaultView;
|
|
276
|
+
if (!host) throw new Error("A browser document is required to mount chat.");
|
|
277
|
+
const url = getOmniChatEmbedUrl(options, host.location.origin);
|
|
278
|
+
const chatOrigin2 = new URL(url).origin;
|
|
279
|
+
return mountHostedChat(container, options, {
|
|
280
|
+
url,
|
|
281
|
+
scope: { deploymentId: options.deploymentId },
|
|
282
|
+
mint: async (previousToken, visitorId, signal) => {
|
|
283
|
+
if (options.getSession) return options.getSession();
|
|
284
|
+
const response = await host.fetch(
|
|
285
|
+
`${chatOrigin2}/api/embed/${options.deploymentId}/session`,
|
|
286
|
+
{
|
|
287
|
+
method: "POST",
|
|
288
|
+
credentials: "include",
|
|
289
|
+
signal,
|
|
290
|
+
headers: {
|
|
291
|
+
"Content-Type": "application/json",
|
|
292
|
+
...previousToken ? { Authorization: `Bearer ${previousToken}` } : {}
|
|
293
|
+
},
|
|
294
|
+
body: JSON.stringify(
|
|
295
|
+
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(
|
|
296
|
+
visitorId
|
|
297
|
+
) ? { visitorId } : {}
|
|
298
|
+
)
|
|
299
|
+
}
|
|
300
|
+
);
|
|
301
|
+
const result = await response.json();
|
|
302
|
+
if (!response.ok)
|
|
303
|
+
throw new ChatSessionError(
|
|
304
|
+
result.error ?? "Could not open the chat session.",
|
|
305
|
+
response.status
|
|
306
|
+
);
|
|
307
|
+
return result;
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// src/chat-layout.tsx
|
|
313
|
+
import {
|
|
314
|
+
createContext,
|
|
315
|
+
useEffect,
|
|
316
|
+
useId,
|
|
317
|
+
useRef,
|
|
318
|
+
useState
|
|
319
|
+
} from "react";
|
|
320
|
+
|
|
321
|
+
// src/chat-layout-styles.ts
|
|
322
|
+
var chatLayoutQuery = "(max-width: 959px)";
|
|
323
|
+
var chatLayoutStyles = `
|
|
324
|
+
.omni-chat-layout {
|
|
325
|
+
display: grid; grid-template-columns: minmax(0, 1fr) clamp(360px, 30vw, 440px);
|
|
326
|
+
height: 100dvh; min-height: 0; min-width: 0; overflow: hidden;
|
|
327
|
+
}
|
|
328
|
+
.omni-chat-layout-content { min-height: 0; min-width: 0; overflow: auto; }
|
|
329
|
+
.omni-chat-layout-panel {
|
|
330
|
+
position: relative; inset: auto; margin: 0; padding: 0; width: 100%; height: 100%;
|
|
331
|
+
max-width: none; max-height: none; min-width: 0; min-height: 0; overflow: hidden;
|
|
332
|
+
box-sizing: border-box; border: 0; border-inline-start: 1px solid #e4e4e7;
|
|
333
|
+
background: white; color: #18181b;
|
|
334
|
+
}
|
|
335
|
+
.omni-chat-layout-panel[open] { display: flex; flex-direction: column; }
|
|
336
|
+
.omni-chat-layout-panel:not([open]) { display: none; }
|
|
337
|
+
.omni-chat-layout-frame { flex: 1; min-height: 0; min-width: 0; overflow: hidden; }
|
|
338
|
+
.omni-chat-layout-launcher, .omni-chat-layout-toolbar { display: none; }
|
|
339
|
+
.omni-chat-layout-launcher, .omni-chat-layout-toolbar button {
|
|
340
|
+
font: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent;
|
|
341
|
+
}
|
|
342
|
+
.omni-chat-layout-launcher:focus-visible, .omni-chat-layout-toolbar button:focus-visible {
|
|
343
|
+
outline: 3px solid #2563eb; outline-offset: 3px;
|
|
344
|
+
}
|
|
345
|
+
@media ${chatLayoutQuery} {
|
|
346
|
+
.omni-chat-layout { grid-template-columns: minmax(0, 1fr); }
|
|
347
|
+
.omni-chat-layout-launcher {
|
|
348
|
+
display: flex; align-items: center; justify-content: center;
|
|
349
|
+
position: fixed; right: calc(20px + env(safe-area-inset-right, 0px));
|
|
350
|
+
bottom: calc(20px + env(safe-area-inset-bottom, 0px)); z-index: 50;
|
|
351
|
+
width: 56px; height: 56px; padding: 0; border: 0; border-radius: 50%;
|
|
352
|
+
background: #18181b; color: white; box-shadow: 0 4px 16px #0003;
|
|
353
|
+
}
|
|
354
|
+
.omni-chat-layout-launcher svg { width: 24px; height: 24px; }
|
|
355
|
+
.omni-chat-layout-launcher[aria-expanded="true"] { visibility: hidden; }
|
|
356
|
+
.omni-chat-layout-panel {
|
|
357
|
+
position: fixed; top: var(--omni-chat-viewport-top, 0px); left: 0; right: 0;
|
|
358
|
+
width: 100%; height: var(--omni-chat-viewport-height, 100dvh); border: 0;
|
|
359
|
+
padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px)
|
|
360
|
+
env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
|
|
361
|
+
}
|
|
362
|
+
.omni-chat-layout-panel::backdrop { background: #0005; }
|
|
363
|
+
.omni-chat-layout-toolbar { display: flex; flex-shrink: 0; border-bottom: 1px solid #e4e4e7; }
|
|
364
|
+
.omni-chat-layout-toolbar button {
|
|
365
|
+
min-height: 44px; padding: 0 16px; border: 0; background: transparent; color: inherit;
|
|
366
|
+
font-size: 14px;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
`;
|
|
370
|
+
|
|
371
|
+
// src/chat-layout.tsx
|
|
372
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
373
|
+
var ChatLayoutCloseContext = createContext(
|
|
374
|
+
void 0
|
|
375
|
+
);
|
|
376
|
+
function ChatLayout({
|
|
377
|
+
children,
|
|
378
|
+
chat,
|
|
379
|
+
className
|
|
380
|
+
}) {
|
|
381
|
+
const panel = useRef(null);
|
|
382
|
+
const launcher = useRef(null);
|
|
383
|
+
const restoreFocus = useRef(false);
|
|
384
|
+
const [compact, setCompact] = useState(null);
|
|
385
|
+
const [open, setOpen] = useState(false);
|
|
386
|
+
const id = useId();
|
|
387
|
+
useEffect(() => {
|
|
388
|
+
const media = window.matchMedia(chatLayoutQuery);
|
|
389
|
+
const update = () => {
|
|
390
|
+
setCompact(media.matches);
|
|
391
|
+
setOpen(false);
|
|
392
|
+
};
|
|
393
|
+
update();
|
|
394
|
+
media.addEventListener("change", update);
|
|
395
|
+
return () => media.removeEventListener("change", update);
|
|
396
|
+
}, []);
|
|
397
|
+
useEffect(() => {
|
|
398
|
+
const dialog = panel.current;
|
|
399
|
+
if (!dialog || compact === null) return;
|
|
400
|
+
if (dialog.open) dialog.close();
|
|
401
|
+
if (!compact) dialog.show();
|
|
402
|
+
else if (open) dialog.showModal();
|
|
403
|
+
else if (restoreFocus.current) launcher.current?.focus();
|
|
404
|
+
restoreFocus.current = false;
|
|
405
|
+
}, [compact, open]);
|
|
406
|
+
useEffect(() => {
|
|
407
|
+
const viewport = window.visualViewport;
|
|
408
|
+
const dialog = panel.current;
|
|
409
|
+
const resize = () => {
|
|
410
|
+
if (compact && open && viewport?.scale === 1) {
|
|
411
|
+
dialog.style.setProperty(
|
|
412
|
+
"--omni-chat-viewport-top",
|
|
413
|
+
`${viewport.offsetTop}px`
|
|
414
|
+
);
|
|
415
|
+
dialog.style.setProperty(
|
|
416
|
+
"--omni-chat-viewport-height",
|
|
417
|
+
`${viewport.height}px`
|
|
418
|
+
);
|
|
419
|
+
} else {
|
|
420
|
+
dialog.style.removeProperty("--omni-chat-viewport-top");
|
|
421
|
+
dialog.style.removeProperty("--omni-chat-viewport-height");
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
resize();
|
|
425
|
+
viewport?.addEventListener("resize", resize);
|
|
426
|
+
viewport?.addEventListener("scroll", resize);
|
|
427
|
+
return () => {
|
|
428
|
+
viewport?.removeEventListener("resize", resize);
|
|
429
|
+
viewport?.removeEventListener("scroll", resize);
|
|
430
|
+
};
|
|
431
|
+
}, [compact, open]);
|
|
432
|
+
const close = () => {
|
|
433
|
+
if (!compact) return;
|
|
434
|
+
restoreFocus.current = true;
|
|
435
|
+
setOpen(false);
|
|
436
|
+
};
|
|
437
|
+
return /* @__PURE__ */ jsxs("div", { className: ["omni-chat-layout", className].filter(Boolean).join(" "), children: [
|
|
438
|
+
/* @__PURE__ */ jsx("style", { children: chatLayoutStyles }),
|
|
439
|
+
/* @__PURE__ */ jsx("div", { className: "omni-chat-layout-content", children }),
|
|
440
|
+
/* @__PURE__ */ jsx(
|
|
441
|
+
"button",
|
|
442
|
+
{
|
|
443
|
+
ref: launcher,
|
|
444
|
+
type: "button",
|
|
445
|
+
className: "omni-chat-layout-launcher",
|
|
446
|
+
"aria-label": "Open assistant",
|
|
447
|
+
"aria-controls": id,
|
|
448
|
+
"aria-expanded": compact === true && open,
|
|
449
|
+
onClick: () => setOpen(true),
|
|
450
|
+
children: /* @__PURE__ */ jsx(
|
|
451
|
+
"svg",
|
|
452
|
+
{
|
|
453
|
+
viewBox: "0 0 24 24",
|
|
454
|
+
fill: "none",
|
|
455
|
+
stroke: "currentColor",
|
|
456
|
+
strokeWidth: "1.8",
|
|
457
|
+
"aria-hidden": "true",
|
|
458
|
+
children: /* @__PURE__ */ jsx(
|
|
459
|
+
"path",
|
|
460
|
+
{
|
|
461
|
+
strokeLinecap: "round",
|
|
462
|
+
strokeLinejoin: "round",
|
|
463
|
+
d: "M21 11.5a8.5 8.5 0 0 1-12.3 7.6L3 21l1.9-5.7A8.5 8.5 0 1 1 21 11.5Z"
|
|
464
|
+
}
|
|
465
|
+
)
|
|
466
|
+
}
|
|
467
|
+
)
|
|
468
|
+
}
|
|
469
|
+
),
|
|
470
|
+
/* @__PURE__ */ jsxs(
|
|
471
|
+
"dialog",
|
|
472
|
+
{
|
|
473
|
+
ref: panel,
|
|
474
|
+
id,
|
|
475
|
+
className: "omni-chat-layout-panel",
|
|
476
|
+
"aria-label": "Application assistant",
|
|
477
|
+
onCancel: (event) => {
|
|
478
|
+
event.preventDefault();
|
|
479
|
+
close();
|
|
480
|
+
},
|
|
481
|
+
children: [
|
|
482
|
+
/* @__PURE__ */ jsx("div", { className: "omni-chat-layout-toolbar", children: /* @__PURE__ */ jsx("button", { type: "button", onClick: close, children: "\u2190 Back to application" }) }),
|
|
483
|
+
/* @__PURE__ */ jsx("div", { className: "omni-chat-layout-frame", children: /* @__PURE__ */ jsx(ChatLayoutCloseContext.Provider, { value: close, children: chat }) })
|
|
484
|
+
]
|
|
485
|
+
}
|
|
486
|
+
)
|
|
487
|
+
] });
|
|
488
|
+
}
|
|
489
|
+
|
|
236
490
|
// src/react.tsx
|
|
237
|
-
import { jsx } from "react/jsx-runtime";
|
|
238
|
-
var OmniChatContext =
|
|
491
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
492
|
+
var OmniChatContext = createContext2(null);
|
|
239
493
|
function OmniProvider({ children, ...options }) {
|
|
240
|
-
return /* @__PURE__ */
|
|
494
|
+
return /* @__PURE__ */ jsx2(OmniChatContext.Provider, { value: options, children });
|
|
495
|
+
}
|
|
496
|
+
function OmniChatLayout({
|
|
497
|
+
children,
|
|
498
|
+
className,
|
|
499
|
+
title
|
|
500
|
+
}) {
|
|
501
|
+
return /* @__PURE__ */ jsx2(
|
|
502
|
+
ChatLayout,
|
|
503
|
+
{
|
|
504
|
+
className,
|
|
505
|
+
chat: /* @__PURE__ */ jsx2(OmniChat, { title, style: { minHeight: 0 } }),
|
|
506
|
+
children
|
|
507
|
+
}
|
|
508
|
+
);
|
|
241
509
|
}
|
|
242
|
-
function OmniChat({
|
|
510
|
+
function OmniChat({
|
|
511
|
+
className,
|
|
512
|
+
style,
|
|
513
|
+
title,
|
|
514
|
+
onRequestClose
|
|
515
|
+
}) {
|
|
243
516
|
const options = useContext(OmniChatContext);
|
|
517
|
+
const closeLayout = useContext(ChatLayoutCloseContext);
|
|
244
518
|
if (!options)
|
|
245
519
|
throw new Error("OmniChat must be rendered inside OmniProvider.");
|
|
246
|
-
const container =
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
|
|
520
|
+
const container = useRef2(null);
|
|
521
|
+
const frame = useRef2(null);
|
|
522
|
+
const { baseUrl, deploymentId, onError, application, frontendTools } = options;
|
|
523
|
+
const getSession = "getSession" in options ? options.getSession : void 0;
|
|
524
|
+
const { organizationId, agentId, appId } = options;
|
|
525
|
+
const requestClose = onRequestClose ?? closeLayout ?? options.onRequestClose;
|
|
526
|
+
const callbacks = useRef2({
|
|
250
527
|
getSession,
|
|
251
528
|
onError,
|
|
252
529
|
application,
|
|
253
|
-
frontendTools
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
callbacks.current = {
|
|
258
|
-
|
|
530
|
+
frontendTools,
|
|
531
|
+
requestClose
|
|
532
|
+
});
|
|
533
|
+
useEffect2(() => {
|
|
534
|
+
callbacks.current = {
|
|
535
|
+
getSession,
|
|
536
|
+
onError,
|
|
537
|
+
application,
|
|
538
|
+
frontendTools,
|
|
539
|
+
requestClose
|
|
540
|
+
};
|
|
541
|
+
}, [getSession, onError, application, frontendTools, requestClose]);
|
|
259
542
|
const hasSessionBroker = Boolean(getSession);
|
|
260
543
|
const frameTitle = title ?? options.title;
|
|
261
|
-
|
|
544
|
+
useEffect2(() => {
|
|
262
545
|
if (!container.current) return;
|
|
263
|
-
const
|
|
546
|
+
const hostedOptions = {
|
|
264
547
|
baseUrl,
|
|
265
|
-
deploymentId,
|
|
266
|
-
getSession: hasSessionBroker ? () => callbacks.current.getSession() : void 0,
|
|
267
548
|
onError: (error) => callbacks.current.onError?.(error),
|
|
549
|
+
onRequestClose: () => callbacks.current.requestClose?.(),
|
|
268
550
|
title: frameTitle,
|
|
551
|
+
...deploymentId ? {
|
|
552
|
+
deploymentId,
|
|
553
|
+
getSession: hasSessionBroker ? () => callbacks.current.getSession() : void 0
|
|
554
|
+
} : { organizationId, agentId, appId },
|
|
269
555
|
get application() {
|
|
270
556
|
return callbacks.current.application;
|
|
271
557
|
},
|
|
272
558
|
get frontendTools() {
|
|
273
559
|
return callbacks.current.frontendTools;
|
|
274
560
|
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
561
|
+
};
|
|
562
|
+
const embed = "deploymentId" in hostedOptions ? createOmniChatEmbed(container.current, hostedOptions) : createOmniApplicationChatEmbed(container.current, hostedOptions);
|
|
563
|
+
frame.current = embed.iframe;
|
|
564
|
+
return () => {
|
|
565
|
+
frame.current = null;
|
|
566
|
+
embed.destroy();
|
|
567
|
+
};
|
|
568
|
+
}, [
|
|
569
|
+
baseUrl,
|
|
570
|
+
deploymentId,
|
|
571
|
+
organizationId,
|
|
572
|
+
agentId,
|
|
573
|
+
appId,
|
|
574
|
+
frameTitle,
|
|
575
|
+
hasSessionBroker
|
|
576
|
+
]);
|
|
577
|
+
useEffect2(() => {
|
|
578
|
+
const minimum = style?.minHeight ?? 320;
|
|
579
|
+
if (frame.current)
|
|
580
|
+
frame.current.style.minHeight = typeof minimum === "number" ? `${minimum}px` : minimum;
|
|
581
|
+
});
|
|
582
|
+
return /* @__PURE__ */ jsx2(
|
|
279
583
|
"div",
|
|
280
584
|
{
|
|
281
585
|
ref: container,
|
|
@@ -286,6 +590,7 @@ function OmniChat({ className, style, title }) {
|
|
|
286
590
|
}
|
|
287
591
|
export {
|
|
288
592
|
OmniChat,
|
|
593
|
+
OmniChatLayout,
|
|
289
594
|
OmniProvider
|
|
290
595
|
};
|
|
291
596
|
//# sourceMappingURL=react.js.map
|