const query = (obj) => Object.keys(obj) .map((k) => encodeURIComponent(k) + "=" + encodeURIComponent(obj[k])) .join("&"); const url_prefix = document.querySelector('body').getAttribute('data-urlprefix') const markdown = window.markdownit(); const message_box = document.getElementById(`messages`); const message_input = document.getElementById(`message-input`); const box_conversations = document.querySelector(`.top`); const spinner = box_conversations.querySelector(".spinner"); const stop_generating = document.querySelector(`.stop-generating`); const send_button = document.querySelector(`#send-button`); const user_image = ``; const gpt_image = ``; let prompt_lock = false; hljs.addPlugin(new CopyButtonPlugin()); message_input.addEventListener("blur", () => { window.scrollTo(0, 0); }); message_input.addEventListener("focus", () => { document.documentElement.scrollTop = document.documentElement.scrollHeight; }); const delete_conversations = async () => { localStorage.clear(); await new_conversation(); }; const handle_ask = async () => { message_input.style.height = `80px`; window.scrollTo(0, 0); let message = message_input.value; if (message.length > 0) { message_input.value = ``; message_input.dispatchEvent(new Event("input")); await ask_gpt(message); } }; const remove_cancel_button = async () => { stop_generating.classList.add(`stop-generating-hiding`); setTimeout(() => { stop_generating.classList.remove(`stop-generating-hiding`); stop_generating.classList.add(`stop-generating-hidden`); }, 300); }; const ask_gpt = async (message) => { try { message_input.value = ``; message_input.innerHTML = ``; message_input.innerText = ``; add_conversation(window.conversation_id, message.substr(0, 20)); window.scrollTo(0, 0); window.controller = new AbortController(); jailbreak = document.getElementById("jailbreak"); model = document.getElementById("model"); prompt_lock = true; window.text = ``; window.token = message_id(); stop_generating.classList.remove(`stop-generating-hidden`); add_user_message_box(message); message_box.scrollTop = message_box.scrollHeight; window.scrollTo(0, 0); await new Promise((r) => setTimeout(r, 500)); window.scrollTo(0, 0); message_box.innerHTML += `
`; message_box.scrollTop = message_box.scrollHeight; window.scrollTo(0, 0); await new Promise((r) => setTimeout(r, 1000)); window.scrollTo(0, 0); const response = await fetch(`${url_prefix}/backend-api/v2/conversation`, { method: `POST`, signal: window.controller.signal, headers: { "content-type": `application/json`, accept: `text/event-stream`, }, body: JSON.stringify({ conversation_id: window.conversation_id, action: `_ask`, model: model.options[model.selectedIndex].value, jailbreak: jailbreak.options[jailbreak.selectedIndex].value, meta: { id: window.token, content: { conversation: await get_conversation(window.conversation_id), internet_access: document.getElementById("switch").checked, content_type: "text", parts: [ { content: message, role: "user", }, ], }, }, }), }); const reader = response.body.getReader(); while (true) { const { value, done } = await reader.read(); if (done) break; chunk = decodeUnicode(new TextDecoder().decode(value)); if (chunk.includes(`