mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-16 22:57:53 +08:00
- Build un-defaulted original request payloads for xAI image generation and edit handlers and propagate them via execution context so payload default rules can distinguish omitted client fields from built-in defaults. - Pass normalized original requests into the xAI image executor payload finalizer and report finalized response_format values back to handlers via metadata callbacks across streaming and non-streaming responses. - Add unit and handler regression tests covering payload default, default-raw, and override rules for xAI image endpoints. Closes: #7048
49 lines
1.9 KiB
Go
49 lines
1.9 KiB
Go
package helps
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
|
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// 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
|
|
|
|
// 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))
|
|
}
|
|
return func(body []byte) []byte {
|
|
out := ApplyPayloadConfigWithRequestForExecutor(cfg, executor, model, protocol, opts.SourceFormat.String(), root, body, source, requestedModel, requestPath, headers)
|
|
if formatCallback != nil {
|
|
formatCallback(strings.TrimSpace(gjson.GetBytes(out, buildPayloadPath(root, "response_format")).String()))
|
|
}
|
|
return out
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|