forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatGPTMemory.swift
More file actions
93 lines (87 loc) · 3.14 KB
/
ChatGPTMemory.swift
File metadata and controls
93 lines (87 loc) · 3.14 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import Foundation
public protocol ChatGPTMemory {
/// The visible messages to the ChatGPT service.
var messages: [ChatMessage] { get async }
/// The remaining tokens available for the reply.
var remainingTokens: Int? { get async }
/// Update the message history.
func mutateHistory(_ update: (inout [ChatMessage]) -> Void) async
/// Refresh `messages` and `remainingTokens`.
/// Sometimes the message history needs time to generate, in such case, you
/// can use this method to refresh the memory, instead of making variable
/// `messages` and `remainingTokens` computed.
func refresh() async
}
public extension ChatGPTMemory {
/// Append a message to the history.
func appendMessage(_ message: ChatMessage) async {
await mutateHistory { history in
if let index = history.firstIndex(where: { $0.id == message.id }) {
history[index] = message
} else {
history.append(message)
}
}
}
/// Update a message in the history.
func updateMessage(id: String, _ update: (inout ChatMessage) -> Void) async {
await mutateHistory { history in
if let index = history.firstIndex(where: { $0.id == id }) {
update(&history[index])
}
}
}
/// Remove a message from the history.
func removeMessage(_ id: String) async {
await mutateHistory {
$0.removeAll { $0.id == id }
}
}
/// Stream a message to the history.
func streamMessage(
id: String,
role: ChatMessage.Role? = nil,
content: String? = nil,
functionCall: ChatMessage.FunctionCall? = nil,
summary: String? = nil
) async {
await mutateHistory { history in
if let index = history.firstIndex(where: { $0.id == id }) {
if let content {
if history[index].content == nil {
history[index].content = content
} else {
history[index].content?.append(content)
}
}
if let role {
history[index].role = role
}
if let functionCall {
if history[index].functionCall == nil {
history[index].functionCall = functionCall
} else {
history[index].functionCall?.name.append(functionCall.name)
history[index].functionCall?.arguments.append(functionCall.arguments)
}
}
if let summary {
history[index].summary = summary
}
} else {
history.append(.init(
id: id,
role: role ?? .system,
content: content,
name: nil,
functionCall: functionCall,
summary: summary
))
}
}
}
/// Clear the history.
func clearHistory() async {
await mutateHistory { $0.removeAll() }
}
}