@lobehub/chat
Version:
Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.
49 lines (42 loc) • 1.51 kB
text/typescript
import { StateCreator } from 'zustand/vanilla';
import { chainSummaryHistory } from '@/chains/summaryHistory';
import { TraceNameMap } from '@/const/trace';
import { chatService } from '@/services/chat';
import { topicService } from '@/services/topic';
import { ChatStore } from '@/store/chat';
import { useUserStore } from '@/store/user';
import { systemAgentSelectors } from '@/store/user/selectors';
import { ChatMessage } from '@/types/message';
export interface ChatMemoryAction {
internal_summaryHistory: (messages: ChatMessage[]) => Promise<void>;
}
export const chatMemory: StateCreator<
ChatStore,
[['zustand/devtools', never]],
[],
ChatMemoryAction
> = (set, get) => ({
internal_summaryHistory: async (messages) => {
const topicId = get().activeTopicId;
if (messages.length <= 1 || !topicId) return;
const { model, provider } = systemAgentSelectors.historyCompress(useUserStore.getState());
let historySummary = '';
await chatService.fetchPresetTaskResult({
onFinish: async (text) => {
historySummary = text;
},
params: { ...chainSummaryHistory(messages), model, provider, stream: false },
trace: {
sessionId: get().activeId,
topicId: get().activeTopicId,
traceName: TraceNameMap.SummaryHistoryMessages,
},
});
await topicService.updateTopic(topicId, {
historySummary,
metadata: { model, provider },
});
await get().refreshTopic();
await get().refreshMessages();
},
});