@xpert-ai/chatkit-ui5 0.4.0 → 0.4.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/dist/chatkit.cjs CHANGED
@@ -48,7 +48,7 @@ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read fr
48
48
  var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
49
49
  var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
50
50
  var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
51
- var _a, _b, _setTrainingOptOut_dec, _hideHistory_dec, _showHistory_dec, _sendCustomAction_dec, _shareThread_dec, _setThreadId_dec, _setRuntimeCapabilities_dec, _setComposerValue_dec, _sendUserMessage_dec, _fetchUpdates_dec, _focusComposer_dec, _c, _opts, _frameUrl, _frame, _wrapper, _launcherCloseButton, _launcherOpen, _chatMinimizedToPet, _framePetOptionsOverride, _petClosedByContextMenu, _shadow, _petOverlay, _resolveLoaded, _loaded, _messenger, _ChatKitElementBase_instances, emitAndThrow_fn, setOptionsDataAttributes_fn, getDisplayMode_fn, getConfiguredPetOptions_fn, mergeConfiguredPetPositionDefaults_fn, getOverlayPetOptions_fn, resolvePetAssetUrl_fn, resolveOverlayPetOptions_fn, getFrameOptions_fn, setLauncherOpen_fn, setChatMinimizedToPet_fn, syncPetOverlayOptions_fn, handlePetActivate_fn, handlePetClose_fn, handlePetReply_fn, handlePetThreadSummaryActivate_fn, _handleLauncherClose, getFrameUrl_fn, setFrameUrl_fn, consumeFrameUrl_fn, _handleFrameLoad, _initialized, maybeInit_fn, _init;
51
+ var _a, _b, _setTrainingOptOut_dec, _hideHistory_dec, _showHistory_dec, _sendCustomAction_dec, _shareThread_dec, _setThreadId_dec, _setRuntimeCapabilities_dec, _setComposerValue_dec, _sendUserMessage_dec, _fetchUpdates_dec, _focusComposer_dec, _c, _opts, _frameUrl, _frame, _wrapper, _launcherCloseButton, _launcherOpen, _chatMinimizedToPet, _framePetOptionsOverride, _petClosedByContextMenu, _shadow, _petOverlay, _resolveLoaded, _loaded, _channelId, _messenger, _ChatKitElementBase_instances, emitAndThrow_fn, setOptionsDataAttributes_fn, getDisplayMode_fn, getConfiguredPetOptions_fn, mergeConfiguredPetPositionDefaults_fn, getOverlayPetOptions_fn, resolvePetAssetUrl_fn, resolveOverlayPetOptions_fn, getFrameOptions_fn, setLauncherOpen_fn, setChatMinimizedToPet_fn, syncPetOverlayOptions_fn, handlePetActivate_fn, handlePetClose_fn, handlePetReply_fn, handlePetThreadSummaryActivate_fn, _handleLauncherClose, getFrameUrl_fn, setFrameUrl_fn, consumeFrameUrl_fn, _handleFrameLoad, _initialized, maybeInit_fn, _init;
52
52
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
53
53
  const chatkitTypes = require("@xpert-ai/chatkit-types");
54
54
  class EventEmitter {
@@ -369,7 +369,7 @@ const fetchEventSourceWithRetry = async (url, params) => {
369
369
  }
370
370
  });
371
371
  };
372
- function safeRandomUUID() {
372
+ function createSecureChannelId() {
373
373
  const cryptoRef = globalThis.crypto;
374
374
  if (typeof (cryptoRef == null ? void 0 : cryptoRef.randomUUID) === "function") {
375
375
  return cryptoRef.randomUUID();
@@ -381,7 +381,30 @@ function safeRandomUUID() {
381
381
  bytes[8] = bytes[8] & 63 | 128;
382
382
  return [...bytes].map((byte) => byte.toString(16).padStart(2, "0")).join("").replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, "$1-$2-$3-$4-$5");
383
383
  }
384
- return `ck_${Date.now()}_${Math.random().toString(16).slice(2)}`;
384
+ return null;
385
+ }
386
+ function safeRandomUUID() {
387
+ return createSecureChannelId() ?? `ck_${Date.now()}_${Math.random().toString(16).slice(2)}`;
388
+ }
389
+ function isTrustedChatKitMessageEvent(event, { channelId, expectedOrigin, expectedSource }) {
390
+ const payload = event.data;
391
+ if (!payload || typeof payload !== "object" || !("__xpaiChatKit" in payload)) {
392
+ return false;
393
+ }
394
+ if (payload.__xpaiChatKit !== true) {
395
+ return false;
396
+ }
397
+ if (expectedOrigin !== "*" && event.origin !== expectedOrigin) {
398
+ return false;
399
+ }
400
+ if (event.source === expectedSource) {
401
+ return true;
402
+ }
403
+ const normalizedChannelId = channelId == null ? void 0 : channelId.trim();
404
+ if (normalizedChannelId) {
405
+ return "channelId" in payload && payload.channelId === normalizedChannelId;
406
+ }
407
+ return false;
385
408
  }
386
409
  let IntegrationError$1 = class IntegrationError extends Error {
387
410
  constructor(message) {
@@ -415,10 +438,12 @@ class BaseMessenger {
415
438
  handlers,
416
439
  target,
417
440
  targetOrigin,
441
+ channelId,
418
442
  fetch: fetch2 = window.fetch
419
443
  }) {
420
444
  __publicField(this, "targetOrigin");
421
445
  __publicField(this, "target");
446
+ __publicField(this, "channelId");
422
447
  __publicField(this, "commandHandlers");
423
448
  __publicField(this, "_fetch");
424
449
  __publicField(this, "emitter", new EventEmitter());
@@ -449,7 +474,11 @@ class BaseMessenger {
449
474
  ));
450
475
  __publicField(this, "handleMessage", async (event) => {
451
476
  var _a2, _b2, _c2, _d;
452
- if (!event.data || event.data.__xpaiChatKit !== true || event.origin !== this.targetOrigin || event.source !== this.target()) {
477
+ if (!isTrustedChatKitMessageEvent(event, {
478
+ channelId: this.channelId,
479
+ expectedOrigin: this.targetOrigin,
480
+ expectedSource: this.target()
481
+ })) {
453
482
  return;
454
483
  }
455
484
  const data = event.data;
@@ -590,6 +619,7 @@ class BaseMessenger {
590
619
  this.commandHandlers = handlers;
591
620
  this.target = target;
592
621
  this.targetOrigin = targetOrigin;
622
+ this.channelId = (channelId == null ? void 0 : channelId.trim()) || void 0;
593
623
  this._fetch = (...args) => fetch2(...args);
594
624
  }
595
625
  setTargetOrigin(targetOrigin) {
@@ -599,6 +629,7 @@ class BaseMessenger {
599
629
  var _a2;
600
630
  const message = {
601
631
  __xpaiChatKit: true,
632
+ ...this.channelId ? { channelId: this.channelId } : {},
602
633
  ...data
603
634
  };
604
635
  (_a2 = this.target()) == null ? void 0 : _a2.postMessage(message, this.targetOrigin, transfer);
@@ -714,6 +745,8 @@ const BASE_CAPABILITY_ALLOWLIST = [
714
745
  "event.response.end",
715
746
  "event.response.stop",
716
747
  "event.thread.change",
748
+ "event.project.change",
749
+ "event.connectors.change",
717
750
  "event.tool.change",
718
751
  "event.thread.load.start",
719
752
  "event.thread.load.end",
@@ -819,7 +852,7 @@ const BASE_CAPABILITY_DENYLIST = [
819
852
  "widget.Map"
820
853
  ];
821
854
  const PROFILE_TO_RULES = {
822
- "chatkit": {
855
+ chatkit: {
823
856
  allow: [...BASE_CAPABILITY_ALLOWLIST, "thread.item.image_generation"],
824
857
  deny: BASE_CAPABILITY_DENYLIST
825
858
  }
@@ -1153,6 +1186,7 @@ class PetOverlay {
1153
1186
  this.lastDragPosition = null;
1154
1187
  this.lastDragClientX = null;
1155
1188
  this.dragAnimation = null;
1189
+ this.activePointerId = null;
1156
1190
  this.isDragging = false;
1157
1191
  this.isHovering = false;
1158
1192
  this.movedDuringDrag = false;
@@ -1284,7 +1318,7 @@ class PetOverlay {
1284
1318
  };
1285
1319
  this.handlePointerDown = (event) => {
1286
1320
  var _a2, _b2;
1287
- if (event.button !== 0 || event.ctrlKey) {
1321
+ if (event.button !== 0 || event.ctrlKey || this.isDragging) {
1288
1322
  return;
1289
1323
  }
1290
1324
  this.closeContextMenu();
@@ -1310,12 +1344,17 @@ class PetOverlay {
1310
1344
  this.movedDuringDrag = false;
1311
1345
  this.dragPosition = nextPosition;
1312
1346
  this.isDragging = true;
1347
+ this.activePointerId = event.pointerId;
1313
1348
  this.clearRestingDelayTimer();
1314
1349
  this.isHovering = true;
1350
+ this.captureActivePointer();
1315
1351
  this.installDragListeners();
1316
1352
  this.rescheduleAnimation();
1317
1353
  };
1318
1354
  this.handlePointerMove = (event) => {
1355
+ if (!this.isActiveDragPointer(event)) {
1356
+ return;
1357
+ }
1319
1358
  const nextPosition = this.getNextDragPosition(event);
1320
1359
  const last = this.lastDragPosition;
1321
1360
  if (last && (Math.abs(last.x - nextPosition.x) > DRAG_DIRECTION_THRESHOLD_PX || Math.abs(last.y - nextPosition.y) > DRAG_DIRECTION_THRESHOLD_PX)) {
@@ -1335,20 +1374,18 @@ class PetOverlay {
1335
1374
  this.render();
1336
1375
  };
1337
1376
  this.handlePointerUp = (event) => {
1377
+ if (!this.isActiveDragPointer(event)) {
1378
+ return;
1379
+ }
1338
1380
  const finalPosition = this.lastDragPosition ?? this.getNextDragPosition(event);
1339
- this.isDragging = false;
1340
- this.dragPosition = null;
1341
- this.dragAnimation = null;
1342
- this.lastDragClientX = null;
1343
- this.removeDragListeners();
1344
- this.persistPosition(finalPosition);
1345
- if (event.pointerType === "mouse" && this.isPointerOverPet(event)) {
1346
- this.isHovering = true;
1347
- this.rescheduleAnimation();
1348
- } else {
1349
- this.enterRestingAfterDelay();
1350
- this.rescheduleAnimation();
1381
+ this.finishDrag(finalPosition, event);
1382
+ };
1383
+ this.handleLostPointerCapture = (event) => {
1384
+ if (!this.isActiveDragPointer(event)) {
1385
+ return;
1351
1386
  }
1387
+ const finalPosition = this.lastDragPosition ?? this.getNextDragPosition(event);
1388
+ this.finishDrag(finalPosition, event);
1352
1389
  };
1353
1390
  this.handleClick = () => {
1354
1391
  var _a2;
@@ -1552,6 +1589,7 @@ class PetOverlay {
1552
1589
  pet.addEventListener("pointerenter", this.handlePointerEnter);
1553
1590
  pet.addEventListener("pointerleave", this.handlePointerLeave);
1554
1591
  pet.addEventListener("pointerdown", this.handlePointerDown);
1592
+ pet.addEventListener("lostpointercapture", this.handleLostPointerCapture);
1555
1593
  pet.addEventListener("contextmenu", this.handleContextMenu);
1556
1594
  pet.addEventListener("click", this.handleClick);
1557
1595
  overlay.append(style, pet);
@@ -1561,6 +1599,7 @@ class PetOverlay {
1561
1599
  }
1562
1600
  removeOverlay() {
1563
1601
  var _a2;
1602
+ this.cancelActiveDrag();
1564
1603
  this.clearRestingDelayTimer();
1565
1604
  this.clearTimers();
1566
1605
  this.closeContextMenu();
@@ -1577,6 +1616,10 @@ class PetOverlay {
1577
1616
  "pointerdown",
1578
1617
  this.handlePointerDown
1579
1618
  );
1619
+ this.petElement.removeEventListener(
1620
+ "lostpointercapture",
1621
+ this.handleLostPointerCapture
1622
+ );
1580
1623
  this.petElement.removeEventListener(
1581
1624
  "contextmenu",
1582
1625
  this.handleContextMenu
@@ -2294,6 +2337,34 @@ class PetOverlay {
2294
2337
  this.render();
2295
2338
  }
2296
2339
  }
2340
+ captureActivePointer() {
2341
+ const pointerId = this.activePointerId;
2342
+ const pet = this.petElement;
2343
+ if (pointerId === null || typeof (pet == null ? void 0 : pet.setPointerCapture) !== "function") {
2344
+ return;
2345
+ }
2346
+ try {
2347
+ pet.setPointerCapture(pointerId);
2348
+ } catch {
2349
+ }
2350
+ }
2351
+ releaseActivePointer() {
2352
+ const pointerId = this.activePointerId;
2353
+ const pet = this.petElement;
2354
+ this.activePointerId = null;
2355
+ if (pointerId === null || typeof (pet == null ? void 0 : pet.hasPointerCapture) !== "function" || typeof pet.releasePointerCapture !== "function") {
2356
+ return;
2357
+ }
2358
+ try {
2359
+ if (pet.hasPointerCapture(pointerId)) {
2360
+ pet.releasePointerCapture(pointerId);
2361
+ }
2362
+ } catch {
2363
+ }
2364
+ }
2365
+ isActiveDragPointer(event) {
2366
+ return this.isDragging && event.pointerId === this.activePointerId;
2367
+ }
2297
2368
  getNextDragPosition(event) {
2298
2369
  if (!this.options) {
2299
2370
  return { x: 0, y: 0 };
@@ -2308,12 +2379,35 @@ class PetOverlay {
2308
2379
  chatkitTypes.normalizeBoundsPadding(this.options.position.boundsPadding)
2309
2380
  );
2310
2381
  }
2382
+ finishDrag(finalPosition, event) {
2383
+ this.isDragging = false;
2384
+ this.dragPosition = null;
2385
+ this.dragAnimation = null;
2386
+ this.lastDragClientX = null;
2387
+ this.removeDragListeners();
2388
+ this.releaseActivePointer();
2389
+ this.persistPosition(finalPosition);
2390
+ if (event.pointerType === "mouse" && this.isPointerOverPet(event)) {
2391
+ this.isHovering = true;
2392
+ this.rescheduleAnimation();
2393
+ } else {
2394
+ this.enterRestingAfterDelay();
2395
+ this.rescheduleAnimation();
2396
+ }
2397
+ }
2398
+ cancelActiveDrag() {
2399
+ this.isDragging = false;
2400
+ this.dragPosition = null;
2401
+ this.dragAnimation = null;
2402
+ this.lastDragPosition = null;
2403
+ this.lastDragClientX = null;
2404
+ this.removeDragListeners();
2405
+ this.releaseActivePointer();
2406
+ }
2311
2407
  installDragListeners() {
2312
2408
  window.addEventListener("pointermove", this.handlePointerMove);
2313
- window.addEventListener("pointerup", this.handlePointerUp, { once: true });
2314
- window.addEventListener("pointercancel", this.handlePointerUp, {
2315
- once: true
2316
- });
2409
+ window.addEventListener("pointerup", this.handlePointerUp);
2410
+ window.addEventListener("pointercancel", this.handlePointerUp);
2317
2411
  }
2318
2412
  removeDragListeners() {
2319
2413
  window.removeEventListener("pointermove", this.handlePointerMove);
@@ -2360,7 +2454,9 @@ class ChatKitElementBase extends (_c = HTMLElement, _focusComposer_dec = [requir
2360
2454
  __privateAdd(this, _loaded, new Promise((resolve) => {
2361
2455
  __privateSet(this, _resolveLoaded, resolve);
2362
2456
  }));
2457
+ __privateAdd(this, _channelId, createSecureChannelId());
2363
2458
  __privateAdd(this, _messenger, new ChatFrameMessenger({
2459
+ channelId: __privateGet(this, _channelId) ?? void 0,
2364
2460
  fetch: (...args) => {
2365
2461
  var _a2;
2366
2462
  const customFetch = ((_a2 = __privateGet(this, _opts)) == null ? void 0 : _a2.api) && "fetch" in __privateGet(this, _opts).api && __privateGet(this, _opts).api.fetch;
@@ -2407,6 +2503,38 @@ class ChatKitElementBase extends (_c = HTMLElement, _focusComposer_dec = [requir
2407
2503
  }
2408
2504
  return onClientTool({ name, params, id, tool_call_id });
2409
2505
  },
2506
+ onToolOutputAttachmentPreview: async (request) => {
2507
+ var _a2, _b2;
2508
+ const onRequestPreview = (_b2 = (_a2 = __privateGet(this, _opts)) == null ? void 0 : _a2.toolOutputAttachments) == null ? void 0 : _b2.onRequestPreview;
2509
+ if (!onRequestPreview) {
2510
+ __privateMethod(this, _ChatKitElementBase_instances, emitAndThrow_fn).call(this, new IntegrationError2(
2511
+ "No handler for tool-output attachment previews. Add toolOutputAttachments.onRequestPreview to your ChatKit options."
2512
+ ));
2513
+ }
2514
+ return onRequestPreview(request);
2515
+ },
2516
+ onWorkbenchClientCommand: async ({
2517
+ commandKey,
2518
+ payload,
2519
+ hostType,
2520
+ hostId,
2521
+ viewKey
2522
+ }) => {
2523
+ var _a2, _b2;
2524
+ const onClientCommand = (_b2 = (_a2 = __privateGet(this, _opts)) == null ? void 0 : _a2.workbench) == null ? void 0 : _b2.onClientCommand;
2525
+ if (!onClientCommand) {
2526
+ __privateMethod(this, _ChatKitElementBase_instances, emitAndThrow_fn).call(this, new IntegrationError2(
2527
+ `No handler for workbench client command "${commandKey}". Add workbench.onClientCommand to your ChatKit options.`
2528
+ ));
2529
+ }
2530
+ return onClientCommand({
2531
+ commandKey,
2532
+ payload,
2533
+ hostType,
2534
+ hostId,
2535
+ viewKey
2536
+ });
2537
+ },
2410
2538
  onWidgetAction: async ({
2411
2539
  action,
2412
2540
  widgetItem
@@ -2827,6 +2955,7 @@ _shadow = new WeakMap();
2827
2955
  _petOverlay = new WeakMap();
2828
2956
  _resolveLoaded = new WeakMap();
2829
2957
  _loaded = new WeakMap();
2958
+ _channelId = new WeakMap();
2830
2959
  _messenger = new WeakMap();
2831
2960
  _ChatKitElementBase_instances = new WeakSet();
2832
2961
  emitAndThrow_fn = function(error) {
@@ -3037,7 +3166,8 @@ maybeInit_fn = function() {
3037
3166
  frameURL.hash = encodeBase64({
3038
3167
  options: getInnerOptions(__privateMethod(this, _ChatKitElementBase_instances, getFrameOptions_fn).call(this, __privateGet(this, _opts))),
3039
3168
  referrer: window.location.origin,
3040
- profile: this.profile
3169
+ profile: this.profile,
3170
+ ...__privateGet(this, _channelId) ? { channelId: __privateGet(this, _channelId) } : {}
3041
3171
  });
3042
3172
  __privateGet(this, _messenger).connect();
3043
3173
  __privateGet(this, _frame).src = frameURL.toString();
@@ -3081,6 +3211,8 @@ const EVENT_HANDLER_MAP = {
3081
3211
  "chatkit.response.start": "onResponseStart",
3082
3212
  "chatkit.log": "onLog",
3083
3213
  "chatkit.thread.change": "onThreadChange",
3214
+ "chatkit.project.change": "onProjectChange",
3215
+ "chatkit.connectors.change": "onConnectorsChange",
3084
3216
  "chatkit.thread.load.start": "onThreadLoadStart",
3085
3217
  "chatkit.thread.load.end": "onThreadLoadEnd",
3086
3218
  "chatkit.ready": "onReady",
@@ -3092,6 +3224,8 @@ const UI5_EVENT_NAME_MAP = {
3092
3224
  "chatkit.response.start": "responseStart",
3093
3225
  "chatkit.log": "log",
3094
3226
  "chatkit.thread.change": "threadChange",
3227
+ "chatkit.project.change": "projectChange",
3228
+ "chatkit.connectors.change": "connectorsChange",
3095
3229
  "chatkit.thread.load.start": "threadLoadStart",
3096
3230
  "chatkit.thread.load.end": "threadLoadEnd",
3097
3231
  "chatkit.ready": "ready",