Files
CLIProxyAPI/internal/runtime/executor/helps/meta_tools.go
Luis Pater 2c27e61fe7 fix(meta): strip unsupported search_content_types from web search tools
- Add `SanitizeMetaWebSearchTools` to remove `search_content_types` from `web_search` tool definitions
- Support sanitizing nested tools within `namespace` tool definitions
- Apply web search tool sanitization during Meta request preparation

Closes: #6156
2026-09-27 22:54:19 +08:00

47 lines
1.4 KiB
Go

package helps
import (
"fmt"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
// 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 {
tools := gjson.GetBytes(body, "tools")
if !tools.Exists() || !tools.IsArray() {
return body
}
var pathsToDelete []string
tools.ForEach(func(index, tool gjson.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 gjson.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 body
}
out := body
for _, p := range pathsToDelete {
if updated, errDel := sjson.DeleteBytes(out, p); errDel == nil {
out = updated
}
}
return out
}