From 2da7c30ac5958fdc8d0ef1a77e8b4dd0af103c8f Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 16 Dec 2024 12:37:11 +0800 Subject: [PATCH] Refactor query handling in Xun conversation management - Introduced a NewQuery method to streamline query creation, enhancing code readability and maintainability. - Updated multiple methods (UpdateChatTitle, GetChats, GetHistory, SaveHistory, GetRequest, SaveRequest, clean) to utilize the new query method, reducing redundancy in query table references. - Improved overall structure of the conversation management code by centralizing query logic. --- neo/conversation/xun.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/neo/conversation/xun.go b/neo/conversation/xun.go index c34cc58b..0afefd9b 100644 --- a/neo/conversation/xun.go +++ b/neo/conversation/xun.go @@ -63,9 +63,16 @@ func NewXun(setting Setting) (*Xun, error) { return conv, nil } +// NewQuery create a new query +func (conv *Xun) NewQuery() query.Query { + qb := conv.query.New() + qb.Table(conv.setting.Table) + return qb +} + // UpdateChatTitle update the chat title func (conv *Xun) UpdateChatTitle(sid string, cid string, title string) error { - _, err := conv.query.Table(conv.setting.Table). + _, err := conv.NewQuery(). Where("sid", sid).Where("cid", cid). Update(map[string]interface{}{"title": title}) return err @@ -73,7 +80,7 @@ func (conv *Xun) UpdateChatTitle(sid string, cid string, title string) error { // GetChats get the chat list func (conv *Xun) GetChats(sid string) ([]map[string]interface{}, error) { - qb := conv.query.Table(conv.setting.Table). + qb := conv.NewQuery(). Select("cid"). Where("sid", sid). GroupBy("cid") @@ -102,7 +109,7 @@ func (conv *Xun) GetChats(sid string) ([]map[string]interface{}, error) { // GetHistory get the history func (conv *Xun) GetHistory(sid string, cid string) ([]map[string]interface{}, error) { - qb := conv.query.Table(conv.setting.Table). + qb := conv.NewQuery(). Select("role", "name", "content"). Where("sid", sid). Where("cid", cid). @@ -160,13 +167,13 @@ func (conv *Xun) SaveHistory(sid string, messages []map[string]interface{}, cid values = append(values, value) } - return conv.query.Table(conv.setting.Table).Insert(values) + return conv.NewQuery().Insert(values) } // GetRequest get the request history func (conv *Xun) GetRequest(sid string, rid string) ([]map[string]interface{}, error) { - qb := conv.query.Table(conv.setting.Table). + qb := conv.NewQuery(). Select("role", "name", "content", "sid"). Where("rid", rid). Where("sid", sid). @@ -225,11 +232,11 @@ func (conv *Xun) SaveRequest(sid string, rid string, cid string, messages []map[ values = append(values, value) } - return conv.query.Table(conv.setting.Table).Insert(values) + return conv.NewQuery().Insert(values) } func (conv *Xun) clean() { - nums, err := conv.query.Table(conv.setting.Table).Where("expired_at", "<=", time.Now()).Delete() + nums, err := conv.NewQuery().Where("expired_at", "<=", time.Now()).Delete() if err != nil { log.Error("Clean the conversation table error: %s", err.Error()) return