fix(chat): normalize commands (case/punctuation), add goals status alias, improve conversational fallback

This commit is contained in:
Prime Backup Bot
2026-03-12 00:18:18 +00:00
parent 02a9460bfd
commit 30aca465a7
+39 -21
View File
@@ -2,6 +2,7 @@ from __future__ import annotations
import json
import os
import shlex
import re
import subprocess
import time
import urllib.parse
@@ -105,6 +106,15 @@ def run_prime(args: list[str]) -> tuple[int, str]:
return p.returncode, out if out else err
def _chat_answer(text: str) -> str:
prompt = f"You are Prime, Jason's assistant. Reply naturally and concisely. User message: {text}"
code, out = run_prime(['dispatch','planning', prompt])
if code == 0 and out:
return out[:3000]
return "Got it — I can help with that."
def help_text() -> str:
return (
"Prime control commands:\n"
@@ -171,8 +181,10 @@ def parse_and_handle(chat_id: int, msg_id: int, text: str) -> str:
parts = shlex.split(raw)
if not parts:
return help_text()
# normalize command tokens for case and punctuation (e.g., "Status?", "Goals status?")
norm = [re.sub(r'[\?\!\.,:;]+$', '', x).lower() for x in parts]
if parts[:2] == ['startup','show']:
if norm[:2] == ['startup','show']:
try:
import json
from pathlib import Path
@@ -187,12 +199,12 @@ def parse_and_handle(chat_id: int, msg_id: int, text: str) -> str:
except Exception as e:
return f"startup show error: {e}"
if parts[:2] == ['allowlist','show']:
if norm[:2] == ['allowlist','show']:
if str(chat_id) != owner_id():
return 'Not authorized.'
return json.dumps({'allowed_user_ids': sorted(load_allowlist())}, indent=2)
if parts[:2] == ['allowlist','add']:
if norm[:2] == ['allowlist','add']:
if str(chat_id) != owner_id():
return 'Not authorized.'
if len(parts) != 3:
@@ -200,7 +212,7 @@ def parse_and_handle(chat_id: int, msg_id: int, text: str) -> str:
s = load_allowlist(); s.add(str(parts[2])); save_allowlist(s)
return 'allowlist updated'
if parts[:2] == ['allowlist','remove']:
if norm[:2] == ['allowlist','remove']:
if str(chat_id) != owner_id():
return 'Not authorized.'
if len(parts) != 3:
@@ -211,15 +223,15 @@ def parse_and_handle(chat_id: int, msg_id: int, text: str) -> str:
s = load_allowlist(); s.discard(uid); save_allowlist(s)
return 'allowlist updated'
if parts[0] == 'status':
if norm[0] == 'status':
code, out = run_prime(['status'])
return f"status (code={code})\n{out[:3500]}"
if parts[:2] == ['queue', 'list']:
if norm[:2] == ['queue', 'list']:
code, out = run_prime(['queue', 'list'])
return f"queue list (code={code})\n{out[:3500]}"
if parts[:2] == ['queue', 'add']:
if norm[:2] == ['queue', 'add']:
if len(parts) < 4:
return 'Usage: queue add <task_type> <task text>'
task_type = parts[2]
@@ -227,13 +239,13 @@ def parse_and_handle(chat_id: int, msg_id: int, text: str) -> str:
code, out = run_prime(['queue', 'add', task_type, task])
return f"queue add (code={code})\n{out[:3500]}"
if parts[:2] == ['queue', 'approve']:
if norm[:2] == ['queue', 'approve']:
if len(parts) != 3:
return 'Usage: queue approve <task_id>'
code, out = run_prime(['queue', 'approve', parts[2]])
return f"queue approve (code={code})\n{out[:3500]}"
if parts[:2] == ['queue', 'deny']:
if norm[:2] == ['queue', 'deny']:
if len(parts) < 3:
return 'Usage: queue deny <task_id> [reason]'
tid = parts[2]
@@ -241,11 +253,11 @@ def parse_and_handle(chat_id: int, msg_id: int, text: str) -> str:
code, out = run_prime(['queue', 'deny', tid, '--reason', reason])
return f"queue deny (code={code})\n{out[:3500]}"
if parts == ['run', 'once']:
if norm == ['run', 'once']:
code, out = run_prime(['queue', 'run-once'])
return f"run once (code={code})\n{out[:3500]}"
if parts[:2] == ['mode', 'autonomous']:
if norm[:2] == ['mode', 'autonomous']:
if len(parts) != 3 or parts[2] not in {'on', 'off'}:
return 'Usage: mode autonomous on|off'
enabled = parts[2] == 'on'
@@ -253,49 +265,52 @@ def parse_and_handle(chat_id: int, msg_id: int, text: str) -> str:
return f"autonomous mode set to {parts[2]}"
if parts[0] == 'blocked':
if norm[0] == 'blocked':
b = list_blocked()
return json.dumps(b, indent=2)[:3500] if b else 'No blocked tasks.'
if parts[0] in {'approve','/approve'}:
if norm[0] in {'approve','/approve'}:
if len(parts) != 2:
return 'Usage: approve <task_id>'
code, out = run_prime(['queue','approve',parts[1]])
return f"approve (code={code})\n{out[:3500]}"
if parts[0] in {'deny','/deny'}:
if norm[0] in {'deny','/deny'}:
if len(parts) < 2:
return 'Usage: deny <task_id> [reason]'
reason = ' '.join(parts[2:]) if len(parts)>2 else 'denied_by_operator'
code, out = run_prime(['queue','deny',parts[1],'--reason',reason])
return f"deny (code={code})\n{out[:3500]}"
if parts[:2] == ['guardrails','show']:
if norm[:2] == ['guardrails','show']:
return json.dumps(get_cfg(), indent=2)
if parts[:3] == ['guardrails','set','max_tasks_per_hour']:
if norm[:3] == ['guardrails','set','max_tasks_per_hour']:
if len(parts) != 4:
return 'Usage: guardrails set max_tasks_per_hour <n>'
cfg = get_cfg(); cfg['max_tasks_per_hour'] = int(parts[3]); set_cfg(cfg)
return 'guardrails updated'
if parts[:2] == ['mode', 'chat']:
if norm[:2] == ['mode', 'chat']:
if len(parts) != 3 or parts[2] not in {'on','off'}:
return 'Usage: mode chat on|off'
m = load_mode(); m['chat_enabled'] = (parts[2]=='on'); save_mode(m)
return f"chat mode set to {parts[2]}"
if parts[:2] == ['mode', 'strict']:
if norm[:2] == ['mode', 'strict']:
if len(parts) != 3 or parts[2] not in {'on','off'}:
return 'Usage: mode strict on|off'
m = load_mode(); m['strict_mode'] = (parts[2]=='on'); save_mode(m)
return f"strict mode set to {parts[2]}"
if parts[:2] == ['goals', 'show']:
if norm[:2] == ['goals', 'status']:
norm[1] = 'show'
if norm[:2] == ['goals', 'show']:
goals = load_goals()
return json.dumps(goals, indent=2)[:3500] if goals else 'No goals set.'
if parts[:2] == ['goals', 'add']:
if norm[:2] == ['goals', 'add']:
if len(parts) < 3:
return 'Usage: goals add <text>'
goals = load_goals()
@@ -336,7 +351,10 @@ def parse_and_handle(chat_id: int, msg_id: int, text: str) -> str:
return f"Queued and moving.\n{out[:3000]}"
if intent in {'chat','ask','unknown'}:
return interp.get('reply') or "I'm here. What would you like to do next?"
r = (interp.get('reply') or '').strip()
if not r or r.lower().startswith('good question'):
return _chat_answer(raw)
return r
return "Unknown command.\n" + help_text()