mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-17 07:08:01 +08:00
- Replace tidwall/gjson and tidwall/sjson with github.com/MoeForge/moego/ojson and encoding/base64 with github.com/MoeForge/moego/base64 across the main module and examples - Introduce cross-method *ojson.Doc request rewriting pipelines across thinking, signature, client/codex, runtime/executor/helps, pluginhost, and all provider executors with FinalizePayloadDoc as the final semantic barrier - Convert all consecutive JSON mutations (>= 2 Set/SetRaw/Delete operations) to batch edits on ojson.Doc - Upgrade read-only byte paths to ojson.GetBytesNoCopy, ojson.GetManyBytesNoCopy, and util.ParseGJSONBytesNoCopy with explicit strings.Clone ownership protection across streaming and persistent state - Use base64.StdEncoding.ValidateString for zero-allocation signature checks and base64.EncodeDataURL for single-allocation data URL construction
62 lines
1.8 KiB
Go
62 lines
1.8 KiB
Go
package helps
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/MoeForge/moego/ojson"
|
|
)
|
|
|
|
// SanitizeMetaWebSearchTools strips unsupported fields from web_search tool definitions
|
|
// for Meta Muse upstreams. Specifically, Meta rejects search_content_types when tool type
|
|
// is "web_search" (it is only accepted on "web_search_preview" tools).
|
|
func SanitizeMetaWebSearchTools(body []byte) []byte {
|
|
if len(body) == 0 {
|
|
return body
|
|
}
|
|
doc := ojson.NewDoc(body)
|
|
if !SanitizeMetaWebSearchToolsDoc(doc) {
|
|
return body
|
|
}
|
|
return doc.Bytes()
|
|
}
|
|
|
|
// SanitizeMetaWebSearchToolsDoc strips unsupported fields from web_search tool definitions
|
|
// in place on doc for Meta Muse upstreams.
|
|
func SanitizeMetaWebSearchToolsDoc(doc *ojson.Doc) bool {
|
|
if doc == nil || doc.Len() == 0 {
|
|
return false
|
|
}
|
|
tools := doc.Get("tools")
|
|
if !tools.Exists() || !tools.IsArray() {
|
|
return false
|
|
}
|
|
var pathsToDelete []string
|
|
tools.ForEach(func(index, tool ojson.Result) bool {
|
|
if tool.Get("type").String() == "web_search" && tool.Get("search_content_types").Exists() {
|
|
pathsToDelete = append(pathsToDelete, fmt.Sprintf("tools.%d.search_content_types", index.Int()))
|
|
}
|
|
if tool.Get("type").String() == "namespace" {
|
|
subtools := tool.Get("tools")
|
|
if subtools.Exists() && subtools.IsArray() {
|
|
subtools.ForEach(func(subIndex, subtool ojson.Result) bool {
|
|
if subtool.Get("type").String() == "web_search" && subtool.Get("search_content_types").Exists() {
|
|
pathsToDelete = append(pathsToDelete, fmt.Sprintf("tools.%d.tools.%d.search_content_types", index.Int(), subIndex.Int()))
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
if len(pathsToDelete) == 0 {
|
|
return false
|
|
}
|
|
modified := false
|
|
for _, p := range pathsToDelete {
|
|
if errDel := doc.Delete(p); errDel == nil {
|
|
modified = true
|
|
}
|
|
}
|
|
return modified
|
|
}
|