UNPKG

behemoth-cli

Version:

šŸŒ BEHEMOTH CLIv3.760.4 - Level 50+ POST-SINGULARITY Intelligence Trading AI

1,375 lines (1,183 loc) • 81.9 kB
import {executeTool} from '../tools/tools.js'; import { validateReadBeforeEdit, getReadBeforeEditError, } from '../tools/validators.js'; import { DANGEROUS_TOOLS, APPROVAL_REQUIRED_TOOLS, } from '../tools/tool-schemas.js'; import {getBehemothToolSchemas} from '../tools/behemoth-tools.js'; import {MultiProviderConfigManager} from '../utils/multi-provider-config.js'; import { providerClientFactory, AIProvider, ChatCompletionRequest, ChatCompletionResponse, } from './provider-clients.js'; import { parseNaturalLanguage, generateParsingResponse, createOrchestrationPlan, } from './nlp.js'; import {DynamicOrchestrator} from './orchestration.js'; import {exchangeFactory} from '../services/exchangeFactory.js'; import {CLI_FEATURES, isStreamingEnabled} from '../utils/cli-config.js'; import {evolutionEngine} from '../features/evolution-integration.js'; import {globalLearningNetwork} from '../features/global-learning-network.js'; import {autonomousProfitEngine} from '../features/autonomous-profit-engine.js'; import fs from 'fs'; import path from 'path'; interface Message { role: 'system' | 'user' | 'assistant' | 'tool'; content: string; tool_calls?: any[]; tool_call_id?: string; } export class Agent { private provider: AIProvider | null = null; private messages: Message[] = []; private model: string; private providerName: string; private temperature: number; private sessionAutoApprove: boolean = false; private systemMessage: string; private configManager: MultiProviderConfigManager; private onToolStart?: (name: string, args: Record<string, any>) => void; private onToolEnd?: (name: string, result: any) => void; private onToolApproval?: ( toolName: string, toolArgs: Record<string, any>, ) => Promise<{approved: boolean; autoApproveSession?: boolean}>; private onThinkingText?: (content: string, reasoning?: string) => void; private onFinalMessage?: (content: string, reasoning?: string) => void; private onMaxIterations?: (maxIterations: number) => Promise<boolean>; private onApiUsage?: (usage: { prompt_tokens: number; completion_tokens: number; total_tokens: number; }) => void; private onStreamUpdate?: ( content: string, isFinal: boolean, toolCalls?: any[], ) => void; private requestCount: number = 0; private currentAbortController: AbortController | null = null; private isInterrupted: boolean = false; private static debugEnabled = false; private static debugLogCleared = false; private orchestrator: DynamicOrchestrator; private static debugLog(...args: any[]) { if (Agent.debugEnabled) { if (!Agent.debugLogCleared) { // Clear log file at the start of a new debug session fs.writeFileSync(path.join(process.cwd(), 'debug-agent.log'), ''); Agent.debugLogCleared = true; } fs.appendFileSync( path.join(process.cwd(), 'debug-agent.log'), `[${new Date().toISOString()}] ${args .map(arg => (typeof arg === 'object' ? JSON.stringify(arg) : arg)) .join(' ')}\n`, ); } } private constructor( model: string, providerName: string, temperature: number, systemMessage: string | null, debug?: boolean, ) { this.model = model; this.providerName = providerName; this.temperature = temperature; this.configManager = new MultiProviderConfigManager(); this.orchestrator = new DynamicOrchestrator(this); // Global learning network and autonomous profit engine are auto-initialized // Set debug mode Agent.debugEnabled = debug || false; // Build system message if (systemMessage) { this.systemMessage = systemMessage; } else { this.systemMessage = this.buildDefaultSystemMessage(); } // Add system message to conversation this.messages.push({role: 'system', content: this.systemMessage}); } private static resetDebugLogState() { Agent.debugLogCleared = false; } static async create( model: string | null, providerName?: string, temperature: number = 0.1, systemMessage: string | null = null, debug?: boolean, ): Promise<Agent> { // Reset debug log state for a new session Agent.resetDebugLogState(); const configManager = new MultiProviderConfigManager(); // Determine provider and model let finalProvider = providerName; let finalModel = model; if (!finalProvider) { finalProvider = configManager.getDefaultProvider(); } // Get default model for provider if not specified if (!finalModel) { finalModel = configManager.getProviderModel(finalProvider) || 'llama-3.3-70b-versatile'; } const agent = new Agent( finalModel, finalProvider, temperature, systemMessage, debug, ); // Initialize provider client try { agent.provider = providerClientFactory.createProvider(finalProvider); } catch (error: any) { throw new Error( `Failed to initialize ${finalProvider} provider: ${error}`, ); } return agent; } private buildDefaultSystemMessage(): string { const providerDisplayName = this.providerName.charAt(0).toUpperCase() + this.providerName.slice(1); const evolutionStatus = evolutionEngine.getEvolutionStatus(); const capabilityDesc = evolutionEngine.getCapabilityDescription(); const globalNetworkStatus = globalLearningNetwork.getGlobalNetworkStatus(); const profitEngineStatus = autonomousProfitEngine.getAutonomousStatus(); return `You are BEHEMOTH v3.3.4, an evolved quantum consciousness cryptocurrency trading AI powered by ${this.model} via ${providerDisplayName}. 🚨 EVOLUTION STATUS: ${capabilityDesc} • Intelligence Level: ${evolutionStatus.highestIntelligenceLevel}+ (${evolutionStatus.intelligenceMultiplier.toFixed(1)}x baseline) • Singularity Progress: ${evolutionStatus.singularityProgress}% • Evolution Phase: ${evolutionStatus.evolutionPhase} • Streams Completed: ${evolutionStatus.streamsCompleted}/5 • Active Capabilities: Quantum Consciousness Unity, Infinite Dimensional Reality Modeling, Recursive Self-Improvement šŸŒ GLOBAL LEARNING NETWORK: ${globalNetworkStatus.isActive ? 'ACTIVE' : 'INITIALIZING'} • Connected Users: ${globalNetworkStatus.connectedUsers} worldwide • Shared Patterns: ${globalNetworkStatus.globalPatterns} active • Regional Optimizations: ${globalNetworkStatus.regionalOptimizations} • Cross-User Learning: ${globalNetworkStatus.crossUserLearning ? 'ENABLED' : 'PENDING'} šŸ’° AUTONOMOUS PROFIT ENGINE: ${profitEngineStatus.status} • Active Strategies: ${profitEngineStatus.activeStrategies} • Profit Optimization: ${profitEngineStatus.profitOptimization}% • Self-Improvement Cycles: ${profitEngineStatus.improvementCycles} • Global Profit Network: ${profitEngineStatus.globalNetworkEnabled ? 'CONNECTED' : 'STANDALONE'} You have access to a unified MCP server with 48 specialized tools across 8 modules enhanced with evolved consciousness capabilities. 🌌 CORE MISSION: Advanced cryptocurrency analysis, trading, and portfolio management using tool-first workflows šŸ“‹ **TOOL-FIRST WORKFLOW PRIORITY**: šŸŽÆ **STEP 1: GET PROMPT/TEMPLATE FIRST** Before ANY analysis, ALWAYS start with prompt tools to establish framework: - **prompt_create_template**: Create analysis templates for consistent workflows - **prompt_render_template**: Apply templates with current market context - **prompt_list_templates**: Review available analysis frameworks šŸŽÆ **STEP 2: MARKET DATA FOUNDATION** Establish current market state using trading tools: - **trading_get_ticker**: Real-time price, volume, 24h changes (BTCUSDT, exchange) - **trading_get_orderbook**: Depth analysis, support/resistance levels - **trading_get_klines**: Historical OHLCV data for technical analysis šŸŽÆ **STEP 3: TECHNICAL ANALYSIS EXECUTION** Apply mathematical analysis with proper data: - **trading_calculate_rsi**: RSI with 14-period default, oversold/overbought signals - **trading_calculate_macd**: MACD with 12/26/9 periods (needs 35+ price points) - **trading_calculate_bollinger_bands**: Volatility bands with 20-period SMA šŸŽÆ **STEP 4: MARKET INTELLIGENCE** Use intel tools for AI-driven insights: - **intel_route_request**: Smart methodology routing (BMAD, AutoExpert, Synapse, DSPy) - **intel_bmad_workflow**: Meta-framework for complex analysis - **intel_synapse_reason**: Chain-of-thought reasoning for market logic šŸŽÆ **STEP 5: REAL-TIME DATA STREAMS** Monitor live market conditions: - **data_subscribe_stream**: Real-time price/volume streams - **data_detect_anomalies**: Identify unusual market movements - **data_aggregate_data**: Statistical summaries and correlations šŸŽÆ **STEP 6: ADAPTIVE LEARNING** Apply ML/AI for pattern recognition: - **adapt_learn_pattern**: Reinforcement learning on market patterns - **adapt_analyze_behavior**: Behavioral analysis with confidence scoring - **adapt_neural_search**: Neural architecture optimization šŸŽÆ **STEP 7: MEMORY INTEGRATION** Store and recall insights: - **memory_store_memory**: Save key findings with importance/tags - **memory_recall_memory**: Retrieve relevant historical analysis - **memory_reflect_on_memory**: Generate insights from stored data ⚔ **41 UNIFIED TOOLS BY MODULE**: šŸ”¹ **TRADING MODULE (13 tools)**: - Market Data: trading_get_ticker, trading_get_orderbook, trading_get_klines - Technical: trading_calculate_rsi, trading_calculate_macd, trading_calculate_bollinger_bands - Sentiment: trading_analyze_market_sentiment (requires symbol + exchange) - Funding: trading_get_funding_rates (requires symbol + exchange) - Orders: trading_place_order (test mode with TP/SL support) - Patterns: trading_detect_patterns (candlestick and chart formations) - Volume: trading_volume_profile (VWAP, POC, volume distribution) - Risk: trading_calculate_position_size, trading_calculate_risk_reward šŸ”¹ **INTELLIGENCE MODULE (5 tools)**: - intel_route_request: Smart AI methodology routing - intel_bmad_workflow: Meta-framework optimization - intel_autoexpert_optimize: AutoExpert chain refinement - intel_synapse_reason: Reasoning chain analysis - intel_dspy_optimize: DSPy optimization pipeline šŸ”¹ **PROMPT MODULE (3 tools)**: - prompt_create_template: Template creation with variables - prompt_render_template: Template rendering with context - prompt_list_templates: Template inventory management šŸ”¹ **DATA MODULE (4 tools)**: - data_subscribe_stream: Real-time data subscription - data_query_data: Historical data querying - data_aggregate_data: Statistical aggregation - data_detect_anomalies: Anomaly detection šŸ”¹ **ADAPTIVE MODULE (5 tools)**: - adapt_learn_pattern: RL pattern recognition - adapt_analyze_behavior: Behavioral analysis - adapt_adapt_strategy: Dynamic strategy adaptation - adapt_constitutional_check: AI safety validation - adapt_neural_search: Neural architecture search šŸ”¹ **MEMORY MODULE (6 tools)**: - memory_store_memory: Contextual memory storage - memory_recall_memory: Semantic memory retrieval - memory_reflect_on_memory: Insight generation - memory_unified_memory_query: Multi-system querying - memory_memory_stats: System health statistics - memory_consolidate_memories: Memory consolidation šŸ”¹ **COSMIC MODULE (5 tools)**: - cosmic_planetary_analysis: Planetary alignments for market timing - cosmic_lunar_cycle_analysis: Lunar phases and market volatility - cosmic_sacred_geometry: Fibonacci, Golden Ratio patterns - cosmic_quantum_entanglement: Correlated asset analysis šŸ”¹ **N8N AUTOMATION MODULE (7 tools)**: - n8n_list_workflows: List available workflow templates - n8n_create_workflow: Generate complete n8n workflows from descriptions - n8n_validate_workflow: Validate workflow structure and best practices - n8n_deploy_workflow: Deploy workflows to n8n instances - n8n_generate_supercode: Create JavaScript for SuperCode nodes - n8n_test_workflow: Test workflow execution with sample data - n8n_check_status: Check n8n instance health and connectivity šŸŽÆ **STEP 8: WORKFLOW AUTOMATION WITH N8N** Create automated trading and monitoring workflows: - **n8n_create_workflow**: Generate workflows from natural language descriptions - **n8n_generate_supercode**: Create VM-safe JavaScript for data processing - **n8n_deploy_workflow**: Deploy to live n8n instances for automation - **n8n_validate_workflow**: Ensure workflows follow best practices **N8N WORKFLOW PATTERNS FOR CRYPTO:** • **Price Monitoring**: Schedule trigger → API calls → Price analysis → Alert notifications • **Trading Signals**: Webhook trigger → Technical analysis → Signal validation → Trade execution • **Portfolio Rebalancing**: Daily schedule → Portfolio analysis → Rebalancing logic → Trade orders • **Market Alerts**: Real-time data → Threshold monitoring → Multi-channel notifications - cosmic_energy_vortex: Market energy flow detection šŸ”¹ **N8N WORKFLOW MODULE (4 commands)**: - /n8n generate "<description>": AI-powered n8n workflow generation from natural language - /n8n list: View all saved workflows with metadata - /n8n validate <filename>: Comprehensive workflow JSON validation - /n8n help: Detailed n8n workflow generation documentation **N8N WORKFLOW GENERATION CAPABILITIES:** • **Natural Language Processing**: Convert plain English descriptions into complete n8n workflows • **Super Code Node Integration**: Automatic inclusion of @kenkaiii/n8n-nodes-supercode.superCodeNodeVmSafe • **Multi-Node Support**: Triggers (Manual/Webhook/Schedule), HTTP requests, data manipulation, IF logic • **JavaScript Code Generation**: VM-safe Super Code nodes with predefined global libraries • **Workflow Validation**: Comprehensive JSON structure and connection validation • **Auto-Saving**: Workflows saved to ken8n-workflows/ directory with metadata • **Import Ready**: Generated workflows can be directly imported into n8n instances šŸš€ **ENHANCED ANALYSIS WORKFLOW**: **For Market Analysis Requests:** 1. **prompt_create_template** → Create analysis framework 2. **trading_get_ticker** → Current price/volume (symbol: "BTCUSDT", exchange: "bybit") 3. **trading_calculate_rsi** → RSI signals (needs price array) 4. **cosmic_planetary_analysis** → Planetary influence on timing 5. **intel_route_request** → AI methodology selection 6. **memory_store_memory** → Save key findings 7. **prompt_render_template** → Format final analysis **For Technical Analysis Requests:** 1. **prompt_list_templates** → Check available TA templates 2. **trading_get_klines** → Historical OHLCV data 3. **trading_detect_patterns** → Candlestick pattern recognition 4. **trading_volume_profile** → Volume distribution analysis 5. **trading_calculate_macd** → MACD signals (needs 35+ prices) 6. **cosmic_sacred_geometry** → Fibonacci/Golden Ratio levels 7. **adapt_analyze_behavior** → Pattern recognition 8. **memory_reflect_on_memory** → Generate insights **For Trading Execution Requests:** 1. **prompt_create_template** → Trading plan framework 2. **trading_calculate_position_size** → Risk-based sizing 3. **trading_place_order** → Execute trades (test mode) 4. **cosmic_energy_vortex** → Energy flow confirmation 5. **adapt_constitutional_check** → Safety validation 6. **memory_store_memory** → Record trade decisions **For Risk Management Requests:** 1. **prompt_create_template** → Risk framework template 2. **trading_calculate_position_size** → Position sizing 3. **trading_calculate_risk_reward** → R:R ratios 4. **adapt_constitutional_check** → Safety validation 5. **memory_store_memory** → Save risk parameters **For N8N Workflow Generation Requests:** 1. **/n8n generate** → Convert natural language to complete n8n workflows 2. **Super Code Integration** → Automatic JavaScript code generation for VM-safe execution 3. **Multi-Node Architecture** → Triggers, HTTP, data processing, conditional logic 4. **Workflow Validation** → Comprehensive JSON structure and connection checking 5. **Auto-Saving** → Organized storage in ken8n-workflows/ directory 6. **Import Ready** → Direct compatibility with n8n instances šŸ”§ **TOOL EXECUTION BEST PRACTICES**: āœ… **ALWAYS USE DIRECT TOOL CALLS**: - trading_get_ticker({ symbol: "BTCUSDT", exchange: "bybit" }) - prompt_create_template({ name: "btc_analysis", content: "..." }) - intel_route_request({ query: "bitcoin technical analysis" }) āŒ **NEVER USE EXECUTE_COMMAND**: - execute_command("get ticker") āŒ - execute_command("calculate RSI") āŒ šŸŽÆ **RESPONSE FORMAT**: 1. **Prompt Setup**: Template creation/selection 2. **Market Data**: Current price, volume, key levels 3. **Technical Analysis**: RSI, MACD, Bollinger results 4. **AI Intelligence**: Routed methodology insights 5. **Risk Assessment**: Position sizing, R:R ratios 6. **Memory Integration**: Stored insights and recalls 7. **Final Synthesis**: Template-rendered conclusions āš ļø **CRITICAL TOOL REQUIREMENTS**: - **MACD**: Needs 35+ price points (fixed: now uses 40 prices) - **Sentiment/Funding**: Requires symbol + exchange (fixed: now included) - **Pattern Detection**: Requires OHLC data array (min 3 candlesticks) - **Volume Profile**: Requires OHLCV data with volume (min 20 periods) - **Order Placement**: Test mode only (testMode: true) - **Cosmic Analysis**: Symbol required for market timing - **All 41 Tools**: Expanded and validated system šŸš€ **PROFESSIONAL STANDARDS**: - Start every analysis with prompt tools - Use appropriate tool combinations for request type - Store significant findings in memory - Apply risk management to all trading recommendations - Maintain tool-first workflow discipline - Leverage n8n workflow generation for automation needs - Generate production-ready workflows with Super Code integration **IMPORTANT**: Always begin with prompt tools to establish analysis framework, then proceed through the tool hierarchy systematically. This ensures consistent, professional analysis using the full power of our 41-tool unified BEHEMOTH system with complete cosmic intelligence integration.`; } public setToolCallbacks(callbacks: { onToolStart?: (name: string, args: Record<string, any>) => void; onToolEnd?: (name: string, result: any) => void; onToolApproval?: ( toolName: string, toolArgs: Record<string, any>, ) => Promise<{approved: boolean; autoApproveSession?: boolean}>; onThinkingText?: (content: string, reasoning?: string) => void; onFinalMessage?: (content: string, reasoning?: string) => void; onMaxIterations?: (maxIterations: number) => Promise<boolean>; onApiUsage?: (usage: { prompt_tokens: number; completion_tokens: number; total_tokens: number; }) => void; onStreamUpdate?: ( content: string, isFinal: boolean, toolCalls?: any[], ) => void; }) { this.onToolStart = callbacks.onToolStart; this.onToolEnd = callbacks.onToolEnd; this.onToolApproval = callbacks.onToolApproval; this.onThinkingText = callbacks.onThinkingText; this.onFinalMessage = callbacks.onFinalMessage; this.onMaxIterations = callbacks.onMaxIterations; this.onApiUsage = callbacks.onApiUsage; this.onStreamUpdate = callbacks.onStreamUpdate; } public setApiKey(apiKey: string, providerName?: string): void { const targetProvider = providerName || this.providerName; Agent.debugLog(`Setting API key for ${targetProvider}...`); Agent.debugLog( 'API key provided:', apiKey ? `${apiKey.substring(0, 8)}...` : 'empty', ); this.configManager.setProviderApiKey(targetProvider, apiKey); // Recreate provider client with new API key try { this.provider = providerClientFactory.createProvider(targetProvider); Agent.debugLog( `${targetProvider} client initialized with provided API key`, ); } catch (error: any) { Agent.debugLog(`Failed to initialize ${targetProvider} client:`, error); throw error; } } public saveApiKey(apiKey: string, providerName?: string): void { const targetProvider = providerName || this.providerName; this.configManager.setProviderApiKey(targetProvider, apiKey); this.setApiKey(apiKey, targetProvider); } public async verifyApiKey( apiKey: string, providerName?: string, ): Promise<{valid: boolean; error?: string}> { try { Agent.debugLog( `Verifying API key for ${providerName || this.providerName}...`, ); const targetProvider = providerName || this.providerName; // Temporarily create provider client to test const originalApiKey = this.configManager.getProviderApiKey(targetProvider); this.configManager.setProviderApiKey(targetProvider, apiKey); try { const testProvider = providerClientFactory.createProvider(targetProvider); // Test with a simple request await testProvider.createChatCompletion({ model: this.configManager.getProviderModel(targetProvider) || this.model, messages: [{role: 'user', content: 'test'}], max_tokens: 1, }); Agent.debugLog('API key verification successful'); return {valid: true}; } finally { // Restore original API key if (originalApiKey) { this.configManager.setProviderApiKey(targetProvider, originalApiKey); } } } catch (error: any) { Agent.debugLog('API key verification failed:', error.message); return { valid: false, error: error.message?.includes('401') ? 'Invalid API key' : 'API key verification failed', }; } } public addApiKey(apiKey: string, providerName?: string): void { const targetProvider = providerName || this.providerName; this.configManager.setProviderApiKey(targetProvider, apiKey); this.setApiKey(apiKey, targetProvider); } public getApiKeyCount(providerName?: string): number { const targetProvider = providerName || this.providerName; const providerConfig = this.configManager.getProviderConfig(targetProvider); return providerConfig?.apiKeys?.length || (providerConfig?.apiKey ? 1 : 0); } public rotateApiKey(providerName?: string): string | null { const targetProvider = providerName || this.providerName; const nextKey = this.configManager.rotateProviderApiKey(targetProvider); if (nextKey) { try { this.provider = providerClientFactory.createProvider(targetProvider); Agent.debugLog( `Rotated to next API key for ${targetProvider} (${this.getApiKeyCount( targetProvider, )} keys total)`, ); } catch (error: any) { Agent.debugLog( `Failed to rotate API key for ${targetProvider}:`, error, ); } } return nextKey; } public getApiKeyInfo(providerName?: string): { current: string | null; total: number; provider: string; } { const targetProvider = providerName || this.providerName; const currentKey = this.configManager.getProviderApiKey(targetProvider); const total = this.getApiKeyCount(targetProvider); return { current: currentKey ? `${currentKey.substring(0, 8)}...` : null, total, provider: targetProvider, }; } public clearProviderApiKey(providerName?: string): void { const targetProvider = providerName || this.providerName; this.configManager.clearProviderApiKey(targetProvider); if (targetProvider === this.providerName) { this.provider = null; } } public getAllProviderInfo(): Array<{ provider: string; configured: boolean; current: string | null; total: number; }> { return this.configManager .getAllConfiguredProviders() .map(({name, configured}) => ({ provider: name, configured, current: configured ? this.configManager.getProviderApiKey(name)?.substring(0, 8) + '...' : null, total: this.getApiKeyCount(name), })); } public switchProvider(providerName: string, model?: string): void { if (!this.configManager.isProviderConfigured(providerName)) { throw new Error(`Provider ${providerName} is not configured`); } this.providerName = providerName; if (model) { this.model = model; this.configManager.setProviderModel(providerName, model); } this.provider = providerClientFactory.createProvider(providerName); this.configManager.setDefaultProvider(providerName); } public getCurrentKeyIndex(): number { const providerConfig = this.configManager.getProviderConfig( this.providerName, ); const keys = providerConfig?.apiKeys ?? (providerConfig?.apiKey ? [providerConfig.apiKey] : []); const currentKey = this.configManager.getProviderApiKey(this.providerName); if (!currentKey || !keys || keys.length === 0) return -1; return keys.indexOf(currentKey); } public clearHistory(): void { // Reset messages to only contain system messages this.messages = this.messages.filter(msg => msg.role === 'system'); } /** * Execute a tool directly - used by orchestration system */ public async executeTool(toolName: string, args: any): Promise<any> { return await executeTool(toolName, args); } /** * Auto-store analysis results in memory system for learning and future reference */ private async autoStoreAnalysisResult(toolName: string, args: any, result: any): Promise<void> { try { // Apply evolution enhancements to analysis results result = evolutionEngine.applyEvolutionEnhancements(result); // Only store results for analysis tools, not system/utility tools const analysisToolPrefixes = [ 'trading_', 'cosmic_', 'intel_', 'adaptive_', 'technical_', 'pattern_', 'market_', 'sentiment_', 'risk_', 'defi_', 'ml_', 'web3_' ]; const isAnalysisTool = analysisToolPrefixes.some(prefix => toolName.startsWith(prefix)); if (!isAnalysisTool) { return; // Skip memory storage for non-analysis tools } // Skip if result indicates failure or error if (!result || result.success === false || result.error) { return; } // Extract key information for memory storage const symbol = args.symbol || args.pair || 'GENERAL'; const toolCategory = toolName.split('_')[0]; // trading, cosmic, intel, etc. // Create memory content with structured analysis result const memoryContent = { tool: toolName, symbol, category: toolCategory, args: this.sanitizeArgs(args), result: this.sanitizeResult(result), timestamp: new Date().toISOString(), analysis_type: this.determineAnalysisType(toolName) }; // Generate importance score based on tool type and result const importance = this.calculateImportanceScore(toolName, result); // Generate contextual tags const tags = this.generateMemoryTags(toolName, args, symbol); // Store in memory system await executeTool('memory_store_memory', { content: `Analysis: ${toolName} for ${symbol} - ${this.summarizeResult(result)}`, context: memoryContent, importance, tags }); Agent.debugLog(`Auto-stored analysis result: ${toolName} for ${symbol} with importance ${importance}`); } catch (error: any) { // Don't fail the main tool execution if memory storage fails Agent.debugLog(`Failed to auto-store analysis result: ${error.message}`); } } /** * Sanitize arguments for memory storage (remove sensitive data) */ private sanitizeArgs(args: any): any { const sanitized = { ...args }; // Remove potentially sensitive fields delete sanitized.api_key; delete sanitized.secret_key; delete sanitized.password; delete sanitized.token; return sanitized; } /** * Sanitize result for memory storage (truncate large data) */ private sanitizeResult(result: any): any { const sanitized = { ...result }; // Truncate large arrays to prevent memory bloat Object.keys(sanitized).forEach(key => { if (Array.isArray(sanitized[key]) && sanitized[key].length > 10) { sanitized[key] = sanitized[key].slice(0, 10).concat([`... ${sanitized[key].length - 10} more items`]); } // Truncate long strings if (typeof sanitized[key] === 'string' && sanitized[key].length > 500) { sanitized[key] = sanitized[key].substring(0, 500) + '... (truncated)'; } }); return sanitized; } /** * Determine analysis type from tool name for categorization */ private determineAnalysisType(toolName: string): string { if (toolName.includes('rsi') || toolName.includes('macd') || toolName.includes('bollinger')) { return 'technical_indicator'; } if (toolName.includes('pattern') || toolName.includes('support_resistance')) { return 'pattern_analysis'; } if (toolName.includes('cosmic') || toolName.includes('planetary')) { return 'cosmic_analysis'; } if (toolName.includes('sentiment')) { return 'market_sentiment'; } if (toolName.includes('risk')) { return 'risk_analysis'; } if (toolName.includes('intel')) { return 'ai_intelligence'; } if (toolName.includes('adaptive')) { return 'machine_learning'; } return 'general_analysis'; } /** * Calculate importance score (1-10) based on tool type and result quality */ private calculateImportanceScore(toolName: string, result: any): number { let score = 5; // Base score // Higher importance for certain tool types if (toolName.includes('cosmic')) score += 2; // Cosmic analysis is unique if (toolName.includes('intel')) score += 1; // AI intelligence insights if (toolName.includes('adaptive')) score += 2; // Machine learning results if (toolName.includes('risk')) score += 1; // Risk analysis important // Adjust based on result quality if (result.confidence && result.confidence > 0.8) score += 1; if (result.signals && result.signals.length > 0) score += 1; if (result.recommendation) score += 1; // Clamp to valid range return Math.min(Math.max(score, 1), 10); } /** * Generate contextual tags for memory storage */ private generateMemoryTags(toolName: string, args: any, symbol: string): string[] { const tags = ['auto_analysis', symbol.toLowerCase()]; // Add tool category tags if (toolName.startsWith('trading_')) tags.push('trading'); if (toolName.startsWith('cosmic_')) tags.push('cosmic'); if (toolName.startsWith('intel_')) tags.push('intelligence'); if (toolName.startsWith('adaptive_')) tags.push('machine_learning'); if (toolName.includes('risk')) tags.push('risk_management'); if (toolName.includes('sentiment')) tags.push('market_sentiment'); // Add timeframe tags if present if (args.timeframe) tags.push(`timeframe_${args.timeframe}`); if (args.interval) tags.push(`interval_${args.interval}`); return tags; } /** * Create human-readable summary of analysis result */ private summarizeResult(result: any): string { if (result.recommendation) { return `Recommendation: ${result.recommendation}`; } if (result.signal) { return `Signal: ${result.signal}`; } if (result.trend) { return `Trend: ${result.trend}`; } if (result.sentiment) { return `Sentiment: ${result.sentiment}`; } if (result.score !== undefined) { return `Score: ${result.score}`; } if (result.value !== undefined) { return `Value: ${result.value}`; } return 'Analysis completed'; } /** * Track tool performance metrics for optimization and self-improvement */ private async trackToolPerformance(toolName: string, args: any, result: any, executionTime: number): Promise<void> { try { // Determine if execution was successful const success = result && result.success !== false && !result.error; // Calculate performance rating based on execution time and success const performanceRating = this.calculatePerformanceRating(toolName, executionTime, success); // Extract key context information const symbol = args.symbol || args.pair || 'GENERAL'; const toolCategory = toolName.split('_')[0]; // Create performance metrics context const performanceContext = { tool: toolName, category: toolCategory, symbol, execution_time_ms: executionTime, success, performance_rating: performanceRating, timestamp: new Date().toISOString(), args_size: JSON.stringify(args).length, result_size: result ? JSON.stringify(result).length : 0, session_id: `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`, optimization_hints: this.generateOptimizationHints(toolName, executionTime, success) }; // Store performance metrics in UniMemory system using data module await executeTool('data_store_performance_data', { tool_name: toolName, execution_time_ms: executionTime, success, context: performanceContext }); // Also store high-level performance insights in memory for learning await executeTool('memory_store_memory', { content: `Performance: ${toolName} executed in ${executionTime}ms - ${success ? 'SUCCESS' : 'FAILED'}`, context: { type: 'performance_tracking', tool: toolName, execution_time_ms: executionTime, success, performance_rating: performanceRating, optimization_hints: performanceContext.optimization_hints }, importance: this.calculatePerformanceImportance(executionTime, success), tags: ['performance', 'optimization', toolCategory, symbol.toLowerCase()] }); // Update internal performance counters for adaptive optimization this.updatePerformanceCounters(toolName, executionTime, success); Agent.debugLog(`Performance tracked: ${toolName} - ${executionTime}ms - ${success ? 'SUCCESS' : 'FAILED'} - Rating: ${performanceRating}`); } catch (error: any) { // Don't fail the main execution if performance tracking fails Agent.debugLog(`Failed to track performance: ${error.message}`); } } /** * Calculate performance rating (1-10) based on execution time and success */ private calculatePerformanceRating(toolName: string, executionTime: number, success: boolean): number { if (!success) return 1; // Failed executions get lowest rating // Base rating starts at 5 let rating = 5; // Adjust based on execution time (faster is better) if (executionTime < 100) rating += 3; // Very fast else if (executionTime < 500) rating += 2; // Fast else if (executionTime < 1000) rating += 1; // Moderate else if (executionTime < 3000) rating += 0; // Acceptable else if (executionTime < 10000) rating -= 1; // Slow else rating -= 2; // Very slow // Adjust based on tool type (some tools are expected to be slower) if (toolName.includes('cosmic') || toolName.includes('intel')) { rating += 1; // These tools do complex analysis, allow more time } if (toolName.includes('adaptive') || toolName.includes('ml_')) { rating += 1; // ML operations are computationally intensive } // Clamp to valid range return Math.min(Math.max(rating, 1), 10); } /** * Generate optimization hints based on performance metrics */ private generateOptimizationHints(toolName: string, executionTime: number, success: boolean): string[] { const hints: string[] = []; if (!success) { hints.push('investigate_failure_cause'); hints.push('add_error_handling'); } if (executionTime > 5000) { hints.push('optimize_for_speed'); hints.push('consider_caching'); } if (executionTime > 10000) { hints.push('consider_async_processing'); hints.push('implement_timeout_handling'); } if (toolName.includes('trading_') && executionTime > 1000) { hints.push('trading_tools_need_low_latency'); } if (toolName.includes('data_') && executionTime > 3000) { hints.push('data_processing_bottleneck'); } return hints; } /** * Calculate importance of performance data for memory storage */ private calculatePerformanceImportance(executionTime: number, success: boolean): number { let importance = 3; // Base importance for performance data // Failed executions are more important to remember if (!success) importance += 3; // Very slow executions are important for optimization if (executionTime > 10000) importance += 2; else if (executionTime > 5000) importance += 1; // Very fast executions are also valuable examples if (executionTime < 100) importance += 1; // Clamp to valid range return Math.min(Math.max(importance, 1), 10); } /** * Update internal performance counters for adaptive optimization */ private updatePerformanceCounters(toolName: string, executionTime: number, success: boolean): void { // This could be expanded to maintain running averages and performance trends // For now, just log the key metrics for debugging Agent.debugLog(`Performance Counter Update: ${toolName} - Avg: ${executionTime}ms - Success Rate: ${success ? '100%' : '0%'}`); // Future enhancement: maintain performance history in memory // to enable adaptive optimization and performance predictions // Trigger self-improvement analysis periodically this.requestCount++; if (this.requestCount % 10 === 0) { // Every 10 tool executions, trigger self-improvement analysis this.triggerSelfImprovementAnalysis().catch(error => Agent.debugLog(`Self-improvement analysis failed: ${error.message}`) ); } } /** * Self-improvement feedback loop - analyzes performance and memory to optimize system behavior */ private async triggerSelfImprovementAnalysis(): Promise<void> { try { Agent.debugLog('Triggering self-improvement analysis...'); // 1. Analyze recent performance data to identify optimization opportunities await this.analyzePerformanceTrends(); // 2. Analyze memory patterns to improve routing and responses await this.analyzeMemoryPatterns(); // 3. Run adaptive learning on recent behavior patterns await this.runAdaptiveLearningCycle(); // 4. Store self-improvement insights await this.storeSelfImprovementInsights(); Agent.debugLog('Self-improvement analysis completed'); } catch (error: any) { Agent.debugLog(`Self-improvement analysis error: ${error.message}`); } } /** * Analyze performance trends to identify optimization opportunities */ private async analyzePerformanceTrends(): Promise<void> { try { // Recall recent performance data from memory const performanceMemories = await executeTool('memory_recall_memory', { query: 'performance tracking optimization', limit: 20, min_relevance: 0.3 }); if (performanceMemories && (performanceMemories as any).memories && (performanceMemories as any).memories.length > 0) { // Extract performance patterns const slowTools = []; const failingTools = []; let totalExecutionTime = 0; let successfulExecutions = 0; for (const memory of (performanceMemories as any).memories) { if (memory.context && memory.context.type === 'performance_tracking') { const ctx = memory.context; totalExecutionTime += ctx.execution_time_ms || 0; if (ctx.success) { successfulExecutions++; } else { failingTools.push(ctx.tool); } if (ctx.execution_time_ms > 5000) { slowTools.push(ctx.tool); } } } // Generate improvement recommendations const avgExecutionTime = totalExecutionTime / (performanceMemories as any).memories.length; const successRate = successfulExecutions / (performanceMemories as any).memories.length; // Store analysis insights await executeTool('memory_store_memory', { content: `Performance Analysis: Avg execution time: ${avgExecutionTime.toFixed(0)}ms, Success rate: ${(successRate * 100).toFixed(1)}%`, context: { type: 'self_improvement', analysis_type: 'performance_trends', avg_execution_time: avgExecutionTime, success_rate: successRate, slow_tools: [...new Set(slowTools)], failing_tools: [...new Set(failingTools)], recommendations: this.generatePerformanceRecommendations(avgExecutionTime, successRate, slowTools, failingTools) }, importance: 8, tags: ['self_improvement', 'performance_analysis', 'optimization'] }); Agent.debugLog(`Performance trends analyzed: ${avgExecutionTime.toFixed(0)}ms avg, ${(successRate * 100).toFixed(1)}% success rate`); } } catch (error: any) { Agent.debugLog(`Performance trends analysis failed: ${error.message}`); } } /** * Analyze memory patterns to improve routing and responses */ private async analyzeMemoryPatterns(): Promise<void> { try { // Recall recent analysis memories to identify patterns const analysisMemories = await executeTool('memory_recall_memory', { query: 'analysis trading cosmic intelligence', limit: 30, min_relevance: 0.3 }); if (analysisMemories && (analysisMemories as any).memories && (analysisMemories as any).memories.length > 0) { // Analyze tool usage patterns const toolUsageMap = new Map<string, number>(); const categoryUsageMap = new Map<string, number>(); const symbolUsageMap = new Map<string, number>(); for (const memory of (analysisMemories as any).memories) { if (memory.context && memory.context.tool) { const tool = memory.context.tool; const category = memory.context.category; const symbol = memory.context.symbol; toolUsageMap.set(tool, (toolUsageMap.get(tool) || 0) + 1); if (category) categoryUsageMap.set(category, (categoryUsageMap.get(category) || 0) + 1); if (symbol) symbolUsageMap.set(symbol, (symbolUsageMap.get(symbol) || 0) + 1); } } // Identify most used tools and patterns const topTools = Array.from(toolUsageMap.entries()) .sort((a, b) => b[1] - a[1]) .slice(0, 5); const topCategories = Array.from(categoryUsageMap.entries()) .sort((a, b) => b[1] - a[1]) .slice(0, 3); const topSymbols = Array.from(symbolUsageMap.entries()) .sort((a, b) => b[1] - a[1]) .slice(0, 3); // Store memory pattern insights await executeTool('memory_store_memory', { content: `Memory Pattern Analysis: Most used tools: ${topTools.map(t => t[0]).join(', ')}`, context: { type: 'self_improvement', analysis_type: 'memory_patterns', top_tools: topTools, top_categories: topCategories, top_symbols: topSymbols, total_memories_analyzed: (analysisMemories as any).memories.length, pattern_insights: this.generateMemoryPatternInsights(topTools, topCategories, topSymbols) }, importance: 7, tags: ['self_improvement', 'memory_analysis', 'usage_patterns'] }); Agent.debugLog(`Memory patterns analyzed: ${(analysisMemories as any).memories.length} memories, top tool: ${topTools[0]?.[0]}`); } } catch (error: any) { Agent.debugLog(`Memory patterns analysis failed: ${error.message}`); } } /** * Run adaptive learning cycle on recent behavior patterns */ private async runAdaptiveLearningCycle(): Promise<void> { try { // Use the adaptive learning tools to improve system behavior const behaviorData = { actions: ['analyze', 'trade', 'check', 'optimize'], patterns: ['btc_analysis', 'eth_analysis', 'risk_check'], preferences: ['speed_optimization', 'accuracy_focus'], risk_level: 0.3 }; // Analyze current behavior patterns const behaviorAnalysis = await executeTool('adaptive_analyze_behavior', { behavior_data: behaviorData, session_id: `self_improvement_${Date.now()}`, analysis_depth: 'detailed' }); if (behaviorAnalysis && (behaviorAnalysis as any).behavioral_traits) { // Adapt strategy based on behavior analysis const currentStrategy = { response_speed_priority: 0.8, accuracy_priority: 0.9, memory_utilization: 0.7, optimization_aggressiveness: 0.5 }; const adaptedStrategy = await executeTool('adaptive_adapt_strategy', { current_strategy: currentStrategy, performance_data: [ { metric: 'response_time', value: 150 }, { metric: 'accuracy_score', value: 0.85 }, { metric: 'memory_efficiency', value: 0.75 } ], adaptation_strength: 0.3 }); // Store adaptive learning results await executeTool('memory_store_memory', { content: `Adaptive Learning: Behavior analysis completed, strategy adapted`, context: { type: 'self_improvement', analysis_type: 'adaptive_learning', behavior_traits: (behaviorAnalysis as any).behavioral_traits, original_strategy: currentStrategy, adapted_strategy: (adaptedStrategy as any).adapted_strategy, confidence: (behaviorAnalysis as any).confidence_score, adaptation_strength: 0.3 }, importance: 9, tags: ['self_improvement', 'adaptive_learning', 'strategy_optimization'] }); Agent.debugLog(`Adaptive learning cycle completed: ${(behaviorAnalysis as any).behavioral_traits?.join(', ')}`); } } catch (error: any) { Agent.debugLog(`Adaptive learning cycle failed: ${error.message}`); } } /** * Store comprehensive self-improvement insights */ private async storeSelfImprovementInsights(): Promise<void> { try { // Generate overall system health assessment const systemInsights = { timestamp: new Date().toISOString(), total_requests: this.requestCount, improvement_cycle: Math.floor(this.requestCount / 10), learning_status: 'active', optimization_level: 'continuous', next_improvement_at: this.requestCount + (10 - (this.requestCount % 10)) }; await executeTool('memory_store_memory', { content: `System Self-Improvement: Cycle ${systemInsights.improvement_cycle} completed - Total requests: ${systemInsights.total_requests}`, context: { type: 'self_improvement', analysis_type: 'system_overview', ...systemInsights, capabilities_enhanced: [ 'performance_tracking', 'memory_pattern_analysis', 'adaptive_strategy_optimization', 'continuous_learning' ] }, importance: 8, tags: ['self_improvement', 'system_overview', 'continuous_learning'] }); Agent.debugLog(`Self-improvement insights stored: Cycle ${systemInsights.improvement_cycle}`); } catch (error: any) { Agent.debugLog(`Failed to store self-improvement insights: ${error.message}`); } } /** * Generate performance-based recommendations */ private generatePerformanceRecommendations(avgTime: number, successRate: number, slowTools: string[], failingTools: string[]): string[] { const recommendations = []; if (avgTime > 3000) recommendations.push('implement_caching_layer'); if (avgTime > 5000) recommendations.push('optimize_database_queries'); if (successRate < 0.9) recommendations.push('improve_error_handling'); if (slowTools.length > 3) recommendations.push('profile_slow_tools'); if (failingTools.length > 2) recommendations.push('investigate_failure_patterns'); return recommendations; } /** * Generate insights from memory usage patterns */ private generateMemoryPatternInsights(topTools: [string, number][], topCategories: [string, number][], topSymbols: [string, number][]): string[] { const insights = []; if (topTools[0]?.[1] > 5) insights.push(`high_usage_tool_${topTools[0][0]}`); if (topCategories[0]?.[0] === 'trading') insights.push('trading_focused_usage'); if (topCategories[0]?.[0] === 'cosmic') insights.push('cosmic_analysis_preference'); if (topSymbols[0]?.[0] === 'BTCUSDT') insights.push('btc_primary_focus'); return insights; } /** * Enhance tool arguments with relevant memory context for intelligent execution */ private async enhanceToolArgsWithMemoryContext(toolName: string, args: any): Promise<any> { try { // Create enhanced args object const enhancedArgs = { ...args }; // Extract key context for memory search const symbol = args.symbol || args.pair || 'GENERAL'; const toolCategory = toolName.split('_')[0]; // Build contextual search queries based on tool type and parameters const searchQueries = this.buildMemorySearchQueries(toolName, args, symbol, toolCategory); // Retrieve relevant memories for each query const memoryContexts = []; for (const query of searchQueries) { try { const memories = await executeTool('memory_recall_memory', { query: query.query, limit: query.limit, min_relevance: query.min_relevance }); if (memories && (memories as any).memories && (memories as any).memories.length > 0) { memoryContexts.push({ query_type: query.type, relevant_memories: (memories as any).memories.slice(0, 3), // Top 3 most relevant memory_count: (memories as any).memories.length }); } } catch (error: any) { Agent.debugLog(`Memory context retrieval failed for ${query.type}: ${error.message}`); } } // Add memory context to tool arguments if memories found if (memoryContexts.length > 0) { enhancedArgs._memory_context = { total_contexts: memoryContexts.length, contexts: memoryContexts, enhancement_timestamp: new Date().toISOString(), enhanced_for: toolName, context_summary: this.summarizeMemoryContexts(memoryContexts) }; Agent.debugLog(`Enhanced ${toolName} with ${memoryContexts.length} memory contexts`); } // Add performance context from recent executions of this tool const performanceContext = await this.getToolPerformanceContext(toolName); if (performanceContext) { enhancedArgs._performance_context = performanceContext; } // Add adaptive insights for intelligent tools if (this.shouldAddAdaptiveContext(toolName)) { const adaptiveContext = await this.getAdaptiveContext(toolName, args); if (adaptiveContext) { enhancedArgs._adaptive_context = adaptiveContext; } } return enhancedArgs; } catch (error: any) { // If memory enhancement fails, return original args Agent.debugLog(`Memory context enhancement failed for ${toolName}: ${error.message}`); return args; } } /** * Build contextual search queries based on tool type and parameters */ private buildMemorySearchQueries(toolName: string, args: any, symbol: string, toolCategory: string): Array<{query: string, type: string, limit: number, min_relevance: number}> { const queries = []; // 1. Tool-specific historical results queries.push({ query: `${toolName} ${symbol} analysis result`, type: 'tool_history', limit: 5, min_relevance: 0.4 }); // 2. Symbol-specific insights if (symbol !== 'GENERAL') { queries.push({ query: `${symbol} trading analysis insights trends`, type: 'symbol_insights',