fix codex context length stream errors

This commit is contained in:
sususu98
2026-05-19 11:56:28 +08:00
parent bbe30f53b5
commit ad868308c0
4 changed files with 480 additions and 2 deletions
+152 -2
View File
@@ -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))
}