Skip to main content

Message Streaming Pipeline Overview

The useMessageStreaming pipeline is the thin, composable layer that turns a user's input into a streamed assistant response with products, metrics, and persisted context. It lives under app/chat/hooks/useMessageStreaming/pipeline.ts and is orchestrated by useMessageStreaming in index.ts.

Purpose

The pipeline serves as the bridge between the user interface and the backend orchestration system, handling:

  • Request preparation and payload construction
  • Server-Sent Events (SSE) streaming
  • Real-time UI state updates
  • Product and metric management
  • Context preservation across conversations
  • Error handling and resilience

Architecture Position

Core Responsibilities

1. Request Management

  • Build and validate payloads
  • Attach conversation context
  • Manage exclude lists for "show more" functionality
  • Include last search parameters

2. Streaming Control

  • Establish SSE connections
  • Parse incoming events
  • Handle timeouts and errors
  • Manage request lifecycle

3. State Synchronization

  • Update UI in real-time
  • Manage product cards
  • Display metrics and context pills
  • Control skeleton loading states

4. Persistence

  • Save user messages (fire-and-forget)
  • Persist assistant responses with metadata
  • Store context data for future requests

Key Files

app/chat/hooks/useMessageStreaming/
├── index.ts # Main hook orchestrator
├── pipeline.ts # Core pipeline logic
├── parser.ts # SSE event parsing
└── config.ts # Configuration constants

Integration Points

  • Context System: Receives and preserves contextData for pills
  • Product Management: Handles product cards and skeletons
  • Metrics System: Captures and displays performance metrics
  • Smart Suggestions: Processes and displays followup actions
  • Error Handling: Manages timeouts and fallback states

Performance Targets

MetricTargetPurpose
TTFC (Time to First Chunk)<800msUser sees immediate response
Request Timeout30sCatch hung requests
Inactivity Timeout45sHandle stalled streams
Skeleton Delay160 chars or 800msShow product placeholders