/
useInitConversation.ts
44 lines (37 loc) 路 1.25 KB
/
useInitConversation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { useEffect } from 'react';
import { useChatStore } from '@/store/chat';
import { useSessionStore } from '@/store/session';
import { agentSelectors } from '@/store/session/selectors';
import { useToolStore } from '@/store/tool';
export const useInitConversation = () => {
const [sessionId] = useSessionStore((s) => [s.activeId]);
const plugins = useSessionStore((s) => agentSelectors.currentAgentPlugins(s));
const [init, activeTopicId, switchTopic, useFetchMessages, useFetchTopics] = useChatStore((s) => [
s.messagesInit,
s.activeTopicId,
s.switchTopic,
s.useFetchMessages,
s.useFetchTopics,
]);
useFetchMessages(sessionId, activeTopicId);
useFetchTopics(sessionId);
const [useFetchPluginStore, useFetchInstalledPlugins, checkPluginsIsInstalled] = useToolStore(
(s) => [s.useFetchPluginStore, s.useFetchInstalledPlugins, s.useCheckPluginsIsInstalled],
);
useFetchPluginStore();
useFetchInstalledPlugins();
checkPluginsIsInstalled(plugins);
useEffect(() => {
// // when activeId changed, switch topic to undefined
const unsubscribe = useSessionStore.subscribe(
(s) => s.activeId,
() => {
switchTopic();
},
);
return () => {
unsubscribe();
};
}, []);
return init;
};