11import AsyncAlgorithms
22import Foundation
3- import Logger
43
54public protocol ChatGPTServiceType {
65 func send( content: String , summary: String ? ) async throws -> AsyncThrowingStream < String , Error >
@@ -64,6 +63,7 @@ public actor ChatGPTService: ChatGPTServiceType, ObservableObject {
6463 didSet { objectWillChange. send ( ) }
6564 }
6665
66+ var uuidGenerator : ( ) -> String = { UUID ( ) . uuidString }
6767 var cancelTask : Cancellable ?
6868 var buildCompletionStreamAPI : CompletionStreamAPIBuilder = OpenAICompletionStreamAPI . init
6969
@@ -89,7 +89,12 @@ public actor ChatGPTService: ChatGPTServiceType, ObservableObject {
8989 ) async throws -> AsyncThrowingStream < String , Error > {
9090 guard !isReceivingMessage else { throw CancellationError ( ) }
9191 guard let url = URL ( string: endpoint) else { throw ChatGPTServiceError . endpointIncorrect }
92- let newMessage = ChatMessage ( role: . user, content: content, summary: summary)
92+ let newMessage = ChatMessage (
93+ id: uuidGenerator ( ) ,
94+ role: . user,
95+ content: content,
96+ summary: summary
97+ )
9398 history. append ( newMessage)
9499
95100 let requestBody = CompletionRequestBody (
@@ -142,7 +147,6 @@ public actor ChatGPTService: ChatGPTServiceType, ObservableObject {
142147 isReceivingMessage = false
143148 continuation. finish ( throwing: error)
144149 } catch {
145- Logger . service. error ( error)
146150 history. append ( . init(
147151 role: . assistant,
148152 content: error. localizedDescription
@@ -178,6 +182,10 @@ extension ChatGPTService {
178182 func changeBuildCompletionStreamAPI( _ builder: @escaping CompletionStreamAPIBuilder ) {
179183 buildCompletionStreamAPI = builder
180184 }
185+
186+ func changeUUIDGenerator( _ generator: @escaping ( ) -> String ) {
187+ uuidGenerator = generator
188+ }
181189
182190 func combineHistoryWithSystemPrompt( ) -> [ CompletionRequestBody . Message ] {
183191 if history. count > 4 {
0 commit comments