relay_info.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. package common
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/QuantumNous/new-api/common"
  9. "github.com/QuantumNous/new-api/constant"
  10. "github.com/QuantumNous/new-api/dto"
  11. relayconstant "github.com/QuantumNous/new-api/relay/constant"
  12. "github.com/QuantumNous/new-api/setting/model_setting"
  13. "github.com/QuantumNous/new-api/types"
  14. "github.com/gin-gonic/gin"
  15. "github.com/gorilla/websocket"
  16. )
  17. type ThinkingContentInfo struct {
  18. IsFirstThinkingContent bool
  19. SendLastThinkingContent bool
  20. HasSentThinkingContent bool
  21. }
  22. const (
  23. LastMessageTypeNone = "none"
  24. LastMessageTypeText = "text"
  25. LastMessageTypeTools = "tools"
  26. LastMessageTypeThinking = "thinking"
  27. )
  28. type ClaudeConvertInfo struct {
  29. LastMessagesType string
  30. Index int
  31. Usage *dto.Usage
  32. FinishReason string
  33. Done bool
  34. }
  35. type RerankerInfo struct {
  36. Documents []any
  37. ReturnDocuments bool
  38. }
  39. type BuildInToolInfo struct {
  40. ToolName string
  41. CallCount int
  42. SearchContextSize string
  43. }
  44. type ResponsesUsageInfo struct {
  45. BuiltInTools map[string]*BuildInToolInfo
  46. }
  47. type ChannelMeta struct {
  48. ChannelType int
  49. ChannelId int
  50. ChannelIsMultiKey bool
  51. ChannelMultiKeyIndex int
  52. ChannelBaseUrl string
  53. ApiType int
  54. ApiVersion string
  55. ApiKey string
  56. Organization string
  57. ChannelCreateTime int64
  58. ParamOverride map[string]interface{}
  59. HeadersOverride map[string]interface{}
  60. ChannelSetting dto.ChannelSettings
  61. ChannelOtherSettings dto.ChannelOtherSettings
  62. UpstreamModelName string
  63. IsModelMapped bool
  64. SupportStreamOptions bool // 是否支持流式选项
  65. }
  66. type TokenCountMeta struct {
  67. //promptTokens int
  68. estimatePromptTokens int
  69. }
  70. type RelayInfo struct {
  71. TokenId int
  72. TokenKey string
  73. TokenGroup string
  74. UserId int
  75. UsingGroup string // 使用的分组,当auto跨分组重试时,会变动
  76. UserGroup string // 用户所在分组
  77. TokenUnlimited bool
  78. StartTime time.Time
  79. FirstResponseTime time.Time
  80. isFirstResponse bool
  81. //SendLastReasoningResponse bool
  82. IsStream bool
  83. IsGeminiBatchEmbedding bool
  84. IsPlayground bool
  85. UsePrice bool
  86. RelayMode int
  87. OriginModelName string
  88. RequestURLPath string
  89. ShouldIncludeUsage bool
  90. DisablePing bool // 是否禁止向下游发送自定义 Ping
  91. ClientWs *websocket.Conn
  92. TargetWs *websocket.Conn
  93. InputAudioFormat string
  94. OutputAudioFormat string
  95. RealtimeTools []dto.RealTimeTool
  96. IsFirstRequest bool
  97. AudioUsage bool
  98. ReasoningEffort string
  99. UserSetting dto.UserSetting
  100. UserEmail string
  101. UserQuota int
  102. RelayFormat types.RelayFormat
  103. SendResponseCount int
  104. FinalPreConsumedQuota int // 最终预消耗的配额
  105. IsClaudeBetaQuery bool // /v1/messages?beta=true
  106. IsChannelTest bool // channel test request
  107. PriceData types.PriceData
  108. Request dto.Request
  109. ThinkingContentInfo
  110. TokenCountMeta
  111. *ClaudeConvertInfo
  112. *RerankerInfo
  113. *ResponsesUsageInfo
  114. *ChannelMeta
  115. *TaskRelayInfo
  116. }
  117. func (info *RelayInfo) InitChannelMeta(c *gin.Context) {
  118. channelType := common.GetContextKeyInt(c, constant.ContextKeyChannelType)
  119. paramOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelParamOverride)
  120. headerOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelHeaderOverride)
  121. apiType, _ := common.ChannelType2APIType(channelType)
  122. channelMeta := &ChannelMeta{
  123. ChannelType: channelType,
  124. ChannelId: common.GetContextKeyInt(c, constant.ContextKeyChannelId),
  125. ChannelIsMultiKey: common.GetContextKeyBool(c, constant.ContextKeyChannelIsMultiKey),
  126. ChannelMultiKeyIndex: common.GetContextKeyInt(c, constant.ContextKeyChannelMultiKeyIndex),
  127. ChannelBaseUrl: common.GetContextKeyString(c, constant.ContextKeyChannelBaseUrl),
  128. ApiType: apiType,
  129. ApiVersion: c.GetString("api_version"),
  130. ApiKey: common.GetContextKeyString(c, constant.ContextKeyChannelKey),
  131. Organization: c.GetString("channel_organization"),
  132. ChannelCreateTime: c.GetInt64("channel_create_time"),
  133. ParamOverride: paramOverride,
  134. HeadersOverride: headerOverride,
  135. UpstreamModelName: common.GetContextKeyString(c, constant.ContextKeyOriginalModel),
  136. IsModelMapped: false,
  137. SupportStreamOptions: false,
  138. }
  139. if channelType == constant.ChannelTypeAzure {
  140. channelMeta.ApiVersion = GetAPIVersion(c)
  141. }
  142. if channelType == constant.ChannelTypeVertexAi {
  143. channelMeta.ApiVersion = c.GetString("region")
  144. }
  145. channelSetting, ok := common.GetContextKeyType[dto.ChannelSettings](c, constant.ContextKeyChannelSetting)
  146. if ok {
  147. channelMeta.ChannelSetting = channelSetting
  148. }
  149. channelOtherSettings, ok := common.GetContextKeyType[dto.ChannelOtherSettings](c, constant.ContextKeyChannelOtherSetting)
  150. if ok {
  151. channelMeta.ChannelOtherSettings = channelOtherSettings
  152. }
  153. if streamSupportedChannels[channelMeta.ChannelType] {
  154. channelMeta.SupportStreamOptions = true
  155. }
  156. info.ChannelMeta = channelMeta
  157. // reset some fields based on channel meta
  158. // 重置某些字段,例如模型名称等
  159. if info.Request != nil {
  160. info.Request.SetModelName(info.OriginModelName)
  161. }
  162. }
  163. func (info *RelayInfo) ToString() string {
  164. if info == nil {
  165. return "RelayInfo<nil>"
  166. }
  167. // Basic info
  168. b := &strings.Builder{}
  169. fmt.Fprintf(b, "RelayInfo{ ")
  170. fmt.Fprintf(b, "RelayFormat: %s, ", info.RelayFormat)
  171. fmt.Fprintf(b, "RelayMode: %d, ", info.RelayMode)
  172. fmt.Fprintf(b, "IsStream: %t, ", info.IsStream)
  173. fmt.Fprintf(b, "IsPlayground: %t, ", info.IsPlayground)
  174. fmt.Fprintf(b, "RequestURLPath: %q, ", info.RequestURLPath)
  175. fmt.Fprintf(b, "OriginModelName: %q, ", info.OriginModelName)
  176. fmt.Fprintf(b, "EstimatePromptTokens: %d, ", info.estimatePromptTokens)
  177. fmt.Fprintf(b, "ShouldIncludeUsage: %t, ", info.ShouldIncludeUsage)
  178. fmt.Fprintf(b, "DisablePing: %t, ", info.DisablePing)
  179. fmt.Fprintf(b, "SendResponseCount: %d, ", info.SendResponseCount)
  180. fmt.Fprintf(b, "FinalPreConsumedQuota: %d, ", info.FinalPreConsumedQuota)
  181. // User & token info (mask secrets)
  182. fmt.Fprintf(b, "User{ Id: %d, Email: %q, Group: %q, UsingGroup: %q, Quota: %d }, ",
  183. info.UserId, common.MaskEmail(info.UserEmail), info.UserGroup, info.UsingGroup, info.UserQuota)
  184. fmt.Fprintf(b, "Token{ Id: %d, Unlimited: %t, Key: ***masked*** }, ", info.TokenId, info.TokenUnlimited)
  185. // Time info
  186. latencyMs := info.FirstResponseTime.Sub(info.StartTime).Milliseconds()
  187. fmt.Fprintf(b, "Timing{ Start: %s, FirstResponse: %s, LatencyMs: %d }, ",
  188. info.StartTime.Format(time.RFC3339Nano), info.FirstResponseTime.Format(time.RFC3339Nano), latencyMs)
  189. // Audio / realtime
  190. if info.InputAudioFormat != "" || info.OutputAudioFormat != "" || len(info.RealtimeTools) > 0 || info.AudioUsage {
  191. fmt.Fprintf(b, "Realtime{ AudioUsage: %t, InFmt: %q, OutFmt: %q, Tools: %d }, ",
  192. info.AudioUsage, info.InputAudioFormat, info.OutputAudioFormat, len(info.RealtimeTools))
  193. }
  194. // Reasoning
  195. if info.ReasoningEffort != "" {
  196. fmt.Fprintf(b, "ReasoningEffort: %q, ", info.ReasoningEffort)
  197. }
  198. // Price data (non-sensitive)
  199. if info.PriceData.UsePrice {
  200. fmt.Fprintf(b, "PriceData{ %s }, ", info.PriceData.ToSetting())
  201. }
  202. // Channel metadata (mask ApiKey)
  203. if info.ChannelMeta != nil {
  204. cm := info.ChannelMeta
  205. fmt.Fprintf(b, "ChannelMeta{ Type: %d, Id: %d, IsMultiKey: %t, MultiKeyIndex: %d, BaseURL: %q, ApiType: %d, ApiVersion: %q, Organization: %q, CreateTime: %d, UpstreamModelName: %q, IsModelMapped: %t, SupportStreamOptions: %t, ApiKey: ***masked*** }, ",
  206. cm.ChannelType, cm.ChannelId, cm.ChannelIsMultiKey, cm.ChannelMultiKeyIndex, cm.ChannelBaseUrl, cm.ApiType, cm.ApiVersion, cm.Organization, cm.ChannelCreateTime, cm.UpstreamModelName, cm.IsModelMapped, cm.SupportStreamOptions)
  207. }
  208. // Responses usage info (non-sensitive)
  209. if info.ResponsesUsageInfo != nil && len(info.ResponsesUsageInfo.BuiltInTools) > 0 {
  210. fmt.Fprintf(b, "ResponsesTools{ ")
  211. first := true
  212. for name, tool := range info.ResponsesUsageInfo.BuiltInTools {
  213. if !first {
  214. fmt.Fprintf(b, ", ")
  215. }
  216. first = false
  217. if tool != nil {
  218. fmt.Fprintf(b, "%s: calls=%d", name, tool.CallCount)
  219. } else {
  220. fmt.Fprintf(b, "%s: calls=0", name)
  221. }
  222. }
  223. fmt.Fprintf(b, " }, ")
  224. }
  225. fmt.Fprintf(b, "}")
  226. return b.String()
  227. }
  228. // 定义支持流式选项的通道类型
  229. var streamSupportedChannels = map[int]bool{
  230. constant.ChannelTypeOpenAI: true,
  231. constant.ChannelTypeAnthropic: true,
  232. constant.ChannelTypeAws: true,
  233. constant.ChannelTypeGemini: true,
  234. constant.ChannelCloudflare: true,
  235. constant.ChannelTypeAzure: true,
  236. constant.ChannelTypeVolcEngine: true,
  237. constant.ChannelTypeOllama: true,
  238. constant.ChannelTypeXai: true,
  239. constant.ChannelTypeDeepSeek: true,
  240. constant.ChannelTypeBaiduV2: true,
  241. constant.ChannelTypeZhipu_v4: true,
  242. constant.ChannelTypeAli: true,
  243. constant.ChannelTypeSubmodel: true,
  244. constant.ChannelTypeCodex: true,
  245. }
  246. func GenRelayInfoWs(c *gin.Context, ws *websocket.Conn) *RelayInfo {
  247. info := genBaseRelayInfo(c, nil)
  248. info.RelayFormat = types.RelayFormatOpenAIRealtime
  249. info.ClientWs = ws
  250. info.InputAudioFormat = "pcm16"
  251. info.OutputAudioFormat = "pcm16"
  252. info.IsFirstRequest = true
  253. return info
  254. }
  255. func GenRelayInfoClaude(c *gin.Context, request dto.Request) *RelayInfo {
  256. info := genBaseRelayInfo(c, request)
  257. info.RelayFormat = types.RelayFormatClaude
  258. info.ShouldIncludeUsage = false
  259. info.ClaudeConvertInfo = &ClaudeConvertInfo{
  260. LastMessagesType: LastMessageTypeNone,
  261. }
  262. if c.Query("beta") == "true" {
  263. info.IsClaudeBetaQuery = true
  264. }
  265. return info
  266. }
  267. func GenRelayInfoRerank(c *gin.Context, request *dto.RerankRequest) *RelayInfo {
  268. info := genBaseRelayInfo(c, request)
  269. info.RelayMode = relayconstant.RelayModeRerank
  270. info.RelayFormat = types.RelayFormatRerank
  271. info.RerankerInfo = &RerankerInfo{
  272. Documents: request.Documents,
  273. ReturnDocuments: request.GetReturnDocuments(),
  274. }
  275. return info
  276. }
  277. func GenRelayInfoOpenAIAudio(c *gin.Context, request dto.Request) *RelayInfo {
  278. info := genBaseRelayInfo(c, request)
  279. info.RelayFormat = types.RelayFormatOpenAIAudio
  280. return info
  281. }
  282. func GenRelayInfoEmbedding(c *gin.Context, request dto.Request) *RelayInfo {
  283. info := genBaseRelayInfo(c, request)
  284. info.RelayFormat = types.RelayFormatEmbedding
  285. return info
  286. }
  287. func GenRelayInfoResponses(c *gin.Context, request *dto.OpenAIResponsesRequest) *RelayInfo {
  288. info := genBaseRelayInfo(c, request)
  289. info.RelayMode = relayconstant.RelayModeResponses
  290. info.RelayFormat = types.RelayFormatOpenAIResponses
  291. info.ResponsesUsageInfo = &ResponsesUsageInfo{
  292. BuiltInTools: make(map[string]*BuildInToolInfo),
  293. }
  294. if len(request.Tools) > 0 {
  295. for _, tool := range request.GetToolsMap() {
  296. toolType := common.Interface2String(tool["type"])
  297. info.ResponsesUsageInfo.BuiltInTools[toolType] = &BuildInToolInfo{
  298. ToolName: toolType,
  299. CallCount: 0,
  300. }
  301. switch toolType {
  302. case dto.BuildInToolWebSearchPreview:
  303. searchContextSize := common.Interface2String(tool["search_context_size"])
  304. if searchContextSize == "" {
  305. searchContextSize = "medium"
  306. }
  307. info.ResponsesUsageInfo.BuiltInTools[toolType].SearchContextSize = searchContextSize
  308. }
  309. }
  310. }
  311. return info
  312. }
  313. func GenRelayInfoGemini(c *gin.Context, request dto.Request) *RelayInfo {
  314. info := genBaseRelayInfo(c, request)
  315. info.RelayFormat = types.RelayFormatGemini
  316. info.ShouldIncludeUsage = false
  317. return info
  318. }
  319. func GenRelayInfoImage(c *gin.Context, request dto.Request) *RelayInfo {
  320. info := genBaseRelayInfo(c, request)
  321. info.RelayFormat = types.RelayFormatOpenAIImage
  322. return info
  323. }
  324. func GenRelayInfoOpenAI(c *gin.Context, request dto.Request) *RelayInfo {
  325. info := genBaseRelayInfo(c, request)
  326. info.RelayFormat = types.RelayFormatOpenAI
  327. return info
  328. }
  329. func genBaseRelayInfo(c *gin.Context, request dto.Request) *RelayInfo {
  330. //channelType := common.GetContextKeyInt(c, constant.ContextKeyChannelType)
  331. //channelId := common.GetContextKeyInt(c, constant.ContextKeyChannelId)
  332. //paramOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelParamOverride)
  333. tokenGroup := common.GetContextKeyString(c, constant.ContextKeyTokenGroup)
  334. // 当令牌分组为空时,表示使用用户分组
  335. if tokenGroup == "" {
  336. tokenGroup = common.GetContextKeyString(c, constant.ContextKeyUserGroup)
  337. }
  338. startTime := common.GetContextKeyTime(c, constant.ContextKeyRequestStartTime)
  339. if startTime.IsZero() {
  340. startTime = time.Now()
  341. }
  342. isStream := false
  343. if request != nil {
  344. isStream = request.IsStream(c)
  345. }
  346. // firstResponseTime = time.Now() - 1 second
  347. info := &RelayInfo{
  348. Request: request,
  349. UserId: common.GetContextKeyInt(c, constant.ContextKeyUserId),
  350. UsingGroup: common.GetContextKeyString(c, constant.ContextKeyUsingGroup),
  351. UserGroup: common.GetContextKeyString(c, constant.ContextKeyUserGroup),
  352. UserQuota: common.GetContextKeyInt(c, constant.ContextKeyUserQuota),
  353. UserEmail: common.GetContextKeyString(c, constant.ContextKeyUserEmail),
  354. OriginModelName: common.GetContextKeyString(c, constant.ContextKeyOriginalModel),
  355. TokenId: common.GetContextKeyInt(c, constant.ContextKeyTokenId),
  356. TokenKey: common.GetContextKeyString(c, constant.ContextKeyTokenKey),
  357. TokenUnlimited: common.GetContextKeyBool(c, constant.ContextKeyTokenUnlimited),
  358. TokenGroup: tokenGroup,
  359. isFirstResponse: true,
  360. RelayMode: relayconstant.Path2RelayMode(c.Request.URL.Path),
  361. RequestURLPath: c.Request.URL.String(),
  362. IsStream: isStream,
  363. StartTime: startTime,
  364. FirstResponseTime: startTime.Add(-time.Second),
  365. ThinkingContentInfo: ThinkingContentInfo{
  366. IsFirstThinkingContent: true,
  367. SendLastThinkingContent: false,
  368. },
  369. TokenCountMeta: TokenCountMeta{
  370. //promptTokens: common.GetContextKeyInt(c, constant.ContextKeyPromptTokens),
  371. estimatePromptTokens: common.GetContextKeyInt(c, constant.ContextKeyEstimatedTokens),
  372. },
  373. }
  374. if info.RelayMode == relayconstant.RelayModeUnknown {
  375. info.RelayMode = c.GetInt("relay_mode")
  376. }
  377. if strings.HasPrefix(c.Request.URL.Path, "/pg") {
  378. info.IsPlayground = true
  379. info.RequestURLPath = strings.TrimPrefix(info.RequestURLPath, "/pg")
  380. info.RequestURLPath = "/v1" + info.RequestURLPath
  381. }
  382. userSetting, ok := common.GetContextKeyType[dto.UserSetting](c, constant.ContextKeyUserSetting)
  383. if ok {
  384. info.UserSetting = userSetting
  385. }
  386. return info
  387. }
  388. func GenRelayInfo(c *gin.Context, relayFormat types.RelayFormat, request dto.Request, ws *websocket.Conn) (*RelayInfo, error) {
  389. switch relayFormat {
  390. case types.RelayFormatOpenAI:
  391. return GenRelayInfoOpenAI(c, request), nil
  392. case types.RelayFormatOpenAIAudio:
  393. return GenRelayInfoOpenAIAudio(c, request), nil
  394. case types.RelayFormatOpenAIImage:
  395. return GenRelayInfoImage(c, request), nil
  396. case types.RelayFormatOpenAIRealtime:
  397. return GenRelayInfoWs(c, ws), nil
  398. case types.RelayFormatClaude:
  399. return GenRelayInfoClaude(c, request), nil
  400. case types.RelayFormatRerank:
  401. if request, ok := request.(*dto.RerankRequest); ok {
  402. return GenRelayInfoRerank(c, request), nil
  403. }
  404. return nil, errors.New("request is not a RerankRequest")
  405. case types.RelayFormatGemini:
  406. return GenRelayInfoGemini(c, request), nil
  407. case types.RelayFormatEmbedding:
  408. return GenRelayInfoEmbedding(c, request), nil
  409. case types.RelayFormatOpenAIResponses:
  410. if request, ok := request.(*dto.OpenAIResponsesRequest); ok {
  411. return GenRelayInfoResponses(c, request), nil
  412. }
  413. return nil, errors.New("request is not a OpenAIResponsesRequest")
  414. case types.RelayFormatTask:
  415. return genBaseRelayInfo(c, nil), nil
  416. case types.RelayFormatMjProxy:
  417. return genBaseRelayInfo(c, nil), nil
  418. default:
  419. return nil, errors.New("invalid relay format")
  420. }
  421. }
  422. //func (info *RelayInfo) SetPromptTokens(promptTokens int) {
  423. // info.promptTokens = promptTokens
  424. //}
  425. func (info *RelayInfo) SetEstimatePromptTokens(promptTokens int) {
  426. info.estimatePromptTokens = promptTokens
  427. }
  428. func (info *RelayInfo) GetEstimatePromptTokens() int {
  429. return info.estimatePromptTokens
  430. }
  431. func (info *RelayInfo) SetFirstResponseTime() {
  432. if info.isFirstResponse {
  433. info.FirstResponseTime = time.Now()
  434. info.isFirstResponse = false
  435. }
  436. }
  437. func (info *RelayInfo) HasSendResponse() bool {
  438. return info.FirstResponseTime.After(info.StartTime)
  439. }
  440. type TaskRelayInfo struct {
  441. Action string
  442. OriginTaskID string
  443. ConsumeQuota bool
  444. }
  445. type TaskSubmitReq struct {
  446. Prompt string `json:"prompt"`
  447. Model string `json:"model,omitempty"`
  448. Mode string `json:"mode,omitempty"`
  449. Image string `json:"image,omitempty"`
  450. Images []string `json:"images,omitempty"`
  451. Size string `json:"size,omitempty"`
  452. Duration int `json:"duration,omitempty"`
  453. Seconds string `json:"seconds,omitempty"`
  454. InputReference string `json:"input_reference,omitempty"`
  455. Metadata map[string]interface{} `json:"metadata,omitempty"`
  456. }
  457. func (t *TaskSubmitReq) GetPrompt() string {
  458. return t.Prompt
  459. }
  460. func (t *TaskSubmitReq) HasImage() bool {
  461. return len(t.Images) > 0
  462. }
  463. func (t *TaskSubmitReq) UnmarshalJSON(data []byte) error {
  464. type Alias TaskSubmitReq
  465. aux := &struct {
  466. Metadata json.RawMessage `json:"metadata,omitempty"`
  467. *Alias
  468. }{
  469. Alias: (*Alias)(t),
  470. }
  471. if err := common.Unmarshal(data, &aux); err != nil {
  472. return err
  473. }
  474. if len(aux.Metadata) > 0 {
  475. var metadataStr string
  476. if err := common.Unmarshal(aux.Metadata, &metadataStr); err == nil && metadataStr != "" {
  477. var metadataObj map[string]interface{}
  478. if err := common.Unmarshal([]byte(metadataStr), &metadataObj); err == nil {
  479. t.Metadata = metadataObj
  480. return nil
  481. }
  482. }
  483. var metadataObj map[string]interface{}
  484. if err := common.Unmarshal(aux.Metadata, &metadataObj); err == nil {
  485. t.Metadata = metadataObj
  486. }
  487. }
  488. return nil
  489. }
  490. func (t *TaskSubmitReq) UnmarshalMetadata(v any) error {
  491. metadata := t.Metadata
  492. if metadata != nil {
  493. metadataBytes, err := json.Marshal(metadata)
  494. if err != nil {
  495. return fmt.Errorf("marshal metadata failed: %w", err)
  496. }
  497. err = json.Unmarshal(metadataBytes, v)
  498. if err != nil {
  499. return fmt.Errorf("unmarshal metadata to target failed: %w", err)
  500. }
  501. }
  502. return nil
  503. }
  504. type TaskInfo struct {
  505. Code int `json:"code"`
  506. TaskID string `json:"task_id"`
  507. Status string `json:"status"`
  508. Reason string `json:"reason,omitempty"`
  509. Url string `json:"url,omitempty"`
  510. RemoteUrl string `json:"remote_url,omitempty"`
  511. Progress string `json:"progress,omitempty"`
  512. CompletionTokens int `json:"completion_tokens,omitempty"` // 用于按倍率计费
  513. TotalTokens int `json:"total_tokens,omitempty"` // 用于按倍率计费
  514. }
  515. func FailTaskInfo(reason string) *TaskInfo {
  516. return &TaskInfo{
  517. Status: "FAILURE",
  518. Reason: reason,
  519. }
  520. }
  521. // RemoveDisabledFields 从请求 JSON 数据中移除渠道设置中禁用的字段
  522. // service_tier: 服务层级字段,可能导致额外计费(OpenAI、Claude、Responses API 支持)
  523. // store: 数据存储授权字段,涉及用户隐私(仅 OpenAI、Responses API 支持,默认允许透传,禁用后可能导致 Codex 无法使用)
  524. // safety_identifier: 安全标识符,用于向 OpenAI 报告违规用户(仅 OpenAI 支持,涉及用户隐私)
  525. func RemoveDisabledFields(jsonData []byte, channelOtherSettings dto.ChannelOtherSettings) ([]byte, error) {
  526. var data map[string]interface{}
  527. if err := common.Unmarshal(jsonData, &data); err != nil {
  528. common.SysError("RemoveDisabledFields Unmarshal error :" + err.Error())
  529. return jsonData, nil
  530. }
  531. // 默认移除 service_tier,除非明确允许(避免额外计费风险)
  532. if !channelOtherSettings.AllowServiceTier {
  533. if _, exists := data["service_tier"]; exists {
  534. delete(data, "service_tier")
  535. }
  536. }
  537. // 默认允许 store 透传,除非明确禁用(禁用可能影响 Codex 使用)
  538. if channelOtherSettings.DisableStore {
  539. if _, exists := data["store"]; exists {
  540. delete(data, "store")
  541. }
  542. }
  543. // 默认移除 safety_identifier,除非明确允许(保护用户隐私,避免向 OpenAI 报告用户信息)
  544. if !channelOtherSettings.AllowSafetyIdentifier {
  545. if _, exists := data["safety_identifier"]; exists {
  546. delete(data, "safety_identifier")
  547. }
  548. }
  549. jsonDataAfter, err := common.Marshal(data)
  550. if err != nil {
  551. common.SysError("RemoveDisabledFields Marshal error :" + err.Error())
  552. return jsonData, nil
  553. }
  554. return jsonDataAfter, nil
  555. }
  556. // RemoveGeminiDisabledFields removes disabled fields from Gemini request JSON data
  557. // Currently supports removing functionResponse.id field which Vertex AI does not support
  558. func RemoveGeminiDisabledFields(jsonData []byte) ([]byte, error) {
  559. if !model_setting.GetGeminiSettings().RemoveFunctionResponseIdEnabled {
  560. return jsonData, nil
  561. }
  562. var data map[string]interface{}
  563. if err := common.Unmarshal(jsonData, &data); err != nil {
  564. common.SysError("RemoveGeminiDisabledFields Unmarshal error: " + err.Error())
  565. return jsonData, nil
  566. }
  567. // Process contents array
  568. // Handle both camelCase (functionResponse) and snake_case (function_response)
  569. if contents, ok := data["contents"].([]interface{}); ok {
  570. for _, content := range contents {
  571. if contentMap, ok := content.(map[string]interface{}); ok {
  572. if parts, ok := contentMap["parts"].([]interface{}); ok {
  573. for _, part := range parts {
  574. if partMap, ok := part.(map[string]interface{}); ok {
  575. // Check functionResponse (camelCase)
  576. if funcResp, ok := partMap["functionResponse"].(map[string]interface{}); ok {
  577. delete(funcResp, "id")
  578. }
  579. // Check function_response (snake_case)
  580. if funcResp, ok := partMap["function_response"].(map[string]interface{}); ok {
  581. delete(funcResp, "id")
  582. }
  583. }
  584. }
  585. }
  586. }
  587. }
  588. }
  589. jsonDataAfter, err := common.Marshal(data)
  590. if err != nil {
  591. common.SysError("RemoveGeminiDisabledFields Marshal error: " + err.Error())
  592. return jsonData, nil
  593. }
  594. return jsonDataAfter, nil
  595. }