Skip to content

Commit

Permalink
Use first server available for Assist when server is not defined (#2797)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoncal authored May 28, 2024
1 parent d163484 commit c560428
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Sources/App/Assist/AssistView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct AssistView: View {
.navigationViewStyle(.stack)
.onAppear {
assistSession.inProgress = true
viewModel.onAppear()
viewModel.initialRoutine()
}
.onDisappear {
assistSession.inProgress = false
Expand Down
47 changes: 24 additions & 23 deletions Sources/App/Assist/AssistViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class AssistViewModel: NSObject, ObservableObject {
private var assistService: AssistServiceProtocol
private(set) var autoStartRecording: Bool
private(set) var audioTask: Task<Void, Error>?
private(set) var routineTask: Task<Void, Error>?

private(set) var canSendAudioData = false

Expand All @@ -43,31 +44,27 @@ final class AssistViewModel: NSObject, ObservableObject {
}

@MainActor
func onAppear() {
func initialRoutine() {
AssistSession.shared.delegate = self
checkForAutoRecordingAndStart()
fetchPipelines()
routineTask?.cancel()
routineTask = Task.detached { [weak self] in
await self?.fetchPipelines()
self?.checkForAutoRecordingAndStart()
}
}

func onDisappear() {
audioRecorder.stopRecording()
audioPlayer.pause()
audioTask?.cancel()
routineTask?.cancel()
}

@MainActor
func assistWithText() {
audioPlayer.pause()
stopStreaming()

guard !inputText.isEmpty else { return }
guard !pipelines.isEmpty, !preferredPipelineId.isEmpty else {
fetchPipelines()
return
}

assistService.assist(source: .text(input: inputText, pipelineId: preferredPipelineId))

appendToChat(.init(content: inputText, itemType: .input))
inputText = ""
}
Expand All @@ -85,7 +82,7 @@ final class AssistViewModel: NSObject, ObservableObject {
inputText = ""

audioRecorder.startRecording()
// Wait untill green light from recorder delegate 'didStartRecording'
// Wait until green light from recorder delegate 'didStartRecording'
}

private func startAssistAudioPipeline(audioSampleRate: Double) {
Expand All @@ -108,18 +105,22 @@ final class AssistViewModel: NSObject, ObservableObject {
}

@MainActor
private func fetchPipelines() {
private func fetchPipelines() async {
showScreenLoader = true
assistService.fetchPipelines { [weak self] response in
self?.showScreenLoader = false
guard let self, let response else {
self?.showError(message: L10n.Assist.Error.pipelinesResponse)
return
}
if preferredPipelineId.isEmpty {
preferredPipelineId = response.preferredPipeline
await withCheckedContinuation { [weak self] continuation in
self?.assistService.fetchPipelines { [weak self] response in
self?.showScreenLoader = false
guard let self, let response else {
self?.showError(message: L10n.Assist.Error.pipelinesResponse)
continuation.resume()
return
}
if preferredPipelineId.isEmpty {
preferredPipelineId = response.preferredPipeline
}
pipelines = response.pipelines
continuation.resume()
}
pipelines = response.pipelines
}
}

Expand Down Expand Up @@ -216,7 +217,7 @@ extension AssistViewModel: AssistSessionDelegate {
}
preferredPipelineId = context.pipelineId
autoStartRecording = context.autoStartRecording
onAppear()
initialRoutine()
}
}
}
20 changes: 14 additions & 6 deletions Sources/App/Assist/Tests/AssistViewModel.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ final class AssistViewModelTests: XCTestCase {
}

@MainActor
func testOnAppearFetchPipelines() {
sut.onAppear()
func testOnAppearFetchPipelines() async throws {
sut.initialRoutine()
mockAssistService.pipelineResponse = .init(preferredPipeline: "", pipelines: [])
try await sut.routineTask?.value
XCTAssert(mockAssistService.fetchPipelinesCalled)
XCTAssertEqual(AssistSession.shared.delegate.debugDescription, sut.debugDescription)
}

@MainActor
func testOnAppearAutoStartRecording() async throws {
sut = makeSut(autoStartRecording: true)
sut.onAppear()
mockAssistService.pipelineResponse = .init(preferredPipeline: "", pipelines: [])

sut.initialRoutine()
try await sut.routineTask?.value
try await sut.audioTask?.value
XCTAssertNotNil(sut.audioTask)
XCTAssertTrue(mockAudioPlayer.pauseCalled)
Expand All @@ -47,14 +52,17 @@ final class AssistViewModelTests: XCTestCase {
}

@MainActor
func testOnDisappear() {
func testOnDisappear() async throws {
sut = makeSut(autoStartRecording: true)
sut.onAppear()
sut.onDisappear()

sut.initialRoutine()
try await sut.routineTask?.value
try await sut.audioTask?.value
sut.onDisappear()
XCTAssertTrue(mockAudioRecorder.stopRecordingCalled)
XCTAssertTrue(mockAudioPlayer.pauseCalled)
XCTAssertTrue(sut.audioTask!.isCancelled)
XCTAssertTrue(sut.routineTask!.isCancelled)
}

@MainActor
Expand Down
4 changes: 2 additions & 2 deletions Sources/App/Assist/Tests/Mocks/MockAssistService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Shared

final class MockAssistService: AssistServiceProtocol {
weak var delegate: AssistServiceDelegate?
var fetchPipelinesCompletion: ((PipelineResponse?) -> Void)?
var pipelineResponse: PipelineResponse?
var fetchPipelinesCalled: Bool = false
var sendAudioDataCalled: Bool = false
var assistSource: AssistSource?
Expand All @@ -13,7 +13,7 @@ final class MockAssistService: AssistServiceProtocol {

func fetchPipelines(completion: @escaping (PipelineResponse?) -> Void) {
fetchPipelinesCalled = true
fetchPipelinesCompletion = completion
completion(pipelineResponse)
}

func assist(source: AssistSource) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/App/WebView/IncomingURLHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class IncomingURLHandler {
Current.Log.info(userActivity)

if let assistInAppIntent = userActivity.interaction?.intent as? AssistInAppIntent {
guard let server = Current.servers.server(for: assistInAppIntent) else { return false }
guard let server = Current.servers.server(for: assistInAppIntent) ?? Current.servers.all.first else { return false }
let pipeline = assistInAppIntent.pipeline
let autoStartRecording = Bool(exactly: assistInAppIntent.withVoice ?? 0) ?? false

Expand Down
5 changes: 5 additions & 0 deletions Sources/Shared/Intents/AssistInApp/AssistModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public struct PipelineResponse: HADataDecodable {
self.preferredPipeline = try data.decode("preferred_pipeline")
self.pipelines = try data.decode("pipelines")
}

public init(preferredPipeline: String, pipelines: [Pipeline]) {
self.preferredPipeline = preferredPipeline
self.pipelines = pipelines
}
}

public struct Pipeline: HADataDecodable {
Expand Down

0 comments on commit c560428

Please sign in to comment.