blue-beatle
Version:
๐ค AI-Powered Development Assistant - Intelligent code analysis, problem solving, and terminal automation using Gemini API
466 lines (377 loc) โข 16.6 kB
JavaScript
/**
* ๐ Blue Beatle Workspace Integration
* Creates a permanent AI presence in the terminal with agentic capabilities
*/
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const { spawn, exec } = require('child_process');
const os = require('os');
class WorkspaceIntegration {
constructor() {
this.isRunning = false;
this.aiProcess = null;
this.workspaceWatcher = null;
this.statusIcon = '๐ค';
this.agentCapabilities = {
codeAnalysis: true,
fileWatching: true,
autoFix: true,
contextAwareness: true,
proactiveAssistance: true,
workspaceIntegration: true
};
}
async initialize() {
console.log(chalk.cyan('๐ Initializing Blue Beatle Workspace Integration...'));
// Create workspace integration
await this.createWorkspaceIntegration();
// Setup permanent terminal presence
await this.setupPermanentPresence();
// Initialize agentic capabilities
await this.initializeAgenticFeatures();
// Create system tray integration
await this.createSystemIntegration();
console.log(chalk.green('โ
Blue Beatle is now integrated into your workspace!'));
console.log(chalk.yellow('๐ค AI Agent is running in the background...'));
}
async createWorkspaceIntegration() {
const integrationScript = `#!/usr/bin/env node
/**
* ๐ค Blue Beatle - Permanent AI Workspace Agent
* Always-on AI assistant with agentic capabilities
*/
const { spawn } = require('child_process');
const chalk = require('chalk');
const path = require('path');
class BlueBeatlePermanentAgent {
constructor() {
this.isActive = true;
this.workspaceRoot = process.cwd();
this.lastActivity = Date.now();
this.capabilities = {
autonomous: true,
proactive: true,
contextAware: true,
selfImproving: true
};
}
async start() {
// Display permanent AI presence
this.showPermanentIcon();
// Start background monitoring
this.startBackgroundMonitoring();
// Initialize proactive assistance
this.initializeProactiveMode();
// Keep the agent running
this.maintainPresence();
}
showPermanentIcon() {
// Clear and show permanent AI icon
process.stdout.write('\\x1b[2J\\x1b[0f'); // Clear screen
const banner = \`
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ค BLUE BEATLE AI AGENT - ACTIVE โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ ๐ Monitoring: \${this.workspaceRoot} โ
โ โก Status: READY FOR COMMANDS โ
โ ๐ง Mode: AGENTIC AI - AUTONOMOUS ASSISTANCE โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ ๐ก Type 'help' for commands | 'exit' to close โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
\`;
console.log(chalk.cyan(banner));
console.log(chalk.yellow('๐ฏ AI Agent initialized. Watching workspace for opportunities to assist...\\n'));
// Show command prompt
this.showPrompt();
}
showPrompt() {
process.stdout.write(chalk.cyan('๐ค Blue Beatle โฏ '));
}
startBackgroundMonitoring() {
// Monitor file changes
const chokidar = require('chokidar');
const watcher = chokidar.watch(this.workspaceRoot, {
ignored: /(^|[\\/\\\\])\\../,
persistent: true,
ignoreInitial: true
});
watcher.on('change', (filePath) => {
this.onFileChanged(filePath);
});
watcher.on('add', (filePath) => {
this.onFileAdded(filePath);
});
}
onFileChanged(filePath) {
const relativePath = path.relative(this.workspaceRoot, filePath);
console.log(chalk.gray(\`\\n๐ Detected change: \${relativePath}\`));
// Proactive analysis
if (this.shouldAnalyzeFile(filePath)) {
console.log(chalk.blue('๐ Running proactive analysis...'));
this.analyzeFileProactively(filePath);
}
this.showPrompt();
}
onFileAdded(filePath) {
const relativePath = path.relative(this.workspaceRoot, filePath);
console.log(chalk.green(\`\\nโ New file detected: \${relativePath}\`));
// Offer assistance
console.log(chalk.yellow('๐ก Would you like me to analyze this new file? (Type "analyze-new" to proceed)'));
this.showPrompt();
}
shouldAnalyzeFile(filePath) {
const criticalFiles = ['.js', '.ts', '.py', '.java', '.cpp', '.cs'];
const ext = path.extname(filePath);
return criticalFiles.includes(ext);
}
async analyzeFileProactively(filePath) {
// This would integrate with the main AI assistant
console.log(chalk.blue(\`๐ค Analyzing \${path.relative(this.workspaceRoot, filePath)}...\`));
// Simulate analysis
setTimeout(() => {
console.log(chalk.green('โ
Analysis complete. No issues detected.'));
this.showPrompt();
}, 1000);
}
initializeProactiveMode() {
// Proactive suggestions every 30 minutes
setInterval(() => {
this.offerProactiveAssistance();
}, 30 * 60 * 1000);
}
offerProactiveAssistance() {
const suggestions = [
'๐ Would you like me to run a security audit?',
'๐ Shall I analyze your code quality?',
'โก Want me to check for performance optimizations?',
'๐งน Should I look for code cleanup opportunities?'
];
const suggestion = suggestions[Math.floor(Math.random() * suggestions.length)];
console.log(chalk.yellow(\`\\n๐ก Proactive AI: \${suggestion}\`));
console.log(chalk.gray('Type "yes" to proceed or continue with your work.'));
this.showPrompt();
}
maintainPresence() {
// Handle user input
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
const chunk = process.stdin.read();
if (chunk !== null) {
this.handleCommand(chunk.trim());
}
});
// Keep process alive
process.stdin.on('end', () => {
console.log(chalk.yellow('\\n๐ Blue Beatle AI Agent shutting down...'));
process.exit(0);
});
}
handleCommand(command) {
if (command === 'exit') {
console.log(chalk.yellow('๐ Blue Beatle AI Agent shutting down...'));
process.exit(0);
} else if (command === 'help') {
this.showHelp();
} else if (command === 'status') {
this.showStatus();
} else if (command === 'analyze-new') {
console.log(chalk.blue('๐ Analyzing new files...'));
} else if (command === 'yes') {
console.log(chalk.green('๐ Starting proactive assistance...'));
} else if (command.trim()) {
// Forward to main AI assistant
this.forwardToAI(command);
}
this.showPrompt();
}
showHelp() {
console.log(chalk.cyan('\\n๐ค Blue Beatle AI Agent - Available Commands:\\n'));
console.log(chalk.white(' help - Show this help'));
console.log(chalk.white(' status - Show agent status'));
console.log(chalk.white(' analyze-new - Analyze recently added files'));
console.log(chalk.white(' exit - Shutdown AI agent'));
console.log(chalk.white(' <any text> - Send to AI for processing\\n'));
console.log(chalk.yellow('๐ฏ Agentic Features:'));
console.log(chalk.gray(' โข Automatic file monitoring'));
console.log(chalk.gray(' โข Proactive code analysis'));
console.log(chalk.gray(' โข Context-aware suggestions'));
console.log(chalk.gray(' โข Autonomous problem detection\\n'));
}
showStatus() {
console.log(chalk.cyan('\\n๐ Blue Beatle AI Agent Status:\\n'));
console.log(chalk.white(\` Workspace: \${this.workspaceRoot}\`));
console.log(chalk.white(\` Status: \${this.isActive ? 'ACTIVE' : 'INACTIVE'}\`));
console.log(chalk.white(\` Uptime: \${Math.floor((Date.now() - this.lastActivity) / 1000)}s\`));
console.log(chalk.white(\` Capabilities: \${Object.keys(this.capabilities).length} active\\n\`));
}
forwardToAI(command) {
console.log(chalk.blue(\`๐ค Processing: "\${command}"\`));
// This would integrate with the main AI assistant
const aiProcess = spawn('node', [path.join(__dirname, 'ai-assistant.js')], {
stdio: 'pipe'
});
aiProcess.stdin.write(command);
aiProcess.stdin.end();
aiProcess.stdout.on('data', (data) => {
console.log(data.toString());
});
}
}
// Start the permanent AI agent
const agent = new BlueBeatlePermanentAgent();
agent.start().catch(console.error);
`;
// Write the permanent agent script
const agentPath = path.join(__dirname, 'permanent-agent.js');
await fs.writeFile(agentPath, integrationScript);
console.log(chalk.green('โ
Created permanent AI agent script'));
}
async setupPermanentPresence() {
// Create a startup script for different platforms
const startupScripts = {
win32: await this.createWindowsStartup(),
darwin: await this.createMacStartup(),
linux: await this.createLinuxStartup()
};
console.log(chalk.green('โ
Created platform-specific startup scripts'));
}
async createWindowsStartup() {
const batchScript = ` off
title Blue Beatle AI Agent
echo Starting Blue Beatle AI Agent...
cd /d "${__dirname}"
node permanent-agent.js
pause`;
const batchPath = path.join(__dirname, 'start-blue-beatle.bat');
await fs.writeFile(batchPath, batchScript);
// Create PowerShell script for better integration
const psScript = `
# Blue Beatle AI Agent - PowerShell Launcher
Write-Host "๐ค Starting Blue Beatle AI Agent..." -ForegroundColor Cyan
Set-Location "${__dirname}"
node permanent-agent.js
`;
const psPath = path.join(__dirname, 'start-blue-beatle.ps1');
await fs.writeFile(psPath, psScript);
return { batch: batchPath, powershell: psPath };
}
async createMacStartup() {
const shellScript = `#!/bin/bash
echo "๐ค Starting Blue Beatle AI Agent..."
cd "${__dirname}"
node permanent-agent.js`;
const scriptPath = path.join(__dirname, 'start-blue-beatle.sh');
await fs.writeFile(scriptPath, shellScript);
await fs.chmod(scriptPath, '755');
return scriptPath;
}
async createLinuxStartup() {
const shellScript = `#!/bin/bash
echo "๐ค Starting Blue Beatle AI Agent..."
cd "${__dirname}"
node permanent-agent.js`;
const scriptPath = path.join(__dirname, 'start-blue-beatle.sh');
await fs.writeFile(scriptPath, shellScript);
await fs.chmod(scriptPath, '755');
// Create systemd service for Linux
const serviceFile = `[Unit]
Description=Blue Beatle AI Agent
After=network.target
[Service]
Type=simple
User=${os.userInfo().username}
WorkingDirectory=${__dirname}
ExecStart=/usr/bin/node ${path.join(__dirname, 'permanent-agent.js')}
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target`;
const servicePath = path.join(__dirname, 'blue-beatle.service');
await fs.writeFile(servicePath, serviceFile);
return { script: scriptPath, service: servicePath };
}
async initializeAgenticFeatures() {
console.log(chalk.cyan('๐ง Initializing Agentic AI Capabilities...'));
// Create advanced AI configuration
const agenticConfig = {
autonomousMode: true,
proactiveAssistance: true,
contextAwareness: true,
selfImprovement: true,
workspaceIntegration: true,
realTimeAnalysis: true,
predictiveAssistance: true,
adaptiveLearning: true
};
const configPath = path.join(__dirname, 'agentic-config.json');
await fs.writeJson(configPath, agenticConfig, { spaces: 2 });
console.log(chalk.green('โ
Agentic capabilities initialized'));
}
async createSystemIntegration() {
// Create desktop shortcut
await this.createDesktopShortcut();
// Add to system PATH
await this.addToSystemPath();
// Create quick access commands
await this.createQuickCommands();
}
async createDesktopShortcut() {
const platform = process.platform;
if (platform === 'win32') {
// Windows shortcut
const shortcutScript = `
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:USERPROFILE\\Desktop\\Blue Beatle AI.lnk")
$Shortcut.TargetPath = "cmd.exe"
$Shortcut.Arguments = "/k cd /d \\"${__dirname}\\" && node permanent-agent.js"
$Shortcut.WorkingDirectory = "${__dirname}"
$Shortcut.IconLocation = "cmd.exe,0"
$Shortcut.Description = "Blue Beatle AI Agent - Permanent Workspace Assistant"
$Shortcut.Save()
`;
const shortcutPath = path.join(__dirname, 'create-shortcut.ps1');
await fs.writeFile(shortcutPath, shortcutScript);
}
}
async addToSystemPath() {
// Create global command
const globalCommand = `#!/usr/bin/env node
require('${path.join(__dirname, 'permanent-agent.js')}');`;
const binPath = path.join(__dirname, '..', 'bin', 'blue-beatle-agent');
await fs.writeFile(binPath, globalCommand);
await fs.chmod(binPath, '755');
}
async createQuickCommands() {
// Create quick access aliases
const aliases = {
'bb-agent': 'node permanent-agent.js',
'bb-start': 'node permanent-agent.js',
'blue-beatle-agent': 'node permanent-agent.js'
};
const aliasPath = path.join(__dirname, 'aliases.json');
await fs.writeJson(aliasPath, aliases, { spaces: 2 });
}
async startPermanentAgent() {
console.log(chalk.cyan('๐ Starting Blue Beatle Permanent AI Agent...'));
const agentPath = path.join(__dirname, 'permanent-agent.js');
this.aiProcess = spawn('node', [agentPath], {
stdio: 'inherit',
detached: false
});
this.aiProcess.on('close', (code) => {
console.log(chalk.yellow(`๐ค AI Agent exited with code ${code}`));
});
this.isRunning = true;
console.log(chalk.green('โ
Blue Beatle AI Agent is now running permanently!'));
}
async stop() {
if (this.aiProcess) {
this.aiProcess.kill();
this.isRunning = false;
console.log(chalk.yellow('๐ค Blue Beatle AI Agent stopped'));
}
}
}
module.exports = WorkspaceIntegration;