156 lines
5.7 KiB
PHP
Executable File
156 lines
5.7 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* S88 Brain Module Activator
|
|
* Run AFTER nginx+PHP installed on S88 and Ollama is LOCAL
|
|
*
|
|
* Activates class-based cognitive modules that need local Ollama:
|
|
* - MetacognitionEngine (confidence calibration, bias detection)
|
|
* - ContextManager (token budget optimization)
|
|
* - OutputFormatter (auto-format responses)
|
|
* - ErrorTaxonomy (smart error recovery)
|
|
* - ChainOfThoughtV2 (structured reasoning)
|
|
* - ConversationMemory (long-term entity tracking)
|
|
* - CodeAnalyzer (code quality assessment)
|
|
* - SmartReporter (auto report generation)
|
|
*/
|
|
|
|
// ═══ ACTIVATION CHECK ═══
|
|
$ollamaLocal = @file_get_contents("http://127.0.0.1:11434/");
|
|
if (!$ollamaLocal || strpos($ollamaLocal, "running") === false) {
|
|
die("ERROR: Ollama not running locally. Abort.\n");
|
|
}
|
|
echo "✅ Ollama LOCAL confirmed\n";
|
|
|
|
// ═══ UPDATE AUTOLOAD ═══
|
|
$autoload = <<<'AUTOLOAD'
|
|
<?php
|
|
/**
|
|
* WEVIA Brain Autoload — S88 Consolidated
|
|
* All cognitive modules loaded from /opt/wevia-brain/
|
|
*/
|
|
spl_autoload_register(function($class) {
|
|
$map = [
|
|
// Core modules
|
|
'MetacognitionEngine' => '/opt/wevia-brain/modules/core/metacognition.php',
|
|
'ContextManager' => '/opt/wevia-brain/modules/core/context-manager.php',
|
|
'OutputFormatter' => '/opt/wevia-brain/modules/core/output-formatter.php',
|
|
'ErrorTaxonomy' => '/opt/wevia-brain/modules/core/error-taxonomy.php',
|
|
'ConversationRouter' => '/opt/wevia-brain/modules/core/conversation-router.php',
|
|
// Advanced modules
|
|
'CodeAnalyzer' => '/opt/wevia-brain/modules/advanced/code-analyzer.php',
|
|
'ConversationMemory' => '/opt/wevia-brain/modules/advanced/conversation-memory.php',
|
|
'MultiAgent' => '/opt/wevia-brain/modules/advanced/multi-agent.php',
|
|
'SmartReporter' => '/opt/wevia-brain/modules/advanced/smart-reporter.php',
|
|
'WebIntelligence' => '/opt/wevia-brain/modules/advanced/web-intelligence.php',
|
|
// Agentic modules
|
|
'Planner' => '/opt/wevia-brain/modules/agentic/planner.php',
|
|
'SelfHealing' => '/opt/wevia-brain/modules/agentic/self-healing.php',
|
|
// Pipeline modules
|
|
'EmbeddingPipeline' => '/opt/wevia-brain/modules/pipeline/embedding-pipeline.php',
|
|
'StreamingHandler' => '/opt/wevia-brain/modules/pipeline/streaming-handler.php',
|
|
'TokenTracker' => '/opt/wevia-brain/modules/pipeline/token-tracker.php',
|
|
// V2 modules
|
|
'ChainOfThoughtV2' => '/opt/wevia-brain/modules/v2/chain-of-thought-v2.php',
|
|
'ToolUseV2' => '/opt/wevia-brain/modules/v2/tool-use-v2.php',
|
|
];
|
|
if (isset($map[$class]) && file_exists($map[$class])) {
|
|
require_once $map[$class];
|
|
}
|
|
});
|
|
AUTOLOAD;
|
|
|
|
file_put_contents("/opt/wevia-brain/autoload.php", $autoload);
|
|
echo "✅ Autoload updated (17 modules mapped)\n";
|
|
|
|
// ═══ CREATE BRAIN INTEGRATION HOOK ═══
|
|
$hook = <<<'HOOK'
|
|
<?php
|
|
/**
|
|
* S88 Brain Hook — Wire class modules into weval-chatbot-api.php
|
|
* Include this AFTER cognitive-brain.php in the main API
|
|
*
|
|
* Usage in weval-chatbot-api.php:
|
|
* require_once "/opt/wevia-brain/s88-brain-hook.php";
|
|
*/
|
|
|
|
// Initialize singletons (lazy — only created when first used)
|
|
$_brainModules = [];
|
|
|
|
function getBrainModule(string $class): ?object {
|
|
global $_brainModules;
|
|
if (!isset($_brainModules[$class])) {
|
|
if (!class_exists($class)) return null;
|
|
try {
|
|
$_brainModules[$class] = new $class('http://127.0.0.1:11434');
|
|
} catch (Exception $e) {
|
|
error_log("WEVIA_BRAIN_MODULE_ERR: $class — " . $e->getMessage());
|
|
return null;
|
|
}
|
|
}
|
|
return $_brainModules[$class];
|
|
}
|
|
|
|
/**
|
|
* Run metacognitive analysis on a query
|
|
* Returns: strategy recommendation + confidence calibration
|
|
*/
|
|
function brainMetacognize(string $message, string $intent): array {
|
|
$meta = getBrainModule('MetacognitionEngine');
|
|
if (!$meta) return ['strategy' => 'default', 'confidence' => 0.5];
|
|
try {
|
|
return $meta->analyze($message, $intent);
|
|
} catch (Exception $e) { return ['strategy' => 'default', 'confidence' => 0.5]; }
|
|
}
|
|
|
|
/**
|
|
* Optimize context window allocation
|
|
*/
|
|
function brainOptimizeContext(string $sys, string $msg, array $history, string $kbContext): string {
|
|
$ctx = getBrainModule('ContextManager');
|
|
if (!$ctx) return $sys;
|
|
try {
|
|
return $ctx->optimize($sys, $msg, $history, $kbContext);
|
|
} catch (Exception $e) { return $sys; }
|
|
}
|
|
|
|
/**
|
|
* Format output optimally
|
|
*/
|
|
function brainFormatOutput(string $response): string {
|
|
$fmt = getBrainModule('OutputFormatter');
|
|
if (!$fmt) return $response;
|
|
try {
|
|
return $fmt->format($response);
|
|
} catch (Exception $e) { return $response; }
|
|
}
|
|
|
|
/**
|
|
* Smart error recovery
|
|
*/
|
|
function brainRecoverError(string $error, string $context): ?string {
|
|
$err = getBrainModule('ErrorTaxonomy');
|
|
if (!$err) return null;
|
|
try {
|
|
$classified = $err->classify($error);
|
|
return $err->suggestRecovery($classified, $context);
|
|
} catch (Exception $e) { return null; }
|
|
}
|
|
HOOK;
|
|
|
|
file_put_contents("/opt/wevia-brain/s88-brain-hook.php", $hook);
|
|
echo "✅ Brain hook created (4 integration functions)\n";
|
|
|
|
// ═══ VERIFY ALL MODULE FILES EXIST ═══
|
|
$modules = glob("/opt/wevia-brain/modules/*/*.php");
|
|
echo "✅ " . count($modules) . " brain modules found\n";
|
|
foreach ($modules as $m) {
|
|
$size = filesize($m);
|
|
$name = basename($m, '.php');
|
|
echo " 📦 $name (" . number_format($size) . " bytes)\n";
|
|
}
|
|
|
|
echo "\n═══ S88 BRAIN READY ═══\n";
|
|
echo "To activate in weval-chatbot-api.php, add AFTER cognitive-brain.php:\n";
|
|
echo " require_once '/opt/wevia-brain/s88-brain-hook.php';\n";
|
|
echo "Then use: brainMetacognize(), brainOptimizeContext(), brainFormatOutput(), brainRecoverError()\n";
|