Swift client for Cloudflare Agents with Vercel AI SDK-compatible chat messages and streaming over WebSockets.
- Swift 5.9+
- Platforms: iOS 17+, macOS 14+, tvOS 17+, watchOS 10+, visionOS 1+
Add via Swift Package Manager:
// In Package.swift dependencies
.package(url: "https://github.com/victorhenrion/Agents.swift.git", from: "0.1.0")Then add the product to your target dependencies:
.product(name: "Agents", package: "Agents.swift")import Foundation
import Agents
struct AppState: Codable {
let userId: String
}
@MainActor
func helloWorld() async {
let baseURL = URL(string: "https://your-domain.example.com")!
// Optional headers (e.g. Authorization)
let options = AgentClientOptions<AppState>(
onClientStateUpdate: { state in
print("Client state set:", state)
},
onServerStateUpdate: { state in
print("Server state:", state)
},
onMcpUpdate: { mcp in
print("MCP servers:", mcp)
},
headers: [
"Authorization": "Bearer <token>"
]
)
let client = await AgentClient<AppState>(
baseURL: baseURL,
agentNamespace: "MyAgent", // will be converted to "my-agent" in the URL path
instanceName: "default",
options: options
)
// Set initial state (optional)
client.setState(AppState(userId: "123"))
// Send a user message
do {
let parts: [ChatMessage.Part] = [.text(.init(text: content))]
let response = try await client.sendMessage(.init(parts))
print("Response:", response)
} catch {
print("Error:", error)
}
}AgentClient is @Observable, so it integrates with SwiftUI’s Observation system. You can observe client.messages for live updates as the stream arrives.
This is work in progress, use at your own risk!
Contributions are most welcome—expect future updates.