@@ -20797,10 +20797,47 @@ Current version indicated by LITEVER below.
2079720797 oaiimgs = oai_append_media(oaiimgs);
2079820798 if(mhistory.length>0 && oaiimgs.length>0)
2079920799 {
20800- mhistory[0].msg = [{type: 'text', text: mhistory[0].msg}];
20801- for(let i=0;i<oaiimgs.length;++i)
20800+ //do we have the same number of images as placeholders? if so, assign each image to the right turns
20801+ let placeholder_count = 0;
20802+ const regex = /\n\(Attached (Image|Audio) \d+\)\n/g;
20803+ for(let i=0;i<mhistory.length;++i)
20804+ {
20805+ const matches = mhistory[i].msg.match(regex);
20806+ const count = matches ? matches.length : 0;
20807+ placeholder_count += count;
20808+ }
20809+ if(placeholder_count==oaiimgs.length)
2080220810 {
20803- mhistory[0].msg.push(oaiimgs[i]);
20811+ //exactly same number, so we can put images into matching turns
20812+ let mediaidx = 0;//for each occurance of the regex in this turn, insert mediaidx from oaiimgs and increment it
20813+ for (let i = 0; i < mhistory.length; ++i)
20814+ {
20815+ let msg = mhistory[i].msg;
20816+ const matches = msg.match(regex);
20817+ const count = matches ? matches.length : 0;
20818+ if(count>0)
20819+ {
20820+ msg = msg.replace(regex, '');
20821+ let newMsg = [{ type: 'text', text: msg }];
20822+ for (let j = 0; j < count; ++j)
20823+ {
20824+ if (mediaidx < oaiimgs.length)
20825+ {
20826+ newMsg.push(oaiimgs[mediaidx]);
20827+ mediaidx++;
20828+ }
20829+ }
20830+ mhistory[i].msg = newMsg;
20831+ }
20832+ }
20833+ }
20834+ else
20835+ {
20836+ mhistory[0].msg = [{type: 'text', text: mhistory[0].msg}];
20837+ for(let i=0;i<oaiimgs.length;++i)
20838+ {
20839+ mhistory[0].msg.push(oaiimgs[i]);
20840+ }
2080420841 }
2080520842 }
2080620843 for(let i=0;i<mhistory.length;++i)
0 commit comments