Skip to content

Commit ed20d8c

Browse files
committed
Adjust plugin cancellation
1 parent 06132d4 commit ed20d8c

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

Core/Sources/ChatPlugins/AITerminalChatPlugin.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public actor AITerminalChatPlugin: ChatPlugin {
3131
history.append(.init(role: .user, content: content))
3232
}
3333
delegate?.pluginDidStartResponding(self)
34+
if isCancelled { return }
3435
if try await checkConfirmation(content: content) {
3536
delegate?.pluginDidEndResponding(self)
3637
delegate?.pluginDidEnd(self)
@@ -52,6 +53,7 @@ public actor AITerminalChatPlugin: ChatPlugin {
5253
delegate?.pluginDidStartResponding(self)
5354
let result = try await generateCommand(task: content)
5455
command = result
56+
if isCancelled { return }
5557
await chatGPTService.mutateHistory { history in
5658
history.append(.init(role: .assistant, content: """
5759
Confirm to run?
@@ -73,6 +75,8 @@ public actor AITerminalChatPlugin: ChatPlugin {
7375

7476
public func cancel() async {
7577
isCancelled = true
78+
delegate?.pluginDidEndResponding(self)
79+
delegate?.pluginDidEnd(self)
7680
}
7781

7882
public func stopResponding() async {}

Core/Sources/ChatPlugins/ChatPlugin.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22
import OpenAIService
33

4-
public protocol ChatPlugin {
4+
public protocol ChatPlugin: AnyObject {
55
/// Should be [a-zA-Z0-9]+
66
static var command: String { get }
77
var name: String { get }

Core/Sources/ChatService/ChatService.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ extension ChatService: ChatPluginDelegate {
112112
runningPlugin = plugin
113113
}
114114

115-
public func pluginDidEnd(_: ChatPlugin) {
116-
runningPlugin = nil
115+
public func pluginDidEnd(_ plugin: ChatPlugin) {
116+
if runningPlugin === plugin {
117+
runningPlugin = nil
118+
}
117119
}
118120

119121
public func shouldStartAnotherPlugin(_ type: ChatPlugin.Type, withContent content: String) {

0 commit comments

Comments
 (0)