/** * zCode Agent Model — Ported from Ruflo Agent.ts * Individual agent with capabilities, status, task execution. */ /** @typedef {'coder'|'tester'|'reviewer'|'architect'|'coordinator'|'designer'|'deployer'|'researcher'|'security'} AgentType */ /** @typedef {'idle'|'active'|'busy'|'error'} AgentStatus */ let _agentCounter = 0; const _id = () => `agent_${Date.now().toString(36)}_${++_agentCounter}`; export class Agent { /** * @param {Object} config * @param {string} [config.id] * @param {AgentType} config.type * @param {string} config.name * @param {string} [config.description] * @param {string[]} [config.capabilities] * @param {string} [config.role] * @param {Object} [config.metadata] */ constructor(config) { this.id = config.id || _id(); this.type = config.type || 'coder'; this.name = config.name || this.type; this.description = config.description || ''; this.status = 'idle'; this.capabilities = config.capabilities || []; this.role = config.role || null; this.parent = config.parent || null; this.metadata = config.metadata || {}; this.createdAt = Date.now(); this.lastActive = Date.now(); this._taskCount = 0; this._errorCount = 0; this._conversationContext = null; } get idle() { return this.status === 'idle'; } get active() { return this.status === 'active' || this.status === 'busy'; } /** Execute a task */ async executeTask(task) { this.status = 'busy'; this.lastActive = Date.now(); this._taskCount++; try { const result = typeof task.execute === 'function' ? await task.execute(this) : { status: 'completed', output: null }; this.status = 'idle'; return result; } catch (err) { this._errorCount++; this.status = 'error'; throw err; } } /** Check if agent has a specific capability */ hasCapability(cap) { return this.capabilities.some(c => c.toLowerCase() === cap.toLowerCase()); } /** Check if agent can handle a task based on capabilities */ canHandleTask(task) { if (!task.requiredCapabilities || task.requiredCapabilities.length === 0) return true; return task.requiredCapabilities.some(c => this.hasCapability(c)); } /** Set agent's conversation context */ setContext(ctx) { this._conversationContext = ctx; } getContext() { return this._conversationContext; } /** Add extra context to conversation */ addContext(key, value) { if (!this._conversationContext) this._conversationContext = {}; this._conversationContext[key] = value; } toJSON() { return { id: this.id, type: this.type, name: this.name, description: this.description, status: this.status, capabilities: this.capabilities, role: this.role, parent: this.parent, taskCount: this._taskCount, errorCount: this._errorCount, createdAt: this.createdAt, lastActive: this.lastActive, }; } /** Create an agent from a config object (deserialization) */ static fromConfig(config) { return new Agent(config); } } export default Agent;