A simplified and more expressive way to create plugins for the Phantomaton AI framework.
npm install phantomaton-plugins
import plugins from 'phantomaton-plugins';
// Create a plugin that exposes extension points
const api = plugins.create({
message: plugins.singleton,
effects: plugins.composite
});
// Create a plugin that provides implementations
const impl = plugins.create([
plugins.define(api.message).as('Hello, world!'),
plugins.define(api.effects).as(msg => console.log(msg))
]);
The plugins.create
function supports several argument patterns for flexibility:
// First argument can be an object mapping names to extension point types
const api = plugins.create({
single: plugins.singleton,
many: plugins.composite,
maybe: plugins.optional
});
// A function that takes config and returns an instance
const plugin = plugins.create(config => ({
value: config.someValue
}));
Components can be provided either as an array:
// Static array of components to install
const plugin = plugins.create([
plugins.define(other.extension).as(value)
]);
Or as a function that receives context:
// Function receiving { configuration, extensions, instance }
const plugin = plugins.create(({ instance }) => [
plugins.define(other.extension).as(instance.value)
]);
These patterns can be combined, with arguments processed in order:
- Extension point declarations (object)
- Instance creator (function, if not last argument)
- Component installer (array or function, must be last argument)
const plugin = plugins.create(
{ api: plugins.singleton }, // Extension points
config => ({ foo: config.foo }), // Instance creator
({ instance }) => [ // Component installer
plugins.define(other.ext).as(instance.foo)
]
);
The library includes start
and input
extension points from priestess
for defining application entry points:
const main = plugins.create([
plugins.define(plugins.start)
.with(plugins.input)
.as(input => () => console.log(input))
]);
Built on top of:
-
sigilium
for extension point patterns -
hierophant
for dependency injection -
priestess
for application lifecycle
We welcome contributions to the Phantomaton Plugins project! If you have any ideas, bug reports, or pull requests, please feel free to submit them on the Phantomaton Plugins GitHub repository.
MIT