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)
| 228 | // returns the concatenated assistant content alongside the final finish |
| 229 | // reason. The stream is always closed before returning. |
| 230 | func 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", |
no test coverage detected