Admin/Chatbot Admin

From Bodhicitta
< Admin
Revision as of 19:47, 6 October 2025 by Jeremi (talk | contribs) ((by SublimeText.Mediawiker))

Chatbot Knowledge Base Editor

           <button onclick="ChatbotAdmin.loadKB()" style="padding:8px 16px; background:#6c757d; color:white; border:none; border-radius:4px; cursor:pointer;">Load Current KB</button>
           <button onclick="ChatbotAdmin.saveKB()" style="padding:8px 16px; background:#28a745; color:white; border:none; border-radius:4px; cursor:pointer;">Save Changes</button>

Edit Knowledge Base JSON

       <textarea id="kb-json-editor" style="width:100%; height:500px; font-family:monospace; font-size:12px; padding:10px; border:1px solid #ddd;"></textarea>

Quick Add Response

               <label style="display:block; margin-bottom:5px;">Patterns (comma-separated):</label>
               <input type="text" id="quick-patterns" style="width:100%; padding:5px;" placeholder="login, sign in, account">
               <label style="display:block; margin-bottom:5px;">Response:</label>
               <textarea id="quick-response" style="width:100%; padding:5px; height:60px;" placeholder="Enter the response text..."></textarea>
               <label style="display:block; margin-bottom:5px;">Category:</label>
               <select id="quick-category" style="width:100%; padding:5px;">
                   <option value="access">Access & Permissions</option>
                   <option value="navigation">Navigation</option>
                   <option value="search">Search</option>
                   <option value="general">General</option>
               </select>
           <button onclick="ChatbotAdmin.quickAdd()" style="padding:8px 16px; background:#8B4513; color:white; border:none; border-radius:4px; cursor:pointer;">Add Response</button>

<script> var ChatbotAdmin = {

   currentData: null,
   
   loadKB: function() {
       var self = this;
       var apiUrl = mw.config.get('wgScriptPath') + '/api.php';
       
       // Show loading message
       document.getElementById('kb-status').innerHTML = 'Loading knowledge base...';
       
       // Try to load the current KB
       $.ajax({
           url: apiUrl,
           data: {
               action: 'query',
               prop: 'revisions',
               titles: 'MediaWiki:Chatbot-KnowledgeBase.json',
               rvprop: 'content',
               format: 'json'
           },
           success: function(data) {
               var pages = data.query.pages;
               var content = null;
               
               for (var pageId in pages) {
                   if (pages[pageId].revisions) {
                       content = pages[pageId].revisions[0]['*'];
                       break;
                   }
               }
               
               if (content) {
                   try {
                       self.currentData = JSON.parse(content);
                       document.getElementById('kb-json-editor').value = JSON.stringify(self.currentData, null, 2);
                       document.getElementById('kb-status').innerHTML = '✓ Knowledge base loaded successfully';
                   } catch(e) {
                       document.getElementById('kb-status').innerHTML = 'Error parsing knowledge base: ' + e.message + '';
                   }
               } else {
                   // Create default structure
                   self.currentData = {
                       version: "1.0",
                       lastUpdated: new Date().toISOString().split('T')[0],
                       categories: {
                           access: {
                               name: "Access & Permissions",
                               enabled: true,
                               items: []
                           },
                           navigation: {
                               name: "Navigation",
                               enabled: true,
                               items: []
                           },
                           search: {
                               name: "Search",
                               enabled: true,
                               items: []
                           },
                           general: {
                               name: "General",
                               enabled: true,
                               items: []
                           }
                       }
                   };
                   document.getElementById('kb-json-editor').value = JSON.stringify(self.currentData, null, 2);
                   document.getElementById('kb-status').innerHTML = 'Knowledge base not found. Created new structure.';
               }
           },
           error: function(xhr, status, error) {
               document.getElementById('kb-status').innerHTML = 'Error loading: ' + error + '';
           }
       });
   },
   
   saveKB: function() {
       var self = this;
       var apiUrl = mw.config.get('wgScriptPath') + '/api.php';
       
       // Parse the JSON from the editor
       try {
           this.currentData = JSON.parse(document.getElementById('kb-json-editor').value);
       } catch(e) {
           alert('Invalid JSON: ' + e.message);
           return;
       }
       
       // Update metadata
       this.currentData.lastUpdated = new Date().toISOString().split('T')[0];
       
       document.getElementById('kb-status').innerHTML = 'Saving...';
       
       // Get edit token
       $.ajax({
           url: apiUrl,
           data: {
               action: 'query',
               meta: 'tokens',
               type: 'csrf',
               format: 'json'
           },
           success: function(data) {
               var token = data.query.tokens.csrftoken;
               
               // Save the page
               $.ajax({
                   url: apiUrl,
                   type: 'POST',
                   data: {
                       action: 'edit',
                       title: 'MediaWiki:Chatbot-KnowledgeBase.json',
                       text: JSON.stringify(self.currentData, null, 2),
                       summary: 'Updated chatbot knowledge base',
                       token: token,
                       format: 'json'
                   },
                   success: function() {
                       document.getElementById('kb-status').innerHTML = '✓ Saved successfully!';
                       // Clear cache
                       try {
                           localStorage.removeItem('tsadra-chatbot-kb');
                       } catch(e) {}
                       // Update editor
                       document.getElementById('kb-json-editor').value = JSON.stringify(self.currentData, null, 2);
                   },
                   error: function(xhr, status, error) {
                       document.getElementById('kb-status').innerHTML = 'Error saving: ' + error + '';
                   }
               });
           }
       });
   },
   
   quickAdd: function() {
       var patterns = document.getElementById('quick-patterns').value;
       var response = document.getElementById('quick-response').value;
       var category = document.getElementById('quick-category').value;
       
       if (!patterns || !response) {
           alert('Please enter patterns and response');
           return;
       }
       
       // Parse current data
       try {
           this.currentData = JSON.parse(document.getElementById('kb-json-editor').value);
       } catch(e) {
           alert('Please load the knowledge base first');
           return;
       }
       
       // Create new entry
       var newEntry = {
           patterns: patterns.split(',').map(function(p) { return p.trim().toLowerCase(); }),
           response: response,
           priority: 5
       };
       
       // Add to category
       if (!this.currentData.categories[category]) {
           this.currentData.categories[category] = {
               name: category,
               enabled: true,
               items: []
           };
       }
       
       this.currentData.categories[category].items.push(newEntry);
       
       // Update editor
       document.getElementById('kb-json-editor').value = JSON.stringify(this.currentData, null, 2);
       
       // Clear form
       document.getElementById('quick-patterns').value = ;
       document.getElementById('quick-response').value = ;
       
       document.getElementById('kb-status').innerHTML = 'Entry added. Don\'t forget to save!';
   }

};

// Load on page ready $(document).ready(function() {

   ChatbotAdmin.loadKB();

}); </script>