49 lines
1.5 KiB
Swift
49 lines
1.5 KiB
Swift
// CxLLM Studio — macOS Application
|
|
// Services/GatewayService.swift — Gateway connection service
|
|
|
|
import Foundation
|
|
import CxCode
|
|
|
|
@Observable
|
|
final class GatewayService {
|
|
private(set) var isConnected = false
|
|
private(set) var healthStatus: String = "unknown"
|
|
private(set) var connectionError: String?
|
|
|
|
let gateway: CxGateway
|
|
|
|
init(gateway: CxGateway) {
|
|
self.gateway = gateway
|
|
}
|
|
|
|
func checkHealth() async {
|
|
do {
|
|
let health = try await gateway.health()
|
|
healthStatus = health.status
|
|
isConnected = health.status == "ok"
|
|
connectionError = nil
|
|
} catch {
|
|
isConnected = false
|
|
connectionError = error.localizedDescription
|
|
}
|
|
}
|
|
|
|
func createSession(model: String) async throws -> String {
|
|
let session = try await gateway.sessions.create(model: model)
|
|
return session.sessionId
|
|
}
|
|
|
|
func sendMessage(sessionId: String, message: String, model: String) async throws -> ChatResponse {
|
|
try await gateway.chat.ask(sessionId: sessionId, message: message, model: model)
|
|
}
|
|
|
|
func streamMessage(sessionId: String, message: String, model: String) -> AsyncThrowingStream<String, Error> {
|
|
gateway.chat.streamAsk(sessionId: sessionId, message: message, model: model)
|
|
}
|
|
|
|
func configure(baseURL: String, apiKey: String) {
|
|
// Recreate gateway with new config would go here
|
|
// For now, config is immutable after init
|
|
}
|
|
}
|