Skip to content

Commit dbf303f

Browse files
committed
Change the way custom command is handled
1 parent 93242c4 commit dbf303f

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

Core/Sources/ChatGPTChatTab/ChatGPTChatTab.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public class ChatGPTChatTab: ChatTab {
104104

105105
return [Builder(title: "Legacy Chat", customCommand: nil)] + customCommands
106106
}
107-
107+
108108
public static func defaultBuilder() -> ChatTabBuilder {
109109
Builder(title: "Legacy Chat", customCommand: nil)
110110
}
@@ -174,5 +174,15 @@ public class ChatGPTChatTab: ChatTab {
174174
}
175175
}
176176
}
177+
178+
public func handleCustomCommand(_ customCommand: CustomCommand) -> Bool {
179+
Task {
180+
if service.isReceivingMessage {
181+
await service.stopReceivingMessage()
182+
}
183+
try? await service.handleCustomCommand(customCommand)
184+
}
185+
return true
186+
}
177187
}
178188

Core/Sources/Service/GUI/GraphicalUserInterfaceController.swift

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -187,31 +187,26 @@ struct GUI {
187187
}
188188

189189
case let .sendCustomCommandToActiveChat(command):
190-
@Sendable func stopAndHandleCommand(_ tab: ChatGPTChatTab) async {
191-
if tab.service.isReceivingMessage {
192-
await tab.service.stopReceivingMessage()
193-
}
194-
try? await tab.service.handleCustomCommand(command)
195-
}
196-
197190
if let info = state.chatTabGroup.selectedTabInfo,
198-
let activeTab = chatTabPool.getTab(of: info.id) as? ChatGPTChatTab
191+
let tab = chatTabPool.getTab(of: info.id),
192+
tab.handleCustomCommand(command)
199193
{
200194
return .run { send in
201195
await send(.openChatPanel(forceDetach: false, activateThisApp: false))
202-
await stopAndHandleCommand(activeTab)
203196
}
204197
}
205198

206-
if let info = state.chatTabGroup.tabInfo.first(where: {
207-
chatTabPool.getTab(of: $0.id) is ChatGPTChatTab
208-
}),
209-
let chatTab = chatTabPool.getTab(of: info.id) as? ChatGPTChatTab
210-
{
211-
state.chatTabGroup.selectedTabId = chatTab.id
212-
return .run { send in
213-
await send(.openChatPanel(forceDetach: false, activateThisApp: false))
214-
await stopAndHandleCommand(chatTab)
199+
for info in state.chatTabGroup.tabInfo {
200+
if let chatTab = chatTabPool.getTab(of: info.id),
201+
chatTab.handleCustomCommand(command)
202+
{
203+
state.chatTabGroup.selectedTabId = chatTab.id
204+
return .run { send in
205+
await send(.openChatPanel(
206+
forceDetach: false,
207+
activateThisApp: false
208+
))
209+
}
215210
}
216211
}
217212

@@ -220,9 +215,7 @@ struct GUI {
220215
else { return }
221216
await send(.suggestionWidget(.chatPanel(.appendAndSelectTab(chatTabInfo))))
222217
await send(.openChatPanel(forceDetach: false, activateThisApp: false))
223-
if let chatTab = chatTab as? ChatGPTChatTab {
224-
await stopAndHandleCommand(chatTab)
225-
}
218+
_ = chatTab.handleCustomCommand(command)
226219
}
227220

228221
case .toggleWidgetsHotkeyPressed:

Tool/Sources/ChatTab/ChatTab.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ComposableArchitecture
2+
import Preferences
23
import Foundation
34
import SwiftUI
45

@@ -49,6 +50,8 @@ public protocol ChatTabType {
4950
func start()
5051
/// Whenever the user close the tab, this method will be called.
5152
func close()
53+
/// Handle custom command.
54+
func handleCustomCommand(_ customCommand: CustomCommand) -> Bool
5255

5356
/// Whether this chat tab should be the default chat tab replacement.
5457
static var isDefaultChatTabReplacement: Bool { get }
@@ -180,6 +183,9 @@ public extension ChatTabType {
180183
/// Default implementation that does nothing.
181184
func close() {}
182185

186+
/// By default it can't handle custom command.
187+
func handleCustomCommand(_ customCommand: CustomCommand) -> Bool { false }
188+
183189
static var canHandleOpenChatCommand: Bool { false }
184190
static var isDefaultChatTabReplacement: Bool { false }
185191
static func defaultChatBuilder() -> ChatTabBuilder {

0 commit comments

Comments
 (0)