Skip to content

Commit

Permalink
feat(bing): 添加刷新accessToken方法
Browse files Browse the repository at this point in the history
  • Loading branch information
bincooo authored and bincooo committed Dec 29, 2024
1 parent 5a71b4e commit 34660d1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
13 changes: 11 additions & 2 deletions core/cache/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type Manager[T any] struct {

var (
toolTasksCacheManager *Manager[[]model.Keyv[string]]

windsurfCacheManager *Manager[string]
windsurfCacheManager *Manager[string]
bingCacheManager *Manager[string]
)

func init() {
Expand All @@ -36,6 +36,11 @@ func init() {
windsurfCacheManager = &Manager[string]{
cache.New[string](gocacheStore.NewGoCache(client)),
}

client = gocache.New(5*time.Minute, 5*time.Minute)
bingCacheManager = &Manager[string]{
cache.New[string](gocacheStore.NewGoCache(client)),
}
})
}

Expand All @@ -47,6 +52,10 @@ func WindsurfCacheManager() *Manager[string] {
return windsurfCacheManager
}

func BingCacheManager() *Manager[string] {
return bingCacheManager
}

func (cacheManager *Manager[T]) SetValue(key string, value T) error {
return cacheManager.SetWithExpiration(key, value, 120*time.Second)
}
Expand Down
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23.3
require (
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/bincooo/coze-api v1.0.2-0.20241222100337-a88cc26a1a5a
github.com/bincooo/edge-api v1.0.4-0.20241228201804-a3b98723b370
github.com/bincooo/edge-api v1.0.4-0.20241229054502-4e2a9a08765e
github.com/bincooo/emit.io v1.0.1-0.20241222074906-3b397f33e381
github.com/bincooo/you.com v0.0.0-20241226004948-53c5f0da9b86
github.com/bogdanfinn/tls-client v1.7.7
Expand All @@ -25,10 +25,8 @@ require (
)

//github.com/iocgo/sdk v0.0.0-20241129021727-ca323c08f298 => ../sdk
replace (
github.com/bincooo/edge-api v1.0.4-0.20241209140454-b708b5d57059 => ../edge-api
github.com/samber/do/v2 v2.0.0-beta.7 => github.com/iocgo/do/v2 v2.0.0-patch.0.20241204032939-7bbcadbc5f38
)
//github.com/bincooo/edge-api v1.0.4-0.20241228201804-a3b98723b370 => ../edge-api
replace github.com/samber/do/v2 v2.0.0-beta.7 => github.com/iocgo/do/v2 v2.0.0-patch.0.20241204032939-7bbcadbc5f38

require (
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bincooo/coze-api v1.0.2-0.20241222100337-a88cc26a1a5a h1:oBGzNQ6EjtBhNR9s629bx4NpYqoiAo4DXXnrIok2HeU=
github.com/bincooo/coze-api v1.0.2-0.20241222100337-a88cc26a1a5a/go.mod h1:ZCiekJWTzAnwJyn7qdClip40nK9Ix54cHZHXhtKK8iQ=
github.com/bincooo/edge-api v1.0.4-0.20241228201804-a3b98723b370 h1:r7Tcvb0kFlWZkeQRNFrk9SuEhw0S3alpAA0Twk3XUy0=
github.com/bincooo/edge-api v1.0.4-0.20241228201804-a3b98723b370/go.mod h1:JMD3hj34imIyW8GLD2cmhynEbG7jXuUQG2/WWtjgzTw=
github.com/bincooo/edge-api v1.0.4-0.20241229054502-4e2a9a08765e h1:ro2bwq/oznWnKUUTAeMg5UaOPI4jlkEL8GAz/+37/FI=
github.com/bincooo/edge-api v1.0.4-0.20241229054502-4e2a9a08765e/go.mod h1:sBHg6xfFnLlkYYMDtYL+MkkXUOlF1+V0xFZ30vd4ZBw=
github.com/bincooo/emit.io v1.0.1-0.20241222074906-3b397f33e381 h1:28pzQ6Dsvy7J8liRQSBtaF3Pxt8ZUR2Nl/VczlXFWEI=
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=
Expand Down
30 changes: 27 additions & 3 deletions relay/llm/bing/adapter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bing

import (
"chatgpt-adapter/core/cache"
"chatgpt-adapter/core/common"
"chatgpt-adapter/core/common/toolcall"
"chatgpt-adapter/core/common/vars"
Expand Down Expand Up @@ -93,18 +94,25 @@ func (api *api) Completion(ctx *gin.Context) (err error) {

timeout, cancel := context.WithTimeout(ctx.Request.Context(), 10*time.Second)
defer cancel()
conversationId, err := edge.CreateConversation(elseOf(proxied, common.HTTPClient, common.NopHTTPClient), timeout, cookie)
accessToken, err := genToken(timeout, cookie)
if err != nil {
return
}

timeout, cancel = context.WithTimeout(ctx.Request.Context(), 10*time.Second)
defer cancel()
conversationId, err := edge.CreateConversation(elseOf(proxied, common.HTTPClient, common.NopHTTPClient), timeout, accessToken)
if err != nil {
return
}

timeout, cancel = context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
defer edge.DeleteConversation(common.HTTPClient, timeout, conversationId, cookie)
defer edge.DeleteConversation(common.HTTPClient, timeout, conversationId, accessToken)

challenge := ""
label:
message, err := edge.Chat(common.HTTPClient, ctx.Request.Context(), cookie, conversationId, challenge, request, "从[\n\nAi:]处继续回复,\n\n当前问题是: "+query)
message, err := edge.Chat(common.HTTPClient, ctx.Request.Context(), accessToken, conversationId, challenge, request, "从[\n\nAi:]处继续回复,\n\n当前问题是: "+query)
if err != nil {
if challenge == "" && err.Error() == "challenge" {
challenge, err = hookCloudflare()
Expand Down Expand Up @@ -135,6 +143,22 @@ func convertRequest(ctx *gin.Context, completion model.Completion) (content stri
return
}

func genToken(ctx context.Context, ident string) (accessToken string, err error) {
cacheManager := cache.BingCacheManager()
accessToken, err = cacheManager.GetValue(ident)
if err != nil || accessToken != "" {
return
}

accessToken, err = edge.RefreshToken(common.HTTPClient, ctx, ident)
if err != nil {
return
}

err = cacheManager.SetWithExpiration(ident, accessToken, 12*time.Hour)
return
}

func elseOf[T any](condition bool, t1, t2 T) T {
if condition {
return t1
Expand Down

0 comments on commit 34660d1

Please sign in to comment.