How to download or export the chat transcript

This question originally came up in our Slack community and the thread has been consolidated here for long-term reference.

Is there any way to download the chat/transcript from a session?

Chat messages are not persistent by default:

To save transcripts, add a shutdown callback in your agent that writes the session history:

from datetime import datetime
import json

def entrypoint(ctx: JobContext):
    async def write_transcript():
        current_date = datetime.now().strftime("%Y%m%d_%H%M%S")
        filename = f"/tmp/transcript_{ctx.room.name}_{current_date}.json"
        
        with open(filename, 'w') as f:
            json.dump(session.history.to_dict(), f, indent=2)
            
        print(f"Transcript saved to {filename}")

    ctx.add_shutdown_callback(write_transcript)
    # ... rest of your entrypoint code