UNPKG

universal-ai-brain

Version:

🧠 UNIVERSAL AI BRAIN 3.3 - The world's most advanced cognitive architecture with 24 specialized systems, MongoDB 8.1 $rankFusion hybrid search, latest Voyage 3.5 embeddings, and framework-agnostic design. Works with Mastra, Vercel AI, LangChain, OpenAI A

479 lines (417 loc) 15.3 kB
/** * @file MastraAdapter - Integration adapter for Mastra framework * * This adapter integrates the Universal AI Brain with Mastra framework, * providing intelligent memory, context injection, and MongoDB-powered features * to Mastra agents and workflows. */ import { BaseFrameworkAdapter } from './BaseFrameworkAdapter'; import { UniversalAIBrain } from '../UniversalAIBrain'; import { FrameworkAdapter, FrameworkCapabilities, AdapterConfig } from '../types'; // Mastra framework types (will be imported from @mastra/core when available) interface MastraAgent { name: string; instructions: string; model: any; memory?: any; tools?: Record<string, any>; generate(messages: any[], options?: any): Promise<{ text: string; [key: string]: any }>; stream(messages: any[], options?: any): Promise<any>; } interface MastraMemory { store(data: any): Promise<void>; retrieve(query: string): Promise<any[]>; } interface MastraWorkflow { name: string; steps: any[]; execute(data: any): Promise<any>; } export interface MastraAdapterConfig extends AdapterConfig { enableWorkflowIntegration?: boolean; enableMemoryReplacement?: boolean; enableToolEnhancement?: boolean; } /** * MastraAdapter - Integrates Universal AI Brain with Mastra framework * * Features: * - Replaces Mastra memory with MongoDB-powered memory * - Enhances agent prompts with intelligent context injection * - Integrates with Mastra workflows for multi-step operations * - Provides MongoDB vector search capabilities */ export class MastraAdapter extends BaseFrameworkAdapter<MastraAgent> { public readonly frameworkName = 'Mastra'; public readonly version = '1.0.0'; private originalMemory: Map<string, MastraMemory> = new Map(); private enhancedAgents: Map<string, MastraAgent> = new Map(); constructor(config?: Partial<MastraAdapterConfig>) { super({ enableMemoryInjection: true, enableContextEnhancement: true, enableToolIntegration: true, enableWorkflowIntegration: true, enableMemoryReplacement: true, enableToolEnhancement: true, ...config }); } /** * Integrate with Mastra framework */ async integrate(brain: UniversalAIBrain): Promise<MastraAgent> { await this.initialize(brain); // Return a factory function for creating enhanced Mastra agents return this.createEnhancedAgentFactory(); } /** * Create an enhanced Mastra agent with MongoDB superpowers */ createEnhancedAgent(config: { name: string; instructions: string; model: any; tools?: Record<string, any>; memory?: MastraMemory; }): MastraAgent { if (!this.brain) { throw new Error('MastraAdapter not initialized. Call integrate() first.'); } const agentId = `mastra_${config.name.toLowerCase().replace(/\s+/g, '_')}`; // Create enhanced agent with MongoDB-powered features const enhancedAgent: MastraAgent = { name: config.name, instructions: config.instructions, model: config.model, tools: config.tools, memory: this.createMongoDBMemory(agentId), // Enhanced generate method with context injection // Following REAL Mastra pattern: resourceId and threadId are REQUIRED for memory generate: async (messages: any[], options?: { resourceId?: string; threadId?: string; [key: string]: any }) => { try { // Validate REAL Mastra memory requirements if (!options?.resourceId || !options?.threadId) { console.warn('⚠️ Mastra memory requires both resourceId and threadId. Memory will not be used.'); } // Extract the latest user message const userMessage = this.extractUserMessage(messages); // Enhance prompt with MongoDB context const enhanced = await this.brain!.enhancePrompt(userMessage, { frameworkType: 'mastra', conversationId: options?.threadId || agentId, userId: options?.resourceId, enhancementStrategy: 'hybrid' }); // Replace user message with enhanced prompt const enhancedMessages = this.replaceUserMessage(messages, enhanced.enhancedPrompt); // Call REAL Mastra model with enhanced prompt const result = await this.callOriginalModel(config.model, enhancedMessages, options); // Store interaction for learning (following Mastra memory pattern) await this.brain!.storeInteractionPublic({ conversationId: options?.threadId || agentId, userMessage, assistantResponse: result.text, context: enhanced.injectedContext, framework: 'mastra', metadata: { agentName: config.name, modelUsed: config.model?.name || 'unknown', enhancementStrategy: 'hybrid', resourceId: options?.resourceId, threadId: options?.threadId, mastraMemoryEnabled: !!(options?.resourceId && options?.threadId) } }); return { ...result, enhancedContext: enhanced.injectedContext, originalPrompt: userMessage, enhancedPrompt: enhanced.enhancedPrompt }; } catch (error) { console.error('Error in enhanced Mastra agent generate:', error); // Fallback to original model return this.callOriginalModel(config.model, messages, options); } }, // Enhanced stream method with context injection stream: async (messages: any[], options?: any) => { try { const userMessage = this.extractUserMessage(messages); const enhanced = await this.brain!.enhancePrompt(userMessage, { frameworkType: 'mastra', conversationId: options?.conversationId || agentId, enhancementStrategy: 'hybrid' }); const enhancedMessages = this.replaceUserMessage(messages, enhanced.enhancedPrompt); // Call original model stream with enhanced prompt const stream = await this.callOriginalModelStream(config.model, enhancedMessages, options); // Store interaction (we'll collect the full response when stream completes) this.storeStreamInteraction( options?.conversationId || agentId, userMessage, enhanced.injectedContext, config.name, stream ); return stream; } catch (error) { console.error('Error in enhanced Mastra agent stream:', error); return this.callOriginalModelStream(config.model, messages, options); } } }; this.enhancedAgents.set(agentId, enhancedAgent); return enhancedAgent; } /** * Create MongoDB-powered memory for Mastra agents */ private createMongoDBMemory(agentId: string): MastraMemory { return { store: async (data: any) => { if (!this.brain) return; try { await this.brain.storeInteractionPublic({ conversationId: agentId, userMessage: data.input || JSON.stringify(data), assistantResponse: data.output || '', context: [], framework: 'mastra', metadata: { type: 'memory_store', agentId, timestamp: new Date() } }); } catch (error) { console.error('Error storing Mastra memory:', error); } }, retrieve: async (query: string) => { if (!this.brain) return []; try { const context = await this.brain.retrieveRelevantContext(query, { conversationId: agentId, framework: 'mastra', limit: 10 }); return context.map(ctx => ({ content: ctx.content, metadata: ctx.metadata, relevanceScore: ctx.relevanceScore, timestamp: ctx.timestamp })); } catch (error) { console.error('Error retrieving Mastra memory:', error); return []; } } }; } // Framework-specific implementation methods public checkFrameworkAvailability(): boolean { try { // Try to import REAL Mastra framework require.resolve('@mastra/core'); return true; } catch { console.warn('⚠️ Mastra framework not found. Install with: npm install @mastra/core'); return false; } } public checkVersionCompatibility(): boolean { try { const packageJson = require('@mastra/core/package.json'); const version = packageJson.version; // Check if version is 0.10.0 or higher (based on docs) const [major, minor] = version.split('.').map(Number); if (major > 0 || (major === 0 && minor >= 10)) { return true; } console.warn(`⚠️ Mastra version ${version} detected. Version 0.10.0+ recommended.`); return false; } catch { // If we can't check version, assume it's compatible return true; } } protected async setupFrameworkIntegration(): Promise<void> { console.log('🔌 Setting up Mastra framework integration...'); if (!this.checkFrameworkAvailability()) { console.warn('⚠️ Mastra framework not available - adapter will use fallback mode'); return; } if (!this.checkVersionCompatibility()) { console.warn('⚠️ Mastra framework version compatibility issue detected'); } console.log('✅ Mastra integration ready'); } protected createIntelligentTools(): any[] { return [ { name: 'mongodb_search', description: 'Search MongoDB knowledge base for relevant information', execute: async (query: string) => { if (!this.brain) return { error: 'Brain not initialized' }; const results = await this.brain.retrieveRelevantContext(query, { limit: 5, framework: 'mastra' }); return { results: results.map(r => ({ content: r.content, relevanceScore: r.relevanceScore, source: r.source })) }; } } ]; } // Helper methods private extractUserMessage(messages: any[]): string { const userMsg = messages.find(m => m.role === 'user'); return userMsg?.content || ''; } private replaceUserMessage(messages: any[], enhancedContent: string): any[] { return messages.map(msg => msg.role === 'user' ? { ...msg, content: enhancedContent } : msg ); } private async callOriginalModel(model: any, messages: any[], options?: any): Promise<{ text: string; [key: string]: any }> { try { // Import REAL Mastra Agent class const { Agent } = await import('@mastra/core'); // Create REAL Mastra agent const agent = new Agent({ name: 'Enhanced Agent', instructions: 'You are an enhanced agent powered by MongoDB context.', model: model }); // Call REAL Mastra agent.generate() method const result = await agent.generate(messages, options); return { text: result.text || (result as any).content || result, model: model?.name || 'unknown', usage: result.usage || { tokens: 0 } }; } catch (error) { // If Mastra not available, provide graceful fallback if (error.code === 'MODULE_NOT_FOUND') { console.warn('⚠️ Mastra framework not installed. Install with: npm install @mastra/core'); return { text: `[Fallback] Enhanced response would be generated here with MongoDB context. Install '@mastra/core' package for real integration.`, model: model?.name || 'unknown', usage: { tokens: 0 }, fallback: true }; } throw error; } } private async callOriginalModelStream(model: any, messages: any[], options?: any): Promise<any> { try { // Import REAL Mastra Agent class const { Agent } = await import('@mastra/core'); // Create REAL Mastra agent const agent = new Agent({ name: 'Enhanced Streaming Agent', instructions: 'You are an enhanced streaming agent powered by MongoDB context.', model: model }); // Call REAL Mastra agent.stream() method const stream = await agent.stream(messages, options); return stream; } catch (error) { // If Mastra not available, provide graceful fallback if (error.code === 'MODULE_NOT_FOUND') { console.warn('⚠️ Mastra framework not installed. Install with: npm install @mastra/core'); return { async *[Symbol.asyncIterator]() { yield { type: 'text', content: '[Fallback] Enhanced streaming response would be generated here with MongoDB context. ' }; yield { type: 'text', content: 'Install "@mastra/core" package for real integration.' }; } }; } throw error; } } private async storeStreamInteraction( conversationId: string, userMessage: string, context: any[], agentName: string, stream: any ): Promise<void> { // Collect stream response and store interaction // This would be implemented based on actual Mastra stream format } private createEnhancedAgentFactory(): any { return { createAgent: (config: any) => this.createEnhancedAgent(config), adapter: this, brain: this.brain }; } /** * Get framework capabilities */ getCapabilities(): FrameworkCapabilities { return { supportsStreaming: true, supportsTools: true, supportsMultiModal: false, supportsMemory: true, supportedModels: [ 'gpt-4o', 'gpt-4o-mini', 'claude-3-5-sonnet', 'gemini-pro' ], maxContextLength: 128000 }; } /** * Enhanced framework integration method */ enhanceWithBrain(originalFunction: any, brain: UniversalAIBrain): any { return async (...args: any[]) => { try { // Extract agent configuration from arguments const config = args[0]; if (config && typeof config === 'object') { // Create enhanced agent instead of original return this.createEnhancedAgent(config); } return await originalFunction(...args); } catch (error) { console.error('Error in enhanceWithBrain:', error); return await originalFunction(...args); } }; } /** * Validate that the adapter is working with REAL Mastra framework */ async validateRealIntegration(): Promise<boolean> { try { // Try to import the actual Mastra classes const mastraCore = await import('@mastra/core'); const { Agent } = mastraCore; const Memory = (mastraCore as any).Memory; // Verify they are constructors if (typeof Agent !== 'function' || typeof Memory !== 'function') { return false; } console.log('✅ REAL Mastra framework integration validated'); return true; } catch (error) { console.warn('⚠️ REAL Mastra framework not available - using fallback mode'); return false; } } }