UNPKG

@xenara/xena-sdk

Version:

A powerful SDK to embed AI agents into any web platform. Enable conversational experiences, automate workflows, and provide intelligent assistance through a customizable chat interface.

2 lines (1 loc) 2.74 kB
!function(){"use strict";window.Xena={init:async function({containerId:e="xena-root",apiKey:t,user_id:n,name:a,email:o,created_at:i}){const r=document.getElementById(e);if(!r)return void console.error(`[XENA] Container "${e}" not found.`);const d=r.shadowRoot||r.attachShadow({mode:"open"}),s=document.createElement("style");s.textContent="\n .xena-widget, .xena-launcher {\n visibility: hidden;\n opacity: 0;\n }\n",d.appendChild(s);const c=await async function(e){try{const t=await fetch(`https://backend.xenara.ai/api/widget/settings?apiKey=${e}`,{headers:{"Content-Type":"application/json","x-db-choice":"xenara"}});return t.ok?(await t.json()).settings:(console.error("[XENA] Invalid API Key or unauthorized domain"),null)}catch(e){return console.error("[XENA] Failed to fetch widget settings:",e),null}}(t);if(!c)return void console.error("[XENA] Failed to load settings. Check API Key and domain.");const l=c?.workspace_id;try{const e=document.createElement("link");e.rel="stylesheet",e.href="https://widget.xenara.ai/widget.css",e.onload=async()=>{const e=await fetch("https://widget.xenara.ai/widget.html"),t=await e.text(),r=(new DOMParser).parseFromString(t,"text/html");Array.from(r.body.children).forEach((e=>{d.appendChild(e)})),function(e,t){const{chatbot_color:n={},chatbot_logo:a="https://widget.xenara.ai/assets/black-logo.png"}=t,o=n.gradient_start_color||"#4c6ef5",i=n.gradient_end_color||"#4c6ef5",r=n.background_color||"#ffffff",d=e.querySelector(".xena-widget");d?(d.style.setProperty("--xena-gradient-start",n.is_gradient_enabled?o:"transparent"),d.style.setProperty("--xena-gradient-end",n.is_gradient_enabled?i:"transparent"),d.style.setProperty("--xena-widget-bg-color",r),console.log("[XENA] Customization applied on .xena-widget element")):console.warn("[XENA] .xena-widget not found in Shadow DOM")}(d,c);Array.from(d.querySelectorAll("script")).forEach((e=>{const t=document.createElement("script");Array.from(e.attributes).forEach((e=>t.setAttribute(e.name,e.value))),t.textContent=e.textContent,d.appendChild(t)}));const s=document.createElement("script");s.src="https://widget.xenara.ai/widget-runtime.js",s.onload=()=>{window.XenaRuntime?.init?window.XenaRuntime.init(d,{user_id:n,name:a,email:o,created_at:i,workspace_id:l,settings:c}):console.error("[XENA] Failed to initialize runtime.")};const p=document.createElement("style");p.textContent="\n .xena-widget, .xena-launcher {\n visibility: visible;\n opacity: 1;\n transition: opacity 0.3s ease;\n }\n ",d.appendChild(p),document.head.appendChild(s)},d.appendChild(e)}catch(e){console.error("[XENA] Failed to load widget assets:",e),d.innerHTML='<div style="color: red;">[XENA] Error loading widget content.</div>'}}}}();