mirror of
https://github.com/home-assistant/iOS.git
synced 2026-06-18 11:15:36 -05:00
<!-- Thank you for submitting a Pull Request and helping to improve Home Assistant. Please complete the following sections to help the processing and review of your changes. Please do not delete anything from this template. --> ## Summary <!-- Provide a brief summary of the changes you have made and most importantly what they aim to achieve --> ## Screenshots <!-- If this is a user-facing change not in the frontend, please include screenshots in light and dark mode. --> ## Link to pull request in Documentation repository <!-- Pull requests that add, change or remove functionality must have a corresponding pull request in the Companion App Documentation repository (https://github.com/home-assistant/companion.home-assistant). Please add the number of this pull request after the "#" --> Documentation: home-assistant/companion.home-assistant# ## Any other notes <!-- If there is any other information of note, like if this Pull Request is part of a bigger change, please include it here. -->
124 lines
4.0 KiB
Swift
124 lines
4.0 KiB
Swift
import Intents
|
|
import ObjectMapper
|
|
import PromiseKit
|
|
|
|
class AssistIntentHandler: NSObject, AssistIntentHandling {
|
|
typealias Intent = AssistIntent
|
|
|
|
private var intentCompletion: ((AssistIntentResponse) -> Void)?
|
|
private var assistService: AssistService?
|
|
|
|
func resolveServer(for intent: Intent, with completion: @escaping (IntentServerResolutionResult) -> Void) {
|
|
if let server = Current.servers.server(for: intent) {
|
|
completion(.success(with: .init(server: server)))
|
|
} else {
|
|
completion(.needsValue())
|
|
}
|
|
}
|
|
|
|
func provideServerOptions(for intent: Intent, with completion: @escaping ([IntentServer]?, Error?) -> Void) {
|
|
completion(IntentServer.all, nil)
|
|
}
|
|
|
|
func provideServerOptionsCollection(
|
|
for intent: Intent,
|
|
with completion: @escaping (INObjectCollection<IntentServer>?, Error?) -> Void
|
|
) {
|
|
completion(.init(items: IntentServer.all), nil)
|
|
}
|
|
|
|
func handle(intent: AssistIntent, completion: @escaping (AssistIntentResponse) -> Void) {
|
|
guard let server = Current.servers.server(for: intent) else {
|
|
completion(.failure(error: "no server provided"))
|
|
return
|
|
}
|
|
|
|
guard server.info.version >= .conversationWebhook else {
|
|
completion(.failure(error: HomeAssistantAPI.APIError.mustUpgradeHomeAssistant(
|
|
current: server.info.version,
|
|
minimum: .conversationWebhook
|
|
).localizedDescription))
|
|
return
|
|
}
|
|
|
|
intentCompletion = completion
|
|
assistService = AssistService(server: server)
|
|
assistService?.delegate = self
|
|
assistService?.assist(source: .text(input: intent.text ?? "", pipelineId: intent.pipeline?.identifier ?? nil))
|
|
}
|
|
|
|
func resolvePipeline(
|
|
for intent: AssistIntent,
|
|
with completion: @escaping (IntentAssistPipelineResolutionResult) -> Void
|
|
) {
|
|
guard let server = Current.servers.server(for: intent) else {
|
|
completion(.needsValue())
|
|
return
|
|
}
|
|
|
|
AssistService(server: server).fetchPipelines { response in
|
|
guard let pipelines = response?.pipelines else {
|
|
completion(.needsValue())
|
|
return
|
|
}
|
|
guard let result = pipelines.first(where: { pipeline in
|
|
pipeline.id == intent.pipeline?.identifier
|
|
}) else {
|
|
completion(.needsValue())
|
|
return
|
|
}
|
|
completion(.success(with: .init(identifier: result.id, display: result.name)))
|
|
}
|
|
}
|
|
|
|
func providePipelineOptionsCollection(
|
|
for intent: AssistIntent,
|
|
with completion: @escaping (INObjectCollection<IntentAssistPipeline>?, (any Error)?) -> Void
|
|
) {
|
|
guard let server = Current.servers.server(for: intent) else {
|
|
completion(.init(items: []), nil)
|
|
return
|
|
}
|
|
|
|
AssistService(server: server).fetchPipelines { response in
|
|
guard let pipelines = response?.pipelines else {
|
|
completion(.init(items: []), nil)
|
|
return
|
|
}
|
|
completion(.init(items: pipelines.map({ pipeline in
|
|
IntentAssistPipeline(identifier: pipeline.id, display: pipeline.name)
|
|
})), nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension AssistIntentHandler: AssistServiceDelegate {
|
|
func didReceiveStreamResponseChunk(_ content: String) {
|
|
/* no-op */
|
|
}
|
|
|
|
func didReceiveEvent(_ event: AssistEvent) {
|
|
/* no-op */
|
|
}
|
|
|
|
func didReceiveSttContent(_ content: String) {
|
|
/* no-op */
|
|
}
|
|
|
|
func didReceiveIntentEndContent(_ content: String) {
|
|
intentCompletion?(.success(result: .init(identifier: nil, display: content)))
|
|
}
|
|
|
|
func didReceiveGreenLightForAudioInput() {
|
|
/* no-op */
|
|
}
|
|
|
|
func didReceiveTtsMediaUrl(_ mediaUrl: URL) {
|
|
/* no-op */
|
|
}
|
|
|
|
func didReceiveError(code: String, message: String) {
|
|
intentCompletion?(.failure(error: "\(code) - \(message)"))
|
|
}
|
|
}
|