File size: 8,817 Bytes
8ede856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
 * Persona 文件夹管理 Store
 */
import { defineStore } from 'pinia';
import axios from 'axios';

// 类型定义
export interface PersonaFolder {
  folder_id: string;
  name: string;
  parent_id: string | null;
  description: string | null;
  sort_order: number;
  created_at: string;
  updated_at: string;
}

export interface Persona {
  persona_id: string;
  system_prompt: string;
  custom_error_message: string | null;
  begin_dialogs: string[];
  tools: string[] | null;
  skills: string[] | null;
  folder_id: string | null;
  sort_order: number;
  created_at: string;
  updated_at: string;
}

export interface FolderTreeNode {
  folder_id: string;
  name: string;
  parent_id: string | null;
  description: string | null;
  sort_order: number;
  children: FolderTreeNode[];
}

export interface ReorderItem {
  id: string;
  type: 'persona' | 'folder';
  sort_order: number;
}

export const usePersonaStore = defineStore({
  id: 'persona',
  state: () => ({
    folderTree: [] as FolderTreeNode[],
    currentFolderId: null as string | null,
    currentFolders: [] as PersonaFolder[],
    currentPersonas: [] as Persona[],
    breadcrumbPath: [] as FolderTreeNode[],
    expandedFolderIds: [] as string[], // Store expanded folder IDs
    loading: false,
    treeLoading: false,
  }),

  getters: {
    // 当前文件夹名称
    currentFolderName(): string {
      if (this.breadcrumbPath.length === 0) {
        return '根目录';
      }
      return this.breadcrumbPath[this.breadcrumbPath.length - 1]?.name || '根目录';
    },
  },

  actions: {
    /**
     * Toggle folder expansion state
     */
    toggleFolderExpansion(folderId: string) {
      const index = this.expandedFolderIds.indexOf(folderId);
      if (index === -1) {
        this.expandedFolderIds.push(folderId);
      } else {
        this.expandedFolderIds.splice(index, 1);
      }
    },

    /**
     * Set folder expansion state
     */
    setFolderExpansion(folderId: string, expanded: boolean) {
      const index = this.expandedFolderIds.indexOf(folderId);
      if (expanded && index === -1) {
        this.expandedFolderIds.push(folderId);
      } else if (!expanded && index !== -1) {
        this.expandedFolderIds.splice(index, 1);
      }
    },

    /**
     * 加载文件夹树形结构
     */
    async loadFolderTree(): Promise<void> {
      this.treeLoading = true;
      try {
        const response = await axios.get('/api/persona/folder/tree');
        if (response.data.status === 'ok') {
          this.folderTree = response.data.data || [];
        } else {
          throw new Error(response.data.message || '获取文件夹树失败');
        }
      } finally {
        this.treeLoading = false;
      }
    },

    /**
     * 导航到指定文件夹
     */
    async navigateToFolder(folderId: string | null): Promise<void> {
      this.loading = true;
      try {
        this.currentFolderId = folderId;

        // 并行加载子文件夹和 Persona
        const [foldersRes, personasRes] = await Promise.all([
          axios.get('/api/persona/folder/list', {
            params: { parent_id: folderId ?? '' }
          }),
          axios.get('/api/persona/list', {
            params: { folder_id: folderId ?? '' }
          }),
        ]);

        if (foldersRes.data.status === 'ok') {
          this.currentFolders = foldersRes.data.data || [];
        }

        if (personasRes.data.status === 'ok') {
          this.currentPersonas = personasRes.data.data || [];
        }

        // 更新面包屑
        this.updateBreadcrumb(folderId);
      } finally {
        this.loading = false;
      }
    },

    /**
     * 更新面包屑路径
     */
    updateBreadcrumb(folderId: string | null): void {
      if (folderId === null) {
        this.breadcrumbPath = [];
        return;
      }

      // 从树中查找路径
      const path: FolderTreeNode[] = [];
      const findPath = (nodes: FolderTreeNode[], targetId: string): boolean => {
        for (const node of nodes) {
          if (node.folder_id === targetId) {
            path.push(node);
            return true;
          }
          if (node.children.length > 0 && findPath(node.children, targetId)) {
            path.unshift(node);
            return true;
          }
        }
        return false;
      };

      findPath(this.folderTree, folderId);
      this.breadcrumbPath = path;
    },

    /**
     * 刷新当前文件夹内容
     */
    async refreshCurrentFolder(): Promise<void> {
      await this.navigateToFolder(this.currentFolderId);
    },

    /**
     * 移动 Persona 到文件夹
     */
    async movePersonaToFolder(personaId: string, targetFolderId: string | null): Promise<void> {
      const response = await axios.post('/api/persona/move', {
        persona_id: personaId,
        folder_id: targetFolderId
      });

      if (response.data.status !== 'ok') {
        throw new Error(response.data.message || '移动人格失败');
      }

      // 刷新当前文件夹内容和文件夹树
      await Promise.all([
        this.refreshCurrentFolder(),
        this.loadFolderTree(),
      ]);
    },

    /**
     * 移动文件夹到另一个文件夹
     */
    async moveFolderToFolder(folderId: string, targetParentId: string | null): Promise<void> {
      const response = await axios.post('/api/persona/folder/update', {
        folder_id: folderId,
        parent_id: targetParentId
      });

      if (response.data.status !== 'ok') {
        throw new Error(response.data.message || '移动文件夹失败');
      }

      // 刷新当前文件夹内容和文件夹树
      await Promise.all([
        this.refreshCurrentFolder(),
        this.loadFolderTree(),
      ]);
    },

    /**
     * 创建文件夹
     */
    async createFolder(data: {
      name: string;
      parent_id?: string | null;
      description?: string;
    }): Promise<PersonaFolder> {
      const response = await axios.post('/api/persona/folder/create', {
        ...data,
        parent_id: data.parent_id ?? this.currentFolderId,
      });

      if (response.data.status !== 'ok') {
        throw new Error(response.data.message || '创建文件夹失败');
      }

      // 刷新当前文件夹内容和文件夹树
      await Promise.all([
        this.refreshCurrentFolder(),
        this.loadFolderTree(),
      ]);

      return response.data.data.folder;
    },

    /**
     * 更新文件夹
     */
    async updateFolder(data: {
      folder_id: string;
      name?: string;
      description?: string;
    }): Promise<void> {
      const response = await axios.post('/api/persona/folder/update', data);

      if (response.data.status !== 'ok') {
        throw new Error(response.data.message || '更新文件夹失败');
      }

      // 刷新当前文件夹内容和文件夹树
      await Promise.all([
        this.refreshCurrentFolder(),
        this.loadFolderTree(),
      ]);
    },

    /**
     * 删除文件夹
     */
    async deleteFolder(folderId: string): Promise<void> {
      const response = await axios.post('/api/persona/folder/delete', {
        folder_id: folderId
      });

      if (response.data.status !== 'ok') {
        throw new Error(response.data.message || '删除文件夹失败');
      }

      // 刷新当前文件夹内容和文件夹树
      await Promise.all([
        this.refreshCurrentFolder(),
        this.loadFolderTree(),
      ]);
    },

    /**
     * 删除 Persona
     */
    async deletePersona(personaId: string): Promise<void> {
      const response = await axios.post('/api/persona/delete', {
        persona_id: personaId
      });

      if (response.data.status !== 'ok') {
        throw new Error(response.data.message || '删除人格失败');
      }

      // 刷新当前文件夹内容
      await this.refreshCurrentFolder();
    },

    /**
     * 批量更新排序
     */
    async reorderItems(items: ReorderItem[]): Promise<void> {
      const response = await axios.post('/api/persona/reorder', { items });

      if (response.data.status !== 'ok') {
        throw new Error(response.data.message || '更新排序失败');
      }

      // 刷新当前文件夹内容
      await this.refreshCurrentFolder();
    },

    /**
     * 根据文件夹 ID 查找树节点
     */
    findFolderInTree(folderId: string): FolderTreeNode | null {
      const findNode = (nodes: FolderTreeNode[]): FolderTreeNode | null => {
        for (const node of nodes) {
          if (node.folder_id === folderId) {
            return node;
          }
          if (node.children.length > 0) {
            const found = findNode(node.children);
            if (found) return found;
          }
        }
        return null;
      };
      return findNode(this.folderTree);
    },
  }
});