Session Recording Example
This example demonstrates the session recording and replay capabilities of PromptKit.
Features Demonstrated
Section titled “Features Demonstrated”- Recording: Capture conversation events to a file-based EventStore
- Export: Save sessions to portable JSON format
- Replay: Deterministically replay sessions using ReplayProvider
- Playback: Timeline-based playback with SessionPlayer
Prerequisites
Section titled “Prerequisites”Set your API key:
export OPENAI_API_KEY=your-keyRunning
Section titled “Running”From the example directory:
cd sdk/examples/session-recordinggo run .Or from the repo root:
go run ./sdk/examples/session-recordingExpected Output
Section titled “Expected Output”=== Session Recording Demo ===
Step 1: Recording a live conversation... User: What is the capital of France? Assistant: The capital of France is Paris... User: What's the population of that city? Assistant: Paris has a population of approximately... User: Thanks! One more question: what river runs through it? Assistant: The Seine River runs through Paris... Session recorded: abc123
Step 2: Exporting session to JSON... Saved 6 events Exported to: /tmp/promptkit-recording-xxx/session.recording.json
Step 3: Replaying session with ReplayProvider... Loaded recording with 6 events Created ReplayProvider with 3 turns Turn 1: The capital of France is Paris... Turn 2: Paris has a population of approximately... Turn 3: The Seine River runs through Paris...
Step 4: Playing back with SessionPlayer... [ 0.00s] message.created [ 0.10s] provider.call.completed [ 0.20s] message.created ... Played 6 events
=== Demo Complete ===Code Walkthrough
Section titled “Code Walkthrough”Recording a Session
Section titled “Recording a Session”// Create an event storestore, _ := events.NewFileEventStore("./recordings")
// Open conversation with recording enabledconv, _ := sdk.Open("./chat.pack.json", "chat", sdk.WithEventStore(store))
// Conversation is automatically recordedresp, _ := conv.Send(ctx, "Hello!")Exporting to Portable Format
Section titled “Exporting to Portable Format”// Get recorded eventsevts, _ := store.GetSessionEvents(ctx, sessionID)
// Build SessionRecordingrec := &recording.SessionRecording{ Metadata: recording.Metadata{SessionID: sessionID}, Events: convertEvents(evts),}
// Save as JSONrec.SaveTo("session.json", recording.FormatJSON)Replaying with ReplayProvider
Section titled “Replaying with ReplayProvider”// Load recordingrec, _ := recording.Load("session.json")
// Create provider (implements providers.Provider interface)provider, _ := replay.NewProvider(rec, &replay.Config{ Timing: replay.TimingInstant, // No delays MatchMode: replay.MatchByTurn, // Sequential})
// Use like any providerresp, _ := provider.Predict(ctx, providers.PredictionRequest{})fmt.Println(resp.Content)Timeline Playback
Section titled “Timeline Playback”// Create playerplayer := events.NewSessionPlayer(evts)player.SetSpeed(2.0) // 2x speed
// Subscribe and playch := player.Subscribe()go player.Play(ctx)
for evt := range ch { fmt.Printf("[%v] %s\n", evt.Offset, evt.Event.Type)}Use Cases
Section titled “Use Cases”- Testing: Replay recorded sessions for deterministic tests
- Debugging: Inspect what happened during a conversation
- Training Data: Export sessions for fine-tuning datasets
- Demo/Presentation: Replay interactions without API calls