(c *gin.Context, info *relaycommon.RelayInfo)
| 21 | ) |
| 22 | |
| 23 | func ResponsesHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types.NewAPIError) { |
| 24 | info.InitChannelMeta(c) |
| 25 | if info.RelayMode == relayconstant.RelayModeResponsesCompact { |
| 26 | switch info.ApiType { |
| 27 | case appconstant.APITypeOpenAI, appconstant.APITypeCodex: |
| 28 | default: |
| 29 | return types.NewErrorWithStatusCode( |
| 30 | fmt.Errorf("unsupported endpoint %q for api type %d", "/v1/responses/compact", info.ApiType), |
| 31 | types.ErrorCodeInvalidRequest, |
| 32 | http.StatusBadRequest, |
| 33 | types.ErrOptionWithSkipRetry(), |
| 34 | ) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | var responsesReq *dto.OpenAIResponsesRequest |
| 39 | switch req := info.Request.(type) { |
| 40 | case *dto.OpenAIResponsesRequest: |
| 41 | responsesReq = req |
| 42 | case *dto.OpenAIResponsesCompactionRequest: |
| 43 | responsesReq = &dto.OpenAIResponsesRequest{ |
| 44 | Model: req.Model, |
| 45 | Input: req.Input, |
| 46 | Instructions: req.Instructions, |
| 47 | PreviousResponseID: req.PreviousResponseID, |
| 48 | } |
| 49 | default: |
| 50 | return types.NewErrorWithStatusCode( |
| 51 | fmt.Errorf("invalid request type, expected dto.OpenAIResponsesRequest or dto.OpenAIResponsesCompactionRequest, got %T", info.Request), |
| 52 | types.ErrorCodeInvalidRequest, |
| 53 | http.StatusBadRequest, |
| 54 | types.ErrOptionWithSkipRetry(), |
| 55 | ) |
| 56 | } |
| 57 | |
| 58 | request, err := common.DeepCopy(responsesReq) |
| 59 | if err != nil { |
| 60 | return types.NewError(fmt.Errorf("failed to copy request to GeneralOpenAIRequest: %w", err), types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry()) |
| 61 | } |
| 62 | |
| 63 | err = helper.ModelMappedHelper(c, info, request) |
| 64 | if err != nil { |
| 65 | return types.NewError(err, types.ErrorCodeChannelModelMappedError, types.ErrOptionWithSkipRetry()) |
| 66 | } |
| 67 | |
| 68 | adaptor := GetAdaptor(info.ApiType) |
| 69 | if adaptor == nil { |
| 70 | return types.NewError(fmt.Errorf("invalid api type: %d", info.ApiType), types.ErrorCodeInvalidApiType, types.ErrOptionWithSkipRetry()) |
| 71 | } |
| 72 | adaptor.Init(info) |
| 73 | var requestBody io.Reader |
| 74 | if model_setting.GetGlobalSettings().PassThroughRequestEnabled || info.ChannelSetting.PassThroughBodyEnabled { |
| 75 | storage, err := common.GetBodyStorage(c) |
| 76 | if err != nil { |
| 77 | return types.NewError(err, types.ErrorCodeReadRequestBodyFailed, types.ErrOptionWithSkipRetry()) |
| 78 | } |
| 79 | requestBody = common.ReaderOnly(storage) |
| 80 | } else { |
no test coverage detected