override.go 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. package common
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "regexp"
  7. "sort"
  8. "strconv"
  9. "strings"
  10. "github.com/QuantumNous/new-api/common"
  11. "github.com/QuantumNous/new-api/types"
  12. "github.com/samber/lo"
  13. "github.com/tidwall/gjson"
  14. "github.com/tidwall/sjson"
  15. )
  16. var negativeIndexRegexp = regexp.MustCompile(`\.(-\d+)`)
  17. const (
  18. paramOverrideContextRequestHeaders = "request_headers"
  19. paramOverrideContextHeaderOverride = "header_override"
  20. )
  21. var errSourceHeaderNotFound = errors.New("source header does not exist")
  22. type ConditionOperation struct {
  23. Path string `json:"path"` // JSON路径
  24. Mode string `json:"mode"` // full, prefix, suffix, contains, gt, gte, lt, lte
  25. Value interface{} `json:"value"` // 匹配的值
  26. Invert bool `json:"invert"` // 反选功能,true表示取反结果
  27. PassMissingKey bool `json:"pass_missing_key"` // 未获取到json key时的行为
  28. }
  29. type ParamOperation struct {
  30. Path string `json:"path"`
  31. 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
  32. Value interface{} `json:"value"`
  33. KeepOrigin bool `json:"keep_origin"`
  34. From string `json:"from,omitempty"`
  35. To string `json:"to,omitempty"`
  36. Conditions []ConditionOperation `json:"conditions,omitempty"` // 条件列表
  37. Logic string `json:"logic,omitempty"` // AND, OR (默认OR)
  38. }
  39. type ParamOverrideReturnError struct {
  40. Message string
  41. StatusCode int
  42. Code string
  43. Type string
  44. SkipRetry bool
  45. }
  46. func (e *ParamOverrideReturnError) Error() string {
  47. if e == nil {
  48. return "param override return error"
  49. }
  50. if e.Message == "" {
  51. return "param override return error"
  52. }
  53. return e.Message
  54. }
  55. func AsParamOverrideReturnError(err error) (*ParamOverrideReturnError, bool) {
  56. if err == nil {
  57. return nil, false
  58. }
  59. var target *ParamOverrideReturnError
  60. if errors.As(err, &target) {
  61. return target, true
  62. }
  63. return nil, false
  64. }
  65. func NewAPIErrorFromParamOverride(err *ParamOverrideReturnError) *types.NewAPIError {
  66. if err == nil {
  67. return types.NewError(
  68. errors.New("param override return error is nil"),
  69. types.ErrorCodeChannelParamOverrideInvalid,
  70. types.ErrOptionWithSkipRetry(),
  71. )
  72. }
  73. statusCode := err.StatusCode
  74. if statusCode < http.StatusContinue || statusCode > http.StatusNetworkAuthenticationRequired {
  75. statusCode = http.StatusBadRequest
  76. }
  77. errorCode := err.Code
  78. if strings.TrimSpace(errorCode) == "" {
  79. errorCode = string(types.ErrorCodeInvalidRequest)
  80. }
  81. errorType := err.Type
  82. if strings.TrimSpace(errorType) == "" {
  83. errorType = "invalid_request_error"
  84. }
  85. message := strings.TrimSpace(err.Message)
  86. if message == "" {
  87. message = "request blocked by param override"
  88. }
  89. opts := make([]types.NewAPIErrorOptions, 0, 1)
  90. if err.SkipRetry {
  91. opts = append(opts, types.ErrOptionWithSkipRetry())
  92. }
  93. return types.WithOpenAIError(types.OpenAIError{
  94. Message: message,
  95. Type: errorType,
  96. Code: errorCode,
  97. }, statusCode, opts...)
  98. }
  99. func ApplyParamOverride(jsonData []byte, paramOverride map[string]interface{}, conditionContext map[string]interface{}) ([]byte, error) {
  100. if len(paramOverride) == 0 {
  101. return jsonData, nil
  102. }
  103. // 尝试断言为操作格式
  104. if operations, ok := tryParseOperations(paramOverride); ok {
  105. legacyOverride := buildLegacyParamOverride(paramOverride)
  106. workingJSON := jsonData
  107. var err error
  108. if len(legacyOverride) > 0 {
  109. workingJSON, err = applyOperationsLegacy(workingJSON, legacyOverride)
  110. if err != nil {
  111. return nil, err
  112. }
  113. }
  114. // 使用新方法
  115. result, err := applyOperations(string(workingJSON), operations, conditionContext)
  116. return []byte(result), err
  117. }
  118. // 直接使用旧方法
  119. return applyOperationsLegacy(jsonData, paramOverride)
  120. }
  121. func buildLegacyParamOverride(paramOverride map[string]interface{}) map[string]interface{} {
  122. if len(paramOverride) == 0 {
  123. return nil
  124. }
  125. legacy := make(map[string]interface{}, len(paramOverride))
  126. for key, value := range paramOverride {
  127. if strings.EqualFold(strings.TrimSpace(key), "operations") {
  128. continue
  129. }
  130. legacy[key] = value
  131. }
  132. return legacy
  133. }
  134. func ApplyParamOverrideWithRelayInfo(jsonData []byte, info *RelayInfo) ([]byte, error) {
  135. paramOverride := getParamOverrideMap(info)
  136. if len(paramOverride) == 0 {
  137. return jsonData, nil
  138. }
  139. overrideCtx := BuildParamOverrideContext(info)
  140. result, err := ApplyParamOverride(jsonData, paramOverride, overrideCtx)
  141. if err != nil {
  142. return nil, err
  143. }
  144. syncRuntimeHeaderOverrideFromContext(info, overrideCtx)
  145. return result, nil
  146. }
  147. func getParamOverrideMap(info *RelayInfo) map[string]interface{} {
  148. if info == nil || info.ChannelMeta == nil {
  149. return nil
  150. }
  151. return info.ChannelMeta.ParamOverride
  152. }
  153. func getHeaderOverrideMap(info *RelayInfo) map[string]interface{} {
  154. if info == nil || info.ChannelMeta == nil {
  155. return nil
  156. }
  157. return info.ChannelMeta.HeadersOverride
  158. }
  159. func sanitizeHeaderOverrideMap(source map[string]interface{}) map[string]interface{} {
  160. if len(source) == 0 {
  161. return map[string]interface{}{}
  162. }
  163. target := make(map[string]interface{}, len(source))
  164. for key, value := range source {
  165. normalizedKey := normalizeHeaderContextKey(key)
  166. if normalizedKey == "" {
  167. continue
  168. }
  169. normalizedValue := strings.TrimSpace(fmt.Sprintf("%v", value))
  170. if normalizedValue == "" {
  171. if isHeaderPassthroughRuleKeyForOverride(normalizedKey) {
  172. target[normalizedKey] = ""
  173. }
  174. continue
  175. }
  176. target[normalizedKey] = normalizedValue
  177. }
  178. return target
  179. }
  180. func isHeaderPassthroughRuleKeyForOverride(key string) bool {
  181. key = strings.TrimSpace(strings.ToLower(key))
  182. if key == "" {
  183. return false
  184. }
  185. if key == "*" {
  186. return true
  187. }
  188. return strings.HasPrefix(key, "re:") || strings.HasPrefix(key, "regex:")
  189. }
  190. func GetEffectiveHeaderOverride(info *RelayInfo) map[string]interface{} {
  191. if info == nil {
  192. return map[string]interface{}{}
  193. }
  194. if info.UseRuntimeHeadersOverride {
  195. return sanitizeHeaderOverrideMap(info.RuntimeHeadersOverride)
  196. }
  197. return sanitizeHeaderOverrideMap(getHeaderOverrideMap(info))
  198. }
  199. func tryParseOperations(paramOverride map[string]interface{}) ([]ParamOperation, bool) {
  200. // 检查是否包含 "operations" 字段
  201. opsValue, exists := paramOverride["operations"]
  202. if !exists {
  203. return nil, false
  204. }
  205. var opMaps []map[string]interface{}
  206. switch ops := opsValue.(type) {
  207. case []interface{}:
  208. opMaps = make([]map[string]interface{}, 0, len(ops))
  209. for _, op := range ops {
  210. opMap, ok := op.(map[string]interface{})
  211. if !ok {
  212. return nil, false
  213. }
  214. opMaps = append(opMaps, opMap)
  215. }
  216. case []map[string]interface{}:
  217. opMaps = ops
  218. default:
  219. return nil, false
  220. }
  221. operations := make([]ParamOperation, 0, len(opMaps))
  222. for _, opMap := range opMaps {
  223. operation := ParamOperation{}
  224. // 断言必要字段
  225. if path, ok := opMap["path"].(string); ok {
  226. operation.Path = path
  227. }
  228. if mode, ok := opMap["mode"].(string); ok {
  229. operation.Mode = mode
  230. } else {
  231. return nil, false // mode 是必需的
  232. }
  233. // 可选字段
  234. if value, exists := opMap["value"]; exists {
  235. operation.Value = value
  236. }
  237. if keepOrigin, ok := opMap["keep_origin"].(bool); ok {
  238. operation.KeepOrigin = keepOrigin
  239. }
  240. if from, ok := opMap["from"].(string); ok {
  241. operation.From = from
  242. }
  243. if to, ok := opMap["to"].(string); ok {
  244. operation.To = to
  245. }
  246. if logic, ok := opMap["logic"].(string); ok {
  247. operation.Logic = logic
  248. } else {
  249. operation.Logic = "OR" // 默认为OR
  250. }
  251. // 解析条件
  252. if conditions, exists := opMap["conditions"]; exists {
  253. parsedConditions, err := parseConditionOperations(conditions)
  254. if err != nil {
  255. return nil, false
  256. }
  257. operation.Conditions = append(operation.Conditions, parsedConditions...)
  258. }
  259. operations = append(operations, operation)
  260. }
  261. return operations, true
  262. }
  263. func checkConditions(jsonStr, contextJSON string, conditions []ConditionOperation, logic string) (bool, error) {
  264. if len(conditions) == 0 {
  265. return true, nil // 没有条件,直接通过
  266. }
  267. results := make([]bool, len(conditions))
  268. for i, condition := range conditions {
  269. result, err := checkSingleCondition(jsonStr, contextJSON, condition)
  270. if err != nil {
  271. return false, err
  272. }
  273. results[i] = result
  274. }
  275. if strings.ToUpper(logic) == "AND" {
  276. return lo.EveryBy(results, func(item bool) bool { return item }), nil
  277. }
  278. return lo.SomeBy(results, func(item bool) bool { return item }), nil
  279. }
  280. func checkSingleCondition(jsonStr, contextJSON string, condition ConditionOperation) (bool, error) {
  281. // 处理负数索引
  282. path := processNegativeIndex(jsonStr, condition.Path)
  283. value := gjson.Get(jsonStr, path)
  284. if !value.Exists() && contextJSON != "" {
  285. value = gjson.Get(contextJSON, condition.Path)
  286. }
  287. if !value.Exists() {
  288. if condition.PassMissingKey {
  289. return true, nil
  290. }
  291. return false, nil
  292. }
  293. // 利用gjson的类型解析
  294. targetBytes, err := common.Marshal(condition.Value)
  295. if err != nil {
  296. return false, fmt.Errorf("failed to marshal condition value: %v", err)
  297. }
  298. targetValue := gjson.ParseBytes(targetBytes)
  299. result, err := compareGjsonValues(value, targetValue, strings.ToLower(condition.Mode))
  300. if err != nil {
  301. return false, fmt.Errorf("comparison failed for path %s: %v", condition.Path, err)
  302. }
  303. if condition.Invert {
  304. result = !result
  305. }
  306. return result, nil
  307. }
  308. func processNegativeIndex(jsonStr string, path string) string {
  309. matches := negativeIndexRegexp.FindAllStringSubmatch(path, -1)
  310. if len(matches) == 0 {
  311. return path
  312. }
  313. result := path
  314. for _, match := range matches {
  315. negIndex := match[1]
  316. index, _ := strconv.Atoi(negIndex)
  317. arrayPath := strings.Split(path, negIndex)[0]
  318. if strings.HasSuffix(arrayPath, ".") {
  319. arrayPath = arrayPath[:len(arrayPath)-1]
  320. }
  321. array := gjson.Get(jsonStr, arrayPath)
  322. if array.IsArray() {
  323. length := len(array.Array())
  324. actualIndex := length + index
  325. if actualIndex >= 0 && actualIndex < length {
  326. result = strings.Replace(result, match[0], "."+strconv.Itoa(actualIndex), 1)
  327. }
  328. }
  329. }
  330. return result
  331. }
  332. // compareGjsonValues 直接比较两个gjson.Result,支持所有比较模式
  333. func compareGjsonValues(jsonValue, targetValue gjson.Result, mode string) (bool, error) {
  334. switch mode {
  335. case "full":
  336. return compareEqual(jsonValue, targetValue)
  337. case "prefix":
  338. return strings.HasPrefix(jsonValue.String(), targetValue.String()), nil
  339. case "suffix":
  340. return strings.HasSuffix(jsonValue.String(), targetValue.String()), nil
  341. case "contains":
  342. return strings.Contains(jsonValue.String(), targetValue.String()), nil
  343. case "gt":
  344. return compareNumeric(jsonValue, targetValue, "gt")
  345. case "gte":
  346. return compareNumeric(jsonValue, targetValue, "gte")
  347. case "lt":
  348. return compareNumeric(jsonValue, targetValue, "lt")
  349. case "lte":
  350. return compareNumeric(jsonValue, targetValue, "lte")
  351. default:
  352. return false, fmt.Errorf("unsupported comparison mode: %s", mode)
  353. }
  354. }
  355. func compareEqual(jsonValue, targetValue gjson.Result) (bool, error) {
  356. // 对null值特殊处理:两个都是null返回true,一个是null另一个不是返回false
  357. if jsonValue.Type == gjson.Null || targetValue.Type == gjson.Null {
  358. return jsonValue.Type == gjson.Null && targetValue.Type == gjson.Null, nil
  359. }
  360. // 对布尔值特殊处理
  361. if (jsonValue.Type == gjson.True || jsonValue.Type == gjson.False) &&
  362. (targetValue.Type == gjson.True || targetValue.Type == gjson.False) {
  363. return jsonValue.Bool() == targetValue.Bool(), nil
  364. }
  365. // 如果类型不同,报错
  366. if jsonValue.Type != targetValue.Type {
  367. return false, fmt.Errorf("compare for different types, got %v and %v", jsonValue.Type, targetValue.Type)
  368. }
  369. switch jsonValue.Type {
  370. case gjson.True, gjson.False:
  371. return jsonValue.Bool() == targetValue.Bool(), nil
  372. case gjson.Number:
  373. return jsonValue.Num == targetValue.Num, nil
  374. case gjson.String:
  375. return jsonValue.String() == targetValue.String(), nil
  376. default:
  377. return jsonValue.String() == targetValue.String(), nil
  378. }
  379. }
  380. func compareNumeric(jsonValue, targetValue gjson.Result, operator string) (bool, error) {
  381. // 只有数字类型才支持数值比较
  382. if jsonValue.Type != gjson.Number || targetValue.Type != gjson.Number {
  383. return false, fmt.Errorf("numeric comparison requires both values to be numbers, got %v and %v", jsonValue.Type, targetValue.Type)
  384. }
  385. jsonNum := jsonValue.Num
  386. targetNum := targetValue.Num
  387. switch operator {
  388. case "gt":
  389. return jsonNum > targetNum, nil
  390. case "gte":
  391. return jsonNum >= targetNum, nil
  392. case "lt":
  393. return jsonNum < targetNum, nil
  394. case "lte":
  395. return jsonNum <= targetNum, nil
  396. default:
  397. return false, fmt.Errorf("unsupported numeric operator: %s", operator)
  398. }
  399. }
  400. // applyOperationsLegacy 原参数覆盖方法
  401. func applyOperationsLegacy(jsonData []byte, paramOverride map[string]interface{}) ([]byte, error) {
  402. reqMap := make(map[string]interface{})
  403. err := common.Unmarshal(jsonData, &reqMap)
  404. if err != nil {
  405. return nil, err
  406. }
  407. for key, value := range paramOverride {
  408. reqMap[key] = value
  409. }
  410. return common.Marshal(reqMap)
  411. }
  412. func applyOperations(jsonStr string, operations []ParamOperation, conditionContext map[string]interface{}) (string, error) {
  413. context := ensureContextMap(conditionContext)
  414. contextJSON, err := marshalContextJSON(context)
  415. if err != nil {
  416. return "", fmt.Errorf("failed to marshal condition context: %v", err)
  417. }
  418. result := jsonStr
  419. for _, op := range operations {
  420. // 检查条件是否满足
  421. ok, err := checkConditions(result, contextJSON, op.Conditions, op.Logic)
  422. if err != nil {
  423. return "", err
  424. }
  425. if !ok {
  426. continue // 条件不满足,跳过当前操作
  427. }
  428. // 处理路径中的负数索引
  429. opPath := processNegativeIndex(result, op.Path)
  430. var opPaths []string
  431. if isPathBasedOperation(op.Mode) {
  432. opPaths, err = resolveOperationPaths(result, opPath)
  433. if err != nil {
  434. return "", err
  435. }
  436. if len(opPaths) == 0 {
  437. continue
  438. }
  439. }
  440. switch op.Mode {
  441. case "delete":
  442. for _, path := range opPaths {
  443. result, err = deleteValue(result, path)
  444. if err != nil {
  445. break
  446. }
  447. }
  448. case "set":
  449. for _, path := range opPaths {
  450. if op.KeepOrigin && gjson.Get(result, path).Exists() {
  451. continue
  452. }
  453. result, err = sjson.Set(result, path, op.Value)
  454. if err != nil {
  455. break
  456. }
  457. }
  458. case "move":
  459. opFrom := processNegativeIndex(result, op.From)
  460. opTo := processNegativeIndex(result, op.To)
  461. result, err = moveValue(result, opFrom, opTo)
  462. case "copy":
  463. if op.From == "" || op.To == "" {
  464. return "", fmt.Errorf("copy from/to is required")
  465. }
  466. opFrom := processNegativeIndex(result, op.From)
  467. opTo := processNegativeIndex(result, op.To)
  468. result, err = copyValue(result, opFrom, opTo)
  469. case "prepend":
  470. for _, path := range opPaths {
  471. result, err = modifyValue(result, path, op.Value, op.KeepOrigin, true)
  472. if err != nil {
  473. break
  474. }
  475. }
  476. case "append":
  477. for _, path := range opPaths {
  478. result, err = modifyValue(result, path, op.Value, op.KeepOrigin, false)
  479. if err != nil {
  480. break
  481. }
  482. }
  483. case "trim_prefix":
  484. for _, path := range opPaths {
  485. result, err = trimStringValue(result, path, op.Value, true)
  486. if err != nil {
  487. break
  488. }
  489. }
  490. case "trim_suffix":
  491. for _, path := range opPaths {
  492. result, err = trimStringValue(result, path, op.Value, false)
  493. if err != nil {
  494. break
  495. }
  496. }
  497. case "ensure_prefix":
  498. for _, path := range opPaths {
  499. result, err = ensureStringAffix(result, path, op.Value, true)
  500. if err != nil {
  501. break
  502. }
  503. }
  504. case "ensure_suffix":
  505. for _, path := range opPaths {
  506. result, err = ensureStringAffix(result, path, op.Value, false)
  507. if err != nil {
  508. break
  509. }
  510. }
  511. case "trim_space":
  512. for _, path := range opPaths {
  513. result, err = transformStringValue(result, path, strings.TrimSpace)
  514. if err != nil {
  515. break
  516. }
  517. }
  518. case "to_lower":
  519. for _, path := range opPaths {
  520. result, err = transformStringValue(result, path, strings.ToLower)
  521. if err != nil {
  522. break
  523. }
  524. }
  525. case "to_upper":
  526. for _, path := range opPaths {
  527. result, err = transformStringValue(result, path, strings.ToUpper)
  528. if err != nil {
  529. break
  530. }
  531. }
  532. case "replace":
  533. for _, path := range opPaths {
  534. result, err = replaceStringValue(result, path, op.From, op.To)
  535. if err != nil {
  536. break
  537. }
  538. }
  539. case "regex_replace":
  540. for _, path := range opPaths {
  541. result, err = regexReplaceStringValue(result, path, op.From, op.To)
  542. if err != nil {
  543. break
  544. }
  545. }
  546. case "return_error":
  547. returnErr, parseErr := parseParamOverrideReturnError(op.Value)
  548. if parseErr != nil {
  549. return "", parseErr
  550. }
  551. return "", returnErr
  552. case "prune_objects":
  553. for _, path := range opPaths {
  554. result, err = pruneObjects(result, path, contextJSON, op.Value)
  555. if err != nil {
  556. break
  557. }
  558. }
  559. case "set_header":
  560. err = setHeaderOverrideInContext(context, op.Path, op.Value, op.KeepOrigin)
  561. if err == nil {
  562. contextJSON, err = marshalContextJSON(context)
  563. }
  564. case "delete_header":
  565. err = deleteHeaderOverrideInContext(context, op.Path)
  566. if err == nil {
  567. contextJSON, err = marshalContextJSON(context)
  568. }
  569. case "copy_header":
  570. sourceHeader := strings.TrimSpace(op.From)
  571. targetHeader := strings.TrimSpace(op.To)
  572. if sourceHeader == "" {
  573. sourceHeader = strings.TrimSpace(op.Path)
  574. }
  575. if targetHeader == "" {
  576. targetHeader = strings.TrimSpace(op.Path)
  577. }
  578. err = copyHeaderInContext(context, sourceHeader, targetHeader, op.KeepOrigin)
  579. if errors.Is(err, errSourceHeaderNotFound) {
  580. err = nil
  581. }
  582. if err == nil {
  583. contextJSON, err = marshalContextJSON(context)
  584. }
  585. case "move_header":
  586. sourceHeader := strings.TrimSpace(op.From)
  587. targetHeader := strings.TrimSpace(op.To)
  588. if sourceHeader == "" {
  589. sourceHeader = strings.TrimSpace(op.Path)
  590. }
  591. if targetHeader == "" {
  592. targetHeader = strings.TrimSpace(op.Path)
  593. }
  594. err = moveHeaderInContext(context, sourceHeader, targetHeader, op.KeepOrigin)
  595. if errors.Is(err, errSourceHeaderNotFound) {
  596. err = nil
  597. }
  598. if err == nil {
  599. contextJSON, err = marshalContextJSON(context)
  600. }
  601. case "pass_headers":
  602. headerNames, parseErr := parseHeaderPassThroughNames(op.Value)
  603. if parseErr != nil {
  604. return "", parseErr
  605. }
  606. for _, headerName := range headerNames {
  607. if err = copyHeaderInContext(context, headerName, headerName, op.KeepOrigin); err != nil {
  608. if errors.Is(err, errSourceHeaderNotFound) {
  609. err = nil
  610. continue
  611. }
  612. break
  613. }
  614. }
  615. if err == nil {
  616. contextJSON, err = marshalContextJSON(context)
  617. }
  618. case "sync_fields":
  619. result, err = syncFieldsBetweenTargets(result, context, op.From, op.To)
  620. if err == nil {
  621. contextJSON, err = marshalContextJSON(context)
  622. }
  623. default:
  624. return "", fmt.Errorf("unknown operation: %s", op.Mode)
  625. }
  626. if err != nil {
  627. return "", fmt.Errorf("operation %s failed: %w", op.Mode, err)
  628. }
  629. }
  630. return result, nil
  631. }
  632. func parseParamOverrideReturnError(value interface{}) (*ParamOverrideReturnError, error) {
  633. result := &ParamOverrideReturnError{
  634. StatusCode: http.StatusBadRequest,
  635. Code: string(types.ErrorCodeInvalidRequest),
  636. Type: "invalid_request_error",
  637. SkipRetry: true,
  638. }
  639. switch raw := value.(type) {
  640. case nil:
  641. return nil, fmt.Errorf("return_error value is required")
  642. case string:
  643. result.Message = strings.TrimSpace(raw)
  644. case map[string]interface{}:
  645. if message, ok := raw["message"].(string); ok {
  646. result.Message = strings.TrimSpace(message)
  647. }
  648. if result.Message == "" {
  649. if message, ok := raw["msg"].(string); ok {
  650. result.Message = strings.TrimSpace(message)
  651. }
  652. }
  653. if code, exists := raw["code"]; exists {
  654. codeStr := strings.TrimSpace(fmt.Sprintf("%v", code))
  655. if codeStr != "" {
  656. result.Code = codeStr
  657. }
  658. }
  659. if errType, ok := raw["type"].(string); ok {
  660. errType = strings.TrimSpace(errType)
  661. if errType != "" {
  662. result.Type = errType
  663. }
  664. }
  665. if skipRetry, ok := raw["skip_retry"].(bool); ok {
  666. result.SkipRetry = skipRetry
  667. }
  668. if statusCodeRaw, exists := raw["status_code"]; exists {
  669. statusCode, ok := parseOverrideInt(statusCodeRaw)
  670. if !ok {
  671. return nil, fmt.Errorf("return_error status_code must be an integer")
  672. }
  673. result.StatusCode = statusCode
  674. } else if statusRaw, exists := raw["status"]; exists {
  675. statusCode, ok := parseOverrideInt(statusRaw)
  676. if !ok {
  677. return nil, fmt.Errorf("return_error status must be an integer")
  678. }
  679. result.StatusCode = statusCode
  680. }
  681. default:
  682. return nil, fmt.Errorf("return_error value must be string or object")
  683. }
  684. if result.Message == "" {
  685. return nil, fmt.Errorf("return_error message is required")
  686. }
  687. if result.StatusCode < http.StatusContinue || result.StatusCode > http.StatusNetworkAuthenticationRequired {
  688. return nil, fmt.Errorf("return_error status code out of range: %d", result.StatusCode)
  689. }
  690. return result, nil
  691. }
  692. func parseOverrideInt(v interface{}) (int, bool) {
  693. switch value := v.(type) {
  694. case int:
  695. return value, true
  696. case float64:
  697. if value != float64(int(value)) {
  698. return 0, false
  699. }
  700. return int(value), true
  701. default:
  702. return 0, false
  703. }
  704. }
  705. func ensureContextMap(conditionContext map[string]interface{}) map[string]interface{} {
  706. if conditionContext != nil {
  707. return conditionContext
  708. }
  709. return make(map[string]interface{})
  710. }
  711. func marshalContextJSON(context map[string]interface{}) (string, error) {
  712. if context == nil || len(context) == 0 {
  713. return "", nil
  714. }
  715. ctxBytes, err := common.Marshal(context)
  716. if err != nil {
  717. return "", err
  718. }
  719. return string(ctxBytes), nil
  720. }
  721. func setHeaderOverrideInContext(context map[string]interface{}, headerName string, value interface{}, keepOrigin bool) error {
  722. headerName = normalizeHeaderContextKey(headerName)
  723. if headerName == "" {
  724. return fmt.Errorf("header name is required")
  725. }
  726. rawHeaders := ensureMapKeyInContext(context, paramOverrideContextHeaderOverride)
  727. if keepOrigin {
  728. if existing, ok := rawHeaders[headerName]; ok {
  729. existingValue := strings.TrimSpace(fmt.Sprintf("%v", existing))
  730. if existingValue != "" {
  731. return nil
  732. }
  733. }
  734. }
  735. headerValue, hasValue, err := resolveHeaderOverrideValue(context, headerName, value)
  736. if err != nil {
  737. return err
  738. }
  739. if !hasValue {
  740. delete(rawHeaders, headerName)
  741. return nil
  742. }
  743. rawHeaders[headerName] = headerValue
  744. return nil
  745. }
  746. func resolveHeaderOverrideValue(context map[string]interface{}, headerName string, value interface{}) (string, bool, error) {
  747. if value == nil {
  748. return "", false, fmt.Errorf("header value is required")
  749. }
  750. if mapping, ok := value.(map[string]interface{}); ok {
  751. return resolveHeaderOverrideValueByMapping(context, headerName, mapping)
  752. }
  753. if mapping, ok := value.(map[string]string); ok {
  754. converted := make(map[string]interface{}, len(mapping))
  755. for key, item := range mapping {
  756. converted[key] = item
  757. }
  758. return resolveHeaderOverrideValueByMapping(context, headerName, converted)
  759. }
  760. headerValue := strings.TrimSpace(fmt.Sprintf("%v", value))
  761. if headerValue == "" {
  762. return "", false, nil
  763. }
  764. return headerValue, true, nil
  765. }
  766. func resolveHeaderOverrideValueByMapping(context map[string]interface{}, headerName string, mapping map[string]interface{}) (string, bool, error) {
  767. if len(mapping) == 0 {
  768. return "", false, fmt.Errorf("header value mapping cannot be empty")
  769. }
  770. appendTokens, err := parseHeaderAppendTokens(mapping)
  771. if err != nil {
  772. return "", false, err
  773. }
  774. keepOnlyDeclared := parseHeaderKeepOnlyDeclared(mapping)
  775. sourceValue, exists := getHeaderValueFromContext(context, headerName)
  776. sourceTokens := make([]string, 0)
  777. if exists {
  778. sourceTokens = splitHeaderListValue(sourceValue)
  779. }
  780. wildcardValue, hasWildcard := mapping["*"]
  781. resultTokens := make([]string, 0, len(sourceTokens)+len(appendTokens))
  782. for _, token := range sourceTokens {
  783. replacementRaw, hasReplacement := mapping[token]
  784. if !hasReplacement && hasWildcard && !keepOnlyDeclared {
  785. replacementRaw = wildcardValue
  786. hasReplacement = true
  787. }
  788. if !hasReplacement {
  789. if keepOnlyDeclared {
  790. continue
  791. }
  792. resultTokens = append(resultTokens, token)
  793. continue
  794. }
  795. replacementTokens, err := parseHeaderReplacementTokens(replacementRaw)
  796. if err != nil {
  797. return "", false, err
  798. }
  799. resultTokens = append(resultTokens, replacementTokens...)
  800. }
  801. resultTokens = append(resultTokens, appendTokens...)
  802. resultTokens = lo.Uniq(resultTokens)
  803. if len(resultTokens) == 0 {
  804. return "", false, nil
  805. }
  806. return strings.Join(resultTokens, ","), true, nil
  807. }
  808. func parseHeaderAppendTokens(mapping map[string]interface{}) ([]string, error) {
  809. appendRaw, ok := mapping["$append"]
  810. if !ok {
  811. return nil, nil
  812. }
  813. return parseHeaderReplacementTokens(appendRaw)
  814. }
  815. func parseHeaderKeepOnlyDeclared(mapping map[string]interface{}) bool {
  816. keepOnlyDeclaredRaw, ok := mapping["$keep_only_declared"]
  817. if !ok {
  818. return false
  819. }
  820. keepOnlyDeclared, ok := keepOnlyDeclaredRaw.(bool)
  821. if !ok {
  822. return false
  823. }
  824. return keepOnlyDeclared
  825. }
  826. func parseHeaderReplacementTokens(value interface{}) ([]string, error) {
  827. switch raw := value.(type) {
  828. case nil:
  829. return nil, nil
  830. case string:
  831. return splitHeaderListValue(raw), nil
  832. case []string:
  833. tokens := make([]string, 0, len(raw))
  834. for _, item := range raw {
  835. tokens = append(tokens, splitHeaderListValue(item)...)
  836. }
  837. return lo.Uniq(tokens), nil
  838. case []interface{}:
  839. tokens := make([]string, 0, len(raw))
  840. for _, item := range raw {
  841. itemTokens, err := parseHeaderReplacementTokens(item)
  842. if err != nil {
  843. return nil, err
  844. }
  845. tokens = append(tokens, itemTokens...)
  846. }
  847. return lo.Uniq(tokens), nil
  848. case map[string]interface{}, map[string]string:
  849. return nil, fmt.Errorf("header replacement value must be string, array or null")
  850. default:
  851. token := strings.TrimSpace(fmt.Sprintf("%v", raw))
  852. if token == "" {
  853. return nil, nil
  854. }
  855. return []string{token}, nil
  856. }
  857. }
  858. func splitHeaderListValue(raw string) []string {
  859. items := strings.Split(raw, ",")
  860. return lo.FilterMap(items, func(item string, _ int) (string, bool) {
  861. token := strings.TrimSpace(item)
  862. if token == "" {
  863. return "", false
  864. }
  865. return token, true
  866. })
  867. }
  868. func copyHeaderInContext(context map[string]interface{}, fromHeader, toHeader string, keepOrigin bool) error {
  869. fromHeader = normalizeHeaderContextKey(fromHeader)
  870. toHeader = normalizeHeaderContextKey(toHeader)
  871. if fromHeader == "" || toHeader == "" {
  872. return fmt.Errorf("copy_header from/to is required")
  873. }
  874. value, exists := getHeaderValueFromContext(context, fromHeader)
  875. if !exists {
  876. return fmt.Errorf("%w: %s", errSourceHeaderNotFound, fromHeader)
  877. }
  878. return setHeaderOverrideInContext(context, toHeader, value, keepOrigin)
  879. }
  880. func moveHeaderInContext(context map[string]interface{}, fromHeader, toHeader string, keepOrigin bool) error {
  881. fromHeader = normalizeHeaderContextKey(fromHeader)
  882. toHeader = normalizeHeaderContextKey(toHeader)
  883. if fromHeader == "" || toHeader == "" {
  884. return fmt.Errorf("move_header from/to is required")
  885. }
  886. if err := copyHeaderInContext(context, fromHeader, toHeader, keepOrigin); err != nil {
  887. return err
  888. }
  889. if strings.EqualFold(fromHeader, toHeader) {
  890. return nil
  891. }
  892. return deleteHeaderOverrideInContext(context, fromHeader)
  893. }
  894. func deleteHeaderOverrideInContext(context map[string]interface{}, headerName string) error {
  895. headerName = normalizeHeaderContextKey(headerName)
  896. if headerName == "" {
  897. return fmt.Errorf("header name is required")
  898. }
  899. rawHeaders := ensureMapKeyInContext(context, paramOverrideContextHeaderOverride)
  900. delete(rawHeaders, headerName)
  901. return nil
  902. }
  903. func parseHeaderPassThroughNames(value interface{}) ([]string, error) {
  904. normalizeNames := func(values []string) []string {
  905. names := lo.FilterMap(values, func(item string, _ int) (string, bool) {
  906. headerName := normalizeHeaderContextKey(item)
  907. if headerName == "" {
  908. return "", false
  909. }
  910. return headerName, true
  911. })
  912. return lo.Uniq(names)
  913. }
  914. switch raw := value.(type) {
  915. case nil:
  916. return nil, fmt.Errorf("pass_headers value is required")
  917. case string:
  918. trimmed := strings.TrimSpace(raw)
  919. if trimmed == "" {
  920. return nil, fmt.Errorf("pass_headers value is required")
  921. }
  922. if strings.HasPrefix(trimmed, "[") || strings.HasPrefix(trimmed, "{") {
  923. var parsed interface{}
  924. if err := common.UnmarshalJsonStr(trimmed, &parsed); err == nil {
  925. return parseHeaderPassThroughNames(parsed)
  926. }
  927. }
  928. names := normalizeNames(strings.Split(trimmed, ","))
  929. if len(names) == 0 {
  930. return nil, fmt.Errorf("pass_headers value is invalid")
  931. }
  932. return names, nil
  933. case []interface{}:
  934. names := lo.FilterMap(raw, func(item interface{}, _ int) (string, bool) {
  935. headerName := normalizeHeaderContextKey(fmt.Sprintf("%v", item))
  936. if headerName == "" {
  937. return "", false
  938. }
  939. return headerName, true
  940. })
  941. names = lo.Uniq(names)
  942. if len(names) == 0 {
  943. return nil, fmt.Errorf("pass_headers value is invalid")
  944. }
  945. return names, nil
  946. case []string:
  947. names := lo.FilterMap(raw, func(item string, _ int) (string, bool) {
  948. headerName := normalizeHeaderContextKey(item)
  949. if headerName == "" {
  950. return "", false
  951. }
  952. return headerName, true
  953. })
  954. names = lo.Uniq(names)
  955. if len(names) == 0 {
  956. return nil, fmt.Errorf("pass_headers value is invalid")
  957. }
  958. return names, nil
  959. case map[string]interface{}:
  960. candidates := make([]string, 0, 8)
  961. if headersRaw, ok := raw["headers"]; ok {
  962. names, err := parseHeaderPassThroughNames(headersRaw)
  963. if err == nil {
  964. candidates = append(candidates, names...)
  965. }
  966. }
  967. if namesRaw, ok := raw["names"]; ok {
  968. names, err := parseHeaderPassThroughNames(namesRaw)
  969. if err == nil {
  970. candidates = append(candidates, names...)
  971. }
  972. }
  973. if headerRaw, ok := raw["header"]; ok {
  974. names, err := parseHeaderPassThroughNames(headerRaw)
  975. if err == nil {
  976. candidates = append(candidates, names...)
  977. }
  978. }
  979. names := normalizeNames(candidates)
  980. if len(names) == 0 {
  981. return nil, fmt.Errorf("pass_headers value is invalid")
  982. }
  983. return names, nil
  984. default:
  985. return nil, fmt.Errorf("pass_headers value must be string, array or object")
  986. }
  987. }
  988. type syncTarget struct {
  989. kind string
  990. key string
  991. }
  992. func parseSyncTarget(spec string) (syncTarget, error) {
  993. raw := strings.TrimSpace(spec)
  994. if raw == "" {
  995. return syncTarget{}, fmt.Errorf("sync_fields target is required")
  996. }
  997. idx := strings.Index(raw, ":")
  998. if idx < 0 {
  999. // Backward compatibility: treat bare value as JSON path.
  1000. return syncTarget{
  1001. kind: "json",
  1002. key: raw,
  1003. }, nil
  1004. }
  1005. kind := strings.ToLower(strings.TrimSpace(raw[:idx]))
  1006. key := strings.TrimSpace(raw[idx+1:])
  1007. if key == "" {
  1008. return syncTarget{}, fmt.Errorf("sync_fields target key is required: %s", raw)
  1009. }
  1010. switch kind {
  1011. case "json", "body":
  1012. return syncTarget{
  1013. kind: "json",
  1014. key: key,
  1015. }, nil
  1016. case "header":
  1017. return syncTarget{
  1018. kind: "header",
  1019. key: key,
  1020. }, nil
  1021. default:
  1022. return syncTarget{}, fmt.Errorf("sync_fields target prefix is invalid: %s", raw)
  1023. }
  1024. }
  1025. func readSyncTargetValue(jsonStr string, context map[string]interface{}, target syncTarget) (interface{}, bool, error) {
  1026. switch target.kind {
  1027. case "json":
  1028. path := processNegativeIndex(jsonStr, target.key)
  1029. value := gjson.Get(jsonStr, path)
  1030. if !value.Exists() || value.Type == gjson.Null {
  1031. return nil, false, nil
  1032. }
  1033. if value.Type == gjson.String && strings.TrimSpace(value.String()) == "" {
  1034. return nil, false, nil
  1035. }
  1036. return value.Value(), true, nil
  1037. case "header":
  1038. value, ok := getHeaderValueFromContext(context, target.key)
  1039. if !ok || strings.TrimSpace(value) == "" {
  1040. return nil, false, nil
  1041. }
  1042. return value, true, nil
  1043. default:
  1044. return nil, false, fmt.Errorf("unsupported sync_fields target kind: %s", target.kind)
  1045. }
  1046. }
  1047. func writeSyncTargetValue(jsonStr string, context map[string]interface{}, target syncTarget, value interface{}) (string, error) {
  1048. switch target.kind {
  1049. case "json":
  1050. path := processNegativeIndex(jsonStr, target.key)
  1051. nextJSON, err := sjson.Set(jsonStr, path, value)
  1052. if err != nil {
  1053. return "", err
  1054. }
  1055. return nextJSON, nil
  1056. case "header":
  1057. if err := setHeaderOverrideInContext(context, target.key, value, false); err != nil {
  1058. return "", err
  1059. }
  1060. return jsonStr, nil
  1061. default:
  1062. return "", fmt.Errorf("unsupported sync_fields target kind: %s", target.kind)
  1063. }
  1064. }
  1065. func syncFieldsBetweenTargets(jsonStr string, context map[string]interface{}, fromSpec string, toSpec string) (string, error) {
  1066. fromTarget, err := parseSyncTarget(fromSpec)
  1067. if err != nil {
  1068. return "", err
  1069. }
  1070. toTarget, err := parseSyncTarget(toSpec)
  1071. if err != nil {
  1072. return "", err
  1073. }
  1074. fromValue, fromExists, err := readSyncTargetValue(jsonStr, context, fromTarget)
  1075. if err != nil {
  1076. return "", err
  1077. }
  1078. toValue, toExists, err := readSyncTargetValue(jsonStr, context, toTarget)
  1079. if err != nil {
  1080. return "", err
  1081. }
  1082. // If one side exists and the other side is missing, sync the missing side.
  1083. if fromExists && !toExists {
  1084. return writeSyncTargetValue(jsonStr, context, toTarget, fromValue)
  1085. }
  1086. if toExists && !fromExists {
  1087. return writeSyncTargetValue(jsonStr, context, fromTarget, toValue)
  1088. }
  1089. return jsonStr, nil
  1090. }
  1091. func ensureMapKeyInContext(context map[string]interface{}, key string) map[string]interface{} {
  1092. if context == nil {
  1093. return map[string]interface{}{}
  1094. }
  1095. if existing, ok := context[key]; ok {
  1096. if mapVal, ok := existing.(map[string]interface{}); ok {
  1097. return mapVal
  1098. }
  1099. }
  1100. result := make(map[string]interface{})
  1101. context[key] = result
  1102. return result
  1103. }
  1104. func getHeaderValueFromContext(context map[string]interface{}, headerName string) (string, bool) {
  1105. headerName = normalizeHeaderContextKey(headerName)
  1106. if headerName == "" {
  1107. return "", false
  1108. }
  1109. for _, key := range []string{paramOverrideContextHeaderOverride, paramOverrideContextRequestHeaders} {
  1110. source := ensureMapKeyInContext(context, key)
  1111. raw, ok := source[headerName]
  1112. if !ok {
  1113. continue
  1114. }
  1115. value := strings.TrimSpace(fmt.Sprintf("%v", raw))
  1116. if value != "" {
  1117. return value, true
  1118. }
  1119. }
  1120. return "", false
  1121. }
  1122. func normalizeHeaderContextKey(key string) string {
  1123. return strings.TrimSpace(strings.ToLower(key))
  1124. }
  1125. func buildRequestHeadersContext(headers map[string]string) map[string]interface{} {
  1126. if len(headers) == 0 {
  1127. return map[string]interface{}{}
  1128. }
  1129. entries := lo.Entries(headers)
  1130. normalizedEntries := lo.FilterMap(entries, func(item lo.Entry[string, string], _ int) (lo.Entry[string, string], bool) {
  1131. normalized := normalizeHeaderContextKey(item.Key)
  1132. value := strings.TrimSpace(item.Value)
  1133. if normalized == "" || value == "" {
  1134. return lo.Entry[string, string]{}, false
  1135. }
  1136. return lo.Entry[string, string]{Key: normalized, Value: value}, true
  1137. })
  1138. return lo.SliceToMap(normalizedEntries, func(item lo.Entry[string, string]) (string, interface{}) {
  1139. return item.Key, item.Value
  1140. })
  1141. }
  1142. func syncRuntimeHeaderOverrideFromContext(info *RelayInfo, context map[string]interface{}) {
  1143. if info == nil || context == nil {
  1144. return
  1145. }
  1146. raw, exists := context[paramOverrideContextHeaderOverride]
  1147. if !exists {
  1148. return
  1149. }
  1150. rawMap, ok := raw.(map[string]interface{})
  1151. if !ok {
  1152. return
  1153. }
  1154. info.RuntimeHeadersOverride = sanitizeHeaderOverrideMap(rawMap)
  1155. info.UseRuntimeHeadersOverride = true
  1156. }
  1157. func moveValue(jsonStr, fromPath, toPath string) (string, error) {
  1158. sourceValue := gjson.Get(jsonStr, fromPath)
  1159. if !sourceValue.Exists() {
  1160. return jsonStr, fmt.Errorf("source path does not exist: %s", fromPath)
  1161. }
  1162. result, err := sjson.Set(jsonStr, toPath, sourceValue.Value())
  1163. if err != nil {
  1164. return "", err
  1165. }
  1166. return sjson.Delete(result, fromPath)
  1167. }
  1168. func copyValue(jsonStr, fromPath, toPath string) (string, error) {
  1169. sourceValue := gjson.Get(jsonStr, fromPath)
  1170. if !sourceValue.Exists() {
  1171. return jsonStr, fmt.Errorf("source path does not exist: %s", fromPath)
  1172. }
  1173. return sjson.Set(jsonStr, toPath, sourceValue.Value())
  1174. }
  1175. func isPathBasedOperation(mode string) bool {
  1176. switch mode {
  1177. case "delete", "set", "prepend", "append", "trim_prefix", "trim_suffix", "ensure_prefix", "ensure_suffix", "trim_space", "to_lower", "to_upper", "replace", "regex_replace", "prune_objects":
  1178. return true
  1179. default:
  1180. return false
  1181. }
  1182. }
  1183. func resolveOperationPaths(jsonStr, path string) ([]string, error) {
  1184. if !strings.Contains(path, "*") {
  1185. return []string{path}, nil
  1186. }
  1187. return expandWildcardPaths(jsonStr, path)
  1188. }
  1189. func expandWildcardPaths(jsonStr, path string) ([]string, error) {
  1190. var root interface{}
  1191. if err := common.Unmarshal([]byte(jsonStr), &root); err != nil {
  1192. return nil, err
  1193. }
  1194. segments := strings.Split(path, ".")
  1195. paths := collectWildcardPaths(root, segments, nil)
  1196. return lo.Uniq(paths), nil
  1197. }
  1198. func collectWildcardPaths(node interface{}, segments []string, prefix []string) []string {
  1199. if len(segments) == 0 {
  1200. return []string{strings.Join(prefix, ".")}
  1201. }
  1202. segment := strings.TrimSpace(segments[0])
  1203. if segment == "" {
  1204. return nil
  1205. }
  1206. isLast := len(segments) == 1
  1207. if segment == "*" {
  1208. switch typed := node.(type) {
  1209. case map[string]interface{}:
  1210. keys := lo.Keys(typed)
  1211. sort.Strings(keys)
  1212. return lo.FlatMap(keys, func(key string, _ int) []string {
  1213. return collectWildcardPaths(typed[key], segments[1:], append(prefix, key))
  1214. })
  1215. case []interface{}:
  1216. return lo.FlatMap(lo.Range(len(typed)), func(index int, _ int) []string {
  1217. return collectWildcardPaths(typed[index], segments[1:], append(prefix, strconv.Itoa(index)))
  1218. })
  1219. default:
  1220. return nil
  1221. }
  1222. }
  1223. switch typed := node.(type) {
  1224. case map[string]interface{}:
  1225. if isLast {
  1226. return []string{strings.Join(append(prefix, segment), ".")}
  1227. }
  1228. next, exists := typed[segment]
  1229. if !exists {
  1230. return nil
  1231. }
  1232. return collectWildcardPaths(next, segments[1:], append(prefix, segment))
  1233. case []interface{}:
  1234. index, err := strconv.Atoi(segment)
  1235. if err != nil || index < 0 || index >= len(typed) {
  1236. return nil
  1237. }
  1238. if isLast {
  1239. return []string{strings.Join(append(prefix, segment), ".")}
  1240. }
  1241. return collectWildcardPaths(typed[index], segments[1:], append(prefix, segment))
  1242. default:
  1243. return nil
  1244. }
  1245. }
  1246. func deleteValue(jsonStr, path string) (string, error) {
  1247. if strings.TrimSpace(path) == "" {
  1248. return jsonStr, nil
  1249. }
  1250. return sjson.Delete(jsonStr, path)
  1251. }
  1252. func modifyValue(jsonStr, path string, value interface{}, keepOrigin, isPrepend bool) (string, error) {
  1253. current := gjson.Get(jsonStr, path)
  1254. switch {
  1255. case current.IsArray():
  1256. return modifyArray(jsonStr, path, value, isPrepend)
  1257. case current.Type == gjson.String:
  1258. return modifyString(jsonStr, path, value, isPrepend)
  1259. case current.Type == gjson.JSON:
  1260. return mergeObjects(jsonStr, path, value, keepOrigin)
  1261. }
  1262. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  1263. }
  1264. func modifyArray(jsonStr, path string, value interface{}, isPrepend bool) (string, error) {
  1265. current := gjson.Get(jsonStr, path)
  1266. var newArray []interface{}
  1267. // 添加新值
  1268. addValue := func() {
  1269. if arr, ok := value.([]interface{}); ok {
  1270. newArray = append(newArray, arr...)
  1271. } else {
  1272. newArray = append(newArray, value)
  1273. }
  1274. }
  1275. // 添加原值
  1276. addOriginal := func() {
  1277. current.ForEach(func(_, val gjson.Result) bool {
  1278. newArray = append(newArray, val.Value())
  1279. return true
  1280. })
  1281. }
  1282. if isPrepend {
  1283. addValue()
  1284. addOriginal()
  1285. } else {
  1286. addOriginal()
  1287. addValue()
  1288. }
  1289. return sjson.Set(jsonStr, path, newArray)
  1290. }
  1291. func modifyString(jsonStr, path string, value interface{}, isPrepend bool) (string, error) {
  1292. current := gjson.Get(jsonStr, path)
  1293. valueStr := fmt.Sprintf("%v", value)
  1294. var newStr string
  1295. if isPrepend {
  1296. newStr = valueStr + current.String()
  1297. } else {
  1298. newStr = current.String() + valueStr
  1299. }
  1300. return sjson.Set(jsonStr, path, newStr)
  1301. }
  1302. func trimStringValue(jsonStr, path string, value interface{}, isPrefix bool) (string, error) {
  1303. current := gjson.Get(jsonStr, path)
  1304. if current.Type != gjson.String {
  1305. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  1306. }
  1307. if value == nil {
  1308. return jsonStr, fmt.Errorf("trim value is required")
  1309. }
  1310. valueStr := fmt.Sprintf("%v", value)
  1311. var newStr string
  1312. if isPrefix {
  1313. newStr = strings.TrimPrefix(current.String(), valueStr)
  1314. } else {
  1315. newStr = strings.TrimSuffix(current.String(), valueStr)
  1316. }
  1317. return sjson.Set(jsonStr, path, newStr)
  1318. }
  1319. func ensureStringAffix(jsonStr, path string, value interface{}, isPrefix bool) (string, error) {
  1320. current := gjson.Get(jsonStr, path)
  1321. if current.Type != gjson.String {
  1322. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  1323. }
  1324. if value == nil {
  1325. return jsonStr, fmt.Errorf("ensure value is required")
  1326. }
  1327. valueStr := fmt.Sprintf("%v", value)
  1328. if valueStr == "" {
  1329. return jsonStr, fmt.Errorf("ensure value is required")
  1330. }
  1331. currentStr := current.String()
  1332. if isPrefix {
  1333. if strings.HasPrefix(currentStr, valueStr) {
  1334. return jsonStr, nil
  1335. }
  1336. return sjson.Set(jsonStr, path, valueStr+currentStr)
  1337. }
  1338. if strings.HasSuffix(currentStr, valueStr) {
  1339. return jsonStr, nil
  1340. }
  1341. return sjson.Set(jsonStr, path, currentStr+valueStr)
  1342. }
  1343. func transformStringValue(jsonStr, path string, transform func(string) string) (string, error) {
  1344. current := gjson.Get(jsonStr, path)
  1345. if current.Type != gjson.String {
  1346. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  1347. }
  1348. return sjson.Set(jsonStr, path, transform(current.String()))
  1349. }
  1350. func replaceStringValue(jsonStr, path, from, to string) (string, error) {
  1351. current := gjson.Get(jsonStr, path)
  1352. if current.Type != gjson.String {
  1353. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  1354. }
  1355. if from == "" {
  1356. return jsonStr, fmt.Errorf("replace from is required")
  1357. }
  1358. return sjson.Set(jsonStr, path, strings.ReplaceAll(current.String(), from, to))
  1359. }
  1360. func regexReplaceStringValue(jsonStr, path, pattern, replacement string) (string, error) {
  1361. current := gjson.Get(jsonStr, path)
  1362. if current.Type != gjson.String {
  1363. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  1364. }
  1365. if pattern == "" {
  1366. return jsonStr, fmt.Errorf("regex pattern is required")
  1367. }
  1368. re, err := regexp.Compile(pattern)
  1369. if err != nil {
  1370. return jsonStr, err
  1371. }
  1372. return sjson.Set(jsonStr, path, re.ReplaceAllString(current.String(), replacement))
  1373. }
  1374. type pruneObjectsOptions struct {
  1375. conditions []ConditionOperation
  1376. logic string
  1377. recursive bool
  1378. }
  1379. func pruneObjects(jsonStr, path, contextJSON string, value interface{}) (string, error) {
  1380. options, err := parsePruneObjectsOptions(value)
  1381. if err != nil {
  1382. return "", err
  1383. }
  1384. if path == "" {
  1385. var root interface{}
  1386. if err := common.Unmarshal([]byte(jsonStr), &root); err != nil {
  1387. return "", err
  1388. }
  1389. cleaned, _, err := pruneObjectsNode(root, options, contextJSON, true)
  1390. if err != nil {
  1391. return "", err
  1392. }
  1393. cleanedBytes, err := common.Marshal(cleaned)
  1394. if err != nil {
  1395. return "", err
  1396. }
  1397. return string(cleanedBytes), nil
  1398. }
  1399. target := gjson.Get(jsonStr, path)
  1400. if !target.Exists() {
  1401. return jsonStr, nil
  1402. }
  1403. var targetNode interface{}
  1404. if target.Type == gjson.JSON {
  1405. if err := common.Unmarshal([]byte(target.Raw), &targetNode); err != nil {
  1406. return "", err
  1407. }
  1408. } else {
  1409. targetNode = target.Value()
  1410. }
  1411. cleaned, _, err := pruneObjectsNode(targetNode, options, contextJSON, true)
  1412. if err != nil {
  1413. return "", err
  1414. }
  1415. cleanedBytes, err := common.Marshal(cleaned)
  1416. if err != nil {
  1417. return "", err
  1418. }
  1419. return sjson.SetRaw(jsonStr, path, string(cleanedBytes))
  1420. }
  1421. func parsePruneObjectsOptions(value interface{}) (pruneObjectsOptions, error) {
  1422. opts := pruneObjectsOptions{
  1423. logic: "AND",
  1424. recursive: true,
  1425. }
  1426. switch raw := value.(type) {
  1427. case nil:
  1428. return opts, fmt.Errorf("prune_objects value is required")
  1429. case string:
  1430. v := strings.TrimSpace(raw)
  1431. if v == "" {
  1432. return opts, fmt.Errorf("prune_objects value is required")
  1433. }
  1434. opts.conditions = []ConditionOperation{
  1435. {
  1436. Path: "type",
  1437. Mode: "full",
  1438. Value: v,
  1439. },
  1440. }
  1441. case map[string]interface{}:
  1442. if logic, ok := raw["logic"].(string); ok && strings.TrimSpace(logic) != "" {
  1443. opts.logic = logic
  1444. }
  1445. if recursive, ok := raw["recursive"].(bool); ok {
  1446. opts.recursive = recursive
  1447. }
  1448. if condRaw, exists := raw["conditions"]; exists {
  1449. conditions, err := parseConditionOperations(condRaw)
  1450. if err != nil {
  1451. return opts, err
  1452. }
  1453. opts.conditions = append(opts.conditions, conditions...)
  1454. }
  1455. if whereRaw, exists := raw["where"]; exists {
  1456. whereMap, ok := whereRaw.(map[string]interface{})
  1457. if !ok {
  1458. return opts, fmt.Errorf("prune_objects where must be object")
  1459. }
  1460. for key, val := range whereMap {
  1461. key = strings.TrimSpace(key)
  1462. if key == "" {
  1463. continue
  1464. }
  1465. opts.conditions = append(opts.conditions, ConditionOperation{
  1466. Path: key,
  1467. Mode: "full",
  1468. Value: val,
  1469. })
  1470. }
  1471. }
  1472. if matchType, exists := raw["type"]; exists {
  1473. opts.conditions = append(opts.conditions, ConditionOperation{
  1474. Path: "type",
  1475. Mode: "full",
  1476. Value: matchType,
  1477. })
  1478. }
  1479. default:
  1480. return opts, fmt.Errorf("prune_objects value must be string or object")
  1481. }
  1482. if len(opts.conditions) == 0 {
  1483. return opts, fmt.Errorf("prune_objects conditions are required")
  1484. }
  1485. return opts, nil
  1486. }
  1487. func parseConditionOperations(raw interface{}) ([]ConditionOperation, error) {
  1488. switch typed := raw.(type) {
  1489. case map[string]interface{}:
  1490. entries := lo.Entries(typed)
  1491. conditions := lo.FilterMap(entries, func(item lo.Entry[string, interface{}], _ int) (ConditionOperation, bool) {
  1492. path := strings.TrimSpace(item.Key)
  1493. if path == "" {
  1494. return ConditionOperation{}, false
  1495. }
  1496. return ConditionOperation{
  1497. Path: path,
  1498. Mode: "full",
  1499. Value: item.Value,
  1500. }, true
  1501. })
  1502. if len(conditions) == 0 {
  1503. return nil, fmt.Errorf("conditions object must contain at least one key")
  1504. }
  1505. return conditions, nil
  1506. case []interface{}:
  1507. items := typed
  1508. result := make([]ConditionOperation, 0, len(items))
  1509. for _, item := range items {
  1510. itemMap, ok := item.(map[string]interface{})
  1511. if !ok {
  1512. return nil, fmt.Errorf("condition must be object")
  1513. }
  1514. path, _ := itemMap["path"].(string)
  1515. mode, _ := itemMap["mode"].(string)
  1516. if strings.TrimSpace(path) == "" || strings.TrimSpace(mode) == "" {
  1517. return nil, fmt.Errorf("condition path/mode is required")
  1518. }
  1519. condition := ConditionOperation{
  1520. Path: path,
  1521. Mode: mode,
  1522. }
  1523. if value, exists := itemMap["value"]; exists {
  1524. condition.Value = value
  1525. }
  1526. if invert, ok := itemMap["invert"].(bool); ok {
  1527. condition.Invert = invert
  1528. }
  1529. if passMissingKey, ok := itemMap["pass_missing_key"].(bool); ok {
  1530. condition.PassMissingKey = passMissingKey
  1531. }
  1532. result = append(result, condition)
  1533. }
  1534. return result, nil
  1535. default:
  1536. return nil, fmt.Errorf("conditions must be an array or object")
  1537. }
  1538. }
  1539. func pruneObjectsNode(node interface{}, options pruneObjectsOptions, contextJSON string, isRoot bool) (interface{}, bool, error) {
  1540. switch value := node.(type) {
  1541. case []interface{}:
  1542. result := make([]interface{}, 0, len(value))
  1543. for _, item := range value {
  1544. next, drop, err := pruneObjectsNode(item, options, contextJSON, false)
  1545. if err != nil {
  1546. return nil, false, err
  1547. }
  1548. if drop {
  1549. continue
  1550. }
  1551. result = append(result, next)
  1552. }
  1553. return result, false, nil
  1554. case map[string]interface{}:
  1555. shouldDrop, err := shouldPruneObject(value, options, contextJSON)
  1556. if err != nil {
  1557. return nil, false, err
  1558. }
  1559. if shouldDrop && !isRoot {
  1560. return nil, true, nil
  1561. }
  1562. if !options.recursive {
  1563. return value, false, nil
  1564. }
  1565. for key, child := range value {
  1566. next, drop, err := pruneObjectsNode(child, options, contextJSON, false)
  1567. if err != nil {
  1568. return nil, false, err
  1569. }
  1570. if drop {
  1571. delete(value, key)
  1572. continue
  1573. }
  1574. value[key] = next
  1575. }
  1576. return value, false, nil
  1577. default:
  1578. return node, false, nil
  1579. }
  1580. }
  1581. func shouldPruneObject(node map[string]interface{}, options pruneObjectsOptions, contextJSON string) (bool, error) {
  1582. nodeBytes, err := common.Marshal(node)
  1583. if err != nil {
  1584. return false, err
  1585. }
  1586. return checkConditions(string(nodeBytes), contextJSON, options.conditions, options.logic)
  1587. }
  1588. func mergeObjects(jsonStr, path string, value interface{}, keepOrigin bool) (string, error) {
  1589. current := gjson.Get(jsonStr, path)
  1590. var currentMap, newMap map[string]interface{}
  1591. // 解析当前值
  1592. if err := common.Unmarshal([]byte(current.Raw), &currentMap); err != nil {
  1593. return "", err
  1594. }
  1595. // 解析新值
  1596. switch v := value.(type) {
  1597. case map[string]interface{}:
  1598. newMap = v
  1599. default:
  1600. jsonBytes, _ := common.Marshal(v)
  1601. if err := common.Unmarshal(jsonBytes, &newMap); err != nil {
  1602. return "", err
  1603. }
  1604. }
  1605. // 合并
  1606. result := make(map[string]interface{})
  1607. for k, v := range currentMap {
  1608. result[k] = v
  1609. }
  1610. for k, v := range newMap {
  1611. if !keepOrigin || result[k] == nil {
  1612. result[k] = v
  1613. }
  1614. }
  1615. return sjson.Set(jsonStr, path, result)
  1616. }
  1617. // BuildParamOverrideContext 提供 ApplyParamOverride 可用的上下文信息。
  1618. // 目前内置以下字段:
  1619. // - upstream_model/model:始终为通道映射后的上游模型名。
  1620. // - original_model:请求最初指定的模型名。
  1621. // - request_path:请求路径
  1622. // - is_channel_test:是否为渠道测试请求(同 is_test)。
  1623. func BuildParamOverrideContext(info *RelayInfo) map[string]interface{} {
  1624. if info == nil {
  1625. return nil
  1626. }
  1627. ctx := make(map[string]interface{})
  1628. if info.ChannelMeta != nil && info.ChannelMeta.UpstreamModelName != "" {
  1629. ctx["model"] = info.ChannelMeta.UpstreamModelName
  1630. ctx["upstream_model"] = info.ChannelMeta.UpstreamModelName
  1631. }
  1632. if info.OriginModelName != "" {
  1633. ctx["original_model"] = info.OriginModelName
  1634. if _, exists := ctx["model"]; !exists {
  1635. ctx["model"] = info.OriginModelName
  1636. }
  1637. }
  1638. if info.RequestURLPath != "" {
  1639. requestPath := info.RequestURLPath
  1640. if requestPath != "" {
  1641. ctx["request_path"] = requestPath
  1642. }
  1643. }
  1644. ctx[paramOverrideContextRequestHeaders] = buildRequestHeadersContext(info.RequestHeaders)
  1645. headerOverrideSource := GetEffectiveHeaderOverride(info)
  1646. ctx[paramOverrideContextHeaderOverride] = sanitizeHeaderOverrideMap(headerOverrideSource)
  1647. ctx["retry_index"] = info.RetryIndex
  1648. ctx["is_retry"] = info.RetryIndex > 0
  1649. ctx["retry"] = map[string]interface{}{
  1650. "index": info.RetryIndex,
  1651. "is_retry": info.RetryIndex > 0,
  1652. }
  1653. if info.LastError != nil {
  1654. code := string(info.LastError.GetErrorCode())
  1655. errorType := string(info.LastError.GetErrorType())
  1656. lastError := map[string]interface{}{
  1657. "status_code": info.LastError.StatusCode,
  1658. "message": info.LastError.Error(),
  1659. "code": code,
  1660. "error_code": code,
  1661. "type": errorType,
  1662. "error_type": errorType,
  1663. "skip_retry": types.IsSkipRetryError(info.LastError),
  1664. }
  1665. ctx["last_error"] = lastError
  1666. ctx["last_error_status_code"] = info.LastError.StatusCode
  1667. ctx["last_error_message"] = info.LastError.Error()
  1668. ctx["last_error_code"] = code
  1669. ctx["last_error_type"] = errorType
  1670. }
  1671. ctx["is_channel_test"] = info.IsChannelTest
  1672. return ctx
  1673. }