Like-api/common/gin.go
hjjjj a9a224e7dd
Some checks failed
CI / Unit tests (push) Has been cancelled
CI / commit_lint (push) Has been cancelled
feat: add model configuration management and logging enhancements
- Introduced a new ModelConfigEditor component for managing model configurations.
- Added API endpoints for model configuration management, including fetching, saving, and importing/exporting configurations.
- Enhanced logging for request bodies, particularly for multimodal requests containing image URLs.
- Updated the .gitignore to include new configuration files.
- Refactored model handling in the controller to support custom model configurations.
2026-04-08 16:33:27 +08:00

67 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package common
import (
"bytes"
"encoding/json"
"io"
"strings"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/common/logger"
)
func GetRequestBody(c *gin.Context) ([]byte, error) {
requestBody, _ := c.Get(ctxkey.KeyRequestBody)
if requestBody != nil {
return requestBody.([]byte), nil
}
requestBody, err := io.ReadAll(c.Request.Body)
if err != nil {
return nil, err
}
_ = c.Request.Body.Close()
c.Set(ctxkey.KeyRequestBody, requestBody)
return requestBody.([]byte), nil
}
func UnmarshalBodyReusable(c *gin.Context, v any) error {
requestBody, err := GetRequestBody(c)
if err != nil {
return err
}
// 添加诊断日志检查原始请求体是否包含image_url
bodyStr := string(requestBody)
if strings.Contains(bodyStr, "image_url") {
logger.Infof(c.Request.Context(), "=== RAW REQUEST BODY contains image_url ===")
if len(bodyStr) < 500 {
logger.Infof(c.Request.Context(), "=== RAW BODY: %s ===", bodyStr)
} else {
logger.Infof(c.Request.Context(), "=== RAW BODY (first 500 chars): %s ===", bodyStr[:500])
}
}
contentType := c.Request.Header.Get("Content-Type")
if strings.HasPrefix(contentType, "application/json") {
err = json.Unmarshal(requestBody, &v)
} else {
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
err = c.ShouldBind(&v)
}
if err != nil {
return err
}
// Reset request body
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
return nil
}
func SetEventStreamHeaders(c *gin.Context) {
c.Writer.Header().Set("Content-Type", "text/event-stream")
c.Writer.Header().Set("Cache-Control", "no-cache")
c.Writer.Header().Set("Connection", "keep-alive")
c.Writer.Header().Set("Transfer-Encoding", "chunked")
c.Writer.Header().Set("X-Accel-Buffering", "no")
}