Browse Source

新建对话返回ID

kobe6258 1 day ago
parent
commit
36fd2ee3f3

+ 4 - 0
controllers/rag/llm_http/response.go

@@ -7,3 +7,7 @@ type UserChatListResp struct {
 	YesterdayList []llm.UserLlmChatListViewItem
 	WeekList      []llm.UserLlmChatListViewItem
 }
+type UserChatResp struct {
+	ChatId    int
+	ChatTitle string
+}

+ 4 - 1
controllers/rag/user_chat_controller.go

@@ -47,7 +47,9 @@ func (ucCtrl *UserChatController) NewChat() {
 		CreatedTime: time.Now(),
 		ChatTitle:   req.ChatTitle,
 	}
-	err = session.CreateChatSession()
+	var chatResp = new(llm_http.UserChatResp)
+	chatResp.ChatTitle = req.ChatTitle
+	chatResp.ChatId, err = session.CreateChatSession()
 	if err != nil {
 		br.Msg = "创建失败"
 		br.ErrMsg = "创建失败,Err:" + err.Error()
@@ -59,6 +61,7 @@ func (ucCtrl *UserChatController) NewChat() {
 		Content:      req.ChatTitle,
 		SendTime:     time.Now().Format(utils.FormatDateTime),
 	})
+	br.Data = chatResp
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "创建成功"

+ 5 - 1
models/llm/user_llm_chat.go

@@ -44,9 +44,13 @@ func CovertItemToView(item UserLlmChatListItem) UserLlmChatListViewItem {
 func (u *UserLlmChat) TableName() string {
 	return "user_llm_chat"
 }
-func (u *UserLlmChat) CreateChatSession() (err error) {
+func (u *UserLlmChat) CreateChatSession() (chatId int, err error) {
 	o := global.DbMap[utils.DbNameAI]
 	err = o.Create(u).Error
+	if err != nil {
+		return
+	}
+	chatId = u.Id
 	return
 }
 func (u *UserLlmChat) RenameChatSession() (err error) {

+ 6 - 1
utils/lock/distrubtLock.go

@@ -36,7 +36,12 @@ func AcquireLock(key string, expiration int, Holder string) bool {
 	}
 	return false
 }
-
+func Lock() error {
+	if !AcquireLock("test", 10, "test") {
+		return fmt.Errorf("加锁失败")
+	}
+	return nil
+}
 func ReleaseLock(key string, holder string) bool {
 	script := redis.NewScript(`
 	   if redis.call("get", KEYS[1]) == ARGV[1] then

+ 28 - 0
utils/lock/reentrant_lock.go

@@ -0,0 +1,28 @@
+package lock
+
+import (
+	"context"
+	"github.com/go-redis/redis/v8"
+	"sync"
+)
+
+type RedisReentrantLock struct {
+	key      string
+	client   *redis.UniversalClient
+	ctx      context.Context
+	identity string
+	count    int
+	mu       sync.Mutex
+	stopChan chan struct{}
+}
+
+//func NewRedisReentrantLock(client *redis.Client, ctx context.Context, key string, identity string) *RedisReentrantLock {
+//	return &RedisReentrantLock{
+//		key:      key,
+//		client:   client,
+//		ctx:      ctx,
+//		identity: identity,
+//		count:    0,
+//		stopChan: make(chan struct{}),
+//	}
+//}