Do not replace inline HTML img with image embed (#4063)

89ef54f2869392e32f3d89cde55f0c4d3c0c536e

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +6 -23Ignore whitespace
public/script.js+6 -23
@@ -3566,7 +3566,7 @@ class StreamingProcessor {
3566 }3566 }
35673567
3568 if (this.image) {3568 if (this.image) {
3569 await processImageAttachment(chat[messageId], { imageUrl: this.image, parsedImage: null });3569 await processImageAttachment(chat[messageId], { imageUrl: this.image });
3570 appendMediaToMessage(chat[messageId], $(this.messageDom));3570 appendMediaToMessage(chat[messageId], $(this.messageDom));
3571 }3571 }
35723572
@@ -6309,17 +6309,11 @@ export function cleanUpMessage({ getMessage, isImpersonate, isContinue, displayI
6309 * Adds an image to the message.6309 * Adds an image to the message.
6310 * @param {object} message Message object6310 * @param {object} message Message object
6311 * @param {object} sources Image sources6311 * @param {object} sources Image sources
6312 * @param {ParsedImage} [sources.parsedImage] Parsed image
6313 * @param {string} [sources.imageUrl] Image URL6312 * @param {string} [sources.imageUrl] Image URL
6314 *6313 *
6315 * @returns {Promise<void>}6314 * @returns {Promise<void>}
6316 */6315 */
6317async function processImageAttachment(message, { parsedImage, imageUrl }) {6316async function processImageAttachment(message, { imageUrl }) {
6318 if (parsedImage?.image) {
6319 saveImageToMessage(parsedImage, message);
6320 return;
6321 }
6322
6323 if (!imageUrl) {6317 if (!imageUrl) {
6324 return;6318 return;
6325 }6319 }
@@ -6378,8 +6372,6 @@ export async function saveReply({ type, getMessage, fromStreaming = false, title
63786372
6379 let oldMessage = '';6373 let oldMessage = '';
6380 const generationFinished = new Date();6374 const generationFinished = new Date();
6381 const parsedImage = extractImageFromMessage(getMessage);
6382 getMessage = parsedImage.getMessage;
6383 if (type === 'swipe') {6375 if (type === 'swipe') {
6384 oldMessage = chat[chat.length - 1]['mes'];6376 oldMessage = chat[chat.length - 1]['mes'];
6385 chat[chat.length - 1]['swipes'].length++;6377 chat[chat.length - 1]['swipes'].length++;
@@ -6393,7 +6385,7 @@ export async function saveReply({ type, getMessage, fromStreaming = false, title
6393 chat[chat.length - 1]['extra']['model'] = getGeneratingModel();6385 chat[chat.length - 1]['extra']['model'] = getGeneratingModel();
6394 chat[chat.length - 1]['extra']['reasoning'] = reasoning;6386 chat[chat.length - 1]['extra']['reasoning'] = reasoning;
6395 chat[chat.length - 1]['extra']['reasoning_duration'] = null;6387 chat[chat.length - 1]['extra']['reasoning_duration'] = null;
6396 await processImageAttachment(chat[chat.length - 1], { parsedImage, imageUrl });6388 await processImageAttachment(chat[chat.length - 1], { imageUrl });
6397 if (power_user.message_token_count_enabled) {6389 if (power_user.message_token_count_enabled) {
6398 const tokenCountText = (reasoning || '') + chat[chat.length - 1]['mes'];6390 const tokenCountText = (reasoning || '') + chat[chat.length - 1]['mes'];
6399 chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);6391 chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);
@@ -6417,7 +6409,7 @@ export async function saveReply({ type, getMessage, fromStreaming = false, title
6417 chat[chat.length - 1]['extra']['model'] = getGeneratingModel();6409 chat[chat.length - 1]['extra']['model'] = getGeneratingModel();
6418 chat[chat.length - 1]['extra']['reasoning'] = reasoning;6410 chat[chat.length - 1]['extra']['reasoning'] = reasoning;
6419 chat[chat.length - 1]['extra']['reasoning_duration'] = null;6411 chat[chat.length - 1]['extra']['reasoning_duration'] = null;
6420 await processImageAttachment(chat[chat.length - 1], { parsedImage, imageUrl });6412 await processImageAttachment(chat[chat.length - 1], { imageUrl });
6421 if (power_user.message_token_count_enabled) {6413 if (power_user.message_token_count_enabled) {
6422 const tokenCountText = (reasoning || '') + chat[chat.length - 1]['mes'];6414 const tokenCountText = (reasoning || '') + chat[chat.length - 1]['mes'];
6423 chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);6415 chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);
@@ -6437,7 +6429,7 @@ export async function saveReply({ type, getMessage, fromStreaming = false, title
6437 chat[chat.length - 1]['extra']['api'] = getGeneratingApi();6429 chat[chat.length - 1]['extra']['api'] = getGeneratingApi();
6438 chat[chat.length - 1]['extra']['model'] = getGeneratingModel();6430 chat[chat.length - 1]['extra']['model'] = getGeneratingModel();
6439 chat[chat.length - 1]['extra']['reasoning'] += reasoning;6431 chat[chat.length - 1]['extra']['reasoning'] += reasoning;
6440 await processImageAttachment(chat[chat.length - 1], { parsedImage, imageUrl });6432 await processImageAttachment(chat[chat.length - 1], { imageUrl });
6441 // We don't know if the reasoning duration extended, so we don't update it here on purpose.6433 // We don't know if the reasoning duration extended, so we don't update it here on purpose.
6442 if (power_user.message_token_count_enabled) {6434 if (power_user.message_token_count_enabled) {
6443 const tokenCountText = (reasoning || '') + chat[chat.length - 1]['mes'];6435 const tokenCountText = (reasoning || '') + chat[chat.length - 1]['mes'];
@@ -6483,7 +6475,7 @@ export async function saveReply({ type, getMessage, fromStreaming = false, title
6483 chat[chat.length - 1]['extra']['gen_id'] = group_generation_id;6475 chat[chat.length - 1]['extra']['gen_id'] = group_generation_id;
6484 }6476 }
64856477
6486 await processImageAttachment(chat[chat.length - 1], { parsedImage, imageUrl: imageUrl });6478 await processImageAttachment(chat[chat.length - 1], { imageUrl });
6487 const chat_id = (chat.length - 1);6479 const chat_id = (chat.length - 1);
64886480
6489 !fromStreaming && await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id, type);6481 !fromStreaming && await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id, type);
@@ -6700,15 +6692,6 @@ function getGeneratingModel(mes) {
6700 return model;6692 return model;
6701}6693}
67026694
6703function extractImageFromMessage(getMessage) {
6704 const regex = /<img src="(.*?)".*?alt="(.*?)".*?>/g;
6705 const results = regex.exec(getMessage);
6706 const image = results ? results[1] : '';
6707 const title = results ? results[2] : '';
6708 getMessage = getMessage.replace(regex, '');
6709 return { getMessage, image, title, inline: true };
6710}
6711
6712/**6695/**
6713 * A function mainly used to switch 'generating' state - setting it to false and activating the buttons again6696 * A function mainly used to switch 'generating' state - setting it to false and activating the buttons again
6714 */6697 */