演示会话列表
(ctx context.Context, service *session.InMemoryService)
| 220 | |
| 221 | // 演示会话列表 |
| 222 | func demonstrateSessionListing(ctx context.Context, service *session.InMemoryService) { |
| 223 | fmt.Println("📚 Session Listing Demo") |
| 224 | fmt.Println("=======================") |
| 225 | |
| 226 | // 创建多个会话 |
| 227 | for i := 1; i <= 3; i++ { |
| 228 | _, _ = service.Create(ctx, &session.CreateRequest{ |
| 229 | AppName: "my-app", |
| 230 | UserID: "user-123", |
| 231 | AgentID: fmt.Sprintf("agent-%03d", i), |
| 232 | }) |
| 233 | } |
| 234 | |
| 235 | // 列出会话 |
| 236 | sessions, err := service.List(ctx, &session.ListRequest{ |
| 237 | AppName: "my-app", |
| 238 | UserID: "user-123", |
| 239 | Limit: 10, |
| 240 | }) |
| 241 | if err != nil { |
| 242 | log.Printf("Error listing sessions: %v", err) |
| 243 | return |
| 244 | } |
| 245 | |
| 246 | fmt.Printf("\n📊 Total sessions for user-123: %d\n", len(sessions)) |
| 247 | for i, sess := range sessions { |
| 248 | s := *sess |
| 249 | fmt.Printf(" %d. %s (Agent: %s, Updated: %s)\n", |
| 250 | i+1, |
| 251 | s.ID(), |
| 252 | s.AgentID(), |
| 253 | s.LastUpdateTime().Format("15:04:05"), |
| 254 | ) |
| 255 | } |
| 256 | |
| 257 | fmt.Println() |
| 258 | } |
| 259 | |
| 260 | // 辅助函数:获取状态作用域 |
| 261 | func getScope(key string) string { |