diff --git a/src/data/conversation.ts b/src/data/conversation.ts index 84ed420ff5..4e7a504f11 100644 --- a/src/data/conversation.ts +++ b/src/data/conversation.ts @@ -1,23 +1,68 @@ import { HomeAssistant } from "../types"; -interface ProcessResults { - card: { [key: string]: Record }; - speech: { - [SpeechType in "plain" | "ssml"]: { extra_data: any; speech: string }; +interface IntentTarget { + type: "area" | "device" | "entity" | "domain" | "device_class" | "custom"; + name: string; + id: string | null; +} + +interface IntentResultBase { + language: string; + speech: + | { + [SpeechType in "plain" | "ssml"]: { extra_data: any; speech: string }; + } + | null; +} + +interface IntentResultActionDone extends IntentResultBase { + response_type: "action_done"; + data: { + targets: IntentTarget[]; + success: IntentTarget[]; + failed: IntentTarget[]; }; } +interface IntentResultQueryAnswer extends IntentResultBase { + response_type: "query_answer"; + data: { + targets: IntentTarget[]; + success: IntentTarget[]; + failed: IntentTarget[]; + }; +} + +interface IntentResultError extends IntentResultBase { + response_type: "error"; + data: { + code: + | "no_intent_match" + | "no_valid_targets" + | "failed_to_handle" + | "unknown"; + }; +} + +interface ConversationResult { + conversation_id: string | null; + response: + | IntentResultActionDone + | IntentResultQueryAnswer + | IntentResultError; +} + export interface AgentInfo { attribution?: { name: string; url: string }; onboarding?: { text: string; url: string }; } -export const processText = ( +export const processConversationInput = ( hass: HomeAssistant, text: string, // eslint-disable-next-line: variable-name conversation_id: string -): Promise => +): Promise => hass.callWS({ type: "conversation/process", text, diff --git a/src/dialogs/voice-command-dialog/ha-voice-command-dialog.ts b/src/dialogs/voice-command-dialog/ha-voice-command-dialog.ts index 64f9d0900c..49c998bcb4 100644 --- a/src/dialogs/voice-command-dialog/ha-voice-command-dialog.ts +++ b/src/dialogs/voice-command-dialog/ha-voice-command-dialog.ts @@ -22,7 +22,7 @@ import type { HaTextField } from "../../components/ha-textfield"; import { AgentInfo, getAgentInfo, - processText, + processConversationInput, setConversationOnboarding, } from "../../data/conversation"; import { haStyleDialog } from "../../resources/styles"; @@ -274,13 +274,17 @@ export class HaVoiceCommandDialog extends LitElement { // To make sure the answer is placed at the right user text, we add it before we process it this._addMessage(message); try { - const response = await processText( + const response = await processConversationInput( this.hass, text, this._conversationId! ); - const plain = response.speech.plain; - message.text = plain.speech; + const plain = response.response.speech?.plain; + if (plain) { + message.text = plain.speech; + } else { + message.text = ""; + } this.requestUpdate("_conversation"); } catch {