const hamburger = document.querySelector('.hamburger') const navMenu = document.querySelector('.sidebar') const navLink = document.querySelectorAll('.nav-item') hamburger.addEventListener('click', mobileMenu); navLink.forEach(n => n.addEventListener('click', closeMenu)); function mobileMenu() { hamburger.classList.toggle('active'); navMenu.classList.toggle('active'); } function closeMenu() { hamburger.classList.remove('active'); navMenu.classList.remove('active'); // hamburger.style.position="fixed"; } var SSE = function (url, options) { if (!(this instanceof SSE)) { return new SSE(url, options); } this.INITIALIZING = -1; this.CONNECTING = 0; this.OPEN = 1; this.CLOSED = 2; this.url = url; options = options || {}; this.headers = options.headers || {}; this.payload = options.payload !== undefined ? options.payload : ''; this.method = options.method || (this.payload && 'POST' || 'GET'); this.withCredentials = !!options.withCredentials; this.FIELD_SEPARATOR = ':'; this.listeners = {}; this.xhr = null; this.readyState = this.INITIALIZING; this.progress = 0; this.chunk = ''; this.addEventListener = function(type, listener) { if (this.listeners[type] === undefined) { this.listeners[type] = []; } if (this.listeners[type].indexOf(listener) === -1) { this.listeners[type].push(listener); } }; this.removeEventListener = function(type, listener) { if (this.listeners[type] === undefined) { return; } var filtered = []; this.listeners[type].forEach(function(element) { if (element !== listener) { filtered.push(element); } }); if (filtered.length === 0) { delete this.listeners[type]; } else { this.listeners[type] = filtered; } }; this.dispatchEvent = function(e) { if (!e) { return true; } e.source = this; var onHandler = 'on' + e.type; if (this.hasOwnProperty(onHandler)) { this[onHandler].call(this, e); if (e.defaultPrevented) { return false; } } if (this.listeners[e.type]) { return this.listeners[e.type].every(function(callback) { callback(e); return !e.defaultPrevented; }); } return true; }; this._setReadyState = function(state) { var event = new CustomEvent('readystatechange'); event.readyState = state; this.readyState = state; this.dispatchEvent(event); }; this._onStreamFailure = function(e) { var event = new CustomEvent('error'); event.data = e.currentTarget.response; this.dispatchEvent(event); this.close(); } this._onStreamAbort = function(e) { this.dispatchEvent(new CustomEvent('abort')); this.close(); } this._onStreamProgress = function(e) { if (!this.xhr) { return; } if (this.xhr.status !== 200) { this._onStreamFailure(e); return; } if(this.xhr.status === 201) { this._onStreamProgress((e)); } if (this.readyState == this.CONNECTING) { this.dispatchEvent(new CustomEvent('open')); this._setReadyState(this.OPEN); } var data = this.xhr.responseText.substring(this.progress); this.progress += data.length; data.split(/(\r\n|\r|\n){2}/g).forEach(function(part) { if (part.trim().length === 0) { this.dispatchEvent(this._parseEventChunk(this.chunk.trim())); this.chunk = ''; } else { this.chunk += part; } }.bind(this)); }; this._onStreamLoaded = function(e) { this._onStreamProgress(e); // Parse the last chunk. this.dispatchEvent(this._parseEventChunk(this.chunk)); this.chunk = ''; }; /** * Parse a received SSE event chunk into a constructed event object. */ this._parseEventChunk = function(chunk) { if (!chunk || chunk.length === 0) { return null; } var e = {'id': null, 'retry': null, 'data': '', 'event': 'message'}; chunk.split(/\n|\r\n|\r/).forEach(function(line) { line = line.trimRight(); var index = line.indexOf(this.FIELD_SEPARATOR); if (index <= 0) { // Line was either empty, or started with a separator and is a comment. // Either way, ignore. return; } var field = line.substring(0, index); if (!(field in e)) { return; } var value = line.substring(index + 1).trimLeft(); if (field === 'data') { e[field] += value; } else { e[field] = value; } }.bind(this)); var event = new CustomEvent(e.event); event.data = e.data; event.id = e.id; return event; }; this._checkStreamClosed = function() { if (!this.xhr) { return; } if (this.xhr.readyState === XMLHttpRequest.DONE) { this._setReadyState(this.CLOSED); } }; this.stream = function() { this._setReadyState(this.CONNECTING); this.xhr = new XMLHttpRequest(); this.xhr.addEventListener('progress', this._onStreamProgress.bind(this)); this.xhr.addEventListener('load', this._onStreamLoaded.bind(this)); this.xhr.addEventListener('readystatechange', this._checkStreamClosed.bind(this)); this.xhr.addEventListener('error', this._onStreamFailure.bind(this)); this.xhr.addEventListener('abort', this._onStreamAbort.bind(this)); this.xhr.open(this.method, this.url); for (var header in this.headers) { this.xhr.setRequestHeader(header, this.headers[header]); } this.xhr.withCredentials = this.withCredentials; this.xhr.send(this.payload); }; this.close = function() { if (this.readyState === this.CLOSED) { return; } this.xhr.abort(); this.xhr = null; this._setReadyState(this.CLOSED); }; }; // Export our SSE module for npm.js if (typeof exports !== 'undefined') { exports.SSE = SSE; } var texttotype = ""; let cti = 0; let fistload = 1; var formData = { message: "", au: '3.142.43.206', chatid: '3470', guru: '' }; function generateRandomNumber() { const min = 1; const max = 100000; return Math.floor(Math.random() * (max - min + 1)) + min; } function handleEnterKey(event) { if (event.keyCode === 13 && !event.shiftKey) { // code to be executed when the enter key is pressed chatwithnando(); } } var inputField = document.querySelector('#chatfield.textarea'); inputField.addEventListener("keydown", handleEnterKey); function isJSON(str) { try { JSON.parse(str); } catch (e) { return false; } return true; } function chatwithnando() { let ctextarea = document.querySelector('#chatfield.textarea'); if(fistload && ctextarea.innerText.length > 1) { console.log("check " + ctextarea.innerText); let story = document.getElementById('createchat'); let respContent = document.getElementById('startchat'); story.style.display = "none"; respContent.style.display = "block"; formData.message = ctextarea.innerText; //document.querySelector('.regenerate').style.display = "block"; respContent.innerHTML += '

' + ctextarea.innerText.replace(/\n/g, '
'); + '

'; ctextarea.innerText = ''; let cmes = generateRandomNumber(); var source = new SSE("https://api.nando.ai/ai_chat_free.php", {payload: JSON.stringify(formData)}); respContent.innerHTML += '
Nando is typing...
'; document.getElementById('startchat').scrollTop = document.getElementById('startchat').scrollHeight; source.addEventListener('message', function (e) { if(e.data){ //console.log('data ' . e.data); if(e.data != '[DONE]'){ if(isJSON(e.data)) { var tokens = JSON.parse(e.data).choices[0].delta.content if(tokens != undefined) { console.log(tokens); if(fistload == 1) { fistload = 0; cti = 0; if (tokens.includes("hit the free limit")) { document.getElementById('chatmes' + cmes).innerHTML = "" + tokens + ""; respContent.scrollTop = respContent.scrollHeight; } else { texttotype = tokens document.getElementById('chatmes' + cmes).innerText = ""; typeWriter(cmes); ctextarea.innerText = ''; //document.getElementById('chatmes' + cmes).innerText = tokens; //respContent.scrollTop = respContent.scrollHeight; } } else { texttotype += tokens; //document.getElementById('chatmes' + cmes).innerText += tokens; //respContent.scrollTop = respContent.scrollHeight; } //textarea.innerHTML += tokens } } }else{ fistload = 1; console.log('Completed'); } } }) source.stream() } } function improveprompt() { let ctextarea = document.querySelector('#chatfield.textarea'); if(ctextarea.innerText != "Nando is improving your prompt...") { var formData = { optimizeprompt: 1, prompt: ctextarea.innerText }; ctextarea.innerText = "Nando is improving your prompt..."; $.ajax({ type: "POST", url: "https://api.nando.ai/ai_chat_free.php", data: formData, dataType: "json", encode: true, }).done(function (data) { ctextarea.innerText = data["prompt"]; console.log("loaded " + data["prompt"]); }); } } function typeWriter(cmes) { if (cti < texttotype.length || fistload == 0) { if(texttotype.charAt(cti-1) == ' ' && texttotype.charAt(cti) && cti > 1) { document.getElementById('chatmes' + cmes).innerText += ' ' + texttotype.charAt(cti); const responseDiv = document.querySelector('.response-div'); const scrollTopPosition = responseDiv.scrollTop; const scrollHeight = responseDiv.scrollHeight; const clientHeight = responseDiv.clientHeight; const scrollPercentage = (scrollTopPosition + clientHeight) / scrollHeight; if (scrollPercentage >= 0.96) { responseDiv.scrollTop = scrollHeight; } cti++; } else if(texttotype.charAt(cti)) { document.getElementById('chatmes' + cmes).innerText += texttotype.charAt(cti); const responseDiv = document.getElementById('startchat'); const scrollTopPosition = responseDiv.scrollTop; const scrollHeight = responseDiv.scrollHeight; const clientHeight = responseDiv.clientHeight; const scrollPercentage = (scrollTopPosition + clientHeight) / scrollHeight; if (scrollPercentage >= 0.96) { responseDiv.scrollTop = scrollHeight; } cti++; } if(fistload == 1) { setTimeout(typeWriter, 1, cmes); } else { setTimeout(typeWriter, 10, cmes); } } else{ //o.querySelector('[data-kt-element="message-text"]').innerHTML = updatediv(o); } } function copytoclipboard(id) { var text = document.getElementById("chatmes" + id).innerText; var elem = document.createElement("textarea"); document.body.appendChild(elem); elem.value = text; elem.select(); document.execCommand("copy"); document.body.removeChild(elem); } document.getElementById('startchat').scrollTop = document.getElementById('startchat').scrollHeight;