Update conversation API (#14763)

* Update conversation API

* Update action done

* Add query done data

* Update conversation_id type
This commit is contained in:
Paulus Schoutsen 2022-12-13 23:10:57 -05:00 committed by GitHub
parent 9c27bb37a0
commit 9fdef3df6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 10 deletions

View File

@ -1,23 +1,68 @@
import { HomeAssistant } from "../types";
interface ProcessResults {
card: { [key: string]: Record<string, string> };
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<ProcessResults> =>
): Promise<ConversationResult> =>
hass.callWS({
type: "conversation/process",
text,

View File

@ -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 = "<silence>";
}
this.requestUpdate("_conversation");
} catch {