MCPcopy
hub / github.com/QuantumNous/new-api / ResponsesHelper

Function ResponsesHelper

relay/responses_handler.go:23–166  ·  view source on GitHub ↗
(c *gin.Context, info *relaycommon.RelayInfo)

Source from the content-addressed store, hash-verified

21)
22
23func 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 {

Callers 1

relayHandlerFunction · 0.92

Calls 15

NewErrorWithStatusCodeFunction · 0.92
ErrOptionWithSkipRetryFunction · 0.92
NewErrorFunction · 0.92
ModelMappedHelperFunction · 0.92
GetGlobalSettingsFunction · 0.92
LogDebugFunction · 0.92
NewOpenAIErrorFunction · 0.92
RelayErrorHandlerFunction · 0.92
ResetStatusCodeFunction · 0.92
ModelPriceHelperFunction · 0.92
ErrOptionWithStatusCodeFunction · 0.92
PostTextConsumeQuotaFunction · 0.92

Tested by

no test coverage detected