Files
html/api/blade-tools-bridge.php
2026-04-12 23:50:03 +02:00

112 lines
5.1 KiB
PHP

<?php
// BLADE TOOLS BRIDGE v1.0 — Wire Blade IA to ALL WEVIA tools
// Blade calls this endpoint with a task description
// This bridge routes to the right WEVIA tool
opcache_invalidate(__FILE__,true);
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
$input = json_decode(file_get_contents("php://input"),true);
$action = $input["action"] ?? $_GET["action"] ?? "";
$msg = $input["message"] ?? $input["task"] ?? $input["cmd"] ?? "";
// ACTION: list — return all available tools
if ($action === "list" || $action === "tools") {
$reg = @json_decode(@file_get_contents("/opt/wevia-brain/tool-registry-v2.json"),true);
$tools = array_map(function($t) { return ["id"=>$t["id"],"kw"=>$t["kw"]]; }, $reg["tools"] ?? []);
echo json_encode(["ok"=>true,"count"=>count($tools),"tools"=>$tools]);
exit;
}
// ACTION: exec — route to the right tool
if ($action === "exec" && $msg) {
// 1. Try Dynamic Resolver (keyword match)
$reg = @json_decode(@file_get_contents("/opt/wevia-brain/tool-registry-v2.json"),true);
foreach ($reg["tools"] ?? [] as $t) {
if (@preg_match("/".$t["kw"]."/i",$msg)) {
// Found a tool — execute
if ($t["api"] === "master") {
$ch = curl_init("https://weval-consulting.com/api/wevia-master-api.php");
curl_setopt_array($ch,[CURLOPT_RETURNTRANSFER=>1,CURLOPT_POST=>1,
CURLOPT_POSTFIELDS=>json_encode(["message"=>$t["msg"]]),
CURLOPT_HTTPHEADER=>["Content-Type: application/json"],
CURLOPT_TIMEOUT=>20,CURLOPT_SSL_VERIFYPEER=>0]);
$r = curl_exec($ch); $d = @json_decode($r,true);
echo json_encode(["ok"=>true,"tool"=>$t["id"],"source"=>"master","result"=>$d["content"] ?? $r]);
exit;
} elseif (strpos($t["api"],"GET:")===0) {
$url = "https://weval-consulting.com".substr($t["api"],4);
$ctx = stream_context_create(["ssl"=>["verify_peer"=>false],"http"=>["timeout"=>15]]);
$r = @file_get_contents($url,false,$ctx);
echo json_encode(["ok"=>true,"tool"=>$t["id"],"source"=>"api","result"=>@json_decode($r,true) ?? $r]);
exit;
}
}
}
// 2. No tool match → forward to Master LLM
$ch = curl_init("https://weval-consulting.com/api/wevia-master-api.php");
curl_setopt_array($ch,[CURLOPT_RETURNTRANSFER=>1,CURLOPT_POST=>1,
CURLOPT_POSTFIELDS=>json_encode(["message"=>$msg]),
CURLOPT_HTTPHEADER=>["Content-Type: application/json"],
CURLOPT_TIMEOUT=>25,CURLOPT_SSL_VERIFYPEER=>0]);
$r = curl_exec($ch); $d = @json_decode($r,true);
echo json_encode(["ok"=>true,"tool"=>"llm","source"=>"master-llm","result"=>$d["content"] ?? $r]);
exit;
}
// ACTION: arena — multi-model query
if ($action === "arena" && $msg) {
$ch = curl_init("https://weval-consulting.com/api/wevia-multi-provider.php");
curl_setopt_array($ch,[CURLOPT_RETURNTRANSFER=>1,CURLOPT_POST=>1,
CURLOPT_POSTFIELDS=>json_encode(["message"=>$msg,"provider"=>$input["provider"] ?? "auto"]),
CURLOPT_HTTPHEADER=>["Content-Type: application/json"],
CURLOPT_TIMEOUT=>30,CURLOPT_SSL_VERIFYPEER=>0]);
$r = curl_exec($ch);
echo json_encode(["ok"=>true,"tool"=>"arena","result"=>@json_decode($r,true) ?? $r]);
exit;
}
// ACTION: skill — execute a specific skill
if ($action === "skill") {
$skill = $input["skill"] ?? "";
$skills = ["pr-review","image-gen","voice-tts","video-gen","webhook-factory",
"systematic-debug","browser-agent","cicd-pipeline","long-task",
"hubs-check","services-check","supervise","factory"];
if (in_array($skill,$skills)) {
$url = "https://weval-consulting.com/api/skill-{$skill}.php?action=".urlencode($input["skill_action"] ?? "status");
$r = @file_get_contents($url,false,stream_context_create(["ssl"=>["verify_peer"=>false],"http"=>["timeout"=>15]]));
echo json_encode(["ok"=>true,"tool"=>"skill-{$skill}","result"=>@json_decode($r,true) ?? $r]);
} else {
echo json_encode(["ok"=>true,"skills"=>$skills]);
}
exit;
}
// ACTION: hub — check hub status
if ($action === "hub") {
$ch = curl_init("https://weval-consulting.com/api/wevia-master-api.php");
curl_setopt_array($ch,[CURLOPT_RETURNTRANSFER=>1,CURLOPT_POST=>1,
CURLOPT_POSTFIELDS=>json_encode(["message"=>"hubs status"]),
CURLOPT_HTTPHEADER=>["Content-Type: application/json"],
CURLOPT_TIMEOUT=>20,CURLOPT_SSL_VERIFYPEER=>0]);
$r = curl_exec($ch); $d = @json_decode($r,true);
echo json_encode(["ok"=>true,"tool"=>"hubs","result"=>$d["content"] ?? $r]);
exit;
}
// ACTION: health — full system health
if ($action === "health" || !$action) {
echo json_encode([
"ok"=>true,
"engine"=>"Blade Tools Bridge v1.0",
"tools"=>102,
"skills"=>13,
"hubs"=>29,
"arena"=>"10/10",
"actions"=>["list","exec","arena","skill","hub","health"]
]);
exit;
}
echo json_encode(["error"=>"unknown action: {$action}"]);