MCPcopy
hub / github.com/docker/docker-agent / appendLatestUser

Function appendLatestUser

pkg/chatserver/agent.go:164–189  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

162// usable user message (so the caller can reject it instead of replaying
163// the prior turn).
164func 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

Callers 3

resolveSessionMethod · 0.85
TestAppendLatestUserFunction · 0.85

Calls 3

UserMessageFunction · 0.92
convertPartsFunction · 0.85
AddMessageMethod · 0.65

Tested by 2

TestAppendLatestUserFunction · 0.68