mirror of
https://github.com/github/awesome-copilot.git
synced 2026-02-20 10:25:13 +00:00
2.0 KiB
2.0 KiB
Session Persistence and Resumption
Save and restore conversation sessions across application restarts.
Example scenario
You want users to be able to continue a conversation even after closing and reopening your application.
Runnable example: recipe/persisting-sessions.go
cd recipe go run persisting-sessions.go
Creating a session with a custom ID
package main
import (
"fmt"
"github.com/github/copilot-sdk/go"
)
func main() {
client := copilot.NewClient()
client.Start()
defer client.Stop()
// Create session with a memorable ID
session, _ := client.CreateSession(copilot.SessionConfig{
SessionID: "user-123-conversation",
Model: "gpt-5",
})
session.Send(copilot.MessageOptions{Prompt: "Let's discuss TypeScript generics"})
// Session ID is preserved
fmt.Println(session.SessionID)
// Destroy session but keep data on disk
session.Destroy()
}
Resuming a session
client := copilot.NewClient()
client.Start()
defer client.Stop()
// Resume the previous session
session, _ := client.ResumeSession("user-123-conversation")
// Previous context is restored
session.Send(copilot.MessageOptions{Prompt: "What were we discussing?"})
session.Destroy()
Listing available sessions
sessions, _ := client.ListSessions()
for _, s := range sessions {
fmt.Println("Session:", s.SessionID)
}
Deleting a session permanently
// Remove session and all its data from disk
client.DeleteSession("user-123-conversation")
Getting session history
messages, _ := session.GetMessages()
for _, msg := range messages {
fmt.Printf("[%s] %v\n", msg.Type, msg.Data)
}
Best practices
- Use meaningful session IDs: Include user ID or context in the session ID
- Handle missing sessions: Check if a session exists before resuming
- Clean up old sessions: Periodically delete sessions that are no longer needed