| package relay |
|
|
| import ( |
| "fmt" |
|
|
| "github.com/QuantumNous/new-api/dto" |
| relaycommon "github.com/QuantumNous/new-api/relay/common" |
| "github.com/QuantumNous/new-api/service" |
| "github.com/QuantumNous/new-api/types" |
|
|
| "github.com/gin-gonic/gin" |
| "github.com/gorilla/websocket" |
| ) |
|
|
| func WssHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types.NewAPIError) { |
| info.InitChannelMeta(c) |
|
|
| adaptor := GetAdaptor(info.ApiType) |
| if adaptor == nil { |
| return types.NewError(fmt.Errorf("invalid api type: %d", info.ApiType), types.ErrorCodeInvalidApiType, types.ErrOptionWithSkipRetry()) |
| } |
| adaptor.Init(info) |
| |
| |
| |
|
|
| statusCodeMappingStr := c.GetString("status_code_mapping") |
| resp, err := adaptor.DoRequest(c, info, nil) |
| if err != nil { |
| return types.NewError(err, types.ErrorCodeDoRequestFailed) |
| } |
|
|
| if resp != nil { |
| info.TargetWs = resp.(*websocket.Conn) |
| defer info.TargetWs.Close() |
| } |
|
|
| usage, newAPIError := adaptor.DoResponse(c, nil, info) |
| if newAPIError != nil { |
| |
| service.ResetStatusCode(newAPIError, statusCodeMappingStr) |
| return newAPIError |
| } |
| service.PostWssConsumeQuota(c, info, info.UpstreamModelName, usage.(*dto.RealtimeUsage), "") |
| return nil |
| } |
|
|