diff --git a/src/services/session/client.ts b/src/services/session/client.ts index 9c8ff18bb5fe..0662b171834b 100644 --- a/src/services/session/client.ts +++ b/src/services/session/client.ts @@ -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, @@ -111,6 +112,18 @@ export class ClientService implements ISessionService { return SessionModel.updateConfig(activeId, config); } + async updateSessionMeta( + activeId: string, + meta: Partial, + // 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, diff --git a/src/services/session/type.ts b/src/services/session/type.ts index 7561c4b11e3c..f51f2f39780d 100644 --- a/src/services/session/type.ts +++ b/src/services/session/type.ts @@ -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, @@ -35,6 +36,9 @@ export interface ISessionService { config: DeepPartial, signal?: AbortSignal, ): Promise; + + updateSessionMeta(id: string, meta: Partial, signal?: AbortSignal): Promise; + updateSessionChatConfig( id: string, config: DeepPartial, diff --git a/src/store/session/slices/session/action.test.ts b/src/store/session/slices/session/action.test.ts index 0e5ec07f9531..ce6e38c7d47f 100644 --- a/src/store/session/slices/session/action.test.ts +++ b/src/store/session/slices/session/action.test.ts @@ -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(), @@ -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'); // 模拟有当前会话 @@ -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(); diff --git a/src/store/session/slices/session/action.ts b/src/store/session/slices/session/action.ts index e96e75950473..e775e87cf6f9 100644 --- a/src/store/session/slices/session/action.ts +++ b/src/store/session/slices/session/action.ts @@ -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(); }, diff --git a/src/store/session/slices/session/initialState.ts b/src/store/session/slices/session/initialState.ts index 9dca43cc0cff..e229244b417d 100644 --- a/src/store/session/slices/session/initialState.ts +++ b/src/store/session/slices/session/initialState.ts @@ -16,6 +16,7 @@ export interface SessionState { * it means defaultSessions */ sessions: LobeAgentSession[]; + signalSessionMeta?: AbortController; } export const initialSessionState: SessionState = {