Files
CLIProxyAPI/internal/runtime/executor/helps/responses_tool_search.go
Luis Pater 6e6afab99a refactor: migrate gjson, sjson, and base64 to MoeForge/moego
- 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
2026-11-03 03:05:04 +08:00

128 lines
3.7 KiB
Go

package helps
import (
"fmt"
"strings"
"github.com/MoeForge/moego/ojson"
)
const (
responsesToolSearchType = "tool_search"
responsesToolSearchCallType = "tool_search_call"
responsesToolSearchOutputType = "tool_search_output"
)
// ResponsesHasToolSearch reports whether a Responses payload carries a client
// tool_search declaration or tool_search history items.
func ResponsesHasToolSearch(payload []byte) bool {
if hasClientToolSearchDeclaration(ojson.GetBytesNoCopy(payload, "tools")) {
return true
}
found := false
ojson.GetBytesNoCopy(payload, "input").ForEach(func(_, item ojson.Result) bool {
switch strings.TrimSpace(item.Get("type").String()) {
case responsesToolSearchCallType, responsesToolSearchOutputType:
found = true
case "additional_tools":
found = hasClientToolSearchDeclaration(item.Get("tools"))
}
return !found
})
return found
}
// StripResponsesToolSearch removes client tool_search declarations and every
// tool_search_call/tool_search_output input item, so the Claude translator
// produces the same output it did before tool_search support. Hosted
// declarations are left for the translator. An unchanged payload is returned
// as is.
func StripResponsesToolSearch(payload []byte) []byte {
if len(payload) == 0 {
return payload
}
doc := ojson.NewDoc(payload)
if !StripResponsesToolSearchDoc(doc) {
return payload
}
return doc.Bytes()
}
// StripResponsesToolSearchDoc removes client tool_search declarations and every
// tool_search_call/tool_search_output input item in place on doc.
func StripResponsesToolSearchDoc(doc *ojson.Doc) bool {
if doc == nil || doc.Len() == 0 {
return false
}
modified := false
if tools := doc.Get("tools"); tools.IsArray() {
if kept, changed := withoutClientToolSearch(tools); changed {
if errSet := doc.SetRawBytes("tools", kept); errSet == nil {
modified = true
}
}
}
input := doc.Get("input")
if !input.IsArray() {
return modified
}
dropped := make(map[int]bool)
for idx, item := range input.Array() {
switch strings.TrimSpace(item.Get("type").String()) {
case responsesToolSearchCallType, responsesToolSearchOutputType:
dropped[idx] = true
modified = true
case "additional_tools":
if kept, keptChanged := withoutClientToolSearch(item.Get("tools")); keptChanged {
if errSet := doc.SetRawBytes(fmt.Sprintf("input.%d.tools", idx), kept); errSet == nil {
modified = true
}
}
}
}
if len(dropped) > 0 {
updatedItems := doc.Get("input").Array()
items := make([]string, 0, len(updatedItems)-len(dropped))
for idx, item := range updatedItems {
if !dropped[idx] {
items = append(items, item.Raw)
}
}
_ = doc.SetRaw("input", "["+strings.Join(items, ",")+"]")
}
return modified
}
func isClientToolSearchDeclaration(tool ojson.Result) bool {
return strings.TrimSpace(tool.Get("type").String()) == responsesToolSearchType &&
strings.TrimSpace(tool.Get("execution").String()) == "client"
}
func hasClientToolSearchDeclaration(tools ojson.Result) bool {
found := false
tools.ForEach(func(_, tool ojson.Result) bool {
found = isClientToolSearchDeclaration(tool)
return !found
})
return found
}
// withoutClientToolSearch rebuilds a tools array without client tool_search
// declarations and reports whether anything was removed.
func withoutClientToolSearch(tools ojson.Result) ([]byte, bool) {
if !tools.IsArray() {
return nil, false
}
changed := false
kept := make([]string, 0, len(tools.Array()))
tools.ForEach(func(_, tool ojson.Result) bool {
if isClientToolSearchDeclaration(tool) {
changed = true
return true
}
kept = append(kept, tool.Raw)
return true
})
return []byte("[" + strings.Join(kept, ",") + "]"), changed
}