/** * Agent Marketplace * Plugin-based agent discovery and installation */ const fs = require('fs'); const path = require('path'); class AgentMarketplace { constructor() { this.marketplacePath = path.join(__dirname, '../../marketplace'); this.installedPath = path.join(__dirname, '../../installed'); this.agents = new Map(); this.installedAgents = new Map(); } initialize() { if (!fs.existsSync(this.marketplacePath)) fs.mkdirSync(this.marketplacePath, { recursive: true }); if (!fs.existsSync(this.installedPath)) fs.mkdirSync(this.installedPath, { recursive: true }); this.loadAgents(); this.swarm?.log?.('success', `Marketplace initialized: ${this.agents.size} available`); } loadAgents() { try { const files = fs.readdirSync(this.marketplacePath).filter(f => f.endsWith('.json')); for (const file of files) { const data = JSON.parse(fs.readFileSync(path.join(this.marketplacePath, file), 'utf8')); this.agents.set(data.id, data); } } catch {} } search(query = '', capabilities = []) { let results = Array.from(this.agents.values()); if (query) { results = results.filter(a => a.name.toLowerCase().includes(query.toLowerCase()) || a.description.toLowerCase().includes(query.toLowerCase()) ); } if (capabilities.length) { results = results.filter(a => capabilities.some(c => a.capabilities?.includes(c))); } return results; } installAgent(agentId) { const agent = this.agents.get(agentId); if (!agent) throw new Error(`Agent not found: ${agentId}`); if (this.installedAgents.has(agentId)) throw new Error(`Already installed: ${agentId}`); const installDir = path.join(this.installedPath, agentId); fs.mkdirSync(installDir, { recursive: true }); this.installedAgents.set(agentId, { ...agent, installedAt: Date.now() }); fs.writeFileSync( path.join(this.installedPath, 'installed.json'), JSON.stringify(Object.fromEntries(this.installedAgents), null, 2) ); return { ...agent, installedAt: Date.now() }; } uninstallAgent(agentId) { if (!this.installedAgents.has(agentId)) throw new Error(`Not installed: ${agentId}`); const dir = path.join(this.installedPath, agentId); if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true }); this.installedAgents.delete(agentId); } listAvailable() { return Array.from(this.agents.values()); } listInstalled() { return Array.from(this.installedAgents.values()); } isInstalled(agentId) { return this.installedAgents.has(agentId); } } module.exports = AgentMarketplace;