@alan-ai/alan-sdk-web 1.8.126 → 1.8.127

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/alan_lib.js CHANGED
@@ -374,16 +374,36 @@
374
374
  function isProjectIdValid(projectId) {
375
375
  return projectId.match(/^[A-Z0-9]{64}\/(prod|stage|testing)$/gi);
376
376
  }
377
+
378
+ function parseHostWithQueryString(host) {
379
+ var baseURL = config.baseURL;
380
+ var queryString = '';
381
+
382
+ if (host) {
383
+ var qIndex = host.indexOf('?');
384
+ if (qIndex >= 0) {
385
+ queryString = host.substring(qIndex);
386
+ baseURL = "wss://" + host.substring(0, qIndex);
387
+ } else {
388
+ baseURL = "wss://" + host;
389
+ }
390
+ } else if (config.baseURL.indexOf('?') >= 0) {
391
+ var qIndex = config.baseURL.indexOf('?');
392
+ queryString = config.baseURL.substring(qIndex);
393
+ baseURL = config.baseURL.substring(0, qIndex);
394
+ }
395
+
396
+ return { baseURL: baseURL, queryString: queryString };
397
+ }
377
398
 
378
399
  function connectProject(projectId, auth, host, mode, ext) {
379
400
  var connect = new ConnectionWrapper();
380
- if (host) {
381
- config.baseURL = "wss://" + host;
382
- }
401
+ var parsed = parseHostWithQueryString(host);
402
+
383
403
  connect._config.projectId = projectId;
384
404
  connect._config.codec = config.codec;
385
405
  connect._config.version = config.version;
386
- connect._config.url = config.baseURL + "/ws_project/" + projectId;
406
+ connect._config.url = parsed.baseURL + "/ws_project/" + projectId + parsed.queryString;
387
407
 
388
408
  if (!isProjectIdValid(projectId)) {
389
409
  throw new Error("Wrong projectId was provided: ", projectId);
@@ -422,13 +442,12 @@
422
442
 
423
443
  function connectProjectTest(projectId, auth, host, mode, ext) {
424
444
  var connect = new ConnectionWrapper();
425
- if (host) {
426
- config.baseURL = "wss://" + host;
427
- }
445
+ var parsed = parseHostWithQueryString(host);
446
+
428
447
  connect._config.projectId = projectId;
429
448
  connect._config.codec = config.codec;
430
449
  connect._config.version = config.version;
431
- connect._config.url = config.baseURL + "/ws_project/" + projectId;
450
+ connect._config.url = parsed.baseURL + "/ws_project/" + projectId + parsed.queryString;
432
451
 
433
452
  if (!isProjectIdValid(projectId)) {
434
453
  throw new Error("Wrong projectId was provided");
@@ -102338,19 +102357,21 @@ code.hljs {
102338
102357
  }
102339
102358
  return pathSegments.join(" > ");
102340
102359
  }
102341
- function collectScrollableElementStates() {
102342
- if (typeof document === "undefined") {
102360
+ function collectScrollableElementStates(doc2 = document) {
102361
+ if (typeof doc2 === "undefined") {
102343
102362
  return [];
102344
102363
  }
102364
+ const win = doc2.defaultView || window;
102365
+ const HTMLElementClass = win.HTMLElement;
102345
102366
  const elements = /* @__PURE__ */ new Set();
102346
- document.querySelectorAll("*").forEach((el) => elements.add(el));
102347
- if (document.body) elements.add(document.body);
102348
- if (document.documentElement) elements.add(document.documentElement);
102349
- if (document.scrollingElement) elements.add(document.scrollingElement);
102367
+ doc2.querySelectorAll("*").forEach((el) => elements.add(el));
102368
+ if (doc2.body) elements.add(doc2.body);
102369
+ if (doc2.documentElement) elements.add(doc2.documentElement);
102370
+ if (doc2.scrollingElement) elements.add(doc2.scrollingElement);
102350
102371
  const scrollableStates = [];
102351
102372
  const overflowRegex = /(auto|scroll|overlay)/i;
102352
102373
  elements.forEach((element) => {
102353
- if (!(element instanceof HTMLElement)) {
102374
+ if (!(element instanceof HTMLElementClass)) {
102354
102375
  return;
102355
102376
  }
102356
102377
  const scrollHeight = element.scrollHeight;
@@ -102360,10 +102381,10 @@ code.hljs {
102360
102381
  if (clientHeight === 0 && clientWidth === 0) {
102361
102382
  return;
102362
102383
  }
102363
- const computedStyle = window.getComputedStyle(element);
102384
+ const computedStyle = win.getComputedStyle(element);
102364
102385
  const overflowY = computedStyle.overflowY;
102365
102386
  const overflowX = computedStyle.overflowX;
102366
- const isRootElement = element === document.body || element === document.documentElement || element === document.scrollingElement;
102387
+ const isRootElement = element === doc2.body || element === doc2.documentElement || element === doc2.scrollingElement;
102367
102388
  const canScrollVertically = scrollHeight - clientHeight > 1;
102368
102389
  const canScrollHorizontally = scrollWidth - clientWidth > 1;
102369
102390
  const allowsVerticalScroll = isRootElement || overflowRegex.test(overflowY);
@@ -102379,17 +102400,41 @@ code.hljs {
102379
102400
  });
102380
102401
  return scrollableStates;
102381
102402
  }
102403
+ var CROSS_ORIGIN_IFRAME_CLASS = "alan-iframe-cross-origin";
102382
102404
  async function collectIframeContent() {
102383
102405
  const iframes = Array.from(document.querySelectorAll("iframe"));
102384
102406
  if (iframes.length === 0) {
102385
102407
  return [];
102386
102408
  }
102409
+ iframes.forEach((iframe, index3) => {
102410
+ if (!iframe.id) {
102411
+ iframe.id = `alan-iframe-${index3}`;
102412
+ }
102413
+ });
102414
+ const directResults = [];
102415
+ const crossOriginIframes = [];
102416
+ for (const iframe of iframes) {
102417
+ try {
102418
+ const doc2 = iframe.contentDocument;
102419
+ if (doc2 && doc2.documentElement) {
102420
+ const clone3 = doc2.documentElement.cloneNode(true);
102421
+ clone3.querySelectorAll("script").forEach((s) => s.remove());
102422
+ directResults.push({ id: iframe.id, html: clone3.outerHTML });
102423
+ } else {
102424
+ iframe.classList.add(CROSS_ORIGIN_IFRAME_CLASS);
102425
+ crossOriginIframes.push(iframe);
102426
+ }
102427
+ } catch (e) {
102428
+ iframe.classList.add(CROSS_ORIGIN_IFRAME_CLASS);
102429
+ crossOriginIframes.push(iframe);
102430
+ }
102431
+ }
102432
+ if (crossOriginIframes.length === 0) {
102433
+ return directResults;
102434
+ }
102387
102435
  return new Promise((resolve) => {
102388
102436
  const responses = /* @__PURE__ */ new Map();
102389
- const expectedIds = /* @__PURE__ */ new Set();
102390
- iframes.forEach((iframe, index3) => {
102391
- expectedIds.add(iframe.id);
102392
- });
102437
+ const expectedIds = new Set(crossOriginIframes.map((iframe) => iframe.id));
102393
102438
  const messageHandler = (event) => {
102394
102439
  if (event.data && event.data.source === "alan-chat-iframe" && event.data.type === "alan-iframe-content-response") {
102395
102440
  const { iframeId, html: html5 } = event.data;
@@ -102397,14 +102442,16 @@ code.hljs {
102397
102442
  responses.set(iframeId, html5);
102398
102443
  if (responses.size === expectedIds.size) {
102399
102444
  window.removeEventListener("message", messageHandler);
102400
- const result = Array.from(responses.entries()).map(([id, html6]) => ({ id, html: html6 }));
102401
- resolve(result);
102445
+ resolve([
102446
+ ...directResults,
102447
+ ...Array.from(responses.entries()).map(([id, html6]) => ({ id, html: html6, crossOrigin: true }))
102448
+ ]);
102402
102449
  }
102403
102450
  }
102404
102451
  }
102405
102452
  };
102406
102453
  window.addEventListener("message", messageHandler);
102407
- iframes.forEach((iframe) => {
102454
+ crossOriginIframes.forEach((iframe) => {
102408
102455
  try {
102409
102456
  if (iframe.contentWindow) {
102410
102457
  iframe.contentWindow.postMessage({
@@ -102419,23 +102466,32 @@ code.hljs {
102419
102466
  });
102420
102467
  setTimeout(() => {
102421
102468
  window.removeEventListener("message", messageHandler);
102422
- const result = Array.from(expectedIds).map((id) => ({
102423
- id,
102424
- html: responses.get(id) || ""
102425
- }));
102426
- resolve(result);
102469
+ resolve([
102470
+ ...directResults,
102471
+ ...Array.from(expectedIds).map((id) => ({
102472
+ id,
102473
+ html: responses.get(id) || "",
102474
+ crossOrigin: true
102475
+ }))
102476
+ ]);
102427
102477
  }, IFRAME_CONTENT_TIMEOUT_MS);
102428
102478
  });
102429
102479
  }
102430
102480
  function replaceIframesWithSrcDoc(page, iframeContents, visibilityMap) {
102431
- const contentMap = new Map(iframeContents.map((item) => [item.id, item.html]));
102432
- const iframes = page.querySelectorAll("iframe.act-embed");
102481
+ const contentMap = new Map(iframeContents.map((item) => [item.id, item]));
102482
+ const iframes = page.querySelectorAll("iframe");
102433
102483
  iframes.forEach((iframe) => {
102434
102484
  const iframeId = iframe.id;
102435
- const html5 = contentMap.get(iframeId);
102436
- if (html5 && visibilityMap.get(iframeId)) {
102485
+ const entry = contentMap.get(iframeId);
102486
+ if (!entry) {
102487
+ return;
102488
+ }
102489
+ if (entry.crossOrigin) {
102490
+ iframe.classList.add(CROSS_ORIGIN_IFRAME_CLASS);
102491
+ }
102492
+ if (entry.html && visibilityMap.get(iframeId)) {
102437
102493
  iframe.removeAttribute("src");
102438
- iframe.setAttribute("srcdoc", html5);
102494
+ iframe.setAttribute("srcdoc", entry.html);
102439
102495
  }
102440
102496
  });
102441
102497
  }
@@ -102446,6 +102502,14 @@ code.hljs {
102446
102502
  if (rect.bottom <= 0 || rect.top >= viewportHeight || rect.right <= 0 || rect.left >= viewportWidth) {
102447
102503
  return false;
102448
102504
  }
102505
+ let node = element;
102506
+ while (node) {
102507
+ const style = window.getComputedStyle(node);
102508
+ if (style.display === "none" || style.visibility === "hidden" || style.opacity === "0") {
102509
+ return false;
102510
+ }
102511
+ node = node.parentElement;
102512
+ }
102449
102513
  let parent3 = element.parentElement;
102450
102514
  while (parent3) {
102451
102515
  const style = window.getComputedStyle(parent3);
@@ -102542,6 +102606,7 @@ code.hljs {
102542
102606
  if (!document.getElementsByTagName("html")[0]) {
102543
102607
  throw new Error("HTML document not available");
102544
102608
  }
102609
+ const iframeContents = await collectIframeContent();
102545
102610
  const page = document.createElement("html");
102546
102611
  page.innerHTML = document.getElementsByTagName("html")[0].innerHTML;
102547
102612
  const scripts = page.getElementsByTagName("script");
@@ -102552,7 +102617,6 @@ code.hljs {
102552
102617
  }
102553
102618
  }
102554
102619
  const pageContent = page.outerHTML;
102555
- const iframeContents = await collectIframeContent();
102556
102620
  const liveIframes = Array.from(document.querySelectorAll("iframe"));
102557
102621
  const visibilityMap = /* @__PURE__ */ new Map();
102558
102622
  liveIframes.forEach((iframe) => {
@@ -102574,6 +102638,10 @@ code.hljs {
102574
102638
  if (!window.tutorProject) {
102575
102639
  return;
102576
102640
  }
102641
+ const animatedElements = page.querySelectorAll(".alan-btn__chat-response.animated, .alan-btn__chat-request.animated");
102642
+ animatedElements.forEach((el) => {
102643
+ el.classList.remove("animated");
102644
+ });
102577
102645
  const params = {
102578
102646
  html: page.outerHTML,
102579
102647
  url: window.location.href,
@@ -102591,11 +102659,29 @@ code.hljs {
102591
102659
  return;
102592
102660
  }
102593
102661
  if (uiState10.pageState?.screenshot?.enabled) {
102662
+ const iframeScrollStates = [];
102663
+ for (const iframe of liveIframes) {
102664
+ if (!visibilityMap.get(iframe.id)) continue;
102665
+ try {
102666
+ const iframeDoc = iframe.contentDocument;
102667
+ if (iframeDoc) {
102668
+ const states = collectScrollableElementStates(iframeDoc);
102669
+ states.forEach((s) => iframeScrollStates.push({
102670
+ ...s,
102671
+ path: `#${iframe.id} >> ${s.path}`
102672
+ }));
102673
+ }
102674
+ } catch (e) {
102675
+ }
102676
+ }
102594
102677
  params.screenshot_data = {
102595
102678
  baseHref: document.baseURI,
102596
102679
  width: window.innerWidth,
102597
102680
  height: window.innerHeight,
102598
- scrollStates: collectScrollableElementStates()
102681
+ scrollStates: [
102682
+ ...collectScrollableElementStates(),
102683
+ ...iframeScrollStates
102684
+ ]
102599
102685
  };
102600
102686
  }
102601
102687
  window.tutorProject.call("syncPageState", params);
@@ -109360,7 +109446,7 @@ var hljs=function(){"use strict";function e(n){return n instanceof Map?n.clear=n
109360
109446
  return match ? match[0] : null;
109361
109447
  }
109362
109448
  function extractFunction(code2, functionName) {
109363
- const ast = parse11(code2, { ecmaVersion: 2020 });
109449
+ const ast = parse11(code2, { ecmaVersion: 2022 });
109364
109450
  for (const node of ast.body) {
109365
109451
  if (node.type === "FunctionDeclaration" && node.id.name === functionName) {
109366
109452
  return generate2(node);
@@ -109547,6 +109633,54 @@ var hljs=function(){"use strict";function e(n){return n instanceof Map?n.clear=n
109547
109633
  }
109548
109634
  } catch (error) {
109549
109635
  console.error(`Failed to fetch or process iframe from ${srcUrl}:`, error);
109636
+ const errorMessage = error?.message || error?.toString() || "Unknown error";
109637
+ const errorHtml = `
109638
+ <!DOCTYPE html>
109639
+ <html>
109640
+ <head>
109641
+ <meta charset="UTF-8">
109642
+ <style>
109643
+ body {
109644
+ font-family: Arial, sans-serif;
109645
+ padding: 20px;
109646
+ background-color: #fff3cd;
109647
+ color: #856404;
109648
+ margin: 0;
109649
+ }
109650
+ .error-container {
109651
+ border: 1px solid #ffc107;
109652
+ border-radius: 4px;
109653
+ padding: 15px;
109654
+ background-color: #fff;
109655
+ }
109656
+ .error-title {
109657
+ font-weight: bold;
109658
+ margin-bottom: 10px;
109659
+ font-size: 16px;
109660
+ }
109661
+ .error-details {
109662
+ font-size: 14px;
109663
+ word-break: break-word;
109664
+ }
109665
+ .error-url {
109666
+ margin-top: 10px;
109667
+ font-size: 12px;
109668
+ color: #666;
109669
+ }
109670
+ </style>
109671
+ </head>
109672
+ <body>
109673
+ <div class="error-container">
109674
+ <div class="error-title">\u26A0\uFE0F Failed to collect iframe content</div>
109675
+ <div class="error-details">${errorMessage.replace(/</g, "&lt;").replace(/>/g, "&gt;")}</div>
109676
+ <div class="error-url">Source: ${srcUrl}</div>
109677
+ </div>
109678
+ </body>
109679
+ </html>
109680
+ `;
109681
+ iframe.removeAttribute("src");
109682
+ iframe.setAttribute("srcdoc", errorHtml);
109683
+ iframe.setAttribute("sandbox", "allow-scripts allow-same-origin");
109550
109684
  }
109551
109685
  }));
109552
109686
  const iframeResizerCode = 'const v=()=>typeof window<"u",C=()=>{try{return window.self!==window.top}catch{return!0}},w=e=>e instanceof HTMLIFrameElement,z=e=>{"complete"===window.document.readyState?e():window.addEventListener("load",e,{once:!0})},B=(e,t)=>{t(),e.addEventListener("load",t,{once:!0})},k=(e,t)=>{const n="complete"===e.contentWindow?.document.readyState;return"about:blank"!==e.src&&"about:blank"!==e.contentWindow?.location.href&&n?t():e.addEventListener("load",t,{once:!0})},W=()=>({offsetSize:0,checkOrigin:!0,enableLegacyLibSupport:!1});async function A(e){try{return"about:blank"===e.contentDocument?.URL?new Promise((t=>{e.addEventListener("load",(()=>t(null!==e.contentDocument)),{once:!0})})):null!==e.contentDocument}catch{return!1}}const P=e=>{try{const t=new URL(e.src).origin;if("about:blank"!==t)return t}catch{}return null},H=e=>(Object.keys(e).forEach((t=>{void 0===e[t]&&delete e[t]})),e),I=e=>{const{height:t,width:n}=e.getBoundingClientRect();return{height:Math.ceil(t),width:Math.ceil(n)}},l=(e,t)=>e?t?e.querySelector(t):e.documentElement:null,O=(e,t)=>{e&&(t.bodyPadding&&(e.body.style.padding=t.bodyPadding),t.bodyMargin&&(e.body.style.margin=t.bodyMargin))},b=e=>e<=100?100:e<=120?1e3:1e4,x=()=>"[iFrameSizer]ID:0:false:false:32:true:true::auto:::0:false:child:auto:true:::true:::false";function F(e){if("string"!=typeof e.data||!e.data.startsWith("[iFrameSizer]")||!e.data.endsWith("mutationObserver")&&!e.data.endsWith("resizeObserver"))return null;const[t,n]=e.data.split(":"),i=+n;return i>0?i:null}const p=V();let m=[];const Z=async(e,t)=>{if(!v())return[];const n={...W(),...H(e??{})},i=N(t),r=U(n,i);return Promise.all(i.map((async e=>{const t={iframe:e,settings:n,interactionState:{isHovered:!1},initContext:{isInitialized:!1,retryAttempts:0}},{unsubscribe:i,resize:o}=await $(t,r);return m.push(t),{unsubscribe:()=>{i(),m=m.filter((t=>t.iframe!==e))},resize:o}})))};function N(e){return"string"==typeof e?Array.from(document.querySelectorAll(e)).filter(w):e?w(e)?[e]:[]:Array.from(document.getElementsByTagName("iframe"))}function U(e,t){if(Array.isArray(e.checkOrigin))return e.checkOrigin;if(!e.checkOrigin)return[];const n=[];for(const e of t){const t=P(e);t&&n.push(t)}return n}async function $(e,t){const n=await A(e.iframe),{unsubscribe:i,resize:r}=n?_(e):q(e,t),o=G(e);return{unsubscribe:()=>{i(),o()},resize:r}}function q(e,t){const{iframe:n,initContext:i,settings:{checkOrigin:r,enableLegacyLibSupport:o,targetElementSelector:s,bodyPadding:a,bodyMargin:c}}=e,d=i=>{const s="null"===i.origin,a=!r||s||t.includes(i.origin);if(n.contentWindow===i.source&&a){if("iframe-resized"===i.data?.type){const{height:t}=i.data;return void(t&&g({newHeight:t,registeredElement:e}))}if(o){const t=F(i);return void(null!==t&&g({newHeight:t,registeredElement:e}))}}};window.addEventListener("message",d);const u=o?x():{type:"iframe-child-init",targetElementSelector:s,bodyPadding:a,bodyMargin:c},l=()=>{B(n,(()=>n.contentWindow?.postMessage(u,"*"))),i.retryAttempts++,i.retryTimeoutId=window.setTimeout(l,b(i.retryAttempts))};return l(),{unsubscribe:()=>window.removeEventListener("message",d),resize:()=>{n.contentWindow?.postMessage({type:"iframe-get-child-dimensions"},"*")}}}function _(e){const{iframe:t,settings:n}=e,{targetElementSelector:i}=n;let r=0;const o=()=>{const e=l(t.contentDocument,i);if(!t.contentDocument||!e)return r++,setTimeout(o,b(r));O(t.contentDocument,n),p().observe(e)};return k(t,o),{unsubscribe:()=>{const e=l(t.contentDocument,i);e&&p().unobserve(e)},resize:()=>L(e)}}function G({iframe:e,interactionState:t,settings:n}){if(!n.onBeforeIframeResize&&!n.onIframeResize)return()=>{};const i=()=>{t.isHovered=!0},r=()=>{t.isHovered=!1};return e.addEventListener("mouseenter",i),e.addEventListener("mouseleave",r),()=>{e.removeEventListener("mouseenter",i),e.removeEventListener("mouseleave",r)}}function V(){let e=null;return()=>{if(!e){const t=({target:e})=>{const t=m.find((({iframe:t})=>t.contentDocument===e.ownerDocument));t&&L(t)};e=new ResizeObserver((e=>e.forEach(t)))}return e}}function L(e){const{iframe:t,settings:n}=e,i=l(t.contentDocument,n.targetElementSelector);if(!i)return;const{height:r}=I(i);r&&g({newHeight:r,registeredElement:e})}function g({registeredElement:e,newHeight:t}){const{iframe:n,settings:i,interactionState:r,initContext:o}=e;if(o.isInitialized||(o.isInitialized=!0,clearTimeout(o.retryTimeoutId)),!1===i.onBeforeIframeResize?.({iframe:n,interactionState:{...r},settings:{...i},observedHeight:t}))return;const s=n.getBoundingClientRect(),a=t+i.offsetSize;if(n.style.height=`${a}px`,!i.onIframeResize)return;const c={iframe:n,settings:{...i},interactionState:{...r},previousRenderState:{rect:s},nextRenderState:{rect:n.getBoundingClientRect()}};i.onIframeResize(c)}const J=X();let R,h=!1;function K(){!v()||!C()||window.addEventListener("message",(e=>"iframe-child-init"===e.data?.type?z((()=>S(e))):"iframe-get-child-dimensions"===e.data?.type?z((()=>Q(e))):void 0))}function S(e,t=0){const{targetElementSelector:n,bodyPadding:i,bodyMargin:r}=e.data,o=l(document,n);if(h||window.parent!==e.source)return;if(!o)return setTimeout((()=>S(e,t+1)),b(t));O(document,{bodyMargin:r,bodyPadding:i}),R=n;const s=J();s.disconnect(),s.observe(o),h=!0}function Q(e){const t=l(document,R);!h||window.parent!==e.source||!t||E(t)}function X(){let e=null;return()=>(e||(e=new ResizeObserver((e=>{e[0].target&&E(e[0].target)}))),e)}K();const E=e=>{const{width:t,height:n}=I(e),i={type:"iframe-resized",width:t,height:n};window.parent.postMessage(i,"*")},j=({previousRenderState:e,nextRenderState:t,iframe:n})=>{document.activeElement===n&&window.scrollBy(0,t.rect.bottom-e.rect.bottom)}; window.iframeResizer={initialize :Z ,initializeChildListener:K,updateParentScrollOnResize:j};';
@@ -110827,7 +110961,7 @@ var hljs=function(){"use strict";function e(n){return n instanceof Map?n.clear=n
110827
110961
  // alan_btn/alan_btn.ts
110828
110962
  (function(ns) {
110829
110963
  const uiState10 = getUIState();
110830
- const version2 = "alan-version.1.8.126".replace("alan-version.", "");
110964
+ const version2 = "alan-version.1.8.127".replace("alan-version.", "");
110831
110965
  uiState10.lib.version = version2;
110832
110966
  window.alanLib = { version: version2 };
110833
110967
  if (window.alanBtn) {