# Building a TUI-Based Chat History Export Tool: Design and Practice

    This article walks through the design and implementation of a terminal-based (TUI) tool for exporting AI chat histories. It covers the migration from offline file parsing to token-based direct API access, lazy loading for paginated browsing, local deduplication, and structured Markdown formatting—offering a reusable pattern for archiving conversations into a personal knowledge base.

    1. Background and Problem Statement

    As large language model chat tools become part of everyday work and study, the Q&A records users accumulate on these platforms often carry real knowledge value. However, on most platforms the conversation history lives only on the server side, with no effective means of local backup or reuse. Whenever users need to search, archive, or turn these records into personal knowledge base content, they typically run into three pain points:

    1. Data is not under your control: history depends on online access to the platform, with no offline search or local management;
    2. Batch operations are tedious: manually copy-pasting individual conversations is slow and easily loses context such as the model’s “deep thinking” traces;
    3. The format is not reusable: the platform’s native export files (such as bulk JSON dumps) are hard to read and don’t fit directly into a note-taking workflow.

    To address these problems, this article describes a tool refactoring effort: upgrading the original workflow—“export file from the platform + drag-and-drop in a CLI”—into an integrated solution of “browser token extraction + terminal UI (TUI) + smart deduplicated export.” The refactored tool is implemented in Go, and its output consists of monthly-grouped, cleanly formatted Markdown files that can be imported directly into mainstream note-taking apps for archival.

    2. Architecture Evolution: From Offline File Parsing to Token-Based Direct Access

    2.1 Limitations of the Old Approach

    The first version of the tool followed this workflow: manually trigger a full export in the web UI, wait for the platform to generate an archive, unzip it, then drag the JSON file into the program for parsing. This flow had clear drawbacks:

    • The export depends on queue-based generation on the platform side, with unpredictable wait times;
    • You cannot selectively export specific conversations—only the full history;
    • Users had to switch back and forth between the file system and the terminal, resulting in a long, clunky operation chain.

    2.2 Core Idea of the New Approach

    The refactored design bypasses the “export file” middleman entirely: it extracts an identity token from the browser session and calls the platform API to pull the conversation list on demand. The flow is reduced to three steps:

    1. Copy Token from browser → Paste and authenticate in the TUI → Browse pages and export selectively

    This design hands control of “data retrieval” back to the user: which conversations to export, and how many, can be decided on the fly in the interactive UI, no longer constrained by full exports.

    How to obtain the token (general guidance): after logging in to the target platform in your browser, press F12 to open Developer Tools, switch to the “Network” tab, locate a request that carries your user credentials in the request list, and copy the token field value from it. The process involves no third-party components; the token is used only locally and only to issue read-only queries to the platform.

    3. Key Technical Details

    3.1 Full-Screen TUI in the Bubble Tea Style

    The terminal user interface (TUI) is the highlight of this refactor. Compared with traditional flag-based command-line interaction, a TUI offers a browsing and selection experience similar to a GUI. The design follows the Elm architecture—program state is modeled as a Model, key presses and timer events drive Update, and the View handles rendering:

    1. type Model struct {
    2. page int // Current page number
    3. conversations []ConvMeta // Conversation metadata for the current page
    4. selected map[int]bool // Checked items
    5. mode Mode // Browse / Select / Exporting
    6. }
    7. func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    8. switch msg := msg.(type) {
    9. case tea.KeyMsg:
    10. switch msg.String() {
    11. case "up", "down": // Move cursor
    12. case " ": // Toggle selection
    13. case "enter": // Confirm export of selected items
    14. case "q", "ctrl+c": // Quit
    15. }
    16. }
    17. return m, nil
    18. }

    Keybindings stay intuitive: arrow keys to navigate, space to toggle selection, enter to execute, and Q to quit. This design is friendly to keyboard-first users and makes the tool usable on remote servers without a graphical environment.

    3.2 Lazy Loading

    A conversation history can contain hundreds or thousands of entries; pulling everything at once wastes bandwidth and easily triggers API rate limits. The refactored version fetches data in batches:

    • On first entry, only the first page is requested (e.g., 20 items per page);
    • When the user pages through, the next page is fetched on demand, with already-loaded pages cached in memory to avoid repeated requests;
    • The rendering layer only processes the current page and adjacent pages, keeping memory usage constant.

    This “load as you browse” pattern mirrors infinite-scroll lazy loading on the frontend and is standard practice for handling large list data.

    3.3 Three Flexible Export Modes

    The tool provides three export granularities covering different scenarios:

    Mode Use Case Description
    Export recent Daily incremental backup Export new conversations from the recent period
    Export all First-time migration Export the entire history in one go
    Manual selection Precise archiving Browse pages and check specific conversations; space to select, enter to confirm

    Manual selection combined with paginated browsing addresses the fine-grained need of “I only want a few key discussions.”

    3.4 Local Deduplication

    For incremental backups, redundant exports are the biggest waste. Before exporting, the tool performs a file system check on the local target directory:

    1. Target filenames are generated using a “date + conversation title” rule;
    2. If a file with the same name already exists, the conversation is considered already exported and skipped automatically;
    3. Deduplication results are reported in real time in the TUI, so users can directly see “N skipped, M new.”

    This local deduplication logic relies purely on file naming conventions—no extra database required—making it simple and reliable. It delivers two direct benefits: fewer API requests and no duplicate files accumulating in the notes library.

    3.5 Deep Markdown Formatting

    Conversation data is typically returned as structured JSON, and a naive serialization into Markdown is barely readable. The formatting module applies multiple layers of processing:

    • Role separation: distinguish user prompts from model responses using heading levels or blockquotes;
    • Deep-thinking reconstruction: content such as “thinking process” is wrapped separately, using collapsible or quote syntax to preserve the original reasoning trail—especially valuable when reviewing how the model reasoned;
    • Code block language annotation: code snippets in responses are detected and given proper language tags, so syntax highlighting works correctly in note-taking apps;
    • File organization: files are named “date + conversation title” and grouped by month in a single export directory, for example:
    1. conversations_export/
    2. ├── 2026-05/
    3. │ ├── 2026-05-03-Discussion-on-Cache-Eviction-Policies.md
    4. │ └── 2026-05-11-Go-Generics-Practice-Notes.md
    5. └── 2026-06/
    6. └── ...

    The monthly directory structure matches the date-based archiving habits of mainstream note-taking apps, so no reorganization is needed after import.

    4. Engineering Considerations

    4.1 Cross-Platform Delivery

    Go’s cross-compilation makes it cheap to cover Windows, macOS, and Linux, shipping both amd64 and arm64 builds. For users without a Go environment, the community can provide an equivalent implementation in a scripting language (such as a Python version) to lower the barrier to entry—a common multi-language delivery strategy for open source tools.

    4.2 Security and Compliance Notes

    Since the tool handles user identity tokens, keep the following in mind:

    • The token should live only in local process memory—never written to disk or logs;
    • Network requests should perform read-only queries only, with no write operations;
    • Users are advised to rotate tokens regularly to reduce the risk of long-lived credential leaks.

    4.3 Version History

    • v1: depended on manually exported JSON files from the platform; the program handled parsing and Markdown conversion, with files named “date + title” and grouped by month;
    • Refactored version: added a full-screen TUI, switched to token-based direct retrieval of history, removed the dependency on export files, offered more flexible export options, and strengthened the deduplication logic.

    5. Conclusion

    The refactor delivers value in three areas: interaction upgrade (a keyboard-first, fluid experience via the TUI), shorter pipeline (token-based direct retrieval replaces offline export files), and cost optimization (lazy loading and local deduplication significantly cut redundant requests). The solution depends on no specific cloud service, and its design ideas—paginated lazy loading, filename-based deduplication, and structured Markdown conversion—transfer readily to other scenarios such as conversation backup, log archiving, and bulk API data fetching. For anyone looking to turn AI conversations into a personal knowledge base, this is a worthwhile engineering reference.