( title: string, messages: ExportMessage[], createdAt: number, format: ExportFormat, labels: ExportLabels )
| 141 | * 导出对话到下载目录 |
| 142 | */ |
| 143 | export async function exportConversation( |
| 144 | title: string, |
| 145 | messages: ExportMessage[], |
| 146 | createdAt: number, |
| 147 | format: ExportFormat, |
| 148 | labels: ExportLabels |
| 149 | ): Promise<{ success: boolean; filePath?: string; error?: string }> { |
| 150 | if (messages.length === 0) { |
| 151 | return { success: false, error: 'No messages to export' } |
| 152 | } |
| 153 | |
| 154 | // 格式化内容 |
| 155 | let content: string |
| 156 | let filename: string |
| 157 | const timestamp = dayjs(createdAt).format('YYYYMMDD_HHmmss') |
| 158 | const safeTitle = sanitizeFilename(title) |
| 159 | |
| 160 | if (format === 'markdown') { |
| 161 | content = formatAsMarkdown(title, messages, createdAt, labels) |
| 162 | filename = `${safeTitle}_${timestamp}.md` |
| 163 | } else { |
| 164 | content = formatAsPlainText(title, messages, createdAt, labels) |
| 165 | filename = `${safeTitle}_${timestamp}.txt` |
| 166 | } |
| 167 | |
| 168 | // 转换为 data URL 并保存 |
| 169 | const dataUrl = `data:text/plain;charset=utf-8,${encodeURIComponent(content)}` |
| 170 | const { useCacheService } = await import('@/services/cache/service') |
| 171 | const result = await useCacheService().saveToDownloads(filename, dataUrl) |
| 172 | |
| 173 | return result |
| 174 | } |
nothing calls this directly
no test coverage detected