Skip to content

Commit e3df057

Browse files
committed
Set as system prompt
1 parent c409699 commit e3df057

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

Core/Sources/ChatService/ChatService.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public final class ChatService: ObservableObject {
6161
await pluginController.cancel()
6262
await chatGPTService.clearHistory()
6363
}
64-
64+
6565
public func resetPrompt() async {
6666
systemPrompt = defaultSystemPrompt
6767
extraSystemPrompt = ""
@@ -79,6 +79,19 @@ public final class ChatService: ObservableObject {
7979
}
8080
}
8181

82+
public func setMessageAsExtraPrompt(id: String) async {
83+
if let message = (await chatGPTService.history).first(where: { $0.id == id }) {
84+
mutateExtraSystemPrompt(message.content)
85+
await mutateHistory { history in
86+
history.append(.init(
87+
role: .assistant,
88+
content: "",
89+
summary: "System prompt updated"
90+
))
91+
}
92+
}
93+
}
94+
8295
/// Setting it to `nil` to reset the system prompt
8396
public func mutateSystemPrompt(_ newPrompt: String?) {
8497
systemPrompt = newPrompt ?? defaultSystemPrompt

Core/Sources/Service/GUI/ChatProvider+Service.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ extension ChatProvider {
9292
await commandHandler.handleCustomCommand(command)
9393
}
9494
}
95+
96+
onSetAsExtraPrompt = { id in
97+
Task {
98+
await service.setMessageAsExtraPrompt(id: id)
99+
}
100+
}
95101
}
96102
}
97103

Core/Sources/SuggestionWidget/ChatProvider.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Preferences
33
import SwiftUI
44

55
public final class ChatProvider: ObservableObject {
6+
public typealias MessageID = String
67
let id = UUID()
78
@Published public var history: [ChatMessage] = []
89
@Published public var isReceivingMessage = false
@@ -13,10 +14,11 @@ public final class ChatProvider: ObservableObject {
1314
public var onClear: () -> Void
1415
public var onClose: () -> Void
1516
public var onSwitchContext: () -> Void
16-
public var onDeleteMessage: (String) -> Void
17-
public var onResendMessage: (String) -> Void
17+
public var onDeleteMessage: (MessageID) -> Void
18+
public var onResendMessage: (MessageID) -> Void
1819
public var onResetPrompt: () -> Void
1920
public var onRunCustomCommand: (CustomCommand) -> Void = { _ in }
21+
public var onSetAsExtraPrompt: (MessageID) -> Void
2022

2123
public init(
2224
history: [ChatMessage] = [],
@@ -26,10 +28,11 @@ public final class ChatProvider: ObservableObject {
2628
onClear: @escaping () -> Void = {},
2729
onClose: @escaping () -> Void = {},
2830
onSwitchContext: @escaping () -> Void = {},
29-
onDeleteMessage: @escaping (String) -> Void = { _ in },
30-
onResendMessage: @escaping (String) -> Void = { _ in },
31+
onDeleteMessage: @escaping (MessageID) -> Void = { _ in },
32+
onResendMessage: @escaping (MessageID) -> Void = { _ in },
3133
onResetPrompt: @escaping () -> Void = {},
32-
onRunCustomCommand: @escaping (CustomCommand) -> Void = { _ in }
34+
onRunCustomCommand: @escaping (CustomCommand) -> Void = { _ in },
35+
onSetAsExtraPrompt: @escaping (MessageID) -> Void = { _ in }
3336
) {
3437
self.history = history
3538
self.isReceivingMessage = isReceivingMessage
@@ -42,19 +45,21 @@ public final class ChatProvider: ObservableObject {
4245
self.onResendMessage = onResendMessage
4346
self.onResetPrompt = onResetPrompt
4447
self.onRunCustomCommand = onRunCustomCommand
48+
self.onSetAsExtraPrompt = onSetAsExtraPrompt
4549
}
4650

4751
public func send(_ message: String) { onMessageSend(message) }
4852
public func stop() { onStop() }
4953
public func clear() { onClear() }
5054
public func close() { onClose() }
5155
public func switchContext() { onSwitchContext() }
52-
public func deleteMessage(id: String) { onDeleteMessage(id) }
53-
public func resendMessage(id: String) { onResendMessage(id) }
56+
public func deleteMessage(id: MessageID) { onDeleteMessage(id) }
57+
public func resendMessage(id: MessageID) { onResendMessage(id) }
5458
public func resetPrompt() { onResetPrompt() }
5559
public func triggerCustomCommand(_ command: CustomCommand) {
5660
onRunCustomCommand(command)
5761
}
62+
public func setAsExtraPrompt(id: MessageID) { onSetAsExtraPrompt(id) }
5863
}
5964

6065
public struct ChatMessage: Equatable {

Core/Sources/SuggestionWidget/SuggestionPanelContent/ChatPanel.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ private struct UserMessage: View {
177177
Button("Send Again") {
178178
chat.resendMessage(id: id)
179179
}
180+
181+
Button("Set as Extra System Prompt") {
182+
chat.setAsExtraPrompt(id: id)
183+
}
180184

181185
Divider()
182186

@@ -230,6 +234,10 @@ private struct BotMessage: View {
230234
NSPasteboard.general.clearContents()
231235
NSPasteboard.general.setString(text, forType: .string)
232236
}
237+
238+
Button("Set as Extra System Prompt") {
239+
chat.setAsExtraPrompt(id: id)
240+
}
233241

234242
Divider()
235243

0 commit comments

Comments
 (0)