# Add Chat Export to a Reflex Chat App

Yes. An agent adds a download button to your Reflex chat app that saves the conversation as Markdown with rx.download, tests the formatter and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-chat-export/reflex/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads how the conversation is held before it writes it out, because in Reflex the state decides what a button can reach. In an app like reflex-dev/reflex-chat, `rxconfig.py` names the app `chat` and `requirements.txt` asks for `reflex` and `openai`; `chat/state.py` keeps every conversation in a private var `_chats`, a dict from chat name to a list of `QA` typed dicts with `question` and `answer`, seeded with an empty `Intros`, with `current_chat`, `processing` and `is_modal_open` beside it. `openai_process_question` appends a `QA` with an empty answer, streams the completion and yields after each chunk while assigning `self._chats = self._chats` so the UI repaints; `selected_chat` and `chat_titles` are computed vars over the dict. `chat/components/chat.py` renders each `QA` through `rx.markdown` inside `rx.auto_scroll`, and `action_bar` is a form whose `Send` button is `loading` and `disabled` while `State.processing`; `chat/components/navbar.py` shows the current chat as a badge, a dialog to create one, and a drawer listing the others with a trash icon each. `chat/chat.py` sets the dark Radix theme with the purple accent. `.github/workflows/ci.yml` runs three jobs with `OPENAI_API_KEY` set to `dummy`: pyright, ruff, and `reflex init` followed by `reflex export`. There are no tests.

Reflex's special events include `rx.download`, which takes either a `url` or `data`, a `filename` and, in current releases, a `mime_type`, and the documentation's pattern is to return it from an event handler when the bytes are not at a known URL. The agent adds a `chat_to_markdown(name, qas)` function in a new `chat/export.py` that writes a level-one heading with the chat name and, for each pair, a `## You` block with the question and a `## Reflex` block with the answer, blank-line separated so the answers, which are already Markdown from the model, render as they were shown; a trailing pair with an empty answer, which is what the dict holds mid-stream, is skipped. The state gets an `export_chat` event that calls it on `self._chats[self.current_chat]` and returns `rx.download(data=..., filename=f"{slug}.md", mime_type="text/markdown")`, with the slug made from the chat name so `My notes` becomes `my-notes.md`. Keeping the formatter outside the state class is deliberate: it takes plain values and returns a string, so a test needs no app.

The button goes where the chat's other actions are: in `navbar`, an `rx.icon_button("download")` before the new-chat button, with `disabled` bound to `State.processing` so a stream in progress cannot be exported half-way, and a tooltip naming the format. A computed var `selected_chat_is_empty` disables it on a fresh chat so the file is never just a heading. Nothing else in the UI changes; the drawer, the dialog and the message list are untouched.

Then the agent runs what CI runs and what a reader would do. `pyright .` and `ruff check .` pass with the new module; `reflex init` and `reflex export` with the dummy key produce the frontend bundle, which is the job that catches a component used wrongly. A new `tests/test_export.py` checks the heading, the two blocks per pair, the skipped empty answer and the filename slug, and the agent adds a pytest job to `ci.yml` beside the other three. It starts the app with `reflex run` and the dummy key in its sandbox, which loads because the check in `state.py` only asks that the variable is set, creates a chat named `Export check` in its own browser and sees the button disabled on the empty chat; since no question can be asked without a real key, it seeds one pair into `_chats` in a scratch copy of the app, clicks the download, reads `export-check.md` from the browser's download directory, and discards the seed before committing.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add a way to export the current conversation of this Reflex chat app as a Markdown file. Read rxconfig.py, requirements.txt, chat/state.py, chat/chat.py, every file under chat/components and the CI workflow first. Write a pure function in a new module that turns a chat name and its question and answer pairs into Markdown, skipping a trailing pair whose answer is still empty, and an event handler on the state that returns rx.download with that text, a filename made from the chat name and a Markdown mime type. Add a download icon button in the navbar next to the new-chat button, disabled while a response is streaming and when the chat is empty. Add pytest tests for the formatter and a pytest job to the CI workflow. Run pyright, ruff, reflex init and reflex export with a dummy API key as CI does, run the app in a browser, trigger a download and read the file, and open a pull request linked to the issue with the commands and their output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request adds `chat/export.py` and `tests/test_export.py`, changes `chat/state.py`, `chat/components/navbar.py`, `requirements.txt` for the test dependency and `.github/workflows/ci.yml`. Its description quotes the pyright, ruff and export output, the pytest run, the downloaded file from the browser check, and a screenshot of the navbar with the button enabled and disabled.

A reviewer reads the Markdown layout and decides whether the heading labels should carry the model name or a timestamp, and whether the export should be offered per chat in the drawer too. After merging, a user downloads any conversation as a `.md` file named after the chat, with the answers rendering as they did on screen.

## Run it on every Linear issue labelled export

An automation in the Project that holds the app takes a `linear` trigger on `linear.issue.labeled` with the label `export`. Its runs are read-only, so the saved prompt asks for a plan: the run reads the state and components, names the module, handler and button it would add and shows the Markdown layout, and posts that as a comment on the issue, where the format can be settled before any code. A handler on the team's own server then starts the run that writes the pull request, sending this body with the issue's URL added to `resources` and a `request_id` that is a UUID v5 of the issue id, so a retried start returns the run already started.

A team with several Reflex apps sends one request per repository, each with its own `resources` entry; an app that stores chats in a database gets the same handler reading rows instead of a dict.

## Why run it on Rush?

The feature is a dozen lines, and the places it goes wrong are in the app's own rules: an export taken mid-stream, a button that works on an empty chat, a file name with spaces, a component call that only fails at `reflex export`. The agent reads the state and the components, runs the three CI jobs and the export in its own sandbox, and drives the download in a real browser, keeping every command, the file and the screenshots in the run's record.

It also leaves a test behind where there were none, on a function written to be testable without the app running. Several Reflex apps get the same change at once, each with the button beside that app's own actions.

## Frequently asked questions

### Why a pure function instead of building the text in the event handler?

An event handler runs on the state and needs the app to exercise; a function that takes a name and a list of pairs is tested with plain pytest in milliseconds. The handler stays three lines: read the current chat, format it, return `rx.download`.

### Does rx.download need a file on disk or a URL?

No. Reflex's documentation says `rx.download` takes either a `url` or `data`, and that returning it from an event handler with the data is the way when the bytes are not already served. The text goes straight from the state to the browser's download.

### Will the exported answers look like the chat?

Yes, because the app already asks the model to respond in Markdown and renders each answer with `rx.markdown`; the export writes the same text under a heading, so a Markdown viewer shows the same lists and code blocks.

### Can the test suite run without an OpenAI key?

The formatter module imports nothing from the state, so its tests run with no key at all. The CI jobs that import the app keep the `dummy` key the workflow already sets, which satisfies the check at the top of `state.py`.

## More Reflex jobs

- [Add Dark Mode to a Reflex App](https://getrush.ai/run/add-dark-mode/reflex/)
- [Add Dependabot to a Reflex App](https://getrush.ai/run/add-dependabot/reflex/)
- [Write a Dockerfile for a Reflex App](https://getrush.ai/run/add-dockerfile/reflex/)

## The same job on other stacks

- [Add Chat History Export to a Streamlit App from a Linear Issue](https://getrush.ai/run/add-chat-export/streamlit/)
- [Add Chat Export to an AI SDK Chatbot](https://getrush.ai/run/add-chat-export/ai-sdk/)

## More jobs like this: Turn a Linear label into a PR

- [Add a Checkout UI Extension to a Shopify App](https://getrush.ai/run/add-checkout-ui-extension/shopify-app/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
