From 50fc7a9146d6dc593c61a5d2dfcc0909146d5fb9 Mon Sep 17 00:00:00 2001 From: Zaki-1052 Date: Sat, 29 Jun 2024 04:33:35 -0700 Subject: [PATCH] fixing code format --- public/script.js | 69 ++++++++++++++++++++++++++---------------------- server.js | 14 ++++++---- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/public/script.js b/public/script.js index df04f9b..8100eaf 100644 --- a/public/script.js +++ b/public/script.js @@ -1316,7 +1316,7 @@ document.getElementById('open-router-model-cohere-command-r-plus').addEventListe const summaryData = await summaryResponse.json(); if (summaryData.summary) { - displayMessage(summaryData.summary, 'response'); + displayMessage(summaryData.summary, 'response', false); } else { console.error('Summary not found.'); } @@ -1875,31 +1875,43 @@ function displayMessage(message, type) { messageElement.classList.add('message', type); if (type === 'image') { - const imageElement = document.createElement('img'); - imageElement.src = message; - imageElement.alt = "Generated Image"; - imageElement.classList.add('generated-image'); // A class for styling images - - messageElement.appendChild(imageElement); + const imageElement = document.createElement('img'); + imageElement.src = message; + imageElement.alt = "Generated Image"; + imageElement.classList.add('generated-image'); + messageElement.appendChild(imageElement); } else { // Check if message contains a code block - if (message.includes('```')) { - // Improved regex pattern to correctly identify and split code blocks - const parts = message.split(/(```[\s\S]+?```)/); - parts.forEach(part => { - if (part.startsWith('```') && part.endsWith('```')) { - // Handle code blocks - const codeContent = part.substring(3, part.length - 3); - const pre = document.createElement('pre'); - const codeElement = document.createElement('code'); - codeElement.innerText = codeContent; // Use innerText to display raw code content - pre.appendChild(codeElement); - messageElement.appendChild(pre); - // Add a "Copy Code" button for this code block - const copyCodeButton = document.createElement('button'); - copyCodeButton.textContent = 'Copy Code'; - copyCodeButton.onclick = function() { copyToClipboard(codeContent); }; - pre.appendChild(copyCodeButton); + if (message.includes('\`\`\`')) { + // Improved regex pattern to correctly identify and split code blocks + const parts = message.split(/(\`\`\`[\s\S]+?\`\`\`)/); + parts.forEach(part => { + if (part.startsWith('\`\`\`') && part.endsWith('\`\`\`')) { + // Handle code blocks + const codeContent = part.substring(3, part.length - 3); + const languageMatch = codeContent.match(/^[^\n]+/); + const language = languageMatch ? languageMatch[0].trim() : ''; + const actualCode = codeContent.replace(/^[^\n]+/, '').trim(); + + const pre = document.createElement('pre'); + const codeElement = document.createElement('code'); + /* + if (language) { + codeElement.classList.add(`language-${language}`); + } + */ + codeElement.textContent = actualCode; + pre.appendChild(codeElement); + messageElement.appendChild(pre); + + // Add a "Copy Code" button on a new line after the code block + const copyCodeButtonWrapper = document.createElement('div'); + copyCodeButtonWrapper.style.marginTop = '10px'; // Add some space above the button + const copyCodeButton = document.createElement('button'); + copyCodeButton.textContent = 'Copy Code'; + copyCodeButton.onclick = function() { copyToClipboard(actualCode); }; + copyCodeButtonWrapper.appendChild(copyCodeButton); + messageElement.appendChild(copyCodeButtonWrapper); } else { // This is regular text, render as markdown const textSpan = document.createElement('span'); @@ -1907,13 +1919,8 @@ function displayMessage(message, type) { const safeHtml = DOMPurify.sanitize(rawHtml); textSpan.innerHTML = safeHtml; messageElement.appendChild(textSpan); - } }); - const copyButton = document.createElement('button'); - copyButton.textContent = 'Copy'; - copyButton.onclick = function() { copyToClipboard(messageElement.innerText); }; - messageElement.appendChild(copyButton); } else { const messageText = document.createElement('span'); // Convert markdown to HTML using marked.js and sanitize it with DOMPurify @@ -1935,10 +1942,9 @@ function displayMessage(message, type) { chatBox.scrollTop = chatBox.scrollHeight; // Auto-scroll to the latest message if (type === 'response' && isVoiceTranscription) { - callTTSAPI(message); // Read out the response message only if it should be read aloud + callTTSAPI(message); // Read out the response message only if it should be read aloud } } - // copy button feature @@ -2114,3 +2120,4 @@ function saveEnvChanges() { alert('An error occurred during setup. Please try again.'); }); } + diff --git a/server.js b/server.js index 5d48373..96660ea 100644 --- a/server.js +++ b/server.js @@ -350,6 +350,7 @@ async function initializeConversationHistory() { systemMessage = `You are a helpful and intelligent AI assistant, knowledgeable about a wide range of topics and highly capable of a great many tasks.\n Specifically:\n ${fileInstructions}`; if (continueConv) { if (summariesOnly) { + console.log("summaries only", summariesOnly); const contextAndSummary = await continueConversation(chosenChat); systemMessage += `\n---\n${contextAndSummary}`; } else { @@ -414,8 +415,7 @@ async function continueConversation(chosenChat) { // Read the chosen chat file const conversationFile = await fs.promises.readFile(path.join(__dirname, 'public/uploads/chats', `${chosenChat}.txt`), 'utf8'); if (summariesOnly) { - return conversationFile - } else { + console.log("summaries only regex", summariesOnly); // Regex to extract everything starting from CONTEXT const regex = /\n\n-----\n\n(.+)/s; const match = conversationFile.match(regex); @@ -425,9 +425,12 @@ async function continueConversation(chosenChat) { } else { throw new Error('Context and summary not found in the conversation file.'); } - } - - } catch (error) { + } else { + console.log("summaries only", summariesOnly); + return conversationFile + + } + } catch (error) { console.error('Error in continueConversation:', error); throw error; } @@ -492,6 +495,7 @@ app.get('/getSummary/:chatName', async (req, res) => { app.post('/setSummariesOnly', (req, res) => { try { summariesOnly = req.body.summariesOnly; + console.log(summariesOnly); res.status(200).json({ message: 'Summaries only setting updated successfully', summariesOnly }); } catch (error) { console.error('Error in /setSummariesOnly endpoint:', error);