creates stub -> wires intent -> zero manual header('Content-Type: application/json'); header('Access-Control-Allow-Origin: *'); $action = $_REQUEST['action'] ?? 'list'; $registry_file = '/var/www/html/api/wevia-tool-registry.json'; $avatars_file = '/var/www/html/api/agent-avatars-v2.json'; $depts_kpi = '/var/www/html/api/wevia-v64-departments-kpi.php'; function load_missing_agents() { global $depts_kpi; if (!is_readable($depts_kpi)) return []; $content = file_get_contents($depts_kpi); preg_match_all("/'gap_agent'=>'([^']+)'/", $content, $m); return array_values(array_unique($m[1] ?? [])); } function agent_id($name) { $id = strtolower($name); $id = preg_replace('/[^a-z0-9]+/', '_', $id); $id = trim($id, '_'); return $id; } function agent_exists_in_registry($name) { global $registry_file; if (!is_readable($registry_file)) return false; $reg = json_decode(file_get_contents($registry_file), true); $tools = $reg['tools'] ?? $reg ?? []; $id = 'agent_' . agent_id($name); foreach ($tools as $t) { if (is_array($t) && ($t['id'] ?? '') === $id) return true; } return false; } function create_agent_stub($name) { $id = agent_id($name); $stub_path = "/var/www/html/api/agent-stubs/agent_{$id}.php"; $stub_dir = dirname($stub_path); if (!is_dir($stub_dir)) @mkdir($stub_dir, 0755, true); $stub = " '{$name}',\n"; $stub .= " 'id' => 'agent_{$id}',\n"; $stub .= " 'status' => 'stub',\n"; $stub .= " 'version' => 'V67',\n"; $stub .= " 'note' => 'Auto-created by agent-factory. Enrich with business logic.',\n"; $stub .= " 'kpis' => [],\n"; $stub .= " 'generated' => date('c')\n"; $stub .= "]);\n"; $wrote = @file_put_contents($stub_path, $stub); return ['ok' => $wrote !== false, 'path' => $stub_path, 'bytes' => $wrote]; } if ($action === 'list') { $missing = load_missing_agents(); $result = []; foreach ($missing as $name) { $result[] = [ 'name' => $name, 'id' => 'agent_' . agent_id($name), 'in_registry' => agent_exists_in_registry($name), 'stub_exists' => file_exists("/var/www/html/api/agent-stubs/agent_" . agent_id($name) . ".php") ]; } echo json_encode(['ok' => true, 'total' => count($result), 'agents' => $result], JSON_PRETTY_PRINT); exit; } if ($action === 'create') { $name = $_REQUEST['agent'] ?? ''; if (!$name) { echo json_encode(['ok' => false, 'error' => 'agent name required']); exit; } $result = create_agent_stub($name); $result['name'] = $name; $result['id'] = 'agent_' . agent_id($name); echo json_encode($result, JSON_PRETTY_PRINT); exit; } if ($action === 'bulk_create') { $missing = load_missing_agents(); $created = 0; $skipped = 0; $errors = 0; $log = []; foreach ($missing as $name) { $id = agent_id($name); $stub_path = "/var/www/html/api/agent-stubs/agent_{$id}.php"; if (file_exists($stub_path)) { $skipped++; continue; } $r = create_agent_stub($name); if ($r['ok']) { $created++; $log[] = $name; } else { $errors++; } } echo json_encode(['ok' => true, 'created' => $created, 'skipped' => $skipped, 'errors' => $errors, 'agents_created' => $log], JSON_PRETTY_PRINT); exit; } if ($action === 'count') { $missing = load_missing_agents(); $created = 0; foreach ($missing as $name) { $id = agent_id($name); if (file_exists("/var/www/html/api/agent-stubs/agent_{$id}.php")) $created++; } echo json_encode([ 'ok' => true, 'total_missing' => count($missing), 'already_created' => $created, 'remaining' => count($missing) - $created ]); exit; } echo json_encode(['ok' => false, 'error' => 'unknown action', 'valid_actions' => ['list','create','bulk_create','count']]);