Skip to content

Commit 1a47903

Browse files
committed
Add backend reset and startNewConversation entry point
1 parent bf2f1e7 commit 1a47903

4 files changed

Lines changed: 51 additions & 1 deletion

File tree

Modules/Sources/WooAIAssistant/Backend/AssistantBackend.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ public protocol AssistantBackend: Sendable {
44
func send(turn: AssistantTurn,
55
context: AssistantContext,
66
session: AssistantSession?) -> AsyncThrowingStream<BackendYield, Error>
7+
func reset() async
8+
}
9+
10+
public extension AssistantBackend {
11+
func reset() async {}
712
}
813

914
public protocol AssistantBackendConfirming: AssistantBackend {

Modules/Sources/WooAIAssistant/State/AssistantController.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,39 @@ public final class AssistantController {
4747
conversation.markCancelled(messageID: messageID)
4848
}
4949
activeAssistantMessageID = nil
50+
for proposalID in pendingProposalIDs() {
51+
conversation.applyConfirmationResolution(proposalID: proposalID, approved: false)
52+
cancelProposal(proposalID)
53+
}
5054
conversation.setStreaming(.idle)
5155
}
5256

57+
private func pendingProposalIDs() -> [UUID] {
58+
var ids: [UUID] = []
59+
for message in conversation.messages {
60+
for segment in message.segments {
61+
if case .confirmation(_, let proposalID, _, _, .pending) = segment {
62+
ids.append(proposalID)
63+
}
64+
}
65+
}
66+
return ids
67+
}
68+
69+
public func startNewConversation() {
70+
cancel()
71+
let token = UUID()
72+
activeTurnToken = token
73+
activeTask = Task { [weak self] in
74+
guard let self else { return }
75+
await backend.reset()
76+
guard activeTurnToken == token else { return }
77+
conversation.reset()
78+
activeTask = nil
79+
activeTurnToken = nil
80+
}
81+
}
82+
5383
public var canSend: Bool {
5484
activeTask == nil
5585
}

Modules/Sources/WooAIAssistant/State/AssistantConversation.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ public final class AssistantConversation {
2626
self.messages = seededMessages
2727
}
2828

29+
func reset() {
30+
messages = []
31+
streamingState = .idle
32+
outcomeUnknownObserved = false
33+
session = nil
34+
}
35+
36+
func applyConfirmationResolution(proposalID: UUID, approved: Bool) {
37+
for index in messages.indices {
38+
messages[index].updateConfirmation(proposalID: proposalID,
39+
to: approved ? .confirmed : .cancelled)
40+
}
41+
}
42+
2943
func appendUserMessage(_ text: String) -> ChatMessage {
3044
let message = ChatMessage(role: .user,
3145
segments: [.text(id: UUID(), content: text)])

Modules/Tests/WooAIAssistantTests/Mocks/MockAssistantBackend.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Foundation
22
@testable import WooAIAssistant
33

4-
final class MockAssistantBackend: AssistantBackendConfirming, @unchecked Sendable {
4+
@MainActor
5+
final class MockAssistantBackend: AssistantBackendConfirming {
56

67
private(set) var recordedTurns: [AssistantTurn] = []
78
private(set) var confirmedProposalIDs: [UUID] = []

0 commit comments

Comments
 (0)