Skip to content

Commit f7c99b0

Browse files
committed
Support setting extra system prompt for chat with selection
1 parent fc9d516 commit f7c99b0

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

Copilot for Xcode/CustomCommandView.swift

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ struct CustomCommandView: View {
6666
Button(action: {
6767
editingCommand = .init(isNew: true, command: CustomCommand(
6868
name: "New Command",
69-
feature: .chatWithSelection(prompt: "Tell me about the code.")
69+
feature: .chatWithSelection(
70+
extraSystemPrompt: nil,
71+
prompt: "Tell me about the code."
72+
)
7073
))
7174
}) {
7275
Image(systemName: "plus.circle.fill")
@@ -163,10 +166,10 @@ struct EditCustomCommandView: View {
163166
originalName = editingCommand.wrappedValue?.command.name ?? ""
164167
name = originalName
165168
switch editingCommand.wrappedValue?.command.feature {
166-
case let .chatWithSelection(prompt):
169+
case let .chatWithSelection(extraSystemPrompt, prompt):
167170
commandType = .chatWithSelection
168171
self.prompt = prompt ?? ""
169-
systemPrompt = ""
172+
systemPrompt = extraSystemPrompt ?? ""
170173
continuousMode = false
171174
case let .customChat(systemPrompt, prompt):
172175
commandType = .customChat
@@ -208,12 +211,13 @@ struct EditCustomCommandView: View {
208211

209212
switch commandType {
210213
case .chatWithSelection:
214+
systemPromptTextField(title: "Extra System Prompt")
211215
promptTextField
212216
case .promptToCode:
213217
promptTextField
214218
continuousModeToggle
215219
case .customChat:
216-
systemPromptTextField
220+
systemPromptTextField()
217221
promptTextField
218222
}
219223
}
@@ -235,7 +239,10 @@ struct EditCustomCommandView: View {
235239
feature: {
236240
switch commandType {
237241
case .chatWithSelection:
238-
return .chatWithSelection(prompt: prompt)
242+
return .chatWithSelection(
243+
extraSystemPrompt: systemPrompt,
244+
prompt: prompt
245+
)
239246
case .promptToCode:
240247
return .promptToCode(prompt: prompt, continuousMode: continuousMode)
241248
case .customChat:
@@ -259,7 +266,6 @@ struct EditCustomCommandView: View {
259266
}
260267
} else {
261268
Button("Update") {
262-
guard let command = editingCommand?.command else { return }
263269
guard !settings.illegalNames.contains(newCommand.name)
264270
|| newCommand.name == originalName
265271
else {
@@ -288,7 +294,7 @@ struct EditCustomCommandView: View {
288294
}
289295
}
290296
.padding()
291-
.frame(minWidth: 500)
297+
.frame(width: 600)
292298
}
293299

294300
var promptTextField: some View {
@@ -301,9 +307,9 @@ struct EditCustomCommandView: View {
301307
}
302308
}
303309

304-
var systemPromptTextField: some View {
310+
func systemPromptTextField(title: String? = nil) -> some View {
305311
if #available(macOS 13.0, *) {
306-
return TextField("System Prompt", text: $systemPrompt, axis: .vertical)
312+
return TextField(title ?? "System Prompt", text: $systemPrompt, axis: .vertical)
307313
.multilineTextAlignment(.leading)
308314
.lineLimit(4, reservesSpace: true)
309315
} else {
@@ -323,7 +329,10 @@ struct CustomCommandView_Preview: PreviewProvider {
323329
CustomCommandView(
324330
isOpen: .constant(true),
325331
settings: .init(customCommands: .init(wrappedValue: [
326-
.init(name: "Explain Code", feature: .chatWithSelection(prompt: "Hello")),
332+
.init(
333+
name: "Explain Code",
334+
feature: .chatWithSelection(extraSystemPrompt: nil, prompt: "Hello")
335+
),
327336
.init(
328337
name: "Refactor Code",
329338
feature: .promptToCode(prompt: "Refactor", continuousMode: false)

Core/Sources/Preferences/CustomCommand.swift

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

Core/Sources/Preferences/Keys.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ public extension UserDefaultPreferenceKeys {
215215
.init(
216216
name: "Explain Selection",
217217
feature: .chatWithSelection(
218+
extraSystemPrompt: nil,
218219
prompt: "Explain the code concisely, do not interpret or translate it."
219220
)
220221
),

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
235235
try await startChatWithSelection(
236236
editor: editor,
237237
specifiedSystemPrompt: nil,
238+
extraSystemPrompt: nil,
238239
sendingMessageImmediately: nil
239240
)
240241
} catch {
@@ -278,16 +279,18 @@ extension WindowBaseCommandHandler {
278279
else { throw CommandNotFoundError() }
279280

280281
switch command.feature {
281-
case let .chatWithSelection(prompt):
282+
case let .chatWithSelection(extraSystemPrompt, prompt):
282283
try await startChatWithSelection(
283284
editor: editor,
284285
specifiedSystemPrompt: nil,
286+
extraSystemPrompt: extraSystemPrompt,
285287
sendingMessageImmediately: prompt
286288
)
287289
case let .customChat(systemPrompt, prompt):
288290
try await startChatWithSelection(
289291
editor: editor,
290292
specifiedSystemPrompt: systemPrompt,
293+
extraSystemPrompt: nil,
291294
sendingMessageImmediately: prompt
292295
)
293296
case let .promptToCode(prompt, continuousMode):
@@ -354,6 +357,7 @@ extension WindowBaseCommandHandler {
354357
private func startChatWithSelection(
355358
editor: EditorContent,
356359
specifiedSystemPrompt: String?,
360+
extraSystemPrompt: String?,
357361
sendingMessageImmediately: String?
358362
) async throws {
359363
presenter.markAsProcessing(true)
@@ -369,7 +373,7 @@ extension WindowBaseCommandHandler {
369373
return editor.selectedCode(in: selection)
370374
}()
371375

372-
let systemPrompt = specifiedSystemPrompt ?? {
376+
var systemPrompt = specifiedSystemPrompt ?? {
373377
if code.isEmpty {
374378
return """
375379
\(language.isEmpty ? "" : "You must always reply in \(language)")
@@ -384,6 +388,10 @@ extension WindowBaseCommandHandler {
384388
```
385389
"""
386390
}()
391+
392+
if let extraSystemPrompt {
393+
systemPrompt += "\n\(extraSystemPrompt)"
394+
}
387395

388396
let chat = WidgetDataSource.shared.createChatIfNeeded(for: fileURL)
389397

0 commit comments

Comments
 (0)