Skip to content

Commit

Permalink
🐛 fix: fix session not reorder after send message (#3239)
Browse files Browse the repository at this point in the history
* 🐛 fix: fix session not reorder after send message

* fix test
  • Loading branch information
arvinxx authored Jul 17, 2024
1 parent 6d19fb2 commit 7245a08
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/services/session/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
5 changes: 4 additions & 1 deletion src/services/session/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
SessionGroupItem,
SessionGroups,
} from '@/types/session';
import { merge } from '@/utils/merge';

import { ISessionService } from './type';

Expand Down Expand Up @@ -94,7 +95,9 @@ export class ClientService implements ISessionService {
data: Partial<Pick<LobeAgentSession, 'group' | 'meta' | 'pinned'>>,
) {
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(
Expand Down
11 changes: 4 additions & 7 deletions src/services/session/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import {
LobeAgentSession,
LobeSessionType,
LobeSessions,
SessionGroupId,
SessionGroupItem,
SessionGroups,
UpdateSessionParams,
} from '@/types/session';

import { ISessionService } from './type';
Expand Down Expand Up @@ -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<any> {
const { group, pinned, meta } = data;
updateSession(id: string, data: Partial<UpdateSessionParams>): Promise<any> {
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 },
});
}

Expand Down
3 changes: 3 additions & 0 deletions src/store/chat/slices/message/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions src/store/session/slices/session/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
LobeSessionGroups,
LobeSessionType,
LobeSessions,
SessionGroupId,
UpdateSessionParams,
} from '@/types/session';
import { merge } from '@/utils/merge';
import { setNamespace } from '@/utils/storeDebug';
Expand Down Expand Up @@ -52,6 +52,7 @@ export interface SessionAction {
isSwitchSession?: boolean,
) => Promise<string>;
duplicateSession: (id: string) => Promise<void>;
triggerSessionUpdate: (id: string) => Promise<void>;
updateSessionGroupId: (sessionId: string, groupId: string) => Promise<void>;
updateSessionMeta: (meta: Partial<MetaData>) => void;

Expand All @@ -75,10 +76,7 @@ export interface SessionAction {
useSearchSessions: (keyword?: string) => SWRResponse<any>;

internal_dispatchSessions: (payload: SessionDispatch) => void;
internal_updateSession: (
id: string,
data: Partial<{ group?: SessionGroupId; meta?: any; pinned?: boolean }>,
) => Promise<void>;
internal_updateSession: (id: string, data: Partial<UpdateSessionParams>) => Promise<void>;
internal_processSessions: (
sessions: LobeSessions,
customGroups: LobeSessionGroups,
Expand Down Expand Up @@ -117,7 +115,6 @@ export const createSessionSlice: StateCreator<

return id;
},

duplicateSession: async (id) => {
const { switchSession, refreshSessions } = get();
const session = sessionSelectors.getSessionById(id)(get());
Expand Down Expand Up @@ -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();
Expand All @@ -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 },
Expand Down
9 changes: 8 additions & 1 deletion src/types/session/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -8,3 +8,10 @@ export interface ChatSessionList {
sessionGroups: LobeSessionGroups;
sessions: LobeSessions;
}

export interface UpdateSessionParams {
group?: SessionGroupId;
meta?: any;
pinned?: boolean;
updatedAt: Date;
}

0 comments on commit 7245a08

Please sign in to comment.