Files
agent-desk-ng/web/public/sdk/agent-desk-sdk.min.js

2 lines
14 KiB
JavaScript

function normalizeWidgetLanguage(e){const t=String(e||"").toLowerCase();return t.startsWith("ru")?"ru-RU":t.startsWith("en")?"en-US":"zh-CN"}function getDefaultWidgetTitle(e){const t=normalizeWidgetLanguage(null==e?void 0:e.language);return"ru-RU"===t?"\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430":"en-US"===t?"Support":"\u5728\u7ebf\u5ba2\u670d"}function getLauncherText(e){const t=normalizeWidgetLanguage(null==e?void 0:e.language);return"ru-RU"===t?"\u0427\u0430\u0442":"en-US"===t?"Support":"\u5ba2\u670d"}!function(){var e;const t={language:"zh-CN",position:"right",themeColor:"#0f6cbd",width:"380px"},n=document.currentScript,i=(null==n?void 0:n.src)?n.src.replace(/\/sdk\/agent-desk-sdk\.min\.js(?:\?.*)?$/,""):"",r=window.__CS_AI_AGENT_WIDGET_STATE__;r&&(null===(e=window.AgentDeskWidget)||void 0===e||e.destroy());const a=r||{button:null,frame:null,frameLoaded:!1,frameReady:!1,initSent:!1,isOpen:!1,opening:!1,isMaximized:!1,mounted:!1,mountGeneration:0,openGeneration:0,cancelOpen:null,configLoading:!1,configPromise:null,frameHideTimer:null,config:null,frameConfig:null,frameUrl:null,animationDuration:260,messageListener:null,resizeListener:null};function o(e){const n=Object.assign(Object.assign({},t),e||{});return n.baseUrl=String(n.baseUrl||window.location.origin).replace(/\/$/,""),n.apiBaseUrl?n.apiBaseUrl=String(n.apiBaseUrl).replace(/\/$/,""):delete n.apiBaseUrl,n.channelId=String(n.channelId||""),n.language=normalizeWidgetLanguage(String(n.language||"zh-CN")),n.externalId&&(n.externalId=String(n.externalId)),"function"!=typeof n.getUserToken&&delete n.getUserToken,n}function s(e){return String(e.widgetBaseUrl||i||e.baseUrl||window.location.origin).replace(/\/$/,"")}function l(e){const t=o(e);return t.widgetBaseUrl=s(t),e.baseUrl||(t.baseUrl=t.widgetBaseUrl),t}function d(e,t,n){const i=s(e),r=new URL(`${i}/support/chat/`);return r.searchParams.set("channelId",e.channelId),r.searchParams.set("baseUrl",e.baseUrl),r.searchParams.set("parentOrigin",window.location.origin),e.requireUserToken&&r.searchParams.set("requireUserToken","true"),e.apiBaseUrl&&r.searchParams.set("apiBaseUrl",e.apiBaseUrl),e.externalId&&r.searchParams.set("externalId",e.externalId),n.signature&&r.searchParams.set("externalIdSignature",n.signature),n.issuedAt&&r.searchParams.set("externalIdSignedAt",String(n.issuedAt)),e.externalName&&r.searchParams.set("externalName",e.externalName),t&&r.searchParams.set("userToken",t),r}function c(e,t,n){const i=Object.assign({},e);return delete i.getUserToken,delete i.signExternalId,t?Object.assign(Object.assign({},i),{userToken:t}):n.signature?Object.assign(Object.assign({},i),{externalIdSignature:n.signature,externalIdSignedAt:n.issuedAt}):i}function u(e){if("function"!=typeof(null==e?void 0:e.getUserToken))return e.requireUserToken?Promise.reject(new Error("Support user token is required")):Promise.resolve("");try{return Promise.resolve(e.getUserToken()).then(t=>{const n=String(t||"").trim();if(e.requireUserToken&&!n)throw new Error("Support user token is required");return n})}catch(e){return Promise.reject(e)}}function m(e){if(!(null==e?void 0:e.externalId)||"function"!=typeof e.signExternalId)return Promise.resolve({signature:"",issuedAt:0});try{return Promise.resolve(e.signExternalId(e.externalId)).then(e=>({signature:String((null==e?void 0:e.signature)||"").trim(),issuedAt:Number(null==e?void 0:e.issuedAt)||0}))}catch(e){return Promise.reject(e)}}function f(e){return Promise.all([u(e),m(e)]).then(([t,n])=>({frameUrl:d(e,t,n),frameConfig:c(e,t,n)}))}function g(e,t){const n="function"==typeof AbortController?new AbortController:null;return new Promise((i,r)=>{const a=window.setTimeout(()=>{null==n||n.abort(),r(new Error("Widget configuration request timed out"))},5e3);Promise.resolve().then(()=>fetch(e,Object.assign(Object.assign({},t),{signal:null==n?void 0:n.signal}))).then(e=>e.json()).then(e=>{window.clearTimeout(a),i(e)},e=>{window.clearTimeout(a),r(e)})})}function p(){null!==a.frameHideTimer&&(window.clearTimeout(a.frameHideTimer),a.frameHideTimer=null)}function h(){const e=a.frame,t=a.config;if(!e||!t)return;e.style.position="fixed",e.style.border="0",e.style.overflow="hidden",e.style.background="#fff",e.style.zIndex="2147483000",e.style.boxShadow="0 0 0 1px rgba(15, 35, 65, 0.06), 0 4px 12px rgba(15, 35, 65, 0.08), 0 24px 64px rgba(15, 35, 65, 0.22)",e.style.willChange="top,right,bottom,left,width,height,opacity,transform,border-radius",e.style.transition="top 260ms cubic-bezier(0.22, 1, 0.36, 1), right 260ms cubic-bezier(0.22, 1, 0.36, 1), bottom 260ms cubic-bezier(0.22, 1, 0.36, 1), left 260ms cubic-bezier(0.22, 1, 0.36, 1), width 260ms cubic-bezier(0.22, 1, 0.36, 1), height 260ms cubic-bezier(0.22, 1, 0.36, 1), opacity 220ms ease, transform 260ms cubic-bezier(0.22, 1, 0.36, 1), border-radius 260ms cubic-bezier(0.22, 1, 0.36, 1), box-shadow 260ms ease",e.style.transformOrigin="left"===t.position?"left bottom":"right bottom";const n="number"==typeof window.innerWidth&&window.innerWidth>0&&window.innerWidth<=480;if(a.isMaximized||n)return e.style.top="max(12px, env(safe-area-inset-top))",e.style.right="max(12px, env(safe-area-inset-right))",e.style.bottom="max(12px, env(safe-area-inset-bottom))",e.style.left="max(12px, env(safe-area-inset-left))",e.style.width="calc(100vw - max(12px, env(safe-area-inset-left)) - max(12px, env(safe-area-inset-right)))",e.style.maxWidth="none",e.style.height="calc(100dvh - max(12px, env(safe-area-inset-top)) - max(12px, env(safe-area-inset-bottom)))",void(e.style.borderRadius="16px");e.style.top="",e.style.bottom="max(88px, calc(84px + env(safe-area-inset-bottom)))",e.style.right="left"===t.position?"":"max(12px, env(safe-area-inset-right))",e.style.left="left"===t.position?"max(12px, env(safe-area-inset-left))":"",e.style.width=t.width||"380px",e.style.maxWidth="calc(100vw - max(12px, env(safe-area-inset-left)) - max(12px, env(safe-area-inset-right)))",e.style.height="min(760px, calc(100dvh - max(104px, calc(88px + env(safe-area-inset-bottom))) - max(12px, env(safe-area-inset-top))))",e.style.borderRadius="16px"}function b(e){var t;if((null===(t=a.frame)||void 0===t?void 0:t.contentWindow)&&a.frameUrl)try{a.frame.contentWindow.postMessage(e,a.frameUrl.origin)}catch(e){console.error("[agent-desk-widget] postMessage failed",e)}}function y(){a.frame&&a.frameLoaded&&a.frameReady&&a.frameConfig&&(a.initSent||(a.initSent=!0,b({type:"agent-desk:init",payload:a.frameConfig})),b({type:a.isOpen?"agent-desk:open":"agent-desk:minimize"}),b({type:"agent-desk:maximized",payload:{isMaximized:a.isMaximized}}))}function x(){var e,t;null===(e=a.button)||void 0===e||e.setAttribute("aria-expanded",String(a.isOpen||a.opening)),null===(t=a.button)||void 0===t||t.setAttribute("aria-busy",String(a.opening))}function w(){x();const e=a.frame;if(e){if(p(),h(),e.style.display="block",a.isOpen)return e.style.visibility="visible",e.style.pointerEvents="auto",a.frameHideTimer=window.setTimeout(()=>{a.frame===e&&a.isOpen&&a.mounted&&(e.style.opacity="1",e.style.transform="translate3d(0, 0, 0) scale(1)")},16),void y();e.style.pointerEvents="none",e.style.opacity="0",e.style.transform=a.isMaximized?"translate3d(0, 10px, 0) scale(0.985)":"translate3d(0, 16px, 0) scale(0.96)",a.frameHideTimer=window.setTimeout(()=>{a.frame===e&&!a.isOpen&&a.mounted&&(e.style.visibility="hidden")},a.animationDuration),y()}}function v(){var e;p(),(null===(e=a.frame)||void 0===e?void 0:e.parentNode)&&a.frame.parentNode.removeChild(a.frame),a.frame=null,a.frameLoaded=!1,a.frameReady=!1,a.initSent=!1}function U(e){if(!a.mounted||!a.frame||!a.frameUrl||e.source!==a.frame.contentWindow||e.origin!==a.frameUrl.origin)return;if(!e.data||"object"!=typeof e.data)return;const t=e.data;if("agent-desk:ready"===t.type)return a.frameReady=!0,void y();"agent-desk:request-minimize"!==t.type&&"agent-desk:request-close"!==t.type?"agent-desk:request-toggle-maximize"===t.type&&(a.isMaximized=!a.isMaximized,w()):I()}function k(e){const t=Object.assign({},e||window.AgentDeskConfig||{channelId:""});if(S(),a.config=l(t),!a.config.channelId)return void console.error("[agent-desk-widget] channelId is required");a.mounted=!0,a.messageListener=U,a.resizeListener=h,window.addEventListener("message",a.messageListener),window.addEventListener("resize",a.resizeListener);const n=a.mountGeneration;a.configLoading=!0;const i=t.language?Promise.resolve(a.config):function(e){const t=String(e.apiBaseUrl||e.baseUrl||"").replace(/\/$/,"");return t&&"function"==typeof fetch?g(`${t}/api/config`,{method:"GET",cache:"no-store"}).then(t=>{var n;return t&&!1!==t.success?o(Object.assign(Object.assign({},e),{language:(null===(n=t.data)||void 0===n?void 0:n.language)||e.language})):e}).catch(()=>e):Promise.resolve(e)}(a.config);a.configPromise=i.then(e=>a.mounted&&a.mountGeneration===n?function(e){const t=String(e.apiBaseUrl||e.baseUrl||"").replace(/\/$/,"");return t&&e.channelId&&"function"==typeof fetch?g(`${t}/api/channel/config?channelId=${encodeURIComponent(e.channelId)}`,{method:"GET",cache:"no-store",headers:{"X-Channel-Id":e.channelId}}).then(t=>t&&!1!==t.success?function(e,t){if(!t)return e;const n=Object.assign({},e);return["title","subtitle","themeColor","position","width"].forEach(e=>{const i=t[e];null!=i&&(n[e]=i)}),n}(e,t.data||{}):e).catch(()=>e):Promise.resolve(e)}(e):null).then(e=>{e&&a.mounted&&a.mountGeneration===n&&(a.configLoading=!1,a.config=o(e),function(){if(a.button)return a.button;const e=a.config;if(!e)return null;const t=document.createElement("button"),n=document.createElementNS("http://www.w3.org/2000/svg","svg"),i=document.createElement("span"),r=e.themeColor||"#0f6cbd",o="function"==typeof window.matchMedia&&window.matchMedia("(prefers-reduced-motion: reduce)").matches,s="0 1px 2px rgba(15, 35, 65, 0.16), 0 8px 24px rgba(15, 35, 65, 0.22)",l="0 2px 4px rgba(15, 35, 65, 0.18), 0 12px 32px rgba(15, 35, 65, 0.28)";t.type="button",t.dataset.agentDeskWidget="launcher",t.setAttribute("aria-label",e.title||getDefaultWidgetTitle(e)),n.setAttribute("viewBox","0 0 24 24"),n.setAttribute("fill","none"),n.setAttribute("stroke","currentColor"),n.setAttribute("stroke-width","2"),n.setAttribute("stroke-linecap","round"),n.setAttribute("stroke-linejoin","round"),n.setAttribute("aria-hidden","true"),n.style.width="26px",n.style.height="26px",n.style.flex="0 0 auto",["M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"].forEach(e=>{const t=document.createElementNS("http://www.w3.org/2000/svg","path");t.setAttribute("d",e),n.appendChild(t)}),i.textContent=getLauncherText(e),i.style.position="absolute",i.style.width="1px",i.style.height="1px",i.style.overflow="hidden",i.style.clip="rect(0 0 0 0)",i.style.whiteSpace="nowrap",t.style.position="fixed",t.style.bottom="max(20px, env(safe-area-inset-bottom))",t.style.right="left"===e.position?"":"max(20px, env(safe-area-inset-right))",t.style.left="left"===e.position?"max(20px, env(safe-area-inset-left))":"",t.style.zIndex="2147483000",t.style.display="inline-flex",t.style.alignItems="center",t.style.justifyContent="center",t.style.width="56px",t.style.height="56px",t.style.border="0",t.style.borderRadius="999px",t.style.padding="0",t.style.background=r,t.style.color="#fff",t.style.boxShadow=s,t.style.cursor="pointer",t.style.outline="none",t.style.transition=o?"none":"transform 180ms cubic-bezier(0.22, 1, 0.36, 1), box-shadow 180ms ease",t.appendChild(n),t.appendChild(i);const d=()=>{t.style.transform="none",t.style.boxShadow=s};t.addEventListener("mouseenter",()=>{t.style.transform="translateY(-2px)",t.style.boxShadow=l}),t.addEventListener("mouseleave",d),t.addEventListener("focus",()=>{t.style.boxShadow=`${l}, 0 0 0 3px ${r}55`}),t.addEventListener("blur",d),t.addEventListener("click",()=>{a.isOpen||a.opening?I():L().catch(()=>{})}),document.body.appendChild(t),a.button=t,x()}())})}function S(){var e;a.mounted=!1,a.mountGeneration=(a.mountGeneration||0)+1,z(),a.messageListener&&window.removeEventListener("message",a.messageListener),a.resizeListener&&window.removeEventListener("resize",a.resizeListener),a.messageListener=null,a.resizeListener=null,v(),(null===(e=a.button)||void 0===e?void 0:e.parentNode)&&a.button.parentNode.removeChild(a.button),a.button=null,a.isOpen=!1,a.isMaximized=!1,a.configLoading=!1,a.configPromise=null,a.frameConfig=null,a.frameUrl=null}function z(){var e;a.openGeneration=(a.openGeneration||0)+1,null===(e=a.cancelOpen)||void 0===e||e.call(a),a.cancelOpen=null,a.opening=!1}function I(){z(),a.isOpen=!1,w()}async function L(){var e;if(a.mounted||k(a.config||window.AgentDeskConfig),!(null===(e=a.config)||void 0===e?void 0:e.channelId)||!a.mounted)return;z();const t=a.mountGeneration,n=a.openGeneration,i=new Promise(e=>{a.cancelOpen=()=>e(null)}),r=()=>a.mounted&&a.mountGeneration===t&&a.openGeneration===n;a.opening=!0,x();try{if(await Promise.race([a.configPromise,i]),!r()||!a.config)return;const e=await Promise.race([f(Object.assign({},a.config)),i]);if(!e||!r())return;if(a.frameUrl=e.frameUrl,a.frameConfig=e.frameConfig,a.frame&&a.frame.src!==e.frameUrl.toString()&&v(),a.frame||function(){if(a.frame)return a.frame;if(!a.frameUrl||!a.config||!a.mounted)return null;const e=document.createElement("iframe");a.frame=e,e.dataset.agentDeskWidget="frame",e.title=a.config.title||getDefaultWidgetTitle(a.config),e.src=a.frameUrl.toString(),h(),e.style.display="block",e.style.visibility="hidden",e.style.pointerEvents="none",e.style.opacity="0",e.style.transform="translate3d(0, 18px, 0) scale(0.96)",e.addEventListener("load",()=>{a.frame===e&&a.mounted&&(a.frameLoaded=!0,w())}),document.body.appendChild(e)}(),!a.frame)return;a.opening=!1,a.isOpen=!0,w()}catch(e){if(r())throw console.error("[agent-desk-widget] open failed",e),e}finally{r()&&(a.cancelOpen=null,a.opening=!1,x())}}r||(window.__CS_AI_AGENT_WIDGET_STATE__=a),window.AgentDeskWidget={mount:k,destroy:S,open:()=>L(),close:I,getChatUrl:()=>{const e=a.config?Object.assign({},a.config):l(window.AgentDeskConfig||{channelId:""});return e.channelId?f(e).then(({frameUrl:e})=>e.toString()):Promise.reject(new Error("channelId is required"))}},window.AgentDeskConfig&&k(window.AgentDeskConfig)}();