Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

11.3 KB
Raw Blame
// Native macOS speech-to-text helper. Streams NDJSON lines to stdout:
//   {"partial":true,"text":"…"}   while recognizing
//   {"partial":false,"text":"…"}  final result, then exit 0
//   {"error":"…"}                 then exit 1
// Runs until the final result or a per-session stop marker. Launched by
// electron/speech.mjs as this background app bundle so macOS can resolve the
// microphone and speech purpose strings in its Info.plist.
//
// `--endpoint-ms N` ends the audio stream after N milliseconds without a
// transcript change. SFSpeechRecognizer does not finalize a buffer-backed
// request on silence by itself; it only produces `isFinal` after endAudio().
// Composer dictation omits this flag and keeps its existing press-to-stop
// behavior, while call mode opts into silence endpointing.
//
// `--finish-on-release control,option` finishes recognition once any of the
// listed modifiers (control, option, shift, command) is no longer held.
import AVFoundation
import CoreGraphics
import Darwin
import Foundation
import Speech

func emit(_ obj: [String: Any]) {
  if let data = try? JSONSerialization.data(withJSONObject: obj),
    let line = String(data: data, encoding: .utf8)
  {
    print(line)
    fflush(stdout)
  }
}

func fail(_ message: String) -> Never {
  emit(["error": message])
  exit(1)
}

let endpointMs: Int = {
  let args = CommandLine.arguments
  guard
    let index = args.firstIndex(of: "--endpoint-ms"),
    index + 1 < args.count,
    let value = Int(args[index + 1])
  else { return 0 }
  return min(5_000, max(250, value))
}()

let stopFile: String? = {
  let args = CommandLine.arguments
  guard let index = args.firstIndex(of: "--stop-file"), index + 1 < args.count else { return nil }
  return args[index + 1]
}()

let finishFile: String? = {
  let args = CommandLine.arguments
  guard let index = args.firstIndex(of: "--finish-file"), index + 1 < args.count else { return nil }
  return args[index + 1]
}()

func releaseModifiers(_ list: String) -> CGEventFlags? {
  var flags = CGEventFlags()
  for name in list.split(separator: ",", omittingEmptySubsequences: false) {
    switch name {
    case "control": flags.insert(.maskControl)
    case "option": flags.insert(.maskAlternate)
    case "shift": flags.insert(.maskShift)
    case "command": flags.insert(.maskCommand)
    default: return nil
    }
  }
  return flags.isEmpty ? nil : flags
}

func released(_ required: CGEventFlags, _ current: CGEventFlags) -> Bool {
  !current.contains(required)
}

if let index = CommandLine.arguments.firstIndex(of: "--release-check") {
  let args = CommandLine.arguments
  guard index + 2 < args.count, let required = releaseModifiers(args[index + 1]),
    let raw = UInt64(args[index + 2])
  else { fail("bad-modifiers") }
  emit(["released": released(required, CGEventFlags(rawValue: raw))])
  exit(0)
}

let finishOnRelease: CGEventFlags? = {
  let args = CommandLine.arguments
  guard let index = args.firstIndex(of: "--finish-on-release") else { return nil }
  guard index + 1 < args.count, let flags = releaseModifiers(args[index + 1]) else {
    fail("bad-modifiers")
  }
  return flags
}()

let lifelineFile: String = {
  let args = CommandLine.arguments
  guard let index = args.firstIndex(of: "--lifeline-file"), index + 1 < args.count else {
    fail("missing-lifeline")
  }
  return args[index + 1]
}()
let lifelineFD = Darwin.open(lifelineFile, O_RDONLY | O_NONBLOCK)
guard lifelineFD >= 0 else { fail("lifeline-unavailable") }

func checkOwner() {
  var byte: UInt8 = 0
  let count = Darwin.read(lifelineFD, &byte, 1)
  guard count == 0 || (count < 0 && errno != EAGAIN && errno != EINTR) else { return }
  let sessionDir = (lifelineFile as NSString).deletingLastPathComponent
  // Owner death leaves private transcripts behind. Remove only this session's known files, never a directory tree.
  for name in ["stdout.ndjson", "stderr.log", "stop", "finish", "owner.pipe"] {
    _ = unlink((sessionDir as NSString).appendingPathComponent(name))
  }
  _ = rmdir(sessionDir)
  exit(0)
}

// LaunchServices reparents the helper. FIFO EOF binds capture to Electron's lifetime without trusting a reusable PID.
checkOwner()
let ownerSource = DispatchSource.makeReadSource(fileDescriptor: lifelineFD, queue: .global(qos: .userInitiated))
ownerSource.setEventHandler { checkOwner() }
ownerSource.resume()

// LaunchServices gives the helper the bundle identity TCC needs, but it also
// means the parent cannot terminate it by killing the `open -W` process. A
// per-session stop marker keeps intentional mute/hang-up deterministic.
var stopTimer: DispatchSourceTimer?
if let stopFile {
  let timer = DispatchSource.makeTimerSource(queue: .global(qos: .userInitiated))
  timer.schedule(deadline: .now() + .milliseconds(100), repeating: .milliseconds(100))
  timer.setEventHandler {
    if FileManager.default.fileExists(atPath: stopFile) { exit(0) }
  }
  stopTimer = timer
  timer.resume()
}

// Push-to-talk release must finalize recognition rather than cancel it. The
// handler is installed once the audio engine exists; the timer keeps polling
// if an unusually fast key release beats authorization/setup.
var finishHandler: (() -> Void)?
var finishTimer: DispatchSourceTimer?
if finishFile != nil || finishOnRelease != nil {
  let timer = DispatchSource.makeTimerSource(queue: .global(qos: .userInitiated))
  timer.schedule(deadline: .now() + .milliseconds(50), repeating: .milliseconds(50))
  timer.setEventHandler {
    let finishRequested =
      finishFile.map { FileManager.default.fileExists(atPath: $0) } == true
      || finishOnRelease.map { released($0, CGEventSource.flagsState(.combinedSessionState)) } == true
    guard finishRequested, let finish = finishHandler else { return }
    timer.cancel()
    finish()
  }
  finishTimer = timer
  timer.resume()
}

/// SFSpeechRecognizer can keep revising/re-emitting a partial transcript
/// after the user stops talking. Only a changed transcript resets the timer.
final class SilenceEndpointer {
  private let queue = DispatchQueue(label: "com.openmausbot.speech.endpoint")
  private let gap: TimeInterval
  private let finish: () -> Void
  private var timer: DispatchSourceTimer?
  private var lastText = ""
  private var lastChange = DispatchTime.now()
  private var finished = false

  init(gapMs: Int, finish: @escaping () -> Void) {
    gap = Double(gapMs) / 1_000
    self.finish = finish
  }

  func start() {
    let source = DispatchSource.makeTimerSource(queue: queue)
    source.schedule(deadline: .now() + .milliseconds(100), repeating: .milliseconds(100))
    source.setEventHandler { [weak self] in self?.tick() }
    timer = source
    source.resume()
  }

  func saw(_ text: String) {
    queue.async {
      guard !self.finished, !text.isEmpty, text != self.lastText else { return }
      self.lastText = text
      self.lastChange = .now()
    }
  }

  private func tick() {
    // Never terminate an empty turn: a call may be quiet for as long as the
    // user needs before they begin speaking.
    guard !finished, !lastText.isEmpty else { return }
    let silentFor = Double(DispatchTime.now().uptimeNanoseconds - lastChange.uptimeNanoseconds) / 1_000_000_000
    guard silentFor >= gap else { return }
    finished = true
    timer?.cancel()
    timer = nil
    finish()
  }
}

SFSpeechRecognizer.requestAuthorization { status in
  guard status == .authorized else { fail("speech-not-authorized") }
  // Recognize in the user's language: a hardcoded en-US recognizer
  // transcribes everyone else into nonsense. First preference that has an
  // available recognizer wins, with en-US as the last resort.
  let candidates =
    Locale.preferredLanguages.map { Locale(identifier: $0) }
    + [Locale.current, Locale(identifier: "en-US")]
  guard
    let recognizer = candidates.lazy.compactMap({ SFSpeechRecognizer(locale: $0) })
      .first(where: { $0.isAvailable })
  else { fail("recognizer-unavailable") }

  let request = SFSpeechAudioBufferRecognitionRequest()
  request.shouldReportPartialResults = true
  if recognizer.supportsOnDeviceRecognition {
    request.requiresOnDeviceRecognition = true
  }

  let engine = AVAudioEngine()
  let node = engine.inputNode
  var audioFinished = false
  let finishAudio = {
    DispatchQueue.main.async {
      guard !audioFinished else { return }
      audioFinished = true
      engine.stop()
      node.removeTap(onBus: 0)
      request.endAudio()
    }
  }
  finishHandler = finishAudio
  var endpointer: SilenceEndpointer?
  if endpointMs > 0 {
    endpointer = SilenceEndpointer(gapMs: endpointMs) {
      // Stop capture before ending the request: appending another audio
      // buffer after endAudio() can make the recognition task fail instead
      // of delivering its final transcript.
      finishAudio()
    }
    endpointer?.start()
  }
  // Audio interfaces can present many input channels (a UA Apollo shows 16) with the mic on only one
  // of them. SFSpeechRecognizer fails with a generic error on such buffers, so above two channels the
  // helper feeds it mono: the channel carrying the most signal, tracked with a slow-decaying peak so
  // it doesn't hop between channels mid-sentence. Mono and stereo devices take the original path.
  let inputFormat = node.outputFormat(forBus: 0)
  let channelCount = Int(inputFormat.channelCount)
  let monoFormat = AVAudioFormat(
    commonFormat: .pcmFormatFloat32, sampleRate: inputFormat.sampleRate, channels: 1, interleaved: false)
  var channelPeaks = [Float](repeating: 0, count: channelCount)
  node.installTap(onBus: 0, bufferSize: 1024, format: inputFormat) { buffer, _ in
    guard channelCount > 2, let monoFormat, let source = buffer.floatChannelData,
      let mono = AVAudioPCMBuffer(pcmFormat: monoFormat, frameCapacity: buffer.frameLength),
      let target = mono.floatChannelData
    else {
      request.append(buffer)
      return
    }
    let frames = Int(buffer.frameLength)
    var loudest = 0
    for channel in 0..<channelCount {
      var peak: Float = 0
      for frame in 0..<frames { peak = max(peak, abs(source[channel][frame])) }
      channelPeaks[channel] = max(channelPeaks[channel] * 0.999, peak)
      if channelPeaks[channel] > channelPeaks[loudest] { loudest = channel }
    }
    mono.frameLength = buffer.frameLength
    target[0].update(from: source[loudest], count: frames)
    request.append(mono)
  }
  do {
    engine.prepare()
    try engine.start()
  } catch { fail("mic-failed") }

  recognizer.recognitionTask(with: request) { result, error in
    if let result = result {
      let text = result.bestTranscription.formattedString
      endpointer?.saw(text)
      var line: [String: Any] = ["partial": !result.isFinal, "text": text]
      // Apple's on-device recognizer can end an utterance without isFinal,
      // then return only the next utterance: developer.apple.com/forums/thread/762952
      if let metadata = result.speechRecognitionMetadata, metadata.speechDuration > 0 {
        line["utteranceFinal"] = true
        line["speechStart"] = metadata.speechStartTimestamp
        line["speechDuration"] = metadata.speechDuration
      }
      emit(line)
      if result.isFinal { exit(0) }
    }
    if let error {
      let nsError = error as NSError
      // macOS can authorize this helper while system Dictation is disabled.
      // Preserve that distinction so the UI points to the right setting.
      if nsError.domain == "kLSRErrorDomain" && nsError.code == 201 {
        fail("dictation-disabled")
      }
      fail("recognition-error")
    }
  }
}

RunLoop.main.run()