override.go 57 KB

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