Files
iOS/Sources/Shared/Intents/AssistIntentHandler.swift
Bruno Pantaleão Gonçalves 632e73ba4a Add experimental on-device STT and TTS for Assist (#4423)
<!-- 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. -->

<img width="603" height="1311" alt="Captura de Tela 2026-03-09 à(s) 15
16 13"
src="https://github.com/user-attachments/assets/17c166ec-815e-4311-acaa-127e431e655a"
/>



## 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. -->
2026-03-09 16:23:38 +01:00

128 lines
4.1 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,
expectTTS: false
))
}
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)"))
}
}