Skip to content

Commit

Permalink
fix: err model_not_found
Browse files Browse the repository at this point in the history
  • Loading branch information
deanxv committed Apr 18, 2024
1 parent efa4b51 commit 7ed930d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 25 deletions.
2 changes: 1 addition & 1 deletion common/constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package common

var Version = "v4.4.4" // this hard coding will be replaced automatically when building, no need to manually change
var Version = "v4.4.5" // this hard coding will be replaced automatically when building, no need to manually change

const (
RequestIdKey = "X-Request-Id"
Expand Down
2 changes: 1 addition & 1 deletion common/err.go → common/myerr/discordunauthorizederror.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package myerr

import "fmt"

Expand Down
13 changes: 13 additions & 0 deletions common/myerr/modelnotfounderror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package myerr

import "fmt"

type ModelNotFoundError struct {
Message string
ErrCode int
}

// 实现 error 接口的 Error 方法
func (e *ModelNotFoundError) Error() string {
return fmt.Sprintf("errCode: %v, message: %v", e.ErrCode, e.Message)
}
44 changes: 27 additions & 17 deletions controller/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package controller
import (
"coze-discord-proxy/common"
"coze-discord-proxy/common/config"
"coze-discord-proxy/common/myerr"
"coze-discord-proxy/discord"
"coze-discord-proxy/model"
"coze-discord-proxy/telegram"
"encoding/json"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"io"
Expand All @@ -30,19 +32,19 @@ import (
//func Chat(c *gin.Context) {
//
// var chatModel model.ChatReq
// err := json.NewDecoder(c.Request.Body).Decode(&chatModel)
// if err != nil {
// common.LogError(c.Request.Context(), err.Error())
// myerr := json.NewDecoder(c.Request.Body).Decode(&chatModel)
// if myerr != nil {
// common.LogError(c.Request.Context(), myerr.Error())
// c.JSON(http.StatusOK, gin.H{
// "message": "无效的参数",
// "success": false,
// })
// return
// }
//
// sendChannelId, calledCozeBotId, err := getSendChannelIdAndCozeBotId(c, false, chatModel)
// if err != nil {
// common.LogError(c.Request.Context(), err.Error())
// sendChannelId, calledCozeBotId, myerr := getSendChannelIdAndCozeBotId(c, false, chatModel)
// if myerr != nil {
// common.LogError(c.Request.Context(), myerr.Error())
// c.JSON(http.StatusOK, model.OpenAIErrorResponse{
// OpenAIError: model.OpenAIError{
// Message: "配置异常",
Expand All @@ -53,11 +55,11 @@ import (
// return
// }
//
// sentMsg, err := discord.SendMessage(c, sendChannelId, calledCozeBotId, chatModel.Content)
// if err != nil {
// sentMsg, myerr := discord.SendMessage(c, sendChannelId, calledCozeBotId, chatModel.Content)
// if myerr != nil {
// c.JSON(http.StatusOK, gin.H{
// "success": false,
// "message": err.Error(),
// "message": myerr.Error(),
// })
// return
// }
Expand All @@ -70,9 +72,9 @@ import (
// discord.ReplyStopChans[sentMsg.ID] = stopChan
// defer delete(discord.ReplyStopChans, sentMsg.ID)
//
// timer, err := setTimerWithHeader(c, chatModel.Stream, config.RequestOutTimeDuration)
// if err != nil {
// common.LogError(c.Request.Context(), err.Error())
// timer, myerr := setTimerWithHeader(c, chatModel.Stream, config.RequestOutTimeDuration)
// if myerr != nil {
// common.LogError(c.Request.Context(), myerr.Error())
// c.JSON(http.StatusBadRequest, gin.H{
// "success": false,
// "message": "超时时间设置异常",
Expand Down Expand Up @@ -165,14 +167,19 @@ func ChatForOpenAI(c *gin.Context) {
sendChannelId, calledCozeBotId, isNewChannel, err := getSendChannelIdAndCozeBotId(c, request.ChannelId, request.Model, true)

if err != nil {
common.LogError(c.Request.Context(), err.Error())
c.JSON(http.StatusInternalServerError, model.OpenAIErrorResponse{
response := model.OpenAIErrorResponse{
OpenAIError: model.OpenAIError{
Message: "config error,check logs",
Type: "request_error",
Code: "500",
},
})
}
common.LogError(c.Request.Context(), err.Error())
var myErr *myerr.ModelNotFoundError
if errors.As(err, &myErr) {
response.OpenAIError.Message = "model_not_found"
}
c.JSON(http.StatusInternalServerError, response)
return
}

Expand Down Expand Up @@ -604,7 +611,10 @@ func getSendChannelIdAndCozeBotId(c *gin.Context, channelId *string, model strin

}
// 没有值抛出异常
return "", "", false, fmt.Errorf("[proxy-secret]+[model]未匹配到有效bot")
return "", "", false, &myerr.ModelNotFoundError{
ErrCode: 500,
Message: fmt.Sprintf("[proxy-secret:%s]+[model:%s]未匹配到有效bot", secret, model),
}
} else {

if channelId != nil && *channelId != "" {
Expand All @@ -616,7 +626,7 @@ func getSendChannelIdAndCozeBotId(c *gin.Context, channelId *string, model strin
} else {
sendChannelId, err := discord.CreateChannelWithRetry(c, discord.GuildId, fmt.Sprintf("cdp-chat-%s", c.Request.Context().Value(common.RequestIdKey)), 0)
if err != nil {
//common.LogError(c, err.Error())
//common.LogError(c, myerr.Error())
return "", "", false, err
}
return sendChannelId, discord.CozeBotId, true, nil
Expand Down
9 changes: 5 additions & 4 deletions discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"coze-discord-proxy/common"
"coze-discord-proxy/common/myerr"
"coze-discord-proxy/model"
"coze-discord-proxy/telegram"
"encoding/base64"
Expand Down Expand Up @@ -269,9 +270,9 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// 尝试获取 stopChan
stopChan, exists := ReplyStopChans[m.ReferencedMessage.ID]
if !exists {
//channel, err := Session.Channel(m.ChannelID)
//channel, myerr := Session.Channel(m.ChannelID)
// 不存在则直接删除频道
//if err != nil || strings.HasPrefix(channel.Name, "cdp-chat-") {
//if myerr != nil || strings.HasPrefix(channel.Name, "cdp-chat-") {
//SetChannelDeleteTimer(m.ChannelID, 5*time.Minute)
return
//}
Expand Down Expand Up @@ -437,13 +438,13 @@ func SendMessage(c *gin.Context, channelID, cozeBotId, message string) (*discord
}

for i, sendContent := range common.ReverseSegment(content, 1888) {
//sentMsg, err := Session.ChannelMessageSend(channelID, sendContent)
//sentMsg, myerr := Session.ChannelMessageSend(channelID, sendContent)
//sentMsgId := sentMsg.ID
// 4.0.0 版本下 用户端发送消息
sendContent = strings.ReplaceAll(sendContent, "\\n", "\n")
sentMsgId, err := SendMsgByAuthorization(c, userAuth, sendContent, channelID)
if err != nil {
var myErr *common.DiscordUnauthorizedError
var myErr *myerr.DiscordUnauthorizedError
if errors.As(err, &myErr) {
// 无效则将此 auth 移除
UserAuthorizations = common.FilterSlice(UserAuthorizations, userAuth)
Expand Down
5 changes: 3 additions & 2 deletions discord/sendmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"coze-discord-proxy/common"
"coze-discord-proxy/common/myerr"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -96,14 +97,14 @@ func SendMsgByAuthorization(c *gin.Context, userAuth, content, channelId string)
if strings.Contains(errMessage, "401: Unauthorized") ||
strings.Contains(errMessage, "You need to verify your account in order to perform this action.") {
common.LogWarn(ctx, fmt.Sprintf("USER_AUTHORIZATION:%s EXPIRED", userAuth))
return "", &common.DiscordUnauthorizedError{
return "", &myerr.DiscordUnauthorizedError{
ErrCode: 401,
Message: "discord 鉴权未通过",
}
}
}
common.LogError(ctx, fmt.Sprintf("user_auth:%s result:%s", userAuth, bodyString))
return "", fmt.Errorf("/api/v9/channels/%s/messages response err", channelId)
return "", fmt.Errorf("/api/v9/channels/%s/messages response myerr", channelId)
} else {
return id, nil
}
Expand Down

0 comments on commit 7ed930d

Please sign in to comment.