Sid Gifari File Manager
🏠 Root
/
home
/
genremedia08
/
thepassage.overlookedtracks.com
/
wp-content9
/
plugins
/
echo-knowledge-base
/
js
/
ai
/
Editing: ai-chat-api.min.js
(function(){"use strict";if(!window.EPKBChatUtils){console.error("ChatAPIClient: EPKBChatUtils not loaded");return}const{isInvalidNonceError:e,parseErrorResponse:s,generateIdempotencyKey:t,prepareMessage:n,createHeaders:r,createFetchOptions:o,safeParseJSON:i}=window.EPKBChatUtils;class a{constructor(e){this.config=e||{};this.baseUrl=this.config.rest_url||"/wp-json";this.restNonce=this.config.rest_nonce||"";this.isAdmin=this.config.isAdmin||false;this.debugMode=this.config.debugMode||false;this.showTechnicalLogs=this.isAdmin||this.debugMode;this.retryTimeoutRef=null;if(this.showTechnicalLogs){if(!e){console.warn("ChatAPIClient: No configuration provided, using defaults")}if(!this.restNonce){console.warn("ChatAPIClient: No REST nonce provided, authentication may fail")}}}setNonce(e){this.restNonce=e}getNonce(){return this.restNonce}async refreshNonce(e=false){if(!e&&this.restNonce){return this.restNonce}const s=await this.startSession();if(s.success&&s.data.rest_nonce){return s.data.rest_nonce}return this.restNonce}async startSession(){try{const e=await fetch(`${this.baseUrl}epkb-public/v1/ai-chat/start-session`,o("POST",{},1e4,null));const t=await i(e);if(!e.ok){throw s(new Error("Failed to start session"),{status:e.status,data:t},this.isAdmin)}if(t.rest_nonce){this.restNonce=t.rest_nonce}return{success:true,data:t}}catch(e){const t=e.type?e:s(e,null,this.isAdmin);if(this.showTechnicalLogs){console.error("Failed to start session:",t)}if(e.name==="AbortError"||t.type==="timeout"){return{success:false,errorInfo:t,canceled:true}}return{success:false,errorInfo:t}}}async sendMessageWithRetry(e,n=null,r=null,o="1",i=null,a=0,c=false,u=false,l={}){try{const s=6e4;if(!n){n=t()}const f=await this.sendMessage(e,n,r,o,i,s,u);if(!f.success){if(f.invalidNonce&&!c&&l.onInvalidNonce){if(this.showTechnicalLogs){console.log("Invalid nonce detected, refreshing and retrying...")}await l.onInvalidNonce();return this.sendMessageWithRetry(e,n,r,o,i,a,true,u,l)}if(f.invalidNonce&&c&&f.errorInfo){if(l.onFinalError){l.onFinalError(new Error("Session expired"),f.errorInfo)}return{success:false,errorInfo:f.errorInfo}}const s=new Error(f.errorInfo?f.errorInfo.finalMessage:"Failed to get response");if(f.errorInfo){s.errorInfo=f.errorInfo;s.code=f.errorInfo.code}throw s}const h=f.data;if(h.new_token&&l.onNewToken){l.onNewToken(h)}if(h.chat_id&&l.onSessionUpdate){l.onSessionUpdate({chatId:h.chat_id,hasSession:true})}if(l.onSuccess){l.onSuccess(h)}return{success:true,data:h}}catch(t){const f=t.errorInfo||s(t,null,this.isAdmin);if(this.showTechnicalLogs){console.error("Chat error:",f)}if(this.showTechnicalLogs&&f.code){console.log(`Chat error code: ${f.code}, will retry: ${f.isRetryable}`)}const h=0;if(a<h&&f.isRetryable&&!c){const s=Math.pow(2,a)*1e3;if(l.onRetry){l.onRetry(a+1,s)}if(this.retryTimeoutRef){clearTimeout(this.retryTimeoutRef)}return new Promise((t=>{this.retryTimeoutRef=setTimeout((()=>{t(this.sendMessageWithRetry(e,n,r,o,i,a+1,false,u,l))}),s)}))}else{if(f.type==="timeout"&&!this.isAdmin&&l.onTimeoutError){l.onTimeoutError(t,f)}if(l.onFinalError){l.onFinalError(t,f)}return{success:false,errorInfo:f}}}}async sendMessage(t,n,r=null,a="1",c=null,u=3e4,l=false){const f={message:t,idempotency_key:n,widget_id:a,page_url:window.location.href,page_title:document.title||""};if(r){f.chat_id=r}if(c){f.collection_id=c}if(l){f.force_new_conversation=true}try{const t=await fetch(`${this.baseUrl}epkb-public/v1/ai-chat/send-message`,o("POST",f,u,this.restNonce));const n=await i(t);if(n.new_token){this.restNonce=n.new_token}if(!t.ok){const r=s(new Error(n.message||"Failed to get response"),{status:t.status,data:n},this.isAdmin);return{success:false,invalidNonce:e(t,n),errorInfo:r}}return{success:true,data:n}}catch(e){const t=s(e,null,this.isAdmin);return{success:false,errorInfo:t}}}async getConversation(t=null){try{const n=new URL(`${this.baseUrl}epkb-public/v1/ai-chat/conversation`);if(t){n.searchParams.append("chat_id",t)}const r=await fetch(n.toString(),o("GET",null,1e4,this.restNonce));const a=await i(r);if(a.new_token){this.restNonce=a.new_token}if(!r.ok){const t=s(new Error(a.message||"Failed to get active conversation"),{status:r.status,data:a},this.isAdmin);const n={success:false,errorInfo:t,action:"continue",userMessage:null,shouldRetry:false,needsNonceRefresh:false};if(e(r,a)||t.code==="invalid_nonce"){n.needsNonceRefresh=true;n.shouldRetry=true;n.action="refresh_nonce";return n}if(t.code==="invalid_session"||t.code==="session_expired"){n.action="create_new_session";n.userMessage=null;return n}if(t.code==="user_state_changed"||t.code==="user_mismatch"){n.action="clear_and_continue";n.userMessage=null;return n}if(t.code==="conversation_expired"||t.code==="invalid_chat_id"||t.code==="chat_not_found"||t.code==="not_found"||t.statusCode===404){n.action="expired";n.userMessage="Your previous conversation has expired. Starting a new conversation...";return n}if(t.type==="rate_limit"){const e=t.context&&t.context.retryAfter?t.context.retryAfter:60;n.action="show_error";n.userMessage=`Too many requests. Please try again in ${e} seconds.`;return n}if(t.statusCode>=500){n.action="show_error";n.userMessage="Service temporarily unavailable. Please try again later.";return n}if(t.type==="timeout"||t.type==="network"){n.action="continue";if(this.showTechnicalLogs){console.warn("Network issue when checking active conversation")}return n}return n}return{success:true,data:a,action:"success"}}catch(e){const t=s(e,null,this.isAdmin);if(t.type==="network"||t.type==="timeout"){return{success:false,errorInfo:t,action:"continue",userMessage:null}}return{success:false,errorInfo:t,action:"continue",userMessage:null}}}async clearConversation(){try{const t=await fetch(`${this.baseUrl}epkb-public/v1/ai-chat/clear`,o("POST",{},1e4,this.restNonce));const n=await i(t);if(n.new_token){this.restNonce=n.new_token}if(!t.ok){const r=s(new Error(n.message||"Failed to clear conversation"),{status:t.status,data:n},this.isAdmin);const o={success:false,errorInfo:r,action:"show_error",userMessage:"Failed to start new conversation. Please try again.",shouldRetry:false,needsNonceRefresh:false};if(e(t,n)||r.code==="invalid_nonce"){o.needsNonceRefresh=true;o.shouldRetry=true;o.action="refresh_nonce";return o}if(r.code==="invalid_session"||r.code==="session_expired"){o.action="create_new_session";o.userMessage=null;return o}if(r.statusCode>=500){o.userMessage="Service temporarily unavailable. Please try again later.";return o}return o}return{success:true,data:n,action:"success"}}catch(e){const t=s(e,null,this.isAdmin);return{success:false,errorInfo:t,action:"show_error",userMessage:"Failed to start new conversation. Please try again."}}}async submitHandoff(e){try{const t=await fetch(`${this.baseUrl}epkb-public/v1/ai-chat/handoff`,o("POST",e,2e4,this.restNonce));const n=await i(t);if(n.new_token){this.restNonce=n.new_token}if(!t.ok){const e=s(new Error(n.message||"Failed to submit handoff"),{status:t.status,data:n},this.isAdmin);return{success:false,errorInfo:e}}return{success:true,data:n}}catch(e){const t=e.type?e:s(e,null,this.isAdmin);return{success:false,errorInfo:t}}}async submitHandoffClick(e){try{const t=await fetch(`${this.baseUrl}epkb-public/v1/ai-chat/handoff-click`,o("POST",e,1e4,this.restNonce));const n=await i(t);if(n.new_token){this.restNonce=n.new_token}if(!t.ok){const e=s(new Error(n.message||"Failed to record handoff click"),{status:t.status,data:n},this.isAdmin);return{success:false,errorInfo:e}}return{success:true,data:n}}catch(e){const t=e.type?e:s(e,null,this.isAdmin);return{success:false,errorInfo:t}}}async submitFeedback(e){try{const t=await fetch(`${this.baseUrl}epkb-public/v1/ai-chat/feedback`,o("POST",e,1e4,this.restNonce));const n=await i(t);if(n.new_token){this.restNonce=n.new_token}if(!t.ok){const e=s(new Error(n.message||"Failed to record feedback"),{status:t.status,data:n},this.isAdmin);return{success:false,errorInfo:e}}return{success:true,data:n}}catch(e){const t=e.type?e:s(e,null,this.isAdmin);return{success:false,errorInfo:t}}}clearRetries(){if(this.retryTimeoutRef){clearTimeout(this.retryTimeoutRef);this.retryTimeoutRef=null}}}window.EPKBChatAPI={ChatAPIClient:a}})();
Save
Cancel