/** * MxChat Test Panel JavaScript * Handles the testing interface for admins - always active when panel is open */ class MxChatTestPanel { constructor() { this.panel = null; this.tab = null; this.isOpen = false; this.lastQueryData = null; this.init(); } init() { // Wait for DOM to be ready if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', () => this.setup()); } else { this.setup(); } } setup() { this.createElements(); this.bindEvents(); this.setupChatInterception(); } createElements() { // Create the test tab this.tab = document.createElement('div'); this.tab.className = 'mxchat-test-tab'; this.tab.innerHTML = 'MXCHAT DEBUGGING'; this.tab.title = 'Open MxChat Debug Panel'; document.body.appendChild(this.tab); // Create the test panel this.panel = document.createElement('div'); this.panel.className = 'mxchat-test-panel'; this.panel.innerHTML = this.getPanelHTML(); document.body.appendChild(this.panel); } getPanelHTML() { return `
Always-on debugging for administrators
${data.data.threshold_percentage}`;
} else {
thresholdEl.innerHTML = 'Error loading threshold';
}
})
.catch(error => {
console.error('Error fetching similarity threshold:', error);
const thresholdEl = this.panel.querySelector('#similarity-threshold');
thresholdEl.innerHTML = 'Connection error';
});
}
updateSystemPrompt() {
const promptEl = this.panel.querySelector('#system-prompt');
promptEl.textContent = 'Loading system prompt...';
fetch(mxchatTestData.ajaxUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
action: 'mxchat_get_system_info',
nonce: mxchatTestData.nonce
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
promptEl.textContent = data.data.system_prompt || 'No system prompt configured';
// Enhanced model display with OpenRouter support
if (data.data.is_openrouter) {
this.log(`๐ค Model: OpenRouter`);
this.log(` โโ Using: ${data.data.openrouter_model}`);
} else {
this.log(`๐ค Model: ${data.data.selected_model}`);
}
// Enhanced API status with OpenRouter
const apiStatus = data.data.api_status;
const configuredApis = Object.keys(apiStatus).filter(key => apiStatus[key]);
if (configuredApis.length > 0) {
this.log(`๐ Configured APIs: ${configuredApis.map(api => {
// Capitalize and format API names
if (api === 'openai') return 'OpenAI';
if (api === 'xai') return 'X.AI';
if (api === 'openrouter') return 'OpenRouter';
return api.charAt(0).toUpperCase() + api.slice(1);
}).join(', ')}`);
} else {
this.log(`โ ๏ธ No API keys configured`);
}
// Specific warning for OpenRouter if selected but no key
if (data.data.is_openrouter && !apiStatus.openrouter) {
this.log(`โ WARNING: OpenRouter selected but no API key configured!`);
}
} else {
promptEl.textContent = 'Error loading system prompt';
}
})
.catch(error => {
console.error('Error fetching system info:', error);
promptEl.textContent = 'Connection error';
});
}
updateKnowledgeBaseStatus() {
const statusEl = this.panel.querySelector('#kb-status');
statusEl.innerHTML = 'Checking...';
fetch(mxchatTestData.ajaxUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
action: 'mxchat_get_kb_status',
nonce: mxchatTestData.nonce
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
const kbData = data.data;
statusEl.innerHTML = `โ ${kbData.status} (${kbData.type} - ${kbData.documents})`;
} else {
statusEl.innerHTML = 'Error loading KB status';
}
})
.catch(error => {
console.error('Error fetching KB status:', error);
statusEl.innerHTML = 'Connection error';
});
}
clearDebugConsole() {
const console = this.panel.querySelector('#debug-console');
console.innerHTML = '