From 979d27b7410124cb92435b8b85caadce1b839c35 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 22 Jan 2025 16:52:37 +0800 Subject: [PATCH] Enhance chat retrieval and update functionality in Xun - Added 'updated_at' field to the chat selection query to include the last modified timestamp in chat responses. - Updated the ordering of chat results to prioritize 'updated_at' over 'created_at', improving the relevance of displayed chats. - Modified the handling of chat creation timestamps to fall back on 'created_at' if 'updated_at' is not available, ensuring consistent date representation. - Implemented an update mechanism for the 'updated_at' field when saving chat history, enhancing data integrity and tracking. These changes improve the chat management capabilities of the Neo API assistant, ensuring more accurate and relevant chat data retrieval. --- neo/store/xun.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/neo/store/xun.go b/neo/store/xun.go index 1bf8a94b..3c810cf0 100644 --- a/neo/store/xun.go +++ b/neo/store/xun.go @@ -335,7 +335,7 @@ func (conv *Xun) GetChats(sid string, filter ChatFilter) (*ChatGroupResponse, er // Build base query qb := conv.newQueryChat(). - Select("chat_id", "title", "created_at"). + Select("chat_id", "title", "created_at", "updated_at"). Where("sid", userID). Where("chat_id", "!=", "") @@ -358,7 +358,9 @@ func (conv *Xun) GetChats(sid string, filter ChatFilter) (*ChatGroupResponse, er lastPage := int(math.Ceil(float64(total) / float64(filter.PageSize))) // Get paginated results - rows, err := qb.OrderBy("created_at", filter.Order). + rows, err := qb. + OrderBy("updated_at", filter.Order). + OrderBy("created_at", filter.Order). Offset(offset). Limit(filter.PageSize). Get() @@ -392,8 +394,13 @@ func (conv *Xun) GetChats(sid string, filter ChatFilter) (*ChatGroupResponse, er "title": row.Get("title"), } + var dbDatetime = row.Get("updated_at") + if dbDatetime == nil { + dbDatetime = row.Get("created_at") + } + var createdAt time.Time - switch v := row.Get("created_at").(type) { + switch v := dbDatetime.(type) { case time.Time: createdAt = v case string: @@ -608,6 +615,15 @@ func (conv *Xun) SaveHistory(sid string, messages []map[string]interface{}, cid return err } + // Update Chat updated_at + _, err = conv.newQueryChat(). + Where("chat_id", cid). + Where("sid", userID). + Update(map[string]interface{}{"updated_at": now}) + if err != nil { + return err + } + return nil }