| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802 |
- package common
- import (
- "errors"
- "fmt"
- "net/http"
- "regexp"
- "sort"
- "strconv"
- "strings"
- "github.com/QuantumNous/new-api/common"
- "github.com/QuantumNous/new-api/types"
- "github.com/samber/lo"
- "github.com/tidwall/gjson"
- "github.com/tidwall/sjson"
- )
- var negativeIndexRegexp = regexp.MustCompile(`\.(-\d+)`)
- const (
- paramOverrideContextRequestHeaders = "request_headers"
- paramOverrideContextHeaderOverride = "header_override"
- )
- var errSourceHeaderNotFound = errors.New("source header does not exist")
- type ConditionOperation struct {
- Path string `json:"path"` // JSON路径
- Mode string `json:"mode"` // full, prefix, suffix, contains, gt, gte, lt, lte
- Value interface{} `json:"value"` // 匹配的值
- Invert bool `json:"invert"` // 反选功能,true表示取反结果
- PassMissingKey bool `json:"pass_missing_key"` // 未获取到json key时的行为
- }
- type ParamOperation struct {
- Path string `json:"path"`
- Mode string `json:"mode"` // delete, set, move, copy, prepend, append, trim_prefix, trim_suffix, ensure_prefix, ensure_suffix, trim_space, to_lower, to_upper, replace, regex_replace, return_error, prune_objects, set_header, delete_header, copy_header, move_header, pass_headers, sync_fields
- Value interface{} `json:"value"`
- KeepOrigin bool `json:"keep_origin"`
- From string `json:"from,omitempty"`
- To string `json:"to,omitempty"`
- Conditions []ConditionOperation `json:"conditions,omitempty"` // 条件列表
- Logic string `json:"logic,omitempty"` // AND, OR (默认OR)
- }
- type ParamOverrideReturnError struct {
- Message string
- StatusCode int
- Code string
- Type string
- SkipRetry bool
- }
- func (e *ParamOverrideReturnError) Error() string {
- if e == nil {
- return "param override return error"
- }
- if e.Message == "" {
- return "param override return error"
- }
- return e.Message
- }
- func AsParamOverrideReturnError(err error) (*ParamOverrideReturnError, bool) {
- if err == nil {
- return nil, false
- }
- var target *ParamOverrideReturnError
- if errors.As(err, &target) {
- return target, true
- }
- return nil, false
- }
- func NewAPIErrorFromParamOverride(err *ParamOverrideReturnError) *types.NewAPIError {
- if err == nil {
- return types.NewError(
- errors.New("param override return error is nil"),
- types.ErrorCodeChannelParamOverrideInvalid,
- types.ErrOptionWithSkipRetry(),
- )
- }
- statusCode := err.StatusCode
- if statusCode < http.StatusContinue || statusCode > http.StatusNetworkAuthenticationRequired {
- statusCode = http.StatusBadRequest
- }
- errorCode := err.Code
- if strings.TrimSpace(errorCode) == "" {
- errorCode = string(types.ErrorCodeInvalidRequest)
- }
- errorType := err.Type
- if strings.TrimSpace(errorType) == "" {
- errorType = "invalid_request_error"
- }
- message := strings.TrimSpace(err.Message)
- if message == "" {
- message = "request blocked by param override"
- }
- opts := make([]types.NewAPIErrorOptions, 0, 1)
- if err.SkipRetry {
- opts = append(opts, types.ErrOptionWithSkipRetry())
- }
- return types.WithOpenAIError(types.OpenAIError{
- Message: message,
- Type: errorType,
- Code: errorCode,
- }, statusCode, opts...)
- }
- func ApplyParamOverride(jsonData []byte, paramOverride map[string]interface{}, conditionContext map[string]interface{}) ([]byte, error) {
- if len(paramOverride) == 0 {
- return jsonData, nil
- }
- // 尝试断言为操作格式
- if operations, ok := tryParseOperations(paramOverride); ok {
- legacyOverride := buildLegacyParamOverride(paramOverride)
- workingJSON := jsonData
- var err error
- if len(legacyOverride) > 0 {
- workingJSON, err = applyOperationsLegacy(workingJSON, legacyOverride)
- if err != nil {
- return nil, err
- }
- }
- // 使用新方法
- result, err := applyOperations(string(workingJSON), operations, conditionContext)
- return []byte(result), err
- }
- // 直接使用旧方法
- return applyOperationsLegacy(jsonData, paramOverride)
- }
- func buildLegacyParamOverride(paramOverride map[string]interface{}) map[string]interface{} {
- if len(paramOverride) == 0 {
- return nil
- }
- legacy := make(map[string]interface{}, len(paramOverride))
- for key, value := range paramOverride {
- if strings.EqualFold(strings.TrimSpace(key), "operations") {
- continue
- }
- legacy[key] = value
- }
- return legacy
- }
- func ApplyParamOverrideWithRelayInfo(jsonData []byte, info *RelayInfo) ([]byte, error) {
- paramOverride := getParamOverrideMap(info)
- if len(paramOverride) == 0 {
- return jsonData, nil
- }
- overrideCtx := BuildParamOverrideContext(info)
- result, err := ApplyParamOverride(jsonData, paramOverride, overrideCtx)
- if err != nil {
- return nil, err
- }
- syncRuntimeHeaderOverrideFromContext(info, overrideCtx)
- return result, nil
- }
- func getParamOverrideMap(info *RelayInfo) map[string]interface{} {
- if info == nil || info.ChannelMeta == nil {
- return nil
- }
- return info.ChannelMeta.ParamOverride
- }
- func getHeaderOverrideMap(info *RelayInfo) map[string]interface{} {
- if info == nil || info.ChannelMeta == nil {
- return nil
- }
- return info.ChannelMeta.HeadersOverride
- }
- func sanitizeHeaderOverrideMap(source map[string]interface{}) map[string]interface{} {
- if len(source) == 0 {
- return map[string]interface{}{}
- }
- target := make(map[string]interface{}, len(source))
- for key, value := range source {
- normalizedKey := normalizeHeaderContextKey(key)
- if normalizedKey == "" {
- continue
- }
- normalizedValue := strings.TrimSpace(fmt.Sprintf("%v", value))
- if normalizedValue == "" {
- if isHeaderPassthroughRuleKeyForOverride(normalizedKey) {
- target[normalizedKey] = ""
- }
- continue
- }
- target[normalizedKey] = normalizedValue
- }
- return target
- }
- func isHeaderPassthroughRuleKeyForOverride(key string) bool {
- key = strings.TrimSpace(strings.ToLower(key))
- if key == "" {
- return false
- }
- if key == "*" {
- return true
- }
- return strings.HasPrefix(key, "re:") || strings.HasPrefix(key, "regex:")
- }
- func GetEffectiveHeaderOverride(info *RelayInfo) map[string]interface{} {
- if info == nil {
- return map[string]interface{}{}
- }
- if info.UseRuntimeHeadersOverride {
- return sanitizeHeaderOverrideMap(info.RuntimeHeadersOverride)
- }
- return sanitizeHeaderOverrideMap(getHeaderOverrideMap(info))
- }
- func tryParseOperations(paramOverride map[string]interface{}) ([]ParamOperation, bool) {
- // 检查是否包含 "operations" 字段
- opsValue, exists := paramOverride["operations"]
- if !exists {
- return nil, false
- }
- var opMaps []map[string]interface{}
- switch ops := opsValue.(type) {
- case []interface{}:
- opMaps = make([]map[string]interface{}, 0, len(ops))
- for _, op := range ops {
- opMap, ok := op.(map[string]interface{})
- if !ok {
- return nil, false
- }
- opMaps = append(opMaps, opMap)
- }
- case []map[string]interface{}:
- opMaps = ops
- default:
- return nil, false
- }
- operations := make([]ParamOperation, 0, len(opMaps))
- for _, opMap := range opMaps {
- operation := ParamOperation{}
- // 断言必要字段
- if path, ok := opMap["path"].(string); ok {
- operation.Path = path
- }
- if mode, ok := opMap["mode"].(string); ok {
- operation.Mode = mode
- } else {
- return nil, false // mode 是必需的
- }
- // 可选字段
- if value, exists := opMap["value"]; exists {
- operation.Value = value
- }
- if keepOrigin, ok := opMap["keep_origin"].(bool); ok {
- operation.KeepOrigin = keepOrigin
- }
- if from, ok := opMap["from"].(string); ok {
- operation.From = from
- }
- if to, ok := opMap["to"].(string); ok {
- operation.To = to
- }
- if logic, ok := opMap["logic"].(string); ok {
- operation.Logic = logic
- } else {
- operation.Logic = "OR" // 默认为OR
- }
- // 解析条件
- if conditions, exists := opMap["conditions"]; exists {
- parsedConditions, err := parseConditionOperations(conditions)
- if err != nil {
- return nil, false
- }
- operation.Conditions = append(operation.Conditions, parsedConditions...)
- }
- operations = append(operations, operation)
- }
- return operations, true
- }
- func checkConditions(jsonStr, contextJSON string, conditions []ConditionOperation, logic string) (bool, error) {
- if len(conditions) == 0 {
- return true, nil // 没有条件,直接通过
- }
- results := make([]bool, len(conditions))
- for i, condition := range conditions {
- result, err := checkSingleCondition(jsonStr, contextJSON, condition)
- if err != nil {
- return false, err
- }
- results[i] = result
- }
- if strings.ToUpper(logic) == "AND" {
- return lo.EveryBy(results, func(item bool) bool { return item }), nil
- }
- return lo.SomeBy(results, func(item bool) bool { return item }), nil
- }
- func checkSingleCondition(jsonStr, contextJSON string, condition ConditionOperation) (bool, error) {
- // 处理负数索引
- path := processNegativeIndex(jsonStr, condition.Path)
- value := gjson.Get(jsonStr, path)
- if !value.Exists() && contextJSON != "" {
- value = gjson.Get(contextJSON, condition.Path)
- }
- if !value.Exists() {
- if condition.PassMissingKey {
- return true, nil
- }
- return false, nil
- }
- // 利用gjson的类型解析
- targetBytes, err := common.Marshal(condition.Value)
- if err != nil {
- return false, fmt.Errorf("failed to marshal condition value: %v", err)
- }
- targetValue := gjson.ParseBytes(targetBytes)
- result, err := compareGjsonValues(value, targetValue, strings.ToLower(condition.Mode))
- if err != nil {
- return false, fmt.Errorf("comparison failed for path %s: %v", condition.Path, err)
- }
- if condition.Invert {
- result = !result
- }
- return result, nil
- }
- func processNegativeIndex(jsonStr string, path string) string {
- matches := negativeIndexRegexp.FindAllStringSubmatch(path, -1)
- if len(matches) == 0 {
- return path
- }
- result := path
- for _, match := range matches {
- negIndex := match[1]
- index, _ := strconv.Atoi(negIndex)
- arrayPath := strings.Split(path, negIndex)[0]
- if strings.HasSuffix(arrayPath, ".") {
- arrayPath = arrayPath[:len(arrayPath)-1]
- }
- array := gjson.Get(jsonStr, arrayPath)
- if array.IsArray() {
- length := len(array.Array())
- actualIndex := length + index
- if actualIndex >= 0 && actualIndex < length {
- result = strings.Replace(result, match[0], "."+strconv.Itoa(actualIndex), 1)
- }
- }
- }
- return result
- }
- // compareGjsonValues 直接比较两个gjson.Result,支持所有比较模式
- func compareGjsonValues(jsonValue, targetValue gjson.Result, mode string) (bool, error) {
- switch mode {
- case "full":
- return compareEqual(jsonValue, targetValue)
- case "prefix":
- return strings.HasPrefix(jsonValue.String(), targetValue.String()), nil
- case "suffix":
- return strings.HasSuffix(jsonValue.String(), targetValue.String()), nil
- case "contains":
- return strings.Contains(jsonValue.String(), targetValue.String()), nil
- case "gt":
- return compareNumeric(jsonValue, targetValue, "gt")
- case "gte":
- return compareNumeric(jsonValue, targetValue, "gte")
- case "lt":
- return compareNumeric(jsonValue, targetValue, "lt")
- case "lte":
- return compareNumeric(jsonValue, targetValue, "lte")
- default:
- return false, fmt.Errorf("unsupported comparison mode: %s", mode)
- }
- }
- func compareEqual(jsonValue, targetValue gjson.Result) (bool, error) {
- // 对null值特殊处理:两个都是null返回true,一个是null另一个不是返回false
- if jsonValue.Type == gjson.Null || targetValue.Type == gjson.Null {
- return jsonValue.Type == gjson.Null && targetValue.Type == gjson.Null, nil
- }
- // 对布尔值特殊处理
- if (jsonValue.Type == gjson.True || jsonValue.Type == gjson.False) &&
- (targetValue.Type == gjson.True || targetValue.Type == gjson.False) {
- return jsonValue.Bool() == targetValue.Bool(), nil
- }
- // 如果类型不同,报错
- if jsonValue.Type != targetValue.Type {
- return false, fmt.Errorf("compare for different types, got %v and %v", jsonValue.Type, targetValue.Type)
- }
- switch jsonValue.Type {
- case gjson.True, gjson.False:
- return jsonValue.Bool() == targetValue.Bool(), nil
- case gjson.Number:
- return jsonValue.Num == targetValue.Num, nil
- case gjson.String:
- return jsonValue.String() == targetValue.String(), nil
- default:
- return jsonValue.String() == targetValue.String(), nil
- }
- }
- func compareNumeric(jsonValue, targetValue gjson.Result, operator string) (bool, error) {
- // 只有数字类型才支持数值比较
- if jsonValue.Type != gjson.Number || targetValue.Type != gjson.Number {
- return false, fmt.Errorf("numeric comparison requires both values to be numbers, got %v and %v", jsonValue.Type, targetValue.Type)
- }
- jsonNum := jsonValue.Num
- targetNum := targetValue.Num
- switch operator {
- case "gt":
- return jsonNum > targetNum, nil
- case "gte":
- return jsonNum >= targetNum, nil
- case "lt":
- return jsonNum < targetNum, nil
- case "lte":
- return jsonNum <= targetNum, nil
- default:
- return false, fmt.Errorf("unsupported numeric operator: %s", operator)
- }
- }
- // applyOperationsLegacy 原参数覆盖方法
- func applyOperationsLegacy(jsonData []byte, paramOverride map[string]interface{}) ([]byte, error) {
- reqMap := make(map[string]interface{})
- err := common.Unmarshal(jsonData, &reqMap)
- if err != nil {
- return nil, err
- }
- for key, value := range paramOverride {
- reqMap[key] = value
- }
- return common.Marshal(reqMap)
- }
- func applyOperations(jsonStr string, operations []ParamOperation, conditionContext map[string]interface{}) (string, error) {
- context := ensureContextMap(conditionContext)
- contextJSON, err := marshalContextJSON(context)
- if err != nil {
- return "", fmt.Errorf("failed to marshal condition context: %v", err)
- }
- result := jsonStr
- for _, op := range operations {
- // 检查条件是否满足
- ok, err := checkConditions(result, contextJSON, op.Conditions, op.Logic)
- if err != nil {
- return "", err
- }
- if !ok {
- continue // 条件不满足,跳过当前操作
- }
- // 处理路径中的负数索引
- opPath := processNegativeIndex(result, op.Path)
- var opPaths []string
- if isPathBasedOperation(op.Mode) {
- opPaths, err = resolveOperationPaths(result, opPath)
- if err != nil {
- return "", err
- }
- if len(opPaths) == 0 {
- continue
- }
- }
- switch op.Mode {
- case "delete":
- for _, path := range opPaths {
- result, err = deleteValue(result, path)
- if err != nil {
- break
- }
- }
- case "set":
- for _, path := range opPaths {
- if op.KeepOrigin && gjson.Get(result, path).Exists() {
- continue
- }
- result, err = sjson.Set(result, path, op.Value)
- if err != nil {
- break
- }
- }
- case "move":
- opFrom := processNegativeIndex(result, op.From)
- opTo := processNegativeIndex(result, op.To)
- result, err = moveValue(result, opFrom, opTo)
- case "copy":
- if op.From == "" || op.To == "" {
- return "", fmt.Errorf("copy from/to is required")
- }
- opFrom := processNegativeIndex(result, op.From)
- opTo := processNegativeIndex(result, op.To)
- result, err = copyValue(result, opFrom, opTo)
- case "prepend":
- for _, path := range opPaths {
- result, err = modifyValue(result, path, op.Value, op.KeepOrigin, true)
- if err != nil {
- break
- }
- }
- case "append":
- for _, path := range opPaths {
- result, err = modifyValue(result, path, op.Value, op.KeepOrigin, false)
- if err != nil {
- break
- }
- }
- case "trim_prefix":
- for _, path := range opPaths {
- result, err = trimStringValue(result, path, op.Value, true)
- if err != nil {
- break
- }
- }
- case "trim_suffix":
- for _, path := range opPaths {
- result, err = trimStringValue(result, path, op.Value, false)
- if err != nil {
- break
- }
- }
- case "ensure_prefix":
- for _, path := range opPaths {
- result, err = ensureStringAffix(result, path, op.Value, true)
- if err != nil {
- break
- }
- }
- case "ensure_suffix":
- for _, path := range opPaths {
- result, err = ensureStringAffix(result, path, op.Value, false)
- if err != nil {
- break
- }
- }
- case "trim_space":
- for _, path := range opPaths {
- result, err = transformStringValue(result, path, strings.TrimSpace)
- if err != nil {
- break
- }
- }
- case "to_lower":
- for _, path := range opPaths {
- result, err = transformStringValue(result, path, strings.ToLower)
- if err != nil {
- break
- }
- }
- case "to_upper":
- for _, path := range opPaths {
- result, err = transformStringValue(result, path, strings.ToUpper)
- if err != nil {
- break
- }
- }
- case "replace":
- for _, path := range opPaths {
- result, err = replaceStringValue(result, path, op.From, op.To)
- if err != nil {
- break
- }
- }
- case "regex_replace":
- for _, path := range opPaths {
- result, err = regexReplaceStringValue(result, path, op.From, op.To)
- if err != nil {
- break
- }
- }
- case "return_error":
- returnErr, parseErr := parseParamOverrideReturnError(op.Value)
- if parseErr != nil {
- return "", parseErr
- }
- return "", returnErr
- case "prune_objects":
- for _, path := range opPaths {
- result, err = pruneObjects(result, path, contextJSON, op.Value)
- if err != nil {
- break
- }
- }
- case "set_header":
- err = setHeaderOverrideInContext(context, op.Path, op.Value, op.KeepOrigin)
- if err == nil {
- contextJSON, err = marshalContextJSON(context)
- }
- case "delete_header":
- err = deleteHeaderOverrideInContext(context, op.Path)
- if err == nil {
- contextJSON, err = marshalContextJSON(context)
- }
- case "copy_header":
- sourceHeader := strings.TrimSpace(op.From)
- targetHeader := strings.TrimSpace(op.To)
- if sourceHeader == "" {
- sourceHeader = strings.TrimSpace(op.Path)
- }
- if targetHeader == "" {
- targetHeader = strings.TrimSpace(op.Path)
- }
- err = copyHeaderInContext(context, sourceHeader, targetHeader, op.KeepOrigin)
- if errors.Is(err, errSourceHeaderNotFound) {
- err = nil
- }
- if err == nil {
- contextJSON, err = marshalContextJSON(context)
- }
- case "move_header":
- sourceHeader := strings.TrimSpace(op.From)
- targetHeader := strings.TrimSpace(op.To)
- if sourceHeader == "" {
- sourceHeader = strings.TrimSpace(op.Path)
- }
- if targetHeader == "" {
- targetHeader = strings.TrimSpace(op.Path)
- }
- err = moveHeaderInContext(context, sourceHeader, targetHeader, op.KeepOrigin)
- if errors.Is(err, errSourceHeaderNotFound) {
- err = nil
- }
- if err == nil {
- contextJSON, err = marshalContextJSON(context)
- }
- case "pass_headers":
- headerNames, parseErr := parseHeaderPassThroughNames(op.Value)
- if parseErr != nil {
- return "", parseErr
- }
- for _, headerName := range headerNames {
- if err = copyHeaderInContext(context, headerName, headerName, op.KeepOrigin); err != nil {
- if errors.Is(err, errSourceHeaderNotFound) {
- err = nil
- continue
- }
- break
- }
- }
- if err == nil {
- contextJSON, err = marshalContextJSON(context)
- }
- case "sync_fields":
- result, err = syncFieldsBetweenTargets(result, context, op.From, op.To)
- if err == nil {
- contextJSON, err = marshalContextJSON(context)
- }
- default:
- return "", fmt.Errorf("unknown operation: %s", op.Mode)
- }
- if err != nil {
- return "", fmt.Errorf("operation %s failed: %w", op.Mode, err)
- }
- }
- return result, nil
- }
- func parseParamOverrideReturnError(value interface{}) (*ParamOverrideReturnError, error) {
- result := &ParamOverrideReturnError{
- StatusCode: http.StatusBadRequest,
- Code: string(types.ErrorCodeInvalidRequest),
- Type: "invalid_request_error",
- SkipRetry: true,
- }
- switch raw := value.(type) {
- case nil:
- return nil, fmt.Errorf("return_error value is required")
- case string:
- result.Message = strings.TrimSpace(raw)
- case map[string]interface{}:
- if message, ok := raw["message"].(string); ok {
- result.Message = strings.TrimSpace(message)
- }
- if result.Message == "" {
- if message, ok := raw["msg"].(string); ok {
- result.Message = strings.TrimSpace(message)
- }
- }
- if code, exists := raw["code"]; exists {
- codeStr := strings.TrimSpace(fmt.Sprintf("%v", code))
- if codeStr != "" {
- result.Code = codeStr
- }
- }
- if errType, ok := raw["type"].(string); ok {
- errType = strings.TrimSpace(errType)
- if errType != "" {
- result.Type = errType
- }
- }
- if skipRetry, ok := raw["skip_retry"].(bool); ok {
- result.SkipRetry = skipRetry
- }
- if statusCodeRaw, exists := raw["status_code"]; exists {
- statusCode, ok := parseOverrideInt(statusCodeRaw)
- if !ok {
- return nil, fmt.Errorf("return_error status_code must be an integer")
- }
- result.StatusCode = statusCode
- } else if statusRaw, exists := raw["status"]; exists {
- statusCode, ok := parseOverrideInt(statusRaw)
- if !ok {
- return nil, fmt.Errorf("return_error status must be an integer")
- }
- result.StatusCode = statusCode
- }
- default:
- return nil, fmt.Errorf("return_error value must be string or object")
- }
- if result.Message == "" {
- return nil, fmt.Errorf("return_error message is required")
- }
- if result.StatusCode < http.StatusContinue || result.StatusCode > http.StatusNetworkAuthenticationRequired {
- return nil, fmt.Errorf("return_error status code out of range: %d", result.StatusCode)
- }
- return result, nil
- }
- func parseOverrideInt(v interface{}) (int, bool) {
- switch value := v.(type) {
- case int:
- return value, true
- case float64:
- if value != float64(int(value)) {
- return 0, false
- }
- return int(value), true
- default:
- return 0, false
- }
- }
- func ensureContextMap(conditionContext map[string]interface{}) map[string]interface{} {
- if conditionContext != nil {
- return conditionContext
- }
- return make(map[string]interface{})
- }
- func marshalContextJSON(context map[string]interface{}) (string, error) {
- if context == nil || len(context) == 0 {
- return "", nil
- }
- ctxBytes, err := common.Marshal(context)
- if err != nil {
- return "", err
- }
- return string(ctxBytes), nil
- }
- func setHeaderOverrideInContext(context map[string]interface{}, headerName string, value interface{}, keepOrigin bool) error {
- headerName = normalizeHeaderContextKey(headerName)
- if headerName == "" {
- return fmt.Errorf("header name is required")
- }
- rawHeaders := ensureMapKeyInContext(context, paramOverrideContextHeaderOverride)
- if keepOrigin {
- if existing, ok := rawHeaders[headerName]; ok {
- existingValue := strings.TrimSpace(fmt.Sprintf("%v", existing))
- if existingValue != "" {
- return nil
- }
- }
- }
- headerValue, hasValue, err := resolveHeaderOverrideValue(context, headerName, value)
- if err != nil {
- return err
- }
- if !hasValue {
- delete(rawHeaders, headerName)
- return nil
- }
- rawHeaders[headerName] = headerValue
- return nil
- }
- func resolveHeaderOverrideValue(context map[string]interface{}, headerName string, value interface{}) (string, bool, error) {
- if value == nil {
- return "", false, fmt.Errorf("header value is required")
- }
- if mapping, ok := value.(map[string]interface{}); ok {
- return resolveHeaderOverrideValueByMapping(context, headerName, mapping)
- }
- if mapping, ok := value.(map[string]string); ok {
- converted := make(map[string]interface{}, len(mapping))
- for key, item := range mapping {
- converted[key] = item
- }
- return resolveHeaderOverrideValueByMapping(context, headerName, converted)
- }
- headerValue := strings.TrimSpace(fmt.Sprintf("%v", value))
- if headerValue == "" {
- return "", false, nil
- }
- return headerValue, true, nil
- }
- func resolveHeaderOverrideValueByMapping(context map[string]interface{}, headerName string, mapping map[string]interface{}) (string, bool, error) {
- if len(mapping) == 0 {
- return "", false, fmt.Errorf("header value mapping cannot be empty")
- }
- sourceValue, exists := getHeaderValueFromContext(context, headerName)
- if !exists {
- return "", false, nil
- }
- sourceTokens := splitHeaderListValue(sourceValue)
- if len(sourceTokens) == 0 {
- return "", false, nil
- }
- wildcardValue, hasWildcard := mapping["*"]
- resultTokens := make([]string, 0, len(sourceTokens))
- for _, token := range sourceTokens {
- replacementRaw, hasReplacement := mapping[token]
- if !hasReplacement && hasWildcard {
- replacementRaw = wildcardValue
- hasReplacement = true
- }
- if !hasReplacement {
- resultTokens = append(resultTokens, token)
- continue
- }
- replacementTokens, err := parseHeaderReplacementTokens(replacementRaw)
- if err != nil {
- return "", false, err
- }
- resultTokens = append(resultTokens, replacementTokens...)
- }
- resultTokens = lo.Uniq(resultTokens)
- if len(resultTokens) == 0 {
- return "", false, nil
- }
- return strings.Join(resultTokens, ","), true, nil
- }
- func parseHeaderReplacementTokens(value interface{}) ([]string, error) {
- switch raw := value.(type) {
- case nil:
- return nil, nil
- case string:
- return splitHeaderListValue(raw), nil
- case []string:
- tokens := make([]string, 0, len(raw))
- for _, item := range raw {
- tokens = append(tokens, splitHeaderListValue(item)...)
- }
- return lo.Uniq(tokens), nil
- case []interface{}:
- tokens := make([]string, 0, len(raw))
- for _, item := range raw {
- itemTokens, err := parseHeaderReplacementTokens(item)
- if err != nil {
- return nil, err
- }
- tokens = append(tokens, itemTokens...)
- }
- return lo.Uniq(tokens), nil
- case map[string]interface{}, map[string]string:
- return nil, fmt.Errorf("header replacement value must be string, array or null")
- default:
- token := strings.TrimSpace(fmt.Sprintf("%v", raw))
- if token == "" {
- return nil, nil
- }
- return []string{token}, nil
- }
- }
- func splitHeaderListValue(raw string) []string {
- items := strings.Split(raw, ",")
- return lo.FilterMap(items, func(item string, _ int) (string, bool) {
- token := strings.TrimSpace(item)
- if token == "" {
- return "", false
- }
- return token, true
- })
- }
- func copyHeaderInContext(context map[string]interface{}, fromHeader, toHeader string, keepOrigin bool) error {
- fromHeader = normalizeHeaderContextKey(fromHeader)
- toHeader = normalizeHeaderContextKey(toHeader)
- if fromHeader == "" || toHeader == "" {
- return fmt.Errorf("copy_header from/to is required")
- }
- value, exists := getHeaderValueFromContext(context, fromHeader)
- if !exists {
- return fmt.Errorf("%w: %s", errSourceHeaderNotFound, fromHeader)
- }
- return setHeaderOverrideInContext(context, toHeader, value, keepOrigin)
- }
- func moveHeaderInContext(context map[string]interface{}, fromHeader, toHeader string, keepOrigin bool) error {
- fromHeader = normalizeHeaderContextKey(fromHeader)
- toHeader = normalizeHeaderContextKey(toHeader)
- if fromHeader == "" || toHeader == "" {
- return fmt.Errorf("move_header from/to is required")
- }
- if err := copyHeaderInContext(context, fromHeader, toHeader, keepOrigin); err != nil {
- return err
- }
- if strings.EqualFold(fromHeader, toHeader) {
- return nil
- }
- return deleteHeaderOverrideInContext(context, fromHeader)
- }
- func deleteHeaderOverrideInContext(context map[string]interface{}, headerName string) error {
- headerName = normalizeHeaderContextKey(headerName)
- if headerName == "" {
- return fmt.Errorf("header name is required")
- }
- rawHeaders := ensureMapKeyInContext(context, paramOverrideContextHeaderOverride)
- delete(rawHeaders, headerName)
- return nil
- }
- func parseHeaderPassThroughNames(value interface{}) ([]string, error) {
- normalizeNames := func(values []string) []string {
- names := lo.FilterMap(values, func(item string, _ int) (string, bool) {
- headerName := normalizeHeaderContextKey(item)
- if headerName == "" {
- return "", false
- }
- return headerName, true
- })
- return lo.Uniq(names)
- }
- switch raw := value.(type) {
- case nil:
- return nil, fmt.Errorf("pass_headers value is required")
- case string:
- trimmed := strings.TrimSpace(raw)
- if trimmed == "" {
- return nil, fmt.Errorf("pass_headers value is required")
- }
- if strings.HasPrefix(trimmed, "[") || strings.HasPrefix(trimmed, "{") {
- var parsed interface{}
- if err := common.UnmarshalJsonStr(trimmed, &parsed); err == nil {
- return parseHeaderPassThroughNames(parsed)
- }
- }
- names := normalizeNames(strings.Split(trimmed, ","))
- if len(names) == 0 {
- return nil, fmt.Errorf("pass_headers value is invalid")
- }
- return names, nil
- case []interface{}:
- names := lo.FilterMap(raw, func(item interface{}, _ int) (string, bool) {
- headerName := normalizeHeaderContextKey(fmt.Sprintf("%v", item))
- if headerName == "" {
- return "", false
- }
- return headerName, true
- })
- names = lo.Uniq(names)
- if len(names) == 0 {
- return nil, fmt.Errorf("pass_headers value is invalid")
- }
- return names, nil
- case []string:
- names := lo.FilterMap(raw, func(item string, _ int) (string, bool) {
- headerName := normalizeHeaderContextKey(item)
- if headerName == "" {
- return "", false
- }
- return headerName, true
- })
- names = lo.Uniq(names)
- if len(names) == 0 {
- return nil, fmt.Errorf("pass_headers value is invalid")
- }
- return names, nil
- case map[string]interface{}:
- candidates := make([]string, 0, 8)
- if headersRaw, ok := raw["headers"]; ok {
- names, err := parseHeaderPassThroughNames(headersRaw)
- if err == nil {
- candidates = append(candidates, names...)
- }
- }
- if namesRaw, ok := raw["names"]; ok {
- names, err := parseHeaderPassThroughNames(namesRaw)
- if err == nil {
- candidates = append(candidates, names...)
- }
- }
- if headerRaw, ok := raw["header"]; ok {
- names, err := parseHeaderPassThroughNames(headerRaw)
- if err == nil {
- candidates = append(candidates, names...)
- }
- }
- names := normalizeNames(candidates)
- if len(names) == 0 {
- return nil, fmt.Errorf("pass_headers value is invalid")
- }
- return names, nil
- default:
- return nil, fmt.Errorf("pass_headers value must be string, array or object")
- }
- }
- type syncTarget struct {
- kind string
- key string
- }
- func parseSyncTarget(spec string) (syncTarget, error) {
- raw := strings.TrimSpace(spec)
- if raw == "" {
- return syncTarget{}, fmt.Errorf("sync_fields target is required")
- }
- idx := strings.Index(raw, ":")
- if idx < 0 {
- // Backward compatibility: treat bare value as JSON path.
- return syncTarget{
- kind: "json",
- key: raw,
- }, nil
- }
- kind := strings.ToLower(strings.TrimSpace(raw[:idx]))
- key := strings.TrimSpace(raw[idx+1:])
- if key == "" {
- return syncTarget{}, fmt.Errorf("sync_fields target key is required: %s", raw)
- }
- switch kind {
- case "json", "body":
- return syncTarget{
- kind: "json",
- key: key,
- }, nil
- case "header":
- return syncTarget{
- kind: "header",
- key: key,
- }, nil
- default:
- return syncTarget{}, fmt.Errorf("sync_fields target prefix is invalid: %s", raw)
- }
- }
- func readSyncTargetValue(jsonStr string, context map[string]interface{}, target syncTarget) (interface{}, bool, error) {
- switch target.kind {
- case "json":
- path := processNegativeIndex(jsonStr, target.key)
- value := gjson.Get(jsonStr, path)
- if !value.Exists() || value.Type == gjson.Null {
- return nil, false, nil
- }
- if value.Type == gjson.String && strings.TrimSpace(value.String()) == "" {
- return nil, false, nil
- }
- return value.Value(), true, nil
- case "header":
- value, ok := getHeaderValueFromContext(context, target.key)
- if !ok || strings.TrimSpace(value) == "" {
- return nil, false, nil
- }
- return value, true, nil
- default:
- return nil, false, fmt.Errorf("unsupported sync_fields target kind: %s", target.kind)
- }
- }
- func writeSyncTargetValue(jsonStr string, context map[string]interface{}, target syncTarget, value interface{}) (string, error) {
- switch target.kind {
- case "json":
- path := processNegativeIndex(jsonStr, target.key)
- nextJSON, err := sjson.Set(jsonStr, path, value)
- if err != nil {
- return "", err
- }
- return nextJSON, nil
- case "header":
- if err := setHeaderOverrideInContext(context, target.key, value, false); err != nil {
- return "", err
- }
- return jsonStr, nil
- default:
- return "", fmt.Errorf("unsupported sync_fields target kind: %s", target.kind)
- }
- }
- func syncFieldsBetweenTargets(jsonStr string, context map[string]interface{}, fromSpec string, toSpec string) (string, error) {
- fromTarget, err := parseSyncTarget(fromSpec)
- if err != nil {
- return "", err
- }
- toTarget, err := parseSyncTarget(toSpec)
- if err != nil {
- return "", err
- }
- fromValue, fromExists, err := readSyncTargetValue(jsonStr, context, fromTarget)
- if err != nil {
- return "", err
- }
- toValue, toExists, err := readSyncTargetValue(jsonStr, context, toTarget)
- if err != nil {
- return "", err
- }
- // If one side exists and the other side is missing, sync the missing side.
- if fromExists && !toExists {
- return writeSyncTargetValue(jsonStr, context, toTarget, fromValue)
- }
- if toExists && !fromExists {
- return writeSyncTargetValue(jsonStr, context, fromTarget, toValue)
- }
- return jsonStr, nil
- }
- func ensureMapKeyInContext(context map[string]interface{}, key string) map[string]interface{} {
- if context == nil {
- return map[string]interface{}{}
- }
- if existing, ok := context[key]; ok {
- if mapVal, ok := existing.(map[string]interface{}); ok {
- return mapVal
- }
- }
- result := make(map[string]interface{})
- context[key] = result
- return result
- }
- func getHeaderValueFromContext(context map[string]interface{}, headerName string) (string, bool) {
- headerName = normalizeHeaderContextKey(headerName)
- if headerName == "" {
- return "", false
- }
- for _, key := range []string{paramOverrideContextHeaderOverride, paramOverrideContextRequestHeaders} {
- source := ensureMapKeyInContext(context, key)
- raw, ok := source[headerName]
- if !ok {
- continue
- }
- value := strings.TrimSpace(fmt.Sprintf("%v", raw))
- if value != "" {
- return value, true
- }
- }
- return "", false
- }
- func normalizeHeaderContextKey(key string) string {
- return strings.TrimSpace(strings.ToLower(key))
- }
- func buildRequestHeadersContext(headers map[string]string) map[string]interface{} {
- if len(headers) == 0 {
- return map[string]interface{}{}
- }
- entries := lo.Entries(headers)
- normalizedEntries := lo.FilterMap(entries, func(item lo.Entry[string, string], _ int) (lo.Entry[string, string], bool) {
- normalized := normalizeHeaderContextKey(item.Key)
- value := strings.TrimSpace(item.Value)
- if normalized == "" || value == "" {
- return lo.Entry[string, string]{}, false
- }
- return lo.Entry[string, string]{Key: normalized, Value: value}, true
- })
- return lo.SliceToMap(normalizedEntries, func(item lo.Entry[string, string]) (string, interface{}) {
- return item.Key, item.Value
- })
- }
- func syncRuntimeHeaderOverrideFromContext(info *RelayInfo, context map[string]interface{}) {
- if info == nil || context == nil {
- return
- }
- raw, exists := context[paramOverrideContextHeaderOverride]
- if !exists {
- return
- }
- rawMap, ok := raw.(map[string]interface{})
- if !ok {
- return
- }
- info.RuntimeHeadersOverride = sanitizeHeaderOverrideMap(rawMap)
- info.UseRuntimeHeadersOverride = true
- }
- func moveValue(jsonStr, fromPath, toPath string) (string, error) {
- sourceValue := gjson.Get(jsonStr, fromPath)
- if !sourceValue.Exists() {
- return jsonStr, fmt.Errorf("source path does not exist: %s", fromPath)
- }
- result, err := sjson.Set(jsonStr, toPath, sourceValue.Value())
- if err != nil {
- return "", err
- }
- return sjson.Delete(result, fromPath)
- }
- func copyValue(jsonStr, fromPath, toPath string) (string, error) {
- sourceValue := gjson.Get(jsonStr, fromPath)
- if !sourceValue.Exists() {
- return jsonStr, fmt.Errorf("source path does not exist: %s", fromPath)
- }
- return sjson.Set(jsonStr, toPath, sourceValue.Value())
- }
- func isPathBasedOperation(mode string) bool {
- switch mode {
- case "delete", "set", "prepend", "append", "trim_prefix", "trim_suffix", "ensure_prefix", "ensure_suffix", "trim_space", "to_lower", "to_upper", "replace", "regex_replace", "prune_objects":
- return true
- default:
- return false
- }
- }
- func resolveOperationPaths(jsonStr, path string) ([]string, error) {
- if !strings.Contains(path, "*") {
- return []string{path}, nil
- }
- return expandWildcardPaths(jsonStr, path)
- }
- func expandWildcardPaths(jsonStr, path string) ([]string, error) {
- var root interface{}
- if err := common.Unmarshal([]byte(jsonStr), &root); err != nil {
- return nil, err
- }
- segments := strings.Split(path, ".")
- paths := collectWildcardPaths(root, segments, nil)
- return lo.Uniq(paths), nil
- }
- func collectWildcardPaths(node interface{}, segments []string, prefix []string) []string {
- if len(segments) == 0 {
- return []string{strings.Join(prefix, ".")}
- }
- segment := strings.TrimSpace(segments[0])
- if segment == "" {
- return nil
- }
- isLast := len(segments) == 1
- if segment == "*" {
- switch typed := node.(type) {
- case map[string]interface{}:
- keys := lo.Keys(typed)
- sort.Strings(keys)
- return lo.FlatMap(keys, func(key string, _ int) []string {
- return collectWildcardPaths(typed[key], segments[1:], append(prefix, key))
- })
- case []interface{}:
- return lo.FlatMap(lo.Range(len(typed)), func(index int, _ int) []string {
- return collectWildcardPaths(typed[index], segments[1:], append(prefix, strconv.Itoa(index)))
- })
- default:
- return nil
- }
- }
- switch typed := node.(type) {
- case map[string]interface{}:
- if isLast {
- return []string{strings.Join(append(prefix, segment), ".")}
- }
- next, exists := typed[segment]
- if !exists {
- return nil
- }
- return collectWildcardPaths(next, segments[1:], append(prefix, segment))
- case []interface{}:
- index, err := strconv.Atoi(segment)
- if err != nil || index < 0 || index >= len(typed) {
- return nil
- }
- if isLast {
- return []string{strings.Join(append(prefix, segment), ".")}
- }
- return collectWildcardPaths(typed[index], segments[1:], append(prefix, segment))
- default:
- return nil
- }
- }
- func deleteValue(jsonStr, path string) (string, error) {
- if strings.TrimSpace(path) == "" {
- return jsonStr, nil
- }
- return sjson.Delete(jsonStr, path)
- }
- func modifyValue(jsonStr, path string, value interface{}, keepOrigin, isPrepend bool) (string, error) {
- current := gjson.Get(jsonStr, path)
- switch {
- case current.IsArray():
- return modifyArray(jsonStr, path, value, isPrepend)
- case current.Type == gjson.String:
- return modifyString(jsonStr, path, value, isPrepend)
- case current.Type == gjson.JSON:
- return mergeObjects(jsonStr, path, value, keepOrigin)
- }
- return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
- }
- func modifyArray(jsonStr, path string, value interface{}, isPrepend bool) (string, error) {
- current := gjson.Get(jsonStr, path)
- var newArray []interface{}
- // 添加新值
- addValue := func() {
- if arr, ok := value.([]interface{}); ok {
- newArray = append(newArray, arr...)
- } else {
- newArray = append(newArray, value)
- }
- }
- // 添加原值
- addOriginal := func() {
- current.ForEach(func(_, val gjson.Result) bool {
- newArray = append(newArray, val.Value())
- return true
- })
- }
- if isPrepend {
- addValue()
- addOriginal()
- } else {
- addOriginal()
- addValue()
- }
- return sjson.Set(jsonStr, path, newArray)
- }
- func modifyString(jsonStr, path string, value interface{}, isPrepend bool) (string, error) {
- current := gjson.Get(jsonStr, path)
- valueStr := fmt.Sprintf("%v", value)
- var newStr string
- if isPrepend {
- newStr = valueStr + current.String()
- } else {
- newStr = current.String() + valueStr
- }
- return sjson.Set(jsonStr, path, newStr)
- }
- func trimStringValue(jsonStr, path string, value interface{}, isPrefix bool) (string, error) {
- current := gjson.Get(jsonStr, path)
- if current.Type != gjson.String {
- return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
- }
- if value == nil {
- return jsonStr, fmt.Errorf("trim value is required")
- }
- valueStr := fmt.Sprintf("%v", value)
- var newStr string
- if isPrefix {
- newStr = strings.TrimPrefix(current.String(), valueStr)
- } else {
- newStr = strings.TrimSuffix(current.String(), valueStr)
- }
- return sjson.Set(jsonStr, path, newStr)
- }
- func ensureStringAffix(jsonStr, path string, value interface{}, isPrefix bool) (string, error) {
- current := gjson.Get(jsonStr, path)
- if current.Type != gjson.String {
- return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
- }
- if value == nil {
- return jsonStr, fmt.Errorf("ensure value is required")
- }
- valueStr := fmt.Sprintf("%v", value)
- if valueStr == "" {
- return jsonStr, fmt.Errorf("ensure value is required")
- }
- currentStr := current.String()
- if isPrefix {
- if strings.HasPrefix(currentStr, valueStr) {
- return jsonStr, nil
- }
- return sjson.Set(jsonStr, path, valueStr+currentStr)
- }
- if strings.HasSuffix(currentStr, valueStr) {
- return jsonStr, nil
- }
- return sjson.Set(jsonStr, path, currentStr+valueStr)
- }
- func transformStringValue(jsonStr, path string, transform func(string) string) (string, error) {
- current := gjson.Get(jsonStr, path)
- if current.Type != gjson.String {
- return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
- }
- return sjson.Set(jsonStr, path, transform(current.String()))
- }
- func replaceStringValue(jsonStr, path, from, to string) (string, error) {
- current := gjson.Get(jsonStr, path)
- if current.Type != gjson.String {
- return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
- }
- if from == "" {
- return jsonStr, fmt.Errorf("replace from is required")
- }
- return sjson.Set(jsonStr, path, strings.ReplaceAll(current.String(), from, to))
- }
- func regexReplaceStringValue(jsonStr, path, pattern, replacement string) (string, error) {
- current := gjson.Get(jsonStr, path)
- if current.Type != gjson.String {
- return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
- }
- if pattern == "" {
- return jsonStr, fmt.Errorf("regex pattern is required")
- }
- re, err := regexp.Compile(pattern)
- if err != nil {
- return jsonStr, err
- }
- return sjson.Set(jsonStr, path, re.ReplaceAllString(current.String(), replacement))
- }
- type pruneObjectsOptions struct {
- conditions []ConditionOperation
- logic string
- recursive bool
- }
- func pruneObjects(jsonStr, path, contextJSON string, value interface{}) (string, error) {
- options, err := parsePruneObjectsOptions(value)
- if err != nil {
- return "", err
- }
- if path == "" {
- var root interface{}
- if err := common.Unmarshal([]byte(jsonStr), &root); err != nil {
- return "", err
- }
- cleaned, _, err := pruneObjectsNode(root, options, contextJSON, true)
- if err != nil {
- return "", err
- }
- cleanedBytes, err := common.Marshal(cleaned)
- if err != nil {
- return "", err
- }
- return string(cleanedBytes), nil
- }
- target := gjson.Get(jsonStr, path)
- if !target.Exists() {
- return jsonStr, nil
- }
- var targetNode interface{}
- if target.Type == gjson.JSON {
- if err := common.Unmarshal([]byte(target.Raw), &targetNode); err != nil {
- return "", err
- }
- } else {
- targetNode = target.Value()
- }
- cleaned, _, err := pruneObjectsNode(targetNode, options, contextJSON, true)
- if err != nil {
- return "", err
- }
- cleanedBytes, err := common.Marshal(cleaned)
- if err != nil {
- return "", err
- }
- return sjson.SetRaw(jsonStr, path, string(cleanedBytes))
- }
- func parsePruneObjectsOptions(value interface{}) (pruneObjectsOptions, error) {
- opts := pruneObjectsOptions{
- logic: "AND",
- recursive: true,
- }
- switch raw := value.(type) {
- case nil:
- return opts, fmt.Errorf("prune_objects value is required")
- case string:
- v := strings.TrimSpace(raw)
- if v == "" {
- return opts, fmt.Errorf("prune_objects value is required")
- }
- opts.conditions = []ConditionOperation{
- {
- Path: "type",
- Mode: "full",
- Value: v,
- },
- }
- case map[string]interface{}:
- if logic, ok := raw["logic"].(string); ok && strings.TrimSpace(logic) != "" {
- opts.logic = logic
- }
- if recursive, ok := raw["recursive"].(bool); ok {
- opts.recursive = recursive
- }
- if condRaw, exists := raw["conditions"]; exists {
- conditions, err := parseConditionOperations(condRaw)
- if err != nil {
- return opts, err
- }
- opts.conditions = append(opts.conditions, conditions...)
- }
- if whereRaw, exists := raw["where"]; exists {
- whereMap, ok := whereRaw.(map[string]interface{})
- if !ok {
- return opts, fmt.Errorf("prune_objects where must be object")
- }
- for key, val := range whereMap {
- key = strings.TrimSpace(key)
- if key == "" {
- continue
- }
- opts.conditions = append(opts.conditions, ConditionOperation{
- Path: key,
- Mode: "full",
- Value: val,
- })
- }
- }
- if matchType, exists := raw["type"]; exists {
- opts.conditions = append(opts.conditions, ConditionOperation{
- Path: "type",
- Mode: "full",
- Value: matchType,
- })
- }
- default:
- return opts, fmt.Errorf("prune_objects value must be string or object")
- }
- if len(opts.conditions) == 0 {
- return opts, fmt.Errorf("prune_objects conditions are required")
- }
- return opts, nil
- }
- func parseConditionOperations(raw interface{}) ([]ConditionOperation, error) {
- switch typed := raw.(type) {
- case map[string]interface{}:
- entries := lo.Entries(typed)
- conditions := lo.FilterMap(entries, func(item lo.Entry[string, interface{}], _ int) (ConditionOperation, bool) {
- path := strings.TrimSpace(item.Key)
- if path == "" {
- return ConditionOperation{}, false
- }
- return ConditionOperation{
- Path: path,
- Mode: "full",
- Value: item.Value,
- }, true
- })
- if len(conditions) == 0 {
- return nil, fmt.Errorf("conditions object must contain at least one key")
- }
- return conditions, nil
- case []interface{}:
- items := typed
- result := make([]ConditionOperation, 0, len(items))
- for _, item := range items {
- itemMap, ok := item.(map[string]interface{})
- if !ok {
- return nil, fmt.Errorf("condition must be object")
- }
- path, _ := itemMap["path"].(string)
- mode, _ := itemMap["mode"].(string)
- if strings.TrimSpace(path) == "" || strings.TrimSpace(mode) == "" {
- return nil, fmt.Errorf("condition path/mode is required")
- }
- condition := ConditionOperation{
- Path: path,
- Mode: mode,
- }
- if value, exists := itemMap["value"]; exists {
- condition.Value = value
- }
- if invert, ok := itemMap["invert"].(bool); ok {
- condition.Invert = invert
- }
- if passMissingKey, ok := itemMap["pass_missing_key"].(bool); ok {
- condition.PassMissingKey = passMissingKey
- }
- result = append(result, condition)
- }
- return result, nil
- default:
- return nil, fmt.Errorf("conditions must be an array or object")
- }
- }
- func pruneObjectsNode(node interface{}, options pruneObjectsOptions, contextJSON string, isRoot bool) (interface{}, bool, error) {
- switch value := node.(type) {
- case []interface{}:
- result := make([]interface{}, 0, len(value))
- for _, item := range value {
- next, drop, err := pruneObjectsNode(item, options, contextJSON, false)
- if err != nil {
- return nil, false, err
- }
- if drop {
- continue
- }
- result = append(result, next)
- }
- return result, false, nil
- case map[string]interface{}:
- shouldDrop, err := shouldPruneObject(value, options, contextJSON)
- if err != nil {
- return nil, false, err
- }
- if shouldDrop && !isRoot {
- return nil, true, nil
- }
- if !options.recursive {
- return value, false, nil
- }
- for key, child := range value {
- next, drop, err := pruneObjectsNode(child, options, contextJSON, false)
- if err != nil {
- return nil, false, err
- }
- if drop {
- delete(value, key)
- continue
- }
- value[key] = next
- }
- return value, false, nil
- default:
- return node, false, nil
- }
- }
- func shouldPruneObject(node map[string]interface{}, options pruneObjectsOptions, contextJSON string) (bool, error) {
- nodeBytes, err := common.Marshal(node)
- if err != nil {
- return false, err
- }
- return checkConditions(string(nodeBytes), contextJSON, options.conditions, options.logic)
- }
- func mergeObjects(jsonStr, path string, value interface{}, keepOrigin bool) (string, error) {
- current := gjson.Get(jsonStr, path)
- var currentMap, newMap map[string]interface{}
- // 解析当前值
- if err := common.Unmarshal([]byte(current.Raw), ¤tMap); err != nil {
- return "", err
- }
- // 解析新值
- switch v := value.(type) {
- case map[string]interface{}:
- newMap = v
- default:
- jsonBytes, _ := common.Marshal(v)
- if err := common.Unmarshal(jsonBytes, &newMap); err != nil {
- return "", err
- }
- }
- // 合并
- result := make(map[string]interface{})
- for k, v := range currentMap {
- result[k] = v
- }
- for k, v := range newMap {
- if !keepOrigin || result[k] == nil {
- result[k] = v
- }
- }
- return sjson.Set(jsonStr, path, result)
- }
- // BuildParamOverrideContext 提供 ApplyParamOverride 可用的上下文信息。
- // 目前内置以下字段:
- // - upstream_model/model:始终为通道映射后的上游模型名。
- // - original_model:请求最初指定的模型名。
- // - request_path:请求路径
- // - is_channel_test:是否为渠道测试请求(同 is_test)。
- func BuildParamOverrideContext(info *RelayInfo) map[string]interface{} {
- if info == nil {
- return nil
- }
- ctx := make(map[string]interface{})
- if info.ChannelMeta != nil && info.ChannelMeta.UpstreamModelName != "" {
- ctx["model"] = info.ChannelMeta.UpstreamModelName
- ctx["upstream_model"] = info.ChannelMeta.UpstreamModelName
- }
- if info.OriginModelName != "" {
- ctx["original_model"] = info.OriginModelName
- if _, exists := ctx["model"]; !exists {
- ctx["model"] = info.OriginModelName
- }
- }
- if info.RequestURLPath != "" {
- requestPath := info.RequestURLPath
- if requestPath != "" {
- ctx["request_path"] = requestPath
- }
- }
- ctx[paramOverrideContextRequestHeaders] = buildRequestHeadersContext(info.RequestHeaders)
- headerOverrideSource := GetEffectiveHeaderOverride(info)
- ctx[paramOverrideContextHeaderOverride] = sanitizeHeaderOverrideMap(headerOverrideSource)
- ctx["retry_index"] = info.RetryIndex
- ctx["is_retry"] = info.RetryIndex > 0
- ctx["retry"] = map[string]interface{}{
- "index": info.RetryIndex,
- "is_retry": info.RetryIndex > 0,
- }
- if info.LastError != nil {
- code := string(info.LastError.GetErrorCode())
- errorType := string(info.LastError.GetErrorType())
- lastError := map[string]interface{}{
- "status_code": info.LastError.StatusCode,
- "message": info.LastError.Error(),
- "code": code,
- "error_code": code,
- "type": errorType,
- "error_type": errorType,
- "skip_retry": types.IsSkipRetryError(info.LastError),
- }
- ctx["last_error"] = lastError
- ctx["last_error_status_code"] = info.LastError.StatusCode
- ctx["last_error_message"] = info.LastError.Error()
- ctx["last_error_code"] = code
- ctx["last_error_type"] = errorType
- }
- ctx["is_channel_test"] = info.IsChannelTest
- return ctx
- }
|