This repository has been archived by the owner on Jun 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
slot_filling_dialog.js
342 lines (306 loc) · 12.5 KB
/
slot_filling_dialog.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Almond
//
// Copyright 2016 Giovanni Campagna <gcampagn@cs.stanford.edu>
//
// See COPYING for details
"use strict";
const Q = require('q');
const assert = require('assert');
const ThingTalk = require('thingtalk');
const Ast = ThingTalk.Ast;
const Type = ThingTalk.Type;
const ValueCategory = require('./semantic').ValueCategory;
const Dialog = require('./dialog');
const ContactSearchDialog = require('./contact_search_dialog');
const UserContextDialog = require('./user_context_dialog');
const Helpers = require('./helpers');
module.exports = class SlotFillingDialog extends Dialog {
constructor(prim, scope) {
super();
this.prim = prim;
this._scope = scope;
this.icon = Helpers.getIcon(prim);
this._resolving = null;
this._paring = false;
this._options = null;
this._aux = null;
let [toFill, toConcretize] = ThingTalk.Generate.computeSlots(prim);
this.toFill = toFill;
this.toConcretize = toConcretize;
}
_complete() {
// make out parameters available in the "scope", which puts
// them as possible options for a later slot fill
for (let outParam of this.prim.out_params) {
let argname = outParam.value;
let schema = this.prim.schema;
let index = schema.index[argname];
let argcanonical = schema.argcanonicals[index] || argname;
this._scope[outParam.name] = {
value: Ast.Value.VarRef(outParam.name),
type: schema.out[argname],
text: this._("Use the %s from %s").format(argcanonical, this.prim.selector.kind)
};
}
this._scope['$event'] = {
value: Ast.Value.Event(null),
type: Type.String,
text: this._("A description of the result")
};
}
static slotFill(parent, prim, scope) {
// if we get here, either we never pushed the SlotFillingDialog,
// or the SlotFillingDialog returned false from .handle(), which
// implies it is done
if (parent.subdialog === null) {
parent.push(new SlotFillingDialog(prim, scope));
return Q(parent.subdialog.continue()).then((waiting) => {
if (waiting) {
return waiting;
} else {
parent.subdialog._complete();
parent.pop();
return false;
}
});
} else {
parent.subdialog._complete();
parent.pop();
return Q(false);
}
}
_lookupContactByAccount(contact, account) {
return this.manager.messaging.getUserByAccount(account).then((user) => {
contact.display = user.name;
}).catch((e) => {
console.log('Failed to lookup account ' + account + ': ' + e.message);
}).then(() => false);
}
_addDisplayToContact(contact) {
if (contact.display)
return Q();
var contactApi = this.manager.platform.getCapability('contacts');
if (contactApi === null)
return Q();
var principal = contact.value;
return contactApi.lookupPrincipal(principal).then((addressBookContact) => {
if (addressBookContact)
contact.display = addressBookContact.displayName;
});
}
_lookupContact(contact) {
var principal = contact.value;
if (principal.startsWith(this.manager.messaging.type + '-account:')) {
if (principal === this.manager.messaging.type + '-account:' + this.manager.messaging.account)
return this._("me");
return this._lookupContactByAccount(contact, principal.substr((this.manager.messaging.type + '-account:').length));
}
return this._addDisplayToContact(contact).then(() => {
return this.manager.messaging.getAccountForIdentity(principal);
}).then((account) => {
if (account) {
var accountPrincipal = this.manager.messaging.type + '-account:' + account;
console.log('Converted ' + contact.value + ' to ' + accountPrincipal);
contact.value = accountPrincipal;
}
return false;
}).catch((e) => {
console.log('Failed to concretize contact: ' + e.message);
return false;
});
}
_getType(slot) {
let argname = slot.name;
let schema = this.prim.schema;
if (schema.inReq[argname])
return schema.inReq[argname];
if (schema.inOpt[argname])
return schema.inOpt[argname];
var type = schema.out[argname];
if (slot instanceof Ast.Filter && slot.operator === 'contains')
return type.elem;
else
return type;
}
_getQuestion(slot) {
let question;
let argname = slot.name;
let schema = this.prim.schema;
let index = schema.index[argname];
if (slot instanceof Ast.InputParam ||
(slot instanceof Ast.Filter && slot.operator === '='))
question = schema.questions[index];
let argcanonical = schema.argcanonicals[index];
if (!question)
return this._("What is the value of %s?").format(argcanonical);
else
return question;
}
_concretize(slot) {
var value = slot.value;
if (value.isEntity && value.type === 'tt:contact' && !value.display)
return this._lookupContact(value);
if (value.isEntity && value.type === 'tt:contact_name')
return ContactSearchDialog.resolve(this, this._getType(slot), slot);
if (value.isLocation && value.value.isRelative)
return UserContextDialog.resolve(this, slot, '$context.location.' + value.value.relativeTag);
return Q(false);
}
_askEnum(entries, question) {
this._aux = entries.map((e) => Ast.Value.Enum(e));
this.ask(ValueCategory.MultipleChoice, question);
entries.forEach((e, i) => {
this.replyChoice(i, "choice", e);
});
return true;
}
_getOptions(slot, slotType) {
let options = [];
for (var vname in this._scope) {
let option = this._scope[vname];
if (Type.isAssignable(option.type, slotType))
options.push(option);
}
return options;
}
continue() {
if (this.toConcretize.length > 0) {
this._resolving = this.toConcretize.shift();
return this._concretize(this._resolving).then((waiting) => {
if (waiting)
return waiting;
else
this._resolving = null;
return this.continue();
});
}
if (this._resolving === null && this.toFill.length > 0)
this._resolving = this.toFill.shift();
if (this._resolving === null)
return false;
let slot = this._resolving;
let question = this._getQuestion(slot);
let slotType = this._getType(slot);
let options = this._getOptions(slot, slotType);
if (!this._paring && options.length > 0) {
this._paring = true;
this._options = options;
if (options.length > 0) {
this.ask(ValueCategory.MultipleChoice, question);
for (var i = 0; i < options.length; i++) {
this.replyChoice(i, "arg", options[i].text);
}
this.replyChoice(i, "arg", this._("None of above"));
return true;
}
}
this._paring = false;
if (slotType.isEntity && slotType.type === 'tt:phone_number')
return this.ask(ValueCategory.PhoneNumber, question);
else if (slotType.isEntity && slotType.type === 'tt:email_address')
return this.ask(ValueCategory.EmailAddress, question);
else if (slotType.isEntity && slotType.type === 'tt:picture')
return this.ask(ValueCategory.Picture, question);
else if (slotType.isEntity && slotType.type === 'tt:contact')
return this.ask(ValueCategory.PhoneNumber, question);
else if (slotType.isString || slotType.isEntity)
return this.ask(ValueCategory.RawString, question);
else if (slotType.isMeasure)
return this.ask(ValueCategory.Measure(slotType.unit), question);
else if (slotType.isNumber)
return this.ask(ValueCategory.Number, question);
else if (slotType.isBoolean)
return this.ask(ValueCategory.YesNo, question);
else if (slotType.isDate)
return this.ask(ValueCategory.Date, question);
else if (slotType.isTime)
return this.ask(ValueCategory.Time, question);
else if (slotType.isLocation)
return this.ask(ValueCategory.Location, question);
else if (slotType.isEnum)
return this._askEnum(slotType.entries, question);
else
throw new TypeError('Unhandled slot type ' + slotType); // can't handle it
}
handleRaw(raw) {
if (this._resolving !== null &&
this.expecting === ValueCategory.RawString) {
var slotType = this._getType(this._resolving);
if (slotType.isEntity)
this._resolving.value = Ast.Value.Entity(raw, slotType.type, null);
else
this._resolving.value = Ast.Value.String(raw);
this._resolving = null;
return this.continue();
} else {
return super.handleRaw(raw);
}
}
handle(command) {
return this.handleGeneric(command).then((handled) => {
if (handled)
return true;
if (this._resolving === null)
return this.continue();
if (this.expecting === ValueCategory.YesNo) {
if (command.isYes)
this._resolving.value = Ast.Value.Boolean(true);
else
this._resolving.value = Ast.Value.Boolean(false);
} else if (this.expecting !== null) {
if (this._paring === true) {
let index = command.value;
if (index !== Math.floor(index) ||
index < 0 ||
index > this._options.length) {
this.reply(this._("Please click on one of the provided choices."));
this.manager.resendChoices();
return true;
}
if (index === this._options.length) {
return this.continue();
} else {
this._resolving.value = this._options[index].value;
this._options = null;
this._paring = false;
}
} else {
let value;
if (this.expecting === ValueCategory.MultipleChoice) {
let index = command.value;
if (index !== Math.floor(index) ||
index < 0 ||
index >= this._aux.length) {
this.reply(this._("Please click on one of the provided choices."));
this.manager.resendChoices();
return true;
}
value = this._aux[index];
this._aux = null;
} else {
value = command.value;
// for a tt:contact entity we ask for a phone number
// convert it back to tt:contact before we type check
// or we'll explode
if (value.isEntity && value.type === 'tt:contact') {
value = Ast.Value.Entity('phone:' + value.value, 'tt:contact', command.value.display);
}
}
var givenType = Ast.typeForValue(value);
assert(Type.isAssignable(givenType, this._getType(this._resolving)));
this._resolving.value = value;
}
}
return this._concretize(this._resolving).then((waiting) => {
if (waiting) {
return waiting;
} else {
this._resolving = null;
return this.continue();
}
});
});
}
}