forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatModelEdit.swift
More file actions
223 lines (199 loc) · 7.86 KB
/
ChatModelEdit.swift
File metadata and controls
223 lines (199 loc) · 7.86 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import AIModel
import ComposableArchitecture
import Dependencies
import Keychain
import OpenAIService
import Preferences
import SwiftUI
import Toast
struct ChatModelEdit: ReducerProtocol {
struct State: Equatable, Identifiable {
var id: String
@BindingState var name: String
@BindingState var format: ChatModel.Format
@BindingState var maxTokens: Int = 4000
@BindingState var supportsFunctionCalling: Bool = true
@BindingState var modelName: String = ""
@BindingState var ollamaKeepAlive: String = ""
@BindingState var apiVersion: String = ""
var apiKeyName: String { apiKeySelection.apiKeyName }
var baseURL: String { baseURLSelection.baseURL }
var isFullURL: Bool { baseURLSelection.isFullURL }
var availableModelNames: [String] = []
var availableAPIKeys: [String] = []
var isTesting = false
var suggestedMaxTokens: Int?
var apiKeySelection: APIKeySelection.State = .init()
var baseURLSelection: BaseURLSelection.State = .init()
}
enum Action: Equatable, BindableAction {
case binding(BindingAction<State>)
case appear
case saveButtonClicked
case cancelButtonClicked
case refreshAvailableModelNames
case testButtonClicked
case testSucceeded(String)
case testFailed(String)
case checkSuggestedMaxTokens
case apiKeySelection(APIKeySelection.Action)
case baseURLSelection(BaseURLSelection.Action)
}
var toast: (String, ToastType) -> Void {
@Dependency(\.namespacedToast) var toast
return {
toast($0, $1, "ChatModelEdit")
}
}
@Dependency(\.apiKeyKeychain) var keychain
var body: some ReducerProtocol<State, Action> {
BindingReducer()
Scope(state: \.apiKeySelection, action: /Action.apiKeySelection) {
APIKeySelection()
}
Scope(state: \.baseURLSelection, action: /Action.baseURLSelection) {
BaseURLSelection()
}
Reduce { state, action in
switch action {
case .appear:
return .run { send in
await send(.refreshAvailableModelNames)
await send(.checkSuggestedMaxTokens)
}
case .saveButtonClicked:
return .none
case .cancelButtonClicked:
return .none
case .testButtonClicked:
guard !state.isTesting else { return .none }
state.isTesting = true
let model = ChatModel(state: state)
return .run { send in
do {
let service = ChatGPTService(
configuration: UserPreferenceChatGPTConfiguration()
.overriding {
$0.model = model
}
)
let reply = try await service
.sendAndWait(content: "Respond with \"Test succeeded\"")
await send(.testSucceeded(reply ?? "No Message"))
let stream = try await service
.send(content: "Respond with \"Stream response is working\"")
var streamReply = ""
for try await chunk in stream {
streamReply += chunk
}
await send(.testSucceeded(streamReply))
} catch {
await send(.testFailed(error.localizedDescription))
}
}
case let .testSucceeded(message):
state.isTesting = false
toast(message.trimmingCharacters(in: .whitespacesAndNewlines), .info)
return .none
case let .testFailed(message):
state.isTesting = false
toast(message.trimmingCharacters(in: .whitespacesAndNewlines), .error)
return .none
case .refreshAvailableModelNames:
if state.format == .openAI {
state.availableModelNames = ChatGPTModel.allCases.map(\.rawValue)
}
return .none
case .checkSuggestedMaxTokens:
switch state.format {
case .openAI:
if let knownModel = ChatGPTModel(rawValue: state.modelName) {
state.suggestedMaxTokens = knownModel.maxToken
} else {
state.suggestedMaxTokens = nil
}
return .none
case .googleAI:
if let knownModel = GoogleGenerativeAIModel(rawValue: state.modelName) {
state.suggestedMaxTokens = knownModel.maxToken
} else {
state.suggestedMaxTokens = nil
}
return .none
case .claude:
if let knownModel = ClaudeChatCompletionsService
.KnownModel(rawValue: state.modelName)
{
state.suggestedMaxTokens = knownModel.contextWindow
} else {
state.suggestedMaxTokens = nil
}
return .none
default:
state.suggestedMaxTokens = nil
return .none
}
case .apiKeySelection:
return .none
case .baseURLSelection:
return .none
case .binding(\.$format):
return .run { send in
await send(.refreshAvailableModelNames)
await send(.checkSuggestedMaxTokens)
}
case .binding(\.$modelName):
return .run { send in
await send(.checkSuggestedMaxTokens)
}
case .binding:
return .none
}
}
}
}
extension ChatModelEdit.State {
init(model: ChatModel) {
self.init(
id: model.id,
name: model.name,
format: model.format,
maxTokens: model.info.maxTokens,
supportsFunctionCalling: model.info.supportsFunctionCalling,
modelName: model.info.modelName,
ollamaKeepAlive: model.info.ollamaInfo.keepAlive,
apiVersion: model.info.googleGenerativeAIInfo.apiVersion,
apiKeySelection: .init(
apiKeyName: model.info.apiKeyName,
apiKeyManagement: .init(availableAPIKeyNames: [model.info.apiKeyName])
),
baseURLSelection: .init(baseURL: model.info.baseURL, isFullURL: model.info.isFullURL)
)
}
}
extension ChatModel {
init(state: ChatModelEdit.State) {
self.init(
id: state.id,
name: state.name,
format: state.format,
info: .init(
apiKeyName: state.apiKeyName,
baseURL: state.baseURL.trimmingCharacters(in: .whitespacesAndNewlines),
isFullURL: state.isFullURL,
maxTokens: state.maxTokens,
supportsFunctionCalling: {
switch state.format {
case .googleAI, .ollama, .claude:
return false
case .azureOpenAI, .openAI, .openAICompatible:
return state.supportsFunctionCalling
}
}(),
modelName: state.modelName.trimmingCharacters(in: .whitespacesAndNewlines),
ollamaInfo: .init(keepAlive: state.ollamaKeepAlive),
googleGenerativeAIInfo: .init(apiVersion: state.apiVersion)
)
)
}
}