Skip to content

Commit 383fad4

Browse files
committed
Add settings for language and max token
1 parent 1e293de commit 383fad4

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

Copilot for Xcode/OpenAIView.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ final class OpenAIViewSettings: ObservableObject {
88
@AppStorage(\.openAIAPIKey) var openAIAPIKey: String
99
@AppStorage(\.chatGPTModel) var chatGPTModel: String
1010
@AppStorage(\.chatGPTEndpoint) var chatGPTEndpoint: String
11+
@AppStorage(\.chatGPTLanguage) var chatGPTLanguage: String
12+
@AppStorage(\.chatGPTMaxToken) var chatGPTMaxToken: Int
1113
init() {}
1214
}
1315

@@ -63,6 +65,29 @@ struct OpenAIView: View {
6365
EmptyView()
6466
}.textFieldStyle(.copilot)
6567
}
68+
69+
HStack {
70+
Text("Reply in Language")
71+
TextField(
72+
text: $settings.chatGPTLanguage,
73+
prompt: Text("English")
74+
) {
75+
EmptyView()
76+
}.textFieldStyle(.copilot)
77+
}
78+
79+
HStack {
80+
Text("Max Token")
81+
TextField(
82+
text: .init(get: {
83+
String(settings.chatGPTMaxToken)
84+
}, set: { newValue in
85+
settings.chatGPTMaxToken = Int(newValue) ?? 0
86+
})
87+
) {
88+
EmptyView()
89+
}.textFieldStyle(.copilot)
90+
}
6691
}
6792
}
6893
}

Core/Sources/Preferences/Keys.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,18 @@ public struct UserDefaultPreferenceKeys {
8585
}
8686

8787
public var chatGPTModel: ChatGPTModel { .init() }
88+
89+
public struct ChatGPTMaxToken: UserDefaultPreferenceKey {
90+
public let defaultValue = 2048
91+
public let key = "ChatGPTMaxToken"
92+
}
93+
94+
public var chatGPTMaxToken: ChatGPTMaxToken { .init() }
95+
96+
public struct ChatGPTLanguage: UserDefaultPreferenceKey {
97+
public let defaultValue = ""
98+
public let key = "ChatGPTLanguage"
99+
}
100+
101+
public var chatGPTLanguage: ChatGPTLanguage { .init() }
88102
}

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,23 +194,32 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
194194
let fileURL = try await Environment.fetchCurrentFileURL()
195195
let endpoint = UserDefaults.shared.value(for: \.chatGPTEndpoint)
196196
let model = UserDefaults.shared.value(for: \.chatGPTModel)
197+
let language = UserDefaults.shared.value(for: \.chatGPTLanguage)
197198
guard let selection = editor.selections.last else { return }
199+
198200
let service = ChatGPTService(
199201
systemPrompt: """
200-
You are a code explanation engine, you can only explain the code, do not interpret or translate it. Reply in Chinese
202+
You are a code explanation engine, you can only explain the code, do not interpret or translate it. Reply in \(language.isEmpty ? language : "English")
201203
""",
202204
apiKey: UserDefaults.shared.value(for: \.openAIAPIKey),
203205
endpoint: endpoint.isEmpty ? nil : endpoint,
204206
model: model.isEmpty ? nil : model,
205-
temperature: 1,
206-
maxToken: 2048
207+
temperature: 0.6,
208+
maxToken: UserDefaults.shared.value(for: \.chatGPTMaxToken)
207209
)
208-
210+
209211
let code = editor.selectedCode(in: selection)
210212
Task {
211-
try? await service.send(content: code, summary: "Explain selected code.")
213+
try? await service.send(
214+
content: removeContinousSpaces(from: code),
215+
summary: "Explain selected code from \(selection.start.line):\(selection.start.character) to \(selection.end.line):\(selection.end.character)."
216+
)
212217
}
213-
218+
214219
presenter.presentChatGPTConversation(service, fileURL: fileURL)
215220
}
216221
}
222+
223+
func removeContinousSpaces(from string: String) -> String {
224+
return string.replacingOccurrences(of: " +", with: " ", options: .regularExpression)
225+
}

0 commit comments

Comments
 (0)