SPB Git

Repository Public

Export.swift 8.5 KB 151 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatConversations/Sources/Export.swift3// Description: Conversation export (Markdown, plain text, lossless versioned JSON) and JSON import.4import Foundation5import GRDB6import LocalChatCore78public enum ExportScope: Sendable { case activeBranch, wholeTree }910public struct ConversationArchive: Codable, Sendable {11    public static let formatName = "localchat.conversation"12    public static let currentVersion = 113    public var format: String14    public var version: Int15    public var exportedAt: Date16    public var scope: String17    public var conversation: Conversation18    public var messages: [ChatMessage]19}2021public enum ImportIDPolicy: Sendable {22    case newIDsOnConflict   // keep ids unless the conversation id already exists (default)23    case alwaysNewIDs24}2526extension ConversationStore {27    func exportMaterial(_ id: UUID, scope: ExportScope) throws -> (Conversation, [ChatMessage], ConversationTree) {28        try db.read { db in29            guard let c = try Self.fetchConversation(db, id) else { throw ConversationStoreError.conversationNotFound }30            let all = try Self.fetchMessages(db, id); let tree = ConversationTree(all)31            return (c, scope == .wholeTree ? all : tree.path(to: c.activeLeafID), tree)32        }33    }3435    public func exportMarkdown(_ id: UUID, scope: ExportScope = .activeBranch) throws -> String {36        let (c, messages, tree) = try exportMaterial(id, scope: scope)37        var out = "# \(c.title)\n\n"38        out += "*Exported from Local Chat on \(Date().formatted(date: .abbreviated, time: .shortened))*\n\n"39        if !c.tags.isEmpty { out += "Tags: " + c.tags.map { "`\($0)`" }.joined(separator: " ") + "\n\n" }40        if !c.systemPrompt.isEmpty { out += "> **System prompt**\n" + c.systemPrompt.split(separator: "\n", omittingEmptySubsequences: false).map { "> \($0)" }.joined(separator: "\n") + "\n\n" }41        switch scope {42        case .activeBranch:43            out += messages.map(Self.markdown).joined(separator: "\n")44        case .wholeTree:45            let leaves = messages.filter { tree.children[$0.id] == nil }.sorted { $0.createdAt < $1.createdAt }46            for (i, leaf) in leaves.enumerated() {47                if leaves.count > 1 { out += "---\n\n## Branch \(i + 1) of \(leaves.count)\(leaf.id == c.activeLeafID ? " (active)" : "")\n\n" }48                out += tree.path(to: leaf.id).map(Self.markdown).joined(separator: "\n")49                out += "\n"50            }51        }52        return out53    }5455    static func markdown(_ m: ChatMessage) -> String {56        var out: String57        switch m.role {58        case .user: out = "### User\n\n"59        case .system: out = "### System\n\n"60        case .tool: out = "### Tool result\(m.toolResult.map { " — `\($0.name)`" } ?? "")\n\n"61        case .assistant:62            var who = m.modelName ?? m.modelID ?? "unknown model"63            if let b = m.backend { who += " · \(b.displayName)" }64            if let node = m.remoteNodeName { who += " · remote: \(node)" }65            out = "### Assistant — \(who)\n\n"66        }67        if let r = m.reasoning, !r.isEmpty { out += "<details>\n<summary>Reasoning</summary>\n\n\(r)\n\n</details>\n\n" }68        if !m.content.isEmpty { out += m.role == .tool ? "```\n\(m.content)\n```\n\n" : m.content + "\n\n" }69        for call in m.toolCalls { out += "**Tool call** `\(call.name)`\n\n```json\n\(call.arguments.serialized(pretty: true))\n```\n\n" }70        if !m.attachments.isEmpty {71            out += "**Attachments**\n\n" + m.attachments.map { "- \($0.fileName) (\($0.kind.rawValue), \(ByteFormat.string(Int64($0.byteCount))))" }.joined(separator: "\n") + "\n\n"72        }73        if !m.sources.isEmpty {74            out += "**Sources**\n\n" + m.sources.enumerated().map { i, s in75                "\(i + 1). \(s.documentName)\(s.page.map { ", p. \($0)" } ?? "") — “\(s.excerpt.replacingOccurrences(of: "\n", with: " ").prefix(200))”"76            }.joined(separator: "\n") + "\n\n"77        }78        if m.status == .cancelled { out += "*(stopped)*\n\n" }79        if m.status == .failed { out += "*(failed\(m.errorText.map { ": \($0)" } ?? ""))*\n\n" }80        return out81    }8283    public func exportPlainText(_ id: UUID, scope: ExportScope = .activeBranch) throws -> String {84        let (c, messages, tree) = try exportMaterial(id, scope: scope)85        func line(_ m: ChatMessage) -> String {86            let who = switch m.role {87            case .user: "User"; case .system: "System"; case .tool: "Tool"88            case .assistant: "Assistant (\(m.modelName ?? m.modelID ?? "unknown model"))"89            }90            return "\(who):\n\(m.content)\n"91        }92        var out = c.title + "\n" + String(repeating: "=", count: max(3, c.title.count)) + "\n\n"93        if !c.systemPrompt.isEmpty { out += "System prompt:\n\(c.systemPrompt)\n\n" }94        if scope == .activeBranch { return out + messages.map(line).joined(separator: "\n") }95        let leaves = messages.filter { tree.children[$0.id] == nil }.sorted { $0.createdAt < $1.createdAt }96        for (i, leaf) in leaves.enumerated() {97            if leaves.count > 1 { out += "--- Branch \(i + 1) of \(leaves.count) ---\n\n" }98            out += tree.path(to: leaf.id).map(line).joined(separator: "\n") + "\n"99        }100        return out101    }102103    /// Lossless archive (dates are exact; every field of the Core types is kept).104    public func exportJSON(_ id: UUID, scope: ExportScope = .wholeTree) throws -> Data {105        let (c, messages, _) = try exportMaterial(id, scope: scope)106        let archive = ConversationArchive(format: ConversationArchive.formatName, version: ConversationArchive.currentVersion, exportedAt: Date(),107                                          scope: scope == .wholeTree ? "wholeTree" : "activeBranch", conversation: c, messages: messages)108        let e = JSONEncoder(); e.outputFormatting = [.prettyPrinted, .sortedKeys]109        return try e.encode(archive)110    }111112    /// Imports an archive produced by `exportJSON`. Attachment files are not part of the archive.113    @discardableResult114    public func importJSON(_ data: Data, idPolicy: ImportIDPolicy = .newIDsOnConflict) throws -> Conversation {115        let archive: ConversationArchive116        do { archive = try JSONDecoder().decode(ConversationArchive.self, from: data) }117        catch { throw ConversationStoreError.invalidImport("not a Local Chat conversation archive.") }118        guard archive.format == ConversationArchive.formatName else { throw ConversationStoreError.invalidImport("unknown format “\(archive.format)”.") }119        guard archive.version <= ConversationArchive.currentVersion else { throw ConversationStoreError.invalidImport("it was written by a newer version of Local Chat.") }120        return try db.write { db in121            var c = archive.conversation; var messages = archive.messages122            let exists = try Bool.fetchOne(db, sql: "SELECT EXISTS(SELECT 1 FROM conversation WHERE id = ?)", arguments: [c.id.uuidString]) ?? false123            var remap = idPolicy == .alwaysNewIDs || exists124            if !remap {125                for m in messages where try Bool.fetchOne(db, sql: "SELECT EXISTS(SELECT 1 FROM message WHERE id = ?)", arguments: [m.id.uuidString]) ?? false { remap = true; break }126            }127            if remap {128                var map: [UUID: UUID] = [:]129                for m in messages { map[m.id] = UUID() }130                c.id = UUID()131                c.activeLeafID = c.activeLeafID.flatMap { map[$0] }132                if let s = c.summary { if let mapped = map[s.coversThroughMessageID] { c.summary?.coversThroughMessageID = mapped } else { c.summary = nil } }133                messages = messages.map { var m = $0; m.id = map[$0.id]!; m.conversationID = c.id; m.parentID = $0.parentID.flatMap { map[$0] }; return m }134            }135            let ids = Set(messages.map(\.id))136            let now = Date()137            c.deletedAt = nil; c.updatedAt = now138            if let f = c.folderID, try !Self.entityExists(db, kind: .folder, id: f.uuidString) { c.folderID = nil }139            if let leaf = c.activeLeafID, !ids.contains(leaf) { c.activeLeafID = nil }140            for var m in messages {141                if let p = m.parentID, !ids.contains(p) { m.parentID = nil }142                if m.status == .streaming { m.status = .cancelled }143                try Self.putMessage(db, m, updatedAt: now)144            }145            if c.activeLeafID == nil { c.activeLeafID = ConversationTree(messages).newestLeaf()?.id }146            try Self.putConversation(db, c)147            return c148        }149    }150}151
Files main
Menu

Theme