@@ -194,15 +194,14 @@ 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)
197+ var language = UserDefaults . shared. value ( for: \. chatGPTLanguage)
198+ if language. isEmpty { language = " English " }
198199 guard let selection = editor. selections. last else { return }
199200
200201 let service = ChatGPTService (
201202 systemPrompt: """
202- You are a code explanation engine, you can only explain the code concisely, do not interpret or translate it. Reply in \(
203- language
204- . isEmpty ? language : " English "
205- )
203+ You are a code explanation engine, you can only explain the code concisely, do not interpret or translate it
204+ Reply in \( language)
206205 """ ,
207206 apiKey: UserDefaults . shared. value ( for: \. openAIAPIKey) ,
208207 endpoint: endpoint. isEmpty ? nil : endpoint,
@@ -214,15 +213,82 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
214213 let code = editor. selectedCode ( in: selection)
215214 Task {
216215 try ? await service. send (
217- content: removeContinousSpaces ( from: code) ,
216+ content: removeContinuousSpaces ( from: code) ,
218217 summary: " Explain selected code from ` \( selection. start. line + 1 ) : \( selection. start. character + 1 ) ` to ` \( selection. end. line + 1 ) : \( selection. end. character + 1 ) `. "
219218 )
220219 }
221220
222221 presenter. presentChatGPTConversation ( service, fileURL: fileURL)
223222 }
223+
224+ func chatWithSelection( editor: EditorContent ) async throws -> UpdatedContent ? {
225+ Task {
226+ do {
227+ try await _chatWithSelection ( editor: editor)
228+ } catch {
229+ presenter. presentError ( error)
230+ }
231+ }
232+ return nil
233+ }
234+
235+ private func _chatWithSelection( editor: EditorContent ) async throws {
236+ presenter. markAsProcessing ( true )
237+ defer { presenter. markAsProcessing ( false ) }
238+
239+ let fileURL = try await Environment . fetchCurrentFileURL ( )
240+ let endpoint = UserDefaults . shared. value ( for: \. chatGPTEndpoint)
241+ let model = UserDefaults . shared. value ( for: \. chatGPTModel)
242+ var language = UserDefaults . shared. value ( for: \. chatGPTLanguage)
243+ if language. isEmpty { language = " English " }
244+
245+ let code = {
246+ guard let selection = editor. selections. last,
247+ selection. start != selection. end else { return " " }
248+ return editor. selectedCode ( in: selection)
249+ } ( )
250+
251+ let prompt = {
252+ if code. isEmpty {
253+ return """
254+ You are a senior programmer, you will answer my questions concisely in \( language)
255+ """
256+ }
257+ return """
258+ You are a senior programmer, you will answer my questions concisely in \(
259+ language
260+ ) about the code
261+ ```
262+ \( removeContinuousSpaces ( from: code) )
263+ ```
264+ """
265+ } ( )
266+
267+ let service = ChatGPTService (
268+ systemPrompt: prompt,
269+ apiKey: UserDefaults . shared. value ( for: \. openAIAPIKey) ,
270+ endpoint: endpoint. isEmpty ? nil : endpoint,
271+ model: model. isEmpty ? nil : model,
272+ temperature: 1 ,
273+ maxToken: UserDefaults . shared. value ( for: \. chatGPTMaxToken)
274+ )
275+
276+ Task {
277+ if !code. isEmpty, let selection = editor. selections. last {
278+ await service. mutateHistory { history in
279+ history. append ( . init(
280+ role: . user,
281+ content: " " ,
282+ summary: " Chat about selected code from ` \( selection. start. line + 1 ) : \( selection. start. character + 1 ) ` to ` \( selection. end. line + 1 ) : \( selection. end. character) `. \n The code will persisted in the conversation. "
283+ ) )
284+ }
285+ }
286+ }
287+
288+ presenter. presentChatGPTConversation ( service, fileURL: fileURL)
289+ }
224290}
225291
226- func removeContinousSpaces ( from string: String ) -> String {
292+ func removeContinuousSpaces ( from string: String ) -> String {
227293 return string. replacingOccurrences ( of: " + " , with: " " , options: . regularExpression)
228294}
0 commit comments