override.go 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. package common
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "github.com/QuantumNous/new-api/common"
  10. "github.com/QuantumNous/new-api/types"
  11. "github.com/tidwall/gjson"
  12. "github.com/tidwall/sjson"
  13. )
  14. var negativeIndexRegexp = regexp.MustCompile(`\.(-\d+)`)
  15. type ConditionOperation struct {
  16. Path string `json:"path"` // JSON路径
  17. Mode string `json:"mode"` // full, prefix, suffix, contains, gt, gte, lt, lte
  18. Value interface{} `json:"value"` // 匹配的值
  19. Invert bool `json:"invert"` // 反选功能,true表示取反结果
  20. PassMissingKey bool `json:"pass_missing_key"` // 未获取到json key时的行为
  21. }
  22. type ParamOperation struct {
  23. Path string `json:"path"`
  24. 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
  25. Value interface{} `json:"value"`
  26. KeepOrigin bool `json:"keep_origin"`
  27. From string `json:"from,omitempty"`
  28. To string `json:"to,omitempty"`
  29. Conditions []ConditionOperation `json:"conditions,omitempty"` // 条件列表
  30. Logic string `json:"logic,omitempty"` // AND, OR (默认OR)
  31. }
  32. type ParamOverrideReturnError struct {
  33. Message string
  34. StatusCode int
  35. Code string
  36. Type string
  37. SkipRetry bool
  38. }
  39. func (e *ParamOverrideReturnError) Error() string {
  40. if e == nil {
  41. return "param override return error"
  42. }
  43. if e.Message == "" {
  44. return "param override return error"
  45. }
  46. return e.Message
  47. }
  48. func AsParamOverrideReturnError(err error) (*ParamOverrideReturnError, bool) {
  49. if err == nil {
  50. return nil, false
  51. }
  52. var target *ParamOverrideReturnError
  53. if errors.As(err, &target) {
  54. return target, true
  55. }
  56. return nil, false
  57. }
  58. func NewAPIErrorFromParamOverride(err *ParamOverrideReturnError) *types.NewAPIError {
  59. if err == nil {
  60. return types.NewError(
  61. errors.New("param override return error is nil"),
  62. types.ErrorCodeChannelParamOverrideInvalid,
  63. types.ErrOptionWithSkipRetry(),
  64. )
  65. }
  66. statusCode := err.StatusCode
  67. if statusCode < http.StatusContinue || statusCode > http.StatusNetworkAuthenticationRequired {
  68. statusCode = http.StatusBadRequest
  69. }
  70. errorCode := err.Code
  71. if strings.TrimSpace(errorCode) == "" {
  72. errorCode = string(types.ErrorCodeInvalidRequest)
  73. }
  74. errorType := err.Type
  75. if strings.TrimSpace(errorType) == "" {
  76. errorType = "invalid_request_error"
  77. }
  78. message := strings.TrimSpace(err.Message)
  79. if message == "" {
  80. message = "request blocked by param override"
  81. }
  82. opts := make([]types.NewAPIErrorOptions, 0, 1)
  83. if err.SkipRetry {
  84. opts = append(opts, types.ErrOptionWithSkipRetry())
  85. }
  86. return types.WithOpenAIError(types.OpenAIError{
  87. Message: message,
  88. Type: errorType,
  89. Code: errorCode,
  90. }, statusCode, opts...)
  91. }
  92. func ApplyParamOverride(jsonData []byte, paramOverride map[string]interface{}, conditionContext map[string]interface{}) ([]byte, error) {
  93. if len(paramOverride) == 0 {
  94. return jsonData, nil
  95. }
  96. // 尝试断言为操作格式
  97. if operations, ok := tryParseOperations(paramOverride); ok {
  98. // 使用新方法
  99. result, err := applyOperations(string(jsonData), operations, conditionContext)
  100. return []byte(result), err
  101. }
  102. // 直接使用旧方法
  103. return applyOperationsLegacy(jsonData, paramOverride)
  104. }
  105. func tryParseOperations(paramOverride map[string]interface{}) ([]ParamOperation, bool) {
  106. // 检查是否包含 "operations" 字段
  107. if opsValue, exists := paramOverride["operations"]; exists {
  108. if opsSlice, ok := opsValue.([]interface{}); ok {
  109. var operations []ParamOperation
  110. for _, op := range opsSlice {
  111. if opMap, ok := op.(map[string]interface{}); ok {
  112. operation := ParamOperation{}
  113. // 断言必要字段
  114. if path, ok := opMap["path"].(string); ok {
  115. operation.Path = path
  116. }
  117. if mode, ok := opMap["mode"].(string); ok {
  118. operation.Mode = mode
  119. } else {
  120. return nil, false // mode 是必需的
  121. }
  122. // 可选字段
  123. if value, exists := opMap["value"]; exists {
  124. operation.Value = value
  125. }
  126. if keepOrigin, ok := opMap["keep_origin"].(bool); ok {
  127. operation.KeepOrigin = keepOrigin
  128. }
  129. if from, ok := opMap["from"].(string); ok {
  130. operation.From = from
  131. }
  132. if to, ok := opMap["to"].(string); ok {
  133. operation.To = to
  134. }
  135. if logic, ok := opMap["logic"].(string); ok {
  136. operation.Logic = logic
  137. } else {
  138. operation.Logic = "OR" // 默认为OR
  139. }
  140. // 解析条件
  141. if conditions, exists := opMap["conditions"]; exists {
  142. if condSlice, ok := conditions.([]interface{}); ok {
  143. for _, cond := range condSlice {
  144. if condMap, ok := cond.(map[string]interface{}); ok {
  145. condition := ConditionOperation{}
  146. if path, ok := condMap["path"].(string); ok {
  147. condition.Path = path
  148. }
  149. if mode, ok := condMap["mode"].(string); ok {
  150. condition.Mode = mode
  151. }
  152. if value, ok := condMap["value"]; ok {
  153. condition.Value = value
  154. }
  155. if invert, ok := condMap["invert"].(bool); ok {
  156. condition.Invert = invert
  157. }
  158. if passMissingKey, ok := condMap["pass_missing_key"].(bool); ok {
  159. condition.PassMissingKey = passMissingKey
  160. }
  161. operation.Conditions = append(operation.Conditions, condition)
  162. }
  163. }
  164. }
  165. }
  166. operations = append(operations, operation)
  167. } else {
  168. return nil, false
  169. }
  170. }
  171. return operations, true
  172. }
  173. }
  174. return nil, false
  175. }
  176. func checkConditions(jsonStr, contextJSON string, conditions []ConditionOperation, logic string) (bool, error) {
  177. if len(conditions) == 0 {
  178. return true, nil // 没有条件,直接通过
  179. }
  180. results := make([]bool, len(conditions))
  181. for i, condition := range conditions {
  182. result, err := checkSingleCondition(jsonStr, contextJSON, condition)
  183. if err != nil {
  184. return false, err
  185. }
  186. results[i] = result
  187. }
  188. if strings.ToUpper(logic) == "AND" {
  189. for _, result := range results {
  190. if !result {
  191. return false, nil
  192. }
  193. }
  194. return true, nil
  195. } else {
  196. for _, result := range results {
  197. if result {
  198. return true, nil
  199. }
  200. }
  201. return false, nil
  202. }
  203. }
  204. func checkSingleCondition(jsonStr, contextJSON string, condition ConditionOperation) (bool, error) {
  205. // 处理负数索引
  206. path := processNegativeIndex(jsonStr, condition.Path)
  207. value := gjson.Get(jsonStr, path)
  208. if !value.Exists() && contextJSON != "" {
  209. value = gjson.Get(contextJSON, condition.Path)
  210. }
  211. if !value.Exists() {
  212. if condition.PassMissingKey {
  213. return true, nil
  214. }
  215. return false, nil
  216. }
  217. // 利用gjson的类型解析
  218. targetBytes, err := common.Marshal(condition.Value)
  219. if err != nil {
  220. return false, fmt.Errorf("failed to marshal condition value: %v", err)
  221. }
  222. targetValue := gjson.ParseBytes(targetBytes)
  223. result, err := compareGjsonValues(value, targetValue, strings.ToLower(condition.Mode))
  224. if err != nil {
  225. return false, fmt.Errorf("comparison failed for path %s: %v", condition.Path, err)
  226. }
  227. if condition.Invert {
  228. result = !result
  229. }
  230. return result, nil
  231. }
  232. func processNegativeIndex(jsonStr string, path string) string {
  233. matches := negativeIndexRegexp.FindAllStringSubmatch(path, -1)
  234. if len(matches) == 0 {
  235. return path
  236. }
  237. result := path
  238. for _, match := range matches {
  239. negIndex := match[1]
  240. index, _ := strconv.Atoi(negIndex)
  241. arrayPath := strings.Split(path, negIndex)[0]
  242. if strings.HasSuffix(arrayPath, ".") {
  243. arrayPath = arrayPath[:len(arrayPath)-1]
  244. }
  245. array := gjson.Get(jsonStr, arrayPath)
  246. if array.IsArray() {
  247. length := len(array.Array())
  248. actualIndex := length + index
  249. if actualIndex >= 0 && actualIndex < length {
  250. result = strings.Replace(result, match[0], "."+strconv.Itoa(actualIndex), 1)
  251. }
  252. }
  253. }
  254. return result
  255. }
  256. // compareGjsonValues 直接比较两个gjson.Result,支持所有比较模式
  257. func compareGjsonValues(jsonValue, targetValue gjson.Result, mode string) (bool, error) {
  258. switch mode {
  259. case "full":
  260. return compareEqual(jsonValue, targetValue)
  261. case "prefix":
  262. return strings.HasPrefix(jsonValue.String(), targetValue.String()), nil
  263. case "suffix":
  264. return strings.HasSuffix(jsonValue.String(), targetValue.String()), nil
  265. case "contains":
  266. return strings.Contains(jsonValue.String(), targetValue.String()), nil
  267. case "gt":
  268. return compareNumeric(jsonValue, targetValue, "gt")
  269. case "gte":
  270. return compareNumeric(jsonValue, targetValue, "gte")
  271. case "lt":
  272. return compareNumeric(jsonValue, targetValue, "lt")
  273. case "lte":
  274. return compareNumeric(jsonValue, targetValue, "lte")
  275. default:
  276. return false, fmt.Errorf("unsupported comparison mode: %s", mode)
  277. }
  278. }
  279. func compareEqual(jsonValue, targetValue gjson.Result) (bool, error) {
  280. // 对null值特殊处理:两个都是null返回true,一个是null另一个不是返回false
  281. if jsonValue.Type == gjson.Null || targetValue.Type == gjson.Null {
  282. return jsonValue.Type == gjson.Null && targetValue.Type == gjson.Null, nil
  283. }
  284. // 对布尔值特殊处理
  285. if (jsonValue.Type == gjson.True || jsonValue.Type == gjson.False) &&
  286. (targetValue.Type == gjson.True || targetValue.Type == gjson.False) {
  287. return jsonValue.Bool() == targetValue.Bool(), nil
  288. }
  289. // 如果类型不同,报错
  290. if jsonValue.Type != targetValue.Type {
  291. return false, fmt.Errorf("compare for different types, got %v and %v", jsonValue.Type, targetValue.Type)
  292. }
  293. switch jsonValue.Type {
  294. case gjson.True, gjson.False:
  295. return jsonValue.Bool() == targetValue.Bool(), nil
  296. case gjson.Number:
  297. return jsonValue.Num == targetValue.Num, nil
  298. case gjson.String:
  299. return jsonValue.String() == targetValue.String(), nil
  300. default:
  301. return jsonValue.String() == targetValue.String(), nil
  302. }
  303. }
  304. func compareNumeric(jsonValue, targetValue gjson.Result, operator string) (bool, error) {
  305. // 只有数字类型才支持数值比较
  306. if jsonValue.Type != gjson.Number || targetValue.Type != gjson.Number {
  307. return false, fmt.Errorf("numeric comparison requires both values to be numbers, got %v and %v", jsonValue.Type, targetValue.Type)
  308. }
  309. jsonNum := jsonValue.Num
  310. targetNum := targetValue.Num
  311. switch operator {
  312. case "gt":
  313. return jsonNum > targetNum, nil
  314. case "gte":
  315. return jsonNum >= targetNum, nil
  316. case "lt":
  317. return jsonNum < targetNum, nil
  318. case "lte":
  319. return jsonNum <= targetNum, nil
  320. default:
  321. return false, fmt.Errorf("unsupported numeric operator: %s", operator)
  322. }
  323. }
  324. // applyOperationsLegacy 原参数覆盖方法
  325. func applyOperationsLegacy(jsonData []byte, paramOverride map[string]interface{}) ([]byte, error) {
  326. reqMap := make(map[string]interface{})
  327. err := common.Unmarshal(jsonData, &reqMap)
  328. if err != nil {
  329. return nil, err
  330. }
  331. for key, value := range paramOverride {
  332. reqMap[key] = value
  333. }
  334. return common.Marshal(reqMap)
  335. }
  336. func applyOperations(jsonStr string, operations []ParamOperation, conditionContext map[string]interface{}) (string, error) {
  337. var contextJSON string
  338. if conditionContext != nil && len(conditionContext) > 0 {
  339. ctxBytes, err := common.Marshal(conditionContext)
  340. if err != nil {
  341. return "", fmt.Errorf("failed to marshal condition context: %v", err)
  342. }
  343. contextJSON = string(ctxBytes)
  344. }
  345. result := jsonStr
  346. for _, op := range operations {
  347. // 检查条件是否满足
  348. ok, err := checkConditions(result, contextJSON, op.Conditions, op.Logic)
  349. if err != nil {
  350. return "", err
  351. }
  352. if !ok {
  353. continue // 条件不满足,跳过当前操作
  354. }
  355. // 处理路径中的负数索引
  356. opPath := processNegativeIndex(result, op.Path)
  357. switch op.Mode {
  358. case "delete":
  359. result, err = sjson.Delete(result, opPath)
  360. case "set":
  361. if op.KeepOrigin && gjson.Get(result, opPath).Exists() {
  362. continue
  363. }
  364. result, err = sjson.Set(result, opPath, op.Value)
  365. case "move":
  366. opFrom := processNegativeIndex(result, op.From)
  367. opTo := processNegativeIndex(result, op.To)
  368. result, err = moveValue(result, opFrom, opTo)
  369. case "copy":
  370. if op.From == "" || op.To == "" {
  371. return "", fmt.Errorf("copy from/to is required")
  372. }
  373. opFrom := processNegativeIndex(result, op.From)
  374. opTo := processNegativeIndex(result, op.To)
  375. result, err = copyValue(result, opFrom, opTo)
  376. case "prepend":
  377. result, err = modifyValue(result, opPath, op.Value, op.KeepOrigin, true)
  378. case "append":
  379. result, err = modifyValue(result, opPath, op.Value, op.KeepOrigin, false)
  380. case "trim_prefix":
  381. result, err = trimStringValue(result, opPath, op.Value, true)
  382. case "trim_suffix":
  383. result, err = trimStringValue(result, opPath, op.Value, false)
  384. case "ensure_prefix":
  385. result, err = ensureStringAffix(result, opPath, op.Value, true)
  386. case "ensure_suffix":
  387. result, err = ensureStringAffix(result, opPath, op.Value, false)
  388. case "trim_space":
  389. result, err = transformStringValue(result, opPath, strings.TrimSpace)
  390. case "to_lower":
  391. result, err = transformStringValue(result, opPath, strings.ToLower)
  392. case "to_upper":
  393. result, err = transformStringValue(result, opPath, strings.ToUpper)
  394. case "replace":
  395. result, err = replaceStringValue(result, opPath, op.From, op.To)
  396. case "regex_replace":
  397. result, err = regexReplaceStringValue(result, opPath, op.From, op.To)
  398. case "return_error":
  399. returnErr, parseErr := parseParamOverrideReturnError(op.Value)
  400. if parseErr != nil {
  401. return "", parseErr
  402. }
  403. return "", returnErr
  404. case "prune_objects":
  405. result, err = pruneObjects(result, opPath, contextJSON, op.Value)
  406. default:
  407. return "", fmt.Errorf("unknown operation: %s", op.Mode)
  408. }
  409. if err != nil {
  410. return "", fmt.Errorf("operation %s failed: %w", op.Mode, err)
  411. }
  412. }
  413. return result, nil
  414. }
  415. func parseParamOverrideReturnError(value interface{}) (*ParamOverrideReturnError, error) {
  416. result := &ParamOverrideReturnError{
  417. StatusCode: http.StatusBadRequest,
  418. Code: string(types.ErrorCodeInvalidRequest),
  419. Type: "invalid_request_error",
  420. SkipRetry: true,
  421. }
  422. switch raw := value.(type) {
  423. case nil:
  424. return nil, fmt.Errorf("return_error value is required")
  425. case string:
  426. result.Message = strings.TrimSpace(raw)
  427. case map[string]interface{}:
  428. if message, ok := raw["message"].(string); ok {
  429. result.Message = strings.TrimSpace(message)
  430. }
  431. if result.Message == "" {
  432. if message, ok := raw["msg"].(string); ok {
  433. result.Message = strings.TrimSpace(message)
  434. }
  435. }
  436. if code, exists := raw["code"]; exists {
  437. codeStr := strings.TrimSpace(fmt.Sprintf("%v", code))
  438. if codeStr != "" {
  439. result.Code = codeStr
  440. }
  441. }
  442. if errType, ok := raw["type"].(string); ok {
  443. errType = strings.TrimSpace(errType)
  444. if errType != "" {
  445. result.Type = errType
  446. }
  447. }
  448. if skipRetry, ok := raw["skip_retry"].(bool); ok {
  449. result.SkipRetry = skipRetry
  450. }
  451. if statusCodeRaw, exists := raw["status_code"]; exists {
  452. statusCode, ok := parseOverrideInt(statusCodeRaw)
  453. if !ok {
  454. return nil, fmt.Errorf("return_error status_code must be an integer")
  455. }
  456. result.StatusCode = statusCode
  457. } else if statusRaw, exists := raw["status"]; exists {
  458. statusCode, ok := parseOverrideInt(statusRaw)
  459. if !ok {
  460. return nil, fmt.Errorf("return_error status must be an integer")
  461. }
  462. result.StatusCode = statusCode
  463. }
  464. default:
  465. return nil, fmt.Errorf("return_error value must be string or object")
  466. }
  467. if result.Message == "" {
  468. return nil, fmt.Errorf("return_error message is required")
  469. }
  470. if result.StatusCode < http.StatusContinue || result.StatusCode > http.StatusNetworkAuthenticationRequired {
  471. return nil, fmt.Errorf("return_error status code out of range: %d", result.StatusCode)
  472. }
  473. return result, nil
  474. }
  475. func parseOverrideInt(v interface{}) (int, bool) {
  476. switch value := v.(type) {
  477. case int:
  478. return value, true
  479. case float64:
  480. if value != float64(int(value)) {
  481. return 0, false
  482. }
  483. return int(value), true
  484. default:
  485. return 0, false
  486. }
  487. }
  488. func moveValue(jsonStr, fromPath, toPath string) (string, error) {
  489. sourceValue := gjson.Get(jsonStr, fromPath)
  490. if !sourceValue.Exists() {
  491. return jsonStr, fmt.Errorf("source path does not exist: %s", fromPath)
  492. }
  493. result, err := sjson.Set(jsonStr, toPath, sourceValue.Value())
  494. if err != nil {
  495. return "", err
  496. }
  497. return sjson.Delete(result, fromPath)
  498. }
  499. func copyValue(jsonStr, fromPath, toPath string) (string, error) {
  500. sourceValue := gjson.Get(jsonStr, fromPath)
  501. if !sourceValue.Exists() {
  502. return jsonStr, fmt.Errorf("source path does not exist: %s", fromPath)
  503. }
  504. return sjson.Set(jsonStr, toPath, sourceValue.Value())
  505. }
  506. func modifyValue(jsonStr, path string, value interface{}, keepOrigin, isPrepend bool) (string, error) {
  507. current := gjson.Get(jsonStr, path)
  508. switch {
  509. case current.IsArray():
  510. return modifyArray(jsonStr, path, value, isPrepend)
  511. case current.Type == gjson.String:
  512. return modifyString(jsonStr, path, value, isPrepend)
  513. case current.Type == gjson.JSON:
  514. return mergeObjects(jsonStr, path, value, keepOrigin)
  515. }
  516. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  517. }
  518. func modifyArray(jsonStr, path string, value interface{}, isPrepend bool) (string, error) {
  519. current := gjson.Get(jsonStr, path)
  520. var newArray []interface{}
  521. // 添加新值
  522. addValue := func() {
  523. if arr, ok := value.([]interface{}); ok {
  524. newArray = append(newArray, arr...)
  525. } else {
  526. newArray = append(newArray, value)
  527. }
  528. }
  529. // 添加原值
  530. addOriginal := func() {
  531. current.ForEach(func(_, val gjson.Result) bool {
  532. newArray = append(newArray, val.Value())
  533. return true
  534. })
  535. }
  536. if isPrepend {
  537. addValue()
  538. addOriginal()
  539. } else {
  540. addOriginal()
  541. addValue()
  542. }
  543. return sjson.Set(jsonStr, path, newArray)
  544. }
  545. func modifyString(jsonStr, path string, value interface{}, isPrepend bool) (string, error) {
  546. current := gjson.Get(jsonStr, path)
  547. valueStr := fmt.Sprintf("%v", value)
  548. var newStr string
  549. if isPrepend {
  550. newStr = valueStr + current.String()
  551. } else {
  552. newStr = current.String() + valueStr
  553. }
  554. return sjson.Set(jsonStr, path, newStr)
  555. }
  556. func trimStringValue(jsonStr, path string, value interface{}, isPrefix bool) (string, error) {
  557. current := gjson.Get(jsonStr, path)
  558. if current.Type != gjson.String {
  559. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  560. }
  561. if value == nil {
  562. return jsonStr, fmt.Errorf("trim value is required")
  563. }
  564. valueStr := fmt.Sprintf("%v", value)
  565. var newStr string
  566. if isPrefix {
  567. newStr = strings.TrimPrefix(current.String(), valueStr)
  568. } else {
  569. newStr = strings.TrimSuffix(current.String(), valueStr)
  570. }
  571. return sjson.Set(jsonStr, path, newStr)
  572. }
  573. func ensureStringAffix(jsonStr, path string, value interface{}, isPrefix bool) (string, error) {
  574. current := gjson.Get(jsonStr, path)
  575. if current.Type != gjson.String {
  576. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  577. }
  578. if value == nil {
  579. return jsonStr, fmt.Errorf("ensure value is required")
  580. }
  581. valueStr := fmt.Sprintf("%v", value)
  582. if valueStr == "" {
  583. return jsonStr, fmt.Errorf("ensure value is required")
  584. }
  585. currentStr := current.String()
  586. if isPrefix {
  587. if strings.HasPrefix(currentStr, valueStr) {
  588. return jsonStr, nil
  589. }
  590. return sjson.Set(jsonStr, path, valueStr+currentStr)
  591. }
  592. if strings.HasSuffix(currentStr, valueStr) {
  593. return jsonStr, nil
  594. }
  595. return sjson.Set(jsonStr, path, currentStr+valueStr)
  596. }
  597. func transformStringValue(jsonStr, path string, transform func(string) string) (string, error) {
  598. current := gjson.Get(jsonStr, path)
  599. if current.Type != gjson.String {
  600. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  601. }
  602. return sjson.Set(jsonStr, path, transform(current.String()))
  603. }
  604. func replaceStringValue(jsonStr, path, from, to string) (string, error) {
  605. current := gjson.Get(jsonStr, path)
  606. if current.Type != gjson.String {
  607. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  608. }
  609. if from == "" {
  610. return jsonStr, fmt.Errorf("replace from is required")
  611. }
  612. return sjson.Set(jsonStr, path, strings.ReplaceAll(current.String(), from, to))
  613. }
  614. func regexReplaceStringValue(jsonStr, path, pattern, replacement string) (string, error) {
  615. current := gjson.Get(jsonStr, path)
  616. if current.Type != gjson.String {
  617. return jsonStr, fmt.Errorf("operation not supported for type: %v", current.Type)
  618. }
  619. if pattern == "" {
  620. return jsonStr, fmt.Errorf("regex pattern is required")
  621. }
  622. re, err := regexp.Compile(pattern)
  623. if err != nil {
  624. return jsonStr, err
  625. }
  626. return sjson.Set(jsonStr, path, re.ReplaceAllString(current.String(), replacement))
  627. }
  628. type pruneObjectsOptions struct {
  629. conditions []ConditionOperation
  630. logic string
  631. recursive bool
  632. }
  633. func pruneObjects(jsonStr, path, contextJSON string, value interface{}) (string, error) {
  634. options, err := parsePruneObjectsOptions(value)
  635. if err != nil {
  636. return "", err
  637. }
  638. if path == "" {
  639. var root interface{}
  640. if err := common.Unmarshal([]byte(jsonStr), &root); err != nil {
  641. return "", err
  642. }
  643. cleaned, _, err := pruneObjectsNode(root, options, contextJSON, true)
  644. if err != nil {
  645. return "", err
  646. }
  647. cleanedBytes, err := common.Marshal(cleaned)
  648. if err != nil {
  649. return "", err
  650. }
  651. return string(cleanedBytes), nil
  652. }
  653. target := gjson.Get(jsonStr, path)
  654. if !target.Exists() {
  655. return jsonStr, nil
  656. }
  657. var targetNode interface{}
  658. if target.Type == gjson.JSON {
  659. if err := common.Unmarshal([]byte(target.Raw), &targetNode); err != nil {
  660. return "", err
  661. }
  662. } else {
  663. targetNode = target.Value()
  664. }
  665. cleaned, _, err := pruneObjectsNode(targetNode, options, contextJSON, true)
  666. if err != nil {
  667. return "", err
  668. }
  669. cleanedBytes, err := common.Marshal(cleaned)
  670. if err != nil {
  671. return "", err
  672. }
  673. return sjson.SetRaw(jsonStr, path, string(cleanedBytes))
  674. }
  675. func parsePruneObjectsOptions(value interface{}) (pruneObjectsOptions, error) {
  676. opts := pruneObjectsOptions{
  677. logic: "AND",
  678. recursive: true,
  679. }
  680. switch raw := value.(type) {
  681. case nil:
  682. return opts, fmt.Errorf("prune_objects value is required")
  683. case string:
  684. v := strings.TrimSpace(raw)
  685. if v == "" {
  686. return opts, fmt.Errorf("prune_objects value is required")
  687. }
  688. opts.conditions = []ConditionOperation{
  689. {
  690. Path: "type",
  691. Mode: "full",
  692. Value: v,
  693. },
  694. }
  695. case map[string]interface{}:
  696. if logic, ok := raw["logic"].(string); ok && strings.TrimSpace(logic) != "" {
  697. opts.logic = logic
  698. }
  699. if recursive, ok := raw["recursive"].(bool); ok {
  700. opts.recursive = recursive
  701. }
  702. if condRaw, exists := raw["conditions"]; exists {
  703. conditions, err := parseConditionOperations(condRaw)
  704. if err != nil {
  705. return opts, err
  706. }
  707. opts.conditions = append(opts.conditions, conditions...)
  708. }
  709. if whereRaw, exists := raw["where"]; exists {
  710. whereMap, ok := whereRaw.(map[string]interface{})
  711. if !ok {
  712. return opts, fmt.Errorf("prune_objects where must be object")
  713. }
  714. for key, val := range whereMap {
  715. key = strings.TrimSpace(key)
  716. if key == "" {
  717. continue
  718. }
  719. opts.conditions = append(opts.conditions, ConditionOperation{
  720. Path: key,
  721. Mode: "full",
  722. Value: val,
  723. })
  724. }
  725. }
  726. if matchType, exists := raw["type"]; exists {
  727. opts.conditions = append(opts.conditions, ConditionOperation{
  728. Path: "type",
  729. Mode: "full",
  730. Value: matchType,
  731. })
  732. }
  733. default:
  734. return opts, fmt.Errorf("prune_objects value must be string or object")
  735. }
  736. if len(opts.conditions) == 0 {
  737. return opts, fmt.Errorf("prune_objects conditions are required")
  738. }
  739. return opts, nil
  740. }
  741. func parseConditionOperations(raw interface{}) ([]ConditionOperation, error) {
  742. items, ok := raw.([]interface{})
  743. if !ok {
  744. return nil, fmt.Errorf("conditions must be an array")
  745. }
  746. result := make([]ConditionOperation, 0, len(items))
  747. for _, item := range items {
  748. itemMap, ok := item.(map[string]interface{})
  749. if !ok {
  750. return nil, fmt.Errorf("condition must be object")
  751. }
  752. path, _ := itemMap["path"].(string)
  753. mode, _ := itemMap["mode"].(string)
  754. if strings.TrimSpace(path) == "" || strings.TrimSpace(mode) == "" {
  755. return nil, fmt.Errorf("condition path/mode is required")
  756. }
  757. condition := ConditionOperation{
  758. Path: path,
  759. Mode: mode,
  760. }
  761. if value, exists := itemMap["value"]; exists {
  762. condition.Value = value
  763. }
  764. if invert, ok := itemMap["invert"].(bool); ok {
  765. condition.Invert = invert
  766. }
  767. if passMissingKey, ok := itemMap["pass_missing_key"].(bool); ok {
  768. condition.PassMissingKey = passMissingKey
  769. }
  770. result = append(result, condition)
  771. }
  772. return result, nil
  773. }
  774. func pruneObjectsNode(node interface{}, options pruneObjectsOptions, contextJSON string, isRoot bool) (interface{}, bool, error) {
  775. switch value := node.(type) {
  776. case []interface{}:
  777. result := make([]interface{}, 0, len(value))
  778. for _, item := range value {
  779. next, drop, err := pruneObjectsNode(item, options, contextJSON, false)
  780. if err != nil {
  781. return nil, false, err
  782. }
  783. if drop {
  784. continue
  785. }
  786. result = append(result, next)
  787. }
  788. return result, false, nil
  789. case map[string]interface{}:
  790. shouldDrop, err := shouldPruneObject(value, options, contextJSON)
  791. if err != nil {
  792. return nil, false, err
  793. }
  794. if shouldDrop && !isRoot {
  795. return nil, true, nil
  796. }
  797. if !options.recursive {
  798. return value, false, nil
  799. }
  800. for key, child := range value {
  801. next, drop, err := pruneObjectsNode(child, options, contextJSON, false)
  802. if err != nil {
  803. return nil, false, err
  804. }
  805. if drop {
  806. delete(value, key)
  807. continue
  808. }
  809. value[key] = next
  810. }
  811. return value, false, nil
  812. default:
  813. return node, false, nil
  814. }
  815. }
  816. func shouldPruneObject(node map[string]interface{}, options pruneObjectsOptions, contextJSON string) (bool, error) {
  817. nodeBytes, err := common.Marshal(node)
  818. if err != nil {
  819. return false, err
  820. }
  821. return checkConditions(string(nodeBytes), contextJSON, options.conditions, options.logic)
  822. }
  823. func mergeObjects(jsonStr, path string, value interface{}, keepOrigin bool) (string, error) {
  824. current := gjson.Get(jsonStr, path)
  825. var currentMap, newMap map[string]interface{}
  826. // 解析当前值
  827. if err := common.Unmarshal([]byte(current.Raw), &currentMap); err != nil {
  828. return "", err
  829. }
  830. // 解析新值
  831. switch v := value.(type) {
  832. case map[string]interface{}:
  833. newMap = v
  834. default:
  835. jsonBytes, _ := common.Marshal(v)
  836. if err := common.Unmarshal(jsonBytes, &newMap); err != nil {
  837. return "", err
  838. }
  839. }
  840. // 合并
  841. result := make(map[string]interface{})
  842. for k, v := range currentMap {
  843. result[k] = v
  844. }
  845. for k, v := range newMap {
  846. if !keepOrigin || result[k] == nil {
  847. result[k] = v
  848. }
  849. }
  850. return sjson.Set(jsonStr, path, result)
  851. }
  852. // BuildParamOverrideContext 提供 ApplyParamOverride 可用的上下文信息。
  853. // 目前内置以下字段:
  854. // - upstream_model/model:始终为通道映射后的上游模型名。
  855. // - original_model:请求最初指定的模型名。
  856. // - request_path:请求路径
  857. // - is_channel_test:是否为渠道测试请求(同 is_test)。
  858. func BuildParamOverrideContext(info *RelayInfo) map[string]interface{} {
  859. if info == nil {
  860. return nil
  861. }
  862. ctx := make(map[string]interface{})
  863. if info.ChannelMeta != nil && info.ChannelMeta.UpstreamModelName != "" {
  864. ctx["model"] = info.ChannelMeta.UpstreamModelName
  865. ctx["upstream_model"] = info.ChannelMeta.UpstreamModelName
  866. }
  867. if info.OriginModelName != "" {
  868. ctx["original_model"] = info.OriginModelName
  869. if _, exists := ctx["model"]; !exists {
  870. ctx["model"] = info.OriginModelName
  871. }
  872. }
  873. if info.RequestURLPath != "" {
  874. requestPath := info.RequestURLPath
  875. if requestPath != "" {
  876. ctx["request_path"] = requestPath
  877. }
  878. }
  879. ctx["retry_index"] = info.RetryIndex
  880. ctx["is_retry"] = info.RetryIndex > 0
  881. ctx["retry"] = map[string]interface{}{
  882. "index": info.RetryIndex,
  883. "is_retry": info.RetryIndex > 0,
  884. }
  885. if info.LastError != nil {
  886. code := string(info.LastError.GetErrorCode())
  887. errorType := string(info.LastError.GetErrorType())
  888. lastError := map[string]interface{}{
  889. "status_code": info.LastError.StatusCode,
  890. "message": info.LastError.Error(),
  891. "code": code,
  892. "error_code": code,
  893. "type": errorType,
  894. "error_type": errorType,
  895. "skip_retry": types.IsSkipRetryError(info.LastError),
  896. }
  897. ctx["last_error"] = lastError
  898. ctx["last_error_status_code"] = info.LastError.StatusCode
  899. ctx["last_error_message"] = info.LastError.Error()
  900. ctx["last_error_code"] = code
  901. ctx["last_error_type"] = errorType
  902. }
  903. ctx["is_channel_test"] = info.IsChannelTest
  904. return ctx
  905. }