@vooodooo/magic
Version:
Vooodooo - AI orchestration platform
124 lines • 4.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtensionRegistry = void 0;
exports.createExtensionRegistry = createExtensionRegistry;
/**
* Registry for plugin extensions
*/
class ExtensionRegistry {
constructor() {
// Map of extension point IDs to their registered extensions
this.extensions = new Map();
// Map of extension IDs to their plugin IDs
this.extensionOwners = new Map();
}
/**
* Register an extension for a specific extension point
* @param pluginId ID of the plugin registering the extension
* @param extension Extension to register
*/
registerExtension(pluginId, extension) {
const extensionPointId = extension.extensionPointId;
// Generate a unique ID for this extension
const uniqueExtensionId = `${pluginId}:${extensionPointId}:${Date.now()}`;
// Ensure extension point exists in the map
if (!this.extensions.has(extensionPointId)) {
this.extensions.set(extensionPointId, []);
}
// Add the extension
this.extensions.get(extensionPointId).push({
...extension,
// Add a unique ID for the extension
id: uniqueExtensionId,
});
// Remember which plugin registered this extension
this.extensionOwners.set(uniqueExtensionId, pluginId);
console.log(`Registered extension for ${extensionPointId} from ${pluginId}`);
}
/**
* Get extensions for a specific extension point
* @param extensionPointId ID of the extension point
* @returns Array of extensions sorted by priority (highest first)
*/
getExtensions(extensionPointId) {
const extensions = this.extensions.get(extensionPointId) || [];
// Sort extensions by priority (highest first)
return [...extensions].sort((a, b) => {
const priorityA = a.priority || 0;
const priorityB = b.priority || 0;
return priorityB - priorityA;
});
}
/**
* Check if an extension point has any registered extensions
* @param extensionPointId ID of the extension point
* @returns True if there are extensions registered for this point
*/
hasExtensions(extensionPointId) {
return (this.extensions.has(extensionPointId) &&
this.extensions.get(extensionPointId).length > 0);
}
/**
* Remove all extensions from a specific plugin
* @param pluginId ID of the plugin
*/
removeExtensionsFromPlugin(pluginId) {
// Find all extensions owned by this plugin
const extensionsToRemove = Array.from(this.extensionOwners.entries())
.filter(([, owner]) => owner === pluginId)
.map(([id]) => id);
// Remove each extension
for (const extensionId of extensionsToRemove) {
// Find which extension point this extension belongs to
for (const [pointId, extensions] of this.extensions.entries()) {
// Remove the extension from the array
const updatedExtensions = extensions.filter((ext) => ext.id !== extensionId);
// Update the extensions map
if (updatedExtensions.length === 0) {
// Remove the extension point if there are no more extensions
this.extensions.delete(pointId);
}
else {
this.extensions.set(pointId, updatedExtensions);
}
}
// Remove from owners map
this.extensionOwners.delete(extensionId);
}
console.log(`Removed ${extensionsToRemove.length} extensions from plugin ${pluginId}`);
}
/**
* Get all registered extension points
* @returns Array of extension point IDs
*/
getExtensionPoints() {
return Array.from(this.extensions.keys());
}
/**
* Execute a function on all extensions for a specific extension point
* @param extensionPointId ID of the extension point
* @param fn Function to execute on each extension
*/
async executeExtensions(extensionPointId, fn) {
const extensions = this.getExtensions(extensionPointId);
const results = [];
for (const extension of extensions) {
try {
const result = await fn(extension);
results.push(result);
}
catch (error) {
console.error(`Error executing extension for ${extensionPointId}:`, error);
}
}
return results;
}
}
exports.ExtensionRegistry = ExtensionRegistry;
/**
* Create an extension registry
*/
function createExtensionRegistry() {
return new ExtensionRegistry();
}
//# sourceMappingURL=extension-registry.js.map