Keep the conversation
Keep a thread outside the screen to resume its conversation when the screen reopens.
Create a TandemThread outside the screen, for example in the State of the page that opens it. Pass that object as thread: to TandemScope each time the screen opens. Keep it in a field rather than creating it in build.
The new session restores the chat and sends the thread's history and agent state to the backend. Without thread:, each session starts a new conversation. session. is the AG-UI thread ID.
Keep the screen's state too
Before sending its first message, the new session compares the screen's . values with those saved when the previous session closed. If they differ, Tandem resets the AG-UI thread and adds a startedOver notice. The chat stays visible.
Keep the screen's state object as long as the thread if you want to resume both. Recreating the state object can change its values and trigger this reset. A conversation that moves between different screens is not supported yet.
When a session closes
- Only the newest session uses a thread. The previous session detaches, even if its screen is still animating out.
- Closing a session during a run or an approved method resets the AG-UI thread. The backend may finish that work after the app stops reading it, so the old history cannot safely continue.
- Closing a session with a pending approval cancels the request. If the AG-UI thread continues, the next message carries the
cancelledresult.
Changing agent: starts a new session. If you pass the same TandemThread, its history and agent state go to the new backend. Pass a new thread to start a separate conversation.
A thread lives in memory. Tandem does not persist it across app restarts.
To clear a thread's history manually, call session..
Use the backend's conversation ID
When your backend keeps the conversation, pass its ID as TandemThread(id: . Keep the thread in a field or a State. A new TandemThread opens a session with an empty chat. Each run uses the supplied ID. ThreadId refuses an empty ID with an ArgumentError.
A reset clears the local history and agent state and keeps the ID. After session. or reopening on a changed screen, the next run carries the calls whose answers no run has sent yet, with those answers, before the new message. The agent backend lists what the backend must do for such a thread.