Skip to content

Commit 6bc43bd

Browse files
review
1 parent a99e0fb commit 6bc43bd

2 files changed

Lines changed: 54 additions & 21 deletions

File tree

internal/provider/anthropic/anthropic.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package anthropic
22

33
import (
44
"encoding/json"
5-
"fmt"
5+
"strings"
66
)
77

88
type Metadata struct {
@@ -49,11 +49,44 @@ func (f *FlexContent) String() string {
4949
return f.Text
5050
}
5151
if len(f.Raw) > 0 {
52-
return fmt.Sprintf("%v", f.Raw)
52+
var builder strings.Builder
53+
for _, block := range f.Raw {
54+
builder.WriteString(extractAnthropicContentText(block))
55+
}
56+
return builder.String()
5357
}
5458
return ""
5559
}
5660

61+
func extractAnthropicContentText(value any) string {
62+
switch v := value.(type) {
63+
case string:
64+
return v
65+
case []any:
66+
var builder strings.Builder
67+
for _, item := range v {
68+
builder.WriteString(extractAnthropicContentText(item))
69+
}
70+
return builder.String()
71+
case map[string]any:
72+
if text, ok := v["text"].(string); ok {
73+
return text
74+
}
75+
if content, ok := v["content"].(string); ok {
76+
return content
77+
}
78+
if raw, ok := v["raw"].([]any); ok {
79+
var builder strings.Builder
80+
for _, item := range raw {
81+
builder.WriteString(extractAnthropicContentText(item))
82+
}
83+
return builder.String()
84+
}
85+
}
86+
87+
return ""
88+
}
89+
5790
type Message struct {
5891
Content FlexContent `json:"content"`
5992
Role string `json:"role"`

internal/server/web/proxy/middleware.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
417417
err = json.Unmarshal(body, cr)
418418
if err != nil {
419419
logError(logWithCid, "error when unmarshalling anthropic completion request", prod, err)
420-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling anthropic completion request")
420+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling anthropic completion request")
421421
c.Abort()
422422
return
423423
}
@@ -444,7 +444,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
444444
err = json.Unmarshal(body, cr)
445445
if err != nil {
446446
logError(logWithCid, "error when unmarshalling bedrock anthropic completion request", prod, err)
447-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling bedrock anthropic completion request")
447+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling bedrock anthropic completion request")
448448
c.Abort()
449449
return
450450
}
@@ -471,7 +471,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
471471
err = json.Unmarshal(body, mr)
472472
if err != nil {
473473
logError(logWithCid, "error when unmarshalling anthropic messages request", prod, err)
474-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling anthropic messages request")
474+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling anthropic messages request")
475475
c.Abort()
476476
return
477477
}
@@ -496,7 +496,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
496496
err = json.Unmarshal(body, mr)
497497
if err != nil {
498498
logError(logWithCid, "error when unmarshalling anthropic messages request", prod, err)
499-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling anthropic messages request")
499+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling anthropic messages request")
500500
c.Abort()
501501
return
502502
}
@@ -634,7 +634,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
634634
err = json.Unmarshal(body, ccr)
635635
if err != nil {
636636
logError(logWithCid, "error when unmarshalling vllm chat completions request", prod, err)
637-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling vllm chat completions request")
637+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling vllm chat completions request")
638638
c.Abort()
639639
return
640640
}
@@ -657,7 +657,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
657657
err = json.Unmarshal(body, cr)
658658
if err != nil {
659659
logError(logWithCid, "error when unmarshalling vllm completions request", prod, err)
660-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling vllm completions request")
660+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling vllm completions request")
661661
c.Abort()
662662
return
663663
}
@@ -680,7 +680,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
680680
err = json.Unmarshal(body, ccr)
681681
if err != nil {
682682
logError(logWithCid, "error when unmarshalling deepinfra chat completions request", prod, err)
683-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling deepinfra chat completions request")
683+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling deepinfra chat completions request")
684684
c.Abort()
685685
return
686686
}
@@ -702,7 +702,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
702702
err = json.Unmarshal(body, cr)
703703
if err != nil {
704704
logError(logWithCid, "error when unmarshalling deepinfra completions request", prod, err)
705-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling deepinfra completions request")
705+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling deepinfra completions request")
706706
c.Abort()
707707
return
708708
}
@@ -724,7 +724,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
724724
err = json.Unmarshal(body, er)
725725
if err != nil {
726726
logError(logWithCid, "error when unmarshalling deepinfra embeddings request", prod, err)
727-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling deepinfra embeddings request")
727+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling deepinfra embeddings request")
728728
c.Abort()
729729
return
730730
}
@@ -743,7 +743,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
743743
err = json.Unmarshal(body, ccr)
744744
if err != nil {
745745
logError(logWithCid, "error when unmarshalling azure openai chat completion request", prod, err)
746-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling azure openai chat completion request")
746+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling azure openai chat completion request")
747747
c.Abort()
748748
return
749749
}
@@ -766,7 +766,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
766766
err = json.Unmarshal(body, cr)
767767
if err != nil {
768768
logError(logWithCid, "error when unmarshalling azure openai completions request", prod, err)
769-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling azure openai completions request")
769+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling azure openai completions request")
770770
c.Abort()
771771
return
772772
}
@@ -789,7 +789,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
789789
err = json.Unmarshal(body, er)
790790
if err != nil {
791791
logError(logWithCid, "error when unmarshalling azure openai embedding request", prod, err)
792-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling azure openai embedding request")
792+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling azure openai embedding request")
793793
c.Abort()
794794
return
795795
}
@@ -815,7 +815,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
815815
err = json.Unmarshal([]byte(cleaned), ccr)
816816
if err != nil {
817817
logError(logWithCid, "error when unmarshalling chat completion request", prod, err)
818-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling chat completion request")
818+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling chat completion request")
819819
c.Abort()
820820
return
821821
}
@@ -840,7 +840,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
840840
err = json.Unmarshal(body, responsesReq)
841841
if err != nil {
842842
logError(logWithCid, "error when unmarshalling openai responses request", prod, err)
843-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling openai responses request")
843+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling openai responses request")
844844
c.Abort()
845845
return
846846
}
@@ -906,7 +906,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
906906
err = json.Unmarshal(body, er)
907907
if err != nil {
908908
logError(logWithCid, "error when unmarshalling embedding request", prod, err)
909-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling embedding request")
909+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling embedding request")
910910
c.Abort()
911911
return
912912
}
@@ -926,7 +926,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
926926
err := json.Unmarshal(body, ir)
927927
if err != nil {
928928
logError(logWithCid, "error when unmarshalling create image request", prod, err)
929-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling create image request")
929+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling create image request")
930930
c.Abort()
931931
return
932932
}
@@ -947,7 +947,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
947947
err := json.Unmarshal(body, ier)
948948
if err != nil {
949949
logError(logWithCid, "error when unmarshalling edit image request", prod, err)
950-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling edit image request")
950+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling edit image request")
951951
c.Abort()
952952
return
953953
}
@@ -977,7 +977,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
977977
err := json.Unmarshal(body, ir)
978978
if err != nil {
979979
logError(logWithCid, "error when unmarshalling image variations request", prod, err)
980-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling image variations request")
980+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling image variations request")
981981
c.Abort()
982982
return
983983
}
@@ -1006,7 +1006,7 @@ func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManag
10061006
err := json.Unmarshal(body, sr)
10071007
if err != nil {
10081008
logError(logWithCid, "error when unmarshalling create speech request", prod, err)
1009-
JSON(c, http.StatusInternalServerError, "[BricksLLM] error when unmarshalling create speech request")
1009+
JSON(c, http.StatusBadRequest, "[BricksLLM] error when unmarshalling create speech request")
10101010
c.Abort()
10111011
return
10121012
}

0 commit comments

Comments
 (0)