Skip to content

Commit 406938a

Browse files
committed
Make ChatGPTServiceType conforms to ObservableObject
1 parent 66b931d commit 406938a

2 files changed

Lines changed: 48 additions & 25 deletions

File tree

Core/Sources/ChatService/ChatService.swift

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ChatPlugins
2+
import Combine
23
import Foundation
34
import OpenAIService
45

@@ -8,11 +9,16 @@ public final class ChatService: ObservableObject {
89
TerminalChatPlugin.self
910
)
1011
var runningPlugin: ChatPlugin?
12+
var cancellable = Set<AnyCancellable>()
1113

12-
public init(chatGPTService: ChatGPTServiceType) {
14+
public init<T: ChatGPTServiceType>(chatGPTService: T) {
1315
self.chatGPTService = chatGPTService
16+
17+
chatGPTService.objectWillChange.sink { [weak self] _ in
18+
self?.objectWillChange.send()
19+
}.store(in: &cancellable)
1420
}
15-
21+
1622
public func send(content: String) async throws {
1723
// look for the prefix of content, see if there is something like /command.
1824
// If there is, then we need to find the plugin that can handle this command.
@@ -43,26 +49,30 @@ public final class ChatService: ObservableObject {
4349
}
4450
await chatGPTService.clearHistory()
4551
}
52+
53+
public func mutateSystemPrompt(_ newPrompt: String) async {
54+
await chatGPTService.mutateSystemPrompt(newPrompt)
55+
}
4656
}
4757

4858
extension ChatService: ChatPluginDelegate {
49-
public func pluginDidStartResponding(_ plugin: ChatPlugins.ChatPlugin) {
59+
public func pluginDidStartResponding(_: ChatPlugins.ChatPlugin) {
5060
Task {
5161
await chatGPTService.markReceivingMessage(true)
5262
}
5363
}
54-
55-
public func pluginDidEndResponding(_ plugin: ChatPlugins.ChatPlugin) {
64+
65+
public func pluginDidEndResponding(_: ChatPlugins.ChatPlugin) {
5666
Task {
5767
await chatGPTService.markReceivingMessage(false)
5868
}
5969
}
60-
70+
6171
public func pluginDidStart(_ plugin: ChatPlugin) {
6272
runningPlugin = plugin
6373
}
64-
65-
public func pluginDidEnd(_ plugin: ChatPlugin) {
74+
75+
public func pluginDidEnd(_: ChatPlugin) {
6676
runningPlugin = nil
6777
}
6878
}

Core/Sources/OpenAIService/ChatGPTService.swift

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import AsyncAlgorithms
22
import Foundation
3+
import Preferences
34

4-
public protocol ChatGPTServiceType {
5+
public protocol ChatGPTServiceType: ObservableObject {
6+
var isReceivingMessage: Bool { get async }
7+
var history: [ChatMessage] { get async }
58
func send(content: String, summary: String?) async throws -> AsyncThrowingStream<String, Error>
69
func stopReceivingMessage() async
710
func clearHistory() async
@@ -49,13 +52,31 @@ public struct ChatGPTError: Error, Codable, LocalizedError {
4952
}
5053
}
5154

52-
public actor ChatGPTService: ChatGPTServiceType, ObservableObject {
53-
public var temperature: Double
54-
public var model: String
55-
public var endpoint: String
56-
public var apiKey: String
55+
public actor ChatGPTService: ChatGPTServiceType {
5756
public var systemPrompt: String
58-
public var maxToken: Int
57+
public var temperature: Double
58+
59+
public var model: String {
60+
let value = UserDefaults.shared.value(for: \.chatGPTModel)
61+
if value.isEmpty { return "gpt-3.5-turbo" }
62+
return value
63+
}
64+
65+
public var endpoint: String {
66+
let value = UserDefaults.shared.value(for: \.chatGPTEndpoint)
67+
if value.isEmpty { return "https://api.openai.com/v1/chat/completions" }
68+
69+
return value
70+
}
71+
72+
public var apiKey: String {
73+
UserDefaults.shared.value(for: \.openAIAPIKey)
74+
}
75+
76+
public var maxToken: Int {
77+
UserDefaults.shared.value(for: \.chatGPTMaxToken)
78+
}
79+
5980
public var history: [ChatMessage] = [] {
6081
didSet { objectWillChange.send() }
6182
}
@@ -69,19 +90,11 @@ public actor ChatGPTService: ChatGPTServiceType, ObservableObject {
6990
var buildCompletionStreamAPI: CompletionStreamAPIBuilder = OpenAICompletionStreamAPI.init
7091

7192
public init(
72-
systemPrompt: String,
73-
apiKey: String,
74-
endpoint: String? = nil,
75-
model: String? = nil,
76-
temperature: Double = 1,
77-
maxToken: Int = 2048
93+
systemPrompt: String = "",
94+
temperature: Double = 1
7895
) {
7996
self.systemPrompt = systemPrompt
80-
self.apiKey = apiKey
81-
self.model = model ?? "gpt-3.5-turbo"
8297
self.temperature = temperature
83-
self.maxToken = maxToken
84-
self.endpoint = endpoint ?? "https://api.openai.com/v1/chat/completions"
8598
}
8699

87100
public func send(

0 commit comments

Comments
 (0)