step method
Executes a single step of the agent's main logic.
This method is called repeatedly by the orchestrator and should:
- Check for work to do
- Perform any necessary actions
- Update status appropriately
Implementations should be idempotent and handle their own errors.
Implementation
@override
Future<void> step() async {
updateStatus(AgentStatus.working);
final pendingTasks = orchestrator.getPendingTasks();
if (pendingTasks.isEmpty) {
logger.detail('[Mayor $id] No pending tasks.');
updateStatus(AgentStatus.idle);
return;
}
final idleWorkers = orchestrator
.getAgentsByRole(AgentRole.worker)
.where((a) => a.status == AgentStatus.idle)
.cast<WorkerAgent>()
.toList();
if (idleWorkers.isEmpty) {
logger.detail('[Mayor $id] No idle workers available.');
updateStatus(AgentStatus.idle);
return;
}
for (var i = 0; i < pendingTasks.length && i < idleWorkers.length; i++) {
final task = pendingTasks[i];
final worker = idleWorkers[i];
logger.info(
'[Mayor $id] Assigning Task #${task.id} to Worker ${worker.id}',
);
worker.assignTask(task);
}
updateStatus(AgentStatus.idle);
}