[FEATURE_REQUEST] Add `GENERATE_BEFORE_DISPATCH` event: an awaited hook with the final payload, generation type and exact token counting
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: addsGENERATE_BEFORE_DISPATCH: 'generate_before_dispatch'.public/script.js, inGenerate(): aftergenerate_datais fully assembled, and immediately before the existingGENERATE_AFTER_DATAemit, it awaits one new event with:{ 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:
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:
- Send a message. Expect
type: 'normal',dryRun: false, and a token count that matches the prompt itemization. - Swipe. Expect
type: 'swipe'. - Open the Prompt Manager or change a prompt toggle. Expect
dryRun: trueevents. - Repeat under a Text Completion API.
countPromptreceives a string there, and the count should still match. - 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
Source: SillyTavern/SillyTavern