(w http.ResponseWriter, req *OpenAIRequest, resp OpenAIResponse)
| 291 | } |
| 292 | |
| 293 | func (s *openAIServer) writeResponsesAPIResponse(w http.ResponseWriter, req *OpenAIRequest, resp OpenAIResponse) { |
| 294 | if resp.Error != nil { |
| 295 | writeErrorResponse(s.t, w, resp.Error) |
| 296 | return |
| 297 | } |
| 298 | |
| 299 | hasStreaming := resp.StreamingChunks != nil |
| 300 | hasNonStreaming := resp.Response != nil |
| 301 | |
| 302 | switch { |
| 303 | case hasStreaming && hasNonStreaming: |
| 304 | http.Error(w, "handler returned both streaming and non-streaming responses", http.StatusInternalServerError) |
| 305 | return |
| 306 | case !hasStreaming && !hasNonStreaming: |
| 307 | http.Error(w, "handler returned empty response", http.StatusInternalServerError) |
| 308 | return |
| 309 | case req.Stream && !hasStreaming: |
| 310 | http.Error(w, "handler returned non-streaming response for streaming request", http.StatusInternalServerError) |
| 311 | return |
| 312 | case !req.Stream && !hasNonStreaming: |
| 313 | http.Error(w, "handler returned streaming response for non-streaming request", http.StatusInternalServerError) |
| 314 | return |
| 315 | case hasStreaming: |
| 316 | writeResponsesAPIStreaming(s.t, w, req.Request, resp) |
| 317 | default: |
| 318 | s.writeResponsesAPINonStreaming(w, resp.Response) |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | func writeChatCompletionsStreaming(w http.ResponseWriter, r *http.Request, chunks <-chan OpenAIChunk) { |
| 323 | w.Header().Set("Content-Type", "text/event-stream") |
no test coverage detected