mirror of
https://github.com/github/awesome-copilot.git
synced 2026-02-21 10:55:13 +00:00
All 5 Go recipes and their markdown docs used incorrect API patterns that don't match the real github.com/github/copilot-sdk/go v0.1.23: - copilot.NewClient() -> copilot.NewClient(nil) (*ClientOptions param) - client.Start() -> client.Start(ctx) (context.Context required) - copilot.SessionConfig -> &copilot.SessionConfig (pointer required) - session.On(func(event copilot.Event)) -> session.On(func(event copilot.SessionEvent)) - Type assertions -> event.Type string check + *event.Data.Content deref - session.WaitForIdle() -> session.SendAndWait(ctx, ...) (WaitForIdle doesn't exist) - copilot.SystemMessage -> copilot.SystemMessageConfig All 5 recipes verified to compile against SDK v0.1.23.
98 lines
2.2 KiB
Markdown
98 lines
2.2 KiB
Markdown
# 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](recipe/persisting-sessions.go)
|
|
>
|
|
> ```bash
|
|
> cd recipe
|
|
> go run persisting-sessions.go
|
|
> ```
|
|
|
|
### Creating a session with a custom ID
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
copilot "github.com/github/copilot-sdk/go"
|
|
)
|
|
|
|
func main() {
|
|
ctx := context.Background()
|
|
client := copilot.NewClient(nil)
|
|
client.Start(ctx)
|
|
defer client.Stop()
|
|
|
|
// Create session with a memorable ID
|
|
session, _ := client.CreateSession(ctx, &copilot.SessionConfig{
|
|
SessionID: "user-123-conversation",
|
|
Model: "gpt-5",
|
|
})
|
|
|
|
session.SendAndWait(ctx, 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
|
|
|
|
```go
|
|
ctx := context.Background()
|
|
client := copilot.NewClient(nil)
|
|
client.Start(ctx)
|
|
defer client.Stop()
|
|
|
|
// Resume the previous session
|
|
session, _ := client.ResumeSession(ctx, "user-123-conversation")
|
|
|
|
// Previous context is restored
|
|
session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "What were we discussing?"})
|
|
|
|
session.Destroy()
|
|
```
|
|
|
|
### Listing available sessions
|
|
|
|
```go
|
|
sessions, _ := client.ListSessions(ctx)
|
|
for _, s := range sessions {
|
|
fmt.Println("Session:", s.SessionID)
|
|
}
|
|
```
|
|
|
|
### Deleting a session permanently
|
|
|
|
```go
|
|
// Remove session and all its data from disk
|
|
client.DeleteSession(ctx, "user-123-conversation")
|
|
```
|
|
|
|
### Getting session history
|
|
|
|
```go
|
|
messages, _ := session.GetMessages(ctx)
|
|
for _, msg := range messages {
|
|
if msg.Data.Content != nil {
|
|
fmt.Printf("[%s] %s\n", msg.Type, *msg.Data.Content)
|
|
}
|
|
}
|
|
```
|
|
|
|
## Best practices
|
|
|
|
1. **Use meaningful session IDs**: Include user ID or context in the session ID
|
|
2. **Handle missing sessions**: Check if a session exists before resuming
|
|
3. **Clean up old sessions**: Periodically delete sessions that are no longer needed
|