104 lines
3.8 KiB
PHP
Executable File
104 lines
3.8 KiB
PHP
Executable File
<?php
|
|
/*
|
|
PATCH · Intégrer wevia-memory-bridge dans wevia-chat-v2-direct.php
|
|
Doctrine 141 · Pilote mémoire persistante universelle
|
|
Opus · 23avr 21h45
|
|
|
|
Points d'injection:
|
|
1. Ligne ~17 (après session def): require_once + load history
|
|
2. Ligne ~end (avant echo json_encode final): save exchange
|
|
|
|
Principe: chatbot garde sa logique existante + gagne mémoire Redis DB 5.
|
|
*/
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$target = '/var/www/html/api/wevia-chat-v2-direct.php';
|
|
$backup = '/var/www/html/vault-gold/opus/wevia-chat-v2-direct-' . date('Ymd-His') . '.bak';
|
|
|
|
if (!file_exists($target)) {
|
|
echo json_encode(['ok'=>false,'err'=>'target missing']);
|
|
exit;
|
|
}
|
|
|
|
@mkdir(dirname($backup), 0755, true);
|
|
copy($target, $backup);
|
|
|
|
$content = file_get_contents($target);
|
|
|
|
// PATCH 1 : Inject after $t0 line (line 17) - load context + require bridge
|
|
$old1 = '$t0 = microtime(true);';
|
|
$new1 = '$t0 = microtime(true);
|
|
// === DOCTRINE 141 · Memory bridge ===
|
|
if (@file_exists(__DIR__.\'/wevia-memory-bridge.php\')) {
|
|
@require_once __DIR__.\'/wevia-memory-bridge.php\';
|
|
}
|
|
$__chat_id = \'wevia-chat-v2\';
|
|
$__user_id = $session ?: \'anon\';
|
|
$__mem_context = function_exists(\'wevia_mem_context\') ? wevia_mem_context($__chat_id, $__user_id, 10) : \'\';';
|
|
|
|
if (strpos($content, $old1) === false) {
|
|
echo json_encode(['ok'=>false, 'err'=>'patch1 anchor not found']);
|
|
exit;
|
|
}
|
|
// Skip if already patched
|
|
if (strpos($content, 'DOCTRINE 141') !== false) {
|
|
echo json_encode(['ok'=>false, 'err'=>'already patched (doctrine 141 marker present)']);
|
|
exit;
|
|
}
|
|
|
|
$content = str_replace($old1, $new1, $content);
|
|
|
|
// PATCH 2 : Inject before final echo json_encode - save to memory
|
|
$old2 = "\$elapsed_ms = (int)((microtime(true) - \$t0) * 1000);\n\necho json_encode([";
|
|
$new2 = "\$elapsed_ms = (int)((microtime(true) - \$t0) * 1000);\n\n// === DOCTRINE 141 · Save exchange to memory ===\nif (function_exists('wevia_mem_save') && \$response) {\n @wevia_mem_save(\$__chat_id, \$__user_id, \$msg, is_string(\$response) ? \$response : json_encode(\$response), 'internal');\n}\n\necho json_encode([";
|
|
|
|
if (strpos($content, $old2) === false) {
|
|
echo json_encode(['ok'=>false, 'err'=>'patch2 anchor not found', 'snippet'=>substr($old2, 0, 60)]);
|
|
exit;
|
|
}
|
|
$content = str_replace($old2, $new2, $content);
|
|
|
|
// Lint
|
|
$tmp = tempnam('/tmp', 'v2d-patch-');
|
|
file_put_contents($tmp, $content);
|
|
$lint = shell_exec("php -l $tmp 2>&1");
|
|
if (strpos($lint, 'No syntax errors') === false) {
|
|
unlink($tmp);
|
|
echo json_encode(['ok'=>false, 'err'=>'php lint failed', 'lint'=>$lint]);
|
|
exit;
|
|
}
|
|
|
|
// Write
|
|
shell_exec("sudo chattr -i $target 2>/dev/null");
|
|
file_put_contents($target, $content);
|
|
shell_exec("sudo chown www-data:www-data $target");
|
|
shell_exec("sudo chattr +i $target 2>/dev/null");
|
|
unlink($tmp);
|
|
@opcache_reset();
|
|
|
|
// Test: send 2 messages + verify history
|
|
$sess = 'test-'.uniqid();
|
|
$r1 = shell_exec("curl -sk -m 8 'http://localhost/api/wevia-chat-v2-direct.php' -H 'Content-Type: application/json' -d '{\"message\":\"intents pool\",\"session\":\"$sess\"}' 2>&1 | head -c 400");
|
|
$r2 = shell_exec("curl -sk -m 8 'http://localhost/api/wevia-chat-v2-direct.php' -H 'Content-Type: application/json' -d '{\"message\":\"quelle heure\",\"session\":\"$sess\"}' 2>&1 | head -c 200");
|
|
|
|
// Verify memory stored
|
|
$mem_check = '';
|
|
if (function_exists('wevia_mem_load')) {
|
|
require_once '/var/www/html/api/wevia-memory-bridge.php';
|
|
$hist = wevia_mem_load('wevia-chat-v2', $sess, 5);
|
|
$mem_check = "History stored: " . count($hist) . " messages";
|
|
}
|
|
|
|
echo json_encode([
|
|
'ok' => true,
|
|
'backup' => $backup,
|
|
'patches_applied' => ['load_context_after_t0', 'save_exchange_before_echo'],
|
|
'lint' => 'ok',
|
|
'opcache_reset' => 'done',
|
|
'test_msg1' => substr($r1, 0, 200),
|
|
'test_msg2' => substr($r2, 0, 150),
|
|
'memory_verification' => $mem_check,
|
|
'ts' => date('c')
|
|
]);
|