feat: add external plugin loader and examples for Brainy

Introduced `pluginLoader.ts` to enable loading and configuring augmentation plugins from external packages. Added `examples/externalPlugins.js` to demonstrate usage. Enhanced storage status reporting in `opfsStorage` and `BrainyData` with detailed storage capacity and usage methods. Updated README with an external plugin usage guide.
This commit is contained in:
David Snelling 2025-05-28 15:39:14 -07:00
parent 767c349f63
commit b031a40b1c
11 changed files with 2220 additions and 1033 deletions

View file

@ -577,7 +577,90 @@ This enum can be used by consumers of the library to identify the different type
Brainy provides an event pipeline that allows registering and executing multiple augmentations of each type. The pipeline supports different execution modes and provides a flexible way to manage augmentations.
#### Using the Pipeline
#### Using External Augmentation Plugins
Brainy provides a plugin loader that makes it easy to load and configure augmentation plugins from external npm packages. This allows you to extend Brainy's capabilities with specialized augmentations developed by third parties or maintained in separate packages.
```typescript
import {
BrainyData,
configureAndStartPipeline,
createSensePluginConfig,
createConduitPluginConfig,
AugmentationType
} from '@soulcraft/brainy';
// Create a new BrainyData instance
const db = new BrainyData();
await db.init();
// Configure and start the augmentation pipeline with external plugins
// Note: These are example plugin names. You would need to install these packages via npm first.
const result = await configureAndStartPipeline([
// Sense augmentations (will be loaded first)
createSensePluginConfig('@example/text-sense-augmentation', {
language: 'english',
enableNER: true
}),
// Conduit augmentations for two-way synchronization
createConduitPluginConfig('@example/websocket-conduit', {
url: 'wss://example.com/sync',
reconnectInterval: 5000
}),
// Other augmentation types
{
plugin: '@example/memory-augmentation',
config: { storageType: 'persistent' },
type: AugmentationType.MEMORY
}
], {
// Optional: Customize the loading options
useDefaultPipeline: true, // Use the default pipeline instance
initializeAfterLoading: true, // Initialize augmentations after loading
augmentationOrder: [ // Custom order (Sense first, as recommended)
AugmentationType.SENSE,
AugmentationType.CONDUIT,
AugmentationType.MEMORY,
AugmentationType.COGNITION,
AugmentationType.PERCEPTION,
AugmentationType.DIALOG,
AugmentationType.ACTIVATION
]
});
// Get the pipeline instance
const { pipeline } = result;
// Use the pipeline to execute augmentations
const senseResults = await pipeline.executeSensePipeline(
'processRawData',
['This is some example text to process', 'text']
);
// Process the results and insert data into BrainyData
for (const resultPromise of senseResults) {
const result = await resultPromise;
if (result.success) {
// Insert the extracted nouns and verbs into BrainyData according to graphTypes
for (const noun of result.data.nouns) {
await db.add(noun, { type: 'noun' });
}
}
}
```
The plugin loader provides several functions for working with external plugins:
- `loadPlugins(plugins, options)`: Loads augmentation plugins from external npm packages
- `configureAndStartPipeline(plugins, options)`: Configures and starts the augmentation pipeline with the specified plugins
- `createSensePluginConfig(plugin, config)`: Creates a plugin configuration for a sense augmentation
- `createConduitPluginConfig(plugin, config)`: Creates a plugin configuration for a conduit augmentation
For more details, see the [externalPlugins.js](examples/externalPlugins.js) example.
#### Using the Pipeline Directly
```typescript
import { augmentationPipeline, ExecutionMode, AugmentationType } from '@soulcraft/brainy';