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 support for dynamically created sensors #67

Closed pipex closed 5 months ago

pipex commented 5 months ago

This adds a capability to Mahler Agents to create/destroy sensors whenever a property the sensor is referencing via a lens is created/removed.

For example, let's think of an agent to create and track the state of multiple proceses

type Processes = { [pid: number]: { command: string[], status: 'running' | 'sleeping' | 'stopping' }};

This new feature allows creating a sensor as follows

const processMonitor = Sensor.of<Processes>.from({
   lens: '/:pid/status',
   sensor: async function* ({ pid }) {
      // yield the updated state of the process on changes
   }
 })

An agent created with the processMonitor sensor will create a new instance of the sensor and subscribe to it whenever a new process is created, and unsubscribe/destroy the sensor whenever the process is removed.

See here for an example within the tests

This PR also adds observable support to sensors and provides additional tools to work with observables

Change-type: minor

pipex commented 5 months ago

I self-certify!