Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

3.7 KB
Raw Blame
import Foundation

/// `b64url(SHA-256(hostSignPub)[0..16])`: derived from the host's signing key, never assigned by the cloud.
public typealias ComputerID = String

/// A computer this device may talk to. Holds no secrets: only pinned public keys and addresses.
public struct Computer: Codable, Hashable, Sendable, Identifiable {
    public var id: ComputerID
    public var name: String
    /// Pinned host signing key: the QR's `sk`, or the account flow's key once its SAS matched.
    public var signKey: String
    /// Pinned mailbox key: only from the QR's `bk` or the E2E `hello`; nil = no mailbox yet.
    public var boxKey: String?
    /// Direct candidates (`http://ip:port`), tried before the relay.
    public var urls: [String]
    /// The cloud relay base URL; nil = the host has the cloud off.
    public var cloud: URL?
    /// laptop · macmini · … · linux, from `hello`; picks the icon.
    public var device: String?
    /// The badge color picked on this device (an `AvatarPalette` id).
    public var color: String?
    /// How this device reaches the computer, picked per computer; nil = `.automatic`.
    public var route: ConnectionRoute?
    /// Whether the computer sends push alerts to this device, picked per computer; nil = on.
    public var notifications: Bool?

    public init(id: ComputerID, name: String, signKey: String, boxKey: String? = nil, urls: [String] = [],
                cloud: URL? = nil, device: String? = nil, color: String? = nil) {
        self.id = id
        self.name = name
        self.signKey = signKey
        self.boxKey = boxKey
        self.urls = urls
        self.cloud = cloud
        self.device = device
        self.color = color
    }

    /// Keeps what this device picked (color, route, push) over a newer copy from pairing, the account or `hello`.
    public mutating func keepDevicePicks(from previous: Computer) {
        color = previous.color
        route = previous.route
        notifications = previous.notifications
    }

    /// `true` when `id` really is the computer ID of `signKey`.
    public var isConsistent: Bool {
        Data(base64URL: signKey).map { $0.count == 32 && RelayCrypto.computerId(signKey: $0) == id } ?? false
    }
}

/// The order routes are tried in. Every paired computer has both routes; this only says which goes first
/// and whether the other one is a fallback.
public enum ConnectionRoute: String, Codable, CaseIterable, Sendable {
    /// Wi-Fi or Tailscale when it answers within 1.5 s, Cloudflare when it doesn't.
    case automatic
    /// Cloudflare, falling back to Wi-Fi or Tailscale when Cloudflare can't be reached.
    case cloudflareFirst
    /// Wi-Fi and Tailscale only: nothing goes through Cloudflare, even when they fail.
    case directOnly

    public init(from decoder: any Decoder) throws {
        self = Self(rawValue: try decoder.singleValueContainer().decode(String.self)) ?? .automatic
    }
}

/// A bot on a computer in an account context: bot IDs are only unique per computer.
/// (Multiple computers are future work; with one computer this is just the bot's address.)
public struct BotReference: Codable, Hashable, Sendable {
    /// `SharedStore.Context.accountID` (raw Clerk user ID; nil = the local context).
    public var accountId: String?
    public var computerId: ComputerID
    public var botId: String

    public init(accountId: String?, computerId: ComputerID, botId: String) {
        self.accountId = accountId
        self.computerId = computerId
        self.botId = botId
    }
}

/// One bot as the widgets see it.
public struct BotSnapshot: Codable, Hashable, Sendable {
    public var computerId: ComputerID
    public var bot: Bot

    public init(computerId: ComputerID, bot: Bot) {
        self.computerId = computerId
        self.bot = bot
    }
}