Files
ai-agent/web/public/sdk/cs-ai-agent-sdk.min.js
T
mlogclub bd9fad68a7 refactor: update Kefu widget configuration to use dynamic user token retrieval
- Changed `userToken` property to `getUserToken` function in KefuWidgetHostConfig for dynamic token fetching.
- Introduced KefuWidgetRuntimeConfig to manage runtime-specific configurations.
- Updated readKefuWidgetConfig and setKefuWidgetConfig functions to accommodate new configuration structure.
- Enhanced cs-ai-agent-sdk.js to handle user token resolution and frame configuration.
- Added tests for dynamic user token retrieval in cs-ai-agent-sdk.test.mjs.
- Removed deprecated cs-agent-widget.js file as part of the refactor.
2026-05-06 18:19:49 +08:00

2 lines
9.1 KiB
JavaScript

!function(){var e={position:"right",themeColor:"#0f6cbd",width:"380px"},t=window.__CS_AGENT_WIDGET_STATE__;function n(t){var n,r={};for(n in e)Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n]);for(n in t=t||{})Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n]);return r.baseUrl=String(r.baseUrl||window.location.origin).replace(/\/$/,""),r.apiBaseUrl?r.apiBaseUrl=String(r.apiBaseUrl).replace(/\/$/,""):delete r.apiBaseUrl,r.channelId=String(r.channelId||""),r.externalId&&(r.externalId=String(r.externalId)),"function"!=typeof r.getUserToken&&delete r.getUserToken,r}function r(e){var t=document.currentScript;return t&&t.src?t.src.replace(/\/sdk\/cs-ai-agent-sdk\.min\.js(?:\?.*)?$/,""):String(e.widgetBaseUrl||e.baseUrl||window.location.origin).replace(/\/$/,"")}function i(e,t){var n,r={};for(n in e)Object.prototype.hasOwnProperty.call(e,n)&&"getUserToken"!==n&&(r[n]=e[n]);return t&&(r.userToken=t),r}function a(){return function(){var e=t.config||{};if("function"!=typeof e.getUserToken)return Promise.resolve("");try{return Promise.resolve(e.getUserToken()).then(function(e){return String(e||"").trim()})}catch(e){return Promise.reject(e)}}().then(function(e){return t.frameUrl=function(e,t){var n=r(e),i=new URL(n+"/kefu/chat/");return i.searchParams.set("channelId",e.channelId),i.searchParams.set("baseUrl",e.baseUrl),e.apiBaseUrl&&i.searchParams.set("apiBaseUrl",e.apiBaseUrl),e.externalId&&i.searchParams.set("externalId",e.externalId),e.externalName&&i.searchParams.set("externalName",e.externalName),t&&i.searchParams.set("userToken",t),i}(t.config,e),t.frameConfig=i(t.config,e),t.frameUrl})}function o(){t.frameHideTimer&&(window.clearTimeout(t.frameHideTimer),t.frameHideTimer=null),t.frameDestroyTimer&&(window.clearTimeout(t.frameDestroyTimer),t.frameDestroyTimer=null)}function s(){var e=t.frame,n=t.config;if(e&&n){if(e.style.position="fixed",e.style.border="0",e.style.overflow="hidden",e.style.background="#fff",e.style.zIndex="2147483000",e.style.boxShadow="0 28px 80px rgba(15, 35, 65, 0.28)",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"===n.position?"left bottom":"right bottom",t.isMaximized)return e.style.top="20px",e.style.right="20px",e.style.bottom="20px",e.style.left="20px",e.style.width="calc(100vw - 40px)",e.style.maxWidth="none",e.style.height="calc(100vh - 40px)",void(e.style.borderRadius="24px");e.style.top="",e.style.bottom="112px",e.style.right="left"===n.position?"":"24px",e.style.left="left"===n.position?"24px":"",e.style.width=n.width||"380px",e.style.maxWidth="calc(100vw - 24px)",e.style.height="min(760px, calc(100vh - 136px))",e.style.borderRadius="28px"}}function l(e){if(t.frame&&t.frame.contentWindow&&t.frameUrl)try{t.frame.contentWindow.postMessage(e,t.frameUrl.origin)}catch(e){console.error("[cs-agent-widget] postMessage failed",e)}}function c(){t.frame&&t.frameLoaded&&t.frameReady&&(t.initSent||(t.initSent=!0,l({type:"cs-agent:init",payload:t.frameConfig||i(t.config,"")})),l({type:t.isOpen?"cs-agent:open":"cs-agent:minimize"}),l({type:"cs-agent:maximized",payload:{isMaximized:t.isMaximized}}))}function f(){var e=t.frame;if(e){if(o(),s(),e.style.display="block",t.isOpen)return e.style.visibility="visible",e.style.pointerEvents="auto",t.frameHideTimer=window.setTimeout(function(){t.frame&&(t.frame.style.opacity="1",t.frame.style.transform="translate3d(0, 0, 0) scale(1)")},16),void c();e.style.pointerEvents="none",e.style.opacity="0",e.style.transform=t.isMaximized?"translate3d(0, 10px, 0) scale(0.985)":"translate3d(0, 16px, 0) scale(0.96)",t.frameHideTimer=window.setTimeout(function(){t.frame&&!t.isOpen&&(t.frame.style.visibility="hidden")},t.animationDuration),c()}}function d(e){var i=e||window.CSAgentConfig||{};t.config=n(i);var a=r(t.config);i.baseUrl||(t.config.baseUrl=a),t.config.channelId?(t.configLoading=!0,function(e){var t=String(e.apiBaseUrl||e.baseUrl||"").replace(/\/$/,"");if(!t||!e.channelId||"function"!=typeof fetch)return Promise.resolve(e);var n=t+"/api/channel/config?channelId="+encodeURIComponent(e.channelId);return fetch(n,{method:"GET",cache:"no-store",headers:{"X-Channel-Id":e.channelId}}).then(function(e){return e.json()}).then(function(t){return t&&!1!==t.success?function(e,t){if(!t)return e;var n,r={};for(n in e)Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n]);for(var i=["title","subtitle","themeColor","position","width"],a=0;a<i.length;a+=1)n=i[a],Object.prototype.hasOwnProperty.call(t,n)&&void 0!==t[n]&&null!==t[n]&&(r[n]=t[n]);return r}(e,t.data||{}):e}).catch(function(){return e})}(t.config).then(function(e){t.configLoading=!1,t.config=n(e),t.button&&t.button.parentNode&&(t.button.parentNode.removeChild(t.button),t.button=null),function(){if(t.button)return t.button;var e=t.config,n=document.createElement("button"),r=document.createElementNS("http://www.w3.org/2000/svg","svg"),i=document.createElement("span");n.type="button",n.dataset.csAgentWidget="launcher",n.setAttribute("aria-label",e.title||"\u5728\u7ebf\u5ba2\u670d"),r.setAttribute("viewBox","0 0 24 24"),r.setAttribute("fill","none"),r.setAttribute("stroke","currentColor"),r.setAttribute("stroke-width","2"),r.setAttribute("stroke-linecap","round"),r.setAttribute("stroke-linejoin","round"),r.setAttribute("aria-hidden","true"),r.style.width="24px",r.style.height="24px",r.style.flex="0 0 auto",["M3 11a9 9 0 1 1 18 0","M3 11h3a2 2 0 0 1 2 2v3a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z","M21 11h-3a2 2 0 0 0-2 2v3a2 2 0 0 0 2 2h1a2 2 0 0 0 2-2z","M21 16v2a4 4 0 0 1-4 4h-5"].forEach(function(e){var t=document.createElementNS("http://www.w3.org/2000/svg","path");t.setAttribute("d",e),r.appendChild(t)}),i.textContent="\u5ba2\u670d",i.style.display="block",n.style.position="fixed",n.style.bottom="24px",n.style.right="left"===e.position?"":"24px",n.style.left="left"===e.position?"24px":"",n.style.zIndex="2147483000",n.style.display="inline-flex",n.style.flexDirection="column",n.style.alignItems="center",n.style.justifyContent="center",n.style.gap="4px",n.style.width="64px",n.style.height="64px",n.style.border="0",n.style.borderRadius="999px",n.style.padding="0",n.style.background=e.themeColor||"#0f6cbd",n.style.color="#fff",n.style.font="600 13px/1 sans-serif",n.style.boxShadow="0 18px 40px rgba(15, 35, 65, 0.24)",n.style.cursor="pointer",n.appendChild(r),n.appendChild(i),n.addEventListener("click",function(){if(t.isOpen)return t.isOpen=!1,void f();m()}),document.body.appendChild(n),t.button=n}()})):console.error("[cs-agent-widget] channelId is required")}function m(){return a().then(function(){t.frame||(t.frame?t.frame:t.frameUrl&&t.config&&(t.frame=document.createElement("iframe"),t.frame.dataset.csAgentWidget="frame",t.frame.title=t.config.title||"\u5728\u7ebf\u5ba2\u670d",t.frame.src=t.frameUrl.toString(),s(),t.frame.style.display="block",t.frame.style.visibility="hidden",t.frame.style.pointerEvents="none",t.frame.style.opacity="0",t.frame.style.transform="translate3d(0, 18px, 0) scale(0.96)",t.frame.addEventListener("load",function(){t.frameLoaded=!0,f()}),document.body.appendChild(t.frame),t.frame)),t.frame&&(t.isOpen=!0,f())}).catch(function(e){console.error("[cs-agent-widget] open failed",e)})}t||(t={button:null,frame:null,frameLoaded:!1,frameReady:!1,initSent:!1,isOpen:!1,isMaximized:!1,configLoading:!1,frameHideTimer:null,frameDestroyTimer:null,config:null,frameConfig:null,frameUrl:null,animationDuration:260},window.__CS_AGENT_WIDGET_STATE__=t),window.CSAgentWidget={mount:d,destroy:function(){o(),t.frame&&t.frame.parentNode&&t.frame.parentNode.removeChild(t.frame),t.button&&t.button.parentNode&&t.button.parentNode.removeChild(t.button),t.button=null,t.frame=null,t.frameLoaded=!1,t.frameReady=!1,t.initSent=!1,t.isOpen=!1,t.isMaximized=!1,t.configLoading=!1,t.frameConfig=null,t.frameUrl=null},open:function(){return m()},close:function(){t.isOpen=!1,f()},getChatUrl:function(){return t.config||d(window.CSAgentConfig||{}),t.config&&t.config.channelId?a().then(function(e){return e.toString()}):Promise.reject(new Error("channelId is required"))}},t.listenerBound||(window.addEventListener("message",function(e){if(t.frame&&e.source===t.frame.contentWindow){var n=e.data||{};return"cs-agent:ready"===n.type?(t.frameReady=!0,void c()):"cs-agent:request-minimize"===n.type?(t.isOpen=!1,void f()):void("cs-agent:request-close"!==n.type?"cs-agent:request-toggle-maximize"===n.type&&(t.isMaximized=!t.isMaximized,f()):t.frame&&(o(),t.frame.style.pointerEvents="none",t.frame.style.opacity="0",t.frame.style.transform="translate3d(0, 18px, 0) scale(0.94)",t.frame.style.visibility="hidden",t.frameDestroyTimer=window.setTimeout(function(){t.frame&&(t.frame.parentNode&&t.frame.parentNode.removeChild(t.frame),t.frame=null,t.frameLoaded=!1,t.frameReady=!1,t.initSent=!1,t.isOpen=!1,t.isMaximized=!1,o())},t.animationDuration)))}}),t.listenerBound=!0),window.CSAgentConfig&&d(window.CSAgentConfig)}();