Skip to content

Commit

Permalink
fixing code format
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaki-1052 committed Jun 29, 2024
1 parent 6d0d5d7 commit 50fc7a9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
69 changes: 38 additions & 31 deletions public/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down Expand Up @@ -1875,45 +1875,52 @@ 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');
const rawHtml = marked.parse(part);
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
Expand All @@ -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

Expand Down Expand Up @@ -2114,3 +2120,4 @@ function saveEnvChanges() {
alert('An error occurred during setup. Please try again.');
});
}

14 changes: 9 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 50fc7a9

Please sign in to comment.