Files
CLIProxyAPI/internal/runtime/executor/helps/thinking.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

112 lines
5.0 KiB
Go

package helps
import (
"strings"
"github.com/MoeForge/moego/ojson"
"github.com/router-for-me/CLIProxyAPI/v8/internal/thinking"
sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
)
// ApplyThinkingWithSourcePayload preserves summary visibility from the original
// client payload while applying thinking configuration to its translated target
// payload. currentSourcePayload is the payload that was translated, while
// originalSourcePayload retains intent removed by an earlier interceptor.
func ApplyThinkingWithSourcePayload(body, currentSourcePayload, originalSourcePayload []byte, model, fromFormat, toFormat, providerKey string) ([]byte, error) {
summary := translatedRequestSummaryConfig(body, currentSourcePayload, originalSourcePayload, model, fromFormat, toFormat)
return thinking.ApplyThinkingWithSummary(body, model, fromFormat, toFormat, providerKey, summary)
}
// ApplyThinkingWithSourcePayloadDoc preserves summary visibility from the original
// client payload while applying thinking configuration onto doc in place.
func ApplyThinkingWithSourcePayloadDoc(doc *ojson.Doc, currentSourcePayload, originalSourcePayload []byte, model, fromFormat, toFormat, providerKey string) error {
if doc == nil {
return nil
}
summary := translatedRequestSummaryConfigDoc(doc, currentSourcePayload, originalSourcePayload, model, fromFormat, toFormat)
return thinking.ApplyThinkingWithSummaryDoc(doc, model, fromFormat, toFormat, providerKey, summary)
}
// translatedRequestSummaryConfig gives the translated target payload precedence
// so a plugin request normalizer can remove or rewrite a canonical summary field.
// The original source is consulted only when the payload that was translated no
// longer carries the inbound intent, or when the target could not represent that
// intent until model-aware thinking is applied later (notably Claude).
func translatedRequestSummaryConfig(body, currentSourcePayload, originalSourcePayload []byte, model, fromFormat, toFormat string) thinking.SummaryConfig {
fromFormat = strings.ToLower(strings.TrimSpace(fromFormat))
toFormat = strings.ToLower(strings.TrimSpace(toFormat))
var targetSummary thinking.SummaryConfig
if fromFormat == toFormat {
targetSummary = thinking.ExtractSummaryConfig(body, toFormat)
} else {
targetSummary = thinking.ExtractExplicitSummaryConfig(body, toFormat)
}
if targetSummary.Mode != thinking.SummaryUnspecified {
return targetSummary
}
currentSummary := thinking.ExtractTranslatedSummaryConfig(currentSourcePayload, fromFormat, toFormat)
originalSummary := thinking.ExtractTranslatedSummaryConfig(originalSourcePayload, fromFormat, toFormat)
if currentSummary.Mode == thinking.SummaryUnspecified {
return originalSummary
}
from := sdktranslator.FromString(fromFormat)
to := sdktranslator.FromString(toFormat)
if !sdktranslator.HasRequestTransformer(from, to) {
// A missing translation must remain source-shaped. Same-format requests
// were handled by targetSummary above, including explicit native aliases.
return thinking.SummaryConfig{}
}
candidate := thinking.ApplySummaryConfigForModel(body, toFormat, model, currentSummary)
if thinking.ExtractExplicitSummaryConfig(candidate, toFormat).Mode != thinking.SummaryUnspecified {
// Registry translation applied this field before plugin normalization. If
// it is absent now but can be represented on the normalized body, the
// normalizer deliberately removed it and must remain authoritative.
return thinking.SummaryConfig{}
}
// Some intents cannot be represented until the final model-aware pass. For
// example, Claude display is invalid on disabled thinking, but a suffix can
// subsequently activate adaptive thinking. Preserve the source in that case.
return currentSummary
}
func translatedRequestSummaryConfigDoc(doc *ojson.Doc, currentSourcePayload, originalSourcePayload []byte, model, fromFormat, toFormat string) thinking.SummaryConfig {
if doc == nil || doc.Len() == 0 {
return thinking.SummaryConfig{}
}
fromFormat = strings.ToLower(strings.TrimSpace(fromFormat))
toFormat = strings.ToLower(strings.TrimSpace(toFormat))
var targetSummary thinking.SummaryConfig
if fromFormat == toFormat {
targetSummary = thinking.ExtractSummaryConfigDoc(doc, toFormat)
} else {
targetSummary = thinking.ExtractExplicitSummaryConfigDoc(doc, toFormat)
}
if targetSummary.Mode != thinking.SummaryUnspecified {
return targetSummary
}
currentSummary := thinking.ExtractTranslatedSummaryConfig(currentSourcePayload, fromFormat, toFormat)
originalSummary := thinking.ExtractTranslatedSummaryConfig(originalSourcePayload, fromFormat, toFormat)
if currentSummary.Mode == thinking.SummaryUnspecified {
return originalSummary
}
from := sdktranslator.FromString(fromFormat)
to := sdktranslator.FromString(toFormat)
if !sdktranslator.HasRequestTransformer(from, to) {
return thinking.SummaryConfig{}
}
if thinking.CanRepresentSummaryConfigForModelDoc(doc, toFormat, model, currentSummary) {
return thinking.SummaryConfig{}
}
return currentSummary
}