mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-17 07:08:01 +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
107 lines
3.6 KiB
Go
107 lines
3.6 KiB
Go
package helps
|
|
|
|
import (
|
|
"context"
|
|
"reflect"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"github.com/MoeForge/moego/ojson"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
|
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
|
)
|
|
|
|
// PayloadFinalizer applies user rules to a fully prepared business payload.
|
|
// Each rebuilt attempt must start from the unconfigured body, not a previous result.
|
|
type PayloadFinalizer func([]byte) []byte
|
|
|
|
var (
|
|
payloadDocProbes sync.Map
|
|
builtinFinalizerCodePtr atomic.Uintptr
|
|
)
|
|
|
|
func payloadFinalizerKeyPtr(f PayloadFinalizer) uintptr {
|
|
if f == nil {
|
|
return 0
|
|
}
|
|
return reflect.ValueOf(f).Pointer()
|
|
}
|
|
|
|
// FinalizeDoc applies the finalizer to doc in place without probing custom wrappers.
|
|
func (f PayloadFinalizer) FinalizeDoc(doc *ojson.Doc) {
|
|
if f == nil || doc == nil {
|
|
return
|
|
}
|
|
if ptr := payloadFinalizerKeyPtr(f); ptr != 0 && ptr == builtinFinalizerCodePtr.Load() {
|
|
var probe [1]byte
|
|
key := &probe[0]
|
|
payloadDocProbes.Store(key, doc)
|
|
_ = f(probe[:0])
|
|
if _, remaining := payloadDocProbes.LoadAndDelete(key); !remaining {
|
|
return
|
|
}
|
|
}
|
|
updated := f(doc.Bytes())
|
|
*doc = *ojson.NewDoc(updated)
|
|
}
|
|
|
|
// NewPayloadFinalizer snapshots matching context before built-in request mutations.
|
|
func NewPayloadFinalizer(cfg *config.Config, executor, model, protocol, root string, original []byte, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) PayloadFinalizer {
|
|
requestedModel := PayloadRequestedModel(opts, req.Model)
|
|
requestPath := PayloadRequestPath(opts)
|
|
headers := opts.Headers.Clone()
|
|
source := append([]byte(nil), original...)
|
|
var formatCallback func(string)
|
|
if opts.Metadata != nil {
|
|
formatCallback, _ = opts.Metadata[cliproxyexecutor.FinalizedResponseFormatCallbackMetadataKey].(func(string))
|
|
}
|
|
fn := PayloadFinalizer(func(body []byte) []byte {
|
|
if len(body) == 0 && cap(body) == 1 {
|
|
key := &body[:1][0]
|
|
if v, ok := payloadDocProbes.LoadAndDelete(key); ok {
|
|
if doc, okDoc := v.(*ojson.Doc); okDoc && doc != nil {
|
|
ApplyPayloadConfigWithRequestForExecutorDoc(cfg, executor, model, protocol, opts.SourceFormat.String(), root, doc, source, requestedModel, requestPath, headers)
|
|
if formatCallback != nil {
|
|
formatCallback(strings.Clone(strings.TrimSpace(doc.Get(buildPayloadPath(root, "response_format")).String())))
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
out := ApplyPayloadConfigWithRequestForExecutor(cfg, executor, model, protocol, opts.SourceFormat.String(), root, body, source, requestedModel, requestPath, headers)
|
|
if formatCallback != nil {
|
|
formatCallback(strings.Clone(strings.TrimSpace(ojson.GetBytesNoCopy(out, buildPayloadPath(root, "response_format")).String())))
|
|
}
|
|
return out
|
|
})
|
|
builtinFinalizerCodePtr.Store(payloadFinalizerKeyPtr(fn))
|
|
return fn
|
|
}
|
|
|
|
type payloadFinalizerKey struct{}
|
|
|
|
// WithPayloadFinalizer carries the final barrier through shared request builders.
|
|
func WithPayloadFinalizer(ctx context.Context, finalize PayloadFinalizer) context.Context {
|
|
return context.WithValue(ctx, payloadFinalizerKey{}, finalize)
|
|
}
|
|
|
|
// FinalizePayload runs immediately before serialization or transport framing.
|
|
func FinalizePayload(ctx context.Context, body []byte) []byte {
|
|
if finalize, ok := ctx.Value(payloadFinalizerKey{}).(PayloadFinalizer); ok && finalize != nil {
|
|
return finalize(body)
|
|
}
|
|
return body
|
|
}
|
|
|
|
// FinalizePayloadDoc runs immediately before serialization or transport framing,
|
|
// mutating doc in place.
|
|
func FinalizePayloadDoc(ctx context.Context, doc *ojson.Doc) {
|
|
if doc == nil {
|
|
return
|
|
}
|
|
if finalize, ok := ctx.Value(payloadFinalizerKey{}).(PayloadFinalizer); ok && finalize != nil {
|
|
finalize.FinalizeDoc(doc)
|
|
}
|
|
}
|