lobe-chat

Форк
0
/
config.ts 
159 строк · 4.8 Кб
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';
12

13
export interface ImportResult {
14
  added: number;
15
  errors: number;
16
  skips: number;
17
}
18
export interface ImportResults {
19
  messages?: ImportResult;
20
  sessionGroups?: ImportResult;
21
  sessions?: ImportResult;
22
  topics?: ImportResult;
23
  type?: string;
24
}
25

26
class ConfigService {
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);
31
      return;
32
    }
33

34
    if (config.exportType === 'all') {
35
      await importService.importSettings(config.state.settings);
36
    }
37

38
    await importService.importData(
39
      {
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,
45
      },
46
      callbacks,
47
    );
48
  };
49

50
  // TODO: Separate export feature into a new service like importService
51

52
  /**
53
   * export all agents
54
   */
55
  exportAgents = async () => {
56
    const agents = await sessionService.getSessionsByType('agent');
57
    const sessionGroups = await sessionService.getSessionGroups();
58

59
    const config = createConfigFile('agents', { sessionGroups, sessions: agents });
60

61
    exportConfigFile(config, 'agents');
62
  };
63

64
  /**
65
   * export all sessions
66
   */
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();
72

73
    const config = createConfigFile('sessions', { messages, sessionGroups, sessions, topics });
74

75
    exportConfigFile(config, 'sessions');
76
  };
77

78
  /**
79
   * export a session
80
   */
81
  exportSingleSession = async (id: string) => {
82
    const session = this.getSession(id);
83
    if (!session) return;
84

85
    const messages = await messageService.getAllMessagesInSession(id);
86
    const topics = await topicService.getTopics({ sessionId: id });
87

88
    const config = createConfigFile('singleSession', { messages, sessions: [session], topics });
89

90
    exportConfigFile(config, `${session.meta?.title}-session`);
91
  };
92

93
  /**
94
   * export a topic
95
   */
96
  exportSingleTopic = async (sessionId: string, topicId: string) => {
97
    const session = this.getSession(sessionId);
98
    if (!session) return;
99

100
    const messages = await messageService.getMessages(sessionId, topicId);
101
    const topics = await topicService.getTopics({ sessionId });
102

103
    const topic = topics.find((item) => item.id === topicId);
104
    if (!topic) return;
105

106
    const config = createConfigFile('singleSession', {
107
      messages,
108
      sessions: [session],
109
      topics: [topic],
110
    });
111

112
    exportConfigFile(config, `${topic.title}-topic`);
113
  };
114

115
  exportSingleAgent = async (id: string) => {
116
    const agent = this.getAgent(id);
117
    if (!agent) return;
118

119
    const config = createConfigFile('agents', { sessionGroups: [], sessions: [agent] });
120

121
    exportConfigFile(config, agent.meta?.title || 'agent');
122
  };
123

124
  /**
125
   * export settings
126
   */
127
  exportSettings = async () => {
128
    const settings = this.getSettings();
129

130
    const config = createConfigFile('settings', { settings });
131

132
    exportConfigFile(config, 'settings');
133
  };
134

135
  /**
136
   * export all data
137
   */
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();
144

145
    const config = createConfigFile('all', { messages, sessionGroups, sessions, settings, topics });
146

147
    exportConfigFile(config, 'config');
148
  };
149

150
  private getSettings = () => settingsSelectors.exportSettings(useUserStore.getState());
151

152
  private getSession = (id: string) =>
153
    sessionSelectors.getSessionById(id)(useSessionStore.getState());
154

155
  private getAgent = (id: string) =>
156
    sessionSelectors.getSessionById(id)(useSessionStore.getState());
157
}
158

159
export const configService = new ConfigService();
160

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.