Skip to content

Commit

Permalink
One object for rules
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmaraev committed Aug 17, 2024
1 parent f66ef8f commit cd410e2
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 173 deletions.
137 changes: 76 additions & 61 deletions src/isu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { assign, createActor, setup, AnyMachineSnapshot, raise } from "xstate";
import { speechstate } from "speechstate";
import { createBrowserInspector } from "@statelyai/inspect";
import { KEY } from "./azure";
import { DMContext, DMEvent, UpdateEvent } from "./types";
import { preconditions, effects } from "./rules";
import { DMContext, DMEvent } from "./types";
import { rules } from "./rules";
import { nlg, nlu } from "./nlug";

const inspector = createBrowserInspector();
Expand All @@ -26,11 +26,14 @@ const settings = {
const dmMachine = setup({
guards: {
/** preconditions */
update: preconditions,
isu: ({ context }, params: { name: string }) =>
rules[params.name](context).preconditions,
},
actions: {
/** effects */
update: effects,
isu: assign(({ context }, params: { name: string }) => {
return { is: rules[params.name](context).effects };
}),
/** update latest_move (outside IS!) based on ASR/TTS (SAYS event) */
updateLatestMove: assign(({ event }) => {
console.debug("[DM updateLatestMove]", event);
Expand Down Expand Up @@ -66,7 +69,14 @@ const dmMachine = setup({
content: null,
},
is: {
private: { agenda: [] },
private: {
agenda: [
{
type: "greet",
content: null,
},
],
},
shared: { lu: undefined, qud: [], com: [] },
},
};
Expand Down Expand Up @@ -109,16 +119,16 @@ const dmMachine = setup({
ASR_NOINPUT: {
target: "Idle",
// FOR TESTING
// actions: raise({
// type: "SAYS",
// value: {
// speaker: "usr",
// move: {
// type: "ask",
// content: (x) => `favorite_food ${x}`,
// },
// },
// }),
actions: raise({
type: "SAYS",
value: {
speaker: "usr",
move: {
type: "ask",
content: (x: string) => `favorite_food ${x}`,
},
},
}),
},
},
},
Expand Down Expand Up @@ -153,26 +163,6 @@ const dmMachine = setup({
},
},
},
UpdateRules: {
on: {
UPDATE: [
{
guard: {
type: "update",
params: ({ event }: { event: UpdateEvent }) => ({
ruleName: event.value,
}),
},
actions: {
type: "update",
params: ({ event }: { event: UpdateEvent }) => ({
ruleName: event.value,
}),
},
},
],
},
},
DME: {
initial: "Update", // todo: shd be Select
states: {
Expand All @@ -183,10 +173,8 @@ const dmMachine = setup({
Init: {
always: {
target: "Grounding",
actions: raise({
type: "UPDATE",
value: "clear_agenda",
}),
guard: { type: "isu", params: { name: "clear_agenda" } },
actions: { type: "isu", params: { name: "clear_agenda" } },
},
},
Grounding: {
Expand All @@ -198,33 +186,60 @@ const dmMachine = setup({
{
type: "updateLatestMove",
},
raise({
type: "UPDATE",
value: "get_latest_move",
}),
{ type: "isu", params: { name: "get_latest_move" } },
],
},
},
},
Integrate: {
always: {
target: "Init",
actions: [
raise({
type: "UPDATE",
value: "integrate_sys_greet",
}),
raise({
type: "UPDATE",
value: "integrate_sys_ask",
}),
raise({
type: "UPDATE",
value: "integrate_usr_ask",
}),
],
},
always: [
{
target: "DowndateQUD",
guard: {
type: "isu",
params: { name: "integrate_sys_ask" },
},
actions: {
type: "isu",
params: { name: "integrate_sys_ask" },
},
},
{
target: "DowndateQUD",
guard: {
type: "isu",
params: { name: "integrate_usr_ask" },
},
actions: {
type: "isu",
params: { name: "integrate_usr_ask" },
},
},
{
target: "DowndateQUD",
guard: {
type: "isu",
params: { name: "integrate_greet" },
},
actions: {
type: "isu",
params: { name: "integrate_greet" },
},
},
],
},
DowndateQUD: {
always: { target: "LoadPlan" },
},
LoadPlan: {
always: { target: "ExecPlan" },
},
ExecPlan: {
type: "final",
},
},
onDone: {
target: "Select",
},
},
},
Expand All @@ -242,10 +257,10 @@ let is = dmActor.getSnapshot().context.is;
console.log("[IS (initial)]", is);
dmActor.subscribe((snapshot: AnyMachineSnapshot) => {
/* if you want to log some parts of the state */

// is !== snapshot.context.is && console.log("[IS]", snapshot.context.is);
is = snapshot.context.is;
// console.log("IS", is);
console.log("%cState value:", "background-color: #056dff", snapshot.value);
});

export function setupButton(element: HTMLElement) {
Expand Down
4 changes: 4 additions & 0 deletions src/nlug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const nluMapping: NLUMapping = {
type: "ask",
content: (x) => `favorite_food ${x}`,
},
Pizza: {
type: "answer",
content: "pizza",
},
};
const nlgMapping: NLGMapping = [
[
Expand Down
Loading

0 comments on commit cd410e2

Please sign in to comment.