Skip to content

Swift client for Cloudflare Agents with Vercel AI SDK-compatible chat messages and streaming over WebSockets.

License

Notifications You must be signed in to change notification settings

victorhenrion/Agents.swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Agents.swift

Swift client for Cloudflare Agents with Vercel AI SDK-compatible chat messages and streaming over WebSockets.

Quickstart

Requirements

  • Swift 5.9+
  • Platforms: iOS 17+, macOS 14+, tvOS 17+, watchOS 10+, visionOS 1+

Installation

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")

Example

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)
    }
}

SwiftUI

AgentClient is @Observable, so it integrates with SwiftUI’s Observation system. You can observe client.messages for live updates as the stream arrives.

Project status

This is work in progress, use at your own risk!
Contributions are most welcome—expect future updates.

About

Swift client for Cloudflare Agents with Vercel AI SDK-compatible chat messages and streaming over WebSockets.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages