@@ -15371,23 +15371,26 @@ Current version indicated by LITEVER below.
1537115371 return inputtxt;
1537215372 }
1537315373
15374- function end_trim_to_sentence(input,include_newline= false) {
15374+ function end_trim_to_sentence(input, include_newline = false) {
1537515375 let last = -1;
1537615376 let enders = ['.', '!', '?', '*', '"', ')', '}', '`', ']', ';', '…'];
15377- for (let i = 0; i < enders.length; ++i)
15378- {
15377+ for (let i = 0; i < enders.length; ++i) {
1537915378 last = Math.max(last, input.lastIndexOf(enders[i]));
1538015379 }
15381-
15382- if(include_newline)
15383- {
15380+ if (include_newline) {
1538415381 let nl = input.lastIndexOf("\n");
1538515382 last = Math.max(last, nl);
1538615383 }
15387- if (last > 0) {
15388- return input.substring(0, last + 1).replace(/[\t\r\n ]+$/, '');
15384+ let cleaned = input.replace(/[\t\r\n ]+$/, '');
15385+ if (last <= 0) { // If no valid end found
15386+ return cleaned;
1538915387 }
15390- return input.replace(/[\t\r\n ]+$/, '');
15388+ let trimmedLength = cleaned.length - (last + 1); // Calculate how many characters would be trimmed
15389+ if (trimmedLength > 150) { // If more than 150 chars would be trimmed, skip trimming
15390+ return cleaned;
15391+ }
15392+ // Otherwise, trim up to the last sentence end
15393+ return cleaned.substring(0, last + 1).replace(/[\t\r\n ]+$/, '');
1539115394 }
1539215395
1539315396 function start_trim_to_sentence(input) {
@@ -18038,29 +18041,30 @@ Current version indicated by LITEVER below.
1803818041 //it's an object/array, so repack the text turn
1803918042 let t1 = mainoaibody[0].text;
1804018043 mhistory = repack_instruct_history(t1);
18044+ }
1804118045
18042- //many backends REFUSE to allow the assistant to go first. If the first turn belongs to assistant, we turn it into user
18043- if(mhistory.length>0 && !mhistory[0].myturn)
18044- {
18045- mhistory[0].myturn = true;
18046- }
18047- //if both first and second turns are from user, merge them into one turn
18048- if(mhistory.length>1 && mhistory[0].myturn && mhistory[1].myturn)
18049- {
18050- mhistory[0].msg += "\n"+mhistory[1].msg;
18051- mhistory.splice(1, 1);
18052- }
18046+ //many backends REFUSE to allow the assistant to go first. If the first turn belongs to assistant, we turn it into user
18047+ if(mhistory.length>0 && !mhistory[0].myturn)
18048+ {
18049+ mhistory[0].myturn = true;
18050+ }
18051+ //if both first and second turns are from user, merge them into one turn
18052+ if(mhistory.length>1 && mhistory[0].myturn && mhistory[1].myturn)
18053+ {
18054+ mhistory[0].msg += "\n"+mhistory[1].msg;
18055+ mhistory.splice(1, 1);
18056+ }
1805318057
18054- if(mhistory.length>0 && mainoaibody.length>1 && mainoaibody[1].type && mainoaibody[1].type!="text")
18058+ if(mhistory.length>0 && mainoaibody.length>1 && mainoaibody[1].type && mainoaibody[1].type!="text")
18059+ {
18060+ mhistory[0].msg = [{type: 'text', text: mhistory[0].msg}];
18061+ for(let i=1;i<mainoaibody.length;++i)
1805518062 {
18056- mhistory[0].msg = [{type: 'text', text: mhistory[0].msg}];
18057- for(let i=1;i<mainoaibody.length;++i)
18058- {
18059- mhistory[0].msg.push(mainoaibody[i]);
18060- }
18063+ mhistory[0].msg.push(mainoaibody[i]);
1806118064 }
1806218065 }
1806318066
18067+
1806418068 for(let i=0;i<mhistory.length;++i)
1806518069 {
1806618070 oai_payload.messages.push({ "role": (mhistory[i].myturn?"user":"assistant"), "content": mhistory[i].msg });
0 commit comments