appendLatestUser walks msgs from the end and appends only the last user-role message into sess. Used by conversation continuation, where the session already contains the full prior history and we just need to inject what the client just said. It returns true when a user message was found and appende
(sess *session.Session, msgs []ChatCompletionMessage)
| 162 | // usable user message (so the caller can reject it instead of replaying |
| 163 | // the prior turn). |
| 164 | func appendLatestUser(sess *session.Session, msgs []ChatCompletionMessage) bool { |
| 165 | for _, m := range slices.Backward(msgs) { |
| 166 | role := strings.ToLower(strings.TrimSpace(m.Role)) |
| 167 | // Treat any non-system/assistant/tool role as user (matches |
| 168 | // buildSession's policy). |
| 169 | if role == "system" || role == "assistant" || role == "tool" { |
| 170 | continue |
| 171 | } |
| 172 | parts := convertParts(m.Parts) |
| 173 | if len(parts) > 0 { |
| 174 | sess.AddMessage(&session.Message{Message: chat.Message{ |
| 175 | Role: chat.MessageRoleUser, |
| 176 | Content: m.Content, |
| 177 | MultiContent: parts, |
| 178 | }}) |
| 179 | return true |
| 180 | } |
| 181 | content := strings.TrimSpace(m.Content) |
| 182 | if content == "" { |
| 183 | continue |
| 184 | } |
| 185 | sess.AddMessage(session.UserMessage(m.Content)) |
| 186 | return true |
| 187 | } |
| 188 | return false |
| 189 | } |
| 190 | |
| 191 | // agentEmit collects the side-effect callbacks invoked by runAgentLoop as |
| 192 | // it drives the runtime. All callbacks are optional; nil means "ignore |