Skip to content

Commit e5000a9

Browse files
authored
Fix for truncated response because of AsyncStream issue in ChatGPTAPI
1 parent e8040e4 commit e5000a9

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Shared/ChatGPTAPI.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,19 @@ class ChatGPTAPI: LLMClient, @unchecked Sendable {
100100
}
101101

102102
var responseText = ""
103+
let streams: AsyncThrowingStream<String, Error> = AsyncThrowingStream { continuation in
104+
Task {
105+
for try await line in result.lines {
106+
try Task.checkCancellation()
107+
continuation.yield(line)
108+
}
109+
continuation.finish()
110+
}
111+
}
112+
103113
return AsyncThrowingStream { [weak self] in
104114
guard let self else { return nil }
105-
for try await line in result.lines {
115+
for try await line in streams {
106116
try Task.checkCancellation()
107117
if line.hasPrefix("data: "),
108118
let data = line.dropFirst(6).data(using: .utf8),

0 commit comments

Comments
 (0)