`refactor/perf-printMessages` #4: Renamed `newMessage` to `messageElement` and `newMessageId` to `messageId`. (#4985) * If insert is false, `newMessage` may not be new, so I renamed `newMessage` to `messageElement` and `newMessageId` to `messageId`. I also rearranged some constants to improve readability. * Accept 0 as valid insertAfter/insertBefore targets * Yeah whatever --------- Co-authored-by: user <user@exmaple.com> Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>
Signed| @@ -2441,7 +2441,7 @@ function getMessageTextHTML(message, { messageId = chat.indexOf(message) }) { | |||
| 2441 | */ | 2441 | */ |
| 2442 | export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll = true, insertBefore = null, forceId = null, showSwipes = true, insert = true } = {}) { | 2442 | export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll = true, insertBefore = null, forceId = null, showSwipes = true, insert = true } = {}) { |
| 2443 | // Callers push the new message to chat before calling addOneMessage | 2443 | // Callers push the new message to chat before calling addOneMessage |
| 2444 | const newMessageId = (() => { | 2444 | const messageId = (() => { |
| 2445 | if (typeof forceId === 'number') { | 2445 | if (typeof forceId === 'number') { |
| 2446 | return forceId; | 2446 | return forceId; |
| 2447 | } | 2447 | } |
| @@ -2458,8 +2458,6 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll | |||
| 2458 | return chat.length - 1; | 2458 | return chat.length - 1; |
| 2459 | })(); | 2459 | })(); |
| 2460 | 2460 | ||
| 2461 | const momentDate = timestampToMoment(mes.send_date); | ||
| 2462 | const timestamp = momentDate.isValid() ? momentDate.format('LL LT') : ''; | ||
| 2463 | 2461 | ||
| 2464 | let avatarImg = getThumbnailUrl('persona', user_avatar); | 2462 | let avatarImg = getThumbnailUrl('persona', user_avatar); |
| 2465 | 2463 | ||
| @@ -2469,12 +2467,10 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll | |||
| 2469 | avatarImg = mes.force_avatar; | 2467 | avatarImg = mes.force_avatar; |
| 2470 | } else if (this_chid === undefined) { | 2468 | } else if (this_chid === undefined) { |
| 2471 | avatarImg = system_avatar; | 2469 | avatarImg = system_avatar; |
| 2470 | } else if (characters[this_chid] && characters[this_chid].avatar !== 'none') { | ||
| 2471 | avatarImg = getThumbnailUrl('avatar', characters[this_chid].avatar); | ||
| 2472 | } else { | 2472 | } else { |
| 2473 | if (characters[this_chid].avatar !== 'none') { | 2473 | avatarImg = default_avatar; |
| 2474 | avatarImg = getThumbnailUrl('avatar', characters[this_chid].avatar); | ||
| 2475 | } else { | ||
| 2476 | avatarImg = default_avatar; | ||
| 2477 | } | ||
| 2478 | } | 2474 | } |
| 2479 | //old processing: | 2475 | //old processing: |
| 2480 | //if message is from system, use the name provided in the message JSONL to proceed, | 2476 | //if message is from system, use the name provided in the message JSONL to proceed, |
| @@ -2484,9 +2480,14 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll | |||
| 2484 | // Special case for persona images. | 2480 | // Special case for persona images. |
| 2485 | avatarImg = mes.force_avatar; | 2481 | avatarImg = mes.force_avatar; |
| 2486 | } | 2482 | } |
| 2483 | const momentDate = timestampToMoment(mes.send_date); | ||
| 2484 | const timestamp = momentDate.isValid() ? momentDate.format('LL LT') : ''; | ||
| 2485 | const messageHTML = getMessageTextHTML(mes, { messageId }); | ||
| 2486 | const bookmarkLink = mes?.extra?.bookmark_link; | ||
| 2487 | const tokenCount = mes.extra?.token_count; | ||
| 2488 | const { timerValue, timerTitle } = formatGenerationTimer(mes.gen_started, mes.gen_finished, mes.extra?.token_count, mes.extra?.reasoning_duration, mes.extra?.time_to_first_token); | ||
| 2487 | 2489 | ||
| 2488 | const messageHTML = getMessageTextHTML(mes, { messageId: newMessageId }); | 2490 | let messageElement; |
| 2489 | let newMessage; | ||
| 2490 | 2491 | ||
| 2491 | if (type === 'swipe') { | 2492 | if (type === 'swipe') { |
| 2492 | // Forbidden black magic | 2493 | // Forbidden black magic |
| @@ -2494,17 +2495,13 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll | |||
| 2494 | mes.swipe_id ??= 0; | 2495 | mes.swipe_id ??= 0; |
| 2495 | mes.swipes ??= [mes.mes]; | 2496 | mes.swipes ??= [mes.mes]; |
| 2496 | //This keeps listeners intact. | 2497 | //This keeps listeners intact. |
| 2497 | newMessage = chatElement.find(`[mesid="${newMessageId}"]`); | 2498 | messageElement = chatElement.find(`[mesid="${messageId}"]`); |
| 2498 | } else { | 2499 | } else { |
| 2499 | newMessage = messageTemplate.clone(); | 2500 | messageElement = messageTemplate.clone(); |
| 2500 | } | 2501 | } |
| 2501 | 2502 | ||
| 2502 | const { timerValue, timerTitle } = formatGenerationTimer(mes.gen_started, mes.gen_finished, mes.extra?.token_count, mes.extra?.reasoning_duration, mes.extra?.time_to_first_token); | 2503 | messageElement.attr({ |
| 2503 | const tokenCount = mes.extra?.token_count; | 2504 | 'mesid': messageId, |
| 2504 | const bookmarkLink = mes?.extra?.bookmark_link; | ||
| 2505 | |||
| 2506 | newMessage.attr({ | ||
| 2507 | 'mesid': newMessageId, | ||
| 2508 | 'swipeid': mes.swipe_id ?? 0, | 2505 | 'swipeid': mes.swipe_id ?? 0, |
| 2509 | 'ch_name': mes.name, | 2506 | 'ch_name': mes.name, |
| 2510 | 'is_user': mes.is_user, | 2507 | 'is_user': mes.is_user, |
| @@ -2516,68 +2513,65 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll | |||
| 2516 | 'type': mes.extra?.type ?? '', | 2513 | 'type': mes.extra?.type ?? '', |
| 2517 | }); | 2514 | }); |
| 2518 | 2515 | ||
| 2519 | newMessage.find('.avatar img').attr('src', avatarImg); | 2516 | messageElement.find('.avatar img').attr('src', avatarImg); |
| 2520 | newMessage.find('.ch_name .name_text').text(mes.name); | 2517 | messageElement.find('.ch_name .name_text').text(mes.name); |
| 2521 | newMessage.find('.timestamp').text(timestamp).attr('title', `${mes.extra?.api ? mes.extra.api + ' - ' : ''}${mes.extra?.model ?? ''}`); | 2518 | messageElement.find('.timestamp').text(timestamp).attr('title', `${mes.extra?.api ? mes.extra.api + ' - ' : ''}${mes.extra?.model ?? ''}`); |
| 2522 | newMessage.find('.mesIDDisplay').text(`#${newMessageId}`); | 2519 | messageElement.find('.mesIDDisplay').text(`#${messageId}`); |
| 2523 | tokenCount && newMessage.find('.tokenCounterDisplay').text(`${tokenCount}t`); | 2520 | tokenCount && messageElement.find('.tokenCounterDisplay').text(`${tokenCount}t`); |
| 2524 | mes.title && newMessage.attr('title', mes.title); | 2521 | mes.title && messageElement.attr('title', mes.title); |
| 2525 | timerValue && newMessage.find('.mes_timer').attr('title', timerTitle).text(timerValue); | 2522 | timerValue && messageElement.find('.mes_timer').attr('title', timerTitle).text(timerValue); |
| 2526 | bookmarkLink && updateBookmarkDisplay(newMessage); | 2523 | bookmarkLink && updateBookmarkDisplay(messageElement); |
| 2527 | 2524 | ||
| 2528 | if (mes.extra?.bias !== '') { | 2525 | if (mes.extra?.bias !== '') { |
| 2529 | const bias = messageFormatting(mes.extra?.bias, '', false, false, -1, {}, false); | 2526 | const bias = messageFormatting(mes.extra?.bias, '', false, false, -1, {}, false); |
| 2530 | newMessage.find('.mes_bias').html(bias); | 2527 | messageElement.find('.mes_bias').html(bias); |
| 2531 | } | 2528 | } |
| 2532 | 2529 | ||
| 2533 | updateReasoningUI(newMessage); | 2530 | updateReasoningUI(messageElement); |
| 2534 | 2531 | ||
| 2535 | if (power_user.timestamp_model_icon && mes.extra?.api) { | 2532 | if (power_user.timestamp_model_icon && mes.extra?.api) { |
| 2536 | insertSVGIcon(newMessage, mes.extra); | 2533 | insertSVGIcon(messageElement, mes.extra); |
| 2537 | } | 2534 | } |
| 2538 | 2535 | ||
| 2539 | if (type !== 'swipe' && insert) { | 2536 | if (type !== 'swipe' && insert) { |
| 2540 | if (!insertAfter && !insertBefore) { | 2537 | if (typeof insertAfter === 'number' && insertAfter >= 0) { |
| 2541 | chatElement.append(newMessage); | ||
| 2542 | } | ||
| 2543 | else if (insertAfter) { | ||
| 2544 | const target = chatElement.find(`.mes[mesid="${insertAfter}"]`); | 2538 | const target = chatElement.find(`.mes[mesid="${insertAfter}"]`); |
| 2545 | $(newMessage).insertAfter(target); | 2539 | $(messageElement).insertAfter(target); |
| 2546 | } else { | 2540 | } else if (typeof insertBefore === 'number' && insertBefore >= 0) { |
| 2547 | const target = chatElement.find(`.mes[mesid="${insertBefore}"]`); | 2541 | const target = chatElement.find(`.mes[mesid="${insertBefore}"]`); |
| 2548 | $(newMessage).insertBefore(target); | 2542 | $(messageElement).insertBefore(target); |
| 2543 | } else { | ||
| 2544 | chatElement.append(messageElement); | ||
| 2549 | } | 2545 | } |
| 2550 | } | 2546 | } |
| 2551 | 2547 | ||
| 2552 | const isSmallSys = mes?.extra?.isSmallSys; | 2548 | if (mes?.extra?.isSmallSys === true) { |
| 2553 | 2549 | messageElement.addClass('smallSysMes'); | |
| 2554 | if (isSmallSys === true) { | ||
| 2555 | newMessage.addClass('smallSysMes'); | ||
| 2556 | } | 2550 | } |
| 2557 | 2551 | ||
| 2558 | if (Array.isArray(mes?.extra?.tool_invocations)) { | 2552 | if (Array.isArray(mes?.extra?.tool_invocations)) { |
| 2559 | newMessage.addClass('toolCall'); | 2553 | messageElement.addClass('toolCall'); |
| 2560 | } | 2554 | } |
| 2561 | 2555 | ||
| 2562 | updateMessageItemizedPromptButton(mes, { messageId: newMessageId, messageElement: newMessage }); | 2556 | updateMessageItemizedPromptButton(mes, { messageId, messageElement }); |
| 2563 | 2557 | ||
| 2564 | newMessage.find('.avatar img').on('error', function () { | 2558 | messageElement.find('.avatar img').on('error', function () { |
| 2565 | $(this).hide(); | 2559 | $(this).hide(); |
| 2566 | $(this).parent().html('<div class="missing-avatar fa-solid fa-user-slash"></div>'); | 2560 | $(this).parent().html('<div class="missing-avatar fa-solid fa-user-slash"></div>'); |
| 2567 | }); | 2561 | }); |
| 2568 | 2562 | ||
| 2569 | appendMediaToMessage(mes, newMessage, scroll ? SCROLL_BEHAVIOR.ADJUST : SCROLL_BEHAVIOR.NONE); | 2563 | appendMediaToMessage(mes, messageElement, scroll ? SCROLL_BEHAVIOR.ADJUST : SCROLL_BEHAVIOR.NONE); |
| 2570 | newMessage.find('.mes_text').html(messageHTML); | 2564 | messageElement.find('.mes_text').html(messageHTML); |
| 2571 | addCopyToCodeBlocks(newMessage); | 2565 | addCopyToCodeBlocks(messageElement); |
| 2572 | 2566 | ||
| 2573 | // Set the swipes counter for all non-user messages. | 2567 | // Set the swipes counter for all non-user messages. |
| 2574 | if (!mes.is_user) { | 2568 | if (!mes.is_user) { |
| 2575 | updateSwipeCounter(newMessageId, { messageElement: newMessage }); | 2569 | updateSwipeCounter(messageId, { message: mes, messageElement }); |
| 2576 | } | 2570 | } |
| 2577 | 2571 | ||
| 2578 | // The caller should handle the rest after adding a message to DOM. | 2572 | // The caller should handle the rest after adding a message to DOM. |
| 2579 | if (!insert) { | 2573 | if (!insert) { |
| 2580 | return newMessage; | 2574 | return messageElement; |
| 2581 | } | 2575 | } |
| 2582 | 2576 | ||
| 2583 | //last_mes should always be updated. | 2577 | //last_mes should always be updated. |
| @@ -2592,10 +2586,10 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll | |||
| 2592 | scrollChatToBottom({ waitForFrame: true }); | 2586 | scrollChatToBottom({ waitForFrame: true }); |
| 2593 | } | 2587 | } |
| 2594 | 2588 | ||
| 2595 | applyCharacterTagsToMessageDivs({ mesIds: newMessageId }); | 2589 | applyCharacterTagsToMessageDivs({ mesIds: messageId }); |
| 2596 | updateEditArrowClasses(); | 2590 | updateEditArrowClasses(); |
| 2597 | 2591 | ||
| 2598 | return newMessage; | 2592 | return messageElement; |
| 2599 | } | 2593 | } |
| 2600 | 2594 | ||
| 2601 | /** | 2595 | /** |