diff --git a/go.mod b/go.mod index 1e52a68..500ed17 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/bincooo/coze-api v1.0.2-0.20241222100337-a88cc26a1a5a github.com/bincooo/edge-api v1.0.4-0.20241229152313-75901c4f8730 github.com/bincooo/emit.io v1.0.1-0.20241222074906-3b397f33e381 - github.com/bincooo/you.com v0.0.0-20241226004948-53c5f0da9b86 + github.com/bincooo/you.com v0.0.0-20250101212728-8baa0b98edea github.com/bogdanfinn/tls-client v1.7.7 github.com/dlclark/regexp2 v1.11.4 github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd diff --git a/go.sum b/go.sum index 026a3d9..cad3d28 100644 --- a/go.sum +++ b/go.sum @@ -58,8 +58,8 @@ github.com/bincooo/emit.io v1.0.1-0.20241222074906-3b397f33e381 h1:28pzQ6Dsvy7J8 github.com/bincooo/emit.io v1.0.1-0.20241222074906-3b397f33e381/go.mod h1:cPNK/qXkuZp+YXYnkRatPMlGUlCD0rqJ8CeAqLAe+LA= github.com/bincooo/go-annotation v0.0.0-20241210101123-2fc3053d2f16 h1:/2MZmTiHa1a+hfUsmZxvOjQh1+lfZU/9gd7QRjD+xo8= github.com/bincooo/go-annotation v0.0.0-20241210101123-2fc3053d2f16/go.mod h1:3ZV3/eOQJ/28O5jINzNi98+K+8WY/pOvDYidT7WWaFs= -github.com/bincooo/you.com v0.0.0-20241226004948-53c5f0da9b86 h1:RD/R5pW0ARZy+WMEYXeJ5jUtl+Zd2Og0lL9EfvRvQPs= -github.com/bincooo/you.com v0.0.0-20241226004948-53c5f0da9b86/go.mod h1:BmBo0qPN22FWFtyByP+3XfRr4lvlpGrOGWKyLoDB4rU= +github.com/bincooo/you.com v0.0.0-20250101212728-8baa0b98edea h1:BCC9FBk1MGmTdRzsBfSHBahoFMFypBp0pjPDGMW1O8E= +github.com/bincooo/you.com v0.0.0-20250101212728-8baa0b98edea/go.mod h1:M7kpyfEvvZ6hzhUtizkSSvnzzhA+RT1HcYDucPQQLLY= github.com/bogdanfinn/fhttp v0.5.28 h1:G6thT8s8v6z1IuvXMUsX9QKy3ZHseTQTzxuIhSiaaAw= github.com/bogdanfinn/fhttp v0.5.28/go.mod h1:oJiYPG3jQTKzk/VFmogH8jxjH5yiv2rrOH48Xso2lrE= github.com/bogdanfinn/tls-client v1.7.7 h1:c3mf6LX6bxEsunJhP2BJeJE7qN/7BniWUpIpBc9Igu8= diff --git a/relay/llm/bing/adapter.go b/relay/llm/bing/adapter.go index a0bef35..d082079 100644 --- a/relay/llm/bing/adapter.go +++ b/relay/llm/bing/adapter.go @@ -8,6 +8,7 @@ import ( "chatgpt-adapter/core/gin/inter" "chatgpt-adapter/core/gin/model" "chatgpt-adapter/core/gin/response" + "chatgpt-adapter/core/logger" "context" "errors" "github.com/bincooo/edge-api" @@ -15,14 +16,46 @@ import ( "github.com/gin-gonic/gin" "github.com/iocgo/sdk/env" "github.com/iocgo/sdk/stream" + "slices" "strings" + "sync" "time" ) var ( Model = "bing" + + mu sync.Mutex + scheduleKeys []string ) +func init() { go timer() } + +// 保活 +func timer() { + t := time.NewTimer(time.Hour) + cacheManager := cache.BingCacheManager() + for { + select { + case <-t.C: + for _, k := range scheduleKeys[:] { + ident, err := cacheManager.GetValue(k) + if err != nil { + logger.Error(err) + continue + } + + timeout, cancel := context.WithTimeout(context.Background(), 10*time.Second) + _, err = genToken(timeout, ident, true) + if err != nil { + logger.Error(err) + } + cancel() + } + } + } +} + type api struct { inter.BaseAdapter @@ -191,9 +224,18 @@ func genToken(ctx context.Context, ident string, new bool) (accessToken string, err = cacheManager.SetWithExpiration(ident, accessToken, 48*time.Hour) accessToken = strings.Split(accessToken, "|")[1] + setTokenTimer(ident) return } +func setTokenTimer(ident string) { + mu.Lock() + defer mu.Unlock() + if !slices.Contains(scheduleKeys, ident) { + scheduleKeys = append(scheduleKeys, ident) + } +} + func elseOf[T any](condition bool, t1, t2 T) T { if condition { return t1