Skip to content

Commit 4d30165

Browse files
committed
Tweak prompts
1 parent 8350266 commit 4d30165

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import CopilotModel
22
import CopilotService
33
import Environment
44
import Foundation
5+
import LanguageServerProtocol
56
import Logger
67
import OpenAIService
78
import SuggestionInjector
89
import SuggestionWidget
910
import XPCShared
10-
import LanguageServerProtocol
1111

1212
@ServiceActor
1313
struct WindowBaseCommandHandler: SuggestionCommandHandler {
@@ -176,7 +176,7 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
176176
}
177177
return nil
178178
}
179-
179+
180180
func prepareCache(editor: EditorContent) async throws -> UpdatedContent? {
181181
let fileURL = try await Environment.fetchCurrentFileURL()
182182
let (_, filespace) = try await Workspace
@@ -211,12 +211,13 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
211211
let endpoint = UserDefaults.shared.value(for: \.chatGPTEndpoint)
212212
let model = UserDefaults.shared.value(for: \.chatGPTModel)
213213
let language = UserDefaults.shared.value(for: \.chatGPTLanguage)
214+
let codeLanguage = languageIdentifierFromFileURL(fileURL)
214215
guard let selection = editor.selections.last else { return }
215216

216217
let service = ChatGPTService(
217218
systemPrompt: """
218-
You are a code explanation engine, you can only explain the code concisely, do not interpret or translate it
219-
Reply in \(language.isEmpty ? "" : "in \(language)")
219+
\(language.isEmpty ? "" : "You must always reply in \(language)")
220+
You are a code explanation engine, you can only explain the code concisely, do not interpret or translate it.
220221
""",
221222
apiKey: UserDefaults.shared.value(for: \.openAIAPIKey),
222223
endpoint: endpoint.isEmpty ? nil : endpoint,
@@ -228,7 +229,11 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
228229
let code = editor.selectedCode(in: selection)
229230
Task {
230231
try? await service.send(
231-
content: removeContinuousSpaces(from: code),
232+
content: """
233+
```\(codeLanguage?.rawValue ?? "")
234+
\(removeContinuousSpaces(from: code))
235+
```
236+
""",
232237
summary: "Explain selected code from `\(selection.start.line + 1):\(selection.start.character + 1)` to `\(selection.end.line + 1):\(selection.end.character + 1)`."
233238
)
234239
}
@@ -255,6 +260,7 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
255260
let endpoint = UserDefaults.shared.value(for: \.chatGPTEndpoint)
256261
let model = UserDefaults.shared.value(for: \.chatGPTModel)
257262
let language = UserDefaults.shared.value(for: \.chatGPTLanguage)
263+
let codeLanguage = languageIdentifierFromFileURL(fileURL)
258264

259265
let code = {
260266
guard let selection = editor.selections.last,
@@ -265,16 +271,14 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
265271
let prompt = {
266272
if code.isEmpty {
267273
return """
268-
You are a senior programmer, you will answer my questions concisely \(
269-
language.isEmpty ? "" : "in \(language)"
270-
)
274+
\(language.isEmpty ? "" : "You must always reply in \(language)")
275+
You are a senior programmer, you will answer my questions concisely. If you are replying with code, embed the code in a code block in markdown.
271276
"""
272277
}
273278
return """
274-
You are a senior programmer, you will answer my questions concisely in \(
275-
language.isEmpty ? "" : "in \(language)"
276-
) about the code
277-
```
279+
\(language.isEmpty ? "" : "You must always reply in \(language)")
280+
You are a senior programmer, you will answer my questions concisely about the code below, or modify it according to my requests. When you receive a modification request, reply with the modified code in a code block.
281+
```\(codeLanguage?.rawValue ?? "")
278282
\(removeContinuousSpaces(from: code))
279283
```
280284
"""

0 commit comments

Comments
 (0)