Files
ai-agent/web/public/sdk/cs-ai-agent-sdk.min.js
T

2 lines
8.6 KiB
JavaScript
Raw Normal View History

(function(){var DEFAULT_CONFIG={position:"right",themeColor:"#0f6cbd",width:"380px",externalSource:"web_chat",};var state=window.__CS_AGENT_WIDGET_STATE__;if(!state){state={button:null,frame:null,frameLoaded:false,frameReady:false,initSent:false,isOpen:false,isMaximized:false,frameHideTimer:null,frameDestroyTimer:null,config:null,frameUrl:null,animationDuration:260,};window.__CS_AGENT_WIDGET_STATE__=state;}function normalizeConfig(config){var merged={};var key;for(key in DEFAULT_CONFIG){if(Object.prototype.hasOwnProperty.call(DEFAULT_CONFIG,key)){merged[key]=DEFAULT_CONFIG[key];}}config=config||{};for(key in config){if(Object.prototype.hasOwnProperty.call(config,key)){merged[key]=config[key];}}merged.baseUrl=String(merged.baseUrl||window.location.origin).replace(/\/$/,"");merged.apiBaseUrl=String(merged.apiBaseUrl||merged.baseUrl).replace(/\/$/,"");merged.channelId=String(merged.channelId||"");merged.externalSource=String(merged.externalSource||"web_chat");return merged;}function resolveWidgetBaseUrl(config){var currentScript=document.currentScript;if(currentScript&&currentScript.src){return currentScript.src.replace(/\/sdk\/cs-ai-agent-sdk\.min\.js(?:\?.*)?$/,"");}return String(config.widgetBaseUrl||config.baseUrl||window.location.origin).replace(/\/$/,"");}function createFrameUrl(config){var widgetBaseUrl=resolveWidgetBaseUrl(config);var frameUrl=new URL(widgetBaseUrl+"/kefu/chat/");frameUrl.searchParams.set("channelId",config.channelId);frameUrl.searchParams.set("baseUrl",config.baseUrl);if(config.apiBaseUrl)frameUrl.searchParams.set("apiBaseUrl",config.apiBaseUrl);if(config.externalSource)frameUrl.searchParams.set("externalSource",config.externalSource);if(config.title)frameUrl.searchParams.set("title",config.title);if(config.subtitle)frameUrl.searchParams.set("subtitle",config.subtitle);if(config.position)frameUrl.searchParams.set("position",config.position);if(config.themeColor)frameUrl.searchParams.set("themeColor",config.themeColor);if(config.width)frameUrl.searchParams.set("width",config.width);if(config.subject)frameUrl.searchParams.set("subject",config.subject);return frameUrl;}function clearFrameTimers(){if(state.frameHideTimer){window.clearTimeout(state.frameHideTimer);state.frameHideTimer=null;}if(state.frameDestroyTimer){window.clearTimeout(state.frameDestroyTimer);state.frameDestroyTimer=null;}}function applyFrameLayout(){var frame=state.frame;var config=state.config;if(!frame||!config){return;}frame.style.position="fixed";frame.style.border="0";frame.style.overflow="hidden";frame.style.background="#fff";frame.style.zIndex="2147483000";frame.style.boxShadow="0 28px 80px rgba(15,35,65,0.28)";frame.style.willChange="top,right,bottom,left,width,height,opacity,transform,border-radius";frame.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";frame.style.transformOrigin=config.position==="left"?"left bottom":"right bottom";if(state.isMaximized){frame.style.top="20px";frame.style.right="20px";frame.style.bottom="20px";frame.style.left="20px";frame.style.width="calc(100vw-40px)";frame.style.maxWidth="none";frame.style.height="calc(100vh-40px)";frame.style.borderRadius="24px";return;}frame.style.top="";frame.style.bottom="88px";frame.style.right=config.position==="left"?"":"24px";frame.style.left=config.position==="left"?"24px":"";frame.style.width=config.width||"380px";frame.style.maxWidth="calc(100vw-24px)";frame.style.height="min(760px,calc(100vh-112px))";frame.style.borderRadius="28px";}function postToFrame(message){if(!state.frame||!state.frame.contentWindow||!state.frameUrl){return;}try{state.frame.contentWindow.postMessage(message,state.frameUrl.origin);}catch(error){console.error("[cs-agent-widget]postMessage failed",error);}}function flushFrameState(){if(!state.frame||!s