diff --git a/Sources/SLlama/Client.swift b/Sources/SLlama/Client.swift index be30dd9..da8d74b 100644 --- a/Sources/SLlama/Client.swift +++ b/Sources/SLlama/Client.swift @@ -51,7 +51,7 @@ public struct Client { continuation.finish(throwing: error) } - stream.resumeStream() + stream.start() } catch { continuation.finish(throwing: error) } diff --git a/Sources/SLlama/ClientStream.swift b/Sources/SLlama/ClientStream.swift index 1c5f2ac..545b701 100644 --- a/Sources/SLlama/ClientStream.swift +++ b/Sources/SLlama/ClientStream.swift @@ -16,36 +16,25 @@ public class ClientStream: NSObject, URLSessionDataDelegate { case finished } - private var session: URLSession! = nil - private var task: URLSessionTask? = nil - + private lazy var session: URLSession = { + let session = URLSession(configuration: .default, delegate: self, delegateQueue: nil) + return session + }() + private let urlRequest: URLRequest + public var status: StreamStatus = .none public var didReceiveModel: ((_ session: ClientStream, _ model: Model) -> ())? = nil public var didFinish: ((_ session: ClientStream, _ error: Error?) -> ())? = nil init(request: URLRequest) { - super.init() - - self.session = URLSession(configuration: .default, delegate: self, delegateQueue: .main) - self.task = self.session.dataTask(with: request) - } - - public func resumeStream() { - task?.resume() - status = .started + urlRequest = request } - public func cancelStream() { - task?.cancel() - status = .canceled + public func start() { + self.session.dataTask(with: urlRequest).resume() } - - public func suspendStream() { - task?.suspend() - status = .suspended - } - + public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { guard var string = String(data: data, encoding: .utf8) else { return } string = string.replacingOccurrences(of: "data: ", with: "")