swift-apple-intelligence-grpc/Sources/AppleIntelligenceGRPC/main.swift
Mathias Beaulieu-Duncan 47feeedf9d Add Apple Intelligence gRPC server
Implements a Swift gRPC server that exposes Apple's Foundation Models
(Apple Intelligence) over the network for LAN access.

Features:
- Complete: Unary RPC for prompt/response
- StreamComplete: Server streaming RPC for token-by-token responses
- Health: Check model availability
- Optional API key authentication via gRPC metadata
- Configurable host/port via CLI args or environment variables

Requires macOS 26 (Tahoe) with Apple Intelligence enabled.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-30 02:54:12 -05:00

62 lines
2.1 KiB
Swift

import Foundation
import GRPCCore
import GRPCNIOTransportHTTP2
import ArgumentParser
@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
}
let provider = AppleIntelligenceProvider(service: service, 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("Health check: grpcurl -plaintext \(bindHost):\(bindPort) appleintelligence.AppleIntelligence/Health")
print("Press Ctrl+C to stop the server")
try await server.serve()
print("Server stopped.")
}
}