relay_info.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. ReceivedResponseCount int
  105. FinalPreConsumedQuota int // 最终预消耗的配额
  106. // BillingSource indicates whether this request is billed from wallet quota or subscription.
  107. // "" or "wallet" => wallet; "subscription" => subscription
  108. BillingSource string
  109. // SubscriptionId is the user_subscriptions.id used when BillingSource == "subscription"
  110. SubscriptionId int
  111. // SubscriptionPreConsumed is the amount pre-consumed on subscription item (quota units or 1)
  112. SubscriptionPreConsumed int64
  113. // SubscriptionPostDelta is the post-consume delta applied to amount_used (quota units; can be negative).
  114. SubscriptionPostDelta int64
  115. // SubscriptionPlanId / SubscriptionPlanTitle are used for logging/UI display.
  116. SubscriptionPlanId int
  117. SubscriptionPlanTitle string
  118. // RequestId is used for idempotent pre-consume/refund
  119. RequestId string
  120. // SubscriptionAmountTotal / SubscriptionAmountUsedAfterPreConsume are used to compute remaining in logs.
  121. SubscriptionAmountTotal int64
  122. SubscriptionAmountUsedAfterPreConsume int64
  123. IsClaudeBetaQuery bool // /v1/messages?beta=true
  124. IsChannelTest bool // channel test request
  125. PriceData types.PriceData
  126. Request dto.Request
  127. // RequestConversionChain records request format conversions in order, e.g.
  128. // ["openai", "openai_responses"] or ["openai", "claude"].
  129. RequestConversionChain []types.RelayFormat
  130. ThinkingContentInfo
  131. TokenCountMeta
  132. *ClaudeConvertInfo
  133. *RerankerInfo
  134. *ResponsesUsageInfo
  135. *ChannelMeta
  136. *TaskRelayInfo
  137. }
  138. func (info *RelayInfo) InitChannelMeta(c *gin.Context) {
  139. channelType := common.GetContextKeyInt(c, constant.ContextKeyChannelType)
  140. paramOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelParamOverride)
  141. headerOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelHeaderOverride)
  142. apiType, _ := common.ChannelType2APIType(channelType)
  143. channelMeta := &ChannelMeta{
  144. ChannelType: channelType,
  145. ChannelId: common.GetContextKeyInt(c, constant.ContextKeyChannelId),
  146. ChannelIsMultiKey: common.GetContextKeyBool(c, constant.ContextKeyChannelIsMultiKey),
  147. ChannelMultiKeyIndex: common.GetContextKeyInt(c, constant.ContextKeyChannelMultiKeyIndex),
  148. ChannelBaseUrl: common.GetContextKeyString(c, constant.ContextKeyChannelBaseUrl),
  149. ApiType: apiType,
  150. ApiVersion: c.GetString("api_version"),
  151. ApiKey: common.GetContextKeyString(c, constant.ContextKeyChannelKey),
  152. Organization: c.GetString("channel_organization"),
  153. ChannelCreateTime: c.GetInt64("channel_create_time"),
  154. ParamOverride: paramOverride,
  155. HeadersOverride: headerOverride,
  156. UpstreamModelName: common.GetContextKeyString(c, constant.ContextKeyOriginalModel),
  157. IsModelMapped: false,
  158. SupportStreamOptions: false,
  159. }
  160. if channelType == constant.ChannelTypeAzure {
  161. channelMeta.ApiVersion = GetAPIVersion(c)
  162. }
  163. if channelType == constant.ChannelTypeVertexAi {
  164. channelMeta.ApiVersion = c.GetString("region")
  165. }
  166. channelSetting, ok := common.GetContextKeyType[dto.ChannelSettings](c, constant.ContextKeyChannelSetting)
  167. if ok {
  168. channelMeta.ChannelSetting = channelSetting
  169. }
  170. channelOtherSettings, ok := common.GetContextKeyType[dto.ChannelOtherSettings](c, constant.ContextKeyChannelOtherSetting)
  171. if ok {
  172. channelMeta.ChannelOtherSettings = channelOtherSettings
  173. }
  174. if streamSupportedChannels[channelMeta.ChannelType] {
  175. channelMeta.SupportStreamOptions = true
  176. }
  177. info.ChannelMeta = channelMeta
  178. // reset some fields based on channel meta
  179. // 重置某些字段,例如模型名称等
  180. if info.Request != nil {
  181. info.Request.SetModelName(info.OriginModelName)
  182. }
  183. }
  184. func (info *RelayInfo) ToString() string {
  185. if info == nil {
  186. return "RelayInfo<nil>"
  187. }
  188. // Basic info
  189. b := &strings.Builder{}
  190. fmt.Fprintf(b, "RelayInfo{ ")
  191. fmt.Fprintf(b, "RelayFormat: %s, ", info.RelayFormat)
  192. fmt.Fprintf(b, "RelayMode: %d, ", info.RelayMode)
  193. fmt.Fprintf(b, "IsStream: %t, ", info.IsStream)
  194. fmt.Fprintf(b, "IsPlayground: %t, ", info.IsPlayground)
  195. fmt.Fprintf(b, "RequestURLPath: %q, ", info.RequestURLPath)
  196. fmt.Fprintf(b, "OriginModelName: %q, ", info.OriginModelName)
  197. fmt.Fprintf(b, "EstimatePromptTokens: %d, ", info.estimatePromptTokens)
  198. fmt.Fprintf(b, "ShouldIncludeUsage: %t, ", info.ShouldIncludeUsage)
  199. fmt.Fprintf(b, "DisablePing: %t, ", info.DisablePing)
  200. fmt.Fprintf(b, "SendResponseCount: %d, ", info.SendResponseCount)
  201. fmt.Fprintf(b, "FinalPreConsumedQuota: %d, ", info.FinalPreConsumedQuota)
  202. // User & token info (mask secrets)
  203. fmt.Fprintf(b, "User{ Id: %d, Email: %q, Group: %q, UsingGroup: %q, Quota: %d }, ",
  204. info.UserId, common.MaskEmail(info.UserEmail), info.UserGroup, info.UsingGroup, info.UserQuota)
  205. fmt.Fprintf(b, "Token{ Id: %d, Unlimited: %t, Key: ***masked*** }, ", info.TokenId, info.TokenUnlimited)
  206. // Time info
  207. latencyMs := info.FirstResponseTime.Sub(info.StartTime).Milliseconds()
  208. fmt.Fprintf(b, "Timing{ Start: %s, FirstResponse: %s, LatencyMs: %d }, ",
  209. info.StartTime.Format(time.RFC3339Nano), info.FirstResponseTime.Format(time.RFC3339Nano), latencyMs)
  210. // Audio / realtime
  211. if info.InputAudioFormat != "" || info.OutputAudioFormat != "" || len(info.RealtimeTools) > 0 || info.AudioUsage {
  212. fmt.Fprintf(b, "Realtime{ AudioUsage: %t, InFmt: %q, OutFmt: %q, Tools: %d }, ",
  213. info.AudioUsage, info.InputAudioFormat, info.OutputAudioFormat, len(info.RealtimeTools))
  214. }
  215. // Reasoning
  216. if info.ReasoningEffort != "" {
  217. fmt.Fprintf(b, "ReasoningEffort: %q, ", info.ReasoningEffort)
  218. }
  219. // Price data (non-sensitive)
  220. if info.PriceData.UsePrice {
  221. fmt.Fprintf(b, "PriceData{ %s }, ", info.PriceData.ToSetting())
  222. }
  223. // Channel metadata (mask ApiKey)
  224. if info.ChannelMeta != nil {
  225. cm := info.ChannelMeta
  226. 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*** }, ",
  227. cm.ChannelType, cm.ChannelId, cm.ChannelIsMultiKey, cm.ChannelMultiKeyIndex, cm.ChannelBaseUrl, cm.ApiType, cm.ApiVersion, cm.Organization, cm.ChannelCreateTime, cm.UpstreamModelName, cm.IsModelMapped, cm.SupportStreamOptions)
  228. }
  229. // Responses usage info (non-sensitive)
  230. if info.ResponsesUsageInfo != nil && len(info.ResponsesUsageInfo.BuiltInTools) > 0 {
  231. fmt.Fprintf(b, "ResponsesTools{ ")
  232. first := true
  233. for name, tool := range info.ResponsesUsageInfo.BuiltInTools {
  234. if !first {
  235. fmt.Fprintf(b, ", ")
  236. }
  237. first = false
  238. if tool != nil {
  239. fmt.Fprintf(b, "%s: calls=%d", name, tool.CallCount)
  240. } else {
  241. fmt.Fprintf(b, "%s: calls=0", name)
  242. }
  243. }
  244. fmt.Fprintf(b, " }, ")
  245. }
  246. fmt.Fprintf(b, "}")
  247. return b.String()
  248. }
  249. // 定义支持流式选项的通道类型
  250. var streamSupportedChannels = map[int]bool{
  251. constant.ChannelTypeOpenAI: true,
  252. constant.ChannelTypeAnthropic: true,
  253. constant.ChannelTypeAws: true,
  254. constant.ChannelTypeGemini: true,
  255. constant.ChannelCloudflare: true,
  256. constant.ChannelTypeAzure: true,
  257. constant.ChannelTypeVolcEngine: true,
  258. constant.ChannelTypeOllama: true,
  259. constant.ChannelTypeXai: true,
  260. constant.ChannelTypeDeepSeek: true,
  261. constant.ChannelTypeBaiduV2: true,
  262. constant.ChannelTypeZhipu_v4: true,
  263. constant.ChannelTypeAli: true,
  264. constant.ChannelTypeSubmodel: true,
  265. constant.ChannelTypeCodex: true,
  266. }
  267. func GenRelayInfoWs(c *gin.Context, ws *websocket.Conn) *RelayInfo {
  268. info := genBaseRelayInfo(c, nil)
  269. info.RelayFormat = types.RelayFormatOpenAIRealtime
  270. info.ClientWs = ws
  271. info.InputAudioFormat = "pcm16"
  272. info.OutputAudioFormat = "pcm16"
  273. info.IsFirstRequest = true
  274. return info
  275. }
  276. func GenRelayInfoClaude(c *gin.Context, request dto.Request) *RelayInfo {
  277. info := genBaseRelayInfo(c, request)
  278. info.RelayFormat = types.RelayFormatClaude
  279. info.ShouldIncludeUsage = false
  280. info.ClaudeConvertInfo = &ClaudeConvertInfo{
  281. LastMessagesType: LastMessageTypeNone,
  282. }
  283. info.IsClaudeBetaQuery = c.Query("beta") == "true" || isClaudeBetaForced(c)
  284. return info
  285. }
  286. func isClaudeBetaForced(c *gin.Context) bool {
  287. channelOtherSettings, ok := common.GetContextKeyType[dto.ChannelOtherSettings](c, constant.ContextKeyChannelOtherSetting)
  288. return ok && channelOtherSettings.ClaudeBetaQuery
  289. }
  290. func GenRelayInfoRerank(c *gin.Context, request *dto.RerankRequest) *RelayInfo {
  291. info := genBaseRelayInfo(c, request)
  292. info.RelayMode = relayconstant.RelayModeRerank
  293. info.RelayFormat = types.RelayFormatRerank
  294. info.RerankerInfo = &RerankerInfo{
  295. Documents: request.Documents,
  296. ReturnDocuments: request.GetReturnDocuments(),
  297. }
  298. return info
  299. }
  300. func GenRelayInfoOpenAIAudio(c *gin.Context, request dto.Request) *RelayInfo {
  301. info := genBaseRelayInfo(c, request)
  302. info.RelayFormat = types.RelayFormatOpenAIAudio
  303. return info
  304. }
  305. func GenRelayInfoEmbedding(c *gin.Context, request dto.Request) *RelayInfo {
  306. info := genBaseRelayInfo(c, request)
  307. info.RelayFormat = types.RelayFormatEmbedding
  308. return info
  309. }
  310. func GenRelayInfoResponses(c *gin.Context, request *dto.OpenAIResponsesRequest) *RelayInfo {
  311. info := genBaseRelayInfo(c, request)
  312. info.RelayMode = relayconstant.RelayModeResponses
  313. info.RelayFormat = types.RelayFormatOpenAIResponses
  314. info.ResponsesUsageInfo = &ResponsesUsageInfo{
  315. BuiltInTools: make(map[string]*BuildInToolInfo),
  316. }
  317. if len(request.Tools) > 0 {
  318. for _, tool := range request.GetToolsMap() {
  319. toolType := common.Interface2String(tool["type"])
  320. info.ResponsesUsageInfo.BuiltInTools[toolType] = &BuildInToolInfo{
  321. ToolName: toolType,
  322. CallCount: 0,
  323. }
  324. switch toolType {
  325. case dto.BuildInToolWebSearchPreview:
  326. searchContextSize := common.Interface2String(tool["search_context_size"])
  327. if searchContextSize == "" {
  328. searchContextSize = "medium"
  329. }
  330. info.ResponsesUsageInfo.BuiltInTools[toolType].SearchContextSize = searchContextSize
  331. }
  332. }
  333. }
  334. return info
  335. }
  336. func GenRelayInfoGemini(c *gin.Context, request dto.Request) *RelayInfo {
  337. info := genBaseRelayInfo(c, request)
  338. info.RelayFormat = types.RelayFormatGemini
  339. info.ShouldIncludeUsage = false
  340. return info
  341. }
  342. func GenRelayInfoImage(c *gin.Context, request dto.Request) *RelayInfo {
  343. info := genBaseRelayInfo(c, request)
  344. info.RelayFormat = types.RelayFormatOpenAIImage
  345. return info
  346. }
  347. func GenRelayInfoOpenAI(c *gin.Context, request dto.Request) *RelayInfo {
  348. info := genBaseRelayInfo(c, request)
  349. info.RelayFormat = types.RelayFormatOpenAI
  350. return info
  351. }
  352. func genBaseRelayInfo(c *gin.Context, request dto.Request) *RelayInfo {
  353. //channelType := common.GetContextKeyInt(c, constant.ContextKeyChannelType)
  354. //channelId := common.GetContextKeyInt(c, constant.ContextKeyChannelId)
  355. //paramOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelParamOverride)
  356. tokenGroup := common.GetContextKeyString(c, constant.ContextKeyTokenGroup)
  357. // 当令牌分组为空时,表示使用用户分组
  358. if tokenGroup == "" {
  359. tokenGroup = common.GetContextKeyString(c, constant.ContextKeyUserGroup)
  360. }
  361. startTime := common.GetContextKeyTime(c, constant.ContextKeyRequestStartTime)
  362. if startTime.IsZero() {
  363. startTime = time.Now()
  364. }
  365. isStream := false
  366. if request != nil {
  367. isStream = request.IsStream(c)
  368. }
  369. // firstResponseTime = time.Now() - 1 second
  370. reqId := common.GetContextKeyString(c, common.RequestIdKey)
  371. if reqId == "" {
  372. reqId = common.GetTimeString() + common.GetRandomString(8)
  373. }
  374. info := &RelayInfo{
  375. Request: request,
  376. RequestId: reqId,
  377. UserId: common.GetContextKeyInt(c, constant.ContextKeyUserId),
  378. UsingGroup: common.GetContextKeyString(c, constant.ContextKeyUsingGroup),
  379. UserGroup: common.GetContextKeyString(c, constant.ContextKeyUserGroup),
  380. UserQuota: common.GetContextKeyInt(c, constant.ContextKeyUserQuota),
  381. UserEmail: common.GetContextKeyString(c, constant.ContextKeyUserEmail),
  382. OriginModelName: common.GetContextKeyString(c, constant.ContextKeyOriginalModel),
  383. TokenId: common.GetContextKeyInt(c, constant.ContextKeyTokenId),
  384. TokenKey: common.GetContextKeyString(c, constant.ContextKeyTokenKey),
  385. TokenUnlimited: common.GetContextKeyBool(c, constant.ContextKeyTokenUnlimited),
  386. TokenGroup: tokenGroup,
  387. isFirstResponse: true,
  388. RelayMode: relayconstant.Path2RelayMode(c.Request.URL.Path),
  389. RequestURLPath: c.Request.URL.String(),
  390. IsStream: isStream,
  391. StartTime: startTime,
  392. FirstResponseTime: startTime.Add(-time.Second),
  393. ThinkingContentInfo: ThinkingContentInfo{
  394. IsFirstThinkingContent: true,
  395. SendLastThinkingContent: false,
  396. },
  397. TokenCountMeta: TokenCountMeta{
  398. //promptTokens: common.GetContextKeyInt(c, constant.ContextKeyPromptTokens),
  399. estimatePromptTokens: common.GetContextKeyInt(c, constant.ContextKeyEstimatedTokens),
  400. },
  401. }
  402. if info.RelayMode == relayconstant.RelayModeUnknown {
  403. info.RelayMode = c.GetInt("relay_mode")
  404. }
  405. if strings.HasPrefix(c.Request.URL.Path, "/pg") {
  406. info.IsPlayground = true
  407. info.RequestURLPath = strings.TrimPrefix(info.RequestURLPath, "/pg")
  408. info.RequestURLPath = "/v1" + info.RequestURLPath
  409. }
  410. userSetting, ok := common.GetContextKeyType[dto.UserSetting](c, constant.ContextKeyUserSetting)
  411. if ok {
  412. info.UserSetting = userSetting
  413. }
  414. return info
  415. }
  416. func GenRelayInfo(c *gin.Context, relayFormat types.RelayFormat, request dto.Request, ws *websocket.Conn) (*RelayInfo, error) {
  417. var info *RelayInfo
  418. var err error
  419. switch relayFormat {
  420. case types.RelayFormatOpenAI:
  421. info = GenRelayInfoOpenAI(c, request)
  422. case types.RelayFormatOpenAIAudio:
  423. info = GenRelayInfoOpenAIAudio(c, request)
  424. case types.RelayFormatOpenAIImage:
  425. info = GenRelayInfoImage(c, request)
  426. case types.RelayFormatOpenAIRealtime:
  427. info = GenRelayInfoWs(c, ws)
  428. case types.RelayFormatClaude:
  429. info = GenRelayInfoClaude(c, request)
  430. case types.RelayFormatRerank:
  431. if request, ok := request.(*dto.RerankRequest); ok {
  432. info = GenRelayInfoRerank(c, request)
  433. break
  434. }
  435. err = errors.New("request is not a RerankRequest")
  436. case types.RelayFormatGemini:
  437. info = GenRelayInfoGemini(c, request)
  438. case types.RelayFormatEmbedding:
  439. info = GenRelayInfoEmbedding(c, request)
  440. case types.RelayFormatOpenAIResponses:
  441. if request, ok := request.(*dto.OpenAIResponsesRequest); ok {
  442. info = GenRelayInfoResponses(c, request)
  443. break
  444. }
  445. err = errors.New("request is not a OpenAIResponsesRequest")
  446. case types.RelayFormatOpenAIResponsesCompaction:
  447. if request, ok := request.(*dto.OpenAIResponsesCompactionRequest); ok {
  448. return GenRelayInfoResponsesCompaction(c, request), nil
  449. }
  450. return nil, errors.New("request is not a OpenAIResponsesCompactionRequest")
  451. case types.RelayFormatTask:
  452. info = genBaseRelayInfo(c, nil)
  453. case types.RelayFormatMjProxy:
  454. info = genBaseRelayInfo(c, nil)
  455. default:
  456. err = errors.New("invalid relay format")
  457. }
  458. if err != nil {
  459. return nil, err
  460. }
  461. if info == nil {
  462. return nil, errors.New("failed to build relay info")
  463. }
  464. info.InitRequestConversionChain()
  465. return info, nil
  466. }
  467. func (info *RelayInfo) InitRequestConversionChain() {
  468. if info == nil {
  469. return
  470. }
  471. if len(info.RequestConversionChain) > 0 {
  472. return
  473. }
  474. if info.RelayFormat == "" {
  475. return
  476. }
  477. info.RequestConversionChain = []types.RelayFormat{info.RelayFormat}
  478. }
  479. func (info *RelayInfo) AppendRequestConversion(format types.RelayFormat) {
  480. if info == nil {
  481. return
  482. }
  483. if format == "" {
  484. return
  485. }
  486. if len(info.RequestConversionChain) == 0 {
  487. info.RequestConversionChain = []types.RelayFormat{format}
  488. return
  489. }
  490. last := info.RequestConversionChain[len(info.RequestConversionChain)-1]
  491. if last == format {
  492. return
  493. }
  494. info.RequestConversionChain = append(info.RequestConversionChain, format)
  495. }
  496. func GenRelayInfoResponsesCompaction(c *gin.Context, request *dto.OpenAIResponsesCompactionRequest) *RelayInfo {
  497. info := genBaseRelayInfo(c, request)
  498. if info.RelayMode == relayconstant.RelayModeUnknown {
  499. info.RelayMode = relayconstant.RelayModeResponsesCompact
  500. }
  501. info.RelayFormat = types.RelayFormatOpenAIResponsesCompaction
  502. return info
  503. }
  504. //func (info *RelayInfo) SetPromptTokens(promptTokens int) {
  505. // info.promptTokens = promptTokens
  506. //}
  507. func (info *RelayInfo) SetEstimatePromptTokens(promptTokens int) {
  508. info.estimatePromptTokens = promptTokens
  509. }
  510. func (info *RelayInfo) GetEstimatePromptTokens() int {
  511. return info.estimatePromptTokens
  512. }
  513. func (info *RelayInfo) SetFirstResponseTime() {
  514. if info.isFirstResponse {
  515. info.FirstResponseTime = time.Now()
  516. info.isFirstResponse = false
  517. }
  518. }
  519. func (info *RelayInfo) HasSendResponse() bool {
  520. return info.FirstResponseTime.After(info.StartTime)
  521. }
  522. type TaskRelayInfo struct {
  523. Action string
  524. OriginTaskID string
  525. ConsumeQuota bool
  526. }
  527. type TaskSubmitReq struct {
  528. Prompt string `json:"prompt"`
  529. Model string `json:"model,omitempty"`
  530. Mode string `json:"mode,omitempty"`
  531. Image string `json:"image,omitempty"`
  532. Images []string `json:"images,omitempty"`
  533. Size string `json:"size,omitempty"`
  534. Duration int `json:"duration,omitempty"`
  535. Seconds string `json:"seconds,omitempty"`
  536. InputReference string `json:"input_reference,omitempty"`
  537. Metadata map[string]interface{} `json:"metadata,omitempty"`
  538. }
  539. func (t *TaskSubmitReq) GetPrompt() string {
  540. return t.Prompt
  541. }
  542. func (t *TaskSubmitReq) HasImage() bool {
  543. return len(t.Images) > 0
  544. }
  545. func (t *TaskSubmitReq) UnmarshalJSON(data []byte) error {
  546. type Alias TaskSubmitReq
  547. aux := &struct {
  548. Metadata json.RawMessage `json:"metadata,omitempty"`
  549. *Alias
  550. }{
  551. Alias: (*Alias)(t),
  552. }
  553. if err := common.Unmarshal(data, &aux); err != nil {
  554. return err
  555. }
  556. if len(aux.Metadata) > 0 {
  557. var metadataStr string
  558. if err := common.Unmarshal(aux.Metadata, &metadataStr); err == nil && metadataStr != "" {
  559. var metadataObj map[string]interface{}
  560. if err := common.Unmarshal([]byte(metadataStr), &metadataObj); err == nil {
  561. t.Metadata = metadataObj
  562. return nil
  563. }
  564. }
  565. var metadataObj map[string]interface{}
  566. if err := common.Unmarshal(aux.Metadata, &metadataObj); err == nil {
  567. t.Metadata = metadataObj
  568. }
  569. }
  570. return nil
  571. }
  572. func (t *TaskSubmitReq) UnmarshalMetadata(v any) error {
  573. metadata := t.Metadata
  574. if metadata != nil {
  575. metadataBytes, err := json.Marshal(metadata)
  576. if err != nil {
  577. return fmt.Errorf("marshal metadata failed: %w", err)
  578. }
  579. err = json.Unmarshal(metadataBytes, v)
  580. if err != nil {
  581. return fmt.Errorf("unmarshal metadata to target failed: %w", err)
  582. }
  583. }
  584. return nil
  585. }
  586. type TaskInfo struct {
  587. Code int `json:"code"`
  588. TaskID string `json:"task_id"`
  589. Status string `json:"status"`
  590. Reason string `json:"reason,omitempty"`
  591. Url string `json:"url,omitempty"`
  592. RemoteUrl string `json:"remote_url,omitempty"`
  593. Progress string `json:"progress,omitempty"`
  594. CompletionTokens int `json:"completion_tokens,omitempty"` // 用于按倍率计费
  595. TotalTokens int `json:"total_tokens,omitempty"` // 用于按倍率计费
  596. }
  597. func FailTaskInfo(reason string) *TaskInfo {
  598. return &TaskInfo{
  599. Status: "FAILURE",
  600. Reason: reason,
  601. }
  602. }
  603. // RemoveDisabledFields 从请求 JSON 数据中移除渠道设置中禁用的字段
  604. // service_tier: 服务层级字段,可能导致额外计费(OpenAI、Claude、Responses API 支持)
  605. // store: 数据存储授权字段,涉及用户隐私(仅 OpenAI、Responses API 支持,默认允许透传,禁用后可能导致 Codex 无法使用)
  606. // safety_identifier: 安全标识符,用于向 OpenAI 报告违规用户(仅 OpenAI 支持,涉及用户隐私)
  607. func RemoveDisabledFields(jsonData []byte, channelOtherSettings dto.ChannelOtherSettings) ([]byte, error) {
  608. var data map[string]interface{}
  609. if err := common.Unmarshal(jsonData, &data); err != nil {
  610. common.SysError("RemoveDisabledFields Unmarshal error :" + err.Error())
  611. return jsonData, nil
  612. }
  613. // 默认移除 service_tier,除非明确允许(避免额外计费风险)
  614. if !channelOtherSettings.AllowServiceTier {
  615. if _, exists := data["service_tier"]; exists {
  616. delete(data, "service_tier")
  617. }
  618. }
  619. // 默认允许 store 透传,除非明确禁用(禁用可能影响 Codex 使用)
  620. if channelOtherSettings.DisableStore {
  621. if _, exists := data["store"]; exists {
  622. delete(data, "store")
  623. }
  624. }
  625. // 默认移除 safety_identifier,除非明确允许(保护用户隐私,避免向 OpenAI 报告用户信息)
  626. if !channelOtherSettings.AllowSafetyIdentifier {
  627. if _, exists := data["safety_identifier"]; exists {
  628. delete(data, "safety_identifier")
  629. }
  630. }
  631. jsonDataAfter, err := common.Marshal(data)
  632. if err != nil {
  633. common.SysError("RemoveDisabledFields Marshal error :" + err.Error())
  634. return jsonData, nil
  635. }
  636. return jsonDataAfter, nil
  637. }
  638. // RemoveGeminiDisabledFields removes disabled fields from Gemini request JSON data
  639. // Currently supports removing functionResponse.id field which Vertex AI does not support
  640. func RemoveGeminiDisabledFields(jsonData []byte) ([]byte, error) {
  641. if !model_setting.GetGeminiSettings().RemoveFunctionResponseIdEnabled {
  642. return jsonData, nil
  643. }
  644. var data map[string]interface{}
  645. if err := common.Unmarshal(jsonData, &data); err != nil {
  646. common.SysError("RemoveGeminiDisabledFields Unmarshal error: " + err.Error())
  647. return jsonData, nil
  648. }
  649. // Process contents array
  650. // Handle both camelCase (functionResponse) and snake_case (function_response)
  651. if contents, ok := data["contents"].([]interface{}); ok {
  652. for _, content := range contents {
  653. if contentMap, ok := content.(map[string]interface{}); ok {
  654. if parts, ok := contentMap["parts"].([]interface{}); ok {
  655. for _, part := range parts {
  656. if partMap, ok := part.(map[string]interface{}); ok {
  657. // Check functionResponse (camelCase)
  658. if funcResp, ok := partMap["functionResponse"].(map[string]interface{}); ok {
  659. delete(funcResp, "id")
  660. }
  661. // Check function_response (snake_case)
  662. if funcResp, ok := partMap["function_response"].(map[string]interface{}); ok {
  663. delete(funcResp, "id")
  664. }
  665. }
  666. }
  667. }
  668. }
  669. }
  670. }
  671. jsonDataAfter, err := common.Marshal(data)
  672. if err != nil {
  673. common.SysError("RemoveGeminiDisabledFields Marshal error: " + err.Error())
  674. return jsonData, nil
  675. }
  676. return jsonDataAfter, nil
  677. }