Skip to content

ChatView: make "stop" and "copy conversation" work again #3336

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

Merged
merged 4 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
91 changes: 44 additions & 47 deletions gpt4all-chat/qml/ChatView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ Rectangle {

Connections {
target: currentChat
function onResponseInProgressChanged() {
if (MySettings.networkIsActive && !currentChat.responseInProgress)
Network.sendConversation(currentChat.id, getConversationJson());
}
// FIXME: https://github.com/nomic-ai/gpt4all/issues/3334
// function onResponseInProgressChanged() {
// if (MySettings.networkIsActive && !currentChat.responseInProgress)
// Network.sendConversation(currentChat.id, getConversationJson());
// }
function onModelLoadingErrorChanged() {
if (currentChat.modelLoadingError !== "")
modelLoadingErrorPopup.open()
Expand Down Expand Up @@ -116,42 +117,44 @@ Rectangle {
}
}

function getConversation() {
var conversation = "";
for (var i = 0; i < chatModel.count; i++) {
var item = chatModel.get(i)
var string = item.name;
var isResponse = item.name === "Response: "
string += chatModel.get(i).value
if (isResponse && item.stopped)
string += " <stopped>"
string += "\n"
conversation += string
}
return conversation
}

function getConversationJson() {
var str = "{\"conversation\": [";
for (var i = 0; i < chatModel.count; i++) {
var item = chatModel.get(i)
var isResponse = item.name === "Response: "
str += "{\"content\": ";
str += JSON.stringify(item.value)
str += ", \"role\": \"" + (isResponse ? "assistant" : "user") + "\"";
if (isResponse && item.thumbsUpState !== item.thumbsDownState)
str += ", \"rating\": \"" + (item.thumbsUpState ? "positive" : "negative") + "\"";
if (isResponse && item.newResponse !== "")
str += ", \"edited_content\": " + JSON.stringify(item.newResponse);
if (isResponse && item.stopped)
str += ", \"stopped\": \"true\""
if (!isResponse)
str += "},"
else
str += ((i < chatModel.count - 1) ? "}," : "}")
}
return str + "]}"
}
// FIXME: https://github.com/nomic-ai/gpt4all/issues/3334
// function getConversation() {
// var conversation = "";
// for (var i = 0; i < chatModel.count; i++) {
// var item = chatModel.get(i)
// var string = item.name;
// var isResponse = item.name === "Response: "
// string += chatModel.get(i).value
// if (isResponse && item.stopped)
// string += " <stopped>"
// string += "\n"
// conversation += string
// }
// return conversation
// }

// FIXME: https://github.com/nomic-ai/gpt4all/issues/3334
// function getConversationJson() {
// var str = "{\"conversation\": [";
// for (var i = 0; i < chatModel.count; i++) {
// var item = chatModel.get(i)
// var isResponse = item.name === "Response: "
// str += "{\"content\": ";
// str += JSON.stringify(item.value)
// str += ", \"role\": \"" + (isResponse ? "assistant" : "user") + "\"";
// if (isResponse && item.thumbsUpState !== item.thumbsDownState)
// str += ", \"rating\": \"" + (item.thumbsUpState ? "positive" : "negative") + "\"";
// if (isResponse && item.newResponse !== "")
// str += ", \"edited_content\": " + JSON.stringify(item.newResponse);
// if (isResponse && item.stopped)
// str += ", \"stopped\": \"true\""
// if (!isResponse)
// str += "},"
// else
// str += ((i < chatModel.count - 1) ? "}," : "}")
// }
// return str + "]}"
// }

ChatDrawer {
id: chatDrawer
Expand Down Expand Up @@ -932,10 +935,7 @@ Rectangle {
visible: false
}
onClicked: {
var conversation = getConversation()
copyEdit.text = conversation
copyEdit.selectAll()
copyEdit.copy()
chatModel.copyToClipboard()
copyMessage.open()
}
ToolTip.visible: copyChatButton.hovered
Expand Down Expand Up @@ -1354,9 +1354,6 @@ Rectangle {
ToolTip.text: Accessible.description

onClicked: {
var index = Math.max(0, chatModel.count - 1);
var listElement = chatModel.get(index);
listElement.stopped = true
currentChat.stopGenerating()
}
}
Expand Down
14 changes: 14 additions & 0 deletions gpt4all-chat/src/chatmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,20 @@ class ChatModel : public QAbstractListModel
emit hasErrorChanged(value);
}

Q_INVOKABLE void copyToClipboard()
{
QMutexLocker locker(&m_mutex);
QString conversation;
for (ChatItem *item : m_chatItems) {
QString string = item->name;
string += item->clipboardContent();
string += "\n";
conversation += string;
}
QClipboard *clipboard = QGuiApplication::clipboard();
clipboard->setText(conversation, QClipboard::Clipboard);
}

Q_INVOKABLE void copyToClipboard(int index)
{
QMutexLocker locker(&m_mutex);
Expand Down
Loading