Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add copy buttons to chat history window #426

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,23 @@ textarea {
}

/* LLM stuff */
#chatLLM_chat_history {
#chatLLM_chat_history_wrapper {
position: relative;
height: 100%;
min-height: 400px;
max-height: 450px;
overflow-y: auto;
padding: 0.5rem;
background-color: #fff;
border: 1px solid #dee2e6;
}
#chatLLM_chat_history {
overflow-y: auto;
height: 100%;
min-height: 400px;
max-height: 450px;
padding: 0.5rem;
padding-bottom: 4rem;
border-radius: 0.25rem;
border: 1px solid #dee2e6;
display: flex;
flex-direction: column;
align-items: column;
Expand All @@ -308,7 +316,8 @@ textarea {
align-self: flex-start;
}

.LLM_suggestions > p > button {
.LLM_suggestions > p > button,
#chatLLM_copy_all {
font-weight: normal;
border-radius: 0.5rem;
border: none;
Expand All @@ -317,3 +326,14 @@ textarea {
.LLM_suggestions > p {
display: inline-block;
}

/* align this to the lower right of the chat history */
#chatLLM_copy_all_wrapper {
position: absolute;
bottom: -0.5rem;
right: 1.5rem;
margin: 0.5rem;
}
.chatLLM_message_copy_button {
font-weight: normal;
}
54 changes: 50 additions & 4 deletions src/js/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ class Resources {
openai: 'OpenAI Vision',
gemini: 'Gemini Pro Vision',
multi: 'Multiple AI',
processing: 'Processing Chart...',
},
},
};
Expand Down Expand Up @@ -895,8 +896,11 @@ class ChatLLM {
</button>
</div>
<div class="modal-body">
<div id="chatLLM_chat_history_wrapper">
<div id="chatLLM_chat_history" aria-live="${constants.ariaMode}" aria-relevant="additions">
</div>
<p id="chatLLM_copy_all_wrapper"><button id="chatLLM_copy_all">Copy all to clipboard</button></p>
</div>
<div id="chatLLM_content">
<p><input type="text" id="chatLLM_input" class="form-control" name="chatLLM_input" aria-labelledby="chatLLM_title" size="50"></p>
<div class="LLM_suggestions">
Expand Down Expand Up @@ -1042,6 +1046,38 @@ class ChatLLM {
chatLLM.ResetChatHistory();
},
]);

// copy to clipboard
constants.events.push([
document.getElementById('chatLLM_copy_all'),
'click',
function (e) {
let text = document.getElementById('chatLLM_chat_history').innerText;
// need newlines instead of paragraphs headings etc
text = text.replace(/<p>/g, '\n').replace(/<\/p>/g, '\n');
text = text.replace(/<h\d>/g, '\n').replace(/<\/h\d>/g, '\n');
text = text.replace(/<.*?>/g, '');

navigator.clipboard.writeText(text);
},
]);
constants.events.push([
document.getElementById('chatLLM_chat_history'),
'click',
function (e) {
// we're delegating here, so set the event on child .chatLLM_message_copy_button
if (e.target.matches('.chatLLM_message_copy_button')) {
// get the innerText of the element before the button
let text = e.target.closest('p').previousElementSibling.innerText;
// need newlines instead of paragraphs headings etc
text = text.replace(/<p>/g, '\n').replace(/<\/p>/g, '\n');
text = text.replace(/<h\d>/g, '\n').replace(/<\/h\d>/g, '\n');
text = text.replace(/<.*?>/g, '');

navigator.clipboard.writeText(text);
}
},
]);
}

/**
Expand Down Expand Up @@ -1122,7 +1158,7 @@ class ChatLLM {
*/
ProcessLLMResponse(data, model) {
chatLLM.WaitingSound(false);
console.log('LLM response: ', data);
//console.log('LLM response: ', data);
let text = '';
let LLMName = resources.GetString(model);

Expand Down Expand Up @@ -1326,9 +1362,9 @@ class ChatLLM {
};

// Generate the content
console.log('LLM request: ', prompt, image);
//console.log('LLM request: ', prompt, image);
const result = await model.generateContent([prompt, image]);
console.log(result.response.text());
//console.log(result.response.text());

// Process the response
chatLLM.ProcessLLMResponse(result.response, 'gemini');
Expand Down Expand Up @@ -1368,6 +1404,12 @@ class ChatLLM {
<p class="chatLLM_message_text">${text}</p>
</div>
`;
// add a copy button to actual messages
if (user != 'User' && text != resources.GetString('processing')) {
html += `
<p class="chatLLM_message_copy"><button class="chatLLM_message_copy_button">Copy</button></p>
`;
}

this.RenderChatMessage(html);
}
Expand Down Expand Up @@ -1445,7 +1487,11 @@ class ChatLLM {
// get name from resource
let LLMName = resources.GetString(constants.LLMModel);
this.firstTime = false;
this.DisplayChatMessage(LLMName, 'Processing Chart...', true);
this.DisplayChatMessage(
LLMName,
resources.GetString('processing'),
true
);
let defaultPrompt = this.GetDefaultPrompt();
this.Submit(defaultPrompt, true);
}
Expand Down
Loading