UNPKG

mahler

Version:

A automated task composer and HTN based planner for building autonomous system agents

40 lines 1.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.zip = zip; const ref_1 = require("../ref"); const task_1 = require("../task"); function expand(s, method) { if (!method.condition(s)) { throw new Error(`${method.description}: condition not met for expanding method`); } const res = method(s); const instructions = Array.isArray(res) ? res : [res]; return instructions.flatMap((i) => (task_1.Method.is(i) ? expand(s, i) : i)); } /** * Packs an instruction into something that can be tested as a function * * If the instruction is a method, it will be expanded into a list of actions before * being executed. If a condition in the action sequence fails, no changes will be performed * to the state. */ function zip(ins) { return async function (s) { let actions; if (task_1.Method.is(ins)) { actions = expand(s, ins); } else { actions = [ins]; } const ref = ref_1.Ref.of(structuredClone(s)); for (const action of actions) { if (!action.condition(s)) { throw new Error(`${action.description}: condition not met for running action`); } await action(ref); } return ref._; }; } //# sourceMappingURL=zip.js.map