- OAuth: safe type assertions for session state and affiliate code (oauth.go, github.go, discord.go, linuxdo.go, oidc.go) - Model: Scan() methods handle string values from DB drivers, not just []byte (channel.go ChannelInfo, task.go Properties/TaskPrivateData) - Model: safe type assertion in CleanupChannelPollingLocks sync.Map iteration - Relay: safe type assertions in audio_handler, AWS InvokeModel, ollama ConvertClaudeRequest, claude stop sequences, zhipu token cache - Service: fix slice bounds panic in Gemini->OpenAI stop sequences conversion - Service: safe type assertion in CleanupFileSources middleware - Middleware: add missing c.Abort() in turnstile session save failure - Middleware: safe type assertion in distributor channelId - Middleware: safe int comparison in auth helper Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
82 lines
2.5 KiB
Go
82 lines
2.5 KiB
Go
package relay
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/heicode/manager/common"
|
|
"github.com/heicode/manager/dto"
|
|
relaycommon "github.com/heicode/manager/relay/common"
|
|
"github.com/heicode/manager/relay/helper"
|
|
"github.com/heicode/manager/service"
|
|
"github.com/heicode/manager/types"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func AudioHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types.NewAPIError) {
|
|
info.InitChannelMeta(c)
|
|
|
|
audioReq, ok := info.Request.(*dto.AudioRequest)
|
|
if !ok {
|
|
return types.NewError(errors.New("invalid request type"), types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry())
|
|
}
|
|
|
|
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)
|
|
if err != nil {
|
|
return types.NewError(err, types.ErrorCodeChannelModelMappedError, types.ErrOptionWithSkipRetry())
|
|
}
|
|
|
|
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)
|
|
|
|
ioReader, err := adaptor.ConvertAudioRequest(c, info, *request)
|
|
if err != nil {
|
|
return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
|
|
}
|
|
|
|
resp, err := adaptor.DoRequest(c, info, ioReader)
|
|
if err != nil {
|
|
return types.NewOpenAIError(err, types.ErrorCodeDoRequestFailed, http.StatusInternalServerError)
|
|
}
|
|
statusCodeMappingStr := c.GetString("status_code_mapping")
|
|
|
|
var httpResp *http.Response
|
|
if resp != nil {
|
|
httpResp = resp.(*http.Response)
|
|
if httpResp.StatusCode != http.StatusOK {
|
|
newAPIError = service.RelayErrorHandler(c.Request.Context(), httpResp, false)
|
|
// reset status code 重置状态码
|
|
service.ResetStatusCode(newAPIError, statusCodeMappingStr)
|
|
return newAPIError
|
|
}
|
|
}
|
|
|
|
usage, newAPIError := adaptor.DoResponse(c, httpResp, info)
|
|
if newAPIError != nil {
|
|
// reset status code 重置状态码
|
|
service.ResetStatusCode(newAPIError, statusCodeMappingStr)
|
|
return newAPIError
|
|
}
|
|
usageData, ok := usage.(*dto.Usage)
|
|
if !ok {
|
|
return types.NewError(fmt.Errorf("invalid usage type"), types.ErrorCodeBadResponseBody)
|
|
}
|
|
if usageData.CompletionTokenDetails.AudioTokens > 0 || usageData.PromptTokensDetails.AudioTokens > 0 {
|
|
service.PostAudioConsumeQuota(c, info, usageData, "")
|
|
} else {
|
|
service.PostTextConsumeQuota(c, info, usageData, nil)
|
|
}
|
|
|
|
return nil
|
|
}
|