Something went wrong. Try again.
A small AI buddy that lives in the bottom right-hand corner of your Mac that wakes when you press a keybind.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117import AppKitimport SwiftUI
/// A step the agent wants to take that the user should OK first.struct ApprovalRequest: Sendable { /// What would happen, e.g. `Click button "Send" in Mail`. let action: String}
/// Asks the user whether the agent may go ahead. Returns false when declined or cancelled.typealias ApprovalHandler = @Sendable (ApprovalRequest) async -> Bool
/// Which actions need the user's OK before the agent takes them.enum ActionRisk { /// Never taken on the strength of the goal alone: money, deletion, account changes. private static let always = [ "delete", "erase", "remove", "empty trash", "trash", "pay", "buy", "purchase", "place order", "checkout", "transfer", "uninstall", "reset", "format", "sign out", "log out", "quit", ] /// Fine when the goal explicitly asks for it, otherwise asked about. private static let unlessAsked = ["send", "post", "publish", "submit", "share", "confirm", "reply", "forward"]
static func requiresApproval(label: String) -> Bool { matches(label, always) } static func requiresApprovalUnlessAsked(label: String) -> Bool { matches(label, unlessAsked) }
private static func matches(_ label: String, _ words: [String]) -> Bool { let text = " " + label.lowercased().split { !$0.isLetter }.joined(separator: " ") + " " return words.contains { text.contains(" \($0) ") || text.contains(" \($0)s ") } }}
/// A small card above the droplet with Allow and Deny. It never takes keyboard focus, so/// the app being driven keeps it; Esc anywhere still stops the whole task.@MainActorfinal class ApprovalPrompt { private var window: NSPanel? private var continuation: CheckedContinuation<Bool, Never>?
func ask(_ request: ApprovalRequest, near screen: NSScreen?) async -> Bool { resolve(false) return await withTaskCancellationHandler { await withCheckedContinuation { continuation in self.continuation = continuation show(request, on: screen ?? NSScreen.main) } } onCancel: { Task { @MainActor in self.resolve(false) } } }
/// Closes the card and answers whoever is waiting. Safe to call when nothing is showing. func resolve(_ allowed: Bool) { window?.orderOut(nil) window = nil let pending = continuation continuation = nil pending?.resume(returning: allowed) }
private func show(_ request: ApprovalRequest, on screen: NSScreen?) { let size = NSSize(width: 420, height: 150) let panel = NSPanel(contentRect: NSRect(origin: .zero, size: size), styleMask: [.nonactivatingPanel, .borderless], backing: .buffered, defer: true) panel.isOpaque = false panel.backgroundColor = .clear panel.hasShadow = true panel.level = .statusBar panel.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary, .ignoresCycle] panel.animationBehavior = .none panel.isReleasedWhenClosed = false panel.contentView = NSHostingView(rootView: ApprovalCard(action: request.action) { [weak self] allowed in self?.resolve(allowed) }) if let screen { // Just above the buddy waiting in the bottom-right corner. panel.setFrameOrigin(NSPoint( x: screen.visibleFrame.maxX - size.width - 20, y: screen.visibleFrame.minY + FloatingPanel.barHeight + 28 )) } panel.orderFrontRegardless() window = panel }}
private struct ApprovalCard: View { let action: String let answer: (Bool) -> Void
var body: some View { VStack(alignment: .leading, spacing: 10) { Label("Seer needs your OK", systemImage: "hand.raised.fill") .font(.system(size: 13, weight: .semibold)) .foregroundStyle(.white.opacity(0.6)) Text(action) .font(.system(size: 16)) .foregroundStyle(.white) .lineLimit(3) .frame(maxWidth: .infinity, alignment: .leading) Spacer(minLength: 0) HStack(spacing: 10) { Spacer() Button("Deny") { answer(false) } .buttonStyle(.bordered) Button("Allow") { answer(true) } .buttonStyle(.borderedProminent) } .controlSize(.large) } .padding(18) .frame(width: 420, height: 150) .background(.black.opacity(0.88), in: RoundedRectangle(cornerRadius: 26)) .overlay(RoundedRectangle(cornerRadius: 26).strokeBorder(.white.opacity(0.14), lineWidth: 1)) .environment(\.colorScheme, .dark) }}