forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatGPTChatTab.swift
More file actions
150 lines (129 loc) · 4.99 KB
/
ChatGPTChatTab.swift
File metadata and controls
150 lines (129 loc) · 4.99 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import ChatContextCollector
import ChatService
import ChatTab
import CodableWrappers
import Combine
import ComposableArchitecture
import Foundation
import OpenAIService
import Preferences
import SwiftUI
/// A chat tab that provides a context aware chat bot, powered by ChatGPT.
public class ChatGPTChatTab: ChatTab {
public static var name: String { "Chat" }
public let service: ChatService
let chat: StoreOf<Chat>
let viewStore: ViewStoreOf<Chat>
private var cancellable = Set<AnyCancellable>()
struct RestorableState: Codable {
var history: [OpenAIService.ChatMessage]
var configuration: OverridingChatGPTConfiguration.Overriding
var systemPrompt: String
var extraSystemPrompt: String
var defaultScopes: Set<ChatContext.Scope>?
}
struct Builder: ChatTabBuilder {
var title: String
var customCommand: CustomCommand?
var afterBuild: (ChatGPTChatTab) async -> Void = { _ in }
func build(store: StoreOf<ChatTabItem>) async -> (any ChatTab)? {
let tab = await ChatGPTChatTab(store: store)
if let customCommand {
try? await tab.service.handleCustomCommand(customCommand)
}
await afterBuild(tab)
return tab
}
}
public func buildView() -> any View {
ChatPanel(chat: chat)
}
public func buildTabItem() -> any View {
ChatTabItemView(chat: chat)
}
public func buildIcon() -> any View {
WithViewStore(chat, observe: \.isReceivingMessage) { viewStore in
if viewStore.state {
Image(systemName: "ellipsis.message")
} else {
Image(systemName: "message")
}
}
}
public func buildMenu() -> any View {
ChatContextMenu(store: chat.scope(state: \.chatMenu, action: Chat.Action.chatMenu))
}
public func restorableState() async -> Data {
let state = RestorableState(
history: await service.memory.history,
configuration: service.configuration.overriding,
systemPrompt: service.systemPrompt,
extraSystemPrompt: service.extraSystemPrompt,
defaultScopes: service.defaultScopes
)
return (try? JSONEncoder().encode(state)) ?? Data()
}
public static func restore(
from data: Data,
externalDependency: Void
) async throws -> any ChatTabBuilder {
let state = try JSONDecoder().decode(RestorableState.self, from: data)
let builder = Builder(title: "Chat") { @MainActor tab in
tab.service.configuration.overriding = state.configuration
tab.service.mutateSystemPrompt(state.systemPrompt)
tab.service.mutateExtraSystemPrompt(state.extraSystemPrompt)
if let scopes = state.defaultScopes {
tab.service.defaultScopes = scopes
}
await tab.service.memory.mutateHistory { history in
history = state.history
}
}
return builder
}
public static func chatBuilders(externalDependency: Void) -> [ChatTabBuilder] {
let customCommands = UserDefaults.shared.value(for: \.customCommands).compactMap {
command in
if case .customChat = command.feature {
return Builder(title: command.name, customCommand: command)
}
return nil
}
return [Builder(title: "New Chat", customCommand: nil)] + customCommands
}
@MainActor
public init(service: ChatService = .init(), store: StoreOf<ChatTabItem>) {
self.service = service
chat = .init(initialState: .init(), reducer: Chat(service: service))
viewStore = .init(chat)
super.init(store: store)
}
public func start() {
chatTabViewStore.send(.updateTitle("Chat"))
chatTabViewStore.publisher.focusTrigger.removeDuplicates().sink { [weak self] _ in
Task { @MainActor [weak self] in
self?.viewStore.send(.focusOnTextField)
}
}.store(in: &cancellable)
service.$systemPrompt.removeDuplicates().sink { [weak self] _ in
Task { @MainActor [weak self] in
self?.chatTabViewStore.send(.tabContentUpdated)
}
}.store(in: &cancellable)
service.$extraSystemPrompt.removeDuplicates().sink { [weak self] _ in
Task { @MainActor [weak self] in
self?.chatTabViewStore.send(.tabContentUpdated)
}
}.store(in: &cancellable)
viewStore.publisher.map(\.title).removeDuplicates().sink { [weak self] title in
Task { @MainActor [weak self] in
self?.chatTabViewStore.send(.updateTitle(title))
}
}.store(in: &cancellable)
viewStore.publisher.removeDuplicates().sink { [weak self] _ in
Task { @MainActor [weak self] in
self?.chatTabViewStore.send(.tabContentUpdated)
}
}.store(in: &cancellable)
}
}