Utils and Helpers
This is a current map of shared utility modules under src/utils/.
Folder Map
Section titled “Folder Map”utils/asyncutils/audioutils/bridgesutils/cacheutils/chatutils/compactionutils/conditioningutils/dbutils/discordutils/documentsutils/embeddingsutils/imageutils/mcputils/mediautils/memoryutils/metricsutils/miscutils/novelaiutils/personautils/providerutils/quotautils/securityutils/storageutils/teachutils/textutils/tools
High-Impact Modules
Section titled “High-Impact Modules”utils/db
Section titled “utils/db”client.ts: DB client wiringinitializeDatabase.ts: schema + seed startup runnersqlSecurity.ts: query parameterisation helperssqlSplitter.ts: SQL file parsing utilitiesragAvailability.ts: pgvector / RAG feature detectionrepositories/: 23 domain-owned Repository classes +index.ts(instance + type re-exports only). All SQL is inlined asprivatemethods on each Repository class — no*ReadSql.ts/*WriteSql.tssibling files exist.ErrorLogRepositoryis a thin shim used bylogger.tsto insert intoerror_logswithout creating a circular import. Seedocs/subsystems/database-schema.mdfor the full repository table and SQL convention.
utils/discord
Section titled “utils/discord”commandLoader.ts: command discovery + localization wiringcommandRegistry.ts: runtime command maps used by handlersinteractionHelper.ts: compatibility barrel for grouped UI helpers inutils/discord/ui/; new code imports the owned UI module directlystreamOrchestrator.ts: public stream orchestration entry point backed by responsibility modules inutils/discord/stream/webhookManager.ts: compatibility barrel for grouped webhook helpers inutils/discord/webhook/; new code imports the owned webhook module directlyembedHelper.ts,historyFetcher.ts,historyFormatter.ts
utils/text
Section titled “utils/text”localizer.ts: locale auto-discovery + lookupcontextBuilder.ts: public structured context routing and native orchestrationcontext/: context-builder support modules for types, template/conditioning blocks, memories, RAG, and history/media helperscontextTruncator.ts: token-budget truncation strategyprocessors/regexUtils.ts:escapeRegExpprocessors/mentionProcessor.ts: mention resolution, template variables, emoji normalizationprocessors/llmOutputProcessor.ts: LLM output cleaning, speaker-turn truncationprocessors/chunkProcessor.ts: message chunking, sentence splittingprocessors/formatters.ts: time formatting, text humanization, boolean displayprocessors/timeUtils.ts: reminder time parsing, lateness calculationemojiHelper.ts,emojiPenalty.tstimezoneHelper.ts,uncensor.ts,youTubeUrlCleaner.ts
utils/cache
Section titled “utils/cache”tomoriStateCache.tsuserCache.tsemojiStickerCache.tschannelLlmCache.ts,channelLlmCacheStore.tschannelWhitelistCache.tsshortTermMemoryCache.tsllmCache.tsopenrouterCapabilityCache.tsgeminiCapabilityCache.tsnovelaiCapabilityCache.tsemergencyCacheClearer.ts: critical-memory cleanup for recoverable caches- lazy sync helpers (
emojiLazySync.ts,stickerLazySync.ts)
utils/security
Section titled “utils/security”secretsManager.ts:.envvs AWS Secrets Manager load pathkeyManager.ts: encryption key version managementcrypto.ts: encryption/decryption helperskeyRotation.ts: rotation workflowsrateLimiter.ts: upload quota cleanup schedulersafeDownload.ts: constrained external content download
utils/quota
Section titled “utils/quota”imageQuotaManager.ts: per-user and server-wide image generation quotastextQuotaManager.ts: per-user and server-wide text trigger quotasvideoQuotaManager.ts: per-user and server-wide video generation quotas
utils/provider
Section titled “utils/provider”providerFactory.ts: provider auto-discovery and instance resolution
utils/mcp
Section titled “utils/mcp”mcpManager.ts: MCP lifecyclemcpExecutor.ts: MCP execution abstractionmcpConfig.ts: MCP config loadingmcpUrlSecurity.ts: guild MCP URL parsing, DNS/IP validation, and SSRF hardening
utils/bridges
Section titled “utils/bridges”bridgeUserId.ts: bridge ID and webhook username parsing utilitiesmatrix/: Matrix appservice bridge runtimematrix/events.ts: appservice init and Matrix inbound event surfacematrix/stateSync.ts: Matrix link cache, typing state, reminder mention surfacematrix/userMapping.ts: Matrix display-name/ID maps and persona intent surfacematrix/rooms.ts: Matrix room join/config/encryption helpersmatrix/matrixManager.ts: thin Matrix public coordinator barrel
New code should use utils/bridges for generic bridge helpers and utils/bridges/matrix for Matrix runtime operations.
utils/image and utils/storage
Section titled “utils/image and utils/storage”avatarHelper.ts,imageProcessor.ts,pngMetadata.tsavatarStorage.tsfor S3/public avatar URL supportcharrefStorage.tsfor NovelAI character reference storage (S3 in production, local filesystem in non-production)
utils/misc
Section titled “utils/misc”logger.ts: structured logging facadeioHelper.ts: filesystem traversal helpershealthTracker.ts: runtime health signals used by/health
Usage Guidance
Section titled “Usage Guidance”- Prefer these shared modules over duplicating logic in commands/events.
- For user-facing responses, always pair utility usage with localization via
localizer(). - For DB writes touching cached data, invalidate the affected caches in the same code path.