-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathconversation.ts
129 lines (115 loc) · 2.56 KB
/
conversation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { ensureArray } from "../common/array/ensure-array";
import type { HomeAssistant } from "../types";
export const enum ConversationEntityFeature {
CONTROL = 1,
}
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";
};
}
export interface ConversationResult {
conversation_id: string | null;
response:
| IntentResultActionDone
| IntentResultQueryAnswer
| IntentResultError;
}
export interface Agent {
id: string;
name: string;
supported_languages: "*" | string[];
}
export interface AssitDebugResult {
intent: {
name: string;
};
entities: Record<
string,
{
name: string;
value: string;
text: string;
}
>;
}
export interface AssistDebugResponse {
results: (AssitDebugResult | null)[];
}
export const processConversationInput = (
hass: HomeAssistant,
text: string,
// eslint-disable-next-line: variable-name
conversation_id: string | null,
language: string
): Promise<ConversationResult> =>
hass.callWS({
type: "conversation/process",
text,
conversation_id,
language,
});
export const listAgents = (
hass: HomeAssistant,
language?: string,
country?: string
): Promise<{ agents: Agent[] }> =>
hass.callWS({
type: "conversation/agent/list",
language,
country,
});
export const prepareConversation = (
hass: HomeAssistant,
language?: string
): Promise<void> =>
hass.callWS({
type: "conversation/prepare",
language,
});
export const debugAgent = (
hass: HomeAssistant,
sentences: string[] | string,
language: string,
device_id?: string
): Promise<AssistDebugResponse> =>
hass.callWS({
type: "conversation/agent/homeassistant/debug",
sentences: ensureArray(sentences),
language,
device_id,
});