Spaces:
Paused
Paused
Update handlers.go
Browse files- handlers.go +10 -11
handlers.go
CHANGED
|
@@ -153,8 +153,11 @@ func MessagesHandler(c *gin.Context) {
|
|
| 153 |
if cfg.OpenAIAPIKey != "" {
|
| 154 |
req.Header.Set("Authorization", "Bearer "+cfg.OpenAIAPIKey)
|
| 155 |
}
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
// Giữ lại: Log INFO quan trọng về việc gửi yêu cầu upstream
|
| 160 |
log.Printf("INFO: [%s] Gửi yêu cầu upstream (Stream=%t) đến %s...", requestID, isStreaming, maskURL(upstreamURL))
|
|
@@ -210,15 +213,13 @@ func MessagesHandler(c *gin.Context) {
|
|
| 210 |
if isStreaming {
|
| 211 |
if cfg.UpstreamNativeClaudeFormat {
|
| 212 |
// Giữ lại: Log INFO quan trọng
|
| 213 |
-
log.Printf("INFO: [%s] Upstream Claude native stream nhận được. Bắt đầu proxy SSE
|
| 214 |
-
proxyClaudeNativeSSE(c, upstreamResp, requestID)
|
| 215 |
} else {
|
| 216 |
// Giữ lại: Log INFO quan trọng
|
| 217 |
log.Printf("INFO: [%s] Upstream OpenAI stream nhận được. Bắt đầu chuyển đổi SSE.", requestID)
|
| 218 |
-
// Use claudeReq.Model as the source of truth for the model name in conversion,
|
| 219 |
-
// openAIReqForLogging might be nil if native format.
|
| 220 |
modelForConversion := claudeReq.Model
|
| 221 |
-
if openAIReqForLogging != nil && openAIReqForLogging.Model != "" {
|
| 222 |
modelForConversion = openAIReqForLogging.Model
|
| 223 |
}
|
| 224 |
streamOpenAIResponseToClaudeSSE(c, upstreamResp, requestID, modelForConversion, &claudeReq)
|
|
@@ -239,18 +240,16 @@ func MessagesHandler(c *gin.Context) {
|
|
| 239 |
if cfg.UpstreamNativeClaudeFormat {
|
| 240 |
// Giữ lại: Log INFO
|
| 241 |
log.Printf("INFO: [%s] Upstream Claude native non-stream response nhận được. Chuyển tiếp trực tiếp.", requestID)
|
| 242 |
-
|
| 243 |
-
var tempClaudeResp map[string]interface{} // Use map for validation, not full struct
|
| 244 |
if errJson := json.Unmarshal(respBodyBytes, &tempClaudeResp); errJson != nil {
|
| 245 |
// Giữ lại: Log lỗi quan trọng
|
| 246 |
log.Printf("ERROR: [%s] Upstream Claude native response không phải JSON hợp lệ: %v. Body: %s", requestID, errJson, string(respBodyBytes))
|
| 247 |
sendClaudeError(c, http.StatusBadGateway, "api_error", "API upstream (Claude native) trả về JSON không hợp lệ.")
|
| 248 |
return
|
| 249 |
}
|
| 250 |
-
// Send the original Claude response bytes
|
| 251 |
clientContentType := upstreamResp.Header.Get("Content-Type")
|
| 252 |
if clientContentType == "" {
|
| 253 |
-
clientContentType = "application/json; charset=utf-8"
|
| 254 |
}
|
| 255 |
c.Data(http.StatusOK, clientContentType, respBodyBytes)
|
| 256 |
} else {
|
|
|
|
| 153 |
if cfg.OpenAIAPIKey != "" {
|
| 154 |
req.Header.Set("Authorization", "Bearer "+cfg.OpenAIAPIKey)
|
| 155 |
}
|
| 156 |
+
if cfg.UpstreamNativeClaudeFormat {
|
| 157 |
+
// Add Anthropic-Version header if upstream is native Claude and it's required
|
| 158 |
+
// Example: req.Header.Set("anthropic-version", "2023-06-01")
|
| 159 |
+
// For now, this is commented out as it depends on specific upstream requirements.
|
| 160 |
+
}
|
| 161 |
|
| 162 |
// Giữ lại: Log INFO quan trọng về việc gửi yêu cầu upstream
|
| 163 |
log.Printf("INFO: [%s] Gửi yêu cầu upstream (Stream=%t) đến %s...", requestID, isStreaming, maskURL(upstreamURL))
|
|
|
|
| 213 |
if isStreaming {
|
| 214 |
if cfg.UpstreamNativeClaudeFormat {
|
| 215 |
// Giữ lại: Log INFO quan trọng
|
| 216 |
+
log.Printf("INFO: [%s] Upstream Claude native stream nhận được. Bắt đầu proxy SSE (với hack usage).", requestID)
|
| 217 |
+
proxyClaudeNativeSSE(c, upstreamResp, requestID, &claudeReq) // SỬA Ở ĐÂY: Thêm &claudeReq
|
| 218 |
} else {
|
| 219 |
// Giữ lại: Log INFO quan trọng
|
| 220 |
log.Printf("INFO: [%s] Upstream OpenAI stream nhận được. Bắt đầu chuyển đổi SSE.", requestID)
|
|
|
|
|
|
|
| 221 |
modelForConversion := claudeReq.Model
|
| 222 |
+
if openAIReqForLogging != nil && openAIReqForLogging.Model != "" {
|
| 223 |
modelForConversion = openAIReqForLogging.Model
|
| 224 |
}
|
| 225 |
streamOpenAIResponseToClaudeSSE(c, upstreamResp, requestID, modelForConversion, &claudeReq)
|
|
|
|
| 240 |
if cfg.UpstreamNativeClaudeFormat {
|
| 241 |
// Giữ lại: Log INFO
|
| 242 |
log.Printf("INFO: [%s] Upstream Claude native non-stream response nhận được. Chuyển tiếp trực tiếp.", requestID)
|
| 243 |
+
var tempClaudeResp map[string]interface{}
|
|
|
|
| 244 |
if errJson := json.Unmarshal(respBodyBytes, &tempClaudeResp); errJson != nil {
|
| 245 |
// Giữ lại: Log lỗi quan trọng
|
| 246 |
log.Printf("ERROR: [%s] Upstream Claude native response không phải JSON hợp lệ: %v. Body: %s", requestID, errJson, string(respBodyBytes))
|
| 247 |
sendClaudeError(c, http.StatusBadGateway, "api_error", "API upstream (Claude native) trả về JSON không hợp lệ.")
|
| 248 |
return
|
| 249 |
}
|
|
|
|
| 250 |
clientContentType := upstreamResp.Header.Get("Content-Type")
|
| 251 |
if clientContentType == "" {
|
| 252 |
+
clientContentType = "application/json; charset=utf-8"
|
| 253 |
}
|
| 254 |
c.Data(http.StatusOK, clientContentType, respBodyBytes)
|
| 255 |
} else {
|