108 lines
5.4 KiB
PHP
108 lines
5.4 KiB
PHP
<?php
|
|
header("Content-Type: application/json; charset=utf-8");
|
|
header("Access-Control-Allow-Origin: *");
|
|
$action = $_GET["action"] ?? "overview";
|
|
|
|
// Paperclip DB
|
|
$pg = @pg_connect("host=127.0.0.1 port=5432 dbname=paperclip user=admin password=admin123");
|
|
|
|
// Registry
|
|
$reg = @json_decode(@file_get_contents("/opt/wevia-brain/tool-registry-v2.json"), true);
|
|
$tools = $reg["tools"] ?? [];
|
|
|
|
if ($action === "overview") {
|
|
$data = ["ok" => true, "sources" => []];
|
|
|
|
// Paperclip agents
|
|
if ($pg) {
|
|
$r = pg_query($pg, "SELECT COUNT(DISTINCT name) FROM agents");
|
|
$agents_count = (int)pg_fetch_result($r, 0, 0);
|
|
$r2 = pg_query($pg, "SELECT role, COUNT(DISTINCT name) as cnt FROM agents GROUP BY role ORDER BY cnt DESC");
|
|
$roles = [];
|
|
while ($row = pg_fetch_assoc($r2)) $roles[] = $row;
|
|
$data["sources"]["paperclip_agents"] = ["count" => $agents_count, "roles" => $roles];
|
|
}
|
|
|
|
// Paperclip skills
|
|
if ($pg) {
|
|
$r = pg_query($pg, "SELECT COUNT(*) FROM company_skills");
|
|
$skills_count = (int)pg_fetch_result($r, 0, 0);
|
|
$r2 = pg_query($pg, "SELECT source_type, COUNT(*) as cnt FROM company_skills GROUP BY source_type ORDER BY cnt DESC");
|
|
$sources = [];
|
|
while ($row = pg_fetch_assoc($r2)) $sources[] = $row;
|
|
$data["sources"]["paperclip_skills"] = ["count" => $skills_count, "sources" => $sources];
|
|
}
|
|
|
|
// DeerFlow skills
|
|
$df_skills = glob("/opt/deer-flow/skills/*");
|
|
$data["sources"]["deerflow"] = ["count" => count($df_skills), "skills" => array_map("basename", array_slice($df_skills, 0, 30))];
|
|
|
|
// Registry tools
|
|
$data["sources"]["registry_tools"] = ["count" => count($tools), "sample" => array_map(function($t) { return $t["id"] ?? "?"; }, array_slice($tools, 0, 20))];
|
|
|
|
// Totals
|
|
$data["totals"] = [
|
|
"agents" => $agents_count ?? 0,
|
|
"skills" => ($skills_count ?? 0) + count($df_skills),
|
|
"tools" => count($tools),
|
|
"grand_total" => ($agents_count ?? 0) + ($skills_count ?? 0) + count($df_skills) + count($tools)
|
|
];
|
|
|
|
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
|
|
|
} elseif ($action === "agents") {
|
|
if (!$pg) { echo json_encode(["error" => "DB_DOWN"]); exit; }
|
|
$role = $_GET["role"] ?? "";
|
|
$where = $role ? "WHERE role='".pg_escape_string($pg, $role)."'" : "";
|
|
$r = pg_query($pg, "SELECT DISTINCT name, role, title, capabilities, status FROM agents $where ORDER BY role, name LIMIT 200");
|
|
$agents = [];
|
|
while ($row = pg_fetch_assoc($r)) $agents[] = $row;
|
|
echo json_encode(["ok" => true, "count" => count($agents), "agents" => $agents]);
|
|
|
|
} elseif ($action === "skills") {
|
|
if (!$pg) { echo json_encode(["error" => "DB_DOWN"]); exit; }
|
|
$source = $_GET["source"] ?? "";
|
|
$search = $_GET["q"] ?? "";
|
|
$where = "WHERE 1=1";
|
|
if ($source) $where .= " AND source_type='".pg_escape_string($pg, $source)."'";
|
|
if ($search) $where .= " AND (name ILIKE '%".pg_escape_string($pg, $search)."%' OR description ILIKE '%".pg_escape_string($pg, $search)."%')";
|
|
$r = pg_query($pg, "SELECT name, description, source_type, trust_level FROM company_skills $where ORDER BY source_type, name LIMIT 100");
|
|
$skills = [];
|
|
while ($row = pg_fetch_assoc($r)) $skills[] = $row;
|
|
echo json_encode(["ok" => true, "count" => count($skills), "skills" => $skills]);
|
|
|
|
} elseif ($action === "tools") {
|
|
$search = $_GET["q"] ?? "";
|
|
$cat = $_GET["cat"] ?? "";
|
|
$filtered = $tools;
|
|
if ($search) $filtered = array_filter($tools, function($t) use ($search) { return stripos($t["id"] ?? "", $search) !== false || stripos($t["kw"] ?? "", $search) !== false; });
|
|
if ($cat) $filtered = array_filter($tools, function($t) use ($cat) { return ($t["cat"] ?? "") === $cat; });
|
|
$result = array_map(function($t) { return ["id" => $t["id"] ?? "?", "kw" => $t["kw"] ?? "", "api" => $t["api"] ?? "", "cat" => $t["cat"] ?? ""]; }, array_values($filtered));
|
|
echo json_encode(["ok" => true, "count" => count($result), "tools" => array_slice($result, 0, 100)]);
|
|
|
|
} elseif ($action === "search") {
|
|
$q = $_GET["q"] ?? "";
|
|
if (!$q) { echo json_encode(["error" => "q required"]); exit; }
|
|
$results = ["query" => $q, "found" => []];
|
|
// Search agents
|
|
if ($pg) {
|
|
$r = pg_query($pg, "SELECT DISTINCT name, role, capabilities FROM agents WHERE name ILIKE '%".pg_escape_string($pg, $q)."%' OR capabilities ILIKE '%".pg_escape_string($pg, $q)."%' LIMIT 10");
|
|
while ($row = pg_fetch_assoc($r)) $results["found"][] = ["type" => "agent", "name" => $row["name"], "role" => $row["role"], "detail" => $row["capabilities"]];
|
|
}
|
|
// Search skills
|
|
if ($pg) {
|
|
$r = pg_query($pg, "SELECT name, source_type, LEFT(description,80) as desc FROM company_skills WHERE name ILIKE '%".pg_escape_string($pg, $q)."%' OR description ILIKE '%".pg_escape_string($pg, $q)."%' LIMIT 10");
|
|
while ($row = pg_fetch_assoc($r)) $results["found"][] = ["type" => "skill", "name" => $row["name"], "source" => $row["source_type"], "detail" => $row["desc"]];
|
|
}
|
|
// Search tools
|
|
foreach ($tools as $t) {
|
|
if (stripos($t["id"] ?? "", $q) !== false || stripos($t["kw"] ?? "", $q) !== false) {
|
|
$results["found"][] = ["type" => "tool", "name" => $t["id"], "detail" => $t["kw"] ?? ""];
|
|
if (count($results["found"]) > 30) break;
|
|
}
|
|
}
|
|
echo json_encode(["ok" => true, "results" => $results], JSON_PRETTY_PRINT);
|
|
}
|
|
|
|
if ($pg) pg_close($pg);
|