fix(memory): align substring index with trimmed input in extractHeuristicFact

This commit is contained in:
Marcos Paulo
2026-08-18 22:00:42 -03:00
parent 01ada6d987
commit e71babca1c
2 changed files with 7 additions and 2 deletions
@@ -205,7 +205,8 @@ final class PlayerMemory {
if (text == null) { if (text == null) {
return null; return null;
} }
String lower = text.toLowerCase(Locale.ROOT).trim(); String clean = text.trim();
String lower = clean.toLowerCase(Locale.ROOT);
String[] triggers = { String[] triggers = {
"minha base", "meu spawn", "minha casa", "estou construindo", "minha base", "meu spawn", "minha casa", "estou construindo",
"meu plano", "meu objetivo", "sou especialista em", "moro em" "meu plano", "meu objetivo", "sou especialista em", "moro em"
@@ -213,7 +214,7 @@ final class PlayerMemory {
for (String trigger : triggers) { for (String trigger : triggers) {
int idx = lower.indexOf(trigger); int idx = lower.indexOf(trigger);
if (idx >= 0) { if (idx >= 0) {
String candidate = text.substring(idx).trim(); String candidate = clean.substring(idx).trim();
candidate = candidate.replaceAll("[?!.]+$", "").trim(); candidate = candidate.replaceAll("[?!.]+$", "").trim();
if (candidate.length() > 60) { if (candidate.length() > 60) {
candidate = candidate.substring(0, 60) + ""; candidate = candidate.substring(0, 60) + "";
@@ -143,6 +143,10 @@ class PlayerMemoryTest {
assertNotNull(fact); assertNotNull(fact);
assertTrue(fact.contains("estou construindo uma pirâmide gigante")); assertTrue(fact.contains("estou construindo uma pirâmide gigante"));
assertNull(PlayerMemory.extractHeuristicFact("quantos blocos tem o mundo?")); assertNull(PlayerMemory.extractHeuristicFact("quantos blocos tem o mundo?"));
// Leading / trailing whitespace index alignment
assertEquals("minha base fica no topo", PlayerMemory.extractHeuristicFact(" minha base fica no topo! "));
assertEquals("estou construindo uma ponte", PlayerMemory.extractHeuristicFact(" oi, estou construindo uma ponte..."));
} }
@Test @Test