Skip to content

Commit

Permalink
Changed session structure
Browse files Browse the repository at this point in the history
Signed-off-by: ActuallyTaylor <[email protected]>
  • Loading branch information
ActuallyTaylor committed Jan 19, 2024
1 parent 590ea49 commit 444bef3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Sources/SLlama/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public struct Client {
continuation.finish(throwing: error)
}

stream.resumeStream()
stream.start()
} catch {
continuation.finish(throwing: error)
}
Expand Down
31 changes: 10 additions & 21 deletions Sources/SLlama/ClientStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,25 @@ public class ClientStream<Model: Codable>: 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: "")
Expand Down

0 comments on commit 444bef3

Please sign in to comment.