resolveSession decides whether to start fresh or continue an existing conversation. When X-Conversation-Id is set and we have an existing session for it, we work on a deep copy and append only the latest user message from the request (the prior history is already in the session). The cached session
(id string, msgs []ChatCompletionMessage)
| 434 | // which case the caller rejects the request rather than replaying the |
| 435 | // prior turn. |
| 436 | func (s *server) resolveSession(id string, msgs []ChatCompletionMessage) (*session.Session, error) { |
| 437 | if id != "" { |
| 438 | if existing := s.conversations.Get(id); existing != nil { |
| 439 | working := existing.Clone() |
| 440 | if !appendLatestUser(working, msgs) { |
| 441 | return nil, errors.New("no user message provided") |
| 442 | } |
| 443 | return working, nil |
| 444 | } |
| 445 | } |
| 446 | sess := buildSession(msgs) |
| 447 | if sess == nil { |
| 448 | return nil, errors.New("no user message provided") |
| 449 | } |
| 450 | return sess, nil |
| 451 | } |
| 452 | |
| 453 | // commitConversation stores the session into the cache after a run, but |
| 454 | // only when the run succeeded. A failed turn must not advance the cached |