forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContextAwareAutoManagedChatGPTMemory.swift
More file actions
58 lines (49 loc) · 1.77 KB
/
ContextAwareAutoManagedChatGPTMemory.swift
File metadata and controls
58 lines (49 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import Foundation
import OpenAIService
public final class ContextAwareAutoManagedChatGPTMemory: ChatGPTMemory {
private let memory: AutoManagedChatGPTMemory
let contextController: DynamicContextController
let functionProvider: ChatFunctionProvider
weak var chatService: ChatService?
public var messages: [ChatMessage] {
get async { await memory.messages }
}
public var remainingTokens: Int? {
get async { await memory.remainingTokens }
}
public var history: [ChatMessage] {
get async { await memory.history }
}
func observeHistoryChange(_ observer: @escaping () -> Void) {
memory.observeHistoryChange(observer)
}
init(
configuration: ChatGPTConfiguration,
functionProvider: ChatFunctionProvider
) {
memory = AutoManagedChatGPTMemory(
systemPrompt: "",
configuration: configuration,
functionProvider: functionProvider
)
contextController = DynamicContextController(
memory: memory,
functionProvider: functionProvider,
configuration: configuration,
contextCollectors: allContextCollectors
)
self.functionProvider = functionProvider
}
public func mutateHistory(_ update: (inout [ChatMessage]) -> Void) async {
await memory.mutateHistory(update)
}
public func refresh() async {
let content = (await memory.history)
.last(where: { $0.role == .user || $0.role == .function })?.content
try? await contextController.updatePromptToMatchContent(systemPrompt: """
\(chatService?.systemPrompt ?? "")
\(chatService?.extraSystemPrompt ?? "")
""", content: content ?? "")
await memory.refresh()
}
}