Skip to content

Commit aa6baa6

Browse files
committed
Add more settings to custom command
1 parent ed92137 commit aa6baa6

File tree

8 files changed

+91
-38
lines changed

8 files changed

+91
-38
lines changed

Core/Sources/HostApp/CustomCommandView.swift

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ struct CustomCommandView: View {
121121
name: "New Command",
122122
feature: .chatWithSelection(
123123
extraSystemPrompt: nil,
124-
prompt: "Tell me about the code."
124+
prompt: "Tell me about the code.",
125+
useExtraSystemPrompt: false
125126
)
126127
))
127128
}) {
@@ -155,8 +156,10 @@ struct EditCustomCommandView: View {
155156
@State var name: String
156157
@State var prompt: String
157158
@State var systemPrompt: String
159+
@State var usePrompt: Bool
158160
@State var continuousMode: Bool
159161
@State var editingContentInFullScreen: Binding<String>?
162+
@State var generatingPromptToCodeDescription: Bool
160163

161164
enum CommandType: Int, CaseIterable {
162165
case chatWithSelection
@@ -173,26 +176,34 @@ struct EditCustomCommandView: View {
173176
originalName = editingCommand.wrappedValue?.command.name ?? ""
174177
name = originalName
175178
switch editingCommand.wrappedValue?.command.feature {
176-
case let .chatWithSelection(extraSystemPrompt, prompt):
179+
case let .chatWithSelection(extraSystemPrompt, prompt, useExtraSystemPrompt):
177180
commandType = .chatWithSelection
178181
self.prompt = prompt ?? ""
179182
systemPrompt = extraSystemPrompt ?? ""
183+
usePrompt = useExtraSystemPrompt ?? true
180184
continuousMode = false
185+
generatingPromptToCodeDescription = true
181186
case let .customChat(systemPrompt, prompt):
182187
commandType = .customChat
183188
self.systemPrompt = systemPrompt ?? ""
184189
self.prompt = prompt ?? ""
190+
usePrompt = false
185191
continuousMode = false
186-
case let .promptToCode(extraSystemPrompt, prompt, continuousMode):
192+
generatingPromptToCodeDescription = true
193+
case let .promptToCode(extraSystemPrompt, prompt, continuousMode, generateDescription):
187194
commandType = .promptToCode
188195
self.prompt = prompt ?? ""
189196
systemPrompt = extraSystemPrompt ?? ""
197+
usePrompt = false
190198
self.continuousMode = continuousMode ?? false
199+
generatingPromptToCodeDescription = generateDescription ?? true
191200
case .none:
192201
commandType = .chatWithSelection
193202
prompt = ""
194203
systemPrompt = ""
195204
continuousMode = false
205+
usePrompt = true
206+
generatingPromptToCodeDescription = true
196207
}
197208
}
198209

@@ -218,14 +229,15 @@ struct EditCustomCommandView: View {
218229

219230
switch commandType {
220231
case .chatWithSelection:
221-
systemPromptTextField(title: "Extra System Prompt")
232+
systemPromptTextField(title: "Extra System Prompt", hasToggle: true)
222233
promptTextField
223234
case .promptToCode:
224235
continuousModeToggle
225-
systemPromptTextField(title: "Extra System Prompt")
236+
generateDescriptionToggle
237+
systemPromptTextField(title: "Extra System Prompt", hasToggle: false)
226238
promptTextField
227239
case .customChat:
228-
systemPromptTextField()
240+
systemPromptTextField(hasToggle: false)
229241
promptTextField
230242
}
231243
}.padding()
@@ -253,16 +265,21 @@ struct EditCustomCommandView: View {
253265
case .chatWithSelection:
254266
return .chatWithSelection(
255267
extraSystemPrompt: systemPrompt,
256-
prompt: prompt
268+
prompt: prompt,
269+
useExtraSystemPrompt: usePrompt
257270
)
258271
case .promptToCode:
259272
return .promptToCode(
260273
extraSystemPrompt: systemPrompt,
261274
prompt: prompt,
262-
continuousMode: continuousMode
275+
continuousMode: continuousMode,
276+
generateDescription: generatingPromptToCodeDescription
263277
)
264278
case .customChat:
265-
return .customChat(systemPrompt: systemPrompt, prompt: prompt)
279+
return .customChat(
280+
systemPrompt: systemPrompt,
281+
prompt: prompt
282+
)
266283
}
267284
}()
268285
)
@@ -354,9 +371,13 @@ struct EditCustomCommandView: View {
354371
}
355372

356373
@ViewBuilder
357-
func systemPromptTextField(title: String? = nil) -> some View {
374+
func systemPromptTextField(title: String? = nil, hasToggle: Bool) -> some View {
358375
VStack(alignment: .leading, spacing: 4) {
359-
Text(title ?? "System Prompt")
376+
if hasToggle {
377+
Toggle(title ?? "System Prompt", isOn: $usePrompt)
378+
} else {
379+
Text(title ?? "System Prompt")
380+
}
360381
editableText($systemPrompt)
361382
}
362383
.padding(.vertical, 4)
@@ -365,6 +386,10 @@ struct EditCustomCommandView: View {
365386
var continuousModeToggle: some View {
366387
Toggle("Continuous Mode", isOn: $continuousMode)
367388
}
389+
390+
var generateDescriptionToggle: some View {
391+
Toggle("Generate Description", isOn: $generatingPromptToCodeDescription)
392+
}
368393

369394
func editableText(_ binding: Binding<String>) -> some View {
370395
Button(action: {
@@ -407,21 +432,30 @@ struct CustomCommandView_Preview: PreviewProvider {
407432
editingCommand: .init(isNew: false, command: .init(
408433
commandId: "1",
409434
name: "Explain Code",
410-
feature: .chatWithSelection(extraSystemPrompt: nil, prompt: "Hello")
435+
feature: .chatWithSelection(
436+
extraSystemPrompt: nil,
437+
prompt: "Hello",
438+
useExtraSystemPrompt: false
439+
)
411440
)),
412441
settings: .init(customCommands: .init(wrappedValue: [
413442
.init(
414443
commandId: "1",
415444
name: "Explain Code",
416-
feature: .chatWithSelection(extraSystemPrompt: nil, prompt: "Hello")
445+
feature: .chatWithSelection(
446+
extraSystemPrompt: nil,
447+
prompt: "Hello",
448+
useExtraSystemPrompt: false
449+
)
417450
),
418451
.init(
419452
commandId: "2",
420453
name: "Refactor Code",
421454
feature: .promptToCode(
422455
extraSystemPrompt: nil,
423456
prompt: "Refactor",
424-
continuousMode: false
457+
continuousMode: false,
458+
generateDescription: true
425459
)
426460
),
427461
], "CustomCommandView_Preview"))
@@ -440,7 +474,8 @@ struct EditCustomCommandView_Preview: PreviewProvider {
440474
feature: .promptToCode(
441475
extraSystemPrompt: nil,
442476
prompt: "Hello",
443-
continuousMode: false
477+
continuousMode: false,
478+
generateDescription: true
444479
)
445480
)
446481
)),

Core/Sources/Preferences/CustomCommand.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ public struct CustomCommand: Codable {
66
///
77
/// Keep everything optional so nothing will break when the format changes.
88
public enum Feature: Codable {
9-
case promptToCode(extraSystemPrompt: String?, prompt: String?, continuousMode: Bool?)
10-
case chatWithSelection(extraSystemPrompt: String?, prompt: String?)
9+
case promptToCode(extraSystemPrompt: String?, prompt: String?, continuousMode: Bool?, generateDescription: Bool?)
10+
case chatWithSelection(extraSystemPrompt: String?, prompt: String?, useExtraSystemPrompt: Bool?)
1111
case customChat(systemPrompt: String?, prompt: String?)
1212
}
1313

Core/Sources/Preferences/Keys.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,19 @@ public extension UserDefaultPreferenceKeys {
211211
commandId: "BuiltInCustomCommandExplainSelection",
212212
name: "Explain Selection",
213213
feature: .chatWithSelection(
214-
extraSystemPrompt: nil,
215-
prompt: "Explain the code concisely, do not interpret or translate it."
214+
extraSystemPrompt: "",
215+
prompt: "Explain the selected code concisely, step-by-step.",
216+
useExtraSystemPrompt: true
216217
)
217218
),
218219
.init(
219220
commandId: "BuiltInCustomCommandAddDocumentationToSelection",
220221
name: "Add Documentation to Selection",
221222
feature: .promptToCode(
222-
extraSystemPrompt: nil,
223+
extraSystemPrompt: "",
223224
prompt: "Add documentation on top of the code. Use triple slash if the language supports it.",
224-
continuousMode: false
225+
continuousMode: false,
226+
generateDescription: true
225227
)
226228
),
227229
], key: "CustomCommands")

Core/Sources/PromptToCodeService/CopilotPromptToCodeAPI.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ final class CopilotPromptToCodeAPI: PromptToCodeAPI {
1919
projectRootURL: URL,
2020
fileURL: URL,
2121
allCode: String,
22-
extraSystemPrompt: String?
22+
extraSystemPrompt: String?,
23+
generateDescriptionRequirement: Bool?
2324
) async throws -> AsyncThrowingStream<(code: String, description: String), Error> {
2425
let copilotService = try GitHubCopilotSuggestionService(projectRootURL: projectRootURL)
2526
let _ = {

Core/Sources/PromptToCodeService/OpenAIPromptToCodeAPI.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
2222
projectRootURL: URL,
2323
fileURL: URL,
2424
allCode: String,
25-
extraSystemPrompt: String?
25+
extraSystemPrompt: String?,
26+
generateDescriptionRequirement: Bool?
2627
) async throws -> AsyncThrowingStream<(code: String, description: String), Error> {
2728
let userPreferredLanguage = UserDefaults.shared.value(for: \.chatGPTLanguage)
2829
let textLanguage = {
@@ -33,10 +34,12 @@ final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
3334
}
3435
return userPreferredLanguage.isEmpty ? "" : "in \(userPreferredLanguage)"
3536
}()
36-
37+
3738
let rule: String = {
3839
func generateDescription(index: Int) -> String {
39-
return UserDefaults.shared.value(for: \.promptToCodeGenerateDescription)
40+
let generateDescription = generateDescriptionRequirement ?? UserDefaults.shared
41+
.value(for: \.promptToCodeGenerateDescription)
42+
return generateDescription
4043
? """
4144
\(index). After the code block, write a clear and concise description \
4245
in 1-3 sentences about what you did in step 1\(textLanguage).
@@ -87,15 +90,15 @@ final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
8790
You are good at writing in \(language.rawValue).
8891
The active file is: \(fileURL.lastPathComponent).
8992
\(extraSystemPrompt ?? "")
90-
93+
9194
\(rule)
9295
"""
9396
} else {
9497
return """
9598
You are good at writing in \(language.rawValue).
9699
The active file is: \(fileURL.lastPathComponent).
97100
\(extraSystemPrompt ?? "")
98-
101+
99102
\(rule)
100103
"""
101104
}
@@ -105,15 +108,15 @@ final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
105108
You are a senior programer in writing in \(language.rawValue).
106109
The active file is: \(fileURL.lastPathComponent).
107110
\(extraSystemPrompt ?? "")
108-
111+
109112
\(rule)
110113
"""
111114
} else {
112115
return """
113116
You are a senior programer in writing in \(language.rawValue).
114117
The active file is: \(fileURL.lastPathComponent).
115118
\(extraSystemPrompt ?? "")
116-
119+
117120
\(rule)
118121
"""
119122
}
@@ -138,7 +141,7 @@ final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
138141
}
139142
}()
140143

141-
let secondMessage: String = """
144+
let secondMessage = """
142145
Requirements:###
143146
\(requirement)
144147
###

Core/Sources/PromptToCodeService/PromptToCodeService.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public final class PromptToCodeService: ObservableObject {
4949
public var fileURL: URL
5050
public var allCode: String
5151
public var extraSystemPrompt: String?
52+
public var generateDescriptionRequirement: Bool?
5253

5354
public init(
5455
code: String,
@@ -59,7 +60,8 @@ public final class PromptToCodeService: ObservableObject {
5960
projectRootURL: URL,
6061
fileURL: URL,
6162
allCode: String,
62-
extraSystemPrompt: String? = nil
63+
extraSystemPrompt: String? = nil,
64+
generateDescriptionRequirement: Bool?
6365
) {
6466
self.code = code
6567
self.selectionRange = selectionRange
@@ -71,6 +73,7 @@ public final class PromptToCodeService: ObservableObject {
7173
self.allCode = allCode
7274
self.history = .empty
7375
self.extraSystemPrompt = extraSystemPrompt
76+
self.generateDescriptionRequirement = generateDescriptionRequirement
7477
}
7578

7679
public func modifyCode(prompt: String) async throws {
@@ -92,7 +95,8 @@ public final class PromptToCodeService: ObservableObject {
9295
projectRootURL: projectRootURL,
9396
fileURL: fileURL,
9497
allCode: allCode,
95-
extraSystemPrompt: extraSystemPrompt
98+
extraSystemPrompt: extraSystemPrompt,
99+
generateDescriptionRequirement: generateDescriptionRequirement
96100
)
97101
for try await fragment in stream {
98102
code = fragment.code
@@ -145,7 +149,8 @@ protocol PromptToCodeAPI {
145149
projectRootURL: URL,
146150
fileURL: URL,
147151
allCode: String,
148-
extraSystemPrompt: String?
152+
extraSystemPrompt: String?,
153+
generateDescriptionRequirement: Bool?
149154
) async throws -> AsyncThrowingStream<(code: String, description: String), Error>
150155

151156
func stopResponding()

Core/Sources/Service/GUI/WidgetDataSource.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ final class WidgetDataSource {
9898
identSize: Int = 4,
9999
usesTabsForIndentation: Bool = false,
100100
extraSystemPrompt: String?,
101+
generateDescriptionRequirement: Bool?,
101102
name: String?
102103
) async -> PromptToCodeService {
103104
let build = {
@@ -110,7 +111,8 @@ final class WidgetDataSource {
110111
projectRootURL: projectURL,
111112
fileURL: url,
112113
allCode: allCode,
113-
extraSystemPrompt: extraSystemPrompt
114+
extraSystemPrompt: extraSystemPrompt,
115+
generateDescriptionRequirement: generateDescriptionRequirement
114116
)
115117
let provider = PromptToCodeProvider(
116118
service: service,

0 commit comments

Comments
 (0)