Skip to content

Commit

Permalink
Updated SSL handshake for stream
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrylo-mukha committed Feb 1, 2024
1 parent 75fd974 commit 865056c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion OpenAIKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'OpenAIKit'
s.version = '1.9.0'
s.version = '1.9.1'
s.summary = 'OpenAI is a community-maintained repository containing Swift implementation over OpenAI public API.'

s.description = <<-DESC
Expand Down
14 changes: 13 additions & 1 deletion Sources/OpenAIKit/Helpers/AIEventStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import FoundationNetworking
import FoundationXML
#endif

@available(swift 5.5)
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
public struct AIStreamResponse<ResponseType: Decodable> {
public let stream: AIEventStream<ResponseType>
public let message: ResponseType?
Expand All @@ -20,6 +22,8 @@ public struct AIStreamResponse<ResponseType: Decodable> {
public var forceEnd: Bool = false
}

@available(swift 5.5)
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
public final class AIEventStream<ResponseType: Decodable>: NSObject, URLSessionDataDelegate {
private let request: URLRequest
private var session: URLSession?
Expand All @@ -33,9 +37,11 @@ public final class AIEventStream<ResponseType: Decodable>: NSObject, URLSessionD

private var fetchError: Error? = nil

init(request: URLRequest) {
private weak var sslDelegate: OpenAISSLDelegate?
init(request: URLRequest, sslDelegate: OpenAISSLDelegate?) {
self.request = request
self.operationQueue = OperationQueue()
self.sslDelegate = sslDelegate
operationQueue.maxConcurrentOperationCount = 1
}

Expand Down Expand Up @@ -114,8 +120,14 @@ public final class AIEventStream<ResponseType: Decodable>: NSObject, URLSessionD
public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
completionHandler(request)
}

public func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
sslDelegate?.urlSession(session, didReceive: challenge, completionHandler: completionHandler)
}
}

@available(swift 5.5)
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
public extension AIEventStream {
func onStart(_ onStartCompletion: @escaping (() -> Void)) {
self.onStartCompletion = onStartCompletion
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAIKit/Helpers/OpenAIKitNetwork.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public final class OpenAIKitNetwork {
request.addValue(value, forHTTPHeaderField: key)
}

let stream = AIEventStream<ResponseType>(request: request)
let stream = AIEventStream<ResponseType>(request: request, sslDelegate: session.delegate as? OpenAISSLDelegate)
activeStreams.append(stream)

return AsyncThrowingStream<AIStreamResponse<ResponseType>, Error> { continuation in
Expand Down

0 comments on commit 865056c

Please sign in to comment.