Skip to content

Commit da4192c

Browse files
committed
refactor(cli): detect streaming responses via Content-Type header
Signed-off-by: Dorin Geman <dorin.geman@docker.com>
1 parent a9b0858 commit da4192c

1 file changed

Lines changed: 9 additions & 37 deletions

File tree

cmd/cli/desktop/desktop.go

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -440,25 +440,18 @@ func (c *Client) ChatWithMessagesContext(ctx context.Context, model string, conv
440440
TotalTokens int `json:"total_tokens"`
441441
}
442442

443-
// Read the first line to detect if this is SSE streaming or a regular JSON response
444-
reader := bufio.NewReader(resp.Body)
445-
firstLine, err := reader.ReadString('\n')
446-
if err != nil && !errors.Is(err, io.EOF) {
447-
return assistantResponse.String(), fmt.Errorf("error reading response: %w", err)
448-
}
449-
firstLine = strings.TrimSpace(firstLine)
443+
// Detect streaming vs non-streaming response via Content-Type header
444+
isStreaming := strings.HasPrefix(resp.Header.Get("Content-Type"), "text/event-stream")
450445

451-
// Check if this is a non-streaming JSON response (doesn't start with "data: ")
452-
if firstLine != "" && !strings.HasPrefix(firstLine, "data: ") {
453-
// This might be a regular JSON response - read the rest and try to parse it
454-
restOfBody, readErr := io.ReadAll(reader)
455-
if readErr != nil {
456-
return assistantResponse.String(), fmt.Errorf("error reading response body: %w", readErr)
446+
if !isStreaming {
447+
// Non-streaming JSON response
448+
body, err := io.ReadAll(resp.Body)
449+
if err != nil {
450+
return assistantResponse.String(), fmt.Errorf("error reading response body: %w", err)
457451
}
458-
fullBody := firstLine + string(restOfBody)
459452

460453
var nonStreamResp OpenAIChatResponse
461-
if err := json.Unmarshal([]byte(fullBody), &nonStreamResp); err != nil {
454+
if err := json.Unmarshal(body, &nonStreamResp); err != nil {
462455
return assistantResponse.String(), fmt.Errorf("error parsing response: %w", err)
463456
}
464457

@@ -474,28 +467,7 @@ func (c *Client) ChatWithMessagesContext(ctx context.Context, model string, conv
474467
}
475468
} else {
476469
// SSE streaming response - process line by line
477-
scanner := bufio.NewScanner(reader)
478-
479-
// Process the first line if it was SSE data
480-
if strings.HasPrefix(firstLine, "data: ") {
481-
data := strings.TrimPrefix(firstLine, "data: ")
482-
if data != "[DONE]" {
483-
var streamResp OpenAIChatResponse
484-
if err := json.Unmarshal([]byte(data), &streamResp); err == nil {
485-
if streamResp.Usage != nil {
486-
finalUsage = streamResp.Usage
487-
}
488-
if len(streamResp.Choices) > 0 {
489-
if streamResp.Choices[0].Delta.Content != "" {
490-
chunk := streamResp.Choices[0].Delta.Content
491-
printerState = chatPrinterContent
492-
outputFunc(chunk)
493-
assistantResponse.WriteString(chunk)
494-
}
495-
}
496-
}
497-
}
498-
}
470+
scanner := bufio.NewScanner(resp.Body)
499471

500472
for scanner.Scan() {
501473
// Check if context was cancelled

0 commit comments

Comments
 (0)