Skip to content

Commit

Permalink
Merge branch 'master' into feature/remove-depricated-and-not-used-code
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoncal authored Jun 4, 2024
2 parents 7b810bc + fe639d2 commit 141c31c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Sources/Shared/Intents/AssistInApp/AssistModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ public struct AssistResponse: HADataDecodable {
public struct IntentOutput: HADataDecodable {
public init(data: HAData) throws {
self.response = try? data.decode("response")
self.conversationId = try? data.decode("conversation_id")
}

public let response: Response?
public let conversationId: String?
}

public struct Response: HADataDecodable {
Expand Down
22 changes: 16 additions & 6 deletions Sources/Shared/Intents/AssistInApp/AssistRequests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,40 @@ import HAKit
public enum AssistRequests {
public static func assistByVoiceTypedSubscription(
preferredPipelineId: String,
audioSampleRate: Double
audioSampleRate: Double,
conversationId: String?
) -> HATypedSubscription<AssistResponse> {
.init(request: .init(type: .webSocket("assist_pipeline/run"), data: [
var data: [String: Any] = [
"pipeline": preferredPipelineId,
"start_stage": "stt",
"end_stage": "tts",
"input": [
"sample_rate": audioSampleRate,
],
]))
]
if let conversationId {
data["conversation_id"] = conversationId
}
return .init(request: .init(type: .webSocket("assist_pipeline/run"), data: data))
}

public static func assistByTextTypedSubscription(
preferredPipelineId: String,
inputText: String
inputText: String,
conversationId: String?
) -> HATypedSubscription<AssistResponse> {
.init(request: .init(type: .webSocket("assist_pipeline/run"), data: [
var data: [String: Any] = [
"pipeline": preferredPipelineId,
"start_stage": "intent",
"end_stage": "intent",
"input": [
"text": inputText,
],
]))
]
if let conversationId {
data["conversation_id"] = conversationId
}
return .init(request: .init(type: .webSocket("assist_pipeline/run"), data: data))
}

public static var fetchPipelinesTypedRequest: HATypedRequest<PipelineResponse> {
Expand Down
20 changes: 18 additions & 2 deletions Sources/Shared/Intents/AssistInApp/AssistService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ public final class AssistService: AssistServiceProtocol {
private var cancellable: HACancellable?
private var sttBinaryHandlerId: UInt8?

/// Conversation Id that is provided after first interation if available, this keeps context
private var conversationId: String?
/// This exists to reset conversationId when pipelineId changes
private var lastPipelineIdUsed: String? {
didSet {
if oldValue != lastPipelineIdUsed {
conversationId = nil
}
}
}

public init(
server: Server
) {
Expand Down Expand Up @@ -88,9 +99,11 @@ public final class AssistService: AssistServiceProtocol {
}

private func assistWithAudio(pipelineId: String, audioSampleRate: Double) {
lastPipelineIdUsed = pipelineId
connection.subscribe(to: AssistRequests.assistByVoiceTypedSubscription(
preferredPipelineId: pipelineId,
audioSampleRate: audioSampleRate
audioSampleRate: audioSampleRate,
conversationId: conversationId
)) { [weak self] cancellable, data in
guard let self else { return }
self.cancellable = cancellable
Expand All @@ -99,9 +112,11 @@ public final class AssistService: AssistServiceProtocol {
}

private func assistWithText(input: String, pipelineId: String) {
lastPipelineIdUsed = pipelineId
connection.subscribe(to: AssistRequests.assistByTextTypedSubscription(
preferredPipelineId: pipelineId,
inputText: input
inputText: input,
conversationId: conversationId
)) { [weak self] cancellable, data in
guard let self else { return }
self.cancellable = cancellable
Expand Down Expand Up @@ -140,6 +155,7 @@ public final class AssistService: AssistServiceProtocol {
case .intentStart:
break
case .intentEnd:
conversationId = data.data?.intentOutput?.conversationId
delegate?.didReceiveIntentEndContent(data.data?.intentOutput?.response?.speech.plain.speech ?? "Unknown")
case .ttsStart:
break
Expand Down

0 comments on commit 141c31c

Please sign in to comment.