@@ -4,14 +4,12 @@ import GPTEncoder
44import Preferences
55
66public protocol ChatGPTServiceType : ObservableObject {
7- var isReceivingMessage : Bool { get async }
87 var history : [ ChatMessage ] { get async }
98 func send( content: String , summary: String ? ) async throws -> AsyncThrowingStream < String , Error >
109 func stopReceivingMessage( ) async
1110 func clearHistory( ) async
1211 func mutateSystemPrompt( _ newPrompt: String ) async
1312 func mutateHistory( _ mutate: ( inout [ ChatMessage ] ) -> Void ) async
14- func markReceivingMessage( _ receiving: Bool ) async
1513}
1614
1715public enum ChatGPTServiceError : Error , LocalizedError {
@@ -105,10 +103,6 @@ public actor ChatGPTService: ChatGPTServiceType {
105103 didSet { objectWillChange. send ( ) }
106104 }
107105
108- public internal( set) var isReceivingMessage = false {
109- didSet { objectWillChange. send ( ) }
110- }
111-
112106 var stop : [ String ]
113107 var uuidGenerator : ( ) -> String = { UUID ( ) . uuidString }
114108 var cancelTask : Cancellable ?
@@ -131,7 +125,6 @@ public actor ChatGPTService: ChatGPTServiceType {
131125 content: String ,
132126 summary: String ? = nil
133127 ) async throws -> AsyncThrowingStream < String , Error > {
134- guard !isReceivingMessage else { throw CancellationError ( ) }
135128 guard let url = URL ( string: endpoint) else { throw ChatGPTServiceError . endpointIncorrect }
136129
137130 if !content. isEmpty || summary != nil {
@@ -155,8 +148,6 @@ public actor ChatGPTService: ChatGPTServiceType {
155148 max_tokens: maxTokenForReply ( model: model, remainingTokens: remainingTokens)
156149 )
157150
158- isReceivingMessage = true
159-
160151 let api = buildCompletionStreamAPI (
161152 apiKey,
162153 designatedProvider ?? UserDefaults . shared. value ( for: \. chatFeatureProvider) ,
@@ -168,10 +159,6 @@ public actor ChatGPTService: ChatGPTServiceType {
168159 Task {
169160 do {
170161 let ( trunks, cancel) = try await api ( )
171- guard isReceivingMessage else {
172- continuation. finish ( )
173- return
174- }
175162 cancelTask = cancel
176163 for try await trunk in trunks {
177164 guard let delta = trunk. choices. first? . delta else { continue }
@@ -199,19 +186,15 @@ public actor ChatGPTService: ChatGPTServiceType {
199186 }
200187
201188 continuation. finish ( )
202- isReceivingMessage = false
203189 } catch let error as CancellationError {
204- isReceivingMessage = false
205190 continuation. finish ( throwing: error)
206191 } catch let error as NSError where error. code == NSURLErrorCancelled {
207- isReceivingMessage = false
208192 continuation. finish ( throwing: error)
209193 } catch {
210194 history. append ( . init(
211195 role: . assistant,
212196 content: error. localizedDescription
213197 ) )
214- isReceivingMessage = false
215198 continuation. finish ( throwing: error)
216199 }
217200 }
@@ -222,7 +205,6 @@ public actor ChatGPTService: ChatGPTServiceType {
222205 content: String ,
223206 summary: String ? = nil
224207 ) async throws -> String ? {
225- guard !isReceivingMessage else { throw CancellationError ( ) }
226208 guard let url = URL ( string: endpoint) else { throw ChatGPTServiceError . endpointIncorrect }
227209
228210 if !content. isEmpty || summary != nil {
@@ -246,9 +228,6 @@ public actor ChatGPTService: ChatGPTServiceType {
246228 max_tokens: maxTokenForReply ( model: model, remainingTokens: remainingTokens)
247229 )
248230
249- isReceivingMessage = true
250- defer { isReceivingMessage = false }
251-
252231 let api = buildCompletionAPI (
253232 apiKey,
254233 designatedProvider ?? UserDefaults . shared. value ( for: \. chatFeatureProvider) ,
@@ -273,7 +252,6 @@ public actor ChatGPTService: ChatGPTServiceType {
273252 public func stopReceivingMessage( ) {
274253 cancelTask ? ( )
275254 cancelTask = nil
276- isReceivingMessage = false
277255 }
278256
279257 public func clearHistory( ) {
@@ -288,10 +266,6 @@ public actor ChatGPTService: ChatGPTServiceType {
288266 public func mutateHistory( _ mutate: ( inout [ ChatMessage ] ) -> Void ) async {
289267 mutate ( & history)
290268 }
291-
292- public func markReceivingMessage( _ receiving: Bool ) {
293- isReceivingMessage = receiving
294- }
295269}
296270
297271extension ChatGPTService {
0 commit comments