71 lines
2.1 KiB
PHP
Executable File
71 lines
2.1 KiB
PHP
Executable File
<?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; }
|
|
} |