@useago/sdk 1.2.0 → 1.3.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/widget.cjs +4 -4
- package/dist/widget.d.cts +9 -0
- package/dist/widget.d.ts +9 -0
- package/dist/widget.js +527 -514
- package/package.json +1 -1
package/dist/widget.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const se=require("./AgoClient-CzSmJQxl.cjs"),dt=require("./createFormCollector-BB3Uscuo.cjs"),Gt=require("./createConversationSession-DV0Xp_3O.cjs");var jt='"IBM Plex Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',Re=`var(--ago-font, ${jt})`,ct={font:"--ago-font",radius:"--ago-radius",messageRadius:"--ago-message-radius",brand:"--ago-brand-color",brandText:"--ago-brand-text-color",headerBg:"--ago-header-background",headerText:"--ago-header-text-color",panelBg:"--ago-panel-background",messagesBg:"--ago-messages-background",text:"--ago-text-color",mutedText:"--ago-muted-text-color",border:"--ago-border-color",accent:"--ago-accent-color",agentBubbleBg:"--ago-agent-bubble-background"},Ae="var(--ago-brand-color, #03182f)",_e="var(--ago-brand-text-color, #fff)",Vt="var(--ago-header-background, var(--ago-brand-color, #03182f))",pt="var(--ago-header-text-color, #e8f0fe)",ee="var(--ago-panel-background, #fff)",ht="var(--ago-messages-background, #fbfbfb)",J="var(--ago-text-color, #30373e)",xt="var(--ago-muted-text-color, #6b6d6f)",L="var(--ago-border-color, #dee3e8)",Be="var(--ago-accent-color, #1b5fc4)",ft="var(--ago-agent-bubble-background, #f1f3f5)",qt="var(--ago-radius, 16px)",De="var(--ago-message-radius, 16px)",Kt="var(--ago-message-radius, 20px)";function Xt(n,o){if(o)for(const i of Object.keys(ct)){const r=o[i];r!=null&&n.style.setProperty(ct[i],r)}}function v(n,o){Object.assign(n.style,o)}function h(n={}){const o=document.createElement("div");return v(o,n),o}var ut="ago-chat-widget-keyframes";function Qt(){if(typeof document>"u"||document.getElementById(ut))return;const n=document.createElement("style");n.id=ut,n.textContent="@keyframes ago-pulse { 0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); } 40% { opacity: 1; transform: scale(1); } }@keyframes ago-spin { to { transform: rotate(360deg); } }",document.head.appendChild(n)}function Yt(n){let o=[];const i=document.createElement("form");v(i,{display:"flex",flexDirection:"column",gap:"8px",padding:"12px",borderTop:`1px solid ${L}`,backgroundColor:ee}),i.className="ago-chat-input";const r=h({display:"flex",flexWrap:"wrap",gap:"6px"}),b=h({display:"flex",gap:"8px",alignItems:"flex-end"}),a=document.createElement("textarea");a.placeholder=n.placeholder,a.setAttribute("aria-label",n.placeholder),a.rows=1,v(a,{flex:"1",resize:"none",boxSizing:"border-box",padding:"10px 12px",border:`1px solid ${L}`,borderRadius:"12px",fontSize:"16px",fontFamily:Re,lineHeight:"1.4",maxHeight:"110px",overflowY:"hidden"});const C=()=>{a.style.height="auto";const c=Math.min(a.scrollHeight+2,110);a.style.height=`${c}px`,a.style.overflowY=a.scrollHeight+2>110?"auto":"hidden"};a.addEventListener("input",()=>{C(),A()});const m='<svg width="18" height="18" viewBox="0 -960 960 960" fill="currentColor" aria-hidden="true"><path d="M440-160v-487L216-423l-56-57 320-320 320 320-56 57-224-224v487h-80Z"/></svg>',x='<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true" style="animation: ago-spin 0.8s linear infinite"><path d="M21 12a9 9 0 1 1-6.219-8.56"/></svg>',p=document.createElement("button");p.type="submit",p.setAttribute("aria-label","Send"),p.innerHTML=m,v(p,{flexShrink:"0",width:"40px",height:"40px",padding:"0",border:"none",borderRadius:"50%",backgroundColor:Ae,color:_e,display:"flex",alignItems:"center",justifyContent:"center",cursor:"pointer"});let f=!1;const A=()=>{const s=a.value.trim()!==""||o.length>0,c=f||!s;p.disabled=c,p.innerHTML=f?x:m,p.style.opacity=c&&!f?"0.5":"1",p.style.cursor=c?"default":"pointer"};let R=null,_=null;n.allowFiles&&(R=document.createElement("input"),R.type="file",R.multiple=!0,v(R,{display:"none"}),_=document.createElement("button"),_.type="button",_.setAttribute("aria-label","Attach file"),_.textContent="📎",v(_,{padding:"10px 12px",border:`1px solid ${L}`,borderRadius:"12px",backgroundColor:ee,cursor:"pointer",fontSize:"14px"}),_.addEventListener("click",()=>R?.click()),R.addEventListener("change",()=>{o=[...o,...Array.from(R?.files??[])],Y()}));function Y(){r.replaceChildren(),o.forEach((s,c)=>{const y=h({display:"flex",alignItems:"center",gap:"4px",padding:"4px 8px",backgroundColor:"#f0f4ff",border:`1px solid ${L}`,borderRadius:"8px",fontSize:"12px"}),T=document.createElement("span");T.textContent=s.name;const k=document.createElement("button");k.type="button",k.setAttribute("aria-label",`Remove ${s.name}`),k.textContent="×",v(k,{border:"none",background:"transparent",cursor:"pointer",fontSize:"14px",lineHeight:"1"}),k.addEventListener("click",()=>{o=o.filter((j,V)=>V!==c),Y()}),y.append(T,k),r.appendChild(y)}),A()}const D=()=>{const s=a.value,c=o;return a.value="",C(),o=[],Y(),{content:s,files:c}},g=()=>{const{content:s,files:c}=D();(s.trim()||c.length>0)&&n.onSend(s.trim(),c.length>0?c:void 0)};return i.addEventListener("submit",s=>{s.preventDefault(),g()}),a.addEventListener("keydown",s=>{s.key==="Enter"&&!s.shiftKey&&(s.preventDefault(),g())}),_&&b.appendChild(_),b.append(a,p),R&&i.appendChild(R),i.append(r,b),A(),{inputRow:i,getValueAndClear:D,setDisabled:s=>{a.disabled=s,f=s,A()},focus:()=>a.focus()}}var Zt={"&":"&","<":"<",">":">",'"':""","'":"'"};function ae(n){return n.replace(/[&<>"']/g,o=>Zt[o])}function mt(n){const o=n.trim();return/^(https?:|mailto:|tel:|#|\/|\.\/|\.\.\/)/i.test(o)||!/^[a-z][a-z0-9+.-]*:/i.test(o)?o:null}var bt='"IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, monospace',Se="";function te(n){const o=[],i=b=>(o.push(b),`${Se}${o.length-1}${Se}`);let r=n;return r=r.replace(/`([^`]+)`/g,(b,a)=>i(`<code style="background:#f0f2f5;padding:2px 5px;border-radius:4px;font-size:13px;font-family:${bt}">${ae(a)}</code>`)),r=r.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g,(b,a,C)=>{const m=mt(C);return m?i(`<img alt="${ae(a)}" src="${ae(m)}" style="max-width:100%;border-radius:8px;margin:4px 0" />`):b}),r=r.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g,(b,a,C)=>{const m=mt(C);return m?i(`<a href="${ae(m)}" target="_blank" rel="noopener noreferrer" style="color:#1b5fc4;text-decoration:underline">${te(a)}</a>`):b}),r=ae(r),r=r.replace(/\*\*([^*]+?)\*\*/g,"<strong>$1</strong>").replace(/__([^_]+?)__/g,"<strong>$1</strong>").replace(/~~([^~]+?)~~/g,"<del>$1</del>").replace(/(^|[^*])\*(?!\s)([^*]+?)\*/g,"$1<em>$2</em>").replace(/(^|[^\w_])_(?!\s)([^_]+?)_/g,"$1<em>$2</em>"),r=r.replace(new RegExp(`${Se}(\\d+)${Se}`,"g"),(b,a)=>o[Number(a)]),r}function B(n,o,i){const r=document.createElement(n);return Object.assign(r.style,o),i!==void 0&&(r.innerHTML=i),r}var vt=/^ {0,3}([-*_])( *\1){2,} *$/,yt=/^ {0,3}(#{1,6})\s+(.*?)\s*#*\s*$/,wt=/^ {0,3}(```+|~~~+)/,ke=/^ {0,3}>\s?/,le=/^(\s*)([-*+]|\d+[.)])\s+(.*)$/,Jt=["20px","18px","16px","15px","14px","13px"],ze=n=>n.match(/^\s*/)?.[0].length??0;function en(n){return n.trim()===""||vt.test(n)||yt.test(n)||wt.test(n)||ke.test(n)||le.test(n)}function Ct(n){const o=n[0].match(le),i=o[1].length,r=/\d/.test(o[2]),b=B(r?"ol":"ul",{margin:"0 0 8px 0",paddingLeft:"20px",listStyleType:r?"decimal":"disc",listStylePosition:"outside"});let a=0;for(;a<n.length;){const C=n[a].match(le),m=[];for(a++;a<n.length&&ze(n[a])>i;)m.push(n[a]),a++;const x=B("li",{display:"list-item",margin:"2px 0",lineHeight:"1.6"}),p=C[3].match(/^\[([ xX])\]\s+(.*)$/);p?(x.style.listStyleType="none",x.style.marginLeft="-20px",x.innerHTML=`<span style="margin-right:6px">${p[1].toLowerCase()==="x"?"☑":"☐"}</span>${te(p[2])}`):x.innerHTML=te(C[3]),m.filter(f=>le.test(f)).length>0&&x.appendChild(Ct(m.filter(f=>f.trim()!==""))),b.appendChild(x)}return b}function tn(n){const o=m=>m.replace(/^\s*\|?/,"").replace(/\|?\s*$/,"").split("|").map(x=>x.trim()),i=B("div",{overflowX:"auto",margin:"0 0 8px 0"}),r=B("table",{borderCollapse:"collapse",width:"100%",fontSize:"13px"}),b=document.createElement("thead"),a=document.createElement("tr");for(const m of o(n[0]))a.appendChild(B("th",{border:"1px solid #dee3e8",padding:"6px 10px",textAlign:"left",backgroundColor:"#f5f7fa",fontWeight:"600"},te(m)));b.appendChild(a),r.appendChild(b);const C=document.createElement("tbody");for(const m of n.slice(2)){const x=document.createElement("tr");for(const p of o(m))x.appendChild(B("td",{border:"1px solid #dee3e8",padding:"6px 10px"},te(p)));C.appendChild(x)}return r.appendChild(C),i.appendChild(r),i}var nn=n=>!!n&&/^\s*\|?\s*:?-{1,}:?\s*(\|\s*:?-{1,}:?\s*)*\|?\s*$/.test(n);function Ie(n){const o=document.createDocumentFragment(),i=n.replace(/\r\n?/g,`
|
|
2
2
|
`).split(`
|
|
3
|
-
`);let r=0;for(;r<i.length;){const a=i[r];if(a.trim()===""){r++;continue}const C=a.match(
|
|
4
|
-
`)))),o.appendChild(
|
|
5
|
-
`))),o.appendChild(m);continue}if(a.includes("|")&&Qt(i[r+1])){const s=[a,i[r+1]];for(r+=2;r<i.length&&i[r].includes("|")&&i[r].trim();)s.push(i[r]),r++;o.appendChild(Xt(s));continue}if(re.test(a)){const s=[],m=Oe(a);for(;r<i.length&&i[r].trim()!=="";)if(re.test(i[r])||Oe(i[r])>m)s.push(i[r]),r++;else break;o.appendChild(xt(s));continue}const b=[a];for(r++;r<i.length&&!Kt(i[r]);)b.push(i[r]),r++;o.appendChild(D("p",{margin:"0 0 8px 0",lineHeight:"1.6"},b.map(s=>ee(s)).join("<br>")))}const v=o.lastElementChild;return v&&(v.style.marginBottom="0"),o}function Yt(t,o){const{isLast:i,isLastOfBlock:r,bubbleStyle:v,showAgentName:a,agentBubble:C,followUpEnabled:u,followUpHandler:b}=o,s=t.role==="user",m=v==="imessage",R=x({display:"flex",flexDirection:"column",alignItems:s?"flex-end":"flex-start",marginBottom:m&&!r?"2px":"16px"});if(R.className=`ago-message ago-message--${t.role}`,!s&&a&&t.agent){const g=x({fontSize:"13px",fontWeight:"500",color:Z,marginBottom:"6px",padding:"0 4px"});g.className="ago-message__agent",g.textContent=t.agent.displayName||t.agent.name,R.appendChild(g)}if(!s&&t.sources&&t.sources.length>0){const g=x({display:"grid",gridTemplateColumns:"1fr 1fr",gap:"6px",marginBottom:"10px",width:"100%",maxWidth:"85%"});g.className="ago-message__sources",t.sources.forEach((h,l)=>{const f=h.url?document.createElement("a"):document.createElement("div");h.url&&f instanceof HTMLAnchorElement&&(f.href=h.url,f.target="_blank",f.rel="noopener noreferrer"),y(f,{display:"flex",alignItems:"center",gap:"8px",padding:"8px 10px",border:`1px solid ${$}`,borderRadius:"8px",backgroundColor:J,textDecoration:"none",color:Z,fontSize:"12px",overflow:"hidden"});const I=x({flexShrink:"0",width:"18px",height:"18px",borderRadius:"3px",border:`1px solid ${Ne}`,color:Ne,fontSize:"11px",fontWeight:"500",display:"flex",alignItems:"center",justifyContent:"center",backgroundColor:"#f0f4ff"});I.textContent=String(l+1);const B=x({overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"});B.textContent=h.title,B.title=h.title,f.append(I,B),g.appendChild(f)}),R.appendChild(g)}if(t.attachments&&t.attachments.length>0){const g=x({display:"flex",flexWrap:"wrap",gap:"6px",marginBottom:"6px",justifyContent:s?"flex-end":"flex-start",maxWidth:"75%"});g.className="ago-message__attachments";for(const h of t.attachments){const l=oe.safeAttachmentUrl(h.url);if(oe.canInlineImage(h)&&l){const _=document.createElement("a");_.href=l,_.target="_blank",_.rel="noopener noreferrer",y(_,{display:"inline-block",textDecoration:"none"});const j=document.createElement("img");j.src=l,j.alt=h.name,j.loading="lazy",y(j,{maxWidth:"180px",maxHeight:"160px",objectFit:"cover",borderRadius:"10px",border:`1px solid ${$}`,display:"block"}),_.appendChild(j),g.appendChild(_);continue}const f=l?document.createElement("a"):document.createElement("div");l&&f instanceof HTMLAnchorElement&&(f.href=l,f.target="_blank",f.rel="noopener noreferrer"),y(f,{display:"inline-flex",alignItems:"center",gap:"8px",padding:"8px 12px",borderRadius:"10px",border:`1px solid ${$}`,backgroundColor:s?"rgba(255,255,255,0.12)":J,color:s?Se:Z,fontSize:"13px",textDecoration:"none",maxWidth:"220px"});const I=document.createElement("span");I.textContent="📄",I.setAttribute("aria-hidden","true"),y(I,{fontSize:"16px",lineHeight:"1",flexShrink:"0"});const B=x({display:"flex",flexDirection:"column",minWidth:"0"}),M=x({overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",maxWidth:"160px"});M.textContent=h.name,M.title=h.name,B.appendChild(M);const Q=oe.formatFileSize(h.fileSize);if(Q){const _=x({fontSize:"11px",color:ft});_.textContent=Q,B.appendChild(_)}f.append(I,B),g.appendChild(f)}R.appendChild(g)}const k=s||C||m,T=!!t.content||t.status==="IN_PROGRESS",O=x({maxWidth:m||s?"75%":k?"85%":"100%",padding:k?"10px 14px":"2px 8px",borderRadius:m?Wt:k?Te:"0",backgroundColor:s?Ee:k?st:"transparent",color:s?Se:Z,overflowWrap:"break-word",textWrap:"pretty",fontSize:"16px",lineHeight:"1.6"});if(O.className="ago-message__content",m&&r){O.style.position="relative";const g=x({position:"absolute",zIndex:"0",bottom:"-2px",width:"20px",height:"20px",background:s?Ee:st});g.className="ago-message__tail";const h=x({position:"absolute",zIndex:"1",bottom:"-2px",width:"10px",height:"20px",background:pt});h.className="ago-message__tail-mask",s?(g.style.right="-8px",g.style.borderBottomLeftRadius="16px 14px",h.style.right="-10px",h.style.borderBottomLeftRadius="10px"):(g.style.left="-7px",g.style.borderBottomRightRadius="16px 14px",h.style.left="-10px",h.style.borderBottomRightRadius="10px"),O.append(g,h)}if(t.content?O.appendChild(Re(t.content)):t.status==="IN_PROGRESS"&&O.appendChild(Jt()),T&&R.appendChild(O),i&&t.followUpReplies&&t.followUpReplies.length>0){const g=x({display:"flex",flexWrap:"wrap",gap:"8px",marginTop:"10px"});g.className="ago-message__followups";for(const h of t.followUpReplies){const l=document.createElement("button");l.type="button",l.className="ago-message__followup-btn",l.textContent=h,l.disabled=!u,y(l,{minHeight:"36px",padding:"6px 14px",fontSize:"14px",borderRadius:Te,border:`1px solid ${$}`,backgroundColor:J,color:Z,cursor:u?"pointer":"default",transition:"border-color 0.15s"}),b&&l.addEventListener("click",()=>b(h)),l.addEventListener("mouseenter",()=>{l.style.borderColor=Ne}),l.addEventListener("mouseleave",()=>{l.style.borderColor=$}),g.appendChild(l)}R.appendChild(g)}return R}function Zt(t){const o=x({display:"flex",alignItems:"center",gap:"8px",padding:"10px 14px",backgroundColor:"#f0fdf4",color:"#15803d",borderRadius:Te,marginTop:"8px",fontSize:"13px",border:"1px solid #bbf7d0"});o.className="ago-form-notice",o.setAttribute("role","status");const i=document.createElement("span");i.textContent="✓",i.setAttribute("aria-hidden","true"),y(i,{fontWeight:"700"});const r=document.createElement("span");return r.textContent=t,o.append(i,r),o}function Jt(){const t=x({display:"flex",gap:"4px",padding:"4px 0"});for(let o=0;o<3;o++){const i=x({width:"6px",height:"6px",borderRadius:"50%",backgroundColor:"#b5bfc8",animation:"ago-pulse 1.2s ease-in-out infinite",animationDelay:`${o*.2}s`});t.appendChild(i)}return t}function en(){const t="http://www.w3.org/2000/svg",o=document.createElementNS(t,"svg");o.setAttribute("width","26"),o.setAttribute("height","26"),o.setAttribute("viewBox","0 0 24 24"),o.setAttribute("fill","none");const i=document.createElementNS(t,"path");return i.setAttribute("d","M21 11.5a8.38 8.38 0 0 1-8.5 8.5 8.5 8.5 0 0 1-3.8-.9L3 21l1.9-5.7a8.5 8.5 0 0 1-.9-3.8 8.38 8.38 0 0 1 8.5-8.5 8.38 8.38 0 0 1 8.5 8.5z"),i.setAttribute("stroke","currentColor"),i.setAttribute("stroke-width","2"),i.setAttribute("stroke-linecap","round"),i.setAttribute("stroke-linejoin","round"),o.appendChild(i),o}var ct="Form submitted.";function tn(t){if(typeof t=="string")return t||null;if(!t||typeof t!="object")return null;const o=t;if(typeof o.message=="string"&&o.message)return o.message;const i=o.result;if(typeof i=="string"&&i)return i;if(i&&typeof i=="object"){const r=i.message;if(typeof r=="string"&&r)return r}return null}var nn=0;function on(t){const o=typeof t=="string"?document.querySelector(t):t;if(!o)throw new Error(`mountChatWidget: target "${String(t)}" not found.`);return o}function rn(t,o){const{title:i="Chat",welcomeMessage:r="Hello! How can I help you today?",placeholder:v="Type a message...",allowFiles:a=!1,height:C=500,placement:u="inline",width:b=400,defaultOpen:s=!1,logoUrl:m,showAgentName:R=!1,agentBubble:k=!1,bubbleStyle:T="default",showHeader:O=!0,theme:g,loadThreads:h=!1,forms:l,formSubmittedMessage:f=ct,onFollowUpClick:I,onMessageSent:B,onMessageReceived:M,onFormSubmitted:Q,onFormError:_,onOpen:j,onClose:Be}=o,ce=typeof r=="string"?{message:r,mode:"static"}:r,Me=ce.message,De=ce.mode??"static",bt="speed"in ce&&ce.speed||45;if(!o.client&&!o.config?.baseUrl)throw new oe.AgoError("mountChatWidget requires either `client` or `config` (with a baseUrl).","config_missing_base_url");const E=o.client??new oe.AgoClient(o.config),ze=on(t),z=u==="left"||u==="right",vt=z&&(o.launcher??!0);let te=z?s:!0;const yt=o.mobile?.breakpoint??768,wt=o.mobile?.trigger??"focus",He=typeof window<"u"&&!!window.matchMedia,F=He?window.matchMedia(`(max-width: ${yt}px)`):void 0,Ct=He?window.matchMedia("(prefers-reduced-motion: reduce)"):void 0,ie=!z&&!!F,ke=o.persistConversation,pe=ke?zt.createConversationSession(ke===!0?{}:ke):void 0;let P=o.conversationId??pe?.getLastActiveThread()??void 0,p=[],Y=!1,ae=null,q;const Fe=[];function Et(e){if(typeof f=="function"){const c=f(e);if(typeof c=="string"&&c)return c}const n=tn(e);return n||(typeof f=="string"?f:ct)}const fe=[],d=x({display:"flex",flexDirection:"column",height:z?"100%":typeof C=="number"?`${C}px`:C,border:`1px solid ${$}`,borderRadius:Pt,overflow:"hidden",backgroundColor:J,fontFamily:we,textAlign:"left",boxShadow:"rgba(15, 15, 15, 0.08) 0px 2px 16px 0px"});d.className="ago-chat-widget",Ut(d,g);let H;if(O){if(H=x({padding:"14px 16px",borderBottom:`1px solid ${$}`,backgroundColor:Ft,color:at,display:"flex",alignItems:"center",gap:"10px"}),H.className="ago-chat-widget__header",m){const n=document.createElement("img");n.src=m,n.alt="Logo",y(n,{height:"24px",width:"auto"}),H.appendChild(n)}const e=document.createElement("h3");if(e.textContent=i,y(e,{margin:"0",fontSize:"15px",fontWeight:"600"}),H.appendChild(e),z){const n=document.createElement("button");n.type="button",n.className="ago-chat-widget__close",n.setAttribute("aria-label","Close"),n.textContent="×",y(n,{marginLeft:"auto",background:"transparent",border:"none",color:at,fontSize:"22px",lineHeight:"1",cursor:"pointer",padding:"0 2px"}),n.addEventListener("click",()=>Le()),H.appendChild(n)}}const A=x({flex:"1",overflow:"auto",padding:"16px",backgroundColor:pt});A.className="ago-chat-widget__messages",A.setAttribute("role","log"),A.setAttribute("aria-live","polite"),A.setAttribute("aria-relevant","additions text"),A.setAttribute("aria-atomic","false");const{inputRow:Pe,setDisabled:St,focus:ue}=jt({placeholder:v,allowFiles:a,onSend:(e,n)=>{Ae(e,n)}});Gt(),d.append(...H?[H]:[],A,Pe);let me=d,W,L;const ge=u==="left"?"left":"right";if(z&&(W=x({position:"fixed",top:"0",bottom:"0",[ge]:"0",width:typeof b=="number"?`${b}px`:b,maxWidth:"100vw",display:"flex",zIndex:"2147483000",transition:"transform 0.3s ease",boxShadow:"rgba(15, 15, 15, 0.18) 0px 0px 24px 0px"}),W.className="ago-chat-widget-panel",y(d,{height:"100%",width:"100%",borderRadius:"0",border:"none",boxShadow:"none",[ge==="left"?"borderRight":"borderLeft"]:`1px solid ${$}`}),W.appendChild(d),me=W),vt){if(L=document.createElement("button"),L.type="button",L.className="ago-chat-widget-launcher",L.setAttribute("aria-label",`Open ${i}`),y(L,{position:"fixed",bottom:"20px",[ge]:"20px",width:"56px",height:"56px",borderRadius:"50%",border:"none",backgroundColor:Ee,color:Se,cursor:"pointer",display:"flex",alignItems:"center",justifyContent:"center",zIndex:"2147483000",boxShadow:"rgba(15, 15, 15, 0.2) 0px 4px 14px 0px",fontFamily:we}),m){const e=document.createElement("img");e.src=m,e.alt="",y(e,{width:"26px",height:"26px",objectFit:"contain"}),L.appendChild(e)}else L.appendChild(en());L.addEventListener("click",()=>$e())}const se=ie?`ago-vt-${++nn}`:"",We=52;let V=!1,U,le,K;const he={height:d.style.height,border:d.style.border,borderRadius:d.style.borderRadius,boxShadow:d.style.boxShadow};ze.appendChild(me),L&&ze.appendChild(L),xe(),ie&&(U=Bt(),d.insertBefore(U,d.firstChild),le=document.createElement("style"),le.id=se,le.textContent=`::view-transition-group(${se}),::view-transition-group(${se}-bar){animation-duration:0.3s;animation-timing-function:cubic-bezier(0.4,0,0.2,1)}`,document.head.appendChild(le),wt==="focus"&&(d.addEventListener("pointerdown",e=>{V||!F?.matches||Pe.contains(e.target)&&(e.preventDefault(),ve())},!0),d.addEventListener("focusin",()=>{F?.matches&&ve()})),document.addEventListener("keydown",Ze)),F&&F.addEventListener("change",Je);const Rt=I!==!1,kt=I===!1?void 0:I??(e=>{Ae(e)});function N(){if(A.replaceChildren(),p.length===0){if(De==="static"){const n=x({textAlign:"center",color:ft,padding:"24px 16px",fontSize:"16px",lineHeight:"1.5"});n.appendChild(Re(Me)),A.appendChild(n)}}else p.forEach((n,c)=>{const S=c===p.length-1||p[c+1].role!==n.role;A.appendChild(Yt(n,{isLast:c===p.length-1,isLastOfBlock:S,bubbleStyle:T,showAgentName:R,agentBubble:k,followUpEnabled:Rt,followUpHandler:kt}))});for(const n of Fe)A.appendChild(Zt(n));if(ae){const n=x({padding:"10px 14px",backgroundColor:"#fef2f2",color:"#dc2626",borderRadius:"12px",marginTop:"8px",fontSize:"13px",border:"1px solid #fecaca"});n.setAttribute("role","alert"),n.textContent=ae,A.appendChild(n)}A.scrollTop=A.scrollHeight;const e=p[p.length-1];St(Y&&e?.role==="assistant"&&e.status==="IN_PROGRESS")}function _t(){for(let e=p.length-1;e>=0;e--){const n=p[e];if(n.role==="assistant"&&n.status==="IN_PROGRESS")return n}}const Ue=e=>{P||(P=e.conversationId)},Ge=e=>{const n=_t();n&&(n.content+=e.content,N())},je=e=>{const n=p.findIndex(c=>c.id===e.id||c.role==="assistant"&&c.status==="IN_PROGRESS");n>=0?p[n]=e:p.push(e),N(),ue()},qe=e=>{const n=p.findIndex(c=>c.id===e.id||c.role==="assistant"&&c.status==="IN_PROGRESS");n>=0?p[n]=e:p.push(e),Y=!1,N(),e.status==="DONE"&&M?.({id:e.id,content:e.content})},Ve=e=>{ae=e.error,Y=!1,p=p.filter(n=>!n.id.startsWith("temp-")),N()};function At(e){const n={id:`ago-intro-${Date.now()}`,conversationId:"",content:"",role:"assistant",status:"IN_PROGRESS",createdAt:new Date};p.push(n),N();const c=e.match(/\S+\s*/g)??[e];let S=0;q=setInterval(()=>{if(S>=c.length){clearInterval(q),q=void 0,n.status="DONE",N();return}n.content+=c[S],S++,N()},bt)}E.on("message:start",Ue),E.on("message:chunk",Ge),E.on("message:answer-complete",je),E.on("message:complete",qe),E.on("message:error",Ve);const Ke=e=>Q?.(e),Xe=e=>_?.(e);Q&&E.on("form:submitted",Ke),_&&E.on("form:error",Xe);const _e=[];let Qe=!1;const Ye=e=>{if(Qe)return;_e.push(e.install(E));let n=e.store.get().submitted;const c=e.store.subscribe(S=>{S.submitted&&!n&&(Fe.push(Et(S.submitResult)),N()),n=S.submitted});_e.push(c)};for(const e of l??[])e.schema!=null?Ye(rt.createFormCollector(e)):rt.loadFormCollector(E,e).then(n=>Ye(n)).catch(()=>{});async function Ae(e,n){const c=e.trim();if(!c&&!n?.length||Y)return;if(B?.(c),Y=!0,ae=null,q){clearInterval(q),q=void 0;const w=p.find(G=>G.id.startsWith("ago-intro-"));w&&!w.content?p=p.filter(G=>G!==w):w&&(w.status="DONE")}const S=Date.now();p.push({id:`temp-user-${S}`,conversationId:P||"",content:c,role:"user",status:"DONE",attachments:n&&n.length>0?oe.attachmentsFromFiles(n):void 0,createdAt:new Date}),p.push({id:`temp-assistant-${S}`,conversationId:P||"",content:"",role:"assistant",status:"IN_PROGRESS",createdAt:new Date}),N();try{const w=await E.sendMessage(c,{conversationId:P,files:n});w.conversationId&&(P||(P=w.conversationId),pe?.setActiveThread(w.conversationId,w.createdAt));const G=p.find(X=>X.id===`temp-user-${S}`);if(G&&(G.id=`user-${S}`,G.conversationId=w.conversationId),!p.some(X=>X.id===w.id)){const X=p.findIndex(Dt=>Dt.id===`temp-assistant-${S}`);X>=0?p[X]=w:p.push(w)}p=p.filter(X=>!X.id.startsWith("temp-")),Y=!1,N(),h&&Ie()}catch(w){ae=w instanceof Error?w.message:"Failed to send message",Y=!1,p=p.filter(G=>!G.id.startsWith("temp-")),N()}}async function Ie(){try{const e=await E.getConversations();fe.splice(0,fe.length,...e)}catch{}return fe}async function It(e){try{const n=(await E.getConversation(e))?.messages??[];n.length>0&&p.length===0&&(p=n,N())}catch{p.length===0&&(pe?.clear(),P=void 0)}}return N(),ie&&F?.matches||ue(),P?It(P):De==="streaming"&&At(Me),h&&Ie(),{client:E,element:me,sendMessage:Ae,...z||ie?{open:Lt,close:Nt,toggle:Tt}:{},session:pe,threads:fe,refreshThreads:Ie,destroy(){q&&clearInterval(q),E.off("message:start",Ue),E.off("message:chunk",Ge),E.off("message:answer-complete",je),E.off("message:complete",qe),E.off("message:error",Ve),Q&&E.off("form:submitted",Ke),_&&E.off("form:error",Xe),Qe=!0,_e.forEach(e=>e()),me.remove(),L?.remove(),U?.remove(),le?.remove(),K?.remove(),F?.removeEventListener("change",Je),ie&&(document.removeEventListener("keydown",Ze),window.visualViewport?.removeEventListener("resize",be),document.documentElement.style.removeProperty("overflow")),o.client||E.destroy()}};function xe(){if(!W)return;{const n=ge==="left"?"border-right":"border-left";F?.matches?(W.style.width="100%",d.style.borderRadius="0",d.style.removeProperty(n)):(W.style.width=typeof b=="number"?`${b}px`:b,d.style.setProperty(n,`1px solid ${$}`))}const e=u==="left"?"translateX(-100%)":"translateX(100%)";W.style.transform=te?"translateX(0)":e,W.setAttribute("aria-hidden",te?"false":"true"),L&&(L.style.display=te?"none":"flex")}function $e(){te=!0,xe(),ue(),j?.()}function Le(){te=!1,xe(),Be?.()}function $t(){te?Le():$e()}function Lt(){z?$e():ve()}function Nt(){z?Le():de()}function Tt(){z?$t():V?de():ve()}function Ot(){return Array.from(d.querySelectorAll('a[href],button:not([disabled]),textarea:not([disabled]),input:not([disabled]),select:not([disabled]),[tabindex]:not([tabindex="-1"])')).filter(e=>e.getClientRects().length>0)}function Ze(e){if(!V)return;if(e.key==="Escape"){de();return}if(e.key!=="Tab")return;const n=Ot();if(n.length===0){e.preventDefault();return}const c=n[0],S=n[n.length-1],w=document.activeElement;e.shiftKey&&(w===c||!d.contains(w))?(e.preventDefault(),S.focus()):!e.shiftKey&&(w===S||!d.contains(w))&&(e.preventDefault(),c.focus())}function Je(e){z?xe():!e.matches&&V&&de()}function et(){return window.visualViewport?.height??window.innerHeight}function be(){d.style.setProperty("--ago-vh",`${et()}px`)}function tt(){A.scrollTop=A.scrollHeight}function Bt(){const e=x({position:"fixed",top:"0",left:"0",right:"0",height:`calc(${We}px + env(safe-area-inset-top))`,display:"none",alignItems:"center",justifyContent:"space-between",padding:"env(safe-area-inset-top) 14px 0",backgroundColor:J,borderBottom:`1px solid ${$}`,zIndex:"2147483001",fontFamily:we});if(e.className="ago-chat-widget-mobile-bar",m){const c=document.createElement("img");c.src=m,c.alt="",y(c,{height:"28px",width:"auto"}),e.appendChild(c)}else if(i){const c=document.createElement("span");c.textContent=i,y(c,{fontSize:"15px",fontWeight:"600",color:Z,overflow:"hidden",whiteSpace:"nowrap",textOverflow:"ellipsis"}),e.appendChild(c)}else e.appendChild(x({}));const n=document.createElement("button");return n.type="button",n.setAttribute("aria-label","Close"),n.textContent="×",y(n,{border:"none",background:"transparent",fontSize:"26px",lineHeight:"1",color:Z,cursor:"pointer",padding:"4px 8px"}),n.addEventListener("click",()=>{de()}),e.appendChild(n),e}function Mt(){return typeof document.startViewTransition=="function"&&!Ct?.matches&&!!F?.matches}function nt(e){const n=document;if(Mt()&&n.startViewTransition){d.style.setProperty("view-transition-name",se),U?.style.setProperty("view-transition-name",`${se}-bar`);const c=()=>{d.style.removeProperty("view-transition-name"),U?.style.removeProperty("view-transition-name")};return n.startViewTransition(e).finished.then(c,c)}return e(),Promise.resolve()}function ot(e){if(e)K&&d.parentElement?.insertBefore(K,d),y(d,{position:"fixed",top:"0",left:"0",right:"0",bottom:"0",height:"var(--ago-vh, 100dvh)",zIndex:"2147483000",border:"none",borderRadius:"0",boxShadow:"none",paddingTop:`calc(${We}px + env(safe-area-inset-top))`,paddingBottom:"env(safe-area-inset-bottom)"}),d.setAttribute("role","dialog"),d.setAttribute("aria-modal","true"),d.setAttribute("aria-label",i),document.documentElement.style.overflow="hidden",H&&(H.style.display="none"),U&&(U.style.display="flex");else{K?.remove(),K=void 0;for(const n of["position","top","left","right","bottom","z-index","padding-top","padding-bottom"])d.style.removeProperty(n);d.style.height=he.height,d.style.border=he.border,d.style.borderRadius=he.borderRadius,d.style.boxShadow=he.boxShadow,d.removeAttribute("role"),d.removeAttribute("aria-modal"),d.removeAttribute("aria-label"),document.documentElement.style.removeProperty("overflow"),H&&(H.style.display="flex"),U&&(U.style.display="none")}}function ve(){if(V||!F?.matches)return Promise.resolve();const e=d.getBoundingClientRect();if(e.height>=et()*.8)return Promise.resolve();V=!0,K=x({width:`${e.width}px`,height:`${e.height}px`}),K.className="ago-chat-widget-spacer",K.setAttribute("aria-hidden","true"),be(),window.visualViewport?.addEventListener("resize",be);const n=nt(()=>ot(!0));return n.then(()=>{tt(),ue()}),tt(),j?.(),n}function de(){if(!V)return Promise.resolve();V=!1,window.visualViewport?.removeEventListener("resize",be),d.querySelector("textarea")?.blur();const e=nt(()=>ot(!1));return e.then(()=>d.style.removeProperty("--ago-vh")),Be?.(),e}}exports.mountChatWidget=rn;exports.renderMarkdown=Re;
|
|
3
|
+
`);let r=0;for(;r<i.length;){const a=i[r];if(a.trim()===""){r++;continue}const C=a.match(wt);if(C){const p=C[1][0],f=[];for(r++;r<i.length&&!new RegExp(`^ {0,3}${p==="`"?"`{3,}":"~{3,}"}\\s*$`).test(i[r]);)f.push(i[r]),r++;r++;const A=B("pre",{margin:"0 0 8px 0"});A.appendChild(B("code",{display:"block",background:"#f5f5f5",padding:"8px 12px",borderRadius:"6px",fontSize:"13px",overflowX:"auto",fontFamily:bt,whiteSpace:"pre"},ae(f.join(`
|
|
4
|
+
`)))),o.appendChild(A);continue}if(vt.test(a)){o.appendChild(B("hr",{border:"none",borderTop:"1px solid #dee3e8",margin:"12px 0"})),r++;continue}const m=a.match(yt);if(m){const p=m[1].length;o.appendChild(B(`h${p}`,{margin:"16px 0 8px 0",fontSize:Jt[p-1],fontWeight:"600",lineHeight:"1.3"},te(m[2]))),r++;continue}if(ke.test(a)){const p=[];for(;r<i.length&&ke.test(i[r]);)p.push(i[r].replace(ke,"")),r++;const f=B("blockquote",{borderLeft:"4px solid #dee3e8",paddingLeft:"12px",margin:"0 0 8px 0",fontStyle:"italic",color:"#6b6d6f"});f.appendChild(Ie(p.join(`
|
|
5
|
+
`))),o.appendChild(f);continue}if(a.includes("|")&&nn(i[r+1])){const p=[a,i[r+1]];for(r+=2;r<i.length&&i[r].includes("|")&&i[r].trim();)p.push(i[r]),r++;o.appendChild(tn(p));continue}if(le.test(a)){const p=[],f=ze(a);for(;r<i.length&&i[r].trim()!=="";)if(le.test(i[r])||ze(i[r])>f)p.push(i[r]),r++;else break;o.appendChild(Ct(p));continue}const x=[a];for(r++;r<i.length&&!en(i[r]);)x.push(i[r]),r++;o.appendChild(B("p",{margin:"0 0 8px 0",lineHeight:"1.6"},x.map(p=>te(p)).join("<br>")))}const b=o.lastElementChild;return b&&(b.style.marginBottom="0"),o}function on(n,o){const{isLast:i,isLastOfBlock:r,bubbleStyle:b,showAgentName:a,agentBubble:C,followUpEnabled:m,followUpHandler:x,isMobile:p}=o,f=n.role==="user",A=b==="imessage",R=h({display:"flex",flexDirection:"column",alignItems:f?"flex-end":"flex-start",marginBottom:A&&!r?"2px":"16px"});if(R.className=`ago-message ago-message--${n.role}`,!f&&a&&n.agent){const g=h({fontSize:"13px",fontWeight:"500",color:J,marginBottom:"6px",padding:"0 4px"});g.className="ago-message__agent",g.textContent=n.agent.displayName||n.agent.name,R.appendChild(g)}if(!f&&n.sources&&n.sources.length>0){const g=h({display:"grid",gridTemplateColumns:"1fr 1fr",gap:"6px",marginBottom:"10px",width:"100%",maxWidth:"85%"});g.className="ago-message__sources",n.sources.forEach((s,c)=>{const y=s.url?document.createElement("a"):document.createElement("div");s.url&&y instanceof HTMLAnchorElement&&(y.href=s.url,y.target="_blank",y.rel="noopener noreferrer"),v(y,{display:"flex",alignItems:"center",gap:"8px",padding:"8px 10px",border:`1px solid ${L}`,borderRadius:"8px",backgroundColor:ee,textDecoration:"none",color:J,fontSize:"12px",overflow:"hidden"});const T=h({flexShrink:"0",width:"18px",height:"18px",borderRadius:"3px",border:`1px solid ${Be}`,color:Be,fontSize:"11px",fontWeight:"500",display:"flex",alignItems:"center",justifyContent:"center",backgroundColor:"#f0f4ff"});T.textContent=String(c+1);const k=h({overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"});k.textContent=s.title,k.title=s.title,y.append(T,k),g.appendChild(y)}),R.appendChild(g)}if(n.attachments&&n.attachments.length>0){const g=h({display:"flex",flexWrap:"wrap",gap:"6px",marginBottom:"6px",justifyContent:f?"flex-end":"flex-start",maxWidth:"75%"});g.className="ago-message__attachments";for(const s of n.attachments){const c=se.safeAttachmentUrl(s.url);if(se.canInlineImage(s)&&c){const M=document.createElement("a");M.href=c,M.target="_blank",M.rel="noopener noreferrer",v(M,{display:"inline-block",textDecoration:"none"});const q=document.createElement("img");q.src=c,q.alt=s.name,q.loading="lazy",v(q,{maxWidth:"180px",maxHeight:"160px",objectFit:"cover",borderRadius:"10px",border:`1px solid ${L}`,display:"block"}),M.appendChild(q),g.appendChild(M);continue}const y=c?document.createElement("a"):document.createElement("div");c&&y instanceof HTMLAnchorElement&&(y.href=c,y.target="_blank",y.rel="noopener noreferrer"),v(y,{display:"inline-flex",alignItems:"center",gap:"8px",padding:"8px 12px",borderRadius:"10px",border:`1px solid ${L}`,backgroundColor:f?"rgba(255,255,255,0.12)":ee,color:f?_e:J,fontSize:"13px",textDecoration:"none",maxWidth:"220px"});const T=document.createElement("span");T.textContent="📄",T.setAttribute("aria-hidden","true"),v(T,{fontSize:"16px",lineHeight:"1",flexShrink:"0"});const k=h({display:"flex",flexDirection:"column",minWidth:"0"}),j=h({overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",maxWidth:"160px"});j.textContent=s.name,j.title=s.name,k.appendChild(j);const V=se.formatFileSize(s.fileSize);if(V){const M=h({fontSize:"11px",color:xt});M.textContent=V,k.appendChild(M)}y.append(T,k),g.appendChild(y)}R.appendChild(g)}const _=f||C||A,Y=!!n.content||n.status==="IN_PROGRESS",D=h({maxWidth:_?A||f?p?"88%":"75%":p?"92%":"85%":"100%",padding:_?"10px 14px":"2px 8px",borderRadius:A?Kt:_?De:"0",backgroundColor:f?Ae:_?ft:"transparent",color:f?_e:J,overflowWrap:"break-word",textWrap:"pretty",fontSize:"16px",lineHeight:"1.6"});if(D.className="ago-message__content",A&&r){D.style.position="relative";const g=h({position:"absolute",zIndex:"0",bottom:"-2px",width:"20px",height:"20px",background:f?Ae:ft});g.className="ago-message__tail";const s=h({position:"absolute",zIndex:"1",bottom:"-2px",width:"10px",height:"20px",background:ht});s.className="ago-message__tail-mask",f?(g.style.right="-8px",g.style.borderBottomLeftRadius="16px 14px",s.style.right="-10px",s.style.borderBottomLeftRadius="10px"):(g.style.left="-7px",g.style.borderBottomRightRadius="16px 14px",s.style.left="-10px",s.style.borderBottomRightRadius="10px"),D.append(g,s)}if(n.content?D.appendChild(Ie(n.content)):n.status==="IN_PROGRESS"&&D.appendChild(an()),Y&&R.appendChild(D),i&&n.followUpReplies&&n.followUpReplies.length>0){const g=h({display:"flex",flexWrap:"wrap",gap:"8px",marginTop:"10px"});g.className="ago-message__followups";for(const s of n.followUpReplies){const c=document.createElement("button");c.type="button",c.className="ago-message__followup-btn",c.textContent=s,c.disabled=!m,v(c,{minHeight:"36px",padding:"6px 14px",fontSize:"14px",borderRadius:De,border:`1px solid ${L}`,backgroundColor:ee,color:J,cursor:m?"pointer":"default",transition:"border-color 0.15s"}),x&&c.addEventListener("click",()=>x(s)),c.addEventListener("mouseenter",()=>{c.style.borderColor=Be}),c.addEventListener("mouseleave",()=>{c.style.borderColor=L}),g.appendChild(c)}R.appendChild(g)}return R}function rn(n){const o=h({display:"flex",alignItems:"center",gap:"8px",padding:"10px 14px",backgroundColor:"#f0fdf4",color:"#15803d",borderRadius:De,marginTop:"8px",fontSize:"13px",border:"1px solid #bbf7d0"});o.className="ago-form-notice",o.setAttribute("role","status");const i=document.createElement("span");i.textContent="✓",i.setAttribute("aria-hidden","true"),v(i,{fontWeight:"700"});const r=document.createElement("span");return r.textContent=n,o.append(i,r),o}function an(){const n=h({display:"flex",gap:"4px",padding:"4px 0"});for(let o=0;o<3;o++){const i=h({width:"6px",height:"6px",borderRadius:"50%",backgroundColor:"#b5bfc8",animation:"ago-pulse 1.2s ease-in-out infinite",animationDelay:`${o*.2}s`});n.appendChild(i)}return n}function sn(){const n="http://www.w3.org/2000/svg",o=document.createElementNS(n,"svg");o.setAttribute("width","26"),o.setAttribute("height","26"),o.setAttribute("viewBox","0 0 24 24"),o.setAttribute("fill","none");const i=document.createElementNS(n,"path");return i.setAttribute("d","M21 11.5a8.38 8.38 0 0 1-8.5 8.5 8.5 8.5 0 0 1-3.8-.9L3 21l1.9-5.7a8.5 8.5 0 0 1-.9-3.8 8.38 8.38 0 0 1 8.5-8.5 8.38 8.38 0 0 1 8.5 8.5z"),i.setAttribute("stroke","currentColor"),i.setAttribute("stroke-width","2"),i.setAttribute("stroke-linecap","round"),i.setAttribute("stroke-linejoin","round"),o.appendChild(i),o}var gt="Form submitted.";function ln(n){if(typeof n=="string")return n||null;if(!n||typeof n!="object")return null;const o=n;if(typeof o.message=="string"&&o.message)return o.message;const i=o.result;if(typeof i=="string"&&i)return i;if(i&&typeof i=="object"){const r=i.message;if(typeof r=="string"&&r)return r}return null}var dn=0;function cn(n){const o=typeof n=="string"?document.querySelector(n):n;if(!o)throw new Error(`mountChatWidget: target "${String(n)}" not found.`);return o}function pn(n,o){const{title:i="Chat",welcomeMessage:r="Hello! How can I help you today?",placeholder:b="Type a message...",allowFiles:a=!1,height:C=500,placement:m="inline",width:x=400,defaultOpen:p=!1,logoUrl:f,showAgentName:A=!1,agentBubble:R=!1,bubbleStyle:_="default",showHeader:Y=!0,theme:D,loadThreads:g=!1,forms:s,formSubmittedMessage:c=gt,onFollowUpClick:y,onMessageSent:T,onMessageReceived:k,onFormSubmitted:j,onFormError:V,onOpen:M,onClose:q}=o,ne=typeof r=="string"?{message:r,mode:"static"}:r,Fe=ne.message,He=ne.mode??"static",Et="speed"in ne&&ne.speed||45,Pe="followUpReplies"in ne&&ne.followUpReplies||void 0;if(!o.client&&!o.config?.baseUrl)throw new se.AgoError("mountChatWidget requires either `client` or `config` (with a baseUrl).","config_missing_base_url");const E=o.client??new se.AgoClient(o.config),Ue=cn(n),z=m==="left"||m==="right",St=z&&(o.launcher??!0);let oe=z?p:!0;const Rt=o.mobile?.breakpoint??768,kt=o.mobile?.trigger??"focus",We=typeof window<"u"&&!!window.matchMedia,O=We?window.matchMedia(`(max-width: ${Rt}px)`):void 0,At=We?window.matchMedia("(prefers-reduced-motion: reduce)"):void 0,re=!z&&!!O,$e=o.persistConversation,ge=$e?Gt.createConversationSession($e===!0?{}:$e):void 0;let P=o.conversationId??ge?.getLastActiveThread()??void 0,u=[],Z=!1,de=null,K;const Ge=[];function _t(e){if(typeof c=="function"){const l=c(e);if(typeof l=="string"&&l)return l}const t=ln(e);return t||(typeof c=="string"?c:gt)}const he=[],d=h({display:"flex",flexDirection:"column",height:z?"100%":typeof C=="number"?`${C}px`:C,border:`1px solid ${L}`,borderRadius:qt,overflow:"hidden",backgroundColor:ee,fontFamily:Re,textAlign:"left",boxShadow:"rgba(15, 15, 15, 0.08) 0px 2px 16px 0px"});d.className="ago-chat-widget",Xt(d,D);let F;if(Y){if(F=h({padding:"14px 16px",borderBottom:`1px solid ${L}`,backgroundColor:Vt,color:pt,display:"flex",alignItems:"center",gap:"10px"}),F.className="ago-chat-widget__header",f){const t=document.createElement("img");t.src=f,t.alt="Logo",v(t,{height:"24px",width:"auto"}),F.appendChild(t)}const e=document.createElement("h3");if(e.textContent=i,v(e,{margin:"0",fontSize:"15px",fontWeight:"600"}),F.appendChild(e),z){const t=document.createElement("button");t.type="button",t.className="ago-chat-widget__close",t.setAttribute("aria-label","Close"),t.textContent="×",v(t,{marginLeft:"auto",background:"transparent",border:"none",color:pt,fontSize:"22px",lineHeight:"1",cursor:"pointer",padding:"0 2px"}),t.addEventListener("click",()=>Oe()),F.appendChild(t)}}const I=h({flex:"1",overflow:"auto",padding:"16px",backgroundColor:ht});I.className="ago-chat-widget__messages",I.setAttribute("role","log"),I.setAttribute("aria-live","polite"),I.setAttribute("aria-relevant","additions text"),I.setAttribute("aria-atomic","false");const{inputRow:Le,setDisabled:It,focus:xe}=Yt({placeholder:b,allowFiles:a,onSend:(e,t)=>{we(e,t)}});Qt(),d.append(...F?[F]:[],I,Le);let be=d,U,N;const ve=m==="left"?"left":"right";if(z&&(U=h({position:"fixed",top:"0",bottom:"0",[ve]:"0",width:typeof x=="number"?`${x}px`:x,maxWidth:"100vw",display:"flex",zIndex:"2147483000",transition:"transform 0.3s ease",boxShadow:"rgba(15, 15, 15, 0.18) 0px 0px 24px 0px"}),U.className="ago-chat-widget-panel",v(d,{height:"100%",width:"100%",borderRadius:"0",border:"none",boxShadow:"none",[ve==="left"?"borderRight":"borderLeft"]:`1px solid ${L}`}),U.appendChild(d),be=U),St){if(N=document.createElement("button"),N.type="button",N.className="ago-chat-widget-launcher",N.setAttribute("aria-label",`Open ${i}`),v(N,{position:"fixed",bottom:"20px",[ve]:"20px",width:"56px",height:"56px",borderRadius:"50%",border:"none",backgroundColor:Ae,color:_e,cursor:"pointer",display:"flex",alignItems:"center",justifyContent:"center",zIndex:"2147483000",boxShadow:"rgba(15, 15, 15, 0.2) 0px 4px 14px 0px",fontFamily:Re}),f){const e=document.createElement("img");e.src=f,e.alt="",v(e,{width:"26px",height:"26px",objectFit:"contain"}),N.appendChild(e)}else N.appendChild(sn());N.addEventListener("click",()=>Me())}const ce=re?`ago-vt-${++dn}`:"",je=52;let W=!1,pe=0,ie=0,H,fe,X;const ye={height:d.style.height,border:d.style.border,borderRadius:d.style.borderRadius,boxShadow:d.style.boxShadow};Ue.appendChild(be),N&&Ue.appendChild(N),Ce(),re&&(H=Pt(),d.insertBefore(H,d.firstChild),fe=document.createElement("style"),fe.id=ce,fe.textContent=`::view-transition-group(${ce}),::view-transition-group(${ce}-bar){animation-duration:0.3s;animation-timing-function:cubic-bezier(0.4,0,0.2,1)}`,document.head.appendChild(fe),kt==="focus"&&(d.addEventListener("pointerdown",e=>{W||!O?.matches||Le.contains(e.target)&&(e.preventDefault(),ue())},!0),d.addEventListener("focusin",e=>{O?.matches&&Le.contains(e.target)&&ue()})),document.addEventListener("keydown",tt)),O&&O.addEventListener("change",nt);const $t=y!==!1,Lt=y===!1?void 0:y??(e=>{re&&O?.matches&&!W?ue().then(()=>we(e)):we(e)});function $(){if(I.replaceChildren(),u.length===0){if(He==="static"){const t=h({textAlign:"center",color:xt,padding:"24px 16px",fontSize:"16px",lineHeight:"1.5"});t.appendChild(Ie(Fe)),I.appendChild(t)}}else u.forEach((t,l)=>{const S=l===u.length-1||u[l+1].role!==t.role;I.appendChild(on(t,{isLast:l===u.length-1,isLastOfBlock:S,bubbleStyle:_,showAgentName:A,agentBubble:R,followUpEnabled:$t,followUpHandler:Lt,isMobile:!!O?.matches}))});for(const t of Ge)I.appendChild(rn(t));if(de){const t=h({padding:"10px 14px",backgroundColor:"#fef2f2",color:"#dc2626",borderRadius:"12px",marginTop:"8px",fontSize:"13px",border:"1px solid #fecaca"});t.setAttribute("role","alert"),t.textContent=de,I.appendChild(t)}I.scrollTop=I.scrollHeight;const e=u[u.length-1];It(Z&&e?.role==="assistant"&&e.status==="IN_PROGRESS")}function Nt(){for(let e=u.length-1;e>=0;e--){const t=u[e];if(t.role==="assistant"&&t.status==="IN_PROGRESS")return t}}const Ve=e=>{P||(P=e.conversationId)},qe=e=>{const t=Nt();t&&(t.content+=e.content,$())},Ke=e=>{const t=u.findIndex(l=>l.id===e.id||l.role==="assistant"&&l.status==="IN_PROGRESS");t>=0?u[t]=e:u.push(e),$(),xe()},Xe=e=>{const t=u.findIndex(l=>l.id===e.id||l.role==="assistant"&&l.status==="IN_PROGRESS");t>=0?u[t]=e:u.push(e),Z=!1,$(),e.status==="DONE"&&k?.({id:e.id,content:e.content})},Qe=e=>{de=e.error,Z=!1,u=u.filter(t=>!t.id.startsWith("temp-")),$()};function Tt(e){const t={id:`ago-intro-${Date.now()}`,conversationId:"",content:"",role:"assistant",status:"IN_PROGRESS",createdAt:new Date};u.push(t),$();const l=e.match(/\S+\s*/g)??[e];let S=0;K=setInterval(()=>{if(S>=l.length){clearInterval(K),K=void 0,t.status="DONE",Pe&&(t.followUpReplies=Pe),$();return}t.content+=l[S],S++,$()},Et)}E.on("message:start",Ve),E.on("message:chunk",qe),E.on("message:answer-complete",Ke),E.on("message:complete",Xe),E.on("message:error",Qe);const Ye=e=>j?.(e),Ze=e=>V?.(e);j&&E.on("form:submitted",Ye),V&&E.on("form:error",Ze);const Ne=[];let Je=!1;const et=e=>{if(Je)return;Ne.push(e.install(E));let t=e.store.get().submitted;const l=e.store.subscribe(S=>{S.submitted&&!t&&(Ge.push(_t(S.submitResult)),$()),t=S.submitted});Ne.push(l)};for(const e of s??[])e.schema!=null?et(dt.createFormCollector(e)):dt.loadFormCollector(E,e).then(t=>et(t)).catch(()=>{});async function we(e,t){const l=e.trim();if(!l&&!t?.length||Z)return;if(T?.(l),Z=!0,de=null,K){clearInterval(K),K=void 0;const w=u.find(G=>G.id.startsWith("ago-intro-"));w&&!w.content?u=u.filter(G=>G!==w):w&&(w.status="DONE")}const S=Date.now();u.push({id:`temp-user-${S}`,conversationId:P||"",content:l,role:"user",status:"DONE",attachments:t&&t.length>0?se.attachmentsFromFiles(t):void 0,createdAt:new Date}),u.push({id:`temp-assistant-${S}`,conversationId:P||"",content:"",role:"assistant",status:"IN_PROGRESS",createdAt:new Date}),$();try{const w=await E.sendMessage(l,{conversationId:P,files:t});w.conversationId&&(P||(P=w.conversationId),ge?.setActiveThread(w.conversationId,w.createdAt));const G=u.find(Q=>Q.id===`temp-user-${S}`);if(G&&(G.id=`user-${S}`,G.conversationId=w.conversationId),!u.some(Q=>Q.id===w.id)){const Q=u.findIndex(Wt=>Wt.id===`temp-assistant-${S}`);Q>=0?u[Q]=w:u.push(w)}u=u.filter(Q=>!Q.id.startsWith("temp-")),Z=!1,$(),g&&Te()}catch(w){de=w instanceof Error?w.message:"Failed to send message",Z=!1,u=u.filter(G=>!G.id.startsWith("temp-")),$()}}async function Te(){try{const e=await E.getConversations();he.splice(0,he.length,...e)}catch{}return he}async function Mt(e){try{const t=(await E.getConversation(e))?.messages??[];t.length>0&&u.length===0&&(u=t,$())}catch{u.length===0&&(ge?.clear(),P=void 0)}}return $(),re&&O?.matches||xe(),P?Mt(P):He==="streaming"&&Tt(Fe),g&&Te(),{client:E,element:be,sendMessage:we,...z||re?{open:Bt,close:Dt,toggle:zt}:{},session:ge,threads:he,refreshThreads:Te,destroy(){K&&clearInterval(K),E.off("message:start",Ve),E.off("message:chunk",qe),E.off("message:answer-complete",Ke),E.off("message:complete",Xe),E.off("message:error",Qe),j&&E.off("form:submitted",Ye),V&&E.off("form:error",Ze),Je=!0,Ne.forEach(e=>e()),be.remove(),N?.remove(),H?.remove(),fe?.remove(),X?.remove(),O?.removeEventListener("change",nt),re&&(document.removeEventListener("keydown",tt),it(),document.documentElement.style.removeProperty("overflow")),o.client||E.destroy()}};function Ce(){if(!U)return;{const t=ve==="left"?"border-right":"border-left";O?.matches?(U.style.width="100%",d.style.borderRadius="0",d.style.removeProperty(t)):(U.style.width=typeof x=="number"?`${x}px`:x,d.style.setProperty(t,`1px solid ${L}`))}const e=m==="left"?"translateX(-100%)":"translateX(100%)";U.style.transform=oe?"translateX(0)":e,U.setAttribute("aria-hidden",oe?"false":"true"),N&&(N.style.display=oe?"none":"flex")}function Me(){oe=!0,Ce(),xe(),M?.()}function Oe(){oe=!1,Ce(),q?.()}function Ot(){oe?Oe():Me()}function Bt(){z?Me():ue()}function Dt(){z?Oe():me()}function zt(){z?Ot():W?me():ue()}function Ft(){return Array.from(d.querySelectorAll('a[href],button:not([disabled]),textarea:not([disabled]),input:not([disabled]),select:not([disabled]),[tabindex]:not([tabindex="-1"])')).filter(e=>e.getClientRects().length>0)}function tt(e){if(!W)return;if(e.key==="Escape"){me();return}if(e.key!=="Tab")return;const t=Ft();if(t.length===0){e.preventDefault();return}const l=t[0],S=t[t.length-1],w=document.activeElement;e.shiftKey&&(w===l||!d.contains(w))?(e.preventDefault(),S.focus()):!e.shiftKey&&(w===S||!d.contains(w))&&(e.preventDefault(),l.focus())}function nt(e){z?Ce():!e.matches&&W&&me(),$()}function ot(){return window.visualViewport?.height??window.innerHeight}function rt(){const e=window.visualViewport,t=e?.offsetTop??0,l=e?.height??pe,S=Math.min(0,t+l-pe);d.style.top=`${S}px`,H&&(H.style.top=`${t}px`)}function Ee(){ie||(ie=requestAnimationFrame(()=>{ie=0,rt()}))}function Ht(){window.visualViewport?.addEventListener("resize",Ee),window.visualViewport?.addEventListener("scroll",Ee)}function it(){window.visualViewport?.removeEventListener("resize",Ee),window.visualViewport?.removeEventListener("scroll",Ee),ie&&(cancelAnimationFrame(ie),ie=0)}function at(){I.scrollTop=I.scrollHeight}function Pt(){const e=h({position:"fixed",top:"0",left:"0",right:"0",height:`calc(${je}px + env(safe-area-inset-top))`,display:"none",alignItems:"center",justifyContent:"space-between",padding:"env(safe-area-inset-top) 14px 0",backgroundColor:ee,borderBottom:`1px solid ${L}`,zIndex:"2147483001",fontFamily:Re});if(e.className="ago-chat-widget-mobile-bar",f){const l=document.createElement("img");l.src=f,l.alt="",v(l,{height:"28px",width:"auto"}),e.appendChild(l)}else if(i){const l=document.createElement("span");l.textContent=i,v(l,{fontSize:"15px",fontWeight:"600",color:J,overflow:"hidden",whiteSpace:"nowrap",textOverflow:"ellipsis"}),e.appendChild(l)}else e.appendChild(h({}));const t=document.createElement("button");return t.type="button",t.setAttribute("aria-label","Close"),t.textContent="×",v(t,{border:"none",background:"transparent",fontSize:"26px",lineHeight:"1",color:J,cursor:"pointer",padding:"4px 8px"}),t.addEventListener("click",()=>{me()}),e.appendChild(t),e}function Ut(){return typeof document.startViewTransition=="function"&&!At?.matches&&!!O?.matches}function st(e){const t=document;if(Ut()&&t.startViewTransition){d.style.setProperty("view-transition-name",ce),H?.style.setProperty("view-transition-name",`${ce}-bar`);const l=()=>{d.style.removeProperty("view-transition-name"),H?.style.removeProperty("view-transition-name")};return t.startViewTransition(e).finished.then(l,l)}return e(),Promise.resolve()}function lt(e){if(e)X&&d.parentElement?.insertBefore(X,d),v(d,{position:"fixed",top:"0",left:"0",right:"0",bottom:"0",height:"var(--ago-vh, 100dvh)",zIndex:"2147483000",border:"none",borderRadius:"0",boxShadow:"none",paddingTop:`calc(${je}px + env(safe-area-inset-top))`,paddingBottom:"env(safe-area-inset-bottom)"}),d.setAttribute("role","dialog"),d.setAttribute("aria-modal","true"),d.setAttribute("aria-label",i),document.documentElement.style.overflow="hidden",F&&(F.style.display="none"),H&&(H.style.display="flex");else{X?.remove(),X=void 0;for(const t of["position","top","left","right","bottom","z-index","padding-top","padding-bottom"])d.style.removeProperty(t);d.style.height=ye.height,d.style.border=ye.border,d.style.borderRadius=ye.borderRadius,d.style.boxShadow=ye.boxShadow,d.removeAttribute("role"),d.removeAttribute("aria-modal"),d.removeAttribute("aria-label"),document.documentElement.style.removeProperty("overflow"),F&&(F.style.display="flex"),H&&(H.style.display="none")}}function ue(){if(W||!O?.matches)return Promise.resolve();const e=d.getBoundingClientRect();if(e.height>=ot()*.8)return Promise.resolve();W=!0,X=h({width:`${e.width}px`,height:`${e.height}px`}),X.className="ago-chat-widget-spacer",X.setAttribute("aria-hidden","true"),pe=ot(),d.style.setProperty("--ago-vh",`${pe}px`),rt(),Ht();const t=st(()=>lt(!0));return t.then(()=>{at(),xe()}),at(),M?.(),t}function me(){if(!W)return Promise.resolve();W=!1,it(),d.querySelector("textarea")?.blur();const e=st(()=>lt(!1));return e.then(()=>d.style.removeProperty("--ago-vh")),pe=0,q?.(),e}}exports.mountChatWidget=pn;exports.renderMarkdown=Ie;
|
package/dist/widget.d.cts
CHANGED
|
@@ -1092,6 +1092,15 @@ export declare type WelcomeMessage = string | {
|
|
|
1092
1092
|
message: string;
|
|
1093
1093
|
mode?: "static" | "streaming";
|
|
1094
1094
|
speed?: number;
|
|
1095
|
+
/**
|
|
1096
|
+
* Suggested follow-up replies rendered as clickable pills under the
|
|
1097
|
+
* greeting once it finishes typing. Only applies to `mode: "streaming"`
|
|
1098
|
+
* (the static empty-state has no message bubble to attach them to).
|
|
1099
|
+
* Clicking one behaves exactly like a backend follow-up reply: it sends
|
|
1100
|
+
* the text as the first message, unless {@link MountChatWidgetOptions.onFollowUpClick}
|
|
1101
|
+
* intercepts or disables it.
|
|
1102
|
+
*/
|
|
1103
|
+
followUpReplies?: string[];
|
|
1095
1104
|
};
|
|
1096
1105
|
|
|
1097
1106
|
/**
|
package/dist/widget.d.ts
CHANGED
|
@@ -1092,6 +1092,15 @@ export declare type WelcomeMessage = string | {
|
|
|
1092
1092
|
message: string;
|
|
1093
1093
|
mode?: "static" | "streaming";
|
|
1094
1094
|
speed?: number;
|
|
1095
|
+
/**
|
|
1096
|
+
* Suggested follow-up replies rendered as clickable pills under the
|
|
1097
|
+
* greeting once it finishes typing. Only applies to `mode: "streaming"`
|
|
1098
|
+
* (the static empty-state has no message bubble to attach them to).
|
|
1099
|
+
* Clicking one behaves exactly like a backend follow-up reply: it sends
|
|
1100
|
+
* the text as the first message, unless {@link MountChatWidgetOptions.onFollowUpClick}
|
|
1101
|
+
* intercepts or disables it.
|
|
1102
|
+
*/
|
|
1103
|
+
followUpReplies?: string[];
|
|
1095
1104
|
};
|
|
1096
1105
|
|
|
1097
1106
|
/**
|