Merge remote-tracking branch 'origin/upstream/main'

This commit is contained in:
Laisky.Cai
2024-05-29 06:12:37 +00:00
88 changed files with 790 additions and 579 deletions

View File

@@ -5,13 +5,13 @@ import (
"encoding/json"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/logger"
"github.com/songquanpeng/one-api/common/random"
"github.com/songquanpeng/one-api/controller"
"github.com/songquanpeng/one-api/model"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
"time"

View File

@@ -9,12 +9,12 @@ import (
"time"
"github.com/Laisky/errors/v2"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/logger"
"github.com/songquanpeng/one-api/controller"
"github.com/songquanpeng/one-api/model"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
type LarkOAuthResponse struct {

View File

@@ -8,11 +8,11 @@ import (
"time"
"github.com/Laisky/errors/v2"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/controller"
"github.com/songquanpeng/one-api/model"
"github.com/gin-gonic/gin"
)
type wechatLoginResponse struct {

View File

@@ -1,11 +1,11 @@
package controller
import (
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/model"
relaymodel "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin"
)
func GetSubscription(c *gin.Context) {

View File

@@ -9,14 +9,13 @@ import (
"time"
"github.com/Laisky/errors/v2"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/client"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/logger"
"github.com/songquanpeng/one-api/model"
"github.com/songquanpeng/one-api/monitor"
"github.com/songquanpeng/one-api/relay/channeltype"
"github.com/songquanpeng/one-api/relay/client"
"github.com/gin-gonic/gin"
)
// https://github.com/songquanpeng/one-api/issues/79

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/Laisky/errors/v2"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/common/logger"
@@ -18,7 +19,6 @@ import (
"github.com/songquanpeng/one-api/relay/meta"
relaymodel "github.com/songquanpeng/one-api/relay/model"
"github.com/songquanpeng/one-api/relay/relaymode"
"github.com/gin-gonic/gin"
"io"
"net/http"
"net/http/httptest"

View File

@@ -1,10 +1,10 @@
package controller
import (
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/helper"
"github.com/songquanpeng/one-api/model"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
"strings"

View File

@@ -1,8 +1,8 @@
package controller
import (
billingratio "github.com/songquanpeng/one-api/relay/billing/ratio"
"github.com/gin-gonic/gin"
billingratio "github.com/songquanpeng/one-api/relay/billing/ratio"
"net/http"
)

View File

@@ -4,10 +4,10 @@ import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/model"
"github.com/gin-gonic/gin"
)
func GetAllLogs(c *gin.Context) {

View File

@@ -5,6 +5,7 @@ import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/model"
relay "github.com/songquanpeng/one-api/relay"
@@ -13,7 +14,6 @@ import (
"github.com/songquanpeng/one-api/relay/channeltype"
"github.com/songquanpeng/one-api/relay/meta"
relaymodel "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin"
)
// https://platform.openai.com/docs/api-reference/models/list

View File

@@ -4,12 +4,12 @@ import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/common/helper"
"github.com/songquanpeng/one-api/common/random"
"github.com/songquanpeng/one-api/model"
"github.com/gin-gonic/gin"
)
func GetAllRedemptions(c *gin.Context) {

View File

@@ -7,6 +7,8 @@ import (
"io"
"net/http"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/ctxkey"
@@ -18,8 +20,6 @@ import (
"github.com/songquanpeng/one-api/relay/controller"
"github.com/songquanpeng/one-api/relay/model"
"github.com/songquanpeng/one-api/relay/relaymode"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
// https://platform.openai.com/docs/api-reference/chat
@@ -46,6 +46,7 @@ func Relay(c *gin.Context) {
ctx := c.Request.Context()
relayMode := relaymode.GetByPath(c.Request.URL.Path)
channelId := c.GetInt(ctxkey.ChannelId)
userId := c.GetInt("id")
bizErr := relayHelper(c, relayMode)
if bizErr == nil {
monitor.Emit(channelId, true)
@@ -56,7 +57,7 @@ func Relay(c *gin.Context) {
group := c.GetString(ctxkey.Group)
originalModel := c.GetString(ctxkey.OriginalModel)
// BUG: bizErr is shared, should not run this function in goroutine to avoid race
go processChannelRelayError(ctx, channelId, channelName, bizErr)
go processChannelRelayError(ctx, userId, channelId, channelName, bizErr)
requestId := c.GetString(helper.RequestIdKey)
retryTimes := config.RetryTimes
if err := shouldRetry(c, bizErr.StatusCode); err != nil {
@@ -84,7 +85,7 @@ func Relay(c *gin.Context) {
lastFailedChannelId = channelId
channelName := c.GetString(ctxkey.ChannelName)
// bizErr is shared, should not run this function in goroutine to avoid race
processChannelRelayError(ctx, channelId, channelName, bizErr)
go processChannelRelayError(ctx, userId, channelId, channelName, bizErr)
}
if bizErr != nil {
@@ -114,8 +115,8 @@ func shouldRetry(c *gin.Context, statusCode int) error {
return nil
}
func processChannelRelayError(ctx context.Context, channelId int, channelName string, err *model.ErrorWithStatusCode) {
logger.Errorf(ctx, "relay error (channel #%d): %s", channelId, err.Message)
func processChannelRelayError(ctx context.Context, userId int, channelId int, channelName string, err *model.ErrorWithStatusCode) {
logger.Errorf(ctx, "relay error (channel id %d, user id: %d): %s", channelId, userId, err.Message)
// https://platform.openai.com/docs/guides/error-codes/api-errors
if monitor.ShouldDisableChannel(&err.Error, err.StatusCode) {
monitor.DisableChannel(channelId, channelName, err.Message)

View File

@@ -5,6 +5,8 @@ import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/jinzhu/copier"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/ctxkey"
@@ -12,8 +14,6 @@ import (
"github.com/songquanpeng/one-api/common/network"
"github.com/songquanpeng/one-api/common/random"
"github.com/songquanpeng/one-api/model"
"github.com/gin-gonic/gin"
"github.com/jinzhu/copier"
)
func GetRequestCost(c *gin.Context) {

View File

@@ -7,13 +7,15 @@ import (
"strconv"
"time"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/common/helper"
"github.com/songquanpeng/one-api/common/logger"
"github.com/songquanpeng/one-api/common/random"
"github.com/songquanpeng/one-api/model"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
type LoginRequest struct {
@@ -115,6 +117,7 @@ func Logout(c *gin.Context) {
}
func Register(c *gin.Context) {
ctx := c.Request.Context()
if !config.RegisterEnabled {
c.JSON(http.StatusOK, gin.H{
"message": "管理员关闭了新用户注册",
@@ -179,6 +182,28 @@ func Register(c *gin.Context) {
})
return
}
go func() {
err := user.ValidateAndFill()
if err != nil {
logger.Errorf(ctx, "user.ValidateAndFill failed: %v", err)
return
}
cleanToken := model.Token{
UserId: user.Id,
Name: "default",
Key: random.GenerateKey(),
CreatedTime: helper.GetTimestamp(),
AccessedTime: helper.GetTimestamp(),
ExpiredTime: -1,
RemainQuota: -1,
UnlimitedQuota: true,
}
err = cleanToken.Insert()
if err != nil {
logger.Errorf(ctx, "cleanToken.Insert failed: %v", err)
return
}
}()
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",