mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Update conversation API (#14763)
* Update conversation API * Update action done * Add query done data * Update conversation_id type
This commit is contained in:
parent
9c27bb37a0
commit
9fdef3df6d
@ -1,23 +1,68 @@
|
|||||||
import { HomeAssistant } from "../types";
|
import { HomeAssistant } from "../types";
|
||||||
|
|
||||||
interface ProcessResults {
|
interface IntentTarget {
|
||||||
card: { [key: string]: Record<string, string> };
|
type: "area" | "device" | "entity" | "domain" | "device_class" | "custom";
|
||||||
speech: {
|
name: string;
|
||||||
[SpeechType in "plain" | "ssml"]: { extra_data: any; speech: 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 {
|
export interface AgentInfo {
|
||||||
attribution?: { name: string; url: string };
|
attribution?: { name: string; url: string };
|
||||||
onboarding?: { text: string; url: string };
|
onboarding?: { text: string; url: string };
|
||||||
}
|
}
|
||||||
|
|
||||||
export const processText = (
|
export const processConversationInput = (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
text: string,
|
text: string,
|
||||||
// eslint-disable-next-line: variable-name
|
// eslint-disable-next-line: variable-name
|
||||||
conversation_id: string
|
conversation_id: string
|
||||||
): Promise<ProcessResults> =>
|
): Promise<ConversationResult> =>
|
||||||
hass.callWS({
|
hass.callWS({
|
||||||
type: "conversation/process",
|
type: "conversation/process",
|
||||||
text,
|
text,
|
||||||
|
@ -22,7 +22,7 @@ import type { HaTextField } from "../../components/ha-textfield";
|
|||||||
import {
|
import {
|
||||||
AgentInfo,
|
AgentInfo,
|
||||||
getAgentInfo,
|
getAgentInfo,
|
||||||
processText,
|
processConversationInput,
|
||||||
setConversationOnboarding,
|
setConversationOnboarding,
|
||||||
} from "../../data/conversation";
|
} from "../../data/conversation";
|
||||||
import { haStyleDialog } from "../../resources/styles";
|
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
|
// To make sure the answer is placed at the right user text, we add it before we process it
|
||||||
this._addMessage(message);
|
this._addMessage(message);
|
||||||
try {
|
try {
|
||||||
const response = await processText(
|
const response = await processConversationInput(
|
||||||
this.hass,
|
this.hass,
|
||||||
text,
|
text,
|
||||||
this._conversationId!
|
this._conversationId!
|
||||||
);
|
);
|
||||||
const plain = response.speech.plain;
|
const plain = response.response.speech?.plain;
|
||||||
message.text = plain.speech;
|
if (plain) {
|
||||||
|
message.text = plain.speech;
|
||||||
|
} else {
|
||||||
|
message.text = "<silence>";
|
||||||
|
}
|
||||||
|
|
||||||
this.requestUpdate("_conversation");
|
this.requestUpdate("_conversation");
|
||||||
} catch {
|
} catch {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user