|
| 1 | +/** |
| 2 | + * Louder - OpenCode Plugin |
| 3 | + * Sound + Haptic feedback when AI tasks complete |
| 4 | + * |
| 5 | + * Haptic requires: macOS + Force Touch trackpad + native binary |
| 6 | + * Build native binary: cd native && ./build.sh |
| 7 | + */ |
| 8 | +import { spawn } from "child_process" |
| 9 | +import { existsSync } from "fs" |
| 10 | +import { dirname, join } from "path" |
| 11 | +import { fileURLToPath } from "url" |
| 12 | +import { homedir } from "os" |
| 13 | + |
| 14 | +const SOUND_MAP = { |
| 15 | + success: "/System/Library/Sounds/Glass.aiff", |
| 16 | + error: "/System/Library/Sounds/Basso.aiff", |
| 17 | + info: "/System/Library/Sounds/Blow.aiff", |
| 18 | + warning: "/System/Library/Sounds/Sosumi.aiff", |
| 19 | + progress: "/System/Library/Sounds/Tink.aiff", |
| 20 | + reminder: "/System/Library/Sounds/Ping.aiff", |
| 21 | +} |
| 22 | + |
| 23 | +function findHapticBinary() { |
| 24 | + const locations = [ |
| 25 | + join(homedir(), ".config/opencode/native/HapticEngine"), |
| 26 | + join(homedir(), ".local/share/louder/HapticEngine"), |
| 27 | + join(dirname(fileURLToPath(import.meta.url)), "native/HapticEngine"), |
| 28 | + ] |
| 29 | + |
| 30 | + for (const loc of locations) { |
| 31 | + if (existsSync(loc)) return loc |
| 32 | + } |
| 33 | + return null |
| 34 | +} |
| 35 | + |
| 36 | +let hapticEngine = null |
| 37 | +let hapticBinaryPath = null |
| 38 | + |
| 39 | +function getHapticEngine() { |
| 40 | + if (hapticEngine) return Promise.resolve(hapticEngine) |
| 41 | + |
| 42 | + return new Promise((resolve) => { |
| 43 | + if (!hapticBinaryPath) { |
| 44 | + hapticBinaryPath = findHapticBinary() |
| 45 | + } |
| 46 | + |
| 47 | + if (!hapticBinaryPath) { |
| 48 | + resolve(null) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + const proc = spawn(hapticBinaryPath, [], { |
| 53 | + stdio: ["pipe", "ignore", "ignore"], |
| 54 | + }) |
| 55 | + |
| 56 | + proc.once("spawn", () => { |
| 57 | + hapticEngine = { |
| 58 | + write: (cmd) => { |
| 59 | + try { |
| 60 | + proc.stdin.write(cmd + "\n") |
| 61 | + return true |
| 62 | + } catch { |
| 63 | + return false |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + resolve(hapticEngine) |
| 68 | + }) |
| 69 | + |
| 70 | + proc.once("error", () => resolve(null)) |
| 71 | + proc.once("exit", () => { |
| 72 | + hapticEngine = null |
| 73 | + }) |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +async function playSound(type = "success") { |
| 78 | + const soundPath = SOUND_MAP[type] || SOUND_MAP.success |
| 79 | + if (existsSync(soundPath)) { |
| 80 | + spawn("afplay", [soundPath], { stdio: "ignore" }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +async function playHaptic(intensity = 2.0) { |
| 85 | + const engine = await getHapticEngine() |
| 86 | + if (engine) { |
| 87 | + engine.write(`15,${intensity}`) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +async function notify(soundType = "success", hapticIntensity = 2.0) { |
| 92 | + await Promise.all([ |
| 93 | + playSound(soundType), |
| 94 | + playHaptic(hapticIntensity) |
| 95 | + ]) |
| 96 | +} |
| 97 | + |
| 98 | +const notifiedSessions = new Set() |
| 99 | +const MAX_SESSIONS = 100 |
| 100 | + |
| 101 | +function trackSession(sessionID) { |
| 102 | + notifiedSessions.add(sessionID) |
| 103 | + if (notifiedSessions.size > MAX_SESSIONS) { |
| 104 | + const oldest = notifiedSessions.values().next().value |
| 105 | + if (oldest) notifiedSessions.delete(oldest) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +export const louderPlugin = async (ctx) => { |
| 110 | + return { |
| 111 | + event: async ({ event }) => { |
| 112 | + const props = event.properties |
| 113 | + const sessionID = props?.info?.id || props?.sessionID |
| 114 | + |
| 115 | + if (event.type === "session.updated" || event.type === "message.updated") { |
| 116 | + if (sessionID) notifiedSessions.delete(sessionID) |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + if (event.type === "session.idle") { |
| 121 | + if (!sessionID || notifiedSessions.has(sessionID)) return |
| 122 | + trackSession(sessionID) |
| 123 | + await notify("success", 2.0) |
| 124 | + } |
| 125 | + |
| 126 | + if (event.type === "session.error") { |
| 127 | + if (!sessionID || notifiedSessions.has(sessionID)) return |
| 128 | + trackSession(sessionID) |
| 129 | + await notify("error", 1.5) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |