Skip to content

Commit d631234

Browse files
committed
Make generateContext async
1 parent eebff15 commit d631234

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

Core/Sources/ChatService/DynamicContextController.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,26 @@ final class DynamicContextController {
4646
functionProvider.removeAll()
4747
let language = UserDefaults.shared.value(for: \.chatGPTLanguage)
4848
let oldMessages = await memory.history
49-
let contexts = contextCollectors.compactMap {
50-
$0.generateContext(
51-
history: oldMessages,
52-
scopes: scopes,
53-
content: content,
54-
configuration: configuration
55-
)
49+
let contexts = await withTaskGroup(
50+
of: ChatContext?.self
51+
) { [scopes, content, configuration] group in
52+
for collector in contextCollectors {
53+
group.addTask {
54+
await collector.generateContext(
55+
history: oldMessages,
56+
scopes: scopes,
57+
content: content,
58+
configuration: configuration
59+
)
60+
}
61+
}
62+
var contexts = [ChatContext]()
63+
for await context in group {
64+
if let context = context {
65+
contexts.append(context)
66+
}
67+
}
68+
return contexts
5669
}
5770
let contextualSystemPrompt = """
5871
\(language.isEmpty ? "" : "You must always reply in \(language)")

Pro

Submodule Pro updated from a8895f1 to ba9117b

Tool/Sources/ChatContextCollector/ChatContextCollector.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ public protocol ChatContextCollector {
1616
scopes: Set<String>,
1717
content: String,
1818
configuration: ChatGPTConfiguration
19-
) -> ChatContext?
19+
) async -> ChatContext?
2020
}
2121

0 commit comments

Comments
 (0)