UNPKG

mahler

Version:

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

97 lines 3.53 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Planner = void 0; const distance_1 = require("../distance"); const task_1 = require("../task"); const findPlan_1 = require("./findPlan"); const types_1 = require("./types"); const assert_1 = require("../assert"); const DAG = require("../dag"); __exportStar(require("./types"), exports); __exportStar(require("./plan"), exports); __exportStar(require("./node"), exports); function from({ tasks: inputTasks = [], config = {}, }) { const ids = new Set(); let tasks = []; // Remove repeated tasks for (const task of inputTasks) { if (!ids.has(task.id)) { ids.add(task.id); tasks.push(task); } } // Sort the tasks putting methods and redirects first tasks = tasks.sort((a, b) => { if (task_1.Task.isMethod(a) && task_1.Task.isAction(b)) { return -1; } if (task_1.Task.isAction(a) && task_1.Task.isMethod(b)) { return 1; } return 0; }); const { trace = () => { /* noop */ }, maxSearchDepth = 1000, } = config; return { findPlan(current, target) { // We clone the current state so the plan cannot be affected // by external changes (e.g. from sensors) current = structuredClone(current); const time = performance.now(); trace({ event: 'start', target }); try { const res = (0, findPlan_1.findPlan)({ initialPlan: { success: true, state: current, start: null, stats: { iterations: 0, maxDepth: 0, time: 0 }, pendingChanges: [], }, distance: distance_1.Distance.from(current, target), tasks, trace, maxSearchDepth, }); res.stats = { ...res.stats, time: performance.now() - time }; if (res.success) { const start = DAG.reverse(res.start); (0, assert_1.assert)(!Array.isArray(start)); res.start = start; trace({ event: 'success', start }); } else { trace({ event: 'failed' }); } return res; } catch (e) { if (e instanceof types_1.Aborted) { trace(e); trace({ event: 'failed' }); return { success: false, stats: e.stats, error: e }; } throw e; } }, }; } exports.Planner = { from, }; //# sourceMappingURL=index.js.map