@@ -63,7 +63,7 @@ public struct ChatGPTError: Error, Codable, LocalizedError {
6363}
6464
6565public enum ChatGPTResponse : Equatable {
66- case status( String )
66+ case status( [ String ] )
6767 case partialText( String )
6868 case toolCalls( [ ChatMessage . ToolCall ] )
6969}
@@ -140,7 +140,9 @@ public class ChatGPTService: ChatGPTServiceType {
140140
141141 if !pendingToolCalls. isEmpty {
142142 if configuration. runFunctionsAutomatically {
143+ var toolCallStatuses = [ String: String] ( )
143144 for toolCall in pendingToolCalls {
145+ let id = toolCall. id
144146 for await response in await runFunctionCall (
145147 toolCall,
146148 memory: memory,
@@ -151,13 +153,19 @@ public class ChatGPTService: ChatGPTServiceType {
151153 functionCallResponses. append ( . init(
152154 role: . tool,
153155 content: output,
154- toolCallId: toolCall . id
156+ toolCallId: id
155157 ) )
156158 case let . status( status) :
157- continuation. yield ( . status( status) )
159+ toolCallStatuses [ id] = status
160+ continuation
161+ . yield ( . status( Array ( toolCallStatuses. values) ) )
158162 }
159163 }
164+ toolCallStatuses [ id] = nil
165+ continuation
166+ . yield ( . status( Array ( toolCallStatuses. values) ) )
160167 }
168+ continuation. yield ( . status( [ ] ) )
161169 } else {
162170 if !configuration. runFunctionsAutomatically {
163171 continuation. yield ( . toolCalls( pendingToolCalls) )
@@ -177,18 +185,21 @@ public class ChatGPTService: ChatGPTServiceType {
177185 try Task . checkCancellation ( )
178186 switch content {
179187 case let . partialText( text) :
180- continuation. yield ( . partialText( text) )
188+ continuation. yield ( ChatGPTResponse . partialText ( text) )
181189
182190 case let . partialToolCalls( toolCalls) :
183191 guard configuration. runFunctionsAutomatically else { break }
192+ var toolCallStatuses = [ String: String] ( )
184193 for toolCall in toolCalls. keys. sorted ( ) {
185194 if let toolCallValue = toolCalls [ toolCall] {
186195 for await status in await prepareFunctionCall (
187196 toolCallValue,
188197 memory: memory,
189198 sourceMessageId: sourceMessageId
190199 ) {
191- continuation. yield ( . status( status) )
200+ toolCallStatuses [ toolCallValue. id] = status
201+ continuation
202+ . yield ( . status( Array ( toolCallStatuses. values) ) )
192203 }
193204 }
194205 }
@@ -576,8 +587,7 @@ extension ChatGPTService {
576587
577588 return requestBody
578589 }
579-
580-
590+
581591 func maxTokenForReply( maxToken: Int , remainingTokens: Int ? ) -> Int ? {
582592 guard let remainingTokens else { return nil }
583593 return min ( maxToken / 2 , remainingTokens)
0 commit comments