feat(discord-gateway): add local badword pre-filter, accurate token estimation, Piscina maxThreads config, and PromptMode support

This commit is contained in:
MythEclipse
2026-06-02 22:50:23 +07:00
parent 6d2da7ce1d
commit f163ead3cf
4 changed files with 69 additions and 1 deletions
@@ -1,6 +1,7 @@
import { formatModerationTextEvidenceForPrompt } from "./indonesianTextNormalizer.js";
import { formatMediaEvidenceForPrompt } from "../message-capture/messageMetadata.js";
import type { MessageRecord } from "../message-capture/types.js";
import { encodingForModel } from "tiktoken";
export interface ConversationContextInput {
contextBefore: MessageRecord[];
@@ -8,6 +9,15 @@ export interface ConversationContextInput {
maxTokens: number;
}
let _encoder: ReturnType<typeof encodingForModel> | null = null;
function getEncoder(): ReturnType<typeof encodingForModel> {
if (!_encoder) {
_encoder = encodingForModel("gpt-4o");
}
return _encoder;
}
/**
* Formats a timestamp to ISO 8601 string
*/
@@ -19,7 +29,8 @@ function formatTimestamp(ms: number): string {
* Estimates token count for a string (pessimistic approximation for Indonesian slang & JSON overhead)
*/
export function estimateTokens(text: string): number {
return Math.ceil(text.length / 3) + 15;
// Use tiktoken for accurate token counting (+15 overhead for JSON structure)
return getEncoder().encode(text).length + 15;
}
/**