(c *gin.Context)
| 123 | } |
| 124 | |
| 125 | func WssRelay(c *gin.Context) { |
| 126 | // 将 HTTP 连接升级为 WebSocket 连接 |
| 127 | |
| 128 | ws, err := upgrader.Upgrade(c.Writer, c.Request, nil) |
| 129 | defer ws.Close() |
| 130 | |
| 131 | if err != nil { |
| 132 | helper.WssError(c, ws, types.NewError(err, types.ErrorCodeGetChannelFailed).ToOpenAIError()) |
| 133 | return |
| 134 | } |
| 135 | |
| 136 | relayMode := relayconstant.Path2RelayMode(c.Request.URL.Path) |
| 137 | requestId := c.GetString(common.RequestIdKey) |
| 138 | group := c.GetString("group") |
| 139 | //wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01 |
| 140 | originalModel := c.GetString("original_model") |
| 141 | var newAPIError *types.NewAPIError |
| 142 | |
| 143 | for i := 0; i <= common.RetryTimes; i++ { |
| 144 | channel, err := getChannel(c, group, originalModel, i) |
| 145 | if err != nil { |
| 146 | common.LogError(c, err.Error()) |
| 147 | newAPIError = err |
| 148 | break |
| 149 | } |
| 150 | |
| 151 | newAPIError = wssRequest(c, ws, relayMode, channel) |
| 152 | |
| 153 | if newAPIError == nil { |
| 154 | return // 成功处理请求,直接返回 |
| 155 | } |
| 156 | |
| 157 | go processChannelError(c, *types.NewChannelError(channel.Id, channel.Type, channel.Name, channel.ChannelInfo.IsMultiKey, common.GetContextKeyString(c, constant.ContextKeyChannelKey), channel.GetAutoBan()), newAPIError) |
| 158 | |
| 159 | if !shouldRetry(c, newAPIError, common.RetryTimes-i) { |
| 160 | break |
| 161 | } |
| 162 | } |
| 163 | useChannel := c.GetStringSlice("use_channel") |
| 164 | if len(useChannel) > 1 { |
| 165 | retryLogStr := fmt.Sprintf("重试:%s", strings.Trim(strings.Join(strings.Fields(fmt.Sprint(useChannel)), "->"), "[]")) |
| 166 | common.LogInfo(c, retryLogStr) |
| 167 | } |
| 168 | |
| 169 | if newAPIError != nil { |
| 170 | //if newAPIError.StatusCode == http.StatusTooManyRequests { |
| 171 | // newAPIError.SetMessage("当前分组上游负载已饱和,请稍后再试") |
| 172 | //} |
| 173 | newAPIError.SetMessage(common.MessageWithRequestId(newAPIError.Error(), requestId)) |
| 174 | helper.WssError(c, ws, newAPIError.ToOpenAIError()) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | func RelayClaude(c *gin.Context) { |
| 179 | //relayMode := constant.Path2RelayMode(c.Request.URL.Path) |
nothing calls this directly
no test coverage detected