override.go 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  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. sourceValue, exists := getHeaderValueFromContext(context, headerName)
  771. if !exists {
  772. return "", false, nil
  773. }
  774. sourceTokens := splitHeaderListValue(sourceValue)
  775. if len(sourceTokens) == 0 {
  776. return "", false, nil
  777. }
  778. wildcardValue, hasWildcard := mapping["*"]
  779. resultTokens := make([]string, 0, len(sourceTokens))
  780. for _, token := range sourceTokens {
  781. replacementRaw, hasReplacement := mapping[token]
  782. if !hasReplacement && hasWildcard {
  783. replacementRaw = wildcardValue
  784. hasReplacement = true
  785. }
  786. if !hasReplacement {
  787. resultTokens = append(resultTokens, token)
  788. continue
  789. }
  790. replacementTokens, err := parseHeaderReplacementTokens(replacementRaw)
  791. if err != nil {
  792. return "", false, err
  793. }
  794. resultTokens = append(resultTokens, replacementTokens...)
  795. }
  796. resultTokens = lo.Uniq(resultTokens)
  797. if len(resultTokens) == 0 {
  798. return "", false, nil
  799. }
  800. return strings.Join(resultTokens, ","), true, nil
  801. }
  802. func parseHeaderReplacementTokens(value interface{}) ([]string, error) {
  803. switch raw := value.(type) {
  804. case nil:
  805. return nil, nil
  806. case string:
  807. return splitHeaderListValue(raw), nil
  808. case []string:
  809. tokens := make([]string, 0, len(raw))
  810. for _, item := range raw {
  811. tokens = append(tokens, splitHeaderListValue(item)...)
  812. }
  813. return lo.Uniq(tokens), nil
  814. case []interface{}:
  815. tokens := make([]string, 0, len(raw))
  816. for _, item := range raw {
  817. itemTokens, err := parseHeaderReplacementTokens(item)
  818. if err != nil {
  819. return nil, err
  820. }
  821. tokens = append(tokens, itemTokens...)
  822. }
  823. return lo.Uniq(tokens), nil
  824. case map[string]interface{}, map[string]string:
  825. return nil, fmt.Errorf("header replacement value must be string, array or null")
  826. default:
  827. token := strings.TrimSpace(fmt.Sprintf("%v", raw))
  828. if token == "" {
  829. return nil, nil
  830. }
  831. return []string{token}, nil
  832. }
  833. }
  834. func splitHeaderListValue(raw string) []string {
  835. items := strings.Split(raw, ",")
  836. return lo.FilterMap(items, func(item string, _ int) (string, bool) {
  837. token := strings.TrimSpace(item)
  838. if token == "" {
  839. return "", false
  840. }
  841. return token, true
  842. })
  843. }
  844. func copyHeaderInContext(context map[string]interface{}, fromHeader, toHeader string, keepOrigin bool) error {
  845. fromHeader = normalizeHeaderContextKey(fromHeader)
  846. toHeader = normalizeHeaderContextKey(toHeader)
  847. if fromHeader == "" || toHeader == "" {
  848. return fmt.Errorf("copy_header from/to is required")
  849. }
  850. value, exists := getHeaderValueFromContext(context, fromHeader)
  851. if !exists {
  852. return fmt.Errorf("%w: %s", errSourceHeaderNotFound, fromHeader)
  853. }
  854. return setHeaderOverrideInContext(context, toHeader, value, keepOrigin)
  855. }
  856. func moveHeaderInContext(context map[string]interface{}, fromHeader, toHeader string, keepOrigin bool) error {
  857. fromHeader = normalizeHeaderContextKey(fromHeader)
  858. toHeader = normalizeHeaderContextKey(toHeader)
  859. if fromHeader == "" || toHeader == "" {
  860. return fmt.Errorf("move_header from/to is required")
  861. }
  862. if err := copyHeaderInContext(context, fromHeader, toHeader, keepOrigin); err != nil {
  863. return err
  864. }
  865. if strings.EqualFold(fromHeader, toHeader) {
  866. return nil
  867. }
  868. return deleteHeaderOverrideInContext(context, fromHeader)
  869. }
  870. func deleteHeaderOverrideInContext(context map[string]interface{}, headerName string) error {
  871. headerName = normalizeHeaderContextKey(headerName)
  872. if headerName == "" {
  873. return fmt.Errorf("header name is required")
  874. }
  875. rawHeaders := ensureMapKeyInContext(context, paramOverrideContextHeaderOverride)
  876. delete(rawHeaders, headerName)
  877. return nil
  878. }
  879. func parseHeaderPassThroughNames(value interface{}) ([]string, error) {
  880. normalizeNames := func(values []string) []string {
  881. names := lo.FilterMap(values, func(item string, _ int) (string, bool) {
  882. headerName := normalizeHeaderContextKey(item)
  883. if headerName == "" {
  884. return "", false
  885. }
  886. return headerName, true
  887. })
  888. return lo.Uniq(names)
  889. }
  890. switch raw := value.(type) {
  891. case nil:
  892. return nil, fmt.Errorf("pass_headers value is required")
  893. case string:
  894. trimmed := strings.TrimSpace(raw)
  895. if trimmed == "" {
  896. return nil, fmt.Errorf("pass_headers value is required")
  897. }
  898. if strings.HasPrefix(trimmed, "[") || strings.HasPrefix(trimmed, "{") {
  899. var parsed interface{}
  900. if err := common.UnmarshalJsonStr(trimmed, &parsed); err == nil {
  901. return parseHeaderPassThroughNames(parsed)
  902. }
  903. }
  904. names := normalizeNames(strings.Split(trimmed, ","))
  905. if len(names) == 0 {
  906. return nil, fmt.Errorf("pass_headers value is invalid")
  907. }
  908. return names, nil
  909. case []interface{}:
  910. names := lo.FilterMap(raw, func(item interface{}, _ int) (string, bool) {
  911. headerName := normalizeHeaderContextKey(fmt.Sprintf("%v", item))
  912. if headerName == "" {
  913. return "", false
  914. }
  915. return headerName, true
  916. })
  917. names = lo.Uniq(names)
  918. if len(names) == 0 {
  919. return nil, fmt.Errorf("pass_headers value is invalid")
  920. }
  921. return names, nil
  922. case []string:
  923. names := lo.FilterMap(raw, func(item string, _ int) (string, bool) {
  924. headerName := normalizeHeaderContextKey(item)
  925. if headerName == "" {
  926. return "", false
  927. }
  928. return headerName, true
  929. })
  930. names = lo.Uniq(names)
  931. if len(names) == 0 {
  932. return nil, fmt.Errorf("pass_headers value is invalid")
  933. }
  934. return names, nil
  935. case map[string]interface{}:
  936. candidates := make([]string, 0, 8)
  937. if headersRaw, ok := raw["headers"]; ok {
  938. names, err := parseHeaderPassThroughNames(headersRaw)
  939. if err == nil {
  940. candidates = append(candidates, names...)
  941. }
  942. }
  943. if namesRaw, ok := raw["names"]; ok {
  944. names, err := parseHeaderPassThroughNames(namesRaw)
  945. if err == nil {
  946. candidates = append(candidates, names...)
  947. }
  948. }
  949. if headerRaw, ok := raw["header"]; ok {
  950. names, err := parseHeaderPassThroughNames(headerRaw)
  951. if err == nil {
  952. candidates = append(candidates, names...)
  953. }
  954. }
  955. names := normalizeNames(candidates)
  956. if len(names) == 0 {
  957. return nil, fmt.Errorf("pass_headers value is invalid")
  958. }
  959. return names, nil
  960. default:
  961. return nil, fmt.Errorf("pass_headers value must be string, array or object")
  962. }
  963. }
  964. type syncTarget struct {
  965. kind string
  966. key string
  967. }
  968. func parseSyncTarget(spec string) (syncTarget, error) {
  969. raw := strings.TrimSpace(spec)
  970. if raw == "" {
  971. return syncTarget{}, fmt.Errorf("sync_fields target is required")
  972. }
  973. idx := strings.Index(raw, ":")
  974. if idx < 0 {
  975. // Backward compatibility: treat bare value as JSON path.
  976. return syncTarget{
  977. kind: "json",
  978. key: raw,
  979. }, nil
  980. }
  981. kind := strings.ToLower(strings.TrimSpace(raw[:idx]))
  982. key := strings.TrimSpace(raw[idx+1:])
  983. if key == "" {
  984. return syncTarget{}, fmt.Errorf("sync_fields target key is required: %s", raw)
  985. }
  986. switch kind {
  987. case "json", "body":
  988. return syncTarget{
  989. kind: "json",
  990. key: key,
  991. }, nil
  992. case "header":
  993. return syncTarget{
  994. kind: "header",
  995. key: key,
  996. }, nil
  997. default:
  998. return syncTarget{}, fmt.Errorf("sync_fields target prefix is invalid: %s", raw)
  999. }
  1000. }
  1001. func readSyncTargetValue(jsonStr string, context map[string]interface{}, target syncTarget) (interface{}, bool, error) {
  1002. switch target.kind {
  1003. case "json":
  1004. path := processNegativeIndex(jsonStr, target.key)
  1005. value := gjson.Get(jsonStr, path)
  1006. if !value.Exists() || value.Type == gjson.Null {
  1007. return nil, false, nil
  1008. }
  1009. if value.Type == gjson.String && strings.TrimSpace(value.String()) == "" {
  1010. return nil, false, nil
  1011. }
  1012. return value.Value(), true, nil
  1013. case "header":
  1014. value, ok := getHeaderValueFromContext(context, target.key)
  1015. if !ok || strings.TrimSpace(value) == "" {
  1016. return nil, false, nil
  1017. }
  1018. return value, true, nil
  1019. default:
  1020. return nil, false, fmt.Errorf("unsupported sync_fields target kind: %s", target.kind)
  1021. }
  1022. }
  1023. func writeSyncTargetValue(jsonStr string, context map[string]interface{}, target syncTarget, value interface{}) (string, error) {
  1024. switch target.kind {
  1025. case "json":
  1026. path := processNegativeIndex(jsonStr, target.key)
  1027. nextJSON, err := sjson.Set(jsonStr, path, value)
  1028. if err != nil {
  1029. return "", err
  1030. }
  1031. return nextJSON, nil
  1032. case "header":
  1033. if err := setHeaderOverrideInContext(context, target.key, value, false); err != nil {
  1034. return "", err
  1035. }
  1036. return jsonStr, nil
  1037. default:
  1038. return "", fmt.Errorf("unsupported sync_fields target kind: %s", target.kind)
  1039. }
  1040. }
  1041. func syncFieldsBetweenTargets(jsonStr string, context map[string]interface{}, fromSpec string, toSpec string) (string, error) {
  1042. fromTarget, err := parseSyncTarget(fromSpec)
  1043. if err != nil {
  1044. return "", err
  1045. }
  1046. toTarget, err := parseSyncTarget(toSpec)
  1047. if err != nil {
  1048. return "", err
  1049. }
  1050. fromValue, fromExists, err := readSyncTargetValue(jsonStr, context, fromTarget)
  1051. if err != nil {
  1052. return "", err
  1053. }
  1054. toValue, toExists, err := readSyncTargetValue(jsonStr, context, toTarget)
  1055. if err != nil {
  1056. return "", err
  1057. }
  1058. // If one side exists and the other side is missing, sync the missing side.
  1059. if fromExists && !toExists {
  1060. return writeSyncTargetValue(jsonStr, context, toTarget, fromValue)
  1061. }
  1062. if toExists && !fromExists {
  1063. return writeSyncTargetValue(jsonStr, context, fromTarget, toValue)
  1064. }
  1065. return jsonStr, nil
  1066. }
  1067. func ensureMapKeyInContext(context map[string]interface{}, key string) map[string]interface{} {
  1068. if context == nil {
  1069. return map[string]interface{}{}
  1070. }
  1071. if existing, ok := context[key]; ok {
  1072. if mapVal, ok := existing.(map[string]interface{}); ok {
  1073. return mapVal
  1074. }
  1075. }
  1076. result := make(map[string]interface{})
  1077. context[key] = result
  1078. return result
  1079. }
  1080. func getHeaderValueFromContext(context map[string]interface{}, headerName string) (string, bool) {
  1081. headerName = normalizeHeaderContextKey(headerName)
  1082. if headerName == "" {
  1083. return "", false
  1084. }
  1085. for _, key := range []string{paramOverrideContextHeaderOverride, paramOverrideContextRequestHeaders} {
  1086. source := ensureMapKeyInContext(context, key)
  1087. raw, ok := source[headerName]
  1088. if !ok {
  1089. continue
  1090. }
  1091. value := strings.TrimSpace(fmt.Sprintf("%v", raw))
  1092. if value != "" {
  1093. return value, true
  1094. }
  1095. }
  1096. return "", false
  1097. }
  1098. func normalizeHeaderContextKey(key string) string {
  1099. return strings.TrimSpace(strings.ToLower(key))
  1100. }
  1101. func buildRequestHeadersContext(headers map[string]string) map[string]interface{} {
  1102. if len(headers) == 0 {
  1103. return map[string]interface{}{}
  1104. }
  1105. entries := lo.Entries(headers)
  1106. normalizedEntries := lo.FilterMap(entries, func(item lo.Entry[string, string], _ int) (lo.Entry[string, string], bool) {
  1107. normalized := normalizeHeaderContextKey(item.Key)
  1108. value := strings.TrimSpace(item.Value)
  1109. if normalized == "" || value == "" {
  1110. return lo.Entry[string, string]{}, false
  1111. }
  1112. return lo.Entry[string, string]{Key: normalized, Value: value}, true
  1113. })
  1114. return lo.SliceToMap(normalizedEntries, func(item lo.Entry[string, string]) (string, interface{}) {
  1115. return item.Key, item.Value
  1116. })
  1117. }
  1118. func syncRuntimeHeaderOverrideFromContext(info *RelayInfo, context map[string]interface{}) {
  1119. if info == nil || context == nil {
  1120. return
  1121. }
  1122. raw, exists := context[paramOverrideContextHeaderOverride]
  1123. if !exists {
  1124. return
  1125. }
  1126. rawMap, ok := raw.(map[string]interface{})
  1127. if !ok {
  1128. return
  1129. }
  1130. info.RuntimeHeadersOverride = sanitizeHeaderOverrideMap(rawMap)
  1131. info.UseRuntimeHeadersOverride = true
  1132. }
  1133. func moveValue(jsonStr, fromPath, toPath string) (string, error) {
  1134. sourceValue := gjson.Get(jsonStr, fromPath)
  1135. if !sourceValue.Exists() {
  1136. return jsonStr, fmt.Errorf("source path does not exist: %s", fromPath)
  1137. }
  1138. result, err := sjson.Set(jsonStr, toPath, sourceValue.Value())
  1139. if err != nil {
  1140. return "", err
  1141. }
  1142. return sjson.Delete(result, fromPath)
  1143. }
  1144. func copyValue(jsonStr, fromPath, toPath string) (string, error) {
  1145. sourceValue := gjson.Get(jsonStr, fromPath)
  1146. if !sourceValue.Exists() {
  1147. return jsonStr, fmt.Errorf("source path does not exist: %s", fromPath)
  1148. }
  1149. return sjson.Set(jsonStr, toPath, sourceValue.Value())
  1150. }
  1151. func isPathBasedOperation(mode string) bool {
  1152. switch mode {
  1153. case "delete", "set", "prepend", "append", "trim_prefix", "trim_suffix", "ensure_prefix", "ensure_suffix", "trim_space", "to_lower", "to_upper", "replace", "regex_replace", "prune_objects":
  1154. return true
  1155. default:
  1156. return false
  1157. }
  1158. }
  1159. func resolveOperationPaths(jsonStr, path string) ([]string, error) {
  1160. if !strings.Contains(path, "*") {
  1161. return []string{path}, nil
  1162. }
  1163. return expandWildcardPaths(jsonStr, path)
  1164. }
  1165. func expandWildcardPaths(jsonStr, path string) ([]string, error) {
  1166. var root interface{}
  1167. if err := common.Unmarshal([]byte(jsonStr), &root); err != nil {
  1168. return nil, err
  1169. }
  1170. segments := strings.Split(path, ".")
  1171. paths := collectWildcardPaths(root, segments, nil)
  1172. return lo.Uniq(paths), nil
  1173. }
  1174. func collectWildcardPaths(node interface{}, segments []string, prefix []string) []string {
  1175. if len(segments) == 0 {
  1176. return []string{strings.Join(prefix, ".")}
  1177. }
  1178. segment := strings.TrimSpace(segments[0])
  1179. if segment == "" {
  1180. return nil
  1181. }
  1182. isLast := len(segments) == 1
  1183. if segment == "*" {
  1184. switch typed := node.(type) {
  1185. case map[string]interface{}:
  1186. keys := lo.Keys(typed)
  1187. sort.Strings(keys)
  1188. return lo.FlatMap(keys, func(key string, _ int) []string {
  1189. return collectWildcardPaths(typed[key], segments[1:], append(prefix, key))
  1190. })
  1191. case []interface{}:
  1192. return lo.FlatMap(lo.Range(len(typed)), func(index int, _ int) []string {
  1193. return collectWildcardPaths(typed[index], segments[1:], append(prefix, strconv.Itoa(index)))
  1194. })
  1195. default:
  1196. return nil
  1197. }
  1198. }
  1199. switch typed := node.(type) {
  1200. case map[string]interface{}:
  1201. if isLast {
  1202. return []string{strings.Join(append(prefix, segment), ".")}
  1203. }
  1204. next, exists := typed[segment]
  1205. if !exists {
  1206. return nil
  1207. }
  1208. return collectWildcardPaths(next, segments[1:], append(prefix, segment))
  1209. case []interface{}:
  1210. index, err := strconv.Atoi(segment)
  1211. if err != nil || index < 0 || index >= len(typed) {
  1212. return nil
  1213. }
  1214. if isLast {
  1215. return []string{strings.Join(append(prefix, segment), ".")}
  1216. }
  1217. return collectWildcardPaths(typed[index], segments[1:], append(prefix, segment))
  1218. default:
  1219. return nil
  1220. }
  1221. }
  1222. func deleteValue(jsonStr, path string) (string, error) {
  1223. if strings.TrimSpace(path) == "" {
  1224. return jsonStr, nil
  1225. }
  1226. return sjson.Delete(jsonStr, path)
  1227. }
  1228. func modifyValue(jsonStr, path string, value interface{}, keepOrigin, isPrepend bool) (string, error) {
  1229. current := gjson.Get(jsonStr, path)
  1230. switch {
  1231. case current.IsArray():
  1232. return modifyArray(jsonStr, path, value, isPrepend)
  1233. case current.Type == gjson.String:
  1234. return modifyString(jsonStr, path, value, isPrepend)
  1235. case current.Type == gjson.JSON:
  1236. return mergeObjects(jsonStr, path, value, keepOrigin)
  1237. }
  1238. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  1239. }
  1240. func modifyArray(jsonStr, path string, value interface{}, isPrepend bool) (string, error) {
  1241. current := gjson.Get(jsonStr, path)
  1242. var newArray []interface{}
  1243. // 添加新值
  1244. addValue := func() {
  1245. if arr, ok := value.([]interface{}); ok {
  1246. newArray = append(newArray, arr...)
  1247. } else {
  1248. newArray = append(newArray, value)
  1249. }
  1250. }
  1251. // 添加原值
  1252. addOriginal := func() {
  1253. current.ForEach(func(_, val gjson.Result) bool {
  1254. newArray = append(newArray, val.Value())
  1255. return true
  1256. })
  1257. }
  1258. if isPrepend {
  1259. addValue()
  1260. addOriginal()
  1261. } else {
  1262. addOriginal()
  1263. addValue()
  1264. }
  1265. return sjson.Set(jsonStr, path, newArray)
  1266. }
  1267. func modifyString(jsonStr, path string, value interface{}, isPrepend bool) (string, error) {
  1268. current := gjson.Get(jsonStr, path)
  1269. valueStr := fmt.Sprintf("%v", value)
  1270. var newStr string
  1271. if isPrepend {
  1272. newStr = valueStr + current.String()
  1273. } else {
  1274. newStr = current.String() + valueStr
  1275. }
  1276. return sjson.Set(jsonStr, path, newStr)
  1277. }
  1278. func trimStringValue(jsonStr, path string, value interface{}, isPrefix bool) (string, error) {
  1279. current := gjson.Get(jsonStr, path)
  1280. if current.Type != gjson.String {
  1281. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  1282. }
  1283. if value == nil {
  1284. return jsonStr, fmt.Errorf("trim value is required")
  1285. }
  1286. valueStr := fmt.Sprintf("%v", value)
  1287. var newStr string
  1288. if isPrefix {
  1289. newStr = strings.TrimPrefix(current.String(), valueStr)
  1290. } else {
  1291. newStr = strings.TrimSuffix(current.String(), valueStr)
  1292. }
  1293. return sjson.Set(jsonStr, path, newStr)
  1294. }
  1295. func ensureStringAffix(jsonStr, path string, value interface{}, isPrefix bool) (string, error) {
  1296. current := gjson.Get(jsonStr, path)
  1297. if current.Type != gjson.String {
  1298. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  1299. }
  1300. if value == nil {
  1301. return jsonStr, fmt.Errorf("ensure value is required")
  1302. }
  1303. valueStr := fmt.Sprintf("%v", value)
  1304. if valueStr == "" {
  1305. return jsonStr, fmt.Errorf("ensure value is required")
  1306. }
  1307. currentStr := current.String()
  1308. if isPrefix {
  1309. if strings.HasPrefix(currentStr, valueStr) {
  1310. return jsonStr, nil
  1311. }
  1312. return sjson.Set(jsonStr, path, valueStr+currentStr)
  1313. }
  1314. if strings.HasSuffix(currentStr, valueStr) {
  1315. return jsonStr, nil
  1316. }
  1317. return sjson.Set(jsonStr, path, currentStr+valueStr)
  1318. }
  1319. func transformStringValue(jsonStr, path string, transform func(string) string) (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. return sjson.Set(jsonStr, path, transform(current.String()))
  1325. }
  1326. func replaceStringValue(jsonStr, path, from, to string) (string, error) {
  1327. current := gjson.Get(jsonStr, path)
  1328. if current.Type != gjson.String {
  1329. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  1330. }
  1331. if from == "" {
  1332. return jsonStr, fmt.Errorf("replace from is required")
  1333. }
  1334. return sjson.Set(jsonStr, path, strings.ReplaceAll(current.String(), from, to))
  1335. }
  1336. func regexReplaceStringValue(jsonStr, path, pattern, replacement string) (string, error) {
  1337. current := gjson.Get(jsonStr, path)
  1338. if current.Type != gjson.String {
  1339. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  1340. }
  1341. if pattern == "" {
  1342. return jsonStr, fmt.Errorf("regex pattern is required")
  1343. }
  1344. re, err := regexp.Compile(pattern)
  1345. if err != nil {
  1346. return jsonStr, err
  1347. }
  1348. return sjson.Set(jsonStr, path, re.ReplaceAllString(current.String(), replacement))
  1349. }
  1350. type pruneObjectsOptions struct {
  1351. conditions []ConditionOperation
  1352. logic string
  1353. recursive bool
  1354. }
  1355. func pruneObjects(jsonStr, path, contextJSON string, value interface{}) (string, error) {
  1356. options, err := parsePruneObjectsOptions(value)
  1357. if err != nil {
  1358. return "", err
  1359. }
  1360. if path == "" {
  1361. var root interface{}
  1362. if err := common.Unmarshal([]byte(jsonStr), &root); err != nil {
  1363. return "", err
  1364. }
  1365. cleaned, _, err := pruneObjectsNode(root, options, contextJSON, true)
  1366. if err != nil {
  1367. return "", err
  1368. }
  1369. cleanedBytes, err := common.Marshal(cleaned)
  1370. if err != nil {
  1371. return "", err
  1372. }
  1373. return string(cleanedBytes), nil
  1374. }
  1375. target := gjson.Get(jsonStr, path)
  1376. if !target.Exists() {
  1377. return jsonStr, nil
  1378. }
  1379. var targetNode interface{}
  1380. if target.Type == gjson.JSON {
  1381. if err := common.Unmarshal([]byte(target.Raw), &targetNode); err != nil {
  1382. return "", err
  1383. }
  1384. } else {
  1385. targetNode = target.Value()
  1386. }
  1387. cleaned, _, err := pruneObjectsNode(targetNode, options, contextJSON, true)
  1388. if err != nil {
  1389. return "", err
  1390. }
  1391. cleanedBytes, err := common.Marshal(cleaned)
  1392. if err != nil {
  1393. return "", err
  1394. }
  1395. return sjson.SetRaw(jsonStr, path, string(cleanedBytes))
  1396. }
  1397. func parsePruneObjectsOptions(value interface{}) (pruneObjectsOptions, error) {
  1398. opts := pruneObjectsOptions{
  1399. logic: "AND",
  1400. recursive: true,
  1401. }
  1402. switch raw := value.(type) {
  1403. case nil:
  1404. return opts, fmt.Errorf("prune_objects value is required")
  1405. case string:
  1406. v := strings.TrimSpace(raw)
  1407. if v == "" {
  1408. return opts, fmt.Errorf("prune_objects value is required")
  1409. }
  1410. opts.conditions = []ConditionOperation{
  1411. {
  1412. Path: "type",
  1413. Mode: "full",
  1414. Value: v,
  1415. },
  1416. }
  1417. case map[string]interface{}:
  1418. if logic, ok := raw["logic"].(string); ok && strings.TrimSpace(logic) != "" {
  1419. opts.logic = logic
  1420. }
  1421. if recursive, ok := raw["recursive"].(bool); ok {
  1422. opts.recursive = recursive
  1423. }
  1424. if condRaw, exists := raw["conditions"]; exists {
  1425. conditions, err := parseConditionOperations(condRaw)
  1426. if err != nil {
  1427. return opts, err
  1428. }
  1429. opts.conditions = append(opts.conditions, conditions...)
  1430. }
  1431. if whereRaw, exists := raw["where"]; exists {
  1432. whereMap, ok := whereRaw.(map[string]interface{})
  1433. if !ok {
  1434. return opts, fmt.Errorf("prune_objects where must be object")
  1435. }
  1436. for key, val := range whereMap {
  1437. key = strings.TrimSpace(key)
  1438. if key == "" {
  1439. continue
  1440. }
  1441. opts.conditions = append(opts.conditions, ConditionOperation{
  1442. Path: key,
  1443. Mode: "full",
  1444. Value: val,
  1445. })
  1446. }
  1447. }
  1448. if matchType, exists := raw["type"]; exists {
  1449. opts.conditions = append(opts.conditions, ConditionOperation{
  1450. Path: "type",
  1451. Mode: "full",
  1452. Value: matchType,
  1453. })
  1454. }
  1455. default:
  1456. return opts, fmt.Errorf("prune_objects value must be string or object")
  1457. }
  1458. if len(opts.conditions) == 0 {
  1459. return opts, fmt.Errorf("prune_objects conditions are required")
  1460. }
  1461. return opts, nil
  1462. }
  1463. func parseConditionOperations(raw interface{}) ([]ConditionOperation, error) {
  1464. switch typed := raw.(type) {
  1465. case map[string]interface{}:
  1466. entries := lo.Entries(typed)
  1467. conditions := lo.FilterMap(entries, func(item lo.Entry[string, interface{}], _ int) (ConditionOperation, bool) {
  1468. path := strings.TrimSpace(item.Key)
  1469. if path == "" {
  1470. return ConditionOperation{}, false
  1471. }
  1472. return ConditionOperation{
  1473. Path: path,
  1474. Mode: "full",
  1475. Value: item.Value,
  1476. }, true
  1477. })
  1478. if len(conditions) == 0 {
  1479. return nil, fmt.Errorf("conditions object must contain at least one key")
  1480. }
  1481. return conditions, nil
  1482. case []interface{}:
  1483. items := typed
  1484. result := make([]ConditionOperation, 0, len(items))
  1485. for _, item := range items {
  1486. itemMap, ok := item.(map[string]interface{})
  1487. if !ok {
  1488. return nil, fmt.Errorf("condition must be object")
  1489. }
  1490. path, _ := itemMap["path"].(string)
  1491. mode, _ := itemMap["mode"].(string)
  1492. if strings.TrimSpace(path) == "" || strings.TrimSpace(mode) == "" {
  1493. return nil, fmt.Errorf("condition path/mode is required")
  1494. }
  1495. condition := ConditionOperation{
  1496. Path: path,
  1497. Mode: mode,
  1498. }
  1499. if value, exists := itemMap["value"]; exists {
  1500. condition.Value = value
  1501. }
  1502. if invert, ok := itemMap["invert"].(bool); ok {
  1503. condition.Invert = invert
  1504. }
  1505. if passMissingKey, ok := itemMap["pass_missing_key"].(bool); ok {
  1506. condition.PassMissingKey = passMissingKey
  1507. }
  1508. result = append(result, condition)
  1509. }
  1510. return result, nil
  1511. default:
  1512. return nil, fmt.Errorf("conditions must be an array or object")
  1513. }
  1514. }
  1515. func pruneObjectsNode(node interface{}, options pruneObjectsOptions, contextJSON string, isRoot bool) (interface{}, bool, error) {
  1516. switch value := node.(type) {
  1517. case []interface{}:
  1518. result := make([]interface{}, 0, len(value))
  1519. for _, item := range value {
  1520. next, drop, err := pruneObjectsNode(item, options, contextJSON, false)
  1521. if err != nil {
  1522. return nil, false, err
  1523. }
  1524. if drop {
  1525. continue
  1526. }
  1527. result = append(result, next)
  1528. }
  1529. return result, false, nil
  1530. case map[string]interface{}:
  1531. shouldDrop, err := shouldPruneObject(value, options, contextJSON)
  1532. if err != nil {
  1533. return nil, false, err
  1534. }
  1535. if shouldDrop && !isRoot {
  1536. return nil, true, nil
  1537. }
  1538. if !options.recursive {
  1539. return value, false, nil
  1540. }
  1541. for key, child := range value {
  1542. next, drop, err := pruneObjectsNode(child, options, contextJSON, false)
  1543. if err != nil {
  1544. return nil, false, err
  1545. }
  1546. if drop {
  1547. delete(value, key)
  1548. continue
  1549. }
  1550. value[key] = next
  1551. }
  1552. return value, false, nil
  1553. default:
  1554. return node, false, nil
  1555. }
  1556. }
  1557. func shouldPruneObject(node map[string]interface{}, options pruneObjectsOptions, contextJSON string) (bool, error) {
  1558. nodeBytes, err := common.Marshal(node)
  1559. if err != nil {
  1560. return false, err
  1561. }
  1562. return checkConditions(string(nodeBytes), contextJSON, options.conditions, options.logic)
  1563. }
  1564. func mergeObjects(jsonStr, path string, value interface{}, keepOrigin bool) (string, error) {
  1565. current := gjson.Get(jsonStr, path)
  1566. var currentMap, newMap map[string]interface{}
  1567. // 解析当前值
  1568. if err := common.Unmarshal([]byte(current.Raw), &currentMap); err != nil {
  1569. return "", err
  1570. }
  1571. // 解析新值
  1572. switch v := value.(type) {
  1573. case map[string]interface{}:
  1574. newMap = v
  1575. default:
  1576. jsonBytes, _ := common.Marshal(v)
  1577. if err := common.Unmarshal(jsonBytes, &newMap); err != nil {
  1578. return "", err
  1579. }
  1580. }
  1581. // 合并
  1582. result := make(map[string]interface{})
  1583. for k, v := range currentMap {
  1584. result[k] = v
  1585. }
  1586. for k, v := range newMap {
  1587. if !keepOrigin || result[k] == nil {
  1588. result[k] = v
  1589. }
  1590. }
  1591. return sjson.Set(jsonStr, path, result)
  1592. }
  1593. // BuildParamOverrideContext 提供 ApplyParamOverride 可用的上下文信息。
  1594. // 目前内置以下字段:
  1595. // - upstream_model/model:始终为通道映射后的上游模型名。
  1596. // - original_model:请求最初指定的模型名。
  1597. // - request_path:请求路径
  1598. // - is_channel_test:是否为渠道测试请求(同 is_test)。
  1599. func BuildParamOverrideContext(info *RelayInfo) map[string]interface{} {
  1600. if info == nil {
  1601. return nil
  1602. }
  1603. ctx := make(map[string]interface{})
  1604. if info.ChannelMeta != nil && info.ChannelMeta.UpstreamModelName != "" {
  1605. ctx["model"] = info.ChannelMeta.UpstreamModelName
  1606. ctx["upstream_model"] = info.ChannelMeta.UpstreamModelName
  1607. }
  1608. if info.OriginModelName != "" {
  1609. ctx["original_model"] = info.OriginModelName
  1610. if _, exists := ctx["model"]; !exists {
  1611. ctx["model"] = info.OriginModelName
  1612. }
  1613. }
  1614. if info.RequestURLPath != "" {
  1615. requestPath := info.RequestURLPath
  1616. if requestPath != "" {
  1617. ctx["request_path"] = requestPath
  1618. }
  1619. }
  1620. ctx[paramOverrideContextRequestHeaders] = buildRequestHeadersContext(info.RequestHeaders)
  1621. headerOverrideSource := GetEffectiveHeaderOverride(info)
  1622. ctx[paramOverrideContextHeaderOverride] = sanitizeHeaderOverrideMap(headerOverrideSource)
  1623. ctx["retry_index"] = info.RetryIndex
  1624. ctx["is_retry"] = info.RetryIndex > 0
  1625. ctx["retry"] = map[string]interface{}{
  1626. "index": info.RetryIndex,
  1627. "is_retry": info.RetryIndex > 0,
  1628. }
  1629. if info.LastError != nil {
  1630. code := string(info.LastError.GetErrorCode())
  1631. errorType := string(info.LastError.GetErrorType())
  1632. lastError := map[string]interface{}{
  1633. "status_code": info.LastError.StatusCode,
  1634. "message": info.LastError.Error(),
  1635. "code": code,
  1636. "error_code": code,
  1637. "type": errorType,
  1638. "error_type": errorType,
  1639. "skip_retry": types.IsSkipRetryError(info.LastError),
  1640. }
  1641. ctx["last_error"] = lastError
  1642. ctx["last_error_status_code"] = info.LastError.StatusCode
  1643. ctx["last_error_message"] = info.LastError.Error()
  1644. ctx["last_error_code"] = code
  1645. ctx["last_error_type"] = errorType
  1646. }
  1647. ctx["is_channel_test"] = info.IsChannelTest
  1648. return ctx
  1649. }