Rebuilding an Open-Source AI Chat App in Vue 3: A Cost-Reduction Practice
This article walks through a practical refactor that reimplements a feature-rich open-source AI chat web app using Vue 3, with a focus on reducing code size without cutting functionality. It explains why a working demo falls short of a production app, how to turn "full replication" into a verifiable capability checklist, and how three rounds of iteration brought the production source down from over 18,000 lines to about 13,000. It also covers tool-call integration, instruction management, performance and maintainability practices, and compatibility requirements for bidirectional history archives. Readers will find concrete architectural patterns, a flattened message-tree example, and engineering recommendations for running similar migrations in their own products.
1. Background: Why a “Runnable” Demo Is Not a “Usable” App
Many teams have seen this pattern: a sample project with just an input box and a text-streaming endpoint demos well, but the moment it has to support real workloads, the gaps show up fast—nowhere to keep session state, no way to trace message versions, broken attachment and voice pipelines, and no path to migrate history. In a recently open-sourced official web example, a cloud vendor shipped a complete browser-to-API application: streaming output, deep thinking, web search, think-while-searching, web page reading, multimodal file upload, voice input, streaming voice playback, a full message branch tree, and ZIP-based history archive import/export—all contained in a single frontend project. The original was built with a mainstream frontend framework and a lightweight state container, and the production source alone exceeded 18,000 lines.
This sets up a typical refactoring challenge: without cutting features, can an equivalent implementation be built on a different view framework while also tightening engineering practices and shrinking the codebase?
2. Goal Breakdown: Turning “100% Replication” into a Verifiable Checklist
“Full replication” easily becomes a slogan without acceptance criteria. A practical approach is to build a capability matrix first, then align item by item:
- Protocol layer: request body structure, streaming chunk parsing, and termination and error semantics stay consistent;
- Session layer: the multi-branch structure of the message tree, node switching, and branch creation;
- Retrieval layer: citation numbering for search results, source cards, and the intermediate state of think-while-searching;
- Asset layer: chunked upload of multimodal attachments, resumable re-upload, and reference write-back;
- Voice layer: timing alignment between speech recognition input and streaming synthesized playback;
- Archive layer: consistent directory structure, version numbers, and validation for history packages, ensuring bidirectional interoperability.
Compatibility is the most underestimated part. If a history archive can be read directly by the other version, then field naming, nested hierarchy, timestamp formats, and attachment addressing must all align byte for byte—far harder than making the UI “look the same.”
3. Architecture Evolution: Three Iterations to Contain Complexity
The first round chased “looking right.” The UI was laid out in a componentized way, splitting the message list, input area, and settings panel into independent units to get the main flow working quickly. The problem at this stage was scattered state, with streaming increments interfering with branch switching.
The second round consolidated state. The session tree was modeled as a flattened node table with parent-child pointers, so any branch operation degrades to a pointer change; streaming content is written to a separate increment buffer, and the render layer commits on a frame-throttled schedule to avoid reflows from high-frequency updates.
// Flattened message tree: branch switching only changes pointers, no full-tree copyconst nodes = reactive(new Map())const activeLeaf = ref(null)function branchTo(nodeId) {activeLeaf.value = nodeId // O(1) switch; history path derived from the pointer chain}function appendDelta(chunk) {const buf = streamBuffer.valuebuf += chunkscheduleFlush() // Coalesce high-frequency increments and commit the view per frame}
The third round expanded capabilities and standardized the code. After unifying formatting conventions and re-counting, the production source dropped from over 18,000 lines to about 13,000—a net reduction of more than 5,000 lines, roughly 27%—and that figure already includes newly added tool-call integration and instruction management.
4. Extension Design: Tool Calls and Instruction Management
New capabilities live behind an entry in the lower-left corner, following a “don’t interrupt by default, expand on demand” principle.
Tool-call integration has to solve three things: storing and masking connection configuration, dynamically discovering the call inventory, and writing back and displaying execution results. For security, the frontend should not hold long-lived keys; sensitive credentials should be managed by a backend proxy or a key management service, with the frontend storing only a reference identifier.
Instruction management supports three sources: local Markdown files, batch import from a directory, and pulling from a code hosting repository. Implementation points include parsing front-matter metadata, building an instruction index, triggering instructions in a session by prefix or slash command, and enforcing size and count limits on imported content to keep abnormal files from slowing down the first screen.
5. Performance and Maintainability
- Render throttling: merge streaming increments into animation frames, and enable virtual scrolling for long lists;
- Structural sharing: reuse references for unchanged message nodes to reduce diff cost;
- Archive chunking: send large attachments directly to object storage, with the frontend maintaining only the index and progress;
- Observability: connect to a general logging service and monitoring alerts, and set baselines for time to first byte, render frame rate, and failure rate;
- Compatibility regression: turn bidirectional import/export into automated test cases and include them in continuous integration.
6. Conclusion
This practice shows that the value of a framework migration is not swapping syntax, but using the refactor to redraw boundaries: state consolidation brings stable branching, protocol alignment brings archive interoperability, and standardized counting turns “less code” into a verifiable number. If an enterprise wants to reuse this approach in its own business, it can rely on relevant Baidu AI Cloud product capabilities for attachment storage, key management, and runtime monitoring, keeping the frontend focused on interaction and state itself.
