Skip to content

Chat Pipeline

The chat pipeline turns a single Discord messageCreate event into zero, one, or many persona replies. It is the primary coordinator of TomoriBot: context construction, tool execution, provider streaming, and memory persistence are all reached through this pipeline.

  • Entry point: tomoriChat() in src/events/messageCreate/tomoriChat.ts
  • Triggered by: Discord messageCreate events, empty-response retries, queue replays, stop-response generation, cross-channel boomerang follow-ups, and command invocations.
flowchart TD
entry["tomoriChat(TomoriChatInput)"] --> s01["[01] normalizeChatInvocation<br/>→ ChatIncoming"]
s01 --> s02["[02] evaluateChatAdmission<br/>→ ChatAdmission (run | ignore | queued | blocked | error)"]
s02 -->|"disposition !== run"| s03["[03] handleChatDisposition"]
s03 --> endRejected["End"]
s02 -->|"disposition === run"| s04
subgraph s04["[04] runWithChannelLock (concurrency wrapper)"]
s05["[05] planChatTurns<br/>→ ChatTurnPlan { turns: ChatTurn[] }"]
s05 -->|"turns.length === 0"| skip["Release lock, replay queue, end"]
s05 -->|"for each turn"| s0601["[06.01] buildChatTurnContext<br/>→ ChatTurnContext"]
s0601 --> s0602["[06.02] createChatResponseSink<br/>→ ChatResponseSink"]
s0602 --> s0603["[06.03] runGenerationTurn<br/>→ GenerationTurnResult"]
s0603 --> s0604["[06.04] runPostTurnEffects"]
end
#StageGuideOwnership
01normalizeChatInvocation01: Input NormalizationSets defaults for optional input parameters.
02evaluateChatAdmission02: Admission CheckFilters suppressed messages, checks permissions, and gates queue entry.
03handleChatDisposition03: Chat DispositionEmits terminal logging and notifies caller of rejected or non-run outcomes.
04runWithChannelLock04: Channel LockEnforces channel mutex, keeps typing active, and replays queued turns.
05planChatTurns05: Turn PlanningEvaluates triggers and quotas, selects personas, and queues multi-persona turns.
06Per-turn loop06: Per-Turn LoopRuns context build, response sink setup, generation, and post-turn effects.
  • Per-channel serialization: runWithChannelLock permits exactly one active turn sequence per channel at a time.
  • Queueing and interruption: messages arriving while a channel is busy are either enqueued for FIFO replay after lock release, converted to a follow-up interrupt if eligible, or ignored. A stop phrase from the active turn’s speaker kills that turn and clears the queue, as /kill does.
  • Re-entrant invocations:
    • Empty-response retries bypass the channel lock using skipLock=true to reuse the existing turn lock.
    • Cross-channel boomerang turns execute with suppressSelfReply: true via setImmediate after lock release.

The /troubleshoot chat command produces a private JSON diagnostic file for a user’s recent message in the current channel. It inspects in-memory traces recorded by diagnosticTimeline.ts across the past hour.

The diagnostic file records UTC timestamps, relative millisecond offsets, admission results, planned persona turns, context message counts, attempt model labels, tool iterations, and Discord send events. It excludes raw message content, prompts, user IDs, and raw channel IDs. The in-memory trace store retains at most 5,000 traces for one hour.

The timeline records typing_deferred when a spontaneous turn skips eager typing and typing_started when the initial typing request succeeds. A spontaneous turn may finish without requesting typing. It also records context_built after context assembly (history REST duration, reply fetch count, participant preparation duration, and native build duration), and provider_first_chunk on the first stream progress callback for each provider call. The callback runs when a chunk reaches the stream consumer, before chunk processing, and refreshes the stream watchdog.

log.metric("chat_turn_latency", fields) emits once when each started persona turn ends, including failed turns and bot or webhook triggers without a diagnostic owner. Admission refusals and plans with no persona turns emit no sample. A queued replay or persona job is a separate invocation; personas in one invocation share its start, admission duration, and planning duration. The async scope isolates concurrent channels and stops accepting updates when its turn ends.

All durations use performance.now() except gateway_ms, which compares wall clocks. Every sample has the same fields. An unstarted duration or an unreached milestone is -1; counts and summed image time start at zero. A failed stage that started still records its elapsed time. Durations overlap and must not be added to reconstruct total latency.

NumberMeaning
gateway_msInvocation entry wall time minus Discord’s trigger createdTimestamp; includes upstream delay and clock skew and can be negative.
admission_msTime in evaluateChatAdmission.
plan_msTime in planChatTurns.
context_msThis persona’s full context build, including intent preflight.
dispatch_msInvocation entry to the first streamToDiscord adapter entry in this turn. Later personas include time spent on earlier personas.
provider_ttfb_msFirst adapter entry to its first stream progress callback; -1 if that call produces no progress, even if a fallback later succeeds.
first_message_msInvocation entry to the first confirmed send recorded by recordChatMessageSent, including held response delivery.
history_fetch_msSum of channel history messages.fetch durations.
assets_msPersona emoji, sticker, and custom expression loading.
history_build_msThe whole buildSimplifiedHistory call, including history_fetch_ms and reply fetches.
participant_msThe prepareParticipantContext call; hydration inside buildContext belongs to native_ms.
participant_candidate_msCandidate-source reads during fresh participant discovery, including repository work and retries.
participant_members_msWall time for the concurrent member-resolution batch, including cache hits and failed lookups.
participant_proxy_msProxy-identity source reads during fresh participant discovery.
participant_composition_msDiscovery-plan composition for the current persona, including request-cache reuse.
participant_candidate_readsCandidate-source read attempts.
participant_member_cache_hitsMember lookups present in the discovery’s initial member-cache snapshot.
participant_member_fetchesMember lookups absent from that snapshot; a concurrent cache fill can still avoid REST.
participant_member_missesMember lookups that returned no member, such as a user who left the guild or never joined it.
participant_nickname_missesMisses on candidates admitted only by the saved-nickname branch: absent from the candidate ID list and without server activity.
participant_nickname_substring_fetchesLookups for nickname-branch candidates whose saved nickname appears in the history only inside a longer word, so its alias cannot match.
participant_nickname_substring_referencesThose candidates that still became referenced participants through another alias.
participant_fetched_referencesReferenced users whose member was absent from the initial cache snapshot.
participant_proxy_readsProxy-identity source read attempts.
naming_msThe history naming projection.
reunion_msReunion note resolution; -1 when a borrowed trigger skips it.
native_msSum of buildContext durations, including native assembly, participant hydration, RAG, and any preset reassembly.
reaction_fetchesCalls to reaction.users.fetch during metadata reveal; ordinary turns use cached reaction counts and record zero.
reply_fetchesReply target REST calls in withReplyContext, after history-window and message-cache misses, including failed calls.
image_fetchesNetwork download attempts at the safeDownload seam in fetchAndOptimizeImage, including failed downloads but excluding data URIs and policy-blocked results.
image_msSum of those download durations, through body download and excluding image optimization; overlapping downloads each contribute their wall time.

The dispatch boundary precedes adapter-side image preparation and the provider HTTP request. Image download time therefore falls after dispatch_ms and inside provider_ttfb_ms when the first call downloads images. The download counter lives immediately around safeDownload in imageProcessor.ts, so a cache returning before that seam skips both the counter and download timing. It counts download operations, rather than individual redirect requests. No URLs, message text, model names, or endpoint identifiers enter this metric.

DimensionFixed values and meaning
provideranthropic, custom, deepseek, google, novelai, nvidia, openrouter, vertex, vertexexpress, zai, zaicoding, or unknown; the first dispatched provider, otherwise the context’s planned provider when known. Custom endpoint IDs collapse to custom.
humanizernone, light, medium, or heavy when the toggles match that /setup preset, custom for any other combination, or unknown before context configuration is available.
sourcemessage, queue, persona_job, retry, manual, or stop, using the diagnostic timeline’s source precedence.
ragyes when built context contains server document retrieval content, otherwise no; it does not claim every retrieval attempt injected content.
historyreused for a busy-stretch window, otherwise rest.
participant_discoveryfresh when this turn starts discovery, reused when it awaits a request-cached discovery, or unmeasured before preparation reaches the cache lookup.
draftingyes when response review enabled holding response text during this turn, otherwise no.

For example, compare dispatch_ms for the same provider and source before inspecting the stage and download fields for the source of a change. context_ms minus the context stage fields (assets_ms, history_build_ms, participant_ms, naming_ms, reunion_ms, native_ms) leaves intent preflight, forced mentions, channel prompt lookups, sprite checks, and prefill.

The four participant substages and their lookup counters record only while participant_ms is active, so later image-generation preparation cannot change them. Subtract these substages only from that parent when attributing participant work. Member timing covers the whole concurrent batch, so it does not sum overlapping lookup durations. Reused discovery leaves candidate, member, and proxy timings at -1 and their counts at zero. Its wait for an in-flight discovery remains inside participant_ms, and composition is measured again for the current persona. Failed stages record elapsed time; unreached stages remain -1. Exclude those sentinels when calculating a remainder.

  • src/events/messageCreate/tomoriChat.ts: main chat entry point and stage coordinator.
  • src/utils/chat/channelQueue.ts: channel locking, queue storage, and re-entry helpers.
  • src/utils/chat/diagnosticTimeline.ts: per-turn event recording and diagnostic trace store.
  • src/utils/chat/turnLatency.ts: independent per-turn latency accumulator and fixed metric shape.
  • src/commands/troubleshoot/chat.ts: troubleshooting diagnostic command implementation.