gent-link 0.1.0

Connect Codex and Claude Code chats across machines with the Gent CLI and desktop companion.
import AppKit

final class FlippedDocument: NSView {
    override var isFlipped: Bool { true }
}

// Keep all AppKit updates on the main thread. Gent writes a JSON object per line.
final class Desktop: NSObject, NSApplicationDelegate, NSWindowDelegate, NSTabViewDelegate {
    let tabs = NSTabView()
    let chats = DirectoryTab(page: "chats")
    let nodes = DirectoryTab(page: "nodes")
    let settings = SettingsTab()
    let dashboard = DashboardTab()
    let account = AccountSection()
    let permissions = ManagementTab(page: "permissions")
    lazy var about = ManagementTab(page: "about", icon: icon)
    let icon: NSImage?
    let trayIcon: NSImage?
    var prepareNotifications: (() -> Void)?
    var deliverNotification: (([String: Any]) -> Void)?
    let openAtLaunch: Bool
    var statusItem: NSStatusItem!
    let trayState = NSMenuItem(title: "Checking daemon…", action: nil, keyEquivalent: "")
    var lastRead = Date.distantPast
    var window: NSWindow!
    private var enrolledViewportHeight: NSLayoutConstraint?
    private let identity = NSStackView()
    let node = NSTextField(labelWithString: "Gent")
    let cluster = NSTextField(labelWithString: "")
    let state = NSTextField(labelWithString: "Connecting…")
    let health = NSView()
    let connection = NSTextField(wrappingLabelWithString: "")
    let errorMessage = NSTextField(wrappingLabelWithString: "")
    let refresh = NSButton(title: "Refresh", target: nil, action: nil)
    let toggle = NSButton(title: "Start daemon", target: nil, action: nil)
    let restart = NSButton(title: "Restart", target: nil, action: nil)
    var metrics: [String: NSTextField] = [:]
    var running = false
    var canStart = false
    var canStop = false
    var pending = false
    private var refreshAfterPending = false

    init(iconPath: String, openAtLaunch: Bool) {
        self.icon = NSImage(contentsOfFile: iconPath)
        self.trayIcon = NSImage(contentsOf: URL(fileURLWithPath: iconPath)
            .deletingLastPathComponent().appendingPathComponent("inkling-tray-128.png"))
        self.openAtLaunch = openAtLaunch
    }

    func applicationDidFinishLaunching(_ notification: Notification) {
        statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
        if let image = trayIcon {
            image.size = NSSize(width: 20, height: 20)
            statusItem.button?.image = image
        } else {
            statusItem.button?.title = "G"
        }
        statusItem.button?.toolTip = "Gent Desktop"
        let menu = NSMenu()
        menu.addItem(trayState)
        menu.addItem(.separator())
        menu.addItem(withTitle: "Open Gent Desktop", action: #selector(show), keyEquivalent: "").target = self
        menu.addItem(.separator())
        menu.addItem(withTitle: "Quit Gent Desktop", action: #selector(quit), keyEquivalent: "q").target = self
        menu.insertItem(withTitle: "Settings…", action: #selector(showSettings), keyEquivalent: ",", at: 3).target = self
        statusItem.menu = menu
        let main = NSMenu()
        let edit = NSMenu(title: "Edit")
        for (title, action, key) in [("Undo","undo:","z"), ("Cut","cut:","x"), ("Copy","copy:","c"),
                                     ("Paste","paste:","v"), ("Select All","selectAll:","a")] {
            edit.addItem(withTitle: title, action: Selector(action), keyEquivalent: key)
        }
        let application = NSMenu(title: "Gent Desktop")
        application.addItem(withTitle: "Settings…", action: #selector(showSettings), keyEquivalent: ",").target = self
        application.addItem(withTitle: "Quit Gent Desktop", action: #selector(quit), keyEquivalent: "q").target = self
        for submenu in [application, edit] {
            let item = NSMenuItem()
            main.addItem(item)
            item.submenu = submenu
        }
        NSApp.mainMenu = main
        makeWindow()
        if openAtLaunch { show() } else { request("refresh") }
        prepareNotifications?()

        Timer.scheduledTimer(withTimeInterval: 15, repeats: true) { [weak self] _ in
            self?.poll()
        }
        DispatchQueue.global(qos: .userInitiated).async {
            while let line = readLine() {
                guard let data = line.data(using: .utf8),
                      let object = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { continue }
                DispatchQueue.main.async { self.receive(object) }
            }
            DispatchQueue.main.async { self.quit() }
        }
    }

    func poll() {
        if window.isVisible && tabs.selectedTabViewItem?.label == "Settings" { settings.pollChannel() }
        if window.isVisible && tabs.selectedTabViewItem?.label == "Dashboard" { dashboard.load() }
        if window.isVisible || Date().timeIntervalSince(lastRead) >= 60 { request("refresh") }
    }

    @objc func show() {
        NSApp.activate(ignoringOtherApps: true)
        window.makeKeyAndOrderFront(nil)
        loadTab(tabs.selectedTabViewItem)
    }

    func loadTab(_ item: NSTabViewItem?) {
        switch item?.identifier as? String {
        case "Dashboard": dashboard.load()
        case "Status": refreshCluster()
        case "Permissions": permissions.load()
        case "About": about.load()
        case "Chats": chats.loadChats()
        case "Settings": settings.load()
        default: request("refresh")
        }
    }

    func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) {
        loadTab(tabViewItem)
    }

    @objc func showSettings() {
        tabs.selectTabViewItem(withIdentifier: "Settings")
        show()
    }

    @objc func quit() { NSApp.terminate(nil) }
    func windowShouldClose(_ sender: NSWindow) -> Bool {
        sender.orderOut(nil)
        return false
    }

    func makeWindow() {
        window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 850, height: 605),
                          styleMask: [.titled, .closable, .miniaturizable, .resizable],
                          backing: .buffered, defer: false)
        window.title = "Gent Desktop"
        window.minSize = NSSize(width: 510, height: 400)
        window.isReleasedWhenClosed = false
        window.delegate = self
        window.center()
        let content = NSScrollView()
        for (title, child) in [("Status", content), ("Dashboard", dashboard.view), ("Chats", chats.view), ("Settings", settings.view), ("Permissions", permissions.view), ("About", about.view)] {
            let tab = NSTabViewItem(identifier: title)
            tab.label = title
            tab.view = child
            tabs.addTabViewItem(tab)
        }
        tabs.delegate = self
        window.contentView = tabs
        let document = FlippedDocument()
        content.hasVerticalScroller = true
        content.drawsBackground = false
        content.documentView = document
        document.translatesAutoresizingMaskIntoConstraints = false
        let clip = content.contentView
        let viewportHeight = document.heightAnchor.constraint(equalTo: clip.heightAnchor)
        // Fit the viewport when enrolled, allowing the controls and table minimum to take precedence.
        viewportHeight.priority = NSLayoutConstraint.Priority(500)
        enrolledViewportHeight = viewportHeight
        NSLayoutConstraint.activate([
            document.leadingAnchor.constraint(equalTo: clip.leadingAnchor),
            document.topAnchor.constraint(equalTo: clip.topAnchor),
            document.widthAnchor.constraint(equalTo: clip.widthAnchor),
        ])

        cluster.font = .systemFont(ofSize: 21, weight: .semibold)
        node.textColor = .secondaryLabelColor
        state.font = .boldSystemFont(ofSize: NSFont.systemFontSize)
        identity.addArrangedSubview(cluster)
        identity.addArrangedSubview(node)
        identity.orientation = .vertical
        identity.alignment = .leading
        let controls = NSStackView(views: [state, NSView(), toggle, restart])
        for button in [refresh, toggle, restart] {
            button.target = self
            button.action = #selector(clicked(_:))
            button.bezelStyle = .rounded
        }
        let stats = NSStackView()
        stats.distribution = .fillEqually
        for (key, name) in [("sync", "Sync"), ("queue", "Queued"), ("chats", "Known chats")] {
            let caption = NSTextField(labelWithString: name)
            caption.textColor = .secondaryLabelColor
            let value = NSTextField(labelWithString: "—")
            value.font = .systemFont(ofSize: 16, weight: .medium)
            metrics[key] = value
            let metric = column([caption, value])
            metric.spacing = 2
            stats.addArrangedSubview(metric)
        }
        connection.textColor = .secondaryLabelColor
        pin(column([controls, stats, connection]), in: health)

        let header = NSStackView(views: [identity, NSView(), refresh])
        identity.spacing = 2
        identity.isHidden = true
        health.isHidden = true
        nodes.view.isHidden = true
        errorMessage.textColor = .systemRed
        errorMessage.isHidden = true
        let body = column([header, errorMessage, health, nodes.view, account.view])
        body.distribution = .fill
        body.spacing = 14
        pin(body, in: document, inset: 16)
        buttons()
    }

    @objc func clicked(_ sender: NSButton) {
        if sender === toggle { request(canStop ? "stop" : "start") }
        else if sender === restart { request("restart") }
        else { refreshCluster() }
    }

    private func refreshCluster() {
        request("refresh")
        account.load()
    }

    private func refreshAfterOperation() {
        // An in-flight status or daemon command may have sampled the directory before the edit.
        if pending { refreshAfterPending = true }
        else { request("refresh") }
    }

    func buttons() {
        toggle.isEnabled = !pending && (canStart || canStop)
        toggle.title = (running || canStop) ? "Stop daemon" : "Start daemon"
        restart.isEnabled = !pending && canStop
    }

    func request(_ action: String) {
        guard !pending else { return }
        pending = true
        buttons()
        FileHandle.standardOutput.write(Data((action + "\n").utf8))
    }

    func receive(_ m: [String: Any]) {
        if let banner = m["notification"] as? [String: Any] {
            deliverNotification?(banner)
            return
        }
        switch m["page"] as? String {
        case "cluster":
            account.receive(m)
            if m["operation"] as? Bool == true && m["error"] == nil { refreshAfterOperation() }
            return
        case "permissions": permissions.receive(m); return
        case "dashboard": dashboard.receive(m); return
        case "about": about.receive(m); return
        case "chats": chats.receive(m); return
        case "nodes":
            if m["operation"] as? Bool == true {
                nodes.actions.completed(m)
                if m["error"] == nil { refreshAfterOperation() }
            }
            return
        case "settings": settings.receive(m); return
        case "release_channel": settings.channel.receive(m); return
        default: break
        }
        if m["show"] as? Bool == true { show(); return }
        if m["quit"] as? Bool == true { quit(); return }
        pending = false
        lastRead = Date()
        errorMessage.stringValue = m["error"] as? String ?? ""
        errorMessage.isHidden = errorMessage.stringValue.isEmpty
        if errorMessage.isHidden {
            let enrolled = m["enrolled"] as? Bool == true
            identity.isHidden = !enrolled
            health.isHidden = !enrolled
            nodes.view.isHidden = !enrolled
            enrolledViewportHeight?.isActive = enrolled
            node.stringValue = "Local node: " + (m["node"] as? String ?? "—")
            cluster.stringValue = m["cluster"] as? String ?? "Cluster"
            let daemonState = m["state"] as? String ?? "Unavailable"
            state.stringValue = "Daemon " + daemonState.lowercased()
            trayState.title = state.stringValue
            for (key, field) in metrics {
                if let n = m[key] as? NSNumber { field.stringValue = n.stringValue }
                else { field.stringValue = (m[key] as? String)?.capitalized ?? "—" }
            }
            connection.stringValue = m["connection"] as? String ?? ""
            connection.isHidden = connection.stringValue.isEmpty
            nodes.display(m["nodes"] as? [String: Any] ?? [:])
            running = m["running"] as? Bool ?? false
            canStart = m["can_start"] as? Bool ?? false
            canStop = m["can_stop"] as? Bool ?? false
        }
        if refreshAfterPending {
            refreshAfterPending = false
            request("refresh")
        } else {
            buttons()
        }
    }
}