File size: 3,547 Bytes
e85fa50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { PromptGroup, Prompt, DslFile } from '../types';
import JSZip from 'jszip';
import { saveAs } from 'file-saver';

// 将提示词转换为 Markdown 格式
const formatPromptToMarkdown = (prompt: Prompt): string => {
  return `# ${prompt.title}



${prompt.content}



## 标签

${prompt.tags.map(tag => `- ${tag}`).join('\n')}



## 元数据

- 创建时间: ${new Date(prompt.createdAt).toLocaleString()}

- 更新时间: ${new Date(prompt.updatedAt).toLocaleString()}

`;
};

// 导出提示词组为 ZIP
export const exportPromptGroupToZip = async (group: PromptGroup) => {
  const zip = new JSZip();
  
  // 创建提示词文件夹
  const promptsFolder = zip.folder('prompts');
  if (!promptsFolder) throw new Error('创建提示词文件夹失败');
  
  // 添加提示词为 Markdown 文件
  group.prompts.forEach(prompt => {
    const content = formatPromptToMarkdown(prompt);
    promptsFolder.file(`${prompt.title.replace(/[\\/:*?"<>|]/g, '_')}.md`, content);

  });

  

  // 创建 DSL 文件文件夹

  const dslFolder = zip.folder('dsl_files');

  if (!dslFolder) throw new Error('创建 DSL 文件夹失败');

  

  // 添加 DSL 文件 - 直接使用文本内容

  group.dslFiles.forEach(file => {

    try {

      dslFolder.file(file.name, file.content);

    } catch (error) {

      console.error(`处理文件 ${file.name} 时出错:`, error);

    }

  });

  

  // 添加元数据文件

  const metadata = JSON.stringify({

    name: group.name,

    description: group.description,

    category: typeof group.category === 'object' ? group.category.name : group.category,

    createdAt: group.createdAt,

    updatedAt: group.updatedAt,

    promptCount: group.prompts.length,

    dslFileCount: group.dslFiles.length

  }, null, 2);

  

  zip.file('metadata.json', metadata);

  

  // 创建并下载 ZIP 文件

  const zipBlob = await zip.generateAsync({ type: 'blob' });

  const fileName = `${group.name.replace(/[\\/:*?"<>|]/g, '_')}_${new Date().toISOString().split('T')[0]}.zip`;
  saveAs(zipBlob, fileName);
};

// 导出单个提示词为 Markdown
export const exportPrompt = (prompt: Prompt) => {
  const content = formatPromptToMarkdown(prompt);
  const blob = new Blob([content], { type: 'text/markdown;charset=utf-8' });
  saveAs(blob, `${prompt.title.replace(/[\\/:*?"<>|]/g, '_')}.md`);

};



// 导出多个提示词为 ZIP

export const exportMultiplePrompts = async (prompts: Prompt[]) => {

  const zip = new JSZip();

  

  prompts.forEach(prompt => {

    const content = formatPromptToMarkdown(prompt);

    zip.file(`${prompt.title.replace(/[\\/:*?"<>|]/g, '_')}.md`, content);
  });
  
  const zipBlob = await zip.generateAsync({ type: 'blob' });
  saveAs(zipBlob, `prompts_${new Date().toISOString().split('T')[0]}.zip`);
};

// 导出单个 DSL 文件
export const exportDslFile = (file: DslFile) => {
  const blob = new Blob([file.content], { type: 'text/yaml;charset=utf-8' });
  saveAs(blob, file.name);
};

// 导出多个 DSL 文件为 ZIP
export const exportMultipleDslFiles = async (files: DslFile[]) => {
  const zip = new JSZip();
  
  files.forEach(file => {
    zip.file(file.name, file.content);
  });
  
  const zipBlob = await zip.generateAsync({ type: 'blob' });
  saveAs(zipBlob, `dsl_files_${new Date().toISOString().split('T')[0]}.zip`);
};
// eslint-disable-next-line
export default {
  exportPrompt,
  exportMultiplePrompts,
  exportPromptGroupToZip,
  exportDslFile,
  exportMultipleDslFiles
};