@alan-ai/alan-sdk-web 1.8.104 → 1.8.106

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
@@ -81026,6 +81026,9 @@
81026
81026
  if (withDetails && message.name !== "laoding" && message.queryProgress?.length === 0) {
81027
81027
  return "";
81028
81028
  }
81029
+ if (message?.ctx?.final === true && !message.queryProgress) {
81030
+ return "";
81031
+ }
81029
81032
  if (message.name !== "text" && message.queryProgress?.length === 0) {
81030
81033
  return "";
81031
81034
  }
@@ -85639,6 +85642,7 @@ code.hljs {
85639
85642
 
85640
85643
  // alan_btn/src/autosync.ts
85641
85644
  var SEND_PAGE_STATE_THROTTLE_MS = 2e3;
85645
+ var IFRAME_CONTENT_TIMEOUT_MS = 3e3;
85642
85646
  var autoSyncEnabled = false;
85643
85647
  var requestCounter = 0;
85644
85648
  var lastSendPageStateTime = 0;
@@ -85722,6 +85726,86 @@ code.hljs {
85722
85726
  });
85723
85727
  return scrollableStates;
85724
85728
  }
85729
+ async function collectIframeContent() {
85730
+ const iframes = Array.from(document.querySelectorAll("iframe"));
85731
+ if (iframes.length === 0) {
85732
+ return [];
85733
+ }
85734
+ return new Promise((resolve) => {
85735
+ const responses = /* @__PURE__ */ new Map();
85736
+ const expectedIds = /* @__PURE__ */ new Set();
85737
+ iframes.forEach((iframe, index2) => {
85738
+ expectedIds.add(iframe.id);
85739
+ });
85740
+ const messageHandler = (event) => {
85741
+ if (event.data && event.data.source === "alan-chat-iframe" && event.data.type === "alan-iframe-content-response") {
85742
+ const { iframeId, html: html3 } = event.data;
85743
+ if (expectedIds.has(iframeId)) {
85744
+ responses.set(iframeId, html3);
85745
+ if (responses.size === expectedIds.size) {
85746
+ window.removeEventListener("message", messageHandler);
85747
+ const result = Array.from(responses.entries()).map(([id, html4]) => ({ id, html: html4 }));
85748
+ resolve(result);
85749
+ }
85750
+ }
85751
+ }
85752
+ };
85753
+ window.addEventListener("message", messageHandler);
85754
+ iframes.forEach((iframe) => {
85755
+ try {
85756
+ if (iframe.contentWindow) {
85757
+ iframe.contentWindow.postMessage({
85758
+ type: "alan-return-iframe-content",
85759
+ id: iframe.id
85760
+ }, "*");
85761
+ }
85762
+ } catch (error) {
85763
+ console.error(`Failed to send message to iframe ${iframe.id}`, error);
85764
+ responses.set(iframe.id, "");
85765
+ }
85766
+ });
85767
+ setTimeout(() => {
85768
+ window.removeEventListener("message", messageHandler);
85769
+ const result = Array.from(expectedIds).map((id) => ({
85770
+ id,
85771
+ html: responses.get(id) || ""
85772
+ }));
85773
+ resolve(result);
85774
+ }, IFRAME_CONTENT_TIMEOUT_MS);
85775
+ });
85776
+ }
85777
+ function replaceIframesWithSrcDoc(page, iframeContents) {
85778
+ const contentMap = new Map(iframeContents.map((item) => [item.id, item.html]));
85779
+ const iframes = page.querySelectorAll("iframe");
85780
+ iframes.forEach((iframe) => {
85781
+ const iframeId = iframe.id;
85782
+ const html3 = contentMap.get(iframeId);
85783
+ if (html3) {
85784
+ iframe.removeAttribute("src");
85785
+ iframe.setAttribute("srcdoc", html3);
85786
+ }
85787
+ });
85788
+ }
85789
+ function isElementVisibleInViewport(element) {
85790
+ const rect = element.getBoundingClientRect();
85791
+ const viewportHeight = window.innerHeight || document.documentElement.clientHeight;
85792
+ const viewportWidth = window.innerWidth || document.documentElement.clientWidth;
85793
+ return rect.top < viewportHeight && rect.bottom > 0 && rect.left < viewportWidth && rect.right > 0;
85794
+ }
85795
+ function markIframeVisibility(page) {
85796
+ const liveIframes = Array.from(document.querySelectorAll("iframe"));
85797
+ const visibilityMap = /* @__PURE__ */ new Map();
85798
+ liveIframes.forEach((iframe) => {
85799
+ if (iframe.id) {
85800
+ visibilityMap.set(iframe.id, isElementVisibleInViewport(iframe));
85801
+ }
85802
+ });
85803
+ const pageIframes = page.querySelectorAll("iframe");
85804
+ pageIframes.forEach((iframe) => {
85805
+ const isVisible = visibilityMap.get(iframe.id) || false;
85806
+ iframe.setAttribute("visible-on-screen", String(isVisible));
85807
+ });
85808
+ }
85725
85809
  function generateContentHash(content) {
85726
85810
  let hash = 0;
85727
85811
  for (let i = 0; i < content.length; i++) {
@@ -85799,17 +85883,19 @@ code.hljs {
85799
85883
  }
85800
85884
  const page = document.createElement("html");
85801
85885
  page.innerHTML = document.getElementsByTagName("html")[0].innerHTML;
85802
- const originalHtml = page.outerHTML;
85803
85886
  const scripts = page.getElementsByTagName("script");
85804
- const styles = page.getElementsByTagName("style");
85805
- const chatElements = page.querySelectorAll('[alan-chat-exclude-from-page-context="true"]');
85806
- const elementsToRemove = [...scripts, ...styles, ...chatElements];
85887
+ const elementsToRemove = [...scripts];
85807
85888
  for (let i = 0; i < elementsToRemove.length; i++) {
85808
85889
  if (elementsToRemove[i] && elementsToRemove[i].remove) {
85809
85890
  elementsToRemove[i].remove();
85810
85891
  }
85811
85892
  }
85812
85893
  const pageContent = page.outerHTML;
85894
+ const iframeContents = await collectIframeContent();
85895
+ if (iframeContents.length > 0) {
85896
+ replaceIframesWithSrcDoc(page, iframeContents);
85897
+ }
85898
+ markIframeVisibility(page);
85813
85899
  if (onSendCb) {
85814
85900
  onSendCb();
85815
85901
  }
@@ -85821,7 +85907,7 @@ code.hljs {
85821
85907
  return;
85822
85908
  }
85823
85909
  const params = {
85824
- html: originalHtml,
85910
+ html: page.outerHTML,
85825
85911
  url: window.location.href,
85826
85912
  scrollPosition: {
85827
85913
  x: prevPageState.scrollX,
@@ -85836,16 +85922,11 @@ code.hljs {
85836
85922
  if (uiState10.pageState?.autoSync === false) {
85837
85923
  return;
85838
85924
  }
85839
- const maxWidth = 1280;
85840
- const maxHeight = 1024;
85841
85925
  if (uiState10.pageState?.screenshot?.enabled) {
85842
85926
  params.screenshot_data = {
85843
85927
  baseHref: window.location.origin,
85844
- width: Math.min(window.innerWidth, maxWidth),
85845
- height: Math.min(window.innerHeight, maxHeight),
85846
- fullPage: true,
85847
- imageType: "jpeg",
85848
- quality: 85,
85928
+ width: window.innerWidth,
85929
+ height: window.innerHeight,
85849
85930
  scrollStates: collectScrollableElementStates()
85850
85931
  };
85851
85932
  }
@@ -93890,8 +93971,8 @@ code.hljs {
93890
93971
  // alan_btn/alan_btn.ts
93891
93972
  (function(ns) {
93892
93973
  const uiState10 = getUIState();
93893
- uiState10.lib.version = "alan-version.1.8.104".replace("alan-version.", "");
93894
- window.alanLib = { version: "alan-version.1.8.104".replace("alan-version.", "") };
93974
+ uiState10.lib.version = "alan-version.1.8.106".replace("alan-version.", "");
93975
+ window.alanLib = { version: "alan-version.1.8.106".replace("alan-version.", "") };
93895
93976
  if (window.alanBtn) {
93896
93977
  console.warn("Alan: the Alan Button source code has already added (v." + uiState10.lib.version + ")");
93897
93978
  }
@@ -95665,7 +95746,6 @@ ${curDialogId}`);
95665
95746
  if (options.onConnectionStatus) {
95666
95747
  options.onConnectionStatus(res);
95667
95748
  }
95668
- isMsgTypingInProcess = false;
95669
95749
  }
95670
95750
  function onMicAllowed() {
95671
95751
  sendClientEvent({ micAllowed: true });
@@ -95811,6 +95891,9 @@ ${curDialogId}`);
95811
95891
  }
95812
95892
  renderMessageInTextChat(event);
95813
95893
  turnOffVoiceFn();
95894
+ if (e?.ctx?.final === true) {
95895
+ syncPageState(null, null, "final_msg_received");
95896
+ }
95814
95897
  }
95815
95898
  function onTextCbInMicBtn(e) {
95816
95899
  if (!isAlanActive && e.ctx?.opts?.activate === true) {
@@ -96710,6 +96793,7 @@ ${LEARN_MORE_LABEL}
96710
96793
  reqId: res.reqId
96711
96794
  });
96712
96795
  saveSentMessages(text3);
96796
+ sendSyncPageState(null, "send");
96713
96797
  }
96714
96798
  var lastSendMsgTs = null;
96715
96799
  function isMsgShouldBeSkipped(msg) {
@@ -96733,7 +96817,6 @@ ${LEARN_MORE_LABEL}
96733
96817
  lastSendMsgTs = null;
96734
96818
  }
96735
96819
  const sendMessageToTextChat = throttle(async function sendMessageToTextChat2() {
96736
- isMsgTypingInProcess = false;
96737
96820
  var textareaEl = getChatTextareaEl();
96738
96821
  var textareaHolderEl = document.getElementById("textarea-holder");
96739
96822
  var text3 = textareaEl.value;
@@ -96747,10 +96830,10 @@ ${LEARN_MORE_LABEL}
96747
96830
  lastSendMsgTs = Date.now();
96748
96831
  if (text3.trim() === "") return;
96749
96832
  textareaEl.value = "";
96833
+ previousTextValue = "";
96750
96834
  _sendText(text3);
96751
96835
  textareaHolderEl.classList.add("alan-btn__inactive");
96752
96836
  textChatScrollPosition = null;
96753
- sendSyncPageState();
96754
96837
  }, 1e3);
96755
96838
  function getMsgElForMathJax(msgInd) {
96756
96839
  const msgEl = document.getElementById("msg-" + msgInd);
@@ -97044,27 +97127,37 @@ ${LEARN_MORE_LABEL}
97044
97127
  el.scrollLeft = el.scrollWidth;
97045
97128
  }
97046
97129
  }
97047
- let isMsgTypingInProcess = false;
97130
+ let hasRefocusedTextarea = false;
97131
+ let previousTextValue = "";
97048
97132
  function sendSyncPageState(forceUpdate = false, reason) {
97049
- if (!isMsgTypingInProcess) {
97050
- syncPageState(function() {
97051
- isMsgTypingInProcess = true;
97052
- }, forceUpdate, reason);
97053
- setTimeout(() => {
97054
- isMsgTypingInProcess = false;
97055
- }, 2e3);
97056
- }
97133
+ syncPageState(null, forceUpdate, reason);
97057
97134
  }
97058
97135
  function onChatTextAreaFocus() {
97059
- sendSyncPageState();
97136
+ const textareaEl = getChatTextareaEl();
97137
+ if (textareaEl && textareaEl.value.trim() !== "") {
97138
+ hasRefocusedTextarea = true;
97139
+ }
97060
97140
  }
97061
97141
  function onChatTextAreaBlur() {
97062
- isMsgTypingInProcess = false;
97142
+ hasRefocusedTextarea = false;
97143
+ const textareaEl = getChatTextareaEl();
97144
+ if (textareaEl) {
97145
+ previousTextValue = textareaEl.value;
97146
+ }
97063
97147
  }
97064
97148
  let delPressed = 0;
97065
97149
  function onChatTextAreaKeyDown(e) {
97066
97150
  const keyCode = e.keyCode || e.which;
97067
- sendSyncPageState();
97151
+ const textareaEl = e.target;
97152
+ const currentText = textareaEl.value;
97153
+ const isTypingNewChar = e.key && e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey;
97154
+ if (previousTextValue.trim() === "" && currentText.trim() === "" && isTypingNewChar || hasRefocusedTextarea && isTypingNewChar && currentText.length >= previousTextValue.length) {
97155
+ sendSyncPageState(null, "user_started_typing");
97156
+ hasRefocusedTextarea = false;
97157
+ previousTextValue = currentText;
97158
+ } else if (isTypingNewChar) {
97159
+ previousTextValue = currentText;
97160
+ }
97068
97161
  if (keyCode === 13 && e.shiftKey) {
97069
97162
  return;
97070
97163
  }