fix codex context length stream errors
This commit is contained in:
@@ -14,6 +14,8 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
. "github.com/router-for-me/CLIProxyAPI/v7/internal/constant"
|
||||
@@ -257,6 +259,15 @@ func (h *ClaudeCodeAPIHandler) handleStreamingResponse(c *gin.Context, rawJSON [
|
||||
return
|
||||
case chunk, ok := <-dataChan:
|
||||
if !ok {
|
||||
if errMsg, okPendingErr := pendingClaudeStreamError(errChan); okPendingErr {
|
||||
h.WriteErrorResponse(c, errMsg)
|
||||
if errMsg != nil {
|
||||
cliCancel(errMsg.Error)
|
||||
} else {
|
||||
cliCancel(nil)
|
||||
}
|
||||
return
|
||||
}
|
||||
// Stream closed without data? Send DONE or just headers.
|
||||
setSSEHeaders()
|
||||
handlers.WriteUpstreamHeaders(c.Writer.Header(), upstreamHeaders)
|
||||
@@ -282,6 +293,21 @@ func (h *ClaudeCodeAPIHandler) handleStreamingResponse(c *gin.Context, rawJSON [
|
||||
}
|
||||
}
|
||||
|
||||
func pendingClaudeStreamError(errs <-chan *interfaces.ErrorMessage) (*interfaces.ErrorMessage, bool) {
|
||||
if errs == nil {
|
||||
return nil, false
|
||||
}
|
||||
select {
|
||||
case errMsg, ok := <-errs:
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
return errMsg, true
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
func (h *ClaudeCodeAPIHandler) forwardClaudeStream(c *gin.Context, flusher http.Flusher, cancel func(error), data <-chan []byte, errs <-chan *interfaces.ErrorMessage) {
|
||||
h.ForwardStream(c, flusher, cancel, data, errs, handlers.StreamForwardOptions{
|
||||
WriteChunk: func(chunk []byte) {
|
||||
@@ -317,11 +343,135 @@ type claudeErrorResponse struct {
|
||||
}
|
||||
|
||||
func (h *ClaudeCodeAPIHandler) toClaudeError(msg *interfaces.ErrorMessage) claudeErrorResponse {
|
||||
status := http.StatusInternalServerError
|
||||
errText := http.StatusText(status)
|
||||
if msg != nil {
|
||||
if msg.StatusCode > 0 {
|
||||
status = msg.StatusCode
|
||||
errText = http.StatusText(status)
|
||||
}
|
||||
if msg.Error != nil {
|
||||
if v := strings.TrimSpace(msg.Error.Error()); v != "" {
|
||||
errText = v
|
||||
}
|
||||
}
|
||||
}
|
||||
errType, message := claudeErrorDetailFromText(status, errText)
|
||||
return claudeErrorResponse{
|
||||
Type: "error",
|
||||
Error: claudeErrorDetail{
|
||||
Type: "api_error",
|
||||
Message: msg.Error.Error(),
|
||||
Type: errType,
|
||||
Message: message,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (h *ClaudeCodeAPIHandler) WriteErrorResponse(c *gin.Context, msg *interfaces.ErrorMessage) {
|
||||
status := http.StatusInternalServerError
|
||||
if msg != nil && msg.StatusCode > 0 {
|
||||
status = msg.StatusCode
|
||||
}
|
||||
if msg != nil && msg.Addon != nil && handlers.PassthroughHeadersEnabled(h.Cfg) {
|
||||
for key, values := range msg.Addon {
|
||||
if len(values) == 0 {
|
||||
continue
|
||||
}
|
||||
c.Writer.Header().Del(key)
|
||||
for _, value := range values {
|
||||
c.Writer.Header().Add(key, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
body, err := json.Marshal(h.toClaudeError(msg))
|
||||
if err != nil {
|
||||
body = []byte(`{"type":"error","error":{"type":"api_error","message":"Internal Server Error"}}`)
|
||||
}
|
||||
appendClaudeAPIResponse(c, body)
|
||||
if !c.Writer.Written() {
|
||||
c.Writer.Header().Set("Content-Type", "application/json")
|
||||
}
|
||||
c.Status(status)
|
||||
_, _ = c.Writer.Write(body)
|
||||
}
|
||||
|
||||
func claudeErrorDetailFromText(status int, errText string) (string, string) {
|
||||
message := strings.TrimSpace(errText)
|
||||
if message == "" {
|
||||
message = http.StatusText(status)
|
||||
}
|
||||
errType := claudeErrorTypeFromStatus(status)
|
||||
|
||||
var payload map[string]any
|
||||
if json.Valid([]byte(message)) {
|
||||
if err := json.Unmarshal([]byte(message), &payload); err == nil {
|
||||
if e, ok := payload["error"].(map[string]any); ok {
|
||||
if t, ok := e["type"].(string); ok && strings.TrimSpace(t) != "" {
|
||||
errType = strings.TrimSpace(t)
|
||||
}
|
||||
if m, ok := e["message"].(string); ok && strings.TrimSpace(m) != "" {
|
||||
message = strings.TrimSpace(m)
|
||||
} else if c, ok := e["code"].(string); ok && strings.TrimSpace(c) != "" {
|
||||
message = strings.TrimSpace(c)
|
||||
}
|
||||
} else {
|
||||
if t, ok := payload["type"].(string); ok && strings.TrimSpace(t) != "" && strings.TrimSpace(t) != "error" {
|
||||
errType = strings.TrimSpace(t)
|
||||
}
|
||||
if m, ok := payload["message"].(string); ok && strings.TrimSpace(m) != "" {
|
||||
message = strings.TrimSpace(m)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return errType, message
|
||||
}
|
||||
|
||||
func claudeErrorTypeFromStatus(status int) string {
|
||||
switch status {
|
||||
case http.StatusUnauthorized:
|
||||
return "authentication_error"
|
||||
case http.StatusPaymentRequired:
|
||||
return "billing_error"
|
||||
case http.StatusForbidden:
|
||||
return "permission_error"
|
||||
case http.StatusNotFound:
|
||||
return "not_found_error"
|
||||
case http.StatusRequestEntityTooLarge:
|
||||
return "request_too_large"
|
||||
case http.StatusTooManyRequests:
|
||||
return "rate_limit_error"
|
||||
case http.StatusGatewayTimeout:
|
||||
return "timeout_error"
|
||||
case 529:
|
||||
return "overloaded_error"
|
||||
default:
|
||||
if status >= http.StatusInternalServerError {
|
||||
return "api_error"
|
||||
}
|
||||
return "invalid_request_error"
|
||||
}
|
||||
}
|
||||
|
||||
func appendClaudeAPIResponse(c *gin.Context, data []byte) {
|
||||
if c == nil || len(data) == 0 {
|
||||
return
|
||||
}
|
||||
if _, exists := c.Get("API_RESPONSE_TIMESTAMP"); !exists {
|
||||
c.Set("API_RESPONSE_TIMESTAMP", time.Now())
|
||||
}
|
||||
if existing, exists := c.Get("API_RESPONSE"); exists {
|
||||
if existingBytes, ok := existing.([]byte); ok && len(existingBytes) > 0 {
|
||||
combined := make([]byte, 0, len(existingBytes)+len(data)+1)
|
||||
combined = append(combined, existingBytes...)
|
||||
if existingBytes[len(existingBytes)-1] != '\n' {
|
||||
combined = append(combined, '\n')
|
||||
}
|
||||
combined = append(combined, data...)
|
||||
c.Set("API_RESPONSE", combined)
|
||||
return
|
||||
}
|
||||
}
|
||||
c.Set("API_RESPONSE", bytes.Clone(data))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package claude
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/interfaces"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func TestClaudeErrorExtractsOpenAIStyleUpstreamJSON(t *testing.T) {
|
||||
handler := &ClaudeCodeAPIHandler{}
|
||||
msg := &interfaces.ErrorMessage{
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Error: errors.New(`{"error":{"message":"Your input exceeds the context window of this model. Please adjust your input and try again.","type":"invalid_request_error","code":"context_too_large"}}`),
|
||||
}
|
||||
|
||||
got := handler.toClaudeError(msg)
|
||||
|
||||
if got.Type != "error" {
|
||||
t.Fatalf("type = %q, want error", got.Type)
|
||||
}
|
||||
if got.Error.Type != "invalid_request_error" {
|
||||
t.Fatalf("error.type = %q, want invalid_request_error", got.Error.Type)
|
||||
}
|
||||
if got.Error.Message != "Your input exceeds the context window of this model. Please adjust your input and try again." {
|
||||
t.Fatalf("error.message = %q", got.Error.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeErrorExtractsClaudeStyleUpstreamJSON(t *testing.T) {
|
||||
handler := &ClaudeCodeAPIHandler{}
|
||||
msg := &interfaces.ErrorMessage{
|
||||
StatusCode: http.StatusTooManyRequests,
|
||||
Error: errors.New(`{"type":"error","error":{"type":"rate_limit_error","message":"This request would exceed your account's rate limit. Please try again later."},"request_id":"req_123"}`),
|
||||
}
|
||||
|
||||
got := handler.toClaudeError(msg)
|
||||
|
||||
if got.Error.Type != "rate_limit_error" {
|
||||
t.Fatalf("error.type = %q, want rate_limit_error", got.Error.Type)
|
||||
}
|
||||
if got.Error.Message != "This request would exceed your account's rate limit. Please try again later." {
|
||||
t.Fatalf("error.message = %q", got.Error.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteClaudeErrorResponseUsesClaudeEnvelope(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
recorder := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(recorder)
|
||||
handler := &ClaudeCodeAPIHandler{}
|
||||
msg := &interfaces.ErrorMessage{
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Error: errors.New(`{"error":{"message":"Your input exceeds the context window of this model. Please adjust your input and try again.","type":"invalid_request_error","code":"context_too_large"}}`),
|
||||
}
|
||||
|
||||
handler.WriteErrorResponse(c, msg)
|
||||
|
||||
if recorder.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusBadRequest)
|
||||
}
|
||||
body := recorder.Body.Bytes()
|
||||
if got := gjson.GetBytes(body, "type").String(); got != "error" {
|
||||
t.Fatalf("type = %q, want error; body=%s", got, body)
|
||||
}
|
||||
if got := gjson.GetBytes(body, "error.type").String(); got != "invalid_request_error" {
|
||||
t.Fatalf("error.type = %q, want invalid_request_error; body=%s", got, body)
|
||||
}
|
||||
if got := gjson.GetBytes(body, "error.message").String(); got != "Your input exceeds the context window of this model. Please adjust your input and try again." {
|
||||
t.Fatalf("error.message = %q; body=%s", got, body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPendingClaudeStreamErrorUsesBufferedError(t *testing.T) {
|
||||
wantErr := &interfaces.ErrorMessage{
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Error: errors.New(`{"error":{"message":"Your input exceeds the context window of this model. Please adjust your input and try again.","type":"invalid_request_error","code":"context_too_large"}}`),
|
||||
}
|
||||
errs := make(chan *interfaces.ErrorMessage, 1)
|
||||
errs <- wantErr
|
||||
close(errs)
|
||||
|
||||
gotErr, ok := pendingClaudeStreamError(errs)
|
||||
if !ok {
|
||||
t.Fatal("expected pending stream error")
|
||||
}
|
||||
if gotErr != wantErr {
|
||||
t.Fatalf("pending error = %p, want %p", gotErr, wantErr)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user