Skip to content

The agent that grows with you

6.5 KB
Raw Blame
// Loaded by src/entry.ts once the machine's shared UI preferences hydrated.
import './styles.css'
// Install the loopback browser host before any store/App modules evaluate.
// Electron preload remains authoritative when its bridge already exists.
import './lib/browser-desktop-bridge-install'
// Side-effect: reports in-flight turns to the main process for the quit guard.
import './store/active-work'
// Side-effect: mirrors the machine's AC/battery state for poll demotion.
import './store/power'
// Side-effect: applies the persisted window translucency on load.
import './store/translucency'
// Side-effect: applies the persisted user-bubble transparency on load.
import './store/user-bubble-transparency'
// Side-effect: restores chat typography before the first conversation paints.
import './store/chat-text-scale'
// Side-effect: mirrors the keep-awake mode to main and listens for main's
// one-time keep-awake offer. Settings loads lazily, too late for the offer.
import './store/keep-awake'
// Dev-only render/state churn counters. MUST precede the `react-dom` import
// below: react-dom captures the devtools hook at module init, so bippy has to
// install during THIS import's evaluation or every commit goes unseen
// (verified — a late install reports renderers=0, commits=0). `vite.config.ts`
// aliases this specifier to a no-op module for non-dev builds, so neither the
// counters nor bippy reach a shipped renderer.
import '@/debug/dev-only'

import { QueryClientProvider } from '@tanstack/react-query'
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { HashRouter } from 'react-router'

import App from './app'
import { RootErrorBoundary } from './components/error-boundary'
import { HapticsProvider } from './components/haptics-provider'
import { RootTooltipProvider } from './components/ui/tooltip'
import { ProfileI18nProvider as I18nProvider } from './i18n/profile-provider'
import { installClipboardShim } from './lib/clipboard'
import { queryClient } from './lib/query-client'
import { installRendererAnimationPauseState } from './lib/renderer-loop-pause'
import { installSelectionCopyColorGuard } from './lib/selection-copy-colors'
import { isTransparentWindow } from './lib/transparent-windows'
import { startUiPreferencesSync } from './lib/ui-preferences/sync'
import { installBrowserTabTitle } from './store/browser-tab-title'
import { ThemeProvider } from './themes/context'

installClipboardShim()
// Chromium serializes selection copies (Cmd+C, right-click Copy) with the
// theme's computed colors inlined; without this guard a dark-theme selection
// pastes as near-white text into light-background targets.
installSelectionCopyColorGuard()
// Browser host only: the tab carries session status while the user is away.
installBrowserTabTitle()
// One preferences store per machine, shared by native Desktop and its Webapp (#145213).
startUiPreferencesSync(window.hermesDesktop?.uiPreferences)

// The perf probe ships in dev, and in a production build ONLY when explicitly
// opted in (VITE_PERF_PROBE=1) — this lets the perf harness measure a real,
// minified production renderer for representative absolute numbers. Normal
// `npm run build` leaves the flag unset, so the probe never reaches users.
if (import.meta.env.MODE !== 'production' || import.meta.env.VITE_PERF_PROBE === '1') {
  import('./app/chat/perf-probe')
}

const winParam = new URLSearchParams(window.location.search).get('win')

if (winParam === 'hud') {
  document.title = 'Hermes HUD'
}

// Each transparent root used to force its host layers see-through when it
// MOUNTED. That is far too late: `styles.css` above paints the theme's opaque
// `--background` as soon as it lands, and the root behind it is a dynamic
// import — a couple of seconds of module fetches under the dev server. The gap
// rendered as a full-screen near-white rectangle. Claim it here instead, in the
// same task as the stylesheet, so no window ever paints a background it does
// not want.
if (isTransparentWindow()) {
  const transparent = document.createElement('style')

  transparent.textContent = 'html,body,#root{background:transparent !important;}'
  document.head.appendChild(transparent)
}

if (winParam === 'overlay') {
  void import('./app/pet-overlay/overlay-root').then(({ mountPetOverlay }) => mountPetOverlay())
} else if (winParam === 'quick') {
  void import('./app/quick-entry/quick-entry-root').then(({ mountQuickEntry }) => mountQuickEntry())
} else if (winParam === 'wake') {
  void import('./app/wake-indicator/wake-indicator-root').then(({ mountWakeIndicator }) => mountWakeIndicator())
} else {
  // CSS animations do not inherit Chromium's JS-loop pause policy. Mirror the
  // main window's visibility state to :root so decorative infinite
  // animations stop producing frames when nobody can see them.
  installRendererAnimationPauseState()

  createRoot(document.getElementById('root')!).render(
    <StrictMode>
      <RootErrorBoundary>
        <QueryClientProvider client={queryClient}>
          <I18nProvider>
            <ThemeProvider>
              <HapticsProvider>
                {/* ONE tooltip provider for the whole app. Every `Tip` used to
                    carry its own, and with ~107 call sites those subtrees
                    dominated unrelated interactions (52,784 TooltipProvider
                    renders in a single sash drag). Radix's provider holds only
                    refs and stable callbacks, so hoisting is what it's for. */}
                <RootTooltipProvider>
                  {/* useTransitions={false}: react-router v7's HashRouter wraps every
                    route state update in React.startTransition() by default. In
                    React 19's concurrent renderer, transitions are non-urgent — React
                    can yield mid-render and resume later. When the app is under load
                    (streaming token deltas, gateway events, store updates), those
                    higher-priority updates keep interrupting the transition, starving
                    the route change commit. The session sidebar highlight + main pane
                    both freeze for seconds despite the main thread being free.
                    Disabling transitions makes navigate() commit at default priority. */}
                  <HashRouter useTransitions={false}>
                    <App />
                  </HashRouter>
                </RootTooltipProvider>
              </HapticsProvider>
            </ThemeProvider>
          </I18nProvider>
        </QueryClientProvider>
      </RootErrorBoundary>
    </StrictMode>
  )
}