From c1112b6e75c9fa155fed9bf262ec489985cce414 Mon Sep 17 00:00:00 2001 From: Giovanni Campagna Date: Wed, 6 Oct 2021 17:53:44 -0700 Subject: [PATCH] Fix LGTM warnings --- lib/thingtalk-dialogues/synthesis-dialogue.ts | 21 +++++++++++++------ lib/transaction-dialogues/context-phrases.ts | 2 +- lib/transaction-dialogues/index.ts | 1 - 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/thingtalk-dialogues/synthesis-dialogue.ts b/lib/thingtalk-dialogues/synthesis-dialogue.ts index ba86e6d71..eb14690e9 100644 --- a/lib/thingtalk-dialogues/synthesis-dialogue.ts +++ b/lib/thingtalk-dialogues/synthesis-dialogue.ts @@ -88,6 +88,17 @@ const enum PartialDialogueState { */ let partialDialogueID = 0; +/** + * Combine the id of the dialogue with the "tag" of the non-deterministic + * agent choice to form a single tag, used to identify contexts in the + * synthesis templates. + */ +function combineDialogueAgentTags(dialogueTag : number, agentTag : number) { + if (agentTag > 65535) + throw new Error('OVERFLOW: too many agent either choices'); + return dialogueTag << 16 | agentTag; +} + /** * A partial dialogue, during synthesis. * @@ -199,7 +210,7 @@ export default class SynthesisDialogue implements AbstractCommandIO, Synthesizer context: this, key: { dialogue: this, - tag: this._id << 65536 | agentTurn.tag + tag: combineDialogueAgentTags(this._id, agentTurn.tag), } }; } @@ -278,12 +289,10 @@ export default class SynthesisDialogue implements AbstractCommandIO, Synthesizer placeholders : TemplatePlaceholderMap, semantics : SemanticAction<[Ast.DialogueState, ...any[]], Ast.DialogueState>) { let ctxNonTerm; - if (tag === null) { + if (tag === null) ctxNonTerm = new NonTerminal('ctx_sys_dynamic_any', undefined, ['dialogue', this]); - } else { - assert(tag < 65536); - ctxNonTerm = new NonTerminal('ctx_sys_dynamic_any', undefined, ['tag', this._id << 65536 | tag]); - } + else + ctxNonTerm = new NonTerminal('ctx_sys_dynamic_any', undefined, ['tag', combineDialogueAgentTags(this._id, tag)]); addTemplate(this._userGenerator, [ctxNonTerm], tmpl, placeholders, (ctx : AgentTurn, ...args : any[]) : UserReplyRecord|null => { const result = semantics(ctx.state, ...args); diff --git a/lib/transaction-dialogues/context-phrases.ts b/lib/transaction-dialogues/context-phrases.ts index bc01a6267..fa334c3e6 100644 --- a/lib/transaction-dialogues/context-phrases.ts +++ b/lib/transaction-dialogues/context-phrases.ts @@ -324,7 +324,7 @@ export class ContextPhraseCreator { utterance.push(piece); } - if (utterance && utterance.length) { + if (utterance.length) { const value = [this.ctx, bag]; output.push({ symbol: this.contextTable.ctx_thingpedia_list_result, diff --git a/lib/transaction-dialogues/index.ts b/lib/transaction-dialogues/index.ts index 62c4963bc..20a6ad64b 100644 --- a/lib/transaction-dialogues/index.ts +++ b/lib/transaction-dialogues/index.ts @@ -290,7 +290,6 @@ export async function policy(dlg : DialogueInterface, startMode : PolicyStartMod switch (cmd.type) { case POLICY_NAME + '.end': dlg.say(dlg._("alright, {bye|good bye}!"), StateM.makeSimpleState(ctx.state, POLICY_NAME, 'sys_end')); - lastReply = await dlg.flush(); return; case POLICY_NAME + '.greet':