adaptor.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package codex
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "github.com/QuantumNous/new-api/common"
  9. "github.com/QuantumNous/new-api/dto"
  10. "github.com/QuantumNous/new-api/relay/channel"
  11. "github.com/QuantumNous/new-api/relay/channel/openai"
  12. relaycommon "github.com/QuantumNous/new-api/relay/common"
  13. relayconstant "github.com/QuantumNous/new-api/relay/constant"
  14. "github.com/QuantumNous/new-api/types"
  15. "github.com/gin-gonic/gin"
  16. )
  17. type Adaptor struct {
  18. }
  19. func (a *Adaptor) ConvertGeminiRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeminiChatRequest) (any, error) {
  20. return nil, errors.New("codex channel: endpoint not supported")
  21. }
  22. func (a *Adaptor) ConvertClaudeRequest(*gin.Context, *relaycommon.RelayInfo, *dto.ClaudeRequest) (any, error) {
  23. return nil, errors.New("codex channel: endpoint not supported")
  24. }
  25. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  26. return nil, errors.New("codex channel: endpoint not supported")
  27. }
  28. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  29. return nil, errors.New("codex channel: endpoint not supported")
  30. }
  31. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  32. }
  33. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  34. return nil, errors.New("codex channel: endpoint not supported")
  35. }
  36. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  37. return nil, errors.New("codex channel: endpoint not supported")
  38. }
  39. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  40. return nil, errors.New("codex channel: endpoint not supported")
  41. }
  42. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  43. if info != nil && info.ChannelSetting.SystemPrompt != "" {
  44. systemPrompt := info.ChannelSetting.SystemPrompt
  45. if len(request.Instructions) == 0 {
  46. if b, err := common.Marshal(systemPrompt); err == nil {
  47. request.Instructions = b
  48. } else {
  49. return nil, err
  50. }
  51. } else if info.ChannelSetting.SystemPromptOverride {
  52. var existing string
  53. if err := common.Unmarshal(request.Instructions, &existing); err == nil {
  54. existing = strings.TrimSpace(existing)
  55. if existing == "" {
  56. if b, err := common.Marshal(systemPrompt); err == nil {
  57. request.Instructions = b
  58. } else {
  59. return nil, err
  60. }
  61. } else {
  62. if b, err := common.Marshal(systemPrompt + "\n" + existing); err == nil {
  63. request.Instructions = b
  64. } else {
  65. return nil, err
  66. }
  67. }
  68. } else {
  69. if b, err := common.Marshal(systemPrompt); err == nil {
  70. request.Instructions = b
  71. } else {
  72. return nil, err
  73. }
  74. }
  75. }
  76. }
  77. // codex: store must be false
  78. request.Store = json.RawMessage("false")
  79. return request, nil
  80. }
  81. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  82. return channel.DoApiRequest(a, c, info, requestBody)
  83. }
  84. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  85. if info.RelayMode != relayconstant.RelayModeResponses {
  86. return nil, types.NewError(errors.New("codex channel: endpoint not supported"), types.ErrorCodeInvalidRequest)
  87. }
  88. if info.IsStream {
  89. return openai.OaiResponsesStreamHandler(c, info, resp)
  90. }
  91. return openai.OaiResponsesHandler(c, info, resp)
  92. }
  93. func (a *Adaptor) GetModelList() []string {
  94. return ModelList
  95. }
  96. func (a *Adaptor) GetChannelName() string {
  97. return ChannelName
  98. }
  99. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  100. if info.RelayMode != relayconstant.RelayModeResponses {
  101. return "", errors.New("codex channel: only /v1/responses is supported")
  102. }
  103. return relaycommon.GetFullRequestURL(info.ChannelBaseUrl, "/backend-api/codex/responses", info.ChannelType), nil
  104. }
  105. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  106. channel.SetupApiRequestHeader(info, c, req)
  107. key := strings.TrimSpace(info.ApiKey)
  108. if !strings.HasPrefix(key, "{") {
  109. return errors.New("codex channel: key must be a JSON object")
  110. }
  111. oauthKey, err := ParseOAuthKey(key)
  112. if err != nil {
  113. return err
  114. }
  115. accessToken := strings.TrimSpace(oauthKey.AccessToken)
  116. accountID := strings.TrimSpace(oauthKey.AccountID)
  117. if accessToken == "" {
  118. return errors.New("codex channel: access_token is required")
  119. }
  120. if accountID == "" {
  121. return errors.New("codex channel: account_id is required")
  122. }
  123. req.Set("Authorization", "Bearer "+accessToken)
  124. req.Set("chatgpt-account-id", accountID)
  125. if req.Get("OpenAI-Beta") == "" {
  126. req.Set("OpenAI-Beta", "responses=experimental")
  127. }
  128. if req.Get("originator") == "" {
  129. req.Set("originator", "codex_cli_rs")
  130. }
  131. return nil
  132. }