---
source: tests/backends_swift_snapshot_test.rs
expression: "&file.content"
---
// Generated by alef. Do not edit by hand.
// swift-format-ignore-file

import Foundation
import RustBridge
public final class DefaultClient {
    private let inner: RustBridge.DefaultClient
    public init(apiKey: String, baseUrl: String? = nil) throws {
        self.inner = try RustBridge.createDefaultClient(apiKey, baseUrl)
    }
    internal init(_ inner: RustBridge.DefaultClient) {
        self.inner = inner
    }
    public func ping() -> String {
        return RustBridge.defaultClientPing(self.inner)
    }
    public func chatStream(_ req: ChatRequest) async throws -> AsyncThrowingStream<ChatChunk, Error> {
        let inner = self.inner
        let handle = try await Task.detached(priority: .userInitiated) {
            try RustBridge.defaultClientChatStreamStart(inner, req)
        }.value

        return AsyncThrowingStream<ChatChunk, Error> { continuation in
            let task = Task.detached(priority: .userInitiated) {
                do {
                    while !Task.isCancelled {
                        let json = try handle.next().toString()
                        if json.isEmpty { break }
                        let chunk = try RustBridge.chatChunkFromJson(json)
                        continuation.yield(chunk)
                    }
                    continuation.finish()
                } catch {
                    continuation.finish(throwing: error)
                }
            }
            continuation.onTermination = { _ in task.cancel() }
        }
    }
}

// MARK: - Sendable conformance for DefaultClient (streaming client inner)
// swift-bridge opaque types are not automatically Sendable.
// Captured by Task.detached in streaming methods — Rust type is Send + Sync.
extension RustBridge.DefaultClient: @unchecked Sendable {}
// swift-bridge opaque types are not automatically Sendable.  The Rust
// side uses Mutex<stream> + tokio Runtime — both Send + Sync — so
// @unchecked is correct: thread-safety is enforced by Rust.
extension RustBridge.DefaultClientChatStreamStreamHandle: @unchecked Sendable {}
// MARK: - Sendable conformance for ChatRequest (streaming request param)
// swift-bridge opaque types are not automatically Sendable.
// Passed into Task.detached for streaming — Rust type is Send + Sync.
extension RustBridge.ChatRequest: @unchecked Sendable {}
