Skip to content

The agent that grows with you

11.7 KB
Raw Blame
import { type GatewayEvent, registryBackendScopeKey } from '@hermes/shared'
import { useCallback, useEffect, useRef } from 'react'

import type { GatewayEventPayload } from '@/lib/chat-messages'
import { acceptExecutionEvent } from '@/lib/execution-authority'
import {
  approvalReplaySessionId,
  resolveGatewayEventSessionId,
  UNSCOPED_STREAM_EVENT_TYPES
} from '@/lib/gateway-events'
import { reconcileSessionCompacting } from '@/store/compaction'
import { $gateway, activeGatewayConnectionId } from '@/store/gateway'
import { $activeGatewayProfile, normalizeProfileKey } from '@/store/profile'
import { replayPendingApproval } from '@/store/prompts'
import { setSessionProviderWait } from '@/store/provider-wait'
import { isSessionGone } from '@/store/session-gone-latch'
import { noteSessionEvent } from '@/store/session-states'
import { setSessionDraftingTool } from '@/store/tool-drafting'

import { handleDeferredPickEvent } from './deferred-pick'
import { handleDesktopBridgeEvent } from './desktop-bridge'
import { handleFreeTierEvent } from './free-tier'
import { handleInputRequestEvent } from './input-requests'
import { handleLifecycleEvent } from './lifecycle'
import { handleMessageStreamEvent } from './message-stream'
import { handleOnboardingEvent } from './onboarding'
import { handleControlEvent } from './session-control'
import { handleSessionInfoEvent } from './session-info'
import { handleStatusEvent } from './status'
import { handleToolEvent, handleToolProgressEvent, retireTurnToolProgress } from './tools'
import type { GatewayEventContext, GatewayEventDeps, GatewayEventHandler } from './types'

export type { GatewayEventDeps } from './types'

/**
 * Events that retire a "drafting a tool call" claim.
 *
 * `tool.generating` opens the claim and nothing closes it — a draft can be
 * abandoned without ever reaching `tool.start`, so enumerating the ways one
 * *ends* left the label on screen for the rest of the turn. Inverted: the
 * claim only covers what the model is emitting right now, and any other output
 * from the session proves it moved on. Same rule the TUI applies to its
 * transient trail lines (`turnController.pruneTransient`).
 */
const DRAFT_SUPERSEDING_EVENT_TYPES = new Set([
  'error',
  'message.complete',
  'message.delta',
  'message.start',
  'reasoning.delta',
  'thinking.delta',
  'tool.complete',
  'tool.start'
])

const COMPACTION_RESUME_EVENT_TYPES = new Set([
  'message.delta',
  'message.interim',
  'thinking.delta',
  'reasoning.delta',
  'reasoning.available',
  'moa.reference',
  'moa.aggregating',
  'moa.progress',
  'moa.phase',
  'tool.start',
  'tool.generating',
  'tool.complete'
])

const PROVIDER_WAIT_SUPERSEDING_EVENT_TYPES = new Set([
  'error',
  'message.complete',
  'message.delta',
  'message.interim',
  'message.start',
  'reasoning.available',
  'reasoning.delta',
  'tool.complete',
  'tool.generating',
  'tool.start'
])

// Ordered family handlers; each consumes its own event types and reports
// whether it did, so dispatch stops at the first taker.
const HANDLERS: GatewayEventHandler[] = [
  handleLifecycleEvent,
  handleFreeTierEvent,
  handleOnboardingEvent,
  handleSessionInfoEvent,
  handleDeferredPickEvent,
  handleControlEvent,
  handleMessageStreamEvent,
  handleToolProgressEvent,
  handleToolEvent,
  handleInputRequestEvent,
  handleDesktopBridgeEvent,
  handleStatusEvent
]

type ExecutionAuthorities = Parameters<typeof acceptExecutionEvent>[0]

/** The event's own timestamp (epoch seconds) when finite, else now. */
function gatewayEventOccurredAt(payload: GatewayEventPayload | undefined): number {
  return typeof payload?.timestamp === 'number' && Number.isFinite(payload.timestamp)
    ? payload.timestamp
    : Date.now() / 1000
}

/** Fence the event against the session's claimed execution. Returns false
 *  when a stamped late frame must be dropped; an accepted newer owner
 *  start/snapshot retires the previous execution's interrupted latch. */
function admitExecutionEvent(
  authorities: ExecutionAuthorities,
  event: GatewayEvent,
  sessionId: null | string,
  deps: GatewayEventDeps
): boolean {
  const authorityKey = `${registryBackendScopeKey(event.connectionId ?? null, event.profile ?? null)}\u0000${sessionId}`

  const previousAuthority = authorities.get(authorityKey)

  if (sessionId && !acceptExecutionEvent(authorities, authorityKey, event.type, event)) {
    return false
  }

  const authority = authorities.get(authorityKey)

  if (
    sessionId &&
    previousAuthority &&
    authority &&
    !authority.terminal &&
    (authority.epoch !== previousAuthority.epoch || authority.generation > previousAuthority.generation)
  ) {
    // Stop belongs to the cancelled execution, not the shared session.
    // Only an accepted newer owner start/snapshot may retire its latch.
    deps.updateSessionState(sessionId, state => (state.interrupted ? { ...state, interrupted: false } : state))
  }

  return true
}

/** The gateway-event dispatcher, extracted from useMessageStream. */
export function useGatewayEventHandler(deps: GatewayEventDeps) {
  const executionAuthorities = useRef(new Map())
  const { activeSessionIdRef, compactedTurnRef, refreshHermesConfig, sessionStateByRuntimeIdRef } = deps

  // One pin per concurrent unscoped stream, not a single shared slot: two chats
  // streaming at once used to clobber each other's pin (#46194 / #62823).
  const unscopedStreamSessionIdsRef = useRef<readonly string[]>([])

  // session.info arrives in bursts (agent build ready + turn end + title /
  // MCP / compress edges within the same second). Each used to fire its own
  // refreshHermesConfig — two REST calls (config + defaults) per event, per
  // turn, including for BACKGROUND sessions whose values the fetch can't even
  // apply. Coalesce to one trailing fetch per burst; the caller gates on
  // `apply` so background traffic doesn't schedule anything.
  const configRefreshTimerRef = useRef<null | number>(null)

  const scheduleConfigRefresh = useCallback(() => {
    if (configRefreshTimerRef.current !== null) {
      return
    }

    if (typeof window === 'undefined') {
      void refreshHermesConfig()

      return
    }

    configRefreshTimerRef.current = window.setTimeout(() => {
      configRefreshTimerRef.current = null
      void refreshHermesConfig()
    }, 300)
  }, [refreshHermesConfig])

  useEffect(
    () => () => {
      if (configRefreshTimerRef.current !== null && typeof window !== 'undefined') {
        window.clearTimeout(configRefreshTimerRef.current)
        configRefreshTimerRef.current = null
      }
    },
    []
  )

  return useCallback(
    (event: GatewayEvent) => {
      const payload = event.payload as GatewayEventPayload | undefined

      // "From the active profile" must mean "from the active SOURCE": every
      // registered connection exposes a 'default' profile, so a bare profile
      // comparison attributes gateway B's 'default' events to gateway A's
      // 'default'. Compare the composite (connectionId, profile) scope with
      // registryBackendScopeKey — untagged primary events keep the legacy
      // bare-profile behavior byte-identical.
      const fromActiveSource = (): boolean =>
        (!event.profile || normalizeProfileKey(event.profile) === normalizeProfileKey($activeGatewayProfile.get())) &&
        registryBackendScopeKey(event.connectionId ?? null, event.profile ?? null) ===
          registryBackendScopeKey(activeGatewayConnectionId(), event.profile ?? null)

      const occurredAt = gatewayEventOccurredAt(payload)

      const explicitSid = event.session_id || ''

      const route = resolveGatewayEventSessionId({
        activeSessionId: activeSessionIdRef.current,
        eventType: event.type,
        explicitSessionId: explicitSid,
        unscopedStreamSessionIds: unscopedStreamSessionIdsRef.current
      })

      unscopedStreamSessionIdsRef.current = route.nextUnscopedStreamSessionIds

      if (route.drop) {
        return
      }

      const sessionId = route.sessionId

      if (!admitExecutionEvent(executionAuthorities.current, event, sessionId, deps)) {
        return
      }

      // Late stragglers: an unscoped stream event attributed via the
      // active-session fallback (no pin) to a session that has no live turn
      // belongs to a turn that already ended elsewhere. Dropping it keeps the
      // previous session's tail events (a delayed `thinking.delta` or
      // `status.update`) from landing in a freshly opened chat (#43142 family:
      // busy/streaming UI inherited when switching sessions).
      if (
        sessionId &&
        !explicitSid &&
        !route.pinned &&
        event.type &&
        event.type !== 'message.start' &&
        UNSCOPED_STREAM_EVENT_TYPES.has(event.type)
      ) {
        const state = sessionStateByRuntimeIdRef.current.get(sessionId)

        const hasLiveTurn = Boolean(
          state && (state.awaitingResponse || state.busy || state.streamId || state.sawAssistantPayload)
        )

        if (!hasLiveTurn) {
          return
        }
      }

      const isActiveEvent = !!sessionId && sessionId === activeSessionIdRef.current

      const replaySessionId = approvalReplaySessionId(event.type, activeSessionIdRef.current, sessionId, {
        explicit: Boolean(explicitSid),
        isGone: isSessionGone
      })

      if (replaySessionId) {
        void replayPendingApproval($gateway.get(), replaySessionId).catch(() => undefined)
      }

      // Mid-turn compaction does not emit another message.start. The first
      // model output or tool event proves summarization has finished and the
      // turn has resumed, so retire the phase label without waiting for the
      // whole turn to complete.
      if (sessionId && COMPACTION_RESUME_EVENT_TYPES.has(event.type) && compactedTurnRef.current.has(sessionId)) {
        reconcileSessionCompacting(sessionId, 'resumed')
      }

      if (sessionId && DRAFT_SUPERSEDING_EVENT_TYPES.has(event.type)) {
        setSessionDraftingTool(sessionId, '')
      }

      if (sessionId && PROVIDER_WAIT_SUPERSEDING_EVENT_TYPES.has(event.type)) {
        setSessionProviderWait(sessionId, '')
      }

      const ctx: GatewayEventContext = {
        deps,
        event,
        payload,
        sessionId,
        explicitSid,
        isActiveEvent,
        occurredAt,
        fromActiveSource,
        scheduleConfigRefresh
      }

      try {
        for (const handler of HANDLERS) {
          if (handler(ctx)) {
            return
          }
        }
      } finally {
        // Any attributed event — including a heartbeat that does not change
        // state — proves this session is still producing. Silence after the
        // last one force-settles a dead turn, partial payload included.
        if (sessionId) {
          noteSessionEvent(sessionId)
          retireTurnToolProgress(event.type, sessionId)
        }
      }
    },
    // The deps object is rebuilt by the caller each render, but every field it
    // carries is stable (refs, useCallback-wrapped fns, queryClient), so
    // depending on the individual fields keeps the handler identity stable.
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [
      deps.appendAssistantDelta,
      deps.appendReasoningDelta,
      deps.activeSessionIdRef,
      deps.activeGatewayProfile,
      deps.compactedTurnRef,
      deps.completeAssistantMessage,
      deps.failAssistantMessage,
      deps.finalizeInterimAssistantMessage,
      deps.flushQueuedDeltas,
      deps.dropQueuedDeltas,
      deps.hydrateFromStoredSession,
      deps.lastCwdInfoSessionRef,
      deps.nativeSubagentSessionsRef,
      deps.queryClient,
      scheduleConfigRefresh,
      deps.scheduleSessionsRefresh,
      deps.sessionInterrupted,
      deps.sessionStateByRuntimeIdRef,
      deps.updateSessionState,
      deps.upsertToolCall
    ]
  )
}