| 4235 | 4235 | |
| 4236 | 4236 | // Do not suffix the message for continuation |
| 4237 | 4237 | if (i === 0 && isContinue) { |
| 4238 | + // Pick something that's very unlikely to be in a message |
| 4239 | + const FORMAT_TOKEN = '\u0000\ufffc\u0000\ufffd'; |
| 4240 | + |
| 4238 | 4241 | if (isInstruct) { |
| 4242 | + const originalMessage = String(coreChat[j].mes ?? ''); |
| 4243 | + coreChat[j].mes = originalMessage.replaceAll(FORMAT_TOKEN, '') + FORMAT_TOKEN; |
| 4239 | 4244 | // Reformat with the last output sequence (if any) |
| 4240 | 4245 | chat2[i] = formatMessageHistoryItem(coreChat[j], isInstruct, force_output_sequence.LAST); |
| 4246 | + coreChat[j].mes = originalMessage; |
| 4241 | 4247 | } |
| 4242 | 4248 | |
| 4243 | | - chat2[i] = chat2[i].slice(0, chat2[i].lastIndexOf(coreChat[j].mes) + coreChat[j].mes.length); |
| 4249 | + chat2[i] = chat2[i].includes(FORMAT_TOKEN) |
| 4250 | + ? chat2[i].slice(0, chat2[i].lastIndexOf(FORMAT_TOKEN)) |
| 4251 | + : chat2[i].slice(0, chat2[i].lastIndexOf(coreChat[j].mes) + coreChat[j].mes.length); |
| 4244 | 4252 | continue_mag = coreChat[j].mes; |
| 4245 | 4253 | } |
| 4246 | 4254 | |