AddMessage adds a message to a session at the next position. Returns the ID of the created message (for in-memory, this is a simple counter).
(_ context.Context, sessionID string, msg *Message)
| 267 | // AddMessage adds a message to a session at the next position. |
| 268 | // Returns the ID of the created message (for in-memory, this is a simple counter). |
| 269 | func (s *InMemorySessionStore) AddMessage(_ context.Context, sessionID string, msg *Message) (int64, error) { |
| 270 | if sessionID == "" { |
| 271 | return 0, ErrEmptyID |
| 272 | } |
| 273 | session, exists := s.sessions.Load(sessionID) |
| 274 | if !exists { |
| 275 | return 0, ErrNotFound |
| 276 | } |
| 277 | // Deep-copy before mutating ID. The caller's pointer may be held |
| 278 | // concurrently by another goroutine (snapshotItems → cloneMessage), |
| 279 | // and writing msg.ID directly races with those reads. |
| 280 | stored := cloneMessage(msg) |
| 281 | s.messageID++ |
| 282 | stored.ID = s.messageID |
| 283 | session.AddMessage(stored) |
| 284 | return s.messageID, nil |
| 285 | } |
| 286 | |
| 287 | // UpdateMessage updates an existing message by its ID. |
| 288 | func (s *InMemorySessionStore) UpdateMessage(_ context.Context, messageID int64, msg *Message) error { |