1
import { importService } from '@/services/import';
2
import { messageService } from '@/services/message';
3
import { sessionService } from '@/services/session';
4
import { topicService } from '@/services/topic';
5
import { useSessionStore } from '@/store/session';
6
import { sessionSelectors } from '@/store/session/selectors';
7
import { useUserStore } from '@/store/user';
8
import { settingsSelectors } from '@/store/user/selectors';
9
import { ConfigFile } from '@/types/exportConfig';
10
import { ImportStage, OnImportCallbacks } from '@/types/importer';
11
import { createConfigFile, exportConfigFile } from '@/utils/config';
13
export interface ImportResult {
18
export interface ImportResults {
19
messages?: ImportResult;
20
sessionGroups?: ImportResult;
21
sessions?: ImportResult;
22
topics?: ImportResult;
27
importConfigState = async (config: ConfigFile, callbacks?: OnImportCallbacks): Promise<void> => {
28
if (config.exportType === 'settings') {
29
await importService.importSettings(config.state.settings);
30
callbacks?.onStageChange?.(ImportStage.Success);
34
if (config.exportType === 'all') {
35
await importService.importSettings(config.state.settings);
38
await importService.importData(
40
messages: (config.state as any).messages || [],
41
sessionGroups: (config.state as any).sessionGroups || [],
42
sessions: (config.state as any).sessions || [],
43
topics: (config.state as any).topics || [],
44
version: config.version,
55
exportAgents = async () => {
56
const agents = await sessionService.getSessionsByType('agent');
57
const sessionGroups = await sessionService.getSessionGroups();
59
const config = createConfigFile('agents', { sessionGroups, sessions: agents });
61
exportConfigFile(config, 'agents');
67
exportSessions = async () => {
68
const sessions = await sessionService.getSessionsByType();
69
const sessionGroups = await sessionService.getSessionGroups();
70
const messages = await messageService.getAllMessages();
71
const topics = await topicService.getAllTopics();
73
const config = createConfigFile('sessions', { messages, sessionGroups, sessions, topics });
75
exportConfigFile(config, 'sessions');
81
exportSingleSession = async (id: string) => {
82
const session = this.getSession(id);
85
const messages = await messageService.getAllMessagesInSession(id);
86
const topics = await topicService.getTopics({ sessionId: id });
88
const config = createConfigFile('singleSession', { messages, sessions: [session], topics });
90
exportConfigFile(config, `${session.meta?.title}-session`);
96
exportSingleTopic = async (sessionId: string, topicId: string) => {
97
const session = this.getSession(sessionId);
100
const messages = await messageService.getMessages(sessionId, topicId);
101
const topics = await topicService.getTopics({ sessionId });
103
const topic = topics.find((item) => item.id === topicId);
106
const config = createConfigFile('singleSession', {
112
exportConfigFile(config, `${topic.title}-topic`);
115
exportSingleAgent = async (id: string) => {
116
const agent = this.getAgent(id);
119
const config = createConfigFile('agents', { sessionGroups: [], sessions: [agent] });
121
exportConfigFile(config, agent.meta?.title || 'agent');
127
exportSettings = async () => {
128
const settings = this.getSettings();
130
const config = createConfigFile('settings', { settings });
132
exportConfigFile(config, 'settings');
138
exportAll = async () => {
139
const sessions = await sessionService.getSessionsByType();
140
const sessionGroups = await sessionService.getSessionGroups();
141
const messages = await messageService.getAllMessages();
142
const topics = await topicService.getAllTopics();
143
const settings = this.getSettings();
145
const config = createConfigFile('all', { messages, sessionGroups, sessions, settings, topics });
147
exportConfigFile(config, 'config');
150
private getSettings = () => settingsSelectors.exportSettings(useUserStore.getState());
152
private getSession = (id: string) =>
153
sessionSelectors.getSessionById(id)(useSessionStore.getState());
155
private getAgent = (id: string) =>
156
sessionSelectors.getSessionById(id)(useSessionStore.getState());
159
export const configService = new ConfigService();