(ctx context.Context, startTurn, endTurn int, previews []string)
| 405 | if payload.UserTurn > 0 { |
| 406 | userTurn = payload.UserTurn |
| 407 | } else if taggedTurn := sessionUserTurn(msg); taggedTurn > 0 { |
| 408 | userTurn = taggedTurn |
| 409 | } else if isRealUserRequest(msg) { |
| 410 | userTurn++ |
| 411 | } |
| 412 | if userTurn == 0 { |
| 413 | continue |
| 414 | } |
| 415 | role := stringValue(msg["role"]) |
| 416 | contentJSON, err := json.Marshal(msg["content"]) |
| 417 | if err != nil { |
| 418 | return err |
| 419 | } |
| 420 | hash := messageHash(userTurn, role, contentJSON) |
| 421 | preview := textPreview(msg, 1200) |
| 422 | now := unixNow() |
| 423 | if _, err := tx.ExecContext(ctx, `INSERT OR IGNORE INTO messages( |
| 424 | turn_count, user_turn_count, role, content_json, text_preview, message_hash, source_event_id, created_at, last_accessed_at |
| 425 | ) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)`, userTurn, userTurn, role, string(contentJSON), preview, hash, event.ID, now, now); err != nil { |
| 426 | return err |
| 427 | } |
| 428 | } |
| 429 | return tx.Commit() |
| 430 | } |
| 431 | |
| 432 | func (s *Store) MaybeCommit(ctx context.Context, force bool) error { |
| 433 | interval := s.cfg.SessionMemoryTurnInterval |
| 434 | if interval <= 0 { |
| 435 | interval = 5 |
| 436 | } |
| 437 | lastCommitted := s.metaInt(ctx, "last_committed_user_turn") |
| 438 | maxUserTurn := s.maxUserTurn(ctx) |
| 439 | if maxUserTurn <= lastCommitted { |
no test coverage detected