@xpert-ai/chatkit-web-component 0.4.1 → 0.5.1

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.
@@ -47,7 +47,7 @@ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read fr
47
47
  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);
48
48
  var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
49
49
  var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
50
- 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;
50
+ 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;
51
51
  class EventEmitter {
52
52
  constructor() {
53
53
  __publicField(this, "callbacks", /* @__PURE__ */ new Map());
@@ -366,7 +366,7 @@ const fetchEventSourceWithRetry = async (url, params) => {
366
366
  }
367
367
  });
368
368
  };
369
- function safeRandomUUID() {
369
+ function createSecureChannelId() {
370
370
  const cryptoRef = globalThis.crypto;
371
371
  if (typeof (cryptoRef == null ? void 0 : cryptoRef.randomUUID) === "function") {
372
372
  return cryptoRef.randomUUID();
@@ -378,7 +378,30 @@ function safeRandomUUID() {
378
378
  bytes[8] = bytes[8] & 63 | 128;
379
379
  return [...bytes].map((byte) => byte.toString(16).padStart(2, "0")).join("").replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, "$1-$2-$3-$4-$5");
380
380
  }
381
- return `ck_${Date.now()}_${Math.random().toString(16).slice(2)}`;
381
+ return null;
382
+ }
383
+ function safeRandomUUID() {
384
+ return createSecureChannelId() ?? `ck_${Date.now()}_${Math.random().toString(16).slice(2)}`;
385
+ }
386
+ function isTrustedChatKitMessageEvent(event, { channelId, expectedOrigin, expectedSource }) {
387
+ const payload = event.data;
388
+ if (!payload || typeof payload !== "object" || !("__xpaiChatKit" in payload)) {
389
+ return false;
390
+ }
391
+ if (payload.__xpaiChatKit !== true) {
392
+ return false;
393
+ }
394
+ if (expectedOrigin !== "*" && event.origin !== expectedOrigin) {
395
+ return false;
396
+ }
397
+ if (event.source === expectedSource) {
398
+ return true;
399
+ }
400
+ const normalizedChannelId = channelId == null ? void 0 : channelId.trim();
401
+ if (normalizedChannelId) {
402
+ return "channelId" in payload && payload.channelId === normalizedChannelId;
403
+ }
404
+ return false;
382
405
  }
383
406
  let IntegrationError$1 = class IntegrationError extends Error {
384
407
  constructor(message) {
@@ -412,10 +435,12 @@ class BaseMessenger {
412
435
  handlers,
413
436
  target,
414
437
  targetOrigin,
438
+ channelId,
415
439
  fetch: fetch2 = window.fetch
416
440
  }) {
417
441
  __publicField(this, "targetOrigin");
418
442
  __publicField(this, "target");
443
+ __publicField(this, "channelId");
419
444
  __publicField(this, "commandHandlers");
420
445
  __publicField(this, "_fetch");
421
446
  __publicField(this, "emitter", new EventEmitter());
@@ -446,7 +471,11 @@ class BaseMessenger {
446
471
  ));
447
472
  __publicField(this, "handleMessage", async (event) => {
448
473
  var _a2, _b2, _c2, _d;
449
- if (!event.data || event.data.__xpaiChatKit !== true || event.origin !== this.targetOrigin || event.source !== this.target()) {
474
+ if (!isTrustedChatKitMessageEvent(event, {
475
+ channelId: this.channelId,
476
+ expectedOrigin: this.targetOrigin,
477
+ expectedSource: this.target()
478
+ })) {
450
479
  return;
451
480
  }
452
481
  const data = event.data;
@@ -587,6 +616,7 @@ class BaseMessenger {
587
616
  this.commandHandlers = handlers;
588
617
  this.target = target;
589
618
  this.targetOrigin = targetOrigin;
619
+ this.channelId = (channelId == null ? void 0 : channelId.trim()) || void 0;
590
620
  this._fetch = (...args) => fetch2(...args);
591
621
  }
592
622
  setTargetOrigin(targetOrigin) {
@@ -596,6 +626,7 @@ class BaseMessenger {
596
626
  var _a2;
597
627
  const message = {
598
628
  __xpaiChatKit: true,
629
+ ...this.channelId ? { channelId: this.channelId } : {},
599
630
  ...data
600
631
  };
601
632
  (_a2 = this.target()) == null ? void 0 : _a2.postMessage(message, this.targetOrigin, transfer);
@@ -2603,7 +2634,9 @@ class ChatKitElementBase extends (_c = HTMLElement, _focusComposer_dec = [requir
2603
2634
  __privateAdd(this, _loaded, new Promise((resolve) => {
2604
2635
  __privateSet(this, _resolveLoaded, resolve);
2605
2636
  }));
2637
+ __privateAdd(this, _channelId, createSecureChannelId());
2606
2638
  __privateAdd(this, _messenger, new ChatFrameMessenger({
2639
+ channelId: __privateGet(this, _channelId) ?? void 0,
2607
2640
  fetch: (...args) => {
2608
2641
  var _a2;
2609
2642
  const customFetch = ((_a2 = __privateGet(this, _opts)) == null ? void 0 : _a2.api) && "fetch" in __privateGet(this, _opts).api && __privateGet(this, _opts).api.fetch;
@@ -2650,6 +2683,16 @@ class ChatKitElementBase extends (_c = HTMLElement, _focusComposer_dec = [requir
2650
2683
  }
2651
2684
  return onClientTool({ name, params, id, tool_call_id });
2652
2685
  },
2686
+ onToolOutputAttachmentPreview: async (request) => {
2687
+ var _a2, _b2;
2688
+ const onRequestPreview = (_b2 = (_a2 = __privateGet(this, _opts)) == null ? void 0 : _a2.toolOutputAttachments) == null ? void 0 : _b2.onRequestPreview;
2689
+ if (!onRequestPreview) {
2690
+ __privateMethod(this, _ChatKitElementBase_instances, emitAndThrow_fn).call(this, new IntegrationError2(
2691
+ "No handler for tool-output attachment previews. Add toolOutputAttachments.onRequestPreview to your ChatKit options."
2692
+ ));
2693
+ }
2694
+ return onRequestPreview(request);
2695
+ },
2653
2696
  onWorkbenchClientCommand: async ({
2654
2697
  commandKey,
2655
2698
  payload,
@@ -3092,6 +3135,7 @@ _shadow = new WeakMap();
3092
3135
  _petOverlay = new WeakMap();
3093
3136
  _resolveLoaded = new WeakMap();
3094
3137
  _loaded = new WeakMap();
3138
+ _channelId = new WeakMap();
3095
3139
  _messenger = new WeakMap();
3096
3140
  _ChatKitElementBase_instances = new WeakSet();
3097
3141
  emitAndThrow_fn = function(error) {
@@ -3302,7 +3346,8 @@ maybeInit_fn = function() {
3302
3346
  frameURL.hash = encodeBase64({
3303
3347
  options: getInnerOptions(__privateMethod(this, _ChatKitElementBase_instances, getFrameOptions_fn).call(this, __privateGet(this, _opts))),
3304
3348
  referrer: window.location.origin,
3305
- profile: this.profile
3349
+ profile: this.profile,
3350
+ ...__privateGet(this, _channelId) ? { channelId: __privateGet(this, _channelId) } : {}
3306
3351
  });
3307
3352
  __privateGet(this, _messenger).connect();
3308
3353
  __privateGet(this, _frame).src = frameURL.toString();