Fix stale Diceroll debug blocks on undiced swipes saveReply reuses the message's extra object in place, so swipes generated without a roll (guided swipes, roll failures, extension off) inherited the previous swipe's roll record and kept showing its debug block. Drop the record when a main-type generation finishes unsteered, hide the block as soon as a swipe/regenerate starts instead of leaving it until the result arrives, and skip rendering while an overswipe's generation is in flight.

da6a5b45329915f4f914fee9105096bc892b6b69

permissionBRICK <40219477+permissionBRICK@users.noreply.github.com>

1 files changed, +36 -9Showing whitespace changes
public/scripts/extensions/diceroll/index.js+36 -9
@@ -86,6 +86,11 @@ let isRolling = false;
86// Roll data waiting to be stamped onto the message the steered generation produces.86// Roll data waiting to be stamped onto the message the steered generation produces.
87let pendingStamp = null;87let pendingStamp = null;
8888
89// Message id stamped by the current generation. MESSAGE_RECEIVED consumes the pending stamp and
90// CHARACTER_MESSAGE_RENDERED fires right after for the same message; without this marker the
91// second event would mistake the freshly stamped message for an unsteered one and wipe it.
92let stampedMesId = null;
93
89// Whether the main generation being intercepted was started with its own custom prompt94// Whether the main generation being intercepted was started with its own custom prompt
90// (e.g. a swipe/regenerate triggered with an additional instruction). Tracked via95// (e.g. a swipe/regenerate triggered with an additional instruction). Tracked via
91// GENERATION_STARTED because the interceptor does not receive the generation params.96// GENERATION_STARTED because the interceptor does not receive the generation params.
@@ -316,6 +321,14 @@ async function onGenerationIntercept(coreChat, _contextSize, _abort, type) {
316 return;321 return;
317 }322 }
318323
324 stampedMesId = null;
325
326 // The message being replaced still shows the previous swipe's debug block; hide it as soon as
327 // the new generation starts instead of leaving it stuck until the result arrives.
328 if (type === 'swipe' || type === 'regenerate') {
329 $(`#chat .mes[mesid="${chat.length - 1}"] .diceroll_debug`).remove();
330 }
331
319 // A guided swipe/response (or any generation carrying its own instruction) takes precedence:332 // A guided swipe/response (or any generation carrying its own instruction) takes precedence:
320 // no roll, no direction injection — the manual instruction alone steers this generation.333 // no roll, no direction injection — the manual instruction alone steers this generation.
321 if (hasManualSteering(s)) {334 if (hasManualSteering(s)) {
@@ -379,21 +392,27 @@ async function onGenerationIntercept(coreChat, _contextSize, _abort, type) {
379392
380/**393/**
381 * Copies the roll that steered a finished generation onto the produced message, so the debug view394 * Copies the roll that steered a finished generation onto the produced message, so the debug view
382 * stays correct per message and survives reloads.395 * stays correct per message and survives reloads. When a main-type generation finishes WITHOUT a
396 * roll (extension disabled, roll failed, or a manual instruction steered it), the roll record
397 * inherited in place from the previous swipe's extra is dropped instead.
383 * @param {number} chatId Message index398 * @param {number} chatId Message index
399 * @param {string} type Generation type the message event was emitted with
384 */400 */
385function stampMessage(chatId) {401function stampMessage(chatId, type) {
386 if (!pendingStamp) {
387 return;
388 }
389 const message = chat[chatId];402 const message = chat[chatId];
390 if (!message || message.is_user || message.is_system) {403 if (!message || message.is_user || message.is_system) {
391 return;404 return;
392 }405 }
406 if (pendingStamp) {
393 message.extra = message.extra || {};407 message.extra = message.extra || {};
394 message.extra.diceroll = structuredClone(pendingStamp);408 message.extra.diceroll = structuredClone(pendingStamp);
395 pendingStamp = null;409 pendingStamp = null;
410 stampedMesId = chatId;
396 saveChatDebounced();411 saveChatDebounced();
412 } else if (STEERED_TYPES.has(type) && stampedMesId !== chatId && message.extra?.diceroll) {
413 delete message.extra.diceroll;
414 saveChatDebounced();
415 }
397}416}
398417
399function renderDebugForMessage(chatId) {418function renderDebugForMessage(chatId) {
@@ -403,11 +422,18 @@ function renderDebugForMessage(chatId) {
403 }422 }
404 mesElement.find('.diceroll_debug').remove();423 mesElement.find('.diceroll_debug').remove();
405424
406 const data = chat[chatId]?.extra?.diceroll;425 const message = chat[chatId];
426 const data = message?.extra?.diceroll;
407 if (!getSettings().debugDisplay || !data || !Array.isArray(data.options)) {427 if (!getSettings().debugDisplay || !data || !Array.isArray(data.options)) {
408 return;428 return;
409 }429 }
410430
431 // An overswipe points swipe_id one past the existing swipes while its generation is running;
432 // the roll record on the message still belongs to the previous swipe then, so show nothing.
433 if (typeof message.swipe_id === 'number' && Array.isArray(message.swipes) && message.swipe_id >= message.swipes.length) {
434 return;
435 }
436
411 const chosen = data.options[data.chosenIndex];437 const chosen = data.options[data.chosenIndex];
412 const details = $('<details class="diceroll_debug"></details>');438 const details = $('<details class="diceroll_debug"></details>');
413 const rollInfo = Number.isFinite(data.roll) ? `, roll ${data.roll.toFixed(1)}/${formatProbability(data.total)}` : '';439 const rollInfo = Number.isFinite(data.roll) ? `, roll ${data.roll.toFixed(1)}/${formatProbability(data.total)}` : '';
@@ -531,14 +557,15 @@ async function init() {
531 }557 }
532 });558 });
533559
534 eventSource.on(event_types.MESSAGE_RECEIVED, (chatId) => stampMessage(chatId));560 eventSource.on(event_types.MESSAGE_RECEIVED, (chatId, type) => stampMessage(chatId, type));
535 eventSource.on(event_types.CHARACTER_MESSAGE_RENDERED, (chatId) => {561 eventSource.on(event_types.CHARACTER_MESSAGE_RENDERED, (chatId, type) => {
536 stampMessage(chatId);562 stampMessage(chatId, type);
537 renderDebugForMessage(chatId);563 renderDebugForMessage(chatId);
538 });564 });
539 eventSource.on(event_types.MESSAGE_SWIPED, (chatId) => renderDebugForMessage(chatId));565 eventSource.on(event_types.MESSAGE_SWIPED, (chatId) => renderDebugForMessage(chatId));
540 eventSource.on(event_types.CHAT_CHANGED, () => {566 eventSource.on(event_types.CHAT_CHANGED, () => {
541 pendingStamp = null;567 pendingStamp = null;
568 stampedMesId = null;
542 clearDirectionInjection();569 clearDirectionInjection();
543 renderAllDebug();570 renderAllDebug();
544 });571 });