Skip to content

Commit cb20e5b

Browse files
committed
Update prompt of prompt to code feature
1 parent b8ac026 commit cb20e5b

File tree

4 files changed

+162
-49
lines changed

4 files changed

+162
-49
lines changed

Core/Sources/HostApp/FeatureSettings/PromptToCodeSettingsView.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,29 @@ struct PromptToCodeSettingsView: View {
88
var suggestionCodeFontSize
99
@AppStorage(\.acceptSuggestionWithAccessibilityAPI)
1010
var acceptSuggestionWithAccessibilityAPI
11+
@AppStorage(\.promptToCodeGenerateDescription)
12+
var promptToCodeGenerateDescription
13+
@AppStorage(\.promptToCodeGenerateDescriptionInUserPreferredLanguage)
14+
var promptToCodeGenerateDescriptionInUserPreferredLanguage
1115
init() {}
1216
}
1317

1418
@StateObject var settings = Settings()
1519

1620
var body: some View {
1721
VStack(alignment: .center) {
22+
Form {
23+
Toggle(isOn: $settings.promptToCodeGenerateDescription) {
24+
Text("Generate Description")
25+
}
26+
27+
Toggle(isOn: $settings.promptToCodeGenerateDescriptionInUserPreferredLanguage) {
28+
Text("Generate Description in user preferred language")
29+
}
30+
}
31+
32+
Divider()
33+
1834
Text("Mirroring Settings of Suggestion Feature")
1935
.foregroundColor(.white)
2036
.padding(.vertical, 2)
@@ -41,7 +57,7 @@ struct PromptToCodeSettingsView: View {
4157

4258
Text("pt")
4359
}.disabled(true)
44-
60+
4561
Divider()
4662

4763
Toggle(isOn: $settings.acceptSuggestionWithAccessibilityAPI) {

Core/Sources/Preferences/Keys.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public struct UserDefaultPreferenceKeys {
1717
}
1818

1919
public var realtimeSuggestionToggle: RealtimeSuggestionToggle { .init() }
20-
20+
2121
// MARK: - Suggestion Feature Provider
22-
22+
2323
public struct SuggestionFeatureProviderKey: UserDefaultPreferenceKey {
2424
public let defaultValue: SuggestionFeatureProvider = .gitHubCopilot
2525
public let key = "SuggestionFeatureProvider"
2626
}
27-
27+
2828
public var suggestionFeatureProvider: SuggestionFeatureProviderKey { .init() }
2929

3030
// MARK: - Realtime Suggestion Debounce
@@ -261,6 +261,24 @@ public extension UserDefaultPreferenceKeys {
261261
var codeiumVerboseLog: CodeiumVerboseLog { .init() }
262262
}
263263

264+
public extension UserDefaultPreferenceKeys {
265+
struct PromptToCodeGenerateDescription: UserDefaultPreferenceKey {
266+
public let defaultValue = true
267+
public let key = "PromptToCodeGenerateDescription"
268+
}
269+
270+
var promptToCodeGenerateDescription: PromptToCodeGenerateDescription { .init() }
271+
272+
struct PromptToCodeGenerateDescriptionInUserPreferredLanguage: UserDefaultPreferenceKey {
273+
public let defaultValue = true
274+
public let key = "PromptToCodeGenerateDescriptionInUserPreferredLanguage"
275+
}
276+
277+
var promptToCodeGenerateDescriptionInUserPreferredLanguage: PromptToCodeGenerateDescriptionInUserPreferredLanguage {
278+
.init()
279+
}
280+
}
281+
264282
// MARK: - UI
265283

266284
public extension UserDefaultPreferenceKeys {

Core/Sources/PromptToCodeService/OpenAIPromptToCodeAPI.swift

Lines changed: 123 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import SuggestionModel
2-
import GitHubCopilotService
31
import Foundation
2+
import GitHubCopilotService
43
import OpenAIService
4+
import Preferences
5+
import SuggestionModel
56

67
final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
78
var service: (any ChatGPTServiceType)?
@@ -24,54 +25,137 @@ final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
2425
extraSystemPrompt: String?
2526
) async throws -> AsyncThrowingStream<(code: String, description: String), Error> {
2627
let userPreferredLanguage = UserDefaults.shared.value(for: \.chatGPTLanguage)
27-
let textLanguage = userPreferredLanguage.isEmpty ? "" : "in \(userPreferredLanguage)"
28-
29-
let prompt = {
30-
let indentRule = usesTabsForIndentation ? "\(indentSize) tabs" : "\(indentSize) spaces"
31-
if code.isEmpty {
32-
return """
33-
You are a senior programer in writing code in \(language.rawValue).
34-
35-
File url: \(fileURL)
36-
37-
\(extraSystemPrompt ?? "")
28+
let textLanguage = {
29+
if !UserDefaults.shared
30+
.value(for: \.promptToCodeGenerateDescriptionInUserPreferredLanguage)
31+
{
32+
return ""
33+
}
34+
return userPreferredLanguage.isEmpty ? "" : "in \(userPreferredLanguage)"
35+
}()
3836

39-
Please write a piece of code that meets my requirements. The indentation should be \(
40-
indentRule
41-
).
37+
let systemPrompt = {
38+
switch language {
39+
case .builtIn(.markdown), .plaintext:
40+
if code.isEmpty {
41+
return """
42+
You are good at writing in \(language.rawValue).
43+
You are editing file: \(fileURL.lastPathComponent).
44+
\(extraSystemPrompt ?? "")
45+
"""
46+
} else {
47+
return """
48+
You are good at writing in \(language.rawValue).
49+
You are editing file: \(fileURL.lastPathComponent).
50+
\(extraSystemPrompt ?? "")
51+
"""
52+
}
53+
default:
54+
if code.isEmpty {
55+
return """
56+
You are a senior programer in writing in \(language.rawValue).
57+
You are editing file: \(fileURL.lastPathComponent).
58+
\(extraSystemPrompt ?? "")
59+
"""
60+
} else {
61+
return """
62+
You are a senior programer in writing in \(language.rawValue).
63+
You are editing file: \(fileURL.lastPathComponent).
64+
\(extraSystemPrompt ?? "")
65+
"""
66+
}
67+
}
68+
}()
4269

43-
Please reply to me start with the code block, followed by a clear and concise description in 1-3 sentences about what you did \(
44-
textLanguage
45-
).
70+
let firstMessage: String? = {
71+
if code.isEmpty { return nil }
72+
switch language {
73+
case .builtIn(.markdown), .plaintext:
74+
return """
75+
Content:\"""
76+
\(code)
77+
\"""
4678
"""
47-
} else {
79+
default:
4880
return """
49-
# Description
50-
51-
You are a senior programer in writing code in \(language.rawValue).
52-
53-
File url: \(fileURL)
54-
55-
\(extraSystemPrompt ?? "")
56-
57-
Please mutate the following code fragment with my requirements. Keep the original indentation. Do not add comments unless told to.
58-
59-
Please reply to me start with the code block followed by a clear and concise description about what you did in 1-3 sentences \(
60-
textLanguage
61-
).
62-
63-
# Code
64-
65-
```\(language.rawValue)
81+
Code:```
6682
\(code)
6783
```
6884
"""
6985
}
7086
}()
7187

72-
let chatGPTService = ChatGPTService(systemPrompt: prompt, temperature: 0.3)
88+
let formattedRequirement = requirement.split(separator: "\n")
89+
.map { " \($0)" }
90+
.joined(separator: "\n")
91+
92+
let secondMessage: String = {
93+
func generateDescription(index: Int) -> String {
94+
return UserDefaults.shared.value(for: \.promptToCodeGenerateDescription)
95+
? """
96+
\(index). After the code block, write a clear and concise description \
97+
in 1-3 sentences about what you did in step 1\(textLanguage).
98+
\(index + 1). Reply with the result.
99+
"""
100+
: "\(index). Reply with the result."
101+
}
102+
switch language {
103+
case .builtIn(.markdown), .plaintext:
104+
if code.isEmpty {
105+
return """
106+
1. Write the content that meets my requirements.
107+
Requirements:\"""
108+
Write the content;
109+
\(formattedRequirement)
110+
\"""
111+
2. Embed the new content in a markdown code block.
112+
\(generateDescription(index: 3))
113+
"""
114+
} else {
115+
return """
116+
1. Update the content to meet my requirements.
117+
Requirements:\"""
118+
\(formattedRequirement)
119+
\"""
120+
2. Format the updated content to use the original indentation. Especially the first line.
121+
3. Embed the updated content in a markdown code block.
122+
\(generateDescription(index: 4))
123+
"""
124+
}
125+
default:
126+
if code.isEmpty {
127+
return """
128+
1. Write the code that meets my requirements.
129+
Requirements:\"""
130+
Write the code in \(language.rawValue);
131+
\(formattedRequirement)
132+
\"""
133+
2. Embed the code in a markdown code block.
134+
\(generateDescription(index: 3))
135+
"""
136+
} else {
137+
return """
138+
1. Update the code to meet my requirements.
139+
Requirements:\"""
140+
Update the code;
141+
\(formattedRequirement)
142+
\"""
143+
2. Format the updated code to use the original indentation. Especially the first line.
144+
3. Embed the updated code in a markdown code block.
145+
\(generateDescription(index: 4))
146+
"""
147+
}
148+
}
149+
}()
150+
151+
let chatGPTService = ChatGPTService(systemPrompt: systemPrompt, temperature: 0.3)
73152
service = chatGPTService
74-
let stream = try await chatGPTService.send(content: requirement)
153+
if let firstMessage {
154+
await chatGPTService.mutateHistory { history in
155+
history.append(.init(role: .user, content: firstMessage))
156+
}
157+
}
158+
let stream = try await chatGPTService.send(content: secondMessage)
75159
return .init { continuation in
76160
Task {
77161
var content = ""

Core/Sources/PromptToCodeService/PromptToCodeService.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ public final class PromptToCodeService: ObservableObject {
1010
return designatedPromptToCodeAPI
1111
}
1212

13-
switch UserDefaults.shared.value(for: \.promptToCodeFeatureProvider) {
14-
case .openAI:
15-
return OpenAIPromptToCodeAPI()
16-
case .githubCopilot:
17-
return CopilotPromptToCodeAPI()
18-
}
13+
return OpenAIPromptToCodeAPI()
1914
}
2015

2116
var runningAPI: PromptToCodeAPI?

0 commit comments

Comments
 (0)