Skip to content

Commit

Permalink
Wrapped ChatMessage construction at end of stream in a do/catch, sinc…
Browse files Browse the repository at this point in the history
…e it requires a try now.
  • Loading branch information
btfranklin committed Jun 18, 2023
1 parent 028a2ad commit 6714f37
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension StreamableChatThread {

// Define the callback closure that appends the message to the chat thread
let addStreamedMessageToThread: (ChatMessage) -> Void = { message in
_ = self.addMessage(message)
self.addMessage(message)
}

let asyncByteStream = try await self.chatThread.connection.createAsyncByteStream(for: requestBody)
Expand All @@ -49,11 +49,15 @@ extension StreamableChatThread {
strongSelf.streamingTask = nil
}
if let responseMessageRole, let responseMessageContent {
var streamedMessage = ChatMessage(role: responseMessageRole,
content: responseMessageContent,
id: responseMessageId)
streamedMessage.id = responseMessageId ?? "unspecified"
addStreamedMessageToThread(streamedMessage)
do {
var streamedMessage = try ChatMessage(role: responseMessageRole,
content: responseMessageContent,
id: responseMessageId)
streamedMessage.id = responseMessageId ?? "unspecified"
addStreamedMessageToThread(streamedMessage)
} catch {
print("error while creating streamed message: \(error.localizedDescription)")
}
}
}

Expand Down

0 comments on commit 6714f37

Please sign in to comment.