Skip to content

Component Frontend App

Reese Hodge edited this page Nov 21, 2026 · 5 revisions

Component Frontend App

The frontend app is a React + Vite single-page shell that renders the TV cabinet experience, owns local TV power/theme/volume state, and wires audio, greeting playback, state stores, and SSE runtime send paths together. See also Component-CRT-Presentation, Component-Audio-Chain, Component-WebSocket-Transport, and Component-State-Stores.

Files

  • packages/frontend/src/main.tsx mounts React into #root, wraps App in StrictMode, and catches render failures with ErrorBoundary.
  • packages/frontend/src/App.tsx composes the TV scene, power sequence, greeting lifecycle, channel/theme switching, text submission, audio chain, and SSE stream hook.
  • packages/frontend/src/config/constants.ts defines avatar themes, greeting duration, background timing, tuning duration bounds, and tune-in glitch pattern.
  • packages/frontend/src/config/buildConfig.ts owns build-time runtime and Cognito configuration.
  • packages/frontend/src/services/authService.ts owns the Cognito auth service and runtime ARN.
  • packages/frontend/src/hooks/useAgentStream.ts posts chat turns and no-session actions over SSE.
  • packages/frontend/src/components/ErrorBoundary.tsx shows an in-character fallback instead of a blank React crash.
  • packages/frontend/src/App.css positions the TV frame, controls, screen, chat bar, overlays, and LED styles.

Exported functions and types

  • GREETING_DISPLAY_MS, AVATAR_THEMES, DEFAULT_AVATAR_THEME, BACKGROUND_CYCLE_MS, BACKGROUND_FRAME_COUNT, BACKGROUND_ASSET_BASE, TUNING_MIN_MS, TUNING_MAX_MS, SETTLING_DURATION_MS, and TUNE_IN_GLITCH_PATTERN are exported constants.
  • type AvatarTheme = 'retro' | 'pop-art' | 'cartoon'
  • nextTheme(theme: AvatarTheme): AvatarTheme
  • getRandomTalkFrameIntervalMs(): number
  • getRandomTalkFrameIndex(): 0 | 1
  • type TvPowerState = 'off' | 'tuning' | 'settling' | 'on'
  • App(): JSX.Element
  • class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState>

Composition and data flow

main.tsx requires a root element and throws if it is missing. It renders:

<StrictMode>
  <ErrorBoundary>
    <App />
  </ErrorBoundary>
</StrictMode>

App creates one AudioChain and one greeting player (createUseGreeting()) through lazy useState initializers, builds the voice player (createUseAudio()) in a mount effect, and disposes the player and the chain on unmount. The visible TV has four power phases:

  • off: screen and chat input disabled.
  • tuning: TuningOverlay and looping white noise run while greeting audio preloads.
  • settling: avatar/background appear and TUNE_IN_GLITCH_PATTERN can force glitch/idle frames.
  • on: greeting playback starts, a no-model runtime.ready SSE action starts concurrently, text input can send messages.

Greeting completion is the startup deadline: a successful readiness action leaves the connected idle/listening state; a failure or unresolved probe becomes SIGNAL_LOST after the greeting completes. Power-off aborts the probe, and a late result cannot change state.

The scene inside CrtFrame is ordered as background, avatar, broadcast text, session overlay, settling glitch overlay, and tuning overlay. The right-side panel contains TvKnob, ChannelKnob, and VolumeKnob. The TextInput lives below the TV in .chat-bar, not in the CrtFrame footer.

Store and service connections

  • useConnectionStore supplies sessionId and sessionState.
  • useConversationStore supplies streaming currentResponseText, completed fullText, and currentTurnIndex.
  • useVoiceStore supplies isMouthOpen and isGreeting for avatar animation.
  • useAgentStream({ runtimeArn, getAccessToken, refreshAccessToken }) opens an SSE request only when send() or runAction() is called.
  • On tvPower === 'on', App calls runAction({ action: 'runtime.ready' }, powerOffSignal) once so local greeting audio and runtime bootup overlap.
  • handleSend(text) sends { message, clientTimestamp, voice: 'on' } through stream.send() only when the trimmed message is non-empty and the session state is not one of SEND_BLOCKED_STATES (ENDED, BUDGET_CAPPED, RATE_LIMITED, DENIED, ERROR; spec 003 FR-037). See the first Gotcha below for why it is not an ACTIVE check.

toOverlayState(state, isGreetingDone) hides terminal overlays until greeting finishes, then maps ENDED, BUDGET_CAPPED, RATE_LIMITED, SIGNAL_LOST, and ERROR to SessionStateOverlay.

Gotchas

  • Never gate sending on ACTIVE. Over SSE a session reaches ACTIVE only when the first session event arrives, which is a response to a send, so a gate on ACTIVE makes the first message unsendable. App.test.tsx once hid this by mocking the store as ACTIVE; the tests now use the real store from INITIALIZING and SIGNAL_LOST. A SIGNAL_LOST session can therefore retry by sending (found in the 2026-11-16 branch review).
  • Never replace a persistent startup transport with request-only SSE and leave startup readiness implicit. SSE runAction() exists for memory/actions and chat send() exists for turns, but neither opens a connection at power-on. App now explicitly races runtime.ready against greeting completion and aborts it on power-off; unresolved or failed readiness becomes SIGNAL_LOST only after the greeting.
  • useRef(createX()) re-runs createX() on every render, because the argument is evaluated each time and only the first result is kept. App once did this for the audio chain, voice player and greeting player, orphaning a set of store subscribers per render. Use a lazy useState(() => createX()). The voice player is built in a mount effect instead, because useAudio().dispose() is permanent (it unsubscribes for good) and <StrictMode> simulates an unmount and remount, which a lazily initialized player would not survive. AudioChain.dispose() is re-initable, so it can live in a state initializer.
  • The visible volume is local App state (useState(6 / 11)), not voiceStore state; TV power cycles preserve the selection. Component-Volume-Knob covers the current implementation.
  • Channel change stops greeting audio and cycles avatar theme, but it does not reset conversation or connection stores.
  • Session terminal states power the TV off for ENDED, BUDGET_CAPPED, RATE_LIMITED, and ERROR, but not for SIGNAL_LOST; the signal-lost overlay is displayed while powered on.

Clone this wiki locally