[FEATURE_REQUEST] Add `GENERATE_BEFORE_DISPATCH` event: an awaited hook with the final payload, generation type and exact token counting

Author: dariusmccloudCreated Sep 28, 2026Updated Sep 28, 2026
Labels🦄 Feature Request

Have you searched for similar requests?

No

Is your feature request related to a problem? If so, please describe.

Extensions that add context to a generation, such as memory, retrieval or lorebook-style tools, need to know how large the final request is before it is sent. Only then can they fit their additions into the remaining context without guessing. No existing event provides everything needed at that moment:

Event Final payload Generation type Exact token counting Listener can replace the payload
GENERATE_AFTER_COMBINE_PROMPTS Text-completion prompt only No. It is also fired by generateRawData without a type. No Prompt string only
GENERATE_AFTER_DATA Yes No No No (in-place mutation only)
GENERATE_BEFORE_DISPATCH (new) Yes Yes Yes, through the host's own tokenizer for the payload's shape Yes

Without the type, an extension cannot tell a user turn from a quiet or background generation. Without the host tokenizer, it has to approximate token counts, and approximating is exactly what goes wrong near the context limit.

Describe the solution you'd like

  • public/scripts/events.js: adds GENERATE_BEFORE_DISPATCH: 'generate_before_dispatch'.

  • public/script.js, in Generate(): after generate_data is fully assembled, and immediately before the existing GENERATE_AFTER_DATA emit, it awaits one new event with:

    javascript
    {
        generateData,   // the final request payload; a listener may replace it
        promptCounts,   // this generation's prompt itemization record, or null
        countPrompt,    // async (prompt) => tokens, using the host tokenizer:
                        // arrays go through countTokensOpenAIAsync, strings through getTokenCountAsync
        type,           // the generation type ('normal', 'swipe', 'continue', 'quiet', ...)
        dryRun,         // true for dry runs such as prompt-manager token refreshes
    }

    Then generate_data = beforeDispatchData.generateData.

Backward compatible. With no listeners it is a no-op. No existing event, argument or ordering changes. GENERATE_AFTER_DATA still fires right after it, exactly as before.

Describe alternatives you've considered

No current alternative exists for this specific requirement.

Additional context

How to Test

In the browser console, with a character chat open:

javascript
const { eventSource, eventTypes } = SillyTavern.getContext();
eventSource.on(eventTypes.GENERATE_BEFORE_DISPATCH, async (d) => {
    const payload = d.generateData.messages ?? d.generateData.prompt;
    console.log('before dispatch', { type: d.type, dryRun: d.dryRun, tokens: await d.countPrompt(payload) });
});

Then:

  1. Send a message. Expect type: 'normal', dryRun: false, and a token count that matches the prompt itemization.
  2. Swipe. Expect type: 'swipe'.
  3. Open the Prompt Manager or change a prompt toggle. Expect dryRun: true events.
  4. Repeat under a Text Completion API. countPrompt receives a string there, and the count should still match.
  5. Replacement check (optional): set d.generateData = { ...d.generateData } in a listener and confirm the generation still dispatches normally.

Priority

Medium (Would be very useful)

Are you willing to test this on staging/unstable branch if this is implemented?

Yes