import Foundation import GRPCCore import GRPCNIOTransportHTTP2 import ArgumentParser import AppleIntelligenceCore @main struct AppleIntelligenceServer: AsyncParsableCommand { static let configuration = CommandConfiguration( abstract: "Apple Intelligence gRPC Server", discussion: "Exposes Apple Intelligence (Foundation Models) over gRPC for LAN access." ) @Option(name: .shortAndLong, help: "Host to bind to") var host: String? @Option(name: .shortAndLong, help: "Port to listen on") var port: Int? func run() async throws { let config = Config() let bindHost = host ?? config.host let bindPort = port ?? config.port print("Initializing Apple Intelligence service...") let service = await AppleIntelligenceService() let modelStatus = await service.getModelStatus() print("Model status: \(modelStatus)") guard await service.isAvailable else { print("Error: Apple Intelligence is not available on this device.") print("Please ensure:") print(" - You are running macOS 26 (Tahoe) or later") print(" - You have an Apple Silicon Mac") print(" - Apple Intelligence is enabled in System Settings") throw ExitCode.failure } // Initialize speech services print("Initializing Text-to-Speech service...") let ttsService = TextToSpeechService() print("Initializing Speech-to-Text service...") let sttService = await SpeechToTextService() let sttStatus = await sttService.getStatus() print("Speech-to-Text status: \(sttStatus)") let provider = AppleIntelligenceProvider( service: service, ttsService: ttsService, sttService: sttService, apiKey: config.apiKey ) let transport = HTTP2ServerTransport.Posix( address: .ipv4(host: bindHost, port: bindPort), transportSecurity: .plaintext, config: .defaults ) let server = GRPCServer(transport: transport, services: [provider]) print("Starting gRPC server on \(bindHost):\(bindPort)...") if config.apiKey != nil { print("API key authentication is enabled") } print("Server is ready to accept connections") print("") print("Available services:") print(" - Complete/StreamComplete: Text generation with Apple Intelligence") print(" - TextToSpeech: Convert text to spoken audio") print(" - ListVoices: List available TTS voices") print(" - Transcribe: Convert audio file to text") print(" - StreamTranscribe: Real-time speech-to-text") print("") print("Health check: grpcurl -plaintext \(bindHost):\(bindPort) appleintelligence.AppleIntelligenceService/Health") print("Press Ctrl+C to stop the server") try await server.serve() print("Server stopped.") } }