Files
wevia-brain/wevia-master-bridge.php
2026-04-12 23:01:36 +02:00

81 lines
2.8 KiB
PHP

<?php
/**
* WEVIA MASTER BRIDGE v1.0
* Bridge between existing weval-chatbot-api.php and Master Router
*
* USAGE (from any PHP file):
* require_once '/opt/wevia-brain/wevia-master-bridge.php';
* $result = wevia_master_route($message, $systemPrompt, $history);
* echo $result['content'];
*
* Or as internal API:
* curl http://127.0.0.1/api/wevia-master-bridge.php -d '{"message":"..."}'
*/
// Load master router + RAG if not already loaded
if (!function_exists('mr_route')) {
require_once __DIR__ . '/wevia-master-router.php';
}
/**
* Simple wrapper function for existing code integration
* Returns response in the same format as existing providers
*/
function wevia_master_route($message, $systemPrompt = '', $history = [], $options = []) {
if (empty($systemPrompt)) {
$systemPrompt = "Tu es WEVIA, l'IA cognitive souveraine de WEVAL Consulting (Casablanca). Réponds de manière précise, technique et directe.";
}
$result = mr_route($message, $systemPrompt, $history, $options);
// Return in format compatible with existing chatbot
return [
'content' => $result['content'] ?? '',
'model' => $result['model'] ?? 'unknown',
'provider' => 'wevia-master-' . ($result['provider'] ?? 'unknown'),
'tier' => $result['tier'] ?? -1,
'latency_ms' => $result['latency_ms'] ?? 0,
'cost' => $result['cost'] ?? 0,
'source' => $result['source'] ?? 'master-router',
'rag_results' => count($result['routing']['rag']['results_count'] ?? []),
// Compatible with existing chatbot format
'text' => $result['content'] ?? '',
'success' => !empty($result['content']),
'error' => empty($result['content']) ? 'No response from any provider' : null,
];
}
/**
* Quick test function
*/
function wevia_master_test($msg = 'Bonjour WEVIA') {
return wevia_master_route($msg);
}
// If called directly as API
if (basename($_SERVER['SCRIPT_FILENAME'] ?? '') === 'wevia-master-bridge.php') {
header("Content-Type: application/json; charset=utf-8");
header("Access-Control-Allow-Origin: *");
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$input = json_decode(file_get_contents("php://input"), true);
$msg = $input['message'] ?? $input['msg'] ?? '';
$sys = $input['system'] ?? '';
$hist = $input['history'] ?? [];
if (empty(trim($msg))) {
echo json_encode(['error' => 'message required']);
exit;
}
echo json_encode(wevia_master_route($msg, $sys, $hist), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
} else {
echo json_encode([
'bridge' => 'WEVIA Master Bridge v1.0',
'usage' => 'POST with {"message":"your question"}',
'function' => 'wevia_master_route($message, $systemPrompt, $history)',
]);
}
}