Building a Streaming AI Chat App in Vue 3: A Code Reduction Case Study
This article walks through rebuilding a full-featured streaming AI chat application in Vue 3, using a state-model-driven architecture instead of a component-first approach. It covers the protocol, state, and view layers, the branching conversation tree, multi-segment streaming, citation tracing, multimodal input, voice, and history portability. It also explains how derived state and a unified capability registry reduced production source code by roughly 27%, along with practical rollout and observability advice.
1. Background: Why “Chatting” Is Far From “Usable”
A demo page with just an input box and a text streaming endpoint can only verify that the pipeline is connected. It cannot support real usage. Once an application needs multi-turn follow-ups, traceable conclusions, cross-modal input, and portable history, complexity quickly grows from a “single request” into a “stateful frontend system.”
A complete application of this kind usually has four characteristics:
- The browser connects directly to the API, so the frontend handles protocol assembly, incremental rendering, and state management.
- A conversation is not a linear message list but a retrievable branch tree.
- Output consists of multiple streams: reasoning, main content, and citations.
- History data must be portable and migratable.
These four points mean the architecture must be centered on the state model, not on components.
2. Technology Choices: Layering Driven by the State Model
During the rebuild, the first step is to define three layer boundaries:
- The protocol layer handles request assembly and incremental parsing.
- The state layer handles the conversation tree, node status, and derived data.
- The view layer only reads derived state and dispatches intents.
This separation keeps streaming protocol changes from polluting the view, and keeps view adjustments from breaking the history structure.
Illustrative code (core state structure):
// Message node: one answer may contain multiple streams and branchesconst node = {id, parentId, children: [],segments: { reasoning: '', content: '', citations: [] },status: 'streaming' // pending | streaming | done | error}
Derived data such as the current path, the list of available branches, and whether a follow-up can continue is produced through computed properties. This avoids stacking conditions in templates and is the key prerequisite for reducing code later.
3. Key Points for Rebuilding Core Capabilities
- Incremental streaming rendering: update state by segment rather than by character to reduce unnecessary renders; merge increments and trigger a single view update.
- Branch tree interaction: any node can serve as the start of a new branch; switching branches only changes the “current path” pointer, while history nodes remain immutable.
- Deep thinking and search while thinking: model the reasoning segment, retrieval segment, and main content segment as ordered parts of the same answer, so the frontend only appends them in order.
- Citation tracing: map citation markers in the main content to the source list, so clicking a marker locates the corresponding source entry.
- Multimodal input and file backfill: validate and preview files locally first, then fill in missing attachments based on the conversation context to avoid duplicate uploads.
- Voice pipeline: voice input and streaming read-aloud share the same “segment queue” abstraction, aligning playback progress with text segments.
- History archiving: export a versioned archive, and on import perform version compatibility and structure validation to keep it usable in both directions across implementations.
4. Going Beyond the Rebuild: Tool Connections and Instruction Management
Two new capabilities are added to the bottom-left entry: call management for external tool connections, and reusable instructions (imported as local documents, directories, or remote repositories). In implementation, they are abstracted as “registerable capabilities” decoupled from the conversation state: a registry describes the name, input schema, and invocation method, and the UI only renders the registry contents.
// Unified description in the capability registry{ name, description, inputSchema, invoke }
The benefit is that adding a capability requires no changes to the conversation tree logic, and it also makes permission prompts and failure fallback easier.
5. Why the Code Volume Dropped by About 27%
Under a snapshot with a unified formatting standard, production source code dropped from about 18,412 lines to about 13,368 lines, a net reduction of about 5,044 lines, including the newly added capabilities. There are three main reasons: first, the component library and runtime suite handle rendering, virtual scrolling, and streaming segment management; second, derived state replaces a large amount of conditional branching and manual synchronization; third, unified formatting and static checks remove stylistic redundancy. Note that such figures depend on the counting method, and local styles and edge details may still vary in engineering practice.
6. Rollout Advice and Observability
Start with a minimal closed loop connecting “protocol, state, and rendering,” then gradually add branches, citations, voice, and archiving; define a version number and migration functions for archive packages; and add common logging and monitoring alert categories on the browser side, focusing on time to first token, rendering frame rate, and error rate. For team collaboration, put static assets in object storage and use a container platform to host the build pipeline for reproducible delivery.
