Design and Refactoring of a Terminal Tool for Exporting AI Conversation History

    This article describes how a terminal tool for exporting AI conversation history was redesigned from a file-based script into an API-driven sync client. It covers the limits of the original workflow, token-based authentication, lazy loading, a full-screen TUI, idempotent deduplication, Markdown formatting for reasoning content, and engineering practices for cross-platform distribution, token security, and error handling.

    1. Background: Why Conversation History Needs Local Management

    As large language model chat platforms become widespread, users accumulate many valuable conversations: troubleshooting approaches, code snippet discussions, requirements analysis, and more. This content is effectively personal knowledge, but platforms usually offer limited support for managing it, which leads to three pain points:

    1. Hard to search: Platform history lists are typically sorted by time only, with no full-text search or tagging system.
    2. Hard to migrate: When users switch platforms or run into account problems, it is difficult to move their entire conversation history.
    3. Hard to reuse: Conversation content cannot easily be imported into personal knowledge bases or note-taking apps for further organization.

    Exporting conversation history to the local machine as structured, readable Markdown files, archived by date and topic, is a practical and general solution. The tool discussed in this article pursues that goal. Its architecture was refactored from “parsing exported files” to “connecting directly to the server API, with a full-screen terminal interface.”

    2. Original Architecture: A One-Way, File-Driven Pipeline

    The early version followed a typical one-off script pattern:

    • The user manually triggers “export all conversations” on the web interface, downloads an archive, and extracts it.
    • The user drags the extracted JSON file onto the executable, or passes the file path on the command line.
    • The program parses the JSON, generates a Markdown file for each conversation named “date + conversation title,” and stores the files in the output directory, grouped by month.

    The limitations of this approach are clear:

    • Heavy manual work: Every export requires repeating the three steps of web export, extraction, and drag and drop. The process cannot be automated.
    • Full processing only: Even if the user wants to export one or two new conversations, they must export and process the entire history.
    • No interaction: Users cannot preview or select conversations within the program.
    • Fragile parsing: If the platform changes the field structure of its export file, the parsing logic breaks immediately.

    These problems led the author to rebuild the tool thoroughly.

    3. Core of the Refactor: From File-Driven to API-Driven

    3.1 Obtaining Authentication Information

    The refactored tool no longer depends on exported platform files. Instead, it takes the user’s access token from the browser developer tools after login, then fetches the conversation history page by page through the platform’s API. The general steps to get the token are: log in to the target platform in your browser; press F12 to open the developer tools; switch to the Network panel; find a request that carries authentication information; and copy the value of the relevant field. This approach skips the manual export step, so the tool can sync new conversations on demand at any time.

    3.2 Pagination and On-Demand Loading

    A user may have hundreds or thousands of conversations. Loading all of them at once is slow and wasteful. The tool uses lazy loading: the terminal interface requests one page at a time and loads the next batch automatically when the user scrolls to the bottom. This design has two benefits. Startup is fast, and the first screen is almost immediately usable. Memory usage stays under control and does not depend on the total number of conversations. For command-line tools, this “lazy loading plus streaming rendering” approach also applies to scenarios such as log viewers and data search tools.

    3.3 Full-Screen Terminal Interface

    The biggest usability improvement in this refactor is a full-screen text user interface (TUI) built on a terminal UI framework. The interaction is keyboard-driven:

    • The Up and Down arrow keys move the cursor and page through the conversation list.
    • The Space key selects or deselects the current conversation.
    • The Enter key confirms the selection and starts the export.
    • The Q key quits the program.

    Compared with the traditional command-line argument mode, the TUI removes the need to memorize parameters for selecting the export scope, which is a frequent operation. It also supports three export strategies: exporting only the most recent conversations, exporting all conversations, and exporting manually selected conversations. Together, these cover everything from routine incremental backups to a first full archive.

    3.4 Local Deduplication

    To prevent duplicate exports from causing file conflicts and redundant requests, the tool scans the local export directory before exporting and skips any conversation whose unique identifier already exists. This makes the “export all” operation idempotent. It can run repeatedly without overwriting or duplicating files, which makes it a natural fit for periodic incremental backups. Similar ideas are widely used in data synchronization and ETL pipelines, where a persistent deduplication set or a watermark marker ensures that only new data is processed.

    3.5 Markdown Formatting

    Export is more than plain text concatenation. Platform conversations often contain two kinds of messages: regular answer content, and reasoning content, which is the model’s thinking process before it produces an answer. The tool renders the two separately. Regular content is output as standard Markdown paragraphs. The reasoning process is visually separated using blockquotes or a collapsible convention. The tool also handles nested structures correctly, including code blocks, lists, and math formulas, so the layout and hierarchy stay intact when the files are imported into any note-taking app that supports Markdown. Files are named “date + conversation title” and sorted automatically into month-based directories, so the local file system itself becomes a searchable knowledge base.

    4. Engineering Considerations

    Cross-platform distribution: The tool is written in a language that supports static compilation. The build output is a single-file executable with no dependencies. It targets major desktop operating systems and both amd64 and arm64 architectures. Users do not need to set up any runtime environment.

    Token security: Tokens and other sensitive information should not be hardcoded or committed to the version repository. Inject them through environment variables or a local configuration file. The documentation should state that a token carries the same permissions as the account and must be kept safe.

    Error handling: Scenarios such as network timeouts, expired tokens, and disk write failures should produce clear error messages and recovery suggestions instead of exiting silently. This matters for making a command-line tool feel professional.

    5. Summary

    The core of this refactor is turning the tool from a passive file parser into an active data sync client. Direct authentication replaces manual export. On-demand loading replaces full fetches. Interactive selection replaces command-line parameters. Idempotent deduplication replaces brute-force overwriting. This design pattern applies broadly. Any requirement that involves extracting personal data from a remote platform and archiving it locally can reference this architecture. It reinforces a simple engineering principle: a good tool does not add steps for the user, but reduces the workflow to the shortest path.