Skip to content

Commit

Permalink
♻️ refactor: refactor session meta method (lobehub#2737)
Browse files Browse the repository at this point in the history
* ♻️ refactor: add session meta

* ✅ test: fix test

* ✅ test: fix test
  • Loading branch information
arvinxx authored May 31, 2024
1 parent 36ce761 commit b103c3c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/services/session/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SessionGroupModel } from '@/database/client/models/sessionGroup';
import { UserModel } from '@/database/client/models/user';
import { useUserStore } from '@/store/user';
import { LobeAgentChatConfig, LobeAgentConfig } from '@/types/agent';
import { MetaData } from '@/types/meta';
import {
ChatSessionList,
LobeAgentSession,
Expand Down Expand Up @@ -111,6 +112,18 @@ export class ClientService implements ISessionService {
return SessionModel.updateConfig(activeId, config);
}

async updateSessionMeta(
activeId: string,
meta: Partial<MetaData>,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_?: AbortSignal,
) {
// inbox 不允许修改 meta
if (activeId === INBOX_SESSION_ID) return;

return SessionModel.update(activeId, { meta });
}

async updateSessionChatConfig(
activeId: string,
config: DeepPartial<LobeAgentChatConfig>,
Expand Down
4 changes: 4 additions & 0 deletions src/services/session/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { DeepPartial } from 'utility-types';

import { LobeAgentChatConfig, LobeAgentConfig } from '@/types/agent';
import { MetaData } from '@/types/meta';
import { BatchTaskResult } from '@/types/service';
import {
ChatSessionList,
Expand Down Expand Up @@ -35,6 +36,9 @@ export interface ISessionService {
config: DeepPartial<LobeAgentConfig>,
signal?: AbortSignal,
): Promise<any>;

updateSessionMeta(id: string, meta: Partial<MetaData>, signal?: AbortSignal): Promise<any>;

updateSessionChatConfig(
id: string,
config: DeepPartial<LobeAgentChatConfig>,
Expand Down
5 changes: 3 additions & 2 deletions src/store/session/slices/session/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ vi.mock('@/services/session', () => ({
removeSession: vi.fn(),
getAllSessions: vi.fn(),
updateSession: vi.fn(),
updateSessionMeta: vi.fn(),
updateSessionGroupId: vi.fn(),
searchSessions: vi.fn(),
updateSessionPinned: vi.fn(),
Expand Down Expand Up @@ -211,7 +212,7 @@ describe('SessionAction', () => {
it('should update session meta and refresh sessions', async () => {
const { result } = renderHook(() => useSessionStore());
const meta = { title: 'Test Agent' };
const updateSessionMock = vi.spyOn(sessionService, 'updateSession');
const updateSessionMock = vi.spyOn(sessionService, 'updateSessionMeta');
const refreshSessionsMock = vi.spyOn(result.current, 'refreshSessions');

// 模拟有当前会话
Expand All @@ -222,7 +223,7 @@ describe('SessionAction', () => {
await result.current.updateSessionMeta(meta);
});

expect(updateSessionMock).toHaveBeenCalledWith('session-id', { meta });
expect(updateSessionMock).toHaveBeenCalledWith('session-id', meta, expect.any(AbortSignal));
expect(refreshSessionsMock).toHaveBeenCalled();
updateSessionMock.mockRestore();
refreshSessionsMock.mockRestore();
Expand Down
7 changes: 6 additions & 1 deletion src/store/session/slices/session/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ export const createSessionSlice: StateCreator<

const { activeId, refreshSessions } = get();

await sessionService.updateSession(activeId, { meta });
const abortController = get().signalSessionMeta as AbortController;
if (abortController) abortController.abort('canceled');
const controller = new AbortController();
set({ signalSessionMeta: controller }, false, 'updateSessionMetaSignal');

await sessionService.updateSessionMeta(activeId, meta, controller.signal);
await refreshSessions();
},

Expand Down
1 change: 1 addition & 0 deletions src/store/session/slices/session/initialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface SessionState {
* it means defaultSessions
*/
sessions: LobeAgentSession[];
signalSessionMeta?: AbortController;
}

export const initialSessionState: SessionState = {
Expand Down

0 comments on commit b103c3c

Please sign in to comment.