您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

872 行
28 KiB

  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. ToolCallBaseIndex int
  35. ToolCallMaxIndexOffset int
  36. }
  37. type RerankerInfo struct {
  38. Documents []any
  39. ReturnDocuments bool
  40. }
  41. type BuildInToolInfo struct {
  42. ToolName string
  43. CallCount int
  44. SearchContextSize string
  45. }
  46. type ResponsesUsageInfo struct {
  47. BuiltInTools map[string]*BuildInToolInfo
  48. }
  49. type ChannelMeta struct {
  50. ChannelType int
  51. ChannelId int
  52. ChannelIsMultiKey bool
  53. ChannelMultiKeyIndex int
  54. ChannelBaseUrl string
  55. ApiType int
  56. ApiVersion string
  57. ApiKey string
  58. Organization string
  59. ChannelCreateTime int64
  60. ParamOverride map[string]interface{}
  61. HeadersOverride map[string]interface{}
  62. ChannelSetting dto.ChannelSettings
  63. ChannelOtherSettings dto.ChannelOtherSettings
  64. UpstreamModelName string
  65. IsModelMapped bool
  66. SupportStreamOptions bool // 是否支持流式选项
  67. }
  68. type TokenCountMeta struct {
  69. //promptTokens int
  70. estimatePromptTokens int
  71. }
  72. type RelayInfo struct {
  73. TokenId int
  74. TokenKey string
  75. TokenGroup string
  76. UserId int
  77. ChatID string
  78. UpstreamID string
  79. UsingGroup string // 使用的分组,当auto跨分组重试时,会变动
  80. UserGroup string // 用户所在分组
  81. TokenUnlimited bool
  82. StartTime time.Time
  83. FirstResponseTime time.Time
  84. isFirstResponse bool
  85. //SendLastReasoningResponse bool
  86. IsStream bool
  87. IsGeminiBatchEmbedding bool
  88. IsPlayground bool
  89. UsePrice bool
  90. RelayMode int
  91. OriginModelName string
  92. RequestURLPath string
  93. ShouldIncludeUsage bool
  94. DisablePing bool // 是否禁止向下游发送自定义 Ping
  95. ClientWs *websocket.Conn
  96. TargetWs *websocket.Conn
  97. InputAudioFormat string
  98. OutputAudioFormat string
  99. RealtimeTools []dto.RealTimeTool
  100. IsFirstRequest bool
  101. AudioUsage bool
  102. ReasoningEffort string
  103. UserSetting dto.UserSetting
  104. UserEmail string
  105. UserQuota int
  106. RelayFormat types.RelayFormat
  107. SendResponseCount int
  108. ReceivedResponseCount int
  109. FinalPreConsumedQuota int // 最终预消耗的配额
  110. // ForcePreConsume 为 true 时禁用 BillingSession 的信任额度旁路,
  111. // 强制预扣全额。用于异步任务(视频/音乐生成等),因为请求返回后任务仍在运行,
  112. // 必须在提交前锁定全额。
  113. ForcePreConsume bool
  114. // Billing 是计费会话,封装了预扣费/结算/退款的统一生命周期。
  115. // 免费模型时为 nil。
  116. Billing BillingSettler
  117. // BillingSource indicates whether this request is billed from wallet quota or subscription.
  118. // "" or "wallet" => wallet; "subscription" => subscription
  119. BillingSource string
  120. // SubscriptionId is the user_subscriptions.id used when BillingSource == "subscription"
  121. SubscriptionId int
  122. // SubscriptionPreConsumed is the amount pre-consumed on subscription item (quota units or 1)
  123. SubscriptionPreConsumed int64
  124. // SubscriptionPostDelta is the post-consume delta applied to amount_used (quota units; can be negative).
  125. SubscriptionPostDelta int64
  126. // SubscriptionPlanId / SubscriptionPlanTitle are used for logging/UI display.
  127. SubscriptionPlanId int
  128. SubscriptionPlanTitle string
  129. // RequestId is used for idempotent pre-consume/refund
  130. RequestId string
  131. // SubscriptionAmountTotal / SubscriptionAmountUsedAfterPreConsume are used to compute remaining in logs.
  132. SubscriptionAmountTotal int64
  133. SubscriptionAmountUsedAfterPreConsume int64
  134. IsClaudeBetaQuery bool // /v1/messages?beta=true
  135. IsChannelTest bool // channel test request
  136. RetryIndex int
  137. LastError *types.NewAPIError
  138. RequestHeaders map[string]string
  139. RuntimeHeadersOverride map[string]interface{}
  140. UseRuntimeHeadersOverride bool
  141. ParamOverrideAudit []string
  142. PriceData types.PriceData
  143. Request dto.Request
  144. // RequestConversionChain records request format conversions in order, e.g.
  145. // ["openai", "openai_responses"] or ["openai", "claude"].
  146. RequestConversionChain []types.RelayFormat
  147. // 最终请求到上游的格式。可由 adaptor 显式设置;
  148. // 若为空,调用 GetFinalRequestRelayFormat 会回退到 RequestConversionChain 的最后一项或 RelayFormat。
  149. FinalRequestRelayFormat types.RelayFormat
  150. ThinkingContentInfo
  151. TokenCountMeta
  152. *ClaudeConvertInfo
  153. *RerankerInfo
  154. *ResponsesUsageInfo
  155. *ChannelMeta
  156. *TaskRelayInfo
  157. }
  158. func (info *RelayInfo) InitChannelMeta(c *gin.Context) {
  159. channelType := common.GetContextKeyInt(c, constant.ContextKeyChannelType)
  160. paramOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelParamOverride)
  161. headerOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelHeaderOverride)
  162. apiType, _ := common.ChannelType2APIType(channelType)
  163. channelMeta := &ChannelMeta{
  164. ChannelType: channelType,
  165. ChannelId: common.GetContextKeyInt(c, constant.ContextKeyChannelId),
  166. ChannelIsMultiKey: common.GetContextKeyBool(c, constant.ContextKeyChannelIsMultiKey),
  167. ChannelMultiKeyIndex: common.GetContextKeyInt(c, constant.ContextKeyChannelMultiKeyIndex),
  168. ChannelBaseUrl: common.GetContextKeyString(c, constant.ContextKeyChannelBaseUrl),
  169. ApiType: apiType,
  170. ApiVersion: c.GetString("api_version"),
  171. ApiKey: common.GetContextKeyString(c, constant.ContextKeyChannelKey),
  172. Organization: c.GetString("channel_organization"),
  173. ChannelCreateTime: c.GetInt64("channel_create_time"),
  174. ParamOverride: paramOverride,
  175. HeadersOverride: headerOverride,
  176. UpstreamModelName: common.GetContextKeyString(c, constant.ContextKeyOriginalModel),
  177. IsModelMapped: false,
  178. SupportStreamOptions: false,
  179. }
  180. if channelType == constant.ChannelTypeAzure {
  181. channelMeta.ApiVersion = GetAPIVersion(c)
  182. }
  183. if channelType == constant.ChannelTypeVertexAi {
  184. channelMeta.ApiVersion = c.GetString("region")
  185. }
  186. channelSetting, ok := common.GetContextKeyType[dto.ChannelSettings](c, constant.ContextKeyChannelSetting)
  187. if ok {
  188. channelMeta.ChannelSetting = channelSetting
  189. }
  190. channelOtherSettings, ok := common.GetContextKeyType[dto.ChannelOtherSettings](c, constant.ContextKeyChannelOtherSetting)
  191. if ok {
  192. channelMeta.ChannelOtherSettings = channelOtherSettings
  193. }
  194. if streamSupportedChannels[channelMeta.ChannelType] {
  195. channelMeta.SupportStreamOptions = true
  196. }
  197. info.ChannelMeta = channelMeta
  198. // reset some fields based on channel meta
  199. // 重置某些字段,例如模型名称等
  200. if info.Request != nil {
  201. info.Request.SetModelName(info.OriginModelName)
  202. }
  203. }
  204. func (info *RelayInfo) ToString() string {
  205. if info == nil {
  206. return "RelayInfo<nil>"
  207. }
  208. // Basic info
  209. b := &strings.Builder{}
  210. fmt.Fprintf(b, "RelayInfo{ ")
  211. fmt.Fprintf(b, "RelayFormat: %s, ", info.RelayFormat)
  212. fmt.Fprintf(b, "RelayMode: %d, ", info.RelayMode)
  213. fmt.Fprintf(b, "IsStream: %t, ", info.IsStream)
  214. fmt.Fprintf(b, "IsPlayground: %t, ", info.IsPlayground)
  215. fmt.Fprintf(b, "RequestURLPath: %q, ", info.RequestURLPath)
  216. fmt.Fprintf(b, "OriginModelName: %q, ", info.OriginModelName)
  217. fmt.Fprintf(b, "EstimatePromptTokens: %d, ", info.estimatePromptTokens)
  218. fmt.Fprintf(b, "ShouldIncludeUsage: %t, ", info.ShouldIncludeUsage)
  219. fmt.Fprintf(b, "DisablePing: %t, ", info.DisablePing)
  220. fmt.Fprintf(b, "SendResponseCount: %d, ", info.SendResponseCount)
  221. fmt.Fprintf(b, "FinalPreConsumedQuota: %d, ", info.FinalPreConsumedQuota)
  222. // User & token info (mask secrets)
  223. fmt.Fprintf(b, "User{ Id: %d, Email: %q, Group: %q, UsingGroup: %q, Quota: %d }, ",
  224. info.UserId, common.MaskEmail(info.UserEmail), info.UserGroup, info.UsingGroup, info.UserQuota)
  225. fmt.Fprintf(b, "Token{ Id: %d, Unlimited: %t, Key: ***masked*** }, ", info.TokenId, info.TokenUnlimited)
  226. // Time info
  227. latencyMs := info.FirstResponseTime.Sub(info.StartTime).Milliseconds()
  228. fmt.Fprintf(b, "Timing{ Start: %s, FirstResponse: %s, LatencyMs: %d }, ",
  229. info.StartTime.Format(time.RFC3339Nano), info.FirstResponseTime.Format(time.RFC3339Nano), latencyMs)
  230. // Audio / realtime
  231. if info.InputAudioFormat != "" || info.OutputAudioFormat != "" || len(info.RealtimeTools) > 0 || info.AudioUsage {
  232. fmt.Fprintf(b, "Realtime{ AudioUsage: %t, InFmt: %q, OutFmt: %q, Tools: %d }, ",
  233. info.AudioUsage, info.InputAudioFormat, info.OutputAudioFormat, len(info.RealtimeTools))
  234. }
  235. // Reasoning
  236. if info.ReasoningEffort != "" {
  237. fmt.Fprintf(b, "ReasoningEffort: %q, ", info.ReasoningEffort)
  238. }
  239. // Price data (non-sensitive)
  240. if info.PriceData.UsePrice {
  241. fmt.Fprintf(b, "PriceData{ %s }, ", info.PriceData.ToSetting())
  242. }
  243. // Channel metadata (mask ApiKey)
  244. if info.ChannelMeta != nil {
  245. cm := info.ChannelMeta
  246. 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*** }, ",
  247. cm.ChannelType, cm.ChannelId, cm.ChannelIsMultiKey, cm.ChannelMultiKeyIndex, cm.ChannelBaseUrl, cm.ApiType, cm.ApiVersion, cm.Organization, cm.ChannelCreateTime, cm.UpstreamModelName, cm.IsModelMapped, cm.SupportStreamOptions)
  248. }
  249. // Responses usage info (non-sensitive)
  250. if info.ResponsesUsageInfo != nil && len(info.ResponsesUsageInfo.BuiltInTools) > 0 {
  251. fmt.Fprintf(b, "ResponsesTools{ ")
  252. first := true
  253. for name, tool := range info.ResponsesUsageInfo.BuiltInTools {
  254. if !first {
  255. fmt.Fprintf(b, ", ")
  256. }
  257. first = false
  258. if tool != nil {
  259. fmt.Fprintf(b, "%s: calls=%d", name, tool.CallCount)
  260. } else {
  261. fmt.Fprintf(b, "%s: calls=0", name)
  262. }
  263. }
  264. fmt.Fprintf(b, " }, ")
  265. }
  266. fmt.Fprintf(b, "}")
  267. return b.String()
  268. }
  269. // 定义支持流式选项的通道类型
  270. var streamSupportedChannels = map[int]bool{
  271. constant.ChannelTypeOpenAI: true,
  272. constant.ChannelTypeAnthropic: true,
  273. constant.ChannelTypeAws: true,
  274. constant.ChannelTypeGemini: true,
  275. constant.ChannelCloudflare: true,
  276. constant.ChannelTypeAzure: true,
  277. constant.ChannelTypeVolcEngine: true,
  278. constant.ChannelTypeOllama: true,
  279. constant.ChannelTypeXai: true,
  280. constant.ChannelTypeDeepSeek: true,
  281. constant.ChannelTypeBaiduV2: true,
  282. constant.ChannelTypeZhipu_v4: true,
  283. constant.ChannelTypeAli: true,
  284. constant.ChannelTypeSubmodel: true,
  285. constant.ChannelTypeCodex: true,
  286. constant.ChannelTypeMoonshot: true,
  287. constant.ChannelTypeMiniMax: true,
  288. constant.ChannelTypeSiliconFlow: true,
  289. }
  290. func GenRelayInfoWs(c *gin.Context, ws *websocket.Conn) *RelayInfo {
  291. info := genBaseRelayInfo(c, nil)
  292. info.RelayFormat = types.RelayFormatOpenAIRealtime
  293. info.ClientWs = ws
  294. info.InputAudioFormat = "pcm16"
  295. info.OutputAudioFormat = "pcm16"
  296. info.IsFirstRequest = true
  297. return info
  298. }
  299. func GenRelayInfoClaude(c *gin.Context, request dto.Request) *RelayInfo {
  300. info := genBaseRelayInfo(c, request)
  301. info.RelayFormat = types.RelayFormatClaude
  302. info.ShouldIncludeUsage = false
  303. info.ClaudeConvertInfo = &ClaudeConvertInfo{
  304. LastMessagesType: LastMessageTypeNone,
  305. }
  306. info.IsClaudeBetaQuery = c.Query("beta") == "true" || isClaudeBetaForced(c)
  307. return info
  308. }
  309. func isClaudeBetaForced(c *gin.Context) bool {
  310. channelOtherSettings, ok := common.GetContextKeyType[dto.ChannelOtherSettings](c, constant.ContextKeyChannelOtherSetting)
  311. return ok && channelOtherSettings.ClaudeBetaQuery
  312. }
  313. func GenRelayInfoRerank(c *gin.Context, request *dto.RerankRequest) *RelayInfo {
  314. info := genBaseRelayInfo(c, request)
  315. info.RelayMode = relayconstant.RelayModeRerank
  316. info.RelayFormat = types.RelayFormatRerank
  317. info.RerankerInfo = &RerankerInfo{
  318. Documents: request.Documents,
  319. ReturnDocuments: request.GetReturnDocuments(),
  320. }
  321. return info
  322. }
  323. func GenRelayInfoOpenAIAudio(c *gin.Context, request dto.Request) *RelayInfo {
  324. info := genBaseRelayInfo(c, request)
  325. info.RelayFormat = types.RelayFormatOpenAIAudio
  326. return info
  327. }
  328. func GenRelayInfoEmbedding(c *gin.Context, request dto.Request) *RelayInfo {
  329. info := genBaseRelayInfo(c, request)
  330. info.RelayFormat = types.RelayFormatEmbedding
  331. return info
  332. }
  333. func GenRelayInfoResponses(c *gin.Context, request *dto.OpenAIResponsesRequest) *RelayInfo {
  334. info := genBaseRelayInfo(c, request)
  335. info.RelayMode = relayconstant.RelayModeResponses
  336. info.RelayFormat = types.RelayFormatOpenAIResponses
  337. info.ResponsesUsageInfo = &ResponsesUsageInfo{
  338. BuiltInTools: make(map[string]*BuildInToolInfo),
  339. }
  340. if len(request.Tools) > 0 {
  341. for _, tool := range request.GetToolsMap() {
  342. toolType := common.Interface2String(tool["type"])
  343. info.ResponsesUsageInfo.BuiltInTools[toolType] = &BuildInToolInfo{
  344. ToolName: toolType,
  345. CallCount: 0,
  346. }
  347. switch toolType {
  348. case dto.BuildInToolWebSearchPreview:
  349. searchContextSize := common.Interface2String(tool["search_context_size"])
  350. if searchContextSize == "" {
  351. searchContextSize = "medium"
  352. }
  353. info.ResponsesUsageInfo.BuiltInTools[toolType].SearchContextSize = searchContextSize
  354. }
  355. }
  356. }
  357. return info
  358. }
  359. func GenRelayInfoGemini(c *gin.Context, request dto.Request) *RelayInfo {
  360. info := genBaseRelayInfo(c, request)
  361. info.RelayFormat = types.RelayFormatGemini
  362. info.ShouldIncludeUsage = false
  363. return info
  364. }
  365. func GenRelayInfoImage(c *gin.Context, request dto.Request) *RelayInfo {
  366. info := genBaseRelayInfo(c, request)
  367. info.RelayFormat = types.RelayFormatOpenAIImage
  368. return info
  369. }
  370. func GenRelayInfoOpenAI(c *gin.Context, request dto.Request) *RelayInfo {
  371. info := genBaseRelayInfo(c, request)
  372. info.RelayFormat = types.RelayFormatOpenAI
  373. return info
  374. }
  375. func genBaseRelayInfo(c *gin.Context, request dto.Request) *RelayInfo {
  376. //channelType := common.GetContextKeyInt(c, constant.ContextKeyChannelType)
  377. //channelId := common.GetContextKeyInt(c, constant.ContextKeyChannelId)
  378. //paramOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelParamOverride)
  379. tokenGroup := common.GetContextKeyString(c, constant.ContextKeyTokenGroup)
  380. // 当令牌分组为空时,表示使用用户分组
  381. if tokenGroup == "" {
  382. tokenGroup = common.GetContextKeyString(c, constant.ContextKeyUserGroup)
  383. }
  384. startTime := common.GetContextKeyTime(c, constant.ContextKeyRequestStartTime)
  385. if startTime.IsZero() {
  386. startTime = time.Now()
  387. }
  388. isStream := false
  389. if request != nil {
  390. isStream = request.IsStream(c)
  391. }
  392. // firstResponseTime = time.Now() - 1 second
  393. reqId := common.GetContextKeyString(c, common.RequestIdKey)
  394. if reqId == "" {
  395. reqId = common.GetTimeString() + common.GetRandomString(8)
  396. }
  397. info := &RelayInfo{
  398. Request: request,
  399. RequestId: reqId,
  400. UserId: common.GetContextKeyInt(c, constant.ContextKeyUserId),
  401. ChatID: GetRelayChatID(c),
  402. UsingGroup: common.GetContextKeyString(c, constant.ContextKeyUsingGroup),
  403. UserGroup: common.GetContextKeyString(c, constant.ContextKeyUserGroup),
  404. UserQuota: common.GetContextKeyInt(c, constant.ContextKeyUserQuota),
  405. UserEmail: common.GetContextKeyString(c, constant.ContextKeyUserEmail),
  406. OriginModelName: common.GetContextKeyString(c, constant.ContextKeyOriginalModel),
  407. TokenId: common.GetContextKeyInt(c, constant.ContextKeyTokenId),
  408. TokenKey: common.GetContextKeyString(c, constant.ContextKeyTokenKey),
  409. TokenUnlimited: common.GetContextKeyBool(c, constant.ContextKeyTokenUnlimited),
  410. TokenGroup: tokenGroup,
  411. isFirstResponse: true,
  412. RelayMode: relayconstant.Path2RelayMode(c.Request.URL.Path),
  413. RequestURLPath: c.Request.URL.String(),
  414. IsStream: isStream,
  415. StartTime: startTime,
  416. FirstResponseTime: startTime.Add(-time.Second),
  417. ThinkingContentInfo: ThinkingContentInfo{
  418. IsFirstThinkingContent: true,
  419. SendLastThinkingContent: false,
  420. },
  421. TokenCountMeta: TokenCountMeta{
  422. //promptTokens: common.GetContextKeyInt(c, constant.ContextKeyPromptTokens),
  423. estimatePromptTokens: common.GetContextKeyInt(c, constant.ContextKeyEstimatedTokens),
  424. },
  425. RequestHeaders: cloneRequestHeaders(c),
  426. }
  427. if info.RelayMode == relayconstant.RelayModeUnknown {
  428. info.RelayMode = c.GetInt("relay_mode")
  429. }
  430. if strings.HasPrefix(c.Request.URL.Path, "/pg") {
  431. info.IsPlayground = true
  432. info.RequestURLPath = strings.TrimPrefix(info.RequestURLPath, "/pg")
  433. info.RequestURLPath = "/v1" + info.RequestURLPath
  434. }
  435. userSetting, ok := common.GetContextKeyType[dto.UserSetting](c, constant.ContextKeyUserSetting)
  436. if ok {
  437. info.UserSetting = userSetting
  438. }
  439. return info
  440. }
  441. func cloneRequestHeaders(c *gin.Context) map[string]string {
  442. if c == nil || c.Request == nil {
  443. return nil
  444. }
  445. if len(c.Request.Header) == 0 {
  446. return nil
  447. }
  448. headers := make(map[string]string, len(c.Request.Header))
  449. for key := range c.Request.Header {
  450. value := strings.TrimSpace(c.Request.Header.Get(key))
  451. if value == "" {
  452. continue
  453. }
  454. headers[key] = value
  455. }
  456. if len(headers) == 0 {
  457. return nil
  458. }
  459. return headers
  460. }
  461. func GenRelayInfo(c *gin.Context, relayFormat types.RelayFormat, request dto.Request, ws *websocket.Conn) (*RelayInfo, error) {
  462. var info *RelayInfo
  463. var err error
  464. switch relayFormat {
  465. case types.RelayFormatOpenAI:
  466. info = GenRelayInfoOpenAI(c, request)
  467. case types.RelayFormatOpenAIAudio:
  468. info = GenRelayInfoOpenAIAudio(c, request)
  469. case types.RelayFormatOpenAIImage:
  470. info = GenRelayInfoImage(c, request)
  471. case types.RelayFormatOpenAIRealtime:
  472. info = GenRelayInfoWs(c, ws)
  473. case types.RelayFormatClaude:
  474. info = GenRelayInfoClaude(c, request)
  475. case types.RelayFormatRerank:
  476. if request, ok := request.(*dto.RerankRequest); ok {
  477. info = GenRelayInfoRerank(c, request)
  478. break
  479. }
  480. err = errors.New("request is not a RerankRequest")
  481. case types.RelayFormatGemini:
  482. info = GenRelayInfoGemini(c, request)
  483. case types.RelayFormatEmbedding:
  484. info = GenRelayInfoEmbedding(c, request)
  485. case types.RelayFormatOpenAIResponses:
  486. if request, ok := request.(*dto.OpenAIResponsesRequest); ok {
  487. info = GenRelayInfoResponses(c, request)
  488. break
  489. }
  490. err = errors.New("request is not a OpenAIResponsesRequest")
  491. case types.RelayFormatOpenAIResponsesCompaction:
  492. if request, ok := request.(*dto.OpenAIResponsesCompactionRequest); ok {
  493. return GenRelayInfoResponsesCompaction(c, request), nil
  494. }
  495. return nil, errors.New("request is not a OpenAIResponsesCompactionRequest")
  496. case types.RelayFormatTask:
  497. info = genBaseRelayInfo(c, nil)
  498. info.TaskRelayInfo = &TaskRelayInfo{}
  499. case types.RelayFormatMjProxy:
  500. info = genBaseRelayInfo(c, nil)
  501. info.TaskRelayInfo = &TaskRelayInfo{}
  502. default:
  503. err = errors.New("invalid relay format")
  504. }
  505. if err != nil {
  506. return nil, err
  507. }
  508. if info == nil {
  509. return nil, errors.New("failed to build relay info")
  510. }
  511. info.InitRequestConversionChain()
  512. return info, nil
  513. }
  514. func (info *RelayInfo) InitRequestConversionChain() {
  515. if info == nil {
  516. return
  517. }
  518. if len(info.RequestConversionChain) > 0 {
  519. return
  520. }
  521. if info.RelayFormat == "" {
  522. return
  523. }
  524. info.RequestConversionChain = []types.RelayFormat{info.RelayFormat}
  525. }
  526. func (info *RelayInfo) AppendRequestConversion(format types.RelayFormat) {
  527. if info == nil {
  528. return
  529. }
  530. if format == "" {
  531. return
  532. }
  533. if len(info.RequestConversionChain) == 0 {
  534. info.RequestConversionChain = []types.RelayFormat{format}
  535. return
  536. }
  537. last := info.RequestConversionChain[len(info.RequestConversionChain)-1]
  538. if last == format {
  539. return
  540. }
  541. info.RequestConversionChain = append(info.RequestConversionChain, format)
  542. }
  543. func (info *RelayInfo) GetFinalRequestRelayFormat() types.RelayFormat {
  544. if info == nil {
  545. return ""
  546. }
  547. if info.FinalRequestRelayFormat != "" {
  548. return info.FinalRequestRelayFormat
  549. }
  550. if n := len(info.RequestConversionChain); n > 0 {
  551. return info.RequestConversionChain[n-1]
  552. }
  553. return info.RelayFormat
  554. }
  555. func GenRelayInfoResponsesCompaction(c *gin.Context, request *dto.OpenAIResponsesCompactionRequest) *RelayInfo {
  556. info := genBaseRelayInfo(c, request)
  557. if info.RelayMode == relayconstant.RelayModeUnknown {
  558. info.RelayMode = relayconstant.RelayModeResponsesCompact
  559. }
  560. info.RelayFormat = types.RelayFormatOpenAIResponsesCompaction
  561. return info
  562. }
  563. //func (info *RelayInfo) SetPromptTokens(promptTokens int) {
  564. // info.promptTokens = promptTokens
  565. //}
  566. func (info *RelayInfo) SetEstimatePromptTokens(promptTokens int) {
  567. info.estimatePromptTokens = promptTokens
  568. }
  569. func (info *RelayInfo) GetEstimatePromptTokens() int {
  570. return info.estimatePromptTokens
  571. }
  572. func (info *RelayInfo) SetFirstResponseTime() {
  573. if info.isFirstResponse {
  574. info.FirstResponseTime = time.Now()
  575. info.isFirstResponse = false
  576. }
  577. }
  578. func (info *RelayInfo) HasSendResponse() bool {
  579. return info.FirstResponseTime.After(info.StartTime)
  580. }
  581. type TaskRelayInfo struct {
  582. Action string
  583. OriginTaskID string
  584. // PublicTaskID 是提交时预生成的 task_xxxx 格式公开 ID,
  585. // 供 DoResponse 在返回给客户端时使用(避免暴露上游真实 ID)。
  586. PublicTaskID string
  587. ConsumeQuota bool
  588. // LockedChannel holds the full channel object when the request is bound to
  589. // a specific channel (e.g., remix on origin task's channel). Stored as any
  590. // to avoid an import cycle with model; callers type-assert to *model.Channel.
  591. LockedChannel any
  592. }
  593. type TaskSubmitReq struct {
  594. Prompt string `json:"prompt"`
  595. Model string `json:"model,omitempty"`
  596. Mode string `json:"mode,omitempty"`
  597. Image string `json:"image,omitempty"`
  598. Images []string `json:"images,omitempty"`
  599. Size string `json:"size,omitempty"`
  600. Duration int `json:"duration,omitempty"`
  601. Seconds string `json:"seconds,omitempty"`
  602. InputReference string `json:"input_reference,omitempty"`
  603. Metadata map[string]interface{} `json:"metadata,omitempty"`
  604. }
  605. func (t *TaskSubmitReq) GetPrompt() string {
  606. return t.Prompt
  607. }
  608. func (t *TaskSubmitReq) HasImage() bool {
  609. return len(t.Images) > 0
  610. }
  611. func (t *TaskSubmitReq) UnmarshalJSON(data []byte) error {
  612. type Alias TaskSubmitReq
  613. aux := &struct {
  614. Metadata json.RawMessage `json:"metadata,omitempty"`
  615. *Alias
  616. }{
  617. Alias: (*Alias)(t),
  618. }
  619. if err := common.Unmarshal(data, &aux); err != nil {
  620. return err
  621. }
  622. if len(aux.Metadata) > 0 {
  623. var metadataStr string
  624. if err := common.Unmarshal(aux.Metadata, &metadataStr); err == nil && metadataStr != "" {
  625. var metadataObj map[string]interface{}
  626. if err := common.Unmarshal([]byte(metadataStr), &metadataObj); err == nil {
  627. t.Metadata = metadataObj
  628. return nil
  629. }
  630. }
  631. var metadataObj map[string]interface{}
  632. if err := common.Unmarshal(aux.Metadata, &metadataObj); err == nil {
  633. t.Metadata = metadataObj
  634. }
  635. }
  636. return nil
  637. }
  638. func (t *TaskSubmitReq) UnmarshalMetadata(v any) error {
  639. metadata := t.Metadata
  640. if metadata != nil {
  641. metadataBytes, err := common.Marshal(metadata)
  642. if err != nil {
  643. return fmt.Errorf("marshal metadata failed: %w", err)
  644. }
  645. err = common.Unmarshal(metadataBytes, v)
  646. if err != nil {
  647. return fmt.Errorf("unmarshal metadata to target failed: %w", err)
  648. }
  649. }
  650. return nil
  651. }
  652. type TaskInfo struct {
  653. Code int `json:"code"`
  654. TaskID string `json:"task_id"`
  655. Status string `json:"status"`
  656. Reason string `json:"reason,omitempty"`
  657. Url string `json:"url,omitempty"`
  658. RemoteUrl string `json:"remote_url,omitempty"`
  659. Progress string `json:"progress,omitempty"`
  660. CompletionTokens int `json:"completion_tokens,omitempty"` // 用于按倍率计费
  661. TotalTokens int `json:"total_tokens,omitempty"` // 用于按倍率计费
  662. }
  663. func FailTaskInfo(reason string) *TaskInfo {
  664. return &TaskInfo{
  665. Status: "FAILURE",
  666. Reason: reason,
  667. }
  668. }
  669. // RemoveDisabledFields 从请求 JSON 数据中移除渠道设置中禁用的字段
  670. // service_tier: 服务层级字段,可能导致额外计费(OpenAI、Claude、Responses API 支持)
  671. // inference_geo: Claude 数据驻留推理区域字段(仅 Claude 支持,默认过滤)
  672. // store: 数据存储授权字段,涉及用户隐私(仅 OpenAI、Responses API 支持,默认允许透传,禁用后可能导致 Codex 无法使用)
  673. // safety_identifier: 安全标识符,用于向 OpenAI 报告违规用户(仅 OpenAI 支持,涉及用户隐私)
  674. // stream_options.include_obfuscation: 响应流混淆控制字段(仅 OpenAI Responses API 支持)
  675. func RemoveDisabledFields(jsonData []byte, channelOtherSettings dto.ChannelOtherSettings, channelPassThroughEnabled bool) ([]byte, error) {
  676. if model_setting.GetGlobalSettings().PassThroughRequestEnabled || channelPassThroughEnabled {
  677. return jsonData, nil
  678. }
  679. var data map[string]interface{}
  680. if err := common.Unmarshal(jsonData, &data); err != nil {
  681. common.SysError("RemoveDisabledFields Unmarshal error :" + err.Error())
  682. return jsonData, nil
  683. }
  684. // 默认移除 service_tier,除非明确允许(避免额外计费风险)
  685. if !channelOtherSettings.AllowServiceTier {
  686. if _, exists := data["service_tier"]; exists {
  687. delete(data, "service_tier")
  688. }
  689. }
  690. // 默认移除 inference_geo,除非明确允许(避免在未授权情况下透传数据驻留区域)
  691. if !channelOtherSettings.AllowInferenceGeo {
  692. if _, exists := data["inference_geo"]; exists {
  693. delete(data, "inference_geo")
  694. }
  695. }
  696. // 默认允许 store 透传,除非明确禁用(禁用可能影响 Codex 使用)
  697. if channelOtherSettings.DisableStore {
  698. if _, exists := data["store"]; exists {
  699. delete(data, "store")
  700. }
  701. }
  702. // 默认移除 safety_identifier,除非明确允许(保护用户隐私,避免向 OpenAI 报告用户信息)
  703. if !channelOtherSettings.AllowSafetyIdentifier {
  704. if _, exists := data["safety_identifier"]; exists {
  705. delete(data, "safety_identifier")
  706. }
  707. }
  708. // 默认移除 stream_options.include_obfuscation,除非明确允许(避免关闭响应流混淆保护)
  709. if !channelOtherSettings.AllowIncludeObfuscation {
  710. if streamOptionsAny, exists := data["stream_options"]; exists {
  711. if streamOptions, ok := streamOptionsAny.(map[string]interface{}); ok {
  712. if _, includeExists := streamOptions["include_obfuscation"]; includeExists {
  713. delete(streamOptions, "include_obfuscation")
  714. }
  715. if len(streamOptions) == 0 {
  716. delete(data, "stream_options")
  717. } else {
  718. data["stream_options"] = streamOptions
  719. }
  720. }
  721. }
  722. }
  723. jsonDataAfter, err := common.Marshal(data)
  724. if err != nil {
  725. common.SysError("RemoveDisabledFields Marshal error :" + err.Error())
  726. return jsonData, nil
  727. }
  728. return jsonDataAfter, nil
  729. }
  730. // RemoveGeminiDisabledFields removes disabled fields from Gemini request JSON data
  731. // Currently supports removing functionResponse.id field which Vertex AI does not support
  732. func RemoveGeminiDisabledFields(jsonData []byte) ([]byte, error) {
  733. if !model_setting.GetGeminiSettings().RemoveFunctionResponseIdEnabled {
  734. return jsonData, nil
  735. }
  736. var data map[string]interface{}
  737. if err := common.Unmarshal(jsonData, &data); err != nil {
  738. common.SysError("RemoveGeminiDisabledFields Unmarshal error: " + err.Error())
  739. return jsonData, nil
  740. }
  741. // Process contents array
  742. // Handle both camelCase (functionResponse) and snake_case (function_response)
  743. if contents, ok := data["contents"].([]interface{}); ok {
  744. for _, content := range contents {
  745. if contentMap, ok := content.(map[string]interface{}); ok {
  746. if parts, ok := contentMap["parts"].([]interface{}); ok {
  747. for _, part := range parts {
  748. if partMap, ok := part.(map[string]interface{}); ok {
  749. // Check functionResponse (camelCase)
  750. if funcResp, ok := partMap["functionResponse"].(map[string]interface{}); ok {
  751. delete(funcResp, "id")
  752. }
  753. // Check function_response (snake_case)
  754. if funcResp, ok := partMap["function_response"].(map[string]interface{}); ok {
  755. delete(funcResp, "id")
  756. }
  757. }
  758. }
  759. }
  760. }
  761. }
  762. }
  763. jsonDataAfter, err := common.Marshal(data)
  764. if err != nil {
  765. common.SysError("RemoveGeminiDisabledFields Marshal error: " + err.Error())
  766. return jsonData, nil
  767. }
  768. return jsonDataAfter, nil
  769. }