Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Sources/Fluid/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2683,9 +2683,10 @@ struct ContentView: View {
if typingTarget.shouldRestoreOriginalFocus {
await self.restoreFocusToRecordingTarget()
}
self.asr.typeTextToActiveField(
self.rewriteModeService.rewrittenText,
preferredTargetPID: typingTarget.pid
self.rewriteModeService.acceptRewrite(
preferredTargetPID: typingTarget.pid,
hideApp: false,
recordAnalytics: false
)
AnalyticsService.shared.capture(
.outputDelivered,
Expand Down
4 changes: 4 additions & 0 deletions Sources/Fluid/Persistence/SettingsStore+CommandMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ extension SettingsStore {
}

var commandModeReadinessIssue: String? {
if self.commandModeRouteToCodex {
return nil
}

let sourceProviderID = self.commandModeLinkedToGlobal ? self.selectedProviderID : self.commandModeSelectedProviderID
if sourceProviderID == "apple-intelligence" || sourceProviderID == "apple-intelligence-disabled" {
return "Command Mode cannot use Apple Intelligence because terminal tools require a chat API. Choose a verified chat provider or turn Sync off."
Expand Down
27 changes: 26 additions & 1 deletion Sources/Fluid/Persistence/SettingsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class SettingsStore: ObservableObject {
static let transcriptionPreviewCharLimitRange: ClosedRange<Int> = 50...800
static let transcriptionPreviewCharLimitStep = 50
static let defaultTranscriptionPreviewCharLimit = 150
static let defaultVisualizerNoiseThreshold = 0.12
private let defaults = UserDefaults.standard
private let keychain = KeychainService.shared
private(set) var launchAtStartupEnabled = false
Expand Down Expand Up @@ -1618,7 +1619,7 @@ final class SettingsStore: ObservableObject {
var visualizerNoiseThreshold: Double {
get {
let value = self.defaults.double(forKey: Keys.visualizerNoiseThreshold)
return value == 0.0 ? 0.4 : value // Default to 0.4 if not set
return value == 0.0 ? Self.defaultVisualizerNoiseThreshold : value
}
set {
// Clamp between 0.0 and 0.95 to avoid division by zero issues in visualizers
Expand Down Expand Up @@ -2411,6 +2412,28 @@ final class SettingsStore: ObservableObject {
}
}

var commandModeRouteToCodex: Bool {
get {
let value = self.defaults.object(forKey: Keys.commandModeRouteToCodex)
return value as? Bool ?? false
}
set {
objectWillChange.send()
self.defaults.set(newValue, forKey: Keys.commandModeRouteToCodex)
}
}

var commandModeCodexHandoffStyle: String {
get {
let value = self.defaults.string(forKey: Keys.commandModeCodexHandoffStyle) ?? "notch"
return ["notch", "app"].contains(value) ? value : "notch"
}
set {
objectWillChange.send()
self.defaults.set(newValue, forKey: Keys.commandModeCodexHandoffStyle)
}
}

// MARK: - Rewrite Mode Settings

var rewriteModeHotkeyShortcut: HotkeyShortcut {
Expand Down Expand Up @@ -4369,6 +4392,8 @@ private extension SettingsStore {
static let cancelRecordingHotkeyShortcut = "CancelRecordingHotkeyShortcut"
static let commandModeLinkedToGlobal = "CommandModeLinkedToGlobal"
static let commandModeShortcutEnabled = "CommandModeShortcutEnabled"
static let commandModeRouteToCodex = "CommandModeRouteToCodex"
static let commandModeCodexHandoffStyle = "CommandModeCodexHandoffStyle"

// Prompt Mode Keys (Transcribe with Prompt)
static let promptModeHotkeyShortcut = "PromptModeHotkeyShortcut"
Expand Down
264 changes: 264 additions & 0 deletions Sources/Fluid/Services/CodexHandoffService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
import AppKit
import Foundation

@MainActor
final class CodexHandoffService {
static let shared = CodexHandoffService()

private static let codexBundleID = "com.openai.codex"
private static let pasteboardSessionSemaphore = DispatchSemaphore(value: 1)
private static let bundledCodexCLIPath = "/Applications/Codex.app/Contents/Resources/codex"

private init() {}

struct HandoffResult {
let success: Bool
let message: String
}

enum HandoffStyle: Equatable {
case notch
case app

init(rawValue: String) {
self = rawValue == "app" ? .app : .notch
}
}

private struct PasteboardItemSnapshot {
let dataByType: [NSPasteboard.PasteboardType: Data]
}

private struct PasteboardSnapshot {
let items: [PasteboardItemSnapshot]
}

func sendToCodex(_ text: String, style: HandoffStyle) async -> HandoffResult {
let prompt = text.trimmingCharacters(in: .whitespacesAndNewlines)
guard !prompt.isEmpty else {
return HandoffResult(success: false, message: "No command text to send to Codex.")
}

switch style {
case .notch:
return await self.runCodexInNotch(prompt)
case .app:
return await self.sendToCodexApp(prompt)
}
}

private func sendToCodexApp(_ prompt: String) async -> HandoffResult {
guard await self.activateCodex() else {
return HandoffResult(success: false, message: "Could not open Codex.")
}

try? await Task.sleep(nanoseconds: 250_000_000)

guard await self.pasteAndSubmit(prompt) else {
return HandoffResult(success: false, message: "Could not paste into Codex.")
}

return HandoffResult(success: true, message: "Sent to Codex.")
}

private func runCodexInNotch(_ prompt: String) async -> HandoffResult {
guard FileManager.default.isExecutableFile(atPath: Self.bundledCodexCLIPath) else {
return HandoffResult(success: false, message: "Codex CLI was not found.")
}

let outputURL = FileManager.default.temporaryDirectory
.appendingPathComponent("fluidvoice-codex-\(UUID().uuidString).txt")
defer { try? FileManager.default.removeItem(at: outputURL) }

let process = Process()
process.executableURL = URL(fileURLWithPath: Self.bundledCodexCLIPath)
process.currentDirectoryURL = URL(fileURLWithPath: NSHomeDirectory())
process.arguments = [
"-a", "never",
"exec",
"--skip-git-repo-check",
"--color", "never",
"-C", NSHomeDirectory(),
"-o", outputURL.path,
"-"
]

let inputPipe = Pipe()
process.standardInput = inputPipe
process.standardOutput = FileHandle.nullDevice
process.standardError = FileHandle.nullDevice

do {
try process.run()
} catch {
DebugLogger.shared.error("Failed to start Codex CLI: \(error.localizedDescription)", source: "CodexHandoffService")
return HandoffResult(success: false, message: "Could not start Codex.")
}

if let data = prompt.data(using: .utf8) {
inputPipe.fileHandleForWriting.write(data)
}
inputPipe.fileHandleForWriting.closeFile()

let completed = await self.waitForProcess(process, timeout: 120)
guard completed else {
process.terminate()
return HandoffResult(success: false, message: "Codex took too long and was stopped.")
}

guard process.terminationStatus == 0 else {
return HandoffResult(success: false, message: "Codex finished with an error.")
}

let output = (try? String(contentsOf: outputURL, encoding: .utf8))?
.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
guard !output.isEmpty else {
return HandoffResult(success: true, message: "Codex finished.")
}

return HandoffResult(success: true, message: output)
}

private func waitForProcess(_ process: Process, timeout: TimeInterval) async -> Bool {
await withCheckedContinuation { continuation in
let lock = NSLock()
var didResume = false

func resume(_ value: Bool) {
lock.lock()
defer { lock.unlock() }
guard !didResume else { return }
didResume = true
continuation.resume(returning: value)
}

process.terminationHandler = { _ in
resume(true)
}

DispatchQueue.global().asyncAfter(deadline: .now() + timeout) {
resume(false)
}
}
}

private func activateCodex() async -> Bool {
if let app = NSWorkspace.shared.runningApplications.first(where: { $0.bundleIdentifier == Self.codexBundleID }) {
return app.activate(options: [.activateAllWindows, .activateIgnoringOtherApps])
}

guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: Self.codexBundleID) else {
return false
}

return await withCheckedContinuation { continuation in
let configuration = NSWorkspace.OpenConfiguration()
configuration.activates = true
NSWorkspace.shared.openApplication(at: url, configuration: configuration) { app, error in
if let error {
DebugLogger.shared.error("Failed to launch Codex: \(error.localizedDescription)", source: "CodexHandoffService")
continuation.resume(returning: false)
return
}
_ = app?.activate(options: [.activateAllWindows, .activateIgnoringOtherApps])
continuation.resume(returning: app != nil)
}
}
}

private func pasteAndSubmit(_ text: String) async -> Bool {
Self.pasteboardSessionSemaphore.wait()

let pasteboard = NSPasteboard.general
let snapshot = self.capturePasteboardSnapshot(pasteboard)

pasteboard.clearContents()
guard pasteboard.setString(text, forType: .string) else {
self.restorePasteboardSnapshot(snapshot, to: pasteboard)
Self.pasteboardSessionSemaphore.signal()
return false
}

guard self.sendCommandKey("v") else {
self.restorePasteboardSnapshot(snapshot, to: pasteboard)
Self.pasteboardSessionSemaphore.signal()
return false
}

try? await Task.sleep(nanoseconds: 100_000_000)
guard self.sendReturnKey() else {
self.restorePasteboardSnapshot(snapshot, to: pasteboard)
Self.pasteboardSessionSemaphore.signal()
return false
}

try? await Task.sleep(nanoseconds: 1_000_000_000)
self.restorePasteboardSnapshot(snapshot, to: pasteboard)
Self.pasteboardSessionSemaphore.signal()
Comment on lines +175 to +197

return true
}

private func sendCommandKey(_ character: Character) -> Bool {
guard let keyCode = Self.keyCode(for: character),
let keyDown = CGEvent(keyboardEventSource: nil, virtualKey: keyCode, keyDown: true),
let keyUp = CGEvent(keyboardEventSource: nil, virtualKey: keyCode, keyDown: false)
else {
return false
}

keyDown.flags = .maskCommand
keyUp.flags = .maskCommand
keyDown.post(tap: .cghidEventTap)
usleep(10_000)
keyUp.post(tap: .cghidEventTap)
return true
}

private func sendReturnKey() -> Bool {
guard let keyDown = CGEvent(keyboardEventSource: nil, virtualKey: 36, keyDown: true),
let keyUp = CGEvent(keyboardEventSource: nil, virtualKey: 36, keyDown: false)
else {
return false
}

keyDown.post(tap: .cghidEventTap)
usleep(10_000)
keyUp.post(tap: .cghidEventTap)
return true
}

private static func keyCode(for character: Character) -> CGKeyCode? {
switch character.lowercased() {
case "v": return 9
default: return nil
}
}

private func capturePasteboardSnapshot(_ pasteboard: NSPasteboard) -> PasteboardSnapshot {
let items: [PasteboardItemSnapshot] = pasteboard.pasteboardItems?.map { item in
var dataByType: [NSPasteboard.PasteboardType: Data] = [:]
for type in item.types {
if let data = item.data(forType: type) {
dataByType[type] = data
}
}
return PasteboardItemSnapshot(dataByType: dataByType)
} ?? []
return PasteboardSnapshot(items: items)
}

private func restorePasteboardSnapshot(_ snapshot: PasteboardSnapshot, to pasteboard: NSPasteboard) {
pasteboard.clearContents()
guard !snapshot.items.isEmpty else { return }

let restoredItems = snapshot.items.map { snap -> NSPasteboardItem in
let item = NSPasteboardItem()
for (type, data) in snap.dataByType {
item.setData(data, forType: type)
}
return item
}
_ = pasteboard.writeObjects(restoredItems)
}
}
Loading