balena-io-modules / mahler

A automated task composer and HTN based planner for building autonomous system agents.
Apache License 2.0
7 stars 1 forks source link

Add test utility to generate mermaid diagrams #25

Closed pipex closed 1 year ago

pipex commented 1 year ago

This allows to visualize the planning process as a decision tree for debugging.

For example, the following task definition

import { Planner } from '@mahler/planner';
import { mermaid } from '@mahler/testing';

const inc = Task.of({
    condition: (state: number, { target }) => state < target,
    effect: (state: number) => state + 1,
    action: async (state: number) => state + 1,
    description: '+1',
});

const byTwo = Task.of({
    condition: (state: number, { target }) => target - state > 1,
    method: (_: number, { target }) => [inc({ target }), inc({ target })],
    description: '+2',
});

const trace = mermaid("find a plan");
const planner = Planner.of<number>({
    tasks: [byTwo, inc],
    config: { trace },
});

// Find a plan to increase from 0 to 3 
planner.findPlan(0, 3);

// Print diagram
console.log(trace.build())

Will print the code to generate the following diagram of the decision tree. The nodes in green joined by the solid arrows are the final plan.

mermaid-diagram-2023-08-09-203824

Change-type: minor

pipex commented 1 year ago

I self-certify!