Skip to content

Commit

Permalink
Merge pull request #469 from stanford-oval/wip/recording-mode-fix
Browse files Browse the repository at this point in the history
Fix logging: update the log whenever agent/user says something
  • Loading branch information
Silei Xu committed Feb 12, 2021
2 parents c0f477e + 2c164db commit c96402d
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 145 deletions.
76 changes: 66 additions & 10 deletions lib/dialogue-agent/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,41 @@ interface ResultLike {
toLocaleString(locale ?: string) : string;
}

export class DialogueTurnLog {
private readonly _turn : DialogueTurn;
private _done : boolean;

constructor() {
this._turn = {
context: null,
agent: null,
agent_target: null,
intermediate_context: null,
user: '',
user_target: ''
};
this._done = false;
}

get turn() {
return this._turn;
}

get done() {
return this._done;
}

finish() {
this._done = true;
}

update(field : keyof DialogueTurn, value : string) {
this._turn[field] = this._turn[field] ? this._turn[field] + '\n' + value : value;
}
}

class DialogueLog {
private readonly _turns : DialogueTurn[];
private readonly _turns : DialogueTurnLog[];
private _done : boolean;

constructor() {
Expand All @@ -100,9 +133,13 @@ class DialogueLog {

finish() {
this._done = true;
if (this.turns.length) {
const lastTurn = this.turns[this.turns.length - 1];
lastTurn.finish();
}
}

append(turn : DialogueTurn) {
append(turn : DialogueTurnLog) {
this._turns.push(turn);
}
}
Expand Down Expand Up @@ -434,10 +471,10 @@ export default class Conversation extends events.EventEmitter {
return this._log[this._log.length - 1];
}

lastTurn() {
private get _lastTurn() {
const lastDialogue = this._lastDialogue;
if (!lastDialogue || lastDialogue.turns.length === 0)
throw new Error('No dialogue is logged');
return null;
return lastDialogue.turns[lastDialogue.turns.length - 1];
}

Expand All @@ -451,7 +488,13 @@ export default class Conversation extends events.EventEmitter {
last.finish();
}

appendNewTurn(turn : DialogueTurn) {
turnFinished() {
const last = this._lastTurn;
if (last)
last.finish();
}

appendNewTurn(turn : DialogueTurnLog) {
const last = this._lastDialogue;
if (!last || last.done)
this.appendNewDialogue();
Expand All @@ -460,13 +503,26 @@ export default class Conversation extends events.EventEmitter {
}

voteLast(vote : 'up'|'down') {
const last = this.lastTurn();
last.vote = vote;
const last = this._lastTurn;
if (!last)
throw new Error('No dialogue is logged');
last.turn.vote = vote;
}

commentLast(comment : string) {
const last = this.lastTurn();
last.comment = comment;
const last = this._lastTurn;
if (!last)
throw new Error('No dialogue is logged');
last.turn.comment = comment;
}

updateLog(field : keyof DialogueTurn, value : string) {
let last = this._lastTurn;
if (!last || last.done) {
last = new DialogueTurnLog();
this.appendNewTurn(last);
}
last.update(field, value);
}

async saveLog() {
Expand All @@ -484,7 +540,7 @@ export default class Conversation extends events.EventEmitter {

serializer.pipe(output);
for (const dialogue of this._log)
serializer.write({ id: this.id, turns: dialogue.turns });
serializer.write({ id: this.id, turns: dialogue.turns.map((log) => log.turn) });
serializer.end();

await StreamUtils.waitFinish(output);
Expand Down
40 changes: 9 additions & 31 deletions lib/dialogue-agent/dialogue-loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import TextFormatter from './card-output/text-formatter';
import CardFormatter, { FormattedChunk } from './card-output/card-formatter';

import ExecutionDialogueAgent from './execution_dialogue_agent';
import { DialogueTurn } from "../dataset-tools/parsers";

// TODO: load the policy.yaml file instead
const POLICY_NAME = 'org.thingpedia.dialogue.transaction';
Expand Down Expand Up @@ -153,7 +152,6 @@ export default class DialogueLoop {
private _dialogueState : ThingTalk.Ast.DialogueState|null;
private _executorState : undefined;
private _lastNotificationApp : string|undefined;
private _currentTurn : DialogueTurn;

private _stopped = false;
private _mgrResolve : (() => void)|null;
Expand Down Expand Up @@ -193,14 +191,6 @@ export default class DialogueLoop {
rng: conversation.rng,
debug : this._debug
});
this._currentTurn = {
context : null,
agent : null,
agent_target : null,
intermediate_context : null,
user: '',
user_target: ''
};
this._dialogueState = null; // thingtalk dialogue state
this._executorState = undefined; // private object managed by DialogueExecutor
this._lastNotificationApp = undefined;
Expand Down Expand Up @@ -416,7 +406,8 @@ export default class DialogueLoop {

private async _doAgentReply() : Promise<[ValueCategory|null, number]> {
const oldState = this._dialogueState;
this._currentTurn.context = oldState ? oldState.prettyprint() : null;
if (oldState)
this.conversation.updateLog('context', oldState.prettyprint());

const policyResult = await this._policy.chooseAction(this._dialogueState);
if (!policyResult) {
Expand All @@ -428,9 +419,10 @@ export default class DialogueLoop {
[this._dialogueState, expect, utterance, entities, numResults] = policyResult;

const policyPrediction = ThingTalkUtils.computePrediction(oldState, this._dialogueState, 'agent');
this._currentTurn.agent_target = policyPrediction.prettyprint();
const agentTarget = policyPrediction.prettyprint();
this.conversation.updateLog('agent_target', agentTarget);
this.debug(`Agent act:`);
this.debug(this._currentTurn.agent_target);
this.debug(agentTarget);

if (this._useNeuralNLG()) {
const [contextCode, contextEntities] = this._prepareContextForPrediction(this._dialogueState, 'agent');
Expand All @@ -450,18 +442,6 @@ export default class DialogueLoop {
return [expect, numResults];
}

private _updateLog() {
this.conversation.appendNewTurn(this._currentTurn);
this._currentTurn = {
context: null,
agent: null,
agent_target: null,
intermediate_context: null,
user: '',
user_target: ''
};
}

private async _handleUICommand(type : CommandAnalysisType) {
switch (type) {
case CommandAnalysisType.STOP:
Expand Down Expand Up @@ -514,9 +494,9 @@ export default class DialogueLoop {
if (analyzed.type !== CommandAnalysisType.IGNORE) {
// save the utterance and complete the turn
// skip the log if the command was ignored
this._currentTurn.user = analyzed.utterance;
this._currentTurn.user_target = analyzed.parsed.prettyprint();
this._updateLog();
this.conversation.updateLog('user', analyzed.utterance);
this.conversation.updateLog('user_target', analyzed.parsed.prettyprint());
this.conversation.turnFinished();
}

switch (analyzed.type) {
Expand Down Expand Up @@ -700,7 +680,6 @@ export default class DialogueLoop {
await this.setExpected(null);
// if the dialogue terminated, save the last utterance from the agent
// in a new turn with an empty utterance from the user
this._updateLog();
this.conversation.dialogueFinished();
} else {
if (item instanceof QueueItem.UserInput) {
Expand Down Expand Up @@ -883,8 +862,7 @@ export default class DialogueLoop {
}

async reply(msg : string, icon ?: string|null) {
this._currentTurn.agent = this._currentTurn.agent ?
(this._currentTurn.agent + '\n' + msg) : msg;
this.conversation.updateLog('agent', msg);
await this.conversation.sendReply(msg, icon || this.icon);
}

Expand Down
Loading

0 comments on commit c96402d

Please sign in to comment.