MCPcopy Index your code
hub / github.com/docker/docker-agent / drainSamplingStream

Function drainSamplingStream

pkg/runtime/sampling.go:230–251  ·  view source on GitHub ↗

drainSamplingStream reads a chat completion stream to completion and returns the concatenated assistant content alongside the final finish reason. The stream is always closed before returning.

(stream chat.MessageStream)

Source from the content-addressed store, hash-verified

228// returns the concatenated assistant content alongside the final finish
229// reason. The stream is always closed before returning.
230func drainSamplingStream(stream chat.MessageStream) (string, chat.FinishReason, error) {
231 defer stream.Close()
232
233 var content strings.Builder
234 var finishReason chat.FinishReason
235 for {
236 response, err := stream.Recv()
237 if errors.Is(err, io.EOF) {
238 return content.String(), finishReason, nil
239 }
240 if err != nil {
241 return "", "", err
242 }
243 if len(response.Choices) > 0 {
244 choice := response.Choices[0]
245 content.WriteString(choice.Delta.Content)
246 if choice.FinishReason != "" {
247 finishReason = choice.FinishReason
248 }
249 }
250 }
251}
252
253// stopReason maps a chat finish reason into the MCP stopReason vocabulary
254// used in CreateMessageResult. Unknown values fall back to "endTurn",

Callers 1

samplingHandlerMethod · 0.85

Calls 3

CloseMethod · 0.65
RecvMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected