mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-11-30 19:21:36 +08:00
* perf: read and edit request JSON and base64 with moego Switch gjson/sjson to moego/ojson and base64 on media, file and plugin paths to moego/base64. Both keep the same results on valid input. - Param overrides apply every edit to one ojson.Doc and write the body once, instead of copying the whole body for each edit. - The video proxy and the audio check validate base64 without decoding it; plugin icons and inlined plugin files build data URLs directly. - Login, password, OAuth and signature code keeps gjson and encoding/base64. * perf: rewrite request fields without re-encoding the whole body Removing disabled fields, the Responses WebSocket create event, AWS Mantle and pass-through bodies, the OpenAI usage patch, alpha search, video result redaction and image SSE events now edit the body with ojson.Doc instead of decoding it into a map and encoding it again. Everything the gateway does not touch keeps the client's member order, number spelling and characters. - A key the client repeats is removed or rewritten in every copy, so an upstream that reads the last copy sees the same value the gateway routed and billed. A key that appears once is rewritten in place. - Image responses and the video proxy read large values without copying them. * perf: keep member order in param override merges and prune_objects Merging an object (append/prepend) now replaces existing members where they stand and appends new members, sorted by key, at the end. prune_objects deletes the matching objects from the body in place instead of encoding the pruned part, or the whole body, again. Everything else keeps the client's member order, number spelling and characters.
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package middleware
|
|
|
|
import (
|
|
"io"
|
|
"mime"
|
|
"strings"
|
|
|
|
"github.com/MoeForge/moego/ojson"
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// rewriteTaskPluginJSONModel patches the top-level JSON "model" field and
|
|
// replaces BodyStorage. Non-JSON bodies are left untouched.
|
|
func rewriteTaskPluginJSONModel(c *gin.Context, spelling string) error {
|
|
mediaType, _, err := mime.ParseMediaType(c.GetHeader("Content-Type"))
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
if mediaType != "application/json" && !strings.HasSuffix(mediaType, "+json") {
|
|
return nil
|
|
}
|
|
storage, err := common.GetBodyStorage(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
raw, err := storage.Bytes()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
patched, err := ojson.SetBytes(raw, "model", spelling)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
newStorage, err := common.CreateBodyStorage(patched)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_ = storage.Close()
|
|
c.Set(common.KeyBodyStorage, newStorage)
|
|
c.Set(common.KeyRequestBody, nil)
|
|
c.Request.Body = io.NopCloser(newStorage)
|
|
c.Request.ContentLength = int64(len(patched))
|
|
return nil
|
|
}
|