@yassirbenmoussa/aicommerce-sdk 1.9.0 → 1.9.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.
- package/dist/ai-commerce.min.js +55 -35
- package/dist/index.cjs +37 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/index.mjs +37 -10
- package/dist/index.mjs.map +1 -1
- package/dist/widget.min.js +37 -17
- package/dist/widget.min.js.map +1 -1
- package/package.json +1 -1
package/dist/widget.min.js
CHANGED
|
@@ -552,7 +552,7 @@ var R=class i{constructor(r){this.sessionToken=null;this.products={create:async
|
|
|
552
552
|
Embedded Mode Styles - ChatGPT Style
|
|
553
553
|
============================================ */
|
|
554
554
|
|
|
555
|
-
/* Embedded container -
|
|
555
|
+
/* Embedded container - starts small, grows to max-height, then scrolls */
|
|
556
556
|
#aicommerce-widget.aicommerce-embedded {
|
|
557
557
|
position: relative;
|
|
558
558
|
bottom: auto;
|
|
@@ -560,6 +560,7 @@ var R=class i{constructor(r){this.sessionToken=null;this.products={create:async
|
|
|
560
560
|
right: auto;
|
|
561
561
|
width: 100%;
|
|
562
562
|
height: auto;
|
|
563
|
+
min-height: 80px;
|
|
563
564
|
max-height: var(--aic-max-height, 600px);
|
|
564
565
|
display: flex;
|
|
565
566
|
flex-direction: column;
|
|
@@ -581,11 +582,12 @@ var R=class i{constructor(r){this.sessionToken=null;this.products={create:async
|
|
|
581
582
|
display: none !important;
|
|
582
583
|
}
|
|
583
584
|
|
|
584
|
-
/* Embedded mode: chat
|
|
585
|
+
/* Embedded mode: chat container grows with content */
|
|
585
586
|
.aicommerce-embedded .aicommerce-chat {
|
|
586
587
|
position: relative;
|
|
587
588
|
width: 100%;
|
|
588
589
|
height: auto;
|
|
590
|
+
max-height: 100%;
|
|
589
591
|
max-width: 100%;
|
|
590
592
|
border-radius: 0;
|
|
591
593
|
background: transparent;
|
|
@@ -604,13 +606,15 @@ var R=class i{constructor(r){this.sessionToken=null;this.products={create:async
|
|
|
604
606
|
pointer-events: auto !important;
|
|
605
607
|
}
|
|
606
608
|
|
|
607
|
-
/* Embedded mode: messages area -
|
|
609
|
+
/* Embedded mode: messages area - scrollable when content overflows */
|
|
608
610
|
.aicommerce-embedded .aicommerce-messages {
|
|
611
|
+
flex: 1 1 auto;
|
|
612
|
+
min-height: 0;
|
|
613
|
+
max-height: calc(var(--aic-max-height, 600px) - 100px);
|
|
609
614
|
display: flex;
|
|
610
615
|
flex-direction: column;
|
|
611
|
-
justify-content: flex-
|
|
616
|
+
justify-content: flex-start;
|
|
612
617
|
overflow-y: auto;
|
|
613
|
-
max-height: calc(var(--aic-max-height, 600px) - 100px);
|
|
614
618
|
padding: 16px;
|
|
615
619
|
max-width: 700px;
|
|
616
620
|
margin: 0 auto;
|
|
@@ -735,19 +739,28 @@ var R=class i{constructor(r){this.sessionToken=null;this.products={create:async
|
|
|
735
739
|
color: var(--aic-text);
|
|
736
740
|
}
|
|
737
741
|
|
|
738
|
-
/* Embedded mode: product cards styling */
|
|
742
|
+
/* Embedded mode: product cards styling - horizontal scroll like widget mode */
|
|
739
743
|
.aicommerce-embedded .aicommerce-products {
|
|
740
744
|
display: flex;
|
|
741
|
-
flex-wrap:
|
|
745
|
+
flex-wrap: nowrap;
|
|
742
746
|
gap: 12px;
|
|
743
747
|
margin-top: 12px;
|
|
748
|
+
overflow-x: auto;
|
|
749
|
+
padding-bottom: 8px;
|
|
750
|
+
cursor: grab;
|
|
751
|
+
scrollbar-width: none;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
.aicommerce-embedded .aicommerce-products::-webkit-scrollbar {
|
|
755
|
+
display: none;
|
|
744
756
|
}
|
|
745
757
|
|
|
746
758
|
.aicommerce-embedded .aicommerce-product-card {
|
|
759
|
+
flex-shrink: 0;
|
|
760
|
+
width: 180px;
|
|
747
761
|
background: var(--aic-bg);
|
|
748
762
|
border: 1px solid var(--aic-border);
|
|
749
763
|
border-radius: 12px;
|
|
750
|
-
max-width: 180px;
|
|
751
764
|
}
|
|
752
765
|
|
|
753
766
|
/* Embedded mode: typing indicator */
|
|
@@ -842,7 +855,7 @@ var R=class i{constructor(r){this.sessionToken=null;this.products={create:async
|
|
|
842
855
|
.aicommerce-spinner {
|
|
843
856
|
animation: aicommerce-spin 1s linear infinite;
|
|
844
857
|
}
|
|
845
|
-
`}function W(i){let r=document.createElement("style");r.id="aicommerce-widget-styles",r.textContent=i;let e=document.getElementById("aicommerce-widget-styles");return e&&e.remove(),document.head.appendChild(r),r}function ee(i){if(!i.apiKey)throw new Error("AICommerceWidget: apiKey is required");let r=new R({apiKey:i.apiKey,storeId:i.storeId,baseUrl:i.baseUrl}),e={isOpen:false,isLoading:true,isRecording:false,messages:[],storeConfig:null},d=null,y=[],s=null,w=null,m;async function F(){try{let a=i.baseUrl||B(),o=await fetch(`${a}/api/v1/store`,{headers:{"x-api-key":i.apiKey}});return o.ok?(await o.json()).store:null}catch(a){return console.error("Failed to fetch store config:",a),null}}function B(){if(typeof window<"u"){let a=document.querySelector("script[data-aicommerce-url]");if(a)return a.getAttribute("data-aicommerce-url")||""}return "https://api.aicommerce.dev"}async function K(){e.storeConfig=await F();let a=i.displayMode||"widget",o=a==="embedded";m={apiKey:i.apiKey,storeId:i.storeId,baseUrl:i.baseUrl||B(),displayMode:a,container:i.container,maxHeight:i.maxHeight||"600px",placeholder:i.placeholder||"Ask me anything about our products...",position:i.position||"bottom-right",theme:i.theme||"auto",primaryColor:i.primaryColor||e.storeConfig?.primaryColor||"#6366f1",welcomeMessage:i.welcomeMessage||e.storeConfig?.welcomeMessage||"Hi! How can I help you find the perfect product today?",botName:i.botName||e.storeConfig?.chatBotName||"Shopping Assistant",zIndex:i.zIndex||9999,buttonText:i.buttonText||"\u{1F4AC}",hideLauncher:i.hideLauncher||false,addToCartText:i.addToCartText,onOpen:i.onOpen,onClose:i.onClose,onProductClick:i.onProductClick,onAddToCart:i.onAddToCart,onMessage:i.onMessage};let c=O(m);if(w=W(c),o){let h=null;if(typeof i.container=="string"?h=document.querySelector(i.container):i.container instanceof HTMLElement&&(h=i.container),!h){console.error("[AI Commerce] Embedded mode requires a valid container element or selector");return}s=document.createElement("div"),s.id="aicommerce-widget",s.className=`aicommerce-widget aicommerce-embedded aicommerce-theme-${m.theme}`,s.style.setProperty("--aic-max-height",m.maxHeight),h.appendChild(s),e.isOpen=true;}else s=document.createElement("div"),s.id="aicommerce-widget",s.className=`aicommerce-widget aicommerce-${m.position} aicommerce-theme-${m.theme}`,document.body.appendChild(s);k(),o||e.messages.push({role:"assistant",content:m.welcomeMessage}),e.isLoading=false,k();}async function N(a,o=1){if(!(await fetch("/cart/add.js",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({id:
|
|
858
|
+
`}function W(i){let r=document.createElement("style");r.id="aicommerce-widget-styles",r.textContent=i;let e=document.getElementById("aicommerce-widget-styles");return e&&e.remove(),document.head.appendChild(r),r}function ee(i){if(!i.apiKey)throw new Error("AICommerceWidget: apiKey is required");let r=new R({apiKey:i.apiKey,storeId:i.storeId,baseUrl:i.baseUrl}),e={isOpen:false,isLoading:true,isRecording:false,messages:[],storeConfig:null},d=null,y=[],s=null,w=null,m;async function F(){try{let a=i.baseUrl||B(),o=await fetch(`${a}/api/v1/store`,{headers:{"x-api-key":i.apiKey}});return o.ok?(await o.json()).store:null}catch(a){return console.error("Failed to fetch store config:",a),null}}function B(){if(typeof window<"u"){let a=document.querySelector("script[data-aicommerce-url]");if(a)return a.getAttribute("data-aicommerce-url")||""}return "https://api.aicommerce.dev"}async function K(){e.storeConfig=await F();let a=i.displayMode||"widget",o=a==="embedded";m={apiKey:i.apiKey,storeId:i.storeId,baseUrl:i.baseUrl||B(),displayMode:a,container:i.container,maxHeight:i.maxHeight||"600px",placeholder:i.placeholder||"Ask me anything about our products...",position:i.position||"bottom-right",theme:i.theme||"auto",primaryColor:i.primaryColor||e.storeConfig?.primaryColor||"#6366f1",welcomeMessage:i.welcomeMessage||e.storeConfig?.welcomeMessage||"Hi! How can I help you find the perfect product today?",botName:i.botName||e.storeConfig?.chatBotName||"Shopping Assistant",zIndex:i.zIndex||9999,buttonText:i.buttonText||"\u{1F4AC}",hideLauncher:i.hideLauncher||false,addToCartText:i.addToCartText,onOpen:i.onOpen,onClose:i.onClose,onProductClick:i.onProductClick,onAddToCart:i.onAddToCart,onMessage:i.onMessage};let c=O(m);if(w=W(c),o){let h=null;if(typeof i.container=="string"?h=document.querySelector(i.container):i.container instanceof HTMLElement&&(h=i.container),!h){console.error("[AI Commerce] Embedded mode requires a valid container element or selector");return}s=document.createElement("div"),s.id="aicommerce-widget",s.className=`aicommerce-widget aicommerce-embedded aicommerce-theme-${m.theme}`,s.style.setProperty("--aic-max-height",m.maxHeight),h.appendChild(s),e.isOpen=true;}else s=document.createElement("div"),s.id="aicommerce-widget",s.className=`aicommerce-widget aicommerce-${m.position} aicommerce-theme-${m.theme}`,document.body.appendChild(s);k(),o||e.messages.push({role:"assistant",content:m.welcomeMessage}),e.isLoading=false,k();}async function N(a,o=1){let c=a;if(a.includes("gid://")){let u=a.match(/\/(\d+)$/);u&&(c=u[1]);}if(!(await fetch("/cart/add.js",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({id:c,quantity:o})})).ok)throw new Error("Failed to add to cart");document.dispatchEvent(new CustomEvent("cart:refresh"));}function k(){if(!s)return;let a=m.displayMode==="embedded",o=e.messages.some(l=>l.role==="user");a&&(s.classList.remove("aicommerce-no-messages","aicommerce-has-messages"),s.classList.add(o?"aicommerce-has-messages":"aicommerce-no-messages"));let c=m.placeholder||"Ask me anything about our products...",h=`
|
|
846
859
|
<div class="aicommerce-input-container">
|
|
847
860
|
${a?`
|
|
848
861
|
<textarea
|
|
@@ -904,15 +917,15 @@ var R=class i{constructor(r){this.sessionToken=null;this.products={create:async
|
|
|
904
917
|
|
|
905
918
|
${a&&o?`
|
|
906
919
|
<div class="aicommerce-messages">
|
|
907
|
-
${e.messages.map((l,L)=>{let
|
|
920
|
+
${e.messages.map((l,L)=>{let P=j(l.content),n=l.role==="user";return `
|
|
908
921
|
<div class="aicommerce-message aicommerce-${l.role}">
|
|
909
|
-
<div class="aicommerce-message-content ${
|
|
922
|
+
<div class="aicommerce-message-content ${P?"aicommerce-rtl":"aicommerce-ltr"}">
|
|
910
923
|
${l.audioUrl?H(l,L,n):T(l.content)}
|
|
911
924
|
</div>
|
|
912
925
|
${l.products&&l.products.length>0?`
|
|
913
926
|
<div class="aicommerce-products">
|
|
914
927
|
${l.products.map(t=>`
|
|
915
|
-
<div class="aicommerce-product-card" data-product-id="${t.id}">
|
|
928
|
+
<div class="aicommerce-product-card" data-product-id="${t.id}" data-variant-id="${t.variantId||""}">
|
|
916
929
|
${t.image||t.imageUrl?`
|
|
917
930
|
<img src="${t.image||t.imageUrl}" alt="${T(t.name)}" class="aicommerce-product-image" />
|
|
918
931
|
`:`
|
|
@@ -922,6 +935,13 @@ var R=class i{constructor(r){this.sessionToken=null;this.products={create:async
|
|
|
922
935
|
<span class="aicommerce-product-name" title="${T(t.name)}">${T(t.name)}</span>
|
|
923
936
|
${t.description?`<p class="aicommerce-product-desc">${T(t.description)}</p>`:""}
|
|
924
937
|
<span class="aicommerce-product-price">${D(t.price,t.currency)}</span>
|
|
938
|
+
<button class="aicommerce-add-to-cart" data-product-id="${t.id}">
|
|
939
|
+
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
940
|
+
<circle cx="9" cy="21" r="1"/><circle cx="20" cy="21" r="1"/>
|
|
941
|
+
<path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"/>
|
|
942
|
+
</svg>
|
|
943
|
+
${m.addToCartText||"Add to Cart"}
|
|
944
|
+
</button>
|
|
925
945
|
</div>
|
|
926
946
|
</div>
|
|
927
947
|
`).join("")}
|
|
@@ -941,9 +961,9 @@ var R=class i{constructor(r){this.sessionToken=null;this.products={create:async
|
|
|
941
961
|
|
|
942
962
|
${a?"":`
|
|
943
963
|
<div class="aicommerce-messages">
|
|
944
|
-
${e.messages.map((l,L)=>{let
|
|
964
|
+
${e.messages.map((l,L)=>{let P=j(l.content),n=l.role==="user";return `
|
|
945
965
|
<div class="aicommerce-message aicommerce-${l.role}">
|
|
946
|
-
<div class="aicommerce-message-content ${
|
|
966
|
+
<div class="aicommerce-message-content ${P?"aicommerce-rtl":"aicommerce-ltr"}">
|
|
947
967
|
${l.audioUrl?H(l,L,n):T(l.content)}
|
|
948
968
|
</div>
|
|
949
969
|
${l.products&&l.products.length>0?`
|
|
@@ -989,7 +1009,7 @@ var R=class i{constructor(r){this.sessionToken=null;this.products={create:async
|
|
|
989
1009
|
</div>
|
|
990
1010
|
`:h}
|
|
991
1011
|
</div>
|
|
992
|
-
`;s.innerHTML=u,Y();let
|
|
1012
|
+
`;s.innerHTML=u,Y();let $=s.querySelector(".aicommerce-messages");$&&($.scrollTop=$.scrollHeight);}function H(a,o,c){return `
|
|
993
1013
|
<div class="aicommerce-audio-player" data-message-index="${o}">
|
|
994
1014
|
<button class="aicommerce-audio-btn">
|
|
995
1015
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="2"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>
|
|
@@ -1007,7 +1027,7 @@ var R=class i{constructor(r){this.sessionToken=null;this.products={create:async
|
|
|
1007
1027
|
</div>
|
|
1008
1028
|
<audio src="${a.audioUrl}" preload="metadata"></audio>
|
|
1009
1029
|
</div>
|
|
1010
|
-
`}function Y(){if(!s)return;let a=s.querySelector(".aicommerce-launcher");a&&a.addEventListener("click",()=>U());let o=s.querySelector(".aicommerce-close");o&&o.addEventListener("click",()=>q());let c=s.querySelector(".aicommerce-input"),h=s.querySelector(".aicommerce-send");c&&(c.addEventListener("keydown",n=>{let t=n;if(t.key==="Enter"){let p=c instanceof HTMLTextAreaElement;if(p&&t.shiftKey)return;n.preventDefault();let b=c.value.trim();b&&(I(b),c.value="",p&&(c.style.height="auto"));}}),c instanceof HTMLTextAreaElement&&c.addEventListener("input",()=>{c.style.height="auto",c.style.height=Math.min(c.scrollHeight,150)+"px";})),h&&c&&h.addEventListener("click",()=>{let n=c.value.trim();n&&(I(n),c.value="",c instanceof HTMLTextAreaElement&&(c.style.height="auto"));});let u=s.querySelector(".aicommerce-mic");u&&u.addEventListener("click",()=>J()),s.querySelectorAll(".aicommerce-product-card").forEach(n=>{n.addEventListener("click",t=>{if(t.target.closest(".aicommerce-add-to-cart"))return;let p=n.getAttribute("data-product-id"),b=e.messages.flatMap(f=>f.products||[]).find(f=>f.id===p);b&&(m.onProductClick?m.onProductClick(b):b.url&&window.open(b.url,"_blank","noopener,noreferrer"));});}),s.querySelectorAll(".aicommerce-add-to-cart").forEach(n=>{n.addEventListener("click",async t=>{t.stopPropagation();let p=n,b=p.closest(".aicommerce-product-card"),f=b?.getAttribute("data-product-id"),x=b?.getAttribute("data-variant-id"),C=e.messages.flatMap(
|
|
1030
|
+
`}function Y(){if(!s)return;let a=s.querySelector(".aicommerce-launcher");a&&a.addEventListener("click",()=>U());let o=s.querySelector(".aicommerce-close");o&&o.addEventListener("click",()=>q());let c=s.querySelector(".aicommerce-input"),h=s.querySelector(".aicommerce-send");c&&(c.addEventListener("keydown",n=>{let t=n;if(t.key==="Enter"){let p=c instanceof HTMLTextAreaElement;if(p&&t.shiftKey)return;n.preventDefault();let b=c.value.trim();b&&(I(b),c.value="",p&&(c.style.height="auto"));}}),c instanceof HTMLTextAreaElement&&c.addEventListener("input",()=>{c.style.height="auto",c.style.height=Math.min(c.scrollHeight,150)+"px";})),h&&c&&h.addEventListener("click",()=>{let n=c.value.trim();n&&(I(n),c.value="",c instanceof HTMLTextAreaElement&&(c.style.height="auto"));});let u=s.querySelector(".aicommerce-mic");u&&u.addEventListener("click",()=>J()),s.querySelectorAll(".aicommerce-product-card").forEach(n=>{n.addEventListener("click",t=>{if(t.target.closest(".aicommerce-add-to-cart"))return;let p=n.getAttribute("data-product-id"),b=e.messages.flatMap(f=>f.products||[]).find(f=>f.id===p);b&&(m.onProductClick?m.onProductClick(b):b.url&&window.open(b.url,"_blank","noopener,noreferrer"));});}),s.querySelectorAll(".aicommerce-add-to-cart").forEach(n=>{n.addEventListener("click",async t=>{t.stopPropagation();let p=n,b=p.closest(".aicommerce-product-card"),f=b?.getAttribute("data-product-id"),x=b?.getAttribute("data-variant-id"),C=e.messages.flatMap(E=>E.products||[]).find(E=>E.id===f);if(!C)return;let v=p.innerHTML;p.disabled=true,p.innerHTML=`
|
|
1011
1031
|
<svg class="aicommerce-spinner" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
1012
1032
|
<circle cx="12" cy="12" r="10" stroke-dasharray="32" stroke-dashoffset="32"/>
|
|
1013
1033
|
</svg>
|
|
@@ -1017,5 +1037,5 @@ var R=class i{constructor(r){this.sessionToken=null;this.products={create:async
|
|
|
1017
1037
|
<polyline points="20 6 9 17 4 12"/>
|
|
1018
1038
|
</svg>
|
|
1019
1039
|
Added!
|
|
1020
|
-
`,setTimeout(()=>{p.innerHTML=v,p.disabled=!1;},2e3);}catch(
|
|
1040
|
+
`,setTimeout(()=>{p.innerHTML=v,p.disabled=!1;},2e3);}catch(E){console.error("[AI Commerce] Add to cart failed:",E),p.innerHTML=v,p.disabled=false;}});}),s.querySelectorAll(".aicommerce-products").forEach(n=>{let t=false,p=0,b=0;n.addEventListener("mousedown",f=>{t=true,n.style.cursor="grabbing",p=f.pageX-n.offsetLeft,b=n.scrollLeft;}),n.addEventListener("mouseleave",()=>{t=false,n.style.cursor="grab";}),n.addEventListener("mouseup",()=>{t=false,n.style.cursor="grab";}),n.addEventListener("mousemove",f=>{if(!t)return;f.preventDefault();let C=(f.pageX-n.offsetLeft-p)*2;n.scrollLeft=b-C;});}),s.querySelectorAll(".aicommerce-audio-player").forEach(n=>{let t=n.querySelector("audio"),p=n.querySelector(".aicommerce-audio-btn"),b=n.querySelectorAll(".aicommerce-waveform-bar"),f=n.querySelector(".aicommerce-current-time");if(!t||!p)return;p.addEventListener("click",()=>{!t.paused?(t.pause(),p.innerHTML='<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="2"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>'):(s?.querySelectorAll("audio").forEach(v=>{if(v!==t&&!v.paused){v.pause();let M=v.closest(".aicommerce-audio-player")?.querySelector(".aicommerce-audio-btn");M&&(M.innerHTML='<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="2"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>');}}),t.play(),p.innerHTML='<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="2"><rect x="6" y="4" width="4" height="16"></rect><rect x="14" y="4" width="4" height="16"></rect></svg>');}),t.addEventListener("timeupdate",()=>{if(f&&(f.textContent=z(t.currentTime)),t.duration){let C=t.currentTime/t.duration*100;b.forEach((v,E)=>{E/b.length*100<=C?v.style.backgroundColor=n.closest(".aicommerce-user")?"rgba(255,255,255,1)":"var(--aic-primary)":v.style.backgroundColor=n.closest(".aicommerce-user")?"rgba(255,255,255,0.4)":"rgba(99,102,241,0.3)";});}}),t.addEventListener("ended",()=>{p.innerHTML='<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="2"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>';});let x=n.querySelector(".aicommerce-waveform-bars");x&&x.addEventListener("click",C=>{let v=x.getBoundingClientRect(),M=(C.clientX-v.left)/v.width;t.duration&&(t.currentTime=M*t.duration);});});}async function J(){if(e.isRecording)d&&d.state!=="inactive"&&d.stop();else try{let a=await navigator.mediaDevices.getUserMedia({audio:!0});y=[],d=new MediaRecorder(a,{mimeType:MediaRecorder.isTypeSupported("audio/webm")?"audio/webm":"audio/mp4"}),d.ondataavailable=o=>{o.data.size>0&&y.push(o.data);},d.onstop=async()=>{if(a.getTracks().forEach(o=>o.stop()),y.length>0){let o=new Blob(y,{type:d?.mimeType||"audio/webm"});await V(o);}e.isRecording=!1,k();},d.start(),e.isRecording=!0,k();}catch(a){console.error("Failed to start recording:",a),e.messages.push({role:"assistant",content:"Unable to access microphone. Please check your permissions."}),k();}}async function V(a){let o=URL.createObjectURL(a),c=Array(40).fill(10),h=0;try{c=await X(a);let u=new Audio(o);await new Promise($=>{u.onloadedmetadata=()=>{h=u.duration,$();},u.onerror=()=>$();});}catch(u){console.error("Audio analysis failed",u);}e.messages.push({role:"user",content:"Voice message",audioUrl:o,audioDuration:h,waveformBars:c}),e.isLoading=true,k();try{let u=await r.chatWithAudio(a);return e.messages.push({role:"assistant",content:u.reply,products:u.products}),m.onMessage&&m.onMessage("Voice message",u),u}catch(u){throw e.messages.push({role:"assistant",content:"Sorry, I encountered an error processing your voice message. Please try again."}),u}finally{e.isLoading=false,k();}}function j(a){return /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]/.test(a)}function z(a){let o=Math.floor(a/60),c=Math.floor(a%60);return `${o}:${c.toString().padStart(2,"0")}`}async function X(a){try{let o=new(window.AudioContext||window.webkitAudioContext),c=await a.arrayBuffer(),u=(await o.decodeAudioData(c)).getChannelData(0),$=40,l=Math.floor(u.length/$),L=[];for(let P=0;P<$;P++){let n=P*l,t=n+l,p=0;for(let x=n;x<t;x++)u[x]&&(p+=u[x]*u[x]);let b=Math.sqrt(p/l),f=Math.min(100,Math.max(10,b*400));L.push(f);}return L}catch(o){return console.error("Analysis error",o),Array.from({length:40},()=>20+Math.random()*60)}}async function I(a){e.messages.push({role:"user",content:a}),e.isLoading=true,k();try{let o=await r.chat(a);return e.messages.push({role:"assistant",content:o.reply,products:o.products}),m.onMessage&&m.onMessage(a,o),o}catch(o){throw e.messages.push({role:"assistant",content:"Sorry, I encountered an error. Please try again."}),o}finally{e.isLoading=false,k();}}function U(){e.isOpen=true,k(),m.onOpen?.(),setTimeout(()=>{s?.querySelector(".aicommerce-input")?.focus();},100);}function q(){e.isOpen=false,k(),m.onClose?.();}function G(){e.isOpen?q():U();}function _(){s&&(s.remove(),s=null),w&&(w.remove(),w=null);}function Q(a){if(Object.assign(m,a),a.primaryColor){let o=O(m);w&&(w.textContent=o);}k();}function T(a){let o=document.createElement("div");return o.textContent=a,o.innerHTML}function D(a,o){let h={USD:"$",EUR:"\u20AC",GBP:"\xA3",MAD:"DH",SAR:"SAR",AED:"AED",JPY:"\xA5",CNY:"\xA5"}[o||"USD"]||o||"$";return `${a.toFixed(2)} ${h}`}return K(),{open:U,close:q,toggle:G,destroy:_,sendMessage:I,updateConfig:Q}}var re={init:ee,VERSION:"1.0.0"};typeof window<"u"&&(window.AICommerceWidget=re);exports.AICommerceWidget=re;exports.createWidget=ee;return exports;})({});//# sourceMappingURL=widget.min.js.map
|
|
1021
1041
|
//# sourceMappingURL=widget.min.js.map
|