mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-18 07:37:50 +08:00
feat(payload): add user text replacement payload rules
Add ordered literal replace rules that rewrite existing user text at selected one-based positions, or all users, in the final upstream payload after overrides and before filters. Support OpenAI Chat, Claude, Responses/Codex, Gemini, and Antigravity layouts while leaving system prompts, tools, images, and thinking untouched. Validate and sanitize rule config, preserve it through YAML/JSON and runtime cloning, surface it in config diffs and SDK types, and document it in the example config.
This commit is contained in:
@@ -356,6 +356,20 @@ requests:
|
||||
# protocol: "codex" # restricts the rule to a specific protocol, options: openai, gemini, claude, codex, antigravity, meta
|
||||
# params: # JSON path (gjson/sjson syntax) -> raw JSON value (strings are used as-is, must be valid JSON)
|
||||
# "response_format": "{\"type\":\"json_schema\",\"json_schema\":{\"name\":\"answer\",\"schema\":{\"type\":\"object\"}}}"
|
||||
# replace: # Ordered literal replacements in existing user text; runs after overrides, before filter.
|
||||
# - models: # Same model, protocol, header, and payload conditions as other rules.
|
||||
# - name: "*"
|
||||
# protocol: "claude"
|
||||
# from-protocol: "openai"
|
||||
# target:
|
||||
# role: user
|
||||
# positions: [1, 2] # One-based user positions in the final upstream body; missing positions are skipped.
|
||||
# # all: true # Select every user instead of positions (mutually exclusive).
|
||||
# replacements: # Each entry has exactly one non-empty string key and a string value.
|
||||
# - "you are hero": "you are fool"
|
||||
# - "remove this phrase": ""
|
||||
# # Text blocks are processed separately; images, tools, and thinking are not modified.
|
||||
# # Supported layouts: OpenAI Chat, Claude, Responses/Codex, Gemini, and Antigravity.
|
||||
# filter: # Filter rules remove specified parameters from the payload.
|
||||
# - models:
|
||||
# - name: "gemini-2.5-pro" # Supports wildcards (e.g., "gemini-*")
|
||||
|
||||
@@ -671,7 +671,7 @@ func ResolveOAuthModelSetting(settings []OAuthModelSetting, modelID, metadataMod
|
||||
return nameMatch
|
||||
}
|
||||
|
||||
// PayloadConfig defines default and override parameter rules applied to provider payloads.
|
||||
// PayloadConfig defines parameter and text mutation rules applied to provider payloads.
|
||||
type PayloadConfig struct {
|
||||
// Default defines rules that only set parameters when they are missing in the payload.
|
||||
Default []PayloadRule `yaml:"default" json:"default"`
|
||||
@@ -681,6 +681,8 @@ type PayloadConfig struct {
|
||||
Override []PayloadRule `yaml:"override" json:"override"`
|
||||
// OverrideRaw defines rules that always set raw JSON values, overwriting any existing values.
|
||||
OverrideRaw []PayloadRule `yaml:"override-raw" json:"override-raw"`
|
||||
// Replace defines ordered literal replacements in selected user messages.
|
||||
Replace []PayloadReplaceRule `yaml:"replace" json:"replace"`
|
||||
// Filter defines rules that remove parameters from the payload by JSON path.
|
||||
Filter []PayloadFilterRule `yaml:"filter" json:"filter"`
|
||||
}
|
||||
|
||||
@@ -8,13 +8,24 @@ import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// SanitizePayloadRules validates raw JSON payload rule params and drops invalid rules.
|
||||
// SanitizePayloadRules validates raw JSON and text replacement rules, dropping invalid rules.
|
||||
func (cfg *Config) SanitizePayloadRules() {
|
||||
if cfg == nil {
|
||||
return
|
||||
}
|
||||
cfg.Payload.DefaultRaw = sanitizePayloadRawRules(cfg.Payload.DefaultRaw, "default-raw")
|
||||
cfg.Payload.OverrideRaw = sanitizePayloadRawRules(cfg.Payload.OverrideRaw, "override-raw")
|
||||
if len(cfg.Payload.Replace) > 0 {
|
||||
valid := make([]PayloadReplaceRule, 0, len(cfg.Payload.Replace))
|
||||
for i, rule := range cfg.Payload.Replace {
|
||||
if errValidate := rule.Validate(); errValidate != nil {
|
||||
log.Warnf("payload.replace rule %d dropped: %v", i+1, errValidate)
|
||||
continue
|
||||
}
|
||||
valid = append(valid, rule)
|
||||
}
|
||||
cfg.Payload.Replace = valid
|
||||
}
|
||||
}
|
||||
|
||||
func sanitizePayloadRawRules(rules []PayloadRule, section string) []PayloadRule {
|
||||
|
||||
120
internal/config/payload_replace.go
Normal file
120
internal/config/payload_replace.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/MoeForge/moego/json"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// PayloadReplaceRule applies ordered literal replacements to existing user text.
|
||||
type PayloadReplaceRule struct {
|
||||
Models []PayloadModelRule `yaml:"models" json:"models"`
|
||||
Target PayloadReplaceTarget `yaml:"target" json:"target"`
|
||||
Replacements []PayloadReplacement `yaml:"replacements" json:"replacements"`
|
||||
}
|
||||
|
||||
// PayloadReplaceTarget selects one-based user positions in the final upstream body.
|
||||
// All user messages count, including messages without editable text.
|
||||
type PayloadReplaceTarget struct {
|
||||
Role string `yaml:"role" json:"role"`
|
||||
Positions []int `yaml:"positions" json:"positions"`
|
||||
All bool `yaml:"all" json:"all"`
|
||||
}
|
||||
|
||||
// UnmarshalYAML rejects fractional positions instead of allowing YAML integer coercion.
|
||||
func (t *PayloadReplaceTarget) UnmarshalYAML(node *yaml.Node) error {
|
||||
var value struct {
|
||||
Role string `yaml:"role"`
|
||||
Positions []yaml.Node `yaml:"positions"`
|
||||
All bool `yaml:"all"`
|
||||
}
|
||||
if errDecode := node.Decode(&value); errDecode != nil {
|
||||
return errDecode
|
||||
}
|
||||
target := PayloadReplaceTarget{Role: value.Role, All: value.All}
|
||||
if value.Positions != nil {
|
||||
target.Positions = make([]int, len(value.Positions))
|
||||
}
|
||||
for i := range value.Positions {
|
||||
position := &value.Positions[i]
|
||||
if position.Kind == yaml.AliasNode {
|
||||
position = position.Alias
|
||||
}
|
||||
if position == nil || position.Kind != yaml.ScalarNode || position.Tag != "!!int" {
|
||||
return fmt.Errorf("payload replacement positions must be integers")
|
||||
}
|
||||
if errDecode := position.Decode(&target.Positions[i]); errDecode != nil {
|
||||
return errDecode
|
||||
}
|
||||
}
|
||||
*t = target
|
||||
return nil
|
||||
}
|
||||
|
||||
// PayloadReplacement is a single literal source-to-destination mapping.
|
||||
// The enclosing slice, not map iteration order, determines replacement order.
|
||||
type PayloadReplacement map[string]string
|
||||
|
||||
// UnmarshalYAML prevents YAML from coercing numeric or boolean operands to strings.
|
||||
func (r *PayloadReplacement) UnmarshalYAML(node *yaml.Node) error {
|
||||
if node.Kind != yaml.MappingNode || len(node.Content) != 2 {
|
||||
return fmt.Errorf("payload replacement must contain exactly one string mapping")
|
||||
}
|
||||
from, to := node.Content[0], node.Content[1]
|
||||
if from.Kind != yaml.ScalarNode || from.Tag != "!!str" || to.Kind != yaml.ScalarNode || to.Tag != "!!str" {
|
||||
return fmt.Errorf("payload replacement source and destination must be strings")
|
||||
}
|
||||
*r = PayloadReplacement{from.Value: to.Value}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON enforces the same string-only mapping contract as YAML.
|
||||
func (r *PayloadReplacement) UnmarshalJSON(data []byte) error {
|
||||
var values map[string]any
|
||||
if errDecode := json.Unmarshal(data, &values); errDecode != nil {
|
||||
return fmt.Errorf("decode payload replacement: %w", errDecode)
|
||||
}
|
||||
if len(values) != 1 {
|
||||
return fmt.Errorf("payload replacement must contain exactly one string mapping")
|
||||
}
|
||||
for from, value := range values {
|
||||
to, ok := value.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("payload replacement destination must be a string")
|
||||
}
|
||||
*r = PayloadReplacement{from: to}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate rejects unsafe implicit targets and ambiguous replacement entries.
|
||||
func (r PayloadReplaceRule) Validate() error {
|
||||
if r.Target.Role != "user" {
|
||||
return fmt.Errorf("target.role must be user")
|
||||
}
|
||||
if r.Target.All {
|
||||
if len(r.Target.Positions) != 0 {
|
||||
return fmt.Errorf("target.all and target.positions are mutually exclusive")
|
||||
}
|
||||
} else if len(r.Target.Positions) == 0 {
|
||||
return fmt.Errorf("target.positions must not be empty unless target.all is true")
|
||||
}
|
||||
for _, position := range r.Target.Positions {
|
||||
if position < 1 {
|
||||
return fmt.Errorf("target.positions must contain positive one-based positions")
|
||||
}
|
||||
}
|
||||
if len(r.Replacements) == 0 {
|
||||
return fmt.Errorf("replacements must not be empty")
|
||||
}
|
||||
for _, replacement := range r.Replacements {
|
||||
if len(replacement) != 1 {
|
||||
return fmt.Errorf("each replacement must contain exactly one string mapping")
|
||||
}
|
||||
if _, empty := replacement[""]; empty {
|
||||
return fmt.Errorf("replacement source must not be empty")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
177
internal/config/payload_replace_test.go
Normal file
177
internal/config/payload_replace_test.go
Normal file
@@ -0,0 +1,177 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/MoeForge/moego/json"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const payloadReplaceFixture = `config-version: 8
|
||||
requests:
|
||||
payload:
|
||||
replace:
|
||||
- models:
|
||||
- name: "*"
|
||||
protocol: claude
|
||||
from-protocol: openai
|
||||
headers: {X-Tier: "prod-*"}
|
||||
match: [{stage: ready}]
|
||||
not-match: [{blocked: true}]
|
||||
exist: [messages]
|
||||
not-exist: [disabled]
|
||||
target:
|
||||
role: user
|
||||
positions: [1, 2, 2]
|
||||
replacements:
|
||||
- "hero": "fool"
|
||||
- "fool": "friend"
|
||||
- "remove": ""
|
||||
`
|
||||
|
||||
func TestPayloadReplaceConfigRoundTrip(t *testing.T) {
|
||||
cfg, errParse := ParseConfigBytes([]byte(payloadReplaceFixture))
|
||||
if errParse != nil {
|
||||
t.Fatal(errParse)
|
||||
}
|
||||
if len(cfg.Payload.Replace) != 1 {
|
||||
t.Fatalf("missing replace config: %#v", cfg.Payload)
|
||||
}
|
||||
rule := cfg.Payload.Replace[0]
|
||||
if rule.Replacements[0]["hero"] != "fool" || rule.Replacements[1]["fool"] != "friend" || rule.Replacements[2]["remove"] != "" {
|
||||
t.Fatalf("replacement order/empty value lost: %#v", rule.Replacements)
|
||||
}
|
||||
if rule.Models[0].Protocol != "claude" || rule.Models[0].FromProtocol != "openai" {
|
||||
t.Fatalf("lost protocol gates: %#v", rule.Models)
|
||||
}
|
||||
for _, format := range []string{"yaml", "json"} {
|
||||
t.Run(format, func(t *testing.T) {
|
||||
var data []byte
|
||||
var errMarshal error
|
||||
var got PayloadConfig
|
||||
if format == "yaml" {
|
||||
data, errMarshal = yaml.Marshal(cfg.Payload)
|
||||
if errMarshal == nil {
|
||||
errMarshal = yaml.Unmarshal(data, &got)
|
||||
}
|
||||
} else {
|
||||
data, errMarshal = json.Marshal(cfg.Payload)
|
||||
if errMarshal == nil {
|
||||
errMarshal = json.Unmarshal(data, &got)
|
||||
}
|
||||
}
|
||||
if errMarshal != nil {
|
||||
t.Fatal(errMarshal)
|
||||
}
|
||||
if !reflect.DeepEqual(cfg.Payload.Replace, got.Replace) {
|
||||
t.Fatalf("round trip changed config: %s", data)
|
||||
}
|
||||
})
|
||||
}
|
||||
clone := cfg.CloneForRuntime()
|
||||
clone.Payload.Replace[0].Target.Positions[0] = 9
|
||||
clone.Payload.Replace[0].Replacements[0]["hero"] = "changed"
|
||||
clone.Payload.Replace[0].Models[0].Headers["X-Tier"] = "changed"
|
||||
if cfg.Payload.Replace[0].Target.Positions[0] != 1 || cfg.Payload.Replace[0].Replacements[0]["hero"] != "fool" || cfg.Payload.Replace[0].Models[0].Headers["X-Tier"] != "prod-*" {
|
||||
t.Fatal("runtime clone shares replacement state")
|
||||
}
|
||||
|
||||
file := filepath.Join(t.TempDir(), "config.yaml")
|
||||
if errWrite := os.WriteFile(file, []byte(payloadReplaceFixture), 0600); errWrite != nil {
|
||||
t.Fatal(errWrite)
|
||||
}
|
||||
if errSave := SaveConfigPreserveComments(file, cfg, true); errSave != nil {
|
||||
t.Fatal(errSave)
|
||||
}
|
||||
reloaded, errLoad := LoadConfig(file)
|
||||
if errLoad != nil {
|
||||
t.Fatal(errLoad)
|
||||
}
|
||||
if !reflect.DeepEqual(cfg.Payload.Replace, reloaded.Payload.Replace) {
|
||||
t.Fatalf("save/load changed replacement config: %#v", reloaded.Payload.Replace)
|
||||
}
|
||||
// Both selector transitions and source-key edits must discard old YAML values.
|
||||
for _, target := range []PayloadReplaceTarget{{Role: "user", All: true, Positions: []int{}}, {Role: "user", Positions: []int{2}}} {
|
||||
cfg.Payload.Replace[0].Target = target
|
||||
cfg.Payload.Replace[0].Replacements = []PayloadReplacement{{"new-source": "new-destination"}}
|
||||
if errSave := SaveConfigPreserveComments(file, cfg, true); errSave != nil {
|
||||
t.Fatal(errSave)
|
||||
}
|
||||
reloaded, errLoad = LoadConfig(file)
|
||||
if errLoad != nil {
|
||||
t.Fatal(errLoad)
|
||||
}
|
||||
if !reflect.DeepEqual(cfg.Payload.Replace, reloaded.Payload.Replace) {
|
||||
t.Fatalf("selector/key edit retained stale config: %#v", reloaded.Payload.Replace)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayloadReplaceConfigValidation(t *testing.T) {
|
||||
valid := PayloadReplaceRule{Models: []PayloadModelRule{{Name: "*"}}, Target: PayloadReplaceTarget{Role: "user", Positions: []int{1, 2}}, Replacements: []PayloadReplacement{{"hero": "fool"}}}
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
mutate func(*PayloadReplaceRule)
|
||||
}{
|
||||
{"missing role", func(r *PayloadReplaceRule) { r.Target.Role = "" }},
|
||||
{"other role", func(r *PayloadReplaceRule) { r.Target.Role = "assistant" }},
|
||||
{"missing target", func(r *PayloadReplaceRule) { r.Target.Positions = nil }},
|
||||
{"zero", func(r *PayloadReplaceRule) { r.Target.Positions = []int{0} }},
|
||||
{"negative", func(r *PayloadReplaceRule) { r.Target.Positions = []int{-1} }},
|
||||
{"conflicting target", func(r *PayloadReplaceRule) { r.Target.All = true }},
|
||||
{"no replacements", func(r *PayloadReplaceRule) { r.Replacements = nil }},
|
||||
{"empty mapping", func(r *PayloadReplaceRule) { r.Replacements = []PayloadReplacement{{}} }},
|
||||
{"multiple mappings", func(r *PayloadReplaceRule) { r.Replacements = []PayloadReplacement{{"a": "b", "c": "d"}} }},
|
||||
{"empty source", func(r *PayloadReplaceRule) { r.Replacements = []PayloadReplacement{{"": "b"}} }},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
rule := valid
|
||||
tt.mutate(&rule)
|
||||
if rule.Validate() == nil {
|
||||
t.Fatal("invalid rule accepted")
|
||||
}
|
||||
cfg := &Config{Payload: PayloadConfig{Replace: []PayloadReplaceRule{rule, valid}}}
|
||||
cfg.SanitizePayloadRules()
|
||||
if len(cfg.Payload.Replace) != 1 || !reflect.DeepEqual(cfg.Payload.Replace[0], valid) {
|
||||
t.Fatalf("sanitizer lost valid rule: %#v", cfg.Payload.Replace)
|
||||
}
|
||||
})
|
||||
}
|
||||
valid.Target = PayloadReplaceTarget{Role: "user", All: true}
|
||||
valid.Replacements = []PayloadReplacement{{" ": ""}}
|
||||
if errValidate := valid.Validate(); errValidate != nil {
|
||||
t.Fatal(errValidate)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayloadReplaceRejectsNonIntegerPositions(t *testing.T) {
|
||||
for _, input := range []string{`{role: user, positions: [1.5]}`, `{role: user, positions: ["1"]}`, `{role: user, positions: [true]}`, `{role: user, positions: [null]}`, `{role: user, positions: [999999999999999999999999999]}`} {
|
||||
var target PayloadReplaceTarget
|
||||
if errDecode := yaml.Unmarshal([]byte(input), &target); errDecode == nil {
|
||||
t.Fatalf("accepted noninteger or overflowing positions: %s", input)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayloadReplacementRejectsInvalidYAML(t *testing.T) {
|
||||
for _, input := range []string{`{1: "b"}`, `{"a": 1}`, `{"a": true}`, `{"a": null}`, `{"a": []}`, `{"a": "b", "c": "d"}`, `{"a": "b", "a": "c"}`, `[]`, `"a"`} {
|
||||
var replacement PayloadReplacement
|
||||
if errDecode := yaml.Unmarshal([]byte(input), &replacement); errDecode == nil {
|
||||
t.Fatalf("accepted invalid mapping %s", input)
|
||||
}
|
||||
}
|
||||
for _, input := range []string{`{"a":1}`, `{"a":true}`, `{"a":null}`, `null`, `{}`, `{"a":"b","c":"d"}`} {
|
||||
var replacement PayloadReplacement
|
||||
if errDecode := json.Unmarshal([]byte(input), &replacement); errDecode == nil {
|
||||
t.Fatalf("accepted nonstring JSON %s", input)
|
||||
}
|
||||
}
|
||||
// Numeric-looking text remains valid when explicitly quoted.
|
||||
var replacement PayloadReplacement
|
||||
if errDecode := yaml.Unmarshal([]byte(`{"1": "false"}`), &replacement); errDecode != nil {
|
||||
t.Fatal(errDecode)
|
||||
}
|
||||
}
|
||||
@@ -143,7 +143,7 @@ func applyPayloadConfigWithTrackedPathsForExecutorAndAuthDoc(cfg *config.Config,
|
||||
}
|
||||
|
||||
rules := cfg.Payload
|
||||
hasPayloadRules := len(rules.Default) != 0 || len(rules.DefaultRaw) != 0 || len(rules.Override) != 0 || len(rules.OverrideRaw) != 0 || len(rules.Filter) != 0
|
||||
hasPayloadRules := len(rules.Default) != 0 || len(rules.DefaultRaw) != 0 || len(rules.Override) != 0 || len(rules.OverrideRaw) != 0 || len(rules.Replace) != 0 || len(rules.Filter) != 0
|
||||
if hasPayloadRules {
|
||||
model = strings.TrimSpace(model)
|
||||
requestedModel = strings.TrimSpace(requestedModel)
|
||||
@@ -375,6 +375,16 @@ func applyPayloadConfigWithTrackedPathsForExecutorAndAuthDoc(cfg *config.Config,
|
||||
}
|
||||
}
|
||||
}
|
||||
// Replace text after overrides and before final field deletion.
|
||||
for i := range rules.Replace {
|
||||
rule := &rules.Replace[i]
|
||||
if rule.Validate() != nil || !payloadModelRulesMatchDoc(rule.Models, protocol, fromProtocol, headers, doc, root, candidates) {
|
||||
continue
|
||||
}
|
||||
if replacePayloadUserTextDoc(doc, protocol, root, rule, noteRuleWrite) {
|
||||
mutated = true
|
||||
}
|
||||
}
|
||||
// Apply filter rules: remove matching paths from payload.
|
||||
for i := range rules.Filter {
|
||||
rule := &rules.Filter[i]
|
||||
|
||||
101
internal/runtime/executor/helps/payload_replace.go
Normal file
101
internal/runtime/executor/helps/payload_replace.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package helps
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/MoeForge/moego/ojson"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
||||
)
|
||||
|
||||
// replacePayloadUserTextDoc edits only existing protocol-defined user text fields.
|
||||
// Paths are collected before writes, so no result views survive a document mutation.
|
||||
func replacePayloadUserTextDoc(doc *ojson.Doc, protocol, root string, rule *config.PayloadReplaceRule, noteWrite func(string)) bool {
|
||||
selected := make(map[int]bool, len(rule.Target.Positions))
|
||||
for _, position := range rule.Target.Positions {
|
||||
selected[position] = true
|
||||
}
|
||||
var paths []string
|
||||
protocol = strings.ToLower(strings.TrimSpace(protocol))
|
||||
var arrayPath string
|
||||
switch protocol {
|
||||
case "openai", "claude":
|
||||
arrayPath = buildPayloadPath(root, "messages")
|
||||
case "codex", "responses", "response", "openai-response", "openai-responses", "meta":
|
||||
arrayPath = buildPayloadPath(root, "input")
|
||||
if doc.Get(arrayPath).Type == ojson.String {
|
||||
if rule.Target.All || selected[1] {
|
||||
paths = append(paths, arrayPath)
|
||||
}
|
||||
}
|
||||
case "gemini", "antigravity":
|
||||
arrayPath = buildPayloadPath(root, "contents")
|
||||
default:
|
||||
return false
|
||||
}
|
||||
array := doc.Get(arrayPath)
|
||||
if array.IsArray() {
|
||||
position := 0
|
||||
for index, message := range array.Array() {
|
||||
if !message.IsObject() || message.Get("role").String() != "user" {
|
||||
continue
|
||||
}
|
||||
if protocol != "openai" && protocol != "claude" && protocol != "gemini" && protocol != "antigravity" {
|
||||
if typ := message.Get("type"); typ.Exists() && (typ.Type != ojson.String || typ.String() != "message") {
|
||||
continue
|
||||
}
|
||||
}
|
||||
position++
|
||||
if !rule.Target.All && !selected[position] {
|
||||
continue
|
||||
}
|
||||
messagePath := arrayPath + "." + strconv.Itoa(index)
|
||||
if protocol == "gemini" || protocol == "antigravity" {
|
||||
parts := message.Get("parts")
|
||||
if !parts.IsArray() {
|
||||
continue
|
||||
}
|
||||
for partIndex, part := range parts.Array() {
|
||||
if part.IsObject() && part.Get("text").Type == ojson.String && !part.Get("thought").Bool() &&
|
||||
!part.Get("functionCall").Exists() && !part.Get("functionResponse").Exists() {
|
||||
paths = append(paths, messagePath+".parts."+strconv.Itoa(partIndex)+".text")
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
content := message.Get("content")
|
||||
if content.Type == ojson.String {
|
||||
paths = append(paths, messagePath+".content")
|
||||
} else if content.IsArray() {
|
||||
for blockIndex, block := range content.Array() {
|
||||
if !block.IsObject() || block.Get("text").Type != ojson.String || block.Get("thought").Bool() {
|
||||
continue
|
||||
}
|
||||
typ := block.Get("type").String()
|
||||
if typ == "text" || (typ == "input_text" && protocol != "openai" && protocol != "claude") {
|
||||
paths = append(paths, messagePath+".content."+strconv.Itoa(blockIndex)+".text")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
mutated := false
|
||||
for _, path := range paths {
|
||||
text := doc.Get(path).String()
|
||||
updated := text
|
||||
for _, replacement := range rule.Replacements {
|
||||
for from, to := range replacement {
|
||||
updated = strings.ReplaceAll(updated, from, to)
|
||||
}
|
||||
}
|
||||
if updated == text {
|
||||
continue
|
||||
}
|
||||
if errSet := doc.Set(path, updated); errSet != nil {
|
||||
continue
|
||||
}
|
||||
mutated = true
|
||||
noteWrite(path)
|
||||
}
|
||||
return mutated
|
||||
}
|
||||
163
internal/runtime/executor/helps/payload_replace_test.go
Normal file
163
internal/runtime/executor/helps/payload_replace_test.go
Normal file
@@ -0,0 +1,163 @@
|
||||
package helps
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func TestPayloadReplaceUserText(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name, protocol, root, input string
|
||||
positions []int
|
||||
all bool
|
||||
want map[string]string
|
||||
}{
|
||||
{"chat roles and occurrences", "openai", "", `{"messages":[{"role":"system","content":"you are hero"},{"role":"user","content":"prefix you are hero; you are hero"},{"role":"assistant","content":"you are hero"},{"role":"user","content":"you are hero"},{"role":"user","content":"you are hero"}]}`, []int{1, 2}, false, map[string]string{"messages.0.content": "you are hero", "messages.1.content": "prefix you are fool; you are fool", "messages.2.content": "you are hero", "messages.3.content": "you are fool", "messages.4.content": "you are hero"}},
|
||||
{"single user skips missing second", "openai", "", `{"messages":[{"role":"user","content":"you are hero"}]}`, []int{1, 2}, false, map[string]string{"messages.0.content": "you are fool"}},
|
||||
{"all users", "openai", "", `{"messages":[{"role":"user","content":"you are hero"},{"role":"user","content":"you are hero"}]}`, nil, true, map[string]string{"messages.0.content": "you are fool", "messages.1.content": "you are fool"}},
|
||||
{"claude tools and metadata protected", "claude", "", `{"system":"you are hero","messages":[{"role":"user","content":[{"type":"tool_result","content":"you are hero"}]},{"role":"user","content":[{"type":"text","text":"you are hero","cache_control":{"type":"ephemeral"}},{"type":"tool_result","content":[{"type":"text","text":"you are hero"}]},{"type":"thinking","text":"you are hero","signature":"you are hero"},{"type":"image","source":{"data":"you are hero"}}]}]}`, []int{2}, false, map[string]string{"system": "you are hero", "messages.0.content.0.content": "you are hero", "messages.1.content.0.text": "you are fool", "messages.1.content.0.cache_control.type": "ephemeral", "messages.1.content.1.content.0.text": "you are hero", "messages.1.content.2.text": "you are hero", "messages.1.content.2.signature": "you are hero", "messages.1.content.3.source.data": "you are hero"}},
|
||||
{"chat multimodal", "openai", "", `{"messages":[{"role":"user","content":[{"type":"text","text":"you are hero"},{"type":"image_url","image_url":{"url":"you are hero"}},{"type":"input_audio","input_audio":{"data":"you are hero"}},{"type":"input_text","text":"you are hero"}]}]}`, []int{1}, false, map[string]string{"messages.0.content.0.text": "you are fool", "messages.0.content.1.image_url.url": "you are hero", "messages.0.content.2.input_audio.data": "you are hero", "messages.0.content.3.text": "you are hero"}},
|
||||
{"responses message types", "openai-response", "", `{"input":[{"type":"function_call_output","role":"user","output":"you are hero","content":"you are hero"},{"type":"message","role":"assistant","content":"you are hero"},{"role":"user","content":[{"type":"input_text","text":"you are hero"},{"type":"input_image","image_url":"you are hero"}]},{"type":"message","role":"user","content":"you are hero"}],"instructions":"you are hero"}`, []int{1, 2}, false, map[string]string{"input.0.output": "you are hero", "input.0.content": "you are hero", "input.1.content": "you are hero", "input.2.content.0.text": "you are fool", "input.2.content.1.image_url": "you are hero", "input.3.content": "you are fool", "instructions": "you are hero"}},
|
||||
{"codex string input", "codex", "", `{"input":"you are hero","previous_response_id":"you are hero"}`, []int{1, 2}, false, map[string]string{"input": "you are fool", "previous_response_id": "you are hero"}},
|
||||
{"gemini parts", "gemini", "", `{"contents":[{"role":"model","parts":[{"text":"you are hero"}]},{"role":"user","parts":[{"text":"you are hero"},{"text":"you are hero","thought":true,"thoughtSignature":"you are hero"},{"functionResponse":{"response":{"text":"you are hero"}}},{"inlineData":{"data":"you are hero"}}]}]}`, []int{1}, false, map[string]string{"contents.0.parts.0.text": "you are hero", "contents.1.parts.0.text": "you are fool", "contents.1.parts.1.text": "you are hero", "contents.1.parts.1.thoughtSignature": "you are hero", "contents.1.parts.2.functionResponse.response.text": "you are hero", "contents.1.parts.3.inlineData.data": "you are hero"}},
|
||||
{"antigravity root", "antigravity", "request", `{"request":{"contents":[{"role":"user","parts":[{"text":"you are hero"}]}]},"text":"you are hero"}`, []int{1}, false, map[string]string{"request.contents.0.parts.0.text": "you are fool", "text": "you are hero"}},
|
||||
{"empty users still count", "claude", "", `{"messages":[{"role":"user","content":null},{"role":"user","content":[]},{"role":"user","content":23},{"role":"user"},{"role":"user","content":"you are hero"}]}`, []int{5}, false, map[string]string{"messages.4.content": "you are fool"}},
|
||||
{"unmatched first still counts", "openai", "", `{"messages":[{"role":"user","content":"hello"},{"role":"user","content":"you are hero"}]}`, []int{1}, false, map[string]string{"messages.0.content": "hello", "messages.1.content": "you are hero"}},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfg := &config.Config{Payload: config.PayloadConfig{Replace: []config.PayloadReplaceRule{{
|
||||
Models: []config.PayloadModelRule{{Name: "*"}}, Target: config.PayloadReplaceTarget{Role: "user", Positions: tt.positions, All: tt.all},
|
||||
Replacements: []config.PayloadReplacement{{"you are hero": "you are fool"}},
|
||||
}}}}
|
||||
out := ApplyPayloadConfigWithRoot(cfg, "model", tt.protocol, tt.root, []byte(tt.input), nil, "", "")
|
||||
for path, want := range tt.want {
|
||||
if got := ojson.GetBytes(out, path).String(); got != want {
|
||||
t.Errorf("%s = %q, want %q; body: %s", path, got, want, out)
|
||||
}
|
||||
}
|
||||
// Replacement must preserve the entire structure and every unrelated field.
|
||||
wantDoc := ojson.NewDocString(tt.input)
|
||||
for path, value := range tt.want {
|
||||
if errSet := wantDoc.Set(path, value); errSet != nil {
|
||||
t.Fatal(errSet)
|
||||
}
|
||||
}
|
||||
if !reflect.DeepEqual(ojson.ParseBytes(out).Value(), ojson.ParseBytes(wantDoc.Bytes()).Value()) {
|
||||
t.Fatalf("unexpected structural change: %s, want %s", out, wantDoc.Bytes())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayloadReplaceNoOp(t *testing.T) {
|
||||
for _, input := range []string{
|
||||
`{}`, `{"messages":null}`, `{"messages":{}}`, `{"messages":[]}`,
|
||||
`{"messages":[null,1,"user",{}, {"role":"assistant","content":"you are hero"}]}`,
|
||||
`{"messages":[{"role":"user","content":null},{"role":"user","content":123},{"role":"user","content":{"text":"you are hero"}}]}`,
|
||||
`{"messages":[{"role":"user","content":[{"type":"text","text":"you are "},{"type":"text","text":"hero"},{"type":"text","text":123},{"text":"you are hero"}]}]}`,
|
||||
`{"messages":[{"role":"user","content":"You are hero"}]}`,
|
||||
} {
|
||||
cfg := &config.Config{Payload: config.PayloadConfig{Replace: []config.PayloadReplaceRule{{Models: []config.PayloadModelRule{{Name: "*"}}, Target: config.PayloadReplaceTarget{Role: "user", All: true}, Replacements: []config.PayloadReplacement{{"you are hero": "you are fool"}}}}}}
|
||||
body := []byte(input)
|
||||
out := ApplyPayloadConfigWithRoot(cfg, "model", "openai", "", body, nil, "", "")
|
||||
if !bytes.Equal(out, body) || &out[0] != &body[0] {
|
||||
t.Fatalf("no-op changed input: %s -> %s", body, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayloadReplaceOrderingAndPositions(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
replacements []config.PayloadReplacement
|
||||
want string
|
||||
}{
|
||||
{"ordered cascading", []config.PayloadReplacement{{"hero": "fool"}, {"fool": "friend"}}, "friend friend"},
|
||||
{"single pass and duplicate positions", []config.PayloadReplacement{{"hero": "hero!"}}, "hero! hero!"},
|
||||
{"delete", []config.PayloadReplacement{{"hero": ""}}, " "},
|
||||
{"identity", []config.PayloadReplacement{{"hero": "hero"}}, "hero hero"},
|
||||
{"literal not regex", []config.PayloadReplacement{{"h.*": "x"}}, "hero hero"},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfg := &config.Config{Payload: config.PayloadConfig{Replace: []config.PayloadReplaceRule{{Models: []config.PayloadModelRule{{Name: "*"}}, Target: config.PayloadReplaceTarget{Role: "user", Positions: []int{2, 1, 1, 1000000}}, Replacements: tt.replacements}}}}
|
||||
out := ApplyPayloadConfigWithRoot(cfg, "model", "openai", "", []byte(`{"messages":[{"role":"user","content":"hero hero"}]}`), nil, "", "")
|
||||
if got := ojson.GetBytes(out, "messages.0.content").String(); got != tt.want {
|
||||
t.Fatalf("got %q, want %q", got, tt.want)
|
||||
}
|
||||
if got := len(ojson.GetBytes(out, "messages").Array()); got != 1 {
|
||||
t.Fatalf("created missing messages: %s", out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayloadReplaceMatchingAndStageOrder(t *testing.T) {
|
||||
rule := config.PayloadReplaceRule{
|
||||
Models: []config.PayloadModelRule{{Name: "alias-*", Protocol: "claude", FromProtocol: "responses", Headers: map[string]string{"X-Tier": "prod-*"}, Match: []map[string]any{{"stage": "ready"}}, NotMatch: []map[string]any{{"blocked": true}}, Exist: []string{"messages.0.content"}, NotExist: []string{"disabled"}}},
|
||||
Target: config.PayloadReplaceTarget{Role: "user", Positions: []int{1}}, Replacements: []config.PayloadReplacement{{"hero": "hero!"}},
|
||||
}
|
||||
// Multiple matching model entries must not execute a rule more than once.
|
||||
rule.Models = append(rule.Models, rule.Models[0])
|
||||
cfg := &config.Config{Payload: config.PayloadConfig{
|
||||
Default: []config.PayloadRule{{Models: []config.PayloadModelRule{{Name: "*"}}, Params: map[string]any{"stage": "ready"}}},
|
||||
Override: []config.PayloadRule{{Models: []config.PayloadModelRule{{Name: "*"}}, Params: map[string]any{"messages.0.content": "hero"}}},
|
||||
Replace: []config.PayloadReplaceRule{rule, {Models: []config.PayloadModelRule{{Name: "*", Match: []map[string]any{{"messages.0.content": "hero!"}}}}, Target: rule.Target, Replacements: []config.PayloadReplacement{{"!": "!!"}}}},
|
||||
Filter: []config.PayloadFilterRule{{Models: []config.PayloadModelRule{{Name: "*", Match: []map[string]any{{"messages.0.content": "hero!!"}}}}, Params: []string{"stage"}}},
|
||||
}}
|
||||
out, touched := ApplyPayloadConfigWithTrackedPaths(cfg, "upstream", "claude", "openai-response", "", []byte(`{"messages":[{"role":"user","content":"original"}]}`), nil, "alias-model", "", http.Header{"X-Tier": {"prod-a"}}, "messages")
|
||||
if got := ojson.GetBytes(out, "messages.0.content").String(); got != "hero!!" || ojson.GetBytes(out, "stage").Exists() || !touched["messages"] {
|
||||
t.Fatalf("unexpected stage order or tracking: %s, touched=%v", out, touched)
|
||||
}
|
||||
for _, tt := range []struct{ name, model, protocol, from, input, header string }{
|
||||
{"model", "other", "claude", "responses", `{}`, "prod-a"},
|
||||
{"protocol", "alias-model", "openai", "responses", `{}`, "prod-a"},
|
||||
{"source", "alias-model", "claude", "openai", `{}`, "prod-a"},
|
||||
{"headers", "alias-model", "claude", "responses", `{}`, "dev"},
|
||||
{"match", "alias-model", "claude", "responses", `{"stage":"wrong"}`, "prod-a"},
|
||||
{"not match", "alias-model", "claude", "responses", `{"stage":"ready","blocked":true}`, "prod-a"},
|
||||
{"not exist", "alias-model", "claude", "responses", `{"stage":"ready","disabled":false}`, "prod-a"},
|
||||
{"exist", "alias-model", "claude", "responses", `{"stage":"ready"}`, "prod-a"},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
doc := ojson.NewDocString(tt.input)
|
||||
if tt.name != "exist" {
|
||||
_ = doc.Set("messages", []any{map[string]any{"role": "user", "content": "hero"}})
|
||||
}
|
||||
body := doc.Bytes()
|
||||
out := ApplyPayloadConfigWithRequest(&config.Config{Payload: config.PayloadConfig{Replace: []config.PayloadReplaceRule{rule}}}, tt.model, tt.protocol, tt.from, "", body, nil, "", "", http.Header{"X-Tier": {tt.header}})
|
||||
if !bytes.Equal(out, body) {
|
||||
t.Fatalf("nonmatching rule modified body: %s", out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayloadReplaceFinalizerRetryAndFilter(t *testing.T) {
|
||||
raw := []byte(`{"input":[{"role":"user","content":"hero"}]}`)
|
||||
cfg := &config.Config{Payload: config.PayloadConfig{Replace: []config.PayloadReplaceRule{{Models: []config.PayloadModelRule{{Name: "*"}}, Target: config.PayloadReplaceTarget{Role: "user", All: true}, Replacements: []config.PayloadReplacement{{"hero": "hero!"}}}}}}
|
||||
finalize := NewPayloadFinalizer(cfg, "codex", "model", "codex", "", raw, cliproxyexecutor.Request{Model: "model", Payload: raw}, cliproxyexecutor.Options{})
|
||||
for attempt := 0; attempt < 3; attempt++ {
|
||||
doc := ojson.NewDoc(append([]byte(nil), raw...))
|
||||
FinalizePayloadDoc(WithPayloadFinalizer(context.Background(), finalize), doc)
|
||||
if got := doc.Get("input.0.content").String(); got != "hero!" {
|
||||
t.Fatalf("attempt %d: %q", attempt, got)
|
||||
}
|
||||
if out := finalize(append([]byte(nil), raw...)); ojson.GetBytes(out, "input.0.content").String() != "hero!" {
|
||||
t.Fatalf("byte finalizer differs: %s", out)
|
||||
}
|
||||
}
|
||||
if ojson.GetBytes(raw, "input.0.content").String() != "hero" {
|
||||
t.Fatal("finalizer mutated original request")
|
||||
}
|
||||
cfg.Payload.Filter = []config.PayloadFilterRule{{Models: []config.PayloadModelRule{{Name: "*"}}, Params: []string{"input"}}}
|
||||
if out := finalize(raw); ojson.GetBytes(out, "input").Exists() {
|
||||
t.Fatalf("filter did not run last: %s", out)
|
||||
}
|
||||
}
|
||||
191
internal/runtime/executor/payload_replace_test.go
Normal file
191
internal/runtime/executor/payload_replace_test.go
Normal file
@@ -0,0 +1,191 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/MoeForge/moego/ojson"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps"
|
||||
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
|
||||
)
|
||||
|
||||
func TestPayloadReplaceCodexTransports(t *testing.T) {
|
||||
for _, transport := range []string{"http", "websocket"} {
|
||||
for _, stream := range []bool{false, true} {
|
||||
t.Run(transport+map[bool]string{false: "/execute", true: "/stream"}[stream], func(t *testing.T) {
|
||||
captured := make(chan []byte, 1)
|
||||
completed := []byte(`{"type":"response.completed","response":{"id":"resp_replace","status":"completed","output":[],"usage":{"input_tokens":0,"output_tokens":0,"total_tokens":0}}}`)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if transport == "websocket" {
|
||||
conn, errUpgrade := (&websocket.Upgrader{}).Upgrade(w, r, nil)
|
||||
if errUpgrade != nil {
|
||||
t.Error(errUpgrade)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if errClose := conn.Close(); errClose != nil {
|
||||
t.Error(errClose)
|
||||
}
|
||||
}()
|
||||
_, body, errRead := conn.ReadMessage()
|
||||
if errRead != nil {
|
||||
t.Error(errRead)
|
||||
return
|
||||
}
|
||||
captured <- body
|
||||
if errWrite := conn.WriteMessage(websocket.TextMessage, completed); errWrite != nil {
|
||||
t.Error(errWrite)
|
||||
}
|
||||
return
|
||||
}
|
||||
body, errRead := io.ReadAll(r.Body)
|
||||
if errRead != nil {
|
||||
t.Error(errRead)
|
||||
return
|
||||
}
|
||||
captured <- body
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
_, _ = w.Write(append(append([]byte("data: "), completed...), '\n', '\n'))
|
||||
}))
|
||||
defer server.Close()
|
||||
cfg := &config.Config{Payload: config.PayloadConfig{Replace: []config.PayloadReplaceRule{{
|
||||
Models: []config.PayloadModelRule{{Name: "*", Protocol: "codex", FromProtocol: "responses"}},
|
||||
Target: config.PayloadReplaceTarget{Role: "user", Positions: []int{1, 2}}, Replacements: []config.PayloadReplacement{{"hero": "hero!"}},
|
||||
}}}}
|
||||
var executor cliproxyauth.ProviderExecutor = NewCodexExecutor(cfg)
|
||||
if transport == "websocket" {
|
||||
executor = NewCodexWebsocketsExecutor(cfg)
|
||||
}
|
||||
auth := &cliproxyauth.Auth{ID: t.Name(), Provider: "codex", Attributes: map[string]string{"api_key": "test", "base_url": server.URL, "plan_type": "pro"}}
|
||||
req := cliproxyexecutor.Request{Model: "gpt-5.4", Payload: []byte(`{"input":[{"role":"user","content":[{"type":"input_text","text":"hero"}]}]}`)}
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatOpenAIResponse}
|
||||
if stream {
|
||||
result, errStream := executor.ExecuteStream(context.Background(), auth, req, opts)
|
||||
if errStream != nil {
|
||||
t.Fatal(errStream)
|
||||
}
|
||||
for chunk := range result.Chunks {
|
||||
if chunk.Err != nil {
|
||||
t.Fatal(chunk.Err)
|
||||
}
|
||||
}
|
||||
} else if _, errExecute := executor.Execute(context.Background(), auth, req, opts); errExecute != nil {
|
||||
t.Fatal(errExecute)
|
||||
}
|
||||
body := <-captured
|
||||
if got := ojson.GetBytes(body, "input.0.content.0.text").String(); got != "hero!" {
|
||||
t.Fatalf("replacement missing or repeated: %s", body)
|
||||
}
|
||||
if len(ojson.GetBytes(body, "input").Array()) != 1 {
|
||||
t.Fatalf("created nonexistent second user: %s", body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayloadReplaceClaudeFinalBarrier(t *testing.T) {
|
||||
cfg := &config.Config{Payload: config.PayloadConfig{Replace: []config.PayloadReplaceRule{{Models: []config.PayloadModelRule{{Name: "*", Protocol: "claude"}}, Target: config.PayloadReplaceTarget{Role: "user", Positions: []int{1, 2}}, Replacements: []config.PayloadReplacement{{"hero": "hero!"}}}}}}
|
||||
for _, stream := range []bool{false, true} {
|
||||
body := executeClaudeContextManagementRequest(t, cfg, []byte(`{"model":"claude-opus-5","messages":[{"role":"user","content":"hero"},{"role":"assistant","content":"hero"},{"role":"user","content":"hero"}]}`), stream)
|
||||
for _, index := range []string{"0", "2"} {
|
||||
content := ojson.GetBytes(body, "messages."+index+".content")
|
||||
matches := 0
|
||||
if content.Type == ojson.String && content.String() == "hero!" {
|
||||
matches++
|
||||
}
|
||||
if content.IsArray() {
|
||||
for _, block := range content.Array() {
|
||||
if block.Get("text").String() == "hero!" {
|
||||
matches++
|
||||
}
|
||||
}
|
||||
}
|
||||
if matches != 1 {
|
||||
t.Fatalf("user %s not replaced exactly once: %s", index, body)
|
||||
}
|
||||
}
|
||||
if got := ojson.GetBytes(body, "messages.1.content").String(); got != "hero" {
|
||||
t.Fatalf("assistant changed: %s", body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayloadReplaceGeminiMergedUsers(t *testing.T) {
|
||||
for _, position := range []int{1, 2} {
|
||||
cfg := &config.Config{Payload: config.PayloadConfig{Replace: []config.PayloadReplaceRule{{
|
||||
Models: []config.PayloadModelRule{{Name: "*", Protocol: "gemini", FromProtocol: "claude"}},
|
||||
Target: config.PayloadReplaceTarget{Role: "user", Positions: []int{position}}, Replacements: []config.PayloadReplacement{{"hero": "hero!"}},
|
||||
}}}}
|
||||
executor := NewAIStudioExecutor(cfg, "aistudio", nil)
|
||||
req := cliproxyexecutor.Request{Model: "gemini-3.1-pro-preview", Payload: []byte(`{"messages":[{"role":"user","content":"hero"},{"role":"user","content":"hero"}]}`)}
|
||||
for _, stream := range []bool{false, true} {
|
||||
body, _, errTranslate := executor.translateRequest(context.Background(), req, cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude}, stream)
|
||||
if errTranslate != nil {
|
||||
t.Fatal(errTranslate)
|
||||
}
|
||||
contents := ojson.GetBytes(body, "contents").Array()
|
||||
if len(contents) != 1 {
|
||||
t.Fatalf("expected adjacent users to merge: %s", body)
|
||||
}
|
||||
want := "hero"
|
||||
if position == 1 {
|
||||
want = "hero!"
|
||||
}
|
||||
parts := contents[0].Get("parts").Array()
|
||||
if len(parts) != 2 || parts[0].Get("text").String() != want || parts[1].Get("text").String() != want {
|
||||
t.Fatalf("selection must use final user positions (%d): %s", position, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayloadReplaceCodexCountTokens(t *testing.T) {
|
||||
cfg := &config.Config{Payload: config.PayloadConfig{Replace: []config.PayloadReplaceRule{{Models: []config.PayloadModelRule{{Name: "*"}}, Target: config.PayloadReplaceTarget{Role: "user", All: true}, Replacements: []config.PayloadReplacement{{"hero": strings.Repeat("replacement text ", 100)}}}}}}
|
||||
req := cliproxyexecutor.Request{Model: "gpt-5", Payload: []byte(`{"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"hero"}]}]}`)}
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatOpenAIResponse}
|
||||
configured, errConfigured := NewCodexExecutor(cfg).CountTokens(context.Background(), nil, req, opts)
|
||||
if errConfigured != nil {
|
||||
t.Fatal(errConfigured)
|
||||
}
|
||||
baseline, errBaseline := NewCodexExecutor(&config.Config{}).CountTokens(context.Background(), nil, req, opts)
|
||||
if errBaseline != nil {
|
||||
t.Fatal(errBaseline)
|
||||
}
|
||||
if ojson.GetBytes(configured.Payload, "response.usage.input_tokens").Int() <= ojson.GetBytes(baseline.Payload, "response.usage.input_tokens").Int() {
|
||||
t.Fatalf("token count did not include replacement: %s vs %s", configured.Payload, baseline.Payload)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayloadReplaceAntigravityRebuiltAttempts(t *testing.T) {
|
||||
cfg := &config.Config{Payload: config.PayloadConfig{Replace: []config.PayloadReplaceRule{{Models: []config.PayloadModelRule{{Name: "*", Protocol: "antigravity"}}, Target: config.PayloadReplaceTarget{Role: "user", Positions: []int{1, 2}}, Replacements: []config.PayloadReplacement{{"hero": "hero!"}}}}}}
|
||||
raw := []byte(`{"request":{"contents":[{"role":"user","parts":[{"text":"hero"}]}]}}`)
|
||||
req := cliproxyexecutor.Request{Model: "gemini-3.1-pro-preview", Payload: raw}
|
||||
ctx := helps.WithPayloadFinalizer(context.Background(), helps.NewPayloadFinalizer(cfg, "antigravity", req.Model, "antigravity", "request", raw, req, cliproxyexecutor.Options{}))
|
||||
executor := NewAntigravityExecutor(cfg)
|
||||
auth := &cliproxyauth.Auth{Metadata: map[string]any{"project_id": "test"}}
|
||||
for attempt := 0; attempt < 2; attempt++ {
|
||||
request, errBuild := executor.buildRequest(ctx, auth, "token", req.Model, raw, attempt == 1, "", "https://example.invalid", "session")
|
||||
if errBuild != nil {
|
||||
t.Fatal(errBuild)
|
||||
}
|
||||
body, errRead := io.ReadAll(request.Body)
|
||||
if errClose := request.Body.Close(); errClose != nil {
|
||||
t.Error(errClose)
|
||||
}
|
||||
if errRead != nil {
|
||||
t.Fatal(errRead)
|
||||
}
|
||||
if got := ojson.GetBytes(body, "request.contents.0.parts.0.text").String(); got != "hero!" {
|
||||
t.Fatalf("attempt %d replacement missing or accumulated: %s", attempt, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -611,18 +611,12 @@ func appendPayloadConfigChanges(changes []string, oldPayload, newPayload config.
|
||||
changes = appendPayloadRuleChanges(changes, "default-raw", oldPayload.DefaultRaw, newPayload.DefaultRaw)
|
||||
changes = appendPayloadRuleChanges(changes, "override", oldPayload.Override, newPayload.Override)
|
||||
changes = appendPayloadRuleChanges(changes, "override-raw", oldPayload.OverrideRaw, newPayload.OverrideRaw)
|
||||
changes = appendPayloadFilterRuleChanges(changes, "filter", oldPayload.Filter, newPayload.Filter)
|
||||
changes = appendPayloadRuleChanges(changes, "replace", oldPayload.Replace, newPayload.Replace)
|
||||
changes = appendPayloadRuleChanges(changes, "filter", oldPayload.Filter, newPayload.Filter)
|
||||
return changes
|
||||
}
|
||||
|
||||
func appendPayloadRuleChanges(changes []string, section string, oldRules, newRules []config.PayloadRule) []string {
|
||||
if reflect.DeepEqual(oldRules, newRules) {
|
||||
return changes
|
||||
}
|
||||
return append(changes, fmt.Sprintf("payload.%s: updated (%d -> %d rules)", section, len(oldRules), len(newRules)))
|
||||
}
|
||||
|
||||
func appendPayloadFilterRuleChanges(changes []string, section string, oldRules, newRules []config.PayloadFilterRule) []string {
|
||||
func appendPayloadRuleChanges[T any](changes []string, section string, oldRules, newRules []T) []string {
|
||||
if reflect.DeepEqual(oldRules, newRules) {
|
||||
return changes
|
||||
}
|
||||
|
||||
39
internal/watcher/diff/payload_replace_test.go
Normal file
39
internal/watcher/diff/payload_replace_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package diff
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
||||
)
|
||||
|
||||
func TestBuildConfigChangeDetailsPayloadReplace(t *testing.T) {
|
||||
oldCfg := &config.Config{}
|
||||
newCfg := &config.Config{Payload: config.PayloadConfig{Replace: []config.PayloadReplaceRule{{
|
||||
Models: []config.PayloadModelRule{{Name: "*"}}, Target: config.PayloadReplaceTarget{Role: "user", Positions: []int{1, 2}}, Replacements: []config.PayloadReplacement{{"private-source": "private-destination"}},
|
||||
}}}}
|
||||
for _, tt := range []struct {
|
||||
old, new *config.Config
|
||||
want string
|
||||
}{
|
||||
{oldCfg, newCfg, "payload.replace: updated (0 -> 1 rules)"},
|
||||
{newCfg, oldCfg, "payload.replace: updated (1 -> 0 rules)"},
|
||||
} {
|
||||
changes := BuildConfigChangeDetails(tt.old, tt.new)
|
||||
if len(changes) != 1 || changes[0] != tt.want {
|
||||
t.Fatalf("changes = %v, want %q", changes, tt.want)
|
||||
}
|
||||
}
|
||||
updated := newCfg.CloneForRuntime()
|
||||
updated.Payload.Replace[0].Replacements[0]["private-source"] = "another-private-destination"
|
||||
changes := BuildConfigChangeDetails(newCfg, updated)
|
||||
if len(changes) != 1 || changes[0] != "payload.replace: updated (1 -> 1 rules)" {
|
||||
t.Fatalf("missed replacement edit: %v", changes)
|
||||
}
|
||||
if strings.Contains(strings.Join(changes, " "), "private") {
|
||||
t.Fatal("diff leaked replacement text")
|
||||
}
|
||||
if changes := BuildConfigChangeDetails(updated, updated); len(changes) != 0 {
|
||||
t.Fatalf("unchanged rules produced diff: %v", changes)
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,9 @@ type OAuthModelSetting = internalconfig.OAuthModelSetting
|
||||
type PayloadConfig = internalconfig.PayloadConfig
|
||||
type PayloadRule = internalconfig.PayloadRule
|
||||
type PayloadFilterRule = internalconfig.PayloadFilterRule
|
||||
type PayloadReplaceRule = internalconfig.PayloadReplaceRule
|
||||
type PayloadReplaceTarget = internalconfig.PayloadReplaceTarget
|
||||
type PayloadReplacement = internalconfig.PayloadReplacement
|
||||
type PayloadModelRule = internalconfig.PayloadModelRule
|
||||
|
||||
type ProxyConnectionPoolConfig = internalconfig.ProxyConnectionPoolConfig
|
||||
|
||||
Reference in New Issue
Block a user