@vooodooo/magic
Version:
Vooodooo - AI orchestration platform
149 lines • 4.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KnowledgeSystem = void 0;
exports.createKnowledgeSystem = createKnowledgeSystem;
/**
* System for storing, retrieving, and exchanging knowledge
*/
class KnowledgeSystem {
constructor(options = {}) {
/** In-memory storage for knowledge */
this.knowledgeStore = new Map();
/** Core knowledge contributed by plugins */
this.coreKnowledge = [];
this.options = {
enablePersistence: false,
...options,
};
// Load from persistent storage if enabled
if (this.options.enablePersistence && this.options.storageDir) {
try {
this.loadFromPersistentStorage();
}
catch (error) {
console.warn('Failed to load knowledge from persistent storage:', error);
}
}
}
/**
* Store knowledge in the system
* @param key Unique key for the knowledge
* @param data Knowledge data to store
* @param metadata Additional metadata about the knowledge
*/
async storeKnowledge(key, data, metadata = {}) {
const knowledgeItem = {
key,
data,
metadata,
updatedAt: new Date(),
};
this.knowledgeStore.set(key, knowledgeItem);
// Persist to storage if enabled
if (this.options.enablePersistence) {
await this.saveToStorage();
}
}
/**
* Retrieve knowledge from the system
* @param key Knowledge key to retrieve
* @returns The stored knowledge data, or null if not found
*/
async retrieveKnowledge(key) {
const item = this.knowledgeStore.get(key);
return item ? item.data : null;
}
/**
* Contribute knowledge to the core system
* @param knowledge Knowledge data to contribute
* @param metadata Additional metadata about the knowledge
*/
async contributeToCore(knowledge, metadata) {
// Store the contribution with metadata
this.coreKnowledge.push({
knowledge,
metadata,
contributedAt: new Date(),
});
// Process the contribution (analyze, learn, etc.)
await this.processContribution(knowledge, metadata);
// Persist to storage if enabled
if (this.options.enablePersistence) {
await this.saveToStorage();
}
}
/**
* Get a list of available knowledge keys
* @param pluginId Optional plugin ID to filter by
* @param pattern Optional pattern to filter keys
* @returns List of matching knowledge keys
*/
async listKnowledgeKeys(pluginId, pattern) {
let keys = Array.from(this.knowledgeStore.keys());
// Filter by plugin ID if provided
if (pluginId) {
keys = keys.filter((key) => {
const item = this.knowledgeStore.get(key);
return item && item.metadata.pluginId === pluginId;
});
}
// Filter by pattern if provided
if (pattern) {
const regex = new RegExp(pattern);
keys = keys.filter((key) => regex.test(key));
}
return keys;
}
/**
* Get all knowledge items
* @returns Array of all knowledge items
*/
getAllKnowledge() {
return Array.from(this.knowledgeStore.values());
}
/**
* Get all core knowledge contributions
* @returns Array of all core knowledge contributions
*/
getCoreKnowledge() {
return [...this.coreKnowledge];
}
/**
* Process a knowledge contribution
* @param knowledge Knowledge data to process
* @param metadata Metadata about the knowledge
*/
async processContribution(knowledge, metadata) {
// In a production system, this would analyze the knowledge,
// extract insights, and potentially update the core capabilities.
// For now, we just log the contribution.
console.log('Processing knowledge contribution:', {
from: metadata.pluginId,
type: metadata.type || 'unknown',
});
}
/**
* Load knowledge from persistent storage
*/
loadFromPersistentStorage() {
// In a production system, this would load from a file or database
// For now, this is a placeholder
console.log('Loading knowledge from persistent storage...');
}
/**
* Save knowledge to persistent storage
*/
async saveToStorage() {
// In a production system, this would save to a file or database
// For now, this is a placeholder
console.log('Saving knowledge to persistent storage...');
}
}
exports.KnowledgeSystem = KnowledgeSystem;
/**
* Create a knowledge system
*/
function createKnowledgeSystem(options) {
return new KnowledgeSystem(options);
}
//# sourceMappingURL=knowledge-system.js.map