What problem does it solve? Building an AI agent chat frontend requires coordinating transient UI state (active thread, streaming messages, sidebar) with server data (threads, messages, agent configs), and mixing these concerns causes re-render storms, stale caches, and duplicated state. ## Core Features & Use Cases - Store-per-domain Zustand design: Separate threadStore, uiStore, and agentBuilderStore with immer and persist middleware, plus granular selector patterns to prevent unnecessary re-renders during streaming. - TanStack Query server state: Query client configuration, infinite thread history pagination, and structured query key factories for agents and threads. - Optimistic updates and cache invalidation: Optimistic thread creation and message sending with rollback on error, plus a cache invalidation matrix for common events. - Use Case: When building a chat page that streams assistant tokens over SSE, use the useAgentThread composite hook to append tokens to the TanStack Query cache while Zustand tracks only the streaming message ID, so sidebar components never re-render. ## Quick Start Ask the AI to design the client-side state layer for an agent chat frontend using Zustand stores and TanStack Query with optimistic thread creation.