(ctx context.Context, w http.ResponseWriter, resp anthropicResponse)
| 569 | } |
| 570 | |
| 571 | func (s *Server) sendAnthropicStream(ctx context.Context, w http.ResponseWriter, resp anthropicResponse) { |
| 572 | w.Header().Set("Content-Type", "text/event-stream") |
| 573 | w.Header().Set("Cache-Control", "no-cache") |
| 574 | w.Header().Set("Connection", "keep-alive") |
| 575 | w.Header().Set("anthropic-version", "2023-06-01") |
| 576 | w.WriteHeader(http.StatusOK) |
| 577 | |
| 578 | flusher, ok := w.(http.Flusher) |
| 579 | if !ok { |
| 580 | s.logger.Error(ctx, "responseWriter does not support flushing", |
| 581 | slog.F("response_id", resp.ID), |
| 582 | ) |
| 583 | return |
| 584 | } |
| 585 | |
| 586 | writeChunk := func(eventType string, data []byte) bool { |
| 587 | if _, err := fmt.Fprintf(w, "event: %s\ndata: %s\n\n", eventType, data); err != nil { |
| 588 | s.logger.Error(ctx, "failed to write Anthropic stream chunk", |
| 589 | slog.F("response_id", resp.ID), |
| 590 | slog.Error(err), |
| 591 | slog.F("error_type", "write_error"), |
| 592 | slog.F("likely_cause", "network_error"), |
| 593 | ) |
| 594 | return false |
| 595 | } |
| 596 | flusher.Flush() |
| 597 | return true |
| 598 | } |
| 599 | |
| 600 | startEventType := "message_start" |
| 601 | startEvent := map[string]interface{}{ |
| 602 | "type": startEventType, |
| 603 | "message": map[string]interface{}{ |
| 604 | "id": resp.ID, |
| 605 | "type": resp.Type, |
| 606 | "role": resp.Role, |
| 607 | "model": resp.Model, |
| 608 | }, |
| 609 | } |
| 610 | startBytes, _ := json.Marshal(startEvent) |
| 611 | if !writeChunk(startEventType, startBytes) { |
| 612 | return |
| 613 | } |
| 614 | |
| 615 | // Send content_block_start event |
| 616 | contentStartEventType := "content_block_start" |
| 617 | contentStartEvent := map[string]interface{}{ |
| 618 | "type": contentStartEventType, |
| 619 | "index": 0, |
| 620 | "content_block": map[string]interface{}{ |
| 621 | "type": "text", |
| 622 | "text": resp.Content[0].Text, |
| 623 | }, |
| 624 | } |
| 625 | contentStartBytes, _ := json.Marshal(contentStartEvent) |
| 626 | if !writeChunk(contentStartEventType, contentStartBytes) { |
| 627 | return |
| 628 | } |
no test coverage detected