2024-02-29 01:08:18 +08:00
|
|
|
package relay
|
2023-08-27 15:28:23 +08:00
|
|
|
|
|
|
|
|
import (
|
2023-10-01 12:49:40 +08:00
|
|
|
"errors"
|
2023-08-27 15:28:23 +08:00
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
2025-08-23 13:12:15 +08:00
|
|
|
"one-api/common"
|
2024-02-29 01:08:18 +08:00
|
|
|
"one-api/dto"
|
2024-02-29 16:21:25 +08:00
|
|
|
relaycommon "one-api/relay/common"
|
2025-02-20 16:41:46 +08:00
|
|
|
"one-api/relay/helper"
|
2024-02-29 01:08:18 +08:00
|
|
|
"one-api/service"
|
2025-07-10 15:02:40 +08:00
|
|
|
"one-api/types"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-08-27 15:28:23 +08:00
|
|
|
)
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
func AudioHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types.NewAPIError) {
|
|
|
|
|
info.InitChannelMeta(c)
|
2024-11-24 16:44:01 +08:00
|
|
|
|
2025-08-23 13:12:15 +08:00
|
|
|
audioReq, ok := info.Request.(*dto.AudioRequest)
|
2025-08-14 20:05:06 +08:00
|
|
|
if !ok {
|
|
|
|
|
return types.NewError(errors.New("invalid request type"), types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry())
|
2023-11-15 21:05:14 +08:00
|
|
|
}
|
2023-08-27 15:28:23 +08:00
|
|
|
|
2025-08-23 13:12:15 +08:00
|
|
|
request, err := common.DeepCopy(audioReq)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return types.NewError(fmt.Errorf("failed to copy request to AudioRequest: %w", err), types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = helper.ModelMappedHelper(c, info, request)
|
2025-02-20 16:41:46 +08:00
|
|
|
if err != nil {
|
2025-07-30 22:35:31 +08:00
|
|
|
return types.NewError(err, types.ErrorCodeChannelModelMappedError, types.ErrOptionWithSkipRetry())
|
2023-08-27 15:28:23 +08:00
|
|
|
}
|
2025-02-20 16:41:46 +08:00
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
adaptor := GetAdaptor(info.ApiType)
|
2024-07-16 22:07:10 +08:00
|
|
|
if adaptor == nil {
|
2025-08-14 20:05:06 +08:00
|
|
|
return types.NewError(fmt.Errorf("invalid api type: %d", info.ApiType), types.ErrorCodeInvalidApiType, types.ErrOptionWithSkipRetry())
|
2024-01-09 15:46:45 +08:00
|
|
|
}
|
2025-08-14 20:05:06 +08:00
|
|
|
adaptor.Init(info)
|
2024-01-09 15:46:45 +08:00
|
|
|
|
2025-08-23 13:12:15 +08:00
|
|
|
ioReader, err := adaptor.ConvertAudioRequest(c, info, *request)
|
2023-08-27 15:28:23 +08:00
|
|
|
if err != nil {
|
2025-07-30 22:35:31 +08:00
|
|
|
return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
2023-08-27 15:28:23 +08:00
|
|
|
}
|
2024-01-09 15:46:45 +08:00
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
resp, err := adaptor.DoRequest(c, info, ioReader)
|
2023-08-27 15:28:23 +08:00
|
|
|
if err != nil {
|
2025-07-10 15:02:40 +08:00
|
|
|
return types.NewError(err, types.ErrorCodeDoRequestFailed)
|
2023-08-27 15:28:23 +08:00
|
|
|
}
|
2024-07-16 22:07:10 +08:00
|
|
|
statusCodeMappingStr := c.GetString("status_code_mapping")
|
2024-10-04 16:08:18 +08:00
|
|
|
|
|
|
|
|
var httpResp *http.Response
|
2024-07-16 22:07:10 +08:00
|
|
|
if resp != nil {
|
2024-10-04 16:08:18 +08:00
|
|
|
httpResp = resp.(*http.Response)
|
|
|
|
|
if httpResp.StatusCode != http.StatusOK {
|
2025-07-10 15:02:40 +08:00
|
|
|
newAPIError = service.RelayErrorHandler(httpResp, false)
|
2024-07-16 22:07:10 +08:00
|
|
|
// reset status code 重置状态码
|
2025-07-10 15:02:40 +08:00
|
|
|
service.ResetStatusCode(newAPIError, statusCodeMappingStr)
|
|
|
|
|
return newAPIError
|
2024-03-20 17:07:42 +08:00
|
|
|
}
|
2023-08-27 15:28:23 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
usage, newAPIError := adaptor.DoResponse(c, httpResp, info)
|
2025-07-10 15:02:40 +08:00
|
|
|
if newAPIError != nil {
|
2024-07-16 22:07:10 +08:00
|
|
|
// reset status code 重置状态码
|
2025-07-10 15:02:40 +08:00
|
|
|
service.ResetStatusCode(newAPIError, statusCodeMappingStr)
|
|
|
|
|
return newAPIError
|
2023-08-27 15:28:23 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
postConsumeQuota(c, info, usage.(*dto.Usage), "")
|
2024-07-16 22:07:10 +08:00
|
|
|
|
2023-08-27 15:28:23 +08:00
|
|
|
return nil
|
|
|
|
|
}
|