Skip to content

Commit

Permalink
Improve merge_messages in GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
hlohaus committed Jan 7, 2025
1 parent ba6c47f commit ef642f5
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions g4f/gui/client/static/js/chat.v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,30 +994,48 @@ const new_conversation = async () => {

function merge_messages(message1, message2) {
let newContent = message2;
// Remove start tokens
if (newContent.startsWith("```")) {
const index = str.indexOf("\n");
newContent = newContent.substring(index);
const index = newContent.indexOf("\n");
if (index != -1) {
newContent = newContent.substring(index);
}
} else if (newContent.startsWith("...")) {
newContent = " " + newContent.substring(3);
}
// Remove duplicate words
if (newContent.indexOf(" ") > 0) {
let words = message1.trim().split(" ");
let lastWord = words[words.length - 1];
if (newContent.startsWith(lastWord)) {
newContent = newContent.substring(lastWord.length);
} else {
// Remove duplicate lines
let lines = message1.trim().split("\n");
let lastLine = lines[lines.length - 1];
let foundLastLine = newContent.indexOf(lastLine + "\n");
if (foundLastLine != -1) {
foundLastLine += 1;
} else {
foundLastLine = newContent.indexOf(lastLine);
}
if (foundLastLine != -1) {
newContent = newContent.substring(foundLastLine + lastLine.length);
}
// Remove duplicate words
if (foundLastLine == -1 && newContent.indexOf(" ") > 0) {
let words = message1.trim().split(" ");
let lastWord = words[words.length - 1];
if (newContent.startsWith(lastWord)) {
newContent = newContent.substring(lastWord.length);
}
}
}
// Remove duplicate lines
let lines = message1.trim().split("\n");
let lastLine = lines[lines.length - 1];
while (newContent && lastLine && newContent.startsWith(lastLine)) {
newContent = newContent.substring(lastLine.length);
lastLine = lines[lines.length - 1];
}
return message1 + newContent;
}

// console.log(merge_messages("Hello", "Hello,\nhow are you?"));
// console.log(merge_messages("Hello", "Hello, how are you?"));
// console.log(merge_messages("Hello", "Hello,\nhow are you?"));
// console.log(merge_messages("Hello,\n", "Hello,\nhow are you?"));
// console.log(merge_messages("Hello,\n", "how are you?"));
// console.log(merge_messages("1 != 2", "1 != 2;"));
// console.log(merge_messages("1 != 2", "```python\n1 != 2;"));
// console.log(merge_messages("1 != 2;\n1 != 3;\n", "1 != 2;\n1 != 3;\n"));

const load_conversation = async (conversation_id, scroll=true) => {
let conversation = await get_conversation(conversation_id);
let messages = conversation?.items || [];
Expand All @@ -1039,7 +1057,6 @@ const load_conversation = async (conversation_id, scroll=true) => {
let last_model = null;
let providers = [];
let buffer = "";
let last_usage = null;
let completion_tokens = 0;

messages.forEach((item, i) => {
Expand All @@ -1051,7 +1068,6 @@ const load_conversation = async (conversation_id, scroll=true) => {
buffer = buffer.replace(/ \[aborted\]$/g, "").replace(/ \[error\]$/g, "");
buffer += merge_messages(buffer, item.content);
last_model = item.provider?.model;
last_usage = item.usage;
providers.push(item.provider?.name);
let next_i = parseInt(i) + 1;
let next_provider = item.provider ? item.provider : (messages.length > next_i ? messages[next_i].provider : null);
Expand Down

0 comments on commit ef642f5

Please # to comment.