Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
ingvarus-bc authored Feb 2, 2024
2 parents c56598e + 60dfce4 commit c271966
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Sources/OpenAI/Private/StreamingSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,22 @@ final class StreamingSession<ResultType: Codable>: NSObject, Identifiable, URLSe
extension StreamingSession {

private func processJSON(from stringContent: String) {
let jsonObjects = "\(previousChunkBuffer)\(stringContent)".trimmingCharacters(in: .whitespacesAndNewlines)
if stringContent.isEmpty {
return
}
let jsonObjects = "\(previousChunkBuffer)\(stringContent)"
.trimmingCharacters(in: .whitespacesAndNewlines)
.components(separatedBy: "data:")
.filter { $0.isEmpty == false }
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }

.filter { $0.isEmpty == false }

previousChunkBuffer = ""

guard jsonObjects.isEmpty == false, jsonObjects.first != streamingCompletionMarker else {
return
}
jsonObjects.enumerated().forEach { (index, jsonContent) in
guard jsonContent != streamingCompletionMarker else {
guard jsonContent != streamingCompletionMarker && !jsonContent.isEmpty else {
return
}
guard let jsonData = jsonContent.data(using: .utf8) else {
Expand Down

0 comments on commit c271966

Please sign in to comment.