-
Notifications
You must be signed in to change notification settings - Fork 949
Expand file tree
/
Copy pathanthropic.go
More file actions
1047 lines (905 loc) · 39.4 KB
/
Copy pathanthropic.go
File metadata and controls
1047 lines (905 loc) · 39.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package anthropic
import (
"bufio"
"context"
"errors"
"fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/bytedance/sonic"
providerUtils "github.com/maximhq/bifrost/core/providers/utils"
schemas "github.com/maximhq/bifrost/core/schemas"
"github.com/valyala/fasthttp"
)
// AnthropicProvider implements the Provider interface for Anthropic's Claude API.
type AnthropicProvider struct {
logger schemas.Logger // Logger for provider operations
client *fasthttp.Client // HTTP client for API requests
apiVersion string // API version for the provider
networkConfig schemas.NetworkConfig // Network configuration including extra headers
sendBackRawRequest bool // Whether to include raw request in BifrostResponse
sendBackRawResponse bool // Whether to include raw response in BifrostResponse
customProviderConfig *schemas.CustomProviderConfig // Custom provider config
}
// anthropicMessageResponsePool provides a pool for Anthropic chat response objects.
var anthropicMessageResponsePool = sync.Pool{
New: func() interface{} {
return &AnthropicMessageResponse{}
},
}
// anthropicTextResponsePool provides a pool for Anthropic text response objects.
var anthropicTextResponsePool = sync.Pool{
New: func() interface{} {
return &AnthropicTextResponse{}
},
}
// AcquireAnthropicMessageResponse gets an Anthropic chat response from the pool.
func AcquireAnthropicMessageResponse() *AnthropicMessageResponse {
resp := anthropicMessageResponsePool.Get().(*AnthropicMessageResponse)
*resp = AnthropicMessageResponse{} // Reset the struct
return resp
}
// ReleaseAnthropicMessageResponse returns an Anthropic chat response to the pool.
func ReleaseAnthropicMessageResponse(resp *AnthropicMessageResponse) {
if resp != nil {
anthropicMessageResponsePool.Put(resp)
}
}
// acquireAnthropicTextResponse gets an Anthropic text response from the pool.
func acquireAnthropicTextResponse() *AnthropicTextResponse {
resp := anthropicTextResponsePool.Get().(*AnthropicTextResponse)
*resp = AnthropicTextResponse{} // Reset the struct
return resp
}
// releaseAnthropicTextResponse returns an Anthropic text response to the pool.
func releaseAnthropicTextResponse(resp *AnthropicTextResponse) {
if resp != nil {
anthropicTextResponsePool.Put(resp)
}
}
// NewAnthropicProvider creates a new Anthropic provider instance.
// It initializes the HTTP client with the provided configuration and sets up response pools.
// The client is configured with timeouts, concurrency limits, and optional proxy settings.
func NewAnthropicProvider(config *schemas.ProviderConfig, logger schemas.Logger) *AnthropicProvider {
config.CheckAndSetDefaults()
client := &fasthttp.Client{
ReadTimeout: time.Second * time.Duration(config.NetworkConfig.DefaultRequestTimeoutInSeconds),
WriteTimeout: time.Second * time.Duration(config.NetworkConfig.DefaultRequestTimeoutInSeconds),
MaxConnsPerHost: 5000,
MaxIdleConnDuration: 60 * time.Second,
MaxConnWaitTimeout: 10 * time.Second,
}
// Pre-warm response pools
for i := 0; i < config.ConcurrencyAndBufferSize.Concurrency; i++ {
anthropicTextResponsePool.Put(&AnthropicTextResponse{})
anthropicMessageResponsePool.Put(&AnthropicMessageResponse{})
}
// Configure proxy if provided
client = providerUtils.ConfigureProxy(client, config.ProxyConfig, logger)
// Set default BaseURL if not provided
if config.NetworkConfig.BaseURL == "" {
config.NetworkConfig.BaseURL = "https://api.anthropic.com"
}
config.NetworkConfig.BaseURL = strings.TrimRight(config.NetworkConfig.BaseURL, "/")
return &AnthropicProvider{
logger: logger,
client: client,
apiVersion: "2023-06-01",
networkConfig: config.NetworkConfig,
sendBackRawRequest: config.SendBackRawRequest,
sendBackRawResponse: config.SendBackRawResponse,
customProviderConfig: config.CustomProviderConfig,
}
}
// GetProviderKey returns the provider identifier for Anthropic.
func (provider *AnthropicProvider) GetProviderKey() schemas.ModelProvider {
return providerUtils.GetProviderName(schemas.Anthropic, provider.customProviderConfig)
}
// buildRequestURL constructs the full request URL using the provider's configuration.
func (provider *AnthropicProvider) buildRequestURL(ctx context.Context, defaultPath string, requestType schemas.RequestType) string {
return provider.networkConfig.BaseURL + providerUtils.GetRequestPath(ctx, defaultPath, provider.customProviderConfig, requestType)
}
// completeRequest sends a request to Anthropic's API and handles the response.
// It constructs the API URL, sets up authentication, and processes the response.
// Returns the response body or an error if the request fails.
func (provider *AnthropicProvider) completeRequest(ctx context.Context, jsonData []byte, url string, key string) ([]byte, time.Duration, *schemas.BifrostError) {
// Create the request with the JSON body
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(resp)
// Set any extra headers from network config
providerUtils.SetExtraHeaders(ctx, req, provider.networkConfig.ExtraHeaders, nil)
req.SetRequestURI(url)
req.Header.SetMethod(http.MethodPost)
req.Header.SetContentType("application/json")
// Can be empty in case of passthrough or keyless custom provider
if key != "" {
req.Header.Set("x-api-key", key)
}
req.Header.Set("anthropic-version", provider.apiVersion)
req.SetBody(jsonData)
// Send the request
latency, bifrostErr := providerUtils.MakeRequestWithContext(ctx, provider.client, req, resp)
if bifrostErr != nil {
return nil, latency, bifrostErr
}
// Handle error response
if resp.StatusCode() != fasthttp.StatusOK {
provider.logger.Debug(fmt.Sprintf("error from %s provider: %s", provider.GetProviderKey(), string(resp.Body())))
return nil, latency, parseAnthropicError(resp)
}
body, err := providerUtils.CheckAndDecodeBody(resp)
if err != nil {
return nil, latency, providerUtils.NewBifrostOperationError(schemas.ErrProviderResponseDecode, err, provider.GetProviderKey())
}
// Read the response body and copy it before releasing the response
// to avoid use-after-free since respBody references fasthttp's internal buffer
bodyCopy := append([]byte(nil), body...)
return bodyCopy, latency, nil
}
// listModelsByKey performs a list models request for a single key.
// Returns the response and latency, or an error if the request fails.
func (provider *AnthropicProvider) listModelsByKey(ctx context.Context, key schemas.Key, request *schemas.BifrostListModelsRequest) (*schemas.BifrostListModelsResponse, *schemas.BifrostError) {
// Create request
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(resp)
// Set any extra headers from network config
providerUtils.SetExtraHeaders(ctx, req, provider.networkConfig.ExtraHeaders, nil)
// Build URL using centralized URL construction
req.SetRequestURI(provider.buildRequestURL(ctx, fmt.Sprintf("/v1/models?limit=%d", schemas.DefaultPageSize), schemas.ListModelsRequest))
req.Header.SetMethod(http.MethodGet)
req.Header.SetContentType("application/json")
if key.Value != "" {
req.Header.Set("x-api-key", key.Value)
}
req.Header.Set("anthropic-version", provider.apiVersion)
// Make request
latency, bifrostErr := providerUtils.MakeRequestWithContext(ctx, provider.client, req, resp)
if bifrostErr != nil {
return nil, bifrostErr
}
// Handle error response
if resp.StatusCode() != fasthttp.StatusOK {
return nil, parseAnthropicError(resp)
}
// Parse Anthropic's response
var anthropicResponse AnthropicListModelsResponse
rawRequest, rawResponse, bifrostErr := providerUtils.HandleProviderResponse(resp.Body(), &anthropicResponse, nil, providerUtils.ShouldSendBackRawRequest(ctx, provider.sendBackRawRequest), providerUtils.ShouldSendBackRawResponse(ctx, provider.sendBackRawResponse))
if bifrostErr != nil {
return nil, bifrostErr
}
// Create final response
response := anthropicResponse.ToBifrostListModelsResponse(provider.GetProviderKey(), key.Models)
response.ExtraFields.Latency = latency.Milliseconds()
// Set raw request if enabled
if providerUtils.ShouldSendBackRawRequest(ctx, provider.sendBackRawRequest) {
response.ExtraFields.RawRequest = rawRequest
}
// Set raw response if enabled
if providerUtils.ShouldSendBackRawResponse(ctx, provider.sendBackRawResponse) {
response.ExtraFields.RawResponse = rawResponse
}
return response, nil
}
// ListModels performs a list models request to Anthropic's API.
// It fetches models using all provided keys and aggregates the results.
// Uses a best-effort approach: continues with remaining keys even if some fail.
// Requests are made concurrently for improved performance.
func (provider *AnthropicProvider) ListModels(ctx context.Context, keys []schemas.Key, request *schemas.BifrostListModelsRequest) (*schemas.BifrostListModelsResponse, *schemas.BifrostError) {
if err := providerUtils.CheckOperationAllowed(schemas.Anthropic, provider.customProviderConfig, schemas.ListModelsRequest); err != nil {
return nil, err
}
if provider.customProviderConfig != nil && provider.customProviderConfig.IsKeyLess {
return provider.listModelsByKey(ctx, schemas.Key{}, request)
}
return providerUtils.HandleMultipleListModelsRequests(
ctx,
keys,
request,
provider.listModelsByKey,
provider.logger,
)
}
// TextCompletion performs a text completion request to Anthropic's API.
// It formats the request, sends it to Anthropic, and processes the response.
// Returns a BifrostResponse containing the completion results or an error if the request fails.
func (provider *AnthropicProvider) TextCompletion(ctx context.Context, key schemas.Key, request *schemas.BifrostTextCompletionRequest) (*schemas.BifrostTextCompletionResponse, *schemas.BifrostError) {
if err := providerUtils.CheckOperationAllowed(schemas.Anthropic, provider.customProviderConfig, schemas.TextCompletionRequest); err != nil {
return nil, err
}
// Convert to Anthropic format using the centralized converter
jsonData, err := providerUtils.CheckContextAndGetRequestBody(
ctx,
request,
func() (any, error) { return ToAnthropicTextCompletionRequest(request), nil },
provider.GetProviderKey())
if err != nil {
return nil, err
}
// Use struct directly for JSON marshaling
responseBody, latency, err := provider.completeRequest(ctx, jsonData, provider.buildRequestURL(ctx, "/v1/complete", schemas.TextCompletionRequest), key.Value)
if err != nil {
return nil, err
}
// Create response object from pool
response := acquireAnthropicTextResponse()
defer releaseAnthropicTextResponse(response)
rawRequest, rawResponse, bifrostErr := providerUtils.HandleProviderResponse(responseBody, response, jsonData, providerUtils.ShouldSendBackRawRequest(ctx, provider.sendBackRawRequest), providerUtils.ShouldSendBackRawResponse(ctx, provider.sendBackRawResponse))
if bifrostErr != nil {
return nil, bifrostErr
}
bifrostResponse := response.ToBifrostTextCompletionResponse()
// Set ExtraFields
bifrostResponse.ExtraFields.Provider = provider.GetProviderKey()
bifrostResponse.ExtraFields.ModelRequested = request.Model
bifrostResponse.ExtraFields.RequestType = schemas.TextCompletionRequest
bifrostResponse.ExtraFields.Latency = latency.Milliseconds()
// Set raw request if enabled
if providerUtils.ShouldSendBackRawRequest(ctx, provider.sendBackRawRequest) {
bifrostResponse.ExtraFields.RawRequest = rawRequest
}
// Set raw response if enabled
if providerUtils.ShouldSendBackRawResponse(ctx, provider.sendBackRawResponse) {
bifrostResponse.ExtraFields.RawResponse = rawResponse
}
return bifrostResponse, nil
}
// TextCompletionStream performs a streaming text completion request to Anthropic's API.
// It formats the request, sends it to Anthropic, and processes the response.
// Returns a channel of BifrostStream objects or an error if the request fails.
func (provider *AnthropicProvider) TextCompletionStream(ctx context.Context, postHookRunner schemas.PostHookRunner, key schemas.Key, request *schemas.BifrostTextCompletionRequest) (chan *schemas.BifrostStream, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.TextCompletionStreamRequest, provider.GetProviderKey())
}
// ChatCompletion performs a chat completion request to Anthropic's API.
// It formats the request, sends it to Anthropic, and processes the response.
// Returns a BifrostResponse containing the completion results or an error if the request fails.
func (provider *AnthropicProvider) ChatCompletion(ctx context.Context, key schemas.Key, request *schemas.BifrostChatRequest) (*schemas.BifrostChatResponse, *schemas.BifrostError) {
if err := providerUtils.CheckOperationAllowed(schemas.Anthropic, provider.customProviderConfig, schemas.ChatCompletionRequest); err != nil {
return nil, err
}
// Convert to Anthropic format using the centralized converter
jsonData, err := providerUtils.CheckContextAndGetRequestBody(
ctx,
request,
func() (any, error) { return ToAnthropicChatRequest(request) },
provider.GetProviderKey())
if err != nil {
return nil, err
}
// Use struct directly for JSON marshaling
responseBody, latency, err := provider.completeRequest(ctx, jsonData, provider.buildRequestURL(ctx, "/v1/messages", schemas.ChatCompletionRequest), key.Value)
if err != nil {
return nil, err
}
// Create response object from pool
response := AcquireAnthropicMessageResponse()
defer ReleaseAnthropicMessageResponse(response)
rawRequest, rawResponse, bifrostErr := providerUtils.HandleProviderResponse(responseBody, response, jsonData, providerUtils.ShouldSendBackRawRequest(ctx, provider.sendBackRawRequest), providerUtils.ShouldSendBackRawResponse(ctx, provider.sendBackRawResponse))
if bifrostErr != nil {
return nil, bifrostErr
}
// Create final response
bifrostResponse := response.ToBifrostChatResponse()
// Set ExtraFields
bifrostResponse.ExtraFields.Provider = provider.GetProviderKey()
bifrostResponse.ExtraFields.ModelRequested = request.Model
bifrostResponse.ExtraFields.RequestType = schemas.ChatCompletionRequest
bifrostResponse.ExtraFields.Latency = latency.Milliseconds()
// Set raw request if enabled
if providerUtils.ShouldSendBackRawRequest(ctx, provider.sendBackRawRequest) {
bifrostResponse.ExtraFields.RawRequest = rawRequest
}
// Set raw response if enabled
if providerUtils.ShouldSendBackRawResponse(ctx, provider.sendBackRawResponse) {
bifrostResponse.ExtraFields.RawResponse = rawResponse
}
return bifrostResponse, nil
}
// ChatCompletionStream performs a streaming chat completion request to the Anthropic API.
// It supports real-time streaming of responses using Server-Sent Events (SSE).
// Returns a channel containing BifrostResponse objects representing the stream or an error if the request fails.
func (provider *AnthropicProvider) ChatCompletionStream(ctx context.Context, postHookRunner schemas.PostHookRunner, key schemas.Key, request *schemas.BifrostChatRequest) (chan *schemas.BifrostStream, *schemas.BifrostError) {
if err := providerUtils.CheckOperationAllowed(schemas.Anthropic, provider.customProviderConfig, schemas.ChatCompletionStreamRequest); err != nil {
return nil, err
}
// Convert to Anthropic format using the centralized converter
jsonData, err := providerUtils.CheckContextAndGetRequestBody(
ctx,
request,
func() (any, error) {
reqBody, err := ToAnthropicChatRequest(request)
if err != nil {
return nil, err
}
reqBody.Stream = schemas.Ptr(true)
return reqBody, nil
},
provider.GetProviderKey())
if err != nil {
return nil, err
}
// Prepare Anthropic headers
headers := map[string]string{
"Content-Type": "application/json",
"anthropic-version": provider.apiVersion,
"Accept": "text/event-stream",
"Cache-Control": "no-cache",
}
if key.Value != "" {
headers["x-api-key"] = key.Value
}
// Use shared Anthropic streaming logic
return HandleAnthropicChatCompletionStreaming(
ctx,
provider.client,
provider.buildRequestURL(ctx, "/v1/messages", schemas.ChatCompletionStreamRequest),
jsonData,
headers,
provider.networkConfig.ExtraHeaders,
providerUtils.ShouldSendBackRawRequest(ctx, provider.sendBackRawRequest),
providerUtils.ShouldSendBackRawResponse(ctx, provider.sendBackRawResponse),
provider.GetProviderKey(),
postHookRunner,
nil,
provider.logger,
)
}
// HandleAnthropicChatCompletionStreaming handles streaming for Anthropic-compatible APIs.
// This shared function reduces code duplication between providers that use the same SSE event format.
func HandleAnthropicChatCompletionStreaming(
ctx context.Context,
client *fasthttp.Client,
url string,
jsonBody []byte,
headers map[string]string,
extraHeaders map[string]string,
sendBackRawRequest bool,
sendBackRawResponse bool,
providerName schemas.ModelProvider,
postHookRunner schemas.PostHookRunner,
postResponseConverter func(*schemas.BifrostChatResponse) *schemas.BifrostChatResponse,
logger schemas.Logger,
) (chan *schemas.BifrostStream, *schemas.BifrostError) {
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
resp.StreamBody = true // Initialize for streaming
defer fasthttp.ReleaseRequest(req)
req.Header.SetMethod(http.MethodPost)
req.SetRequestURI(url)
req.Header.SetContentType("application/json")
providerUtils.SetExtraHeaders(ctx, req, extraHeaders, nil)
// Set headers
for key, value := range headers {
req.Header.Set(key, value)
}
req.SetBody(jsonBody)
// Make the request
err := client.Do(req, resp)
if err != nil {
defer providerUtils.ReleaseStreamingResponse(resp)
if errors.Is(err, context.Canceled) {
return nil, &schemas.BifrostError{
IsBifrostError: false,
Error: &schemas.ErrorField{
Type: schemas.Ptr(schemas.RequestCancelled),
Message: schemas.ErrRequestCancelled,
Error: err,
},
}
}
if errors.Is(err, fasthttp.ErrTimeout) || errors.Is(err, context.DeadlineExceeded) {
return nil, providerUtils.NewBifrostOperationError(schemas.ErrProviderRequestTimedOut, err, providerName)
}
return nil, providerUtils.NewBifrostOperationError(schemas.ErrProviderDoRequest, err, providerName)
}
// Check for HTTP errors
if resp.StatusCode() != fasthttp.StatusOK {
defer providerUtils.ReleaseStreamingResponse(resp)
return nil, parseAnthropicError(resp)
}
// Create response channel
responseChan := make(chan *schemas.BifrostStream, schemas.DefaultStreamBufferSize)
// Start streaming in a goroutine
go func() {
defer close(responseChan)
defer providerUtils.ReleaseStreamingResponse(resp)
if resp.BodyStream() == nil {
bifrostErr := providerUtils.NewBifrostOperationError(
"Provider returned an empty response",
fmt.Errorf("provider returned an empty response"),
providerName,
)
ctx = context.WithValue(ctx, schemas.BifrostContextKeyStreamEndIndicator, true)
providerUtils.ProcessAndSendBifrostError(ctx, postHookRunner, bifrostErr, responseChan, logger)
return
}
scanner := bufio.NewScanner(resp.BodyStream())
buf := make([]byte, 0, 1024*1024)
scanner.Buffer(buf, 10*1024*1024)
chunkIndex := 0
startTime := time.Now()
lastChunkTime := startTime
// Track minimal state needed for response format
var messageID string
var modelName string
var finishReason *string
usage := &schemas.BifrostLLMUsage{}
// Track SSE event parsing state
var eventType string
var eventData string
for scanner.Scan() {
line := scanner.Text()
// Skip empty lines and comments
if line == "" || strings.HasPrefix(line, ":") {
continue
}
// Parse SSE event - track event type and data separately
if after, ok := strings.CutPrefix(line, "event: "); ok {
eventType = after
continue
} else if strings.HasPrefix(line, "data: ") {
eventData = strings.TrimPrefix(line, "data: ")
} else {
continue
}
// Skip if we don't have both event type and data
if eventType == "" || eventData == "" {
continue
}
var event AnthropicStreamEvent
if err := sonic.Unmarshal([]byte(eventData), &event); err != nil {
logger.Warn(fmt.Sprintf("Failed to parse message_start event: %v", err))
continue
}
if event.Type == AnthropicStreamEventTypeMessageStart && event.Message != nil && event.Message.ID != "" {
messageID = event.Message.ID
}
// Check for usage in both top-level event.Usage and nested event.Message.Usage
// message_start events have usage nested in message.usage, while message_delta has it at top level
var usageToProcess *AnthropicUsage
if event.Usage != nil {
usageToProcess = event.Usage
} else if event.Message != nil && event.Message.Usage != nil {
usageToProcess = event.Message.Usage
}
if usageToProcess != nil {
// Collect usage information and send at the end of the stream
// Here in some cases usage comes before final message
// So we need to check if the response.Usage is nil and then if usage != nil
// then add up all tokens
if usageToProcess.InputTokens > usage.PromptTokens {
usage.PromptTokens = usageToProcess.InputTokens
}
if usageToProcess.OutputTokens > usage.CompletionTokens {
usage.CompletionTokens = usageToProcess.OutputTokens
}
calculatedTotal := usage.PromptTokens + usage.CompletionTokens
if calculatedTotal > usage.TotalTokens {
usage.TotalTokens = calculatedTotal
}
// Handle cached tokens if present
if usageToProcess.CacheReadInputTokens > 0 {
if usage.PromptTokensDetails == nil {
usage.PromptTokensDetails = &schemas.ChatPromptTokensDetails{}
}
if usageToProcess.CacheReadInputTokens > usage.PromptTokensDetails.CachedTokens {
usage.PromptTokensDetails.CachedTokens = usageToProcess.CacheReadInputTokens
}
}
// Handle cached tokens if present
if usageToProcess.CacheCreationInputTokens > 0 {
if usage.CompletionTokensDetails == nil {
usage.CompletionTokensDetails = &schemas.ChatCompletionTokensDetails{}
}
if usageToProcess.CacheCreationInputTokens > usage.CompletionTokensDetails.CachedTokens {
usage.CompletionTokensDetails.CachedTokens = usageToProcess.CacheCreationInputTokens
}
}
}
if event.Delta != nil && event.Delta.StopReason != nil {
mappedReason := ConvertAnthropicFinishReasonToBifrost(*event.Delta.StopReason)
finishReason = &mappedReason
}
if event.Message != nil {
// Handle different event types
modelName = event.Message.Model
}
response, bifrostErr, isLastChunk := event.ToBifrostChatCompletionStream()
if bifrostErr != nil {
bifrostErr.ExtraFields = schemas.BifrostErrorExtraFields{
RequestType: schemas.ChatCompletionStreamRequest,
Provider: providerName,
ModelRequested: modelName,
}
ctx = context.WithValue(ctx, schemas.BifrostContextKeyStreamEndIndicator, true)
providerUtils.ProcessAndSendBifrostError(ctx, postHookRunner, bifrostErr, responseChan, logger)
break
}
if response != nil {
response.ExtraFields = schemas.BifrostResponseExtraFields{
RequestType: schemas.ChatCompletionStreamRequest,
Provider: providerName,
ModelRequested: modelName,
ChunkIndex: chunkIndex,
Latency: time.Since(lastChunkTime).Milliseconds(),
}
if postResponseConverter != nil {
response = postResponseConverter(response)
if response == nil {
logger.Warn("postResponseConverter returned nil; skipping chunk")
continue
}
}
response.ID = messageID
lastChunkTime = time.Now()
chunkIndex++
if sendBackRawResponse {
response.ExtraFields.RawResponse = eventData
}
providerUtils.ProcessAndSendResponse(ctx, postHookRunner, providerUtils.GetBifrostResponseForStreamResponse(nil, response, nil, nil, nil), responseChan)
}
if isLastChunk {
break
}
// Reset for next event
eventType = ""
eventData = ""
}
if err := scanner.Err(); err != nil {
logger.Warn(fmt.Sprintf("Error reading %s stream: %v", providerName, err))
providerUtils.ProcessAndSendError(ctx, postHookRunner, err, responseChan, schemas.ChatCompletionStreamRequest, providerName, modelName, logger)
} else {
response := providerUtils.CreateBifrostChatCompletionChunkResponse(messageID, usage, finishReason, chunkIndex, schemas.ChatCompletionStreamRequest, providerName, modelName)
if postResponseConverter != nil {
response = postResponseConverter(response)
if response == nil {
logger.Warn("postResponseConverter returned nil; skipping chunk")
return
}
}
// Set raw request if enabled
if sendBackRawRequest {
providerUtils.ParseAndSetRawRequest(&response.ExtraFields, jsonBody)
}
response.ExtraFields.Latency = time.Since(startTime).Milliseconds()
ctx = context.WithValue(ctx, schemas.BifrostContextKeyStreamEndIndicator, true)
providerUtils.ProcessAndSendResponse(ctx, postHookRunner, providerUtils.GetBifrostResponseForStreamResponse(nil, response, nil, nil, nil), responseChan)
}
}()
return responseChan, nil
}
// Responses performs a chat completion request to Anthropic's API.
// It formats the request, sends it to Anthropic, and processes the response.
// Returns a BifrostResponse containing the completion results or an error if the request fails.
func (provider *AnthropicProvider) Responses(ctx context.Context, key schemas.Key, request *schemas.BifrostResponsesRequest) (*schemas.BifrostResponsesResponse, *schemas.BifrostError) {
if err := providerUtils.CheckOperationAllowed(schemas.Anthropic, provider.customProviderConfig, schemas.ResponsesRequest); err != nil {
if ctx, shouldFallback := providerUtils.ShouldAttemptIntegrationFallback(ctx); shouldFallback {
chatResponse, err := provider.ChatCompletion(ctx, key, request.ToChatRequest())
if err != nil {
return nil, err
}
response := chatResponse.ToBifrostResponsesResponse()
response.ExtraFields.RequestType = schemas.ResponsesRequest
response.ExtraFields.Provider = provider.GetProviderKey()
response.ExtraFields.ModelRequested = request.Model
return response, nil
}
return nil, err
}
jsonBody, err := getRequestBodyForResponses(ctx, request, provider.GetProviderKey(), false)
if err != nil {
return nil, err
}
// Use struct directly for JSON marshaling
responseBody, latency, err := provider.completeRequest(ctx, jsonBody, provider.buildRequestURL(ctx, "/v1/messages", schemas.ResponsesRequest), key.Value)
if err != nil {
return nil, err
}
// Create response object from pool
response := AcquireAnthropicMessageResponse()
defer ReleaseAnthropicMessageResponse(response)
rawRequest, rawResponse, bifrostErr := providerUtils.HandleProviderResponse(responseBody, response, jsonBody, providerUtils.ShouldSendBackRawRequest(ctx, provider.sendBackRawRequest), providerUtils.ShouldSendBackRawResponse(ctx, provider.sendBackRawResponse))
if bifrostErr != nil {
return nil, bifrostErr
}
// Create final response
bifrostResponse := response.ToBifrostResponsesResponse()
// Set ExtraFields
bifrostResponse.ExtraFields.Provider = provider.GetProviderKey()
bifrostResponse.ExtraFields.ModelRequested = request.Model
bifrostResponse.ExtraFields.RequestType = schemas.ResponsesRequest
bifrostResponse.ExtraFields.Latency = latency.Milliseconds()
// Set raw request if enabled
if providerUtils.ShouldSendBackRawRequest(ctx, provider.sendBackRawRequest) {
bifrostResponse.ExtraFields.RawRequest = rawRequest
}
// Set raw response if enabled
if providerUtils.ShouldSendBackRawResponse(ctx, provider.sendBackRawResponse) {
bifrostResponse.ExtraFields.RawResponse = rawResponse
}
return bifrostResponse, nil
}
// ResponsesStream performs a streaming responses request to the Anthropic API.
func (provider *AnthropicProvider) ResponsesStream(ctx context.Context, postHookRunner schemas.PostHookRunner, key schemas.Key, request *schemas.BifrostResponsesRequest) (chan *schemas.BifrostStream, *schemas.BifrostError) {
if err := providerUtils.CheckOperationAllowed(schemas.Anthropic, provider.customProviderConfig, schemas.ResponsesStreamRequest); err != nil {
if ctx, shouldFallback := providerUtils.ShouldAttemptIntegrationFallback(ctx); shouldFallback {
ctx = context.WithValue(ctx, schemas.BifrostContextKeyIsResponsesToChatCompletionFallback, true)
return provider.ChatCompletionStream(
ctx,
postHookRunner,
key,
request.ToChatRequest(),
)
}
return nil, err
}
// Convert to Anthropic format using the centralized converter
jsonBody, err := getRequestBodyForResponses(ctx, request, provider.GetProviderKey(), true)
if err != nil {
return nil, err
}
// Prepare Anthropic headers
headers := map[string]string{
"Content-Type": "application/json",
"anthropic-version": provider.apiVersion,
"Accept": "text/event-stream",
"Cache-Control": "no-cache",
}
if key.Value != "" {
headers["x-api-key"] = key.Value
}
return HandleAnthropicResponsesStream(
ctx,
provider.client,
provider.buildRequestURL(ctx, "/v1/messages", schemas.ResponsesStreamRequest),
jsonBody,
headers,
provider.networkConfig.ExtraHeaders,
providerUtils.ShouldSendBackRawRequest(ctx, provider.sendBackRawRequest),
providerUtils.ShouldSendBackRawResponse(ctx, provider.sendBackRawResponse),
provider.GetProviderKey(),
postHookRunner,
nil,
provider.logger,
)
}
// HandleAnthropicResponsesStream handles streaming for Anthropic-compatible APIs.
// This shared function reduces code duplication between providers that use the same SSE event format.
func HandleAnthropicResponsesStream(
ctx context.Context,
client *fasthttp.Client,
url string,
jsonBody []byte,
headers map[string]string,
extraHeaders map[string]string,
sendBackRawRequest bool,
sendBackRawResponse bool,
providerName schemas.ModelProvider,
postHookRunner schemas.PostHookRunner,
postResponseConverter func(*schemas.BifrostResponsesStreamResponse) *schemas.BifrostResponsesStreamResponse,
logger schemas.Logger,
) (chan *schemas.BifrostStream, *schemas.BifrostError) {
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
resp.StreamBody = true
defer fasthttp.ReleaseRequest(req)
req.Header.SetMethod(http.MethodPost)
req.SetRequestURI(url)
req.Header.SetContentType("application/json")
providerUtils.SetExtraHeaders(ctx, req, extraHeaders, nil)
for key, value := range headers {
req.Header.Set(key, value)
}
// Set body
req.SetBody(jsonBody)
// Make the request
err := client.Do(req, resp)
if err != nil {
defer providerUtils.ReleaseStreamingResponse(resp)
if errors.Is(err, context.Canceled) {
return nil, &schemas.BifrostError{
IsBifrostError: false,
Error: &schemas.ErrorField{
Type: schemas.Ptr(schemas.RequestCancelled),
Message: schemas.ErrRequestCancelled,
Error: err,
},
}
}
if errors.Is(err, fasthttp.ErrTimeout) || errors.Is(err, context.DeadlineExceeded) {
return nil, providerUtils.NewBifrostOperationError(schemas.ErrProviderRequestTimedOut, err, providerName)
}
return nil, providerUtils.NewBifrostOperationError(schemas.ErrProviderDoRequest, err, providerName)
}
// Check for HTTP errors
if resp.StatusCode() != fasthttp.StatusOK {
defer providerUtils.ReleaseStreamingResponse(resp)
return nil, parseAnthropicError(resp)
}
// Create response channel
responseChan := make(chan *schemas.BifrostStream, schemas.DefaultStreamBufferSize)
// Start streaming in a goroutine
go func() {
defer providerUtils.ReleaseStreamingResponse(resp)
defer close(responseChan)
if resp.BodyStream() == nil {
bifrostErr := providerUtils.NewBifrostOperationError(
"Provider returned an empty response",
fmt.Errorf("provider returned an empty response"),
providerName,
)
ctx = context.WithValue(ctx, schemas.BifrostContextKeyStreamEndIndicator, true)
providerUtils.ProcessAndSendBifrostError(ctx, postHookRunner, bifrostErr, responseChan, logger)
return
}
scanner := bufio.NewScanner(resp.BodyStream())
chunkIndex := 0
startTime := time.Now()
lastChunkTime := startTime
// Track minimal state needed for response format
usage := &schemas.ResponsesResponseUsage{}
// Create stream state for stateful conversions
streamState := acquireAnthropicResponsesStreamState()
defer releaseAnthropicResponsesStreamState(streamState)
// Track SSE event parsing state
var eventType string
var eventData string
var modelName string
for scanner.Scan() {
line := scanner.Text()
// Skip empty lines and comments
if line == "" || strings.HasPrefix(line, ":") {
continue
}
// Parse SSE event - track event type and data separately
if after, ok := strings.CutPrefix(line, "event: "); ok {
eventType = after
continue
} else if strings.HasPrefix(line, "data: ") {
eventData = strings.TrimPrefix(line, "data: ")
} else {
continue
}
// Skip if we don't have both event type and data
if eventType == "" || eventData == "" {
continue
}
var event AnthropicStreamEvent
if err := sonic.Unmarshal([]byte(eventData), &event); err != nil {
logger.Warn(fmt.Sprintf("Failed to parse message_start event: %v", err))
continue
}
if event.Message != nil && modelName == "" {
modelName = event.Message.Model
}
// Note: response.created and response.in_progress are now emitted by ToBifrostResponsesStream
// from the message_start event, so we don't need to call them manually here
// Check for usage in both top-level event.Usage and nested event.Message.Usage
// message_start events have usage nested in message.usage, while message_delta has it at top level
var usageToProcess *AnthropicUsage
if event.Usage != nil {
usageToProcess = event.Usage
} else if event.Message != nil && event.Message.Usage != nil {
usageToProcess = event.Message.Usage
}
if usageToProcess != nil {
// Collect usage information and send at the end of the stream
// Here in some cases usage comes before final message
// So we need to check if the response.Usage is nil and then if usage != nil
// then add up all tokens
if usageToProcess.InputTokens > usage.InputTokens {
usage.InputTokens = usageToProcess.InputTokens
}
if usageToProcess.OutputTokens > usage.OutputTokens {
usage.OutputTokens = usageToProcess.OutputTokens
}
calculatedTotal := usage.InputTokens + usage.OutputTokens
if calculatedTotal > usage.TotalTokens {
usage.TotalTokens = calculatedTotal
}
// Handle cached tokens if present
if usageToProcess.CacheReadInputTokens > 0 {
if usage.InputTokensDetails == nil {
usage.InputTokensDetails = &schemas.ResponsesResponseInputTokens{}
}
if usageToProcess.CacheReadInputTokens > usage.InputTokensDetails.CachedTokens {
usage.InputTokensDetails.CachedTokens = usageToProcess.CacheReadInputTokens
}
}
// Handle cached tokens if present
if usageToProcess.CacheCreationInputTokens > 0 {
if usage.OutputTokensDetails == nil {
usage.OutputTokensDetails = &schemas.ResponsesResponseOutputTokens{}
}
if usageToProcess.CacheCreationInputTokens > usage.OutputTokensDetails.CachedTokens {
usage.OutputTokensDetails.CachedTokens = usageToProcess.CacheCreationInputTokens
}
}
}
responses, bifrostErr, isLastChunk := event.ToBifrostResponsesStream(ctx, chunkIndex, streamState)
if bifrostErr != nil {
bifrostErr.ExtraFields = schemas.BifrostErrorExtraFields{
RequestType: schemas.ResponsesStreamRequest,
Provider: providerName,
ModelRequested: modelName,
}
ctx = context.WithValue(ctx, schemas.BifrostContextKeyStreamEndIndicator, true)
providerUtils.ProcessAndSendBifrostError(ctx, postHookRunner, bifrostErr, responseChan, logger)
break
}
// Handle each response in the slice
for i, response := range responses {
if response != nil {
response.ExtraFields = schemas.BifrostResponseExtraFields{
RequestType: schemas.ResponsesStreamRequest,
Provider: providerName,
ModelRequested: modelName,
ChunkIndex: chunkIndex,
Latency: time.Since(lastChunkTime).Milliseconds(),
}
if postResponseConverter != nil {
response = postResponseConverter(response)
if response == nil {
logger.Warn("postResponseConverter returned nil; skipping chunk")
continue
}
}
lastChunkTime = time.Now()
chunkIndex++
// Only add raw response to the last chunk of the incoming event
if providerUtils.ShouldSendBackRawResponse(ctx, sendBackRawResponse) && i == len(responses)-1 {
response.ExtraFields.RawResponse = eventData
}
if isLastChunk && i == len(responses)-1 {
if response.Response == nil {
response.Response = &schemas.BifrostResponsesResponse{}
}
response.Response.Usage = usage
// Set raw request if enabled
if sendBackRawRequest {
providerUtils.ParseAndSetRawRequest(&response.ExtraFields, jsonBody)
}