Files
CLIProxyAPI/internal/thinking/provider/codex/apply.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

104 lines
2.7 KiB
Go

// Package codex implements thinking configuration for Codex (OpenAI Responses API) models.
package codex
import (
"github.com/MoeForge/moego/ojson"
"github.com/router-for-me/CLIProxyAPI/v8/internal/registry"
"github.com/router-for-me/CLIProxyAPI/v8/internal/thinking"
)
// Applier implements thinking.ProviderApplier for Codex models.
type Applier struct{}
var _ thinking.ProviderDocApplier = (*Applier)(nil)
// NewApplier creates a new Codex thinking applier.
func NewApplier() *Applier {
return &Applier{}
}
func init() {
thinking.RegisterProvider("codex", NewApplier())
}
// Apply applies thinking configuration to Codex request body.
func (a *Applier) Apply(body []byte, config thinking.ThinkingConfig, modelInfo *registry.ModelInfo) ([]byte, error) {
effort, ok := resolveCodexEffort(config, modelInfo)
if !ok {
return body, nil
}
if len(body) == 0 || !ojson.ValidBytes(body) {
body = []byte("{}")
}
result, _ := ojson.SetBytes(body, "reasoning.effort", effort)
return result, nil
}
// ApplyDoc applies thinking configuration onto doc in place.
func (a *Applier) ApplyDoc(doc *ojson.Doc, config thinking.ThinkingConfig, modelInfo *registry.ModelInfo) error {
if doc == nil {
return nil
}
effort, ok := resolveCodexEffort(config, modelInfo)
if !ok {
return nil
}
_ = doc.Set("reasoning.effort", effort)
return nil
}
func resolveCodexEffort(config thinking.ThinkingConfig, modelInfo *registry.ModelInfo) (string, bool) {
if thinking.IsUserDefinedModel(modelInfo) {
switch config.Mode {
case thinking.ModeLevel:
if config.Level == "" {
return "", false
}
return string(config.Level), true
case thinking.ModeNone:
effort := string(thinking.LevelNone)
if config.Level != "" {
effort = string(config.Level)
}
return effort, true
case thinking.ModeAuto:
return string(thinking.LevelAuto), true
case thinking.ModeBudget:
level, ok := thinking.ConvertBudgetToLevel(config.Budget)
if !ok {
return "", false
}
return level, true
default:
return "", false
}
}
if modelInfo.Thinking == nil {
return "", false
}
if config.Mode != thinking.ModeLevel && config.Mode != thinking.ModeNone {
return "", false
}
if config.Mode == thinking.ModeLevel {
return string(config.Level), true
}
effort := ""
support := modelInfo.Thinking
if config.Budget == 0 {
if support.ZeroAllowed || thinking.HasLevel(support.Levels, string(thinking.LevelNone)) {
effort = string(thinking.LevelNone)
}
}
if effort == "" && config.Level != "" {
effort = string(config.Level)
}
if effort == "" && len(support.Levels) > 0 {
effort = support.Levels[0]
}
if effort == "" {
return "", false
}
return effort, true
}