Rebuilding a Complex AI Chat Application in Vue 3: Architecture and Code Reduction Practices
This article shares a practical engineering case of rebuilding a complex AI chat application with Vue 3. It explains why chat front ends are far more complex than a simple input box, how to choose a runtime-first architecture with pluggable capabilities, and how three rounds of architectural evolution led to a more extensible design. It also covers key implementation points for streaming, message branching, citations, multimodal attachments, voice, and versioned archives, along with three reusable methods that reduced production source code from over 18,000 lines to about 13,000 lines while adding more features.
1. Background: Why Chat Applications Are More Complex Than They Look
Many teams still think of an AI chat front end as “an input box plus a text streaming API call.” In real production scenarios, however, a deliverable chat application has to solve at least six categories of problems at the same time: streaming rendering with interruption and recovery, context and message organization, segmented display of reasoning and final answers, citation and source tracing for external retrieval results, upload and re-upload of multimodal attachments, and the real-time pipeline for voice input and streaming read-aloud.
When one cloud vendor open-sourced its official web demo, it put all of these capabilities into a single front-end project that connects directly to the API from the browser. The production source code reached more than 18,000 lines. That number itself shows the complexity does not come from “calling the model”; it comes from state management, protocol compatibility, interaction details, and the long-term evolution of historical data structures.
This raised an engineering question worth validating: while keeping core functionality 100% aligned, could rebuilding with a more mature front-end paradigm significantly reduce the code size and leave room for future capabilities?
2. The Core Technical Decision: Push Complexity into the Runtime, Not into Business Code
The most common mistake when rebuilding this kind of application is “build the pages first, then patch in the state.” The correct order is the reverse: define the runtime and component abstractions first, then let business code handle only orchestration.
In practice, I chose a Vue 3-based chat component library and runtime suite for three main reasons:
- The message model is a tree rather than a linear array, so the component library must natively support branches, replay, and node-level state.
- The streaming protocol needs a pluggable parsing layer to keep transport details out of the views.
- Capabilities such as attachments, voice, and citations should be mounted as “capability plugins” instead of being scattered across page logic.
The following is an illustrative runtime setup:
// Assemble the runtime with capability plugins; the business layer only orchestratesconst runtime = createChatRuntime({transport: sseTransport(), // streaming transport parsingmessageTree: true, // enable the branch treecapabilities: [reasoning(), // segmented deep reasoningretrieval({ citations: true }),// web retrieval with citationsmultimodal(), // file and image attachmentsvoice({ streamingTTS: true }) // voice input and streaming read-aloud]})
The key benefit of this approach is that adding a capability keeps changes inside the plugin. The view layer barely changes, and the code increment stays controllable.
3. Three Rounds of Architectural Evolution: From “It Runs” to “It Scales”
Round 1: Direct connection and a minimal loop. The goal was to get basic interactions working—streaming chat, stop generation, retry, and copy—and to validate protocol parsing and incremental rendering. This stage deliberately avoided abstractions to prevent premature design.
Round 2: Decoupling state from views. The message tree, conversation list, and attachment queue were split into independent stores, while views only subscribed and dispatched. Code size dropped noticeably here because a large amount of duplicated local state was consolidated.
Round 3: Capability pluginization and archive compatibility. Retrieval, reasoning, voice, and file handling were all turned into plugins, and the historical archive structure was unified so that export packages could be imported and exported bidirectionally with different implementations.
There is a counterintuitive pitfall worth emphasizing: after introducing an “advanced suite” in Round 2, the code size actually increased instead of decreasing. The reason was that the suite’s default capabilities overlapped with my existing custom logic, creating two parallel implementations. The solution was not to add more abstraction but to subtract: delete the self-built duplicate modules and keep only the business differences the suite did not cover. The gains from architectural evolution often come from “deleting,” not “adding.”
4. Key Implementation Points
Streaming and interruption. Incremental rendering must be driven by stable keys to avoid re-rendering the entire list. On interruption, preserve the generated fragments and mark the state so that retries can locate the continuation point.
Message branch tree. Every regeneration by the user creates a new branch, so node-level parent-child pointers and a “current active path” are required. Switching history is essentially switching paths, not copying data.
Citation tracing. Retrieval results must establish a traceable mapping to answer fragments. The front end keeps an index from citation numbers to sources, renders superscript markers, and lets clicks locate the original text.
Multimodal attachments. Upload and “re-upload” must be separated: reserve a placeholder first, then fill in the content, with retries on failure. For oversized files, use chunking or direct upload to object storage, and the front end holds only a reference identifier.
Voice pipeline. Voice input relies on streaming recognition callbacks to overwrite and render intermediate results. Streaming read-aloud splits text by sentence boundaries and queues playback as generation proceeds, avoiding waiting for the whole segment.
History archives. Archives must be versioned with backward-compatible fields. Import should perform structure validation and migration so that different implementations can exchange data.
5. Why the Code Size Dropped: Three Reusable Methods
First, unify formatting and counting standards. Line-count comparisons are meaningful only under the same formatting rules; otherwise, differences may come only from line-break style.
Second, replace imperative code with declarative code. Once structures such as the branch tree and attachment queue are described declaratively, boilerplate code is greatly reduced.
Third, reuse through capability pluginization. The same capability is shared across pages, so the marginal cost approaches zero.
Under the same counting standard, production source code was compressed from more than 18,000 lines to about 13,000 lines, a net reduction of roughly 5,000 lines, or nearly 30%, and this includes the newly added tool-connection and command management capabilities. This shows that “more features with less code” is not a contradiction but the result of better abstraction quality.
6. Actionable Engineering Recommendations
- Define the protocol and message model before building the UI to avoid late-stage refactoring.
- Assign version numbers to archive structures and write migration tests.
- For streaming scenarios, always design a state machine for interruption and retry.
- Prioritize pluginization for new capabilities, and never stack conditional branches in views.
- If introducing an external suite causes code bloat, subtract first instead of adding more wrappers.
In summary, the competitiveness of a complex AI chat application lies not in “what it integrates” but in the rigor of state and protocol governance. Only by carrying complexity in the runtime, extension in plugins, and evolution in versioning can you deliver more complete functionality with less code. If you need to support large-scale conversations and files, you can combine general cloud capabilities such as object storage, message queues, container platforms, and log monitoring for a layered deployment.
