Skip to content

Commit e19db0b

Browse files
committed
Support multiple status
1 parent 192669e commit e19db0b

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

Tool/Sources/ChatBasic/ChatAgent.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public enum ChatAgentResponse {
66
}
77

88
/// Post the status of the current message.
9-
case status(String)
9+
case status([String])
1010
/// Update the text message to the current message.
1111
case content(Content)
1212
/// Update the attachments of the current message.

Tool/Sources/OpenAIService/ChatGPTService.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public struct ChatGPTError: Error, Codable, LocalizedError {
6363
}
6464

6565
public 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

Comments
 (0)