40 lines
1.9 KiB
PHP
40 lines
1.9 KiB
PHP
<?php
|
|
/**
|
|
* WEDROID Learning Loop v1.0
|
|
* Stores successful command patterns + feedback for continuous improvement
|
|
*/
|
|
function wedroidLearn($cmd, $output, $success, $context = '') {
|
|
$pdo = new PDO("pgsql:host=127.0.0.1;dbname=adx_system","postgres","");
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS admin.wedroid_knowledge (id SERIAL PRIMARY KEY, pattern TEXT, cmd TEXT, output_sample TEXT, success BOOLEAN, context TEXT, used_count INT DEFAULT 1, created_at TIMESTAMP DEFAULT NOW(), last_used TIMESTAMP DEFAULT NOW())");
|
|
|
|
// Check if pattern exists
|
|
$pattern = preg_replace('/\d+/', 'N', $cmd); // Normalize numbers
|
|
$pattern = preg_replace('/\s+/', ' ', trim($pattern));
|
|
|
|
$existing = $pdo->prepare("SELECT id, used_count FROM admin.wedroid_knowledge WHERE pattern = ?");
|
|
$existing->execute([$pattern]);
|
|
$row = $existing->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($row) {
|
|
$pdo->prepare("UPDATE admin.wedroid_knowledge SET used_count=used_count+1, last_used=NOW(), success=? WHERE id=?")
|
|
->execute([$success, $row['id']]);
|
|
} else {
|
|
$pdo->prepare("INSERT INTO admin.wedroid_knowledge (pattern, cmd, output_sample, success, context) VALUES (?,?,?,?,?)")
|
|
->execute([$pattern, $cmd, substr($output, 0, 200), $success, $context]);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function wedroidRecall($query) {
|
|
$pdo = new PDO("pgsql:host=127.0.0.1;dbname=adx_system","postgres","");
|
|
$words = explode(' ', strtolower($query));
|
|
$conditions = [];
|
|
foreach (array_slice($words, 0, 3) as $w) {
|
|
if (strlen($w) > 2) $conditions[] = "LOWER(pattern) LIKE '%" . pg_escape_string($w) . "%'";
|
|
}
|
|
if (empty($conditions)) return [];
|
|
$where = implode(' OR ', $conditions);
|
|
$r = $pdo->query("SELECT cmd, output_sample, used_count, success FROM admin.wedroid_knowledge WHERE ($where) AND success=true ORDER BY used_count DESC LIMIT 3");
|
|
return $r ? $r->fetchAll(PDO::FETCH_ASSOC) : [];
|
|
}
|