({ chatId, subChatId, format, isRemote = false }: ExportOptions)
| 109 | * Shows download dialog to save the exported content. |
| 110 | */ |
| 111 | export async function exportChat({ chatId, subChatId, format, isRemote = false }: ExportOptions): Promise<void> { |
| 112 | try { |
| 113 | let exportData: { content: string; filename: string } |
| 114 | |
| 115 | if (isRemote) { |
| 116 | // Remote chat export - fetch from remote API and format locally |
| 117 | const chat = await remoteApi.getAgentChat(chatId) |
| 118 | const subChat = subChatId |
| 119 | ? chat.subChats.find(sc => sc.id === subChatId) |
| 120 | : chat.subChats[0] |
| 121 | |
| 122 | if (!subChat) { |
| 123 | throw new Error("No chat data found") |
| 124 | } |
| 125 | |
| 126 | const messages = (subChat.messages || []) as Message[] |
| 127 | const chatName = subChat.name || chat.name || "remote-chat" |
| 128 | exportData = formatMessages(messages, format, chatName) |
| 129 | } else { |
| 130 | // Local chat export - use existing tRPC endpoint |
| 131 | exportData = await trpcClient.chats.exportChat.query({ |
| 132 | chatId, |
| 133 | subChatId, |
| 134 | format, |
| 135 | }) |
| 136 | } |
| 137 | |
| 138 | const blob = new Blob([exportData.content], { type: "text/plain;charset=utf-8" }) |
| 139 | const url = URL.createObjectURL(blob) |
| 140 | const link = document.createElement("a") |
| 141 | link.href = url |
| 142 | link.download = exportData.filename |
| 143 | document.body.appendChild(link) |
| 144 | link.click() |
| 145 | document.body.removeChild(link) |
| 146 | URL.revokeObjectURL(url) |
| 147 | |
| 148 | toast.success("Export complete", { |
| 149 | description: `Saved as ${exportData.filename}`, |
| 150 | }) |
| 151 | } catch (error) { |
| 152 | console.error("[exportChat] Error:", error) |
| 153 | toast.error("Export failed", { |
| 154 | description: error instanceof Error ? error.message : "Unable to export chat", |
| 155 | }) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Copy chat or sub-chat content to clipboard. |
no test coverage detected