mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-16 22:57:53 +08:00
- 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
71 lines
3.9 KiB
Go
71 lines
3.9 KiB
Go
package helps
|
|
|
|
import (
|
|
"github.com/MoeForge/moego/ojson"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/registry"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/thinking"
|
|
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
|
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
|
)
|
|
|
|
// APIKeyModelIsCompat reports whether the selected API-key model enables
|
|
// compatibility handling for Claude thinking blocks.
|
|
func APIKeyModelIsCompat(req cliproxyexecutor.Request) bool {
|
|
modelInfo, ok := cliproxyauth.ResolvedModelInfo(req)
|
|
return ok && modelInfo != nil && modelInfo.IsCompat
|
|
}
|
|
|
|
// ApplyRequestThinking preserves the registry lookup path unless the auth
|
|
// manager bound authoritative model capabilities to this execution attempt.
|
|
func ApplyRequestThinking(body []byte, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, fromFormat, toFormat, provider string, normalizedUpdatesChanged ...bool) ([]byte, error) {
|
|
return ApplyRequestThinkingWithModelInfo(body, req, opts, fromFormat, toFormat, provider, nil, normalizedUpdatesChanged...)
|
|
}
|
|
|
|
// ApplyRequestThinkingDoc applies request thinking onto doc in place, preserving
|
|
// the registry lookup path unless the auth manager bound authoritative model
|
|
// capabilities to this execution attempt.
|
|
func ApplyRequestThinkingDoc(doc *ojson.Doc, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, fromFormat, toFormat, providerKey string, PreserveConfigurationUpdates ...bool) error {
|
|
return ApplyRequestThinkingWithModelInfoDoc(doc, req, opts, fromFormat, toFormat, providerKey, nil, PreserveConfigurationUpdates...)
|
|
}
|
|
|
|
// ApplyRequestThinkingWithModelInfo applies request thinking and falls back to
|
|
// fallbackModelInfo when neither request metadata nor the global registry
|
|
// resolves a model definition.
|
|
func ApplyRequestThinkingWithModelInfo(body []byte, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, fromFormat, toFormat, provider string, fallbackModelInfo *registry.ModelInfo, normalizedUpdatesChanged ...bool) ([]byte, error) {
|
|
originalSource := opts.OriginalRequest
|
|
if len(originalSource) == 0 {
|
|
originalSource = req.Payload
|
|
}
|
|
source := req.Payload
|
|
if len(source) == 0 {
|
|
source = opts.OriginalRequest
|
|
}
|
|
summaryConfig := translatedRequestSummaryConfig(body, req.Payload, originalSource, req.Model, fromFormat, toFormat)
|
|
if modelInfo, ok := cliproxyauth.ResolvedModelInfo(req); ok {
|
|
return thinking.ApplyThinkingWithModelInfoAndSummary(body, source, req.Model, fromFormat, toFormat, provider, modelInfo, summaryConfig, normalizedUpdatesChanged...)
|
|
}
|
|
return thinking.ApplyThinkingWithFallbackModelInfoAndSummary(body, source, req.Model, fromFormat, toFormat, provider, fallbackModelInfo, summaryConfig, normalizedUpdatesChanged...)
|
|
}
|
|
|
|
// ApplyRequestThinkingWithModelInfoDoc applies request thinking onto doc in place
|
|
// and falls back to fallbackModelInfo when neither request metadata nor the global
|
|
// registry resolves a model definition.
|
|
func ApplyRequestThinkingWithModelInfoDoc(doc *ojson.Doc, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, fromFormat, toFormat, provider string, fallbackModelInfo *registry.ModelInfo, normalizedUpdatesChanged ...bool) error {
|
|
if doc == nil {
|
|
return nil
|
|
}
|
|
originalSource := opts.OriginalRequest
|
|
if len(originalSource) == 0 {
|
|
originalSource = req.Payload
|
|
}
|
|
source := req.Payload
|
|
if len(source) == 0 {
|
|
source = opts.OriginalRequest
|
|
}
|
|
summaryConfig := translatedRequestSummaryConfigDoc(doc, req.Payload, originalSource, req.Model, fromFormat, toFormat)
|
|
if modelInfo, ok := cliproxyauth.ResolvedModelInfo(req); ok {
|
|
return thinking.ApplyThinkingWithModelInfoAndSummaryDoc(doc, source, req.Model, fromFormat, toFormat, provider, modelInfo, summaryConfig, normalizedUpdatesChanged...)
|
|
}
|
|
return thinking.ApplyThinkingWithFallbackModelInfoAndSummaryDoc(doc, source, req.Model, fromFormat, toFormat, provider, fallbackModelInfo, summaryConfig, normalizedUpdatesChanged...)
|
|
}
|