Skip to content

Commit 1ef0aae

Browse files
committed
Split start/end plugin and start/end responding
1 parent b987f17 commit 1ef0aae

4 files changed

Lines changed: 31 additions & 14 deletions

File tree

Core/Sources/ChatPlugins/ChatPlugin.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ public protocol ChatPlugin {
99
init(inside chatGPTService: ChatGPTServiceType, delegate: ChatPluginDelegate)
1010
func send(content: String) async
1111
func cancel() async
12+
func stopResponding() async
1213
}
1314

1415
public protocol ChatPluginDelegate: AnyObject {
1516
func pluginDidStart(_ plugin: ChatPlugin)
1617
func pluginDidEnd(_ plugin: ChatPlugin)
18+
func pluginDidStartResponding(_ plugin: ChatPlugin)
19+
func pluginDidEndResponding(_ plugin: ChatPlugin)
1720
}

Core/Sources/ChatPlugins/TerminalChatPlugin.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public actor TerminalChatPlugin: ChatPlugin {
1919

2020
public func send(content: String) async {
2121
delegate?.pluginDidStart(self)
22+
delegate?.pluginDidStartResponding(self)
2223

2324
let id = "\(Self.command)-\(UUID().uuidString)"
2425
var message = ChatMessage(id: id, role: .assistant, content: "")
@@ -87,11 +88,17 @@ public actor TerminalChatPlugin: ChatPlugin {
8788
}
8889
}
8990

91+
delegate?.pluginDidEndResponding(self)
9092
delegate?.pluginDidEnd(self)
9193
}
9294

9395
public func cancel() async {
9496
isCancelled = true
9597
await terminal.terminate()
9698
}
99+
100+
public func stopResponding() async {
101+
isCancelled = true
102+
await terminal.terminate()
103+
}
97104
}

Core/Sources/ChatService/ChatService.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ public final class ChatService: ObservableObject {
1313
self.chatGPTService = chatGPTService
1414
}
1515

16-
deinit {
17-
print("deinit")
18-
}
19-
2016
public func send(content: String) async throws {
2117
// look for the prefix of content, see if there is something like /command.
2218
// If there is, then we need to find the plugin that can handle this command.
@@ -50,17 +46,23 @@ public final class ChatService: ObservableObject {
5046
}
5147

5248
extension ChatService: ChatPluginDelegate {
53-
public func pluginDidStart(_ plugin: ChatPlugin) {
49+
public func pluginDidStartResponding(_ plugin: ChatPlugins.ChatPlugin) {
5450
Task {
5551
await chatGPTService.markReceivingMessage(true)
5652
}
57-
runningPlugin = plugin
5853
}
5954

60-
public func pluginDidEnd(_ plugin: ChatPlugin) {
55+
public func pluginDidEndResponding(_ plugin: ChatPlugins.ChatPlugin) {
6156
Task {
6257
await chatGPTService.markReceivingMessage(false)
6358
}
59+
}
60+
61+
public func pluginDidStart(_ plugin: ChatPlugin) {
62+
runningPlugin = plugin
63+
}
64+
65+
public func pluginDidEnd(_ plugin: ChatPlugin) {
6466
runningPlugin = nil
6567
}
6668
}

Core/Sources/Terminal/Terminal.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ public protocol TerminalType {
1515
currentDirectoryPath: String,
1616
environment: [String: String]
1717
) async throws -> String
18-
18+
1919
func terminate() async
20+
func writeInput(_ input: String) async
21+
var isRunning: Bool { get }
2022
}
2123

2224
public final class Terminal: TerminalType, @unchecked Sendable {
2325
var process: Process?
2426
var outputPipe: Pipe?
2527
var inputPipe: Pipe?
28+
29+
public var isRunning: Bool { process?.isRunning ?? false }
2630

2731
public struct TerminationError: Error {
2832
public let reason: Process.TerminationReason
@@ -84,13 +88,14 @@ public final class Terminal: TerminalType, @unchecked Sendable {
8488
if !(self.process?.isRunning ?? false) {
8589
let reason = self.process?.terminationReason ?? .exit
8690
let status = self.process?.terminationStatus ?? 1
87-
if let output = (self.process?.standardOutput as? Pipe)?.fileHandleForReading.readDataToEndOfFile(),
88-
let content = String(data: output, encoding: .utf8),
89-
!content.isEmpty
91+
if let output = (self.process?.standardOutput as? Pipe)?.fileHandleForReading
92+
.readDataToEndOfFile(),
93+
let content = String(data: output, encoding: .utf8),
94+
!content.isEmpty
9095
{
9196
continuation?.yield(content)
9297
}
93-
98+
9499
if status == 0 {
95100
continuation?.finish()
96101
} else {
@@ -139,15 +144,15 @@ public final class Terminal: TerminalType, @unchecked Sendable {
139144
return result
140145
}
141146

142-
func writeInput(_ input: String) {
147+
public func writeInput(_ input: String) {
143148
guard let data = input.data(using: .utf8) else {
144149
return
145150
}
146151

147152
inputPipe?.fileHandleForWriting.write(data)
148153
inputPipe?.fileHandleForWriting.closeFile()
149154
}
150-
155+
151156
public func terminate() async {
152157
process?.terminate()
153158
process = nil

0 commit comments

Comments
 (0)