Skip to content

Commit c05df61

Browse files
committed
Update prompt to code to generate according to the extra system prompt
1 parent fc2a8a6 commit c05df61

File tree

5 files changed

+62
-22
lines changed

5 files changed

+62
-22
lines changed

Core/Sources/PromptToCodeService/CopilotPromptToCodeAPI.swift

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ final class CopilotPromptToCodeAPI: PromptToCodeAPI {
1818
requirement: String,
1919
projectRootURL: URL,
2020
fileURL: URL,
21-
allCode: String
21+
allCode: String,
22+
extraSystemPrompt: String?
2223
) async throws -> AsyncThrowingStream<(code: String, description: String), Error> {
2324
let copilotService = CopilotSuggestionService(projectRootURL: projectRootURL)
2425
let relativePath = {
@@ -36,14 +37,25 @@ final class CopilotPromptToCodeAPI: PromptToCodeAPI {
3637
return filePath
3738
}()
3839

39-
let comment = """
40-
// update the following code, \(requirement.split(separator: "\n").joined(separator: " ")).
41-
\(code.split(separator: "\n").map { "//\($0)" }.joined(separator: "\n"))
40+
func convertToComment(_ s: String) -> String {
41+
s.split(separator: "\n").map { "// \($0)" }.joined(separator: "\n")
42+
}
4243

44+
let comment = """
45+
// A file to refactor the following code
46+
//
47+
// Code:
48+
// ```
49+
\(convertToComment(code))
50+
// ```
51+
//
52+
// Requirements:
53+
\(convertToComment((extraSystemPrompt ?? "\n") + requirement))
54+
//
4355
4456
45-
// Path: \(relativePath)
4657
58+
// end of file
4759
"""
4860
let lineCount = comment.breakLines().count
4961

@@ -53,7 +65,7 @@ final class CopilotPromptToCodeAPI: PromptToCodeAPI {
5365
let result = try await copilotService.getCompletions(
5466
fileURL: fileURL,
5567
content: comment,
56-
cursorPosition: .init(line: lineCount - 4, character: 0),
68+
cursorPosition: .init(line: lineCount - 3, character: 0),
5769
tabSize: indentSize,
5870
indentSize: indentSize,
5971
usesTabsForIndentation: usesTabsForIndentation,

Core/Sources/PromptToCodeService/OpenAIPromptToCodeAPI.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
2020
requirement: String,
2121
projectRootURL: URL,
2222
fileURL: URL,
23-
allCode: String
23+
allCode: String,
24+
extraSystemPrompt: String?
2425
) async throws -> AsyncThrowingStream<(code: String, description: String), Error> {
2526
let userPreferredLanguage = UserDefaults.shared.value(for: \.chatGPTLanguage)
2627
let textLanguage = userPreferredLanguage.isEmpty ? "" : "in \(userPreferredLanguage)"
@@ -38,25 +39,33 @@ final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
3839
Please reply to me start with the code block, followed by a clear and concise description in 1-3 sentences about what you did \(
3940
textLanguage
4041
).
42+
43+
\(extraSystemPrompt ?? "")
4144
"""
4245
} else {
4346
return """
47+
# Description
48+
4449
You are a senior programer in writing code in \(language.rawValue).
4550
4651
Please mutate the following code fragment with my requirements. Keep the original indentation. Do not add comments unless told to.
4752
4853
Please reply to me start with the code block followed by a clear and concise description about what you did in 1-3 sentences \(
4954
textLanguage
5055
).
56+
57+
\(extraSystemPrompt ?? "")
5158
59+
# Code
60+
5261
```
5362
\(code)
5463
```
5564
"""
5665
}
5766
}()
5867

59-
let chatGPTService = ChatGPTService(systemPrompt: prompt, temperature: 0.5)
68+
let chatGPTService = ChatGPTService(systemPrompt: prompt, temperature: 0.3)
6069
service = chatGPTService
6170
let stream = try await chatGPTService.send(content: requirement)
6271
return .init { continuation in

Core/Sources/PromptToCodeService/PromptToCodeService.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public final class PromptToCodeService: ObservableObject {
5353
public var projectRootURL: URL
5454
public var fileURL: URL
5555
public var allCode: String
56+
public var extraSystemPrompt: String?
5657

5758
public init(
5859
code: String,
@@ -62,7 +63,8 @@ public final class PromptToCodeService: ObservableObject {
6263
usesTabsForIndentation: Bool,
6364
projectRootURL: URL,
6465
fileURL: URL,
65-
allCode: String
66+
allCode: String,
67+
extraSystemPrompt: String? = nil
6668
) {
6769
self.code = code
6870
self.selectionRange = selectionRange
@@ -73,6 +75,7 @@ public final class PromptToCodeService: ObservableObject {
7375
self.fileURL = fileURL
7476
self.allCode = allCode
7577
self.history = .empty
78+
self.extraSystemPrompt = extraSystemPrompt
7679
}
7780

7881
public func modifyCode(prompt: String) async throws {
@@ -93,7 +96,8 @@ public final class PromptToCodeService: ObservableObject {
9396
requirement: prompt,
9497
projectRootURL: projectRootURL,
9598
fileURL: fileURL,
96-
allCode: allCode
99+
allCode: allCode,
100+
extraSystemPrompt: extraSystemPrompt
97101
)
98102
for try await fragment in stream {
99103
code = fragment.code
@@ -145,7 +149,8 @@ protocol PromptToCodeAPI {
145149
requirement: String,
146150
projectRootURL: URL,
147151
fileURL: URL,
148-
allCode: String
152+
allCode: String,
153+
extraSystemPrompt: String?
149154
) async throws -> AsyncThrowingStream<(code: String, description: String), Error>
150155

151156
func stopResponding()

Core/Sources/Service/GUI/WidgetDataSource.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ final class WidgetDataSource {
9191
selectionRange: CursorRange,
9292
language: CopilotLanguage,
9393
identSize: Int = 4,
94-
usesTabsForIndentation: Bool = false
94+
usesTabsForIndentation: Bool = false,
95+
extraSystemPrompt: String?,
96+
name: String?
9597
) async -> PromptToCodeService {
9698
let build = {
9799
let service = PromptToCodeService(
@@ -102,7 +104,8 @@ final class WidgetDataSource {
102104
usesTabsForIndentation: usesTabsForIndentation,
103105
projectRootURL: projectURL,
104106
fileURL: url,
105-
allCode: allCode
107+
allCode: allCode,
108+
extraSystemPrompt: extraSystemPrompt
106109
)
107110
let provider = PromptToCodeProvider(
108111
service: service,

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,12 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
248248
func promptToCode(editor: EditorContent) async throws -> UpdatedContent? {
249249
Task {
250250
do {
251-
try await presentPromptToCode(editor: editor, prompt: nil, isContinuous: false)
251+
try await presentPromptToCode(
252+
editor: editor,
253+
extraSystemPrompt: nil,
254+
prompt: nil,
255+
isContinuous: false
256+
)
252257
} catch {
253258
presenter.presentError(error)
254259
}
@@ -293,9 +298,10 @@ extension WindowBaseCommandHandler {
293298
extraSystemPrompt: nil,
294299
sendingMessageImmediately: prompt
295300
)
296-
case let .promptToCode(_, prompt, continuousMode):
301+
case let .promptToCode(extraSystemPrompt, prompt, continuousMode):
297302
try await presentPromptToCode(
298303
editor: editor,
304+
extraSystemPrompt: extraSystemPrompt,
299305
prompt: prompt,
300306
isContinuous: continuousMode ?? false
301307
)
@@ -304,6 +310,7 @@ extension WindowBaseCommandHandler {
304310

305311
func presentPromptToCode(
306312
editor: EditorContent,
313+
extraSystemPrompt: String?,
307314
prompt: String?,
308315
isContinuous: Bool
309316
) async throws {
@@ -323,7 +330,7 @@ extension WindowBaseCommandHandler {
323330
guard var selection = editor.selections.last,
324331
selection.start != selection.end
325332
else { return ("", .cursor(editor.cursorPosition)) }
326-
333+
327334
let isMultipleLine = selection.start.line != selection.end.line
328335
let isSpaceOnlyBeforeStartPositionOnTheSameLine = {
329336
guard selection.start.line >= 0, selection.start.line < editor.lines.count else {
@@ -333,10 +340,12 @@ extension WindowBaseCommandHandler {
333340
guard selection.start.character > 0, selection.start.character < line.count else {
334341
return false
335342
}
336-
let substring = line[line.startIndex..<line.index(line.startIndex, offsetBy: selection.start.character)]
337-
return substring.allSatisfy({ $0.isWhitespace })
343+
let substring =
344+
line[line.startIndex..<line
345+
.index(line.startIndex, offsetBy: selection.start.character)]
346+
return substring.allSatisfy { $0.isWhitespace }
338347
}()
339-
348+
340349
if isMultipleLine || isSpaceOnlyBeforeStartPositionOnTheSameLine {
341350
// when there are multiple lines start from char 0 so that it can keep the
342351
// indentation.
@@ -357,11 +366,13 @@ extension WindowBaseCommandHandler {
357366
selectedCode: code,
358367
allCode: editor.content,
359368
selectionRange: selection,
360-
language: codeLanguage
369+
language: codeLanguage,
370+
extraSystemPrompt: extraSystemPrompt,
371+
name: nil
361372
)
362373

363374
promptToCode.isContinuous = isContinuous
364-
if let prompt {
375+
if let prompt, !prompt.isEmpty {
365376
Task { try await promptToCode.modifyCode(prompt: prompt) }
366377
}
367378

@@ -402,7 +413,7 @@ extension WindowBaseCommandHandler {
402413
```
403414
"""
404415
}()
405-
416+
406417
if let extraSystemPrompt {
407418
systemPrompt += "\n\(extraSystemPrompt)"
408419
}

0 commit comments

Comments
 (0)