refactor: simplify context_management compatibility handling

This commit is contained in:
fan
2026-02-21 22:20:48 +08:00
parent d693d7993b
commit afc8a0f9be

View File

@@ -2,7 +2,6 @@ package responses
import (
"fmt"
"strings"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
@@ -47,32 +46,12 @@ func ConvertOpenAIResponsesRequestToCodex(modelName string, inputRawJSON []byte,
//
// Compatibility strategy:
// 1) Remove context_management before forwarding to Codex upstream.
// 2) Remove truncation as Codex upstream currently rejects it as unsupported.
func applyResponsesCompactionCompatibility(rawJSON []byte) []byte {
contextManagement := gjson.GetBytes(rawJSON, "context_management")
if !contextManagement.Exists() {
if !gjson.GetBytes(rawJSON, "context_management").Exists() {
return rawJSON
}
hasCompactionRule := false
switch {
case contextManagement.IsArray():
for _, item := range contextManagement.Array() {
if strings.EqualFold(item.Get("type").String(), "compaction") {
hasCompactionRule = true
break
}
}
case contextManagement.IsObject():
hasCompactionRule = strings.EqualFold(contextManagement.Get("type").String(), "compaction")
}
if hasCompactionRule {
// no-op marker: compaction hint detected and consumed for compatibility.
}
rawJSON, _ = sjson.DeleteBytes(rawJSON, "context_management")
rawJSON, _ = sjson.DeleteBytes(rawJSON, "truncation")
return rawJSON
}