import Foundation @main struct VoiceAgentLauncher { static func main() { let fileManager = FileManager.default let homeDir = fileManager.homeDirectoryForCurrentUser.path // Load configuration from .env files if present var envConfig = [String: String]() let envPaths = [ "\(homeDir)/.voiceagent.env", "\(homeDir)/.config/voiceagent/env", "\(homeDir)/.env" ] for envPath in envPaths { if let content = try? String(contentsOfFile: envPath, encoding: .utf8) { for line in content.components(separatedBy: .newlines) { let trimmed = line.trimmingCharacters(in: .whitespaces) if trimmed.isEmpty || trimmed.hasPrefix("#") { continue } let parts = trimmed.split(separator: "=", maxSplits: 1).map { String($0).trimmingCharacters(in: .whitespacesAndNewlines) } if parts.count == 2 { let key = parts[0] var val = parts[1] if (val.hasPrefix("\"") && val.hasSuffix("\"")) || (val.hasPrefix("'") && val.hasSuffix("'")) { val = String(val.dropFirst().dropLast()) } envConfig[key] = val } } } } let defaultProjDir = "/Users/adolforeyna/Projects/VoiceAgent1" let configuredProjDir = ProcessInfo.processInfo.environment["VOICEAGENT_DIR"] ?? envConfig["VOICEAGENT_DIR"] ?? envConfig["WORKSPACE_DIR"] ?? defaultProjDir let bundleResPath = Bundle.main.resourcePath ?? "" let bundledSrcPath = "\(bundleResPath)/src" let workDir = fileManager.fileExists(atPath: configuredProjDir) ? configuredProjDir : bundledSrcPath let defaultPythonBin = "\(workDir)/.venv/bin/python" let configuredPython = ProcessInfo.processInfo.environment["VOICEAGENT_PYTHON"] ?? envConfig["VOICEAGENT_PYTHON"] ?? envConfig["PYTHON_PATH"] ?? defaultPythonBin let fallbackPython = "/usr/bin/python3" let targetPython = fileManager.fileExists(atPath: configuredPython) ? configuredPython : fallbackPython let targetScript = "\(workDir)/app_main.py" setenv("SSL_CERT_FILE", "/etc/ssl/cert.pem", 1) setenv("REQUESTS_CA_BUNDLE", "/etc/ssl/cert.pem", 1) let logDir = fileManager.homeDirectoryForCurrentUser.appendingPathComponent("Library/Logs/VoiceAgent") try? fileManager.createDirectory(at: logDir, withIntermediateDirectories: true) let logFile = logDir.appendingPathComponent("voiceagent.log") if !fileManager.fileExists(atPath: logFile.path) { fileManager.createFile(atPath: logFile.path, contents: nil) } let process = Process() process.executableURL = URL(fileURLWithPath: targetPython) process.arguments = [targetScript] process.currentDirectoryURL = URL(fileURLWithPath: workDir) if let logHandle = try? FileHandle(forWritingTo: logFile) { logHandle.seekToEndOfFile() process.standardOutput = logHandle process.standardError = logHandle } do { try process.run() process.waitUntilExit() } catch { print("Failed to run VoiceAgent: \(error)") } } }