Files
luxe-macos-tray-notifier/Sources/NotifierCore/NotifyClient.swift
cottongin ced6a44ed0 Add luxe Notifier: macOS menu bar app that tells you when luxe needs you
A luxe hook runs luxe-notify, which sends one JSON line over a Unix socket to
a SwiftUI menu bar app. The app shows a banner, keeps a badge count and
history, and brings the terminal forward when you click a banner.

Notifies on approvals (PermissionRequested), question prompts
(BeforeToolUse, matched to the question tool), errors, and turns that run at
least --min-seconds (default 30, timed from BeforeAgentStart to AgentEnd).

The menu warns when macOS has notifications blocked for the app. The app
writes status.txt beside its socket for debugging.

Known limits are in README.md and PLAN.md: a crowded menu bar can hide the
icon, and the first test build after a change can fail with a TestingMacros
error under Command Line Tools, which make test retries once.
2026-10-24 22:41:42 -04:00

32 lines
1.3 KiB
Swift

import Foundation
public enum NotifyClient {
/// Sends one event line. Throws a descriptive error when the app is not listening, so the CLI can
/// report it on stderr while still exiting 0.
public static func send(_ event: NotifyEvent, to path: String) throws {
let line = try NotifySocket.encodeLine(event)
let descriptor = socket(AF_UNIX, SOCK_STREAM, 0)
guard descriptor >= 0 else { throw SocketError.syscall("socket", errno) }
defer { close(descriptor) }
var address = try NotifySocket.makeAddress(path)
let connected = withUnsafePointer(to: &address) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
connect(descriptor, $0, socklen_t(MemoryLayout<sockaddr_un>.size))
}
}
guard connected == 0 else { throw SocketError.syscall("connect to \(path)", errno) }
try writeAll(line, to: descriptor)
}
private static func writeAll(_ data: Data, to descriptor: Int32) throws {
var sent = 0
try data.withUnsafeBytes { buffer in
while sent < data.count {
let count = write(descriptor, buffer.baseAddress!.advanced(by: sent), data.count - sent)
guard count > 0 else { throw SocketError.syscall("write", errno) }
sent += count
}
}
}
}