From e411086482346687ab1dcd38354389a1152a56aa Mon Sep 17 00:00:00 2001 From: Arvin Xu Date: Wed, 17 Jul 2024 08:10:23 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix:=20fix=20session=20not?= =?UTF-8?q?=20reorder=20after=20send=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/session/client.ts | 5 ++++- src/services/session/server.ts | 11 ++++------- src/store/chat/slices/message/action.ts | 3 +++ src/store/session/slices/session/action.ts | 14 +++++++------- src/types/session/index.ts | 9 ++++++++- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/services/session/client.ts b/src/services/session/client.ts index 0662b171834b..0f17fcefe778 100644 --- a/src/services/session/client.ts +++ b/src/services/session/client.ts @@ -15,6 +15,7 @@ import { SessionGroupItem, SessionGroups, } from '@/types/session'; +import { merge } from '@/utils/merge'; import { ISessionService } from './type'; @@ -94,7 +95,9 @@ export class ClientService implements ISessionService { data: Partial>, ) { const pinned = typeof data.pinned === 'boolean' ? (data.pinned ? 1 : 0) : undefined; - return SessionModel.update(id, { ...data, pinned }); + const prev = await SessionModel.findById(id); + + return SessionModel.update(id, merge(prev, { ...data, pinned })); } async updateSessionConfig( diff --git a/src/services/session/server.ts b/src/services/session/server.ts index a3fe684b438f..8827fbba36b3 100644 --- a/src/services/session/server.ts +++ b/src/services/session/server.ts @@ -13,9 +13,9 @@ import { LobeAgentSession, LobeSessionType, LobeSessions, - SessionGroupId, SessionGroupItem, SessionGroups, + UpdateSessionParams, } from '@/types/session'; import { ISessionService } from './type'; @@ -54,14 +54,11 @@ export class ServerService implements ISessionService { return lambdaClient.session.countSessions.query(); } - updateSession( - id: string, - data: Partial<{ group?: SessionGroupId; meta?: any; pinned?: boolean }>, - ): Promise { - const { group, pinned, meta } = data; + updateSession(id: string, data: Partial): Promise { + const { group, pinned, meta, updatedAt } = data; return lambdaClient.session.updateSession.mutate({ id, - value: { groupId: group === 'default' ? null : group, pinned, ...meta }, + value: { groupId: group === 'default' ? null : group, pinned, ...meta, updatedAt }, }); } diff --git a/src/store/chat/slices/message/action.ts b/src/store/chat/slices/message/action.ts index 10c0045e5756..c1937f3777b4 100644 --- a/src/store/chat/slices/message/action.ts +++ b/src/store/chat/slices/message/action.ts @@ -19,6 +19,7 @@ import { agentSelectors } from '@/store/agent/selectors'; import { chatHelpers } from '@/store/chat/helpers'; import { messageMapKey } from '@/store/chat/slices/message/utils'; import { ChatStore } from '@/store/chat/store'; +import { useSessionStore } from '@/store/session'; import { ChatMessage, ChatMessageError, MessageToolCall } from '@/types/message'; import { TraceEventPayloads } from '@/types/trace'; import { setNamespace } from '@/utils/storeDebug'; @@ -319,6 +320,8 @@ export const chatMessage: StateCreator< } } } + // update assistant update to make it rerank + useSessionStore.getState().triggerSessionUpdate(get().activeId); const id = await get().internal_createMessage(newMessage, { tempMessageId, diff --git a/src/store/session/slices/session/action.ts b/src/store/session/slices/session/action.ts index af70e7858c4c..6de698b63154 100644 --- a/src/store/session/slices/session/action.ts +++ b/src/store/session/slices/session/action.ts @@ -18,7 +18,7 @@ import { LobeSessionGroups, LobeSessionType, LobeSessions, - SessionGroupId, + UpdateSessionParams, } from '@/types/session'; import { merge } from '@/utils/merge'; import { setNamespace } from '@/utils/storeDebug'; @@ -52,6 +52,7 @@ export interface SessionAction { isSwitchSession?: boolean, ) => Promise; duplicateSession: (id: string) => Promise; + triggerSessionUpdate: (id: string) => Promise; updateSessionGroupId: (sessionId: string, groupId: string) => Promise; updateSessionMeta: (meta: Partial) => void; @@ -75,10 +76,7 @@ export interface SessionAction { useSearchSessions: (keyword?: string) => SWRResponse; internal_dispatchSessions: (payload: SessionDispatch) => void; - internal_updateSession: ( - id: string, - data: Partial<{ group?: SessionGroupId; meta?: any; pinned?: boolean }>, - ) => Promise; + internal_updateSession: (id: string, data: Partial) => Promise; internal_processSessions: ( sessions: LobeSessions, customGroups: LobeSessionGroups, @@ -117,7 +115,6 @@ export const createSessionSlice: StateCreator< return id; }, - duplicateSession: async (id) => { const { switchSession, refreshSessions } = get(); const session = sessionSelectors.getSessionById(id)(get()); @@ -153,7 +150,6 @@ export const createSessionSlice: StateCreator< pinSession: async (id, pinned) => { await get().internal_updateSession(id, { pinned }); }, - removeSession: async (sessionId) => { await sessionService.removeSession(sessionId); await get().refreshSessions(); @@ -170,6 +166,10 @@ export const createSessionSlice: StateCreator< set({ activeId: sessionId }, false, n(`activeSession/${sessionId}`)); }, + triggerSessionUpdate: async (id) => { + await get().internal_updateSession(id, { updatedAt: new Date() }); + }, + updateSearchKeywords: (keywords) => { set( { isSearching: !!keywords, sessionSearchKeywords: keywords }, diff --git a/src/types/session/index.ts b/src/types/session/index.ts index e94bb538dc55..d79493038167 100644 --- a/src/types/session/index.ts +++ b/src/types/session/index.ts @@ -1,5 +1,5 @@ import { LobeSessions } from '@/types/session/agentSession'; -import { LobeSessionGroups } from '@/types/session/sessionGroup'; +import { LobeSessionGroups, SessionGroupId } from '@/types/session/sessionGroup'; export * from './agentSession'; export * from './sessionGroup'; @@ -8,3 +8,10 @@ export interface ChatSessionList { sessionGroups: LobeSessionGroups; sessions: LobeSessions; } + +export interface UpdateSessionParams { + group?: SessionGroupId; + meta?: any; + pinned?: boolean; + updatedAt: Date; +} From 2c0b8b4d5800359714034c7f5e16655b143057e8 Mon Sep 17 00:00:00 2001 From: Arvin Xu Date: Wed, 17 Jul 2024 09:31:00 +0000 Subject: [PATCH 2/2] fix test --- src/services/session/client.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/services/session/client.test.ts b/src/services/session/client.test.ts index 0ea122a85f6e..aa5299782b79 100644 --- a/src/services/session/client.test.ts +++ b/src/services/session/client.test.ts @@ -20,6 +20,7 @@ vi.mock('@/database/client/models/session', () => { update: vi.fn(), count: vi.fn(), batchCreate: vi.fn(), + findById: vi.fn(), isEmpty: vi.fn(), queryByKeyword: vi.fn(), updateConfig: vi.fn(),