Skip to content

Commit c49c524

Browse files
committed
Make auto completion dynamic to registered plugins
1 parent b036757 commit c49c524

2 files changed

Lines changed: 28 additions & 23 deletions

File tree

Core/Sources/SuggestionWidget/ChatProvider.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public final class ChatProvider: ObservableObject {
77
let id = UUID()
88
@Published public var history: [ChatMessage] = []
99
@Published public var isReceivingMessage = false
10+
public var pluginIdentifiers: [String] = []
1011
public var systemPrompt = ""
1112
public var extraSystemPrompt = ""
1213
public var onMessageSend: (String) -> Void
@@ -23,6 +24,7 @@ public final class ChatProvider: ObservableObject {
2324
public init(
2425
history: [ChatMessage] = [],
2526
isReceivingMessage: Bool = false,
27+
pluginIdentifiers: [String] = [],
2628
onMessageSend: @escaping (String) -> Void = { _ in },
2729
onStop: @escaping () -> Void = {},
2830
onClear: @escaping () -> Void = {},
@@ -36,6 +38,7 @@ public final class ChatProvider: ObservableObject {
3638
) {
3739
self.history = history
3840
self.isReceivingMessage = isReceivingMessage
41+
self.pluginIdentifiers = pluginIdentifiers
3942
self.onMessageSend = onMessageSend
4043
self.onStop = onStop
4144
self.onClear = onClear
@@ -59,6 +62,7 @@ public final class ChatProvider: ObservableObject {
5962
public func triggerCustomCommand(_ command: CustomCommand) {
6063
onRunCustomCommand(command)
6164
}
65+
6266
public func setAsExtraPrompt(id: MessageID) { onSetAsExtraPrompt(id) }
6367
}
6468

Core/Sources/SuggestionWidget/SuggestionPanelContent/ChatPanel.swift

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -355,29 +355,7 @@ struct ChatPanelInputArea: View {
355355
text: $typedMessage,
356356
font: .systemFont(ofSize: 14),
357357
onSubmit: { submitText() },
358-
completions: { text, _, range in
359-
if text.isEmpty { return [] }
360-
let availableFeatures = [
361-
"/run",
362-
"/airun",
363-
"/math",
364-
"/search",
365-
"/shortcut",
366-
"/exit",
367-
"@selection",
368-
"@file",
369-
]
370-
return availableFeatures
371-
.filter { $0.hasPrefix(text) && $0 != text }
372-
.compactMap {
373-
guard let index = $0.index(
374-
$0.startIndex,
375-
offsetBy: range.location,
376-
limitedBy: $0.endIndex
377-
) else { return nil }
378-
return String($0[index...])
379-
}
380-
}
358+
completions: chatAutoCompletion
381359
)
382360
.padding(.top, 1)
383361
.padding(.bottom, -1)
@@ -420,6 +398,26 @@ struct ChatPanelInputArea: View {
420398
chat.send(typedMessage)
421399
typedMessage = ""
422400
}
401+
402+
func chatAutoCompletion(text: String, proposed: [String], range: NSRange) -> [String] {
403+
if text.isEmpty { return [] }
404+
let plugins = chat.pluginIdentifiers.map { "/\($0)" }
405+
let availableFeatures = plugins + [
406+
"/exit",
407+
"@selection",
408+
"@file",
409+
]
410+
return availableFeatures
411+
.filter { $0.hasPrefix(text) && $0 != text }
412+
.compactMap {
413+
guard let index = $0.index(
414+
$0.startIndex,
415+
offsetBy: range.location,
416+
limitedBy: $0.endIndex
417+
) else { return nil }
418+
return String($0[index...])
419+
}
420+
}
423421
}
424422

425423
struct ChatContextMenu: View {
@@ -700,3 +698,6 @@ struct ChatPanel_Light_Preview: PreviewProvider {
700698
}
701699
}
702700

701+
702+
703+

0 commit comments

Comments
 (0)