Skip to content

Commit a883763

Browse files
committed
Add ChatGPTService
1 parent a50cd21 commit a883763

File tree

8 files changed

+971
-393
lines changed

8 files changed

+971
-393
lines changed

Tool/Sources/ChatBasic/ChatAgent.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import Foundation
22

33
public enum ChatAgentResponse {
4+
public enum Content {
5+
case text(String)
6+
case modification
7+
}
8+
49
/// Post the status of the current message.
510
case status(String)
6-
/// Send a token of the text message to the current message.
7-
case contentToken(String)
11+
/// Update the text message to the current message.
12+
case content([Content])
813
/// Update the attachments of the current message.
914
case attachments([URL])
1015
/// Update the references of the current message.
1116
case references([ChatMessage.Reference])
12-
/// End the message. The next contents will be sent as a new message.
13-
case finishMessage
17+
/// End the current message. The next contents will be sent as a new message.
18+
case startNewMessage
1419
}
1520

1621
public struct ChatAgentRequest {

Tool/Sources/ChatBasic/ChatGPTFunction.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,18 @@ public extension ChatGPTFunction {
4343
argumentsJsonString: String,
4444
reportProgress: @escaping ReportProgress
4545
) async throws -> Result {
46-
do {
47-
let arguments = try JSONDecoder()
48-
.decode(Arguments.self, from: argumentsJsonString.data(using: .utf8) ?? Data())
49-
return try await call(arguments: arguments, reportProgress: reportProgress)
50-
} catch {
51-
await reportProgress("Error: Failed to decode arguments. \(error.localizedDescription)")
52-
throw error
53-
}
46+
let arguments = try await {
47+
do {
48+
return try JSONDecoder()
49+
.decode(Arguments.self, from: argumentsJsonString.data(using: .utf8) ?? Data())
50+
} catch {
51+
await reportProgress(
52+
"Error: Failed to decode arguments. \(error.localizedDescription)"
53+
)
54+
throw error
55+
}
56+
}()
57+
return try await call(arguments: arguments, reportProgress: reportProgress)
5458
}
5559
}
5660

@@ -85,7 +89,7 @@ public extension ChatGPTArgumentsCollectingFunction {
8589
assertionFailure("This function is only used to get a structured output from the bot.")
8690
return ""
8791
}
88-
92+
8993
@available(
9094
*,
9195
deprecated,

0 commit comments

Comments
 (0)